MSE: On init segment received, set need random access point flag on all track buffers
[chromium-blink-merge.git] / media / filters / chunk_demuxer.cc
blobc4afc02c5430fccdb6d1f791c9789fb7fa80f44e
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "media/filters/chunk_demuxer.h"
7 #include <algorithm>
8 #include <limits>
9 #include <list>
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/location.h"
14 #include "base/message_loop/message_loop_proxy.h"
15 #include "base/stl_util.h"
16 #include "media/base/audio_decoder_config.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/stream_parser_buffer.h"
19 #include "media/base/video_decoder_config.h"
20 #include "media/filters/frame_processor.h"
21 #include "media/filters/legacy_frame_processor.h"
22 #include "media/filters/stream_parser_factory.h"
24 using base::TimeDelta;
26 namespace media {
28 static TimeDelta EndTimestamp(const StreamParser::BufferQueue& queue) {
29 return queue.back()->timestamp() + queue.back()->duration();
32 // List of time ranges for each SourceBuffer.
33 typedef std::list<Ranges<TimeDelta> > RangesList;
34 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
35 bool ended) {
36 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
37 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
39 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
40 // TimeRanges object and abort these steps.
41 if (activeRanges.empty())
42 return Ranges<TimeDelta>();
44 // Step 2: Let active ranges be the ranges returned by buffered for each
45 // SourceBuffer object in activeSourceBuffers.
46 // Step 3: Let highest end time be the largest range end time in the active
47 // ranges.
48 TimeDelta highest_end_time;
49 for (RangesList::const_iterator itr = activeRanges.begin();
50 itr != activeRanges.end(); ++itr) {
51 if (!itr->size())
52 continue;
54 highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
57 // Step 4: Let intersection ranges equal a TimeRange object containing a
58 // single range from 0 to highest end time.
59 Ranges<TimeDelta> intersection_ranges;
60 intersection_ranges.Add(TimeDelta(), highest_end_time);
62 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
63 // following steps:
64 for (RangesList::const_iterator itr = activeRanges.begin();
65 itr != activeRanges.end(); ++itr) {
66 // Step 5.1: Let source ranges equal the ranges returned by the buffered
67 // attribute on the current SourceBuffer.
68 Ranges<TimeDelta> source_ranges = *itr;
70 // Step 5.2: If readyState is "ended", then set the end time on the last
71 // range in source ranges to highest end time.
72 if (ended && source_ranges.size() > 0u) {
73 source_ranges.Add(source_ranges.start(source_ranges.size() - 1),
74 highest_end_time);
77 // Step 5.3: Let new intersection ranges equal the intersection between
78 // the intersection ranges and the source ranges.
79 // Step 5.4: Replace the ranges in intersection ranges with the new
80 // intersection ranges.
81 intersection_ranges = intersection_ranges.IntersectionWith(source_ranges);
84 return intersection_ranges;
87 // Contains state belonging to a source id.
88 class SourceState {
89 public:
90 // Callback signature used to create ChunkDemuxerStreams.
91 typedef base::Callback<ChunkDemuxerStream*(
92 DemuxerStream::Type)> CreateDemuxerStreamCB;
94 typedef base::Callback<void(
95 ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
97 SourceState(
98 scoped_ptr<StreamParser> stream_parser,
99 scoped_ptr<FrameProcessorBase> frame_processor, const LogCB& log_cb,
100 const CreateDemuxerStreamCB& create_demuxer_stream_cb);
102 ~SourceState();
104 void Init(const StreamParser::InitCB& init_cb,
105 bool allow_audio,
106 bool allow_video,
107 const StreamParser::NeedKeyCB& need_key_cb,
108 const NewTextTrackCB& new_text_track_cb);
110 // Appends new data to the StreamParser.
111 // Returns true if the data was successfully appended. Returns false if an
112 // error occurred. |*timestamp_offset| is used and possibly updated by the
113 // append. |append_window_start| and |append_window_end| correspond to the MSE
114 // spec's similarly named source buffer attributes that are used in coded
115 // frame processing.
116 bool Append(const uint8* data, size_t length,
117 TimeDelta append_window_start,
118 TimeDelta append_window_end,
119 TimeDelta* timestamp_offset);
121 // Aborts the current append sequence and resets the parser.
122 void Abort(TimeDelta append_window_start,
123 TimeDelta append_window_end,
124 TimeDelta* timestamp_offset);
126 // Calls Remove(|start|, |end|, |duration|) on all
127 // ChunkDemuxerStreams managed by this object.
128 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
130 // Returns true if currently parsing a media segment, or false otherwise.
131 bool parsing_media_segment() const { return parsing_media_segment_; }
133 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
134 void SetSequenceMode(bool sequence_mode);
136 // Signals the coded frame processor to update its group start timestamp to be
137 // |timestamp_offset| if it is in sequence append mode.
138 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset);
140 // Returns the range of buffered data in this source, capped at |duration|.
141 // |ended| - Set to true if end of stream has been signaled and the special
142 // end of stream range logic needs to be executed.
143 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
145 // Returns the highest buffered duration across all streams managed
146 // by this object.
147 // Returns TimeDelta() if none of the streams contain buffered data.
148 TimeDelta GetMaxBufferedDuration() const;
150 // Helper methods that call methods with similar names on all the
151 // ChunkDemuxerStreams managed by this object.
152 void StartReturningData();
153 void AbortReads();
154 void Seek(TimeDelta seek_time);
155 void CompletePendingReadIfPossible();
156 void OnSetDuration(TimeDelta duration);
157 void MarkEndOfStream();
158 void UnmarkEndOfStream();
159 void Shutdown();
160 // Sets the memory limit on each stream. |memory_limit| is the
161 // maximum number of bytes each stream is allowed to hold in its buffer.
162 void SetMemoryLimitsForTesting(int memory_limit);
163 bool IsSeekWaitingForData() const;
165 private:
166 // Called by the |stream_parser_| when a new initialization segment is
167 // encountered.
168 // Returns true on a successful call. Returns false if an error occurred while
169 // processing decoder configurations.
170 bool OnNewConfigs(bool allow_audio, bool allow_video,
171 const AudioDecoderConfig& audio_config,
172 const VideoDecoderConfig& video_config,
173 const StreamParser::TextTrackConfigMap& text_configs);
175 // Called by the |stream_parser_| at the beginning of a new media segment.
176 void OnNewMediaSegment();
178 // Called by the |stream_parser_| at the end of a media segment.
179 void OnEndOfMediaSegment();
181 // Called by the |stream_parser_| when new buffers have been parsed.
182 // It processes the new buffers using |frame_processor_|, which includes
183 // appending the processed frames to associated demuxer streams for each
184 // frame's track.
185 // Returns true on a successful call. Returns false if an error occurred while
186 // processing the buffers.
187 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
188 const StreamParser::BufferQueue& video_buffers,
189 const StreamParser::TextBufferQueueMap& text_map);
191 void OnSourceInitDone(bool success,
192 const StreamParser::InitParameters& params);
194 CreateDemuxerStreamCB create_demuxer_stream_cb_;
195 NewTextTrackCB new_text_track_cb_;
197 // During Append(), if OnNewBuffers() coded frame processing updates the
198 // timestamp offset then |*timestamp_offset_during_append_| is also updated
199 // so Append()'s caller can know the new offset. This pointer is only non-NULL
200 // during the lifetime of an Append() call.
201 TimeDelta* timestamp_offset_during_append_;
203 // During Append(), coded frame processing triggered by OnNewBuffers()
204 // requires these two attributes. These are only valid during the lifetime of
205 // an Append() call.
206 TimeDelta append_window_start_during_append_;
207 TimeDelta append_window_end_during_append_;
209 // Set to true if the next buffers appended within the append window
210 // represent the start of a new media segment. This flag being set
211 // triggers a call to |new_segment_cb_| when the new buffers are
212 // appended. The flag is set on actual media segment boundaries and
213 // when the "append window" filtering causes discontinuities in the
214 // appended data.
215 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
216 // processing's discontinuity logic is enough. See http://crbug.com/351489.
217 bool new_media_segment_;
219 // Keeps track of whether a media segment is being parsed.
220 bool parsing_media_segment_;
222 // The object used to parse appended data.
223 scoped_ptr<StreamParser> stream_parser_;
225 ChunkDemuxerStream* audio_; // Not owned by |this|.
226 ChunkDemuxerStream* video_; // Not owned by |this|.
228 typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
229 TextStreamMap text_stream_map_; // |this| owns the map's stream pointers.
231 scoped_ptr<FrameProcessorBase> frame_processor_;
232 LogCB log_cb_;
233 StreamParser::InitCB init_cb_;
235 // Indicates that timestampOffset should be updated automatically during
236 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
237 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
238 // changes to MSE spec. See http://crbug.com/371499.
239 bool auto_update_timestamp_offset_;
241 DISALLOW_COPY_AND_ASSIGN(SourceState);
244 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
245 scoped_ptr<FrameProcessorBase> frame_processor,
246 const LogCB& log_cb,
247 const CreateDemuxerStreamCB& create_demuxer_stream_cb)
248 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
249 timestamp_offset_during_append_(NULL),
250 new_media_segment_(false),
251 parsing_media_segment_(false),
252 stream_parser_(stream_parser.release()),
253 audio_(NULL),
254 video_(NULL),
255 frame_processor_(frame_processor.release()),
256 log_cb_(log_cb),
257 auto_update_timestamp_offset_(false) {
258 DCHECK(!create_demuxer_stream_cb_.is_null());
259 DCHECK(frame_processor_);
262 SourceState::~SourceState() {
263 Shutdown();
265 STLDeleteValues(&text_stream_map_);
268 void SourceState::Init(const StreamParser::InitCB& init_cb,
269 bool allow_audio,
270 bool allow_video,
271 const StreamParser::NeedKeyCB& need_key_cb,
272 const NewTextTrackCB& new_text_track_cb) {
273 new_text_track_cb_ = new_text_track_cb;
274 init_cb_ = init_cb;
276 stream_parser_->Init(
277 base::Bind(&SourceState::OnSourceInitDone, base::Unretained(this)),
278 base::Bind(&SourceState::OnNewConfigs,
279 base::Unretained(this),
280 allow_audio,
281 allow_video),
282 base::Bind(&SourceState::OnNewBuffers, base::Unretained(this)),
283 new_text_track_cb_.is_null(),
284 need_key_cb,
285 base::Bind(&SourceState::OnNewMediaSegment, base::Unretained(this)),
286 base::Bind(&SourceState::OnEndOfMediaSegment, base::Unretained(this)),
287 log_cb_);
290 void SourceState::SetSequenceMode(bool sequence_mode) {
291 DCHECK(!parsing_media_segment_);
293 frame_processor_->SetSequenceMode(sequence_mode);
296 void SourceState::SetGroupStartTimestampIfInSequenceMode(
297 base::TimeDelta timestamp_offset) {
298 DCHECK(!parsing_media_segment_);
300 frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
303 bool SourceState::Append(const uint8* data, size_t length,
304 TimeDelta append_window_start,
305 TimeDelta append_window_end,
306 TimeDelta* timestamp_offset) {
307 DCHECK(timestamp_offset);
308 DCHECK(!timestamp_offset_during_append_);
309 append_window_start_during_append_ = append_window_start;
310 append_window_end_during_append_ = append_window_end;
311 timestamp_offset_during_append_ = timestamp_offset;
313 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
314 // append window and timestamp offset pointer. See http://crbug.com/351454.
315 bool err = stream_parser_->Parse(data, length);
316 timestamp_offset_during_append_ = NULL;
317 return err;
320 void SourceState::Abort(TimeDelta append_window_start,
321 TimeDelta append_window_end,
322 base::TimeDelta* timestamp_offset) {
323 DCHECK(timestamp_offset);
324 DCHECK(!timestamp_offset_during_append_);
325 timestamp_offset_during_append_ = timestamp_offset;
326 append_window_start_during_append_ = append_window_start;
327 append_window_end_during_append_ = append_window_end;
329 stream_parser_->Flush();
330 timestamp_offset_during_append_ = NULL;
332 frame_processor_->Reset();
333 parsing_media_segment_ = false;
336 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
337 if (audio_)
338 audio_->Remove(start, end, duration);
340 if (video_)
341 video_->Remove(start, end, duration);
343 for (TextStreamMap::iterator itr = text_stream_map_.begin();
344 itr != text_stream_map_.end(); ++itr) {
345 itr->second->Remove(start, end, duration);
349 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
350 bool ended) const {
351 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
352 // this code to only add ranges from active tracks.
353 RangesList ranges_list;
354 if (audio_)
355 ranges_list.push_back(audio_->GetBufferedRanges(duration));
357 if (video_)
358 ranges_list.push_back(video_->GetBufferedRanges(duration));
360 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
361 itr != text_stream_map_.end(); ++itr) {
362 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
365 return ComputeIntersection(ranges_list, ended);
368 TimeDelta SourceState::GetMaxBufferedDuration() const {
369 TimeDelta max_duration;
371 if (audio_)
372 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
374 if (video_)
375 max_duration = std::max(max_duration, video_->GetBufferedDuration());
377 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
378 itr != text_stream_map_.end(); ++itr) {
379 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
382 return max_duration;
385 void SourceState::StartReturningData() {
386 if (audio_)
387 audio_->StartReturningData();
389 if (video_)
390 video_->StartReturningData();
392 for (TextStreamMap::iterator itr = text_stream_map_.begin();
393 itr != text_stream_map_.end(); ++itr) {
394 itr->second->StartReturningData();
398 void SourceState::AbortReads() {
399 if (audio_)
400 audio_->AbortReads();
402 if (video_)
403 video_->AbortReads();
405 for (TextStreamMap::iterator itr = text_stream_map_.begin();
406 itr != text_stream_map_.end(); ++itr) {
407 itr->second->AbortReads();
411 void SourceState::Seek(TimeDelta seek_time) {
412 if (audio_)
413 audio_->Seek(seek_time);
415 if (video_)
416 video_->Seek(seek_time);
418 for (TextStreamMap::iterator itr = text_stream_map_.begin();
419 itr != text_stream_map_.end(); ++itr) {
420 itr->second->Seek(seek_time);
424 void SourceState::CompletePendingReadIfPossible() {
425 if (audio_)
426 audio_->CompletePendingReadIfPossible();
428 if (video_)
429 video_->CompletePendingReadIfPossible();
431 for (TextStreamMap::iterator itr = text_stream_map_.begin();
432 itr != text_stream_map_.end(); ++itr) {
433 itr->second->CompletePendingReadIfPossible();
437 void SourceState::OnSetDuration(TimeDelta duration) {
438 if (audio_)
439 audio_->OnSetDuration(duration);
441 if (video_)
442 video_->OnSetDuration(duration);
444 for (TextStreamMap::iterator itr = text_stream_map_.begin();
445 itr != text_stream_map_.end(); ++itr) {
446 itr->second->OnSetDuration(duration);
450 void SourceState::MarkEndOfStream() {
451 if (audio_)
452 audio_->MarkEndOfStream();
454 if (video_)
455 video_->MarkEndOfStream();
457 for (TextStreamMap::iterator itr = text_stream_map_.begin();
458 itr != text_stream_map_.end(); ++itr) {
459 itr->second->MarkEndOfStream();
463 void SourceState::UnmarkEndOfStream() {
464 if (audio_)
465 audio_->UnmarkEndOfStream();
467 if (video_)
468 video_->UnmarkEndOfStream();
470 for (TextStreamMap::iterator itr = text_stream_map_.begin();
471 itr != text_stream_map_.end(); ++itr) {
472 itr->second->UnmarkEndOfStream();
476 void SourceState::Shutdown() {
477 if (audio_)
478 audio_->Shutdown();
480 if (video_)
481 video_->Shutdown();
483 for (TextStreamMap::iterator itr = text_stream_map_.begin();
484 itr != text_stream_map_.end(); ++itr) {
485 itr->second->Shutdown();
489 void SourceState::SetMemoryLimitsForTesting(int memory_limit) {
490 if (audio_)
491 audio_->set_memory_limit_for_testing(memory_limit);
493 if (video_)
494 video_->set_memory_limit_for_testing(memory_limit);
496 for (TextStreamMap::iterator itr = text_stream_map_.begin();
497 itr != text_stream_map_.end(); ++itr) {
498 itr->second->set_memory_limit_for_testing(memory_limit);
502 bool SourceState::IsSeekWaitingForData() const {
503 if (audio_ && audio_->IsSeekWaitingForData())
504 return true;
506 if (video_ && video_->IsSeekWaitingForData())
507 return true;
509 // NOTE: We are intentionally not checking the text tracks
510 // because text tracks are discontinuous and may not have data
511 // for the seek position. This is ok and playback should not be
512 // stalled because we don't have cues. If cues, with timestamps after
513 // the seek time, eventually arrive they will be delivered properly
514 // in response to ChunkDemuxerStream::Read() calls.
516 return false;
519 bool SourceState::OnNewConfigs(
520 bool allow_audio, bool allow_video,
521 const AudioDecoderConfig& audio_config,
522 const VideoDecoderConfig& video_config,
523 const StreamParser::TextTrackConfigMap& text_configs) {
524 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
525 << ", " << audio_config.IsValidConfig()
526 << ", " << video_config.IsValidConfig() << ")";
528 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
529 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
530 return false;
533 // Signal an error if we get configuration info for stream types that weren't
534 // specified in AddId() or more configs after a stream is initialized.
535 if (allow_audio != audio_config.IsValidConfig()) {
536 MEDIA_LOG(log_cb_)
537 << "Initialization segment"
538 << (audio_config.IsValidConfig() ? " has" : " does not have")
539 << " an audio track, but the mimetype"
540 << (allow_audio ? " specifies" : " does not specify")
541 << " an audio codec.";
542 return false;
545 if (allow_video != video_config.IsValidConfig()) {
546 MEDIA_LOG(log_cb_)
547 << "Initialization segment"
548 << (video_config.IsValidConfig() ? " has" : " does not have")
549 << " a video track, but the mimetype"
550 << (allow_video ? " specifies" : " does not specify")
551 << " a video codec.";
552 return false;
555 bool success = true;
556 if (audio_config.IsValidConfig()) {
557 if (!audio_) {
558 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
560 if (!audio_) {
561 DVLOG(1) << "Failed to create an audio stream.";
562 return false;
565 if (!frame_processor_->AddTrack(FrameProcessorBase::kAudioTrackId,
566 audio_)) {
567 DVLOG(1) << "Failed to add audio track to frame processor.";
568 return false;
572 frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
573 success &= audio_->UpdateAudioConfig(audio_config, log_cb_);
576 if (video_config.IsValidConfig()) {
577 if (!video_) {
578 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
580 if (!video_) {
581 DVLOG(1) << "Failed to create a video stream.";
582 return false;
585 if (!frame_processor_->AddTrack(FrameProcessorBase::kVideoTrackId,
586 video_)) {
587 DVLOG(1) << "Failed to add video track to frame processor.";
588 return false;
592 success &= video_->UpdateVideoConfig(video_config, log_cb_);
595 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
596 if (text_stream_map_.empty()) {
597 for (TextConfigItr itr = text_configs.begin();
598 itr != text_configs.end(); ++itr) {
599 ChunkDemuxerStream* const text_stream =
600 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
601 if (!frame_processor_->AddTrack(itr->first, text_stream)) {
602 success &= false;
603 MEDIA_LOG(log_cb_) << "Failed to add text track ID " << itr->first
604 << " to frame processor.";
605 break;
607 text_stream->UpdateTextConfig(itr->second, log_cb_);
608 text_stream_map_[itr->first] = text_stream;
609 new_text_track_cb_.Run(text_stream, itr->second);
611 } else {
612 const size_t text_count = text_stream_map_.size();
613 if (text_configs.size() != text_count) {
614 success &= false;
615 MEDIA_LOG(log_cb_) << "The number of text track configs changed.";
616 } else if (text_count == 1) {
617 TextConfigItr config_itr = text_configs.begin();
618 const TextTrackConfig& new_config = config_itr->second;
619 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
620 ChunkDemuxerStream* text_stream = stream_itr->second;
621 TextTrackConfig old_config = text_stream->text_track_config();
622 if (!new_config.Matches(old_config)) {
623 success &= false;
624 MEDIA_LOG(log_cb_) << "New text track config does not match old one.";
625 } else {
626 StreamParser::TrackId old_id = stream_itr->first;
627 StreamParser::TrackId new_id = config_itr->first;
628 if (new_id != old_id) {
629 if (frame_processor_->UpdateTrack(old_id, new_id)) {
630 text_stream_map_.clear();
631 text_stream_map_[config_itr->first] = text_stream;
632 } else {
633 success &= false;
634 MEDIA_LOG(log_cb_) << "Error remapping single text track number";
638 } else {
639 for (TextConfigItr config_itr = text_configs.begin();
640 config_itr != text_configs.end(); ++config_itr) {
641 TextStreamMap::iterator stream_itr =
642 text_stream_map_.find(config_itr->first);
643 if (stream_itr == text_stream_map_.end()) {
644 success &= false;
645 MEDIA_LOG(log_cb_) << "Unexpected text track configuration "
646 "for track ID "
647 << config_itr->first;
648 break;
651 const TextTrackConfig& new_config = config_itr->second;
652 ChunkDemuxerStream* stream = stream_itr->second;
653 TextTrackConfig old_config = stream->text_track_config();
654 if (!new_config.Matches(old_config)) {
655 success &= false;
656 MEDIA_LOG(log_cb_) << "New text track config for track ID "
657 << config_itr->first
658 << " does not match old one.";
659 break;
665 frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
667 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
668 return success;
671 void SourceState::OnNewMediaSegment() {
672 DVLOG(2) << "OnNewMediaSegment()";
673 parsing_media_segment_ = true;
674 new_media_segment_ = true;
677 void SourceState::OnEndOfMediaSegment() {
678 DVLOG(2) << "OnEndOfMediaSegment()";
679 parsing_media_segment_ = false;
680 new_media_segment_ = false;
683 bool SourceState::OnNewBuffers(
684 const StreamParser::BufferQueue& audio_buffers,
685 const StreamParser::BufferQueue& video_buffers,
686 const StreamParser::TextBufferQueueMap& text_map) {
687 DVLOG(2) << "OnNewBuffers()";
688 DCHECK(timestamp_offset_during_append_);
689 DCHECK(parsing_media_segment_);
691 const TimeDelta timestamp_offset_before_processing =
692 *timestamp_offset_during_append_;
694 // Calculate the new timestamp offset for audio/video tracks if the stream
695 // parser has requested automatic updates.
696 TimeDelta new_timestamp_offset = timestamp_offset_before_processing;
697 if (auto_update_timestamp_offset_) {
698 const bool have_audio_buffers = !audio_buffers.empty();
699 const bool have_video_buffers = !video_buffers.empty();
700 if (have_audio_buffers && have_video_buffers) {
701 new_timestamp_offset +=
702 std::min(EndTimestamp(audio_buffers), EndTimestamp(video_buffers));
703 } else if (have_audio_buffers) {
704 new_timestamp_offset += EndTimestamp(audio_buffers);
705 } else if (have_video_buffers) {
706 new_timestamp_offset += EndTimestamp(video_buffers);
710 if (!frame_processor_->ProcessFrames(audio_buffers,
711 video_buffers,
712 text_map,
713 append_window_start_during_append_,
714 append_window_end_during_append_,
715 &new_media_segment_,
716 timestamp_offset_during_append_)) {
717 return false;
720 // Only update the timestamp offset if the frame processor hasn't already.
721 if (auto_update_timestamp_offset_ &&
722 timestamp_offset_before_processing == *timestamp_offset_during_append_) {
723 *timestamp_offset_during_append_ = new_timestamp_offset;
726 return true;
729 void SourceState::OnSourceInitDone(bool success,
730 const StreamParser::InitParameters& params) {
731 auto_update_timestamp_offset_ = params.auto_update_timestamp_offset;
732 base::ResetAndReturn(&init_cb_).Run(success, params);
735 ChunkDemuxerStream::ChunkDemuxerStream(Type type, bool splice_frames_enabled)
736 : type_(type),
737 state_(UNINITIALIZED),
738 splice_frames_enabled_(splice_frames_enabled),
739 partial_append_window_trimming_enabled_(false) {
742 void ChunkDemuxerStream::StartReturningData() {
743 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
744 base::AutoLock auto_lock(lock_);
745 DCHECK(read_cb_.is_null());
746 ChangeState_Locked(RETURNING_DATA_FOR_READS);
749 void ChunkDemuxerStream::AbortReads() {
750 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
751 base::AutoLock auto_lock(lock_);
752 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
753 if (!read_cb_.is_null())
754 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
757 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
758 base::AutoLock auto_lock(lock_);
759 if (read_cb_.is_null())
760 return;
762 CompletePendingReadIfPossible_Locked();
765 void ChunkDemuxerStream::Shutdown() {
766 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
767 base::AutoLock auto_lock(lock_);
768 ChangeState_Locked(SHUTDOWN);
770 // Pass an end of stream buffer to the pending callback to signal that no more
771 // data will be sent.
772 if (!read_cb_.is_null()) {
773 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
774 StreamParserBuffer::CreateEOSBuffer());
778 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
779 base::AutoLock auto_lock(lock_);
781 // This method should not be called for text tracks. See the note in
782 // SourceState::IsSeekWaitingForData().
783 DCHECK_NE(type_, DemuxerStream::TEXT);
785 return stream_->IsSeekPending();
788 void ChunkDemuxerStream::Seek(TimeDelta time) {
789 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
790 base::AutoLock auto_lock(lock_);
791 DCHECK(read_cb_.is_null());
792 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
793 << state_;
795 stream_->Seek(time);
798 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
799 if (buffers.empty())
800 return false;
802 base::AutoLock auto_lock(lock_);
803 DCHECK_NE(state_, SHUTDOWN);
804 if (!stream_->Append(buffers)) {
805 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
806 return false;
809 if (!read_cb_.is_null())
810 CompletePendingReadIfPossible_Locked();
812 return true;
815 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
816 TimeDelta duration) {
817 base::AutoLock auto_lock(lock_);
818 stream_->Remove(start, end, duration);
821 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
822 base::AutoLock auto_lock(lock_);
823 stream_->OnSetDuration(duration);
826 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
827 TimeDelta duration) const {
828 base::AutoLock auto_lock(lock_);
830 if (type_ == TEXT) {
831 // Since text tracks are discontinuous and the lack of cues should not block
832 // playback, report the buffered range for text tracks as [0, |duration|) so
833 // that intesections with audio & video tracks are computed correctly when
834 // no cues are present.
835 Ranges<TimeDelta> text_range;
836 text_range.Add(TimeDelta(), duration);
837 return text_range;
840 Ranges<TimeDelta> range = stream_->GetBufferedTime();
842 if (range.size() == 0u)
843 return range;
845 // Clamp the end of the stream's buffered ranges to fit within the duration.
846 // This can be done by intersecting the stream's range with the valid time
847 // range.
848 Ranges<TimeDelta> valid_time_range;
849 valid_time_range.Add(range.start(0), duration);
850 return range.IntersectionWith(valid_time_range);
853 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
854 return stream_->GetBufferedDuration();
857 void ChunkDemuxerStream::OnNewMediaSegment(TimeDelta start_timestamp) {
858 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
859 << start_timestamp.InSecondsF() << ")";
860 base::AutoLock auto_lock(lock_);
861 stream_->OnNewMediaSegment(start_timestamp);
864 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig& config,
865 const LogCB& log_cb) {
866 DCHECK(config.IsValidConfig());
867 DCHECK_EQ(type_, AUDIO);
868 base::AutoLock auto_lock(lock_);
869 if (!stream_) {
870 DCHECK_EQ(state_, UNINITIALIZED);
872 // On platforms which support splice frames, enable splice frames and
873 // partial append window support for most codecs (notably: not opus).
874 const bool codec_supported = config.codec() == kCodecMP3 ||
875 config.codec() == kCodecAAC ||
876 config.codec() == kCodecVorbis;
877 splice_frames_enabled_ = splice_frames_enabled_ && codec_supported;
878 partial_append_window_trimming_enabled_ =
879 splice_frames_enabled_ && codec_supported;
881 stream_.reset(
882 new SourceBufferStream(config, log_cb, splice_frames_enabled_));
883 return true;
886 return stream_->UpdateAudioConfig(config);
889 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig& config,
890 const LogCB& log_cb) {
891 DCHECK(config.IsValidConfig());
892 DCHECK_EQ(type_, VIDEO);
893 base::AutoLock auto_lock(lock_);
895 if (!stream_) {
896 DCHECK_EQ(state_, UNINITIALIZED);
897 stream_.reset(
898 new SourceBufferStream(config, log_cb, splice_frames_enabled_));
899 return true;
902 return stream_->UpdateVideoConfig(config);
905 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig& config,
906 const LogCB& log_cb) {
907 DCHECK_EQ(type_, TEXT);
908 base::AutoLock auto_lock(lock_);
909 DCHECK(!stream_);
910 DCHECK_EQ(state_, UNINITIALIZED);
911 stream_.reset(new SourceBufferStream(config, log_cb, splice_frames_enabled_));
914 void ChunkDemuxerStream::MarkEndOfStream() {
915 base::AutoLock auto_lock(lock_);
916 stream_->MarkEndOfStream();
919 void ChunkDemuxerStream::UnmarkEndOfStream() {
920 base::AutoLock auto_lock(lock_);
921 stream_->UnmarkEndOfStream();
924 // DemuxerStream methods.
925 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
926 base::AutoLock auto_lock(lock_);
927 DCHECK_NE(state_, UNINITIALIZED);
928 DCHECK(read_cb_.is_null());
930 read_cb_ = BindToCurrentLoop(read_cb);
931 CompletePendingReadIfPossible_Locked();
934 DemuxerStream::Type ChunkDemuxerStream::type() { return type_; }
936 void ChunkDemuxerStream::EnableBitstreamConverter() {}
938 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
939 CHECK_EQ(type_, AUDIO);
940 base::AutoLock auto_lock(lock_);
941 return stream_->GetCurrentAudioDecoderConfig();
944 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
945 CHECK_EQ(type_, VIDEO);
946 base::AutoLock auto_lock(lock_);
947 return stream_->GetCurrentVideoDecoderConfig();
950 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
952 TextTrackConfig ChunkDemuxerStream::text_track_config() {
953 CHECK_EQ(type_, TEXT);
954 base::AutoLock auto_lock(lock_);
955 return stream_->GetCurrentTextTrackConfig();
958 void ChunkDemuxerStream::ChangeState_Locked(State state) {
959 lock_.AssertAcquired();
960 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
961 << "type " << type_
962 << " - " << state_ << " -> " << state;
963 state_ = state;
966 ChunkDemuxerStream::~ChunkDemuxerStream() {}
968 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
969 lock_.AssertAcquired();
970 DCHECK(!read_cb_.is_null());
972 DemuxerStream::Status status;
973 scoped_refptr<StreamParserBuffer> buffer;
975 switch (state_) {
976 case UNINITIALIZED:
977 NOTREACHED();
978 return;
979 case RETURNING_DATA_FOR_READS:
980 switch (stream_->GetNextBuffer(&buffer)) {
981 case SourceBufferStream::kSuccess:
982 status = DemuxerStream::kOk;
983 break;
984 case SourceBufferStream::kNeedBuffer:
985 // Return early without calling |read_cb_| since we don't have
986 // any data to return yet.
987 return;
988 case SourceBufferStream::kEndOfStream:
989 status = DemuxerStream::kOk;
990 buffer = StreamParserBuffer::CreateEOSBuffer();
991 break;
992 case SourceBufferStream::kConfigChange:
993 DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
994 status = kConfigChanged;
995 buffer = NULL;
996 break;
998 break;
999 case RETURNING_ABORT_FOR_READS:
1000 // Null buffers should be returned in this state since we are waiting
1001 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1002 // because they are associated with the seek.
1003 status = DemuxerStream::kAborted;
1004 buffer = NULL;
1005 break;
1006 case SHUTDOWN:
1007 status = DemuxerStream::kOk;
1008 buffer = StreamParserBuffer::CreateEOSBuffer();
1009 break;
1012 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1015 ChunkDemuxer::ChunkDemuxer(const base::Closure& open_cb,
1016 const NeedKeyCB& need_key_cb,
1017 const LogCB& log_cb,
1018 bool splice_frames_enabled)
1019 : state_(WAITING_FOR_INIT),
1020 cancel_next_seek_(false),
1021 host_(NULL),
1022 open_cb_(open_cb),
1023 need_key_cb_(need_key_cb),
1024 enable_text_(false),
1025 log_cb_(log_cb),
1026 duration_(kNoTimestamp()),
1027 user_specified_duration_(-1),
1028 liveness_(LIVENESS_UNKNOWN),
1029 splice_frames_enabled_(splice_frames_enabled) {
1030 DCHECK(!open_cb_.is_null());
1031 DCHECK(!need_key_cb_.is_null());
1034 void ChunkDemuxer::Initialize(
1035 DemuxerHost* host,
1036 const PipelineStatusCB& cb,
1037 bool enable_text_tracks) {
1038 DVLOG(1) << "Init()";
1040 base::AutoLock auto_lock(lock_);
1042 init_cb_ = BindToCurrentLoop(cb);
1043 if (state_ == SHUTDOWN) {
1044 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1045 return;
1047 DCHECK_EQ(state_, WAITING_FOR_INIT);
1048 host_ = host;
1049 enable_text_ = enable_text_tracks;
1051 ChangeState_Locked(INITIALIZING);
1053 base::ResetAndReturn(&open_cb_).Run();
1056 void ChunkDemuxer::Stop(const base::Closure& callback) {
1057 DVLOG(1) << "Stop()";
1058 Shutdown();
1059 callback.Run();
1062 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1063 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1064 DCHECK(time >= TimeDelta());
1066 base::AutoLock auto_lock(lock_);
1067 DCHECK(seek_cb_.is_null());
1069 seek_cb_ = BindToCurrentLoop(cb);
1070 if (state_ != INITIALIZED && state_ != ENDED) {
1071 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1072 return;
1075 if (cancel_next_seek_) {
1076 cancel_next_seek_ = false;
1077 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1078 return;
1081 SeekAllSources(time);
1082 StartReturningData();
1084 if (IsSeekWaitingForData_Locked()) {
1085 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1086 return;
1089 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1092 // Demuxer implementation.
1093 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1094 DCHECK_NE(type, DemuxerStream::TEXT);
1095 base::AutoLock auto_lock(lock_);
1096 if (type == DemuxerStream::VIDEO)
1097 return video_.get();
1099 if (type == DemuxerStream::AUDIO)
1100 return audio_.get();
1102 return NULL;
1105 TimeDelta ChunkDemuxer::GetStartTime() const {
1106 return TimeDelta();
1109 base::Time ChunkDemuxer::GetTimelineOffset() const {
1110 return timeline_offset_;
1113 Demuxer::Liveness ChunkDemuxer::GetLiveness() const {
1114 return liveness_;
1117 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1118 DVLOG(1) << "StartWaitingForSeek()";
1119 base::AutoLock auto_lock(lock_);
1120 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1121 state_ == PARSE_ERROR) << state_;
1122 DCHECK(seek_cb_.is_null());
1124 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1125 return;
1127 AbortPendingReads();
1128 SeekAllSources(seek_time);
1130 // Cancel state set in CancelPendingSeek() since we want to
1131 // accept the next Seek().
1132 cancel_next_seek_ = false;
1135 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1136 base::AutoLock auto_lock(lock_);
1137 DCHECK_NE(state_, INITIALIZING);
1138 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1140 if (cancel_next_seek_)
1141 return;
1143 AbortPendingReads();
1144 SeekAllSources(seek_time);
1146 if (seek_cb_.is_null()) {
1147 cancel_next_seek_ = true;
1148 return;
1151 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1154 ChunkDemuxer::Status ChunkDemuxer::AddId(
1155 const std::string& id,
1156 const std::string& type,
1157 std::vector<std::string>& codecs,
1158 const bool use_legacy_frame_processor) {
1159 base::AutoLock auto_lock(lock_);
1161 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1162 return kReachedIdLimit;
1164 bool has_audio = false;
1165 bool has_video = false;
1166 scoped_ptr<media::StreamParser> stream_parser(
1167 StreamParserFactory::Create(type, codecs, log_cb_,
1168 &has_audio, &has_video));
1170 if (!stream_parser)
1171 return ChunkDemuxer::kNotSupported;
1173 if ((has_audio && !source_id_audio_.empty()) ||
1174 (has_video && !source_id_video_.empty()))
1175 return kReachedIdLimit;
1177 if (has_audio)
1178 source_id_audio_ = id;
1180 if (has_video)
1181 source_id_video_ = id;
1183 scoped_ptr<FrameProcessorBase> frame_processor;
1184 if (use_legacy_frame_processor) {
1185 frame_processor.reset(new LegacyFrameProcessor(
1186 base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1187 base::Unretained(this))));
1188 } else {
1189 frame_processor.reset(new FrameProcessor(
1190 base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1191 base::Unretained(this))));
1194 scoped_ptr<SourceState> source_state(
1195 new SourceState(stream_parser.Pass(),
1196 frame_processor.Pass(), log_cb_,
1197 base::Bind(&ChunkDemuxer::CreateDemuxerStream,
1198 base::Unretained(this))));
1200 SourceState::NewTextTrackCB new_text_track_cb;
1202 if (enable_text_) {
1203 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1204 base::Unretained(this));
1207 source_state->Init(
1208 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1209 has_audio,
1210 has_video,
1211 need_key_cb_,
1212 new_text_track_cb);
1214 source_state_map_[id] = source_state.release();
1215 return kOk;
1218 void ChunkDemuxer::RemoveId(const std::string& id) {
1219 base::AutoLock auto_lock(lock_);
1220 CHECK(IsValidId(id));
1222 delete source_state_map_[id];
1223 source_state_map_.erase(id);
1225 if (source_id_audio_ == id)
1226 source_id_audio_.clear();
1228 if (source_id_video_ == id)
1229 source_id_video_.clear();
1232 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1233 base::AutoLock auto_lock(lock_);
1234 DCHECK(!id.empty());
1236 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1238 DCHECK(itr != source_state_map_.end());
1239 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1242 void ChunkDemuxer::AppendData(const std::string& id,
1243 const uint8* data, size_t length,
1244 TimeDelta append_window_start,
1245 TimeDelta append_window_end,
1246 TimeDelta* timestamp_offset) {
1247 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1249 DCHECK(!id.empty());
1250 DCHECK(timestamp_offset);
1252 Ranges<TimeDelta> ranges;
1255 base::AutoLock auto_lock(lock_);
1256 DCHECK_NE(state_, ENDED);
1258 // Capture if any of the SourceBuffers are waiting for data before we start
1259 // parsing.
1260 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1262 if (length == 0u)
1263 return;
1265 DCHECK(data);
1267 switch (state_) {
1268 case INITIALIZING:
1269 DCHECK(IsValidId(id));
1270 if (!source_state_map_[id]->Append(data, length,
1271 append_window_start,
1272 append_window_end,
1273 timestamp_offset)) {
1274 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1275 return;
1277 break;
1279 case INITIALIZED: {
1280 DCHECK(IsValidId(id));
1281 if (!source_state_map_[id]->Append(data, length,
1282 append_window_start,
1283 append_window_end,
1284 timestamp_offset)) {
1285 ReportError_Locked(PIPELINE_ERROR_DECODE);
1286 return;
1288 } break;
1290 case PARSE_ERROR:
1291 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1292 return;
1294 case WAITING_FOR_INIT:
1295 case ENDED:
1296 case SHUTDOWN:
1297 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1298 return;
1301 // Check to see if data was appended at the pending seek point. This
1302 // indicates we have parsed enough data to complete the seek.
1303 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1304 !seek_cb_.is_null()) {
1305 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1308 ranges = GetBufferedRanges_Locked();
1311 for (size_t i = 0; i < ranges.size(); ++i)
1312 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1315 void ChunkDemuxer::Abort(const std::string& id,
1316 TimeDelta append_window_start,
1317 TimeDelta append_window_end,
1318 TimeDelta* timestamp_offset) {
1319 DVLOG(1) << "Abort(" << id << ")";
1320 base::AutoLock auto_lock(lock_);
1321 DCHECK(!id.empty());
1322 CHECK(IsValidId(id));
1323 source_state_map_[id]->Abort(append_window_start,
1324 append_window_end,
1325 timestamp_offset);
1328 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1329 TimeDelta end) {
1330 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1331 << ", " << end.InSecondsF() << ")";
1332 base::AutoLock auto_lock(lock_);
1334 DCHECK(!id.empty());
1335 CHECK(IsValidId(id));
1336 DCHECK(start >= base::TimeDelta()) << start.InSecondsF();
1337 DCHECK(start < end) << "start " << start.InSecondsF()
1338 << " end " << end.InSecondsF();
1339 DCHECK(duration_ != kNoTimestamp());
1340 DCHECK(start <= duration_) << "start " << start.InSecondsF()
1341 << " duration " << duration_.InSecondsF();
1343 if (start == duration_)
1344 return;
1346 source_state_map_[id]->Remove(start, end, duration_);
1349 double ChunkDemuxer::GetDuration() {
1350 base::AutoLock auto_lock(lock_);
1351 return GetDuration_Locked();
1354 double ChunkDemuxer::GetDuration_Locked() {
1355 lock_.AssertAcquired();
1356 if (duration_ == kNoTimestamp())
1357 return std::numeric_limits<double>::quiet_NaN();
1359 // Return positive infinity if the resource is unbounded.
1360 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1361 if (duration_ == kInfiniteDuration())
1362 return std::numeric_limits<double>::infinity();
1364 if (user_specified_duration_ >= 0)
1365 return user_specified_duration_;
1367 return duration_.InSecondsF();
1370 void ChunkDemuxer::SetDuration(double duration) {
1371 base::AutoLock auto_lock(lock_);
1372 DVLOG(1) << "SetDuration(" << duration << ")";
1373 DCHECK_GE(duration, 0);
1375 if (duration == GetDuration_Locked())
1376 return;
1378 // Compute & bounds check the TimeDelta representation of duration.
1379 // This can be different if the value of |duration| doesn't fit the range or
1380 // precision of TimeDelta.
1381 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1382 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1383 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1384 double min_duration_in_seconds = min_duration.InSecondsF();
1385 double max_duration_in_seconds = max_duration.InSecondsF();
1387 TimeDelta duration_td;
1388 if (duration == std::numeric_limits<double>::infinity()) {
1389 duration_td = media::kInfiniteDuration();
1390 } else if (duration < min_duration_in_seconds) {
1391 duration_td = min_duration;
1392 } else if (duration > max_duration_in_seconds) {
1393 duration_td = max_duration;
1394 } else {
1395 duration_td = TimeDelta::FromMicroseconds(
1396 duration * base::Time::kMicrosecondsPerSecond);
1399 DCHECK(duration_td > TimeDelta());
1401 user_specified_duration_ = duration;
1402 duration_ = duration_td;
1403 host_->SetDuration(duration_);
1405 for (SourceStateMap::iterator itr = source_state_map_.begin();
1406 itr != source_state_map_.end(); ++itr) {
1407 itr->second->OnSetDuration(duration_);
1411 bool ChunkDemuxer::IsParsingMediaSegment(const std::string& id) {
1412 base::AutoLock auto_lock(lock_);
1413 DVLOG(1) << "IsParsingMediaSegment(" << id << ")";
1414 CHECK(IsValidId(id));
1416 return source_state_map_[id]->parsing_media_segment();
1419 void ChunkDemuxer::SetSequenceMode(const std::string& id,
1420 bool sequence_mode) {
1421 base::AutoLock auto_lock(lock_);
1422 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1423 CHECK(IsValidId(id));
1424 DCHECK_NE(state_, ENDED);
1426 source_state_map_[id]->SetSequenceMode(sequence_mode);
1429 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1430 const std::string& id,
1431 base::TimeDelta timestamp_offset) {
1432 base::AutoLock auto_lock(lock_);
1433 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id << ", "
1434 << timestamp_offset.InSecondsF() << ")";
1435 CHECK(IsValidId(id));
1436 DCHECK_NE(state_, ENDED);
1438 source_state_map_[id]->SetGroupStartTimestampIfInSequenceMode(
1439 timestamp_offset);
1443 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1444 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1445 base::AutoLock auto_lock(lock_);
1446 DCHECK_NE(state_, WAITING_FOR_INIT);
1447 DCHECK_NE(state_, ENDED);
1449 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1450 return;
1452 if (state_ == INITIALIZING) {
1453 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1454 return;
1457 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1458 for (SourceStateMap::iterator itr = source_state_map_.begin();
1459 itr != source_state_map_.end(); ++itr) {
1460 itr->second->MarkEndOfStream();
1463 CompletePendingReadsIfPossible();
1465 // Give a chance to resume the pending seek process.
1466 if (status != PIPELINE_OK) {
1467 ReportError_Locked(status);
1468 return;
1471 ChangeState_Locked(ENDED);
1472 DecreaseDurationIfNecessary();
1474 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1475 !seek_cb_.is_null()) {
1476 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1480 void ChunkDemuxer::UnmarkEndOfStream() {
1481 DVLOG(1) << "UnmarkEndOfStream()";
1482 base::AutoLock auto_lock(lock_);
1483 DCHECK_EQ(state_, ENDED);
1485 ChangeState_Locked(INITIALIZED);
1487 for (SourceStateMap::iterator itr = source_state_map_.begin();
1488 itr != source_state_map_.end(); ++itr) {
1489 itr->second->UnmarkEndOfStream();
1493 void ChunkDemuxer::Shutdown() {
1494 DVLOG(1) << "Shutdown()";
1495 base::AutoLock auto_lock(lock_);
1497 if (state_ == SHUTDOWN)
1498 return;
1500 ShutdownAllStreams();
1502 ChangeState_Locked(SHUTDOWN);
1504 if(!seek_cb_.is_null())
1505 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1508 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit) {
1509 for (SourceStateMap::iterator itr = source_state_map_.begin();
1510 itr != source_state_map_.end(); ++itr) {
1511 itr->second->SetMemoryLimitsForTesting(memory_limit);
1515 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1516 lock_.AssertAcquired();
1517 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1518 << state_ << " -> " << new_state;
1519 state_ = new_state;
1522 ChunkDemuxer::~ChunkDemuxer() {
1523 DCHECK_NE(state_, INITIALIZED);
1525 STLDeleteValues(&source_state_map_);
1528 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1529 DVLOG(1) << "ReportError_Locked(" << error << ")";
1530 lock_.AssertAcquired();
1531 DCHECK_NE(error, PIPELINE_OK);
1533 ChangeState_Locked(PARSE_ERROR);
1535 PipelineStatusCB cb;
1537 if (!init_cb_.is_null()) {
1538 std::swap(cb, init_cb_);
1539 } else {
1540 if (!seek_cb_.is_null())
1541 std::swap(cb, seek_cb_);
1543 ShutdownAllStreams();
1546 if (!cb.is_null()) {
1547 cb.Run(error);
1548 return;
1551 base::AutoUnlock auto_unlock(lock_);
1552 host_->OnDemuxerError(error);
1555 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1556 lock_.AssertAcquired();
1557 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1558 itr != source_state_map_.end(); ++itr) {
1559 if (itr->second->IsSeekWaitingForData())
1560 return true;
1563 return false;
1566 void ChunkDemuxer::OnSourceInitDone(
1567 bool success,
1568 const StreamParser::InitParameters& params) {
1569 DVLOG(1) << "OnSourceInitDone(" << success << ", "
1570 << params.duration.InSecondsF() << ")";
1571 lock_.AssertAcquired();
1572 DCHECK_EQ(state_, INITIALIZING);
1573 if (!success || (!audio_ && !video_)) {
1574 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1575 return;
1578 if (params.duration != TimeDelta() && duration_ == kNoTimestamp())
1579 UpdateDuration(params.duration);
1581 if (!params.timeline_offset.is_null()) {
1582 if (!timeline_offset_.is_null() &&
1583 params.timeline_offset != timeline_offset_) {
1584 MEDIA_LOG(log_cb_)
1585 << "Timeline offset is not the same across all SourceBuffers.";
1586 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1587 return;
1590 timeline_offset_ = params.timeline_offset;
1593 if (params.liveness != LIVENESS_UNKNOWN) {
1594 if (liveness_ != LIVENESS_UNKNOWN && params.liveness != liveness_) {
1595 MEDIA_LOG(log_cb_)
1596 << "Liveness is not the same across all SourceBuffers.";
1597 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1598 return;
1601 liveness_ = params.liveness;
1604 // Wait until all streams have initialized.
1605 if ((!source_id_audio_.empty() && !audio_) ||
1606 (!source_id_video_.empty() && !video_)) {
1607 return;
1610 SeekAllSources(GetStartTime());
1611 StartReturningData();
1613 if (duration_ == kNoTimestamp())
1614 duration_ = kInfiniteDuration();
1616 // The demuxer is now initialized after the |start_timestamp_| was set.
1617 ChangeState_Locked(INITIALIZED);
1618 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1621 ChunkDemuxerStream*
1622 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1623 switch (type) {
1624 case DemuxerStream::AUDIO:
1625 if (audio_)
1626 return NULL;
1627 audio_.reset(
1628 new ChunkDemuxerStream(DemuxerStream::AUDIO, splice_frames_enabled_));
1629 return audio_.get();
1630 break;
1631 case DemuxerStream::VIDEO:
1632 if (video_)
1633 return NULL;
1634 video_.reset(
1635 new ChunkDemuxerStream(DemuxerStream::VIDEO, splice_frames_enabled_));
1636 return video_.get();
1637 break;
1638 case DemuxerStream::TEXT: {
1639 return new ChunkDemuxerStream(DemuxerStream::TEXT,
1640 splice_frames_enabled_);
1641 break;
1643 case DemuxerStream::UNKNOWN:
1644 case DemuxerStream::NUM_TYPES:
1645 NOTREACHED();
1646 return NULL;
1648 NOTREACHED();
1649 return NULL;
1652 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1653 const TextTrackConfig& config) {
1654 lock_.AssertAcquired();
1655 DCHECK_NE(state_, SHUTDOWN);
1656 host_->AddTextStream(text_stream, config);
1659 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1660 lock_.AssertAcquired();
1661 return source_state_map_.count(source_id) > 0u;
1664 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1665 DCHECK(duration_ != new_duration);
1666 user_specified_duration_ = -1;
1667 duration_ = new_duration;
1668 host_->SetDuration(new_duration);
1671 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration) {
1672 DCHECK(new_duration != kNoTimestamp());
1673 DCHECK(new_duration != kInfiniteDuration());
1675 // Per April 1, 2014 MSE spec editor's draft:
1676 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1677 // media-source.html#sourcebuffer-coded-frame-processing
1678 // 5. If the media segment contains data beyond the current duration, then run
1679 // the duration change algorithm with new duration set to the maximum of
1680 // the current duration and the group end timestamp.
1682 if (new_duration <= duration_)
1683 return;
1685 DVLOG(2) << __FUNCTION__ << ": Increasing duration: "
1686 << duration_.InSecondsF() << " -> " << new_duration.InSecondsF();
1688 UpdateDuration(new_duration);
1691 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1692 lock_.AssertAcquired();
1694 TimeDelta max_duration;
1696 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1697 itr != source_state_map_.end(); ++itr) {
1698 max_duration = std::max(max_duration,
1699 itr->second->GetMaxBufferedDuration());
1702 if (max_duration == TimeDelta())
1703 return;
1705 if (max_duration < duration_)
1706 UpdateDuration(max_duration);
1709 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1710 base::AutoLock auto_lock(lock_);
1711 return GetBufferedRanges_Locked();
1714 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1715 lock_.AssertAcquired();
1717 bool ended = state_ == ENDED;
1718 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1719 // we'll need to update this loop to only add ranges from active sources.
1720 RangesList ranges_list;
1721 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1722 itr != source_state_map_.end(); ++itr) {
1723 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1726 return ComputeIntersection(ranges_list, ended);
1729 void ChunkDemuxer::StartReturningData() {
1730 for (SourceStateMap::iterator itr = source_state_map_.begin();
1731 itr != source_state_map_.end(); ++itr) {
1732 itr->second->StartReturningData();
1736 void ChunkDemuxer::AbortPendingReads() {
1737 for (SourceStateMap::iterator itr = source_state_map_.begin();
1738 itr != source_state_map_.end(); ++itr) {
1739 itr->second->AbortReads();
1743 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1744 for (SourceStateMap::iterator itr = source_state_map_.begin();
1745 itr != source_state_map_.end(); ++itr) {
1746 itr->second->Seek(seek_time);
1750 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1751 for (SourceStateMap::iterator itr = source_state_map_.begin();
1752 itr != source_state_map_.end(); ++itr) {
1753 itr->second->CompletePendingReadIfPossible();
1757 void ChunkDemuxer::ShutdownAllStreams() {
1758 for (SourceStateMap::iterator itr = source_state_map_.begin();
1759 itr != source_state_map_.end(); ++itr) {
1760 itr->second->Shutdown();
1764 } // namespace media