1 /*
2    Copyright (C) 2003 Commonwealth Scientific and Industrial Research
3    Organisation (CSIRO) Australia
4 
5    Redistribution and use in source and binary forms, with or without
6    modification, are permitted provided that the following conditions
7    are met:
8 
9    - Redistributions of source code must retain the above copyright
10    notice, this list of conditions and the following disclaimer.
11 
12    - Redistributions in binary form must reproduce the above copyright
13    notice, this list of conditions and the following disclaimer in the
14    documentation and/or other materials provided with the distribution.
15 
16    - Neither the name of CSIRO Australia nor the names of its
17    contributors may be used to endorse or promote products derived from
18    this software without specific prior written permission.
19 
20    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21    ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
23    PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
24    CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25    EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26    PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
27    PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28    LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
29    NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
30    SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 */
32 
33 #include "oggz/oggz.h"
34 
35 #include "oggz_tests.h"
36 
37 static long serialno1, serialno2;
38 static ogg_int64_t granulepos = 0;
39 static ogg_int64_t packetno = 0;
40 
41 static int
hungry(OGGZ * oggz,int empty,void * user_data)42 hungry (OGGZ * oggz, int empty, void * user_data)
43 {
44   unsigned char buf[1];
45   ogg_packet op;
46   long serialno = serialno1;
47   long err;
48 
49   buf[0] = 'x';
50 
51   op.packet = buf;
52   op.bytes = 1;
53   op.granulepos = granulepos;
54   op.packetno = packetno;
55 
56   switch (packetno) {
57   case 0:
58     INFO ("Feeding stream prefix");
59     op.b_o_s = 1;
60     op.e_o_s = 0;
61     serialno = serialno1;
62     break;
63   case 1:
64     op.b_o_s = 1;
65     op.e_o_s = 0;
66     serialno = serialno2;
67     break;
68   case 2:
69     op.b_o_s = 0;
70     op.e_o_s = 0;
71     serialno = serialno2;
72     break;
73   case 3:
74     op.b_o_s = 0;
75     op.e_o_s = 0;
76     serialno = serialno1;
77     break;
78   }
79 
80 #ifdef DEBUG
81   fprintf (stderr, "Writing packet %d, serialno %d, bos=%d, eos=%d1\n",
82 	   packetno, (serialno==serialno1)?1:2, op.b_o_s, op.e_o_s);
83 #endif
84 
85   err = oggz_write_feed (oggz, &op, serialno, 0, NULL);
86   if (err != 0) {
87     printf ("Error %ld\n", err);
88     FAIL ("Could not feed OGGZ");
89 #if 0
90     switch (err) {
91     case OGGZ_ERR_BAD_SERIALNO:
92       FAIL ("Incorrect failure writing non-bos packet of new serialno");
93       break;
94     case OGGZ_ERR_EOS:
95       FAIL ("Incorrect failure writing eos packet of stream suffix");
96       break;
97     default:
98       FAIL ("Could not feed OGGZ");
99       break;
100     }
101 #endif
102   }
103 
104   granulepos++;
105   packetno++;
106 
107   if (packetno > 3) {
108     return OGGZ_STOP_OK; /* Cancel write */
109   } else {
110     return OGGZ_CONTINUE;
111   }
112 }
113 
114 int
main(int argc,char * argv[])115 main (int argc, char * argv[])
116 {
117   OGGZ * oggz;
118   unsigned char buf[1];
119   long n;
120 
121   oggz = oggz_new (OGGZ_WRITE|OGGZ_PREFIX);
122   if (oggz == NULL)
123     FAIL("newly created OGGZ == NULL");
124 
125   serialno1 = oggz_serialno_new (oggz);
126   serialno2 = oggz_serialno_new (oggz);
127 
128   if (oggz_write_set_hungry_callback (oggz, hungry, 1, NULL) != 0)
129     FAIL("Could not set hungry callback");
130 
131   while ((n = oggz_write_output (oggz, buf, 1)) > 0);
132 
133   if (oggz_close (oggz) != 0)
134     FAIL("Could not close OGGZ");
135 
136   exit (0);
137 }
138