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_
14 #include "base/synchronization/lock.h"
15 #include "media/base/byte_queue.h"
16 #include "media/base/demuxer.h"
17 #include "media/base/demuxer_stream.h"
18 #include "media/base/ranges.h"
19 #include "media/base/stream_parser.h"
20 #include "media/filters/source_buffer_stream.h"
24 class FFmpegURLProtocol
;
27 class MEDIA_EXPORT ChunkDemuxerStream
: public DemuxerStream
{
29 typedef std::deque
<scoped_refptr
<StreamParserBuffer
> > BufferQueue
;
31 ChunkDemuxerStream(Type type
, Liveness liveness
, bool splice_frames_enabled
);
32 ~ChunkDemuxerStream() override
;
34 // ChunkDemuxerStream control methods.
35 void StartReturningData();
37 void CompletePendingReadIfPossible();
40 // SourceBufferStream manipulation methods.
41 void Seek(base::TimeDelta time
);
42 bool IsSeekWaitingForData() const;
44 // Add buffers to this stream. Buffers are stored in SourceBufferStreams,
45 // which handle ordering and overlap resolution.
46 // Returns true if buffers were successfully added.
47 bool Append(const StreamParser::BufferQueue
& buffers
);
49 // Removes buffers between |start| and |end| according to the steps
50 // in the "Coded Frame Removal Algorithm" in the Media Source
52 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
54 // |duration| is the current duration of the presentation. It is
55 // required by the computation outlined in the spec.
56 void Remove(base::TimeDelta start
, base::TimeDelta end
,
57 base::TimeDelta duration
);
59 // Signal to the stream that duration has changed to |duration|.
60 void OnSetDuration(base::TimeDelta duration
);
62 // Returns the range of buffered data in this stream, capped at |duration|.
63 Ranges
<base::TimeDelta
> GetBufferedRanges(base::TimeDelta duration
) const;
65 // Returns the duration of the buffered data.
66 // Returns base::TimeDelta() if the stream has no buffered data.
67 base::TimeDelta
GetBufferedDuration() const;
69 // Signal to the stream that buffers handed in through subsequent calls to
70 // Append() belong to a media segment that starts at |start_timestamp|.
71 void OnNewMediaSegment(DecodeTimestamp start_timestamp
);
73 // Called when midstream config updates occur.
74 // Returns true if the new config is accepted.
75 // Returns false if the new config should trigger an error.
76 bool UpdateAudioConfig(const AudioDecoderConfig
& config
, const LogCB
& log_cb
);
77 bool UpdateVideoConfig(const VideoDecoderConfig
& config
, const LogCB
& log_cb
);
78 void UpdateTextConfig(const TextTrackConfig
& config
, const LogCB
& log_cb
);
80 void MarkEndOfStream();
81 void UnmarkEndOfStream();
83 // DemuxerStream methods.
84 void Read(const ReadCB
& read_cb
) override
;
85 Type
type() const override
;
86 Liveness
liveness() const override
;
87 AudioDecoderConfig
audio_decoder_config() override
;
88 VideoDecoderConfig
video_decoder_config() override
;
89 bool SupportsConfigChanges() override
;
90 VideoRotation
video_rotation() override
;
92 // Returns the text track configuration. It is an error to call this method
94 TextTrackConfig
text_track_config();
96 // Sets the memory limit, in bytes, on the SourceBufferStream.
97 void set_memory_limit(int memory_limit
) {
98 stream_
->set_memory_limit(memory_limit
);
101 bool supports_partial_append_window_trimming() const {
102 return partial_append_window_trimming_enabled_
;
105 void SetLiveness(Liveness liveness
);
110 RETURNING_DATA_FOR_READS
,
111 RETURNING_ABORT_FOR_READS
,
115 // Assigns |state_| to |state|
116 void ChangeState_Locked(State state
);
118 void CompletePendingReadIfPossible_Locked();
120 // Specifies the type of the stream.
125 scoped_ptr
<SourceBufferStream
> stream_
;
127 mutable base::Lock lock_
;
130 bool splice_frames_enabled_
;
131 bool partial_append_window_trimming_enabled_
;
133 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream
);
136 // Demuxer implementation that allows chunks of media data to be passed
137 // from JavaScript to the media stack.
138 class MEDIA_EXPORT ChunkDemuxer
: public Demuxer
{
141 kOk
, // ID added w/o error.
142 kNotSupported
, // Type specified is not supported.
143 kReachedIdLimit
, // Reached ID limit. We can't handle any more IDs.
146 typedef base::Closure InitSegmentReceivedCB
;
148 // |open_cb| Run when Initialize() is called to signal that the demuxer
149 // is ready to receive media data via AppenData().
150 // |encrypted_media_init_data_cb| Run when the demuxer determines that an
151 // encryption key is needed to decrypt the content.
152 // |enable_text| Process inband text tracks in the normal way when true,
153 // otherwise ignore them.
154 // |log_cb| Run when the demuxer needs to emit MediaLog messages.
155 // |splice_frames_enabled| Indicates that it's okay to generate splice frames
156 // per the MSE specification. Renderers must understand DecoderBuffer's
157 // splice_timestamp() field.
158 ChunkDemuxer(const base::Closure
& open_cb
,
159 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
161 const scoped_refptr
<MediaLog
>& media_log
,
162 bool splice_frames_enabled
);
163 ~ChunkDemuxer() override
;
165 // Demuxer implementation.
166 void Initialize(DemuxerHost
* host
,
167 const PipelineStatusCB
& cb
,
168 bool enable_text_tracks
) override
;
169 void Stop() override
;
170 void Seek(base::TimeDelta time
, const PipelineStatusCB
& cb
) override
;
171 base::Time
GetTimelineOffset() const override
;
172 DemuxerStream
* GetStream(DemuxerStream::Type type
) override
;
173 base::TimeDelta
GetStartTime() const override
;
175 // Methods used by an external object to control this demuxer.
177 // Indicates that a new Seek() call is on its way. Any pending Reads on the
178 // DemuxerStream objects should be aborted immediately inside this call and
179 // future Read calls should return kAborted until the Seek() call occurs.
180 // This method MUST ALWAYS be called before Seek() is called to signal that
181 // the next Seek() call represents the seek point we actually want to return
183 // |seek_time| - The presentation timestamp for the seek that triggered this
184 // call. It represents the most recent position the caller is trying to seek
186 void StartWaitingForSeek(base::TimeDelta seek_time
);
188 // Indicates that a Seek() call is on its way, but another seek has been
189 // requested that will override the impending Seek() call. Any pending Reads
190 // on the DemuxerStream objects should be aborted immediately inside this call
191 // and future Read calls should return kAborted until the next
192 // StartWaitingForSeek() call. This method also arranges for the next Seek()
193 // call received before a StartWaitingForSeek() call to immediately call its
194 // callback without waiting for any data.
195 // |seek_time| - The presentation timestamp for the seek request that
196 // triggered this call. It represents the most recent position the caller is
197 // trying to seek to.
198 void CancelPendingSeek(base::TimeDelta seek_time
);
200 // Registers a new |id| to use for AppendData() calls. |type| indicates
201 // the MIME type for the data that we intend to append for this ID.
202 // kOk is returned if the demuxer has enough resources to support another ID
203 // and supports the format indicated by |type|.
204 // kNotSupported is returned if |type| is not a supported format.
205 // kReachedIdLimit is returned if the demuxer cannot handle another ID right
207 Status
AddId(const std::string
& id
, const std::string
& type
,
208 std::vector
<std::string
>& codecs
);
210 // Removed an ID & associated resources that were previously added with
212 void RemoveId(const std::string
& id
);
214 // Gets the currently buffered ranges for the specified ID.
215 Ranges
<base::TimeDelta
> GetBufferedRanges(const std::string
& id
) const;
217 // Appends media data to the source buffer associated with |id|, applying
218 // and possibly updating |*timestamp_offset| during coded frame processing.
219 // |append_window_start| and |append_window_end| correspond to the MSE spec's
220 // similarly named source buffer attributes that are used in coded frame
222 // |init_segment_received_cb| is run for each newly successfully parsed
223 // initialization segment.
224 void AppendData(const std::string
& id
, const uint8
* data
, size_t length
,
225 base::TimeDelta append_window_start
,
226 base::TimeDelta append_window_end
,
227 base::TimeDelta
* timestamp_offset
,
228 const InitSegmentReceivedCB
& init_segment_received_cb
);
230 // Aborts parsing the current segment and reset the parser to a state where
231 // it can accept a new segment.
232 // Some pending frames can be emitted during that process. These frames are
233 // applied |timestamp_offset|.
234 void Abort(const std::string
& id
,
235 base::TimeDelta append_window_start
,
236 base::TimeDelta append_window_end
,
237 base::TimeDelta
* timestamp_offset
);
239 // Remove buffers between |start| and |end| for the source buffer
240 // associated with |id|.
241 void Remove(const std::string
& id
, base::TimeDelta start
,
242 base::TimeDelta end
);
244 // Returns the current presentation duration.
245 double GetDuration();
246 double GetDuration_Locked();
248 // Notifies the demuxer that the duration of the media has changed to
250 void SetDuration(double duration
);
252 // Returns true if the source buffer associated with |id| is currently parsing
253 // a media segment, or false otherwise.
254 bool IsParsingMediaSegment(const std::string
& id
);
256 // Set the append mode to be applied to subsequent buffers appended to the
257 // source buffer associated with |id|. If |sequence_mode| is true, caller
258 // is requesting "sequence" mode. Otherwise, caller is requesting "segments"
260 void SetSequenceMode(const std::string
& id
, bool sequence_mode
);
262 // Signals the coded frame processor for the source buffer associated with
263 // |id| to update its group start timestamp to be |timestamp_offset| if it is
264 // in sequence append mode.
265 void SetGroupStartTimestampIfInSequenceMode(const std::string
& id
,
266 base::TimeDelta timestamp_offset
);
268 // Called to signal changes in the "end of stream"
269 // state. UnmarkEndOfStream() must not be called if a matching
270 // MarkEndOfStream() has not come before it.
271 void MarkEndOfStream(PipelineStatus status
);
272 void UnmarkEndOfStream();
276 // Sets the memory limit on each stream of a specific type.
277 // |memory_limit| is the maximum number of bytes each stream of type |type|
278 // is allowed to hold in its buffer.
279 void SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
);
281 // Returns the ranges representing the buffered data in the demuxer.
282 // TODO(wolenetz): Remove this method once MediaSourceDelegate no longer
283 // requires it for doing hack browser seeks to I-frame on Android. See
284 // http://crbug.com/304234.
285 Ranges
<base::TimeDelta
> GetBufferedRanges() const;
297 void ChangeState_Locked(State new_state
);
299 // Reports an error and puts the demuxer in a state where it won't accept more
301 void ReportError_Locked(PipelineStatus error
);
303 // Returns true if any stream has seeked to a time without buffered data.
304 bool IsSeekWaitingForData_Locked() const;
306 // Returns true if all streams can successfully call EndOfStream,
307 // false if any can not.
308 bool CanEndOfStream_Locked() const;
310 // SourceState callbacks.
311 void OnSourceInitDone(const StreamParser::InitParameters
& params
);
313 // Creates a DemuxerStream for the specified |type|.
314 // Returns a new ChunkDemuxerStream instance if a stream of this type
315 // has not been created before. Returns NULL otherwise.
316 ChunkDemuxerStream
* CreateDemuxerStream(DemuxerStream::Type type
);
318 void OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
319 const TextTrackConfig
& config
);
321 // Returns true if |source_id| is valid, false otherwise.
322 bool IsValidId(const std::string
& source_id
) const;
324 // Increases |duration_| to |new_duration|, if |new_duration| is higher.
325 void IncreaseDurationIfNecessary(base::TimeDelta new_duration
);
327 // Decreases |duration_| if the buffered region is less than |duration_| when
328 // EndOfStream() is called.
329 void DecreaseDurationIfNecessary();
331 // Sets |duration_| to |new_duration|, sets |user_specified_duration_| to -1
332 // and notifies |host_|.
333 void UpdateDuration(base::TimeDelta new_duration
);
335 // Returns the ranges representing the buffered data in the demuxer.
336 Ranges
<base::TimeDelta
> GetBufferedRanges_Locked() const;
338 // Start returning data on all DemuxerStreams.
339 void StartReturningData();
341 // Aborts pending reads on all DemuxerStreams.
342 void AbortPendingReads();
344 // Completes any pending reads if it is possible to do so.
345 void CompletePendingReadsIfPossible();
347 // Seeks all SourceBufferStreams to |seek_time|.
348 void SeekAllSources(base::TimeDelta seek_time
);
350 // Shuts down all DemuxerStreams by calling Shutdown() on
351 // all objects in |source_state_map_|.
352 void ShutdownAllStreams();
354 mutable base::Lock lock_
;
356 bool cancel_next_seek_
;
359 base::Closure open_cb_
;
360 EncryptedMediaInitDataCB encrypted_media_init_data_cb_
;
362 // Callback used to report log messages that can help the web developer
363 // figure out what is wrong with the content.
365 scoped_refptr
<MediaLog
> media_log_
;
367 PipelineStatusCB init_cb_
;
368 // Callback to execute upon seek completion.
369 // TODO(wolenetz/acolwell): Protect against possible double-locking by first
370 // releasing |lock_| before executing this callback. See
371 // http://crbug.com/308226
372 PipelineStatusCB seek_cb_
;
374 scoped_ptr
<ChunkDemuxerStream
> audio_
;
375 scoped_ptr
<ChunkDemuxerStream
> video_
;
377 base::TimeDelta duration_
;
379 // The duration passed to the last SetDuration(). If
380 // SetDuration() is never called or an AppendData() call or
381 // a EndOfStream() call changes |duration_|, then this
382 // variable is set to < 0 to indicate that the |duration_| represents
383 // the actual duration instead of a user specified value.
384 double user_specified_duration_
;
386 base::Time timeline_offset_
;
387 DemuxerStream::Liveness liveness_
;
389 typedef std::map
<std::string
, SourceState
*> SourceStateMap
;
390 SourceStateMap source_state_map_
;
392 // Used to ensure that (1) config data matches the type and codec provided in
393 // AddId(), (2) only 1 audio and 1 video sources are added, and (3) ids may be
394 // removed with RemoveID() but can not be re-added (yet).
395 std::string source_id_audio_
;
396 std::string source_id_video_
;
398 // Indicates that splice frame generation is enabled.
399 const bool splice_frames_enabled_
;
401 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer
);
406 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_