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 suffix");
59     op.b_o_s = 0;
60     op.e_o_s = 0;
61     serialno = serialno1;
62     break;
63   case 1:
64     op.b_o_s = 0;
65     op.e_o_s = 1;
66     serialno = serialno1;
67     break;
68   case 2:
69     op.b_o_s = 0;
70     op.e_o_s = 1;
71     serialno = serialno2;
72     break;
73   }
74 
75 #ifdef DEBUG
76   fprintf (stderr, "Writing packet %d, serialno %d, bos=%d, eos=%d1\n",
77 	   packetno, (serialno==serialno1)?1:2, op.b_o_s, op.e_o_s);
78 #endif
79 
80   err = oggz_write_feed (oggz, &op, serialno, 0, NULL);
81   if (err != 0) {
82     switch (err) {
83     case OGGZ_ERR_BAD_SERIALNO:
84       FAIL ("Incorrect failure writing non-bos packet of new serialno");
85       break;
86     case OGGZ_ERR_EOS:
87       FAIL ("Incorrect failure writing eos packet of stream suffix");
88       break;
89     default:
90       FAIL ("Could not feed OGGZ");
91       break;
92     }
93   }
94 
95   granulepos++;
96   packetno++;
97 
98   if (packetno > 2) {
99     return OGGZ_STOP_OK; /* Cancel write */
100   } else {
101     return OGGZ_CONTINUE;
102   }
103 }
104 
105 int
main(int argc,char * argv[])106 main (int argc, char * argv[])
107 {
108   OGGZ * oggz;
109   unsigned char buf[1];
110   long n;
111 
112   oggz = oggz_new (OGGZ_WRITE|OGGZ_SUFFIX);
113   if (oggz == NULL)
114     FAIL("newly created OGGZ == NULL");
115 
116   serialno1 = oggz_serialno_new (oggz);
117   serialno2 = oggz_serialno_new (oggz);
118 
119   if (oggz_write_set_hungry_callback (oggz, hungry, 1, NULL) != 0)
120     FAIL("Could not set hungry callback");
121 
122   while ((n = oggz_write_output (oggz, buf, 1)) > 0);
123 
124   if (oggz_close (oggz) != 0)
125     FAIL("Could not close OGGZ");
126 
127   exit (0);
128 }
129