1 // Copyright 2012 The Chromium Authors
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 order to feed decoder, generally near presentation
9 // order though not necessarily the same as presentation order within GOPs of
10 // out-of-order codecs.
12 #ifndef MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
13 #define MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
20 #include <type_traits>
24 #include "base/memory/memory_pressure_listener.h"
25 #include "base/memory/raw_ptr.h"
26 #include "base/memory/scoped_refptr.h"
27 #include "base/time/time.h"
28 #include "media/base/audio_decoder_config.h"
29 #include "media/base/media_export.h"
30 #include "media/base/media_log.h"
31 #include "media/base/ranges.h"
32 #include "media/base/stream_parser_buffer.h"
33 #include "media/base/video_decoder_config.h"
34 #include "media/filters/source_buffer_range.h"
38 // Status returned by GetNextBuffer().
39 // kSuccess: Indicates that the next buffer was returned.
40 // kNeedBuffer: Indicates that we need more data before a buffer can be
42 // kConfigChange: Indicates that the next buffer requires a config change.
43 enum class SourceBufferStreamStatus {
50 enum class SourceBufferStreamType { kAudio, kVideo };
52 // See file-level comment for complete description.
53 class MEDIA_EXPORT SourceBufferStream {
55 using BufferQueue = StreamParser::BufferQueue;
56 using RangeList = std::list<std::unique_ptr<SourceBufferRange>>;
58 // Helper for PrepareRangesForNextAppend and BufferQueueToLogString that
59 // populates |start| and |end| with the presentation interval of |buffers|.
60 static void GetTimestampInterval(const BufferQueue& buffers,
61 base::TimeDelta* start,
62 base::TimeDelta* end);
64 SourceBufferStream(const AudioDecoderConfig& audio_config,
66 SourceBufferStream(const VideoDecoderConfig& video_config,
69 SourceBufferStream(const SourceBufferStream&) = delete;
70 SourceBufferStream& operator=(const SourceBufferStream&) = delete;
72 ~SourceBufferStream();
74 // Signals that the next buffers appended are part of a new coded frame group
75 // starting at |coded_frame_group_start_pts|.
76 void OnStartOfCodedFrameGroup(base::TimeDelta coded_frame_group_start_pts);
78 // Add the |buffers| to the SourceBufferStream. Buffers within the queue are
79 // expected to be in order, but multiple calls to Append() may add buffers out
80 // of order or overlapping. Assumes all buffers within |buffers| are in
81 // presentation order and are non-overlapping.
82 void Append(const BufferQueue& buffers);
84 // Removes buffers between |start| and |end| according to the steps
85 // in the "Coded Frame Removal Algorithm" in the Media Source
87 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
89 // |duration| is the current duration of the presentation. It is
90 // required by the computation outlined in the spec.
91 void Remove(base::TimeDelta start, base::TimeDelta end,
92 base::TimeDelta duration);
94 // Frees up space if the SourceBufferStream is taking up too much memory.
95 // |media_time| is current playback position.
96 bool GarbageCollectIfNeeded(base::TimeDelta media_time, size_t newDataSize);
98 // Gets invoked when the system is experiencing memory pressure, i.e. there's
99 // not enough free memory. The |media_time| is the media playback position at
100 // the time of memory pressure notification (needed for accurate GC). The
101 // |memory_pressure_level| indicates memory pressure severity. The
102 // |force_instant_gc| is used to force the MSE garbage collection algorithm to
103 // be run right away, without waiting for the next append.
104 void OnMemoryPressure(
105 base::TimeDelta media_time,
106 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level,
107 bool force_instant_gc);
109 // Changes the SourceBufferStream's state so that it will start returning
110 // buffers starting from the closest keyframe before |timestamp|.
111 void Seek(base::TimeDelta timestamp);
113 // Returns true if the SourceBufferStream has seeked to a time without
114 // buffered data and is waiting for more data to be appended.
115 bool IsSeekPending() const;
117 // Notifies the SourceBufferStream that the media duration has been changed to
118 // |duration| so it should drop any data past that point.
119 void OnSetDuration(base::TimeDelta duration);
121 // Fills |out_buffer| with a new buffer. Buffers are presented in order from
122 // the last call to Seek(), or starting with the first buffer appended if
123 // Seek() has not been called yet.
124 // |out_buffer|'s timestamp may be earlier than the |timestamp| passed to
125 // the last Seek() call.
126 // Returns kSuccess if |out_buffer| is filled with a valid buffer, kNeedBuffer
127 // if there is not enough data buffered to fulfill the request, and
128 // kConfigChange if the next buffer requires a config change.
129 SourceBufferStreamStatus GetNextBuffer(
130 scoped_refptr<StreamParserBuffer>* out_buffer);
132 // Returns a list of the buffered time ranges.
133 Ranges<base::TimeDelta> GetBufferedTime() const;
135 // Returns the lowest buffered PTS or base::TimeDelta() if nothing is
137 base::TimeDelta GetLowestPresentationTimestamp() const;
139 // Returns the highest buffered PTS or base::TimeDelta() if nothing is
141 base::TimeDelta GetHighestPresentationTimestamp() const;
143 // Returns the duration of the buffered ranges, which is equivalent
144 // to the end timestamp of the last buffered range. If no data is buffered
145 // then base::TimeDelta() is returned.
146 base::TimeDelta GetBufferedDuration() const;
148 // Returns the size of the buffered data in bytes.
149 size_t GetBufferedSize() const;
151 // Notifies this object that end of stream has been signalled.
152 void MarkEndOfStream();
154 // Clear the end of stream state set by MarkEndOfStream().
155 void UnmarkEndOfStream();
157 const AudioDecoderConfig& GetCurrentAudioDecoderConfig();
158 const VideoDecoderConfig& GetCurrentVideoDecoderConfig();
160 // Notifies this object that the audio config has changed and buffers in
161 // future Append() calls should be associated with this new config.
162 // If the codec is allowed to change, the caller should set
163 // |allow_codec_change| to true.
164 bool UpdateAudioConfig(const AudioDecoderConfig& config,
165 bool allow_codec_change);
167 // Notifies this object that the video config has changed and buffers in
168 // future Append() calls should be associated with this new config.
169 // If the codec is allowed to change, the caller should set
170 // |allow_codec_change| to true.
171 bool UpdateVideoConfig(const VideoDecoderConfig& config,
172 bool allow_codec_change);
174 // Returns the largest distance between two adjacent buffers in this stream,
175 // or an estimate if no two adjacent buffers have been appended to the stream
177 base::TimeDelta GetMaxInterbufferDistance() const;
179 void set_memory_limit(size_t memory_limit) {
180 memory_limit_ = memory_limit;
183 // A helper function for detecting video/audio config change, so that we
184 // can "peek" the next buffer instead of dequeuing it directly from the source
185 // stream buffer queue.
186 bool IsNextBufferConfigChanged();
189 friend class SourceBufferStreamTest;
191 // Attempts to delete approximately |total_bytes_to_free| amount of data
192 // |ranges_|, starting at the front of |ranges_| and moving linearly forward
193 // through the buffers. Deletes starting from the back if |reverse_direction|
194 // is true. |media_time| is current playback position.
195 // Returns the number of bytes freed.
196 size_t FreeBuffers(size_t total_bytes_to_free,
197 base::TimeDelta media_time,
198 bool reverse_direction);
200 // Attempts to delete approximately |total_bytes_to_free| amount of data from
201 // |ranges_|, starting after the last appended media
202 // (|highest_buffered_end_time_in_append_sequence_|) but before the current
203 // playback position |media_time|.
204 size_t FreeBuffersAfterLastAppended(size_t total_bytes_to_free,
205 base::TimeDelta media_time);
207 // Gets the removal range to secure |total_bytes_to_free| from
208 // [|start_timestamp|, |end_timestamp|).
209 // Returns the size of buffers to secure if future
210 // Remove(|start_timestamp|, |removal_end_timestamp|, duration) is called.
211 // Will not update |removal_end_timestamp| if the returned size is 0.
212 size_t GetRemovalRange(base::TimeDelta start_timestamp,
213 base::TimeDelta end_timestamp,
214 size_t total_bytes_to_free,
215 base::TimeDelta* removal_end_timestamp);
217 // Prepares |range_for_next_append_| so |new_buffers| can be appended.
218 // This involves removing buffers between the end of the previous append
219 // and any buffers covered by the time range in |new_buffers|.
220 // |deleted_buffers| is an output parameter containing candidates for
221 // |track_buffer_| if this method ends up removing the current playback
222 // position from the range.
223 void PrepareRangesForNextAppend(const BufferQueue& new_buffers,
224 BufferQueue* deleted_buffers);
226 // Removes buffers, from the |track_buffer_|, that come after |timestamp|.
227 // Due to out-of-order decode versus presentation times for some kinds of
228 // media, |timestamp| should be the time of a keyframe known by the caller.
229 // |timestamp| must not be kNoTimestamp.
230 void PruneTrackBuffer(const base::TimeDelta timestamp);
232 // Checks to see if |range_with_new_buffers_itr| can be merged with the range
233 // next to it, and merges them if so while preserving correctness of
234 // |range_for_next_append_| and |selected_range_|.
235 void MergeWithNextRangeIfNecessary(
236 const RangeList::iterator& range_with_new_buffers_itr);
238 // Merges any adjacent ranges while preserving correctness of
239 // |range_for_next_append_| and |selected_range_|.
240 void MergeAllAdjacentRanges();
242 // Returns true if |next_gop_timestamp| follows
243 // |highest_timestamp_in_append_sequence_| within fudge room.
244 bool IsNextGopAdjacentToEndOfCurrentAppendSequence(
245 base::TimeDelta next_gop_timestamp) const;
247 // Helper method that returns the timestamp for the next buffer that
248 // |selected_range_| will return from GetNextBuffer() call, or kNoTimestamp
249 // if in between seeking (i.e. |selected_range_| is null).
250 base::TimeDelta GetNextBufferTimestamp();
252 // Finds the range that should contain a coded frame group that begins with
253 // |start_timestamp| (presentation time) and returns the iterator pointing to
254 // it. Returns |ranges_.end()| if there's no such existing range.
255 RangeList::iterator FindExistingRangeFor(base::TimeDelta start_timestamp);
257 // Inserts |new_range| into |ranges_| preserving sorted order. Returns an
258 // iterator in |ranges_| that points to |new_range|. |new_range| becomes owned
260 RangeList::iterator AddToRanges(std::unique_ptr<SourceBufferRange> new_range);
262 // Sets the |selected_range_| to |range| and resets the next buffer position
263 // for the previous |selected_range_|.
264 void SetSelectedRange(SourceBufferRange* range);
266 // Seeks |range| to |seek_timestamp| and then calls SetSelectedRange() with
268 void SeekAndSetSelectedRange(SourceBufferRange* range,
269 base::TimeDelta seek_timestamp);
271 // Resets this stream back to an unseeked state.
272 void ResetSeekState();
274 // Reset state tracking various metadata about the last appended buffer.
275 void ResetLastAppendedState();
277 // Returns true if |seek_timestamp| refers to the beginning of the first range
278 // in |ranges_|, false otherwise or if |ranges_| is empty.
279 bool ShouldSeekToStartOfBuffered(base::TimeDelta seek_timestamp) const;
281 // Returns true if the decode timestamps of |buffers| are monotonically
282 // increasing (within each GOP) since the previous append to the coded frame
283 // group, false otherwise.
284 bool IsDtsMonotonicallyIncreasing(const BufferQueue& buffers);
286 // Returns true if |selected_range_| is the only range in |ranges_| that
287 // HasNextBufferPosition().
288 bool OnlySelectedRangeIsSeeked() const;
290 // Measures the distances between buffer decode timestamps and tracks the max.
291 // This enables a reasonable approximation of adjacency fudge room, even for
292 // out-of-order PTS vs DTS sequences. Returns true if
293 // |max_interbuffer_distance_| was changed.
294 bool UpdateMaxInterbufferDtsDistance(const BufferQueue& buffers);
296 // Sets the config ID for each buffer to |append_config_index_|.
297 void SetConfigIds(const BufferQueue& buffers);
299 // Called to complete a config change. Updates |current_config_index_| to
300 // match the index of the next buffer. Calling this method causes
301 // GetNextBuffer() to stop returning kConfigChange and start returning
303 void CompleteConfigChange();
305 // Sets |selected_range_| and seeks to the nearest keyframe after
306 // |timestamp| if necessary and possible. This method only attempts to
307 // set |selected_range_| if |seleted_range_| is null and |track_buffer_|
309 void SetSelectedRangeIfNeeded(const base::TimeDelta timestamp);
311 // Find a keyframe timestamp that is >= |start_timestamp| and can be used to
312 // find a new selected range.
313 // Returns kNoTimestamp if an appropriate keyframe timestamp could not be
315 base::TimeDelta FindNewSelectedRangeSeekTimestamp(
316 const base::TimeDelta start_timestamp);
318 // Searches |ranges_| for the first keyframe timestamp that is >= |timestamp|.
319 // If |ranges_| doesn't contain a GOP that covers |timestamp| or doesn't
320 // have a keyframe after |timestamp| then kNoTimestamp is returned.
321 base::TimeDelta FindKeyframeAfterTimestamp(const base::TimeDelta timestamp);
323 // Returns "VIDEO" for a video SourceBufferStream and "AUDIO" for an audio
325 std::string GetStreamTypeName() const;
327 // (Audio only) If |new_buffers| overlap existing buffers, trims end of
328 // existing buffers to remove overlap. |new_buffers| are not modified.
329 void TrimSpliceOverlap(const BufferQueue& new_buffers);
331 // Returns true if end of stream has been reached, i.e. the
332 // following conditions are met:
333 // 1. end of stream is marked and there is nothing in the track_buffer.
334 // 2. We don't have any ranges, or the last or no range is selected,
335 // or there is a pending seek beyond any existing ranges.
336 bool IsEndOfStreamReached() const;
338 // Deletes the range pointed to by |*itr| and removes it from |ranges_|.
339 // If |*itr| points to |selected_range_|, then |selected_range_| is set to
340 // NULL. After the range is removed, |*itr| is to the range after the one that
341 // was removed or to |ranges_.end()| if the last range was removed.
342 void DeleteAndRemoveRange(RangeList::iterator* itr);
344 // Helper function used when updating |range_for_next_append_|. Returns a
345 // guess of what the next append timestamp will be based on
346 // |last_appended_buffer_timestamp_|, |new_coded_frame_group_| and
347 // |coded_frame_group_start_pts_|. Returns kNoTimestamp if unable to guess,
348 // which can occur prior to first OnStartOfCodedFrameGroup(), or when the most
349 // recent GOP appended to since the last OnStartOfCodedFrameGroup() is
351 base::TimeDelta PotentialNextAppendTimestamp() const;
353 // Helper function used by Remove() and PrepareRangesForNextAppend() to
354 // remove buffers and ranges between |start| and |end|.
355 // |exclude_start| - If set to true, buffers with timestamps that
356 // match |start| are not removed. If set to false, buffers with
357 // timestamps that match |start| will be removed.
358 // |*deleted_buffers| - Filled with buffers for the current playback position
359 // if the removal range included the current playback position. These buffers
360 // can be used as candidates for placing in the |track_buffer_|.
361 void RemoveInternal(base::TimeDelta start,
364 BufferQueue* deleted_buffers);
366 // Helper function used by RemoveInternal() to evaluate whether remove will
367 // disrupt the last appended GOP. If disruption is expected, reset state
368 // tracking the last append. This will trigger frame filtering in Append()
369 // until a new key frame is provided.
370 void UpdateLastAppendStateForRemove(base::TimeDelta remove_start,
371 base::TimeDelta remove_end,
374 SourceBufferStreamType GetType() const;
376 // See GetNextBuffer() for additional details. This method handles preroll
378 SourceBufferStreamStatus HandleNextBufferWithPreroll(
379 scoped_refptr<StreamParserBuffer>* out_buffer);
381 // See GetNextBuffer() for additional details. The internal method hands out
382 // single buffers from the |track_buffer_| and |selected_range_| without
383 // additional processing for preroll buffers.
384 SourceBufferStreamStatus GetNextBufferInternal(
385 scoped_refptr<StreamParserBuffer>* out_buffer);
387 // If the next buffer's timestamp is significantly beyond the last output
388 // buffer, and if we just exhausted |track_buffer_| on the previous read, this
389 // method logs a warning to |media_log_| that there could be perceivable
390 // delay. Apps can avoid this behavior by not overlap-appending buffers near
391 // current playback position.
392 void WarnIfTrackBufferExhaustionSkipsForward(
393 scoped_refptr<StreamParserBuffer> next_buffer);
395 // If |out_buffer| has preroll, sets |pending_buffer_| to feed out preroll and
396 // returns true. Otherwise returns false.
397 bool SetPendingBuffer(scoped_refptr<StreamParserBuffer>* out_buffer);
399 // Used to report log messages that can help the web developer figure out what
400 // is wrong with the content.
401 raw_ptr<MediaLog> media_log_;
403 // List of disjoint buffered ranges, ordered by start time.
406 // Indicates which decoder config is being used by the decoder.
407 // GetNextBuffer() is only allows to return buffers that have a
408 // config ID that matches this index. If there is a mismatch then
409 // it must signal that a config change is needed.
410 int current_config_index_ = 0;
412 // Indicates which decoder config to associate with new buffers
413 // being appended. Each new buffer appended has its config ID set
414 // to the value of this field.
415 int append_config_index_ = 0;
417 // Holds the audio/video configs for this stream. |current_config_index_|
418 // and |append_config_index_| represent indexes into one of these vectors.
419 std::vector<AudioDecoderConfig> audio_configs_;
420 std::vector<VideoDecoderConfig> video_configs_;
422 // True if more data needs to be appended before the Seek() can complete,
423 // false if no Seek() has been requested or the Seek() is completed.
424 bool seek_pending_ = false;
426 // True if the end of the stream has been signalled.
427 bool end_of_stream_ = false;
429 // Timestamp of the last request to Seek().
430 base::TimeDelta seek_buffer_timestamp_;
432 // Pointer to the seeked-to Range. This is the range from which
433 // GetNextBuffer() calls are fulfilled after the |track_buffer_| has been
435 raw_ptr<SourceBufferRange> selected_range_ = nullptr;
437 // Queue of the next buffers to be returned from calls to GetNextBuffer(). If
438 // |track_buffer_| is empty, return buffers from |selected_range_|.
439 BufferQueue track_buffer_;
441 // If there has been no intervening Seek, this will be true if the last
442 // emitted buffer emptied |track_buffer_|.
443 bool just_exhausted_track_buffer_ = false;
445 // The start presentation time of the current coded frame group being
447 base::TimeDelta coded_frame_group_start_pts_;
449 // Points to the range containing the current coded frame group being
451 RangeList::iterator range_for_next_append_;
453 // True when the next call to Append() begins a new coded frame group.
454 // TODO(wolenetz): Simplify by passing this flag into Append().
455 bool new_coded_frame_group_ = false;
457 // The timestamp of the last buffer appended to the coded frame group, set to
458 // kNoTimestamp if the beginning of the group.
459 base::TimeDelta last_appended_buffer_timestamp_ = kNoTimestamp;
460 base::TimeDelta last_appended_buffer_duration_ = kNoTimestamp;
461 bool last_appended_buffer_is_keyframe_ = false;
463 // When buffering GOPs by keyframe PTS and intra-gop by nonkeyframe DTS, to
464 // verify monotonically increasing intra-GOP DTS sequence and to update max
465 // interbuffer distance also by DTS deltas within a coded frame group, the
466 // following is needed.
467 DecodeTimestamp last_appended_buffer_decode_timestamp_ = kNoDecodeTimestamp;
469 // The following is the highest presentation timestamp appended so far in this
470 // coded frame group. Due to potentially out-of-order decode versus
471 // presentation time sequence, this isn't necessarily the most recently
472 // appended frame. This is used as the lower bound of removing previously
473 // buffered media when processing new appends.
474 base::TimeDelta highest_timestamp_in_append_sequence_ = kNoTimestamp;
476 // The following is used in determining if FreeBuffersAfterLastAppended() is
477 // allowed during garbage collection. Due to out-of-order decode versus
478 // presentation sequence, this isn't necessarily the end time of the most
479 // recently appended frame.
480 base::TimeDelta highest_buffered_end_time_in_append_sequence_ = kNoTimestamp;
482 // The highest presentation timestamp for buffers returned by recent
483 // GetNextBuffer() calls. Set to kNoTimestamp if GetNextBuffer() hasn't been
484 // called yet or a seek has happened since the last GetNextBuffer() call.
485 base::TimeDelta highest_output_buffer_timestamp_;
487 // Stores the largest distance between two adjacent buffers in this stream.
488 base::TimeDelta max_interbuffer_distance_;
490 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level_ =
491 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE;
493 // The maximum amount of data in bytes the stream will keep in memory.
494 // |memory_limit_| is initialized based on the audio/video configuration in
495 // the constructor, but either user-setting of |memory_limit_| or
496 // memory-pressure-based adjustment to determine effective limit in the
497 // eviction heuristic can cause the result to vary from the value set in
499 size_t memory_limit_;
501 // Indicates that a kConfigChanged status has been reported by GetNextBuffer()
502 // and GetCurrentXXXDecoderConfig() must be called to update the current
503 // config. GetNextBuffer() must not be called again until
504 // GetCurrentXXXDecoderConfig() has been called.
505 bool config_change_pending_ = false;
507 // Used by HandleNextBufferWithPreroll() when a buffer with preroll is
508 // returned from GetNextBufferInternal().
509 scoped_refptr<StreamParserBuffer> pending_buffer_;
511 // Indicates that all buffers before |pending_buffer_| have been handed out.
512 bool pending_buffers_complete_ = false;
514 // To prevent log spam, count the number of logs for different log scenarios.
515 int num_splice_logs_ = 0;
516 int num_track_buffer_gap_warning_logs_ = 0;
517 int num_garbage_collect_algorithm_logs_ = 0;
522 #endif // MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_