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
;
97 SourceState(scoped_ptr
<StreamParser
> stream_parser
,
98 scoped_ptr
<FrameProcessor
> frame_processor
,
99 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
100 const scoped_refptr
<MediaLog
>& media_log
);
104 void Init(const StreamParser::InitCB
& init_cb
,
107 const StreamParser::EncryptedMediaInitDataCB
&
108 encrypted_media_init_data_cb
,
109 const NewTextTrackCB
& new_text_track_cb
);
111 // Appends new data to the StreamParser.
112 // Returns true if the data was successfully appended. Returns false if an
113 // error occurred. |*timestamp_offset| is used and possibly updated by the
114 // append. |append_window_start| and |append_window_end| correspond to the MSE
115 // spec's similarly named source buffer attributes that are used in coded
116 // frame processing. |init_segment_received_cb| is run for each new fully
117 // parsed initialization segment.
118 bool Append(const uint8
* data
,
120 TimeDelta append_window_start
,
121 TimeDelta append_window_end
,
122 TimeDelta
* timestamp_offset
,
123 const InitSegmentReceivedCB
& init_segment_received_cb
);
125 // Aborts the current append sequence and resets the parser.
126 void Abort(TimeDelta append_window_start
,
127 TimeDelta append_window_end
,
128 TimeDelta
* timestamp_offset
);
130 // Calls Remove(|start|, |end|, |duration|) on all
131 // ChunkDemuxerStreams managed by this object.
132 void Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
);
134 // Returns true if currently parsing a media segment, or false otherwise.
135 bool parsing_media_segment() const { return parsing_media_segment_
; }
137 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
138 void SetSequenceMode(bool sequence_mode
);
140 // Signals the coded frame processor to update its group start timestamp to be
141 // |timestamp_offset| if it is in sequence append mode.
142 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset
);
144 // Returns the range of buffered data in this source, capped at |duration|.
145 // |ended| - Set to true if end of stream has been signaled and the special
146 // end of stream range logic needs to be executed.
147 Ranges
<TimeDelta
> GetBufferedRanges(TimeDelta duration
, bool ended
) const;
149 // Returns the highest buffered duration across all streams managed
151 // Returns TimeDelta() if none of the streams contain buffered data.
152 TimeDelta
GetMaxBufferedDuration() const;
154 // Helper methods that call methods with similar names on all the
155 // ChunkDemuxerStreams managed by this object.
156 void StartReturningData();
158 void Seek(TimeDelta seek_time
);
159 void CompletePendingReadIfPossible();
160 void OnSetDuration(TimeDelta duration
);
161 void MarkEndOfStream();
162 void UnmarkEndOfStream();
164 // Sets the memory limit on each stream of a specific type.
165 // |memory_limit| is the maximum number of bytes each stream of type |type|
166 // is allowed to hold in its buffer.
167 void SetMemoryLimits(DemuxerStream::Type type
, size_t memory_limit
);
168 bool IsSeekWaitingForData() const;
171 // Called by the |stream_parser_| when a new initialization segment is
173 // Returns true on a successful call. Returns false if an error occurred while
174 // processing decoder configurations.
175 bool OnNewConfigs(bool allow_audio
, bool allow_video
,
176 const AudioDecoderConfig
& audio_config
,
177 const VideoDecoderConfig
& video_config
,
178 const StreamParser::TextTrackConfigMap
& text_configs
);
180 // Called by the |stream_parser_| at the beginning of a new media segment.
181 void OnNewMediaSegment();
183 // Called by the |stream_parser_| at the end of a media segment.
184 void OnEndOfMediaSegment();
186 // Called by the |stream_parser_| when new buffers have been parsed.
187 // It processes the new buffers using |frame_processor_|, which includes
188 // appending the processed frames to associated demuxer streams for each
190 // Returns true on a successful call. Returns false if an error occurred while
191 // processing the buffers.
192 bool OnNewBuffers(const StreamParser::BufferQueue
& audio_buffers
,
193 const StreamParser::BufferQueue
& video_buffers
,
194 const StreamParser::TextBufferQueueMap
& text_map
);
196 void OnSourceInitDone(const StreamParser::InitParameters
& params
);
198 CreateDemuxerStreamCB create_demuxer_stream_cb_
;
199 NewTextTrackCB new_text_track_cb_
;
201 // During Append(), if OnNewBuffers() coded frame processing updates the
202 // timestamp offset then |*timestamp_offset_during_append_| is also updated
203 // so Append()'s caller can know the new offset. This pointer is only non-NULL
204 // during the lifetime of an Append() call.
205 TimeDelta
* timestamp_offset_during_append_
;
207 // During Append(), coded frame processing triggered by OnNewBuffers()
208 // requires these two attributes. These are only valid during the lifetime of
210 TimeDelta append_window_start_during_append_
;
211 TimeDelta append_window_end_during_append_
;
213 // Set to true if the next buffers appended within the append window
214 // represent the start of a new media segment. This flag being set
215 // triggers a call to |new_segment_cb_| when the new buffers are
216 // appended. The flag is set on actual media segment boundaries and
217 // when the "append window" filtering causes discontinuities in the
219 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
220 // processing's discontinuity logic is enough. See http://crbug.com/351489.
221 bool new_media_segment_
;
223 // Keeps track of whether a media segment is being parsed.
224 bool parsing_media_segment_
;
226 // The object used to parse appended data.
227 scoped_ptr
<StreamParser
> stream_parser_
;
229 ChunkDemuxerStream
* audio_
; // Not owned by |this|.
230 ChunkDemuxerStream
* video_
; // Not owned by |this|.
232 typedef std::map
<StreamParser::TrackId
, ChunkDemuxerStream
*> TextStreamMap
;
233 TextStreamMap text_stream_map_
; // |this| owns the map's stream pointers.
235 scoped_ptr
<FrameProcessor
> frame_processor_
;
236 scoped_refptr
<MediaLog
> media_log_
;
237 StreamParser::InitCB init_cb_
;
239 // During Append(), OnNewConfigs() will trigger the initialization segment
240 // received algorithm. This callback is only non-NULL during the lifetime of
241 // an Append() call. Note, the MSE spec explicitly disallows this algorithm
242 // during an Abort(), since Abort() is allowed only to emit coded frames, and
243 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
244 InitSegmentReceivedCB init_segment_received_cb_
;
246 // Indicates that timestampOffset should be updated automatically during
247 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
248 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
249 // changes to MSE spec. See http://crbug.com/371499.
250 bool auto_update_timestamp_offset_
;
252 DISALLOW_COPY_AND_ASSIGN(SourceState
);
255 SourceState::SourceState(scoped_ptr
<StreamParser
> stream_parser
,
256 scoped_ptr
<FrameProcessor
> frame_processor
,
257 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
258 const scoped_refptr
<MediaLog
>& media_log
)
259 : create_demuxer_stream_cb_(create_demuxer_stream_cb
),
260 timestamp_offset_during_append_(NULL
),
261 new_media_segment_(false),
262 parsing_media_segment_(false),
263 stream_parser_(stream_parser
.release()),
266 frame_processor_(frame_processor
.release()),
267 media_log_(media_log
),
268 auto_update_timestamp_offset_(false) {
269 DCHECK(!create_demuxer_stream_cb_
.is_null());
270 DCHECK(frame_processor_
);
273 SourceState::~SourceState() {
276 STLDeleteValues(&text_stream_map_
);
279 void SourceState::Init(
280 const StreamParser::InitCB
& init_cb
,
283 const StreamParser::EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
284 const NewTextTrackCB
& new_text_track_cb
) {
285 new_text_track_cb_
= new_text_track_cb
;
288 stream_parser_
->Init(
289 base::Bind(&SourceState::OnSourceInitDone
, base::Unretained(this)),
290 base::Bind(&SourceState::OnNewConfigs
, base::Unretained(this),
291 allow_audio
, allow_video
),
292 base::Bind(&SourceState::OnNewBuffers
, base::Unretained(this)),
293 new_text_track_cb_
.is_null(), encrypted_media_init_data_cb
,
294 base::Bind(&SourceState::OnNewMediaSegment
, base::Unretained(this)),
295 base::Bind(&SourceState::OnEndOfMediaSegment
, base::Unretained(this)),
299 void SourceState::SetSequenceMode(bool sequence_mode
) {
300 DCHECK(!parsing_media_segment_
);
302 frame_processor_
->SetSequenceMode(sequence_mode
);
305 void SourceState::SetGroupStartTimestampIfInSequenceMode(
306 base::TimeDelta timestamp_offset
) {
307 DCHECK(!parsing_media_segment_
);
309 frame_processor_
->SetGroupStartTimestampIfInSequenceMode(timestamp_offset
);
312 bool SourceState::Append(
315 TimeDelta append_window_start
,
316 TimeDelta append_window_end
,
317 TimeDelta
* timestamp_offset
,
318 const InitSegmentReceivedCB
& init_segment_received_cb
) {
319 DCHECK(timestamp_offset
);
320 DCHECK(!timestamp_offset_during_append_
);
321 DCHECK(!init_segment_received_cb
.is_null());
322 DCHECK(init_segment_received_cb_
.is_null());
323 append_window_start_during_append_
= append_window_start
;
324 append_window_end_during_append_
= append_window_end
;
325 timestamp_offset_during_append_
= timestamp_offset
;
326 init_segment_received_cb_
= init_segment_received_cb
;
328 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
329 // append window and timestamp offset pointer. See http://crbug.com/351454.
330 bool result
= stream_parser_
->Parse(data
, length
);
332 MEDIA_LOG(ERROR
, media_log_
)
333 << __FUNCTION__
<< ": stream parsing failed."
334 << " Data size=" << length
335 << " append_window_start=" << append_window_start
.InSecondsF()
336 << " append_window_end=" << append_window_end
.InSecondsF();
338 timestamp_offset_during_append_
= NULL
;
339 init_segment_received_cb_
.Reset();
343 void SourceState::Abort(TimeDelta append_window_start
,
344 TimeDelta append_window_end
,
345 base::TimeDelta
* timestamp_offset
) {
346 DCHECK(timestamp_offset
);
347 DCHECK(!timestamp_offset_during_append_
);
348 timestamp_offset_during_append_
= timestamp_offset
;
349 append_window_start_during_append_
= append_window_start
;
350 append_window_end_during_append_
= append_window_end
;
352 stream_parser_
->Flush();
353 timestamp_offset_during_append_
= NULL
;
355 frame_processor_
->Reset();
356 parsing_media_segment_
= false;
359 void SourceState::Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
) {
361 audio_
->Remove(start
, end
, duration
);
364 video_
->Remove(start
, end
, duration
);
366 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
367 itr
!= text_stream_map_
.end(); ++itr
) {
368 itr
->second
->Remove(start
, end
, duration
);
372 Ranges
<TimeDelta
> SourceState::GetBufferedRanges(TimeDelta duration
,
374 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
375 // this code to only add ranges from active tracks.
376 RangesList ranges_list
;
378 ranges_list
.push_back(audio_
->GetBufferedRanges(duration
));
381 ranges_list
.push_back(video_
->GetBufferedRanges(duration
));
383 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
384 itr
!= text_stream_map_
.end(); ++itr
) {
385 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration
));
388 return ComputeIntersection(ranges_list
, ended
);
391 TimeDelta
SourceState::GetMaxBufferedDuration() const {
392 TimeDelta max_duration
;
395 max_duration
= std::max(max_duration
, audio_
->GetBufferedDuration());
398 max_duration
= std::max(max_duration
, video_
->GetBufferedDuration());
400 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
401 itr
!= text_stream_map_
.end(); ++itr
) {
402 max_duration
= std::max(max_duration
, itr
->second
->GetBufferedDuration());
408 void SourceState::StartReturningData() {
410 audio_
->StartReturningData();
413 video_
->StartReturningData();
415 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
416 itr
!= text_stream_map_
.end(); ++itr
) {
417 itr
->second
->StartReturningData();
421 void SourceState::AbortReads() {
423 audio_
->AbortReads();
426 video_
->AbortReads();
428 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
429 itr
!= text_stream_map_
.end(); ++itr
) {
430 itr
->second
->AbortReads();
434 void SourceState::Seek(TimeDelta seek_time
) {
436 audio_
->Seek(seek_time
);
439 video_
->Seek(seek_time
);
441 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
442 itr
!= text_stream_map_
.end(); ++itr
) {
443 itr
->second
->Seek(seek_time
);
447 void SourceState::CompletePendingReadIfPossible() {
449 audio_
->CompletePendingReadIfPossible();
452 video_
->CompletePendingReadIfPossible();
454 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
455 itr
!= text_stream_map_
.end(); ++itr
) {
456 itr
->second
->CompletePendingReadIfPossible();
460 void SourceState::OnSetDuration(TimeDelta duration
) {
462 audio_
->OnSetDuration(duration
);
465 video_
->OnSetDuration(duration
);
467 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
468 itr
!= text_stream_map_
.end(); ++itr
) {
469 itr
->second
->OnSetDuration(duration
);
473 void SourceState::MarkEndOfStream() {
475 audio_
->MarkEndOfStream();
478 video_
->MarkEndOfStream();
480 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
481 itr
!= text_stream_map_
.end(); ++itr
) {
482 itr
->second
->MarkEndOfStream();
486 void SourceState::UnmarkEndOfStream() {
488 audio_
->UnmarkEndOfStream();
491 video_
->UnmarkEndOfStream();
493 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
494 itr
!= text_stream_map_
.end(); ++itr
) {
495 itr
->second
->UnmarkEndOfStream();
499 void SourceState::Shutdown() {
506 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
507 itr
!= text_stream_map_
.end(); ++itr
) {
508 itr
->second
->Shutdown();
512 void SourceState::SetMemoryLimits(DemuxerStream::Type type
,
513 size_t memory_limit
) {
515 case DemuxerStream::AUDIO
:
517 audio_
->SetStreamMemoryLimit(memory_limit
);
519 case DemuxerStream::VIDEO
:
521 video_
->SetStreamMemoryLimit(memory_limit
);
523 case DemuxerStream::TEXT
:
524 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
525 itr
!= text_stream_map_
.end(); ++itr
) {
526 itr
->second
->SetStreamMemoryLimit(memory_limit
);
529 case DemuxerStream::UNKNOWN
:
530 case DemuxerStream::NUM_TYPES
:
536 bool SourceState::IsSeekWaitingForData() const {
537 if (audio_
&& audio_
->IsSeekWaitingForData())
540 if (video_
&& video_
->IsSeekWaitingForData())
543 // NOTE: We are intentionally not checking the text tracks
544 // because text tracks are discontinuous and may not have data
545 // for the seek position. This is ok and playback should not be
546 // stalled because we don't have cues. If cues, with timestamps after
547 // the seek time, eventually arrive they will be delivered properly
548 // in response to ChunkDemuxerStream::Read() calls.
553 bool SourceState::OnNewConfigs(
554 bool allow_audio
, bool allow_video
,
555 const AudioDecoderConfig
& audio_config
,
556 const VideoDecoderConfig
& video_config
,
557 const StreamParser::TextTrackConfigMap
& text_configs
) {
558 DVLOG(1) << "OnNewConfigs(" << allow_audio
<< ", " << allow_video
559 << ", " << audio_config
.IsValidConfig()
560 << ", " << video_config
.IsValidConfig() << ")";
561 DCHECK(!init_segment_received_cb_
.is_null());
563 if (!audio_config
.IsValidConfig() && !video_config
.IsValidConfig()) {
564 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
568 // Signal an error if we get configuration info for stream types that weren't
569 // specified in AddId() or more configs after a stream is initialized.
570 if (allow_audio
!= audio_config
.IsValidConfig()) {
571 MEDIA_LOG(ERROR
, media_log_
)
572 << "Initialization segment"
573 << (audio_config
.IsValidConfig() ? " has" : " does not have")
574 << " an audio track, but the mimetype"
575 << (allow_audio
? " specifies" : " does not specify")
576 << " an audio codec.";
580 if (allow_video
!= video_config
.IsValidConfig()) {
581 MEDIA_LOG(ERROR
, media_log_
)
582 << "Initialization segment"
583 << (video_config
.IsValidConfig() ? " has" : " does not have")
584 << " a video track, but the mimetype"
585 << (allow_video
? " specifies" : " does not specify")
586 << " a video codec.";
591 if (audio_config
.IsValidConfig()) {
593 media_log_
->SetBooleanProperty("found_audio_stream", true);
596 audio_
->audio_decoder_config().codec() != audio_config
.codec()) {
597 media_log_
->SetStringProperty("audio_codec_name",
598 audio_config
.GetHumanReadableCodecName());
602 audio_
= create_demuxer_stream_cb_
.Run(DemuxerStream::AUDIO
);
605 DVLOG(1) << "Failed to create an audio stream.";
609 if (!frame_processor_
->AddTrack(FrameProcessor::kAudioTrackId
, audio_
)) {
610 DVLOG(1) << "Failed to add audio track to frame processor.";
615 frame_processor_
->OnPossibleAudioConfigUpdate(audio_config
);
616 success
&= audio_
->UpdateAudioConfig(audio_config
, media_log_
);
619 if (video_config
.IsValidConfig()) {
621 media_log_
->SetBooleanProperty("found_video_stream", true);
624 video_
->video_decoder_config().codec() != video_config
.codec()) {
625 media_log_
->SetStringProperty("video_codec_name",
626 video_config
.GetHumanReadableCodecName());
630 video_
= create_demuxer_stream_cb_
.Run(DemuxerStream::VIDEO
);
633 DVLOG(1) << "Failed to create a video stream.";
637 if (!frame_processor_
->AddTrack(FrameProcessor::kVideoTrackId
, video_
)) {
638 DVLOG(1) << "Failed to add video track to frame processor.";
643 success
&= video_
->UpdateVideoConfig(video_config
, media_log_
);
646 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr
;
647 if (text_stream_map_
.empty()) {
648 for (TextConfigItr itr
= text_configs
.begin();
649 itr
!= text_configs
.end(); ++itr
) {
650 ChunkDemuxerStream
* const text_stream
=
651 create_demuxer_stream_cb_
.Run(DemuxerStream::TEXT
);
652 if (!frame_processor_
->AddTrack(itr
->first
, text_stream
)) {
654 MEDIA_LOG(ERROR
, media_log_
) << "Failed to add text track ID "
655 << itr
->first
<< " to frame processor.";
658 text_stream
->UpdateTextConfig(itr
->second
, media_log_
);
659 text_stream_map_
[itr
->first
] = text_stream
;
660 new_text_track_cb_
.Run(text_stream
, itr
->second
);
663 const size_t text_count
= text_stream_map_
.size();
664 if (text_configs
.size() != text_count
) {
666 MEDIA_LOG(ERROR
, media_log_
)
667 << "The number of text track configs changed.";
668 } else if (text_count
== 1) {
669 TextConfigItr config_itr
= text_configs
.begin();
670 TextStreamMap::iterator stream_itr
= text_stream_map_
.begin();
671 ChunkDemuxerStream
* text_stream
= stream_itr
->second
;
672 TextTrackConfig old_config
= text_stream
->text_track_config();
673 TextTrackConfig
new_config(config_itr
->second
.kind(),
674 config_itr
->second
.label(),
675 config_itr
->second
.language(),
677 if (!new_config
.Matches(old_config
)) {
679 MEDIA_LOG(ERROR
, media_log_
)
680 << "New text track config does not match old one.";
682 StreamParser::TrackId old_id
= stream_itr
->first
;
683 StreamParser::TrackId new_id
= config_itr
->first
;
684 if (new_id
!= old_id
) {
685 if (frame_processor_
->UpdateTrack(old_id
, new_id
)) {
686 text_stream_map_
.clear();
687 text_stream_map_
[config_itr
->first
] = text_stream
;
690 MEDIA_LOG(ERROR
, media_log_
)
691 << "Error remapping single text track number";
696 for (TextConfigItr config_itr
= text_configs
.begin();
697 config_itr
!= text_configs
.end(); ++config_itr
) {
698 TextStreamMap::iterator stream_itr
=
699 text_stream_map_
.find(config_itr
->first
);
700 if (stream_itr
== text_stream_map_
.end()) {
702 MEDIA_LOG(ERROR
, media_log_
)
703 << "Unexpected text track configuration for track ID "
704 << config_itr
->first
;
708 const TextTrackConfig
& new_config
= config_itr
->second
;
709 ChunkDemuxerStream
* stream
= stream_itr
->second
;
710 TextTrackConfig old_config
= stream
->text_track_config();
711 if (!new_config
.Matches(old_config
)) {
713 MEDIA_LOG(ERROR
, media_log_
) << "New text track config for track ID "
715 << " does not match old one.";
722 frame_processor_
->SetAllTrackBuffersNeedRandomAccessPoint();
724 DVLOG(1) << "OnNewConfigs() : " << (success
? "success" : "failed");
726 init_segment_received_cb_
.Run();
731 void SourceState::OnNewMediaSegment() {
732 DVLOG(2) << "OnNewMediaSegment()";
733 parsing_media_segment_
= true;
734 new_media_segment_
= true;
737 void SourceState::OnEndOfMediaSegment() {
738 DVLOG(2) << "OnEndOfMediaSegment()";
739 parsing_media_segment_
= false;
740 new_media_segment_
= false;
743 bool SourceState::OnNewBuffers(
744 const StreamParser::BufferQueue
& audio_buffers
,
745 const StreamParser::BufferQueue
& video_buffers
,
746 const StreamParser::TextBufferQueueMap
& text_map
) {
747 DVLOG(2) << "OnNewBuffers()";
748 DCHECK(timestamp_offset_during_append_
);
749 DCHECK(parsing_media_segment_
);
751 const TimeDelta timestamp_offset_before_processing
=
752 *timestamp_offset_during_append_
;
754 // Calculate the new timestamp offset for audio/video tracks if the stream
755 // parser has requested automatic updates.
756 TimeDelta new_timestamp_offset
= timestamp_offset_before_processing
;
757 if (auto_update_timestamp_offset_
) {
758 const bool have_audio_buffers
= !audio_buffers
.empty();
759 const bool have_video_buffers
= !video_buffers
.empty();
760 if (have_audio_buffers
&& have_video_buffers
) {
761 new_timestamp_offset
+=
762 std::min(EndTimestamp(audio_buffers
), EndTimestamp(video_buffers
));
763 } else if (have_audio_buffers
) {
764 new_timestamp_offset
+= EndTimestamp(audio_buffers
);
765 } else if (have_video_buffers
) {
766 new_timestamp_offset
+= EndTimestamp(video_buffers
);
770 if (!frame_processor_
->ProcessFrames(audio_buffers
,
773 append_window_start_during_append_
,
774 append_window_end_during_append_
,
776 timestamp_offset_during_append_
)) {
780 // Only update the timestamp offset if the frame processor hasn't already.
781 if (auto_update_timestamp_offset_
&&
782 timestamp_offset_before_processing
== *timestamp_offset_during_append_
) {
783 *timestamp_offset_during_append_
= new_timestamp_offset
;
789 void SourceState::OnSourceInitDone(const StreamParser::InitParameters
& params
) {
790 auto_update_timestamp_offset_
= params
.auto_update_timestamp_offset
;
791 base::ResetAndReturn(&init_cb_
).Run(params
);
794 ChunkDemuxerStream::ChunkDemuxerStream(Type type
,
795 bool splice_frames_enabled
)
797 liveness_(DemuxerStream::LIVENESS_UNKNOWN
),
798 state_(UNINITIALIZED
),
799 splice_frames_enabled_(splice_frames_enabled
),
800 partial_append_window_trimming_enabled_(false) {
803 void ChunkDemuxerStream::StartReturningData() {
804 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
805 base::AutoLock
auto_lock(lock_
);
806 DCHECK(read_cb_
.is_null());
807 ChangeState_Locked(RETURNING_DATA_FOR_READS
);
810 void ChunkDemuxerStream::AbortReads() {
811 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
812 base::AutoLock
auto_lock(lock_
);
813 ChangeState_Locked(RETURNING_ABORT_FOR_READS
);
814 if (!read_cb_
.is_null())
815 base::ResetAndReturn(&read_cb_
).Run(kAborted
, NULL
);
818 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
819 base::AutoLock
auto_lock(lock_
);
820 if (read_cb_
.is_null())
823 CompletePendingReadIfPossible_Locked();
826 void ChunkDemuxerStream::Shutdown() {
827 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
828 base::AutoLock
auto_lock(lock_
);
829 ChangeState_Locked(SHUTDOWN
);
831 // Pass an end of stream buffer to the pending callback to signal that no more
832 // data will be sent.
833 if (!read_cb_
.is_null()) {
834 base::ResetAndReturn(&read_cb_
).Run(DemuxerStream::kOk
,
835 StreamParserBuffer::CreateEOSBuffer());
839 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
840 base::AutoLock
auto_lock(lock_
);
842 // This method should not be called for text tracks. See the note in
843 // SourceState::IsSeekWaitingForData().
844 DCHECK_NE(type_
, DemuxerStream::TEXT
);
846 return stream_
->IsSeekPending();
849 void ChunkDemuxerStream::Seek(TimeDelta time
) {
850 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time
.InSecondsF() << ")";
851 base::AutoLock
auto_lock(lock_
);
852 DCHECK(read_cb_
.is_null());
853 DCHECK(state_
== UNINITIALIZED
|| state_
== RETURNING_ABORT_FOR_READS
)
859 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue
& buffers
) {
863 base::AutoLock
auto_lock(lock_
);
864 DCHECK_NE(state_
, SHUTDOWN
);
865 if (!stream_
->Append(buffers
)) {
866 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
870 if (!read_cb_
.is_null())
871 CompletePendingReadIfPossible_Locked();
876 void ChunkDemuxerStream::Remove(TimeDelta start
, TimeDelta end
,
877 TimeDelta duration
) {
878 base::AutoLock
auto_lock(lock_
);
879 stream_
->Remove(start
, end
, duration
);
882 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration
) {
883 base::AutoLock
auto_lock(lock_
);
884 stream_
->OnSetDuration(duration
);
887 Ranges
<TimeDelta
> ChunkDemuxerStream::GetBufferedRanges(
888 TimeDelta duration
) const {
889 base::AutoLock
auto_lock(lock_
);
892 // Since text tracks are discontinuous and the lack of cues should not block
893 // playback, report the buffered range for text tracks as [0, |duration|) so
894 // that intesections with audio & video tracks are computed correctly when
895 // no cues are present.
896 Ranges
<TimeDelta
> text_range
;
897 text_range
.Add(TimeDelta(), duration
);
901 Ranges
<TimeDelta
> range
= stream_
->GetBufferedTime();
903 if (range
.size() == 0u)
906 // Clamp the end of the stream's buffered ranges to fit within the duration.
907 // This can be done by intersecting the stream's range with the valid time
909 Ranges
<TimeDelta
> valid_time_range
;
910 valid_time_range
.Add(range
.start(0), duration
);
911 return range
.IntersectionWith(valid_time_range
);
914 TimeDelta
ChunkDemuxerStream::GetBufferedDuration() const {
915 return stream_
->GetBufferedDuration();
918 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp
) {
919 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
920 << start_timestamp
.InSecondsF() << ")";
921 base::AutoLock
auto_lock(lock_
);
922 stream_
->OnNewMediaSegment(start_timestamp
);
925 bool ChunkDemuxerStream::UpdateAudioConfig(
926 const AudioDecoderConfig
& config
,
927 const scoped_refptr
<MediaLog
>& media_log
) {
928 DCHECK(config
.IsValidConfig());
929 DCHECK_EQ(type_
, AUDIO
);
930 base::AutoLock
auto_lock(lock_
);
932 DCHECK_EQ(state_
, UNINITIALIZED
);
934 // On platforms which support splice frames, enable splice frames and
935 // partial append window support for most codecs (notably: not opus).
936 const bool codec_supported
= config
.codec() == kCodecMP3
||
937 config
.codec() == kCodecAAC
||
938 config
.codec() == kCodecVorbis
;
939 splice_frames_enabled_
= splice_frames_enabled_
&& codec_supported
;
940 partial_append_window_trimming_enabled_
=
941 splice_frames_enabled_
&& codec_supported
;
944 new SourceBufferStream(config
, media_log
, splice_frames_enabled_
));
948 return stream_
->UpdateAudioConfig(config
);
951 bool ChunkDemuxerStream::UpdateVideoConfig(
952 const VideoDecoderConfig
& config
,
953 const scoped_refptr
<MediaLog
>& media_log
) {
954 DCHECK(config
.IsValidConfig());
955 DCHECK_EQ(type_
, VIDEO
);
956 base::AutoLock
auto_lock(lock_
);
959 DCHECK_EQ(state_
, UNINITIALIZED
);
961 new SourceBufferStream(config
, media_log
, splice_frames_enabled_
));
965 return stream_
->UpdateVideoConfig(config
);
968 void ChunkDemuxerStream::UpdateTextConfig(
969 const TextTrackConfig
& config
,
970 const scoped_refptr
<MediaLog
>& media_log
) {
971 DCHECK_EQ(type_
, TEXT
);
972 base::AutoLock
auto_lock(lock_
);
974 DCHECK_EQ(state_
, UNINITIALIZED
);
976 new SourceBufferStream(config
, media_log
, splice_frames_enabled_
));
979 void ChunkDemuxerStream::MarkEndOfStream() {
980 base::AutoLock
auto_lock(lock_
);
981 stream_
->MarkEndOfStream();
984 void ChunkDemuxerStream::UnmarkEndOfStream() {
985 base::AutoLock
auto_lock(lock_
);
986 stream_
->UnmarkEndOfStream();
989 // DemuxerStream methods.
990 void ChunkDemuxerStream::Read(const ReadCB
& read_cb
) {
991 base::AutoLock
auto_lock(lock_
);
992 DCHECK_NE(state_
, UNINITIALIZED
);
993 DCHECK(read_cb_
.is_null());
995 read_cb_
= BindToCurrentLoop(read_cb
);
996 CompletePendingReadIfPossible_Locked();
999 DemuxerStream::Type
ChunkDemuxerStream::type() const { return type_
; }
1001 DemuxerStream::Liveness
ChunkDemuxerStream::liveness() const {
1002 base::AutoLock
auto_lock(lock_
);
1006 AudioDecoderConfig
ChunkDemuxerStream::audio_decoder_config() {
1007 CHECK_EQ(type_
, AUDIO
);
1008 base::AutoLock
auto_lock(lock_
);
1009 return stream_
->GetCurrentAudioDecoderConfig();
1012 VideoDecoderConfig
ChunkDemuxerStream::video_decoder_config() {
1013 CHECK_EQ(type_
, VIDEO
);
1014 base::AutoLock
auto_lock(lock_
);
1015 return stream_
->GetCurrentVideoDecoderConfig();
1018 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
1020 VideoRotation
ChunkDemuxerStream::video_rotation() {
1021 return VIDEO_ROTATION_0
;
1024 TextTrackConfig
ChunkDemuxerStream::text_track_config() {
1025 CHECK_EQ(type_
, TEXT
);
1026 base::AutoLock
auto_lock(lock_
);
1027 return stream_
->GetCurrentTextTrackConfig();
1030 void ChunkDemuxerStream::SetStreamMemoryLimit(size_t memory_limit
) {
1031 stream_
->set_memory_limit(memory_limit
);
1034 void ChunkDemuxerStream::SetLiveness(Liveness liveness
) {
1035 base::AutoLock
auto_lock(lock_
);
1036 liveness_
= liveness
;
1039 void ChunkDemuxerStream::ChangeState_Locked(State state
) {
1040 lock_
.AssertAcquired();
1041 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1043 << " - " << state_
<< " -> " << state
;
1047 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1049 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1050 lock_
.AssertAcquired();
1051 DCHECK(!read_cb_
.is_null());
1053 DemuxerStream::Status status
;
1054 scoped_refptr
<StreamParserBuffer
> buffer
;
1060 case RETURNING_DATA_FOR_READS
:
1061 switch (stream_
->GetNextBuffer(&buffer
)) {
1062 case SourceBufferStream::kSuccess
:
1063 status
= DemuxerStream::kOk
;
1064 DVLOG(2) << __FUNCTION__
<< ": returning kOk, type " << type_
1065 << ", dts " << buffer
->GetDecodeTimestamp().InSecondsF()
1066 << ", pts " << buffer
->timestamp().InSecondsF()
1067 << ", dur " << buffer
->duration().InSecondsF()
1068 << ", key " << buffer
->is_key_frame();
1070 case SourceBufferStream::kNeedBuffer
:
1071 // Return early without calling |read_cb_| since we don't have
1072 // any data to return yet.
1073 DVLOG(2) << __FUNCTION__
<< ": returning kNeedBuffer, type "
1076 case SourceBufferStream::kEndOfStream
:
1077 status
= DemuxerStream::kOk
;
1078 buffer
= StreamParserBuffer::CreateEOSBuffer();
1079 DVLOG(2) << __FUNCTION__
<< ": returning kOk with EOS buffer, type "
1082 case SourceBufferStream::kConfigChange
:
1083 status
= kConfigChanged
;
1085 DVLOG(2) << __FUNCTION__
<< ": returning kConfigChange, type "
1090 case RETURNING_ABORT_FOR_READS
:
1091 // Null buffers should be returned in this state since we are waiting
1092 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1093 // because they are associated with the seek.
1094 status
= DemuxerStream::kAborted
;
1096 DVLOG(2) << __FUNCTION__
<< ": returning kAborted, type " << type_
;
1099 status
= DemuxerStream::kOk
;
1100 buffer
= StreamParserBuffer::CreateEOSBuffer();
1101 DVLOG(2) << __FUNCTION__
<< ": returning kOk with EOS buffer, type "
1106 base::ResetAndReturn(&read_cb_
).Run(status
, buffer
);
1109 ChunkDemuxer::ChunkDemuxer(
1110 const base::Closure
& open_cb
,
1111 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
1112 const scoped_refptr
<MediaLog
>& media_log
,
1113 bool splice_frames_enabled
)
1114 : state_(WAITING_FOR_INIT
),
1115 cancel_next_seek_(false),
1118 encrypted_media_init_data_cb_(encrypted_media_init_data_cb
),
1119 enable_text_(false),
1120 media_log_(media_log
),
1121 duration_(kNoTimestamp()),
1122 user_specified_duration_(-1),
1123 liveness_(DemuxerStream::LIVENESS_UNKNOWN
),
1124 splice_frames_enabled_(splice_frames_enabled
) {
1125 DCHECK(!open_cb_
.is_null());
1126 DCHECK(!encrypted_media_init_data_cb_
.is_null());
1129 std::string
ChunkDemuxer::GetDisplayName() const {
1130 return "ChunkDemuxer";
1133 void ChunkDemuxer::Initialize(
1135 const PipelineStatusCB
& cb
,
1136 bool enable_text_tracks
) {
1137 DVLOG(1) << "Init()";
1139 base::AutoLock
auto_lock(lock_
);
1141 // The |init_cb_| must only be run after this method returns, so always post.
1142 init_cb_
= BindToCurrentLoop(cb
);
1143 if (state_
== SHUTDOWN
) {
1144 base::ResetAndReturn(&init_cb_
).Run(DEMUXER_ERROR_COULD_NOT_OPEN
);
1147 DCHECK_EQ(state_
, WAITING_FOR_INIT
);
1149 enable_text_
= enable_text_tracks
;
1151 ChangeState_Locked(INITIALIZING
);
1153 base::ResetAndReturn(&open_cb_
).Run();
1156 void ChunkDemuxer::Stop() {
1157 DVLOG(1) << "Stop()";
1161 void ChunkDemuxer::Seek(TimeDelta time
, const PipelineStatusCB
& cb
) {
1162 DVLOG(1) << "Seek(" << time
.InSecondsF() << ")";
1163 DCHECK(time
>= TimeDelta());
1165 base::AutoLock
auto_lock(lock_
);
1166 DCHECK(seek_cb_
.is_null());
1168 seek_cb_
= BindToCurrentLoop(cb
);
1169 if (state_
!= INITIALIZED
&& state_
!= ENDED
) {
1170 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_INVALID_STATE
);
1174 if (cancel_next_seek_
) {
1175 cancel_next_seek_
= false;
1176 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1180 SeekAllSources(time
);
1181 StartReturningData();
1183 if (IsSeekWaitingForData_Locked()) {
1184 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1188 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1191 // Demuxer implementation.
1192 base::Time
ChunkDemuxer::GetTimelineOffset() const {
1193 return timeline_offset_
;
1196 DemuxerStream
* ChunkDemuxer::GetStream(DemuxerStream::Type type
) {
1197 DCHECK_NE(type
, DemuxerStream::TEXT
);
1198 base::AutoLock
auto_lock(lock_
);
1199 if (type
== DemuxerStream::VIDEO
)
1200 return video_
.get();
1202 if (type
== DemuxerStream::AUDIO
)
1203 return audio_
.get();
1208 TimeDelta
ChunkDemuxer::GetStartTime() const {
1212 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time
) {
1213 DVLOG(1) << "StartWaitingForSeek()";
1214 base::AutoLock
auto_lock(lock_
);
1215 DCHECK(state_
== INITIALIZED
|| state_
== ENDED
|| state_
== SHUTDOWN
||
1216 state_
== PARSE_ERROR
) << state_
;
1217 DCHECK(seek_cb_
.is_null());
1219 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1222 AbortPendingReads();
1223 SeekAllSources(seek_time
);
1225 // Cancel state set in CancelPendingSeek() since we want to
1226 // accept the next Seek().
1227 cancel_next_seek_
= false;
1230 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time
) {
1231 base::AutoLock
auto_lock(lock_
);
1232 DCHECK_NE(state_
, INITIALIZING
);
1233 DCHECK(seek_cb_
.is_null() || IsSeekWaitingForData_Locked());
1235 if (cancel_next_seek_
)
1238 AbortPendingReads();
1239 SeekAllSources(seek_time
);
1241 if (seek_cb_
.is_null()) {
1242 cancel_next_seek_
= true;
1246 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1249 ChunkDemuxer::Status
ChunkDemuxer::AddId(const std::string
& id
,
1250 const std::string
& type
,
1251 std::vector
<std::string
>& codecs
) {
1252 base::AutoLock
auto_lock(lock_
);
1254 if ((state_
!= WAITING_FOR_INIT
&& state_
!= INITIALIZING
) || IsValidId(id
))
1255 return kReachedIdLimit
;
1257 bool has_audio
= false;
1258 bool has_video
= false;
1259 scoped_ptr
<media::StreamParser
> stream_parser(StreamParserFactory::Create(
1260 type
, codecs
, media_log_
, &has_audio
, &has_video
));
1263 return ChunkDemuxer::kNotSupported
;
1265 if ((has_audio
&& !source_id_audio_
.empty()) ||
1266 (has_video
&& !source_id_video_
.empty()))
1267 return kReachedIdLimit
;
1270 source_id_audio_
= id
;
1273 source_id_video_
= id
;
1275 scoped_ptr
<FrameProcessor
> frame_processor(
1276 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary
,
1277 base::Unretained(this)),
1280 scoped_ptr
<SourceState
> source_state(new SourceState(
1281 stream_parser
.Pass(), frame_processor
.Pass(),
1282 base::Bind(&ChunkDemuxer::CreateDemuxerStream
, base::Unretained(this)),
1285 SourceState::NewTextTrackCB new_text_track_cb
;
1288 new_text_track_cb
= base::Bind(&ChunkDemuxer::OnNewTextTrack
,
1289 base::Unretained(this));
1293 base::Bind(&ChunkDemuxer::OnSourceInitDone
, base::Unretained(this)),
1294 has_audio
, has_video
, encrypted_media_init_data_cb_
, new_text_track_cb
);
1296 source_state_map_
[id
] = source_state
.release();
1300 void ChunkDemuxer::RemoveId(const std::string
& id
) {
1301 base::AutoLock
auto_lock(lock_
);
1302 CHECK(IsValidId(id
));
1304 delete source_state_map_
[id
];
1305 source_state_map_
.erase(id
);
1307 if (source_id_audio_
== id
)
1308 source_id_audio_
.clear();
1310 if (source_id_video_
== id
)
1311 source_id_video_
.clear();
1314 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges(const std::string
& id
) const {
1315 base::AutoLock
auto_lock(lock_
);
1316 DCHECK(!id
.empty());
1318 SourceStateMap::const_iterator itr
= source_state_map_
.find(id
);
1320 DCHECK(itr
!= source_state_map_
.end());
1321 return itr
->second
->GetBufferedRanges(duration_
, state_
== ENDED
);
1324 void ChunkDemuxer::AppendData(
1325 const std::string
& id
,
1328 TimeDelta append_window_start
,
1329 TimeDelta append_window_end
,
1330 TimeDelta
* timestamp_offset
,
1331 const InitSegmentReceivedCB
& init_segment_received_cb
) {
1332 DVLOG(1) << "AppendData(" << id
<< ", " << length
<< ")";
1334 DCHECK(!id
.empty());
1335 DCHECK(timestamp_offset
);
1336 DCHECK(!init_segment_received_cb
.is_null());
1338 Ranges
<TimeDelta
> ranges
;
1341 base::AutoLock
auto_lock(lock_
);
1342 DCHECK_NE(state_
, ENDED
);
1344 // Capture if any of the SourceBuffers are waiting for data before we start
1346 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1356 DCHECK(IsValidId(id
));
1357 if (!source_state_map_
[id
]->Append(data
, length
,
1358 append_window_start
,
1361 init_segment_received_cb
)) {
1362 ReportError_Locked(PIPELINE_ERROR_DECODE
);
1368 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1371 case WAITING_FOR_INIT
:
1374 DVLOG(1) << "AppendData(): called in unexpected state " << state_
;
1378 // Check to see if data was appended at the pending seek point. This
1379 // indicates we have parsed enough data to complete the seek.
1380 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1381 !seek_cb_
.is_null()) {
1382 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1385 ranges
= GetBufferedRanges_Locked();
1388 for (size_t i
= 0; i
< ranges
.size(); ++i
)
1389 host_
->AddBufferedTimeRange(ranges
.start(i
), ranges
.end(i
));
1392 void ChunkDemuxer::Abort(const std::string
& id
,
1393 TimeDelta append_window_start
,
1394 TimeDelta append_window_end
,
1395 TimeDelta
* timestamp_offset
) {
1396 DVLOG(1) << "Abort(" << id
<< ")";
1397 base::AutoLock
auto_lock(lock_
);
1398 DCHECK(!id
.empty());
1399 CHECK(IsValidId(id
));
1400 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1401 source_state_map_
[id
]->Abort(append_window_start
,
1404 // Abort can possibly emit some buffers.
1405 // Need to check whether seeking can be completed.
1406 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1407 !seek_cb_
.is_null()) {
1408 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1412 void ChunkDemuxer::Remove(const std::string
& id
, TimeDelta start
,
1414 DVLOG(1) << "Remove(" << id
<< ", " << start
.InSecondsF()
1415 << ", " << end
.InSecondsF() << ")";
1416 base::AutoLock
auto_lock(lock_
);
1418 DCHECK(!id
.empty());
1419 CHECK(IsValidId(id
));
1420 DCHECK(start
>= base::TimeDelta()) << start
.InSecondsF();
1421 DCHECK(start
< end
) << "start " << start
.InSecondsF()
1422 << " end " << end
.InSecondsF();
1423 DCHECK(duration_
!= kNoTimestamp());
1424 DCHECK(start
<= duration_
) << "start " << start
.InSecondsF()
1425 << " duration " << duration_
.InSecondsF();
1427 if (start
== duration_
)
1430 source_state_map_
[id
]->Remove(start
, end
, duration_
);
1433 double ChunkDemuxer::GetDuration() {
1434 base::AutoLock
auto_lock(lock_
);
1435 return GetDuration_Locked();
1438 double ChunkDemuxer::GetDuration_Locked() {
1439 lock_
.AssertAcquired();
1440 if (duration_
== kNoTimestamp())
1441 return std::numeric_limits
<double>::quiet_NaN();
1443 // Return positive infinity if the resource is unbounded.
1444 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1445 if (duration_
== kInfiniteDuration())
1446 return std::numeric_limits
<double>::infinity();
1448 if (user_specified_duration_
>= 0)
1449 return user_specified_duration_
;
1451 return duration_
.InSecondsF();
1454 void ChunkDemuxer::SetDuration(double duration
) {
1455 base::AutoLock
auto_lock(lock_
);
1456 DVLOG(1) << "SetDuration(" << duration
<< ")";
1457 DCHECK_GE(duration
, 0);
1459 if (duration
== GetDuration_Locked())
1462 // Compute & bounds check the TimeDelta representation of duration.
1463 // This can be different if the value of |duration| doesn't fit the range or
1464 // precision of TimeDelta.
1465 TimeDelta min_duration
= TimeDelta::FromInternalValue(1);
1466 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1467 TimeDelta max_duration
= TimeDelta::FromInternalValue(kint64max
- 1);
1468 double min_duration_in_seconds
= min_duration
.InSecondsF();
1469 double max_duration_in_seconds
= max_duration
.InSecondsF();
1471 TimeDelta duration_td
;
1472 if (duration
== std::numeric_limits
<double>::infinity()) {
1473 duration_td
= media::kInfiniteDuration();
1474 } else if (duration
< min_duration_in_seconds
) {
1475 duration_td
= min_duration
;
1476 } else if (duration
> max_duration_in_seconds
) {
1477 duration_td
= max_duration
;
1479 duration_td
= TimeDelta::FromMicroseconds(
1480 duration
* base::Time::kMicrosecondsPerSecond
);
1483 DCHECK(duration_td
> TimeDelta());
1485 user_specified_duration_
= duration
;
1486 duration_
= duration_td
;
1487 host_
->SetDuration(duration_
);
1489 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1490 itr
!= source_state_map_
.end(); ++itr
) {
1491 itr
->second
->OnSetDuration(duration_
);
1495 bool ChunkDemuxer::IsParsingMediaSegment(const std::string
& id
) {
1496 base::AutoLock
auto_lock(lock_
);
1497 DVLOG(1) << "IsParsingMediaSegment(" << id
<< ")";
1498 CHECK(IsValidId(id
));
1500 return source_state_map_
[id
]->parsing_media_segment();
1503 void ChunkDemuxer::SetSequenceMode(const std::string
& id
,
1504 bool sequence_mode
) {
1505 base::AutoLock
auto_lock(lock_
);
1506 DVLOG(1) << "SetSequenceMode(" << id
<< ", " << sequence_mode
<< ")";
1507 CHECK(IsValidId(id
));
1508 DCHECK_NE(state_
, ENDED
);
1510 source_state_map_
[id
]->SetSequenceMode(sequence_mode
);
1513 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1514 const std::string
& id
,
1515 base::TimeDelta timestamp_offset
) {
1516 base::AutoLock
auto_lock(lock_
);
1517 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id
<< ", "
1518 << timestamp_offset
.InSecondsF() << ")";
1519 CHECK(IsValidId(id
));
1520 DCHECK_NE(state_
, ENDED
);
1522 source_state_map_
[id
]->SetGroupStartTimestampIfInSequenceMode(
1527 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status
) {
1528 DVLOG(1) << "MarkEndOfStream(" << status
<< ")";
1529 base::AutoLock
auto_lock(lock_
);
1530 DCHECK_NE(state_
, WAITING_FOR_INIT
);
1531 DCHECK_NE(state_
, ENDED
);
1533 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1536 if (state_
== INITIALIZING
) {
1537 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1541 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1542 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1543 itr
!= source_state_map_
.end(); ++itr
) {
1544 itr
->second
->MarkEndOfStream();
1547 CompletePendingReadsIfPossible();
1549 // Give a chance to resume the pending seek process.
1550 if (status
!= PIPELINE_OK
) {
1551 ReportError_Locked(status
);
1555 ChangeState_Locked(ENDED
);
1556 DecreaseDurationIfNecessary();
1558 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1559 !seek_cb_
.is_null()) {
1560 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1564 void ChunkDemuxer::UnmarkEndOfStream() {
1565 DVLOG(1) << "UnmarkEndOfStream()";
1566 base::AutoLock
auto_lock(lock_
);
1567 DCHECK_EQ(state_
, ENDED
);
1569 ChangeState_Locked(INITIALIZED
);
1571 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1572 itr
!= source_state_map_
.end(); ++itr
) {
1573 itr
->second
->UnmarkEndOfStream();
1577 void ChunkDemuxer::Shutdown() {
1578 DVLOG(1) << "Shutdown()";
1579 base::AutoLock
auto_lock(lock_
);
1581 if (state_
== SHUTDOWN
)
1584 ShutdownAllStreams();
1586 ChangeState_Locked(SHUTDOWN
);
1588 if(!seek_cb_
.is_null())
1589 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_ABORT
);
1592 void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type
,
1593 size_t memory_limit
) {
1594 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1595 itr
!= source_state_map_
.end(); ++itr
) {
1596 itr
->second
->SetMemoryLimits(type
, memory_limit
);
1600 void ChunkDemuxer::ChangeState_Locked(State new_state
) {
1601 lock_
.AssertAcquired();
1602 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1603 << state_
<< " -> " << new_state
;
1607 ChunkDemuxer::~ChunkDemuxer() {
1608 DCHECK_NE(state_
, INITIALIZED
);
1610 STLDeleteValues(&source_state_map_
);
1613 void ChunkDemuxer::ReportError_Locked(PipelineStatus error
) {
1614 DVLOG(1) << "ReportError_Locked(" << error
<< ")";
1615 lock_
.AssertAcquired();
1616 DCHECK_NE(error
, PIPELINE_OK
);
1618 ChangeState_Locked(PARSE_ERROR
);
1620 PipelineStatusCB cb
;
1622 if (!init_cb_
.is_null()) {
1623 std::swap(cb
, init_cb_
);
1625 if (!seek_cb_
.is_null())
1626 std::swap(cb
, seek_cb_
);
1628 ShutdownAllStreams();
1631 if (!cb
.is_null()) {
1636 base::AutoUnlock
auto_unlock(lock_
);
1637 host_
->OnDemuxerError(error
);
1640 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1641 lock_
.AssertAcquired();
1642 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1643 itr
!= source_state_map_
.end(); ++itr
) {
1644 if (itr
->second
->IsSeekWaitingForData())
1651 void ChunkDemuxer::OnSourceInitDone(
1652 const StreamParser::InitParameters
& params
) {
1653 DVLOG(1) << "OnSourceInitDone(" << params
.duration
.InSecondsF() << ")";
1654 lock_
.AssertAcquired();
1655 DCHECK_EQ(state_
, INITIALIZING
);
1656 if (!audio_
&& !video_
) {
1657 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1661 if (params
.duration
!= TimeDelta() && duration_
== kNoTimestamp())
1662 UpdateDuration(params
.duration
);
1664 if (!params
.timeline_offset
.is_null()) {
1665 if (!timeline_offset_
.is_null() &&
1666 params
.timeline_offset
!= timeline_offset_
) {
1667 MEDIA_LOG(ERROR
, media_log_
)
1668 << "Timeline offset is not the same across all SourceBuffers.";
1669 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1673 timeline_offset_
= params
.timeline_offset
;
1676 if (params
.liveness
!= DemuxerStream::LIVENESS_UNKNOWN
) {
1678 audio_
->SetLiveness(params
.liveness
);
1680 video_
->SetLiveness(params
.liveness
);
1683 // Wait until all streams have initialized.
1684 if ((!source_id_audio_
.empty() && !audio_
) ||
1685 (!source_id_video_
.empty() && !video_
)) {
1689 SeekAllSources(GetStartTime());
1690 StartReturningData();
1692 if (duration_
== kNoTimestamp())
1693 duration_
= kInfiniteDuration();
1695 // The demuxer is now initialized after the |start_timestamp_| was set.
1696 ChangeState_Locked(INITIALIZED
);
1697 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
1701 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type
) {
1703 case DemuxerStream::AUDIO
:
1707 new ChunkDemuxerStream(DemuxerStream::AUDIO
, splice_frames_enabled_
));
1708 return audio_
.get();
1710 case DemuxerStream::VIDEO
:
1714 new ChunkDemuxerStream(DemuxerStream::VIDEO
, splice_frames_enabled_
));
1715 return video_
.get();
1717 case DemuxerStream::TEXT
: {
1718 return new ChunkDemuxerStream(DemuxerStream::TEXT
,
1719 splice_frames_enabled_
);
1722 case DemuxerStream::UNKNOWN
:
1723 case DemuxerStream::NUM_TYPES
:
1731 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
1732 const TextTrackConfig
& config
) {
1733 lock_
.AssertAcquired();
1734 DCHECK_NE(state_
, SHUTDOWN
);
1735 host_
->AddTextStream(text_stream
, config
);
1738 bool ChunkDemuxer::IsValidId(const std::string
& source_id
) const {
1739 lock_
.AssertAcquired();
1740 return source_state_map_
.count(source_id
) > 0u;
1743 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration
) {
1744 DCHECK(duration_
!= new_duration
);
1745 user_specified_duration_
= -1;
1746 duration_
= new_duration
;
1747 host_
->SetDuration(new_duration
);
1750 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration
) {
1751 DCHECK(new_duration
!= kNoTimestamp());
1752 DCHECK(new_duration
!= kInfiniteDuration());
1754 // Per April 1, 2014 MSE spec editor's draft:
1755 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1756 // media-source.html#sourcebuffer-coded-frame-processing
1757 // 5. If the media segment contains data beyond the current duration, then run
1758 // the duration change algorithm with new duration set to the maximum of
1759 // the current duration and the group end timestamp.
1761 if (new_duration
<= duration_
)
1764 DVLOG(2) << __FUNCTION__
<< ": Increasing duration: "
1765 << duration_
.InSecondsF() << " -> " << new_duration
.InSecondsF();
1767 UpdateDuration(new_duration
);
1770 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1771 lock_
.AssertAcquired();
1773 TimeDelta max_duration
;
1775 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1776 itr
!= source_state_map_
.end(); ++itr
) {
1777 max_duration
= std::max(max_duration
,
1778 itr
->second
->GetMaxBufferedDuration());
1781 if (max_duration
== TimeDelta())
1784 if (max_duration
< duration_
)
1785 UpdateDuration(max_duration
);
1788 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges() const {
1789 base::AutoLock
auto_lock(lock_
);
1790 return GetBufferedRanges_Locked();
1793 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges_Locked() const {
1794 lock_
.AssertAcquired();
1796 bool ended
= state_
== ENDED
;
1797 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1798 // we'll need to update this loop to only add ranges from active sources.
1799 RangesList ranges_list
;
1800 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1801 itr
!= source_state_map_
.end(); ++itr
) {
1802 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration_
, ended
));
1805 return ComputeIntersection(ranges_list
, ended
);
1808 void ChunkDemuxer::StartReturningData() {
1809 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1810 itr
!= source_state_map_
.end(); ++itr
) {
1811 itr
->second
->StartReturningData();
1815 void ChunkDemuxer::AbortPendingReads() {
1816 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1817 itr
!= source_state_map_
.end(); ++itr
) {
1818 itr
->second
->AbortReads();
1822 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time
) {
1823 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1824 itr
!= source_state_map_
.end(); ++itr
) {
1825 itr
->second
->Seek(seek_time
);
1829 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1830 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1831 itr
!= source_state_map_
.end(); ++itr
) {
1832 itr
->second
->CompletePendingReadIfPossible();
1836 void ChunkDemuxer::ShutdownAllStreams() {
1837 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1838 itr
!= source_state_map_
.end(); ++itr
) {
1839 itr
->second
->Shutdown();
1843 } // namespace media