1 <chapter id="chapter-queryevents">
2 <title>Position tracking and seeking</title>
5 So far, we've looked at how to create a pipeline to do media processing
6 and how to make it run ("iterate"). Most application developers will be
7 interested in providing feedback to the user on media progress. Media
8 players, for example, will want to show a slider showing the progress in
9 the song, and usually also a label indicating stream length. Transcoding
10 applications will want to show a progress bar on how much % of the task
11 is done. &GStreamer; has built-in support for doing all this using a
12 concept known as <emphasis>querying</emphasis>. Since seeking is very
13 similar, it will be discussed here as well. Seeking is done using the
14 concept of <emphasis>events</emphasis>.
17 <sect1 id="section-querying">
18 <title>Querying: getting the position or length of a stream</title>
21 Querying is defined as requesting a specific stream-property related
22 to progress tracking. This includes getting the length of a stream (if
23 available) or getting the current position. Those stream properties
24 can be retrieved in various formats such as time, audio samples, video
25 frames or bytes. The functions used are <function>gst_element_query
26 ()</function> and <function>gst_pad_query ()</function>.
30 Obviously, using either of the above-mentioned functions requires the
31 application to know <emphasis>which</emphasis> element or pad to run
32 the query on. This is tricky, but there are some good sides to the
33 story. The good thing is that elements (or, rather, pads - since
34 <function>gst_element_query ()</function> internally calls
35 <function>gst_pad_query ()</function>) forward (<quote>dispatch</quote>)
36 events and queries to peer pads (or elements) if they don't handle it
37 themselves. The bad side is that some elements (or pads) will handle
38 events, but not the specific formats that you want, and therefore it
43 Most queries will, fortunately, work fine. Queries are always
44 dispatched backwards. This means, effectively, that it's easiest to
45 run the query on your video or audio output element, and it will take
46 care of dispatching the query to the element that knows the answer
47 (such as the current position or the media length; usually the demuxer
52 #include <gst/gst.h>
58 GstElement *sink, *pipeline;
64 GstFormat fmt = GST_FORMAT_TIME;
66 if (gst_element_query (sink, GST_QUERY_POSITION, &fmt, &pos) &&
67 gst_element_query (sink, GST_QUERY_TOTAL, &fmt, &len)) {
68 g_print ("Time: %" GST_FORMAT_TIME " / %" GST_FORMAT_TIME "\r",
69 GST_TIME_ARGS (pos), GST_TIME_ARGS (len));
71 } while (gst_bin_iterate (GST_BIN (pipeline)));
78 If you are having problems with the dispatching behaviour, your best
79 bet is to manually decide which element to start running the query on.
80 You can get a list of supported formats and query-types with
81 <function>gst_element_get_query_types ()</function> and
82 <function>gst_element_get_formats ()</function>.
86 <sect1 id="section-eventsseek">
87 <title>Events: seeking (and more)</title>
90 Events work in a very similar way as queries. Dispatching, for
91 example, works exactly the same for events (and also has the same
92 limitations). Although there are more ways in which applications
93 and elements can interact using events, we will only focus on seeking
94 here. This is done using the seek-event. A seek-event contains a
95 seeking offset, a seek method (which indicates relative to what the
96 offset was given), a seek format (which is the unit of the offset,
97 e.g. time, audio samples, video frames or bytes) and optionally a
98 set of seeking-related flags (e.g. whether internal buffers should be
99 flushed). The behaviour of a seek is also wrapped in the function
100 <function>gst_element_seek ()</function>.
104 #include <gst/gst.h>
107 seek_to_time (GstElement *audiosink,
108 gint64 time_nanonseconds)
110 gst_element_seek (audiosink,
111 GST_SEEK_METHOD_SET | GST_FORMAT_TIME |
112 GST_SEEK_FLAG_FLUSH, time_nanoseconds);