| OLD | NEW |
| (Empty) | |
| 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. |
| 4 |
| 5 #ifndef MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_ |
| 6 #define MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_ |
| 7 |
| 8 #include <deque> |
| 9 #include <list> |
| 10 #include <map> |
| 11 |
| 12 #include "base/basictypes.h" |
| 13 #include "base/memory/ref_counted.h" |
| 14 #include "media/base/buffers.h" |
| 15 #include "media/base/media_export.h" |
| 16 |
| 17 namespace media { |
| 18 |
| 19 class Range; |
| 20 |
| 21 // SourceBufferStream is a data structure that stores media Buffers in ranges. |
| 22 // Buffers can be appended out of presentation order. Buffers are retrieved by |
| 23 // seeking to the desired start point and calling GetNextBuffer(). Buffers are |
| 24 // returned in sequential presentation order. |
| 25 class MEDIA_EXPORT SourceBufferStream { |
| 26 public: |
| 27 typedef std::deque<scoped_refptr<Buffer> > BufferQueue; |
| 28 typedef std::pair<base::TimeDelta, base::TimeDelta> Timespan; |
| 29 typedef std::list<Timespan> TimespanList; |
| 30 |
| 31 SourceBufferStream(); |
| 32 ~SourceBufferStream(); |
| 33 |
| 34 // Add the |buffers| to the SourceBufferStream. Buffers within the queue are |
| 35 // expected to be in order, but multiple calls to Append() may add buffers out |
| 36 // of order or overlapping. |
| 37 // TODO(vrk): Implement proper end-overlapping. (crbug.com/125072) |
| 38 // This may trigger garbage collection. |
| 39 // TODO(vrk): Implement garbage collection. (crbug.com/125070) |
| 40 void Append(const BufferQueue& buffers); |
| 41 |
| 42 // Changes the SourceBufferStream's state so that it will start returning |
| 43 // buffers starting from |timestamp|. |
| 44 void Seek(base::TimeDelta timestamp); |
| 45 |
| 46 // Fills |out_buffer| with a new buffer. Buffers are presented in order from |
| 47 // the last call to Seek(). |out_buffer|'s timestamp may be earlier than the |
| 48 // |timestamp| passed to the last Seek() call. |
| 49 // Returns true if |out_buffer| is filled with a valid buffer, false if |
| 50 // no Seek() has been called yet or if there is not enough data buffered to |
| 51 // fufill the request. |
| 52 bool GetNextBuffer(scoped_refptr<Buffer>* out_buffer); |
| 53 |
| 54 // Returns a list of the buffered time ranges. |
| 55 TimespanList GetBufferedTime() const; |
| 56 |
| 57 private: |
| 58 // Helper class representing a range of buffered data. All buffers in a Range |
| 59 // are ordered sequentially in presentation order with no gaps. |
| 60 class Range { |
| 61 public: |
| 62 Range(); |
| 63 void Append(const BufferQueue& buffers); |
| 64 void Seek(base::TimeDelta timestamp); |
| 65 bool GetNextBuffer(scoped_refptr<Buffer>* out_buffer); |
| 66 Timespan GetBufferedTime() const; |
| 67 |
| 68 // Moves the buffers from |range| into this range. |
| 69 // The first buffer in |range| must come directly after the last buffer |
| 70 // in this range. |
| 71 void Merge(Range* range); |
| 72 bool CanMerge(const Range* range) const; |
| 73 |
| 74 // Returns whether a buffer with a starting timestamp of |timestamp| would |
| 75 // belong in this range. This includes a buffer that would be appended to |
| 76 // the end of the range. |
| 77 // Returns 0 if |timestamp| is in this range, a negative value if |
| 78 // |timestamp| appears before this range, or a positive value if |timestamp| |
| 79 // appears after this range. |
| 80 int BelongsToRange(base::TimeDelta timestamp) const; |
| 81 |
| 82 // Returns true if a buffer with a starting timestamp of |timestamp| is |
| 83 // buffered in this range, false otherwise. |
| 84 bool CanSeekTo(base::TimeDelta timestamp) const; |
| 85 |
| 86 // Returns true if the end of this range contains buffers that overlaps with |
| 87 // the beginning of |range|. |
| 88 bool EndOverlaps(const Range* range) const; |
| 89 |
| 90 void set_selected_range(bool is_current) { |
| 91 is_selected_range_ = is_current; |
| 92 } |
| 93 |
| 94 bool is_selected_range() const { |
| 95 return is_selected_range_; |
| 96 } |
| 97 |
| 98 private: |
| 99 void AppendToEnd(const BufferQueue& buffers); |
| 100 base::TimeDelta BufferedStart() const; |
| 101 base::TimeDelta BufferedEnd() const; |
| 102 base::TimeDelta MaxNextTimestamp() const; |
| 103 void Reset(); |
| 104 |
| 105 // An ordered list of buffers in this range. |
| 106 BufferQueue buffers_; |
| 107 |
| 108 // Maps keyframe timestamps to its index position in |buffers_|. |
| 109 typedef std::map<base::TimeDelta, size_t> KeyframeMap; |
| 110 KeyframeMap keyframe_map_; |
| 111 |
| 112 // True if the last call to Seek() was is in this Range, false otherwise. |
| 113 bool is_selected_range_; |
| 114 |
| 115 // Index into |buffers_| for the next buffer to be returned by |
| 116 // GetBufferedTime(); |
| 117 int next_buffer_index_; |
| 118 |
| 119 DISALLOW_COPY_AND_ASSIGN(Range); |
| 120 }; |
| 121 |
| 122 bool IsRangeListSorted() const; |
| 123 |
| 124 typedef std::list<Range*> RangeList; |
| 125 // List of disjoint buffered ranges, ordered by start time. |
| 126 RangeList ranges_; |
| 127 |
| 128 // True if more data needs to be appended before the Seek() can complete, |
| 129 // false if no Seek() has been requested or the Seek() is completed. |
| 130 bool seek_pending_; |
| 131 |
| 132 // Timestamp of the last request to Seek(). |
| 133 base::TimeDelta seek_buffer_timestamp_; |
| 134 |
| 135 // Pointer to the seeked-to Range. This is the Range from which |
| 136 // GetNextBuffer() calls are fulfilled. |
| 137 Range* selected_range_; |
| 138 |
| 139 DISALLOW_COPY_AND_ASSIGN(SourceBufferStream); |
| 140 }; |
| 141 |
| 142 } // namespace media |
| 143 |
| 144 #endif // MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_ |
| OLD | NEW |