oggz_write.c 23.4 KB
Newer Older
andre's avatar
andre committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/*
   Copyright (C) 2003 Commonwealth Scientific and Industrial Research
   Organisation (CSIRO) Australia

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   - Neither the name of CSIRO Australia nor the names of its
   contributors may be used to endorse or promote products derived from
   this software without specific prior written permission.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
   PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE ORGANISATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "config.h"

#if OGGZ_CONFIG_WRITE

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
42
43

#ifdef HAVE_UNISTD_H
andre's avatar
andre committed
44
#include <unistd.h>
45
#endif
46

andre's avatar
andre committed
47
48
49
50
51
52
53
54
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <time.h>

#include <ogg/ogg.h>

#include "oggz_private.h"
conrad's avatar
conrad committed
55
#include "oggz_vector.h"
andre's avatar
andre committed
56
57
58

/* #define DEBUG */

59
60
/* Define to 0 or 1 */
#define ALWAYS_FLUSH 0
andre's avatar
andre committed
61

62
63
#define OGGZ_WRITE_EMPTY (-707)

64
65
66
/* #define ZPACKET_CMP */

#ifdef ZPACKET_CMP
andre's avatar
andre committed
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
static int
oggz_zpacket_cmp (oggz_writer_packet_t * a, oggz_writer_packet_t * b,
		  void * user_data)
{
  OGGZ * oggz = (OGGZ *)user_data;
  long serialno_a, serialno_b;
  ogg_int64_t unit_a, unit_b;

  serialno_a = a->stream->ogg_stream.serialno;
  serialno_b = b->stream->ogg_stream.serialno;

  unit_a = oggz_get_unit (oggz, serialno_a, a->op.granulepos);
  unit_b = oggz_get_unit (oggz, serialno_b, b->op.granulepos);

  if (unit_a < unit_b) return -1;
  else return (unit_a > unit_b);
}
84
#endif
andre's avatar
andre committed
85
86
87
88
89
90
91
92

OGGZ *
oggz_write_init (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;

  writer->next_zpacket = NULL;

conrad's avatar
conrad committed
93
  writer->packet_queue = oggz_vector_new ();
andre's avatar
andre committed
94

95
#ifdef ZPACKET_CMP
andre's avatar
andre committed
96
  /* XXX: comparison function should only kick in when a metric is set */
97
  oggz_vector_set_cmp (writer->packet_queue,
andre's avatar
andre committed
98
99
100
101
102
103
104
105
		       (OggzCmpFunc)oggz_zpacket_cmp, oggz);
#endif

  writer->hungry = NULL;
  writer->hungry_user_data = NULL;
  writer->hungry_only_when_empty = 0;

  writer->writing = 0;
106
  writer->no_more_packets = 0;
andre's avatar
andre committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
  writer->state = OGGZ_MAKING_PACKETS;

  writer->flushing = 0;
#if 0
  writer->eog = 1;
  writer->eop = 1; /* init ready to start next packet */
#endif
  writer->eos = 0;

  writer->current_zpacket = NULL;

  writer->packet_offset = 0;
  writer->page_offset = 0;

121
122
  writer->current_stream = NULL;

andre's avatar
andre committed
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
  return oggz;
}

static int
oggz_writer_packet_free (oggz_writer_packet_t * zpacket)
{
  if (!zpacket) return 0;

  if (zpacket->guard) {
    /* managed by user; flag guard */
    *zpacket->guard = 1;
  } else {
    /* managed by oggz; free copied data */
    oggz_free (zpacket->op.packet);
  }
  oggz_free (zpacket);

  return 0;
}

143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
int
oggz_write_flush (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;
  ogg_stream_state * os;
  ogg_page * og;
  int ret = 0;

  os = writer->current_stream;
  og = &oggz->current_page;

  if (os != NULL)
    ret = ogg_stream_flush (os, og);

  return ret;
}

andre's avatar
andre committed
160
161
162
163
164
OGGZ *
oggz_write_close (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;

165
166
  oggz_write_flush (oggz);

167
168
169
  oggz_writer_packet_free (writer->current_zpacket);
  oggz_writer_packet_free (writer->next_zpacket);

conrad's avatar
conrad committed
170
  oggz_vector_foreach (writer->packet_queue,
andre's avatar
andre committed
171
		       (OggzFunc)oggz_writer_packet_free);
conrad's avatar
conrad committed
172
  oggz_vector_delete (writer->packet_queue);
andre's avatar
andre committed
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209

  return oggz;
}

/******** Packet queueing ********/

int
oggz_write_set_hungry_callback (OGGZ * oggz, OggzWriteHungry hungry,
				int only_when_empty, void * user_data)
{
  OggzWriter * writer;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (!(oggz->flags & OGGZ_WRITE)) {
    return OGGZ_ERR_INVALID;
  }

  writer = &oggz->x.writer;

  writer->hungry = hungry;
  writer->hungry_user_data = user_data;
  writer->hungry_only_when_empty = only_when_empty;

  return 0;
}

int
oggz_write_feed (OGGZ * oggz, ogg_packet * op, long serialno, int flush,
		 int * guard)
{
  OggzWriter * writer;
  oggz_stream_t * stream;
  oggz_writer_packet_t * packet;
  ogg_packet * new_op;
  unsigned char * new_buf;
  int b_o_s, e_o_s, bos_auto;
210
  int strict, prefix, suffix;
andre's avatar
andre committed
211

212
213
214
215
#ifdef DEBUG
  printf ("oggz_write_feed: IN\n");
#endif

andre's avatar
andre committed
216
217
218
219
220
221
222
223
224
225
  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  if (!(oggz->flags & OGGZ_WRITE)) {
    return OGGZ_ERR_INVALID;
  }

  writer = &oggz->x.writer;

  if (guard && *guard != 0) return OGGZ_ERR_BAD_GUARD;

226
227
  /* Check that the serialno is in the valid range for an Ogg page header,
   * ie. that it fits within 32 bits and does not equal the special value -1 */
228
  if ((long)((ogg_int32_t)serialno) != serialno || serialno == -1) {
229
230
231
#ifdef DEBUG
    printf ("oggz_write_feed: serialno %010ld\n", serialno);
#endif
232
    return OGGZ_ERR_BAD_SERIALNO;
233
234
  }

andre's avatar
andre committed
235
236
237
238
#ifdef DEBUG
  printf ("oggz_write_feed: (%010ld) FLUSH: %d\n", serialno, flush);
#endif

239
  /* Cache strict, prefix, suffix */
andre's avatar
andre committed
240
  strict = !(oggz->flags & OGGZ_NONSTRICT);
241
242
  prefix = (oggz->flags & OGGZ_PREFIX);
  suffix = (oggz->flags & OGGZ_SUFFIX);
andre's avatar
andre committed
243
244
245
246
247
248
249
250
251
252
253
254
255
256

  /* Set bos, eos to canonical values */
  bos_auto = (op->b_o_s == -1) ? 1 : 0;
  b_o_s = op->b_o_s ? 1 : 0;
  e_o_s = op->e_o_s ? 1 : 0;

  stream = oggz_get_stream (oggz, serialno);
  if (stream == NULL) {
    if (bos_auto) b_o_s = 1;

    if (strict && b_o_s && !oggz_get_bos (oggz, -1)) {
	return OGGZ_ERR_BOS;
    }

257
    if (b_o_s || !strict || suffix) {
andre's avatar
andre committed
258
      stream = oggz_add_stream (oggz, serialno);
259
      oggz_auto_identify_packet (oggz, op, serialno);
andre's avatar
andre committed
260
261
262
263
264
265
    } else {
      return OGGZ_ERR_BAD_SERIALNO;
    }
  } else {
    if (bos_auto) b_o_s = 0;

266
    if (!suffix && strict && stream->e_o_s)
andre's avatar
andre committed
267
268
269
270
271
272
      return OGGZ_ERR_EOS;
  }

  /* Check packet against mapping restrictions */
  if (strict) {
    if (op->bytes < 0) return OGGZ_ERR_BAD_BYTES;
273
    if (!suffix && b_o_s != stream->b_o_s) return OGGZ_ERR_BAD_B_O_S;
274
275
276
    if (op->granulepos != -1 && op->granulepos < stream->granulepos &&
        /* Allow negative granulepos immediately after headers, for Dirac: */
        !(stream->granulepos == 0 && op->granulepos < 0))
andre's avatar
andre committed
277
      return OGGZ_ERR_BAD_GRANULEPOS;
278

andre's avatar
andre committed
279
280
    /* Allow packetno == -1 to indicate oggz should fill it in; otherwise:
     * if op is the first packet in the stream, initialize the stream's
281
282
     * packetno to the given one, otherwise check it for strictness.
     * For stream suffixes, believe the packetno value */
andre's avatar
andre committed
283
    if (op->packetno != -1) {
284
      if (b_o_s || suffix) {
andre's avatar
andre committed
285
286
287
288
289
290
291
292
293
	stream->packetno = op->packetno;
      } else if (op->packetno <= stream->packetno) {
	return OGGZ_ERR_BAD_PACKETNO;
      }
    }
  }

  /* OK -- Update stream's memory of packet details */

294
  if (!stream->metric && (oggz->flags & OGGZ_AUTO)) {
295
    oggz_auto_read_bos_packet (oggz, op, serialno, NULL);
296
297
  }

andre's avatar
andre committed
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
  stream->b_o_s = 0; /* The stream is henceforth no longer at bos */
  stream->e_o_s = e_o_s; /* We believe the eos value */
  stream->granulepos = op->granulepos; /* and the granulepos */

  /* If the user specified a packetno of -1, fill it in automatically;
   * otherwise, use the user-specified value */
  stream->packetno = (op->packetno != -1) ? op->packetno : stream->packetno+1;

  /* Now set up the packet and add it to the queue */
  if (guard == NULL) {
    new_buf = oggz_malloc ((size_t)op->bytes);
    memcpy (new_buf, op->packet, (size_t)op->bytes);
  } else {
    new_buf = op->packet;
  }

  packet = oggz_malloc (sizeof (oggz_writer_packet_t));

  new_op = &packet->op;
  new_op->packet = new_buf;
  new_op->bytes = op->bytes;
319
320
  new_op->b_o_s = b_o_s;
  new_op->e_o_s = e_o_s;
andre's avatar
andre committed
321
322
323
324
325
326
327
328
329
330
331
332
  new_op->granulepos = op->granulepos;
  new_op->packetno = stream->packetno;

  packet->stream = stream;
  packet->flush = flush;
  packet->guard = guard;

#ifdef DEBUG
  printf ("oggz_write_feed: made packet bos %ld eos %ld (%ld bytes) FLUSH: %d\n",
	  new_op->b_o_s, new_op->e_o_s, new_op->bytes, packet->flush);
#endif

conrad's avatar
conrad committed
333
  if (oggz_vector_insert_p (writer->packet_queue, packet) == NULL) {
andre's avatar
andre committed
334
335
336
337
338
    oggz_free (packet);
    if (!guard) oggz_free (new_buf);
    return -1;
  }

339
340
  writer->no_more_packets = 0;

341
342
343
344
345
#ifdef DEBUG
  printf ("oggz_write_feed: enqueued packet, queue size %d\n",
	  oggz_vector_size (writer->packet_queue));
#endif

andre's avatar
andre committed
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
  return 0;
}

/******** Page creation ********/

/*
 *

            ____           __________________\___           ____
           /    \         /    Page ready    /   \         /    \
          |      \ /=========\                /=========\ /      |
  Page    |       || Make    ||              ||  Write  ||       | Page
  !ready  V       || packet  ||              ||  page   ||       V ready
          |      / \=========/                \=========/ \      |
           \____/         \___/__________________/         \____/
                              \   Page !ready
 *
 */


/*
 * oggz_page_init (oggz)
 *
 * Initialises the next page of the current packet.
 *
 * If this returns 0, the page is not ready for writing.
 */
static long
oggz_page_init (OGGZ * oggz)
{
376
  OggzWriter * writer;
andre's avatar
andre committed
377
378
379
380
381
382
  ogg_stream_state * os;
  ogg_page * og;
  int ret;

  if (oggz == NULL) return -1;

383
  writer = &oggz->x.writer;
andre's avatar
andre committed
384
385
386
  os = writer->current_stream;
  og = &oggz->current_page;

387
  if (ALWAYS_FLUSH || writer->flushing) {
andre's avatar
andre committed
388
#ifdef DEBUG
389
    printf ("oggz_page_init: ATTEMPT FLUSH: ");
andre's avatar
andre committed
390
#endif
391
    ret = oggz_write_flush (oggz);
andre's avatar
andre committed
392
393
  } else {
#ifdef DEBUG
394
    printf ("oggz_page_init: ATTEMPT pageout: ");
andre's avatar
andre committed
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
#endif
    ret = ogg_stream_pageout (os, og);
  }

  if (ret) {
    writer->page_offset = 0;
  }

#ifdef DEBUG
  printf ("%s\n", ret ? "OK" : "NO");
#endif

  return ret;
}

/*
 * oggz_packet_init (oggz, buf, n)
 *
 * Initialises the next packet with data from buf, length n
 */
static long
oggz_packet_init (OGGZ * oggz, oggz_writer_packet_t * next_zpacket)
{
418
  OggzWriter * writer;
andre's avatar
andre committed
419
420
421
422
423
424
  oggz_stream_t * stream;
  ogg_stream_state * os;
  ogg_packet * op;

  if (oggz == NULL) return -1L;

425
  writer = &oggz->x.writer;
andre's avatar
andre committed
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
  writer->current_zpacket = next_zpacket;
  op = &next_zpacket->op;

#ifdef DEBUG
  printf ("oggz_packet_init: %ld bytes\n", (long)op->bytes);
#endif

  stream = next_zpacket->stream;

  /* Mark this stream as having delivered a non b_o_s packet if so */
  if (!op->b_o_s) stream->delivered_non_b_o_s = 1;

  os = &stream->ogg_stream;
  ogg_stream_packetin (os, op);

  writer->flushing = (next_zpacket->flush & OGGZ_FLUSH_AFTER);
#ifdef DEBUG
  printf ("oggz_packet_init: set flush to %d\n", writer->flushing);
#endif
  writer->current_stream = os;
  writer->packet_offset = 0;

  return 0;
}

static long
oggz_page_copyout (OGGZ * oggz, unsigned char * buf, long n)
{
454
  OggzWriter * writer;
andre's avatar
andre committed
455
456
457
458
459
  long h, b;
  ogg_page * og;

  if (oggz == NULL) return -1L;

460
  writer = &oggz->x.writer;
andre's avatar
andre committed
461
462
463
464
465
466
467
468
469
470
471
  og = &oggz->current_page;

  h = MIN (n, og->header_len - writer->page_offset);
  if (h > 0) {
    memcpy (buf, og->header + writer->page_offset, h);
    writer->page_offset += h;
    n -= h;
    buf += h;
  } else {
    h = 0;
  }
472

andre's avatar
andre committed
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
  b = MIN (n, og->header_len + og->body_len - writer->page_offset);
  if (b > 0) {
#ifdef DEBUG
    {
      unsigned char * c = &og->body[writer->page_offset - og->header_len];
      printf ("oggz_page_copyout [%d] (@%ld): %c%c%c%c ...\n",
	      ogg_page_serialno (og), (long) ogg_page_granulepos (og),
	      c[0], c[1], c[2], c[3]);
    }
#endif
    memcpy (buf, og->body + (writer->page_offset - og->header_len), b);
    writer->page_offset += b;
    n -= b;
    buf += b;
  } else {
    b = 0;
  }

  return h + b;
}

static long
oggz_page_writeout (OGGZ * oggz, long n)
{
497
  OggzWriter * writer;
andre's avatar
andre committed
498
499
500
  long h, b, nwritten;
  ogg_page * og;

conrad's avatar
conrad committed
501
502
503
504
#ifdef OGGZ_WRITE_DIRECT
  int fd;
#endif

andre's avatar
andre committed
505
506
  if (oggz == NULL) return -1L;

507
  writer = &oggz->x.writer;
andre's avatar
andre committed
508
509
  og = &oggz->current_page;

conrad's avatar
conrad committed
510
511
512
513
#ifdef OGGZ_WRITE_DIRECT
  fd = fileno (oggz->file);
#endif

andre's avatar
andre committed
514
515
  h = MIN (n, og->header_len - writer->page_offset);
  if (h > 0) {
conrad's avatar
conrad committed
516
517
518
#ifdef OGGZ_WRITE_DIRECT
    nwritten = write (fd, og->header + writer->page_offset, h);
#else
519
    nwritten = (long)oggz_io_write (oggz, og->header + writer->page_offset, h);
conrad's avatar
conrad committed
520
#endif
521
522

#ifdef DEBUG
conrad's avatar
conrad committed
523
524
525
    if (nwritten < h) {
      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, h);
    }
526
#endif
andre's avatar
andre committed
527
528
529
530
531
    writer->page_offset += h;
    n -= h;
  } else {
    h = 0;
  }
532

andre's avatar
andre committed
533
534
  b = MIN (n, og->header_len + og->body_len - writer->page_offset);
  if (b > 0) {
535
536
537
538
539
540
541
542
#ifdef DEBUG
    {
      unsigned char * c = &og->body[writer->page_offset - og->header_len];
      printf ("oggz_page_writeout [%d] (@%ld): %c%c%c%c ...\n",
	      ogg_page_serialno (og), (long) ogg_page_granulepos (og),
	      c[0], c[1], c[2], c[3]);
    }
#endif
conrad's avatar
conrad committed
543
544
#ifdef OGGZ_WRITE_DIRECT
    nwritten = write (fd,
andre's avatar
andre committed
545
		      og->body + (writer->page_offset - og->header_len), b);
conrad's avatar
conrad committed
546
#else
547
    nwritten = (long)oggz_io_write (oggz, og->body + (writer->page_offset - og->header_len), b);
conrad's avatar
conrad committed
548
#endif
549
#ifdef DEBUG
conrad's avatar
conrad committed
550
551
552
    if (nwritten < b) {
      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, b);
    }
553
#endif
andre's avatar
andre committed
554
555
556
557
558
559
560
561
562
    writer->page_offset += b;
    n -= b;
  } else {
    b = 0;
  }

  return h + b;
}

563
564
static int
oggz_dequeue_packet (OGGZ * oggz, oggz_writer_packet_t ** next_zpacket)
andre's avatar
andre committed
565
566
{
  OggzWriter * writer = &oggz->x.writer;
567
  int ret = 0;
andre's avatar
andre committed
568
569

  if (writer->next_zpacket != NULL) {
570
571
572
#ifdef DEBUG
    printf ("oggz_dequeue_packet: queue EMPTY\n");
#endif
573
    *next_zpacket = writer->next_zpacket;
andre's avatar
andre committed
574
575
    writer->next_zpacket = NULL;
  } else {
576
    *next_zpacket = oggz_vector_pop (writer->packet_queue);
andre's avatar
andre committed
577

578
    if (*next_zpacket == NULL) {
andre's avatar
andre committed
579
      if (writer->hungry) {
580
581
        ret = writer->hungry (oggz, 1, writer->hungry_user_data);
        *next_zpacket = oggz_vector_pop (writer->packet_queue);
582
583
584
585
586
587
588
589
590
591
#ifdef DEBUG
        printf ("oggz_dequeue_packet: called hungry and popped, new queue size %d\n",
  	        oggz_vector_size (writer->packet_queue));
#endif

#ifdef DEBUG
      } else {
        printf ("oggz_dequeue_packet: no packet, no hungry, queue size %d\n",
                oggz_vector_size (writer->packet_queue));
#endif
andre's avatar
andre committed
592
      }
593
594
595
596
597
#ifdef DEBUG
    } else {
    printf ("oggz_dequeue_packet: dequeued packet, queue size %d\n",
            oggz_vector_size (writer->packet_queue));
#endif
andre's avatar
andre committed
598
    }
599

andre's avatar
andre committed
600
601
  }

602
603
604
#ifdef DEBUG
  printf("oggz_dequeue_packeT: returning %d\n", ret);
#endif
605
  return ret;
andre's avatar
andre committed
606
607
608
609
610
611
}

static long
oggz_writer_make_packet (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;
612
  oggz_writer_packet_t * zpacket, * next_zpacket = NULL;
613
  int cb_ret = 0;
andre's avatar
andre committed
614
615

#ifdef DEBUG
616
  printf ("oggz_writer_make_packet: IN\n");
andre's avatar
andre committed
617
618
619
620
621
622
623
624
625
626
627
#endif

  /* finished with current packet; unguard */
  zpacket = writer->current_zpacket;
  oggz_writer_packet_free (zpacket);
  writer->current_zpacket = NULL;

  /* if the user wants the hungry callback after every packet, give
   * it to them, marking emptiness appropriately
   */
  if (writer->hungry && !writer->hungry_only_when_empty) {
conrad's avatar
conrad committed
628
    int empty = (oggz_vector_size (writer->packet_queue) == 0);
andre's avatar
andre committed
629
630
631
632
633
    cb_ret = writer->hungry (oggz, empty, writer->hungry_user_data);
  }

  if (cb_ret == 0) {
    /* dequeue and init the next packet */
634
635
    cb_ret = oggz_dequeue_packet (oggz, &next_zpacket);
    if (next_zpacket == NULL) {
636
637
638
#ifdef DEBUG
      printf ("oggz_writer_make_packet: packet queue empty\n");
#endif
andre's avatar
andre committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
      /*writer->eos = 1;*/
    } else {
      if ((writer->current_stream != NULL) &&
	  (next_zpacket->flush & OGGZ_FLUSH_BEFORE)) {
	writer->flushing = 1;
#ifdef DEBUG
	printf ("oggz_writer_make_packet: set flush to %d\n",
		writer->flushing);
#endif
	next_zpacket->flush &= OGGZ_FLUSH_AFTER;
	writer->next_zpacket = next_zpacket;
      } else {
	oggz_packet_init (oggz, next_zpacket);
      }
    }
  }

656
657
658
659
#ifdef DEBUG
  printf("oggz_writer_make_packet: cb_ret is %d\n", cb_ret);
#endif
  
660
661
  if (cb_ret == 0 && next_zpacket == NULL) return OGGZ_WRITE_EMPTY;

662
  return cb_ret;
andre's avatar
andre committed
663
664
665
666
667
}

long
oggz_write_output (OGGZ * oggz, unsigned char * buf, long n)
{
668
  OggzWriter * writer;
andre's avatar
andre committed
669
  long bytes, bytes_written = 1, remaining = n, nwritten = 0;
670
  int active = 1, cb_ret = 0;
andre's avatar
andre committed
671
672

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;
673
674
 
  writer = &oggz->x.writer;
andre's avatar
andre committed
675
676
677
678
679
680
681
682

  if (!(oggz->flags & OGGZ_WRITE)) {
    return OGGZ_ERR_INVALID;
  }

  if (writer->writing) return OGGZ_ERR_RECURSIVE_WRITE;
  writer->writing = 1;

683
684
685
686
#ifdef DEBUG
  printf ("oggz_write_output: IN\n");
#endif

687
688
  if ((cb_ret = oggz->cb_next) != OGGZ_CONTINUE) {
    oggz->cb_next = 0;
689
    writer->writing = 0;
690
    writer->no_more_packets = 0;
691
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
692
693
694
    return oggz_map_return_value_to_error (cb_ret);
  }

andre's avatar
andre committed
695
696
697
  while (active && remaining > 0) {
    bytes = MIN (remaining, 1024);

698
699
700
701
702
#ifdef DEBUG
    printf ("oggz_write_output: write loop (%ld , %ld remain) ...\n", bytes,
	    remaining);
#endif

andre's avatar
andre committed
703
    while (writer->state == OGGZ_MAKING_PACKETS) {
704
705
706
#ifdef DEBUG
      	printf ("oggz_write_output: MAKING_PACKETS\n");
#endif
707
      if ((cb_ret = oggz_writer_make_packet (oggz)) != OGGZ_CONTINUE) {
708
709
710
#ifdef DEBUG
        printf ("oggz_write_output: no packets (cb_ret is %d)\n", cb_ret);
#endif
711
712
713
714
        if (cb_ret == OGGZ_WRITE_EMPTY) {
          writer->flushing = 1;
          writer->no_more_packets = 1;
        }
715
716
717
718
719
        /* At this point, in contrast to oggz_write(), we break out of this
         * loop unconditionally.
         */
        active = 0;
        break;
andre's avatar
andre committed
720
721
      }
      if (oggz_page_init (oggz)) {
722
        writer->state = OGGZ_WRITING_PAGES;
723
724
725
726
727
728
729
730
      } else {
#ifdef DEBUG
        printf ("oggz_write_output: unable to make page...\n");
#endif
        if (writer->no_more_packets) {
          active = 0;
          break;
        }
andre's avatar
andre committed
731
732
733
734
735
736
737
      }
    }

    if (writer->state == OGGZ_WRITING_PAGES) {
      bytes_written = oggz_page_copyout (oggz, buf, bytes);

      if (bytes_written == -1) {
738
        active = 0;
739
        cb_ret = OGGZ_ERR_SYSTEM; /* XXX: catch next */
andre's avatar
andre committed
740
      } else if (bytes_written == 0) {
741
742
743
744
        if (writer->no_more_packets) {
          active = 0;
          break;
        } else if (!oggz_page_init (oggz)) {
745
746
747
#ifdef DEBUG
          printf ("oggz_write_output: bytes_written == 0, DONE\n");
#endif
748
749
          writer->state = OGGZ_MAKING_PACKETS;
        }
andre's avatar
andre committed
750
751
752
753
754
755
756
757
758
      }

      buf += bytes_written;

      remaining -= bytes_written;
      nwritten += bytes_written;
    }
  }

759
760
761
762
#ifdef DEBUG
  printf ("oggz_write_output: OUT %ld\n", nwritten);
#endif

andre's avatar
andre committed
763
764
  writer->writing = 0;

765
  if (nwritten == 0) {
766
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
767
768
769
770
771
    return oggz_map_return_value_to_error (cb_ret);
  } else {
    oggz->cb_next = cb_ret;
  }

andre's avatar
andre committed
772
773
774
775
776
777
  return nwritten;
}

long
oggz_write (OGGZ * oggz, long n)
{
778
  OggzWriter * writer;
andre's avatar
andre committed
779
  long bytes, bytes_written = 1, remaining = n, nwritten = 0;
780
  int active = 1, cb_ret = 0;
andre's avatar
andre committed
781
782
783

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

784
785
  writer = &oggz->x.writer;

andre's avatar
andre committed
786
787
788
789
790
791
792
793
794
795
796
  if (!(oggz->flags & OGGZ_WRITE)) {
    return OGGZ_ERR_INVALID;
  }

  if (writer->writing) return OGGZ_ERR_RECURSIVE_WRITE;
  writer->writing = 1;

#ifdef DEBUG
  printf ("oggz_write: IN\n");
#endif

797
798
  if ((cb_ret = oggz->cb_next) != OGGZ_CONTINUE) {
    oggz->cb_next = 0;
799
    writer->writing = 0;
800
    writer->no_more_packets = 0;
801
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
802
803
804
    return oggz_map_return_value_to_error (cb_ret);
  }

andre's avatar
andre committed
805
806
807
808
809
810
811
812
813
  while (active && remaining > 0) {
    bytes = MIN (remaining, 1024);

#ifdef DEBUG
    printf ("oggz_write: write loop (%ld , %ld remain) ...\n", bytes,
	    remaining);
#endif

    while (writer->state == OGGZ_MAKING_PACKETS) {
814
815
816
#ifdef DEBUG
      printf ("oggz_write: MAKING PACKETS\n");
#endif
817
      if ((cb_ret = oggz_writer_make_packet (oggz)) != OGGZ_CONTINUE) {
andre's avatar
andre committed
818
#ifdef DEBUG
819
        printf ("oggz_write: no packets (cb_ret is %d)\n", cb_ret);
andre's avatar
andre committed
820
#endif
821
822
823
824
825
826
827
828
829
        /*
         * if we're out of packets because we're at the end of the file,
         * we can't finish just yet.  Instead we need to force a page flush,
         * and write the page out.  So we set flushing and no_more_packets to
         * 1.  This causes oggz_page_init to flush the page, then we 
         * will switch the state to OGGZ_WRITING_PAGES, which will trigger
         * the writing code below.
         */
        if (cb_ret == OGGZ_WRITE_EMPTY) {
830
831
832
#ifdef DEBUG
          printf ("oggz_write: Inferred end of data, forcing a page flush.\n");
#endif
833
834
835
          writer->flushing = 1;
          writer->no_more_packets = 1;
          cb_ret = OGGZ_CONTINUE;
836
        } else {
837
838
839
#ifdef DEBUG
          printf ("oggz_write: Stopped by user callback.\n");
#endif
840
841
          active = 0;
          break;
842
        }
andre's avatar
andre committed
843
844
      }
      if (oggz_page_init (oggz)) {
845
        writer->state = OGGZ_WRITING_PAGES;
846
847
848
849
850
851
852
853
      } else {
#ifdef DEBUG
        printf ("oggz_write: unable to make page...\n");
#endif
        if (writer->no_more_packets) {
          active = 0;
          break;
        }
andre's avatar
andre committed
854
855
856
857
858
      }
    }

    if (writer->state == OGGZ_WRITING_PAGES) {
      bytes_written = oggz_page_writeout (oggz, bytes);
859
860
861
#ifdef DEBUG
      printf ("oggz_write: MAKING PAGES; wrote %ld bytes\n", bytes_written);
#endif
andre's avatar
andre committed
862
863

      if (bytes_written == -1) {
864
865
        active = 0;
        return OGGZ_ERR_SYSTEM; /* XXX: catch next */
andre's avatar
andre committed
866
      } else if (bytes_written == 0) {
867
868
869
870
871
872
873
874
875
876
        /*
         * OK so we've completely written the current page.  If no_more_packets
         * is set then that means there's no more pages after this one, so
         * we set active to 0, break out of the loop, pack up our things and
         * go home.
         */
        if (writer->no_more_packets) {
          active = 0;
          break;
        } else if (!oggz_page_init (oggz)) {
andre's avatar
andre committed
877
#ifdef DEBUG
878
          printf ("oggz_write: bytes_written == 0, DONE\n");
andre's avatar
andre committed
879
#endif
880
881
          writer->state = OGGZ_MAKING_PACKETS;
        }
andre's avatar
andre committed
882
883
884
      }

      remaining -= bytes_written;
885
      nwritten += bytes_written;
andre's avatar
andre committed
886
887
888
889
890
891
892
893
894
    }
  }

#ifdef DEBUG
  printf ("oggz_write: OUT %ld\n", nwritten);
#endif

  writer->writing = 0;

895
  if (nwritten == 0) {
896
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
897
898
899
900
901
    return oggz_map_return_value_to_error (cb_ret);
  } else {
    oggz->cb_next = cb_ret;
  }

andre's avatar
andre committed
902
903
904
905
906
907
  return nwritten;
}

long
oggz_write_get_next_page_size (OGGZ * oggz)
{
908
  OggzWriter * writer;
andre's avatar
andre committed
909
910
911
912
  ogg_page * og;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

913
914
  writer = &oggz->x.writer;

andre's avatar
andre committed
915
916
917
918
919
920
921
922
923
924
925
926
927
928
  if (!(oggz->flags & OGGZ_WRITE)) {
    return OGGZ_ERR_INVALID;
  }

  og = &oggz->current_page;

  return (og->header_len + og->body_len - (long)writer->page_offset);
}

#else /* OGGZ_CONFIG_WRITE */

#include <ogg/ogg.h>
#include "oggz_private.h"

929
930
931
932
933
934
OGGZ *
oggz_write_init (OGGZ * oggz)
{
  return NULL;
}

935
936
937
938
939
940
int
oggz_write_flush (OGGZ * oggz)
{
  return OGGZ_ERR_DISABLED;
}

941
942
943
944
945
946
OGGZ *
oggz_write_close (OGGZ * oggz)
{
  return NULL;
}

andre's avatar
andre committed
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
int
oggz_write_set_hungry_callback (OGGZ * oggz, OggzWriteHungry hungry,
				int only_when_empty, void * user_data)
{
  return OGGZ_ERR_DISABLED;
}

int
oggz_write_feed (OGGZ * oggz, ogg_packet * op, long serialno, int flush,
		 int * guard)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_write_output (OGGZ * oggz, unsigned char * buf, long n)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_write (OGGZ * oggz, long n)
{
  return OGGZ_ERR_DISABLED;
}

long
oggz_write_get_next_page_size (OGGZ * oggz)
{
  return OGGZ_ERR_DISABLED;
}

#endif