[MD settings] moving attached() code
[chromium-blink-merge.git] / media / filters / chunk_demuxer.h
blob466818378ad9788809417dece69d6ed8d7f1fbc4
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_
8 #include <deque>
9 #include <map>
10 #include <string>
11 #include <utility>
12 #include <vector>
14 #include "base/basictypes.h"
15 #include "base/synchronization/lock.h"
16 #include "media/base/byte_queue.h"
17 #include "media/base/demuxer.h"
18 #include "media/base/demuxer_stream.h"
19 #include "media/base/ranges.h"
20 #include "media/base/stream_parser.h"
21 #include "media/filters/source_buffer_stream.h"
23 namespace media {
25 class FFmpegURLProtocol;
26 class SourceState;
28 class MEDIA_EXPORT ChunkDemuxerStream : public DemuxerStream {
29 public:
30 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
32 ChunkDemuxerStream(Type type, bool splice_frames_enabled);
33 ~ChunkDemuxerStream() override;
35 // ChunkDemuxerStream control methods.
36 void StartReturningData();
37 void AbortReads();
38 void CompletePendingReadIfPossible();
39 void Shutdown();
41 // SourceBufferStream manipulation methods.
42 void Seek(base::TimeDelta time);
43 bool IsSeekWaitingForData() const;
45 // Add buffers to this stream. Buffers are stored in SourceBufferStreams,
46 // which handle ordering and overlap resolution.
47 // Returns true if buffers were successfully added.
48 bool Append(const StreamParser::BufferQueue& buffers);
50 // Removes buffers between |start| and |end| according to the steps
51 // in the "Coded Frame Removal Algorithm" in the Media Source
52 // Extensions Spec.
53 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
55 // |duration| is the current duration of the presentation. It is
56 // required by the computation outlined in the spec.
57 void Remove(base::TimeDelta start, base::TimeDelta end,
58 base::TimeDelta duration);
60 // If the buffer is full, attempts to try to free up space, as specified in
61 // the "Coded Frame Eviction Algorithm" in the Media Source Extensions Spec.
62 // Returns false iff buffer is still full after running eviction.
63 // https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction
64 bool EvictCodedFrames(DecodeTimestamp media_time, size_t newDataSize);
66 // Signal to the stream that duration has changed to |duration|.
67 void OnSetDuration(base::TimeDelta duration);
69 // Returns the range of buffered data in this stream, capped at |duration|.
70 Ranges<base::TimeDelta> GetBufferedRanges(base::TimeDelta duration) const;
72 // Returns the duration of the buffered data.
73 // Returns base::TimeDelta() if the stream has no buffered data.
74 base::TimeDelta GetBufferedDuration() const;
76 // Returns the size of the buffered data in bytes.
77 size_t GetBufferedSize() const;
79 // Signal to the stream that buffers handed in through subsequent calls to
80 // Append() belong to a media segment that starts at |start_timestamp|.
81 void OnNewMediaSegment(DecodeTimestamp start_timestamp);
83 // Called when midstream config updates occur.
84 // Returns true if the new config is accepted.
85 // Returns false if the new config should trigger an error.
86 bool UpdateAudioConfig(const AudioDecoderConfig& config,
87 const scoped_refptr<MediaLog>& media_log);
88 bool UpdateVideoConfig(const VideoDecoderConfig& config,
89 const scoped_refptr<MediaLog>& media_log);
90 void UpdateTextConfig(const TextTrackConfig& config,
91 const scoped_refptr<MediaLog>& media_log);
93 void MarkEndOfStream();
94 void UnmarkEndOfStream();
96 // DemuxerStream methods.
97 void Read(const ReadCB& read_cb) override;
98 Type type() const override;
99 Liveness liveness() const override;
100 AudioDecoderConfig audio_decoder_config() override;
101 VideoDecoderConfig video_decoder_config() override;
102 bool SupportsConfigChanges() override;
103 VideoRotation video_rotation() override;
105 // Returns the text track configuration. It is an error to call this method
106 // if type() != TEXT.
107 TextTrackConfig text_track_config();
109 // Sets the memory limit, in bytes, on the SourceBufferStream.
110 void SetStreamMemoryLimit(size_t memory_limit);
112 bool supports_partial_append_window_trimming() const {
113 return partial_append_window_trimming_enabled_;
116 void SetLiveness(Liveness liveness);
118 private:
119 enum State {
120 UNINITIALIZED,
121 RETURNING_DATA_FOR_READS,
122 RETURNING_ABORT_FOR_READS,
123 SHUTDOWN,
126 // Assigns |state_| to |state|
127 void ChangeState_Locked(State state);
129 void CompletePendingReadIfPossible_Locked();
131 // Specifies the type of the stream.
132 Type type_;
134 Liveness liveness_;
136 scoped_ptr<SourceBufferStream> stream_;
138 mutable base::Lock lock_;
139 State state_;
140 ReadCB read_cb_;
141 bool splice_frames_enabled_;
142 bool partial_append_window_trimming_enabled_;
144 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream);
147 // Demuxer implementation that allows chunks of media data to be passed
148 // from JavaScript to the media stack.
149 class MEDIA_EXPORT ChunkDemuxer : public Demuxer {
150 public:
151 enum Status {
152 kOk, // ID added w/o error.
153 kNotSupported, // Type specified is not supported.
154 kReachedIdLimit, // Reached ID limit. We can't handle any more IDs.
157 typedef base::Closure InitSegmentReceivedCB;
159 // |open_cb| Run when Initialize() is called to signal that the demuxer
160 // is ready to receive media data via AppenData().
161 // |encrypted_media_init_data_cb| Run when the demuxer determines that an
162 // encryption key is needed to decrypt the content.
163 // |media_log| Used to report content and engine debug messages.
164 // |splice_frames_enabled| Indicates that it's okay to generate splice frames
165 // per the MSE specification. Renderers must understand DecoderBuffer's
166 // splice_timestamp() field.
167 ChunkDemuxer(const base::Closure& open_cb,
168 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
169 const scoped_refptr<MediaLog>& media_log,
170 bool splice_frames_enabled);
171 ~ChunkDemuxer() override;
173 // Demuxer implementation.
174 std::string GetDisplayName() const override;
176 // |enable_text| Process inband text tracks in the normal way when true,
177 // otherwise ignore them.
178 void Initialize(DemuxerHost* host,
179 const PipelineStatusCB& cb,
180 bool enable_text_tracks) override;
181 void Stop() override;
182 void Seek(base::TimeDelta time, const PipelineStatusCB& cb) override;
183 base::Time GetTimelineOffset() const override;
184 DemuxerStream* GetStream(DemuxerStream::Type type) override;
185 base::TimeDelta GetStartTime() const override;
187 // Methods used by an external object to control this demuxer.
189 // Indicates that a new Seek() call is on its way. Any pending Reads on the
190 // DemuxerStream objects should be aborted immediately inside this call and
191 // future Read calls should return kAborted until the Seek() call occurs.
192 // This method MUST ALWAYS be called before Seek() is called to signal that
193 // the next Seek() call represents the seek point we actually want to return
194 // data for.
195 // |seek_time| - The presentation timestamp for the seek that triggered this
196 // call. It represents the most recent position the caller is trying to seek
197 // to.
198 void StartWaitingForSeek(base::TimeDelta seek_time);
200 // Indicates that a Seek() call is on its way, but another seek has been
201 // requested that will override the impending Seek() call. Any pending Reads
202 // on the DemuxerStream objects should be aborted immediately inside this call
203 // and future Read calls should return kAborted until the next
204 // StartWaitingForSeek() call. This method also arranges for the next Seek()
205 // call received before a StartWaitingForSeek() call to immediately call its
206 // callback without waiting for any data.
207 // |seek_time| - The presentation timestamp for the seek request that
208 // triggered this call. It represents the most recent position the caller is
209 // trying to seek to.
210 void CancelPendingSeek(base::TimeDelta seek_time);
212 // Registers a new |id| to use for AppendData() calls. |type| indicates
213 // the MIME type for the data that we intend to append for this ID.
214 // kOk is returned if the demuxer has enough resources to support another ID
215 // and supports the format indicated by |type|.
216 // kNotSupported is returned if |type| is not a supported format.
217 // kReachedIdLimit is returned if the demuxer cannot handle another ID right
218 // now.
219 Status AddId(const std::string& id, const std::string& type,
220 std::vector<std::string>& codecs);
222 // Removed an ID & associated resources that were previously added with
223 // AddId().
224 void RemoveId(const std::string& id);
226 // Gets the currently buffered ranges for the specified ID.
227 Ranges<base::TimeDelta> GetBufferedRanges(const std::string& id) const;
229 // Appends media data to the source buffer associated with |id|, applying
230 // and possibly updating |*timestamp_offset| during coded frame processing.
231 // |append_window_start| and |append_window_end| correspond to the MSE spec's
232 // similarly named source buffer attributes that are used in coded frame
233 // processing.
234 // |init_segment_received_cb| is run for each newly successfully parsed
235 // initialization segment.
236 void AppendData(const std::string& id, const uint8* data, size_t length,
237 base::TimeDelta append_window_start,
238 base::TimeDelta append_window_end,
239 base::TimeDelta* timestamp_offset,
240 const InitSegmentReceivedCB& init_segment_received_cb);
242 // Aborts parsing the current segment and reset the parser to a state where
243 // it can accept a new segment.
244 // Some pending frames can be emitted during that process. These frames are
245 // applied |timestamp_offset|.
246 void ResetParserState(const std::string& id,
247 base::TimeDelta append_window_start,
248 base::TimeDelta append_window_end,
249 base::TimeDelta* timestamp_offset);
251 // Remove buffers between |start| and |end| for the source buffer
252 // associated with |id|.
253 void Remove(const std::string& id, base::TimeDelta start,
254 base::TimeDelta end);
256 // If the buffer is full, attempts to try to free up space, as specified in
257 // the "Coded Frame Eviction Algorithm" in the Media Source Extensions Spec.
258 // Returns false iff buffer is still full after running eviction.
259 // https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction
260 bool EvictCodedFrames(const std::string& id,
261 base::TimeDelta currentMediaTime,
262 size_t newDataSize);
264 // Returns the current presentation duration.
265 double GetDuration();
266 double GetDuration_Locked();
268 // Notifies the demuxer that the duration of the media has changed to
269 // |duration|.
270 void SetDuration(double duration);
272 // Returns true if the source buffer associated with |id| is currently parsing
273 // a media segment, or false otherwise.
274 bool IsParsingMediaSegment(const std::string& id);
276 // Set the append mode to be applied to subsequent buffers appended to the
277 // source buffer associated with |id|. If |sequence_mode| is true, caller
278 // is requesting "sequence" mode. Otherwise, caller is requesting "segments"
279 // mode.
280 void SetSequenceMode(const std::string& id, bool sequence_mode);
282 // Signals the coded frame processor for the source buffer associated with
283 // |id| to update its group start timestamp to be |timestamp_offset| if it is
284 // in sequence append mode.
285 void SetGroupStartTimestampIfInSequenceMode(const std::string& id,
286 base::TimeDelta timestamp_offset);
288 // Called to signal changes in the "end of stream"
289 // state. UnmarkEndOfStream() must not be called if a matching
290 // MarkEndOfStream() has not come before it.
291 void MarkEndOfStream(PipelineStatus status);
292 void UnmarkEndOfStream();
294 void Shutdown();
296 // Sets the memory limit on each stream of a specific type.
297 // |memory_limit| is the maximum number of bytes each stream of type |type|
298 // is allowed to hold in its buffer.
299 void SetMemoryLimits(DemuxerStream::Type type, size_t memory_limit);
301 // Returns the ranges representing the buffered data in the demuxer.
302 // TODO(wolenetz): Remove this method once MediaSourceDelegate no longer
303 // requires it for doing hack browser seeks to I-frame on Android. See
304 // http://crbug.com/304234.
305 Ranges<base::TimeDelta> GetBufferedRanges() const;
307 private:
308 enum State {
309 WAITING_FOR_INIT,
310 INITIALIZING,
311 INITIALIZED,
312 ENDED,
313 PARSE_ERROR,
314 SHUTDOWN,
317 void ChangeState_Locked(State new_state);
319 // Reports an error and puts the demuxer in a state where it won't accept more
320 // data.
321 void ReportError_Locked(PipelineStatus error);
323 // Returns true if any stream has seeked to a time without buffered data.
324 bool IsSeekWaitingForData_Locked() const;
326 // Returns true if all streams can successfully call EndOfStream,
327 // false if any can not.
328 bool CanEndOfStream_Locked() const;
330 // SourceState callbacks.
331 void OnSourceInitDone(const StreamParser::InitParameters& params);
333 // Creates a DemuxerStream for the specified |type|.
334 // Returns a new ChunkDemuxerStream instance if a stream of this type
335 // has not been created before. Returns NULL otherwise.
336 ChunkDemuxerStream* CreateDemuxerStream(DemuxerStream::Type type);
338 void OnNewTextTrack(ChunkDemuxerStream* text_stream,
339 const TextTrackConfig& config);
341 // Returns true if |source_id| is valid, false otherwise.
342 bool IsValidId(const std::string& source_id) const;
344 // Increases |duration_| to |new_duration|, if |new_duration| is higher.
345 void IncreaseDurationIfNecessary(base::TimeDelta new_duration);
347 // Decreases |duration_| if the buffered region is less than |duration_| when
348 // EndOfStream() is called.
349 void DecreaseDurationIfNecessary();
351 // Sets |duration_| to |new_duration|, sets |user_specified_duration_| to -1
352 // and notifies |host_|.
353 void UpdateDuration(base::TimeDelta new_duration);
355 // Returns the ranges representing the buffered data in the demuxer.
356 Ranges<base::TimeDelta> GetBufferedRanges_Locked() const;
358 // Start returning data on all DemuxerStreams.
359 void StartReturningData();
361 // Aborts pending reads on all DemuxerStreams.
362 void AbortPendingReads();
364 // Completes any pending reads if it is possible to do so.
365 void CompletePendingReadsIfPossible();
367 // Seeks all SourceBufferStreams to |seek_time|.
368 void SeekAllSources(base::TimeDelta seek_time);
370 // Shuts down all DemuxerStreams by calling Shutdown() on
371 // all objects in |source_state_map_|.
372 void ShutdownAllStreams();
374 mutable base::Lock lock_;
375 State state_;
376 bool cancel_next_seek_;
378 DemuxerHost* host_;
379 base::Closure open_cb_;
380 EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
381 bool enable_text_;
383 // MediaLog for reporting messages and properties to debug content and engine.
384 scoped_refptr<MediaLog> media_log_;
386 PipelineStatusCB init_cb_;
387 // Callback to execute upon seek completion.
388 // TODO(wolenetz/acolwell): Protect against possible double-locking by first
389 // releasing |lock_| before executing this callback. See
390 // http://crbug.com/308226
391 PipelineStatusCB seek_cb_;
393 scoped_ptr<ChunkDemuxerStream> audio_;
394 scoped_ptr<ChunkDemuxerStream> video_;
396 base::TimeDelta duration_;
398 // The duration passed to the last SetDuration(). If
399 // SetDuration() is never called or an AppendData() call or
400 // a EndOfStream() call changes |duration_|, then this
401 // variable is set to < 0 to indicate that the |duration_| represents
402 // the actual duration instead of a user specified value.
403 double user_specified_duration_;
405 base::Time timeline_offset_;
406 DemuxerStream::Liveness liveness_;
408 typedef std::map<std::string, SourceState*> SourceStateMap;
409 SourceStateMap source_state_map_;
411 // Used to ensure that (1) config data matches the type and codec provided in
412 // AddId(), (2) only 1 audio and 1 video sources are added, and (3) ids may be
413 // removed with RemoveID() but can not be re-added (yet).
414 std::string source_id_audio_;
415 std::string source_id_video_;
417 // Indicates that splice frame generation is enabled.
418 const bool splice_frames_enabled_;
420 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer);
423 } // namespace media
425 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_