Supervised user import: Listen for profile creation/deletion
[chromium-blink-merge.git] / media / filters / chunk_demuxer.cc
blobd641ded72c00e74a3fd48b9805c1e5f319cf9efa
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/stream_parser_factory.h"
23 using base::TimeDelta;
25 namespace media {
27 static TimeDelta EndTimestamp(const StreamParser::BufferQueue& queue) {
28 return queue.back()->timestamp() + queue.back()->duration();
31 // List of time ranges for each SourceBuffer.
32 typedef std::list<Ranges<TimeDelta> > RangesList;
33 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
34 bool ended) {
35 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
36 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
38 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
39 // TimeRanges object and abort these steps.
40 if (activeRanges.empty())
41 return Ranges<TimeDelta>();
43 // Step 2: Let active ranges be the ranges returned by buffered for each
44 // SourceBuffer object in activeSourceBuffers.
45 // Step 3: Let highest end time be the largest range end time in the active
46 // ranges.
47 TimeDelta highest_end_time;
48 for (RangesList::const_iterator itr = activeRanges.begin();
49 itr != activeRanges.end(); ++itr) {
50 if (!itr->size())
51 continue;
53 highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
56 // Step 4: Let intersection ranges equal a TimeRange object containing a
57 // single range from 0 to highest end time.
58 Ranges<TimeDelta> intersection_ranges;
59 intersection_ranges.Add(TimeDelta(), highest_end_time);
61 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
62 // following steps:
63 for (RangesList::const_iterator itr = activeRanges.begin();
64 itr != activeRanges.end(); ++itr) {
65 // Step 5.1: Let source ranges equal the ranges returned by the buffered
66 // attribute on the current SourceBuffer.
67 Ranges<TimeDelta> source_ranges = *itr;
69 // Step 5.2: If readyState is "ended", then set the end time on the last
70 // range in source ranges to highest end time.
71 if (ended && source_ranges.size() > 0u) {
72 source_ranges.Add(source_ranges.start(source_ranges.size() - 1),
73 highest_end_time);
76 // Step 5.3: Let new intersection ranges equal the intersection between
77 // the intersection ranges and the source ranges.
78 // Step 5.4: Replace the ranges in intersection ranges with the new
79 // intersection ranges.
80 intersection_ranges = intersection_ranges.IntersectionWith(source_ranges);
83 return intersection_ranges;
86 // Contains state belonging to a source id.
87 class SourceState {
88 public:
89 // Callback signature used to create ChunkDemuxerStreams.
90 typedef base::Callback<ChunkDemuxerStream*(
91 DemuxerStream::Type)> CreateDemuxerStreamCB;
93 typedef ChunkDemuxer::InitSegmentReceivedCB InitSegmentReceivedCB;
95 typedef base::Callback<void(
96 ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
98 SourceState(
99 scoped_ptr<StreamParser> stream_parser,
100 scoped_ptr<FrameProcessor> frame_processor, const LogCB& log_cb,
101 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
102 const scoped_refptr<MediaLog>& media_log);
104 ~SourceState();
106 void Init(const StreamParser::InitCB& init_cb,
107 bool allow_audio,
108 bool allow_video,
109 const StreamParser::EncryptedMediaInitDataCB&
110 encrypted_media_init_data_cb,
111 const NewTextTrackCB& new_text_track_cb);
113 // Appends new data to the StreamParser.
114 // Returns true if the data was successfully appended. Returns false if an
115 // error occurred. |*timestamp_offset| is used and possibly updated by the
116 // append. |append_window_start| and |append_window_end| correspond to the MSE
117 // spec's similarly named source buffer attributes that are used in coded
118 // frame processing. |init_segment_received_cb| is run for each new fully
119 // parsed initialization segment.
120 bool Append(const uint8* data,
121 size_t length,
122 TimeDelta append_window_start,
123 TimeDelta append_window_end,
124 TimeDelta* timestamp_offset,
125 const InitSegmentReceivedCB& init_segment_received_cb);
127 // Aborts the current append sequence and resets the parser.
128 void Abort(TimeDelta append_window_start,
129 TimeDelta append_window_end,
130 TimeDelta* timestamp_offset);
132 // Calls Remove(|start|, |end|, |duration|) on all
133 // ChunkDemuxerStreams managed by this object.
134 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
136 // Returns true if currently parsing a media segment, or false otherwise.
137 bool parsing_media_segment() const { return parsing_media_segment_; }
139 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
140 void SetSequenceMode(bool sequence_mode);
142 // Signals the coded frame processor to update its group start timestamp to be
143 // |timestamp_offset| if it is in sequence append mode.
144 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset);
146 // Returns the range of buffered data in this source, capped at |duration|.
147 // |ended| - Set to true if end of stream has been signaled and the special
148 // end of stream range logic needs to be executed.
149 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
151 // Returns the highest buffered duration across all streams managed
152 // by this object.
153 // Returns TimeDelta() if none of the streams contain buffered data.
154 TimeDelta GetMaxBufferedDuration() const;
156 // Helper methods that call methods with similar names on all the
157 // ChunkDemuxerStreams managed by this object.
158 void StartReturningData();
159 void AbortReads();
160 void Seek(TimeDelta seek_time);
161 void CompletePendingReadIfPossible();
162 void OnSetDuration(TimeDelta duration);
163 void MarkEndOfStream();
164 void UnmarkEndOfStream();
165 void Shutdown();
166 // Sets the memory limit on each stream of a specific type.
167 // |memory_limit| is the maximum number of bytes each stream of type |type|
168 // is allowed to hold in its buffer.
169 void SetMemoryLimits(DemuxerStream::Type type, int memory_limit);
170 bool IsSeekWaitingForData() const;
172 private:
173 // Called by the |stream_parser_| when a new initialization segment is
174 // encountered.
175 // Returns true on a successful call. Returns false if an error occurred while
176 // processing decoder configurations.
177 bool OnNewConfigs(bool allow_audio, bool allow_video,
178 const AudioDecoderConfig& audio_config,
179 const VideoDecoderConfig& video_config,
180 const StreamParser::TextTrackConfigMap& text_configs);
182 // Called by the |stream_parser_| at the beginning of a new media segment.
183 void OnNewMediaSegment();
185 // Called by the |stream_parser_| at the end of a media segment.
186 void OnEndOfMediaSegment();
188 // Called by the |stream_parser_| when new buffers have been parsed.
189 // It processes the new buffers using |frame_processor_|, which includes
190 // appending the processed frames to associated demuxer streams for each
191 // frame's track.
192 // Returns true on a successful call. Returns false if an error occurred while
193 // processing the buffers.
194 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
195 const StreamParser::BufferQueue& video_buffers,
196 const StreamParser::TextBufferQueueMap& text_map);
198 void OnSourceInitDone(const StreamParser::InitParameters& params);
200 CreateDemuxerStreamCB create_demuxer_stream_cb_;
201 NewTextTrackCB new_text_track_cb_;
203 // During Append(), if OnNewBuffers() coded frame processing updates the
204 // timestamp offset then |*timestamp_offset_during_append_| is also updated
205 // so Append()'s caller can know the new offset. This pointer is only non-NULL
206 // during the lifetime of an Append() call.
207 TimeDelta* timestamp_offset_during_append_;
209 // During Append(), coded frame processing triggered by OnNewBuffers()
210 // requires these two attributes. These are only valid during the lifetime of
211 // an Append() call.
212 TimeDelta append_window_start_during_append_;
213 TimeDelta append_window_end_during_append_;
215 // Set to true if the next buffers appended within the append window
216 // represent the start of a new media segment. This flag being set
217 // triggers a call to |new_segment_cb_| when the new buffers are
218 // appended. The flag is set on actual media segment boundaries and
219 // when the "append window" filtering causes discontinuities in the
220 // appended data.
221 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
222 // processing's discontinuity logic is enough. See http://crbug.com/351489.
223 bool new_media_segment_;
225 // Keeps track of whether a media segment is being parsed.
226 bool parsing_media_segment_;
228 // The object used to parse appended data.
229 scoped_ptr<StreamParser> stream_parser_;
231 ChunkDemuxerStream* audio_; // Not owned by |this|.
232 ChunkDemuxerStream* video_; // Not owned by |this|.
234 typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
235 TextStreamMap text_stream_map_; // |this| owns the map's stream pointers.
237 scoped_ptr<FrameProcessor> frame_processor_;
238 LogCB log_cb_;
239 scoped_refptr<MediaLog> media_log_;
240 StreamParser::InitCB init_cb_;
242 // During Append(), OnNewConfigs() will trigger the initialization segment
243 // received algorithm. This callback is only non-NULL during the lifetime of
244 // an Append() call. Note, the MSE spec explicitly disallows this algorithm
245 // during an Abort(), since Abort() is allowed only to emit coded frames, and
246 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
247 InitSegmentReceivedCB init_segment_received_cb_;
249 // Indicates that timestampOffset should be updated automatically during
250 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
251 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
252 // changes to MSE spec. See http://crbug.com/371499.
253 bool auto_update_timestamp_offset_;
255 DISALLOW_COPY_AND_ASSIGN(SourceState);
258 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
259 scoped_ptr<FrameProcessor> frame_processor,
260 const LogCB& log_cb,
261 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
262 const scoped_refptr<MediaLog>& media_log)
263 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
264 timestamp_offset_during_append_(NULL),
265 new_media_segment_(false),
266 parsing_media_segment_(false),
267 stream_parser_(stream_parser.release()),
268 audio_(NULL),
269 video_(NULL),
270 frame_processor_(frame_processor.release()),
271 log_cb_(log_cb),
272 media_log_(media_log),
273 auto_update_timestamp_offset_(false) {
274 DCHECK(!create_demuxer_stream_cb_.is_null());
275 DCHECK(frame_processor_);
278 SourceState::~SourceState() {
279 Shutdown();
281 STLDeleteValues(&text_stream_map_);
284 void SourceState::Init(
285 const StreamParser::InitCB& init_cb,
286 bool allow_audio,
287 bool allow_video,
288 const StreamParser::EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
289 const NewTextTrackCB& new_text_track_cb) {
290 new_text_track_cb_ = new_text_track_cb;
291 init_cb_ = init_cb;
293 stream_parser_->Init(
294 base::Bind(&SourceState::OnSourceInitDone, base::Unretained(this)),
295 base::Bind(&SourceState::OnNewConfigs, base::Unretained(this),
296 allow_audio, allow_video),
297 base::Bind(&SourceState::OnNewBuffers, base::Unretained(this)),
298 new_text_track_cb_.is_null(), encrypted_media_init_data_cb,
299 base::Bind(&SourceState::OnNewMediaSegment, base::Unretained(this)),
300 base::Bind(&SourceState::OnEndOfMediaSegment, base::Unretained(this)),
301 log_cb_);
304 void SourceState::SetSequenceMode(bool sequence_mode) {
305 DCHECK(!parsing_media_segment_);
307 frame_processor_->SetSequenceMode(sequence_mode);
310 void SourceState::SetGroupStartTimestampIfInSequenceMode(
311 base::TimeDelta timestamp_offset) {
312 DCHECK(!parsing_media_segment_);
314 frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
317 bool SourceState::Append(
318 const uint8* data,
319 size_t length,
320 TimeDelta append_window_start,
321 TimeDelta append_window_end,
322 TimeDelta* timestamp_offset,
323 const InitSegmentReceivedCB& init_segment_received_cb) {
324 DCHECK(timestamp_offset);
325 DCHECK(!timestamp_offset_during_append_);
326 DCHECK(!init_segment_received_cb.is_null());
327 DCHECK(init_segment_received_cb_.is_null());
328 append_window_start_during_append_ = append_window_start;
329 append_window_end_during_append_ = append_window_end;
330 timestamp_offset_during_append_ = timestamp_offset;
331 init_segment_received_cb_= init_segment_received_cb;
333 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
334 // append window and timestamp offset pointer. See http://crbug.com/351454.
335 bool result = stream_parser_->Parse(data, length);
336 if (!result) {
337 MEDIA_LOG(ERROR, log_cb_)
338 << __FUNCTION__ << ": stream parsing failed."
339 << " Data size=" << length
340 << " append_window_start=" << append_window_start.InSecondsF()
341 << " append_window_end=" << append_window_end.InSecondsF();
343 timestamp_offset_during_append_ = NULL;
344 init_segment_received_cb_.Reset();
345 return result;
348 void SourceState::Abort(TimeDelta append_window_start,
349 TimeDelta append_window_end,
350 base::TimeDelta* timestamp_offset) {
351 DCHECK(timestamp_offset);
352 DCHECK(!timestamp_offset_during_append_);
353 timestamp_offset_during_append_ = timestamp_offset;
354 append_window_start_during_append_ = append_window_start;
355 append_window_end_during_append_ = append_window_end;
357 stream_parser_->Flush();
358 timestamp_offset_during_append_ = NULL;
360 frame_processor_->Reset();
361 parsing_media_segment_ = false;
364 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
365 if (audio_)
366 audio_->Remove(start, end, duration);
368 if (video_)
369 video_->Remove(start, end, duration);
371 for (TextStreamMap::iterator itr = text_stream_map_.begin();
372 itr != text_stream_map_.end(); ++itr) {
373 itr->second->Remove(start, end, duration);
377 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
378 bool ended) const {
379 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
380 // this code to only add ranges from active tracks.
381 RangesList ranges_list;
382 if (audio_)
383 ranges_list.push_back(audio_->GetBufferedRanges(duration));
385 if (video_)
386 ranges_list.push_back(video_->GetBufferedRanges(duration));
388 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
389 itr != text_stream_map_.end(); ++itr) {
390 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
393 return ComputeIntersection(ranges_list, ended);
396 TimeDelta SourceState::GetMaxBufferedDuration() const {
397 TimeDelta max_duration;
399 if (audio_)
400 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
402 if (video_)
403 max_duration = std::max(max_duration, video_->GetBufferedDuration());
405 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
406 itr != text_stream_map_.end(); ++itr) {
407 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
410 return max_duration;
413 void SourceState::StartReturningData() {
414 if (audio_)
415 audio_->StartReturningData();
417 if (video_)
418 video_->StartReturningData();
420 for (TextStreamMap::iterator itr = text_stream_map_.begin();
421 itr != text_stream_map_.end(); ++itr) {
422 itr->second->StartReturningData();
426 void SourceState::AbortReads() {
427 if (audio_)
428 audio_->AbortReads();
430 if (video_)
431 video_->AbortReads();
433 for (TextStreamMap::iterator itr = text_stream_map_.begin();
434 itr != text_stream_map_.end(); ++itr) {
435 itr->second->AbortReads();
439 void SourceState::Seek(TimeDelta seek_time) {
440 if (audio_)
441 audio_->Seek(seek_time);
443 if (video_)
444 video_->Seek(seek_time);
446 for (TextStreamMap::iterator itr = text_stream_map_.begin();
447 itr != text_stream_map_.end(); ++itr) {
448 itr->second->Seek(seek_time);
452 void SourceState::CompletePendingReadIfPossible() {
453 if (audio_)
454 audio_->CompletePendingReadIfPossible();
456 if (video_)
457 video_->CompletePendingReadIfPossible();
459 for (TextStreamMap::iterator itr = text_stream_map_.begin();
460 itr != text_stream_map_.end(); ++itr) {
461 itr->second->CompletePendingReadIfPossible();
465 void SourceState::OnSetDuration(TimeDelta duration) {
466 if (audio_)
467 audio_->OnSetDuration(duration);
469 if (video_)
470 video_->OnSetDuration(duration);
472 for (TextStreamMap::iterator itr = text_stream_map_.begin();
473 itr != text_stream_map_.end(); ++itr) {
474 itr->second->OnSetDuration(duration);
478 void SourceState::MarkEndOfStream() {
479 if (audio_)
480 audio_->MarkEndOfStream();
482 if (video_)
483 video_->MarkEndOfStream();
485 for (TextStreamMap::iterator itr = text_stream_map_.begin();
486 itr != text_stream_map_.end(); ++itr) {
487 itr->second->MarkEndOfStream();
491 void SourceState::UnmarkEndOfStream() {
492 if (audio_)
493 audio_->UnmarkEndOfStream();
495 if (video_)
496 video_->UnmarkEndOfStream();
498 for (TextStreamMap::iterator itr = text_stream_map_.begin();
499 itr != text_stream_map_.end(); ++itr) {
500 itr->second->UnmarkEndOfStream();
504 void SourceState::Shutdown() {
505 if (audio_)
506 audio_->Shutdown();
508 if (video_)
509 video_->Shutdown();
511 for (TextStreamMap::iterator itr = text_stream_map_.begin();
512 itr != text_stream_map_.end(); ++itr) {
513 itr->second->Shutdown();
517 void SourceState::SetMemoryLimits(DemuxerStream::Type type, int memory_limit) {
518 switch (type) {
519 case DemuxerStream::AUDIO:
520 if (audio_)
521 audio_->set_memory_limit(memory_limit);
522 break;
523 case DemuxerStream::VIDEO:
524 if (video_)
525 video_->set_memory_limit(memory_limit);
526 break;
527 case DemuxerStream::TEXT:
528 for (TextStreamMap::iterator itr = text_stream_map_.begin();
529 itr != text_stream_map_.end(); ++itr) {
530 itr->second->set_memory_limit(memory_limit);
532 break;
533 case DemuxerStream::UNKNOWN:
534 case DemuxerStream::NUM_TYPES:
535 NOTREACHED();
536 break;
540 bool SourceState::IsSeekWaitingForData() const {
541 if (audio_ && audio_->IsSeekWaitingForData())
542 return true;
544 if (video_ && video_->IsSeekWaitingForData())
545 return true;
547 // NOTE: We are intentionally not checking the text tracks
548 // because text tracks are discontinuous and may not have data
549 // for the seek position. This is ok and playback should not be
550 // stalled because we don't have cues. If cues, with timestamps after
551 // the seek time, eventually arrive they will be delivered properly
552 // in response to ChunkDemuxerStream::Read() calls.
554 return false;
557 bool SourceState::OnNewConfigs(
558 bool allow_audio, bool allow_video,
559 const AudioDecoderConfig& audio_config,
560 const VideoDecoderConfig& video_config,
561 const StreamParser::TextTrackConfigMap& text_configs) {
562 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
563 << ", " << audio_config.IsValidConfig()
564 << ", " << video_config.IsValidConfig() << ")";
565 DCHECK(!init_segment_received_cb_.is_null());
567 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
568 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
569 return false;
572 // Signal an error if we get configuration info for stream types that weren't
573 // specified in AddId() or more configs after a stream is initialized.
574 if (allow_audio != audio_config.IsValidConfig()) {
575 MEDIA_LOG(ERROR, log_cb_)
576 << "Initialization segment"
577 << (audio_config.IsValidConfig() ? " has" : " does not have")
578 << " an audio track, but the mimetype"
579 << (allow_audio ? " specifies" : " does not specify")
580 << " an audio codec.";
581 return false;
584 if (allow_video != video_config.IsValidConfig()) {
585 MEDIA_LOG(ERROR, log_cb_)
586 << "Initialization segment"
587 << (video_config.IsValidConfig() ? " has" : " does not have")
588 << " a video track, but the mimetype"
589 << (allow_video ? " specifies" : " does not specify")
590 << " a video codec.";
591 return false;
594 bool success = true;
595 if (audio_config.IsValidConfig()) {
596 if (!audio_) {
597 media_log_->SetBooleanProperty("found_audio_stream", true);
599 if (!audio_ ||
600 audio_->audio_decoder_config().codec() != audio_config.codec()) {
601 media_log_->SetStringProperty("audio_codec_name",
602 audio_config.GetHumanReadableCodecName());
605 if (!audio_) {
606 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
608 if (!audio_) {
609 DVLOG(1) << "Failed to create an audio stream.";
610 return false;
613 if (!frame_processor_->AddTrack(FrameProcessor::kAudioTrackId, audio_)) {
614 DVLOG(1) << "Failed to add audio track to frame processor.";
615 return false;
619 frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
620 success &= audio_->UpdateAudioConfig(audio_config, log_cb_);
623 if (video_config.IsValidConfig()) {
624 if (!video_) {
625 media_log_->SetBooleanProperty("found_video_stream", true);
627 if (!video_ ||
628 video_->video_decoder_config().codec() != video_config.codec()) {
629 media_log_->SetStringProperty("video_codec_name",
630 video_config.GetHumanReadableCodecName());
633 if (!video_) {
634 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
636 if (!video_) {
637 DVLOG(1) << "Failed to create a video stream.";
638 return false;
641 if (!frame_processor_->AddTrack(FrameProcessor::kVideoTrackId, video_)) {
642 DVLOG(1) << "Failed to add video track to frame processor.";
643 return false;
647 success &= video_->UpdateVideoConfig(video_config, log_cb_);
650 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
651 if (text_stream_map_.empty()) {
652 for (TextConfigItr itr = text_configs.begin();
653 itr != text_configs.end(); ++itr) {
654 ChunkDemuxerStream* const text_stream =
655 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
656 if (!frame_processor_->AddTrack(itr->first, text_stream)) {
657 success &= false;
658 MEDIA_LOG(ERROR, log_cb_) << "Failed to add text track ID "
659 << itr->first << " to frame processor.";
660 break;
662 text_stream->UpdateTextConfig(itr->second, log_cb_);
663 text_stream_map_[itr->first] = text_stream;
664 new_text_track_cb_.Run(text_stream, itr->second);
666 } else {
667 const size_t text_count = text_stream_map_.size();
668 if (text_configs.size() != text_count) {
669 success &= false;
670 MEDIA_LOG(ERROR, log_cb_) << "The number of text track configs changed.";
671 } else if (text_count == 1) {
672 TextConfigItr config_itr = text_configs.begin();
673 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
674 ChunkDemuxerStream* text_stream = stream_itr->second;
675 TextTrackConfig old_config = text_stream->text_track_config();
676 TextTrackConfig new_config(config_itr->second.kind(),
677 config_itr->second.label(),
678 config_itr->second.language(),
679 old_config.id());
680 if (!new_config.Matches(old_config)) {
681 success &= false;
682 MEDIA_LOG(ERROR, log_cb_)
683 << "New text track config does not match old one.";
684 } else {
685 StreamParser::TrackId old_id = stream_itr->first;
686 StreamParser::TrackId new_id = config_itr->first;
687 if (new_id != old_id) {
688 if (frame_processor_->UpdateTrack(old_id, new_id)) {
689 text_stream_map_.clear();
690 text_stream_map_[config_itr->first] = text_stream;
691 } else {
692 success &= false;
693 MEDIA_LOG(ERROR, log_cb_)
694 << "Error remapping single text track number";
698 } else {
699 for (TextConfigItr config_itr = text_configs.begin();
700 config_itr != text_configs.end(); ++config_itr) {
701 TextStreamMap::iterator stream_itr =
702 text_stream_map_.find(config_itr->first);
703 if (stream_itr == text_stream_map_.end()) {
704 success &= false;
705 MEDIA_LOG(ERROR, log_cb_)
706 << "Unexpected text track configuration for track ID "
707 << config_itr->first;
708 break;
711 const TextTrackConfig& new_config = config_itr->second;
712 ChunkDemuxerStream* stream = stream_itr->second;
713 TextTrackConfig old_config = stream->text_track_config();
714 if (!new_config.Matches(old_config)) {
715 success &= false;
716 MEDIA_LOG(ERROR, log_cb_) << "New text track config for track ID "
717 << config_itr->first
718 << " does not match old one.";
719 break;
725 frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
727 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
728 if (success)
729 init_segment_received_cb_.Run();
731 return success;
734 void SourceState::OnNewMediaSegment() {
735 DVLOG(2) << "OnNewMediaSegment()";
736 parsing_media_segment_ = true;
737 new_media_segment_ = true;
740 void SourceState::OnEndOfMediaSegment() {
741 DVLOG(2) << "OnEndOfMediaSegment()";
742 parsing_media_segment_ = false;
743 new_media_segment_ = false;
746 bool SourceState::OnNewBuffers(
747 const StreamParser::BufferQueue& audio_buffers,
748 const StreamParser::BufferQueue& video_buffers,
749 const StreamParser::TextBufferQueueMap& text_map) {
750 DVLOG(2) << "OnNewBuffers()";
751 DCHECK(timestamp_offset_during_append_);
752 DCHECK(parsing_media_segment_);
754 const TimeDelta timestamp_offset_before_processing =
755 *timestamp_offset_during_append_;
757 // Calculate the new timestamp offset for audio/video tracks if the stream
758 // parser has requested automatic updates.
759 TimeDelta new_timestamp_offset = timestamp_offset_before_processing;
760 if (auto_update_timestamp_offset_) {
761 const bool have_audio_buffers = !audio_buffers.empty();
762 const bool have_video_buffers = !video_buffers.empty();
763 if (have_audio_buffers && have_video_buffers) {
764 new_timestamp_offset +=
765 std::min(EndTimestamp(audio_buffers), EndTimestamp(video_buffers));
766 } else if (have_audio_buffers) {
767 new_timestamp_offset += EndTimestamp(audio_buffers);
768 } else if (have_video_buffers) {
769 new_timestamp_offset += EndTimestamp(video_buffers);
773 if (!frame_processor_->ProcessFrames(audio_buffers,
774 video_buffers,
775 text_map,
776 append_window_start_during_append_,
777 append_window_end_during_append_,
778 &new_media_segment_,
779 timestamp_offset_during_append_)) {
780 return false;
783 // Only update the timestamp offset if the frame processor hasn't already.
784 if (auto_update_timestamp_offset_ &&
785 timestamp_offset_before_processing == *timestamp_offset_during_append_) {
786 *timestamp_offset_during_append_ = new_timestamp_offset;
789 return true;
792 void SourceState::OnSourceInitDone(const StreamParser::InitParameters& params) {
793 auto_update_timestamp_offset_ = params.auto_update_timestamp_offset;
794 base::ResetAndReturn(&init_cb_).Run(params);
797 ChunkDemuxerStream::ChunkDemuxerStream(Type type,
798 Liveness liveness,
799 bool splice_frames_enabled)
800 : type_(type),
801 liveness_(liveness),
802 state_(UNINITIALIZED),
803 splice_frames_enabled_(splice_frames_enabled),
804 partial_append_window_trimming_enabled_(false) {
807 void ChunkDemuxerStream::StartReturningData() {
808 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
809 base::AutoLock auto_lock(lock_);
810 DCHECK(read_cb_.is_null());
811 ChangeState_Locked(RETURNING_DATA_FOR_READS);
814 void ChunkDemuxerStream::AbortReads() {
815 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
816 base::AutoLock auto_lock(lock_);
817 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
818 if (!read_cb_.is_null())
819 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
822 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
823 base::AutoLock auto_lock(lock_);
824 if (read_cb_.is_null())
825 return;
827 CompletePendingReadIfPossible_Locked();
830 void ChunkDemuxerStream::Shutdown() {
831 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
832 base::AutoLock auto_lock(lock_);
833 ChangeState_Locked(SHUTDOWN);
835 // Pass an end of stream buffer to the pending callback to signal that no more
836 // data will be sent.
837 if (!read_cb_.is_null()) {
838 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
839 StreamParserBuffer::CreateEOSBuffer());
843 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
844 base::AutoLock auto_lock(lock_);
846 // This method should not be called for text tracks. See the note in
847 // SourceState::IsSeekWaitingForData().
848 DCHECK_NE(type_, DemuxerStream::TEXT);
850 return stream_->IsSeekPending();
853 void ChunkDemuxerStream::Seek(TimeDelta time) {
854 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
855 base::AutoLock auto_lock(lock_);
856 DCHECK(read_cb_.is_null());
857 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
858 << state_;
860 stream_->Seek(time);
863 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
864 if (buffers.empty())
865 return false;
867 base::AutoLock auto_lock(lock_);
868 DCHECK_NE(state_, SHUTDOWN);
869 if (!stream_->Append(buffers)) {
870 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
871 return false;
874 if (!read_cb_.is_null())
875 CompletePendingReadIfPossible_Locked();
877 return true;
880 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
881 TimeDelta duration) {
882 base::AutoLock auto_lock(lock_);
883 stream_->Remove(start, end, duration);
886 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
887 base::AutoLock auto_lock(lock_);
888 stream_->OnSetDuration(duration);
891 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
892 TimeDelta duration) const {
893 base::AutoLock auto_lock(lock_);
895 if (type_ == TEXT) {
896 // Since text tracks are discontinuous and the lack of cues should not block
897 // playback, report the buffered range for text tracks as [0, |duration|) so
898 // that intesections with audio & video tracks are computed correctly when
899 // no cues are present.
900 Ranges<TimeDelta> text_range;
901 text_range.Add(TimeDelta(), duration);
902 return text_range;
905 Ranges<TimeDelta> range = stream_->GetBufferedTime();
907 if (range.size() == 0u)
908 return range;
910 // Clamp the end of the stream's buffered ranges to fit within the duration.
911 // This can be done by intersecting the stream's range with the valid time
912 // range.
913 Ranges<TimeDelta> valid_time_range;
914 valid_time_range.Add(range.start(0), duration);
915 return range.IntersectionWith(valid_time_range);
918 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
919 return stream_->GetBufferedDuration();
922 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp) {
923 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
924 << start_timestamp.InSecondsF() << ")";
925 base::AutoLock auto_lock(lock_);
926 stream_->OnNewMediaSegment(start_timestamp);
929 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig& config,
930 const LogCB& log_cb) {
931 DCHECK(config.IsValidConfig());
932 DCHECK_EQ(type_, AUDIO);
933 base::AutoLock auto_lock(lock_);
934 if (!stream_) {
935 DCHECK_EQ(state_, UNINITIALIZED);
937 // On platforms which support splice frames, enable splice frames and
938 // partial append window support for most codecs (notably: not opus).
939 const bool codec_supported = config.codec() == kCodecMP3 ||
940 config.codec() == kCodecAAC ||
941 config.codec() == kCodecVorbis;
942 splice_frames_enabled_ = splice_frames_enabled_ && codec_supported;
943 partial_append_window_trimming_enabled_ =
944 splice_frames_enabled_ && codec_supported;
946 stream_.reset(
947 new SourceBufferStream(config, log_cb, splice_frames_enabled_));
948 return true;
951 return stream_->UpdateAudioConfig(config);
954 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig& config,
955 const LogCB& log_cb) {
956 DCHECK(config.IsValidConfig());
957 DCHECK_EQ(type_, VIDEO);
958 base::AutoLock auto_lock(lock_);
960 if (!stream_) {
961 DCHECK_EQ(state_, UNINITIALIZED);
962 stream_.reset(
963 new SourceBufferStream(config, log_cb, splice_frames_enabled_));
964 return true;
967 return stream_->UpdateVideoConfig(config);
970 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig& config,
971 const LogCB& log_cb) {
972 DCHECK_EQ(type_, TEXT);
973 base::AutoLock auto_lock(lock_);
974 DCHECK(!stream_);
975 DCHECK_EQ(state_, UNINITIALIZED);
976 stream_.reset(new SourceBufferStream(config, log_cb, splice_frames_enabled_));
979 void ChunkDemuxerStream::MarkEndOfStream() {
980 base::AutoLock auto_lock(lock_);
981 stream_->MarkEndOfStream();
984 void ChunkDemuxerStream::UnmarkEndOfStream() {
985 base::AutoLock auto_lock(lock_);
986 stream_->UnmarkEndOfStream();
989 // DemuxerStream methods.
990 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
991 base::AutoLock auto_lock(lock_);
992 DCHECK_NE(state_, UNINITIALIZED);
993 DCHECK(read_cb_.is_null());
995 read_cb_ = BindToCurrentLoop(read_cb);
996 CompletePendingReadIfPossible_Locked();
999 DemuxerStream::Type ChunkDemuxerStream::type() const { return type_; }
1001 DemuxerStream::Liveness ChunkDemuxerStream::liveness() const {
1002 base::AutoLock auto_lock(lock_);
1003 return liveness_;
1006 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
1007 CHECK_EQ(type_, AUDIO);
1008 base::AutoLock auto_lock(lock_);
1009 return stream_->GetCurrentAudioDecoderConfig();
1012 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
1013 CHECK_EQ(type_, VIDEO);
1014 base::AutoLock auto_lock(lock_);
1015 return stream_->GetCurrentVideoDecoderConfig();
1018 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
1020 VideoRotation ChunkDemuxerStream::video_rotation() {
1021 return VIDEO_ROTATION_0;
1024 TextTrackConfig ChunkDemuxerStream::text_track_config() {
1025 CHECK_EQ(type_, TEXT);
1026 base::AutoLock auto_lock(lock_);
1027 return stream_->GetCurrentTextTrackConfig();
1030 void ChunkDemuxerStream::SetLiveness(Liveness liveness) {
1031 base::AutoLock auto_lock(lock_);
1032 liveness_ = liveness;
1035 void ChunkDemuxerStream::ChangeState_Locked(State state) {
1036 lock_.AssertAcquired();
1037 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1038 << "type " << type_
1039 << " - " << state_ << " -> " << state;
1040 state_ = state;
1043 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1045 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1046 lock_.AssertAcquired();
1047 DCHECK(!read_cb_.is_null());
1049 DemuxerStream::Status status;
1050 scoped_refptr<StreamParserBuffer> buffer;
1052 switch (state_) {
1053 case UNINITIALIZED:
1054 NOTREACHED();
1055 return;
1056 case RETURNING_DATA_FOR_READS:
1057 switch (stream_->GetNextBuffer(&buffer)) {
1058 case SourceBufferStream::kSuccess:
1059 status = DemuxerStream::kOk;
1060 DVLOG(2) << __FUNCTION__ << ": returning kOk, type " << type_
1061 << ", dts " << buffer->GetDecodeTimestamp().InSecondsF()
1062 << ", pts " << buffer->timestamp().InSecondsF()
1063 << ", dur " << buffer->duration().InSecondsF()
1064 << ", key " << buffer->is_key_frame();
1065 break;
1066 case SourceBufferStream::kNeedBuffer:
1067 // Return early without calling |read_cb_| since we don't have
1068 // any data to return yet.
1069 DVLOG(2) << __FUNCTION__ << ": returning kNeedBuffer, type "
1070 << type_;
1071 return;
1072 case SourceBufferStream::kEndOfStream:
1073 status = DemuxerStream::kOk;
1074 buffer = StreamParserBuffer::CreateEOSBuffer();
1075 DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
1076 << type_;
1077 break;
1078 case SourceBufferStream::kConfigChange:
1079 status = kConfigChanged;
1080 buffer = NULL;
1081 DVLOG(2) << __FUNCTION__ << ": returning kConfigChange, type "
1082 << type_;
1083 break;
1085 break;
1086 case RETURNING_ABORT_FOR_READS:
1087 // Null buffers should be returned in this state since we are waiting
1088 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1089 // because they are associated with the seek.
1090 status = DemuxerStream::kAborted;
1091 buffer = NULL;
1092 DVLOG(2) << __FUNCTION__ << ": returning kAborted, type " << type_;
1093 break;
1094 case SHUTDOWN:
1095 status = DemuxerStream::kOk;
1096 buffer = StreamParserBuffer::CreateEOSBuffer();
1097 DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
1098 << type_;
1099 break;
1102 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1105 ChunkDemuxer::ChunkDemuxer(
1106 const base::Closure& open_cb,
1107 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
1108 const LogCB& log_cb,
1109 const scoped_refptr<MediaLog>& media_log,
1110 bool splice_frames_enabled)
1111 : state_(WAITING_FOR_INIT),
1112 cancel_next_seek_(false),
1113 host_(NULL),
1114 open_cb_(open_cb),
1115 encrypted_media_init_data_cb_(encrypted_media_init_data_cb),
1116 enable_text_(false),
1117 log_cb_(log_cb),
1118 media_log_(media_log),
1119 duration_(kNoTimestamp()),
1120 user_specified_duration_(-1),
1121 liveness_(DemuxerStream::LIVENESS_UNKNOWN),
1122 splice_frames_enabled_(splice_frames_enabled) {
1123 DCHECK(!open_cb_.is_null());
1124 DCHECK(!encrypted_media_init_data_cb_.is_null());
1127 void ChunkDemuxer::Initialize(
1128 DemuxerHost* host,
1129 const PipelineStatusCB& cb,
1130 bool enable_text_tracks) {
1131 DVLOG(1) << "Init()";
1133 base::AutoLock auto_lock(lock_);
1135 // The |init_cb_| must only be run after this method returns, so always post.
1136 init_cb_ = BindToCurrentLoop(cb);
1137 if (state_ == SHUTDOWN) {
1138 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1139 return;
1141 DCHECK_EQ(state_, WAITING_FOR_INIT);
1142 host_ = host;
1143 enable_text_ = enable_text_tracks;
1145 ChangeState_Locked(INITIALIZING);
1147 base::ResetAndReturn(&open_cb_).Run();
1150 void ChunkDemuxer::Stop() {
1151 DVLOG(1) << "Stop()";
1152 Shutdown();
1155 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1156 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1157 DCHECK(time >= TimeDelta());
1159 base::AutoLock auto_lock(lock_);
1160 DCHECK(seek_cb_.is_null());
1162 seek_cb_ = BindToCurrentLoop(cb);
1163 if (state_ != INITIALIZED && state_ != ENDED) {
1164 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1165 return;
1168 if (cancel_next_seek_) {
1169 cancel_next_seek_ = false;
1170 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1171 return;
1174 SeekAllSources(time);
1175 StartReturningData();
1177 if (IsSeekWaitingForData_Locked()) {
1178 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1179 return;
1182 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1185 // Demuxer implementation.
1186 base::Time ChunkDemuxer::GetTimelineOffset() const {
1187 return timeline_offset_;
1190 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1191 DCHECK_NE(type, DemuxerStream::TEXT);
1192 base::AutoLock auto_lock(lock_);
1193 if (type == DemuxerStream::VIDEO)
1194 return video_.get();
1196 if (type == DemuxerStream::AUDIO)
1197 return audio_.get();
1199 return NULL;
1202 TimeDelta ChunkDemuxer::GetStartTime() const {
1203 return TimeDelta();
1206 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1207 DVLOG(1) << "StartWaitingForSeek()";
1208 base::AutoLock auto_lock(lock_);
1209 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1210 state_ == PARSE_ERROR) << state_;
1211 DCHECK(seek_cb_.is_null());
1213 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1214 return;
1216 AbortPendingReads();
1217 SeekAllSources(seek_time);
1219 // Cancel state set in CancelPendingSeek() since we want to
1220 // accept the next Seek().
1221 cancel_next_seek_ = false;
1224 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1225 base::AutoLock auto_lock(lock_);
1226 DCHECK_NE(state_, INITIALIZING);
1227 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1229 if (cancel_next_seek_)
1230 return;
1232 AbortPendingReads();
1233 SeekAllSources(seek_time);
1235 if (seek_cb_.is_null()) {
1236 cancel_next_seek_ = true;
1237 return;
1240 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1243 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1244 const std::string& type,
1245 std::vector<std::string>& codecs) {
1246 base::AutoLock auto_lock(lock_);
1248 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1249 return kReachedIdLimit;
1251 bool has_audio = false;
1252 bool has_video = false;
1253 scoped_ptr<media::StreamParser> stream_parser(
1254 StreamParserFactory::Create(type, codecs, log_cb_,
1255 &has_audio, &has_video));
1257 if (!stream_parser)
1258 return ChunkDemuxer::kNotSupported;
1260 if ((has_audio && !source_id_audio_.empty()) ||
1261 (has_video && !source_id_video_.empty()))
1262 return kReachedIdLimit;
1264 if (has_audio)
1265 source_id_audio_ = id;
1267 if (has_video)
1268 source_id_video_ = id;
1270 scoped_ptr<FrameProcessor> frame_processor(
1271 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1272 base::Unretained(this))));
1274 scoped_ptr<SourceState> source_state(
1275 new SourceState(stream_parser.Pass(),
1276 frame_processor.Pass(), log_cb_,
1277 base::Bind(&ChunkDemuxer::CreateDemuxerStream,
1278 base::Unretained(this)),
1279 media_log_));
1281 SourceState::NewTextTrackCB new_text_track_cb;
1283 if (enable_text_) {
1284 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1285 base::Unretained(this));
1288 source_state->Init(
1289 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1290 has_audio, has_video, encrypted_media_init_data_cb_, new_text_track_cb);
1292 source_state_map_[id] = source_state.release();
1293 return kOk;
1296 void ChunkDemuxer::RemoveId(const std::string& id) {
1297 base::AutoLock auto_lock(lock_);
1298 CHECK(IsValidId(id));
1300 delete source_state_map_[id];
1301 source_state_map_.erase(id);
1303 if (source_id_audio_ == id)
1304 source_id_audio_.clear();
1306 if (source_id_video_ == id)
1307 source_id_video_.clear();
1310 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1311 base::AutoLock auto_lock(lock_);
1312 DCHECK(!id.empty());
1314 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1316 DCHECK(itr != source_state_map_.end());
1317 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1320 void ChunkDemuxer::AppendData(
1321 const std::string& id,
1322 const uint8* data,
1323 size_t length,
1324 TimeDelta append_window_start,
1325 TimeDelta append_window_end,
1326 TimeDelta* timestamp_offset,
1327 const InitSegmentReceivedCB& init_segment_received_cb) {
1328 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1330 DCHECK(!id.empty());
1331 DCHECK(timestamp_offset);
1332 DCHECK(!init_segment_received_cb.is_null());
1334 Ranges<TimeDelta> ranges;
1337 base::AutoLock auto_lock(lock_);
1338 DCHECK_NE(state_, ENDED);
1340 // Capture if any of the SourceBuffers are waiting for data before we start
1341 // parsing.
1342 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1344 if (length == 0u)
1345 return;
1347 DCHECK(data);
1349 switch (state_) {
1350 case INITIALIZING:
1351 case INITIALIZED:
1352 DCHECK(IsValidId(id));
1353 if (!source_state_map_[id]->Append(data, length,
1354 append_window_start,
1355 append_window_end,
1356 timestamp_offset,
1357 init_segment_received_cb)) {
1358 ReportError_Locked(PIPELINE_ERROR_DECODE);
1359 return;
1361 break;
1363 case PARSE_ERROR:
1364 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1365 return;
1367 case WAITING_FOR_INIT:
1368 case ENDED:
1369 case SHUTDOWN:
1370 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1371 return;
1374 // Check to see if data was appended at the pending seek point. This
1375 // indicates we have parsed enough data to complete the seek.
1376 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1377 !seek_cb_.is_null()) {
1378 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1381 ranges = GetBufferedRanges_Locked();
1384 for (size_t i = 0; i < ranges.size(); ++i)
1385 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1388 void ChunkDemuxer::Abort(const std::string& id,
1389 TimeDelta append_window_start,
1390 TimeDelta append_window_end,
1391 TimeDelta* timestamp_offset) {
1392 DVLOG(1) << "Abort(" << id << ")";
1393 base::AutoLock auto_lock(lock_);
1394 DCHECK(!id.empty());
1395 CHECK(IsValidId(id));
1396 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1397 source_state_map_[id]->Abort(append_window_start,
1398 append_window_end,
1399 timestamp_offset);
1400 // Abort can possibly emit some buffers.
1401 // Need to check whether seeking can be completed.
1402 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1403 !seek_cb_.is_null()) {
1404 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1408 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1409 TimeDelta end) {
1410 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1411 << ", " << end.InSecondsF() << ")";
1412 base::AutoLock auto_lock(lock_);
1414 DCHECK(!id.empty());
1415 CHECK(IsValidId(id));
1416 DCHECK(start >= base::TimeDelta()) << start.InSecondsF();
1417 DCHECK(start < end) << "start " << start.InSecondsF()
1418 << " end " << end.InSecondsF();
1419 DCHECK(duration_ != kNoTimestamp());
1420 DCHECK(start <= duration_) << "start " << start.InSecondsF()
1421 << " duration " << duration_.InSecondsF();
1423 if (start == duration_)
1424 return;
1426 source_state_map_[id]->Remove(start, end, duration_);
1429 double ChunkDemuxer::GetDuration() {
1430 base::AutoLock auto_lock(lock_);
1431 return GetDuration_Locked();
1434 double ChunkDemuxer::GetDuration_Locked() {
1435 lock_.AssertAcquired();
1436 if (duration_ == kNoTimestamp())
1437 return std::numeric_limits<double>::quiet_NaN();
1439 // Return positive infinity if the resource is unbounded.
1440 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1441 if (duration_ == kInfiniteDuration())
1442 return std::numeric_limits<double>::infinity();
1444 if (user_specified_duration_ >= 0)
1445 return user_specified_duration_;
1447 return duration_.InSecondsF();
1450 void ChunkDemuxer::SetDuration(double duration) {
1451 base::AutoLock auto_lock(lock_);
1452 DVLOG(1) << "SetDuration(" << duration << ")";
1453 DCHECK_GE(duration, 0);
1455 if (duration == GetDuration_Locked())
1456 return;
1458 // Compute & bounds check the TimeDelta representation of duration.
1459 // This can be different if the value of |duration| doesn't fit the range or
1460 // precision of TimeDelta.
1461 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1462 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1463 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1464 double min_duration_in_seconds = min_duration.InSecondsF();
1465 double max_duration_in_seconds = max_duration.InSecondsF();
1467 TimeDelta duration_td;
1468 if (duration == std::numeric_limits<double>::infinity()) {
1469 duration_td = media::kInfiniteDuration();
1470 } else if (duration < min_duration_in_seconds) {
1471 duration_td = min_duration;
1472 } else if (duration > max_duration_in_seconds) {
1473 duration_td = max_duration;
1474 } else {
1475 duration_td = TimeDelta::FromMicroseconds(
1476 duration * base::Time::kMicrosecondsPerSecond);
1479 DCHECK(duration_td > TimeDelta());
1481 user_specified_duration_ = duration;
1482 duration_ = duration_td;
1483 host_->SetDuration(duration_);
1485 for (SourceStateMap::iterator itr = source_state_map_.begin();
1486 itr != source_state_map_.end(); ++itr) {
1487 itr->second->OnSetDuration(duration_);
1491 bool ChunkDemuxer::IsParsingMediaSegment(const std::string& id) {
1492 base::AutoLock auto_lock(lock_);
1493 DVLOG(1) << "IsParsingMediaSegment(" << id << ")";
1494 CHECK(IsValidId(id));
1496 return source_state_map_[id]->parsing_media_segment();
1499 void ChunkDemuxer::SetSequenceMode(const std::string& id,
1500 bool sequence_mode) {
1501 base::AutoLock auto_lock(lock_);
1502 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1503 CHECK(IsValidId(id));
1504 DCHECK_NE(state_, ENDED);
1506 source_state_map_[id]->SetSequenceMode(sequence_mode);
1509 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1510 const std::string& id,
1511 base::TimeDelta timestamp_offset) {
1512 base::AutoLock auto_lock(lock_);
1513 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id << ", "
1514 << timestamp_offset.InSecondsF() << ")";
1515 CHECK(IsValidId(id));
1516 DCHECK_NE(state_, ENDED);
1518 source_state_map_[id]->SetGroupStartTimestampIfInSequenceMode(
1519 timestamp_offset);
1523 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1524 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1525 base::AutoLock auto_lock(lock_);
1526 DCHECK_NE(state_, WAITING_FOR_INIT);
1527 DCHECK_NE(state_, ENDED);
1529 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1530 return;
1532 if (state_ == INITIALIZING) {
1533 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1534 return;
1537 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1538 for (SourceStateMap::iterator itr = source_state_map_.begin();
1539 itr != source_state_map_.end(); ++itr) {
1540 itr->second->MarkEndOfStream();
1543 CompletePendingReadsIfPossible();
1545 // Give a chance to resume the pending seek process.
1546 if (status != PIPELINE_OK) {
1547 ReportError_Locked(status);
1548 return;
1551 ChangeState_Locked(ENDED);
1552 DecreaseDurationIfNecessary();
1554 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1555 !seek_cb_.is_null()) {
1556 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1560 void ChunkDemuxer::UnmarkEndOfStream() {
1561 DVLOG(1) << "UnmarkEndOfStream()";
1562 base::AutoLock auto_lock(lock_);
1563 DCHECK_EQ(state_, ENDED);
1565 ChangeState_Locked(INITIALIZED);
1567 for (SourceStateMap::iterator itr = source_state_map_.begin();
1568 itr != source_state_map_.end(); ++itr) {
1569 itr->second->UnmarkEndOfStream();
1573 void ChunkDemuxer::Shutdown() {
1574 DVLOG(1) << "Shutdown()";
1575 base::AutoLock auto_lock(lock_);
1577 if (state_ == SHUTDOWN)
1578 return;
1580 ShutdownAllStreams();
1582 ChangeState_Locked(SHUTDOWN);
1584 if(!seek_cb_.is_null())
1585 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1588 void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type, int memory_limit) {
1589 for (SourceStateMap::iterator itr = source_state_map_.begin();
1590 itr != source_state_map_.end(); ++itr) {
1591 itr->second->SetMemoryLimits(type, memory_limit);
1595 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1596 lock_.AssertAcquired();
1597 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1598 << state_ << " -> " << new_state;
1599 state_ = new_state;
1602 ChunkDemuxer::~ChunkDemuxer() {
1603 DCHECK_NE(state_, INITIALIZED);
1605 STLDeleteValues(&source_state_map_);
1608 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1609 DVLOG(1) << "ReportError_Locked(" << error << ")";
1610 lock_.AssertAcquired();
1611 DCHECK_NE(error, PIPELINE_OK);
1613 ChangeState_Locked(PARSE_ERROR);
1615 PipelineStatusCB cb;
1617 if (!init_cb_.is_null()) {
1618 std::swap(cb, init_cb_);
1619 } else {
1620 if (!seek_cb_.is_null())
1621 std::swap(cb, seek_cb_);
1623 ShutdownAllStreams();
1626 if (!cb.is_null()) {
1627 cb.Run(error);
1628 return;
1631 base::AutoUnlock auto_unlock(lock_);
1632 host_->OnDemuxerError(error);
1635 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1636 lock_.AssertAcquired();
1637 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1638 itr != source_state_map_.end(); ++itr) {
1639 if (itr->second->IsSeekWaitingForData())
1640 return true;
1643 return false;
1646 void ChunkDemuxer::OnSourceInitDone(
1647 const StreamParser::InitParameters& params) {
1648 DVLOG(1) << "OnSourceInitDone(" << params.duration.InSecondsF() << ")";
1649 lock_.AssertAcquired();
1650 DCHECK_EQ(state_, INITIALIZING);
1651 if (!audio_ && !video_) {
1652 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1653 return;
1656 if (params.duration != TimeDelta() && duration_ == kNoTimestamp())
1657 UpdateDuration(params.duration);
1659 if (!params.timeline_offset.is_null()) {
1660 if (!timeline_offset_.is_null() &&
1661 params.timeline_offset != timeline_offset_) {
1662 MEDIA_LOG(ERROR, log_cb_)
1663 << "Timeline offset is not the same across all SourceBuffers.";
1664 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1665 return;
1668 timeline_offset_ = params.timeline_offset;
1671 if (params.liveness != DemuxerStream::LIVENESS_UNKNOWN) {
1672 if (liveness_ != DemuxerStream::LIVENESS_UNKNOWN &&
1673 params.liveness != liveness_) {
1674 MEDIA_LOG(ERROR, log_cb_)
1675 << "Liveness is not the same across all SourceBuffers.";
1676 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1677 return;
1680 if (liveness_ != params.liveness) {
1681 liveness_ = params.liveness;
1682 if (audio_)
1683 audio_->SetLiveness(liveness_);
1684 if (video_)
1685 video_->SetLiveness(liveness_);
1689 // Wait until all streams have initialized.
1690 if ((!source_id_audio_.empty() && !audio_) ||
1691 (!source_id_video_.empty() && !video_)) {
1692 return;
1695 SeekAllSources(GetStartTime());
1696 StartReturningData();
1698 if (duration_ == kNoTimestamp())
1699 duration_ = kInfiniteDuration();
1701 // The demuxer is now initialized after the |start_timestamp_| was set.
1702 ChangeState_Locked(INITIALIZED);
1703 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1706 ChunkDemuxerStream*
1707 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1708 switch (type) {
1709 case DemuxerStream::AUDIO:
1710 if (audio_)
1711 return NULL;
1712 audio_.reset(new ChunkDemuxerStream(DemuxerStream::AUDIO, liveness_,
1713 splice_frames_enabled_));
1714 return audio_.get();
1715 break;
1716 case DemuxerStream::VIDEO:
1717 if (video_)
1718 return NULL;
1719 video_.reset(new ChunkDemuxerStream(DemuxerStream::VIDEO, liveness_,
1720 splice_frames_enabled_));
1721 return video_.get();
1722 break;
1723 case DemuxerStream::TEXT: {
1724 return new ChunkDemuxerStream(DemuxerStream::TEXT, liveness_,
1725 splice_frames_enabled_);
1726 break;
1728 case DemuxerStream::UNKNOWN:
1729 case DemuxerStream::NUM_TYPES:
1730 NOTREACHED();
1731 return NULL;
1733 NOTREACHED();
1734 return NULL;
1737 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1738 const TextTrackConfig& config) {
1739 lock_.AssertAcquired();
1740 DCHECK_NE(state_, SHUTDOWN);
1741 host_->AddTextStream(text_stream, config);
1744 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1745 lock_.AssertAcquired();
1746 return source_state_map_.count(source_id) > 0u;
1749 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1750 DCHECK(duration_ != new_duration);
1751 user_specified_duration_ = -1;
1752 duration_ = new_duration;
1753 host_->SetDuration(new_duration);
1756 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration) {
1757 DCHECK(new_duration != kNoTimestamp());
1758 DCHECK(new_duration != kInfiniteDuration());
1760 // Per April 1, 2014 MSE spec editor's draft:
1761 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1762 // media-source.html#sourcebuffer-coded-frame-processing
1763 // 5. If the media segment contains data beyond the current duration, then run
1764 // the duration change algorithm with new duration set to the maximum of
1765 // the current duration and the group end timestamp.
1767 if (new_duration <= duration_)
1768 return;
1770 DVLOG(2) << __FUNCTION__ << ": Increasing duration: "
1771 << duration_.InSecondsF() << " -> " << new_duration.InSecondsF();
1773 UpdateDuration(new_duration);
1776 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1777 lock_.AssertAcquired();
1779 TimeDelta max_duration;
1781 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1782 itr != source_state_map_.end(); ++itr) {
1783 max_duration = std::max(max_duration,
1784 itr->second->GetMaxBufferedDuration());
1787 if (max_duration == TimeDelta())
1788 return;
1790 if (max_duration < duration_)
1791 UpdateDuration(max_duration);
1794 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1795 base::AutoLock auto_lock(lock_);
1796 return GetBufferedRanges_Locked();
1799 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1800 lock_.AssertAcquired();
1802 bool ended = state_ == ENDED;
1803 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1804 // we'll need to update this loop to only add ranges from active sources.
1805 RangesList ranges_list;
1806 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1807 itr != source_state_map_.end(); ++itr) {
1808 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1811 return ComputeIntersection(ranges_list, ended);
1814 void ChunkDemuxer::StartReturningData() {
1815 for (SourceStateMap::iterator itr = source_state_map_.begin();
1816 itr != source_state_map_.end(); ++itr) {
1817 itr->second->StartReturningData();
1821 void ChunkDemuxer::AbortPendingReads() {
1822 for (SourceStateMap::iterator itr = source_state_map_.begin();
1823 itr != source_state_map_.end(); ++itr) {
1824 itr->second->AbortReads();
1828 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1829 for (SourceStateMap::iterator itr = source_state_map_.begin();
1830 itr != source_state_map_.end(); ++itr) {
1831 itr->second->Seek(seek_time);
1835 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1836 for (SourceStateMap::iterator itr = source_state_map_.begin();
1837 itr != source_state_map_.end(); ++itr) {
1838 itr->second->CompletePendingReadIfPossible();
1842 void ChunkDemuxer::ShutdownAllStreams() {
1843 for (SourceStateMap::iterator itr = source_state_map_.begin();
1844 itr != source_state_map_.end(); ++itr) {
1845 itr->second->Shutdown();
1849 } // namespace media