Events: Seeking, Navigation and More
There are many different event types but only 2 ways they can travel across
the pipeline: downstream or upstream. It is very important to understand
how both of those methods work because if one element in the pipeline is not
handling them correctly the whole event system of the pipeline is broken.
We will try to explain here how these methods work and how elements are
supposed to implement them.
Downstream events
Downstream events are received through the sink pad's dataflow. Depending
if your element is loop or chain based you will receive events in your
loop/chain function as a GstData with gst_pad_pull
or directly in the function call arguments. So when receiving dataflow
from the sink pad you have to check first if this data chunk is an event.
If that's the case you check what kind of event it is to react on relevant
ones and then forward others downstream using
gst_pad_event_default. Here is an example for both
loop and chain based elements.
/* Chain based element */
static void
gst_my_filter_chain (GstPad *pad,
GstData *data)
{
GstMyFilter *filter = GST_MY_FILTER (gst_pad_get_parent (pad));
...
if (GST_IS_EVENT (data)) {
GstEvent *event = GST_EVENT (data);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
/* end-of-stream, we should close down all stream leftovers here */
gst_my_filter_stop_processing (filter);
/* fall-through to default event handling */
default:
gst_pad_event_default (pad, event);
break;
}
return;
}
...
}
/* Loop based element */
static void
gst_my_filter_loop (GstElement *element)
{
GstMyFilter *filter = GST_MY_FILTER (element);
GstData *data = NULL;
data = gst_pad_pull (filter->sinkpad);
if (GST_IS_EVENT (data)) {
GstEvent *event = GST_EVENT (data);
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_EOS:
/* end-of-stream, we should close down all stream leftovers here */
gst_my_filter_stop_processing (filter);
/* fall-through to default event handling */
default:
gst_pad_event_default (filter->sinkpad, event);
break;
}
return;
}
...
}
Upstream events
Upstream events are generated by an element somewhere in the pipeline and
sent using the gst_pad_send_event function. This
function simply realizes the pad and call the default event handler of that
pad. The default event handler of pads is gst_pad_event_default
, it basically sends the event to the peer pad. So upstream events always
arrive on the src pad of your element and are handled by the default event
handler except if you override that handler to handle it yourself. There
are some specific cases where you have to do that :
If you have multiple sink pads in your element. In that case you will
have to decide which one of the sink pads you will send the event to.
If you need to handle that event locally. For example a navigation
event that you will want to convert before sending it upstream.
The processing you will do in that event handler does not really matter
but there are important rules you have to absolutely respect because
one broken element event handler is breaking the whole pipeline event
handling. Here they are :
Always forward events you won't handle upstream using the default
gst_pad_event_default method.
If you are generating some new event based on the one you received
don't forget to gst_event_unref the event you received.
Event handler function are supposed to return TRUE or FALSE indicating
if the event has been handled or not. Never simply return TRUE/FALSE
in that handler except if you really know that you have handled that
event.
Here is an example of correct upstream event handling for a plugin
that wants to modify navigation events.
static gboolean
gst_my_filter_handle_src_event (GstPad *pad,
GstEvent *event)
{
GstMyFilter *filter = GST_MY_FILTER (gst_pad_get_parent (pad));
switch (GST_EVENT_TYPE (event)) {
case GST_EVENT_NAVIGATION:
GstEvent *new_event = gst_event_new (GST_EVENT_NAVIGATION);;
/* Create a new event based on received one and then send it */
...
gst_event_unref (event);
return gst_pad_event_default (pad, new_event);
default:
/* Falling back to default event handling for that pad */
return gst_pad_event_default (pad, event);
}
}
All Events Together
In this chapter follows a list of all defined events that are currently
being used, plus how they should be used/interpreted. Events are stored
in a GstEvent
structure, which is simply a big C union with the
types for each event in it. For the next development cycle, we intend to
switch events over to GstStructure
,
but you don't need to worry about that too much for now.
In this chapter, we will discuss the following events:
End of Stream (EOS)
End-of-stream events are sent if the stream that an element sends out
is finished. An element receiving this event (from upstream, so it
receives it on its sinkpad) will generally forward the event further
downstream and set itself to EOS (gst_element_set_eos ()).
gst_pad_event_default () takes care of all this,
so most elements do not need to support this event. Exceptions are
elements that explicitly need to close a resource down on EOS, and
N-to-1 elements. Note that the stream itself is not
a resource that should be closed down on EOS! Applications might seek
back to a point before EOS and set the pipeline to PLAYING again.
N-to-1 elements have been discussed previously in
.
The EOS event (GST_EVENT_EOS) has no properties,
and that makes it one of the simplest events in &GStreamer;. It is
created using gst_event_new (GST_EVENT_EOS);.
Some elements support the EOS event upstream, too. This signals the
element to go into EOS as soon as possible and signal the EOS event
forward downstream. This is useful for elements that have no concept
of end-of-stream themselves. Examples are TV card sources, audio card
sources, etc. This is not (yet) part of the official specifications of
this event, though.
Flush
The flush event is being sent downstream if all buffers and caches
in the pipeline should be emptied. Queue
elements will
empty their internal list of buffers when they receive this event, for
example. File sink elements (e.g. filesink
) will flush
the kernel-to-disk cache (fdatasync () or
fflush ()) when they receive this event. Normally,
elements receiving this event will simply just forward it, since most
filter or filter-like elements don't have an internal cache of data.
gst_pad_event_default () does just that, so for
most elements, it is enough to forward the event using the default
event handler.
The flush event is created with gst_event_new (GST_EVENT_FLUSH);.
Like the EOS event, it has no properties.
Stream Discontinuity
A discontinuity event is sent downstream to indicate a discontinuity in
the data stream. This can happen because the application used the seek
event to seek to a different position in the stream, but it can also be
because a real-time network source temporarily lost the connection.
After the connection is restored, the data stream will continue, but
not at the same point where it got lost. Therefore, a discontinuity
event is being sent downstream, too.
Depending on the element type, the event can simply be forwarded using
gst_pad_event_default (), or it should be parsed
and a modified event should be sent on. The last is true for demuxers,
which generally have a byte-to-time conversion concept. Their input
is usually byte-based, so the incoming event will have an offset in
byte units (GST_FORMAT_BYTES), too. Elements
downstream, however, expect discontinuity events in time units, so that
it can be used to update the pipeline clock. Therefore, demuxers and
similar elements should not forward the event, but parse it, free it
and send a new discontinuity event (in time units,
GST_FORMAT_TIME) further downstream.
The discontinuity event is created using the function
gst_event_new_discontinuous (). It should set a
boolean value which indicates if the discontinuity event is sent
because of a new media type (this can happen if - during iteration -
a new location was set on a network source or on a file source).
then, it should give a list of formats and offsets in that format. The
list should be terminated by 0 as format.
static void
my_filter_some_function (GstMyFilter *filter)
{
GstEvent *event;
[..]
event = gst_event_new_discontinuous (FALSE,
GST_FORMAT_BYTES, 0,
GST_FORMAT_TIME, 0,
0);
gst_pad_push (filter->srcpad, GST_DATA (event));
[..]
}
Elements parsing this event can use macros and functions to access the
various properties. GST_EVENT_DISCONT_NEW_MEDIA (event)
checks the new-media boolean value.
gst_event_discont_get_value (event, format, &value)
gets the offset of the new stream position in the specified format. If
that format was not specified when creating the event, the function
returns FALSE.
Seek Request
Seek events are meant to request a new stream position to elements.
This new position can be set in several formats (time, bytes or
units
[a term indicating frames for video, samples for
audio, etc.]). Seeking can be done with respect to the end-of-file,
start-of-file or current position, and can happen in both upstream and
downstream direction. Elements receiving seek events should, depending
on the element type, either forward it (filters, decoders), change the
format in which the event is given and forward it (demuxers), handle
the event by changing the file pointer in their internal stream
resource (file sources) or something else.
Seek events are, like discontinuity events, built up using positions in
specified formats (time, bytes, units). They are created using the
function gst_event_new_seek (), where the first
argument is the seek type (indicating with respect to which position
[current, end, start] the seek should be applied, and the format in
which the new position is given (time, bytes, units), and an offset
which is the requested position in the specified format.
static void
my_filter_some_function (GstMyFilter *filter)
{
GstEvent *event;
[..]
/* seek to the start of a resource */
event = gst_event_new_seek (GST_SEEK_SET | GST_FORMAT_BYTES, 0);
gst_pad_push (filter->srcpad, GST_DATA (event));
[..]
}
Elements parsing this event can use macros and functions to access the
properties. The seek type can be retrieved using
GST_EVENT_SEEK_TYPE (event). This seek type
contains both the indicator of with respect to what position the seek
should be applied, and the format in which the seek event is given.
To get either one of these properties separately, use
GST_EVENT_SEEK_FORMAT (event) or
GST_EVENT_SEEK_METHOD (event). The requested
position is available through GST_EVENT_SEEK_OFFSET (event),
and is given in the specified format.
Stream Filler
The filler event is, as the name says, a filler
of the
stream which has no special meaning associated with itself. It is used
to provide data to downstream elements and should be interpreted as a
way of assuring that the normal data flow will continue further
downstream. The event is especially intended for real-time MIDI source
elements, which only generate data when something changes.
MIDI decoders will therefore stall if nothing changes for several
seconds, and therefore playback will stop. The filler event is sent
downstream to assure the MIDI decoder that nothing changed, so that the
normal decoding process will continue and playback will, too. Unless
you intend to work with MIDI or other control-language-based data
types, you don't need this event. You can mostly simply forward it
with gst_pad_event_default ().
The stream filler is created using gst_event_new (GST_EVENT_FILLER);.
It has no properties.
Interruption
The interrupt event is generated by queue elements and sent downstream
if a timeout occurs on the stream. The scheduler will use this event to
get back in its own main loop and schedule other elements. This
prevents deadlocks or a stream stall if no data is generated over a
part of the pipeline for a considerable amount of time. The scheduler
will process this event internally, so any normal elements do not need
to generate or handle this event at all.
The difference between the filler event and the interrupt event is that
the filler event is a real part of a pipeline, so it will reach fellow
elements, which can use it to "do nothing else than what I used to do".
The interrupt event never reaches fellow elements.
The interrupt event (gst_event_new (GST_EVENT_INTERRUPT);)
has no properties.
Navigation
WRITEME
Tag (metadata)
Tagging events are being sent downstream to indicate the tags as parsed
from the stream data. This is currently used to preserve tags during
stream transcoding from one format to the other. Tags are discussed
extensively in . Most
elements will simply forward the event by calling
gst_pad_event_default ().
The tag event is created using the function
gst_event_new_tag (). It requires a filled
taglist as argument.
Elements parsing this event can use the function
gst_event_tag_get_list (event) to acquire the
taglist that was parsed.