source_buffer_stream.h revision f2477e01787aa58f445919b809d89e252beef54f
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved.
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file.
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// SourceBufferStream is a data structure that stores media Buffers in ranges.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Buffers can be appended out of presentation order. Buffers are retrieved by
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// seeking to the desired start point and calling GetNextBuffer(). Buffers are
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// returned in sequential presentation order.
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <deque>
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <list>
152a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include <string>
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <utility>
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/ref_counted.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/audio_decoder_config.h"
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/media_export.h"
222a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "media/base/media_log.h"
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/ranges.h"
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/stream_parser_buffer.h"
25f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)#include "media/base/text_track_config.h"
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "media/base/video_decoder_config.h"
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace media {
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class SourceBufferRange;
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// See file-level comment for complete description.
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class MEDIA_EXPORT SourceBufferStream {
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Status returned by GetNextBuffer().
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kSuccess: Indicates that the next buffer was returned.
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kNeedBuffer: Indicates that we need more data before a buffer can be
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //              returned.
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kConfigChange: Indicates that the next buffer requires a config change.
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum Status {
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kSuccess,
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kNeedBuffer,
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    kConfigChange,
467d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)    kEndOfStream
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SourceBufferStream(const AudioDecoderConfig& audio_config,
502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     const LogCB& log_cb);
512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  SourceBufferStream(const VideoDecoderConfig& video_config,
522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                     const LogCB& log_cb);
53f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  SourceBufferStream(const TextTrackConfig& text_config,
54f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)                     const LogCB& log_cb);
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~SourceBufferStream();
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Signals that the next buffers appended are part of a new media segment
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // starting at |media_segment_start_time|.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnNewMediaSegment(base::TimeDelta media_segment_start_time);
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Add the |buffers| to the SourceBufferStream. Buffers within the queue are
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // expected to be in order, but multiple calls to Append() may add buffers out
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // of order or overlapping. Assumes all buffers within |buffers| are in
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // presentation order and are non-overlapping.
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if Append() was successful, false if |buffers| are not added.
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // TODO(vrk): Implement garbage collection. (crbug.com/125070)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool Append(const BufferQueue& buffers);
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
70bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // Removes buffers between |start| and |end| according to the steps
71bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // in the "Coded Frame Removal Algorithm" in the Media Source
72bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // Extensions Spec.
73bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
74bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  //
75bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // |duration| is the current duration of the presentation. It is
76bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  // required by the computation outlined in the spec.
77bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch  void Remove(base::TimeDelta start, base::TimeDelta end,
78bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch              base::TimeDelta duration);
79bbcdd45c55eb7c4641ab97aef9889b0fc828e7d3Ben Murdoch
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Changes the SourceBufferStream's state so that it will start returning
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // buffers starting from the closest keyframe before |timestamp|.
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void Seek(base::TimeDelta timestamp);
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the SourceBufferStream has seeked to a time without
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // buffered data and is waiting for more data to be appended.
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsSeekPending() const;
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notifies the SourceBufferStream that the media duration has been changed to
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |duration| so it should drop any data past that point.
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void OnSetDuration(base::TimeDelta duration);
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fills |out_buffer| with a new buffer. Buffers are presented in order from
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the last call to Seek(), or starting with the first buffer appended if
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Seek() has not been called yet.
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |out_buffer|'s timestamp may be earlier than the |timestamp| passed to
965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the last Seek() call.
975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns kSuccess if |out_buffer| is filled with a valid buffer, kNeedBuffer
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if there is not enough data buffered to fulfill the request, and
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kConfigChange if the next buffer requires a config change.
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Status GetNextBuffer(scoped_refptr<StreamParserBuffer>* out_buffer);
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns a list of the buffered time ranges.
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  Ranges<base::TimeDelta> GetBufferedTime() const;
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1057d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Notifies this object that end of stream has been signalled.
106ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  void MarkEndOfStream();
1077d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
108ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  // Clear the end of stream state set by MarkEndOfStream().
109ca12bfac764ba476d6cd062bf1dde12cc64c3f40Ben Murdoch  void UnmarkEndOfStream();
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const AudioDecoderConfig& GetCurrentAudioDecoderConfig();
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  const VideoDecoderConfig& GetCurrentVideoDecoderConfig();
113f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  const TextTrackConfig& GetCurrentTextTrackConfig();
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notifies this object that the audio config has changed and buffers in
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // future Append() calls should be associated with this new config.
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateAudioConfig(const AudioDecoderConfig& config);
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Notifies this object that the video config has changed and buffers in
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // future Append() calls should be associated with this new config.
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool UpdateVideoConfig(const VideoDecoderConfig& config);
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the largest distance between two adjacent buffers in this stream,
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // or an estimate if no two adjacent buffers have been appended to the stream
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // yet.
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeDelta GetMaxInterbufferDistance() const;
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
128eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void set_memory_limit_for_testing(int memory_limit) {
129eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch    memory_limit_ = memory_limit;
130eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  }
131eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
13368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  friend class SourceBufferStreamTest;
13468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  typedef std::list<SourceBufferRange*> RangeList;
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Frees up space if the SourceBufferStream is taking up too much memory.
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void GarbageCollectIfNeeded();
1395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Attempts to delete approximately |total_bytes_to_free| amount of data
1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |ranges_|, starting at the front of |ranges_| and moving linearly forward
1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // through the buffers. Deletes starting from the back if |reverse_direction|
1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is true. Returns the number of bytes freed.
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int FreeBuffers(int total_bytes_to_free, bool reverse_direction);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
14668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Attempts to delete approximately |total_bytes_to_free| amount of data from
14768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // |ranges_|, starting after the last appended buffer before the current
14868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // playback position.
14968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int FreeBuffersAfterLastAppended(int total_bytes_to_free);
15068043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
15168043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Gets the removal range to secure |byte_to_free| from
15268043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // [|start_timestamp|, |end_timestamp|).
15368043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Returns the size of buffers to secure if future
15468043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Remove(|start_timestamp|, |removal_end_timestamp|, duration) is called.
15568043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  // Will not update |removal_end_timestamp| if the returned size is 0.
15668043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)  int GetRemovalRange(base::TimeDelta start_timestamp,
15768043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      base::TimeDelta end_timestamp, int byte_to_free,
15868043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)      base::TimeDelta* removal_end_timestamp);
15968043e1e95eeb07d5cae7aca370b26518b0867d6Torne (Richard Coles)
1601e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Prepares |range_for_next_append_| so |new_buffers| can be appended.
1611e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // This involves removing buffers between the end of the previous append
1621e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // and any buffers covered by the time range in |new_buffers|.
1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |deleted_buffers| is an output parameter containing candidates for
1641e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // |track_buffer_| if this method ends up removing the current playback
1651e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // position from the range.
1661e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void PrepareRangesForNextAppend(const BufferQueue& new_buffers,
1671e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                                  BufferQueue* deleted_buffers);
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Removes buffers, from the |track_buffer_|, that come after |timestamp|.
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void PruneTrackBuffer(const base::TimeDelta timestamp);
1715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Checks to see if |range_with_new_buffers_itr| can be merged with the range
1735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // next to it, and merges them if so.
1745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void MergeWithAdjacentRangeIfNecessary(
1755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const RangeList::iterator& range_with_new_buffers_itr);
1765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if |second_timestamp| is the timestamp of the next buffer in
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // sequence after |first_timestamp|, false otherwise.
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool AreAdjacentInSequence(
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      base::TimeDelta first_timestamp, base::TimeDelta second_timestamp) const;
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper method that returns the timestamp for the next buffer that
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selected_range_| will return from GetNextBuffer() call, or kNoTimestamp()
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if in between seeking (i.e. |selected_range_| is null).
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeDelta GetNextBufferTimestamp();
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns the timestamp of the last buffer in the |selected_range_| or
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kNoTimestamp() if |selected_range_| is null.
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeDelta GetEndBufferTimestamp();
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Finds the range that should contain a media segment that begins with
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |start_timestamp| and returns the iterator pointing to it. Returns
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |ranges_.end()| if there's no such existing range.
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RangeList::iterator FindExistingRangeFor(base::TimeDelta start_timestamp);
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Inserts |new_range| into |ranges_| preserving sorted order. Returns an
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // iterator in |ranges_| that points to |new_range|.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RangeList::iterator AddToRanges(SourceBufferRange* new_range);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns an iterator that points to the place in |ranges_| where
2015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |selected_range_| lives.
2025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RangeList::iterator GetSelectedRangeItr();
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the |selected_range_| to |range| and resets the next buffer position
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // for the previous |selected_range_|.
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetSelectedRange(SourceBufferRange* range);
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2082a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Seeks |range| to |seek_timestamp| and then calls SetSelectedRange() with
2092a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |range|.
2102a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SeekAndSetSelectedRange(SourceBufferRange* range,
2112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                               base::TimeDelta seek_timestamp);
2122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Resets this stream back to an unseeked state.
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ResetSeekState();
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if |seek_timestamp| refers to the beginning of the first range
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // in |ranges_|, false otherwise or if |ranges_| is empty.
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool ShouldSeekToStartOfBuffered(base::TimeDelta seek_timestamp) const;
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if the timestamps of |buffers| are monotonically increasing
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // since the previous append to the media segment, false otherwise.
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool IsMonotonicallyIncreasing(const BufferQueue& buffers) const;
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2241e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Returns true if |next_timestamp| and |next_is_keyframe| are valid for
2251e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // the first buffer after the previous append.
2261e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool IsNextTimestampValid(base::TimeDelta next_timestamp,
2271e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)                            bool next_is_keyframe) const;
2281e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Returns true if |selected_range_| is the only range in |ranges_| that
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // HasNextBufferPosition().
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool OnlySelectedRangeIsSeeked() const;
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Measures the distances between buffer timestamps and tracks the max.
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void UpdateMaxInterbufferDistance(const BufferQueue& buffers);
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sets the config ID for each buffer to |append_config_index_|.
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void SetConfigIds(const BufferQueue& buffers);
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Called to complete a config change. Updates |current_config_index_| to
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // match the index of the next buffer. Calling this method causes
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetNextBuffer() to stop returning kConfigChange and start returning
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kSuccess.
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CompleteConfigChange();
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Sets |selected_range_| and seeks to the nearest keyframe after
2462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // |timestamp| if necessary and possible. This method only attempts to
2472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // set |selected_range_| if |seleted_range_| is null and |track_buffer_|
2482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // is empty.
2492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  void SetSelectedRangeIfNeeded(const base::TimeDelta timestamp);
2502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Find a keyframe timestamp that is >= |start_timestamp| and can be used to
2522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // find a new selected range.
2532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns kNoTimestamp() if an appropriate keyframe timestamp could not be
2542a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // found.
2552a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta FindNewSelectedRangeSeekTimestamp(
2562a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)      const base::TimeDelta start_timestamp);
2572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Searches |ranges_| for the first keyframe timestamp that is >= |timestamp|.
2592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // If |ranges_| doesn't contain a GOP that covers |timestamp| or doesn't
2602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // have a keyframe after |timestamp| then kNoTimestamp() is returned.
2612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta FindKeyframeAfterTimestamp(const base::TimeDelta timestamp);
2622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Returns "VIDEO" for a video SourceBufferStream and "AUDIO" for an audio
2642a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // one.
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::string GetStreamTypeName() const;
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2677d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Returns true if we don't have any ranges or the last range is selected
2687d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // or there is a pending seek beyond any existing ranges.
2697d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  bool IsEndSelected() const;
2707d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
2718bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // Deletes the range pointed to by |*itr| and removes it from |ranges_|.
2728bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // If |*itr| points to |selected_range_|, then |selected_range_| is set to
2738bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // NULL. After the range is removed, |*itr| is to the range after the one that
2748bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  // was removed or to |ranges_.end()| if the last range was removed.
2758bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void DeleteAndRemoveRange(RangeList::iterator* itr);
2768bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)
2771e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // Helper function used by Remove() and PrepareRangesForNextAppend() to
2781e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // remove buffers and ranges between |start| and |end|.
2791e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // |is_exclusive| - If set to true, buffers with timestamps that
2801e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // match |start| are not removed. If set to false, buffers with
2811e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // timestamps that match |start| will be removed.
2821e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // |*deleted_buffers| - Filled with buffers for the current playback position
2831e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // if the removal range included the current playback position. These buffers
2841e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  // can be used as candidates for placing in the |track_buffer_|.
2851e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  void RemoveInternal(
2861e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      base::TimeDelta start, base::TimeDelta end, bool is_exclusive,
2871e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)      BufferQueue* deleted_buffers);
2881e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2891e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)  bool is_video() const { return video_configs_.size() > 0; }
2901e9bf3e0803691d0a228da41fc608347b6db4340Torne (Richard Coles)
2912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Callback used to report error strings that can help the web developer
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // figure out what is wrong with the content.
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  LogCB log_cb_;
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // List of disjoint buffered ranges, ordered by start time.
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RangeList ranges_;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indicates which decoder config is being used by the decoder.
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetNextBuffer() is only allows to return buffers that have a
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // config ID that matches this index. If there is a mismatch then
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it must signal that a config change is needed.
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int current_config_index_;
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indicates which decoder config to associate with new buffers
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // being appended. Each new buffer appended has its config ID set
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to the value of this field.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int append_config_index_;
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Holds the audio/video configs for this stream. |current_config_index_|
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and |append_config_index_| represent indexes into one of these vectors.
3112a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<AudioDecoderConfig> audio_configs_;
3122a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  std::vector<VideoDecoderConfig> video_configs_;
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
314f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Holds the text config for this stream.
315f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  TextTrackConfig text_track_config_;
316f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True if more data needs to be appended before the Seek() can complete,
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // false if no Seek() has been requested or the Seek() is completed.
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool seek_pending_;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3217d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // True if the end of the stream has been signalled.
3227d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  bool end_of_stream_;
3237d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Timestamp of the last request to Seek().
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeDelta seek_buffer_timestamp_;
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Pointer to the seeked-to Range. This is the range from which
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetNextBuffer() calls are fulfilled after the |track_buffer_| has been
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // emptied.
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  SourceBufferRange* selected_range_;
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Queue of the next buffers to be returned from calls to GetNextBuffer(). If
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |track_buffer_| is empty, return buffers from |selected_range_|.
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  BufferQueue track_buffer_;
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The start time of the current media segment being appended.
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeDelta media_segment_start_time_;
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Points to the range containing the current media segment being appended.
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  RangeList::iterator range_for_next_append_;
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // True when the next call to Append() begins a new media segment.
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool new_media_segment_;
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The timestamp of the last buffer appended to the media segment, set to
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kNoTimestamp() if the beginning of the segment.
3472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta last_appended_buffer_timestamp_;
3482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  bool last_appended_buffer_is_keyframe_;
3492a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
3502a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The decode timestamp on the last buffer returned by the most recent
3512a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // GetNextBuffer() call. Set to kNoTimestamp() if GetNextBuffer() hasn't been
3522a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // called yet or a seek has happened since the last GetNextBuffer() call.
3532a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  base::TimeDelta last_output_buffer_timestamp_;
3545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Stores the largest distance between two adjacent buffers in this stream.
3565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::TimeDelta max_interbuffer_distance_;
3575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The maximum amount of data in bytes the stream will keep in memory.
3595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int memory_limit_;
3605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Indicates that a kConfigChanged status has been reported by GetNextBuffer()
3625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // and GetCurrentXXXDecoderConfig() must be called to update the current
3635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // config. GetNextBuffer() must not be called again until
3645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // GetCurrentXXXDecoderConfig() has been called.
3655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool config_change_pending_;
3665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(SourceBufferStream);
3685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}  // namespace media
3715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // MEDIA_FILTERS_SOURCE_BUFFER_STREAM_H_
373