liboggz: Writing with OggzHungry callbacks

liboggz 1.1.1

Writing with OggzHungry callbacks

You can add packets to the Oggz packet queue only when it is "hungry" by providing an OggzHungry callback. More...

You can add packets to the Oggz packet queue only when it is "hungry" by providing an OggzHungry callback.

An OggzHungry callback will:

- Create an ogg_packet structure
- Add it to the packet queue with oggz_write_feed()

Once you have set such a callback with oggz_write_set_hungry_callback(), simply call oggz_write() or oggz_write_output() repeatedly, and Oggz will call your callback to provide packets when it is hungry.

This process is illustrated in the following diagram:

The following example code generates a stream of ten packets, each containing a single byte ('A', 'B', ... , 'J'):

#include <stdlib.h> /* exit */
#include "oggz/oggz.h"
static long serialno;
static ogg_int64_t granulepos = 0;
static ogg_int64_t packetno = 0;
static int
hungry (OGGZ * oggz, int empty, void * user_data)
{
ogg_packet op;
unsigned char buf[1];
buf[0] = 'A' + (int)packetno;
op.packet = buf;
op.bytes = 1;
op.granulepos = granulepos;
op.packetno = packetno;
if (packetno == 0) op.b_o_s = 1;
else op.b_o_s = 0;
if (packetno == 9) op.e_o_s = 1;
else op.e_o_s = 0;
oggz_write_feed (oggz, &op, serialno, OGGZ_FLUSH_AFTER, NULL);
granulepos += 100;
packetno++;
return 0;
}
int
main (int argc, char * argv[])
{
char * progname, * filename = NULL;
OGGZ * oggz;
long n;
progname = argv[0];
if (argc > 1) filename = argv[1];
if (filename) {
oggz = oggz_open (filename, OGGZ_WRITE);
} else {
oggz = oggz_open_stdio (stdout, OGGZ_WRITE);
}
if (oggz == NULL) {
fprintf (stderr, "%s: Error creating oggz\n", progname);
exit (1);
}
serialno = oggz_serialno_new (oggz);
if (oggz_write_set_hungry_callback (oggz, hungry, 1, NULL) == -1) {
fprintf (stderr, "%s: Error setting OggzHungry callback\n", progname);
exit (1);
}
while ((n = oggz_write (oggz, 32)) > 0);
oggz_close (oggz);
exit (0);
}
oggz.h
oggz_close
int oggz_close(OGGZ *oggz)
Close an OGGZ handle.
oggz_open_stdio
OGGZ * oggz_open_stdio(FILE *file, int flags)
Create an OGGZ handle associated with a stdio stream.
OGGZ_WRITE
@ OGGZ_WRITE
Write only.
Definition: oggz_constants.h:51
OGGZ
void OGGZ
An opaque handle to an Ogg file.
Definition: oggz.h:441
OGGZ_FLUSH_AFTER
@ OGGZ_FLUSH_AFTER
Flush after this packet.
Definition: oggz_constants.h:100
oggz_write
long oggz_write(OGGZ *oggz, long n)
Write n bytes from an OGGZ handle.
oggz_open
OGGZ * oggz_open(const char *filename, int flags)
Open an Ogg file, creating an OGGZ handle for it.
oggz_write_set_hungry_callback
int oggz_write_set_hungry_callback(OGGZ *oggz, OggzWriteHungry hungry, int only_when_empty, void *user_data)
Set a callback for Oggz to call when oggz is hungry .
oggz_write_feed
int oggz_write_feed(OGGZ *oggz, ogg_packet *op, long serialno, int flush, int *guard)
Add a packet to oggz's packet queue.
oggz_serialno_new
long oggz_serialno_new(OGGZ *oggz)
Request a new serialno, as required for a new stream, ensuring the serialno is not yet used for any o...