oggz_write.c 23.2 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;
andre's avatar
andre committed
274
275
    if (op->granulepos != -1 && op->granulepos < stream->granulepos)
      return OGGZ_ERR_BAD_GRANULEPOS;
276

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

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

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

andre's avatar
andre committed
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
  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;
317
318
  new_op->b_o_s = b_o_s;
  new_op->e_o_s = e_o_s;
andre's avatar
andre committed
319
320
321
322
323
324
325
326
327
328
329
330
  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
331
  if (oggz_vector_insert_p (writer->packet_queue, packet) == NULL) {
andre's avatar
andre committed
332
333
334
335
336
    oggz_free (packet);
    if (!guard) oggz_free (new_buf);
    return -1;
  }

337
338
  writer->no_more_packets = 0;

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

andre's avatar
andre committed
344
345
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
376
377
378
379
380
381
382
383
  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)
{
  OggzWriter * writer = &oggz->x.writer;
  ogg_stream_state * os;
  ogg_page * og;
  int ret;

  if (oggz == NULL) return -1;

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

384
  if (ALWAYS_FLUSH || writer->flushing) {
andre's avatar
andre committed
385
#ifdef DEBUG
386
    printf ("oggz_page_init: ATTEMPT FLUSH: ");
andre's avatar
andre committed
387
#endif
388
    ret = oggz_write_flush (oggz);
andre's avatar
andre committed
389
390
  } else {
#ifdef DEBUG
391
    printf ("oggz_page_init: ATTEMPT pageout: ");
andre's avatar
andre committed
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
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
454
455
456
457
458
459
460
461
462
463
464
465
466
#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)
{
  OggzWriter * writer = &oggz->x.writer;
  oggz_stream_t * stream;
  ogg_stream_state * os;
  ogg_packet * op;

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

  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)
{
  OggzWriter * writer = &oggz->x.writer;
  long h, b;
  ogg_page * og;

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

  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;
  }
467

andre's avatar
andre committed
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
  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)
{
  OggzWriter * writer = &oggz->x.writer;
  long h, b, nwritten;
  ogg_page * og;

conrad's avatar
conrad committed
496
497
498
499
#ifdef OGGZ_WRITE_DIRECT
  int fd;
#endif

andre's avatar
andre committed
500
501
502
503
  if (oggz == NULL) return -1L;

  og = &oggz->current_page;

conrad's avatar
conrad committed
504
505
506
507
#ifdef OGGZ_WRITE_DIRECT
  fd = fileno (oggz->file);
#endif

andre's avatar
andre committed
508
509
  h = MIN (n, og->header_len - writer->page_offset);
  if (h > 0) {
conrad's avatar
conrad committed
510
511
512
#ifdef OGGZ_WRITE_DIRECT
    nwritten = write (fd, og->header + writer->page_offset, h);
#else
513
    nwritten = (long)oggz_io_write (oggz, og->header + writer->page_offset, h);
conrad's avatar
conrad committed
514
#endif
515
516

#ifdef DEBUG
conrad's avatar
conrad committed
517
518
519
    if (nwritten < h) {
      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, h);
    }
520
#endif
andre's avatar
andre committed
521
522
523
524
525
    writer->page_offset += h;
    n -= h;
  } else {
    h = 0;
  }
526

andre's avatar
andre committed
527
528
  b = MIN (n, og->header_len + og->body_len - writer->page_offset);
  if (b > 0) {
529
530
531
532
533
534
535
536
#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
537
538
#ifdef OGGZ_WRITE_DIRECT
    nwritten = write (fd,
andre's avatar
andre committed
539
		      og->body + (writer->page_offset - og->header_len), b);
conrad's avatar
conrad committed
540
#else
541
    nwritten = (long)oggz_io_write (oggz, og->body + (writer->page_offset - og->header_len), b);
conrad's avatar
conrad committed
542
#endif
543
#ifdef DEBUG
conrad's avatar
conrad committed
544
545
546
    if (nwritten < b) {
      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, b);
    }
547
#endif
andre's avatar
andre committed
548
549
550
551
552
553
554
555
556
    writer->page_offset += b;
    n -= b;
  } else {
    b = 0;
  }

  return h + b;
}

557
558
static int
oggz_dequeue_packet (OGGZ * oggz, oggz_writer_packet_t ** next_zpacket)
andre's avatar
andre committed
559
560
{
  OggzWriter * writer = &oggz->x.writer;
561
  int ret = 0;
andre's avatar
andre committed
562
563

  if (writer->next_zpacket != NULL) {
564
565
566
#ifdef DEBUG
    printf ("oggz_dequeue_packet: queue EMPTY\n");
#endif
567
    *next_zpacket = writer->next_zpacket;
andre's avatar
andre committed
568
569
    writer->next_zpacket = NULL;
  } else {
570
    *next_zpacket = oggz_vector_pop (writer->packet_queue);
andre's avatar
andre committed
571

572
    if (*next_zpacket == NULL) {
andre's avatar
andre committed
573
      if (writer->hungry) {
574
575
        ret = writer->hungry (oggz, 1, writer->hungry_user_data);
        *next_zpacket = oggz_vector_pop (writer->packet_queue);
576
577
578
579
580
581
582
583
584
585
#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
586
      }
587
588
589
590
591
#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
592
    }
593

andre's avatar
andre committed
594
595
  }

596
597
598
#ifdef DEBUG
  printf("oggz_dequeue_packeT: returning %d\n", ret);
#endif
599
  return ret;
andre's avatar
andre committed
600
601
602
603
604
605
}

static long
oggz_writer_make_packet (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;
606
  oggz_writer_packet_t * zpacket, * next_zpacket = NULL;
607
  int cb_ret = 0;
andre's avatar
andre committed
608
609

#ifdef DEBUG
610
  printf ("oggz_writer_make_packet: IN\n");
andre's avatar
andre committed
611
612
613
614
615
616
617
618
619
620
621
#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
622
    int empty = (oggz_vector_size (writer->packet_queue) == 0);
andre's avatar
andre committed
623
624
625
626
627
    cb_ret = writer->hungry (oggz, empty, writer->hungry_user_data);
  }

  if (cb_ret == 0) {
    /* dequeue and init the next packet */
628
629
    cb_ret = oggz_dequeue_packet (oggz, &next_zpacket);
    if (next_zpacket == NULL) {
630
631
632
#ifdef DEBUG
      printf ("oggz_writer_make_packet: packet queue empty\n");
#endif
andre's avatar
andre committed
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
      /*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);
      }
    }
  }

650
651
652
653
#ifdef DEBUG
  printf("oggz_writer_make_packet: cb_ret is %d\n", cb_ret);
#endif
  
654
655
  if (cb_ret == 0 && next_zpacket == NULL) return OGGZ_WRITE_EMPTY;

656
  return cb_ret;
andre's avatar
andre committed
657
658
659
660
661
}

long
oggz_write_output (OGGZ * oggz, unsigned char * buf, long n)
{
662
  OggzWriter * writer;
andre's avatar
andre committed
663
  long bytes, bytes_written = 1, remaining = n, nwritten = 0;
664
  int active = 1, cb_ret = 0;
andre's avatar
andre committed
665
666

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;
667
668
 
  writer = &oggz->x.writer;
andre's avatar
andre committed
669
670
671
672
673
674
675
676

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

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

677
678
679
680
#ifdef DEBUG
  printf ("oggz_write_output: IN\n");
#endif

681
682
  if ((cb_ret = oggz->cb_next) != OGGZ_CONTINUE) {
    oggz->cb_next = 0;
683
    writer->writing = 0;
684
    writer->no_more_packets = 0;
685
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
686
687
688
    return oggz_map_return_value_to_error (cb_ret);
  }

andre's avatar
andre committed
689
690
691
  while (active && remaining > 0) {
    bytes = MIN (remaining, 1024);

692
693
694
695
696
#ifdef DEBUG
    printf ("oggz_write_output: write loop (%ld , %ld remain) ...\n", bytes,
	    remaining);
#endif

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

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

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

      buf += bytes_written;

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

753
754
755
756
#ifdef DEBUG
  printf ("oggz_write_output: OUT %ld\n", nwritten);
#endif

andre's avatar
andre committed
757
758
  writer->writing = 0;

759
  if (nwritten == 0) {
760
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
761
762
763
764
765
    return oggz_map_return_value_to_error (cb_ret);
  } else {
    oggz->cb_next = cb_ret;
  }

andre's avatar
andre committed
766
767
768
769
770
771
  return nwritten;
}

long
oggz_write (OGGZ * oggz, long n)
{
772
  OggzWriter * writer;
andre's avatar
andre committed
773
  long bytes, bytes_written = 1, remaining = n, nwritten = 0;
774
  int active = 1, cb_ret = 0;
andre's avatar
andre committed
775
776
777

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

778
779
  writer = &oggz->x.writer;

andre's avatar
andre committed
780
781
782
783
784
785
786
787
788
789
790
  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

791
792
  if ((cb_ret = oggz->cb_next) != OGGZ_CONTINUE) {
    oggz->cb_next = 0;
793
    writer->writing = 0;
794
    writer->no_more_packets = 0;
795
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
796
797
798
    return oggz_map_return_value_to_error (cb_ret);
  }

andre's avatar
andre committed
799
800
801
802
803
804
805
806
807
  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) {
808
809
810
#ifdef DEBUG
      printf ("oggz_write: MAKING PACKETS\n");
#endif
811
      if ((cb_ret = oggz_writer_make_packet (oggz)) != OGGZ_CONTINUE) {
andre's avatar
andre committed
812
#ifdef DEBUG
813
        printf ("oggz_write: no packets (cb_ret is %d)\n", cb_ret);
andre's avatar
andre committed
814
#endif
815
816
817
818
819
820
821
822
823
        /*
         * 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) {
824
825
826
#ifdef DEBUG
          printf ("oggz_write: Inferred end of data, forcing a page flush.\n");
#endif
827
828
829
          writer->flushing = 1;
          writer->no_more_packets = 1;
          cb_ret = OGGZ_CONTINUE;
830
        } else {
831
832
833
#ifdef DEBUG
          printf ("oggz_write: Stopped by user callback.\n");
#endif
834
835
          active = 0;
          break;
836
        }
andre's avatar
andre committed
837
838
      }
      if (oggz_page_init (oggz)) {
839
        writer->state = OGGZ_WRITING_PAGES;
840
841
842
843
844
845
846
847
      } 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
848
849
850
851
852
      }
    }

    if (writer->state == OGGZ_WRITING_PAGES) {
      bytes_written = oggz_page_writeout (oggz, bytes);
853
854
855
#ifdef DEBUG
      printf ("oggz_write: MAKING PAGES; wrote %ld bytes\n", bytes_written);
#endif
andre's avatar
andre committed
856
857

      if (bytes_written == -1) {
858
859
        active = 0;
        return OGGZ_ERR_SYSTEM; /* XXX: catch next */
andre's avatar
andre committed
860
      } else if (bytes_written == 0) {
861
862
863
864
865
866
867
868
869
870
        /*
         * 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
871
#ifdef DEBUG
872
          printf ("oggz_write: bytes_written == 0, DONE\n");
andre's avatar
andre committed
873
#endif
874
875
          writer->state = OGGZ_MAKING_PACKETS;
        }
andre's avatar
andre committed
876
877
878
      }

      remaining -= bytes_written;
879
      nwritten += bytes_written;
andre's avatar
andre committed
880
881
882
883
884
885
886
887
888
    }
  }

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

  writer->writing = 0;

889
  if (nwritten == 0) {
890
    if (cb_ret == OGGZ_WRITE_EMPTY) cb_ret = 0;
891
892
893
894
895
    return oggz_map_return_value_to_error (cb_ret);
  } else {
    oggz->cb_next = cb_ret;
  }

andre's avatar
andre committed
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
  return nwritten;
}

long
oggz_write_get_next_page_size (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;
  ogg_page * og;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  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"

921
922
923
924
925
926
OGGZ *
oggz_write_init (OGGZ * oggz)
{
  return NULL;
}

927
928
929
930
931
932
int
oggz_write_flush (OGGZ * oggz)
{
  return OGGZ_ERR_DISABLED;
}

933
934
935
936
937
938
OGGZ *
oggz_write_close (OGGZ * oggz)
{
  return NULL;
}

andre's avatar
andre committed
939
940
941
942
943
944
945
946
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
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