Update V8 to version 4.7.42.
[chromium-blink-merge.git] / media / filters / chunk_demuxer.cc
blobc8543577c7e769c3bcfccba62bf1bde6e106099f
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/stl_util.h"
15 #include "media/base/audio_decoder_config.h"
16 #include "media/base/bind_to_current_loop.h"
17 #include "media/base/stream_parser_buffer.h"
18 #include "media/base/timestamp_constants.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 // TODO: SourceState needs to be moved to a separate file and covered with unit
88 // tests (see crbug.com/525836)
89 class SourceState {
90 public:
91 // Callback signature used to create ChunkDemuxerStreams.
92 typedef base::Callback<ChunkDemuxerStream*(
93 DemuxerStream::Type)> CreateDemuxerStreamCB;
95 typedef ChunkDemuxer::InitSegmentReceivedCB InitSegmentReceivedCB;
97 typedef base::Callback<void(
98 ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
100 SourceState(scoped_ptr<StreamParser> stream_parser,
101 scoped_ptr<FrameProcessor> frame_processor,
102 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
103 const scoped_refptr<MediaLog>& media_log);
105 ~SourceState();
107 void Init(const StreamParser::InitCB& init_cb,
108 bool allow_audio,
109 bool allow_video,
110 const StreamParser::EncryptedMediaInitDataCB&
111 encrypted_media_init_data_cb,
112 const NewTextTrackCB& new_text_track_cb);
114 // Appends new data to the StreamParser.
115 // Returns true if the data was successfully appended. Returns false if an
116 // error occurred. |*timestamp_offset| is used and possibly updated by the
117 // append. |append_window_start| and |append_window_end| correspond to the MSE
118 // spec's similarly named source buffer attributes that are used in coded
119 // frame processing. |init_segment_received_cb| is run for each new fully
120 // parsed initialization segment.
121 bool Append(const uint8* data,
122 size_t length,
123 TimeDelta append_window_start,
124 TimeDelta append_window_end,
125 TimeDelta* timestamp_offset,
126 const InitSegmentReceivedCB& init_segment_received_cb);
128 // Aborts the current append sequence and resets the parser.
129 void Abort(TimeDelta append_window_start,
130 TimeDelta append_window_end,
131 TimeDelta* timestamp_offset);
133 // Calls Remove(|start|, |end|, |duration|) on all
134 // ChunkDemuxerStreams managed by this object.
135 void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
137 // If the buffer is full, attempts to try to free up space, as specified in
138 // the "Coded Frame Eviction Algorithm" in the Media Source Extensions Spec.
139 // Returns false iff buffer is still full after running eviction.
140 // https://w3c.github.io/media-source/#sourcebuffer-coded-frame-eviction
141 bool EvictCodedFrames(DecodeTimestamp media_time, size_t newDataSize);
143 // Returns true if currently parsing a media segment, or false otherwise.
144 bool parsing_media_segment() const { return parsing_media_segment_; }
146 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
147 void SetSequenceMode(bool sequence_mode);
149 // Signals the coded frame processor to update its group start timestamp to be
150 // |timestamp_offset| if it is in sequence append mode.
151 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset);
153 // Returns the range of buffered data in this source, capped at |duration|.
154 // |ended| - Set to true if end of stream has been signaled and the special
155 // end of stream range logic needs to be executed.
156 Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
158 // Returns the highest buffered duration across all streams managed
159 // by this object.
160 // Returns TimeDelta() if none of the streams contain buffered data.
161 TimeDelta GetMaxBufferedDuration() const;
163 // Helper methods that call methods with similar names on all the
164 // ChunkDemuxerStreams managed by this object.
165 void StartReturningData();
166 void AbortReads();
167 void Seek(TimeDelta seek_time);
168 void CompletePendingReadIfPossible();
169 void OnSetDuration(TimeDelta duration);
170 void MarkEndOfStream();
171 void UnmarkEndOfStream();
172 void Shutdown();
173 // Sets the memory limit on each stream of a specific type.
174 // |memory_limit| is the maximum number of bytes each stream of type |type|
175 // is allowed to hold in its buffer.
176 void SetMemoryLimits(DemuxerStream::Type type, size_t memory_limit);
177 bool IsSeekWaitingForData() const;
179 private:
180 // Called by the |stream_parser_| when a new initialization segment is
181 // encountered.
182 // Returns true on a successful call. Returns false if an error occurred while
183 // processing decoder configurations.
184 bool OnNewConfigs(bool allow_audio, bool allow_video,
185 const AudioDecoderConfig& audio_config,
186 const VideoDecoderConfig& video_config,
187 const StreamParser::TextTrackConfigMap& text_configs);
189 // Called by the |stream_parser_| at the beginning of a new media segment.
190 void OnNewMediaSegment();
192 // Called by the |stream_parser_| at the end of a media segment.
193 void OnEndOfMediaSegment();
195 // Called by the |stream_parser_| when new buffers have been parsed.
196 // It processes the new buffers using |frame_processor_|, which includes
197 // appending the processed frames to associated demuxer streams for each
198 // frame's track.
199 // Returns true on a successful call. Returns false if an error occurred while
200 // processing the buffers.
201 bool OnNewBuffers(const StreamParser::BufferQueue& audio_buffers,
202 const StreamParser::BufferQueue& video_buffers,
203 const StreamParser::TextBufferQueueMap& text_map);
205 void OnSourceInitDone(const StreamParser::InitParameters& params);
207 // EstimateVideoDataSize uses some heuristics to estimate the size of the
208 // video size in the chunk of muxed audio/video data without parsing it.
209 // This is used by EvictCodedFrames algorithm, which happens before Append
210 // (and therefore before parsing is performed) to prepare space for new data.
211 size_t EstimateVideoDataSize(size_t muxed_data_chunk_size) const;
213 CreateDemuxerStreamCB create_demuxer_stream_cb_;
214 NewTextTrackCB new_text_track_cb_;
216 // During Append(), if OnNewBuffers() coded frame processing updates the
217 // timestamp offset then |*timestamp_offset_during_append_| is also updated
218 // so Append()'s caller can know the new offset. This pointer is only non-NULL
219 // during the lifetime of an Append() call.
220 TimeDelta* timestamp_offset_during_append_;
222 // During Append(), coded frame processing triggered by OnNewBuffers()
223 // requires these two attributes. These are only valid during the lifetime of
224 // an Append() call.
225 TimeDelta append_window_start_during_append_;
226 TimeDelta append_window_end_during_append_;
228 // Set to true if the next buffers appended within the append window
229 // represent the start of a new media segment. This flag being set
230 // triggers a call to |new_segment_cb_| when the new buffers are
231 // appended. The flag is set on actual media segment boundaries and
232 // when the "append window" filtering causes discontinuities in the
233 // appended data.
234 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
235 // processing's discontinuity logic is enough. See http://crbug.com/351489.
236 bool new_media_segment_;
238 // Keeps track of whether a media segment is being parsed.
239 bool parsing_media_segment_;
241 // The object used to parse appended data.
242 scoped_ptr<StreamParser> stream_parser_;
244 ChunkDemuxerStream* audio_; // Not owned by |this|.
245 ChunkDemuxerStream* video_; // Not owned by |this|.
247 typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
248 TextStreamMap text_stream_map_; // |this| owns the map's stream pointers.
250 scoped_ptr<FrameProcessor> frame_processor_;
251 scoped_refptr<MediaLog> media_log_;
252 StreamParser::InitCB init_cb_;
254 // During Append(), OnNewConfigs() will trigger the initialization segment
255 // received algorithm. This callback is only non-NULL during the lifetime of
256 // an Append() call. Note, the MSE spec explicitly disallows this algorithm
257 // during an Abort(), since Abort() is allowed only to emit coded frames, and
258 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
259 InitSegmentReceivedCB init_segment_received_cb_;
261 // Indicates that timestampOffset should be updated automatically during
262 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
263 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
264 // changes to MSE spec. See http://crbug.com/371499.
265 bool auto_update_timestamp_offset_;
267 DISALLOW_COPY_AND_ASSIGN(SourceState);
270 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
271 scoped_ptr<FrameProcessor> frame_processor,
272 const CreateDemuxerStreamCB& create_demuxer_stream_cb,
273 const scoped_refptr<MediaLog>& media_log)
274 : create_demuxer_stream_cb_(create_demuxer_stream_cb),
275 timestamp_offset_during_append_(NULL),
276 new_media_segment_(false),
277 parsing_media_segment_(false),
278 stream_parser_(stream_parser.release()),
279 audio_(NULL),
280 video_(NULL),
281 frame_processor_(frame_processor.release()),
282 media_log_(media_log),
283 auto_update_timestamp_offset_(false) {
284 DCHECK(!create_demuxer_stream_cb_.is_null());
285 DCHECK(frame_processor_);
288 SourceState::~SourceState() {
289 Shutdown();
291 STLDeleteValues(&text_stream_map_);
294 void SourceState::Init(
295 const StreamParser::InitCB& init_cb,
296 bool allow_audio,
297 bool allow_video,
298 const StreamParser::EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
299 const NewTextTrackCB& new_text_track_cb) {
300 new_text_track_cb_ = new_text_track_cb;
301 init_cb_ = init_cb;
303 stream_parser_->Init(
304 base::Bind(&SourceState::OnSourceInitDone, base::Unretained(this)),
305 base::Bind(&SourceState::OnNewConfigs, base::Unretained(this),
306 allow_audio, allow_video),
307 base::Bind(&SourceState::OnNewBuffers, base::Unretained(this)),
308 new_text_track_cb_.is_null(), encrypted_media_init_data_cb,
309 base::Bind(&SourceState::OnNewMediaSegment, base::Unretained(this)),
310 base::Bind(&SourceState::OnEndOfMediaSegment, base::Unretained(this)),
311 media_log_);
314 void SourceState::SetSequenceMode(bool sequence_mode) {
315 DCHECK(!parsing_media_segment_);
317 frame_processor_->SetSequenceMode(sequence_mode);
320 void SourceState::SetGroupStartTimestampIfInSequenceMode(
321 base::TimeDelta timestamp_offset) {
322 DCHECK(!parsing_media_segment_);
324 frame_processor_->SetGroupStartTimestampIfInSequenceMode(timestamp_offset);
327 bool SourceState::Append(
328 const uint8* data,
329 size_t length,
330 TimeDelta append_window_start,
331 TimeDelta append_window_end,
332 TimeDelta* timestamp_offset,
333 const InitSegmentReceivedCB& init_segment_received_cb) {
334 DCHECK(timestamp_offset);
335 DCHECK(!timestamp_offset_during_append_);
336 DCHECK(!init_segment_received_cb.is_null());
337 DCHECK(init_segment_received_cb_.is_null());
338 append_window_start_during_append_ = append_window_start;
339 append_window_end_during_append_ = append_window_end;
340 timestamp_offset_during_append_ = timestamp_offset;
341 init_segment_received_cb_= init_segment_received_cb;
343 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
344 // append window and timestamp offset pointer. See http://crbug.com/351454.
345 bool result = stream_parser_->Parse(data, length);
346 if (!result) {
347 MEDIA_LOG(ERROR, media_log_)
348 << __FUNCTION__ << ": stream parsing failed."
349 << " Data size=" << length
350 << " append_window_start=" << append_window_start.InSecondsF()
351 << " append_window_end=" << append_window_end.InSecondsF();
353 timestamp_offset_during_append_ = NULL;
354 init_segment_received_cb_.Reset();
355 return result;
358 void SourceState::Abort(TimeDelta append_window_start,
359 TimeDelta append_window_end,
360 base::TimeDelta* timestamp_offset) {
361 DCHECK(timestamp_offset);
362 DCHECK(!timestamp_offset_during_append_);
363 timestamp_offset_during_append_ = timestamp_offset;
364 append_window_start_during_append_ = append_window_start;
365 append_window_end_during_append_ = append_window_end;
367 stream_parser_->Flush();
368 timestamp_offset_during_append_ = NULL;
370 frame_processor_->Reset();
371 parsing_media_segment_ = false;
374 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
375 if (audio_)
376 audio_->Remove(start, end, duration);
378 if (video_)
379 video_->Remove(start, end, duration);
381 for (TextStreamMap::iterator itr = text_stream_map_.begin();
382 itr != text_stream_map_.end(); ++itr) {
383 itr->second->Remove(start, end, duration);
387 size_t SourceState::EstimateVideoDataSize(size_t muxed_data_chunk_size) const {
388 DCHECK(audio_);
389 DCHECK(video_);
391 size_t videoBufferedSize = video_->GetBufferedSize();
392 size_t audioBufferedSize = audio_->GetBufferedSize();
393 if (videoBufferedSize == 0 || audioBufferedSize == 0) {
394 // At this point either audio or video buffer is empty, which means buffer
395 // levels are probably low anyway and we should have enough space in the
396 // buffers for appending new data, so just take a very rough guess.
397 return muxed_data_chunk_size / 2;
400 // We need to estimate how much audio and video data is going to be in the
401 // newly appended data chunk to make space for the new data. And we need to do
402 // that without parsing the data (which will happen later, in the Append
403 // phase). So for now we can only rely on some heuristic here. Let's assume
404 // that the proportion of the audio/video in the new data chunk is the same as
405 // the current ratio of buffered audio/video.
406 // Longer term this should go away once we further change the MSE GC algorithm
407 // to work across all streams of a SourceBuffer (see crbug.com/520704).
408 double videoBufferedSizeF = static_cast<double>(videoBufferedSize);
409 double audioBufferedSizeF = static_cast<double>(audioBufferedSize);
411 double totalBufferedSizeF = videoBufferedSizeF + audioBufferedSizeF;
412 CHECK_GT(totalBufferedSizeF, 0.0);
414 double videoRatio = videoBufferedSizeF / totalBufferedSizeF;
415 CHECK_GE(videoRatio, 0.0);
416 CHECK_LE(videoRatio, 1.0);
417 double estimatedVideoSize = muxed_data_chunk_size * videoRatio;
418 return static_cast<size_t>(estimatedVideoSize);
421 bool SourceState::EvictCodedFrames(DecodeTimestamp media_time,
422 size_t newDataSize) {
423 bool success = true;
425 DVLOG(3) << __FUNCTION__ << " media_time=" << media_time.InSecondsF()
426 << " newDataSize=" << newDataSize
427 << " videoBufferedSize=" << (video_ ? video_->GetBufferedSize() : 0)
428 << " audioBufferedSize=" << (audio_ ? audio_->GetBufferedSize() : 0);
430 size_t newAudioSize = 0;
431 size_t newVideoSize = 0;
432 if (audio_ && video_) {
433 newVideoSize = EstimateVideoDataSize(newDataSize);
434 newAudioSize = newDataSize - newVideoSize;
435 } else if (video_) {
436 newVideoSize = newDataSize;
437 } else if (audio_) {
438 newAudioSize = newDataSize;
441 DVLOG(3) << __FUNCTION__ << " estimated audio/video sizes: "
442 << " newVideoSize=" << newVideoSize
443 << " newAudioSize=" << newAudioSize;
445 if (audio_)
446 success = audio_->EvictCodedFrames(media_time, newAudioSize) && success;
448 if (video_)
449 success = video_->EvictCodedFrames(media_time, newVideoSize) && success;
451 for (TextStreamMap::iterator itr = text_stream_map_.begin();
452 itr != text_stream_map_.end(); ++itr) {
453 success = itr->second->EvictCodedFrames(media_time, 0) && success;
456 DVLOG(3) << __FUNCTION__ << " result=" << success
457 << " videoBufferedSize=" << (video_ ? video_->GetBufferedSize() : 0)
458 << " audioBufferedSize=" << (audio_ ? audio_->GetBufferedSize() : 0);
460 return success;
463 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
464 bool ended) const {
465 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
466 // this code to only add ranges from active tracks.
467 RangesList ranges_list;
468 if (audio_)
469 ranges_list.push_back(audio_->GetBufferedRanges(duration));
471 if (video_)
472 ranges_list.push_back(video_->GetBufferedRanges(duration));
474 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
475 itr != text_stream_map_.end(); ++itr) {
476 ranges_list.push_back(itr->second->GetBufferedRanges(duration));
479 return ComputeIntersection(ranges_list, ended);
482 TimeDelta SourceState::GetMaxBufferedDuration() const {
483 TimeDelta max_duration;
485 if (audio_)
486 max_duration = std::max(max_duration, audio_->GetBufferedDuration());
488 if (video_)
489 max_duration = std::max(max_duration, video_->GetBufferedDuration());
491 for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
492 itr != text_stream_map_.end(); ++itr) {
493 max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
496 return max_duration;
499 void SourceState::StartReturningData() {
500 if (audio_)
501 audio_->StartReturningData();
503 if (video_)
504 video_->StartReturningData();
506 for (TextStreamMap::iterator itr = text_stream_map_.begin();
507 itr != text_stream_map_.end(); ++itr) {
508 itr->second->StartReturningData();
512 void SourceState::AbortReads() {
513 if (audio_)
514 audio_->AbortReads();
516 if (video_)
517 video_->AbortReads();
519 for (TextStreamMap::iterator itr = text_stream_map_.begin();
520 itr != text_stream_map_.end(); ++itr) {
521 itr->second->AbortReads();
525 void SourceState::Seek(TimeDelta seek_time) {
526 if (audio_)
527 audio_->Seek(seek_time);
529 if (video_)
530 video_->Seek(seek_time);
532 for (TextStreamMap::iterator itr = text_stream_map_.begin();
533 itr != text_stream_map_.end(); ++itr) {
534 itr->second->Seek(seek_time);
538 void SourceState::CompletePendingReadIfPossible() {
539 if (audio_)
540 audio_->CompletePendingReadIfPossible();
542 if (video_)
543 video_->CompletePendingReadIfPossible();
545 for (TextStreamMap::iterator itr = text_stream_map_.begin();
546 itr != text_stream_map_.end(); ++itr) {
547 itr->second->CompletePendingReadIfPossible();
551 void SourceState::OnSetDuration(TimeDelta duration) {
552 if (audio_)
553 audio_->OnSetDuration(duration);
555 if (video_)
556 video_->OnSetDuration(duration);
558 for (TextStreamMap::iterator itr = text_stream_map_.begin();
559 itr != text_stream_map_.end(); ++itr) {
560 itr->second->OnSetDuration(duration);
564 void SourceState::MarkEndOfStream() {
565 if (audio_)
566 audio_->MarkEndOfStream();
568 if (video_)
569 video_->MarkEndOfStream();
571 for (TextStreamMap::iterator itr = text_stream_map_.begin();
572 itr != text_stream_map_.end(); ++itr) {
573 itr->second->MarkEndOfStream();
577 void SourceState::UnmarkEndOfStream() {
578 if (audio_)
579 audio_->UnmarkEndOfStream();
581 if (video_)
582 video_->UnmarkEndOfStream();
584 for (TextStreamMap::iterator itr = text_stream_map_.begin();
585 itr != text_stream_map_.end(); ++itr) {
586 itr->second->UnmarkEndOfStream();
590 void SourceState::Shutdown() {
591 if (audio_)
592 audio_->Shutdown();
594 if (video_)
595 video_->Shutdown();
597 for (TextStreamMap::iterator itr = text_stream_map_.begin();
598 itr != text_stream_map_.end(); ++itr) {
599 itr->second->Shutdown();
603 void SourceState::SetMemoryLimits(DemuxerStream::Type type,
604 size_t memory_limit) {
605 switch (type) {
606 case DemuxerStream::AUDIO:
607 if (audio_)
608 audio_->SetStreamMemoryLimit(memory_limit);
609 break;
610 case DemuxerStream::VIDEO:
611 if (video_)
612 video_->SetStreamMemoryLimit(memory_limit);
613 break;
614 case DemuxerStream::TEXT:
615 for (TextStreamMap::iterator itr = text_stream_map_.begin();
616 itr != text_stream_map_.end(); ++itr) {
617 itr->second->SetStreamMemoryLimit(memory_limit);
619 break;
620 case DemuxerStream::UNKNOWN:
621 case DemuxerStream::NUM_TYPES:
622 NOTREACHED();
623 break;
627 bool SourceState::IsSeekWaitingForData() const {
628 if (audio_ && audio_->IsSeekWaitingForData())
629 return true;
631 if (video_ && video_->IsSeekWaitingForData())
632 return true;
634 // NOTE: We are intentionally not checking the text tracks
635 // because text tracks are discontinuous and may not have data
636 // for the seek position. This is ok and playback should not be
637 // stalled because we don't have cues. If cues, with timestamps after
638 // the seek time, eventually arrive they will be delivered properly
639 // in response to ChunkDemuxerStream::Read() calls.
641 return false;
644 bool SourceState::OnNewConfigs(
645 bool allow_audio, bool allow_video,
646 const AudioDecoderConfig& audio_config,
647 const VideoDecoderConfig& video_config,
648 const StreamParser::TextTrackConfigMap& text_configs) {
649 DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
650 << ", " << audio_config.IsValidConfig()
651 << ", " << video_config.IsValidConfig() << ")";
652 DCHECK(!init_segment_received_cb_.is_null());
654 if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
655 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
656 return false;
659 // Signal an error if we get configuration info for stream types that weren't
660 // specified in AddId() or more configs after a stream is initialized.
661 if (allow_audio != audio_config.IsValidConfig()) {
662 MEDIA_LOG(ERROR, media_log_)
663 << "Initialization segment"
664 << (audio_config.IsValidConfig() ? " has" : " does not have")
665 << " an audio track, but the mimetype"
666 << (allow_audio ? " specifies" : " does not specify")
667 << " an audio codec.";
668 return false;
671 if (allow_video != video_config.IsValidConfig()) {
672 MEDIA_LOG(ERROR, media_log_)
673 << "Initialization segment"
674 << (video_config.IsValidConfig() ? " has" : " does not have")
675 << " a video track, but the mimetype"
676 << (allow_video ? " specifies" : " does not specify")
677 << " a video codec.";
678 return false;
681 bool success = true;
682 if (audio_config.IsValidConfig()) {
683 if (!audio_) {
684 media_log_->SetBooleanProperty("found_audio_stream", true);
686 if (!audio_ ||
687 audio_->audio_decoder_config().codec() != audio_config.codec()) {
688 media_log_->SetStringProperty("audio_codec_name",
689 audio_config.GetHumanReadableCodecName());
692 if (!audio_) {
693 audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
695 if (!audio_) {
696 DVLOG(1) << "Failed to create an audio stream.";
697 return false;
700 if (!frame_processor_->AddTrack(FrameProcessor::kAudioTrackId, audio_)) {
701 DVLOG(1) << "Failed to add audio track to frame processor.";
702 return false;
706 frame_processor_->OnPossibleAudioConfigUpdate(audio_config);
707 success &= audio_->UpdateAudioConfig(audio_config, media_log_);
710 if (video_config.IsValidConfig()) {
711 if (!video_) {
712 media_log_->SetBooleanProperty("found_video_stream", true);
714 if (!video_ ||
715 video_->video_decoder_config().codec() != video_config.codec()) {
716 media_log_->SetStringProperty("video_codec_name",
717 video_config.GetHumanReadableCodecName());
720 if (!video_) {
721 video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
723 if (!video_) {
724 DVLOG(1) << "Failed to create a video stream.";
725 return false;
728 if (!frame_processor_->AddTrack(FrameProcessor::kVideoTrackId, video_)) {
729 DVLOG(1) << "Failed to add video track to frame processor.";
730 return false;
734 success &= video_->UpdateVideoConfig(video_config, media_log_);
737 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
738 if (text_stream_map_.empty()) {
739 for (TextConfigItr itr = text_configs.begin();
740 itr != text_configs.end(); ++itr) {
741 ChunkDemuxerStream* const text_stream =
742 create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
743 if (!frame_processor_->AddTrack(itr->first, text_stream)) {
744 success &= false;
745 MEDIA_LOG(ERROR, media_log_) << "Failed to add text track ID "
746 << itr->first << " to frame processor.";
747 break;
749 text_stream->UpdateTextConfig(itr->second, media_log_);
750 text_stream_map_[itr->first] = text_stream;
751 new_text_track_cb_.Run(text_stream, itr->second);
753 } else {
754 const size_t text_count = text_stream_map_.size();
755 if (text_configs.size() != text_count) {
756 success &= false;
757 MEDIA_LOG(ERROR, media_log_)
758 << "The number of text track configs changed.";
759 } else if (text_count == 1) {
760 TextConfigItr config_itr = text_configs.begin();
761 TextStreamMap::iterator stream_itr = text_stream_map_.begin();
762 ChunkDemuxerStream* text_stream = stream_itr->second;
763 TextTrackConfig old_config = text_stream->text_track_config();
764 TextTrackConfig new_config(config_itr->second.kind(),
765 config_itr->second.label(),
766 config_itr->second.language(),
767 old_config.id());
768 if (!new_config.Matches(old_config)) {
769 success &= false;
770 MEDIA_LOG(ERROR, media_log_)
771 << "New text track config does not match old one.";
772 } else {
773 StreamParser::TrackId old_id = stream_itr->first;
774 StreamParser::TrackId new_id = config_itr->first;
775 if (new_id != old_id) {
776 if (frame_processor_->UpdateTrack(old_id, new_id)) {
777 text_stream_map_.clear();
778 text_stream_map_[config_itr->first] = text_stream;
779 } else {
780 success &= false;
781 MEDIA_LOG(ERROR, media_log_)
782 << "Error remapping single text track number";
786 } else {
787 for (TextConfigItr config_itr = text_configs.begin();
788 config_itr != text_configs.end(); ++config_itr) {
789 TextStreamMap::iterator stream_itr =
790 text_stream_map_.find(config_itr->first);
791 if (stream_itr == text_stream_map_.end()) {
792 success &= false;
793 MEDIA_LOG(ERROR, media_log_)
794 << "Unexpected text track configuration for track ID "
795 << config_itr->first;
796 break;
799 const TextTrackConfig& new_config = config_itr->second;
800 ChunkDemuxerStream* stream = stream_itr->second;
801 TextTrackConfig old_config = stream->text_track_config();
802 if (!new_config.Matches(old_config)) {
803 success &= false;
804 MEDIA_LOG(ERROR, media_log_) << "New text track config for track ID "
805 << config_itr->first
806 << " does not match old one.";
807 break;
813 frame_processor_->SetAllTrackBuffersNeedRandomAccessPoint();
815 DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
816 if (success)
817 init_segment_received_cb_.Run();
819 return success;
822 void SourceState::OnNewMediaSegment() {
823 DVLOG(2) << "OnNewMediaSegment()";
824 parsing_media_segment_ = true;
825 new_media_segment_ = true;
828 void SourceState::OnEndOfMediaSegment() {
829 DVLOG(2) << "OnEndOfMediaSegment()";
830 parsing_media_segment_ = false;
831 new_media_segment_ = false;
834 bool SourceState::OnNewBuffers(
835 const StreamParser::BufferQueue& audio_buffers,
836 const StreamParser::BufferQueue& video_buffers,
837 const StreamParser::TextBufferQueueMap& text_map) {
838 DVLOG(2) << "OnNewBuffers()";
839 DCHECK(timestamp_offset_during_append_);
840 DCHECK(parsing_media_segment_);
842 const TimeDelta timestamp_offset_before_processing =
843 *timestamp_offset_during_append_;
845 // Calculate the new timestamp offset for audio/video tracks if the stream
846 // parser has requested automatic updates.
847 TimeDelta new_timestamp_offset = timestamp_offset_before_processing;
848 if (auto_update_timestamp_offset_) {
849 const bool have_audio_buffers = !audio_buffers.empty();
850 const bool have_video_buffers = !video_buffers.empty();
851 if (have_audio_buffers && have_video_buffers) {
852 new_timestamp_offset +=
853 std::min(EndTimestamp(audio_buffers), EndTimestamp(video_buffers));
854 } else if (have_audio_buffers) {
855 new_timestamp_offset += EndTimestamp(audio_buffers);
856 } else if (have_video_buffers) {
857 new_timestamp_offset += EndTimestamp(video_buffers);
861 if (!frame_processor_->ProcessFrames(audio_buffers,
862 video_buffers,
863 text_map,
864 append_window_start_during_append_,
865 append_window_end_during_append_,
866 &new_media_segment_,
867 timestamp_offset_during_append_)) {
868 return false;
871 // Only update the timestamp offset if the frame processor hasn't already.
872 if (auto_update_timestamp_offset_ &&
873 timestamp_offset_before_processing == *timestamp_offset_during_append_) {
874 *timestamp_offset_during_append_ = new_timestamp_offset;
877 return true;
880 void SourceState::OnSourceInitDone(const StreamParser::InitParameters& params) {
881 auto_update_timestamp_offset_ = params.auto_update_timestamp_offset;
882 base::ResetAndReturn(&init_cb_).Run(params);
885 ChunkDemuxerStream::ChunkDemuxerStream(Type type,
886 bool splice_frames_enabled)
887 : type_(type),
888 liveness_(DemuxerStream::LIVENESS_UNKNOWN),
889 state_(UNINITIALIZED),
890 splice_frames_enabled_(splice_frames_enabled),
891 partial_append_window_trimming_enabled_(false) {
894 void ChunkDemuxerStream::StartReturningData() {
895 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
896 base::AutoLock auto_lock(lock_);
897 DCHECK(read_cb_.is_null());
898 ChangeState_Locked(RETURNING_DATA_FOR_READS);
901 void ChunkDemuxerStream::AbortReads() {
902 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
903 base::AutoLock auto_lock(lock_);
904 ChangeState_Locked(RETURNING_ABORT_FOR_READS);
905 if (!read_cb_.is_null())
906 base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
909 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
910 base::AutoLock auto_lock(lock_);
911 if (read_cb_.is_null())
912 return;
914 CompletePendingReadIfPossible_Locked();
917 void ChunkDemuxerStream::Shutdown() {
918 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
919 base::AutoLock auto_lock(lock_);
920 ChangeState_Locked(SHUTDOWN);
922 // Pass an end of stream buffer to the pending callback to signal that no more
923 // data will be sent.
924 if (!read_cb_.is_null()) {
925 base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
926 StreamParserBuffer::CreateEOSBuffer());
930 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
931 base::AutoLock auto_lock(lock_);
933 // This method should not be called for text tracks. See the note in
934 // SourceState::IsSeekWaitingForData().
935 DCHECK_NE(type_, DemuxerStream::TEXT);
937 return stream_->IsSeekPending();
940 void ChunkDemuxerStream::Seek(TimeDelta time) {
941 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
942 base::AutoLock auto_lock(lock_);
943 DCHECK(read_cb_.is_null());
944 DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
945 << state_;
947 stream_->Seek(time);
950 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
951 if (buffers.empty())
952 return false;
954 base::AutoLock auto_lock(lock_);
955 DCHECK_NE(state_, SHUTDOWN);
956 if (!stream_->Append(buffers)) {
957 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
958 return false;
961 if (!read_cb_.is_null())
962 CompletePendingReadIfPossible_Locked();
964 return true;
967 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
968 TimeDelta duration) {
969 base::AutoLock auto_lock(lock_);
970 stream_->Remove(start, end, duration);
973 bool ChunkDemuxerStream::EvictCodedFrames(DecodeTimestamp media_time,
974 size_t newDataSize) {
975 base::AutoLock auto_lock(lock_);
976 return stream_->GarbageCollectIfNeeded(media_time, newDataSize);
979 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
980 base::AutoLock auto_lock(lock_);
981 stream_->OnSetDuration(duration);
984 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
985 TimeDelta duration) const {
986 base::AutoLock auto_lock(lock_);
988 if (type_ == TEXT) {
989 // Since text tracks are discontinuous and the lack of cues should not block
990 // playback, report the buffered range for text tracks as [0, |duration|) so
991 // that intesections with audio & video tracks are computed correctly when
992 // no cues are present.
993 Ranges<TimeDelta> text_range;
994 text_range.Add(TimeDelta(), duration);
995 return text_range;
998 Ranges<TimeDelta> range = stream_->GetBufferedTime();
1000 if (range.size() == 0u)
1001 return range;
1003 // Clamp the end of the stream's buffered ranges to fit within the duration.
1004 // This can be done by intersecting the stream's range with the valid time
1005 // range.
1006 Ranges<TimeDelta> valid_time_range;
1007 valid_time_range.Add(range.start(0), duration);
1008 return range.IntersectionWith(valid_time_range);
1011 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
1012 return stream_->GetBufferedDuration();
1015 size_t ChunkDemuxerStream::GetBufferedSize() const {
1016 return stream_->GetBufferedSize();
1019 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp) {
1020 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
1021 << start_timestamp.InSecondsF() << ")";
1022 base::AutoLock auto_lock(lock_);
1023 stream_->OnNewMediaSegment(start_timestamp);
1026 bool ChunkDemuxerStream::UpdateAudioConfig(
1027 const AudioDecoderConfig& config,
1028 const scoped_refptr<MediaLog>& media_log) {
1029 DCHECK(config.IsValidConfig());
1030 DCHECK_EQ(type_, AUDIO);
1031 base::AutoLock auto_lock(lock_);
1032 if (!stream_) {
1033 DCHECK_EQ(state_, UNINITIALIZED);
1035 // On platforms which support splice frames, enable splice frames and
1036 // partial append window support for most codecs (notably: not opus).
1037 const bool codec_supported = config.codec() == kCodecMP3 ||
1038 config.codec() == kCodecAAC ||
1039 config.codec() == kCodecVorbis;
1040 splice_frames_enabled_ = splice_frames_enabled_ && codec_supported;
1041 partial_append_window_trimming_enabled_ =
1042 splice_frames_enabled_ && codec_supported;
1044 stream_.reset(
1045 new SourceBufferStream(config, media_log, splice_frames_enabled_));
1046 return true;
1049 return stream_->UpdateAudioConfig(config);
1052 bool ChunkDemuxerStream::UpdateVideoConfig(
1053 const VideoDecoderConfig& config,
1054 const scoped_refptr<MediaLog>& media_log) {
1055 DCHECK(config.IsValidConfig());
1056 DCHECK_EQ(type_, VIDEO);
1057 base::AutoLock auto_lock(lock_);
1059 if (!stream_) {
1060 DCHECK_EQ(state_, UNINITIALIZED);
1061 stream_.reset(
1062 new SourceBufferStream(config, media_log, splice_frames_enabled_));
1063 return true;
1066 return stream_->UpdateVideoConfig(config);
1069 void ChunkDemuxerStream::UpdateTextConfig(
1070 const TextTrackConfig& config,
1071 const scoped_refptr<MediaLog>& media_log) {
1072 DCHECK_EQ(type_, TEXT);
1073 base::AutoLock auto_lock(lock_);
1074 DCHECK(!stream_);
1075 DCHECK_EQ(state_, UNINITIALIZED);
1076 stream_.reset(
1077 new SourceBufferStream(config, media_log, splice_frames_enabled_));
1080 void ChunkDemuxerStream::MarkEndOfStream() {
1081 base::AutoLock auto_lock(lock_);
1082 stream_->MarkEndOfStream();
1085 void ChunkDemuxerStream::UnmarkEndOfStream() {
1086 base::AutoLock auto_lock(lock_);
1087 stream_->UnmarkEndOfStream();
1090 // DemuxerStream methods.
1091 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
1092 base::AutoLock auto_lock(lock_);
1093 DCHECK_NE(state_, UNINITIALIZED);
1094 DCHECK(read_cb_.is_null());
1096 read_cb_ = BindToCurrentLoop(read_cb);
1097 CompletePendingReadIfPossible_Locked();
1100 DemuxerStream::Type ChunkDemuxerStream::type() const { return type_; }
1102 DemuxerStream::Liveness ChunkDemuxerStream::liveness() const {
1103 base::AutoLock auto_lock(lock_);
1104 return liveness_;
1107 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
1108 CHECK_EQ(type_, AUDIO);
1109 base::AutoLock auto_lock(lock_);
1110 return stream_->GetCurrentAudioDecoderConfig();
1113 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
1114 CHECK_EQ(type_, VIDEO);
1115 base::AutoLock auto_lock(lock_);
1116 return stream_->GetCurrentVideoDecoderConfig();
1119 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
1121 VideoRotation ChunkDemuxerStream::video_rotation() {
1122 return VIDEO_ROTATION_0;
1125 TextTrackConfig ChunkDemuxerStream::text_track_config() {
1126 CHECK_EQ(type_, TEXT);
1127 base::AutoLock auto_lock(lock_);
1128 return stream_->GetCurrentTextTrackConfig();
1131 void ChunkDemuxerStream::SetStreamMemoryLimit(size_t memory_limit) {
1132 stream_->set_memory_limit(memory_limit);
1135 void ChunkDemuxerStream::SetLiveness(Liveness liveness) {
1136 base::AutoLock auto_lock(lock_);
1137 liveness_ = liveness;
1140 void ChunkDemuxerStream::ChangeState_Locked(State state) {
1141 lock_.AssertAcquired();
1142 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1143 << "type " << type_
1144 << " - " << state_ << " -> " << state;
1145 state_ = state;
1148 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1150 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1151 lock_.AssertAcquired();
1152 DCHECK(!read_cb_.is_null());
1154 DemuxerStream::Status status;
1155 scoped_refptr<StreamParserBuffer> buffer;
1157 switch (state_) {
1158 case UNINITIALIZED:
1159 NOTREACHED();
1160 return;
1161 case RETURNING_DATA_FOR_READS:
1162 switch (stream_->GetNextBuffer(&buffer)) {
1163 case SourceBufferStream::kSuccess:
1164 status = DemuxerStream::kOk;
1165 DVLOG(2) << __FUNCTION__ << ": returning kOk, type " << type_
1166 << ", dts " << buffer->GetDecodeTimestamp().InSecondsF()
1167 << ", pts " << buffer->timestamp().InSecondsF()
1168 << ", dur " << buffer->duration().InSecondsF()
1169 << ", key " << buffer->is_key_frame();
1170 break;
1171 case SourceBufferStream::kNeedBuffer:
1172 // Return early without calling |read_cb_| since we don't have
1173 // any data to return yet.
1174 DVLOG(2) << __FUNCTION__ << ": returning kNeedBuffer, type "
1175 << type_;
1176 return;
1177 case SourceBufferStream::kEndOfStream:
1178 status = DemuxerStream::kOk;
1179 buffer = StreamParserBuffer::CreateEOSBuffer();
1180 DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
1181 << type_;
1182 break;
1183 case SourceBufferStream::kConfigChange:
1184 status = kConfigChanged;
1185 buffer = NULL;
1186 DVLOG(2) << __FUNCTION__ << ": returning kConfigChange, type "
1187 << type_;
1188 break;
1190 break;
1191 case RETURNING_ABORT_FOR_READS:
1192 // Null buffers should be returned in this state since we are waiting
1193 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1194 // because they are associated with the seek.
1195 status = DemuxerStream::kAborted;
1196 buffer = NULL;
1197 DVLOG(2) << __FUNCTION__ << ": returning kAborted, type " << type_;
1198 break;
1199 case SHUTDOWN:
1200 status = DemuxerStream::kOk;
1201 buffer = StreamParserBuffer::CreateEOSBuffer();
1202 DVLOG(2) << __FUNCTION__ << ": returning kOk with EOS buffer, type "
1203 << type_;
1204 break;
1207 base::ResetAndReturn(&read_cb_).Run(status, buffer);
1210 ChunkDemuxer::ChunkDemuxer(
1211 const base::Closure& open_cb,
1212 const EncryptedMediaInitDataCB& encrypted_media_init_data_cb,
1213 const scoped_refptr<MediaLog>& media_log,
1214 bool splice_frames_enabled)
1215 : state_(WAITING_FOR_INIT),
1216 cancel_next_seek_(false),
1217 host_(NULL),
1218 open_cb_(open_cb),
1219 encrypted_media_init_data_cb_(encrypted_media_init_data_cb),
1220 enable_text_(false),
1221 media_log_(media_log),
1222 duration_(kNoTimestamp()),
1223 user_specified_duration_(-1),
1224 liveness_(DemuxerStream::LIVENESS_UNKNOWN),
1225 splice_frames_enabled_(splice_frames_enabled) {
1226 DCHECK(!open_cb_.is_null());
1227 DCHECK(!encrypted_media_init_data_cb_.is_null());
1230 std::string ChunkDemuxer::GetDisplayName() const {
1231 return "ChunkDemuxer";
1234 void ChunkDemuxer::Initialize(
1235 DemuxerHost* host,
1236 const PipelineStatusCB& cb,
1237 bool enable_text_tracks) {
1238 DVLOG(1) << "Init()";
1240 base::AutoLock auto_lock(lock_);
1242 // The |init_cb_| must only be run after this method returns, so always post.
1243 init_cb_ = BindToCurrentLoop(cb);
1244 if (state_ == SHUTDOWN) {
1245 base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1246 return;
1248 DCHECK_EQ(state_, WAITING_FOR_INIT);
1249 host_ = host;
1250 enable_text_ = enable_text_tracks;
1252 ChangeState_Locked(INITIALIZING);
1254 base::ResetAndReturn(&open_cb_).Run();
1257 void ChunkDemuxer::Stop() {
1258 DVLOG(1) << "Stop()";
1259 Shutdown();
1262 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1263 DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1264 DCHECK(time >= TimeDelta());
1266 base::AutoLock auto_lock(lock_);
1267 DCHECK(seek_cb_.is_null());
1269 seek_cb_ = BindToCurrentLoop(cb);
1270 if (state_ != INITIALIZED && state_ != ENDED) {
1271 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1272 return;
1275 if (cancel_next_seek_) {
1276 cancel_next_seek_ = false;
1277 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1278 return;
1281 SeekAllSources(time);
1282 StartReturningData();
1284 if (IsSeekWaitingForData_Locked()) {
1285 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1286 return;
1289 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1292 // Demuxer implementation.
1293 base::Time ChunkDemuxer::GetTimelineOffset() const {
1294 return timeline_offset_;
1297 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1298 DCHECK_NE(type, DemuxerStream::TEXT);
1299 base::AutoLock auto_lock(lock_);
1300 if (type == DemuxerStream::VIDEO)
1301 return video_.get();
1303 if (type == DemuxerStream::AUDIO)
1304 return audio_.get();
1306 return NULL;
1309 TimeDelta ChunkDemuxer::GetStartTime() const {
1310 return TimeDelta();
1313 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1314 DVLOG(1) << "StartWaitingForSeek()";
1315 base::AutoLock auto_lock(lock_);
1316 DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1317 state_ == PARSE_ERROR) << state_;
1318 DCHECK(seek_cb_.is_null());
1320 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1321 return;
1323 AbortPendingReads();
1324 SeekAllSources(seek_time);
1326 // Cancel state set in CancelPendingSeek() since we want to
1327 // accept the next Seek().
1328 cancel_next_seek_ = false;
1331 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1332 base::AutoLock auto_lock(lock_);
1333 DCHECK_NE(state_, INITIALIZING);
1334 DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1336 if (cancel_next_seek_)
1337 return;
1339 AbortPendingReads();
1340 SeekAllSources(seek_time);
1342 if (seek_cb_.is_null()) {
1343 cancel_next_seek_ = true;
1344 return;
1347 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1350 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1351 const std::string& type,
1352 std::vector<std::string>& codecs) {
1353 base::AutoLock auto_lock(lock_);
1355 if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1356 return kReachedIdLimit;
1358 bool has_audio = false;
1359 bool has_video = false;
1360 scoped_ptr<media::StreamParser> stream_parser(StreamParserFactory::Create(
1361 type, codecs, media_log_, &has_audio, &has_video));
1363 if (!stream_parser)
1364 return ChunkDemuxer::kNotSupported;
1366 if ((has_audio && !source_id_audio_.empty()) ||
1367 (has_video && !source_id_video_.empty()))
1368 return kReachedIdLimit;
1370 if (has_audio)
1371 source_id_audio_ = id;
1373 if (has_video)
1374 source_id_video_ = id;
1376 scoped_ptr<FrameProcessor> frame_processor(
1377 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1378 base::Unretained(this)),
1379 media_log_));
1381 scoped_ptr<SourceState> source_state(new SourceState(
1382 stream_parser.Pass(), frame_processor.Pass(),
1383 base::Bind(&ChunkDemuxer::CreateDemuxerStream, base::Unretained(this)),
1384 media_log_));
1386 SourceState::NewTextTrackCB new_text_track_cb;
1388 if (enable_text_) {
1389 new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1390 base::Unretained(this));
1393 source_state->Init(
1394 base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1395 has_audio, has_video, encrypted_media_init_data_cb_, new_text_track_cb);
1397 source_state_map_[id] = source_state.release();
1398 return kOk;
1401 void ChunkDemuxer::RemoveId(const std::string& id) {
1402 base::AutoLock auto_lock(lock_);
1403 CHECK(IsValidId(id));
1405 delete source_state_map_[id];
1406 source_state_map_.erase(id);
1408 if (source_id_audio_ == id)
1409 source_id_audio_.clear();
1411 if (source_id_video_ == id)
1412 source_id_video_.clear();
1415 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1416 base::AutoLock auto_lock(lock_);
1417 DCHECK(!id.empty());
1419 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1421 DCHECK(itr != source_state_map_.end());
1422 return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1425 bool ChunkDemuxer::EvictCodedFrames(const std::string& id,
1426 base::TimeDelta currentMediaTime,
1427 size_t newDataSize) {
1428 DVLOG(1) << __FUNCTION__ << "(" << id << ")"
1429 << " media_time=" << currentMediaTime.InSecondsF()
1430 << " newDataSize=" << newDataSize;
1431 base::AutoLock auto_lock(lock_);
1433 // Note: The direct conversion from PTS to DTS is safe here, since we don't
1434 // need to know currentTime precisely for GC. GC only needs to know which GOP
1435 // currentTime points to.
1436 DecodeTimestamp media_time_dts =
1437 DecodeTimestamp::FromPresentationTime(currentMediaTime);
1439 DCHECK(!id.empty());
1440 SourceStateMap::const_iterator itr = source_state_map_.find(id);
1441 if (itr == source_state_map_.end()) {
1442 LOG(WARNING) << __FUNCTION__ << " stream " << id << " not found";
1443 return false;
1445 return itr->second->EvictCodedFrames(media_time_dts, newDataSize);
1448 void ChunkDemuxer::AppendData(
1449 const std::string& id,
1450 const uint8* data,
1451 size_t length,
1452 TimeDelta append_window_start,
1453 TimeDelta append_window_end,
1454 TimeDelta* timestamp_offset,
1455 const InitSegmentReceivedCB& init_segment_received_cb) {
1456 DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1458 DCHECK(!id.empty());
1459 DCHECK(timestamp_offset);
1460 DCHECK(!init_segment_received_cb.is_null());
1462 Ranges<TimeDelta> ranges;
1465 base::AutoLock auto_lock(lock_);
1466 DCHECK_NE(state_, ENDED);
1468 // Capture if any of the SourceBuffers are waiting for data before we start
1469 // parsing.
1470 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1472 if (length == 0u)
1473 return;
1475 DCHECK(data);
1477 switch (state_) {
1478 case INITIALIZING:
1479 case INITIALIZED:
1480 DCHECK(IsValidId(id));
1481 if (!source_state_map_[id]->Append(data, length,
1482 append_window_start,
1483 append_window_end,
1484 timestamp_offset,
1485 init_segment_received_cb)) {
1486 ReportError_Locked(PIPELINE_ERROR_DECODE);
1487 return;
1489 break;
1491 case PARSE_ERROR:
1492 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1493 return;
1495 case WAITING_FOR_INIT:
1496 case ENDED:
1497 case SHUTDOWN:
1498 DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1499 return;
1502 // Check to see if data was appended at the pending seek point. This
1503 // indicates we have parsed enough data to complete the seek.
1504 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1505 !seek_cb_.is_null()) {
1506 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1509 ranges = GetBufferedRanges_Locked();
1512 for (size_t i = 0; i < ranges.size(); ++i)
1513 host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1516 void ChunkDemuxer::Abort(const std::string& id,
1517 TimeDelta append_window_start,
1518 TimeDelta append_window_end,
1519 TimeDelta* timestamp_offset) {
1520 DVLOG(1) << "Abort(" << id << ")";
1521 base::AutoLock auto_lock(lock_);
1522 DCHECK(!id.empty());
1523 CHECK(IsValidId(id));
1524 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1525 source_state_map_[id]->Abort(append_window_start,
1526 append_window_end,
1527 timestamp_offset);
1528 // Abort can possibly emit some buffers.
1529 // Need to check whether seeking can be completed.
1530 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1531 !seek_cb_.is_null()) {
1532 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1536 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1537 TimeDelta end) {
1538 DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1539 << ", " << end.InSecondsF() << ")";
1540 base::AutoLock auto_lock(lock_);
1542 DCHECK(!id.empty());
1543 CHECK(IsValidId(id));
1544 DCHECK(start >= base::TimeDelta()) << start.InSecondsF();
1545 DCHECK(start < end) << "start " << start.InSecondsF()
1546 << " end " << end.InSecondsF();
1547 DCHECK(duration_ != kNoTimestamp());
1548 DCHECK(start <= duration_) << "start " << start.InSecondsF()
1549 << " duration " << duration_.InSecondsF();
1551 if (start == duration_)
1552 return;
1554 source_state_map_[id]->Remove(start, end, duration_);
1557 double ChunkDemuxer::GetDuration() {
1558 base::AutoLock auto_lock(lock_);
1559 return GetDuration_Locked();
1562 double ChunkDemuxer::GetDuration_Locked() {
1563 lock_.AssertAcquired();
1564 if (duration_ == kNoTimestamp())
1565 return std::numeric_limits<double>::quiet_NaN();
1567 // Return positive infinity if the resource is unbounded.
1568 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1569 if (duration_ == kInfiniteDuration())
1570 return std::numeric_limits<double>::infinity();
1572 if (user_specified_duration_ >= 0)
1573 return user_specified_duration_;
1575 return duration_.InSecondsF();
1578 void ChunkDemuxer::SetDuration(double duration) {
1579 base::AutoLock auto_lock(lock_);
1580 DVLOG(1) << "SetDuration(" << duration << ")";
1581 DCHECK_GE(duration, 0);
1583 if (duration == GetDuration_Locked())
1584 return;
1586 // Compute & bounds check the TimeDelta representation of duration.
1587 // This can be different if the value of |duration| doesn't fit the range or
1588 // precision of TimeDelta.
1589 TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1590 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1591 TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1592 double min_duration_in_seconds = min_duration.InSecondsF();
1593 double max_duration_in_seconds = max_duration.InSecondsF();
1595 TimeDelta duration_td;
1596 if (duration == std::numeric_limits<double>::infinity()) {
1597 duration_td = media::kInfiniteDuration();
1598 } else if (duration < min_duration_in_seconds) {
1599 duration_td = min_duration;
1600 } else if (duration > max_duration_in_seconds) {
1601 duration_td = max_duration;
1602 } else {
1603 duration_td = TimeDelta::FromMicroseconds(
1604 duration * base::Time::kMicrosecondsPerSecond);
1607 DCHECK(duration_td > TimeDelta());
1609 user_specified_duration_ = duration;
1610 duration_ = duration_td;
1611 host_->SetDuration(duration_);
1613 for (SourceStateMap::iterator itr = source_state_map_.begin();
1614 itr != source_state_map_.end(); ++itr) {
1615 itr->second->OnSetDuration(duration_);
1619 bool ChunkDemuxer::IsParsingMediaSegment(const std::string& id) {
1620 base::AutoLock auto_lock(lock_);
1621 DVLOG(1) << "IsParsingMediaSegment(" << id << ")";
1622 CHECK(IsValidId(id));
1624 return source_state_map_[id]->parsing_media_segment();
1627 void ChunkDemuxer::SetSequenceMode(const std::string& id,
1628 bool sequence_mode) {
1629 base::AutoLock auto_lock(lock_);
1630 DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1631 CHECK(IsValidId(id));
1632 DCHECK_NE(state_, ENDED);
1634 source_state_map_[id]->SetSequenceMode(sequence_mode);
1637 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1638 const std::string& id,
1639 base::TimeDelta timestamp_offset) {
1640 base::AutoLock auto_lock(lock_);
1641 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id << ", "
1642 << timestamp_offset.InSecondsF() << ")";
1643 CHECK(IsValidId(id));
1644 DCHECK_NE(state_, ENDED);
1646 source_state_map_[id]->SetGroupStartTimestampIfInSequenceMode(
1647 timestamp_offset);
1651 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1652 DVLOG(1) << "MarkEndOfStream(" << status << ")";
1653 base::AutoLock auto_lock(lock_);
1654 DCHECK_NE(state_, WAITING_FOR_INIT);
1655 DCHECK_NE(state_, ENDED);
1657 if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1658 return;
1660 if (state_ == INITIALIZING) {
1661 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1662 return;
1665 bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1666 for (SourceStateMap::iterator itr = source_state_map_.begin();
1667 itr != source_state_map_.end(); ++itr) {
1668 itr->second->MarkEndOfStream();
1671 CompletePendingReadsIfPossible();
1673 // Give a chance to resume the pending seek process.
1674 if (status != PIPELINE_OK) {
1675 ReportError_Locked(status);
1676 return;
1679 ChangeState_Locked(ENDED);
1680 DecreaseDurationIfNecessary();
1682 if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1683 !seek_cb_.is_null()) {
1684 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1688 void ChunkDemuxer::UnmarkEndOfStream() {
1689 DVLOG(1) << "UnmarkEndOfStream()";
1690 base::AutoLock auto_lock(lock_);
1691 DCHECK_EQ(state_, ENDED);
1693 ChangeState_Locked(INITIALIZED);
1695 for (SourceStateMap::iterator itr = source_state_map_.begin();
1696 itr != source_state_map_.end(); ++itr) {
1697 itr->second->UnmarkEndOfStream();
1701 void ChunkDemuxer::Shutdown() {
1702 DVLOG(1) << "Shutdown()";
1703 base::AutoLock auto_lock(lock_);
1705 if (state_ == SHUTDOWN)
1706 return;
1708 ShutdownAllStreams();
1710 ChangeState_Locked(SHUTDOWN);
1712 if(!seek_cb_.is_null())
1713 base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1716 void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type,
1717 size_t memory_limit) {
1718 for (SourceStateMap::iterator itr = source_state_map_.begin();
1719 itr != source_state_map_.end(); ++itr) {
1720 itr->second->SetMemoryLimits(type, memory_limit);
1724 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1725 lock_.AssertAcquired();
1726 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1727 << state_ << " -> " << new_state;
1728 state_ = new_state;
1731 ChunkDemuxer::~ChunkDemuxer() {
1732 DCHECK_NE(state_, INITIALIZED);
1734 STLDeleteValues(&source_state_map_);
1737 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1738 DVLOG(1) << "ReportError_Locked(" << error << ")";
1739 lock_.AssertAcquired();
1740 DCHECK_NE(error, PIPELINE_OK);
1742 ChangeState_Locked(PARSE_ERROR);
1744 PipelineStatusCB cb;
1746 if (!init_cb_.is_null()) {
1747 std::swap(cb, init_cb_);
1748 } else {
1749 if (!seek_cb_.is_null())
1750 std::swap(cb, seek_cb_);
1752 ShutdownAllStreams();
1755 if (!cb.is_null()) {
1756 cb.Run(error);
1757 return;
1760 base::AutoUnlock auto_unlock(lock_);
1761 host_->OnDemuxerError(error);
1764 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1765 lock_.AssertAcquired();
1766 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1767 itr != source_state_map_.end(); ++itr) {
1768 if (itr->second->IsSeekWaitingForData())
1769 return true;
1772 return false;
1775 void ChunkDemuxer::OnSourceInitDone(
1776 const StreamParser::InitParameters& params) {
1777 DVLOG(1) << "OnSourceInitDone(" << params.duration.InSecondsF() << ")";
1778 lock_.AssertAcquired();
1779 DCHECK_EQ(state_, INITIALIZING);
1780 if (!audio_ && !video_) {
1781 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1782 return;
1785 if (params.duration != TimeDelta() && duration_ == kNoTimestamp())
1786 UpdateDuration(params.duration);
1788 if (!params.timeline_offset.is_null()) {
1789 if (!timeline_offset_.is_null() &&
1790 params.timeline_offset != timeline_offset_) {
1791 MEDIA_LOG(ERROR, media_log_)
1792 << "Timeline offset is not the same across all SourceBuffers.";
1793 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1794 return;
1797 timeline_offset_ = params.timeline_offset;
1800 if (params.liveness != DemuxerStream::LIVENESS_UNKNOWN) {
1801 if (audio_)
1802 audio_->SetLiveness(params.liveness);
1803 if (video_)
1804 video_->SetLiveness(params.liveness);
1807 // Wait until all streams have initialized.
1808 if ((!source_id_audio_.empty() && !audio_) ||
1809 (!source_id_video_.empty() && !video_)) {
1810 return;
1813 SeekAllSources(GetStartTime());
1814 StartReturningData();
1816 if (duration_ == kNoTimestamp())
1817 duration_ = kInfiniteDuration();
1819 // The demuxer is now initialized after the |start_timestamp_| was set.
1820 ChangeState_Locked(INITIALIZED);
1821 base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1824 ChunkDemuxerStream*
1825 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1826 switch (type) {
1827 case DemuxerStream::AUDIO:
1828 if (audio_)
1829 return NULL;
1830 audio_.reset(
1831 new ChunkDemuxerStream(DemuxerStream::AUDIO, splice_frames_enabled_));
1832 return audio_.get();
1833 break;
1834 case DemuxerStream::VIDEO:
1835 if (video_)
1836 return NULL;
1837 video_.reset(
1838 new ChunkDemuxerStream(DemuxerStream::VIDEO, splice_frames_enabled_));
1839 return video_.get();
1840 break;
1841 case DemuxerStream::TEXT: {
1842 return new ChunkDemuxerStream(DemuxerStream::TEXT,
1843 splice_frames_enabled_);
1844 break;
1846 case DemuxerStream::UNKNOWN:
1847 case DemuxerStream::NUM_TYPES:
1848 NOTREACHED();
1849 return NULL;
1851 NOTREACHED();
1852 return NULL;
1855 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1856 const TextTrackConfig& config) {
1857 lock_.AssertAcquired();
1858 DCHECK_NE(state_, SHUTDOWN);
1859 host_->AddTextStream(text_stream, config);
1862 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1863 lock_.AssertAcquired();
1864 return source_state_map_.count(source_id) > 0u;
1867 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1868 DCHECK(duration_ != new_duration);
1869 user_specified_duration_ = -1;
1870 duration_ = new_duration;
1871 host_->SetDuration(new_duration);
1874 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration) {
1875 DCHECK(new_duration != kNoTimestamp());
1876 DCHECK(new_duration != kInfiniteDuration());
1878 // Per April 1, 2014 MSE spec editor's draft:
1879 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1880 // media-source.html#sourcebuffer-coded-frame-processing
1881 // 5. If the media segment contains data beyond the current duration, then run
1882 // the duration change algorithm with new duration set to the maximum of
1883 // the current duration and the group end timestamp.
1885 if (new_duration <= duration_)
1886 return;
1888 DVLOG(2) << __FUNCTION__ << ": Increasing duration: "
1889 << duration_.InSecondsF() << " -> " << new_duration.InSecondsF();
1891 UpdateDuration(new_duration);
1894 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1895 lock_.AssertAcquired();
1897 TimeDelta max_duration;
1899 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1900 itr != source_state_map_.end(); ++itr) {
1901 max_duration = std::max(max_duration,
1902 itr->second->GetMaxBufferedDuration());
1905 if (max_duration == TimeDelta())
1906 return;
1908 if (max_duration < duration_)
1909 UpdateDuration(max_duration);
1912 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1913 base::AutoLock auto_lock(lock_);
1914 return GetBufferedRanges_Locked();
1917 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1918 lock_.AssertAcquired();
1920 bool ended = state_ == ENDED;
1921 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1922 // we'll need to update this loop to only add ranges from active sources.
1923 RangesList ranges_list;
1924 for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1925 itr != source_state_map_.end(); ++itr) {
1926 ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1929 return ComputeIntersection(ranges_list, ended);
1932 void ChunkDemuxer::StartReturningData() {
1933 for (SourceStateMap::iterator itr = source_state_map_.begin();
1934 itr != source_state_map_.end(); ++itr) {
1935 itr->second->StartReturningData();
1939 void ChunkDemuxer::AbortPendingReads() {
1940 for (SourceStateMap::iterator itr = source_state_map_.begin();
1941 itr != source_state_map_.end(); ++itr) {
1942 itr->second->AbortReads();
1946 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1947 for (SourceStateMap::iterator itr = source_state_map_.begin();
1948 itr != source_state_map_.end(); ++itr) {
1949 itr->second->Seek(seek_time);
1953 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1954 for (SourceStateMap::iterator itr = source_state_map_.begin();
1955 itr != source_state_map_.end(); ++itr) {
1956 itr->second->CompletePendingReadIfPossible();
1960 void ChunkDemuxer::ShutdownAllStreams() {
1961 for (SourceStateMap::iterator itr = source_state_map_.begin();
1962 itr != source_state_map_.end(); ++itr) {
1963 itr->second->Shutdown();
1967 } // namespace media