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"
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/video_decoder_config.h"
19 #include "media/filters/frame_processor.h"
20 #include "media/filters/stream_parser_factory.h"
22 using base::TimeDelta
;
26 static TimeDelta
EndTimestamp(const StreamParser::BufferQueue
& queue
) {
27 return queue
.back()->timestamp() + queue
.back()->duration();
30 // List of time ranges for each SourceBuffer.
31 typedef std::list
<Ranges
<TimeDelta
> > RangesList
;
32 static Ranges
<TimeDelta
> ComputeIntersection(const RangesList
& activeRanges
,
34 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
35 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
37 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
38 // TimeRanges object and abort these steps.
39 if (activeRanges
.empty())
40 return Ranges
<TimeDelta
>();
42 // Step 2: Let active ranges be the ranges returned by buffered for each
43 // SourceBuffer object in activeSourceBuffers.
44 // Step 3: Let highest end time be the largest range end time in the active
46 TimeDelta highest_end_time
;
47 for (RangesList::const_iterator itr
= activeRanges
.begin();
48 itr
!= activeRanges
.end(); ++itr
) {
52 highest_end_time
= std::max(highest_end_time
, itr
->end(itr
->size() - 1));
55 // Step 4: Let intersection ranges equal a TimeRange object containing a
56 // single range from 0 to highest end time.
57 Ranges
<TimeDelta
> intersection_ranges
;
58 intersection_ranges
.Add(TimeDelta(), highest_end_time
);
60 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
62 for (RangesList::const_iterator itr
= activeRanges
.begin();
63 itr
!= activeRanges
.end(); ++itr
) {
64 // Step 5.1: Let source ranges equal the ranges returned by the buffered
65 // attribute on the current SourceBuffer.
66 Ranges
<TimeDelta
> source_ranges
= *itr
;
68 // Step 5.2: If readyState is "ended", then set the end time on the last
69 // range in source ranges to highest end time.
70 if (ended
&& source_ranges
.size() > 0u) {
71 source_ranges
.Add(source_ranges
.start(source_ranges
.size() - 1),
75 // Step 5.3: Let new intersection ranges equal the intersection between
76 // the intersection ranges and the source ranges.
77 // Step 5.4: Replace the ranges in intersection ranges with the new
78 // intersection ranges.
79 intersection_ranges
= intersection_ranges
.IntersectionWith(source_ranges
);
82 return intersection_ranges
;
85 // Contains state belonging to a source id.
88 // Callback signature used to create ChunkDemuxerStreams.
89 typedef base::Callback
<ChunkDemuxerStream
*(
90 DemuxerStream::Type
)> CreateDemuxerStreamCB
;
92 typedef ChunkDemuxer::InitSegmentReceivedCB InitSegmentReceivedCB
;
94 typedef base::Callback
<void(
95 ChunkDemuxerStream
*, const TextTrackConfig
&)> NewTextTrackCB
;
98 scoped_ptr
<StreamParser
> stream_parser
,
99 scoped_ptr
<FrameProcessor
> frame_processor
, const LogCB
& log_cb
,
100 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
101 const scoped_refptr
<MediaLog
>& media_log
);
105 void Init(const StreamParser::InitCB
& init_cb
,
108 const StreamParser::EncryptedMediaInitDataCB
&
109 encrypted_media_init_data_cb
,
110 const NewTextTrackCB
& new_text_track_cb
);
112 // Appends new data to the StreamParser.
113 // Returns true if the data was successfully appended. Returns false if an
114 // error occurred. |*timestamp_offset| is used and possibly updated by the
115 // append. |append_window_start| and |append_window_end| correspond to the MSE
116 // spec's similarly named source buffer attributes that are used in coded
117 // frame processing. |init_segment_received_cb| is run for each new fully
118 // parsed initialization segment.
119 bool Append(const uint8
* data
,
121 TimeDelta append_window_start
,
122 TimeDelta append_window_end
,
123 TimeDelta
* timestamp_offset
,
124 const InitSegmentReceivedCB
& init_segment_received_cb
);
126 // Aborts the current append sequence and resets the parser.
127 void Abort(TimeDelta append_window_start
,
128 TimeDelta append_window_end
,
129 TimeDelta
* timestamp_offset
);
131 // Calls Remove(|start|, |end|, |duration|) on all
132 // ChunkDemuxerStreams managed by this object.
133 void Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
);
135 // Returns true if currently parsing a media segment, or false otherwise.
136 bool parsing_media_segment() const { return parsing_media_segment_
; }
138 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
139 void SetSequenceMode(bool sequence_mode
);
141 // Signals the coded frame processor to update its group start timestamp to be
142 // |timestamp_offset| if it is in sequence append mode.
143 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset
);
145 // Returns the range of buffered data in this source, capped at |duration|.
146 // |ended| - Set to true if end of stream has been signaled and the special
147 // end of stream range logic needs to be executed.
148 Ranges
<TimeDelta
> GetBufferedRanges(TimeDelta duration
, bool ended
) const;
150 // Returns the highest buffered duration across all streams managed
152 // Returns TimeDelta() if none of the streams contain buffered data.
153 TimeDelta
GetMaxBufferedDuration() const;
155 // Helper methods that call methods with similar names on all the
156 // ChunkDemuxerStreams managed by this object.
157 void StartReturningData();
159 void Seek(TimeDelta seek_time
);
160 void CompletePendingReadIfPossible();
161 void OnSetDuration(TimeDelta duration
);
162 void MarkEndOfStream();
163 void UnmarkEndOfStream();
165 // Sets the memory limit on each stream of a specific type.
166 // |memory_limit| is the maximum number of bytes each stream of type |type|
167 // is allowed to hold in its buffer.
168 void SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
);
169 bool IsSeekWaitingForData() const;
172 // Called by the |stream_parser_| when a new initialization segment is
174 // Returns true on a successful call. Returns false if an error occurred while
175 // processing decoder configurations.
176 bool OnNewConfigs(bool allow_audio
, bool allow_video
,
177 const AudioDecoderConfig
& audio_config
,
178 const VideoDecoderConfig
& video_config
,
179 const StreamParser::TextTrackConfigMap
& text_configs
);
181 // Called by the |stream_parser_| at the beginning of a new media segment.
182 void OnNewMediaSegment();
184 // Called by the |stream_parser_| at the end of a media segment.
185 void OnEndOfMediaSegment();
187 // Called by the |stream_parser_| when new buffers have been parsed.
188 // It processes the new buffers using |frame_processor_|, which includes
189 // appending the processed frames to associated demuxer streams for each
191 // Returns true on a successful call. Returns false if an error occurred while
192 // processing the buffers.
193 bool OnNewBuffers(const StreamParser::BufferQueue
& audio_buffers
,
194 const StreamParser::BufferQueue
& video_buffers
,
195 const StreamParser::TextBufferQueueMap
& text_map
);
197 void OnSourceInitDone(const StreamParser::InitParameters
& params
);
199 CreateDemuxerStreamCB create_demuxer_stream_cb_
;
200 NewTextTrackCB new_text_track_cb_
;
202 // During Append(), if OnNewBuffers() coded frame processing updates the
203 // timestamp offset then |*timestamp_offset_during_append_| is also updated
204 // so Append()'s caller can know the new offset. This pointer is only non-NULL
205 // during the lifetime of an Append() call.
206 TimeDelta
* timestamp_offset_during_append_
;
208 // During Append(), coded frame processing triggered by OnNewBuffers()
209 // requires these two attributes. These are only valid during the lifetime of
211 TimeDelta append_window_start_during_append_
;
212 TimeDelta append_window_end_during_append_
;
214 // Set to true if the next buffers appended within the append window
215 // represent the start of a new media segment. This flag being set
216 // triggers a call to |new_segment_cb_| when the new buffers are
217 // appended. The flag is set on actual media segment boundaries and
218 // when the "append window" filtering causes discontinuities in the
220 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
221 // processing's discontinuity logic is enough. See http://crbug.com/351489.
222 bool new_media_segment_
;
224 // Keeps track of whether a media segment is being parsed.
225 bool parsing_media_segment_
;
227 // The object used to parse appended data.
228 scoped_ptr
<StreamParser
> stream_parser_
;
230 ChunkDemuxerStream
* audio_
; // Not owned by |this|.
231 ChunkDemuxerStream
* video_
; // Not owned by |this|.
233 typedef std::map
<StreamParser::TrackId
, ChunkDemuxerStream
*> TextStreamMap
;
234 TextStreamMap text_stream_map_
; // |this| owns the map's stream pointers.
236 scoped_ptr
<FrameProcessor
> frame_processor_
;
238 scoped_refptr
<MediaLog
> media_log_
;
239 StreamParser::InitCB init_cb_
;
241 // During Append(), OnNewConfigs() will trigger the initialization segment
242 // received algorithm. This callback is only non-NULL during the lifetime of
243 // an Append() call. Note, the MSE spec explicitly disallows this algorithm
244 // during an Abort(), since Abort() is allowed only to emit coded frames, and
245 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
246 InitSegmentReceivedCB init_segment_received_cb_
;
248 // Indicates that timestampOffset should be updated automatically during
249 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
250 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
251 // changes to MSE spec. See http://crbug.com/371499.
252 bool auto_update_timestamp_offset_
;
254 DISALLOW_COPY_AND_ASSIGN(SourceState
);
257 SourceState::SourceState(scoped_ptr
<StreamParser
> stream_parser
,
258 scoped_ptr
<FrameProcessor
> frame_processor
,
260 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
261 const scoped_refptr
<MediaLog
>& media_log
)
262 : create_demuxer_stream_cb_(create_demuxer_stream_cb
),
263 timestamp_offset_during_append_(NULL
),
264 new_media_segment_(false),
265 parsing_media_segment_(false),
266 stream_parser_(stream_parser
.release()),
269 frame_processor_(frame_processor
.release()),
271 media_log_(media_log
),
272 auto_update_timestamp_offset_(false) {
273 DCHECK(!create_demuxer_stream_cb_
.is_null());
274 DCHECK(frame_processor_
);
277 SourceState::~SourceState() {
280 STLDeleteValues(&text_stream_map_
);
283 void SourceState::Init(
284 const StreamParser::InitCB
& init_cb
,
287 const StreamParser::EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
288 const NewTextTrackCB
& new_text_track_cb
) {
289 new_text_track_cb_
= new_text_track_cb
;
292 stream_parser_
->Init(
293 base::Bind(&SourceState::OnSourceInitDone
, base::Unretained(this)),
294 base::Bind(&SourceState::OnNewConfigs
, base::Unretained(this),
295 allow_audio
, allow_video
),
296 base::Bind(&SourceState::OnNewBuffers
, base::Unretained(this)),
297 new_text_track_cb_
.is_null(), encrypted_media_init_data_cb
,
298 base::Bind(&SourceState::OnNewMediaSegment
, base::Unretained(this)),
299 base::Bind(&SourceState::OnEndOfMediaSegment
, base::Unretained(this)),
303 void SourceState::SetSequenceMode(bool sequence_mode
) {
304 DCHECK(!parsing_media_segment_
);
306 frame_processor_
->SetSequenceMode(sequence_mode
);
309 void SourceState::SetGroupStartTimestampIfInSequenceMode(
310 base::TimeDelta timestamp_offset
) {
311 DCHECK(!parsing_media_segment_
);
313 frame_processor_
->SetGroupStartTimestampIfInSequenceMode(timestamp_offset
);
316 bool SourceState::Append(
319 TimeDelta append_window_start
,
320 TimeDelta append_window_end
,
321 TimeDelta
* timestamp_offset
,
322 const InitSegmentReceivedCB
& init_segment_received_cb
) {
323 DCHECK(timestamp_offset
);
324 DCHECK(!timestamp_offset_during_append_
);
325 DCHECK(!init_segment_received_cb
.is_null());
326 DCHECK(init_segment_received_cb_
.is_null());
327 append_window_start_during_append_
= append_window_start
;
328 append_window_end_during_append_
= append_window_end
;
329 timestamp_offset_during_append_
= timestamp_offset
;
330 init_segment_received_cb_
= init_segment_received_cb
;
332 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
333 // append window and timestamp offset pointer. See http://crbug.com/351454.
334 bool result
= stream_parser_
->Parse(data
, length
);
336 MEDIA_LOG(ERROR
, log_cb_
)
337 << __FUNCTION__
<< ": stream parsing failed."
338 << " Data size=" << length
339 << " append_window_start=" << append_window_start
.InSecondsF()
340 << " append_window_end=" << append_window_end
.InSecondsF();
342 timestamp_offset_during_append_
= NULL
;
343 init_segment_received_cb_
.Reset();
347 void SourceState::Abort(TimeDelta append_window_start
,
348 TimeDelta append_window_end
,
349 base::TimeDelta
* timestamp_offset
) {
350 DCHECK(timestamp_offset
);
351 DCHECK(!timestamp_offset_during_append_
);
352 timestamp_offset_during_append_
= timestamp_offset
;
353 append_window_start_during_append_
= append_window_start
;
354 append_window_end_during_append_
= append_window_end
;
356 stream_parser_
->Flush();
357 timestamp_offset_during_append_
= NULL
;
359 frame_processor_
->Reset();
360 parsing_media_segment_
= false;
363 void SourceState::Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
) {
365 audio_
->Remove(start
, end
, duration
);
368 video_
->Remove(start
, end
, duration
);
370 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
371 itr
!= text_stream_map_
.end(); ++itr
) {
372 itr
->second
->Remove(start
, end
, duration
);
376 Ranges
<TimeDelta
> SourceState::GetBufferedRanges(TimeDelta duration
,
378 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
379 // this code to only add ranges from active tracks.
380 RangesList ranges_list
;
382 ranges_list
.push_back(audio_
->GetBufferedRanges(duration
));
385 ranges_list
.push_back(video_
->GetBufferedRanges(duration
));
387 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
388 itr
!= text_stream_map_
.end(); ++itr
) {
389 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration
));
392 return ComputeIntersection(ranges_list
, ended
);
395 TimeDelta
SourceState::GetMaxBufferedDuration() const {
396 TimeDelta max_duration
;
399 max_duration
= std::max(max_duration
, audio_
->GetBufferedDuration());
402 max_duration
= std::max(max_duration
, video_
->GetBufferedDuration());
404 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
405 itr
!= text_stream_map_
.end(); ++itr
) {
406 max_duration
= std::max(max_duration
, itr
->second
->GetBufferedDuration());
412 void SourceState::StartReturningData() {
414 audio_
->StartReturningData();
417 video_
->StartReturningData();
419 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
420 itr
!= text_stream_map_
.end(); ++itr
) {
421 itr
->second
->StartReturningData();
425 void SourceState::AbortReads() {
427 audio_
->AbortReads();
430 video_
->AbortReads();
432 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
433 itr
!= text_stream_map_
.end(); ++itr
) {
434 itr
->second
->AbortReads();
438 void SourceState::Seek(TimeDelta seek_time
) {
440 audio_
->Seek(seek_time
);
443 video_
->Seek(seek_time
);
445 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
446 itr
!= text_stream_map_
.end(); ++itr
) {
447 itr
->second
->Seek(seek_time
);
451 void SourceState::CompletePendingReadIfPossible() {
453 audio_
->CompletePendingReadIfPossible();
456 video_
->CompletePendingReadIfPossible();
458 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
459 itr
!= text_stream_map_
.end(); ++itr
) {
460 itr
->second
->CompletePendingReadIfPossible();
464 void SourceState::OnSetDuration(TimeDelta duration
) {
466 audio_
->OnSetDuration(duration
);
469 video_
->OnSetDuration(duration
);
471 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
472 itr
!= text_stream_map_
.end(); ++itr
) {
473 itr
->second
->OnSetDuration(duration
);
477 void SourceState::MarkEndOfStream() {
479 audio_
->MarkEndOfStream();
482 video_
->MarkEndOfStream();
484 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
485 itr
!= text_stream_map_
.end(); ++itr
) {
486 itr
->second
->MarkEndOfStream();
490 void SourceState::UnmarkEndOfStream() {
492 audio_
->UnmarkEndOfStream();
495 video_
->UnmarkEndOfStream();
497 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
498 itr
!= text_stream_map_
.end(); ++itr
) {
499 itr
->second
->UnmarkEndOfStream();
503 void SourceState::Shutdown() {
510 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
511 itr
!= text_stream_map_
.end(); ++itr
) {
512 itr
->second
->Shutdown();
516 void SourceState::SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
) {
518 case DemuxerStream::AUDIO
:
520 audio_
->set_memory_limit(memory_limit
);
522 case DemuxerStream::VIDEO
:
524 video_
->set_memory_limit(memory_limit
);
526 case DemuxerStream::TEXT
:
527 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
528 itr
!= text_stream_map_
.end(); ++itr
) {
529 itr
->second
->set_memory_limit(memory_limit
);
532 case DemuxerStream::UNKNOWN
:
533 case DemuxerStream::NUM_TYPES
:
539 bool SourceState::IsSeekWaitingForData() const {
540 if (audio_
&& audio_
->IsSeekWaitingForData())
543 if (video_
&& video_
->IsSeekWaitingForData())
546 // NOTE: We are intentionally not checking the text tracks
547 // because text tracks are discontinuous and may not have data
548 // for the seek position. This is ok and playback should not be
549 // stalled because we don't have cues. If cues, with timestamps after
550 // the seek time, eventually arrive they will be delivered properly
551 // in response to ChunkDemuxerStream::Read() calls.
556 bool SourceState::OnNewConfigs(
557 bool allow_audio
, bool allow_video
,
558 const AudioDecoderConfig
& audio_config
,
559 const VideoDecoderConfig
& video_config
,
560 const StreamParser::TextTrackConfigMap
& text_configs
) {
561 DVLOG(1) << "OnNewConfigs(" << allow_audio
<< ", " << allow_video
562 << ", " << audio_config
.IsValidConfig()
563 << ", " << video_config
.IsValidConfig() << ")";
564 DCHECK(!init_segment_received_cb_
.is_null());
566 if (!audio_config
.IsValidConfig() && !video_config
.IsValidConfig()) {
567 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
571 // Signal an error if we get configuration info for stream types that weren't
572 // specified in AddId() or more configs after a stream is initialized.
573 if (allow_audio
!= audio_config
.IsValidConfig()) {
574 MEDIA_LOG(ERROR
, log_cb_
)
575 << "Initialization segment"
576 << (audio_config
.IsValidConfig() ? " has" : " does not have")
577 << " an audio track, but the mimetype"
578 << (allow_audio
? " specifies" : " does not specify")
579 << " an audio codec.";
583 if (allow_video
!= video_config
.IsValidConfig()) {
584 MEDIA_LOG(ERROR
, log_cb_
)
585 << "Initialization segment"
586 << (video_config
.IsValidConfig() ? " has" : " does not have")
587 << " a video track, but the mimetype"
588 << (allow_video
? " specifies" : " does not specify")
589 << " a video codec.";
594 if (audio_config
.IsValidConfig()) {
596 media_log_
->SetBooleanProperty("found_audio_stream", true);
599 audio_
->audio_decoder_config().codec() != audio_config
.codec()) {
600 media_log_
->SetStringProperty("audio_codec_name",
601 audio_config
.GetHumanReadableCodecName());
605 audio_
= create_demuxer_stream_cb_
.Run(DemuxerStream::AUDIO
);
608 DVLOG(1) << "Failed to create an audio stream.";
612 if (!frame_processor_
->AddTrack(FrameProcessor::kAudioTrackId
, audio_
)) {
613 DVLOG(1) << "Failed to add audio track to frame processor.";
618 frame_processor_
->OnPossibleAudioConfigUpdate(audio_config
);
619 success
&= audio_
->UpdateAudioConfig(audio_config
, log_cb_
);
622 if (video_config
.IsValidConfig()) {
624 media_log_
->SetBooleanProperty("found_video_stream", true);
627 video_
->video_decoder_config().codec() != video_config
.codec()) {
628 media_log_
->SetStringProperty("video_codec_name",
629 video_config
.GetHumanReadableCodecName());
633 video_
= create_demuxer_stream_cb_
.Run(DemuxerStream::VIDEO
);
636 DVLOG(1) << "Failed to create a video stream.";
640 if (!frame_processor_
->AddTrack(FrameProcessor::kVideoTrackId
, video_
)) {
641 DVLOG(1) << "Failed to add video track to frame processor.";
646 success
&= video_
->UpdateVideoConfig(video_config
, log_cb_
);
649 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr
;
650 if (text_stream_map_
.empty()) {
651 for (TextConfigItr itr
= text_configs
.begin();
652 itr
!= text_configs
.end(); ++itr
) {
653 ChunkDemuxerStream
* const text_stream
=
654 create_demuxer_stream_cb_
.Run(DemuxerStream::TEXT
);
655 if (!frame_processor_
->AddTrack(itr
->first
, text_stream
)) {
657 MEDIA_LOG(ERROR
, log_cb_
) << "Failed to add text track ID "
658 << itr
->first
<< " to frame processor.";
661 text_stream
->UpdateTextConfig(itr
->second
, log_cb_
);
662 text_stream_map_
[itr
->first
] = text_stream
;
663 new_text_track_cb_
.Run(text_stream
, itr
->second
);
666 const size_t text_count
= text_stream_map_
.size();
667 if (text_configs
.size() != text_count
) {
669 MEDIA_LOG(ERROR
, log_cb_
) << "The number of text track configs changed.";
670 } else if (text_count
== 1) {
671 TextConfigItr config_itr
= text_configs
.begin();
672 TextStreamMap::iterator stream_itr
= text_stream_map_
.begin();
673 ChunkDemuxerStream
* text_stream
= stream_itr
->second
;
674 TextTrackConfig old_config
= text_stream
->text_track_config();
675 TextTrackConfig
new_config(config_itr
->second
.kind(),
676 config_itr
->second
.label(),
677 config_itr
->second
.language(),
679 if (!new_config
.Matches(old_config
)) {
681 MEDIA_LOG(ERROR
, log_cb_
)
682 << "New text track config does not match old one.";
684 StreamParser::TrackId old_id
= stream_itr
->first
;
685 StreamParser::TrackId new_id
= config_itr
->first
;
686 if (new_id
!= old_id
) {
687 if (frame_processor_
->UpdateTrack(old_id
, new_id
)) {
688 text_stream_map_
.clear();
689 text_stream_map_
[config_itr
->first
] = text_stream
;
692 MEDIA_LOG(ERROR
, log_cb_
)
693 << "Error remapping single text track number";
698 for (TextConfigItr config_itr
= text_configs
.begin();
699 config_itr
!= text_configs
.end(); ++config_itr
) {
700 TextStreamMap::iterator stream_itr
=
701 text_stream_map_
.find(config_itr
->first
);
702 if (stream_itr
== text_stream_map_
.end()) {
704 MEDIA_LOG(ERROR
, log_cb_
)
705 << "Unexpected text track configuration for track ID "
706 << config_itr
->first
;
710 const TextTrackConfig
& new_config
= config_itr
->second
;
711 ChunkDemuxerStream
* stream
= stream_itr
->second
;
712 TextTrackConfig old_config
= stream
->text_track_config();
713 if (!new_config
.Matches(old_config
)) {
715 MEDIA_LOG(ERROR
, log_cb_
) << "New text track config for track ID "
717 << " does not match old one.";
724 frame_processor_
->SetAllTrackBuffersNeedRandomAccessPoint();
726 DVLOG(1) << "OnNewConfigs() : " << (success
? "success" : "failed");
728 init_segment_received_cb_
.Run();
733 void SourceState::OnNewMediaSegment() {
734 DVLOG(2) << "OnNewMediaSegment()";
735 parsing_media_segment_
= true;
736 new_media_segment_
= true;
739 void SourceState::OnEndOfMediaSegment() {
740 DVLOG(2) << "OnEndOfMediaSegment()";
741 parsing_media_segment_
= false;
742 new_media_segment_
= false;
745 bool SourceState::OnNewBuffers(
746 const StreamParser::BufferQueue
& audio_buffers
,
747 const StreamParser::BufferQueue
& video_buffers
,
748 const StreamParser::TextBufferQueueMap
& text_map
) {
749 DVLOG(2) << "OnNewBuffers()";
750 DCHECK(timestamp_offset_during_append_
);
751 DCHECK(parsing_media_segment_
);
753 const TimeDelta timestamp_offset_before_processing
=
754 *timestamp_offset_during_append_
;
756 // Calculate the new timestamp offset for audio/video tracks if the stream
757 // parser has requested automatic updates.
758 TimeDelta new_timestamp_offset
= timestamp_offset_before_processing
;
759 if (auto_update_timestamp_offset_
) {
760 const bool have_audio_buffers
= !audio_buffers
.empty();
761 const bool have_video_buffers
= !video_buffers
.empty();
762 if (have_audio_buffers
&& have_video_buffers
) {
763 new_timestamp_offset
+=
764 std::min(EndTimestamp(audio_buffers
), EndTimestamp(video_buffers
));
765 } else if (have_audio_buffers
) {
766 new_timestamp_offset
+= EndTimestamp(audio_buffers
);
767 } else if (have_video_buffers
) {
768 new_timestamp_offset
+= EndTimestamp(video_buffers
);
772 if (!frame_processor_
->ProcessFrames(audio_buffers
,
775 append_window_start_during_append_
,
776 append_window_end_during_append_
,
778 timestamp_offset_during_append_
)) {
782 // Only update the timestamp offset if the frame processor hasn't already.
783 if (auto_update_timestamp_offset_
&&
784 timestamp_offset_before_processing
== *timestamp_offset_during_append_
) {
785 *timestamp_offset_during_append_
= new_timestamp_offset
;
791 void SourceState::OnSourceInitDone(const StreamParser::InitParameters
& params
) {
792 auto_update_timestamp_offset_
= params
.auto_update_timestamp_offset
;
793 base::ResetAndReturn(&init_cb_
).Run(params
);
796 ChunkDemuxerStream::ChunkDemuxerStream(Type type
,
797 bool splice_frames_enabled
)
799 liveness_(DemuxerStream::LIVENESS_UNKNOWN
),
800 state_(UNINITIALIZED
),
801 splice_frames_enabled_(splice_frames_enabled
),
802 partial_append_window_trimming_enabled_(false) {
805 void ChunkDemuxerStream::StartReturningData() {
806 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
807 base::AutoLock
auto_lock(lock_
);
808 DCHECK(read_cb_
.is_null());
809 ChangeState_Locked(RETURNING_DATA_FOR_READS
);
812 void ChunkDemuxerStream::AbortReads() {
813 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
814 base::AutoLock
auto_lock(lock_
);
815 ChangeState_Locked(RETURNING_ABORT_FOR_READS
);
816 if (!read_cb_
.is_null())
817 base::ResetAndReturn(&read_cb_
).Run(kAborted
, NULL
);
820 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
821 base::AutoLock
auto_lock(lock_
);
822 if (read_cb_
.is_null())
825 CompletePendingReadIfPossible_Locked();
828 void ChunkDemuxerStream::Shutdown() {
829 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
830 base::AutoLock
auto_lock(lock_
);
831 ChangeState_Locked(SHUTDOWN
);
833 // Pass an end of stream buffer to the pending callback to signal that no more
834 // data will be sent.
835 if (!read_cb_
.is_null()) {
836 base::ResetAndReturn(&read_cb_
).Run(DemuxerStream::kOk
,
837 StreamParserBuffer::CreateEOSBuffer());
841 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
842 base::AutoLock
auto_lock(lock_
);
844 // This method should not be called for text tracks. See the note in
845 // SourceState::IsSeekWaitingForData().
846 DCHECK_NE(type_
, DemuxerStream::TEXT
);
848 return stream_
->IsSeekPending();
851 void ChunkDemuxerStream::Seek(TimeDelta time
) {
852 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time
.InSecondsF() << ")";
853 base::AutoLock
auto_lock(lock_
);
854 DCHECK(read_cb_
.is_null());
855 DCHECK(state_
== UNINITIALIZED
|| state_
== RETURNING_ABORT_FOR_READS
)
861 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue
& buffers
) {
865 base::AutoLock
auto_lock(lock_
);
866 DCHECK_NE(state_
, SHUTDOWN
);
867 if (!stream_
->Append(buffers
)) {
868 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
872 if (!read_cb_
.is_null())
873 CompletePendingReadIfPossible_Locked();
878 void ChunkDemuxerStream::Remove(TimeDelta start
, TimeDelta end
,
879 TimeDelta duration
) {
880 base::AutoLock
auto_lock(lock_
);
881 stream_
->Remove(start
, end
, duration
);
884 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration
) {
885 base::AutoLock
auto_lock(lock_
);
886 stream_
->OnSetDuration(duration
);
889 Ranges
<TimeDelta
> ChunkDemuxerStream::GetBufferedRanges(
890 TimeDelta duration
) const {
891 base::AutoLock
auto_lock(lock_
);
894 // Since text tracks are discontinuous and the lack of cues should not block
895 // playback, report the buffered range for text tracks as [0, |duration|) so
896 // that intesections with audio & video tracks are computed correctly when
897 // no cues are present.
898 Ranges
<TimeDelta
> text_range
;
899 text_range
.Add(TimeDelta(), duration
);
903 Ranges
<TimeDelta
> range
= stream_
->GetBufferedTime();
905 if (range
.size() == 0u)
908 // Clamp the end of the stream's buffered ranges to fit within the duration.
909 // This can be done by intersecting the stream's range with the valid time
911 Ranges
<TimeDelta
> valid_time_range
;
912 valid_time_range
.Add(range
.start(0), duration
);
913 return range
.IntersectionWith(valid_time_range
);
916 TimeDelta
ChunkDemuxerStream::GetBufferedDuration() const {
917 return stream_
->GetBufferedDuration();
920 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp
) {
921 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
922 << start_timestamp
.InSecondsF() << ")";
923 base::AutoLock
auto_lock(lock_
);
924 stream_
->OnNewMediaSegment(start_timestamp
);
927 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig
& config
,
928 const LogCB
& log_cb
) {
929 DCHECK(config
.IsValidConfig());
930 DCHECK_EQ(type_
, AUDIO
);
931 base::AutoLock
auto_lock(lock_
);
933 DCHECK_EQ(state_
, UNINITIALIZED
);
935 // On platforms which support splice frames, enable splice frames and
936 // partial append window support for most codecs (notably: not opus).
937 const bool codec_supported
= config
.codec() == kCodecMP3
||
938 config
.codec() == kCodecAAC
||
939 config
.codec() == kCodecVorbis
;
940 splice_frames_enabled_
= splice_frames_enabled_
&& codec_supported
;
941 partial_append_window_trimming_enabled_
=
942 splice_frames_enabled_
&& codec_supported
;
945 new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
949 return stream_
->UpdateAudioConfig(config
);
952 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig
& config
,
953 const LogCB
& log_cb
) {
954 DCHECK(config
.IsValidConfig());
955 DCHECK_EQ(type_
, VIDEO
);
956 base::AutoLock
auto_lock(lock_
);
959 DCHECK_EQ(state_
, UNINITIALIZED
);
961 new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
965 return stream_
->UpdateVideoConfig(config
);
968 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig
& config
,
969 const LogCB
& log_cb
) {
970 DCHECK_EQ(type_
, TEXT
);
971 base::AutoLock
auto_lock(lock_
);
973 DCHECK_EQ(state_
, UNINITIALIZED
);
974 stream_
.reset(new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
977 void ChunkDemuxerStream::MarkEndOfStream() {
978 base::AutoLock
auto_lock(lock_
);
979 stream_
->MarkEndOfStream();
982 void ChunkDemuxerStream::UnmarkEndOfStream() {
983 base::AutoLock
auto_lock(lock_
);
984 stream_
->UnmarkEndOfStream();
987 // DemuxerStream methods.
988 void ChunkDemuxerStream::Read(const ReadCB
& read_cb
) {
989 base::AutoLock
auto_lock(lock_
);
990 DCHECK_NE(state_
, UNINITIALIZED
);
991 DCHECK(read_cb_
.is_null());
993 read_cb_
= BindToCurrentLoop(read_cb
);
994 CompletePendingReadIfPossible_Locked();
997 DemuxerStream::Type
ChunkDemuxerStream::type() const { return type_
; }
999 DemuxerStream::Liveness
ChunkDemuxerStream::liveness() const {
1000 base::AutoLock
auto_lock(lock_
);
1004 AudioDecoderConfig
ChunkDemuxerStream::audio_decoder_config() {
1005 CHECK_EQ(type_
, AUDIO
);
1006 base::AutoLock
auto_lock(lock_
);
1007 return stream_
->GetCurrentAudioDecoderConfig();
1010 VideoDecoderConfig
ChunkDemuxerStream::video_decoder_config() {
1011 CHECK_EQ(type_
, VIDEO
);
1012 base::AutoLock
auto_lock(lock_
);
1013 return stream_
->GetCurrentVideoDecoderConfig();
1016 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
1018 VideoRotation
ChunkDemuxerStream::video_rotation() {
1019 return VIDEO_ROTATION_0
;
1022 TextTrackConfig
ChunkDemuxerStream::text_track_config() {
1023 CHECK_EQ(type_
, TEXT
);
1024 base::AutoLock
auto_lock(lock_
);
1025 return stream_
->GetCurrentTextTrackConfig();
1028 void ChunkDemuxerStream::SetLiveness(Liveness liveness
) {
1029 base::AutoLock
auto_lock(lock_
);
1030 liveness_
= liveness
;
1033 void ChunkDemuxerStream::ChangeState_Locked(State state
) {
1034 lock_
.AssertAcquired();
1035 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1037 << " - " << state_
<< " -> " << state
;
1041 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1043 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1044 lock_
.AssertAcquired();
1045 DCHECK(!read_cb_
.is_null());
1047 DemuxerStream::Status status
;
1048 scoped_refptr
<StreamParserBuffer
> buffer
;
1054 case RETURNING_DATA_FOR_READS
:
1055 switch (stream_
->GetNextBuffer(&buffer
)) {
1056 case SourceBufferStream::kSuccess
:
1057 status
= DemuxerStream::kOk
;
1058 DVLOG(2) << __FUNCTION__
<< ": returning kOk, type " << type_
1059 << ", dts " << buffer
->GetDecodeTimestamp().InSecondsF()
1060 << ", pts " << buffer
->timestamp().InSecondsF()
1061 << ", dur " << buffer
->duration().InSecondsF()
1062 << ", key " << buffer
->is_key_frame();
1064 case SourceBufferStream::kNeedBuffer
:
1065 // Return early without calling |read_cb_| since we don't have
1066 // any data to return yet.
1067 DVLOG(2) << __FUNCTION__
<< ": returning kNeedBuffer, type "
1070 case SourceBufferStream::kEndOfStream
:
1071 status
= DemuxerStream::kOk
;
1072 buffer
= StreamParserBuffer::CreateEOSBuffer();
1073 DVLOG(2) << __FUNCTION__
<< ": returning kOk with EOS buffer, type "
1076 case SourceBufferStream::kConfigChange
:
1077 status
= kConfigChanged
;
1079 DVLOG(2) << __FUNCTION__
<< ": returning kConfigChange, type "
1084 case RETURNING_ABORT_FOR_READS
:
1085 // Null buffers should be returned in this state since we are waiting
1086 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1087 // because they are associated with the seek.
1088 status
= DemuxerStream::kAborted
;
1090 DVLOG(2) << __FUNCTION__
<< ": returning kAborted, type " << type_
;
1093 status
= DemuxerStream::kOk
;
1094 buffer
= StreamParserBuffer::CreateEOSBuffer();
1095 DVLOG(2) << __FUNCTION__
<< ": returning kOk with EOS buffer, type "
1100 base::ResetAndReturn(&read_cb_
).Run(status
, buffer
);
1103 ChunkDemuxer::ChunkDemuxer(
1104 const base::Closure
& open_cb
,
1105 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
1106 const LogCB
& log_cb
,
1107 const scoped_refptr
<MediaLog
>& media_log
,
1108 bool splice_frames_enabled
)
1109 : state_(WAITING_FOR_INIT
),
1110 cancel_next_seek_(false),
1113 encrypted_media_init_data_cb_(encrypted_media_init_data_cb
),
1114 enable_text_(false),
1116 media_log_(media_log
),
1117 duration_(kNoTimestamp()),
1118 user_specified_duration_(-1),
1119 liveness_(DemuxerStream::LIVENESS_UNKNOWN
),
1120 splice_frames_enabled_(splice_frames_enabled
) {
1121 DCHECK(!open_cb_
.is_null());
1122 DCHECK(!encrypted_media_init_data_cb_
.is_null());
1125 std::string
ChunkDemuxer::GetDisplayName() const {
1126 return "ChunkDemuxer";
1129 void ChunkDemuxer::Initialize(
1131 const PipelineStatusCB
& cb
,
1132 bool enable_text_tracks
) {
1133 DVLOG(1) << "Init()";
1135 base::AutoLock
auto_lock(lock_
);
1137 // The |init_cb_| must only be run after this method returns, so always post.
1138 init_cb_
= BindToCurrentLoop(cb
);
1139 if (state_
== SHUTDOWN
) {
1140 base::ResetAndReturn(&init_cb_
).Run(DEMUXER_ERROR_COULD_NOT_OPEN
);
1143 DCHECK_EQ(state_
, WAITING_FOR_INIT
);
1145 enable_text_
= enable_text_tracks
;
1147 ChangeState_Locked(INITIALIZING
);
1149 base::ResetAndReturn(&open_cb_
).Run();
1152 void ChunkDemuxer::Stop() {
1153 DVLOG(1) << "Stop()";
1157 void ChunkDemuxer::Seek(TimeDelta time
, const PipelineStatusCB
& cb
) {
1158 DVLOG(1) << "Seek(" << time
.InSecondsF() << ")";
1159 DCHECK(time
>= TimeDelta());
1161 base::AutoLock
auto_lock(lock_
);
1162 DCHECK(seek_cb_
.is_null());
1164 seek_cb_
= BindToCurrentLoop(cb
);
1165 if (state_
!= INITIALIZED
&& state_
!= ENDED
) {
1166 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_INVALID_STATE
);
1170 if (cancel_next_seek_
) {
1171 cancel_next_seek_
= false;
1172 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1176 SeekAllSources(time
);
1177 StartReturningData();
1179 if (IsSeekWaitingForData_Locked()) {
1180 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1184 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1187 // Demuxer implementation.
1188 base::Time
ChunkDemuxer::GetTimelineOffset() const {
1189 return timeline_offset_
;
1192 DemuxerStream
* ChunkDemuxer::GetStream(DemuxerStream::Type type
) {
1193 DCHECK_NE(type
, DemuxerStream::TEXT
);
1194 base::AutoLock
auto_lock(lock_
);
1195 if (type
== DemuxerStream::VIDEO
)
1196 return video_
.get();
1198 if (type
== DemuxerStream::AUDIO
)
1199 return audio_
.get();
1204 TimeDelta
ChunkDemuxer::GetStartTime() const {
1208 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time
) {
1209 DVLOG(1) << "StartWaitingForSeek()";
1210 base::AutoLock
auto_lock(lock_
);
1211 DCHECK(state_
== INITIALIZED
|| state_
== ENDED
|| state_
== SHUTDOWN
||
1212 state_
== PARSE_ERROR
) << state_
;
1213 DCHECK(seek_cb_
.is_null());
1215 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1218 AbortPendingReads();
1219 SeekAllSources(seek_time
);
1221 // Cancel state set in CancelPendingSeek() since we want to
1222 // accept the next Seek().
1223 cancel_next_seek_
= false;
1226 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time
) {
1227 base::AutoLock
auto_lock(lock_
);
1228 DCHECK_NE(state_
, INITIALIZING
);
1229 DCHECK(seek_cb_
.is_null() || IsSeekWaitingForData_Locked());
1231 if (cancel_next_seek_
)
1234 AbortPendingReads();
1235 SeekAllSources(seek_time
);
1237 if (seek_cb_
.is_null()) {
1238 cancel_next_seek_
= true;
1242 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1245 ChunkDemuxer::Status
ChunkDemuxer::AddId(const std::string
& id
,
1246 const std::string
& type
,
1247 std::vector
<std::string
>& codecs
) {
1248 base::AutoLock
auto_lock(lock_
);
1250 if ((state_
!= WAITING_FOR_INIT
&& state_
!= INITIALIZING
) || IsValidId(id
))
1251 return kReachedIdLimit
;
1253 bool has_audio
= false;
1254 bool has_video
= false;
1255 scoped_ptr
<media::StreamParser
> stream_parser(
1256 StreamParserFactory::Create(type
, codecs
, log_cb_
,
1257 &has_audio
, &has_video
));
1260 return ChunkDemuxer::kNotSupported
;
1262 if ((has_audio
&& !source_id_audio_
.empty()) ||
1263 (has_video
&& !source_id_video_
.empty()))
1264 return kReachedIdLimit
;
1267 source_id_audio_
= id
;
1270 source_id_video_
= id
;
1272 scoped_ptr
<FrameProcessor
> frame_processor(
1273 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary
,
1274 base::Unretained(this))));
1276 scoped_ptr
<SourceState
> source_state(
1277 new SourceState(stream_parser
.Pass(),
1278 frame_processor
.Pass(), log_cb_
,
1279 base::Bind(&ChunkDemuxer::CreateDemuxerStream
,
1280 base::Unretained(this)),
1283 SourceState::NewTextTrackCB new_text_track_cb
;
1286 new_text_track_cb
= base::Bind(&ChunkDemuxer::OnNewTextTrack
,
1287 base::Unretained(this));
1291 base::Bind(&ChunkDemuxer::OnSourceInitDone
, base::Unretained(this)),
1292 has_audio
, has_video
, encrypted_media_init_data_cb_
, new_text_track_cb
);
1294 source_state_map_
[id
] = source_state
.release();
1298 void ChunkDemuxer::RemoveId(const std::string
& id
) {
1299 base::AutoLock
auto_lock(lock_
);
1300 CHECK(IsValidId(id
));
1302 delete source_state_map_
[id
];
1303 source_state_map_
.erase(id
);
1305 if (source_id_audio_
== id
)
1306 source_id_audio_
.clear();
1308 if (source_id_video_
== id
)
1309 source_id_video_
.clear();
1312 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges(const std::string
& id
) const {
1313 base::AutoLock
auto_lock(lock_
);
1314 DCHECK(!id
.empty());
1316 SourceStateMap::const_iterator itr
= source_state_map_
.find(id
);
1318 DCHECK(itr
!= source_state_map_
.end());
1319 return itr
->second
->GetBufferedRanges(duration_
, state_
== ENDED
);
1322 void ChunkDemuxer::AppendData(
1323 const std::string
& id
,
1326 TimeDelta append_window_start
,
1327 TimeDelta append_window_end
,
1328 TimeDelta
* timestamp_offset
,
1329 const InitSegmentReceivedCB
& init_segment_received_cb
) {
1330 DVLOG(1) << "AppendData(" << id
<< ", " << length
<< ")";
1332 DCHECK(!id
.empty());
1333 DCHECK(timestamp_offset
);
1334 DCHECK(!init_segment_received_cb
.is_null());
1336 Ranges
<TimeDelta
> ranges
;
1339 base::AutoLock
auto_lock(lock_
);
1340 DCHECK_NE(state_
, ENDED
);
1342 // Capture if any of the SourceBuffers are waiting for data before we start
1344 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1354 DCHECK(IsValidId(id
));
1355 if (!source_state_map_
[id
]->Append(data
, length
,
1356 append_window_start
,
1359 init_segment_received_cb
)) {
1360 ReportError_Locked(PIPELINE_ERROR_DECODE
);
1366 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1369 case WAITING_FOR_INIT
:
1372 DVLOG(1) << "AppendData(): called in unexpected state " << state_
;
1376 // Check to see if data was appended at the pending seek point. This
1377 // indicates we have parsed enough data to complete the seek.
1378 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1379 !seek_cb_
.is_null()) {
1380 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1383 ranges
= GetBufferedRanges_Locked();
1386 for (size_t i
= 0; i
< ranges
.size(); ++i
)
1387 host_
->AddBufferedTimeRange(ranges
.start(i
), ranges
.end(i
));
1390 void ChunkDemuxer::Abort(const std::string
& id
,
1391 TimeDelta append_window_start
,
1392 TimeDelta append_window_end
,
1393 TimeDelta
* timestamp_offset
) {
1394 DVLOG(1) << "Abort(" << id
<< ")";
1395 base::AutoLock
auto_lock(lock_
);
1396 DCHECK(!id
.empty());
1397 CHECK(IsValidId(id
));
1398 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1399 source_state_map_
[id
]->Abort(append_window_start
,
1402 // Abort can possibly emit some buffers.
1403 // Need to check whether seeking can be completed.
1404 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1405 !seek_cb_
.is_null()) {
1406 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1410 void ChunkDemuxer::Remove(const std::string
& id
, TimeDelta start
,
1412 DVLOG(1) << "Remove(" << id
<< ", " << start
.InSecondsF()
1413 << ", " << end
.InSecondsF() << ")";
1414 base::AutoLock
auto_lock(lock_
);
1416 DCHECK(!id
.empty());
1417 CHECK(IsValidId(id
));
1418 DCHECK(start
>= base::TimeDelta()) << start
.InSecondsF();
1419 DCHECK(start
< end
) << "start " << start
.InSecondsF()
1420 << " end " << end
.InSecondsF();
1421 DCHECK(duration_
!= kNoTimestamp());
1422 DCHECK(start
<= duration_
) << "start " << start
.InSecondsF()
1423 << " duration " << duration_
.InSecondsF();
1425 if (start
== duration_
)
1428 source_state_map_
[id
]->Remove(start
, end
, duration_
);
1431 double ChunkDemuxer::GetDuration() {
1432 base::AutoLock
auto_lock(lock_
);
1433 return GetDuration_Locked();
1436 double ChunkDemuxer::GetDuration_Locked() {
1437 lock_
.AssertAcquired();
1438 if (duration_
== kNoTimestamp())
1439 return std::numeric_limits
<double>::quiet_NaN();
1441 // Return positive infinity if the resource is unbounded.
1442 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1443 if (duration_
== kInfiniteDuration())
1444 return std::numeric_limits
<double>::infinity();
1446 if (user_specified_duration_
>= 0)
1447 return user_specified_duration_
;
1449 return duration_
.InSecondsF();
1452 void ChunkDemuxer::SetDuration(double duration
) {
1453 base::AutoLock
auto_lock(lock_
);
1454 DVLOG(1) << "SetDuration(" << duration
<< ")";
1455 DCHECK_GE(duration
, 0);
1457 if (duration
== GetDuration_Locked())
1460 // Compute & bounds check the TimeDelta representation of duration.
1461 // This can be different if the value of |duration| doesn't fit the range or
1462 // precision of TimeDelta.
1463 TimeDelta min_duration
= TimeDelta::FromInternalValue(1);
1464 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1465 TimeDelta max_duration
= TimeDelta::FromInternalValue(kint64max
- 1);
1466 double min_duration_in_seconds
= min_duration
.InSecondsF();
1467 double max_duration_in_seconds
= max_duration
.InSecondsF();
1469 TimeDelta duration_td
;
1470 if (duration
== std::numeric_limits
<double>::infinity()) {
1471 duration_td
= media::kInfiniteDuration();
1472 } else if (duration
< min_duration_in_seconds
) {
1473 duration_td
= min_duration
;
1474 } else if (duration
> max_duration_in_seconds
) {
1475 duration_td
= max_duration
;
1477 duration_td
= TimeDelta::FromMicroseconds(
1478 duration
* base::Time::kMicrosecondsPerSecond
);
1481 DCHECK(duration_td
> TimeDelta());
1483 user_specified_duration_
= duration
;
1484 duration_
= duration_td
;
1485 host_
->SetDuration(duration_
);
1487 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1488 itr
!= source_state_map_
.end(); ++itr
) {
1489 itr
->second
->OnSetDuration(duration_
);
1493 bool ChunkDemuxer::IsParsingMediaSegment(const std::string
& id
) {
1494 base::AutoLock
auto_lock(lock_
);
1495 DVLOG(1) << "IsParsingMediaSegment(" << id
<< ")";
1496 CHECK(IsValidId(id
));
1498 return source_state_map_
[id
]->parsing_media_segment();
1501 void ChunkDemuxer::SetSequenceMode(const std::string
& id
,
1502 bool sequence_mode
) {
1503 base::AutoLock
auto_lock(lock_
);
1504 DVLOG(1) << "SetSequenceMode(" << id
<< ", " << sequence_mode
<< ")";
1505 CHECK(IsValidId(id
));
1506 DCHECK_NE(state_
, ENDED
);
1508 source_state_map_
[id
]->SetSequenceMode(sequence_mode
);
1511 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1512 const std::string
& id
,
1513 base::TimeDelta timestamp_offset
) {
1514 base::AutoLock
auto_lock(lock_
);
1515 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id
<< ", "
1516 << timestamp_offset
.InSecondsF() << ")";
1517 CHECK(IsValidId(id
));
1518 DCHECK_NE(state_
, ENDED
);
1520 source_state_map_
[id
]->SetGroupStartTimestampIfInSequenceMode(
1525 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status
) {
1526 DVLOG(1) << "MarkEndOfStream(" << status
<< ")";
1527 base::AutoLock
auto_lock(lock_
);
1528 DCHECK_NE(state_
, WAITING_FOR_INIT
);
1529 DCHECK_NE(state_
, ENDED
);
1531 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1534 if (state_
== INITIALIZING
) {
1535 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1539 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1540 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1541 itr
!= source_state_map_
.end(); ++itr
) {
1542 itr
->second
->MarkEndOfStream();
1545 CompletePendingReadsIfPossible();
1547 // Give a chance to resume the pending seek process.
1548 if (status
!= PIPELINE_OK
) {
1549 ReportError_Locked(status
);
1553 ChangeState_Locked(ENDED
);
1554 DecreaseDurationIfNecessary();
1556 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1557 !seek_cb_
.is_null()) {
1558 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1562 void ChunkDemuxer::UnmarkEndOfStream() {
1563 DVLOG(1) << "UnmarkEndOfStream()";
1564 base::AutoLock
auto_lock(lock_
);
1565 DCHECK_EQ(state_
, ENDED
);
1567 ChangeState_Locked(INITIALIZED
);
1569 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1570 itr
!= source_state_map_
.end(); ++itr
) {
1571 itr
->second
->UnmarkEndOfStream();
1575 void ChunkDemuxer::Shutdown() {
1576 DVLOG(1) << "Shutdown()";
1577 base::AutoLock
auto_lock(lock_
);
1579 if (state_
== SHUTDOWN
)
1582 ShutdownAllStreams();
1584 ChangeState_Locked(SHUTDOWN
);
1586 if(!seek_cb_
.is_null())
1587 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_ABORT
);
1590 void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
) {
1591 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1592 itr
!= source_state_map_
.end(); ++itr
) {
1593 itr
->second
->SetMemoryLimits(type
, memory_limit
);
1597 void ChunkDemuxer::ChangeState_Locked(State new_state
) {
1598 lock_
.AssertAcquired();
1599 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1600 << state_
<< " -> " << new_state
;
1604 ChunkDemuxer::~ChunkDemuxer() {
1605 DCHECK_NE(state_
, INITIALIZED
);
1607 STLDeleteValues(&source_state_map_
);
1610 void ChunkDemuxer::ReportError_Locked(PipelineStatus error
) {
1611 DVLOG(1) << "ReportError_Locked(" << error
<< ")";
1612 lock_
.AssertAcquired();
1613 DCHECK_NE(error
, PIPELINE_OK
);
1615 ChangeState_Locked(PARSE_ERROR
);
1617 PipelineStatusCB cb
;
1619 if (!init_cb_
.is_null()) {
1620 std::swap(cb
, init_cb_
);
1622 if (!seek_cb_
.is_null())
1623 std::swap(cb
, seek_cb_
);
1625 ShutdownAllStreams();
1628 if (!cb
.is_null()) {
1633 base::AutoUnlock
auto_unlock(lock_
);
1634 host_
->OnDemuxerError(error
);
1637 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1638 lock_
.AssertAcquired();
1639 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1640 itr
!= source_state_map_
.end(); ++itr
) {
1641 if (itr
->second
->IsSeekWaitingForData())
1648 void ChunkDemuxer::OnSourceInitDone(
1649 const StreamParser::InitParameters
& params
) {
1650 DVLOG(1) << "OnSourceInitDone(" << params
.duration
.InSecondsF() << ")";
1651 lock_
.AssertAcquired();
1652 DCHECK_EQ(state_
, INITIALIZING
);
1653 if (!audio_
&& !video_
) {
1654 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1658 if (params
.duration
!= TimeDelta() && duration_
== kNoTimestamp())
1659 UpdateDuration(params
.duration
);
1661 if (!params
.timeline_offset
.is_null()) {
1662 if (!timeline_offset_
.is_null() &&
1663 params
.timeline_offset
!= timeline_offset_
) {
1664 MEDIA_LOG(ERROR
, log_cb_
)
1665 << "Timeline offset is not the same across all SourceBuffers.";
1666 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1670 timeline_offset_
= params
.timeline_offset
;
1673 if (params
.liveness
!= DemuxerStream::LIVENESS_UNKNOWN
) {
1675 audio_
->SetLiveness(params
.liveness
);
1677 video_
->SetLiveness(params
.liveness
);
1680 // Wait until all streams have initialized.
1681 if ((!source_id_audio_
.empty() && !audio_
) ||
1682 (!source_id_video_
.empty() && !video_
)) {
1686 SeekAllSources(GetStartTime());
1687 StartReturningData();
1689 if (duration_
== kNoTimestamp())
1690 duration_
= kInfiniteDuration();
1692 // The demuxer is now initialized after the |start_timestamp_| was set.
1693 ChangeState_Locked(INITIALIZED
);
1694 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
1698 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type
) {
1700 case DemuxerStream::AUDIO
:
1704 new ChunkDemuxerStream(DemuxerStream::AUDIO
, splice_frames_enabled_
));
1705 return audio_
.get();
1707 case DemuxerStream::VIDEO
:
1711 new ChunkDemuxerStream(DemuxerStream::VIDEO
, splice_frames_enabled_
));
1712 return video_
.get();
1714 case DemuxerStream::TEXT
: {
1715 return new ChunkDemuxerStream(DemuxerStream::TEXT
,
1716 splice_frames_enabled_
);
1719 case DemuxerStream::UNKNOWN
:
1720 case DemuxerStream::NUM_TYPES
:
1728 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
1729 const TextTrackConfig
& config
) {
1730 lock_
.AssertAcquired();
1731 DCHECK_NE(state_
, SHUTDOWN
);
1732 host_
->AddTextStream(text_stream
, config
);
1735 bool ChunkDemuxer::IsValidId(const std::string
& source_id
) const {
1736 lock_
.AssertAcquired();
1737 return source_state_map_
.count(source_id
) > 0u;
1740 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration
) {
1741 DCHECK(duration_
!= new_duration
);
1742 user_specified_duration_
= -1;
1743 duration_
= new_duration
;
1744 host_
->SetDuration(new_duration
);
1747 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration
) {
1748 DCHECK(new_duration
!= kNoTimestamp());
1749 DCHECK(new_duration
!= kInfiniteDuration());
1751 // Per April 1, 2014 MSE spec editor's draft:
1752 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1753 // media-source.html#sourcebuffer-coded-frame-processing
1754 // 5. If the media segment contains data beyond the current duration, then run
1755 // the duration change algorithm with new duration set to the maximum of
1756 // the current duration and the group end timestamp.
1758 if (new_duration
<= duration_
)
1761 DVLOG(2) << __FUNCTION__
<< ": Increasing duration: "
1762 << duration_
.InSecondsF() << " -> " << new_duration
.InSecondsF();
1764 UpdateDuration(new_duration
);
1767 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1768 lock_
.AssertAcquired();
1770 TimeDelta max_duration
;
1772 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1773 itr
!= source_state_map_
.end(); ++itr
) {
1774 max_duration
= std::max(max_duration
,
1775 itr
->second
->GetMaxBufferedDuration());
1778 if (max_duration
== TimeDelta())
1781 if (max_duration
< duration_
)
1782 UpdateDuration(max_duration
);
1785 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges() const {
1786 base::AutoLock
auto_lock(lock_
);
1787 return GetBufferedRanges_Locked();
1790 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges_Locked() const {
1791 lock_
.AssertAcquired();
1793 bool ended
= state_
== ENDED
;
1794 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1795 // we'll need to update this loop to only add ranges from active sources.
1796 RangesList ranges_list
;
1797 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1798 itr
!= source_state_map_
.end(); ++itr
) {
1799 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration_
, ended
));
1802 return ComputeIntersection(ranges_list
, ended
);
1805 void ChunkDemuxer::StartReturningData() {
1806 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1807 itr
!= source_state_map_
.end(); ++itr
) {
1808 itr
->second
->StartReturningData();
1812 void ChunkDemuxer::AbortPendingReads() {
1813 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1814 itr
!= source_state_map_
.end(); ++itr
) {
1815 itr
->second
->AbortReads();
1819 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time
) {
1820 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1821 itr
!= source_state_map_
.end(); ++itr
) {
1822 itr
->second
->Seek(seek_time
);
1826 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1827 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1828 itr
!= source_state_map_
.end(); ++itr
) {
1829 itr
->second
->CompletePendingReadIfPossible();
1833 void ChunkDemuxer::ShutdownAllStreams() {
1834 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1835 itr
!= source_state_map_
.end(); ++itr
) {
1836 itr
->second
->Shutdown();
1840 } // namespace media