[AndroidWebViewShell] Replace rebaseline script with a new version using test_runner.py
[chromium-blink-merge.git] / media / filters / chunk_demuxer.h
blob350803e4b4815598e58d9f91b18e4819b65665d7
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/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"
22 namespace media {
24 class FFmpegURLProtocol;
25 class SourceState;
27 class MEDIA_EXPORT ChunkDemuxerStream : public DemuxerStream {
28 public:
29 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
31 ChunkDemuxerStream(Type type, bool splice_frames_enabled);
32 ~ChunkDemuxerStream() override;
34 // ChunkDemuxerStream control methods.
35 void StartReturningData();
36 void AbortReads();
37 void CompletePendingReadIfPossible();
38 void Shutdown();
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
51 // Extensions Spec.
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,
77 const scoped_refptr<MediaLog>& media_log);
78 bool UpdateVideoConfig(const VideoDecoderConfig& config,
79 const scoped_refptr<MediaLog>& media_log);
80 void UpdateTextConfig(const TextTrackConfig& config,
81 const scoped_refptr<MediaLog>& media_log);
83 void MarkEndOfStream();
84 void UnmarkEndOfStream();
86 // DemuxerStream methods.
87 void Read(const ReadCB& read_cb) override;
88 Type type() const override;
89 Liveness liveness() const override;
90 AudioDecoderConfig audio_decoder_config() override;
91 VideoDecoderConfig video_decoder_config() override;
92 bool SupportsConfigChanges() override;
93 VideoRotation video_rotation() override;
95 // Returns the text track configuration. It is an error to call this method
96 // if type() != TEXT.
97 TextTrackConfig text_track_config();
99 // Sets the memory limit, in bytes, on the SourceBufferStream.
100 void set_memory_limit(int memory_limit) {
101 stream_->set_memory_limit(memory_limit);
104 bool supports_partial_append_window_trimming() const {
105 return partial_append_window_trimming_enabled_;
108 void SetLiveness(Liveness liveness);
110 private:
111 enum State {
112 UNINITIALIZED,
113 RETURNING_DATA_FOR_READS,
114 RETURNING_ABORT_FOR_READS,
115 SHUTDOWN,
118 // Assigns |state_| to |state|
119 void ChangeState_Locked(State state);
121 void CompletePendingReadIfPossible_Locked();
123 // Specifies the type of the stream.
124 Type type_;
126 Liveness liveness_;
128 scoped_ptr<SourceBufferStream> stream_;
130 mutable base::Lock lock_;
131 State state_;
132 ReadCB read_cb_;
133 bool splice_frames_enabled_;
134 bool partial_append_window_trimming_enabled_;
136 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream);
139 // Demuxer implementation that allows chunks of media data to be passed
140 // from JavaScript to the media stack.
141 class MEDIA_EXPORT ChunkDemuxer : public Demuxer {
142 public:
143 enum Status {
144 kOk, // ID added w/o error.
145 kNotSupported, // Type specified is not supported.
146 kReachedIdLimit, // Reached ID limit. We can't handle any more IDs.
149 typedef base::Closure InitSegmentReceivedCB;
151 // |open_cb| Run when Initialize() is called to signal that the demuxer
152 // is ready to receive media data via AppenData().
153 // |encrypted_media_init_data_cb| Run when the demuxer determines that an
154 // encryption key is needed to decrypt the content.
155 // |media_log| Used to report content and engine debug messages.
156 // |splice_frames_enabled| Indicates that it's okay to generate splice frames
157 // per the MSE specification. Renderers must understand DecoderBuffer's
158 // splice_timestamp() field.
159 ChunkDemuxer(const base::Closure& open_cb,
160 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 std::string GetDisplayName() const override;
168 // |enable_text| Process inband text tracks in the normal way when true,
169 // otherwise ignore them.
170 void Initialize(DemuxerHost* host,
171 const PipelineStatusCB& cb,
172 bool enable_text_tracks) override;
173 void Stop() override;
174 void Seek(base::TimeDelta time, const PipelineStatusCB& cb) override;
175 base::Time GetTimelineOffset() const override;
176 DemuxerStream* GetStream(DemuxerStream::Type type) override;
177 base::TimeDelta GetStartTime() const override;
179 // Methods used by an external object to control this demuxer.
181 // Indicates that a new Seek() call is on its way. Any pending Reads on the
182 // DemuxerStream objects should be aborted immediately inside this call and
183 // future Read calls should return kAborted until the Seek() call occurs.
184 // This method MUST ALWAYS be called before Seek() is called to signal that
185 // the next Seek() call represents the seek point we actually want to return
186 // data for.
187 // |seek_time| - The presentation timestamp for the seek that triggered this
188 // call. It represents the most recent position the caller is trying to seek
189 // to.
190 void StartWaitingForSeek(base::TimeDelta seek_time);
192 // Indicates that a Seek() call is on its way, but another seek has been
193 // requested that will override the impending Seek() call. Any pending Reads
194 // on the DemuxerStream objects should be aborted immediately inside this call
195 // and future Read calls should return kAborted until the next
196 // StartWaitingForSeek() call. This method also arranges for the next Seek()
197 // call received before a StartWaitingForSeek() call to immediately call its
198 // callback without waiting for any data.
199 // |seek_time| - The presentation timestamp for the seek request that
200 // triggered this call. It represents the most recent position the caller is
201 // trying to seek to.
202 void CancelPendingSeek(base::TimeDelta seek_time);
204 // Registers a new |id| to use for AppendData() calls. |type| indicates
205 // the MIME type for the data that we intend to append for this ID.
206 // kOk is returned if the demuxer has enough resources to support another ID
207 // and supports the format indicated by |type|.
208 // kNotSupported is returned if |type| is not a supported format.
209 // kReachedIdLimit is returned if the demuxer cannot handle another ID right
210 // now.
211 Status AddId(const std::string& id, const std::string& type,
212 std::vector<std::string>& codecs);
214 // Removed an ID & associated resources that were previously added with
215 // AddId().
216 void RemoveId(const std::string& id);
218 // Gets the currently buffered ranges for the specified ID.
219 Ranges<base::TimeDelta> GetBufferedRanges(const std::string& id) const;
221 // Appends media data to the source buffer associated with |id|, applying
222 // and possibly updating |*timestamp_offset| during coded frame processing.
223 // |append_window_start| and |append_window_end| correspond to the MSE spec's
224 // similarly named source buffer attributes that are used in coded frame
225 // processing.
226 // |init_segment_received_cb| is run for each newly successfully parsed
227 // initialization segment.
228 void AppendData(const std::string& id, const uint8* data, size_t length,
229 base::TimeDelta append_window_start,
230 base::TimeDelta append_window_end,
231 base::TimeDelta* timestamp_offset,
232 const InitSegmentReceivedCB& init_segment_received_cb);
234 // Aborts parsing the current segment and reset the parser to a state where
235 // it can accept a new segment.
236 // Some pending frames can be emitted during that process. These frames are
237 // applied |timestamp_offset|.
238 void Abort(const std::string& id,
239 base::TimeDelta append_window_start,
240 base::TimeDelta append_window_end,
241 base::TimeDelta* timestamp_offset);
243 // Remove buffers between |start| and |end| for the source buffer
244 // associated with |id|.
245 void Remove(const std::string& id, base::TimeDelta start,
246 base::TimeDelta end);
248 // Returns the current presentation duration.
249 double GetDuration();
250 double GetDuration_Locked();
252 // Notifies the demuxer that the duration of the media has changed to
253 // |duration|.
254 void SetDuration(double duration);
256 // Returns true if the source buffer associated with |id| is currently parsing
257 // a media segment, or false otherwise.
258 bool IsParsingMediaSegment(const std::string& id);
260 // Set the append mode to be applied to subsequent buffers appended to the
261 // source buffer associated with |id|. If |sequence_mode| is true, caller
262 // is requesting "sequence" mode. Otherwise, caller is requesting "segments"
263 // mode.
264 void SetSequenceMode(const std::string& id, bool sequence_mode);
266 // Signals the coded frame processor for the source buffer associated with
267 // |id| to update its group start timestamp to be |timestamp_offset| if it is
268 // in sequence append mode.
269 void SetGroupStartTimestampIfInSequenceMode(const std::string& id,
270 base::TimeDelta timestamp_offset);
272 // Called to signal changes in the "end of stream"
273 // state. UnmarkEndOfStream() must not be called if a matching
274 // MarkEndOfStream() has not come before it.
275 void MarkEndOfStream(PipelineStatus status);
276 void UnmarkEndOfStream();
278 void Shutdown();
280 // Sets the memory limit on each stream of a specific type.
281 // |memory_limit| is the maximum number of bytes each stream of type |type|
282 // is allowed to hold in its buffer.
283 void SetMemoryLimits(DemuxerStream::Type type, int memory_limit);
285 // Returns the ranges representing the buffered data in the demuxer.
286 // TODO(wolenetz): Remove this method once MediaSourceDelegate no longer
287 // requires it for doing hack browser seeks to I-frame on Android. See
288 // http://crbug.com/304234.
289 Ranges<base::TimeDelta> GetBufferedRanges() const;
291 private:
292 enum State {
293 WAITING_FOR_INIT,
294 INITIALIZING,
295 INITIALIZED,
296 ENDED,
297 PARSE_ERROR,
298 SHUTDOWN,
301 void ChangeState_Locked(State new_state);
303 // Reports an error and puts the demuxer in a state where it won't accept more
304 // data.
305 void ReportError_Locked(PipelineStatus error);
307 // Returns true if any stream has seeked to a time without buffered data.
308 bool IsSeekWaitingForData_Locked() const;
310 // Returns true if all streams can successfully call EndOfStream,
311 // false if any can not.
312 bool CanEndOfStream_Locked() const;
314 // SourceState callbacks.
315 void OnSourceInitDone(const StreamParser::InitParameters& params);
317 // Creates a DemuxerStream for the specified |type|.
318 // Returns a new ChunkDemuxerStream instance if a stream of this type
319 // has not been created before. Returns NULL otherwise.
320 ChunkDemuxerStream* CreateDemuxerStream(DemuxerStream::Type type);
322 void OnNewTextTrack(ChunkDemuxerStream* text_stream,
323 const TextTrackConfig& config);
325 // Returns true if |source_id| is valid, false otherwise.
326 bool IsValidId(const std::string& source_id) const;
328 // Increases |duration_| to |new_duration|, if |new_duration| is higher.
329 void IncreaseDurationIfNecessary(base::TimeDelta new_duration);
331 // Decreases |duration_| if the buffered region is less than |duration_| when
332 // EndOfStream() is called.
333 void DecreaseDurationIfNecessary();
335 // Sets |duration_| to |new_duration|, sets |user_specified_duration_| to -1
336 // and notifies |host_|.
337 void UpdateDuration(base::TimeDelta new_duration);
339 // Returns the ranges representing the buffered data in the demuxer.
340 Ranges<base::TimeDelta> GetBufferedRanges_Locked() const;
342 // Start returning data on all DemuxerStreams.
343 void StartReturningData();
345 // Aborts pending reads on all DemuxerStreams.
346 void AbortPendingReads();
348 // Completes any pending reads if it is possible to do so.
349 void CompletePendingReadsIfPossible();
351 // Seeks all SourceBufferStreams to |seek_time|.
352 void SeekAllSources(base::TimeDelta seek_time);
354 // Shuts down all DemuxerStreams by calling Shutdown() on
355 // all objects in |source_state_map_|.
356 void ShutdownAllStreams();
358 mutable base::Lock lock_;
359 State state_;
360 bool cancel_next_seek_;
362 DemuxerHost* host_;
363 base::Closure open_cb_;
364 EncryptedMediaInitDataCB encrypted_media_init_data_cb_;
365 bool enable_text_;
367 // MediaLog for reporting messages and properties to debug content and engine.
368 scoped_refptr<MediaLog> media_log_;
370 PipelineStatusCB init_cb_;
371 // Callback to execute upon seek completion.
372 // TODO(wolenetz/acolwell): Protect against possible double-locking by first
373 // releasing |lock_| before executing this callback. See
374 // http://crbug.com/308226
375 PipelineStatusCB seek_cb_;
377 scoped_ptr<ChunkDemuxerStream> audio_;
378 scoped_ptr<ChunkDemuxerStream> video_;
380 base::TimeDelta duration_;
382 // The duration passed to the last SetDuration(). If
383 // SetDuration() is never called or an AppendData() call or
384 // a EndOfStream() call changes |duration_|, then this
385 // variable is set to < 0 to indicate that the |duration_| represents
386 // the actual duration instead of a user specified value.
387 double user_specified_duration_;
389 base::Time timeline_offset_;
390 DemuxerStream::Liveness liveness_;
392 typedef std::map<std::string, SourceState*> SourceStateMap;
393 SourceStateMap source_state_map_;
395 // Used to ensure that (1) config data matches the type and codec provided in
396 // AddId(), (2) only 1 audio and 1 video sources are added, and (3) ids may be
397 // removed with RemoveID() but can not be re-added (yet).
398 std::string source_id_audio_;
399 std::string source_id_video_;
401 // Indicates that splice frame generation is enabled.
402 const bool splice_frames_enabled_;
404 DISALLOW_COPY_AND_ASSIGN(ChunkDemuxer);
407 } // namespace media
409 #endif // MEDIA_FILTERS_CHUNK_DEMUXER_H_