Remove old school plugins listing generator
[platform/upstream/gstreamer.git] / markdown / additional / design / latency.md
1 # Latency
2
3 The latency is the time it takes for a sample captured at timestamp 0 to
4 reach the sink. This time is measured against the pipeline's clock.
5 For pipelines where the only elements that synchronize against the clock
6 are the sinks, the latency is always 0, since no other element is
7 delaying the buffer.
8
9 For pipelines with live sources, a latency is introduced, mostly because
10 of the way a live source works. Consider an audio source, it will start
11 capturing the first sample at time 0. If the source pushes buffers with
12 44100 samples at a time at 44100Hz, it will have collected the buffer at
13 second 1. Since the timestamp of the buffer is 0 and the time of the
14 clock is now `>= 1` second, the sink will drop this buffer because it is
15 too late. Without any latency compensation in the sink, all buffers will
16 be dropped.
17
18 The situation becomes more complex in the presence of:
19
20 - 2 live sources connected to 2 live sinks with different latencies
21   - audio/video capture with synchronized live preview.
22   - added latencies due to effects (delays, resamplers…)
23
24 - 1 live source connected to 2 live sinks
25   - firewire DV
26   - RTP, with added latencies because of jitter buffers.
27
28 - mixed live source and non-live source scenarios.
29   - synchronized audio capture with non-live playback. (overdubs,..)
30
31 - clock slaving in the sinks due to the live sources providing their
32     own clocks.
33
34 To perform the needed latency corrections in the above scenarios, we
35 must develop an algorithm to calculate a global latency for the
36 pipeline. This algorithm must be extensible, so that it can optimize the
37 latency at runtime. It must also be possible to disable or tune the
38 algorithm based on specific application needs (required minimal
39 latency).
40
41 ## Pipelines without latency compensation
42
43 We show some examples to demonstrate the problem of latency in typical
44 capture pipelines.
45
46 ### Example 1
47
48 An audio capture/playback pipeline.
49
50 * asrc: audio source, provides a clock
51 * asink audio sink, provides a clock
52
53 ```
54 +--------------------------+
55 | pipeline                 |
56 | +------+      +-------+  |
57 | | asrc |      | asink |  |
58 | |     src -> sink     |  |
59 | +------+      +-------+  |
60 +--------------------------+
61 ```
62
63 * *NULL→READY*:
64     * asink: *NULL→READY*: probes device, returns `SUCCESS`
65     * asrc: *NULL→READY*:  probes device, returns `SUCCESS`
66
67 * *READY→PAUSED*:
68     * asink: *READY:→PAUSED* open device, returns `ASYNC`
69     * asrc: *READY→PAUSED*:  open device, returns `NO_PREROLL`
70
71 - Since the source is a live source, it will only produce data in
72 the `PLAYING` state. To note this fact, it returns `NO_PREROLL`
73 from the state change function.
74
75 - This sink returns `ASYNC` because it can only complete the state
76 change to `PAUSED` when it receives the first buffer.
77
78 At this point the pipeline is not processing data and the clock is not
79 running. Unless a new action is performed on the pipeline, this situation will
80 never change.
81
82 * *PAUSED→PLAYING*: asrc clock selected because it is the most upstream clock
83 provider. asink can only provide a clock when it received the first buffer and
84 configured the device with the samplerate in the caps.
85
86 * sink: *PAUSED:→PLAYING*, sets pending state to `PLAYING`, returns `ASYNC` because it
87 is not prerolled. The sink will commit state to `PLAYING` when it prerolls.
88 * src: *PAUSED→PLAYING*: starts pushing buffers.
89
90 - since the sink is still performing a state change from `READY→PAUSED`, it remains `ASYNC`. The pending state will be set to
91 `PLAYING`.
92
93 - The clock starts running as soon as all the elements have been
94 set to `PLAYING`.
95
96 - the source is a live source with a latency. Since it is
97 synchronized with the clock, it will produce a buffer with
98 timestamp 0 and duration D after time D, ie. it will only be
99 able to produce the last sample of the buffer (with timestamp D)
100 at time D. This latency depends on the size of the buffer.
101
102 - the sink will receive the buffer with timestamp 0 at time `>= D`.
103 At this point the buffer is too late already and might be
104 dropped. This state of constantly dropping data will not change
105 unless a constant latency correction is added to the incoming
106 buffer timestamps.
107
108 The problem is due to the fact that the sink is set to (pending) `PLAYING`
109 without being prerolled, which only happens in live pipelines.
110
111 ### Example 2
112
113 An audio/video capture/playback pipeline. We capture both audio and video and
114 have them played back synchronized again.
115
116 * asrc: audio source, provides a clock
117 * asink audio sink, provides a clock
118 * vsrc: video source
119 * vsink video sink
120
121 ```
122 .--------------------------.
123 | pipeline                 |
124 | .------.      .-------.  |
125 | | asrc |      | asink |  |
126 | |     src -> sink     |  |
127 | '------'      '-------'  |
128 | .------.      .-------.  |
129 | | vsrc |      | vsink |  |
130 | |     src -> sink     |  |
131 | '------'      '-------'  |
132 '--------------------------'
133 ```
134
135 The state changes happen in the same way as example 1. Both sinks end up with
136 pending state of `PLAYING` and a return value of `ASYNC` until they receive the
137 first buffer.
138
139 For audio and video to be played in sync, both sinks must compensate for the
140 latency of its source but must also use exactly the same latency correction.
141
142 Suppose asrc has a latency of 20ms and vsrc a latency of 33ms, the total
143 latency in the pipeline has to be at least 33ms. This also means that the
144 pipeline must have at least a `33 - 20 = 13ms` buffering on the audio stream or
145 else the audio src will underrun while the audiosink waits for the previous
146 sample to play.
147
148 ### Example 3
149
150 An example of the combination of a non-live (file) and a live source (vsrc)
151 connected to live sinks (vsink, sink).
152
153 ```
154 .--------------------------.
155 | pipeline                 |
156 | .------.      .-------.  |
157 | | file |      | sink  |  |
158 | |     src -> sink     |  |
159 | '------'      '-------'  |
160 | .------.      .-------.  |
161 | | vsrc |      | vsink |  |
162 | |     src -> sink     |  |
163 | '------'      '-------'  |
164 '--------------------------'
165 ```
166
167 The state changes happen in the same way as example 1. Except sink will be
168 able to preroll (commit its state to `PAUSED`).
169
170 In this case sink will have no latency but vsink will. The total latency
171 should be that of vsink.
172
173 Note that because of the presence of a live source (vsrc), the pipeline can be
174 set to playing before the sink is able to preroll. Without compensation for the
175 live source, this might lead to synchronisation problems because the latency
176 should be configured in the element before it can go to `PLAYING`.
177
178 ### Example 4
179
180 An example of the combination of a non-live and a live source. The non-live
181 source is connected to a live sink and the live source to a non-live sink.
182
183 ```
184 .--------------------------.
185 | pipeline                 |
186 | .------.      .-------.  |
187 | | file |      | sink  |  |
188 | |     src -> sink     |  |
189 | '------'      '-------'  |
190 | .------.      .-------.  |
191 | | vsrc |      | files |  |
192 | |     src -> sink     |  |
193 | '------'      '-------'  |
194 '--------------------------'
195 ```
196
197 The state changes happen in the same way as example 3. Sink will be
198 able to preroll (commit its state to `PAUSED`). files will not be able to
199 preroll.
200
201 sink will have no latency since it is not connected to a live source. files
202 does not do synchronisation so it does not care about latency.
203
204 The total latency in the pipeline is 0. The vsrc captures in sync with the
205 playback in sink.
206
207 As in example 3, sink can only be set to `PLAYING` after it successfully
208 prerolled.
209
210 ## State Changes
211
212 A sink is never set to `PLAYING` before it is prerolled. In order to do
213 this, the pipeline (at the `GstBin` level) keeps track of all elements
214 that require preroll (the ones that return `ASYNC` from the state change).
215 These elements posted an `ASYNC_START` message without a matching
216 `ASYNC_DONE` one.
217
218 The pipeline will not change the state of the elements that are still
219 doing an `ASYNC` state change.
220
221 When an ASYNC element prerolls, it commits its state to `PAUSED` and posts
222 an `ASYNC_DONE` message. The pipeline notices this `ASYNC_DONE` message
223 and matches it with the `ASYNC_START` message it cached for the
224 corresponding element.
225
226 When all `ASYNC_START` messages are matched with an `ASYNC_DONE` message,
227 the pipeline proceeds with setting the elements to the final state
228 again.
229
230 The base time of the element was already set by the pipeline when it
231 changed the `NO_PREROLL` element to `PLAYING`. This operation has to be
232 performed in the separate async state change thread (like the one
233 currently used for going from `PAUSED→PLAYING` in a non-live pipeline).
234
235 ## Query
236
237 The pipeline latency is queried with the `LATENCY` query.
238
239 * **`live`** `G_TYPE_BOOLEAN` (default `FALSE`): - if a live element is found upstream
240
241 * **`min-latency`** `G_TYPE_UINT64` (default 0, must not be `NONE`): - the minimum
242 latency in the pipeline, meaning the minimum time downstream elements
243 synchronizing to the clock have to wait until they can be sure all data
244 for the current running time has been received.
245
246 Elements answering the latency query and introducing latency must
247 set this to the maximum time for which they will delay data, while
248 considering upstream's minimum latency. As such, from an element's
249 perspective this is *not* its own minimum latency but its own
250 maximum latency.
251 Considering upstream's minimum latency generally means that the
252 element's own value is added to upstream's value, as this will give
253 the overall minimum latency of all elements from the source to the
254 current element:
255
256 ```c
257 min_latency = upstream_min_latency + own_min_latency
258 ```
259
260 * **`max-latency`** `G_TYPE_UINT64` (default 0, `NONE` meaning infinity): - the
261 maximum latency in the pipeline, meaning the maximum time an element
262 synchronizing to the clock is allowed to wait for receiving all data for the
263 current running time. Waiting for a longer time will result in data loss,
264 buffer overruns and underruns and, in general, breaks synchronized data flow
265 in the pipeline.
266
267 Elements answering the latency query should set this to the maximum
268 time for which they can buffer upstream data without blocking or
269 dropping further data. For an element, this value will generally be
270 its own minimum latency, but might be bigger than that if it can
271 buffer more data. As such, queue elements can be used to increase
272 the maximum latency.
273
274 The value set in the query should again consider upstream's maximum
275 latency:
276
277 - If the current element has blocking buffering, i.e. it does not drop data by
278 itself when its internal buffer is full, it should just add its own maximum
279 latency (i.e. the size of its internal buffer) to upstream's value. If
280 upstream's maximum latency, or the elements internal maximum latency was NONE
281 (i.e. infinity), it will be set to infinity.
282
283 ```c
284 if (upstream_max_latency == NONE || own_max_latency == NONE)
285   max_latency = NONE;
286 else
287   max_latency = upstream_max_latency + own_max_latency;
288 ```
289
290 If the element has multiple sinkpads, the minimum upstream latency is
291 the maximum of all live upstream minimum latencies.
292
293 If the current element has leaky buffering, i.e. it drops data by itself
294 when its internal buffer is full, it should take the minimum of its own
295 maximum latency and upstream’s. Examples for such elements are audio sinks
296 and sources with an internal ringbuffer, leaky queues and in general live
297 sources with a limited amount of internal buffers that can be used.
298
299 ```c
300 max_latency = MIN (upstream_max_latency, own_max_latency)
301 ```
302
303 > Note: many GStreamer base classes allow subclasses to set a
304 > minimum and maximum latency and handle the query themselves. These
305 > base classes assume non-leaky (i.e. blocking) buffering for the
306 > maximum latency. The base class' default query handler needs to be
307 > overridden to correctly handle leaky buffering.
308
309  If the element has multiple sinkpads, the maximum upstream latency is the
310  minimum of all live upstream maximum latencies.
311
312 ## Event
313
314 The latency in the pipeline is configured with the `LATENCY` event, which
315 contains the following fields:
316
317 * **`latency`** `G_TYPE_UINT64`: the configured latency in the pipeline
318
319 ## Latency compensation
320
321 Latency calculation and compensation is performed before the pipeline
322 proceeds to the `PLAYING` state.
323
324 When the pipeline collected all `ASYNC_DONE` messages it can calculate
325 the global latency as follows:
326
327 - perform a latency query on all sinks
328 - sources set their minimum and maximum latency
329 - other elements add their own values as described above
330 - latency = MAX (all min latencies)
331 - `if MIN (all max latencies) < latency`, we have an impossible
332 situation and we must generate an error indicating that this
333 pipeline cannot be played. This usually means that there is not
334 enough buffering in some chain of the pipeline. A queue can be added
335 to those chains.
336
337 The sinks gather this information with a `LATENCY` query upstream.
338 Intermediate elements pass the query upstream and add the amount of
339 latency they add to the result.
340
341 ```
342 ex1: sink1: [20 - 20] sink2: [33 - 40]
343
344     MAX (20, 33) = 33
345     MIN (20, 40) = 20 < 33 -> impossible
346
347 ex2: sink1: [20 - 50] sink2: [33 - 40]
348
349     MAX (20, 33) = 33
350     MIN (50, 40) = 40 >= 33 -> latency = 33
351 ```
352
353 The latency is set on the pipeline by sending a `LATENCY` event to the
354 sinks in the pipeline. This event configures the total latency on the
355 sinks. The sink forwards this `LATENCY` event upstream so that
356 intermediate elements can configure themselves as well.
357
358 After this step, the pipeline continues setting the pending state on its
359 elements.
360
361 A sink adds the latency value, received in the `LATENCY` event, to the
362 times used for synchronizing against the clock. This will effectively
363 delay the rendering of the buffer with the required latency. Since this
364 delay is the same for all sinks, all sinks will render data relatively
365 synchronised.
366
367 ## Flushing a playing pipeline
368
369 We can implement resynchronisation after an uncontrolled `FLUSH` in (part
370 of) a pipeline in the same way. Indeed, when a flush is performed on a
371 `PLAYING` live element, a new base time must be distributed to this
372 element.
373
374 A flush in a pipeline can happen in the following cases:
375
376   - flushing seek in the pipeline
377
378   - performed by the application on the pipeline
379
380   - performed by the application on an element
381
382   - flush preformed by an element
383
384   - after receiving a navigation event (DVD, …)
385
386 When a playing sink is flushed by a `FLUSH_START` event, an `ASYNC_START`
387 message is posted by the element. As part of the message, the fact that
388 the element got flushed is included. The element also goes to a pending
389 PAUSED state and has to be set to the `PLAYING` state again later.
390
391 The `ASYNC_START` message is kept by the parent bin. When the element
392 prerolls, it posts an `ASYNC_DONE` message.
393
394 When all `ASYNC_START` messages are matched with an `ASYNC_DONE` message,
395 the bin will capture a new `base_time` from the clock and will bring all
396 the sinks back to `PLAYING` after setting the new base time on them. It’s
397 also possible to perform additional latency calculations and adjustments
398 before doing this.
399
400 ## Dynamically adjusting latency
401
402 An element that wants to change the latency in the pipeline can do this
403 by posting a `LATENCY` message on the bus. This message instructs the
404 pipeline to:
405
406   - query the latency in the pipeline (which might now have changed)
407     with a `LATENCY` query.
408
409   - redistribute a new global latency to all elements with a `LATENCY`
410     event.
411
412 A use case where the latency in a pipeline can change could be a network
413 element that observes an increased inter-packet arrival jitter or
414 excessive packet loss and decides to increase its internal buffering
415 (and thus the latency). The element must post a `LATENCY` message and
416 perform the additional latency adjustments when it receives the `LATENCY`
417 event from the downstream peer element.
418
419 In a similar way, the latency can be decreased when network conditions
420 improve.
421
422 Latency adjustments will introduce playback glitches in the sinks and
423 must only be performed in special conditions.