Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / media / filters / source_buffer_stream.h
blobdeec40970ba53217343f32302db684e628dff651
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // SourceBufferStream is a data structure that stores media Buffers in ranges.
6 // Buffers can be appended out of presentation order. Buffers are retrieved by
7 // seeking to the desired start point and calling GetNextBuffer(). Buffers are
8 // returned in sequential presentation order.
10 #ifndef MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
11 #define MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
13 #include <deque>
14 #include <list>
15 #include <string>
16 #include <utility>
17 #include <vector>
19 #include "base/basictypes.h"
20 #include "base/memory/ref_counted.h"
21 #include "media/base/audio_decoder_config.h"
22 #include "media/base/media_export.h"
23 #include "media/base/media_log.h"
24 #include "media/base/ranges.h"
25 #include "media/base/stream_parser_buffer.h"
26 #include "media/base/text_track_config.h"
27 #include "media/base/video_decoder_config.h"
29 namespace media {
31 class SourceBufferRange;
33 // See file-level comment for complete description.
34 class MEDIA_EXPORT SourceBufferStream {
35 public:
36 typedef StreamParser::BufferQueue BufferQueue;
37 typedef std::list<SourceBufferRange*> RangeList;
39 // Status returned by GetNextBuffer().
40 // kSuccess: Indicates that the next buffer was returned.
41 // kNeedBuffer: Indicates that we need more data before a buffer can be
42 // returned.
43 // kConfigChange: Indicates that the next buffer requires a config change.
44 enum Status {
45 kSuccess,
46 kNeedBuffer,
47 kConfigChange,
48 kEndOfStream
51 enum Type {
52 kAudio,
53 kVideo,
54 kText
57 SourceBufferStream(const AudioDecoderConfig& audio_config,
58 const scoped_refptr<MediaLog>& media_log,
59 bool splice_frames_enabled);
60 SourceBufferStream(const VideoDecoderConfig& video_config,
61 const scoped_refptr<MediaLog>& media_log,
62 bool splice_frames_enabled);
63 SourceBufferStream(const TextTrackConfig& text_config,
64 const scoped_refptr<MediaLog>& media_log,
65 bool splice_frames_enabled);
67 ~SourceBufferStream();
69 // Signals that the next buffers appended are part of a new media segment
70 // starting at |media_segment_start_time|.
71 // TODO(acolwell/wolenetz): This should be changed to a presentation
72 // timestamp. See http://crbug.com/402502
73 void OnNewMediaSegment(DecodeTimestamp media_segment_start_time);
75 // Add the |buffers| to the SourceBufferStream. Buffers within the queue are
76 // expected to be in order, but multiple calls to Append() may add buffers out
77 // of order or overlapping. Assumes all buffers within |buffers| are in
78 // presentation order and are non-overlapping.
79 // Returns true if Append() was successful, false if |buffers| are not added.
80 // TODO(vrk): Implement garbage collection. (crbug.com/125070)
81 bool Append(const BufferQueue& buffers);
83 // Removes buffers between |start| and |end| according to the steps
84 // in the "Coded Frame Removal Algorithm" in the Media Source
85 // Extensions Spec.
86 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
88 // |duration| is the current duration of the presentation. It is
89 // required by the computation outlined in the spec.
90 void Remove(base::TimeDelta start, base::TimeDelta end,
91 base::TimeDelta duration);
93 // Frees up space if the SourceBufferStream is taking up too much memory.
94 // |media_time| is current playback position.
95 bool GarbageCollectIfNeeded(DecodeTimestamp media_time,
96 size_t newDataSize);
98 // Changes the SourceBufferStream's state so that it will start returning
99 // buffers starting from the closest keyframe before |timestamp|.
100 void Seek(base::TimeDelta timestamp);
102 // Returns true if the SourceBufferStream has seeked to a time without
103 // buffered data and is waiting for more data to be appended.
104 bool IsSeekPending() const;
106 // Notifies the SourceBufferStream that the media duration has been changed to
107 // |duration| so it should drop any data past that point.
108 void OnSetDuration(base::TimeDelta duration);
110 // Fills |out_buffer| with a new buffer. Buffers are presented in order from
111 // the last call to Seek(), or starting with the first buffer appended if
112 // Seek() has not been called yet.
113 // |out_buffer|'s timestamp may be earlier than the |timestamp| passed to
114 // the last Seek() call.
115 // Returns kSuccess if |out_buffer| is filled with a valid buffer, kNeedBuffer
116 // if there is not enough data buffered to fulfill the request, and
117 // kConfigChange if the next buffer requires a config change.
118 Status GetNextBuffer(scoped_refptr<StreamParserBuffer>* out_buffer);
120 // Returns a list of the buffered time ranges.
121 Ranges<base::TimeDelta> GetBufferedTime() const;
123 // Returns the duration of the buffered ranges, which is equivalent
124 // to the end timestamp of the last buffered range. If no data is buffered
125 // then base::TimeDelta() is returned.
126 base::TimeDelta GetBufferedDuration() const;
128 // Returns the size of the buffered data in bytes.
129 size_t GetBufferedSize() const;
131 // Notifies this object that end of stream has been signalled.
132 void MarkEndOfStream();
134 // Clear the end of stream state set by MarkEndOfStream().
135 void UnmarkEndOfStream();
137 const AudioDecoderConfig& GetCurrentAudioDecoderConfig();
138 const VideoDecoderConfig& GetCurrentVideoDecoderConfig();
139 const TextTrackConfig& GetCurrentTextTrackConfig();
141 // Notifies this object that the audio config has changed and buffers in
142 // future Append() calls should be associated with this new config.
143 bool UpdateAudioConfig(const AudioDecoderConfig& config);
145 // Notifies this object that the video config has changed and buffers in
146 // future Append() calls should be associated with this new config.
147 bool UpdateVideoConfig(const VideoDecoderConfig& config);
149 // Returns the largest distance between two adjacent buffers in this stream,
150 // or an estimate if no two adjacent buffers have been appended to the stream
151 // yet.
152 base::TimeDelta GetMaxInterbufferDistance() const;
154 void set_memory_limit(size_t memory_limit) {
155 memory_limit_ = memory_limit;
158 private:
159 friend class SourceBufferStreamTest;
161 // Attempts to delete approximately |total_bytes_to_free| amount of data
162 // |ranges_|, starting at the front of |ranges_| and moving linearly forward
163 // through the buffers. Deletes starting from the back if |reverse_direction|
164 // is true. |media_time| is current playback position.
165 // Returns the number of bytes freed.
166 size_t FreeBuffers(size_t total_bytes_to_free,
167 DecodeTimestamp media_time,
168 bool reverse_direction);
170 // Attempts to delete approximately |total_bytes_to_free| amount of data from
171 // |ranges_|, starting after the last appended buffer before the current
172 // playback position |media_time|.
173 size_t FreeBuffersAfterLastAppended(size_t total_bytes_to_free,
174 DecodeTimestamp media_time);
176 // Gets the removal range to secure |byte_to_free| from
177 // [|start_timestamp|, |end_timestamp|).
178 // Returns the size of buffers to secure if future
179 // Remove(|start_timestamp|, |removal_end_timestamp|, duration) is called.
180 // Will not update |removal_end_timestamp| if the returned size is 0.
181 size_t GetRemovalRange(DecodeTimestamp start_timestamp,
182 DecodeTimestamp end_timestamp, size_t byte_to_free,
183 DecodeTimestamp* removal_end_timestamp);
185 // Prepares |range_for_next_append_| so |new_buffers| can be appended.
186 // This involves removing buffers between the end of the previous append
187 // and any buffers covered by the time range in |new_buffers|.
188 // |deleted_buffers| is an output parameter containing candidates for
189 // |track_buffer_| if this method ends up removing the current playback
190 // position from the range.
191 void PrepareRangesForNextAppend(const BufferQueue& new_buffers,
192 BufferQueue* deleted_buffers);
194 // Removes buffers, from the |track_buffer_|, that come after |timestamp|.
195 void PruneTrackBuffer(const DecodeTimestamp timestamp);
197 // Checks to see if |range_with_new_buffers_itr| can be merged with the range
198 // next to it, and merges them if so.
199 void MergeWithAdjacentRangeIfNecessary(
200 const RangeList::iterator& range_with_new_buffers_itr);
202 // Returns true if |second_timestamp| is the timestamp of the next buffer in
203 // sequence after |first_timestamp|, false otherwise.
204 bool AreAdjacentInSequence(
205 DecodeTimestamp first_timestamp, DecodeTimestamp second_timestamp) const;
207 // Helper method that returns the timestamp for the next buffer that
208 // |selected_range_| will return from GetNextBuffer() call, or kNoTimestamp()
209 // if in between seeking (i.e. |selected_range_| is null).
210 DecodeTimestamp GetNextBufferTimestamp();
212 // Finds the range that should contain a media segment that begins with
213 // |start_timestamp| and returns the iterator pointing to it. Returns
214 // |ranges_.end()| if there's no such existing range.
215 RangeList::iterator FindExistingRangeFor(DecodeTimestamp start_timestamp);
217 // Inserts |new_range| into |ranges_| preserving sorted order. Returns an
218 // iterator in |ranges_| that points to |new_range|.
219 RangeList::iterator AddToRanges(SourceBufferRange* new_range);
221 // Returns an iterator that points to the place in |ranges_| where
222 // |selected_range_| lives.
223 RangeList::iterator GetSelectedRangeItr();
225 // Sets the |selected_range_| to |range| and resets the next buffer position
226 // for the previous |selected_range_|.
227 void SetSelectedRange(SourceBufferRange* range);
229 // Seeks |range| to |seek_timestamp| and then calls SetSelectedRange() with
230 // |range|.
231 void SeekAndSetSelectedRange(SourceBufferRange* range,
232 DecodeTimestamp seek_timestamp);
234 // Resets this stream back to an unseeked state.
235 void ResetSeekState();
237 // Returns true if |seek_timestamp| refers to the beginning of the first range
238 // in |ranges_|, false otherwise or if |ranges_| is empty.
239 bool ShouldSeekToStartOfBuffered(base::TimeDelta seek_timestamp) const;
241 // Returns true if the timestamps of |buffers| are monotonically increasing
242 // since the previous append to the media segment, false otherwise.
243 bool IsMonotonicallyIncreasing(const BufferQueue& buffers) const;
245 // Returns true if |next_timestamp| and |next_is_keyframe| are valid for
246 // the first buffer after the previous append.
247 bool IsNextTimestampValid(DecodeTimestamp next_timestamp,
248 bool next_is_keyframe) const;
250 // Returns true if |selected_range_| is the only range in |ranges_| that
251 // HasNextBufferPosition().
252 bool OnlySelectedRangeIsSeeked() const;
254 // Measures the distances between buffer timestamps and tracks the max.
255 void UpdateMaxInterbufferDistance(const BufferQueue& buffers);
257 // Sets the config ID for each buffer to |append_config_index_|.
258 void SetConfigIds(const BufferQueue& buffers);
260 // Called to complete a config change. Updates |current_config_index_| to
261 // match the index of the next buffer. Calling this method causes
262 // GetNextBuffer() to stop returning kConfigChange and start returning
263 // kSuccess.
264 void CompleteConfigChange();
266 // Sets |selected_range_| and seeks to the nearest keyframe after
267 // |timestamp| if necessary and possible. This method only attempts to
268 // set |selected_range_| if |seleted_range_| is null and |track_buffer_|
269 // is empty.
270 void SetSelectedRangeIfNeeded(const DecodeTimestamp timestamp);
272 // Find a keyframe timestamp that is >= |start_timestamp| and can be used to
273 // find a new selected range.
274 // Returns kNoTimestamp() if an appropriate keyframe timestamp could not be
275 // found.
276 DecodeTimestamp FindNewSelectedRangeSeekTimestamp(
277 const DecodeTimestamp start_timestamp);
279 // Searches |ranges_| for the first keyframe timestamp that is >= |timestamp|.
280 // If |ranges_| doesn't contain a GOP that covers |timestamp| or doesn't
281 // have a keyframe after |timestamp| then kNoTimestamp() is returned.
282 DecodeTimestamp FindKeyframeAfterTimestamp(const DecodeTimestamp timestamp);
284 // Returns "VIDEO" for a video SourceBufferStream, "AUDIO" for an audio
285 // stream, and "TEXT" for a text stream.
286 std::string GetStreamTypeName() const;
288 // Returns true if end of stream has been reached, i.e. the
289 // following conditions are met:
290 // 1. end of stream is marked and there is nothing in the track_buffer.
291 // 2. We don't have any ranges, or the last or no range is selected,
292 // or there is a pending seek beyond any existing ranges.
293 bool IsEndOfStreamReached() const;
295 // Deletes the range pointed to by |*itr| and removes it from |ranges_|.
296 // If |*itr| points to |selected_range_|, then |selected_range_| is set to
297 // NULL. After the range is removed, |*itr| is to the range after the one that
298 // was removed or to |ranges_.end()| if the last range was removed.
299 void DeleteAndRemoveRange(RangeList::iterator* itr);
301 // Helper function used by Remove() and PrepareRangesForNextAppend() to
302 // remove buffers and ranges between |start| and |end|.
303 // |exclude_start| - If set to true, buffers with timestamps that
304 // match |start| are not removed. If set to false, buffers with
305 // timestamps that match |start| will be removed.
306 // |*deleted_buffers| - Filled with buffers for the current playback position
307 // if the removal range included the current playback position. These buffers
308 // can be used as candidates for placing in the |track_buffer_|.
309 void RemoveInternal(DecodeTimestamp start,
310 DecodeTimestamp end,
311 bool exclude_start,
312 BufferQueue* deleted_buffers);
314 Type GetType() const;
316 // See GetNextBuffer() for additional details. This method handles splice
317 // frame processing.
318 Status HandleNextBufferWithSplice(
319 scoped_refptr<StreamParserBuffer>* out_buffer);
321 // See GetNextBuffer() for additional details. This method handles preroll
322 // frame processing.
323 Status HandleNextBufferWithPreroll(
324 scoped_refptr<StreamParserBuffer>* out_buffer);
326 // See GetNextBuffer() for additional details. The internal method hands out
327 // single buffers from the |track_buffer_| and |selected_range_| without
328 // additional processing for splice frame or preroll buffers.
329 Status GetNextBufferInternal(scoped_refptr<StreamParserBuffer>* out_buffer);
331 // If the next buffer's timestamp is significantly beyond the last output
332 // buffer, and if we just exhausted |track_buffer_| on the previous read, this
333 // method logs a warning to |media_log_| that there could be perceivable
334 // delay. Apps can avoid this behavior by not overlap-appending buffers near
335 // current playback position.
336 void WarnIfTrackBufferExhaustionSkipsForward(
337 const scoped_refptr<StreamParserBuffer>& next_buffer);
339 // Called by PrepareRangesForNextAppend() before pruning overlapped buffers to
340 // generate a splice frame with a small portion of the overlapped buffers. If
341 // a splice frame is generated, the first buffer in |new_buffers| will have
342 // its timestamps, duration, and fade out preroll updated.
343 void GenerateSpliceFrame(const BufferQueue& new_buffers);
345 // If |out_buffer| has splice buffers or preroll, sets |pending_buffer_|
346 // appropriately and returns true. Otherwise returns false.
347 bool SetPendingBuffer(scoped_refptr<StreamParserBuffer>* out_buffer);
349 // Used to report log messages that can help the web developer figure out what
350 // is wrong with the content.
351 scoped_refptr<MediaLog> media_log_;
353 // List of disjoint buffered ranges, ordered by start time.
354 RangeList ranges_;
356 // Indicates which decoder config is being used by the decoder.
357 // GetNextBuffer() is only allows to return buffers that have a
358 // config ID that matches this index. If there is a mismatch then
359 // it must signal that a config change is needed.
360 int current_config_index_ = 0;
362 // Indicates which decoder config to associate with new buffers
363 // being appended. Each new buffer appended has its config ID set
364 // to the value of this field.
365 int append_config_index_ = 0;
367 // Holds the audio/video configs for this stream. |current_config_index_|
368 // and |append_config_index_| represent indexes into one of these vectors.
369 std::vector<AudioDecoderConfig> audio_configs_;
370 std::vector<VideoDecoderConfig> video_configs_;
372 // Holds the text config for this stream.
373 TextTrackConfig text_track_config_;
375 // True if more data needs to be appended before the Seek() can complete,
376 // false if no Seek() has been requested or the Seek() is completed.
377 bool seek_pending_ = false;
379 // True if the end of the stream has been signalled.
380 bool end_of_stream_ = false;
382 // Timestamp of the last request to Seek().
383 base::TimeDelta seek_buffer_timestamp_;
385 // Pointer to the seeked-to Range. This is the range from which
386 // GetNextBuffer() calls are fulfilled after the |track_buffer_| has been
387 // emptied.
388 SourceBufferRange* selected_range_ = nullptr;
390 // Queue of the next buffers to be returned from calls to GetNextBuffer(). If
391 // |track_buffer_| is empty, return buffers from |selected_range_|.
392 BufferQueue track_buffer_;
394 // If there has been no intervening Seek, this will be true if the last
395 // emitted buffer emptied |track_buffer_|.
396 bool just_exhausted_track_buffer_ = false;
398 // The start time of the current media segment being appended.
399 DecodeTimestamp media_segment_start_time_;
401 // Points to the range containing the current media segment being appended.
402 RangeList::iterator range_for_next_append_;
404 // True when the next call to Append() begins a new media segment.
405 bool new_media_segment_ = false;
407 // The timestamp of the last buffer appended to the media segment, set to
408 // kNoDecodeTimestamp() if the beginning of the segment.
409 DecodeTimestamp last_appended_buffer_timestamp_;
410 bool last_appended_buffer_is_keyframe_ = false;
412 // The decode timestamp on the last buffer returned by the most recent
413 // GetNextBuffer() call. Set to kNoDecodeTimestamp() if GetNextBuffer() hasn't
414 // been called yet or a seek has happened since the last GetNextBuffer() call.
415 DecodeTimestamp last_output_buffer_timestamp_;
417 // Stores the largest distance between two adjacent buffers in this stream.
418 base::TimeDelta max_interbuffer_distance_;
420 // The maximum amount of data in bytes the stream will keep in memory.
421 size_t memory_limit_;
423 // Indicates that a kConfigChanged status has been reported by GetNextBuffer()
424 // and GetCurrentXXXDecoderConfig() must be called to update the current
425 // config. GetNextBuffer() must not be called again until
426 // GetCurrentXXXDecoderConfig() has been called.
427 bool config_change_pending_ = false;
429 // Used by HandleNextBufferWithSplice() or HandleNextBufferWithPreroll() when
430 // a splice frame buffer or buffer with preroll is returned from
431 // GetNextBufferInternal().
432 scoped_refptr<StreamParserBuffer> pending_buffer_;
434 // Indicates which of the splice buffers in |splice_buffer_| should be
435 // handled out next.
436 size_t splice_buffers_index_ = 0;
438 // Indicates that all buffers before |pending_buffer_| have been handed out.
439 bool pending_buffers_complete_ = false;
441 // Indicates that splice frame generation is enabled.
442 const bool splice_frames_enabled_;
444 // To prevent log spam, count the number of warnings and successes logged.
445 int num_splice_generation_warning_logs_ = 0;
446 int num_splice_generation_success_logs_ = 0;
447 int num_track_buffer_gap_warning_logs_ = 0;
448 int num_garbage_collect_algorithm_logs_ = 0;
450 DISALLOW_COPY_AND_ASSIGN(SourceBufferStream);
453 } // namespace media
455 #endif // MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_