oggz_write.c 17.1 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 */

conrad's avatar
conrad committed
59
/* #define ALWAYS_FLUSH */
andre's avatar
andre committed
60

61 62 63
/* #define ZPACKET_CMP */

#ifdef ZPACKET_CMP
andre's avatar
andre committed
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
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);
}
81
#endif
andre's avatar
andre committed
82 83 84 85 86 87 88 89

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

  writer->next_zpacket = NULL;

conrad's avatar
conrad committed
90
  writer->packet_queue = oggz_vector_new ();
andre's avatar
andre committed
91

92
#ifdef ZPACKET_CMP
andre's avatar
andre committed
93
  /* XXX: comparison function should only kick in when a metric is set */
94
  oggz_vector_set_cmp (writer->packet_queue,
andre's avatar
andre committed
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
		       (OggzCmpFunc)oggz_zpacket_cmp, oggz);
#endif

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

  writer->writing = 0;
  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;

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

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

conrad's avatar
conrad committed
142
  oggz_vector_foreach (writer->packet_queue,
andre's avatar
andre committed
143
		       (OggzFunc)oggz_writer_packet_free);
conrad's avatar
conrad committed
144
  oggz_vector_delete (writer->packet_queue);
andre's avatar
andre committed
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 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 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232

  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;
  int strict;

  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;

#ifdef DEBUG
  printf ("oggz_write_feed: (%010ld) FLUSH: %d\n", serialno, flush);
#endif

  /* Cache strict */
  strict = !(oggz->flags & OGGZ_NONSTRICT);

  /* 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;
    }

    if (b_o_s || !strict) {
      stream = oggz_add_stream (oggz, serialno);
    } else {
      return OGGZ_ERR_BAD_SERIALNO;
    }
  } else {
    if (bos_auto) b_o_s = 0;

    if (strict && stream->e_o_s)
      return OGGZ_ERR_EOS;
  }

  /* Check packet against mapping restrictions */
  if (strict) {
    if (op->bytes < 0) return OGGZ_ERR_BAD_BYTES;
    if (b_o_s != stream->b_o_s) return OGGZ_ERR_BAD_B_O_S;

    if (op->granulepos != -1 && op->granulepos < stream->granulepos)
      return OGGZ_ERR_BAD_GRANULEPOS;
233

andre's avatar
andre committed
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    /* Allow packetno == -1 to indicate oggz should fill it in; otherwise:
     * if op is the first packet in the stream, initialize the stream's
     * packetno to the given one, otherwise check it for strictness */
    if (op->packetno != -1) {
      if (b_o_s) {
	stream->packetno = op->packetno;
      } else if (op->packetno <= stream->packetno) {
	return OGGZ_ERR_BAD_PACKETNO;
      }
    }
  }

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

  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;
  new_op->b_o_s = op->b_o_s;
  new_op->e_o_s = op->e_o_s;
  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
283
  if (oggz_vector_insert_p (writer->packet_queue, packet) == NULL) {
andre's avatar
andre committed
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 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 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
    oggz_free (packet);
    if (!guard) oggz_free (new_buf);
    return -1;
  }

  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;

#ifdef ALWAYS_FLUSH
  ret = ogg_stream_flush (os, og);
#else
  if (writer->flushing) {
#ifdef DEBUG
    printf ("*** ATTEMPT FLUSH: ");
#endif
    ret = ogg_stream_flush (os, og);
  } else {
#ifdef DEBUG
    printf ("*** ATTEMPT pageout: ");
#endif
    ret = ogg_stream_pageout (os, og);
  }
#endif

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

andre's avatar
andre committed
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
  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
445 446 447 448
#ifdef OGGZ_WRITE_DIRECT
  int fd;
#endif

andre's avatar
andre committed
449 450 451 452
  if (oggz == NULL) return -1L;

  og = &oggz->current_page;

conrad's avatar
conrad committed
453 454 455 456
#ifdef OGGZ_WRITE_DIRECT
  fd = fileno (oggz->file);
#endif

andre's avatar
andre committed
457 458
  h = MIN (n, og->header_len - writer->page_offset);
  if (h > 0) {
conrad's avatar
conrad committed
459 460 461
#ifdef OGGZ_WRITE_DIRECT
    nwritten = write (fd, og->header + writer->page_offset, h);
#else
462
    nwritten = (long)oggz_io_write (oggz, og->header + writer->page_offset, h);
conrad's avatar
conrad committed
463
#endif
464 465

#ifdef DEBUG
conrad's avatar
conrad committed
466 467 468
    if (nwritten < h) {
      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, h);
    }
469
#endif
andre's avatar
andre committed
470 471 472 473 474
    writer->page_offset += h;
    n -= h;
  } else {
    h = 0;
  }
475

andre's avatar
andre committed
476 477
  b = MIN (n, og->header_len + og->body_len - writer->page_offset);
  if (b > 0) {
conrad's avatar
conrad committed
478 479
#ifdef OGGZ_WRITE_DIRECT
    nwritten = write (fd,
andre's avatar
andre committed
480
		      og->body + (writer->page_offset - og->header_len), b);
conrad's avatar
conrad committed
481
#else
482
    nwritten = (long)oggz_io_write (oggz, og->body + (writer->page_offset - og->header_len), b);
conrad's avatar
conrad committed
483
#endif
484
#ifdef DEBUG
conrad's avatar
conrad committed
485 486 487
    if (nwritten < b) {
      printf ("oggz_page_writeout: %ld < %ld\n", nwritten, b);
    }
488
#endif
andre's avatar
andre committed
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507
    writer->page_offset += b;
    n -= b;
  } else {
    b = 0;
  }

  return h + b;
}

static oggz_writer_packet_t *
oggz_dequeue_packet (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;
  oggz_writer_packet_t * next_zpacket;

  if (writer->next_zpacket != NULL) {
    next_zpacket = writer->next_zpacket;
    writer->next_zpacket = NULL;
  } else {
conrad's avatar
conrad committed
508
    next_zpacket = oggz_vector_pop (writer->packet_queue);
andre's avatar
andre committed
509 510 511 512

    if (next_zpacket == NULL) {
      if (writer->hungry) {
	writer->hungry (oggz, 1, writer->hungry_user_data);
conrad's avatar
conrad committed
513
	next_zpacket = oggz_vector_pop (writer->packet_queue);
andre's avatar
andre committed
514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540
      }
    }
  }

  return next_zpacket;
}

static long
oggz_writer_make_packet (OGGZ * oggz)
{
  OggzWriter * writer = &oggz->x.writer;
  oggz_writer_packet_t * zpacket, * next_zpacket;
  int cb_ret = 0, ret = 0;

#ifdef DEBUG
  printf ("make packet ...\n");
#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
541
    int empty = (oggz_vector_size (writer->packet_queue) == 0);
andre's avatar
andre committed
542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681
    cb_ret = writer->hungry (oggz, empty, writer->hungry_user_data);
  }

  if (cb_ret == 0) {
    /* dequeue and init the next packet */
    if ((next_zpacket = oggz_dequeue_packet (oggz)) == NULL) {
      /*writer->eos = 1;*/
      ret = 0;
    } 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);
      }
      ret = 1;
    }
  } else {
    ret = 0;
  }

  return ret;
}

long
oggz_write_output (OGGZ * oggz, unsigned char * buf, long n)
{
  OggzWriter * writer = &oggz->x.writer;
  long bytes, bytes_written = 1, remaining = n, nwritten = 0;
  int active = 1;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

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

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

  while (active && remaining > 0) {
    bytes = MIN (remaining, 1024);

    while (writer->state == OGGZ_MAKING_PACKETS) {
      if (!oggz_writer_make_packet (oggz)) {
	active = 0;
	break;
      }
      if (oggz_page_init (oggz)) {
	writer->state = OGGZ_WRITING_PAGES;
      }
    }

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

      if (bytes_written == -1) {
	active = 0;
	return OGGZ_ERR_SYSTEM; /* XXX: catch next */
      } else if (bytes_written == 0) {
	if (!oggz_page_init (oggz)) {
	  writer->state = OGGZ_MAKING_PACKETS;
	}
      }

      buf += bytes_written;

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

  writer->writing = 0;

  return nwritten;
}

long
oggz_write (OGGZ * oggz, long n)
{
  OggzWriter * writer = &oggz->x.writer;
  long bytes, bytes_written = 1, remaining = n, nwritten = 0;
  int active = 1;

  if (oggz == NULL) return OGGZ_ERR_BAD_OGGZ;

  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

  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) {
      if (!oggz_writer_make_packet (oggz)) {
	active = 0;
#ifdef DEBUG
	printf ("oggz_write: no packets\n");
#endif
	break;
      }
      if (oggz_page_init (oggz)) {
	writer->state = OGGZ_WRITING_PAGES;
      }
    }

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

      if (bytes_written == -1) {
	active = 0;
	return OGGZ_ERR_SYSTEM; /* XXX: catch next */
      } else if (bytes_written == 0) {
	if (!oggz_page_init (oggz)) {
#ifdef DEBUG
	  printf ("oggz_write: bytes_written == 0, DONE\n");
#endif
	  writer->state = OGGZ_MAKING_PACKETS;
	}
      }

      remaining -= bytes_written;
682
      nwritten += bytes_written;
andre's avatar
andre committed
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716
    }
  }

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

  writer->writing = 0;

  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"

717 718 719 720 721 722 723 724 725 726 727 728
OGGZ *
oggz_write_init (OGGZ * oggz)
{
  return NULL;
}

OGGZ *
oggz_write_close (OGGZ * oggz)
{
  return NULL;
}

andre's avatar
andre committed
729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761
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