Re-enable index-basics-workers test to see if still times
[chromium-blink-merge.git] / media / filters / source_buffer_stream.h
blob07dd087dc77f9993e0d4a6df69dc9f54b7cfbca8
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/memory/ref_counted.h"
20 #include "media/base/audio_decoder_config.h"
21 #include "media/base/media_export.h"
22 #include "media/base/media_log.h"
23 #include "media/base/ranges.h"
24 #include "media/base/stream_parser_buffer.h"
25 #include "media/base/video_decoder_config.h"
27 namespace media {
29 class SourceBufferRange;
31 // See file-level comment for complete description.
32 class MEDIA_EXPORT SourceBufferStream {
33 public:
34 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
36 // Status returned by GetNextBuffer().
37 // kSuccess: Indicates that the next buffer was returned.
38 // kNeedBuffer: Indicates that we need more data before a buffer can be
39 // returned.
40 // kConfigChange: Indicates that the next buffer requires a config change.
41 enum Status {
42 kSuccess,
43 kNeedBuffer,
44 kConfigChange,
47 SourceBufferStream(const AudioDecoderConfig& audio_config,
48 const LogCB& log_cb);
49 SourceBufferStream(const VideoDecoderConfig& video_config,
50 const LogCB& log_cb);
52 ~SourceBufferStream();
54 // Signals that the next buffers appended are part of a new media segment
55 // starting at |media_segment_start_time|.
56 void OnNewMediaSegment(base::TimeDelta media_segment_start_time);
58 // Add the |buffers| to the SourceBufferStream. Buffers within the queue are
59 // expected to be in order, but multiple calls to Append() may add buffers out
60 // of order or overlapping. Assumes all buffers within |buffers| are in
61 // presentation order and are non-overlapping.
62 // Returns true if Append() was successful, false if |buffers| are not added.
63 // TODO(vrk): Implement garbage collection. (crbug.com/125070)
64 bool Append(const BufferQueue& buffers);
66 // Changes the SourceBufferStream's state so that it will start returning
67 // buffers starting from the closest keyframe before |timestamp|.
68 void Seek(base::TimeDelta timestamp);
70 // Returns true if the SourceBufferStream has seeked to a time without
71 // buffered data and is waiting for more data to be appended.
72 bool IsSeekPending() const;
74 // Notifies the SourceBufferStream that the media duration has been changed to
75 // |duration| so it should drop any data past that point.
76 void OnSetDuration(base::TimeDelta duration);
78 // Fills |out_buffer| with a new buffer. Buffers are presented in order from
79 // the last call to Seek(), or starting with the first buffer appended if
80 // Seek() has not been called yet.
81 // |out_buffer|'s timestamp may be earlier than the |timestamp| passed to
82 // the last Seek() call.
83 // Returns kSuccess if |out_buffer| is filled with a valid buffer, kNeedBuffer
84 // if there is not enough data buffered to fulfill the request, and
85 // kConfigChange if the next buffer requires a config change.
86 Status GetNextBuffer(scoped_refptr<StreamParserBuffer>* out_buffer);
88 // Returns a list of the buffered time ranges.
89 Ranges<base::TimeDelta> GetBufferedTime() const;
91 // Returns true if we don't have any ranges or the last range is selected.
92 bool IsEndSelected() const;
94 const AudioDecoderConfig& GetCurrentAudioDecoderConfig();
95 const VideoDecoderConfig& GetCurrentVideoDecoderConfig();
97 // Notifies this object that the audio config has changed and buffers in
98 // future Append() calls should be associated with this new config.
99 bool UpdateAudioConfig(const AudioDecoderConfig& config);
101 // Notifies this object that the video config has changed and buffers in
102 // future Append() calls should be associated with this new config.
103 bool UpdateVideoConfig(const VideoDecoderConfig& config);
105 // Returns the largest distance between two adjacent buffers in this stream,
106 // or an estimate if no two adjacent buffers have been appended to the stream
107 // yet.
108 base::TimeDelta GetMaxInterbufferDistance() const;
110 private:
111 friend class SourceBufferStreamTest;
112 typedef std::list<SourceBufferRange*> RangeList;
114 void set_memory_limit(int memory_limit) { memory_limit_ = memory_limit; }
116 // Frees up space if the SourceBufferStream is taking up too much memory.
117 void GarbageCollectIfNeeded();
119 // Attempts to delete approximately |total_bytes_to_free| amount of data
120 // |ranges_|, starting at the front of |ranges_| and moving linearly forward
121 // through the buffers. Deletes starting from the back if |reverse_direction|
122 // is true. Returns the number of bytes freed.
123 int FreeBuffers(int total_bytes_to_free, bool reverse_direction);
125 // Appends |new_buffers| into |range_for_new_buffers_itr|, handling start and
126 // end overlaps if necessary.
127 // |deleted_buffers| is an output parameter containing candidates for
128 // |track_buffer_|.
129 // Returns true if the buffers were successfully inserted into the existing
130 // range.
131 // Returns false if the buffers being inserted triggered an error.
132 bool InsertIntoExistingRange(
133 const RangeList::iterator& range_for_new_buffers_itr,
134 const BufferQueue& new_buffers,
135 BufferQueue* deleted_buffers);
137 // Resolve overlapping ranges such that no ranges overlap anymore.
138 // |range_with_new_buffers_itr| points to the range that has newly appended
139 // buffers.
140 // |deleted_buffers| is an output parameter containing candidates for
141 // |track_buffer_|.
142 void ResolveCompleteOverlaps(
143 const RangeList::iterator& range_with_new_buffers_itr,
144 BufferQueue* deleted_buffers);
145 void ResolveEndOverlap(
146 const RangeList::iterator& range_with_new_buffers_itr,
147 BufferQueue* deleted_buffers);
149 // Removes buffers, from the |track_buffer_|, that come after |timestamp|.
150 void PruneTrackBuffer(const base::TimeDelta timestamp);
152 // Checks to see if |range_with_new_buffers_itr| can be merged with the range
153 // next to it, and merges them if so.
154 void MergeWithAdjacentRangeIfNecessary(
155 const RangeList::iterator& range_with_new_buffers_itr);
157 // Deletes the buffers between |start_timestamp|, |end_timestamp| from
158 // the range that |range_itr| points to. Deletes between [start,end] if
159 // |is_range_exclusive| is true, or (start,end) if |is_range_exclusive| is
160 // false. Buffers are deleted in GOPs, so this method may delete buffers past
161 // |end_timestamp| if the keyframe a buffer depends on was deleted.
162 void DeleteBetween(const RangeList::iterator& range_itr,
163 base::TimeDelta start_timestamp,
164 base::TimeDelta end_timestamp,
165 bool is_range_exclusive,
166 BufferQueue* deleted_buffers);
168 // Returns true if |second_timestamp| is the timestamp of the next buffer in
169 // sequence after |first_timestamp|, false otherwise.
170 bool AreAdjacentInSequence(
171 base::TimeDelta first_timestamp, base::TimeDelta second_timestamp) const;
173 // Helper method that returns the timestamp for the next buffer that
174 // |selected_range_| will return from GetNextBuffer() call, or kNoTimestamp()
175 // if in between seeking (i.e. |selected_range_| is null).
176 base::TimeDelta GetNextBufferTimestamp();
178 // Returns the timestamp of the last buffer in the |selected_range_| or
179 // kNoTimestamp() if |selected_range_| is null.
180 base::TimeDelta GetEndBufferTimestamp();
182 // Finds the range that should contain a media segment that begins with
183 // |start_timestamp| and returns the iterator pointing to it. Returns
184 // |ranges_.end()| if there's no such existing range.
185 RangeList::iterator FindExistingRangeFor(base::TimeDelta start_timestamp);
187 // Inserts |new_range| into |ranges_| preserving sorted order. Returns an
188 // iterator in |ranges_| that points to |new_range|.
189 RangeList::iterator AddToRanges(SourceBufferRange* new_range);
191 // Returns an iterator that points to the place in |ranges_| where
192 // |selected_range_| lives.
193 RangeList::iterator GetSelectedRangeItr();
195 // Sets the |selected_range_| to |range| and resets the next buffer position
196 // for the previous |selected_range_|.
197 void SetSelectedRange(SourceBufferRange* range);
199 // Seeks |range| to |seek_timestamp| and then calls SetSelectedRange() with
200 // |range|.
201 void SeekAndSetSelectedRange(SourceBufferRange* range,
202 base::TimeDelta seek_timestamp);
204 // Resets this stream back to an unseeked state.
205 void ResetSeekState();
207 // Returns true if |seek_timestamp| refers to the beginning of the first range
208 // in |ranges_|, false otherwise or if |ranges_| is empty.
209 bool ShouldSeekToStartOfBuffered(base::TimeDelta seek_timestamp) const;
211 // Returns true if the |prev_is_keyframe| & |current_is_keyframe| combination
212 // on buffers with the same timestamp should be allowed. Returns false if the
213 // combination should signal an error.
214 bool AllowSameTimestamp(bool prev_is_keyframe,
215 bool current_is_keyframe) const;
217 // Returns true if the timestamps of |buffers| are monotonically increasing
218 // since the previous append to the media segment, false otherwise.
219 bool IsMonotonicallyIncreasing(const BufferQueue& buffers) const;
221 // Returns true if |selected_range_| is the only range in |ranges_| that
222 // HasNextBufferPosition().
223 bool OnlySelectedRangeIsSeeked() const;
225 // Measures the distances between buffer timestamps and tracks the max.
226 void UpdateMaxInterbufferDistance(const BufferQueue& buffers);
228 // Sets the config ID for each buffer to |append_config_index_|.
229 void SetConfigIds(const BufferQueue& buffers);
231 // Called to complete a config change. Updates |current_config_index_| to
232 // match the index of the next buffer. Calling this method causes
233 // GetNextBuffer() to stop returning kConfigChange and start returning
234 // kSuccess.
235 void CompleteConfigChange();
237 // Sets |selected_range_| and seeks to the nearest keyframe after
238 // |timestamp| if necessary and possible. This method only attempts to
239 // set |selected_range_| if |seleted_range_| is null and |track_buffer_|
240 // is empty.
241 void SetSelectedRangeIfNeeded(const base::TimeDelta timestamp);
243 // Find a keyframe timestamp that is >= |start_timestamp| and can be used to
244 // find a new selected range.
245 // Returns kNoTimestamp() if an appropriate keyframe timestamp could not be
246 // found.
247 base::TimeDelta FindNewSelectedRangeSeekTimestamp(
248 const base::TimeDelta start_timestamp);
250 // Searches |ranges_| for the first keyframe timestamp that is >= |timestamp|.
251 // If |ranges_| doesn't contain a GOP that covers |timestamp| or doesn't
252 // have a keyframe after |timestamp| then kNoTimestamp() is returned.
253 base::TimeDelta FindKeyframeAfterTimestamp(const base::TimeDelta timestamp);
255 // Returns "VIDEO" for a video SourceBufferStream and "AUDIO" for an audio
256 // one.
257 std::string GetStreamTypeName() const;
259 // Callback used to report error strings that can help the web developer
260 // figure out what is wrong with the content.
261 LogCB log_cb_;
263 // List of disjoint buffered ranges, ordered by start time.
264 RangeList ranges_;
266 // Indicates which decoder config is being used by the decoder.
267 // GetNextBuffer() is only allows to return buffers that have a
268 // config ID that matches this index. If there is a mismatch then
269 // it must signal that a config change is needed.
270 int current_config_index_;
272 // Indicates which decoder config to associate with new buffers
273 // being appended. Each new buffer appended has its config ID set
274 // to the value of this field.
275 int append_config_index_;
277 // Holds the audio/video configs for this stream. |current_config_index_|
278 // and |append_config_index_| represent indexes into one of these vectors.
279 std::vector<AudioDecoderConfig> audio_configs_;
280 std::vector<VideoDecoderConfig> video_configs_;
282 // True if more data needs to be appended before the Seek() can complete,
283 // false if no Seek() has been requested or the Seek() is completed.
284 bool seek_pending_;
286 // Timestamp of the last request to Seek().
287 base::TimeDelta seek_buffer_timestamp_;
289 // Pointer to the seeked-to Range. This is the range from which
290 // GetNextBuffer() calls are fulfilled after the |track_buffer_| has been
291 // emptied.
292 SourceBufferRange* selected_range_;
294 // Queue of the next buffers to be returned from calls to GetNextBuffer(). If
295 // |track_buffer_| is empty, return buffers from |selected_range_|.
296 BufferQueue track_buffer_;
298 // The start time of the current media segment being appended.
299 base::TimeDelta media_segment_start_time_;
301 // Points to the range containing the current media segment being appended.
302 RangeList::iterator range_for_next_append_;
304 // True when the next call to Append() begins a new media segment.
305 bool new_media_segment_;
307 // The timestamp of the last buffer appended to the media segment, set to
308 // kNoTimestamp() if the beginning of the segment.
309 base::TimeDelta last_appended_buffer_timestamp_;
310 bool last_appended_buffer_is_keyframe_;
312 // The decode timestamp on the last buffer returned by the most recent
313 // GetNextBuffer() call. Set to kNoTimestamp() if GetNextBuffer() hasn't been
314 // called yet or a seek has happened since the last GetNextBuffer() call.
315 base::TimeDelta last_output_buffer_timestamp_;
317 // Stores the largest distance between two adjacent buffers in this stream.
318 base::TimeDelta max_interbuffer_distance_;
320 // The maximum amount of data in bytes the stream will keep in memory.
321 int memory_limit_;
323 // Indicates that a kConfigChanged status has been reported by GetNextBuffer()
324 // and GetCurrentXXXDecoderConfig() must be called to update the current
325 // config. GetNextBuffer() must not be called again until
326 // GetCurrentXXXDecoderConfig() has been called.
327 bool config_change_pending_;
329 DISALLOW_COPY_AND_ASSIGN(SourceBufferStream);
332 } // namespace media
334 #endif // MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_