[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / media / filters / source_buffer_range.h
blob0c6a8b36f343ef9da993a854d1963d0e59840343
1 // Copyright 2014 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 #ifndef MEDIA_FILTERS_SOURCE_BUFFER_RANGE_H_
6 #define MEDIA_FILTERS_SOURCE_BUFFER_RANGE_H_
8 #include <map>
10 #include "base/callback.h"
11 #include "base/memory/ref_counted.h"
12 #include "media/base/stream_parser_buffer.h"
14 namespace media {
16 // Helper class representing a range of buffered data. All buffers in a
17 // SourceBufferRange are ordered sequentially in decode timestamp order with no
18 // gaps.
19 class SourceBufferRange {
20 public:
21 // Returns the maximum distance in time between any buffer seen in this
22 // stream. Used to estimate the duration of a buffer if its duration is not
23 // known.
24 typedef base::Callback<base::TimeDelta()> InterbufferDistanceCB;
26 typedef StreamParser::BufferQueue BufferQueue;
28 // Policy for handling large gaps between buffers. Continuous media like
29 // audio & video should use NO_GAPS_ALLOWED. Discontinuous media like
30 // timed text should use ALLOW_GAPS because large differences in timestamps
31 // are common and acceptable.
32 enum GapPolicy {
33 NO_GAPS_ALLOWED,
34 ALLOW_GAPS
37 // Buffers with the same timestamp are only allowed under certain conditions.
38 // More precisely, it is allowed in all situations except when the previous
39 // frame is not a key frame and the current is a key frame.
40 // Examples of situations where DTS of two consecutive frames can be equal:
41 // - Video: VP8 Alt-Ref frames.
42 // - Video: IPBPBP...: DTS for I frame and for P frame can be equal.
43 // - Text track cues that start at same time.
44 // Returns true if |prev_is_keyframe| and |current_is_keyframe| indicate a
45 // same timestamp situation that is allowed. False is returned otherwise.
46 static bool AllowSameTimestamp(bool prev_is_keyframe,
47 bool current_is_keyframe);
49 // Creates a source buffer range with |new_buffers|. |new_buffers| cannot be
50 // empty and the front of |new_buffers| must be a keyframe.
51 // |media_segment_start_time| refers to the starting timestamp for the media
52 // segment to which these buffers belong.
53 SourceBufferRange(GapPolicy gap_policy,
54 const BufferQueue& new_buffers,
55 DecodeTimestamp media_segment_start_time,
56 const InterbufferDistanceCB& interbuffer_distance_cb);
58 ~SourceBufferRange();
60 // Appends |buffers| to the end of the range and updates |keyframe_map_| as
61 // it encounters new keyframes. Assumes |buffers| belongs at the end of the
62 // range.
63 void AppendBuffersToEnd(const BufferQueue& buffers);
64 bool CanAppendBuffersToEnd(const BufferQueue& buffers) const;
66 // Appends the buffers from |range| into this range.
67 // The first buffer in |range| must come directly after the last buffer
68 // in this range.
69 // If |transfer_current_position| is true, |range|'s |next_buffer_index_|
70 // is transfered to this SourceBufferRange.
71 void AppendRangeToEnd(const SourceBufferRange& range,
72 bool transfer_current_position);
73 bool CanAppendRangeToEnd(const SourceBufferRange& range) const;
75 // Updates |next_buffer_index_| to point to the Buffer containing |timestamp|.
76 // Assumes |timestamp| is valid and in this range.
77 void Seek(DecodeTimestamp timestamp);
79 // Updates |next_buffer_index_| to point to next keyframe after or equal to
80 // |timestamp|.
81 void SeekAheadTo(DecodeTimestamp timestamp);
83 // Updates |next_buffer_index_| to point to next keyframe strictly after
84 // |timestamp|.
85 void SeekAheadPast(DecodeTimestamp timestamp);
87 // Seeks to the beginning of the range.
88 void SeekToStart();
90 // Finds the next keyframe from |buffers_| starting at or after |timestamp|
91 // and creates and returns a new SourceBufferRange with the buffers from that
92 // keyframe onward. The buffers in the new SourceBufferRange are moved out of
93 // this range. If there is no keyframe at or after |timestamp|, SplitRange()
94 // returns null and this range is unmodified.
95 SourceBufferRange* SplitRange(DecodeTimestamp timestamp);
97 // Deletes the buffers from this range starting at |timestamp|, exclusive if
98 // |is_exclusive| is true, inclusive otherwise.
99 // Resets |next_buffer_index_| if the buffer at |next_buffer_index_| was
100 // deleted, and deletes the |keyframe_map_| entries for the buffers that
101 // were removed.
102 // |deleted_buffers| contains the buffers that were deleted from this range,
103 // starting at the buffer that had been at |next_buffer_index_|.
104 // Returns true if everything in the range was deleted. Otherwise
105 // returns false.
106 bool TruncateAt(DecodeTimestamp timestamp,
107 BufferQueue* deleted_buffers, bool is_exclusive);
108 // Deletes all buffers in range.
109 void DeleteAll(BufferQueue* deleted_buffers);
111 // Deletes a GOP from the front or back of the range and moves these
112 // buffers into |deleted_buffers|. Returns the number of bytes deleted from
113 // the range (i.e. the size in bytes of |deleted_buffers|).
114 int DeleteGOPFromFront(BufferQueue* deleted_buffers);
115 int DeleteGOPFromBack(BufferQueue* deleted_buffers);
117 // Gets the range of GOP to secure at least |bytes_to_free| from
118 // [|start_timestamp|, |end_timestamp|).
119 // Returns the size of the buffers to secure if the buffers of
120 // [|start_timestamp|, |end_removal_timestamp|) is removed.
121 // Will not update |end_removal_timestamp| if the returned size is 0.
122 int GetRemovalGOP(
123 DecodeTimestamp start_timestamp, DecodeTimestamp end_timestamp,
124 int bytes_to_free, DecodeTimestamp* end_removal_timestamp);
126 // Indicates whether the GOP at the beginning or end of the range contains the
127 // next buffer position.
128 bool FirstGOPContainsNextBufferPosition() const;
129 bool LastGOPContainsNextBufferPosition() const;
131 // Updates |out_buffer| with the next buffer in presentation order. Seek()
132 // must be called before calls to GetNextBuffer(), and buffers are returned
133 // in order from the last call to Seek(). Returns true if |out_buffer| is
134 // filled with a valid buffer, false if there is not enough data to fulfill
135 // the request.
136 bool GetNextBuffer(scoped_refptr<StreamParserBuffer>* out_buffer);
137 bool HasNextBuffer() const;
139 // Returns the config ID for the buffer that will be returned by
140 // GetNextBuffer().
141 int GetNextConfigId() const;
143 // Returns true if the range knows the position of the next buffer it should
144 // return, i.e. it has been Seek()ed. This does not necessarily mean that it
145 // has the next buffer yet.
146 bool HasNextBufferPosition() const;
148 // Resets this range to an "unseeked" state.
149 void ResetNextBufferPosition();
151 // Returns the timestamp of the next buffer that will be returned from
152 // GetNextBuffer(), or kNoTimestamp() if the timestamp is unknown.
153 DecodeTimestamp GetNextTimestamp() const;
155 // Returns the start timestamp of the range.
156 DecodeTimestamp GetStartTimestamp() const;
158 // Returns the timestamp of the last buffer in the range.
159 DecodeTimestamp GetEndTimestamp() const;
161 // Returns the timestamp for the end of the buffered region in this range.
162 // This is an approximation if the duration for the last buffer in the range
163 // is unset.
164 DecodeTimestamp GetBufferedEndTimestamp() const;
166 // Gets the timestamp for the keyframe that is after |timestamp|. If
167 // there isn't a keyframe in the range after |timestamp| then kNoTimestamp()
168 // is returned. If |timestamp| is in the "gap" between the value returned by
169 // GetStartTimestamp() and the timestamp on the first buffer in |buffers_|,
170 // then |timestamp| is returned.
171 DecodeTimestamp NextKeyframeTimestamp(DecodeTimestamp timestamp);
173 // Gets the timestamp for the closest keyframe that is <= |timestamp|. If
174 // there isn't a keyframe before |timestamp| or |timestamp| is outside
175 // this range, then kNoTimestamp() is returned.
176 DecodeTimestamp KeyframeBeforeTimestamp(DecodeTimestamp timestamp);
178 // Returns whether a buffer with a starting timestamp of |timestamp| would
179 // belong in this range. This includes a buffer that would be appended to
180 // the end of the range.
181 bool BelongsToRange(DecodeTimestamp timestamp) const;
183 // Returns true if the range has enough data to seek to the specified
184 // |timestamp|, false otherwise.
185 bool CanSeekTo(DecodeTimestamp timestamp) const;
187 // Returns true if this range's buffered timespan completely overlaps the
188 // buffered timespan of |range|.
189 bool CompletelyOverlaps(const SourceBufferRange& range) const;
191 // Returns true if the end of this range contains buffers that overlaps with
192 // the beginning of |range|.
193 bool EndOverlaps(const SourceBufferRange& range) const;
195 // Returns true if |timestamp| is the timestamp of the next buffer in
196 // sequence after |buffers_.back()|, false otherwise.
197 bool IsNextInSequence(DecodeTimestamp timestamp, bool is_key_frame) const;
199 // Adds all buffers which overlap [start, end) to the end of |buffers|. If
200 // no buffers exist in the range returns false, true otherwise.
201 bool GetBuffersInRange(DecodeTimestamp start, DecodeTimestamp end,
202 BufferQueue* buffers);
204 int size_in_bytes() const { return size_in_bytes_; }
206 private:
207 typedef std::map<DecodeTimestamp, int> KeyframeMap;
209 // Called during AppendBuffersToEnd to adjust estimated duration at the
210 // end of the last append to match the delta in timestamps between
211 // the last append and the upcoming append. This is a workaround for
212 // WebM media where a duration is not always specified.
213 void AdjustEstimatedDurationForNewAppend(const BufferQueue& new_buffers);
215 // Seeks the range to the next keyframe after |timestamp|. If
216 // |skip_given_timestamp| is true, the seek will go to a keyframe with a
217 // timestamp strictly greater than |timestamp|.
218 void SeekAhead(DecodeTimestamp timestamp, bool skip_given_timestamp);
220 // Returns an iterator in |buffers_| pointing to the buffer at |timestamp|.
221 // If |skip_given_timestamp| is true, this returns the first buffer with
222 // timestamp greater than |timestamp|.
223 BufferQueue::iterator GetBufferItrAt(
224 DecodeTimestamp timestamp, bool skip_given_timestamp);
226 // Returns an iterator in |keyframe_map_| pointing to the next keyframe after
227 // |timestamp|. If |skip_given_timestamp| is true, this returns the first
228 // keyframe with a timestamp strictly greater than |timestamp|.
229 KeyframeMap::iterator GetFirstKeyframeAt(
230 DecodeTimestamp timestamp, bool skip_given_timestamp);
232 // Returns an iterator in |keyframe_map_| pointing to the first keyframe
233 // before or at |timestamp|.
234 KeyframeMap::iterator GetFirstKeyframeAtOrBefore(DecodeTimestamp timestamp);
236 // Helper method to delete buffers in |buffers_| starting at
237 // |starting_point|, an iterator in |buffers_|.
238 // Returns true if everything in the range was removed. Returns
239 // false if the range still contains buffers.
240 bool TruncateAt(const BufferQueue::iterator& starting_point,
241 BufferQueue* deleted_buffers);
243 // Frees the buffers in |buffers_| from [|start_point|,|ending_point|) and
244 // updates the |size_in_bytes_| accordingly. Does not update |keyframe_map_|.
245 void FreeBufferRange(const BufferQueue::iterator& starting_point,
246 const BufferQueue::iterator& ending_point);
248 // Returns the distance in time estimating how far from the beginning or end
249 // of this range a buffer can be to considered in the range.
250 base::TimeDelta GetFudgeRoom() const;
252 // Returns the approximate duration of a buffer in this range.
253 base::TimeDelta GetApproximateDuration() const;
255 // Keeps track of whether gaps are allowed.
256 const GapPolicy gap_policy_;
258 // An ordered list of buffers in this range.
259 BufferQueue buffers_;
261 // Maps keyframe timestamps to its index position in |buffers_|.
262 KeyframeMap keyframe_map_;
264 // Index base of all positions in |keyframe_map_|. In other words, the
265 // real position of entry |k| of |keyframe_map_| in the range is:
266 // keyframe_map_[k] - keyframe_map_index_base_
267 int keyframe_map_index_base_;
269 // Index into |buffers_| for the next buffer to be returned by
270 // GetNextBuffer(), set to -1 before Seek().
271 int next_buffer_index_;
273 // If the first buffer in this range is the beginning of a media segment,
274 // |media_segment_start_time_| is the time when the media segment begins.
275 // |media_segment_start_time_| may be <= the timestamp of the first buffer in
276 // |buffers_|. |media_segment_start_time_| is kNoTimestamp() if this range
277 // does not start at the beginning of a media segment, which can only happen
278 // garbage collection or after an end overlap that results in a split range
279 // (we don't have a way of knowing the media segment timestamp for the new
280 // range).
281 DecodeTimestamp media_segment_start_time_;
283 // Called to get the largest interbuffer distance seen so far in the stream.
284 InterbufferDistanceCB interbuffer_distance_cb_;
286 // Stores the amount of memory taken up by the data in |buffers_|.
287 int size_in_bytes_;
289 DISALLOW_COPY_AND_ASSIGN(SourceBufferRange);
292 } // namespace media
294 #endif // MEDIA_FILTERS_SOURCE_BUFFER_RANGE_H_