Add explicit |forceOnlineSignin| to user pod status
[chromium-blink-merge.git] / media / filters / chunk_demuxer.cc
blob4c1de9413229ac1914fbed0ba7a79cb126e8754f
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/chunk_demuxer.h"
7 #include <algorithm>
8 #include <deque>
9 #include <limits>
10 #include <list>
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;
25 namespace media {
27 // List of time ranges for each SourceBuffer.
28 typedef std::list<Ranges<TimeDelta> > RangesList;
29 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
30 bool ended) {
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
42 // ranges.
43 TimeDelta highest_end_time;
44 for (RangesList::const_iterator itr = activeRanges.begin();
45 itr != activeRanges.end(); ++itr) {
46 if (!itr->size())
47 continue;
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
58 // following steps:
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),
69 highest_end_time);
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.
83 class SourceState {
84 public:
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);
101 ~SourceState();
103 void Init(const StreamParser::InitCB& init_cb,
104 bool allow_audio,
105 bool allow_video,
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.
112 bool Append(const uint8* data, size_t length);
114 // Aborts the current append sequence and resets the parser.
115 void Abort();
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
144 // by this object.
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();
151 void AbortReads();
152 void Seek(TimeDelta seek_time);
153 void CompletePendingReadIfPossible();
154 void OnSetDuration(TimeDelta duration);
155 void MarkEndOfStream();
156 void UnmarkEndOfStream();
157 void Shutdown();
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;
163 private:
164 // Called by the |stream_parser_| when a new initialization segment is
165 // encountered.
166 // Returns true on a successful call. Returns false if an error occured 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| and
181 // |video_buffers| and then calls Append() on |audio_| and/or
182 // |video_| with the modified buffers.
183 // Returns true on a successful call. Returns false if an error occured while
184 // processing the buffers.
185 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
186 const StreamParser::BufferQueue& video_buffers);
188 // Called by the |stream_parser_| when new text buffers have been parsed. It
189 // applies |timestamp_offset_| to all buffers in |buffers| and then appends
190 // the (modified) buffers to the demuxer stream associated with
191 // the track having |text_track_number|.
192 // Returns true on a successful call. Returns false if an error occured while
193 // processing the buffers.
194 bool OnTextBuffers(int text_track_number,
195 const StreamParser::BufferQueue& buffers);
197 // Helper function that appends |buffers| to |stream| and calls
198 // |increase_duration_cb_| to potentially update the duration.
199 // Returns true if the append was successful. Returns false if
200 // |stream| is NULL or something in |buffers| caused the append to fail.
201 bool AppendAndUpdateDuration(ChunkDemuxerStream* stream,
202 const StreamParser::BufferQueue& buffers);
204 // Helper function that adds |timestamp_offset_| to each buffer in |buffers|.
205 void AdjustBufferTimestamps(const StreamParser::BufferQueue& buffers);
207 // Filters out buffers that are outside of the append window
208 // [|append_window_start_|, |append_window_end_|).
209 // |needs_keyframe| is a pointer to the |xxx_need_keyframe_| flag
210 // associated with the |buffers|. Its state is read an updated as
211 // this method filters |buffers|.
212 // Buffers that are inside the append window are appended to the end
213 // of |filtered_buffers|.
214 void FilterWithAppendWindow(const StreamParser::BufferQueue& buffers,
215 bool* needs_keyframe,
216 StreamParser::BufferQueue* filtered_buffers);
218 CreateDemuxerStreamCB create_demuxer_stream_cb_;
219 IncreaseDurationCB increase_duration_cb_;
220 NewTextTrackCB new_text_track_cb_;
222 // The offset to apply to media segment timestamps.
223 TimeDelta timestamp_offset_;
225 // Tracks the mode by which appended media is processed. If true, then
226 // appended media is processed using "sequence" mode. Otherwise, appended
227 // media is processed using "segments" mode.
228 // TODO(wolenetz): Enable "sequence" mode logic. See http://crbug.com/249422
229 // and http://crbug.com/333437.
230 bool sequence_mode_;
232 TimeDelta append_window_start_;
233 TimeDelta append_window_end_;
235 // Set to true if the next buffers appended within the append window
236 // represent the start of a new media segment. This flag being set
237 // triggers a call to |new_segment_cb_| when the new buffers are
238 // appended. The flag is set on actual media segment boundaries and
239 // when the "append window" filtering causes discontinuities in the
240 // appended data.
241 bool new_media_segment_;
243 // Keeps track of whether |timestamp_offset_| or |sequence_mode_| can be
244 // updated. These cannot be updated if a media segment is being parsed.
245 bool parsing_media_segment_;
247 // The object used to parse appended data.
248 scoped_ptr<StreamParser> stream_parser_;
250 ChunkDemuxerStream* audio_;
251 bool audio_needs_keyframe_;
253 ChunkDemuxerStream* video_;
254 bool video_needs_keyframe_;
256 typedef std::map<int, ChunkDemuxerStream*> TextStreamMap;
257 TextStreamMap text_stream_map_;
259 LogCB log_cb_;
261 DISALLOW_COPY_AND_ASSIGN(SourceState);
264 class ChunkDemuxerStream : public DemuxerStream {
265 public:
266 typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
268 explicit ChunkDemuxerStream(Type type);
269 virtual ~ChunkDemuxerStream();
271 // ChunkDemuxerStream control methods.
272 void StartReturningData();
273 void AbortReads();
274 void CompletePendingReadIfPossible();
275 void Shutdown();
277 // SourceBufferStream manipulation methods.
278 void Seek(TimeDelta time);
279 bool IsSeekWaitingForData() const;
281 // Add buffers to this stream. Buffers are stored in SourceBufferStreams,
282 // which handle ordering and overlap resolution.
283 // Returns true if buffers were successfully added.
284 bool Append(const StreamParser::BufferQueue& buffers);
286 // Removes buffers between |start| and |end| according to the steps
287 // in the "Coded Frame Removal Algorithm" in the Media Source
288 // Extensions Spec.
289 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
291 // |duration| is the current duration of the presentation. It is
292 // required by the computation outlined in the spec.
293 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
295 // Signal to the stream that duration has changed to |duration|.
296 void OnSetDuration(TimeDelta duration);
298 // Returns the range of buffered data in this stream, capped at |duration|.
299 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration) const;
301 // Returns the duration of the buffered data.
302 // Returns TimeDelta() if the stream has no buffered data.
303 TimeDelta GetBufferedDuration() const;
305 // Signal to the stream that buffers handed in through subsequent calls to
306 // Append() belong to a media segment that starts at |start_timestamp|.
307 void OnNewMediaSegment(TimeDelta start_timestamp);
309 // Called when midstream config updates occur.
310 // Returns true if the new config is accepted.
311 // Returns false if the new config should trigger an error.
312 bool UpdateAudioConfig(const AudioDecoderConfig& config, const LogCB& log_cb);
313 bool UpdateVideoConfig(const VideoDecoderConfig& config, const LogCB& log_cb);
314 void UpdateTextConfig(const TextTrackConfig& config, const LogCB& log_cb);
316 void MarkEndOfStream();
317 void UnmarkEndOfStream();
319 // DemuxerStream methods.
320 virtual void Read(const ReadCB& read_cb) OVERRIDE;
321 virtual Type type() OVERRIDE;
322 virtual void EnableBitstreamConverter() OVERRIDE;
323 virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
324 virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
326 // Returns the text track configuration. It is an error to call this method
327 // if type() != TEXT.
328 TextTrackConfig text_track_config();
330 // Sets the memory limit, in bytes, on the SourceBufferStream.
331 void set_memory_limit_for_testing(int memory_limit) {
332 stream_->set_memory_limit_for_testing(memory_limit);
335 private:
336 enum State {
337 UNINITIALIZED,
338 RETURNING_DATA_FOR_READS,
339 RETURNING_ABORT_FOR_READS,
340 SHUTDOWN,
343 // Assigns |state_| to |state|
344 void ChangeState_Locked(State state);
346 void CompletePendingReadIfPossible_Locked();
348 // Specifies the type of the stream.
349 Type type_;
351 scoped_ptr<SourceBufferStream> stream_;
353 mutable base::Lock lock_;
354 State state_;
355 ReadCB read_cb_;
357 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream);
360 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
361 const LogCB& log_cb,
362 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
363 const IncreaseDurationCB& increase_duration_cb)
364 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
365 increase_duration_cb_(increase_duration_cb),
366 sequence_mode_(false),
367 append_window_end_(kInfiniteDuration()),
368 new_media_segment_(false),
369 parsing_media_segment_(false),
370 stream_parser_(stream_parser.release()),
371 audio_(NULL),
372 audio_needs_keyframe_(true),
373 video_(NULL),
374 video_needs_keyframe_(true),
375 log_cb_(log_cb) {
376 DCHECK(!create_demuxer_stream_cb_.is_null());
377 DCHECK(!increase_duration_cb_.is_null());
380 SourceState::~SourceState() {
381 Shutdown();
383 STLDeleteValues(&text_stream_map_);
386 void SourceState::Init(const StreamParser::InitCB& init_cb,
387 bool allow_audio,
388 bool allow_video,
389 const StreamParser::NeedKeyCB& need_key_cb,
390 const NewTextTrackCB& new_text_track_cb) {
391 new_text_track_cb_ = new_text_track_cb;
393 StreamParser::NewTextBuffersCB new_text_buffers_cb;
395 if (!new_text_track_cb_.is_null()) {
396 new_text_buffers_cb = base::Bind(&SourceState::OnTextBuffers,
397 base::Unretained(this));
400 stream_parser_->Init(init_cb,
401 base::Bind(&SourceState::OnNewConfigs,
402 base::Unretained(this),
403 allow_audio,
404 allow_video),
405 base::Bind(&SourceState::OnNewBuffers,
406 base::Unretained(this)),
407 new_text_buffers_cb,
408 need_key_cb,
409 base::Bind(&SourceState::OnNewMediaSegment,
410 base::Unretained(this)),
411 base::Bind(&SourceState::OnEndOfMediaSegment,
412 base::Unretained(this)),
413 log_cb_);
416 bool SourceState::SetTimestampOffset(TimeDelta timestamp_offset) {
417 if (parsing_media_segment_)
418 return false;
420 timestamp_offset_ = timestamp_offset;
421 return true;
424 bool SourceState::SetSequenceMode(bool sequence_mode) {
425 if (parsing_media_segment_)
426 return false;
428 sequence_mode_ = sequence_mode;
429 return true;
433 bool SourceState::Append(const uint8* data, size_t length) {
434 return stream_parser_->Parse(data, length);
437 void SourceState::Abort() {
438 stream_parser_->Flush();
439 audio_needs_keyframe_ = true;
440 video_needs_keyframe_ = true;
441 parsing_media_segment_ = false;
444 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
445 if (audio_)
446 audio_->Remove(start, end, duration);
448 if (video_)
449 video_->Remove(start, end, duration);
451 for (TextStreamMap::iterator itr = text_stream_map_.begin();
452 itr != text_stream_map_.end(); ++itr) {
453 itr->second->Remove(start, end, duration);
457 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
458 bool ended) const {
459 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
460 // this code to only add ranges from active tracks.
461 RangesList ranges_list;
462 if (audio_)
463 ranges_list.push_back(audio_->GetBufferedRanges(duration));
465 if (video_)
466 ranges_list.push_back(video_->GetBufferedRanges(duration));
468 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
469 itr != text_stream_map_.end(); ++itr) {
470 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
473 return ComputeIntersection(ranges_list, ended);
476 TimeDelta SourceState::GetMaxBufferedDuration() const {
477 TimeDelta max_duration;
479 if (audio_)
480 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
482 if (video_)
483 max_duration = std::max(max_duration, video_->GetBufferedDuration());
485 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
486 itr != text_stream_map_.end(); ++itr) {
487 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
490 return max_duration;
493 void SourceState::StartReturningData() {
494 if (audio_)
495 audio_->StartReturningData();
497 if (video_)
498 video_->StartReturningData();
500 for (TextStreamMap::iterator itr = text_stream_map_.begin();
501 itr != text_stream_map_.end(); ++itr) {
502 itr->second->StartReturningData();
506 void SourceState::AbortReads() {
507 if (audio_)
508 audio_->AbortReads();
510 if (video_)
511 video_->AbortReads();
513 for (TextStreamMap::iterator itr = text_stream_map_.begin();
514 itr != text_stream_map_.end(); ++itr) {
515 itr->second->AbortReads();
519 void SourceState::Seek(TimeDelta seek_time) {
520 if (audio_)
521 audio_->Seek(seek_time);
523 if (video_)
524 video_->Seek(seek_time);
526 for (TextStreamMap::iterator itr = text_stream_map_.begin();
527 itr != text_stream_map_.end(); ++itr) {
528 itr->second->Seek(seek_time);
532 void SourceState::CompletePendingReadIfPossible() {
533 if (audio_)
534 audio_->CompletePendingReadIfPossible();
536 if (video_)
537 video_->CompletePendingReadIfPossible();
539 for (TextStreamMap::iterator itr = text_stream_map_.begin();
540 itr != text_stream_map_.end(); ++itr) {
541 itr->second->CompletePendingReadIfPossible();
545 void SourceState::OnSetDuration(TimeDelta duration) {
546 if (audio_)
547 audio_->OnSetDuration(duration);
549 if (video_)
550 video_->OnSetDuration(duration);
552 for (TextStreamMap::iterator itr = text_stream_map_.begin();
553 itr != text_stream_map_.end(); ++itr) {
554 itr->second->OnSetDuration(duration);
558 void SourceState::MarkEndOfStream() {
559 if (audio_)
560 audio_->MarkEndOfStream();
562 if (video_)
563 video_->MarkEndOfStream();
565 for (TextStreamMap::iterator itr = text_stream_map_.begin();
566 itr != text_stream_map_.end(); ++itr) {
567 itr->second->MarkEndOfStream();
571 void SourceState::UnmarkEndOfStream() {
572 if (audio_)
573 audio_->UnmarkEndOfStream();
575 if (video_)
576 video_->UnmarkEndOfStream();
578 for (TextStreamMap::iterator itr = text_stream_map_.begin();
579 itr != text_stream_map_.end(); ++itr) {
580 itr->second->UnmarkEndOfStream();
584 void SourceState::Shutdown() {
585 if (audio_)
586 audio_->Shutdown();
588 if (video_)
589 video_->Shutdown();
591 for (TextStreamMap::iterator itr = text_stream_map_.begin();
592 itr != text_stream_map_.end(); ++itr) {
593 itr->second->Shutdown();
597 void SourceState::SetMemoryLimitsForTesting(int memory_limit) {
598 if (audio_)
599 audio_->set_memory_limit_for_testing(memory_limit);
601 if (video_)
602 video_->set_memory_limit_for_testing(memory_limit);
604 for (TextStreamMap::iterator itr = text_stream_map_.begin();
605 itr != text_stream_map_.end(); ++itr) {
606 itr->second->set_memory_limit_for_testing(memory_limit);
610 bool SourceState::IsSeekWaitingForData() const {
611 if (audio_ && audio_->IsSeekWaitingForData())
612 return true;
614 if (video_ && video_->IsSeekWaitingForData())
615 return true;
617 // NOTE: We are intentionally not checking the text tracks
618 // because text tracks are discontinuous and may not have data
619 // for the seek position. This is ok and playback should not be
620 // stalled because we don't have cues. If cues, with timestamps after
621 // the seek time, eventually arrive they will be delivered properly
622 // in response to ChunkDemuxerStream::Read() calls.
624 return false;
627 void SourceState::AdjustBufferTimestamps(
628 const StreamParser::BufferQueue& buffers) {
629 if (timestamp_offset_ == TimeDelta())
630 return;
632 for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
633 itr != buffers.end(); ++itr) {
634 (*itr)->SetDecodeTimestamp(
635 (*itr)->GetDecodeTimestamp() + timestamp_offset_);
636 (*itr)->set_timestamp((*itr)->timestamp() + timestamp_offset_);
640 bool SourceState::OnNewConfigs(
641 bool allow_audio, bool allow_video,
642 const AudioDecoderConfig& audio_config,
643 const VideoDecoderConfig& video_config,
644 const StreamParser::TextTrackConfigMap& text_configs) {
645 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
646 << ", " << audio_config.IsValidConfig()
647 << ", " << video_config.IsValidConfig() << ")";
649 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
650 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
651 return false;
654 // Signal an error if we get configuration info for stream types that weren't
655 // specified in AddId() or more configs after a stream is initialized.
656 if (allow_audio != audio_config.IsValidConfig()) {
657 MEDIA_LOG(log_cb_)
658 << "Initialization segment"
659 << (audio_config.IsValidConfig() ? " has" : " does not have")
660 << " an audio track, but the mimetype"
661 << (allow_audio ? " specifies" : " does not specify")
662 << " an audio codec.";
663 return false;
666 if (allow_video != video_config.IsValidConfig()) {
667 MEDIA_LOG(log_cb_)
668 << "Initialization segment"
669 << (video_config.IsValidConfig() ? " has" : " does not have")
670 << " a video track, but the mimetype"
671 << (allow_video ? " specifies" : " does not specify")
672 << " a video codec.";
673 return false;
676 bool success = true;
677 if (audio_config.IsValidConfig()) {
678 if (!audio_) {
679 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
681 if (!audio_) {
682 DVLOG(1) << "Failed to create an audio stream.";
683 return false;
687 success &= audio_->UpdateAudioConfig(audio_config, log_cb_);
690 if (video_config.IsValidConfig()) {
691 if (!video_) {
692 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
694 if (!video_) {
695 DVLOG(1) << "Failed to create a video stream.";
696 return false;
700 success &= video_->UpdateVideoConfig(video_config, log_cb_);
703 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
704 if (text_stream_map_.empty()) {
705 for (TextConfigItr itr = text_configs.begin();
706 itr != text_configs.end(); ++itr) {
707 ChunkDemuxerStream* const text_stream =
708 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
709 text_stream->UpdateTextConfig(itr->second, log_cb_);
710 text_stream_map_[itr->first] = text_stream;
711 new_text_track_cb_.Run(text_stream, itr->second);
713 } else {
714 const size_t text_count = text_stream_map_.size();
715 if (text_configs.size() != text_count) {
716 success &= false;
717 MEDIA_LOG(log_cb_) << "The number of text track configs changed.";
718 } else if (text_count == 1) {
719 TextConfigItr config_itr = text_configs.begin();
720 const TextTrackConfig& new_config = config_itr->second;
721 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
722 ChunkDemuxerStream* text_stream = stream_itr->second;
723 TextTrackConfig old_config = text_stream->text_track_config();
724 if (!new_config.Matches(old_config)) {
725 success &= false;
726 MEDIA_LOG(log_cb_) << "New text track config does not match old one.";
727 } else {
728 text_stream_map_.clear();
729 text_stream_map_[config_itr->first] = text_stream;
731 } else {
732 for (TextConfigItr config_itr = text_configs.begin();
733 config_itr != text_configs.end(); ++config_itr) {
734 TextStreamMap::iterator stream_itr =
735 text_stream_map_.find(config_itr->first);
736 if (stream_itr == text_stream_map_.end()) {
737 success &= false;
738 MEDIA_LOG(log_cb_) << "Unexpected text track configuration "
739 "for track ID "
740 << config_itr->first;
741 break;
744 const TextTrackConfig& new_config = config_itr->second;
745 ChunkDemuxerStream* stream = stream_itr->second;
746 TextTrackConfig old_config = stream->text_track_config();
747 if (!new_config.Matches(old_config)) {
748 success &= false;
749 MEDIA_LOG(log_cb_) << "New text track config for track ID "
750 << config_itr->first
751 << " does not match old one.";
752 break;
758 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
759 return success;
762 void SourceState::OnNewMediaSegment() {
763 DVLOG(2) << "OnNewMediaSegment()";
764 parsing_media_segment_ = true;
765 new_media_segment_ = true;
768 void SourceState::OnEndOfMediaSegment() {
769 DVLOG(2) << "OnEndOfMediaSegment()";
770 parsing_media_segment_ = false;
771 new_media_segment_ = false;
774 bool SourceState::OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
775 const StreamParser::BufferQueue& video_buffers) {
776 DCHECK(!audio_buffers.empty() || !video_buffers.empty());
777 AdjustBufferTimestamps(audio_buffers);
778 AdjustBufferTimestamps(video_buffers);
780 StreamParser::BufferQueue filtered_audio;
781 StreamParser::BufferQueue filtered_video;
783 FilterWithAppendWindow(audio_buffers, &audio_needs_keyframe_,
784 &filtered_audio);
786 FilterWithAppendWindow(video_buffers, &video_needs_keyframe_,
787 &filtered_video);
789 if (filtered_audio.empty() && filtered_video.empty())
790 return true;
792 if (new_media_segment_) {
793 // Find the earliest timestamp in the filtered buffers and use that for the
794 // segment start timestamp.
795 TimeDelta segment_timestamp = kNoTimestamp();
797 if (!filtered_audio.empty())
798 segment_timestamp = filtered_audio.front()->GetDecodeTimestamp();
800 if (!filtered_video.empty() &&
801 (segment_timestamp == kNoTimestamp() ||
802 filtered_video.front()->GetDecodeTimestamp() < segment_timestamp)) {
803 segment_timestamp = filtered_video.front()->GetDecodeTimestamp();
806 new_media_segment_ = false;
808 if (audio_)
809 audio_->OnNewMediaSegment(segment_timestamp);
811 if (video_)
812 video_->OnNewMediaSegment(segment_timestamp);
814 for (TextStreamMap::iterator itr = text_stream_map_.begin();
815 itr != text_stream_map_.end(); ++itr) {
816 itr->second->OnNewMediaSegment(segment_timestamp);
820 if (!filtered_audio.empty() &&
821 !AppendAndUpdateDuration(audio_, filtered_audio)) {
822 return false;
825 if (!filtered_video.empty() &&
826 !AppendAndUpdateDuration(video_, filtered_video)) {
827 return false;
830 return true;
833 bool SourceState::OnTextBuffers(
834 int text_track_number,
835 const StreamParser::BufferQueue& buffers) {
836 DCHECK(!buffers.empty());
838 TextStreamMap::iterator itr = text_stream_map_.find(text_track_number);
839 if (itr == text_stream_map_.end())
840 return false;
842 AdjustBufferTimestamps(buffers);
844 StreamParser::BufferQueue filtered_buffers;
845 bool needs_keyframe = false;
846 FilterWithAppendWindow(buffers, &needs_keyframe, &filtered_buffers);
848 if (filtered_buffers.empty())
849 return true;
851 return AppendAndUpdateDuration(itr->second, filtered_buffers);
854 bool SourceState::AppendAndUpdateDuration(
855 ChunkDemuxerStream* stream,
856 const StreamParser::BufferQueue& buffers) {
857 DCHECK(!buffers.empty());
859 if (!stream || !stream->Append(buffers))
860 return false;
862 increase_duration_cb_.Run(buffers.back()->timestamp(), stream);
863 return true;
866 void SourceState::FilterWithAppendWindow(
867 const StreamParser::BufferQueue& buffers, bool* needs_keyframe,
868 StreamParser::BufferQueue* filtered_buffers) {
869 DCHECK(needs_keyframe);
870 DCHECK(filtered_buffers);
872 // This loop implements steps 1.9, 1.10, & 1.11 of the "Coded frame
873 // processing loop" in the Media Source Extensions spec.
874 // These steps filter out buffers that are not within the "append
875 // window" and handles resyncing on the next random access point
876 // (i.e., next keyframe) if a buffer gets dropped.
877 for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
878 itr != buffers.end(); ++itr) {
879 // Filter out buffers that are outside the append window. Anytime
880 // a buffer gets dropped we need to set |*needs_keyframe| to true
881 // because we can only resume decoding at keyframes.
882 TimeDelta presentation_timestamp = (*itr)->timestamp();
884 // TODO(acolwell): Change |frame_end_timestamp| value to
885 // |presentation_timestamp + (*itr)->duration()|, like the spec
886 // requires, once frame durations are actually present in all buffers.
887 TimeDelta frame_end_timestamp = presentation_timestamp;
888 if (presentation_timestamp < append_window_start_ ||
889 frame_end_timestamp > append_window_end_) {
890 DVLOG(1) << "Dropping buffer outside append window."
891 << " presentation_timestamp "
892 << presentation_timestamp.InSecondsF();
893 *needs_keyframe = true;
895 // This triggers a discontinuity so we need to treat the next frames
896 // appended within the append window as if they were the beginning of a
897 // new segment.
898 new_media_segment_ = true;
899 continue;
902 // If |*needs_keyframe| is true then filter out buffers until we
903 // encounter the next keyframe.
904 if (*needs_keyframe) {
905 if (!(*itr)->IsKeyframe()) {
906 DVLOG(1) << "Dropping non-keyframe. presentation_timestamp "
907 << presentation_timestamp.InSecondsF();
908 continue;
911 *needs_keyframe = false;
914 filtered_buffers->push_back(*itr);
918 ChunkDemuxerStream::ChunkDemuxerStream(Type type)
919 : type_(type),
920 state_(UNINITIALIZED) {
923 void ChunkDemuxerStream::StartReturningData() {
924 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
925 base::AutoLock auto_lock(lock_);
926 DCHECK(read_cb_.is_null());
927 ChangeState_Locked(RETURNING_DATA_FOR_READS);
930 void ChunkDemuxerStream::AbortReads() {
931 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
932 base::AutoLock auto_lock(lock_);
933 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
934 if (!read_cb_.is_null())
935 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
938 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
939 base::AutoLock auto_lock(lock_);
940 if (read_cb_.is_null())
941 return;
943 CompletePendingReadIfPossible_Locked();
946 void ChunkDemuxerStream::Shutdown() {
947 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
948 base::AutoLock auto_lock(lock_);
949 ChangeState_Locked(SHUTDOWN);
951 // Pass an end of stream buffer to the pending callback to signal that no more
952 // data will be sent.
953 if (!read_cb_.is_null()) {
954 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
955 StreamParserBuffer::CreateEOSBuffer());
959 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
960 base::AutoLock auto_lock(lock_);
962 // This method should not be called for text tracks. See the note in
963 // SourceState::IsSeekWaitingForData().
964 DCHECK_NE(type_, DemuxerStream::TEXT);
966 return stream_->IsSeekPending();
969 void ChunkDemuxerStream::Seek(TimeDelta time) {
970 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
971 base::AutoLock auto_lock(lock_);
972 DCHECK(read_cb_.is_null());
973 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
974 << state_;
976 stream_->Seek(time);
979 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
980 if (buffers.empty())
981 return false;
983 base::AutoLock auto_lock(lock_);
984 DCHECK_NE(state_, SHUTDOWN);
985 if (!stream_->Append(buffers)) {
986 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
987 return false;
990 if (!read_cb_.is_null())
991 CompletePendingReadIfPossible_Locked();
993 return true;
996 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
997 TimeDelta duration) {
998 base::AutoLock auto_lock(lock_);
999 stream_->Remove(start, end, duration);
1002 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
1003 base::AutoLock auto_lock(lock_);
1004 stream_->OnSetDuration(duration);
1007 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
1008 TimeDelta duration) const {
1009 base::AutoLock auto_lock(lock_);
1011 if (type_ == TEXT) {
1012 // Since text tracks are discontinuous and the lack of cues should not block
1013 // playback, report the buffered range for text tracks as [0, |duration|) so
1014 // that intesections with audio & video tracks are computed correctly when
1015 // no cues are present.
1016 Ranges<TimeDelta> text_range;
1017 text_range.Add(TimeDelta(), duration);
1018 return text_range;
1021 Ranges<TimeDelta> range = stream_->GetBufferedTime();
1023 if (range.size() == 0u)
1024 return range;
1026 // Clamp the end of the stream's buffered ranges to fit within the duration.
1027 // This can be done by intersecting the stream's range with the valid time
1028 // range.
1029 Ranges<TimeDelta> valid_time_range;
1030 valid_time_range.Add(range.start(0), duration);
1031 return range.IntersectionWith(valid_time_range);
1034 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
1035 return stream_->GetBufferedDuration();
1038 void ChunkDemuxerStream::OnNewMediaSegment(TimeDelta start_timestamp) {
1039 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
1040 << start_timestamp.InSecondsF() << ")";
1041 base::AutoLock auto_lock(lock_);
1042 stream_->OnNewMediaSegment(start_timestamp);
1045 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig& config,
1046 const LogCB& log_cb) {
1047 DCHECK(config.IsValidConfig());
1048 DCHECK_EQ(type_, AUDIO);
1049 base::AutoLock auto_lock(lock_);
1050 if (!stream_) {
1051 DCHECK_EQ(state_, UNINITIALIZED);
1052 stream_.reset(new SourceBufferStream(config, log_cb));
1053 return true;
1056 return stream_->UpdateAudioConfig(config);
1059 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig& config,
1060 const LogCB& log_cb) {
1061 DCHECK(config.IsValidConfig());
1062 DCHECK_EQ(type_, VIDEO);
1063 base::AutoLock auto_lock(lock_);
1065 if (!stream_) {
1066 DCHECK_EQ(state_, UNINITIALIZED);
1067 stream_.reset(new SourceBufferStream(config, log_cb));
1068 return true;
1071 return stream_->UpdateVideoConfig(config);
1074 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig& config,
1075 const LogCB& log_cb) {
1076 DCHECK_EQ(type_, TEXT);
1077 base::AutoLock auto_lock(lock_);
1078 DCHECK(!stream_);
1079 DCHECK_EQ(state_, UNINITIALIZED);
1080 stream_.reset(new SourceBufferStream(config, log_cb));
1083 void ChunkDemuxerStream::MarkEndOfStream() {
1084 base::AutoLock auto_lock(lock_);
1085 stream_->MarkEndOfStream();
1088 void ChunkDemuxerStream::UnmarkEndOfStream() {
1089 base::AutoLock auto_lock(lock_);
1090 stream_->UnmarkEndOfStream();
1093 // DemuxerStream methods.
1094 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
1095 base::AutoLock auto_lock(lock_);
1096 DCHECK_NE(state_, UNINITIALIZED);
1097 DCHECK(read_cb_.is_null());
1099 read_cb_ = BindToCurrentLoop(read_cb);
1100 CompletePendingReadIfPossible_Locked();
1103 DemuxerStream::Type ChunkDemuxerStream::type() { return type_; }
1105 void ChunkDemuxerStream::EnableBitstreamConverter() {}
1107 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
1108 CHECK_EQ(type_, AUDIO);
1109 base::AutoLock auto_lock(lock_);
1110 return stream_->GetCurrentAudioDecoderConfig();
1113 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
1114 CHECK_EQ(type_, VIDEO);
1115 base::AutoLock auto_lock(lock_);
1116 return stream_->GetCurrentVideoDecoderConfig();
1119 TextTrackConfig ChunkDemuxerStream::text_track_config() {
1120 CHECK_EQ(type_, TEXT);
1121 base::AutoLock auto_lock(lock_);
1122 return stream_->GetCurrentTextTrackConfig();
1125 void ChunkDemuxerStream::ChangeState_Locked(State state) {
1126 lock_.AssertAcquired();
1127 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1128 << "type " << type_
1129 << " - " << state_ << " -> " << state;
1130 state_ = state;
1133 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1135 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1136 lock_.AssertAcquired();
1137 DCHECK(!read_cb_.is_null());
1139 DemuxerStream::Status status;
1140 scoped_refptr<StreamParserBuffer> buffer;
1142 switch (state_) {
1143 case UNINITIALIZED:
1144 NOTREACHED();
1145 return;
1146 case RETURNING_DATA_FOR_READS:
1147 switch (stream_->GetNextBuffer(&buffer)) {
1148 case SourceBufferStream::kSuccess:
1149 status = DemuxerStream::kOk;
1150 break;
1151 case SourceBufferStream::kNeedBuffer:
1152 // Return early without calling |read_cb_| since we don't have
1153 // any data to return yet.
1154 return;
1155 case SourceBufferStream::kEndOfStream:
1156 status = DemuxerStream::kOk;
1157 buffer = StreamParserBuffer::CreateEOSBuffer();
1158 break;
1159 case SourceBufferStream::kConfigChange:
1160 DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
1161 status = kConfigChanged;
1162 buffer = NULL;
1163 break;
1165 break;
1166 case RETURNING_ABORT_FOR_READS:
1167 // Null buffers should be returned in this state since we are waiting
1168 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1169 // because they are associated with the seek.
1170 status = DemuxerStream::kAborted;
1171 buffer = NULL;
1172 break;
1173 case SHUTDOWN:
1174 status = DemuxerStream::kOk;
1175 buffer = StreamParserBuffer::CreateEOSBuffer();
1176 break;
1179 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1182 ChunkDemuxer::ChunkDemuxer(const base::Closure& open_cb,
1183 const NeedKeyCB& need_key_cb,
1184 const LogCB& log_cb)
1185 : state_(WAITING_FOR_INIT),
1186 cancel_next_seek_(false),
1187 host_(NULL),
1188 open_cb_(open_cb),
1189 need_key_cb_(need_key_cb),
1190 enable_text_(false),
1191 log_cb_(log_cb),
1192 duration_(kNoTimestamp()),
1193 user_specified_duration_(-1) {
1194 DCHECK(!open_cb_.is_null());
1195 DCHECK(!need_key_cb_.is_null());
1198 void ChunkDemuxer::Initialize(
1199 DemuxerHost* host,
1200 const PipelineStatusCB& cb,
1201 bool enable_text_tracks) {
1202 DVLOG(1) << "Init()";
1204 base::AutoLock auto_lock(lock_);
1206 init_cb_ = BindToCurrentLoop(cb);
1207 if (state_ == SHUTDOWN) {
1208 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1209 return;
1211 DCHECK_EQ(state_, WAITING_FOR_INIT);
1212 host_ = host;
1213 enable_text_ = enable_text_tracks;
1215 ChangeState_Locked(INITIALIZING);
1217 base::ResetAndReturn(&open_cb_).Run();
1220 void ChunkDemuxer::Stop(const base::Closure& callback) {
1221 DVLOG(1) << "Stop()";
1222 Shutdown();
1223 callback.Run();
1226 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1227 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1228 DCHECK(time >= TimeDelta());
1230 base::AutoLock auto_lock(lock_);
1231 DCHECK(seek_cb_.is_null());
1233 seek_cb_ = BindToCurrentLoop(cb);
1234 if (state_ != INITIALIZED && state_ != ENDED) {
1235 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1236 return;
1239 if (cancel_next_seek_) {
1240 cancel_next_seek_ = false;
1241 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1242 return;
1245 SeekAllSources(time);
1246 StartReturningData();
1248 if (IsSeekWaitingForData_Locked()) {
1249 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1250 return;
1253 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1256 void ChunkDemuxer::OnAudioRendererDisabled() {
1257 base::AutoLock auto_lock(lock_);
1258 audio_->Shutdown();
1259 disabled_audio_ = audio_.Pass();
1262 // Demuxer implementation.
1263 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1264 DCHECK_NE(type, DemuxerStream::TEXT);
1265 base::AutoLock auto_lock(lock_);
1266 if (type == DemuxerStream::VIDEO)
1267 return video_.get();
1269 if (type == DemuxerStream::AUDIO)
1270 return audio_.get();
1272 return NULL;
1275 TimeDelta ChunkDemuxer::GetStartTime() const {
1276 return TimeDelta();
1279 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1280 DVLOG(1) << "StartWaitingForSeek()";
1281 base::AutoLock auto_lock(lock_);
1282 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1283 state_ == PARSE_ERROR) << state_;
1284 DCHECK(seek_cb_.is_null());
1286 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1287 return;
1289 AbortPendingReads();
1290 SeekAllSources(seek_time);
1292 // Cancel state set in CancelPendingSeek() since we want to
1293 // accept the next Seek().
1294 cancel_next_seek_ = false;
1297 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1298 base::AutoLock auto_lock(lock_);
1299 DCHECK_NE(state_, INITIALIZING);
1300 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1302 if (cancel_next_seek_)
1303 return;
1305 AbortPendingReads();
1306 SeekAllSources(seek_time);
1308 if (seek_cb_.is_null()) {
1309 cancel_next_seek_ = true;
1310 return;
1313 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1316 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1317 const std::string& type,
1318 std::vector<std::string>& codecs) {
1319 base::AutoLock auto_lock(lock_);
1321 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1322 return kReachedIdLimit;
1324 bool has_audio = false;
1325 bool has_video = false;
1326 scoped_ptr<media::StreamParser> stream_parser(
1327 StreamParserFactory::Create(type, codecs, log_cb_,
1328 &has_audio, &has_video));
1330 if (!stream_parser)
1331 return ChunkDemuxer::kNotSupported;
1333 if ((has_audio && !source_id_audio_.empty()) ||
1334 (has_video && !source_id_video_.empty()))
1335 return kReachedIdLimit;
1337 if (has_audio)
1338 source_id_audio_ = id;
1340 if (has_video)
1341 source_id_video_ = id;
1343 scoped_ptr<SourceState> source_state(
1344 new SourceState(stream_parser.Pass(), log_cb_,
1345 base::Bind(&ChunkDemuxer::CreateDemuxerStream,
1346 base::Unretained(this)),
1347 base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1348 base::Unretained(this))));
1350 SourceState::NewTextTrackCB new_text_track_cb;
1352 if (enable_text_) {
1353 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1354 base::Unretained(this));
1357 source_state->Init(
1358 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1359 has_audio,
1360 has_video,
1361 need_key_cb_,
1362 new_text_track_cb);
1364 source_state_map_[id] = source_state.release();
1365 return kOk;
1368 void ChunkDemuxer::RemoveId(const std::string& id) {
1369 base::AutoLock auto_lock(lock_);
1370 CHECK(IsValidId(id));
1372 delete source_state_map_[id];
1373 source_state_map_.erase(id);
1375 if (source_id_audio_ == id)
1376 source_id_audio_.clear();
1378 if (source_id_video_ == id)
1379 source_id_video_.clear();
1382 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1383 base::AutoLock auto_lock(lock_);
1384 DCHECK(!id.empty());
1386 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1388 DCHECK(itr != source_state_map_.end());
1389 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1392 void ChunkDemuxer::AppendData(const std::string& id,
1393 const uint8* data,
1394 size_t length) {
1395 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1397 DCHECK(!id.empty());
1399 Ranges<TimeDelta> ranges;
1402 base::AutoLock auto_lock(lock_);
1403 DCHECK_NE(state_, ENDED);
1405 // Capture if any of the SourceBuffers are waiting for data before we start
1406 // parsing.
1407 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1409 if (length == 0u)
1410 return;
1412 DCHECK(data);
1414 switch (state_) {
1415 case INITIALIZING:
1416 DCHECK(IsValidId(id));
1417 if (!source_state_map_[id]->Append(data, length)) {
1418 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1419 return;
1421 break;
1423 case INITIALIZED: {
1424 DCHECK(IsValidId(id));
1425 if (!source_state_map_[id]->Append(data, length)) {
1426 ReportError_Locked(PIPELINE_ERROR_DECODE);
1427 return;
1429 } break;
1431 case PARSE_ERROR:
1432 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1433 return;
1435 case WAITING_FOR_INIT:
1436 case ENDED:
1437 case SHUTDOWN:
1438 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1439 return;
1442 // Check to see if data was appended at the pending seek point. This
1443 // indicates we have parsed enough data to complete the seek.
1444 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1445 !seek_cb_.is_null()) {
1446 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1449 ranges = GetBufferedRanges_Locked();
1452 for (size_t i = 0; i < ranges.size(); ++i)
1453 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1456 void ChunkDemuxer::Abort(const std::string& id) {
1457 DVLOG(1) << "Abort(" << id << ")";
1458 base::AutoLock auto_lock(lock_);
1459 DCHECK(!id.empty());
1460 CHECK(IsValidId(id));
1461 source_state_map_[id]->Abort();
1464 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1465 TimeDelta end) {
1466 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1467 << ", " << end.InSecondsF() << ")";
1468 base::AutoLock auto_lock(lock_);
1470 DCHECK(!id.empty());
1471 CHECK(IsValidId(id));
1472 source_state_map_[id]->Remove(start, end, duration_);
1475 double ChunkDemuxer::GetDuration() {
1476 base::AutoLock auto_lock(lock_);
1477 return GetDuration_Locked();
1480 double ChunkDemuxer::GetDuration_Locked() {
1481 lock_.AssertAcquired();
1482 if (duration_ == kNoTimestamp())
1483 return std::numeric_limits<double>::quiet_NaN();
1485 // Return positive infinity if the resource is unbounded.
1486 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1487 if (duration_ == kInfiniteDuration())
1488 return std::numeric_limits<double>::infinity();
1490 if (user_specified_duration_ >= 0)
1491 return user_specified_duration_;
1493 return duration_.InSecondsF();
1496 void ChunkDemuxer::SetDuration(double duration) {
1497 base::AutoLock auto_lock(lock_);
1498 DVLOG(1) << "SetDuration(" << duration << ")";
1499 DCHECK_GE(duration, 0);
1501 if (duration == GetDuration_Locked())
1502 return;
1504 // Compute & bounds check the TimeDelta representation of duration.
1505 // This can be different if the value of |duration| doesn't fit the range or
1506 // precision of TimeDelta.
1507 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1508 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1509 double min_duration_in_seconds = min_duration.InSecondsF();
1510 double max_duration_in_seconds = max_duration.InSecondsF();
1512 TimeDelta duration_td;
1513 if (duration == std::numeric_limits<double>::infinity()) {
1514 duration_td = media::kInfiniteDuration();
1515 } else if (duration < min_duration_in_seconds) {
1516 duration_td = min_duration;
1517 } else if (duration > max_duration_in_seconds) {
1518 duration_td = max_duration;
1519 } else {
1520 duration_td = TimeDelta::FromMicroseconds(
1521 duration * base::Time::kMicrosecondsPerSecond);
1524 DCHECK(duration_td > TimeDelta());
1526 user_specified_duration_ = duration;
1527 duration_ = duration_td;
1528 host_->SetDuration(duration_);
1530 for (SourceStateMap::iterator itr = source_state_map_.begin();
1531 itr != source_state_map_.end(); ++itr) {
1532 itr->second->OnSetDuration(duration_);
1536 bool ChunkDemuxer::SetTimestampOffset(const std::string& id, TimeDelta offset) {
1537 base::AutoLock auto_lock(lock_);
1538 DVLOG(1) << "SetTimestampOffset(" << id << ", " << offset.InSecondsF() << ")";
1539 CHECK(IsValidId(id));
1541 return source_state_map_[id]->SetTimestampOffset(offset);
1544 bool ChunkDemuxer::SetSequenceMode(const std::string& id,
1545 bool sequence_mode) {
1546 base::AutoLock auto_lock(lock_);
1547 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1548 CHECK(IsValidId(id));
1549 DCHECK_NE(state_, ENDED);
1551 return source_state_map_[id]->SetSequenceMode(sequence_mode);
1554 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1555 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1556 base::AutoLock auto_lock(lock_);
1557 DCHECK_NE(state_, WAITING_FOR_INIT);
1558 DCHECK_NE(state_, ENDED);
1560 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1561 return;
1563 if (state_ == INITIALIZING) {
1564 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1565 return;
1568 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1569 for (SourceStateMap::iterator itr = source_state_map_.begin();
1570 itr != source_state_map_.end(); ++itr) {
1571 itr->second->MarkEndOfStream();
1574 CompletePendingReadsIfPossible();
1576 // Give a chance to resume the pending seek process.
1577 if (status != PIPELINE_OK) {
1578 ReportError_Locked(status);
1579 return;
1582 ChangeState_Locked(ENDED);
1583 DecreaseDurationIfNecessary();
1585 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1586 !seek_cb_.is_null()) {
1587 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1591 void ChunkDemuxer::UnmarkEndOfStream() {
1592 DVLOG(1) << "UnmarkEndOfStream()";
1593 base::AutoLock auto_lock(lock_);
1594 DCHECK_EQ(state_, ENDED);
1596 ChangeState_Locked(INITIALIZED);
1598 for (SourceStateMap::iterator itr = source_state_map_.begin();
1599 itr != source_state_map_.end(); ++itr) {
1600 itr->second->UnmarkEndOfStream();
1604 void ChunkDemuxer::SetAppendWindowStart(const std::string& id,
1605 TimeDelta start) {
1606 base::AutoLock auto_lock(lock_);
1607 DVLOG(1) << "SetAppendWindowStart(" << id << ", "
1608 << start.InSecondsF() << ")";
1609 CHECK(IsValidId(id));
1610 source_state_map_[id]->set_append_window_start(start);
1613 void ChunkDemuxer::SetAppendWindowEnd(const std::string& id, TimeDelta end) {
1614 base::AutoLock auto_lock(lock_);
1615 DVLOG(1) << "SetAppendWindowEnd(" << id << ", " << end.InSecondsF() << ")";
1616 CHECK(IsValidId(id));
1617 source_state_map_[id]->set_append_window_end(end);
1620 void ChunkDemuxer::Shutdown() {
1621 DVLOG(1) << "Shutdown()";
1622 base::AutoLock auto_lock(lock_);
1624 if (state_ == SHUTDOWN)
1625 return;
1627 ShutdownAllStreams();
1629 ChangeState_Locked(SHUTDOWN);
1631 if(!seek_cb_.is_null())
1632 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1635 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit) {
1636 for (SourceStateMap::iterator itr = source_state_map_.begin();
1637 itr != source_state_map_.end(); ++itr) {
1638 itr->second->SetMemoryLimitsForTesting(memory_limit);
1642 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1643 lock_.AssertAcquired();
1644 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1645 << state_ << " -> " << new_state;
1646 state_ = new_state;
1649 ChunkDemuxer::~ChunkDemuxer() {
1650 DCHECK_NE(state_, INITIALIZED);
1652 STLDeleteValues(&source_state_map_);
1655 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1656 DVLOG(1) << "ReportError_Locked(" << error << ")";
1657 lock_.AssertAcquired();
1658 DCHECK_NE(error, PIPELINE_OK);
1660 ChangeState_Locked(PARSE_ERROR);
1662 PipelineStatusCB cb;
1664 if (!init_cb_.is_null()) {
1665 std::swap(cb, init_cb_);
1666 } else {
1667 if (!seek_cb_.is_null())
1668 std::swap(cb, seek_cb_);
1670 ShutdownAllStreams();
1673 if (!cb.is_null()) {
1674 cb.Run(error);
1675 return;
1678 base::AutoUnlock auto_unlock(lock_);
1679 host_->OnDemuxerError(error);
1682 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1683 lock_.AssertAcquired();
1684 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1685 itr != source_state_map_.end(); ++itr) {
1686 if (itr->second->IsSeekWaitingForData())
1687 return true;
1690 return false;
1693 void ChunkDemuxer::OnSourceInitDone(bool success, TimeDelta duration) {
1694 DVLOG(1) << "OnSourceInitDone(" << success << ", "
1695 << duration.InSecondsF() << ")";
1696 lock_.AssertAcquired();
1697 DCHECK_EQ(state_, INITIALIZING);
1698 if (!success || (!audio_ && !video_)) {
1699 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1700 return;
1703 if (duration != TimeDelta() && duration_ == kNoTimestamp())
1704 UpdateDuration(duration);
1706 // Wait until all streams have initialized.
1707 if ((!source_id_audio_.empty() && !audio_) ||
1708 (!source_id_video_.empty() && !video_))
1709 return;
1711 SeekAllSources(GetStartTime());
1712 StartReturningData();
1714 if (duration_ == kNoTimestamp())
1715 duration_ = kInfiniteDuration();
1717 // The demuxer is now initialized after the |start_timestamp_| was set.
1718 ChangeState_Locked(INITIALIZED);
1719 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1722 ChunkDemuxerStream*
1723 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1724 switch (type) {
1725 case DemuxerStream::AUDIO:
1726 if (audio_)
1727 return NULL;
1728 audio_.reset(new ChunkDemuxerStream(DemuxerStream::AUDIO));
1729 return audio_.get();
1730 break;
1731 case DemuxerStream::VIDEO:
1732 if (video_)
1733 return NULL;
1734 video_.reset(new ChunkDemuxerStream(DemuxerStream::VIDEO));
1735 return video_.get();
1736 break;
1737 case DemuxerStream::TEXT: {
1738 return new ChunkDemuxerStream(DemuxerStream::TEXT);
1739 break;
1741 case DemuxerStream::UNKNOWN:
1742 case DemuxerStream::NUM_TYPES:
1743 NOTREACHED();
1744 return NULL;
1746 NOTREACHED();
1747 return NULL;
1750 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1751 const TextTrackConfig& config) {
1752 lock_.AssertAcquired();
1753 DCHECK_NE(state_, SHUTDOWN);
1754 host_->AddTextStream(text_stream, config);
1757 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1758 lock_.AssertAcquired();
1759 return source_state_map_.count(source_id) > 0u;
1762 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1763 DCHECK(duration_ != new_duration);
1764 user_specified_duration_ = -1;
1765 duration_ = new_duration;
1766 host_->SetDuration(new_duration);
1769 void ChunkDemuxer::IncreaseDurationIfNecessary(
1770 TimeDelta last_appended_buffer_timestamp,
1771 ChunkDemuxerStream* stream) {
1772 DCHECK(last_appended_buffer_timestamp != kNoTimestamp());
1773 if (last_appended_buffer_timestamp <= duration_)
1774 return;
1776 TimeDelta stream_duration = stream->GetBufferedDuration();
1777 DCHECK(stream_duration > TimeDelta());
1779 if (stream_duration > duration_)
1780 UpdateDuration(stream_duration);
1783 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1784 lock_.AssertAcquired();
1786 TimeDelta max_duration;
1788 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1789 itr != source_state_map_.end(); ++itr) {
1790 max_duration = std::max(max_duration,
1791 itr->second->GetMaxBufferedDuration());
1794 if (max_duration == TimeDelta())
1795 return;
1797 if (max_duration < duration_)
1798 UpdateDuration(max_duration);
1801 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1802 base::AutoLock auto_lock(lock_);
1803 return GetBufferedRanges_Locked();
1806 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1807 lock_.AssertAcquired();
1809 bool ended = state_ == ENDED;
1810 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1811 // we'll need to update this loop to only add ranges from active sources.
1812 RangesList ranges_list;
1813 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1814 itr != source_state_map_.end(); ++itr) {
1815 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1818 return ComputeIntersection(ranges_list, ended);
1821 void ChunkDemuxer::StartReturningData() {
1822 for (SourceStateMap::iterator itr = source_state_map_.begin();
1823 itr != source_state_map_.end(); ++itr) {
1824 itr->second->StartReturningData();
1828 void ChunkDemuxer::AbortPendingReads() {
1829 for (SourceStateMap::iterator itr = source_state_map_.begin();
1830 itr != source_state_map_.end(); ++itr) {
1831 itr->second->AbortReads();
1835 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1836 for (SourceStateMap::iterator itr = source_state_map_.begin();
1837 itr != source_state_map_.end(); ++itr) {
1838 itr->second->Seek(seek_time);
1842 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1843 for (SourceStateMap::iterator itr = source_state_map_.begin();
1844 itr != source_state_map_.end(); ++itr) {
1845 itr->second->CompletePendingReadIfPossible();
1849 void ChunkDemuxer::ShutdownAllStreams() {
1850 for (SourceStateMap::iterator itr = source_state_map_.begin();
1851 itr != source_state_map_.end(); ++itr) {
1852 itr->second->Shutdown();
1856 } // namespace media