docs: review the buffering docs
[platform/upstream/gstreamer.git] / docs / design / part-buffering.txt
1 Buffering
2 ---------
3
4 This document outlines the buffering policy used in the GStreamer
5 core that can be used by plugins and applications.
6
7 The purpose of buffering is to accumulate enough data in a pipeline so that
8 playback can occur smoothly and without interruptions. It is typically done
9 when reading from a (slow) and non-live network source but can also be used for
10 live sources.
11
12 We want to be able to implement the following features:
13
14  - buffering up to a specific amount of data, in memory, before starting playback
15    so that network fluctuations are minimized.
16  - download of the network file to a local disk with fast seeking in the
17    downloaded data. This is similar to the quicktime/youtube players.
18  - caching of semi-live streams to a local, on disk, ringbuffer with seeking in
19    the cached area. This is similar to tivo-like timeshifting.
20  - progress report about the buffering operations
21  - the possibility for the application to do more complex buffering
22
23 Some use cases:
24
25  * Stream buffering:
26
27    +---------+     +---------+     +-------+
28    | httpsrc |     | buffer  |     | demux |
29    |        src - sink      src - sink     ....
30    +---------+     +---------+     +-------+
31
32   In this case we are reading from a slow network source into a buffer element
33   (such as queue2). 
34   
35   The buffer element has a low and high watermark expressed in bytes. The
36   buffer uses the watermarks as follows:
37   
38    - The buffer element will post BUFFERING messages until the high watermark
39      is hit. This instructs the application to keep the pipeline PAUSED, which
40      will eventually block the srcpad from pushing while data is prerolled in
41      the sinks.
42    - When the high watermark is hit, a BUFFERING message with 100% will be
43      posted, which instructs the application to continue playback.
44    - When during playback, the low watermark is hit, the queue will start posting
45      BUFFERING messages again, making the application PAUSE the pipeline again
46      until the high watermark is hit again. This is called the rebuffering
47      stage.
48    - during playback, the queue level will fluctuate between the high and the
49      low watermark as a way to compensate for network irregularities.
50
51   This buffering method is usable when the demuxer operates in push mode.
52   Seeking in the stream requires the seek to happen in the network source.
53   It is mostly desirable when the total duration of the file is not know, such
54   as in live streaming or when efficient seeking is not possible/required.
55
56  * Incremental download
57
58    +---------+     +---------+     +-------+
59    | httpsrc |     | buffer  |     | demux |
60    |        src - sink      src - sink     ....
61    +---------+     +----|----+     +-------+
62                         V
63                        file
64   
65   In this case, we know the server is streaming a fixed length file to the
66   client. The application can choose to download the file on disk. The buffer
67   element will provide a push or pull based srcpad to the demuxer to navigate in
68   the downloaded file.
69
70   This mode is only suitable when the client can determine the length of the
71   file on the server.
72
73   In this case, buffering messages will be emited as usual when the requested
74   range is not within the downloaded area + buffersize. The buffering message
75   will also contain an indication that incremental download is being performed.
76   This flag can be used to let the application control the buffering in a more
77   intelligent way, using the BUFFERING query, for example.
78
79   The application can use the BUFFERING query to get the estimated download time
80   and match this time to the current/remaining playback time to control when
81   playback should start to have a non-interrupted playback experience.
82
83
84  * Timeshifting
85
86    +---------+     +---------+     +-------+
87    | httpsrc |     | buffer  |     | demux |
88    |        src - sink      src - sink     ....
89    +---------+     +----|----+     +-------+
90                         V
91                     file-ringbuffer
92
93   In this mode, a fixed size ringbuffer is kept to download the server content.
94   This allows for seeking in the buffered data. Depending on the size of the
95   buffer one can seek further back in time.
96
97   This mode is suitable for all live streams.
98
99   As with the incremental download mode, buffering messages are emited along
100   with an indication that timeshifting download is in progress. 
101
102
103  * Live buffering
104
105   In live pipelines we usually introduce some latency between the capture and
106   the playback elements. This latency can be introduced by a queue (such as a
107   jitterbuffer) or by other means (in the audiosink).
108
109   Buffering messages can be emited in those live pipelines as well and serve as
110   an indication to the user of the latency buffering. The application usually
111   does not react to these buffering messages with a state change.
112
113
114 Messages
115 ~~~~~~~~
116
117 A GST_MESSAGE_BUFFERING must be posted on the bus when playback temporarily
118 stops to buffer and when buffering finishes. When percentage field in the
119 BUFFERING message is 100, buffering is done. Values less than 100 mean that
120 buffering is in progress. 
121
122 The BUFFERING message should be intercepted and acted upon by the application.
123 The message contains at least one field that is sufficient for basic
124 functionality:
125
126   "buffer-percent", G_TYPE_INT, between 0 and 100
127
128 Several more clever ways of dealing with the buffering messages can be used when
129 in incremental or timeshifting download mode. For this purpose additional fields
130 are added to the buffering message:
131
132   "buffering-mode", GST_TYPE_BUFFERING_MODE, 
133                    enum { "stream", "download", "timeshift", "live" }
134       - gives the buffering mode in use. See above for an explanation of the
135         different modes of buffering. This field can be used to let the
136         application have more control over the buffering process.
137
138   "avg-in-rate", G_TYPE_INT
139       - gives the average input buffering speed in bytes/second. -1 is unknown.
140         This is the average number of bytes per second that is received on the
141         buffering element input (sink) pads. It is a measurement of the network
142         speed in most cases.
143
144   "avg-out-rate", G_TYPE_INT
145       - gives the average consumption speed in bytes/second. -1 is unknown.
146         This is the average number of bytes per second that is consumed by the
147         downstream element of the buffering element. 
148
149   "buffering-left", G_TYPE_INT64
150       - gives the estimated time that bufferring will take in milliseconds.
151         -1 unknown.
152         This is measured based on the avg-in-rate and the filled level of the
153         queue. The application can use this hint to update the GUI about the
154         estimated remaining time that buffering will take.
155
156 Application
157 ~~~~~~~~~~~
158
159 While data is buffered, the pipeline should remain in the PAUSED state. It is
160 also possible that more data should be buffered while the pipeline is PLAYING,
161 in which case the pipeline should be PAUSED until the buffering finished.
162
163 BUFFERING messages can be posted while the pipeline is prerolling. The
164 application should not set the pipeline to PLAYING before a BUFFERING message
165 with 100 percent value is received, which might only happen after the pipeline
166 prerolled.
167
168 An exception is made for live pipelines. The application may not change
169 the state of a live pipeline when a buffering message is received. Usually these
170 buffering messages contain the "buffering-mode" = "live".
171
172 The buffering message can also instruct the application to switch to a periodical
173 BUFFERING query instead to more precisely control the buffering process. The
174 application can, for example, choose to not act on the BUFFERING message with
175 100 percent fill level to resume playback but instead use the estimated download
176 time to resume playback to get uninterrupted playback.
177
178
179 Buffering Query
180 ~~~~~~~~~~~~~~~
181
182 In addition to the BUFFERING messages posted by the buffering elements we want
183 to be able to query the same information from the application. We also want to
184 be able to present the user with information about the downloaded range in the
185 file so that the GUI can react on it.
186
187 In addition to all the fields present in the buffering message, the BUFFERING
188 query contains the following field, which indicate the available downloaded
189 range in a specific format and the estimated time to complete:
190
191   "busy", G_TYPE_BOOLEAN
192     - if buffering was busy. This flag allows the application to pause the
193       pipeline by using the query only.
194
195   "format", GST_TYPE_FORMAT
196     - the format of the "start" and "stop" values below  
197    
198   "start", G_TYPE_INT64, -1 unknown
199     - the start position of the available data
200
201   "stop", G_TYPE_INT64, -1 unknown
202     - the stop position of the available data
203
204   "estimated-total", G_TYPE_INT64
205     - gives the estimated download time in milliseconds. -1 unknown.
206
207     When the size of the downloaded file is known, this value will contain
208     the latest estimate of the remaining download time. This value is usualy
209     only filled for the "download" buffering mode. The application can use
210     this information to estimate the amount of remaining time to download the
211     complete file.
212
213   "buffering-ranges", G_TYPE_ARRAY of GstQueryBufferingRange
214     - contains optionally the downloaded areas in the format given above. One
215       of the ranges contains the same start/stop position as above.
216
217       typedef struct
218       {
219         gint64 start;
220         gint64 stop;
221       } GstQueryBufferingRange;
222
223
224 For the "download" and "timeshift" buffering-modes, the start and stop positions
225 specify the ranges where efficient seeking in the downloaded media is possible.
226 Seeking outside of these ranges might be slow or not at all possible.
227
228 For the "stream" and "live" mode the start and stop values describe the oldest
229 and newest item (expressed in "format") in the buffer. 
230
231
232 Defaults
233 ~~~~~~~~
234
235 Some defaults for common elements:
236
237 A GstBaseSrc with random access replies to the BUFFERING query with:
238
239   "buffer-percent" = 100
240   "buffering-mode" = "stream"
241   "avg-in-rate" = -1
242   "avg-out-rate" = -1
243   "buffering-left" = 0
244   "format" = GST_FORMAT_BYTES
245   "start" = 0
246   "stop" = the total filesize
247   "estimated-total" = 0
248   "buffering-ranges" = NULL
249
250 A GstBaseSrc in push mode replies to the BUFFERING query with:
251
252   "buffer-percent" = 100
253   "buffering-mode" = "stream"
254   "avg-in-rate" = -1
255   "avg-out-rate" = -1
256   "buffering-left" = 0
257   "format" = a valid GST_TYPE_FORMAT
258   "start" = current position
259   "stop" = current position
260   "estimated-total" = -1
261   "buffering-ranges" = NULL
262
263
264 Buffering strategies
265 ~~~~~~~~~~~~~~~~~~~~
266
267  Buffering strategies are specific implementations based on the buffering
268  message and query described above.
269
270  Most strategies have to balance buffering time versus maximal playback
271  experience.
272
273  * simple buffering
274
275    NON-live pipelines are kept in the paused state while buffering messages with
276    a percent < 100% are received.
277
278    This buffering strategy relies on the buffer size and low/high watermarks of
279    the element. It can work with a fixed size buffer in memory or on disk.
280
281    The size of the buffer is usually expressed in a fixed amount of time units
282    and the estimated bitrate of the upstream source is used to convert this time
283    to bytes.
284
285    All GStreamer applications must implement this strategy. Failure to do so
286    will result in starvation at the sink.
287
288  * no-rebuffer strategy
289
290    This strategy tries to buffer as much data as possible so that playback can
291    continue without any further rebuffering.
292
293    This strategy is initially similar to simple buffering, the difference is in
294    deciding on the condition to continue playback. When a 100% buffering message
295    has been received, the application will not yet start the playback but it will
296    start a periodic buffering query, which will return the estimated amount of
297    buffering time left. When the estimated time left is less than the remaining
298    playback time, playback can continue.
299
300    This strategy requires a unlimited buffer size in memory or on disk, such as
301    provided by elements that implement the incremental download buffering mode.
302
303    Usually, the application can choose to start playback even before the
304    remaining buffer time elapsed in order to more quickly start the playback at
305    the expense of a possible rebuffering phase.
306
307  * Incremental rebuffering
308
309    The application implements the simple buffering strategy but with each
310    rebuffering phase, it increases the size of the buffer. 
311
312    This strategy has quick, fixed time startup times but incrementally longer
313    rebuffering times if the network is slower than the media bitrate.
314
315    
316
317
318
319
320