1 <chapter id="chapter-advanced-clock">
2 <title>Clocking</title>
5 When playing complex media, each sound and video sample must be played in a
6 specific order at a specific time. For this purpose, GStreamer provides a
7 synchronization mechanism.
10 <sect1 id="section-clocks" xreflabel="Clocks">
13 Time in &GStreamer; is defined as the value returned from a particular
14 <classname>GstClock</classname> object from the method
15 <function>gst_clock_get_time ()</function>.
18 In a typical computer, there are many sources that can be used as a
19 time source, e.g., the system time, soundcards, CPU performance
20 counters, ... For this reason, there are many
21 <classname>GstClock</classname> implementations available in &GStreamer;.
22 The clock time doesn't always start from 0 or from some known value.
23 Some clocks start counting from some known start date, other clocks start
24 counting since last reboot, etc...
27 As clocks return an absolute measure of time, they are not usually used
28 directly. Instead, differences between two clock times are used to
29 measure elapsed time according to a clock.
33 <sect1 id="section-clock-time-types" xreflabel="Clock running-time">
34 <title> Clock running-time </title>
36 A clock returns the <emphasis role="strong">absolute-time</emphasis>
37 according to that clock with <function>gst_clock_get_time ()</function>.
38 From the absolute-time is a <emphasis role="strong">running-time</emphasis>
39 calculated, which is simply the difference between a previous snapshot
40 of the absolute-time called the <emphasis role="strong">base-time</emphasis>.
44 running-time = absolute-time - base-time
47 A &GStreamer; <classname>GstPipeline</classname> object maintains a
48 <classname>GstClock</classname> object and a base-time when it goes
49 to the PLAYING state. The pipeline gives a handle to the selected
50 <classname>GstClock</classname> to each element in the pipeline along
51 with selected base-time. The pipeline will select a base-time in such
52 a way that the running-time reflects the total time spent in the
53 PLAYING state. As a result, when the pipeline is PAUSED, the
54 running-time stands still.
57 Because all objects in the pipeline have the same clock and base-time,
58 they can thus all calculate the running-time according to the pipeline
63 <sect1 id="section-buffer-time-types" xreflabel="Buffer running-time">
64 <title> Buffer running-time </title>
66 To calculate a buffer running-time, we need a buffer timestamp and
67 the SEGMENT event that preceded the buffer. First we can convert
68 the SEGMENT event into a <classname>GstSegment</classname> object
69 and then we can use the
70 <function>gst_segment_to_running_time ()</function> function to
71 perform the calculation of the buffer running-time.
74 Synchronization is now a matter of making sure that a buffer with a
75 certain running-time is played when the clock reaches the same
76 running-time. Usually this task is done by sink elements. Sink also
77 have to take into account the latency configured in the pipeline and
78 add this to the buffer running-time before synchronizing to the
84 <sect1 id="section-clock-obligations-of-each-element" xreflabel="Obligations
87 Obligations of each element.
91 Let us clarify the contract between GStreamer and each element in the
96 <title>Non-live source elements </title>
98 Non-live source elements must place a timestamp in each buffer that
99 they deliver when this is possible. They must choose the timestamps
100 and the values of the SEGMENT event in such a way that the
101 running-time of the buffer starts from 0.
104 Some sources, such as filesrc, is not able to generate timestamps
105 on all buffers. It can and must however create a timestamp on the
106 first buffer (with a running-time of 0).
109 The source then pushes out the SEGMENT event followed by the
115 <title>Live source elements </title>
117 Live source elements must place a timestamp in each buffer that
118 they deliver. They must choose the timestamps and the values of the
119 SEGMENT event in such a way that the running-time of the buffer
120 matches exactly the running-time of the pipeline clock when the first
121 byte in the buffer was captured.
126 <title>Parser/Decoder/Encoder elements </title>
128 Parser/Decoder elements must use the incoming timestamps and transfer
129 those to the resulting output buffers. They are allowed to interpolate
130 or reconstruct timestamps on missing input buffers when they can.
135 <title>Demuxer elements </title>
137 Demuxer elements can usually set the timestamps stored inside the media
138 file onto the outgoing buffers. They need to make sure that outgoing
139 buffers that are to be played at the same time have the same
140 running-time. Demuxers also need to take into account the incoming
141 timestamps on buffers and use that to calculate an offset on the outgoing
147 <title>Muxer elements</title>
149 Muxer elements should use the incoming buffer running-time to mux the
150 different streams together. They should copy the incoming running-time
151 to the outgoing buffers.
156 <title>Sink elements</title>
158 If the element is intended to emit samples at a specific time (real time
159 playing), the element should require a clock, and thus implement the
160 method <function>set_clock</function>.
163 The sink should then make sure that the sample with running-time is played
164 exactly when the pipeline clock reaches that running-time + latency.
165 Some elements might use the clock API such as
166 <function>gst_clock_id_wait()</function>
167 to perform this action. Other sinks might need to use other means of
168 scheduling timely playback of the data.