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"
12 #include "base/bind.h"
13 #include "base/callback_helpers.h"
14 #include "base/location.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/stl_util.h"
17 #include "media/base/audio_decoder_config.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/stream_parser_buffer.h"
20 #include "media/base/video_decoder_config.h"
21 #include "media/filters/stream_parser_factory.h"
23 using base::TimeDelta;
27 // List of time ranges for each SourceBuffer.
28 typedef std::list<Ranges<TimeDelta> > RangesList;
29 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
31 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
32 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
34 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
35 // TimeRanges object and abort these steps.
36 if (activeRanges.empty())
37 return Ranges<TimeDelta>();
39 // Step 2: Let active ranges be the ranges returned by buffered for each
40 // SourceBuffer object in activeSourceBuffers.
41 // Step 3: Let highest end time be the largest range end time in the active
43 TimeDelta highest_end_time;
44 for (RangesList::const_iterator itr = activeRanges.begin();
45 itr != activeRanges.end(); ++itr) {
49 highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
52 // Step 4: Let intersection ranges equal a TimeRange object containing a
53 // single range from 0 to highest end time.
54 Ranges<TimeDelta> intersection_ranges;
55 intersection_ranges.Add(TimeDelta(), highest_end_time);
57 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
59 for (RangesList::const_iterator itr = activeRanges.begin();
60 itr != activeRanges.end(); ++itr) {
61 // Step 5.1: Let source ranges equal the ranges returned by the buffered
62 // attribute on the current SourceBuffer.
63 Ranges<TimeDelta> source_ranges = *itr;
65 // Step 5.2: If readyState is "ended", then set the end time on the last
66 // range in source ranges to highest end time.
67 if (ended && source_ranges.size() > 0u) {
68 source_ranges.Add(source_ranges.start(source_ranges.size() - 1),
72 // Step 5.3: Let new intersection ranges equal the intersection between
73 // the intersection ranges and the source ranges.
74 // Step 5.4: Replace the ranges in intersection ranges with the new
75 // intersection ranges.
76 intersection_ranges = intersection_ranges.IntersectionWith(source_ranges);
79 return intersection_ranges;
82 // Contains state belonging to a source id.
85 // Callback signature used to create ChunkDemuxerStreams.
86 typedef base::Callback<ChunkDemuxerStream*(
87 DemuxerStream::Type)> CreateDemuxerStreamCB;
89 // Callback signature used to notify ChunkDemuxer of timestamps
90 // that may cause the duration to be updated.
91 typedef base::Callback<void(
92 TimeDelta, ChunkDemuxerStream*)> IncreaseDurationCB;
94 typedef base::Callback<void(
95 ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
97 SourceState(scoped_ptr<StreamParser> stream_parser, const LogCB& log_cb,
98 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
99 const IncreaseDurationCB& increase_duration_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
112 bool Append(const uint8* data, size_t length);
114 // Aborts the current append sequence and resets the parser.
117 // Calls Remove(|start|, |end|, |duration|) on all
118 // ChunkDemuxerStreams managed by this object.
119 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
121 // Sets |timestamp_offset_| if possible.
122 // Returns if the offset was set. Returns false if the offset could not be
123 // updated at this time.
124 bool SetTimestampOffset(TimeDelta timestamp_offset);
126 // Sets |sequence_mode_| to |sequence_mode| if possible.
127 // Returns true if the mode update was allowed. Returns false if the mode
128 // could not be updated at this time.
129 bool SetSequenceMode(bool sequence_mode);
131 TimeDelta timestamp_offset() const { return timestamp_offset_; }
133 void set_append_window_start(TimeDelta start) {
134 append_window_start_ = start;
136 void set_append_window_end(TimeDelta end) { append_window_end_ = end; }
138 // Returns the range of buffered data in this source, capped at |duration|.
139 // |ended| - Set to true if end of stream has been signalled and the special
140 // end of stream range logic needs to be executed.
141 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
143 // Returns the highest buffered duration across all streams managed
145 // Returns TimeDelta() if none of the streams contain buffered data.
146 TimeDelta GetMaxBufferedDuration() const;
148 // Helper methods that call methods with similar names on all the
149 // ChunkDemuxerStreams managed by this object.
150 void StartReturningData();
152 void Seek(TimeDelta seek_time);
153 void CompletePendingReadIfPossible();
154 void OnSetDuration(TimeDelta duration);
155 void MarkEndOfStream();
156 void UnmarkEndOfStream();
158 // Sets the memory limit on each stream. |memory_limit| is the
159 // maximum number of bytes each stream is allowed to hold in its buffer.
160 void SetMemoryLimitsForTesting(int memory_limit);
161 bool IsSeekWaitingForData() const;
164 // Called by the |stream_parser_| when a new initialization segment is
166 // Returns true on a successful call. Returns false if an error occurred while
167 // processing decoder configurations.
168 bool OnNewConfigs(bool allow_audio, bool allow_video,
169 const AudioDecoderConfig& audio_config,
170 const VideoDecoderConfig& video_config,
171 const StreamParser::TextTrackConfigMap& text_configs);
173 // Called by the |stream_parser_| at the beginning of a new media segment.
174 void OnNewMediaSegment();
176 // Called by the |stream_parser_| at the end of a media segment.
177 void OnEndOfMediaSegment();
179 // Called by the |stream_parser_| when new buffers have been parsed. It
180 // applies |timestamp_offset_| to all buffers in |audio_buffers|,
181 // |video_buffers| and |text_map| and then calls Append() with the modified
182 // buffers on |audio_|, |video_| and/or the text demuxer streams associated
183 // with the track numbers in |text_map|.
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 // Helper function for OnNewBuffers() when new text buffers have been parsed.
191 // It applies |timestamp_offset_| to all buffers in |buffers| and then appends
192 // the (modified) buffers to the demuxer stream associated with
193 // the track having |text_track_id|.
194 // Returns true on a successful call. Returns false if an error occurred while
195 // processing the buffers.
196 bool OnTextBuffers(StreamParser::TrackId text_track_id,
197 const StreamParser::BufferQueue& buffers);
199 // Helper function that appends |buffers| to |stream| and calls
200 // |increase_duration_cb_| to potentially update the duration.
201 // Returns true if the append was successful. Returns false if
202 // |stream| is NULL or something in |buffers| caused the append to fail.
203 bool AppendAndUpdateDuration(ChunkDemuxerStream* stream,
204 const StreamParser::BufferQueue& buffers);
206 // Helper function that adds |timestamp_offset_| to each buffer in |buffers|.
207 void AdjustBufferTimestamps(const StreamParser::BufferQueue& buffers);
209 // Filters out buffers that are outside of the append window
210 // [|append_window_start_|, |append_window_end_|).
211 // |needs_keyframe| is a pointer to the |xxx_need_keyframe_| flag
212 // associated with the |buffers|. Its state is read an updated as
213 // this method filters |buffers|.
214 // Buffers that are inside the append window are appended to the end
215 // of |filtered_buffers|.
216 void FilterWithAppendWindow(const StreamParser::BufferQueue& buffers,
217 bool* needs_keyframe,
218 StreamParser::BufferQueue* filtered_buffers);
220 CreateDemuxerStreamCB create_demuxer_stream_cb_;
221 IncreaseDurationCB increase_duration_cb_;
222 NewTextTrackCB new_text_track_cb_;
224 // The offset to apply to media segment timestamps.
225 TimeDelta timestamp_offset_;
227 // Tracks the mode by which appended media is processed. If true, then
228 // appended media is processed using "sequence" mode. Otherwise, appended
229 // media is processed using "segments" mode.
230 // TODO(wolenetz): Enable "sequence" mode logic. See http://crbug.com/249422
231 // and http://crbug.com/333437.
234 TimeDelta append_window_start_;
235 TimeDelta append_window_end_;
237 // Set to true if the next buffers appended within the append window
238 // represent the start of a new media segment. This flag being set
239 // triggers a call to |new_segment_cb_| when the new buffers are
240 // appended. The flag is set on actual media segment boundaries and
241 // when the "append window" filtering causes discontinuities in the
243 bool new_media_segment_;
245 // Keeps track of whether |timestamp_offset_| or |sequence_mode_| can be
246 // updated. These cannot be updated if a media segment is being parsed.
247 bool parsing_media_segment_;
249 // The object used to parse appended data.
250 scoped_ptr<StreamParser> stream_parser_;
252 ChunkDemuxerStream* audio_;
253 bool audio_needs_keyframe_;
255 ChunkDemuxerStream* video_;
256 bool video_needs_keyframe_;
258 typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
259 TextStreamMap text_stream_map_;
263 DISALLOW_COPY_AND_ASSIGN(SourceState);
266 class ChunkDemuxerStream : public DemuxerStream {
268 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
270 explicit ChunkDemuxerStream(Type type);
271 virtual ~ChunkDemuxerStream();
273 // ChunkDemuxerStream control methods.
274 void StartReturningData();
276 void CompletePendingReadIfPossible();
279 // SourceBufferStream manipulation methods.
280 void Seek(TimeDelta time);
281 bool IsSeekWaitingForData() const;
283 // Add buffers to this stream. Buffers are stored in SourceBufferStreams,
284 // which handle ordering and overlap resolution.
285 // Returns true if buffers were successfully added.
286 bool Append(const StreamParser::BufferQueue& buffers);
288 // Removes buffers between |start| and |end| according to the steps
289 // in the "Coded Frame Removal Algorithm" in the Media Source
291 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
293 // |duration| is the current duration of the presentation. It is
294 // required by the computation outlined in the spec.
295 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
297 // Signal to the stream that duration has changed to |duration|.
298 void OnSetDuration(TimeDelta duration);
300 // Returns the range of buffered data in this stream, capped at |duration|.
301 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration) const;
303 // Returns the duration of the buffered data.
304 // Returns TimeDelta() if the stream has no buffered data.
305 TimeDelta GetBufferedDuration() const;
307 // Signal to the stream that buffers handed in through subsequent calls to
308 // Append() belong to a media segment that starts at |start_timestamp|.
309 void OnNewMediaSegment(TimeDelta start_timestamp);
311 // Called when midstream config updates occur.
312 // Returns true if the new config is accepted.
313 // Returns false if the new config should trigger an error.
314 bool UpdateAudioConfig(const AudioDecoderConfig& config, const LogCB& log_cb);
315 bool UpdateVideoConfig(const VideoDecoderConfig& config, const LogCB& log_cb);
316 void UpdateTextConfig(const TextTrackConfig& config, const LogCB& log_cb);
318 void MarkEndOfStream();
319 void UnmarkEndOfStream();
321 // DemuxerStream methods.
322 virtual void Read(const ReadCB& read_cb) OVERRIDE;
323 virtual Type type() OVERRIDE;
324 virtual void EnableBitstreamConverter() OVERRIDE;
325 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
326 virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
328 // Returns the text track configuration. It is an error to call this method
329 // if type() != TEXT.
330 TextTrackConfig text_track_config();
332 // Sets the memory limit, in bytes, on the SourceBufferStream.
333 void set_memory_limit_for_testing(int memory_limit) {
334 stream_->set_memory_limit_for_testing(memory_limit);
340 RETURNING_DATA_FOR_READS,
341 RETURNING_ABORT_FOR_READS,
345 // Assigns |state_| to |state|
346 void ChangeState_Locked(State state);
348 void CompletePendingReadIfPossible_Locked();
350 // Specifies the type of the stream.
353 scoped_ptr<SourceBufferStream> stream_;
355 mutable base::Lock lock_;
359 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream);
362 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
364 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
365 const IncreaseDurationCB& increase_duration_cb)
366 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
367 increase_duration_cb_(increase_duration_cb),
368 sequence_mode_(false),
369 append_window_end_(kInfiniteDuration()),
370 new_media_segment_(false),
371 parsing_media_segment_(false),
372 stream_parser_(stream_parser.release()),
374 audio_needs_keyframe_(true),
376 video_needs_keyframe_(true),
378 DCHECK(!create_demuxer_stream_cb_.is_null());
379 DCHECK(!increase_duration_cb_.is_null());
382 SourceState::~SourceState() {
385 STLDeleteValues(&text_stream_map_);
388 void SourceState::Init(const StreamParser::InitCB& init_cb,
391 const StreamParser::NeedKeyCB& need_key_cb,
392 const NewTextTrackCB& new_text_track_cb) {
393 new_text_track_cb_ = new_text_track_cb;
395 stream_parser_->Init(init_cb,
396 base::Bind(&SourceState::OnNewConfigs,
397 base::Unretained(this),
400 base::Bind(&SourceState::OnNewBuffers,
401 base::Unretained(this)),
402 new_text_track_cb_.is_null(),
404 base::Bind(&SourceState::OnNewMediaSegment,
405 base::Unretained(this)),
406 base::Bind(&SourceState::OnEndOfMediaSegment,
407 base::Unretained(this)),
411 bool SourceState::SetTimestampOffset(TimeDelta timestamp_offset) {
412 if (parsing_media_segment_)
415 timestamp_offset_ = timestamp_offset;
419 bool SourceState::SetSequenceMode(bool sequence_mode) {
420 if (parsing_media_segment_)
423 sequence_mode_ = sequence_mode;
428 bool SourceState::Append(const uint8* data, size_t length) {
429 return stream_parser_->Parse(data, length);
432 void SourceState::Abort() {
433 stream_parser_->Flush();
434 audio_needs_keyframe_ = true;
435 video_needs_keyframe_ = true;
436 parsing_media_segment_ = false;
439 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
441 audio_->Remove(start, end, duration);
444 video_->Remove(start, end, duration);
446 for (TextStreamMap::iterator itr = text_stream_map_.begin();
447 itr != text_stream_map_.end(); ++itr) {
448 itr->second->Remove(start, end, duration);
452 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
454 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
455 // this code to only add ranges from active tracks.
456 RangesList ranges_list;
458 ranges_list.push_back(audio_->GetBufferedRanges(duration));
461 ranges_list.push_back(video_->GetBufferedRanges(duration));
463 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
464 itr != text_stream_map_.end(); ++itr) {
465 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
468 return ComputeIntersection(ranges_list, ended);
471 TimeDelta SourceState::GetMaxBufferedDuration() const {
472 TimeDelta max_duration;
475 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
478 max_duration = std::max(max_duration, video_->GetBufferedDuration());
480 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
481 itr != text_stream_map_.end(); ++itr) {
482 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
488 void SourceState::StartReturningData() {
490 audio_->StartReturningData();
493 video_->StartReturningData();
495 for (TextStreamMap::iterator itr = text_stream_map_.begin();
496 itr != text_stream_map_.end(); ++itr) {
497 itr->second->StartReturningData();
501 void SourceState::AbortReads() {
503 audio_->AbortReads();
506 video_->AbortReads();
508 for (TextStreamMap::iterator itr = text_stream_map_.begin();
509 itr != text_stream_map_.end(); ++itr) {
510 itr->second->AbortReads();
514 void SourceState::Seek(TimeDelta seek_time) {
516 audio_->Seek(seek_time);
519 video_->Seek(seek_time);
521 for (TextStreamMap::iterator itr = text_stream_map_.begin();
522 itr != text_stream_map_.end(); ++itr) {
523 itr->second->Seek(seek_time);
527 void SourceState::CompletePendingReadIfPossible() {
529 audio_->CompletePendingReadIfPossible();
532 video_->CompletePendingReadIfPossible();
534 for (TextStreamMap::iterator itr = text_stream_map_.begin();
535 itr != text_stream_map_.end(); ++itr) {
536 itr->second->CompletePendingReadIfPossible();
540 void SourceState::OnSetDuration(TimeDelta duration) {
542 audio_->OnSetDuration(duration);
545 video_->OnSetDuration(duration);
547 for (TextStreamMap::iterator itr = text_stream_map_.begin();
548 itr != text_stream_map_.end(); ++itr) {
549 itr->second->OnSetDuration(duration);
553 void SourceState::MarkEndOfStream() {
555 audio_->MarkEndOfStream();
558 video_->MarkEndOfStream();
560 for (TextStreamMap::iterator itr = text_stream_map_.begin();
561 itr != text_stream_map_.end(); ++itr) {
562 itr->second->MarkEndOfStream();
566 void SourceState::UnmarkEndOfStream() {
568 audio_->UnmarkEndOfStream();
571 video_->UnmarkEndOfStream();
573 for (TextStreamMap::iterator itr = text_stream_map_.begin();
574 itr != text_stream_map_.end(); ++itr) {
575 itr->second->UnmarkEndOfStream();
579 void SourceState::Shutdown() {
586 for (TextStreamMap::iterator itr = text_stream_map_.begin();
587 itr != text_stream_map_.end(); ++itr) {
588 itr->second->Shutdown();
592 void SourceState::SetMemoryLimitsForTesting(int memory_limit) {
594 audio_->set_memory_limit_for_testing(memory_limit);
597 video_->set_memory_limit_for_testing(memory_limit);
599 for (TextStreamMap::iterator itr = text_stream_map_.begin();
600 itr != text_stream_map_.end(); ++itr) {
601 itr->second->set_memory_limit_for_testing(memory_limit);
605 bool SourceState::IsSeekWaitingForData() const {
606 if (audio_ && audio_->IsSeekWaitingForData())
609 if (video_ && video_->IsSeekWaitingForData())
612 // NOTE: We are intentionally not checking the text tracks
613 // because text tracks are discontinuous and may not have data
614 // for the seek position. This is ok and playback should not be
615 // stalled because we don't have cues. If cues, with timestamps after
616 // the seek time, eventually arrive they will be delivered properly
617 // in response to ChunkDemuxerStream::Read() calls.
622 void SourceState::AdjustBufferTimestamps(
623 const StreamParser::BufferQueue& buffers) {
624 if (timestamp_offset_ == TimeDelta())
627 for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
628 itr != buffers.end(); ++itr) {
629 (*itr)->SetDecodeTimestamp(
630 (*itr)->GetDecodeTimestamp() + timestamp_offset_);
631 (*itr)->set_timestamp((*itr)->timestamp() + timestamp_offset_);
635 bool SourceState::OnNewConfigs(
636 bool allow_audio, bool allow_video,
637 const AudioDecoderConfig& audio_config,
638 const VideoDecoderConfig& video_config,
639 const StreamParser::TextTrackConfigMap& text_configs) {
640 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
641 << ", " << audio_config.IsValidConfig()
642 << ", " << video_config.IsValidConfig() << ")";
644 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
645 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
649 // Signal an error if we get configuration info for stream types that weren't
650 // specified in AddId() or more configs after a stream is initialized.
651 if (allow_audio != audio_config.IsValidConfig()) {
653 << "Initialization segment"
654 << (audio_config.IsValidConfig() ? " has" : " does not have")
655 << " an audio track, but the mimetype"
656 << (allow_audio ? " specifies" : " does not specify")
657 << " an audio codec.";
661 if (allow_video != video_config.IsValidConfig()) {
663 << "Initialization segment"
664 << (video_config.IsValidConfig() ? " has" : " does not have")
665 << " a video track, but the mimetype"
666 << (allow_video ? " specifies" : " does not specify")
667 << " a video codec.";
672 if (audio_config.IsValidConfig()) {
674 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
677 DVLOG(1) << "Failed to create an audio stream.";
682 success &= audio_->UpdateAudioConfig(audio_config, log_cb_);
685 if (video_config.IsValidConfig()) {
687 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
690 DVLOG(1) << "Failed to create a video stream.";
695 success &= video_->UpdateVideoConfig(video_config, log_cb_);
698 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
699 if (text_stream_map_.empty()) {
700 for (TextConfigItr itr = text_configs.begin();
701 itr != text_configs.end(); ++itr) {
702 ChunkDemuxerStream* const text_stream =
703 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
704 text_stream->UpdateTextConfig(itr->second, log_cb_);
705 text_stream_map_[itr->first] = text_stream;
706 new_text_track_cb_.Run(text_stream, itr->second);
709 const size_t text_count = text_stream_map_.size();
710 if (text_configs.size() != text_count) {
712 MEDIA_LOG(log_cb_) << "The number of text track configs changed.";
713 } else if (text_count == 1) {
714 TextConfigItr config_itr = text_configs.begin();
715 const TextTrackConfig& new_config = config_itr->second;
716 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
717 ChunkDemuxerStream* text_stream = stream_itr->second;
718 TextTrackConfig old_config = text_stream->text_track_config();
719 if (!new_config.Matches(old_config)) {
721 MEDIA_LOG(log_cb_) << "New text track config does not match old one.";
723 text_stream_map_.clear();
724 text_stream_map_[config_itr->first] = text_stream;
727 for (TextConfigItr config_itr = text_configs.begin();
728 config_itr != text_configs.end(); ++config_itr) {
729 TextStreamMap::iterator stream_itr =
730 text_stream_map_.find(config_itr->first);
731 if (stream_itr == text_stream_map_.end()) {
733 MEDIA_LOG(log_cb_) << "Unexpected text track configuration "
735 << config_itr->first;
739 const TextTrackConfig& new_config = config_itr->second;
740 ChunkDemuxerStream* stream = stream_itr->second;
741 TextTrackConfig old_config = stream->text_track_config();
742 if (!new_config.Matches(old_config)) {
744 MEDIA_LOG(log_cb_) << "New text track config for track ID "
746 << " does not match old one.";
753 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
757 void SourceState::OnNewMediaSegment() {
758 DVLOG(2) << "OnNewMediaSegment()";
759 parsing_media_segment_ = true;
760 new_media_segment_ = true;
763 void SourceState::OnEndOfMediaSegment() {
764 DVLOG(2) << "OnEndOfMediaSegment()";
765 parsing_media_segment_ = false;
766 new_media_segment_ = false;
769 bool SourceState::OnNewBuffers(
770 const StreamParser::BufferQueue& audio_buffers,
771 const StreamParser::BufferQueue& video_buffers,
772 const StreamParser::TextBufferQueueMap& text_map) {
773 DCHECK(!audio_buffers.empty() || !video_buffers.empty() ||
776 // TODO(wolenetz): DCHECK + return false if any of these buffers have UNKNOWN
777 // type() in upcoming coded frame processing compliant implementation. See
778 // http://crbug.com/249422.
780 AdjustBufferTimestamps(audio_buffers);
781 AdjustBufferTimestamps(video_buffers);
783 StreamParser::BufferQueue filtered_audio;
784 StreamParser::BufferQueue filtered_video;
786 FilterWithAppendWindow(audio_buffers, &audio_needs_keyframe_,
789 FilterWithAppendWindow(video_buffers, &video_needs_keyframe_,
792 if ((!filtered_audio.empty() || !filtered_video.empty()) &&
793 new_media_segment_) {
794 // Find the earliest timestamp in the filtered buffers and use that for the
795 // segment start timestamp.
796 TimeDelta segment_timestamp = kNoTimestamp();
798 if (!filtered_audio.empty())
799 segment_timestamp = filtered_audio.front()->GetDecodeTimestamp();
801 if (!filtered_video.empty() &&
802 (segment_timestamp == kNoTimestamp() ||
803 filtered_video.front()->GetDecodeTimestamp() < segment_timestamp)) {
804 segment_timestamp = filtered_video.front()->GetDecodeTimestamp();
807 new_media_segment_ = false;
810 audio_->OnNewMediaSegment(segment_timestamp);
813 video_->OnNewMediaSegment(segment_timestamp);
815 for (TextStreamMap::iterator itr = text_stream_map_.begin();
816 itr != text_stream_map_.end(); ++itr) {
817 itr->second->OnNewMediaSegment(segment_timestamp);
821 if (!filtered_audio.empty() &&
822 !AppendAndUpdateDuration(audio_, filtered_audio)) {
826 if (!filtered_video.empty() &&
827 !AppendAndUpdateDuration(video_, filtered_video)) {
831 if (text_map.empty())
834 // Process any buffers for each of the text tracks in the map.
835 bool all_text_buffers_empty = true;
836 for (StreamParser::TextBufferQueueMap::const_iterator itr = text_map.begin();
837 itr != text_map.end();
839 const StreamParser::BufferQueue text_buffers = itr->second;
840 if (!text_buffers.empty()) {
841 all_text_buffers_empty = false;
842 if (!OnTextBuffers(itr->first, text_buffers))
847 DCHECK(!all_text_buffers_empty);
851 bool SourceState::OnTextBuffers(
852 StreamParser::TrackId text_track_id,
853 const StreamParser::BufferQueue& buffers) {
854 DCHECK(!buffers.empty());
856 TextStreamMap::iterator itr = text_stream_map_.find(text_track_id);
857 if (itr == text_stream_map_.end())
860 AdjustBufferTimestamps(buffers);
862 StreamParser::BufferQueue filtered_buffers;
863 bool needs_keyframe = false;
864 FilterWithAppendWindow(buffers, &needs_keyframe, &filtered_buffers);
866 if (filtered_buffers.empty())
869 return AppendAndUpdateDuration(itr->second, filtered_buffers);
872 bool SourceState::AppendAndUpdateDuration(
873 ChunkDemuxerStream* stream,
874 const StreamParser::BufferQueue& buffers) {
875 DCHECK(!buffers.empty());
877 if (!stream || !stream->Append(buffers))
880 increase_duration_cb_.Run(buffers.back()->timestamp(), stream);
884 void SourceState::FilterWithAppendWindow(
885 const StreamParser::BufferQueue& buffers, bool* needs_keyframe,
886 StreamParser::BufferQueue* filtered_buffers) {
887 DCHECK(needs_keyframe);
888 DCHECK(filtered_buffers);
890 // This loop implements steps 1.9, 1.10, & 1.11 of the "Coded frame
891 // processing loop" in the Media Source Extensions spec.
892 // These steps filter out buffers that are not within the "append
893 // window" and handles resyncing on the next random access point
894 // (i.e., next keyframe) if a buffer gets dropped.
895 for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
896 itr != buffers.end(); ++itr) {
897 // Filter out buffers that are outside the append window. Anytime
898 // a buffer gets dropped we need to set |*needs_keyframe| to true
899 // because we can only resume decoding at keyframes.
900 TimeDelta presentation_timestamp = (*itr)->timestamp();
902 // TODO(acolwell): Change |frame_end_timestamp| value to
903 // |presentation_timestamp + (*itr)->duration()|, like the spec
904 // requires, once frame durations are actually present in all buffers.
905 TimeDelta frame_end_timestamp = presentation_timestamp;
906 if (presentation_timestamp < append_window_start_ ||
907 frame_end_timestamp > append_window_end_) {
908 DVLOG(1) << "Dropping buffer outside append window."
909 << " presentation_timestamp "
910 << presentation_timestamp.InSecondsF();
911 *needs_keyframe = true;
913 // This triggers a discontinuity so we need to treat the next frames
914 // appended within the append window as if they were the beginning of a
916 new_media_segment_ = true;
920 // If |*needs_keyframe| is true then filter out buffers until we
921 // encounter the next keyframe.
922 if (*needs_keyframe) {
923 if (!(*itr)->IsKeyframe()) {
924 DVLOG(1) << "Dropping non-keyframe. presentation_timestamp "
925 << presentation_timestamp.InSecondsF();
929 *needs_keyframe = false;
932 filtered_buffers->push_back(*itr);
936 ChunkDemuxerStream::ChunkDemuxerStream(Type type)
938 state_(UNINITIALIZED) {
941 void ChunkDemuxerStream::StartReturningData() {
942 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
943 base::AutoLock auto_lock(lock_);
944 DCHECK(read_cb_.is_null());
945 ChangeState_Locked(RETURNING_DATA_FOR_READS);
948 void ChunkDemuxerStream::AbortReads() {
949 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
950 base::AutoLock auto_lock(lock_);
951 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
952 if (!read_cb_.is_null())
953 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
956 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
957 base::AutoLock auto_lock(lock_);
958 if (read_cb_.is_null())
961 CompletePendingReadIfPossible_Locked();
964 void ChunkDemuxerStream::Shutdown() {
965 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
966 base::AutoLock auto_lock(lock_);
967 ChangeState_Locked(SHUTDOWN);
969 // Pass an end of stream buffer to the pending callback to signal that no more
970 // data will be sent.
971 if (!read_cb_.is_null()) {
972 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
973 StreamParserBuffer::CreateEOSBuffer());
977 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
978 base::AutoLock auto_lock(lock_);
980 // This method should not be called for text tracks. See the note in
981 // SourceState::IsSeekWaitingForData().
982 DCHECK_NE(type_, DemuxerStream::TEXT);
984 return stream_->IsSeekPending();
987 void ChunkDemuxerStream::Seek(TimeDelta time) {
988 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
989 base::AutoLock auto_lock(lock_);
990 DCHECK(read_cb_.is_null());
991 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
997 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
1001 base::AutoLock auto_lock(lock_);
1002 DCHECK_NE(state_, SHUTDOWN);
1003 if (!stream_->Append(buffers)) {
1004 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
1008 if (!read_cb_.is_null())
1009 CompletePendingReadIfPossible_Locked();
1014 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
1015 TimeDelta duration) {
1016 base::AutoLock auto_lock(lock_);
1017 stream_->Remove(start, end, duration);
1020 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
1021 base::AutoLock auto_lock(lock_);
1022 stream_->OnSetDuration(duration);
1025 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
1026 TimeDelta duration) const {
1027 base::AutoLock auto_lock(lock_);
1029 if (type_ == TEXT) {
1030 // Since text tracks are discontinuous and the lack of cues should not block
1031 // playback, report the buffered range for text tracks as [0, |duration|) so
1032 // that intesections with audio & video tracks are computed correctly when
1033 // no cues are present.
1034 Ranges<TimeDelta> text_range;
1035 text_range.Add(TimeDelta(), duration);
1039 Ranges<TimeDelta> range = stream_->GetBufferedTime();
1041 if (range.size() == 0u)
1044 // Clamp the end of the stream's buffered ranges to fit within the duration.
1045 // This can be done by intersecting the stream's range with the valid time
1047 Ranges<TimeDelta> valid_time_range;
1048 valid_time_range.Add(range.start(0), duration);
1049 return range.IntersectionWith(valid_time_range);
1052 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
1053 return stream_->GetBufferedDuration();
1056 void ChunkDemuxerStream::OnNewMediaSegment(TimeDelta start_timestamp) {
1057 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
1058 << start_timestamp.InSecondsF() << ")";
1059 base::AutoLock auto_lock(lock_);
1060 stream_->OnNewMediaSegment(start_timestamp);
1063 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig& config,
1064 const LogCB& log_cb) {
1065 DCHECK(config.IsValidConfig());
1066 DCHECK_EQ(type_, AUDIO);
1067 base::AutoLock auto_lock(lock_);
1069 DCHECK_EQ(state_, UNINITIALIZED);
1070 stream_.reset(new SourceBufferStream(config, log_cb));
1074 return stream_->UpdateAudioConfig(config);
1077 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig& config,
1078 const LogCB& log_cb) {
1079 DCHECK(config.IsValidConfig());
1080 DCHECK_EQ(type_, VIDEO);
1081 base::AutoLock auto_lock(lock_);
1084 DCHECK_EQ(state_, UNINITIALIZED);
1085 stream_.reset(new SourceBufferStream(config, log_cb));
1089 return stream_->UpdateVideoConfig(config);
1092 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig& config,
1093 const LogCB& log_cb) {
1094 DCHECK_EQ(type_, TEXT);
1095 base::AutoLock auto_lock(lock_);
1097 DCHECK_EQ(state_, UNINITIALIZED);
1098 stream_.reset(new SourceBufferStream(config, log_cb));
1101 void ChunkDemuxerStream::MarkEndOfStream() {
1102 base::AutoLock auto_lock(lock_);
1103 stream_->MarkEndOfStream();
1106 void ChunkDemuxerStream::UnmarkEndOfStream() {
1107 base::AutoLock auto_lock(lock_);
1108 stream_->UnmarkEndOfStream();
1111 // DemuxerStream methods.
1112 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
1113 base::AutoLock auto_lock(lock_);
1114 DCHECK_NE(state_, UNINITIALIZED);
1115 DCHECK(read_cb_.is_null());
1117 read_cb_ = BindToCurrentLoop(read_cb);
1118 CompletePendingReadIfPossible_Locked();
1121 DemuxerStream::Type ChunkDemuxerStream::type() { return type_; }
1123 void ChunkDemuxerStream::EnableBitstreamConverter() {}
1125 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
1126 CHECK_EQ(type_, AUDIO);
1127 base::AutoLock auto_lock(lock_);
1128 return stream_->GetCurrentAudioDecoderConfig();
1131 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
1132 CHECK_EQ(type_, VIDEO);
1133 base::AutoLock auto_lock(lock_);
1134 return stream_->GetCurrentVideoDecoderConfig();
1137 TextTrackConfig ChunkDemuxerStream::text_track_config() {
1138 CHECK_EQ(type_, TEXT);
1139 base::AutoLock auto_lock(lock_);
1140 return stream_->GetCurrentTextTrackConfig();
1143 void ChunkDemuxerStream::ChangeState_Locked(State state) {
1144 lock_.AssertAcquired();
1145 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1147 << " - " << state_ << " -> " << state;
1151 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1153 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1154 lock_.AssertAcquired();
1155 DCHECK(!read_cb_.is_null());
1157 DemuxerStream::Status status;
1158 scoped_refptr<StreamParserBuffer> buffer;
1164 case RETURNING_DATA_FOR_READS:
1165 switch (stream_->GetNextBuffer(&buffer)) {
1166 case SourceBufferStream::kSuccess:
1167 status = DemuxerStream::kOk;
1169 case SourceBufferStream::kNeedBuffer:
1170 // Return early without calling |read_cb_| since we don't have
1171 // any data to return yet.
1173 case SourceBufferStream::kEndOfStream:
1174 status = DemuxerStream::kOk;
1175 buffer = StreamParserBuffer::CreateEOSBuffer();
1177 case SourceBufferStream::kConfigChange:
1178 DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
1179 status = kConfigChanged;
1184 case RETURNING_ABORT_FOR_READS:
1185 // Null buffers should be returned in this state since we are waiting
1186 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1187 // because they are associated with the seek.
1188 status = DemuxerStream::kAborted;
1192 status = DemuxerStream::kOk;
1193 buffer = StreamParserBuffer::CreateEOSBuffer();
1197 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1200 ChunkDemuxer::ChunkDemuxer(const base::Closure& open_cb,
1201 const NeedKeyCB& need_key_cb,
1202 const LogCB& log_cb)
1203 : state_(WAITING_FOR_INIT),
1204 cancel_next_seek_(false),
1207 need_key_cb_(need_key_cb),
1208 enable_text_(false),
1210 duration_(kNoTimestamp()),
1211 user_specified_duration_(-1) {
1212 DCHECK(!open_cb_.is_null());
1213 DCHECK(!need_key_cb_.is_null());
1216 void ChunkDemuxer::Initialize(
1218 const PipelineStatusCB& cb,
1219 bool enable_text_tracks) {
1220 DVLOG(1) << "Init()";
1222 base::AutoLock auto_lock(lock_);
1224 init_cb_ = BindToCurrentLoop(cb);
1225 if (state_ == SHUTDOWN) {
1226 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1229 DCHECK_EQ(state_, WAITING_FOR_INIT);
1231 enable_text_ = enable_text_tracks;
1233 ChangeState_Locked(INITIALIZING);
1235 base::ResetAndReturn(&open_cb_).Run();
1238 void ChunkDemuxer::Stop(const base::Closure& callback) {
1239 DVLOG(1) << "Stop()";
1244 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1245 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1246 DCHECK(time >= TimeDelta());
1248 base::AutoLock auto_lock(lock_);
1249 DCHECK(seek_cb_.is_null());
1251 seek_cb_ = BindToCurrentLoop(cb);
1252 if (state_ != INITIALIZED && state_ != ENDED) {
1253 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1257 if (cancel_next_seek_) {
1258 cancel_next_seek_ = false;
1259 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1263 SeekAllSources(time);
1264 StartReturningData();
1266 if (IsSeekWaitingForData_Locked()) {
1267 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1271 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1274 void ChunkDemuxer::OnAudioRendererDisabled() {
1275 base::AutoLock auto_lock(lock_);
1277 disabled_audio_ = audio_.Pass();
1280 // Demuxer implementation.
1281 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1282 DCHECK_NE(type, DemuxerStream::TEXT);
1283 base::AutoLock auto_lock(lock_);
1284 if (type == DemuxerStream::VIDEO)
1285 return video_.get();
1287 if (type == DemuxerStream::AUDIO)
1288 return audio_.get();
1293 TimeDelta ChunkDemuxer::GetStartTime() const {
1297 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1298 DVLOG(1) << "StartWaitingForSeek()";
1299 base::AutoLock auto_lock(lock_);
1300 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1301 state_ == PARSE_ERROR) << state_;
1302 DCHECK(seek_cb_.is_null());
1304 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1307 AbortPendingReads();
1308 SeekAllSources(seek_time);
1310 // Cancel state set in CancelPendingSeek() since we want to
1311 // accept the next Seek().
1312 cancel_next_seek_ = false;
1315 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1316 base::AutoLock auto_lock(lock_);
1317 DCHECK_NE(state_, INITIALIZING);
1318 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1320 if (cancel_next_seek_)
1323 AbortPendingReads();
1324 SeekAllSources(seek_time);
1326 if (seek_cb_.is_null()) {
1327 cancel_next_seek_ = true;
1331 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1334 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1335 const std::string& type,
1336 std::vector<std::string>& codecs) {
1337 base::AutoLock auto_lock(lock_);
1339 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1340 return kReachedIdLimit;
1342 bool has_audio = false;
1343 bool has_video = false;
1344 scoped_ptr<media::StreamParser> stream_parser(
1345 StreamParserFactory::Create(type, codecs, log_cb_,
1346 &has_audio, &has_video));
1349 return ChunkDemuxer::kNotSupported;
1351 if ((has_audio && !source_id_audio_.empty()) ||
1352 (has_video && !source_id_video_.empty()))
1353 return kReachedIdLimit;
1356 source_id_audio_ = id;
1359 source_id_video_ = id;
1361 scoped_ptr<SourceState> source_state(
1362 new SourceState(stream_parser.Pass(), log_cb_,
1363 base::Bind(&ChunkDemuxer::CreateDemuxerStream,
1364 base::Unretained(this)),
1365 base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1366 base::Unretained(this))));
1368 SourceState::NewTextTrackCB new_text_track_cb;
1371 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1372 base::Unretained(this));
1376 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1382 source_state_map_[id] = source_state.release();
1386 void ChunkDemuxer::RemoveId(const std::string& id) {
1387 base::AutoLock auto_lock(lock_);
1388 CHECK(IsValidId(id));
1390 delete source_state_map_[id];
1391 source_state_map_.erase(id);
1393 if (source_id_audio_ == id)
1394 source_id_audio_.clear();
1396 if (source_id_video_ == id)
1397 source_id_video_.clear();
1400 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1401 base::AutoLock auto_lock(lock_);
1402 DCHECK(!id.empty());
1404 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1406 DCHECK(itr != source_state_map_.end());
1407 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1410 void ChunkDemuxer::AppendData(const std::string& id,
1413 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1415 DCHECK(!id.empty());
1417 Ranges<TimeDelta> ranges;
1420 base::AutoLock auto_lock(lock_);
1421 DCHECK_NE(state_, ENDED);
1423 // Capture if any of the SourceBuffers are waiting for data before we start
1425 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1434 DCHECK(IsValidId(id));
1435 if (!source_state_map_[id]->Append(data, length)) {
1436 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1442 DCHECK(IsValidId(id));
1443 if (!source_state_map_[id]->Append(data, length)) {
1444 ReportError_Locked(PIPELINE_ERROR_DECODE);
1450 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1453 case WAITING_FOR_INIT:
1456 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1460 // Check to see if data was appended at the pending seek point. This
1461 // indicates we have parsed enough data to complete the seek.
1462 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1463 !seek_cb_.is_null()) {
1464 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1467 ranges = GetBufferedRanges_Locked();
1470 for (size_t i = 0; i < ranges.size(); ++i)
1471 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1474 void ChunkDemuxer::Abort(const std::string& id) {
1475 DVLOG(1) << "Abort(" << id << ")";
1476 base::AutoLock auto_lock(lock_);
1477 DCHECK(!id.empty());
1478 CHECK(IsValidId(id));
1479 source_state_map_[id]->Abort();
1482 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1484 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1485 << ", " << end.InSecondsF() << ")";
1486 base::AutoLock auto_lock(lock_);
1488 DCHECK(!id.empty());
1489 CHECK(IsValidId(id));
1490 source_state_map_[id]->Remove(start, end, duration_);
1493 double ChunkDemuxer::GetDuration() {
1494 base::AutoLock auto_lock(lock_);
1495 return GetDuration_Locked();
1498 double ChunkDemuxer::GetDuration_Locked() {
1499 lock_.AssertAcquired();
1500 if (duration_ == kNoTimestamp())
1501 return std::numeric_limits<double>::quiet_NaN();
1503 // Return positive infinity if the resource is unbounded.
1504 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1505 if (duration_ == kInfiniteDuration())
1506 return std::numeric_limits<double>::infinity();
1508 if (user_specified_duration_ >= 0)
1509 return user_specified_duration_;
1511 return duration_.InSecondsF();
1514 void ChunkDemuxer::SetDuration(double duration) {
1515 base::AutoLock auto_lock(lock_);
1516 DVLOG(1) << "SetDuration(" << duration << ")";
1517 DCHECK_GE(duration, 0);
1519 if (duration == GetDuration_Locked())
1522 // Compute & bounds check the TimeDelta representation of duration.
1523 // This can be different if the value of |duration| doesn't fit the range or
1524 // precision of TimeDelta.
1525 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1526 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1527 double min_duration_in_seconds = min_duration.InSecondsF();
1528 double max_duration_in_seconds = max_duration.InSecondsF();
1530 TimeDelta duration_td;
1531 if (duration == std::numeric_limits<double>::infinity()) {
1532 duration_td = media::kInfiniteDuration();
1533 } else if (duration < min_duration_in_seconds) {
1534 duration_td = min_duration;
1535 } else if (duration > max_duration_in_seconds) {
1536 duration_td = max_duration;
1538 duration_td = TimeDelta::FromMicroseconds(
1539 duration * base::Time::kMicrosecondsPerSecond);
1542 DCHECK(duration_td > TimeDelta());
1544 user_specified_duration_ = duration;
1545 duration_ = duration_td;
1546 host_->SetDuration(duration_);
1548 for (SourceStateMap::iterator itr = source_state_map_.begin();
1549 itr != source_state_map_.end(); ++itr) {
1550 itr->second->OnSetDuration(duration_);
1554 bool ChunkDemuxer::SetTimestampOffset(const std::string& id, TimeDelta offset) {
1555 base::AutoLock auto_lock(lock_);
1556 DVLOG(1) << "SetTimestampOffset(" << id << ", " << offset.InSecondsF() << ")";
1557 CHECK(IsValidId(id));
1559 return source_state_map_[id]->SetTimestampOffset(offset);
1562 bool ChunkDemuxer::SetSequenceMode(const std::string& id,
1563 bool sequence_mode) {
1564 base::AutoLock auto_lock(lock_);
1565 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1566 CHECK(IsValidId(id));
1567 DCHECK_NE(state_, ENDED);
1569 return source_state_map_[id]->SetSequenceMode(sequence_mode);
1572 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1573 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1574 base::AutoLock auto_lock(lock_);
1575 DCHECK_NE(state_, WAITING_FOR_INIT);
1576 DCHECK_NE(state_, ENDED);
1578 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1581 if (state_ == INITIALIZING) {
1582 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1586 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1587 for (SourceStateMap::iterator itr = source_state_map_.begin();
1588 itr != source_state_map_.end(); ++itr) {
1589 itr->second->MarkEndOfStream();
1592 CompletePendingReadsIfPossible();
1594 // Give a chance to resume the pending seek process.
1595 if (status != PIPELINE_OK) {
1596 ReportError_Locked(status);
1600 ChangeState_Locked(ENDED);
1601 DecreaseDurationIfNecessary();
1603 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1604 !seek_cb_.is_null()) {
1605 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1609 void ChunkDemuxer::UnmarkEndOfStream() {
1610 DVLOG(1) << "UnmarkEndOfStream()";
1611 base::AutoLock auto_lock(lock_);
1612 DCHECK_EQ(state_, ENDED);
1614 ChangeState_Locked(INITIALIZED);
1616 for (SourceStateMap::iterator itr = source_state_map_.begin();
1617 itr != source_state_map_.end(); ++itr) {
1618 itr->second->UnmarkEndOfStream();
1622 void ChunkDemuxer::SetAppendWindowStart(const std::string& id,
1624 base::AutoLock auto_lock(lock_);
1625 DVLOG(1) << "SetAppendWindowStart(" << id << ", "
1626 << start.InSecondsF() << ")";
1627 CHECK(IsValidId(id));
1628 source_state_map_[id]->set_append_window_start(start);
1631 void ChunkDemuxer::SetAppendWindowEnd(const std::string& id, TimeDelta end) {
1632 base::AutoLock auto_lock(lock_);
1633 DVLOG(1) << "SetAppendWindowEnd(" << id << ", " << end.InSecondsF() << ")";
1634 CHECK(IsValidId(id));
1635 source_state_map_[id]->set_append_window_end(end);
1638 void ChunkDemuxer::Shutdown() {
1639 DVLOG(1) << "Shutdown()";
1640 base::AutoLock auto_lock(lock_);
1642 if (state_ == SHUTDOWN)
1645 ShutdownAllStreams();
1647 ChangeState_Locked(SHUTDOWN);
1649 if(!seek_cb_.is_null())
1650 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1653 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit) {
1654 for (SourceStateMap::iterator itr = source_state_map_.begin();
1655 itr != source_state_map_.end(); ++itr) {
1656 itr->second->SetMemoryLimitsForTesting(memory_limit);
1660 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1661 lock_.AssertAcquired();
1662 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1663 << state_ << " -> " << new_state;
1667 ChunkDemuxer::~ChunkDemuxer() {
1668 DCHECK_NE(state_, INITIALIZED);
1670 STLDeleteValues(&source_state_map_);
1673 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1674 DVLOG(1) << "ReportError_Locked(" << error << ")";
1675 lock_.AssertAcquired();
1676 DCHECK_NE(error, PIPELINE_OK);
1678 ChangeState_Locked(PARSE_ERROR);
1680 PipelineStatusCB cb;
1682 if (!init_cb_.is_null()) {
1683 std::swap(cb, init_cb_);
1685 if (!seek_cb_.is_null())
1686 std::swap(cb, seek_cb_);
1688 ShutdownAllStreams();
1691 if (!cb.is_null()) {
1696 base::AutoUnlock auto_unlock(lock_);
1697 host_->OnDemuxerError(error);
1700 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1701 lock_.AssertAcquired();
1702 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1703 itr != source_state_map_.end(); ++itr) {
1704 if (itr->second->IsSeekWaitingForData())
1711 void ChunkDemuxer::OnSourceInitDone(bool success, TimeDelta duration) {
1712 DVLOG(1) << "OnSourceInitDone(" << success << ", "
1713 << duration.InSecondsF() << ")";
1714 lock_.AssertAcquired();
1715 DCHECK_EQ(state_, INITIALIZING);
1716 if (!success || (!audio_ && !video_)) {
1717 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1721 if (duration != TimeDelta() && duration_ == kNoTimestamp())
1722 UpdateDuration(duration);
1724 // Wait until all streams have initialized.
1725 if ((!source_id_audio_.empty() && !audio_) ||
1726 (!source_id_video_.empty() && !video_))
1729 SeekAllSources(GetStartTime());
1730 StartReturningData();
1732 if (duration_ == kNoTimestamp())
1733 duration_ = kInfiniteDuration();
1735 // The demuxer is now initialized after the |start_timestamp_| was set.
1736 ChangeState_Locked(INITIALIZED);
1737 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1741 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1743 case DemuxerStream::AUDIO:
1746 audio_.reset(new ChunkDemuxerStream(DemuxerStream::AUDIO));
1747 return audio_.get();
1749 case DemuxerStream::VIDEO:
1752 video_.reset(new ChunkDemuxerStream(DemuxerStream::VIDEO));
1753 return video_.get();
1755 case DemuxerStream::TEXT: {
1756 return new ChunkDemuxerStream(DemuxerStream::TEXT);
1759 case DemuxerStream::UNKNOWN:
1760 case DemuxerStream::NUM_TYPES:
1768 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1769 const TextTrackConfig& config) {
1770 lock_.AssertAcquired();
1771 DCHECK_NE(state_, SHUTDOWN);
1772 host_->AddTextStream(text_stream, config);
1775 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1776 lock_.AssertAcquired();
1777 return source_state_map_.count(source_id) > 0u;
1780 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1781 DCHECK(duration_ != new_duration);
1782 user_specified_duration_ = -1;
1783 duration_ = new_duration;
1784 host_->SetDuration(new_duration);
1787 void ChunkDemuxer::IncreaseDurationIfNecessary(
1788 TimeDelta last_appended_buffer_timestamp,
1789 ChunkDemuxerStream* stream) {
1790 DCHECK(last_appended_buffer_timestamp != kNoTimestamp());
1791 if (last_appended_buffer_timestamp <= duration_)
1794 TimeDelta stream_duration = stream->GetBufferedDuration();
1795 DCHECK(stream_duration > TimeDelta());
1797 if (stream_duration > duration_)
1798 UpdateDuration(stream_duration);
1801 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1802 lock_.AssertAcquired();
1804 TimeDelta max_duration;
1806 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1807 itr != source_state_map_.end(); ++itr) {
1808 max_duration = std::max(max_duration,
1809 itr->second->GetMaxBufferedDuration());
1812 if (max_duration == TimeDelta())
1815 if (max_duration < duration_)
1816 UpdateDuration(max_duration);
1819 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1820 base::AutoLock auto_lock(lock_);
1821 return GetBufferedRanges_Locked();
1824 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1825 lock_.AssertAcquired();
1827 bool ended = state_ == ENDED;
1828 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1829 // we'll need to update this loop to only add ranges from active sources.
1830 RangesList ranges_list;
1831 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1832 itr != source_state_map_.end(); ++itr) {
1833 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1836 return ComputeIntersection(ranges_list, ended);
1839 void ChunkDemuxer::StartReturningData() {
1840 for (SourceStateMap::iterator itr = source_state_map_.begin();
1841 itr != source_state_map_.end(); ++itr) {
1842 itr->second->StartReturningData();
1846 void ChunkDemuxer::AbortPendingReads() {
1847 for (SourceStateMap::iterator itr = source_state_map_.begin();
1848 itr != source_state_map_.end(); ++itr) {
1849 itr->second->AbortReads();
1853 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1854 for (SourceStateMap::iterator itr = source_state_map_.begin();
1855 itr != source_state_map_.end(); ++itr) {
1856 itr->second->Seek(seek_time);
1860 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1861 for (SourceStateMap::iterator itr = source_state_map_.begin();
1862 itr != source_state_map_.end(); ++itr) {
1863 itr->second->CompletePendingReadIfPossible();
1867 void ChunkDemuxer::ShutdownAllStreams() {
1868 for (SourceStateMap::iterator itr = source_state_map_.begin();
1869 itr != source_state_map_.end(); ++itr) {
1870 itr->second->Shutdown();
1874 } // namespace media