GStreamer Plugin Writer's Guide (0.8.10) | ||
---|---|---|
<<< Previous | Writing a 1-to-N Element, Demuxer or Parser | Next >>> |
One particular convention that GStreamer demuxers follow is that of separation of parsing and interpreting. The reason for this is maintainability, clarity and code reuse. An easy example of this is something like RIFF, which has a chunk header of 4 bytes, then a length indicator of 4 bytes and then the actual data. We write special functions to read one chunk, to peek a chunk ID and all those; that's the parsing part of the demuxer. Then, somewhere else, we like to write the main data processing function, which calls this parse function, reads one chunk and then does with the data whatever it needs to do.
Some example code for RIFF-reading to illustrate the above two points:
static gboolean gst_my_demuxer_peek (GstMyDemuxer *demux, guint32 *id, guint32 *size) { guint8 *data; while (gst_bytestream_peek_bytes (demux->bs, &data, 4) != 4) { guint32 remaining; GstEvent *event; gst_bytestream_get_status (demux->bs, &remaining, &event); if (event) { GstEventType type = GST_EVENT_TYPE (event); /* or maybe custom event handling, up to you - we lose reference! */ gst_pad_event_default (demux->sinkpad, event); if (type == GST_EVENT_EOS) return FALSE; } else { GST_ELEMENT_ERROR (demux, STREAM, READ, (NULL), (NULL)); return FALSE; } } *id = GUINT32_FROM_LE (((guint32 *) data)[0]); *size = GUINT32_FROM_LE (((guint32 *) data)[0]); return TRUE; } static void gst_my_demuxer_loop (GstElement *element) { GstMyDemuxer *demux = GST_MY_DEMUXER (element); guint32 id, size; if (!gst_my_demuxer_peek (demux, &id, &size)) return; switch (id) { [.. normal chunk handling ..] } } |
Reason for this is that event handling is now centralized in one place and the _loop () function is a lot cleaner and more readable. Those are common code practices, but since the mistake of not using such common code practices has been made too often, we explicitely mention this here.
<<< Previous | Home | Next >>> |
Data processing and downstream events | Up | Simple seeking and indexes |