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 // Appending may update |*timestamp_offset| if |timestamp_offset| is not NULL.
103 void AppendData(const std::string
& id
, const uint8
* data
, size_t length
,
104 double* timestamp_offset
);
106 // Aborts parsing the current segment and reset the parser to a state where
107 // it can accept a new segment.
108 void Abort(const std::string
& id
);
110 // Remove buffers between |start| and |end| for the source buffer
111 // associated with |id|.
112 void Remove(const std::string
& id
, base::TimeDelta start
,
113 base::TimeDelta end
);
115 // Returns the current presentation duration.
116 double GetDuration();
117 double GetDuration_Locked();
119 // Notifies the demuxer that the duration of the media has changed to
121 void SetDuration(double duration
);
123 // Sets a time |offset| to be applied to subsequent buffers appended to the
124 // source buffer associated with |id|. Returns true if the offset is set
125 // properly, false if the offset cannot be applied because we're in the
126 // middle of parsing a media segment.
127 bool SetTimestampOffset(const std::string
& id
, base::TimeDelta offset
);
129 // Set the append mode to be applied to subsequent buffers appended to the
130 // source buffer associated with |id|. If |sequence_mode| is true, caller
131 // is requesting "sequence" mode. Otherwise, caller is requesting "segments"
132 // mode. Returns true if the mode update was allowed. Returns false if
133 // the mode cannot be updated because we're in the middle of parsing a media
135 // In "sequence" mode, appended media will be treated as adjacent in time.
136 // In "segments" mode, timestamps in appended media determine coded frame
138 bool SetSequenceMode(const std::string
& id
, bool sequence_mode
);
140 // Called to signal changes in the "end of stream"
141 // state. UnmarkEndOfStream() must not be called if a matching
142 // MarkEndOfStream() has not come before it.
143 void MarkEndOfStream(PipelineStatus status
);
144 void UnmarkEndOfStream();
146 // Set the append window start and end values for the source buffer
147 // associated with |id|.
148 void SetAppendWindowStart(const std::string
& id
, base::TimeDelta start
);
149 void SetAppendWindowEnd(const std::string
& id
, base::TimeDelta end
);
153 // Sets the memory limit on each stream. |memory_limit| is the
154 // maximum number of bytes each stream is allowed to hold in its buffer.
155 void SetMemoryLimitsForTesting(int memory_limit
);
157 // Returns the ranges representing the buffered data in the demuxer.
158 // TODO(wolenetz): Remove this method once MediaSourceDelegate no longer
159 // requires it for doing hack browser seeks to I-frame on Android. See
160 // http://crbug.com/304234.
161 Ranges
<base::TimeDelta
> GetBufferedRanges() const;
173 void ChangeState_Locked(State new_state
);
175 // Reports an error and puts the demuxer in a state where it won't accept more
177 void ReportError_Locked(PipelineStatus error
);
179 // Returns true if any stream has seeked to a time without buffered data.
180 bool IsSeekWaitingForData_Locked() const;
182 // Returns true if all streams can successfully call EndOfStream,
183 // false if any can not.
184 bool CanEndOfStream_Locked() const;
186 // SourceState callbacks.
187 void OnSourceInitDone(bool success
, base::TimeDelta duration
);
189 // Creates a DemuxerStream for the specified |type|.
190 // Returns a new ChunkDemuxerStream instance if a stream of this type
191 // has not been created before. Returns NULL otherwise.
192 ChunkDemuxerStream
* CreateDemuxerStream(DemuxerStream::Type type
);
194 void OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
195 const TextTrackConfig
& config
);
196 void OnNewMediaSegment(const std::string
& source_id
,
197 base::TimeDelta start_timestamp
);
199 // Returns true if |source_id| is valid, false otherwise.
200 bool IsValidId(const std::string
& source_id
) const;
202 // Increases |duration_| if |last_appended_buffer_timestamp| exceeds the
203 // current |duration_|. The |duration_| is set to the end buffered timestamp
205 void IncreaseDurationIfNecessary(
206 base::TimeDelta last_appended_buffer_timestamp
,
207 ChunkDemuxerStream
* stream
);
209 // Decreases |duration_| if the buffered region is less than |duration_| when
210 // EndOfStream() is called.
211 void DecreaseDurationIfNecessary();
213 // Sets |duration_| to |new_duration|, sets |user_specified_duration_| to -1
214 // and notifies |host_|.
215 void UpdateDuration(base::TimeDelta new_duration
);
217 // Returns the ranges representing the buffered data in the demuxer.
218 Ranges
<base::TimeDelta
> GetBufferedRanges_Locked() const;
220 // Start returning data on all DemuxerStreams.
221 void StartReturningData();
223 // Aborts pending reads on all DemuxerStreams.
224 void AbortPendingReads();
226 // Completes any pending reads if it is possible to do so.
227 void CompletePendingReadsIfPossible();
229 // Seeks all SourceBufferStreams to |seek_time|.
230 void SeekAllSources(base::TimeDelta seek_time
);
232 // Shuts down all DemuxerStreams by calling Shutdown() on
233 // all objects in |source_state_map_|.
234 void ShutdownAllStreams();
236 mutable base::Lock lock_
;
238 bool cancel_next_seek_
;
241 base::Closure open_cb_
;
242 NeedKeyCB need_key_cb_
;
244 // Callback used to report error strings that can help the web developer
245 // figure out what is wrong with the content.
248 PipelineStatusCB init_cb_
;
249 // Callback to execute upon seek completion.
250 // TODO(wolenetz/acolwell): Protect against possible double-locking by first
251 // releasing |lock_| before executing this callback. See
252 // http://crbug.com/308226
253 PipelineStatusCB seek_cb_
;
255 scoped_ptr
<ChunkDemuxerStream
> audio_
;
256 scoped_ptr
<ChunkDemuxerStream
> video_
;
258 // Keeps |audio_| alive when audio has been disabled.
259 scoped_ptr
<ChunkDemuxerStream
> disabled_audio_
;
261 base::TimeDelta duration_
;
263 // The duration passed to the last SetDuration(). If
264 // SetDuration() is never called or an AppendData() call or
265 // a EndOfStream() call changes |duration_|, then this
266 // variable is set to < 0 to indicate that the |duration_| represents
267 // the actual duration instead of a user specified value.
268 double user_specified_duration_
;
270 typedef std::map
<std::string
, SourceState
*> SourceStateMap
;
271 SourceStateMap source_state_map_
;
273 // Used to ensure that (1) config data matches the type and codec provided in
274 // AddId(), (2) only 1 audio and 1 video sources are added, and (3) ids may be
275 // removed with RemoveID() but can not be re-added (yet).
276 std::string source_id_audio_
;
277 std::string source_id_video_
;
279 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer
);
284 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_