Update V8 to version 4.7.24.
[chromium-blink-merge.git] / media / filters / chunk_demuxer.cc
blob1a5517aceb6103107d57760b62cc15bbea505f17
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 #include "media/filters/chunk_demuxer.h"
7 #include <algorithm>
8 #include <limits>
9 #include <list>
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/location.h"
14 #include "base/stl_util.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "media/base/stream_parser_buffer.h"
18 #include "media/base/video_decoder_config.h"
19 #include "media/filters/frame_processor.h"
20 #include "media/filters/stream_parser_factory.h"
22 using base::TimeDelta;
24 namespace media {
26 static TimeDelta EndTimestamp(const StreamParser::BufferQueue& queue) {
27 return queue.back()->timestamp() + queue.back()->duration();
30 // List of time ranges for each SourceBuffer.
31 typedef std::list<Ranges<TimeDelta> > RangesList;
32 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
33 bool ended) {
34 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
35 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
37 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
38 // TimeRanges object and abort these steps.
39 if (activeRanges.empty())
40 return Ranges<TimeDelta>();
42 // Step 2: Let active ranges be the ranges returned by buffered for each
43 // SourceBuffer object in activeSourceBuffers.
44 // Step 3: Let highest end time be the largest range end time in the active
45 // ranges.
46 TimeDelta highest_end_time;
47 for (RangesList::const_iterator itr = activeRanges.begin();
48 itr != activeRanges.end(); ++itr) {
49 if (!itr->size())
50 continue;
52 highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
55 // Step 4: Let intersection ranges equal a TimeRange object containing a
56 // single range from 0 to highest end time.
57 Ranges<TimeDelta> intersection_ranges;
58 intersection_ranges.Add(TimeDelta(), highest_end_time);
60 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
61 // following steps:
62 for (RangesList::const_iterator itr = activeRanges.begin();
63 itr != activeRanges.end(); ++itr) {
64 // Step 5.1: Let source ranges equal the ranges returned by the buffered
65 // attribute on the current SourceBuffer.
66 Ranges<TimeDelta> source_ranges = *itr;
68 // Step 5.2: If readyState is "ended", then set the end time on the last
69 // range in source ranges to highest end time.
70 if (ended && source_ranges.size() > 0u) {
71 source_ranges.Add(source_ranges.start(source_ranges.size() - 1),
72 highest_end_time);
75 // Step 5.3: Let new intersection ranges equal the intersection between
76 // the intersection ranges and the source ranges.
77 // Step 5.4: Replace the ranges in intersection ranges with the new
78 // intersection ranges.
79 intersection_ranges = intersection_ranges.IntersectionWith(source_ranges);
82 return intersection_ranges;
85 // Contains state belonging to a source id.
86 // TODO: SourceState needs to be moved to a separate file and covered with unit
87 // tests (see crbug.com/525836)
88 class SourceState {
89 public:
90 // Callback signature used to create ChunkDemuxerStreams.
91 typedef base::Callback<ChunkDemuxerStream*(
92 DemuxerStream::Type)> CreateDemuxerStreamCB;
94 typedef ChunkDemuxer::InitSegmentReceivedCB InitSegmentReceivedCB;
96 typedef base::Callback<void(
97 ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
99 SourceState(scoped_ptr<StreamParser> stream_parser,
100 scoped_ptr<FrameProcessor> frame_processor,
101 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
102 const scoped_refptr<MediaLog>& media_log);
104 ~SourceState();
106 void Init(const StreamParser::InitCB& init_cb,
107 bool allow_audio,
108 bool allow_video,
109 const StreamParser::EncryptedMediaInitDataCB&
110 encrypted_media_init_data_cb,
111 const NewTextTrackCB& new_text_track_cb);
113 // Appends new data to the StreamParser.
114 // Returns true if the data was successfully appended. Returns false if an
115 // error occurred. |*timestamp_offset| is used and possibly updated by the
116 // append. |append_window_start| and |append_window_end| correspond to the MSE
117 // spec's similarly named source buffer attributes that are used in coded
118 // frame processing. |init_segment_received_cb| is run for each new fully
119 // parsed initialization segment.
120 bool Append(const uint8* data,
121 size_t length,
122 TimeDelta append_window_start,
123 TimeDelta append_window_end,
124 TimeDelta* timestamp_offset,
125 const InitSegmentReceivedCB& init_segment_received_cb);
127 // Aborts the current append sequence and resets the parser.
128 void Abort(TimeDelta append_window_start,
129 TimeDelta append_window_end,
130 TimeDelta* timestamp_offset);
132 // Calls Remove(|start|, |end|, |duration|) on all
133 // ChunkDemuxerStreams managed by this object.
134 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
136 // If the buffer is full, attempts to try to free up space, as specified in
137 // the "Coded Frame Eviction Algorithm" in the Media Source Extensions Spec.
138 // Returns false iff buffer is still full after running eviction.
139 // https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction
140 bool EvictCodedFrames(DecodeTimestamp media_time, size_t newDataSize);
142 // Returns true if currently parsing a media segment, or false otherwise.
143 bool parsing_media_segment() const { return parsing_media_segment_; }
145 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
146 void SetSequenceMode(bool sequence_mode);
148 // Signals the coded frame processor to update its group start timestamp to be
149 // |timestamp_offset| if it is in sequence append mode.
150 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset);
152 // Returns the range of buffered data in this source, capped at |duration|.
153 // |ended| - Set to true if end of stream has been signaled and the special
154 // end of stream range logic needs to be executed.
155 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
157 // Returns the highest buffered duration across all streams managed
158 // by this object.
159 // Returns TimeDelta() if none of the streams contain buffered data.
160 TimeDelta GetMaxBufferedDuration() const;
162 // Helper methods that call methods with similar names on all the
163 // ChunkDemuxerStreams managed by this object.
164 void StartReturningData();
165 void AbortReads();
166 void Seek(TimeDelta seek_time);
167 void CompletePendingReadIfPossible();
168 void OnSetDuration(TimeDelta duration);
169 void MarkEndOfStream();
170 void UnmarkEndOfStream();
171 void Shutdown();
172 // Sets the memory limit on each stream of a specific type.
173 // |memory_limit| is the maximum number of bytes each stream of type |type|
174 // is allowed to hold in its buffer.
175 void SetMemoryLimits(DemuxerStream::Type type, size_t memory_limit);
176 bool IsSeekWaitingForData() const;
178 private:
179 // Called by the |stream_parser_| when a new initialization segment is
180 // encountered.
181 // Returns true on a successful call. Returns false if an error occurred while
182 // processing decoder configurations.
183 bool OnNewConfigs(bool allow_audio, bool allow_video,
184 const AudioDecoderConfig& audio_config,
185 const VideoDecoderConfig& video_config,
186 const StreamParser::TextTrackConfigMap& text_configs);
188 // Called by the |stream_parser_| at the beginning of a new media segment.
189 void OnNewMediaSegment();
191 // Called by the |stream_parser_| at the end of a media segment.
192 void OnEndOfMediaSegment();
194 // Called by the |stream_parser_| when new buffers have been parsed.
195 // It processes the new buffers using |frame_processor_|, which includes
196 // appending the processed frames to associated demuxer streams for each
197 // frame's track.
198 // Returns true on a successful call. Returns false if an error occurred while
199 // processing the buffers.
200 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
201 const StreamParser::BufferQueue& video_buffers,
202 const StreamParser::TextBufferQueueMap& text_map);
204 void OnSourceInitDone(const StreamParser::InitParameters& params);
206 // EstimateVideoDataSize uses some heuristics to estimate the size of the
207 // video size in the chunk of muxed audio/video data without parsing it.
208 // This is used by EvictCodedFrames algorithm, which happens before Append
209 // (and therefore before parsing is performed) to prepare space for new data.
210 size_t EstimateVideoDataSize(size_t muxed_data_chunk_size) const;
212 CreateDemuxerStreamCB create_demuxer_stream_cb_;
213 NewTextTrackCB new_text_track_cb_;
215 // During Append(), if OnNewBuffers() coded frame processing updates the
216 // timestamp offset then |*timestamp_offset_during_append_| is also updated
217 // so Append()'s caller can know the new offset. This pointer is only non-NULL
218 // during the lifetime of an Append() call.
219 TimeDelta* timestamp_offset_during_append_;
221 // During Append(), coded frame processing triggered by OnNewBuffers()
222 // requires these two attributes. These are only valid during the lifetime of
223 // an Append() call.
224 TimeDelta append_window_start_during_append_;
225 TimeDelta append_window_end_during_append_;
227 // Set to true if the next buffers appended within the append window
228 // represent the start of a new media segment. This flag being set
229 // triggers a call to |new_segment_cb_| when the new buffers are
230 // appended. The flag is set on actual media segment boundaries and
231 // when the "append window" filtering causes discontinuities in the
232 // appended data.
233 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
234 // processing's discontinuity logic is enough. See http://crbug.com/351489.
235 bool new_media_segment_;
237 // Keeps track of whether a media segment is being parsed.
238 bool parsing_media_segment_;
240 // The object used to parse appended data.
241 scoped_ptr<StreamParser> stream_parser_;
243 ChunkDemuxerStream* audio_; // Not owned by |this|.
244 ChunkDemuxerStream* video_; // Not owned by |this|.
246 typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
247 TextStreamMap text_stream_map_; // |this| owns the map's stream pointers.
249 scoped_ptr<FrameProcessor> frame_processor_;
250 scoped_refptr<MediaLog> media_log_;
251 StreamParser::InitCB init_cb_;
253 // During Append(), OnNewConfigs() will trigger the initialization segment
254 // received algorithm. This callback is only non-NULL during the lifetime of
255 // an Append() call. Note, the MSE spec explicitly disallows this algorithm
256 // during an Abort(), since Abort() is allowed only to emit coded frames, and
257 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
258 InitSegmentReceivedCB init_segment_received_cb_;
260 // Indicates that timestampOffset should be updated automatically during
261 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
262 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
263 // changes to MSE spec. See http://crbug.com/371499.
264 bool auto_update_timestamp_offset_;
266 DISALLOW_COPY_AND_ASSIGN(SourceState);
269 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
270 scoped_ptr<FrameProcessor> frame_processor,
271 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
272 const scoped_refptr<MediaLog>& media_log)
273 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
274 timestamp_offset_during_append_(NULL),
275 new_media_segment_(false),
276 parsing_media_segment_(false),
277 stream_parser_(stream_parser.release()),
278 audio_(NULL),
279 video_(NULL),
280 frame_processor_(frame_processor.release()),
281 media_log_(media_log),
282 auto_update_timestamp_offset_(false) {
283 DCHECK(!create_demuxer_stream_cb_.is_null());
284 DCHECK(frame_processor_);
287 SourceState::~SourceState() {
288 Shutdown();
290 STLDeleteValues(&text_stream_map_);
293 void SourceState::Init(
294 const StreamParser::InitCB& init_cb,
295 bool allow_audio,
296 bool allow_video,
297 const StreamParser::EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
298 const NewTextTrackCB& new_text_track_cb) {
299 new_text_track_cb_ = new_text_track_cb;
300 init_cb_ = init_cb;
302 stream_parser_->Init(
303 base::Bind(&SourceState::OnSourceInitDone, base::Unretained(this)),
304 base::Bind(&SourceState::OnNewConfigs, base::Unretained(this),
305 allow_audio, allow_video),
306 base::Bind(&SourceState::OnNewBuffers, base::Unretained(this)),
307 new_text_track_cb_.is_null(), encrypted_media_init_data_cb,
308 base::Bind(&SourceState::OnNewMediaSegment, base::Unretained(this)),
309 base::Bind(&SourceState::OnEndOfMediaSegment, base::Unretained(this)),
310 media_log_);
313 void SourceState::SetSequenceMode(bool sequence_mode) {
314 DCHECK(!parsing_media_segment_);
316 frame_processor_->SetSequenceMode(sequence_mode);
319 void SourceState::SetGroupStartTimestampIfInSequenceMode(
320 base::TimeDelta timestamp_offset) {
321 DCHECK(!parsing_media_segment_);
323 frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
326 bool SourceState::Append(
327 const uint8* data,
328 size_t length,
329 TimeDelta append_window_start,
330 TimeDelta append_window_end,
331 TimeDelta* timestamp_offset,
332 const InitSegmentReceivedCB& init_segment_received_cb) {
333 DCHECK(timestamp_offset);
334 DCHECK(!timestamp_offset_during_append_);
335 DCHECK(!init_segment_received_cb.is_null());
336 DCHECK(init_segment_received_cb_.is_null());
337 append_window_start_during_append_ = append_window_start;
338 append_window_end_during_append_ = append_window_end;
339 timestamp_offset_during_append_ = timestamp_offset;
340 init_segment_received_cb_= init_segment_received_cb;
342 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
343 // append window and timestamp offset pointer. See http://crbug.com/351454.
344 bool result = stream_parser_->Parse(data, length);
345 if (!result) {
346 MEDIA_LOG(ERROR, media_log_)
347 << __FUNCTION__ << ": stream parsing failed."
348 << " Data size=" << length
349 << " append_window_start=" << append_window_start.InSecondsF()
350 << " append_window_end=" << append_window_end.InSecondsF();
352 timestamp_offset_during_append_ = NULL;
353 init_segment_received_cb_.Reset();
354 return result;
357 void SourceState::Abort(TimeDelta append_window_start,
358 TimeDelta append_window_end,
359 base::TimeDelta* timestamp_offset) {
360 DCHECK(timestamp_offset);
361 DCHECK(!timestamp_offset_during_append_);
362 timestamp_offset_during_append_ = timestamp_offset;
363 append_window_start_during_append_ = append_window_start;
364 append_window_end_during_append_ = append_window_end;
366 stream_parser_->Flush();
367 timestamp_offset_during_append_ = NULL;
369 frame_processor_->Reset();
370 parsing_media_segment_ = false;
373 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
374 if (audio_)
375 audio_->Remove(start, end, duration);
377 if (video_)
378 video_->Remove(start, end, duration);
380 for (TextStreamMap::iterator itr = text_stream_map_.begin();
381 itr != text_stream_map_.end(); ++itr) {
382 itr->second->Remove(start, end, duration);
386 size_t SourceState::EstimateVideoDataSize(size_t muxed_data_chunk_size) const {
387 DCHECK(audio_);
388 DCHECK(video_);
390 size_t videoBufferedSize = video_->GetBufferedSize();
391 size_t audioBufferedSize = audio_->GetBufferedSize();
392 if (videoBufferedSize == 0 || audioBufferedSize == 0) {
393 // At this point either audio or video buffer is empty, which means buffer
394 // levels are probably low anyway and we should have enough space in the
395 // buffers for appending new data, so just take a very rough guess.
396 return muxed_data_chunk_size / 2;
399 // We need to estimate how much audio and video data is going to be in the
400 // newly appended data chunk to make space for the new data. And we need to do
401 // that without parsing the data (which will happen later, in the Append
402 // phase). So for now we can only rely on some heuristic here. Let's assume
403 // that the proportion of the audio/video in the new data chunk is the same as
404 // the current ratio of buffered audio/video.
405 // Longer term this should go away once we further change the MSE GC algorithm
406 // to work across all streams of a SourceBuffer (see crbug.com/520704).
407 double videoBufferedSizeF = static_cast<double>(videoBufferedSize);
408 double audioBufferedSizeF = static_cast<double>(audioBufferedSize);
410 double totalBufferedSizeF = videoBufferedSizeF + audioBufferedSizeF;
411 CHECK_GT(totalBufferedSizeF, 0.0);
413 double videoRatio = videoBufferedSizeF / totalBufferedSizeF;
414 CHECK_GE(videoRatio, 0.0);
415 CHECK_LE(videoRatio, 1.0);
416 double estimatedVideoSize = muxed_data_chunk_size * videoRatio;
417 return static_cast<size_t>(estimatedVideoSize);
420 bool SourceState::EvictCodedFrames(DecodeTimestamp media_time,
421 size_t newDataSize) {
422 bool success = true;
424 DVLOG(3) << __FUNCTION__ << " media_time=" << media_time.InSecondsF()
425 << " newDataSize=" << newDataSize
426 << " videoBufferedSize=" << (video_ ? video_->GetBufferedSize() : 0)
427 << " audioBufferedSize=" << (audio_ ? audio_->GetBufferedSize() : 0);
429 size_t newAudioSize = 0;
430 size_t newVideoSize = 0;
431 if (audio_ && video_) {
432 newVideoSize = EstimateVideoDataSize(newDataSize);
433 newAudioSize = newDataSize - newVideoSize;
434 } else if (video_) {
435 newVideoSize = newDataSize;
436 } else if (audio_) {
437 newAudioSize = newDataSize;
440 DVLOG(3) << __FUNCTION__ << " estimated audio/video sizes: "
441 << " newVideoSize=" << newVideoSize
442 << " newAudioSize=" << newAudioSize;
444 if (audio_)
445 success = audio_->EvictCodedFrames(media_time, newAudioSize) && success;
447 if (video_)
448 success = video_->EvictCodedFrames(media_time, newVideoSize) && success;
450 for (TextStreamMap::iterator itr = text_stream_map_.begin();
451 itr != text_stream_map_.end(); ++itr) {
452 success = itr->second->EvictCodedFrames(media_time, 0) && success;
455 DVLOG(3) << __FUNCTION__ << " result=" << success
456 << " videoBufferedSize=" << (video_ ? video_->GetBufferedSize() : 0)
457 << " audioBufferedSize=" << (audio_ ? audio_->GetBufferedSize() : 0);
459 return success;
462 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
463 bool ended) const {
464 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
465 // this code to only add ranges from active tracks.
466 RangesList ranges_list;
467 if (audio_)
468 ranges_list.push_back(audio_->GetBufferedRanges(duration));
470 if (video_)
471 ranges_list.push_back(video_->GetBufferedRanges(duration));
473 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
474 itr != text_stream_map_.end(); ++itr) {
475 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
478 return ComputeIntersection(ranges_list, ended);
481 TimeDelta SourceState::GetMaxBufferedDuration() const {
482 TimeDelta max_duration;
484 if (audio_)
485 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
487 if (video_)
488 max_duration = std::max(max_duration, video_->GetBufferedDuration());
490 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
491 itr != text_stream_map_.end(); ++itr) {
492 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
495 return max_duration;
498 void SourceState::StartReturningData() {
499 if (audio_)
500 audio_->StartReturningData();
502 if (video_)
503 video_->StartReturningData();
505 for (TextStreamMap::iterator itr = text_stream_map_.begin();
506 itr != text_stream_map_.end(); ++itr) {
507 itr->second->StartReturningData();
511 void SourceState::AbortReads() {
512 if (audio_)
513 audio_->AbortReads();
515 if (video_)
516 video_->AbortReads();
518 for (TextStreamMap::iterator itr = text_stream_map_.begin();
519 itr != text_stream_map_.end(); ++itr) {
520 itr->second->AbortReads();
524 void SourceState::Seek(TimeDelta seek_time) {
525 if (audio_)
526 audio_->Seek(seek_time);
528 if (video_)
529 video_->Seek(seek_time);
531 for (TextStreamMap::iterator itr = text_stream_map_.begin();
532 itr != text_stream_map_.end(); ++itr) {
533 itr->second->Seek(seek_time);
537 void SourceState::CompletePendingReadIfPossible() {
538 if (audio_)
539 audio_->CompletePendingReadIfPossible();
541 if (video_)
542 video_->CompletePendingReadIfPossible();
544 for (TextStreamMap::iterator itr = text_stream_map_.begin();
545 itr != text_stream_map_.end(); ++itr) {
546 itr->second->CompletePendingReadIfPossible();
550 void SourceState::OnSetDuration(TimeDelta duration) {
551 if (audio_)
552 audio_->OnSetDuration(duration);
554 if (video_)
555 video_->OnSetDuration(duration);
557 for (TextStreamMap::iterator itr = text_stream_map_.begin();
558 itr != text_stream_map_.end(); ++itr) {
559 itr->second->OnSetDuration(duration);
563 void SourceState::MarkEndOfStream() {
564 if (audio_)
565 audio_->MarkEndOfStream();
567 if (video_)
568 video_->MarkEndOfStream();
570 for (TextStreamMap::iterator itr = text_stream_map_.begin();
571 itr != text_stream_map_.end(); ++itr) {
572 itr->second->MarkEndOfStream();
576 void SourceState::UnmarkEndOfStream() {
577 if (audio_)
578 audio_->UnmarkEndOfStream();
580 if (video_)
581 video_->UnmarkEndOfStream();
583 for (TextStreamMap::iterator itr = text_stream_map_.begin();
584 itr != text_stream_map_.end(); ++itr) {
585 itr->second->UnmarkEndOfStream();
589 void SourceState::Shutdown() {
590 if (audio_)
591 audio_->Shutdown();
593 if (video_)
594 video_->Shutdown();
596 for (TextStreamMap::iterator itr = text_stream_map_.begin();
597 itr != text_stream_map_.end(); ++itr) {
598 itr->second->Shutdown();
602 void SourceState::SetMemoryLimits(DemuxerStream::Type type,
603 size_t memory_limit) {
604 switch (type) {
605 case DemuxerStream::AUDIO:
606 if (audio_)
607 audio_->SetStreamMemoryLimit(memory_limit);
608 break;
609 case DemuxerStream::VIDEO:
610 if (video_)
611 video_->SetStreamMemoryLimit(memory_limit);
612 break;
613 case DemuxerStream::TEXT:
614 for (TextStreamMap::iterator itr = text_stream_map_.begin();
615 itr != text_stream_map_.end(); ++itr) {
616 itr->second->SetStreamMemoryLimit(memory_limit);
618 break;
619 case DemuxerStream::UNKNOWN:
620 case DemuxerStream::NUM_TYPES:
621 NOTREACHED();
622 break;
626 bool SourceState::IsSeekWaitingForData() const {
627 if (audio_ && audio_->IsSeekWaitingForData())
628 return true;
630 if (video_ && video_->IsSeekWaitingForData())
631 return true;
633 // NOTE: We are intentionally not checking the text tracks
634 // because text tracks are discontinuous and may not have data
635 // for the seek position. This is ok and playback should not be
636 // stalled because we don't have cues. If cues, with timestamps after
637 // the seek time, eventually arrive they will be delivered properly
638 // in response to ChunkDemuxerStream::Read() calls.
640 return false;
643 bool SourceState::OnNewConfigs(
644 bool allow_audio, bool allow_video,
645 const AudioDecoderConfig& audio_config,
646 const VideoDecoderConfig& video_config,
647 const StreamParser::TextTrackConfigMap& text_configs) {
648 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
649 << ", " << audio_config.IsValidConfig()
650 << ", " << video_config.IsValidConfig() << ")";
651 DCHECK(!init_segment_received_cb_.is_null());
653 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
654 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
655 return false;
658 // Signal an error if we get configuration info for stream types that weren't
659 // specified in AddId() or more configs after a stream is initialized.
660 if (allow_audio != audio_config.IsValidConfig()) {
661 MEDIA_LOG(ERROR, media_log_)
662 << "Initialization segment"
663 << (audio_config.IsValidConfig() ? " has" : " does not have")
664 << " an audio track, but the mimetype"
665 << (allow_audio ? " specifies" : " does not specify")
666 << " an audio codec.";
667 return false;
670 if (allow_video != video_config.IsValidConfig()) {
671 MEDIA_LOG(ERROR, media_log_)
672 << "Initialization segment"
673 << (video_config.IsValidConfig() ? " has" : " does not have")
674 << " a video track, but the mimetype"
675 << (allow_video ? " specifies" : " does not specify")
676 << " a video codec.";
677 return false;
680 bool success = true;
681 if (audio_config.IsValidConfig()) {
682 if (!audio_) {
683 media_log_->SetBooleanProperty("found_audio_stream", true);
685 if (!audio_ ||
686 audio_->audio_decoder_config().codec() != audio_config.codec()) {
687 media_log_->SetStringProperty("audio_codec_name",
688 audio_config.GetHumanReadableCodecName());
691 if (!audio_) {
692 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
694 if (!audio_) {
695 DVLOG(1) << "Failed to create an audio stream.";
696 return false;
699 if (!frame_processor_->AddTrack(FrameProcessor::kAudioTrackId, audio_)) {
700 DVLOG(1) << "Failed to add audio track to frame processor.";
701 return false;
705 frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
706 success &= audio_->UpdateAudioConfig(audio_config, media_log_);
709 if (video_config.IsValidConfig()) {
710 if (!video_) {
711 media_log_->SetBooleanProperty("found_video_stream", true);
713 if (!video_ ||
714 video_->video_decoder_config().codec() != video_config.codec()) {
715 media_log_->SetStringProperty("video_codec_name",
716 video_config.GetHumanReadableCodecName());
719 if (!video_) {
720 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
722 if (!video_) {
723 DVLOG(1) << "Failed to create a video stream.";
724 return false;
727 if (!frame_processor_->AddTrack(FrameProcessor::kVideoTrackId, video_)) {
728 DVLOG(1) << "Failed to add video track to frame processor.";
729 return false;
733 success &= video_->UpdateVideoConfig(video_config, media_log_);
736 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
737 if (text_stream_map_.empty()) {
738 for (TextConfigItr itr = text_configs.begin();
739 itr != text_configs.end(); ++itr) {
740 ChunkDemuxerStream* const text_stream =
741 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
742 if (!frame_processor_->AddTrack(itr->first, text_stream)) {
743 success &= false;
744 MEDIA_LOG(ERROR, media_log_) << "Failed to add text track ID "
745 << itr->first << " to frame processor.";
746 break;
748 text_stream->UpdateTextConfig(itr->second, media_log_);
749 text_stream_map_[itr->first] = text_stream;
750 new_text_track_cb_.Run(text_stream, itr->second);
752 } else {
753 const size_t text_count = text_stream_map_.size();
754 if (text_configs.size() != text_count) {
755 success &= false;
756 MEDIA_LOG(ERROR, media_log_)
757 << "The number of text track configs changed.";
758 } else if (text_count == 1) {
759 TextConfigItr config_itr = text_configs.begin();
760 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
761 ChunkDemuxerStream* text_stream = stream_itr->second;
762 TextTrackConfig old_config = text_stream->text_track_config();
763 TextTrackConfig new_config(config_itr->second.kind(),
764 config_itr->second.label(),
765 config_itr->second.language(),
766 old_config.id());
767 if (!new_config.Matches(old_config)) {
768 success &= false;
769 MEDIA_LOG(ERROR, media_log_)
770 << "New text track config does not match old one.";
771 } else {
772 StreamParser::TrackId old_id = stream_itr->first;
773 StreamParser::TrackId new_id = config_itr->first;
774 if (new_id != old_id) {
775 if (frame_processor_->UpdateTrack(old_id, new_id)) {
776 text_stream_map_.clear();
777 text_stream_map_[config_itr->first] = text_stream;
778 } else {
779 success &= false;
780 MEDIA_LOG(ERROR, media_log_)
781 << "Error remapping single text track number";
785 } else {
786 for (TextConfigItr config_itr = text_configs.begin();
787 config_itr != text_configs.end(); ++config_itr) {
788 TextStreamMap::iterator stream_itr =
789 text_stream_map_.find(config_itr->first);
790 if (stream_itr == text_stream_map_.end()) {
791 success &= false;
792 MEDIA_LOG(ERROR, media_log_)
793 << "Unexpected text track configuration for track ID "
794 << config_itr->first;
795 break;
798 const TextTrackConfig& new_config = config_itr->second;
799 ChunkDemuxerStream* stream = stream_itr->second;
800 TextTrackConfig old_config = stream->text_track_config();
801 if (!new_config.Matches(old_config)) {
802 success &= false;
803 MEDIA_LOG(ERROR, media_log_) << "New text track config for track ID "
804 << config_itr->first
805 << " does not match old one.";
806 break;
812 frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
814 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
815 if (success)
816 init_segment_received_cb_.Run();
818 return success;
821 void SourceState::OnNewMediaSegment() {
822 DVLOG(2) << "OnNewMediaSegment()";
823 parsing_media_segment_ = true;
824 new_media_segment_ = true;
827 void SourceState::OnEndOfMediaSegment() {
828 DVLOG(2) << "OnEndOfMediaSegment()";
829 parsing_media_segment_ = false;
830 new_media_segment_ = false;
833 bool SourceState::OnNewBuffers(
834 const StreamParser::BufferQueue& audio_buffers,
835 const StreamParser::BufferQueue& video_buffers,
836 const StreamParser::TextBufferQueueMap& text_map) {
837 DVLOG(2) << "OnNewBuffers()";
838 DCHECK(timestamp_offset_during_append_);
839 DCHECK(parsing_media_segment_);
841 const TimeDelta timestamp_offset_before_processing =
842 *timestamp_offset_during_append_;
844 // Calculate the new timestamp offset for audio/video tracks if the stream
845 // parser has requested automatic updates.
846 TimeDelta new_timestamp_offset = timestamp_offset_before_processing;
847 if (auto_update_timestamp_offset_) {
848 const bool have_audio_buffers = !audio_buffers.empty();
849 const bool have_video_buffers = !video_buffers.empty();
850 if (have_audio_buffers && have_video_buffers) {
851 new_timestamp_offset +=
852 std::min(EndTimestamp(audio_buffers), EndTimestamp(video_buffers));
853 } else if (have_audio_buffers) {
854 new_timestamp_offset += EndTimestamp(audio_buffers);
855 } else if (have_video_buffers) {
856 new_timestamp_offset += EndTimestamp(video_buffers);
860 if (!frame_processor_->ProcessFrames(audio_buffers,
861 video_buffers,
862 text_map,
863 append_window_start_during_append_,
864 append_window_end_during_append_,
865 &new_media_segment_,
866 timestamp_offset_during_append_)) {
867 return false;
870 // Only update the timestamp offset if the frame processor hasn't already.
871 if (auto_update_timestamp_offset_ &&
872 timestamp_offset_before_processing == *timestamp_offset_during_append_) {
873 *timestamp_offset_during_append_ = new_timestamp_offset;
876 return true;
879 void SourceState::OnSourceInitDone(const StreamParser::InitParameters& params) {
880 auto_update_timestamp_offset_ = params.auto_update_timestamp_offset;
881 base::ResetAndReturn(&init_cb_).Run(params);
884 ChunkDemuxerStream::ChunkDemuxerStream(Type type,
885 bool splice_frames_enabled)
886 : type_(type),
887 liveness_(DemuxerStream::LIVENESS_UNKNOWN),
888 state_(UNINITIALIZED),
889 splice_frames_enabled_(splice_frames_enabled),
890 partial_append_window_trimming_enabled_(false) {
893 void ChunkDemuxerStream::StartReturningData() {
894 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
895 base::AutoLock auto_lock(lock_);
896 DCHECK(read_cb_.is_null());
897 ChangeState_Locked(RETURNING_DATA_FOR_READS);
900 void ChunkDemuxerStream::AbortReads() {
901 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
902 base::AutoLock auto_lock(lock_);
903 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
904 if (!read_cb_.is_null())
905 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
908 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
909 base::AutoLock auto_lock(lock_);
910 if (read_cb_.is_null())
911 return;
913 CompletePendingReadIfPossible_Locked();
916 void ChunkDemuxerStream::Shutdown() {
917 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
918 base::AutoLock auto_lock(lock_);
919 ChangeState_Locked(SHUTDOWN);
921 // Pass an end of stream buffer to the pending callback to signal that no more
922 // data will be sent.
923 if (!read_cb_.is_null()) {
924 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
925 StreamParserBuffer::CreateEOSBuffer());
929 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
930 base::AutoLock auto_lock(lock_);
932 // This method should not be called for text tracks. See the note in
933 // SourceState::IsSeekWaitingForData().
934 DCHECK_NE(type_, DemuxerStream::TEXT);
936 return stream_->IsSeekPending();
939 void ChunkDemuxerStream::Seek(TimeDelta time) {
940 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
941 base::AutoLock auto_lock(lock_);
942 DCHECK(read_cb_.is_null());
943 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
944 << state_;
946 stream_->Seek(time);
949 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
950 if (buffers.empty())
951 return false;
953 base::AutoLock auto_lock(lock_);
954 DCHECK_NE(state_, SHUTDOWN);
955 if (!stream_->Append(buffers)) {
956 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
957 return false;
960 if (!read_cb_.is_null())
961 CompletePendingReadIfPossible_Locked();
963 return true;
966 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
967 TimeDelta duration) {
968 base::AutoLock auto_lock(lock_);
969 stream_->Remove(start, end, duration);
972 bool ChunkDemuxerStream::EvictCodedFrames(DecodeTimestamp media_time,
973 size_t newDataSize) {
974 base::AutoLock auto_lock(lock_);
975 return stream_->GarbageCollectIfNeeded(media_time, newDataSize);
978 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
979 base::AutoLock auto_lock(lock_);
980 stream_->OnSetDuration(duration);
983 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
984 TimeDelta duration) const {
985 base::AutoLock auto_lock(lock_);
987 if (type_ == TEXT) {
988 // Since text tracks are discontinuous and the lack of cues should not block
989 // playback, report the buffered range for text tracks as [0, |duration|) so
990 // that intesections with audio & video tracks are computed correctly when
991 // no cues are present.
992 Ranges<TimeDelta> text_range;
993 text_range.Add(TimeDelta(), duration);
994 return text_range;
997 Ranges<TimeDelta> range = stream_->GetBufferedTime();
999 if (range.size() == 0u)
1000 return range;
1002 // Clamp the end of the stream's buffered ranges to fit within the duration.
1003 // This can be done by intersecting the stream's range with the valid time
1004 // range.
1005 Ranges<TimeDelta> valid_time_range;
1006 valid_time_range.Add(range.start(0), duration);
1007 return range.IntersectionWith(valid_time_range);
1010 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
1011 return stream_->GetBufferedDuration();
1014 size_t ChunkDemuxerStream::GetBufferedSize() const {
1015 return stream_->GetBufferedSize();
1018 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp) {
1019 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
1020 << start_timestamp.InSecondsF() << ")";
1021 base::AutoLock auto_lock(lock_);
1022 stream_->OnNewMediaSegment(start_timestamp);
1025 bool ChunkDemuxerStream::UpdateAudioConfig(
1026 const AudioDecoderConfig& config,
1027 const scoped_refptr<MediaLog>& media_log) {
1028 DCHECK(config.IsValidConfig());
1029 DCHECK_EQ(type_, AUDIO);
1030 base::AutoLock auto_lock(lock_);
1031 if (!stream_) {
1032 DCHECK_EQ(state_, UNINITIALIZED);
1034 // On platforms which support splice frames, enable splice frames and
1035 // partial append window support for most codecs (notably: not opus).
1036 const bool codec_supported = config.codec() == kCodecMP3 ||
1037 config.codec() == kCodecAAC ||
1038 config.codec() == kCodecVorbis;
1039 splice_frames_enabled_ = splice_frames_enabled_ && codec_supported;
1040 partial_append_window_trimming_enabled_ =
1041 splice_frames_enabled_ && codec_supported;
1043 stream_.reset(
1044 new SourceBufferStream(config, media_log, splice_frames_enabled_));
1045 return true;
1048 return stream_->UpdateAudioConfig(config);
1051 bool ChunkDemuxerStream::UpdateVideoConfig(
1052 const VideoDecoderConfig& config,
1053 const scoped_refptr<MediaLog>& media_log) {
1054 DCHECK(config.IsValidConfig());
1055 DCHECK_EQ(type_, VIDEO);
1056 base::AutoLock auto_lock(lock_);
1058 if (!stream_) {
1059 DCHECK_EQ(state_, UNINITIALIZED);
1060 stream_.reset(
1061 new SourceBufferStream(config, media_log, splice_frames_enabled_));
1062 return true;
1065 return stream_->UpdateVideoConfig(config);
1068 void ChunkDemuxerStream::UpdateTextConfig(
1069 const TextTrackConfig& config,
1070 const scoped_refptr<MediaLog>& media_log) {
1071 DCHECK_EQ(type_, TEXT);
1072 base::AutoLock auto_lock(lock_);
1073 DCHECK(!stream_);
1074 DCHECK_EQ(state_, UNINITIALIZED);
1075 stream_.reset(
1076 new SourceBufferStream(config, media_log, splice_frames_enabled_));
1079 void ChunkDemuxerStream::MarkEndOfStream() {
1080 base::AutoLock auto_lock(lock_);
1081 stream_->MarkEndOfStream();
1084 void ChunkDemuxerStream::UnmarkEndOfStream() {
1085 base::AutoLock auto_lock(lock_);
1086 stream_->UnmarkEndOfStream();
1089 // DemuxerStream methods.
1090 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
1091 base::AutoLock auto_lock(lock_);
1092 DCHECK_NE(state_, UNINITIALIZED);
1093 DCHECK(read_cb_.is_null());
1095 read_cb_ = BindToCurrentLoop(read_cb);
1096 CompletePendingReadIfPossible_Locked();
1099 DemuxerStream::Type ChunkDemuxerStream::type() const { return type_; }
1101 DemuxerStream::Liveness ChunkDemuxerStream::liveness() const {
1102 base::AutoLock auto_lock(lock_);
1103 return liveness_;
1106 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
1107 CHECK_EQ(type_, AUDIO);
1108 base::AutoLock auto_lock(lock_);
1109 return stream_->GetCurrentAudioDecoderConfig();
1112 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
1113 CHECK_EQ(type_, VIDEO);
1114 base::AutoLock auto_lock(lock_);
1115 return stream_->GetCurrentVideoDecoderConfig();
1118 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
1120 VideoRotation ChunkDemuxerStream::video_rotation() {
1121 return VIDEO_ROTATION_0;
1124 TextTrackConfig ChunkDemuxerStream::text_track_config() {
1125 CHECK_EQ(type_, TEXT);
1126 base::AutoLock auto_lock(lock_);
1127 return stream_->GetCurrentTextTrackConfig();
1130 void ChunkDemuxerStream::SetStreamMemoryLimit(size_t memory_limit) {
1131 stream_->set_memory_limit(memory_limit);
1134 void ChunkDemuxerStream::SetLiveness(Liveness liveness) {
1135 base::AutoLock auto_lock(lock_);
1136 liveness_ = liveness;
1139 void ChunkDemuxerStream::ChangeState_Locked(State state) {
1140 lock_.AssertAcquired();
1141 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1142 << "type " << type_
1143 << " - " << state_ << " -> " << state;
1144 state_ = state;
1147 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1149 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1150 lock_.AssertAcquired();
1151 DCHECK(!read_cb_.is_null());
1153 DemuxerStream::Status status;
1154 scoped_refptr<StreamParserBuffer> buffer;
1156 switch (state_) {
1157 case UNINITIALIZED:
1158 NOTREACHED();
1159 return;
1160 case RETURNING_DATA_FOR_READS:
1161 switch (stream_->GetNextBuffer(&buffer)) {
1162 case SourceBufferStream::kSuccess:
1163 status = DemuxerStream::kOk;
1164 DVLOG(2) << __FUNCTION__ << ": returning kOk, type " << type_
1165 << ", dts " << buffer->GetDecodeTimestamp().InSecondsF()
1166 << ", pts " << buffer->timestamp().InSecondsF()
1167 << ", dur " << buffer->duration().InSecondsF()
1168 << ", key " << buffer->is_key_frame();
1169 break;
1170 case SourceBufferStream::kNeedBuffer:
1171 // Return early without calling |read_cb_| since we don't have
1172 // any data to return yet.
1173 DVLOG(2) << __FUNCTION__ << ": returning kNeedBuffer, type "
1174 << type_;
1175 return;
1176 case SourceBufferStream::kEndOfStream:
1177 status = DemuxerStream::kOk;
1178 buffer = StreamParserBuffer::CreateEOSBuffer();
1179 DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
1180 << type_;
1181 break;
1182 case SourceBufferStream::kConfigChange:
1183 status = kConfigChanged;
1184 buffer = NULL;
1185 DVLOG(2) << __FUNCTION__ << ": returning kConfigChange, type "
1186 << type_;
1187 break;
1189 break;
1190 case RETURNING_ABORT_FOR_READS:
1191 // Null buffers should be returned in this state since we are waiting
1192 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1193 // because they are associated with the seek.
1194 status = DemuxerStream::kAborted;
1195 buffer = NULL;
1196 DVLOG(2) << __FUNCTION__ << ": returning kAborted, type " << type_;
1197 break;
1198 case SHUTDOWN:
1199 status = DemuxerStream::kOk;
1200 buffer = StreamParserBuffer::CreateEOSBuffer();
1201 DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
1202 << type_;
1203 break;
1206 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1209 ChunkDemuxer::ChunkDemuxer(
1210 const base::Closure& open_cb,
1211 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
1212 const scoped_refptr<MediaLog>& media_log,
1213 bool splice_frames_enabled)
1214 : state_(WAITING_FOR_INIT),
1215 cancel_next_seek_(false),
1216 host_(NULL),
1217 open_cb_(open_cb),
1218 encrypted_media_init_data_cb_(encrypted_media_init_data_cb),
1219 enable_text_(false),
1220 media_log_(media_log),
1221 duration_(kNoTimestamp()),
1222 user_specified_duration_(-1),
1223 liveness_(DemuxerStream::LIVENESS_UNKNOWN),
1224 splice_frames_enabled_(splice_frames_enabled) {
1225 DCHECK(!open_cb_.is_null());
1226 DCHECK(!encrypted_media_init_data_cb_.is_null());
1229 std::string ChunkDemuxer::GetDisplayName() const {
1230 return "ChunkDemuxer";
1233 void ChunkDemuxer::Initialize(
1234 DemuxerHost* host,
1235 const PipelineStatusCB& cb,
1236 bool enable_text_tracks) {
1237 DVLOG(1) << "Init()";
1239 base::AutoLock auto_lock(lock_);
1241 // The |init_cb_| must only be run after this method returns, so always post.
1242 init_cb_ = BindToCurrentLoop(cb);
1243 if (state_ == SHUTDOWN) {
1244 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1245 return;
1247 DCHECK_EQ(state_, WAITING_FOR_INIT);
1248 host_ = host;
1249 enable_text_ = enable_text_tracks;
1251 ChangeState_Locked(INITIALIZING);
1253 base::ResetAndReturn(&open_cb_).Run();
1256 void ChunkDemuxer::Stop() {
1257 DVLOG(1) << "Stop()";
1258 Shutdown();
1261 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1262 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1263 DCHECK(time >= TimeDelta());
1265 base::AutoLock auto_lock(lock_);
1266 DCHECK(seek_cb_.is_null());
1268 seek_cb_ = BindToCurrentLoop(cb);
1269 if (state_ != INITIALIZED && state_ != ENDED) {
1270 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1271 return;
1274 if (cancel_next_seek_) {
1275 cancel_next_seek_ = false;
1276 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1277 return;
1280 SeekAllSources(time);
1281 StartReturningData();
1283 if (IsSeekWaitingForData_Locked()) {
1284 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1285 return;
1288 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1291 // Demuxer implementation.
1292 base::Time ChunkDemuxer::GetTimelineOffset() const {
1293 return timeline_offset_;
1296 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1297 DCHECK_NE(type, DemuxerStream::TEXT);
1298 base::AutoLock auto_lock(lock_);
1299 if (type == DemuxerStream::VIDEO)
1300 return video_.get();
1302 if (type == DemuxerStream::AUDIO)
1303 return audio_.get();
1305 return NULL;
1308 TimeDelta ChunkDemuxer::GetStartTime() const {
1309 return TimeDelta();
1312 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1313 DVLOG(1) << "StartWaitingForSeek()";
1314 base::AutoLock auto_lock(lock_);
1315 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1316 state_ == PARSE_ERROR) << state_;
1317 DCHECK(seek_cb_.is_null());
1319 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1320 return;
1322 AbortPendingReads();
1323 SeekAllSources(seek_time);
1325 // Cancel state set in CancelPendingSeek() since we want to
1326 // accept the next Seek().
1327 cancel_next_seek_ = false;
1330 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1331 base::AutoLock auto_lock(lock_);
1332 DCHECK_NE(state_, INITIALIZING);
1333 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1335 if (cancel_next_seek_)
1336 return;
1338 AbortPendingReads();
1339 SeekAllSources(seek_time);
1341 if (seek_cb_.is_null()) {
1342 cancel_next_seek_ = true;
1343 return;
1346 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1349 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1350 const std::string& type,
1351 std::vector<std::string>& codecs) {
1352 base::AutoLock auto_lock(lock_);
1354 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1355 return kReachedIdLimit;
1357 bool has_audio = false;
1358 bool has_video = false;
1359 scoped_ptr<media::StreamParser> stream_parser(StreamParserFactory::Create(
1360 type, codecs, media_log_, &has_audio, &has_video));
1362 if (!stream_parser)
1363 return ChunkDemuxer::kNotSupported;
1365 if ((has_audio && !source_id_audio_.empty()) ||
1366 (has_video && !source_id_video_.empty()))
1367 return kReachedIdLimit;
1369 if (has_audio)
1370 source_id_audio_ = id;
1372 if (has_video)
1373 source_id_video_ = id;
1375 scoped_ptr<FrameProcessor> frame_processor(
1376 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1377 base::Unretained(this)),
1378 media_log_));
1380 scoped_ptr<SourceState> source_state(new SourceState(
1381 stream_parser.Pass(), frame_processor.Pass(),
1382 base::Bind(&ChunkDemuxer::CreateDemuxerStream, base::Unretained(this)),
1383 media_log_));
1385 SourceState::NewTextTrackCB new_text_track_cb;
1387 if (enable_text_) {
1388 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1389 base::Unretained(this));
1392 source_state->Init(
1393 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1394 has_audio, has_video, encrypted_media_init_data_cb_, new_text_track_cb);
1396 source_state_map_[id] = source_state.release();
1397 return kOk;
1400 void ChunkDemuxer::RemoveId(const std::string& id) {
1401 base::AutoLock auto_lock(lock_);
1402 CHECK(IsValidId(id));
1404 delete source_state_map_[id];
1405 source_state_map_.erase(id);
1407 if (source_id_audio_ == id)
1408 source_id_audio_.clear();
1410 if (source_id_video_ == id)
1411 source_id_video_.clear();
1414 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1415 base::AutoLock auto_lock(lock_);
1416 DCHECK(!id.empty());
1418 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1420 DCHECK(itr != source_state_map_.end());
1421 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1424 bool ChunkDemuxer::EvictCodedFrames(const std::string& id,
1425 base::TimeDelta currentMediaTime,
1426 size_t newDataSize) {
1427 DVLOG(1) << __FUNCTION__ << "(" << id << ")"
1428 << " media_time=" << currentMediaTime.InSecondsF()
1429 << " newDataSize=" << newDataSize;
1430 base::AutoLock auto_lock(lock_);
1432 // Note: The direct conversion from PTS to DTS is safe here, since we don't
1433 // need to know currentTime precisely for GC. GC only needs to know which GOP
1434 // currentTime points to.
1435 DecodeTimestamp media_time_dts =
1436 DecodeTimestamp::FromPresentationTime(currentMediaTime);
1438 DCHECK(!id.empty());
1439 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1440 if (itr == source_state_map_.end()) {
1441 LOG(WARNING) << __FUNCTION__ << " stream " << id << " not found";
1442 return false;
1444 return itr->second->EvictCodedFrames(media_time_dts, newDataSize);
1447 void ChunkDemuxer::AppendData(
1448 const std::string& id,
1449 const uint8* data,
1450 size_t length,
1451 TimeDelta append_window_start,
1452 TimeDelta append_window_end,
1453 TimeDelta* timestamp_offset,
1454 const InitSegmentReceivedCB& init_segment_received_cb) {
1455 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1457 DCHECK(!id.empty());
1458 DCHECK(timestamp_offset);
1459 DCHECK(!init_segment_received_cb.is_null());
1461 Ranges<TimeDelta> ranges;
1464 base::AutoLock auto_lock(lock_);
1465 DCHECK_NE(state_, ENDED);
1467 // Capture if any of the SourceBuffers are waiting for data before we start
1468 // parsing.
1469 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1471 if (length == 0u)
1472 return;
1474 DCHECK(data);
1476 switch (state_) {
1477 case INITIALIZING:
1478 case INITIALIZED:
1479 DCHECK(IsValidId(id));
1480 if (!source_state_map_[id]->Append(data, length,
1481 append_window_start,
1482 append_window_end,
1483 timestamp_offset,
1484 init_segment_received_cb)) {
1485 ReportError_Locked(PIPELINE_ERROR_DECODE);
1486 return;
1488 break;
1490 case PARSE_ERROR:
1491 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1492 return;
1494 case WAITING_FOR_INIT:
1495 case ENDED:
1496 case SHUTDOWN:
1497 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1498 return;
1501 // Check to see if data was appended at the pending seek point. This
1502 // indicates we have parsed enough data to complete the seek.
1503 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1504 !seek_cb_.is_null()) {
1505 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1508 ranges = GetBufferedRanges_Locked();
1511 for (size_t i = 0; i < ranges.size(); ++i)
1512 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1515 void ChunkDemuxer::Abort(const std::string& id,
1516 TimeDelta append_window_start,
1517 TimeDelta append_window_end,
1518 TimeDelta* timestamp_offset) {
1519 DVLOG(1) << "Abort(" << id << ")";
1520 base::AutoLock auto_lock(lock_);
1521 DCHECK(!id.empty());
1522 CHECK(IsValidId(id));
1523 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1524 source_state_map_[id]->Abort(append_window_start,
1525 append_window_end,
1526 timestamp_offset);
1527 // Abort can possibly emit some buffers.
1528 // Need to check whether seeking can be completed.
1529 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1530 !seek_cb_.is_null()) {
1531 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1535 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1536 TimeDelta end) {
1537 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1538 << ", " << end.InSecondsF() << ")";
1539 base::AutoLock auto_lock(lock_);
1541 DCHECK(!id.empty());
1542 CHECK(IsValidId(id));
1543 DCHECK(start >= base::TimeDelta()) << start.InSecondsF();
1544 DCHECK(start < end) << "start " << start.InSecondsF()
1545 << " end " << end.InSecondsF();
1546 DCHECK(duration_ != kNoTimestamp());
1547 DCHECK(start <= duration_) << "start " << start.InSecondsF()
1548 << " duration " << duration_.InSecondsF();
1550 if (start == duration_)
1551 return;
1553 source_state_map_[id]->Remove(start, end, duration_);
1556 double ChunkDemuxer::GetDuration() {
1557 base::AutoLock auto_lock(lock_);
1558 return GetDuration_Locked();
1561 double ChunkDemuxer::GetDuration_Locked() {
1562 lock_.AssertAcquired();
1563 if (duration_ == kNoTimestamp())
1564 return std::numeric_limits<double>::quiet_NaN();
1566 // Return positive infinity if the resource is unbounded.
1567 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1568 if (duration_ == kInfiniteDuration())
1569 return std::numeric_limits<double>::infinity();
1571 if (user_specified_duration_ >= 0)
1572 return user_specified_duration_;
1574 return duration_.InSecondsF();
1577 void ChunkDemuxer::SetDuration(double duration) {
1578 base::AutoLock auto_lock(lock_);
1579 DVLOG(1) << "SetDuration(" << duration << ")";
1580 DCHECK_GE(duration, 0);
1582 if (duration == GetDuration_Locked())
1583 return;
1585 // Compute & bounds check the TimeDelta representation of duration.
1586 // This can be different if the value of |duration| doesn't fit the range or
1587 // precision of TimeDelta.
1588 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1589 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1590 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1591 double min_duration_in_seconds = min_duration.InSecondsF();
1592 double max_duration_in_seconds = max_duration.InSecondsF();
1594 TimeDelta duration_td;
1595 if (duration == std::numeric_limits<double>::infinity()) {
1596 duration_td = media::kInfiniteDuration();
1597 } else if (duration < min_duration_in_seconds) {
1598 duration_td = min_duration;
1599 } else if (duration > max_duration_in_seconds) {
1600 duration_td = max_duration;
1601 } else {
1602 duration_td = TimeDelta::FromMicroseconds(
1603 duration * base::Time::kMicrosecondsPerSecond);
1606 DCHECK(duration_td > TimeDelta());
1608 user_specified_duration_ = duration;
1609 duration_ = duration_td;
1610 host_->SetDuration(duration_);
1612 for (SourceStateMap::iterator itr = source_state_map_.begin();
1613 itr != source_state_map_.end(); ++itr) {
1614 itr->second->OnSetDuration(duration_);
1618 bool ChunkDemuxer::IsParsingMediaSegment(const std::string& id) {
1619 base::AutoLock auto_lock(lock_);
1620 DVLOG(1) << "IsParsingMediaSegment(" << id << ")";
1621 CHECK(IsValidId(id));
1623 return source_state_map_[id]->parsing_media_segment();
1626 void ChunkDemuxer::SetSequenceMode(const std::string& id,
1627 bool sequence_mode) {
1628 base::AutoLock auto_lock(lock_);
1629 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1630 CHECK(IsValidId(id));
1631 DCHECK_NE(state_, ENDED);
1633 source_state_map_[id]->SetSequenceMode(sequence_mode);
1636 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1637 const std::string& id,
1638 base::TimeDelta timestamp_offset) {
1639 base::AutoLock auto_lock(lock_);
1640 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id << ", "
1641 << timestamp_offset.InSecondsF() << ")";
1642 CHECK(IsValidId(id));
1643 DCHECK_NE(state_, ENDED);
1645 source_state_map_[id]->SetGroupStartTimestampIfInSequenceMode(
1646 timestamp_offset);
1650 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1651 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1652 base::AutoLock auto_lock(lock_);
1653 DCHECK_NE(state_, WAITING_FOR_INIT);
1654 DCHECK_NE(state_, ENDED);
1656 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1657 return;
1659 if (state_ == INITIALIZING) {
1660 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1661 return;
1664 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1665 for (SourceStateMap::iterator itr = source_state_map_.begin();
1666 itr != source_state_map_.end(); ++itr) {
1667 itr->second->MarkEndOfStream();
1670 CompletePendingReadsIfPossible();
1672 // Give a chance to resume the pending seek process.
1673 if (status != PIPELINE_OK) {
1674 ReportError_Locked(status);
1675 return;
1678 ChangeState_Locked(ENDED);
1679 DecreaseDurationIfNecessary();
1681 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1682 !seek_cb_.is_null()) {
1683 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1687 void ChunkDemuxer::UnmarkEndOfStream() {
1688 DVLOG(1) << "UnmarkEndOfStream()";
1689 base::AutoLock auto_lock(lock_);
1690 DCHECK_EQ(state_, ENDED);
1692 ChangeState_Locked(INITIALIZED);
1694 for (SourceStateMap::iterator itr = source_state_map_.begin();
1695 itr != source_state_map_.end(); ++itr) {
1696 itr->second->UnmarkEndOfStream();
1700 void ChunkDemuxer::Shutdown() {
1701 DVLOG(1) << "Shutdown()";
1702 base::AutoLock auto_lock(lock_);
1704 if (state_ == SHUTDOWN)
1705 return;
1707 ShutdownAllStreams();
1709 ChangeState_Locked(SHUTDOWN);
1711 if(!seek_cb_.is_null())
1712 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1715 void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type,
1716 size_t memory_limit) {
1717 for (SourceStateMap::iterator itr = source_state_map_.begin();
1718 itr != source_state_map_.end(); ++itr) {
1719 itr->second->SetMemoryLimits(type, memory_limit);
1723 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1724 lock_.AssertAcquired();
1725 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1726 << state_ << " -> " << new_state;
1727 state_ = new_state;
1730 ChunkDemuxer::~ChunkDemuxer() {
1731 DCHECK_NE(state_, INITIALIZED);
1733 STLDeleteValues(&source_state_map_);
1736 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1737 DVLOG(1) << "ReportError_Locked(" << error << ")";
1738 lock_.AssertAcquired();
1739 DCHECK_NE(error, PIPELINE_OK);
1741 ChangeState_Locked(PARSE_ERROR);
1743 PipelineStatusCB cb;
1745 if (!init_cb_.is_null()) {
1746 std::swap(cb, init_cb_);
1747 } else {
1748 if (!seek_cb_.is_null())
1749 std::swap(cb, seek_cb_);
1751 ShutdownAllStreams();
1754 if (!cb.is_null()) {
1755 cb.Run(error);
1756 return;
1759 base::AutoUnlock auto_unlock(lock_);
1760 host_->OnDemuxerError(error);
1763 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1764 lock_.AssertAcquired();
1765 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1766 itr != source_state_map_.end(); ++itr) {
1767 if (itr->second->IsSeekWaitingForData())
1768 return true;
1771 return false;
1774 void ChunkDemuxer::OnSourceInitDone(
1775 const StreamParser::InitParameters& params) {
1776 DVLOG(1) << "OnSourceInitDone(" << params.duration.InSecondsF() << ")";
1777 lock_.AssertAcquired();
1778 DCHECK_EQ(state_, INITIALIZING);
1779 if (!audio_ && !video_) {
1780 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1781 return;
1784 if (params.duration != TimeDelta() && duration_ == kNoTimestamp())
1785 UpdateDuration(params.duration);
1787 if (!params.timeline_offset.is_null()) {
1788 if (!timeline_offset_.is_null() &&
1789 params.timeline_offset != timeline_offset_) {
1790 MEDIA_LOG(ERROR, media_log_)
1791 << "Timeline offset is not the same across all SourceBuffers.";
1792 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1793 return;
1796 timeline_offset_ = params.timeline_offset;
1799 if (params.liveness != DemuxerStream::LIVENESS_UNKNOWN) {
1800 if (audio_)
1801 audio_->SetLiveness(params.liveness);
1802 if (video_)
1803 video_->SetLiveness(params.liveness);
1806 // Wait until all streams have initialized.
1807 if ((!source_id_audio_.empty() && !audio_) ||
1808 (!source_id_video_.empty() && !video_)) {
1809 return;
1812 SeekAllSources(GetStartTime());
1813 StartReturningData();
1815 if (duration_ == kNoTimestamp())
1816 duration_ = kInfiniteDuration();
1818 // The demuxer is now initialized after the |start_timestamp_| was set.
1819 ChangeState_Locked(INITIALIZED);
1820 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1823 ChunkDemuxerStream*
1824 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1825 switch (type) {
1826 case DemuxerStream::AUDIO:
1827 if (audio_)
1828 return NULL;
1829 audio_.reset(
1830 new ChunkDemuxerStream(DemuxerStream::AUDIO, splice_frames_enabled_));
1831 return audio_.get();
1832 break;
1833 case DemuxerStream::VIDEO:
1834 if (video_)
1835 return NULL;
1836 video_.reset(
1837 new ChunkDemuxerStream(DemuxerStream::VIDEO, splice_frames_enabled_));
1838 return video_.get();
1839 break;
1840 case DemuxerStream::TEXT: {
1841 return new ChunkDemuxerStream(DemuxerStream::TEXT,
1842 splice_frames_enabled_);
1843 break;
1845 case DemuxerStream::UNKNOWN:
1846 case DemuxerStream::NUM_TYPES:
1847 NOTREACHED();
1848 return NULL;
1850 NOTREACHED();
1851 return NULL;
1854 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1855 const TextTrackConfig& config) {
1856 lock_.AssertAcquired();
1857 DCHECK_NE(state_, SHUTDOWN);
1858 host_->AddTextStream(text_stream, config);
1861 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1862 lock_.AssertAcquired();
1863 return source_state_map_.count(source_id) > 0u;
1866 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1867 DCHECK(duration_ != new_duration);
1868 user_specified_duration_ = -1;
1869 duration_ = new_duration;
1870 host_->SetDuration(new_duration);
1873 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration) {
1874 DCHECK(new_duration != kNoTimestamp());
1875 DCHECK(new_duration != kInfiniteDuration());
1877 // Per April 1, 2014 MSE spec editor's draft:
1878 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1879 // media-source.html#sourcebuffer-coded-frame-processing
1880 // 5. If the media segment contains data beyond the current duration, then run
1881 // the duration change algorithm with new duration set to the maximum of
1882 // the current duration and the group end timestamp.
1884 if (new_duration <= duration_)
1885 return;
1887 DVLOG(2) << __FUNCTION__ << ": Increasing duration: "
1888 << duration_.InSecondsF() << " -> " << new_duration.InSecondsF();
1890 UpdateDuration(new_duration);
1893 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1894 lock_.AssertAcquired();
1896 TimeDelta max_duration;
1898 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1899 itr != source_state_map_.end(); ++itr) {
1900 max_duration = std::max(max_duration,
1901 itr->second->GetMaxBufferedDuration());
1904 if (max_duration == TimeDelta())
1905 return;
1907 if (max_duration < duration_)
1908 UpdateDuration(max_duration);
1911 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1912 base::AutoLock auto_lock(lock_);
1913 return GetBufferedRanges_Locked();
1916 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1917 lock_.AssertAcquired();
1919 bool ended = state_ == ENDED;
1920 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1921 // we'll need to update this loop to only add ranges from active sources.
1922 RangesList ranges_list;
1923 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1924 itr != source_state_map_.end(); ++itr) {
1925 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1928 return ComputeIntersection(ranges_list, ended);
1931 void ChunkDemuxer::StartReturningData() {
1932 for (SourceStateMap::iterator itr = source_state_map_.begin();
1933 itr != source_state_map_.end(); ++itr) {
1934 itr->second->StartReturningData();
1938 void ChunkDemuxer::AbortPendingReads() {
1939 for (SourceStateMap::iterator itr = source_state_map_.begin();
1940 itr != source_state_map_.end(); ++itr) {
1941 itr->second->AbortReads();
1945 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1946 for (SourceStateMap::iterator itr = source_state_map_.begin();
1947 itr != source_state_map_.end(); ++itr) {
1948 itr->second->Seek(seek_time);
1952 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1953 for (SourceStateMap::iterator itr = source_state_map_.begin();
1954 itr != source_state_map_.end(); ++itr) {
1955 itr->second->CompletePendingReadIfPossible();
1959 void ChunkDemuxer::ShutdownAllStreams() {
1960 for (SourceStateMap::iterator itr = source_state_map_.begin();
1961 itr != source_state_map_.end(); ++itr) {
1962 itr->second->Shutdown();
1966 } // namespace media