4 This document outlines the buffering policy used in the GStreamer
5 core that can be used by plugins and applications.
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
12 We want to be able to implement the following features:
14 - buffering up to a specifc 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 - easy (backward compatible) application notification of buffering
22 - the possibility for the application to do more complex buffering
28 +---------+ +---------+ +-------+
29 | httpsrc | | buffer | | demux |
30 | src - sink src - sink ....
31 +---------+ +---------+ +-------+
33 In this case we are reading from a slow network source into a buffer element
36 The buffer element has a low and high watermark expressed in bytes. The
37 buffer uses the watermarks as follows:
39 - The buffer element will post BUFFERING messages until the high watermark
40 is hit. This instructs the application to keep the pipeline PAUSED, which
41 will eventually block the srcpad from pushing while data is prerolled in
43 - When the high watermark is hit, a BUFFERING message with 100% will be
44 posted, which instructs the application to continue playback.
45 - When during playback, the low watermark is hit, the queue will start posting
46 BUFFERING messages again, making the application PAUSE the pipeline again
47 until the high watermark is hit again.
48 - during playback, the queue level will fluctuate between the high and the
49 low watermark as a way to compensate for network irregularities.
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.
56 * Incremental download
58 +---------+ +---------+ +-------+
59 | httpsrc | | buffer | | demux |
60 | src - sink src - sink ....
61 +---------+ +----|----+ +-------+
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
70 This mode is only suitable when the client can determine the length of the
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.
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-interupted playback experience.
86 +---------+ +---------+ +-------+
87 | httpsrc | | buffer | | demux |
88 | src - sink src - sink ....
89 +---------+ +----|----+ +-------+
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.
97 This mode is suitable for all live streams.
99 As with the incremental download mode, buffering messages are emited along
100 with an indication that timeshifting download is in progress.
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).
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.
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.
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
126 "buffer-percent", G_TYPE_INT, between 0 and 100
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:
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.
138 "avg-in-rate", G_TYPE_INT
139 - gives the average input buffering speed in bytes/second. -1 is unknown.
141 This is the average number of bytes per second that is received on the
142 buffering element input (sink) pads. It is a measurement of the network
145 "avg-out-rate", G_TYPE_INT
146 - gives the average consumption speed in bytes/second. -1 is unknown.
148 This is the average number of bytes per second that is consumed by the
149 downstream element of the buffering element.
151 "buffering-left", G_TYPE_INT64
152 - gives the estimated time that bufferring will take in milliseconds.
155 This is measured based on the avg-in-rate and the filled level of the
156 queue. The application can use this hint to update the GUI about the
157 estimated remaining time that buffering will take.
162 While data is buffered, the pipeline should remain in the PAUSED state. It is
163 also possible that more data should be buffered while the pipeline is PLAYING,
164 in which case the pipeline should be PAUSED until the buffering finished.
166 BUFFERING messages can be posted while the pipeline is prerolling. The
167 application should not set the pipeline to PLAYING before a BUFFERING message
168 with 100 percent value is received, which might only happen after the pipeline
171 An exception is made for live pipelines. The application may not change
172 the state of a live pipeline when a buffering message is received. Usually these
173 buffering messages contain the "buffering-mode" = "live".
175 The buffering message can also instruct the application to switch to a periodical
176 BUFFERING query instead to more precisely control the buffering process. The
177 application can, for example, choose to not act on the BUFFERING message with
178 100 percent fill level to resume playback but instead use the estimated download
179 time to resume playback to get uninterrupted playback.
185 In addition to the BUFFERING messages posted by the buffering elements we want
186 to be able to query the same information from the application. We also want to
187 be able to present the user with information about the downloaded range in the
188 file so that the GUI can react on it.
190 In addition to all the fields present in the buffering message, the BUFFERING
191 query contains the following field, which indicate the available downloaded
192 range in a specific format and the estimated time to complete:
194 "format", GST_TYPE_FORMAT
195 - the format of the "start" and "stop" values below
197 "start", G_TYPE_INT64, -1 unknown
198 - the start position of the available data
200 "stop", G_TYPE_INT64, -1 unknown
201 - the stop position of the available data
203 "estimated-total", G_TYPE_INT64
204 - gives the estimated download time in milliseconds. -1 unknown.
206 When the size of the downloaded file is known, this value will contain
207 the latest estimate of the remaining download time. This value is usualy
208 only filled for the "download" buffering mode. The application can use
209 this information to estimate the amount of remaining time to download the
212 For the "download" and "timeshift" buffering-modes, the start and stop positions
213 specify the ranges where efficient seeking in the downloaded media is possible.
214 Seeking outside of these ranges might be slow or not at all possible.
216 For the "stream" and "live" mode the start and stop values describe the oldest
217 and newest item (expressed in "format") in the buffer.
223 Some defaults for common elements:
225 A GstBaseSrc with random access replies to the BUFFERING query with:
227 "buffer-percent" = 100
228 "buffering-mode" = "stream"
232 "format" = GST_FORMAT_BYTES
234 "stop" = the total filesize
237 A GstBaseSrc in push mode replies to the BUFFERING query with:
239 "buffer-percent" = 100
240 "buffering-mode" = "stream"
244 "format" = a valid GST_TYPE_FORMAT
245 "start" = current position
246 "stop" = current position