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 #ifndef MEDIA_FILTERS_CHUNK_DEMUXER_H_
6 #define MEDIA_FILTERS_CHUNK_DEMUXER_H_
13 #include "base/synchronization/lock.h"
14 #include "media/base/byte_queue.h"
15 #include "media/base/demuxer.h"
16 #include "media/base/ranges.h"
17 #include "media/base/stream_parser.h"
18 #include "media/filters/source_buffer_stream.h"
22 class ChunkDemuxerStream
;
23 class FFmpegURLProtocol
;
26 // Demuxer implementation that allows chunks of media data to be passed
27 // from JavaScript to the media stack.
28 class MEDIA_EXPORT ChunkDemuxer
: public Demuxer
{
31 kOk
, // ID added w/o error.
32 kNotSupported
, // Type specified is not supported.
33 kReachedIdLimit
, // Reached ID limit. We can't handle any more IDs.
36 // |open_cb| Run when Initialize() is called to signal that the demuxer
37 // is ready to receive media data via AppenData().
38 // |need_key_cb| Run when the demuxer determines that an encryption key is
39 // needed to decrypt the content.
40 // |enable_text| Process inband text tracks in the normal way when true,
41 // otherwise ignore them.
42 // |log_cb| Run when parsing error messages need to be logged to the error
44 ChunkDemuxer(const base::Closure
& open_cb
,
45 const NeedKeyCB
& need_key_cb
,
47 virtual ~ChunkDemuxer();
49 // Demuxer implementation.
50 virtual void Initialize(DemuxerHost
* host
,
51 const PipelineStatusCB
& cb
,
52 bool enable_text_tracks
) OVERRIDE
;
53 virtual void Stop(const base::Closure
& callback
) OVERRIDE
;
54 virtual void Seek(base::TimeDelta time
, const PipelineStatusCB
& cb
) OVERRIDE
;
55 virtual void OnAudioRendererDisabled() OVERRIDE
;
56 virtual DemuxerStream
* GetStream(DemuxerStream::Type type
) OVERRIDE
;
57 virtual base::TimeDelta
GetStartTime() const OVERRIDE
;
59 // Methods used by an external object to control this demuxer.
61 // Indicates that a new Seek() call is on its way. Any pending Reads on the
62 // DemuxerStream objects should be aborted immediately inside this call and
63 // future Read calls should return kAborted until the Seek() call occurs.
64 // This method MUST ALWAYS be called before Seek() is called to signal that
65 // the next Seek() call represents the seek point we actually want to return
67 // |seek_time| - The presentation timestamp for the seek that triggered this
68 // call. It represents the most recent position the caller is trying to seek
70 void StartWaitingForSeek(base::TimeDelta seek_time
);
72 // Indicates that a Seek() call is on its way, but another seek has been
73 // requested that will override the impending Seek() call. Any pending Reads
74 // on the DemuxerStream objects should be aborted immediately inside this call
75 // and future Read calls should return kAborted until the next
76 // StartWaitingForSeek() call. This method also arranges for the next Seek()
77 // call received before a StartWaitingForSeek() call to immediately call its
78 // callback without waiting for any data.
79 // |seek_time| - The presentation timestamp for the seek request that
80 // triggered this call. It represents the most recent position the caller is
82 void CancelPendingSeek(base::TimeDelta seek_time
);
84 // Registers a new |id| to use for AppendData() calls. |type| indicates
85 // the MIME type for the data that we intend to append for this ID.
86 // kOk is returned if the demuxer has enough resources to support another ID
87 // and supports the format indicated by |type|.
88 // kNotSupported is returned if |type| is not a supported format.
89 // kReachedIdLimit is returned if the demuxer cannot handle another ID right
91 Status
AddId(const std::string
& id
, const std::string
& type
,
92 std::vector
<std::string
>& codecs
);
94 // Removed an ID & associated resources that were previously added with
96 void RemoveId(const std::string
& id
);
98 // Gets the currently buffered ranges for the specified ID.
99 Ranges
<base::TimeDelta
> GetBufferedRanges(const std::string
& id
) const;
101 // Appends media data to the source buffer associated with |id|.
102 void AppendData(const std::string
& id
, const uint8
* data
, size_t length
);
104 // Aborts parsing the current segment and reset the parser to a state where
105 // it can accept a new segment.
106 void Abort(const std::string
& id
);
108 // Remove buffers between |start| and |end| for the source buffer
109 // associated with |id|.
110 void Remove(const std::string
& id
, base::TimeDelta start
,
111 base::TimeDelta end
);
113 // Returns the current presentation duration.
114 double GetDuration();
115 double GetDuration_Locked();
117 // Notifies the demuxer that the duration of the media has changed to
119 void SetDuration(double duration
);
121 // Sets a time |offset| to be applied to subsequent buffers appended to the
122 // source buffer associated with |id|. Returns true if the offset is set
123 // properly, false if the offset cannot be applied because we're in the
124 // middle of parsing a media segment.
125 bool SetTimestampOffset(const std::string
& id
, base::TimeDelta offset
);
127 // Set the append mode to be applied to subsequent buffers appended to the
128 // source buffer associated with |id|. If |sequence_mode| is true, caller
129 // is requesting "sequence" mode. Otherwise, caller is requesting "segments"
130 // mode. Returns true if the mode update was allowed. Returns false if
131 // the mode cannot be updated because we're in the middle of parsing a media
133 // In "sequence" mode, appended media will be treated as adjacent in time.
134 // In "segments" mode, timestamps in appended media determine coded frame
136 bool SetSequenceMode(const std::string
& id
, bool sequence_mode
);
138 // Called to signal changes in the "end of stream"
139 // state. UnmarkEndOfStream() must not be called if a matching
140 // MarkEndOfStream() has not come before it.
141 void MarkEndOfStream(PipelineStatus status
);
142 void UnmarkEndOfStream();
144 // Set the append window start and end values for the source buffer
145 // associated with |id|.
146 void SetAppendWindowStart(const std::string
& id
, base::TimeDelta start
);
147 void SetAppendWindowEnd(const std::string
& id
, base::TimeDelta end
);
151 // Sets the memory limit on each stream. |memory_limit| is the
152 // maximum number of bytes each stream is allowed to hold in its buffer.
153 void SetMemoryLimitsForTesting(int memory_limit
);
155 // Returns the ranges representing the buffered data in the demuxer.
156 // TODO(wolenetz): Remove this method once MediaSourceDelegate no longer
157 // requires it for doing hack browser seeks to I-frame on Android. See
158 // http://crbug.com/304234.
159 Ranges
<base::TimeDelta
> GetBufferedRanges() const;
171 void ChangeState_Locked(State new_state
);
173 // Reports an error and puts the demuxer in a state where it won't accept more
175 void ReportError_Locked(PipelineStatus error
);
177 // Returns true if any stream has seeked to a time without buffered data.
178 bool IsSeekWaitingForData_Locked() const;
180 // Returns true if all streams can successfully call EndOfStream,
181 // false if any can not.
182 bool CanEndOfStream_Locked() const;
184 // SourceState callbacks.
185 void OnSourceInitDone(bool success
, base::TimeDelta duration
);
187 // Creates a DemuxerStream for the specified |type|.
188 // Returns a new ChunkDemuxerStream instance if a stream of this type
189 // has not been created before. Returns NULL otherwise.
190 ChunkDemuxerStream
* CreateDemuxerStream(DemuxerStream::Type type
);
192 void OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
193 const TextTrackConfig
& config
);
194 void OnNewMediaSegment(const std::string
& source_id
,
195 base::TimeDelta start_timestamp
);
197 // Applies |time_offset| to the timestamps of |buffers|.
198 void AdjustBufferTimestamps(const StreamParser::BufferQueue
& buffers
,
199 base::TimeDelta timestamp_offset
);
201 // Returns true if |source_id| is valid, false otherwise.
202 bool IsValidId(const std::string
& source_id
) const;
204 // Increases |duration_| if |last_appended_buffer_timestamp| exceeds the
205 // current |duration_|. The |duration_| is set to the end buffered timestamp
207 void IncreaseDurationIfNecessary(
208 base::TimeDelta last_appended_buffer_timestamp
,
209 ChunkDemuxerStream
* stream
);
211 // Decreases |duration_| if the buffered region is less than |duration_| when
212 // EndOfStream() is called.
213 void DecreaseDurationIfNecessary();
215 // Sets |duration_| to |new_duration|, sets |user_specified_duration_| to -1
216 // and notifies |host_|.
217 void UpdateDuration(base::TimeDelta new_duration
);
219 // Returns the ranges representing the buffered data in the demuxer.
220 Ranges
<base::TimeDelta
> GetBufferedRanges_Locked() const;
222 // Start returning data on all DemuxerStreams.
223 void StartReturningData();
225 // Aborts pending reads on all DemuxerStreams.
226 void AbortPendingReads();
228 // Completes any pending reads if it is possible to do so.
229 void CompletePendingReadsIfPossible();
231 // Seeks all SourceBufferStreams to |seek_time|.
232 void SeekAllSources(base::TimeDelta seek_time
);
234 // Shuts down all DemuxerStreams by calling Shutdown() on
235 // all objects in |source_state_map_|.
236 void ShutdownAllStreams();
238 mutable base::Lock lock_
;
240 bool cancel_next_seek_
;
243 base::Closure open_cb_
;
244 NeedKeyCB need_key_cb_
;
246 // Callback used to report error strings that can help the web developer
247 // figure out what is wrong with the content.
250 PipelineStatusCB init_cb_
;
251 // Callback to execute upon seek completion.
252 // TODO(wolenetz/acolwell): Protect against possible double-locking by first
253 // releasing |lock_| before executing this callback. See
254 // http://crbug.com/308226
255 PipelineStatusCB seek_cb_
;
257 scoped_ptr
<ChunkDemuxerStream
> audio_
;
258 scoped_ptr
<ChunkDemuxerStream
> video_
;
260 // Keeps |audio_| alive when audio has been disabled.
261 scoped_ptr
<ChunkDemuxerStream
> disabled_audio_
;
263 base::TimeDelta duration_
;
265 // The duration passed to the last SetDuration(). If
266 // SetDuration() is never called or an AppendData() call or
267 // a EndOfStream() call changes |duration_|, then this
268 // variable is set to < 0 to indicate that the |duration_| represents
269 // the actual duration instead of a user specified value.
270 double user_specified_duration_
;
272 typedef std::map
<std::string
, SourceState
*> SourceStateMap
;
273 SourceStateMap source_state_map_
;
275 // Used to ensure that (1) config data matches the type and codec provided in
276 // AddId(), (2) only 1 audio and 1 video sources are added, and (3) ids may be
277 // removed with RemoveID() but can not be re-added (yet).
278 std::string source_id_audio_
;
279 std::string source_id_video_
;
281 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer
);
286 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_