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"
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/location.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/stl_util.h"
16 #include "media/base/audio_decoder_config.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/stream_parser_buffer.h"
19 #include "media/base/video_decoder_config.h"
20 #include "media/filters/frame_processor.h"
21 #include "media/filters/stream_parser_factory.h"
23 using base::TimeDelta;
27 static TimeDelta EndTimestamp(const StreamParser::BufferQueue& queue) {
28 return queue.back()->timestamp() + queue.back()->duration();
31 // List of time ranges for each SourceBuffer.
32 typedef std::list<Ranges<TimeDelta> > RangesList;
33 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
35 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
36 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
38 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
39 // TimeRanges object and abort these steps.
40 if (activeRanges.empty())
41 return Ranges<TimeDelta>();
43 // Step 2: Let active ranges be the ranges returned by buffered for each
44 // SourceBuffer object in activeSourceBuffers.
45 // Step 3: Let highest end time be the largest range end time in the active
47 TimeDelta highest_end_time;
48 for (RangesList::const_iterator itr = activeRanges.begin();
49 itr != activeRanges.end(); ++itr) {
53 highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
56 // Step 4: Let intersection ranges equal a TimeRange object containing a
57 // single range from 0 to highest end time.
58 Ranges<TimeDelta> intersection_ranges;
59 intersection_ranges.Add(TimeDelta(), highest_end_time);
61 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
63 for (RangesList::const_iterator itr = activeRanges.begin();
64 itr != activeRanges.end(); ++itr) {
65 // Step 5.1: Let source ranges equal the ranges returned by the buffered
66 // attribute on the current SourceBuffer.
67 Ranges<TimeDelta> source_ranges = *itr;
69 // Step 5.2: If readyState is "ended", then set the end time on the last
70 // range in source ranges to highest end time.
71 if (ended && source_ranges.size() > 0u) {
72 source_ranges.Add(source_ranges.start(source_ranges.size() - 1),
76 // Step 5.3: Let new intersection ranges equal the intersection between
77 // the intersection ranges and the source ranges.
78 // Step 5.4: Replace the ranges in intersection ranges with the new
79 // intersection ranges.
80 intersection_ranges = intersection_ranges.IntersectionWith(source_ranges);
83 return intersection_ranges;
86 // Contains state belonging to a source id.
89 // Callback signature used to create ChunkDemuxerStreams.
90 typedef base::Callback<ChunkDemuxerStream*(
91 DemuxerStream::Type)> CreateDemuxerStreamCB;
93 typedef base::Callback<void(
94 ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
97 scoped_ptr<StreamParser> stream_parser,
98 scoped_ptr<FrameProcessor> frame_processor, const LogCB& log_cb,
99 const CreateDemuxerStreamCB& create_demuxer_stream_cb);
103 void Init(const StreamParser::InitCB& init_cb,
106 const StreamParser::NeedKeyCB& need_key_cb,
107 const NewTextTrackCB& new_text_track_cb);
109 // Appends new data to the StreamParser.
110 // Returns true if the data was successfully appended. Returns false if an
111 // error occurred. |*timestamp_offset| is used and possibly updated by the
112 // append. |append_window_start| and |append_window_end| correspond to the MSE
113 // spec's similarly named source buffer attributes that are used in coded
115 bool Append(const uint8* data, size_t length,
116 TimeDelta append_window_start,
117 TimeDelta append_window_end,
118 TimeDelta* timestamp_offset);
120 // Aborts the current append sequence and resets the parser.
121 void Abort(TimeDelta append_window_start,
122 TimeDelta append_window_end,
123 TimeDelta* timestamp_offset);
125 // Calls Remove(|start|, |end|, |duration|) on all
126 // ChunkDemuxerStreams managed by this object.
127 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
129 // Returns true if currently parsing a media segment, or false otherwise.
130 bool parsing_media_segment() const { return parsing_media_segment_; }
132 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
133 void SetSequenceMode(bool sequence_mode);
135 // Signals the coded frame processor to update its group start timestamp to be
136 // |timestamp_offset| if it is in sequence append mode.
137 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset);
139 // Returns the range of buffered data in this source, capped at |duration|.
140 // |ended| - Set to true if end of stream has been signaled and the special
141 // end of stream range logic needs to be executed.
142 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
144 // Returns the highest buffered duration across all streams managed
146 // Returns TimeDelta() if none of the streams contain buffered data.
147 TimeDelta GetMaxBufferedDuration() const;
149 // Helper methods that call methods with similar names on all the
150 // ChunkDemuxerStreams managed by this object.
151 void StartReturningData();
153 void Seek(TimeDelta seek_time);
154 void CompletePendingReadIfPossible();
155 void OnSetDuration(TimeDelta duration);
156 void MarkEndOfStream();
157 void UnmarkEndOfStream();
159 // Sets the memory limit on each stream. |memory_limit| is the
160 // maximum number of bytes each stream is allowed to hold in its buffer.
161 void SetMemoryLimitsForTesting(int memory_limit);
162 bool IsSeekWaitingForData() const;
165 // Called by the |stream_parser_| when a new initialization segment is
167 // Returns true on a successful call. Returns false if an error occurred while
168 // processing decoder configurations.
169 bool OnNewConfigs(bool allow_audio, bool allow_video,
170 const AudioDecoderConfig& audio_config,
171 const VideoDecoderConfig& video_config,
172 const StreamParser::TextTrackConfigMap& text_configs);
174 // Called by the |stream_parser_| at the beginning of a new media segment.
175 void OnNewMediaSegment();
177 // Called by the |stream_parser_| at the end of a media segment.
178 void OnEndOfMediaSegment();
180 // Called by the |stream_parser_| when new buffers have been parsed.
181 // It processes the new buffers using |frame_processor_|, which includes
182 // appending the processed frames to associated demuxer streams for each
184 // Returns true on a successful call. Returns false if an error occurred while
185 // processing the buffers.
186 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
187 const StreamParser::BufferQueue& video_buffers,
188 const StreamParser::TextBufferQueueMap& text_map);
190 void OnSourceInitDone(bool success,
191 const StreamParser::InitParameters& params);
193 CreateDemuxerStreamCB create_demuxer_stream_cb_;
194 NewTextTrackCB new_text_track_cb_;
196 // During Append(), if OnNewBuffers() coded frame processing updates the
197 // timestamp offset then |*timestamp_offset_during_append_| is also updated
198 // so Append()'s caller can know the new offset. This pointer is only non-NULL
199 // during the lifetime of an Append() call.
200 TimeDelta* timestamp_offset_during_append_;
202 // During Append(), coded frame processing triggered by OnNewBuffers()
203 // requires these two attributes. These are only valid during the lifetime of
205 TimeDelta append_window_start_during_append_;
206 TimeDelta append_window_end_during_append_;
208 // Set to true if the next buffers appended within the append window
209 // represent the start of a new media segment. This flag being set
210 // triggers a call to |new_segment_cb_| when the new buffers are
211 // appended. The flag is set on actual media segment boundaries and
212 // when the "append window" filtering causes discontinuities in the
214 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
215 // processing's discontinuity logic is enough. See http://crbug.com/351489.
216 bool new_media_segment_;
218 // Keeps track of whether a media segment is being parsed.
219 bool parsing_media_segment_;
221 // The object used to parse appended data.
222 scoped_ptr<StreamParser> stream_parser_;
224 ChunkDemuxerStream* audio_; // Not owned by |this|.
225 ChunkDemuxerStream* video_; // Not owned by |this|.
227 typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
228 TextStreamMap text_stream_map_; // |this| owns the map's stream pointers.
230 scoped_ptr<FrameProcessor> frame_processor_;
232 StreamParser::InitCB init_cb_;
234 // Indicates that timestampOffset should be updated automatically during
235 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
236 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
237 // changes to MSE spec. See http://crbug.com/371499.
238 bool auto_update_timestamp_offset_;
240 DISALLOW_COPY_AND_ASSIGN(SourceState);
243 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
244 scoped_ptr<FrameProcessor> frame_processor,
246 const CreateDemuxerStreamCB& create_demuxer_stream_cb)
247 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
248 timestamp_offset_during_append_(NULL),
249 new_media_segment_(false),
250 parsing_media_segment_(false),
251 stream_parser_(stream_parser.release()),
254 frame_processor_(frame_processor.release()),
256 auto_update_timestamp_offset_(false) {
257 DCHECK(!create_demuxer_stream_cb_.is_null());
258 DCHECK(frame_processor_);
261 SourceState::~SourceState() {
264 STLDeleteValues(&text_stream_map_);
267 void SourceState::Init(const StreamParser::InitCB& init_cb,
270 const StreamParser::NeedKeyCB& need_key_cb,
271 const NewTextTrackCB& new_text_track_cb) {
272 new_text_track_cb_ = new_text_track_cb;
275 stream_parser_->Init(
276 base::Bind(&SourceState::OnSourceInitDone, base::Unretained(this)),
277 base::Bind(&SourceState::OnNewConfigs,
278 base::Unretained(this),
281 base::Bind(&SourceState::OnNewBuffers, base::Unretained(this)),
282 new_text_track_cb_.is_null(),
284 base::Bind(&SourceState::OnNewMediaSegment, base::Unretained(this)),
285 base::Bind(&SourceState::OnEndOfMediaSegment, base::Unretained(this)),
289 void SourceState::SetSequenceMode(bool sequence_mode) {
290 DCHECK(!parsing_media_segment_);
292 frame_processor_->SetSequenceMode(sequence_mode);
295 void SourceState::SetGroupStartTimestampIfInSequenceMode(
296 base::TimeDelta timestamp_offset) {
297 DCHECK(!parsing_media_segment_);
299 frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
302 bool SourceState::Append(const uint8* data, size_t length,
303 TimeDelta append_window_start,
304 TimeDelta append_window_end,
305 TimeDelta* timestamp_offset) {
306 DCHECK(timestamp_offset);
307 DCHECK(!timestamp_offset_during_append_);
308 append_window_start_during_append_ = append_window_start;
309 append_window_end_during_append_ = append_window_end;
310 timestamp_offset_during_append_ = timestamp_offset;
312 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
313 // append window and timestamp offset pointer. See http://crbug.com/351454.
314 bool err = stream_parser_->Parse(data, length);
315 timestamp_offset_during_append_ = NULL;
319 void SourceState::Abort(TimeDelta append_window_start,
320 TimeDelta append_window_end,
321 base::TimeDelta* timestamp_offset) {
322 DCHECK(timestamp_offset);
323 DCHECK(!timestamp_offset_during_append_);
324 timestamp_offset_during_append_ = timestamp_offset;
325 append_window_start_during_append_ = append_window_start;
326 append_window_end_during_append_ = append_window_end;
328 stream_parser_->Flush();
329 timestamp_offset_during_append_ = NULL;
331 frame_processor_->Reset();
332 parsing_media_segment_ = false;
335 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
337 audio_->Remove(start, end, duration);
340 video_->Remove(start, end, duration);
342 for (TextStreamMap::iterator itr = text_stream_map_.begin();
343 itr != text_stream_map_.end(); ++itr) {
344 itr->second->Remove(start, end, duration);
348 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
350 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
351 // this code to only add ranges from active tracks.
352 RangesList ranges_list;
354 ranges_list.push_back(audio_->GetBufferedRanges(duration));
357 ranges_list.push_back(video_->GetBufferedRanges(duration));
359 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
360 itr != text_stream_map_.end(); ++itr) {
361 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
364 return ComputeIntersection(ranges_list, ended);
367 TimeDelta SourceState::GetMaxBufferedDuration() const {
368 TimeDelta max_duration;
371 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
374 max_duration = std::max(max_duration, video_->GetBufferedDuration());
376 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
377 itr != text_stream_map_.end(); ++itr) {
378 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
384 void SourceState::StartReturningData() {
386 audio_->StartReturningData();
389 video_->StartReturningData();
391 for (TextStreamMap::iterator itr = text_stream_map_.begin();
392 itr != text_stream_map_.end(); ++itr) {
393 itr->second->StartReturningData();
397 void SourceState::AbortReads() {
399 audio_->AbortReads();
402 video_->AbortReads();
404 for (TextStreamMap::iterator itr = text_stream_map_.begin();
405 itr != text_stream_map_.end(); ++itr) {
406 itr->second->AbortReads();
410 void SourceState::Seek(TimeDelta seek_time) {
412 audio_->Seek(seek_time);
415 video_->Seek(seek_time);
417 for (TextStreamMap::iterator itr = text_stream_map_.begin();
418 itr != text_stream_map_.end(); ++itr) {
419 itr->second->Seek(seek_time);
423 void SourceState::CompletePendingReadIfPossible() {
425 audio_->CompletePendingReadIfPossible();
428 video_->CompletePendingReadIfPossible();
430 for (TextStreamMap::iterator itr = text_stream_map_.begin();
431 itr != text_stream_map_.end(); ++itr) {
432 itr->second->CompletePendingReadIfPossible();
436 void SourceState::OnSetDuration(TimeDelta duration) {
438 audio_->OnSetDuration(duration);
441 video_->OnSetDuration(duration);
443 for (TextStreamMap::iterator itr = text_stream_map_.begin();
444 itr != text_stream_map_.end(); ++itr) {
445 itr->second->OnSetDuration(duration);
449 void SourceState::MarkEndOfStream() {
451 audio_->MarkEndOfStream();
454 video_->MarkEndOfStream();
456 for (TextStreamMap::iterator itr = text_stream_map_.begin();
457 itr != text_stream_map_.end(); ++itr) {
458 itr->second->MarkEndOfStream();
462 void SourceState::UnmarkEndOfStream() {
464 audio_->UnmarkEndOfStream();
467 video_->UnmarkEndOfStream();
469 for (TextStreamMap::iterator itr = text_stream_map_.begin();
470 itr != text_stream_map_.end(); ++itr) {
471 itr->second->UnmarkEndOfStream();
475 void SourceState::Shutdown() {
482 for (TextStreamMap::iterator itr = text_stream_map_.begin();
483 itr != text_stream_map_.end(); ++itr) {
484 itr->second->Shutdown();
488 void SourceState::SetMemoryLimitsForTesting(int memory_limit) {
490 audio_->set_memory_limit_for_testing(memory_limit);
493 video_->set_memory_limit_for_testing(memory_limit);
495 for (TextStreamMap::iterator itr = text_stream_map_.begin();
496 itr != text_stream_map_.end(); ++itr) {
497 itr->second->set_memory_limit_for_testing(memory_limit);
501 bool SourceState::IsSeekWaitingForData() const {
502 if (audio_ && audio_->IsSeekWaitingForData())
505 if (video_ && video_->IsSeekWaitingForData())
508 // NOTE: We are intentionally not checking the text tracks
509 // because text tracks are discontinuous and may not have data
510 // for the seek position. This is ok and playback should not be
511 // stalled because we don't have cues. If cues, with timestamps after
512 // the seek time, eventually arrive they will be delivered properly
513 // in response to ChunkDemuxerStream::Read() calls.
518 bool SourceState::OnNewConfigs(
519 bool allow_audio, bool allow_video,
520 const AudioDecoderConfig& audio_config,
521 const VideoDecoderConfig& video_config,
522 const StreamParser::TextTrackConfigMap& text_configs) {
523 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
524 << ", " << audio_config.IsValidConfig()
525 << ", " << video_config.IsValidConfig() << ")";
527 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
528 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
532 // Signal an error if we get configuration info for stream types that weren't
533 // specified in AddId() or more configs after a stream is initialized.
534 if (allow_audio != audio_config.IsValidConfig()) {
536 << "Initialization segment"
537 << (audio_config.IsValidConfig() ? " has" : " does not have")
538 << " an audio track, but the mimetype"
539 << (allow_audio ? " specifies" : " does not specify")
540 << " an audio codec.";
544 if (allow_video != video_config.IsValidConfig()) {
546 << "Initialization segment"
547 << (video_config.IsValidConfig() ? " has" : " does not have")
548 << " a video track, but the mimetype"
549 << (allow_video ? " specifies" : " does not specify")
550 << " a video codec.";
555 if (audio_config.IsValidConfig()) {
557 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
560 DVLOG(1) << "Failed to create an audio stream.";
564 if (!frame_processor_->AddTrack(FrameProcessor::kAudioTrackId, audio_)) {
565 DVLOG(1) << "Failed to add audio track to frame processor.";
570 frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
571 success &= audio_->UpdateAudioConfig(audio_config, log_cb_);
574 if (video_config.IsValidConfig()) {
576 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
579 DVLOG(1) << "Failed to create a video stream.";
583 if (!frame_processor_->AddTrack(FrameProcessor::kVideoTrackId, video_)) {
584 DVLOG(1) << "Failed to add video track to frame processor.";
589 success &= video_->UpdateVideoConfig(video_config, log_cb_);
592 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
593 if (text_stream_map_.empty()) {
594 for (TextConfigItr itr = text_configs.begin();
595 itr != text_configs.end(); ++itr) {
596 ChunkDemuxerStream* const text_stream =
597 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
598 if (!frame_processor_->AddTrack(itr->first, text_stream)) {
600 MEDIA_LOG(log_cb_) << "Failed to add text track ID " << itr->first
601 << " to frame processor.";
604 text_stream->UpdateTextConfig(itr->second, log_cb_);
605 text_stream_map_[itr->first] = text_stream;
606 new_text_track_cb_.Run(text_stream, itr->second);
609 const size_t text_count = text_stream_map_.size();
610 if (text_configs.size() != text_count) {
612 MEDIA_LOG(log_cb_) << "The number of text track configs changed.";
613 } else if (text_count == 1) {
614 TextConfigItr config_itr = text_configs.begin();
615 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
616 ChunkDemuxerStream* text_stream = stream_itr->second;
617 TextTrackConfig old_config = text_stream->text_track_config();
618 TextTrackConfig new_config(config_itr->second.kind(),
619 config_itr->second.label(),
620 config_itr->second.language(),
622 if (!new_config.Matches(old_config)) {
624 MEDIA_LOG(log_cb_) << "New text track config does not match old one.";
626 StreamParser::TrackId old_id = stream_itr->first;
627 StreamParser::TrackId new_id = config_itr->first;
628 if (new_id != old_id) {
629 if (frame_processor_->UpdateTrack(old_id, new_id)) {
630 text_stream_map_.clear();
631 text_stream_map_[config_itr->first] = text_stream;
634 MEDIA_LOG(log_cb_) << "Error remapping single text track number";
639 for (TextConfigItr config_itr = text_configs.begin();
640 config_itr != text_configs.end(); ++config_itr) {
641 TextStreamMap::iterator stream_itr =
642 text_stream_map_.find(config_itr->first);
643 if (stream_itr == text_stream_map_.end()) {
645 MEDIA_LOG(log_cb_) << "Unexpected text track configuration "
647 << config_itr->first;
651 const TextTrackConfig& new_config = config_itr->second;
652 ChunkDemuxerStream* stream = stream_itr->second;
653 TextTrackConfig old_config = stream->text_track_config();
654 if (!new_config.Matches(old_config)) {
656 MEDIA_LOG(log_cb_) << "New text track config for track ID "
658 << " does not match old one.";
665 frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
667 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
671 void SourceState::OnNewMediaSegment() {
672 DVLOG(2) << "OnNewMediaSegment()";
673 parsing_media_segment_ = true;
674 new_media_segment_ = true;
677 void SourceState::OnEndOfMediaSegment() {
678 DVLOG(2) << "OnEndOfMediaSegment()";
679 parsing_media_segment_ = false;
680 new_media_segment_ = false;
683 bool SourceState::OnNewBuffers(
684 const StreamParser::BufferQueue& audio_buffers,
685 const StreamParser::BufferQueue& video_buffers,
686 const StreamParser::TextBufferQueueMap& text_map) {
687 DVLOG(2) << "OnNewBuffers()";
688 DCHECK(timestamp_offset_during_append_);
689 DCHECK(parsing_media_segment_);
691 const TimeDelta timestamp_offset_before_processing =
692 *timestamp_offset_during_append_;
694 // Calculate the new timestamp offset for audio/video tracks if the stream
695 // parser has requested automatic updates.
696 TimeDelta new_timestamp_offset = timestamp_offset_before_processing;
697 if (auto_update_timestamp_offset_) {
698 const bool have_audio_buffers = !audio_buffers.empty();
699 const bool have_video_buffers = !video_buffers.empty();
700 if (have_audio_buffers && have_video_buffers) {
701 new_timestamp_offset +=
702 std::min(EndTimestamp(audio_buffers), EndTimestamp(video_buffers));
703 } else if (have_audio_buffers) {
704 new_timestamp_offset += EndTimestamp(audio_buffers);
705 } else if (have_video_buffers) {
706 new_timestamp_offset += EndTimestamp(video_buffers);
710 if (!frame_processor_->ProcessFrames(audio_buffers,
713 append_window_start_during_append_,
714 append_window_end_during_append_,
716 timestamp_offset_during_append_)) {
720 // Only update the timestamp offset if the frame processor hasn't already.
721 if (auto_update_timestamp_offset_ &&
722 timestamp_offset_before_processing == *timestamp_offset_during_append_) {
723 *timestamp_offset_during_append_ = new_timestamp_offset;
729 void SourceState::OnSourceInitDone(bool success,
730 const StreamParser::InitParameters& params) {
731 auto_update_timestamp_offset_ = params.auto_update_timestamp_offset;
732 base::ResetAndReturn(&init_cb_).Run(success, params);
735 ChunkDemuxerStream::ChunkDemuxerStream(Type type, bool splice_frames_enabled)
737 state_(UNINITIALIZED),
738 splice_frames_enabled_(splice_frames_enabled),
739 partial_append_window_trimming_enabled_(false) {
742 void ChunkDemuxerStream::StartReturningData() {
743 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
744 base::AutoLock auto_lock(lock_);
745 DCHECK(read_cb_.is_null());
746 ChangeState_Locked(RETURNING_DATA_FOR_READS);
749 void ChunkDemuxerStream::AbortReads() {
750 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
751 base::AutoLock auto_lock(lock_);
752 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
753 if (!read_cb_.is_null())
754 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
757 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
758 base::AutoLock auto_lock(lock_);
759 if (read_cb_.is_null())
762 CompletePendingReadIfPossible_Locked();
765 void ChunkDemuxerStream::Shutdown() {
766 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
767 base::AutoLock auto_lock(lock_);
768 ChangeState_Locked(SHUTDOWN);
770 // Pass an end of stream buffer to the pending callback to signal that no more
771 // data will be sent.
772 if (!read_cb_.is_null()) {
773 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
774 StreamParserBuffer::CreateEOSBuffer());
778 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
779 base::AutoLock auto_lock(lock_);
781 // This method should not be called for text tracks. See the note in
782 // SourceState::IsSeekWaitingForData().
783 DCHECK_NE(type_, DemuxerStream::TEXT);
785 return stream_->IsSeekPending();
788 void ChunkDemuxerStream::Seek(TimeDelta time) {
789 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
790 base::AutoLock auto_lock(lock_);
791 DCHECK(read_cb_.is_null());
792 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
798 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
802 base::AutoLock auto_lock(lock_);
803 DCHECK_NE(state_, SHUTDOWN);
804 if (!stream_->Append(buffers)) {
805 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
809 if (!read_cb_.is_null())
810 CompletePendingReadIfPossible_Locked();
815 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
816 TimeDelta duration) {
817 base::AutoLock auto_lock(lock_);
818 stream_->Remove(start, end, duration);
821 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
822 base::AutoLock auto_lock(lock_);
823 stream_->OnSetDuration(duration);
826 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
827 TimeDelta duration) const {
828 base::AutoLock auto_lock(lock_);
831 // Since text tracks are discontinuous and the lack of cues should not block
832 // playback, report the buffered range for text tracks as [0, |duration|) so
833 // that intesections with audio & video tracks are computed correctly when
834 // no cues are present.
835 Ranges<TimeDelta> text_range;
836 text_range.Add(TimeDelta(), duration);
840 Ranges<TimeDelta> range = stream_->GetBufferedTime();
842 if (range.size() == 0u)
845 // Clamp the end of the stream's buffered ranges to fit within the duration.
846 // This can be done by intersecting the stream's range with the valid time
848 Ranges<TimeDelta> valid_time_range;
849 valid_time_range.Add(range.start(0), duration);
850 return range.IntersectionWith(valid_time_range);
853 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
854 return stream_->GetBufferedDuration();
857 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp) {
858 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
859 << start_timestamp.InSecondsF() << ")";
860 base::AutoLock auto_lock(lock_);
861 stream_->OnNewMediaSegment(start_timestamp);
864 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig& config,
865 const LogCB& log_cb) {
866 DCHECK(config.IsValidConfig());
867 DCHECK_EQ(type_, AUDIO);
868 base::AutoLock auto_lock(lock_);
870 DCHECK_EQ(state_, UNINITIALIZED);
872 // On platforms which support splice frames, enable splice frames and
873 // partial append window support for most codecs (notably: not opus).
874 const bool codec_supported = config.codec() == kCodecMP3 ||
875 config.codec() == kCodecAAC ||
876 config.codec() == kCodecVorbis;
877 splice_frames_enabled_ = splice_frames_enabled_ && codec_supported;
878 partial_append_window_trimming_enabled_ =
879 splice_frames_enabled_ && codec_supported;
882 new SourceBufferStream(config, log_cb, splice_frames_enabled_));
886 return stream_->UpdateAudioConfig(config);
889 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig& config,
890 const LogCB& log_cb) {
891 DCHECK(config.IsValidConfig());
892 DCHECK_EQ(type_, VIDEO);
893 base::AutoLock auto_lock(lock_);
896 DCHECK_EQ(state_, UNINITIALIZED);
898 new SourceBufferStream(config, log_cb, splice_frames_enabled_));
902 return stream_->UpdateVideoConfig(config);
905 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig& config,
906 const LogCB& log_cb) {
907 DCHECK_EQ(type_, TEXT);
908 base::AutoLock auto_lock(lock_);
910 DCHECK_EQ(state_, UNINITIALIZED);
911 stream_.reset(new SourceBufferStream(config, log_cb, splice_frames_enabled_));
914 void ChunkDemuxerStream::MarkEndOfStream() {
915 base::AutoLock auto_lock(lock_);
916 stream_->MarkEndOfStream();
919 void ChunkDemuxerStream::UnmarkEndOfStream() {
920 base::AutoLock auto_lock(lock_);
921 stream_->UnmarkEndOfStream();
924 // DemuxerStream methods.
925 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
926 base::AutoLock auto_lock(lock_);
927 DCHECK_NE(state_, UNINITIALIZED);
928 DCHECK(read_cb_.is_null());
930 read_cb_ = BindToCurrentLoop(read_cb);
931 CompletePendingReadIfPossible_Locked();
934 DemuxerStream::Type ChunkDemuxerStream::type() { return type_; }
936 void ChunkDemuxerStream::EnableBitstreamConverter() {}
938 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
939 CHECK_EQ(type_, AUDIO);
940 base::AutoLock auto_lock(lock_);
941 return stream_->GetCurrentAudioDecoderConfig();
944 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
945 CHECK_EQ(type_, VIDEO);
946 base::AutoLock auto_lock(lock_);
947 return stream_->GetCurrentVideoDecoderConfig();
950 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
952 TextTrackConfig ChunkDemuxerStream::text_track_config() {
953 CHECK_EQ(type_, TEXT);
954 base::AutoLock auto_lock(lock_);
955 return stream_->GetCurrentTextTrackConfig();
958 VideoRotation ChunkDemuxerStream::video_rotation() {
959 return VIDEO_ROTATION_0;
962 void ChunkDemuxerStream::ChangeState_Locked(State state) {
963 lock_.AssertAcquired();
964 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
966 << " - " << state_ << " -> " << state;
970 ChunkDemuxerStream::~ChunkDemuxerStream() {}
972 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
973 lock_.AssertAcquired();
974 DCHECK(!read_cb_.is_null());
976 DemuxerStream::Status status;
977 scoped_refptr<StreamParserBuffer> buffer;
983 case RETURNING_DATA_FOR_READS:
984 switch (stream_->GetNextBuffer(&buffer)) {
985 case SourceBufferStream::kSuccess:
986 status = DemuxerStream::kOk;
988 case SourceBufferStream::kNeedBuffer:
989 // Return early without calling |read_cb_| since we don't have
990 // any data to return yet.
992 case SourceBufferStream::kEndOfStream:
993 status = DemuxerStream::kOk;
994 buffer = StreamParserBuffer::CreateEOSBuffer();
996 case SourceBufferStream::kConfigChange:
997 DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
998 status = kConfigChanged;
1003 case RETURNING_ABORT_FOR_READS:
1004 // Null buffers should be returned in this state since we are waiting
1005 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1006 // because they are associated with the seek.
1007 status = DemuxerStream::kAborted;
1011 status = DemuxerStream::kOk;
1012 buffer = StreamParserBuffer::CreateEOSBuffer();
1016 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1019 ChunkDemuxer::ChunkDemuxer(const base::Closure& open_cb,
1020 const NeedKeyCB& need_key_cb,
1021 const LogCB& log_cb,
1022 bool splice_frames_enabled)
1023 : state_(WAITING_FOR_INIT),
1024 cancel_next_seek_(false),
1027 need_key_cb_(need_key_cb),
1028 enable_text_(false),
1030 duration_(kNoTimestamp()),
1031 user_specified_duration_(-1),
1032 liveness_(LIVENESS_UNKNOWN),
1033 splice_frames_enabled_(splice_frames_enabled) {
1034 DCHECK(!open_cb_.is_null());
1035 DCHECK(!need_key_cb_.is_null());
1038 void ChunkDemuxer::Initialize(
1040 const PipelineStatusCB& cb,
1041 bool enable_text_tracks) {
1042 DVLOG(1) << "Init()";
1044 base::AutoLock auto_lock(lock_);
1046 init_cb_ = BindToCurrentLoop(cb);
1047 if (state_ == SHUTDOWN) {
1048 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1051 DCHECK_EQ(state_, WAITING_FOR_INIT);
1053 enable_text_ = enable_text_tracks;
1055 ChangeState_Locked(INITIALIZING);
1057 base::ResetAndReturn(&open_cb_).Run();
1060 void ChunkDemuxer::Stop(const base::Closure& callback) {
1061 DVLOG(1) << "Stop()";
1066 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1067 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1068 DCHECK(time >= TimeDelta());
1070 base::AutoLock auto_lock(lock_);
1071 DCHECK(seek_cb_.is_null());
1073 seek_cb_ = BindToCurrentLoop(cb);
1074 if (state_ != INITIALIZED && state_ != ENDED) {
1075 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1079 if (cancel_next_seek_) {
1080 cancel_next_seek_ = false;
1081 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1085 SeekAllSources(time);
1086 StartReturningData();
1088 if (IsSeekWaitingForData_Locked()) {
1089 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1093 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1096 // Demuxer implementation.
1097 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1098 DCHECK_NE(type, DemuxerStream::TEXT);
1099 base::AutoLock auto_lock(lock_);
1100 if (type == DemuxerStream::VIDEO)
1101 return video_.get();
1103 if (type == DemuxerStream::AUDIO)
1104 return audio_.get();
1109 base::Time ChunkDemuxer::GetTimelineOffset() const {
1110 return timeline_offset_;
1113 TimeDelta ChunkDemuxer::GetStartTime() const {
1117 Demuxer::Liveness ChunkDemuxer::GetLiveness() const {
1121 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1122 DVLOG(1) << "StartWaitingForSeek()";
1123 base::AutoLock auto_lock(lock_);
1124 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1125 state_ == PARSE_ERROR) << state_;
1126 DCHECK(seek_cb_.is_null());
1128 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1131 AbortPendingReads();
1132 SeekAllSources(seek_time);
1134 // Cancel state set in CancelPendingSeek() since we want to
1135 // accept the next Seek().
1136 cancel_next_seek_ = false;
1139 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1140 base::AutoLock auto_lock(lock_);
1141 DCHECK_NE(state_, INITIALIZING);
1142 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1144 if (cancel_next_seek_)
1147 AbortPendingReads();
1148 SeekAllSources(seek_time);
1150 if (seek_cb_.is_null()) {
1151 cancel_next_seek_ = true;
1155 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1158 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1159 const std::string& type,
1160 std::vector<std::string>& codecs) {
1161 base::AutoLock auto_lock(lock_);
1163 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1164 return kReachedIdLimit;
1166 bool has_audio = false;
1167 bool has_video = false;
1168 scoped_ptr<media::StreamParser> stream_parser(
1169 StreamParserFactory::Create(type, codecs, log_cb_,
1170 &has_audio, &has_video));
1173 return ChunkDemuxer::kNotSupported;
1175 if ((has_audio && !source_id_audio_.empty()) ||
1176 (has_video && !source_id_video_.empty()))
1177 return kReachedIdLimit;
1180 source_id_audio_ = id;
1183 source_id_video_ = id;
1185 scoped_ptr<FrameProcessor> frame_processor(
1186 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1187 base::Unretained(this))));
1189 scoped_ptr<SourceState> source_state(
1190 new SourceState(stream_parser.Pass(),
1191 frame_processor.Pass(), log_cb_,
1192 base::Bind(&ChunkDemuxer::CreateDemuxerStream,
1193 base::Unretained(this))));
1195 SourceState::NewTextTrackCB new_text_track_cb;
1198 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1199 base::Unretained(this));
1203 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1209 source_state_map_[id] = source_state.release();
1213 void ChunkDemuxer::RemoveId(const std::string& id) {
1214 base::AutoLock auto_lock(lock_);
1215 CHECK(IsValidId(id));
1217 delete source_state_map_[id];
1218 source_state_map_.erase(id);
1220 if (source_id_audio_ == id)
1221 source_id_audio_.clear();
1223 if (source_id_video_ == id)
1224 source_id_video_.clear();
1227 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1228 base::AutoLock auto_lock(lock_);
1229 DCHECK(!id.empty());
1231 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1233 DCHECK(itr != source_state_map_.end());
1234 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1237 void ChunkDemuxer::AppendData(const std::string& id,
1238 const uint8* data, size_t length,
1239 TimeDelta append_window_start,
1240 TimeDelta append_window_end,
1241 TimeDelta* timestamp_offset) {
1242 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1244 DCHECK(!id.empty());
1245 DCHECK(timestamp_offset);
1247 Ranges<TimeDelta> ranges;
1250 base::AutoLock auto_lock(lock_);
1251 DCHECK_NE(state_, ENDED);
1253 // Capture if any of the SourceBuffers are waiting for data before we start
1255 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1264 DCHECK(IsValidId(id));
1265 if (!source_state_map_[id]->Append(data, length,
1266 append_window_start,
1268 timestamp_offset)) {
1269 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1275 DCHECK(IsValidId(id));
1276 if (!source_state_map_[id]->Append(data, length,
1277 append_window_start,
1279 timestamp_offset)) {
1280 ReportError_Locked(PIPELINE_ERROR_DECODE);
1286 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1289 case WAITING_FOR_INIT:
1292 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1296 // Check to see if data was appended at the pending seek point. This
1297 // indicates we have parsed enough data to complete the seek.
1298 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1299 !seek_cb_.is_null()) {
1300 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1303 ranges = GetBufferedRanges_Locked();
1306 for (size_t i = 0; i < ranges.size(); ++i)
1307 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1310 void ChunkDemuxer::Abort(const std::string& id,
1311 TimeDelta append_window_start,
1312 TimeDelta append_window_end,
1313 TimeDelta* timestamp_offset) {
1314 DVLOG(1) << "Abort(" << id << ")";
1315 base::AutoLock auto_lock(lock_);
1316 DCHECK(!id.empty());
1317 CHECK(IsValidId(id));
1318 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1319 source_state_map_[id]->Abort(append_window_start,
1322 // Abort can possibly emit some buffers.
1323 // Need to check whether seeking can be completed.
1324 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1325 !seek_cb_.is_null()) {
1326 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1330 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1332 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1333 << ", " << end.InSecondsF() << ")";
1334 base::AutoLock auto_lock(lock_);
1336 DCHECK(!id.empty());
1337 CHECK(IsValidId(id));
1338 DCHECK(start >= base::TimeDelta()) << start.InSecondsF();
1339 DCHECK(start < end) << "start " << start.InSecondsF()
1340 << " end " << end.InSecondsF();
1341 DCHECK(duration_ != kNoTimestamp());
1342 DCHECK(start <= duration_) << "start " << start.InSecondsF()
1343 << " duration " << duration_.InSecondsF();
1345 if (start == duration_)
1348 source_state_map_[id]->Remove(start, end, duration_);
1351 double ChunkDemuxer::GetDuration() {
1352 base::AutoLock auto_lock(lock_);
1353 return GetDuration_Locked();
1356 double ChunkDemuxer::GetDuration_Locked() {
1357 lock_.AssertAcquired();
1358 if (duration_ == kNoTimestamp())
1359 return std::numeric_limits<double>::quiet_NaN();
1361 // Return positive infinity if the resource is unbounded.
1362 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1363 if (duration_ == kInfiniteDuration())
1364 return std::numeric_limits<double>::infinity();
1366 if (user_specified_duration_ >= 0)
1367 return user_specified_duration_;
1369 return duration_.InSecondsF();
1372 void ChunkDemuxer::SetDuration(double duration) {
1373 base::AutoLock auto_lock(lock_);
1374 DVLOG(1) << "SetDuration(" << duration << ")";
1375 DCHECK_GE(duration, 0);
1377 if (duration == GetDuration_Locked())
1380 // Compute & bounds check the TimeDelta representation of duration.
1381 // This can be different if the value of |duration| doesn't fit the range or
1382 // precision of TimeDelta.
1383 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1384 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1385 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1386 double min_duration_in_seconds = min_duration.InSecondsF();
1387 double max_duration_in_seconds = max_duration.InSecondsF();
1389 TimeDelta duration_td;
1390 if (duration == std::numeric_limits<double>::infinity()) {
1391 duration_td = media::kInfiniteDuration();
1392 } else if (duration < min_duration_in_seconds) {
1393 duration_td = min_duration;
1394 } else if (duration > max_duration_in_seconds) {
1395 duration_td = max_duration;
1397 duration_td = TimeDelta::FromMicroseconds(
1398 duration * base::Time::kMicrosecondsPerSecond);
1401 DCHECK(duration_td > TimeDelta());
1403 user_specified_duration_ = duration;
1404 duration_ = duration_td;
1405 host_->SetDuration(duration_);
1407 for (SourceStateMap::iterator itr = source_state_map_.begin();
1408 itr != source_state_map_.end(); ++itr) {
1409 itr->second->OnSetDuration(duration_);
1413 bool ChunkDemuxer::IsParsingMediaSegment(const std::string& id) {
1414 base::AutoLock auto_lock(lock_);
1415 DVLOG(1) << "IsParsingMediaSegment(" << id << ")";
1416 CHECK(IsValidId(id));
1418 return source_state_map_[id]->parsing_media_segment();
1421 void ChunkDemuxer::SetSequenceMode(const std::string& id,
1422 bool sequence_mode) {
1423 base::AutoLock auto_lock(lock_);
1424 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1425 CHECK(IsValidId(id));
1426 DCHECK_NE(state_, ENDED);
1428 source_state_map_[id]->SetSequenceMode(sequence_mode);
1431 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1432 const std::string& id,
1433 base::TimeDelta timestamp_offset) {
1434 base::AutoLock auto_lock(lock_);
1435 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id << ", "
1436 << timestamp_offset.InSecondsF() << ")";
1437 CHECK(IsValidId(id));
1438 DCHECK_NE(state_, ENDED);
1440 source_state_map_[id]->SetGroupStartTimestampIfInSequenceMode(
1445 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1446 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1447 base::AutoLock auto_lock(lock_);
1448 DCHECK_NE(state_, WAITING_FOR_INIT);
1449 DCHECK_NE(state_, ENDED);
1451 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1454 if (state_ == INITIALIZING) {
1455 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1459 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1460 for (SourceStateMap::iterator itr = source_state_map_.begin();
1461 itr != source_state_map_.end(); ++itr) {
1462 itr->second->MarkEndOfStream();
1465 CompletePendingReadsIfPossible();
1467 // Give a chance to resume the pending seek process.
1468 if (status != PIPELINE_OK) {
1469 ReportError_Locked(status);
1473 ChangeState_Locked(ENDED);
1474 DecreaseDurationIfNecessary();
1476 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1477 !seek_cb_.is_null()) {
1478 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1482 void ChunkDemuxer::UnmarkEndOfStream() {
1483 DVLOG(1) << "UnmarkEndOfStream()";
1484 base::AutoLock auto_lock(lock_);
1485 DCHECK_EQ(state_, ENDED);
1487 ChangeState_Locked(INITIALIZED);
1489 for (SourceStateMap::iterator itr = source_state_map_.begin();
1490 itr != source_state_map_.end(); ++itr) {
1491 itr->second->UnmarkEndOfStream();
1495 void ChunkDemuxer::Shutdown() {
1496 DVLOG(1) << "Shutdown()";
1497 base::AutoLock auto_lock(lock_);
1499 if (state_ == SHUTDOWN)
1502 ShutdownAllStreams();
1504 ChangeState_Locked(SHUTDOWN);
1506 if(!seek_cb_.is_null())
1507 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1510 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit) {
1511 for (SourceStateMap::iterator itr = source_state_map_.begin();
1512 itr != source_state_map_.end(); ++itr) {
1513 itr->second->SetMemoryLimitsForTesting(memory_limit);
1517 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1518 lock_.AssertAcquired();
1519 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1520 << state_ << " -> " << new_state;
1524 ChunkDemuxer::~ChunkDemuxer() {
1525 DCHECK_NE(state_, INITIALIZED);
1527 STLDeleteValues(&source_state_map_);
1530 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1531 DVLOG(1) << "ReportError_Locked(" << error << ")";
1532 lock_.AssertAcquired();
1533 DCHECK_NE(error, PIPELINE_OK);
1535 ChangeState_Locked(PARSE_ERROR);
1537 PipelineStatusCB cb;
1539 if (!init_cb_.is_null()) {
1540 std::swap(cb, init_cb_);
1542 if (!seek_cb_.is_null())
1543 std::swap(cb, seek_cb_);
1545 ShutdownAllStreams();
1548 if (!cb.is_null()) {
1553 base::AutoUnlock auto_unlock(lock_);
1554 host_->OnDemuxerError(error);
1557 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1558 lock_.AssertAcquired();
1559 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1560 itr != source_state_map_.end(); ++itr) {
1561 if (itr->second->IsSeekWaitingForData())
1568 void ChunkDemuxer::OnSourceInitDone(
1570 const StreamParser::InitParameters& params) {
1571 DVLOG(1) << "OnSourceInitDone(" << success << ", "
1572 << params.duration.InSecondsF() << ")";
1573 lock_.AssertAcquired();
1574 DCHECK_EQ(state_, INITIALIZING);
1575 if (!success || (!audio_ && !video_)) {
1576 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1580 if (params.duration != TimeDelta() && duration_ == kNoTimestamp())
1581 UpdateDuration(params.duration);
1583 if (!params.timeline_offset.is_null()) {
1584 if (!timeline_offset_.is_null() &&
1585 params.timeline_offset != timeline_offset_) {
1587 << "Timeline offset is not the same across all SourceBuffers.";
1588 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1592 timeline_offset_ = params.timeline_offset;
1595 if (params.liveness != LIVENESS_UNKNOWN) {
1596 if (liveness_ != LIVENESS_UNKNOWN && params.liveness != liveness_) {
1598 << "Liveness is not the same across all SourceBuffers.";
1599 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1603 liveness_ = params.liveness;
1606 // Wait until all streams have initialized.
1607 if ((!source_id_audio_.empty() && !audio_) ||
1608 (!source_id_video_.empty() && !video_)) {
1612 SeekAllSources(GetStartTime());
1613 StartReturningData();
1615 if (duration_ == kNoTimestamp())
1616 duration_ = kInfiniteDuration();
1618 // The demuxer is now initialized after the |start_timestamp_| was set.
1619 ChangeState_Locked(INITIALIZED);
1620 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1624 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1626 case DemuxerStream::AUDIO:
1630 new ChunkDemuxerStream(DemuxerStream::AUDIO, splice_frames_enabled_));
1631 return audio_.get();
1633 case DemuxerStream::VIDEO:
1637 new ChunkDemuxerStream(DemuxerStream::VIDEO, splice_frames_enabled_));
1638 return video_.get();
1640 case DemuxerStream::TEXT: {
1641 return new ChunkDemuxerStream(DemuxerStream::TEXT,
1642 splice_frames_enabled_);
1645 case DemuxerStream::UNKNOWN:
1646 case DemuxerStream::NUM_TYPES:
1654 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1655 const TextTrackConfig& config) {
1656 lock_.AssertAcquired();
1657 DCHECK_NE(state_, SHUTDOWN);
1658 host_->AddTextStream(text_stream, config);
1661 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1662 lock_.AssertAcquired();
1663 return source_state_map_.count(source_id) > 0u;
1666 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1667 DCHECK(duration_ != new_duration);
1668 user_specified_duration_ = -1;
1669 duration_ = new_duration;
1670 host_->SetDuration(new_duration);
1673 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration) {
1674 DCHECK(new_duration != kNoTimestamp());
1675 DCHECK(new_duration != kInfiniteDuration());
1677 // Per April 1, 2014 MSE spec editor's draft:
1678 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1679 // media-source.html#sourcebuffer-coded-frame-processing
1680 // 5. If the media segment contains data beyond the current duration, then run
1681 // the duration change algorithm with new duration set to the maximum of
1682 // the current duration and the group end timestamp.
1684 if (new_duration <= duration_)
1687 DVLOG(2) << __FUNCTION__ << ": Increasing duration: "
1688 << duration_.InSecondsF() << " -> " << new_duration.InSecondsF();
1690 UpdateDuration(new_duration);
1693 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1694 lock_.AssertAcquired();
1696 TimeDelta max_duration;
1698 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1699 itr != source_state_map_.end(); ++itr) {
1700 max_duration = std::max(max_duration,
1701 itr->second->GetMaxBufferedDuration());
1704 if (max_duration == TimeDelta())
1707 if (max_duration < duration_)
1708 UpdateDuration(max_duration);
1711 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1712 base::AutoLock auto_lock(lock_);
1713 return GetBufferedRanges_Locked();
1716 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1717 lock_.AssertAcquired();
1719 bool ended = state_ == ENDED;
1720 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1721 // we'll need to update this loop to only add ranges from active sources.
1722 RangesList ranges_list;
1723 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1724 itr != source_state_map_.end(); ++itr) {
1725 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1728 return ComputeIntersection(ranges_list, ended);
1731 void ChunkDemuxer::StartReturningData() {
1732 for (SourceStateMap::iterator itr = source_state_map_.begin();
1733 itr != source_state_map_.end(); ++itr) {
1734 itr->second->StartReturningData();
1738 void ChunkDemuxer::AbortPendingReads() {
1739 for (SourceStateMap::iterator itr = source_state_map_.begin();
1740 itr != source_state_map_.end(); ++itr) {
1741 itr->second->AbortReads();
1745 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1746 for (SourceStateMap::iterator itr = source_state_map_.begin();
1747 itr != source_state_map_.end(); ++itr) {
1748 itr->second->Seek(seek_time);
1752 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1753 for (SourceStateMap::iterator itr = source_state_map_.begin();
1754 itr != source_state_map_.end(); ++itr) {
1755 itr->second->CompletePendingReadIfPossible();
1759 void ChunkDemuxer::ShutdownAllStreams() {
1760 for (SourceStateMap::iterator itr = source_state_map_.begin();
1761 itr != source_state_map_.end(); ++itr) {
1762 itr->second->Shutdown();
1766 } // namespace media