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/message_loop/message_loop_proxy.h"
15 #include "base/stl_util.h"
16 #include "media/base/audio_decoder_config.h"
17 #include "media/base/bind_to_current_loop.h"
18 #include "media/base/stream_parser_buffer.h"
19 #include "media/base/video_decoder_config.h"
20 #include "media/filters/frame_processor.h"
21 #include "media/filters/stream_parser_factory.h"
23 using base::TimeDelta
;
27 static TimeDelta
EndTimestamp(const StreamParser::BufferQueue
& queue
) {
28 return queue
.back()->timestamp() + queue
.back()->duration();
31 // List of time ranges for each SourceBuffer.
32 typedef std::list
<Ranges
<TimeDelta
> > RangesList
;
33 static Ranges
<TimeDelta
> ComputeIntersection(const RangesList
& activeRanges
,
35 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
36 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
38 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
39 // TimeRanges object and abort these steps.
40 if (activeRanges
.empty())
41 return Ranges
<TimeDelta
>();
43 // Step 2: Let active ranges be the ranges returned by buffered for each
44 // SourceBuffer object in activeSourceBuffers.
45 // Step 3: Let highest end time be the largest range end time in the active
47 TimeDelta highest_end_time
;
48 for (RangesList::const_iterator itr
= activeRanges
.begin();
49 itr
!= activeRanges
.end(); ++itr
) {
53 highest_end_time
= std::max(highest_end_time
, itr
->end(itr
->size() - 1));
56 // Step 4: Let intersection ranges equal a TimeRange object containing a
57 // single range from 0 to highest end time.
58 Ranges
<TimeDelta
> intersection_ranges
;
59 intersection_ranges
.Add(TimeDelta(), highest_end_time
);
61 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
63 for (RangesList::const_iterator itr
= activeRanges
.begin();
64 itr
!= activeRanges
.end(); ++itr
) {
65 // Step 5.1: Let source ranges equal the ranges returned by the buffered
66 // attribute on the current SourceBuffer.
67 Ranges
<TimeDelta
> source_ranges
= *itr
;
69 // Step 5.2: If readyState is "ended", then set the end time on the last
70 // range in source ranges to highest end time.
71 if (ended
&& source_ranges
.size() > 0u) {
72 source_ranges
.Add(source_ranges
.start(source_ranges
.size() - 1),
76 // Step 5.3: Let new intersection ranges equal the intersection between
77 // the intersection ranges and the source ranges.
78 // Step 5.4: Replace the ranges in intersection ranges with the new
79 // intersection ranges.
80 intersection_ranges
= intersection_ranges
.IntersectionWith(source_ranges
);
83 return intersection_ranges
;
86 // Contains state belonging to a source id.
89 // Callback signature used to create ChunkDemuxerStreams.
90 typedef base::Callback
<ChunkDemuxerStream
*(
91 DemuxerStream::Type
)> CreateDemuxerStreamCB
;
93 typedef ChunkDemuxer::InitSegmentReceivedCB InitSegmentReceivedCB
;
95 typedef base::Callback
<void(
96 ChunkDemuxerStream
*, const TextTrackConfig
&)> NewTextTrackCB
;
99 scoped_ptr
<StreamParser
> stream_parser
,
100 scoped_ptr
<FrameProcessor
> frame_processor
, const LogCB
& log_cb
,
101 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
102 const scoped_refptr
<MediaLog
>& media_log
);
106 void Init(const StreamParser::InitCB
& init_cb
,
109 const StreamParser::EncryptedMediaInitDataCB
&
110 encrypted_media_init_data_cb
,
111 const NewTextTrackCB
& new_text_track_cb
);
113 // Appends new data to the StreamParser.
114 // Returns true if the data was successfully appended. Returns false if an
115 // error occurred. |*timestamp_offset| is used and possibly updated by the
116 // append. |append_window_start| and |append_window_end| correspond to the MSE
117 // spec's similarly named source buffer attributes that are used in coded
118 // frame processing. |init_segment_received_cb| is run for each new fully
119 // parsed initialization segment.
120 bool Append(const uint8
* data
,
122 TimeDelta append_window_start
,
123 TimeDelta append_window_end
,
124 TimeDelta
* timestamp_offset
,
125 const InitSegmentReceivedCB
& init_segment_received_cb
);
127 // Aborts the current append sequence and resets the parser.
128 void Abort(TimeDelta append_window_start
,
129 TimeDelta append_window_end
,
130 TimeDelta
* timestamp_offset
);
132 // Calls Remove(|start|, |end|, |duration|) on all
133 // ChunkDemuxerStreams managed by this object.
134 void Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
);
136 // Returns true if currently parsing a media segment, or false otherwise.
137 bool parsing_media_segment() const { return parsing_media_segment_
; }
139 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
140 void SetSequenceMode(bool sequence_mode
);
142 // Signals the coded frame processor to update its group start timestamp to be
143 // |timestamp_offset| if it is in sequence append mode.
144 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset
);
146 // Returns the range of buffered data in this source, capped at |duration|.
147 // |ended| - Set to true if end of stream has been signaled and the special
148 // end of stream range logic needs to be executed.
149 Ranges
<TimeDelta
> GetBufferedRanges(TimeDelta duration
, bool ended
) const;
151 // Returns the highest buffered duration across all streams managed
153 // Returns TimeDelta() if none of the streams contain buffered data.
154 TimeDelta
GetMaxBufferedDuration() const;
156 // Helper methods that call methods with similar names on all the
157 // ChunkDemuxerStreams managed by this object.
158 void StartReturningData();
160 void Seek(TimeDelta seek_time
);
161 void CompletePendingReadIfPossible();
162 void OnSetDuration(TimeDelta duration
);
163 void MarkEndOfStream();
164 void UnmarkEndOfStream();
166 // Sets the memory limit on each stream of a specific type.
167 // |memory_limit| is the maximum number of bytes each stream of type |type|
168 // is allowed to hold in its buffer.
169 void SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
);
170 bool IsSeekWaitingForData() const;
173 // Called by the |stream_parser_| when a new initialization segment is
175 // Returns true on a successful call. Returns false if an error occurred while
176 // processing decoder configurations.
177 bool OnNewConfigs(bool allow_audio
, bool allow_video
,
178 const AudioDecoderConfig
& audio_config
,
179 const VideoDecoderConfig
& video_config
,
180 const StreamParser::TextTrackConfigMap
& text_configs
);
182 // Called by the |stream_parser_| at the beginning of a new media segment.
183 void OnNewMediaSegment();
185 // Called by the |stream_parser_| at the end of a media segment.
186 void OnEndOfMediaSegment();
188 // Called by the |stream_parser_| when new buffers have been parsed.
189 // It processes the new buffers using |frame_processor_|, which includes
190 // appending the processed frames to associated demuxer streams for each
192 // Returns true on a successful call. Returns false if an error occurred while
193 // processing the buffers.
194 bool OnNewBuffers(const StreamParser::BufferQueue
& audio_buffers
,
195 const StreamParser::BufferQueue
& video_buffers
,
196 const StreamParser::TextBufferQueueMap
& text_map
);
198 void OnSourceInitDone(const StreamParser::InitParameters
& params
);
200 CreateDemuxerStreamCB create_demuxer_stream_cb_
;
201 NewTextTrackCB new_text_track_cb_
;
203 // During Append(), if OnNewBuffers() coded frame processing updates the
204 // timestamp offset then |*timestamp_offset_during_append_| is also updated
205 // so Append()'s caller can know the new offset. This pointer is only non-NULL
206 // during the lifetime of an Append() call.
207 TimeDelta
* timestamp_offset_during_append_
;
209 // During Append(), coded frame processing triggered by OnNewBuffers()
210 // requires these two attributes. These are only valid during the lifetime of
212 TimeDelta append_window_start_during_append_
;
213 TimeDelta append_window_end_during_append_
;
215 // Set to true if the next buffers appended within the append window
216 // represent the start of a new media segment. This flag being set
217 // triggers a call to |new_segment_cb_| when the new buffers are
218 // appended. The flag is set on actual media segment boundaries and
219 // when the "append window" filtering causes discontinuities in the
221 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
222 // processing's discontinuity logic is enough. See http://crbug.com/351489.
223 bool new_media_segment_
;
225 // Keeps track of whether a media segment is being parsed.
226 bool parsing_media_segment_
;
228 // The object used to parse appended data.
229 scoped_ptr
<StreamParser
> stream_parser_
;
231 ChunkDemuxerStream
* audio_
; // Not owned by |this|.
232 ChunkDemuxerStream
* video_
; // Not owned by |this|.
234 typedef std::map
<StreamParser::TrackId
, ChunkDemuxerStream
*> TextStreamMap
;
235 TextStreamMap text_stream_map_
; // |this| owns the map's stream pointers.
237 scoped_ptr
<FrameProcessor
> frame_processor_
;
239 scoped_refptr
<MediaLog
> media_log_
;
240 StreamParser::InitCB init_cb_
;
242 // During Append(), OnNewConfigs() will trigger the initialization segment
243 // received algorithm. This callback is only non-NULL during the lifetime of
244 // an Append() call. Note, the MSE spec explicitly disallows this algorithm
245 // during an Abort(), since Abort() is allowed only to emit coded frames, and
246 // only if the parser is PARSING_MEDIA_SEGMENT (not an INIT segment).
247 InitSegmentReceivedCB init_segment_received_cb_
;
249 // Indicates that timestampOffset should be updated automatically during
250 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
251 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
252 // changes to MSE spec. See http://crbug.com/371499.
253 bool auto_update_timestamp_offset_
;
255 DISALLOW_COPY_AND_ASSIGN(SourceState
);
258 SourceState::SourceState(scoped_ptr
<StreamParser
> stream_parser
,
259 scoped_ptr
<FrameProcessor
> frame_processor
,
261 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
262 const scoped_refptr
<MediaLog
>& media_log
)
263 : create_demuxer_stream_cb_(create_demuxer_stream_cb
),
264 timestamp_offset_during_append_(NULL
),
265 new_media_segment_(false),
266 parsing_media_segment_(false),
267 stream_parser_(stream_parser
.release()),
270 frame_processor_(frame_processor
.release()),
272 media_log_(media_log
),
273 auto_update_timestamp_offset_(false) {
274 DCHECK(!create_demuxer_stream_cb_
.is_null());
275 DCHECK(frame_processor_
);
278 SourceState::~SourceState() {
281 STLDeleteValues(&text_stream_map_
);
284 void SourceState::Init(
285 const StreamParser::InitCB
& init_cb
,
288 const StreamParser::EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
289 const NewTextTrackCB
& new_text_track_cb
) {
290 new_text_track_cb_
= new_text_track_cb
;
293 stream_parser_
->Init(
294 base::Bind(&SourceState::OnSourceInitDone
, base::Unretained(this)),
295 base::Bind(&SourceState::OnNewConfigs
, base::Unretained(this),
296 allow_audio
, allow_video
),
297 base::Bind(&SourceState::OnNewBuffers
, base::Unretained(this)),
298 new_text_track_cb_
.is_null(), encrypted_media_init_data_cb
,
299 base::Bind(&SourceState::OnNewMediaSegment
, base::Unretained(this)),
300 base::Bind(&SourceState::OnEndOfMediaSegment
, base::Unretained(this)),
304 void SourceState::SetSequenceMode(bool sequence_mode
) {
305 DCHECK(!parsing_media_segment_
);
307 frame_processor_
->SetSequenceMode(sequence_mode
);
310 void SourceState::SetGroupStartTimestampIfInSequenceMode(
311 base::TimeDelta timestamp_offset
) {
312 DCHECK(!parsing_media_segment_
);
314 frame_processor_
->SetGroupStartTimestampIfInSequenceMode(timestamp_offset
);
317 bool SourceState::Append(
320 TimeDelta append_window_start
,
321 TimeDelta append_window_end
,
322 TimeDelta
* timestamp_offset
,
323 const InitSegmentReceivedCB
& init_segment_received_cb
) {
324 DCHECK(timestamp_offset
);
325 DCHECK(!timestamp_offset_during_append_
);
326 DCHECK(!init_segment_received_cb
.is_null());
327 DCHECK(init_segment_received_cb_
.is_null());
328 append_window_start_during_append_
= append_window_start
;
329 append_window_end_during_append_
= append_window_end
;
330 timestamp_offset_during_append_
= timestamp_offset
;
331 init_segment_received_cb_
= init_segment_received_cb
;
333 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
334 // append window and timestamp offset pointer. See http://crbug.com/351454.
335 bool result
= stream_parser_
->Parse(data
, length
);
337 MEDIA_LOG(ERROR
, log_cb_
)
338 << __FUNCTION__
<< ": stream parsing failed."
339 << " Data size=" << length
340 << " append_window_start=" << append_window_start
.InSecondsF()
341 << " append_window_end=" << append_window_end
.InSecondsF();
343 timestamp_offset_during_append_
= NULL
;
344 init_segment_received_cb_
.Reset();
348 void SourceState::Abort(TimeDelta append_window_start
,
349 TimeDelta append_window_end
,
350 base::TimeDelta
* timestamp_offset
) {
351 DCHECK(timestamp_offset
);
352 DCHECK(!timestamp_offset_during_append_
);
353 timestamp_offset_during_append_
= timestamp_offset
;
354 append_window_start_during_append_
= append_window_start
;
355 append_window_end_during_append_
= append_window_end
;
357 stream_parser_
->Flush();
358 timestamp_offset_during_append_
= NULL
;
360 frame_processor_
->Reset();
361 parsing_media_segment_
= false;
364 void SourceState::Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
) {
366 audio_
->Remove(start
, end
, duration
);
369 video_
->Remove(start
, end
, duration
);
371 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
372 itr
!= text_stream_map_
.end(); ++itr
) {
373 itr
->second
->Remove(start
, end
, duration
);
377 Ranges
<TimeDelta
> SourceState::GetBufferedRanges(TimeDelta duration
,
379 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
380 // this code to only add ranges from active tracks.
381 RangesList ranges_list
;
383 ranges_list
.push_back(audio_
->GetBufferedRanges(duration
));
386 ranges_list
.push_back(video_
->GetBufferedRanges(duration
));
388 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
389 itr
!= text_stream_map_
.end(); ++itr
) {
390 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration
));
393 return ComputeIntersection(ranges_list
, ended
);
396 TimeDelta
SourceState::GetMaxBufferedDuration() const {
397 TimeDelta max_duration
;
400 max_duration
= std::max(max_duration
, audio_
->GetBufferedDuration());
403 max_duration
= std::max(max_duration
, video_
->GetBufferedDuration());
405 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
406 itr
!= text_stream_map_
.end(); ++itr
) {
407 max_duration
= std::max(max_duration
, itr
->second
->GetBufferedDuration());
413 void SourceState::StartReturningData() {
415 audio_
->StartReturningData();
418 video_
->StartReturningData();
420 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
421 itr
!= text_stream_map_
.end(); ++itr
) {
422 itr
->second
->StartReturningData();
426 void SourceState::AbortReads() {
428 audio_
->AbortReads();
431 video_
->AbortReads();
433 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
434 itr
!= text_stream_map_
.end(); ++itr
) {
435 itr
->second
->AbortReads();
439 void SourceState::Seek(TimeDelta seek_time
) {
441 audio_
->Seek(seek_time
);
444 video_
->Seek(seek_time
);
446 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
447 itr
!= text_stream_map_
.end(); ++itr
) {
448 itr
->second
->Seek(seek_time
);
452 void SourceState::CompletePendingReadIfPossible() {
454 audio_
->CompletePendingReadIfPossible();
457 video_
->CompletePendingReadIfPossible();
459 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
460 itr
!= text_stream_map_
.end(); ++itr
) {
461 itr
->second
->CompletePendingReadIfPossible();
465 void SourceState::OnSetDuration(TimeDelta duration
) {
467 audio_
->OnSetDuration(duration
);
470 video_
->OnSetDuration(duration
);
472 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
473 itr
!= text_stream_map_
.end(); ++itr
) {
474 itr
->second
->OnSetDuration(duration
);
478 void SourceState::MarkEndOfStream() {
480 audio_
->MarkEndOfStream();
483 video_
->MarkEndOfStream();
485 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
486 itr
!= text_stream_map_
.end(); ++itr
) {
487 itr
->second
->MarkEndOfStream();
491 void SourceState::UnmarkEndOfStream() {
493 audio_
->UnmarkEndOfStream();
496 video_
->UnmarkEndOfStream();
498 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
499 itr
!= text_stream_map_
.end(); ++itr
) {
500 itr
->second
->UnmarkEndOfStream();
504 void SourceState::Shutdown() {
511 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
512 itr
!= text_stream_map_
.end(); ++itr
) {
513 itr
->second
->Shutdown();
517 void SourceState::SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
) {
519 case DemuxerStream::AUDIO
:
521 audio_
->set_memory_limit(memory_limit
);
523 case DemuxerStream::VIDEO
:
525 video_
->set_memory_limit(memory_limit
);
527 case DemuxerStream::TEXT
:
528 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
529 itr
!= text_stream_map_
.end(); ++itr
) {
530 itr
->second
->set_memory_limit(memory_limit
);
533 case DemuxerStream::UNKNOWN
:
534 case DemuxerStream::NUM_TYPES
:
540 bool SourceState::IsSeekWaitingForData() const {
541 if (audio_
&& audio_
->IsSeekWaitingForData())
544 if (video_
&& video_
->IsSeekWaitingForData())
547 // NOTE: We are intentionally not checking the text tracks
548 // because text tracks are discontinuous and may not have data
549 // for the seek position. This is ok and playback should not be
550 // stalled because we don't have cues. If cues, with timestamps after
551 // the seek time, eventually arrive they will be delivered properly
552 // in response to ChunkDemuxerStream::Read() calls.
557 bool SourceState::OnNewConfigs(
558 bool allow_audio
, bool allow_video
,
559 const AudioDecoderConfig
& audio_config
,
560 const VideoDecoderConfig
& video_config
,
561 const StreamParser::TextTrackConfigMap
& text_configs
) {
562 DVLOG(1) << "OnNewConfigs(" << allow_audio
<< ", " << allow_video
563 << ", " << audio_config
.IsValidConfig()
564 << ", " << video_config
.IsValidConfig() << ")";
565 DCHECK(!init_segment_received_cb_
.is_null());
567 if (!audio_config
.IsValidConfig() && !video_config
.IsValidConfig()) {
568 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
572 // Signal an error if we get configuration info for stream types that weren't
573 // specified in AddId() or more configs after a stream is initialized.
574 if (allow_audio
!= audio_config
.IsValidConfig()) {
575 MEDIA_LOG(ERROR
, log_cb_
)
576 << "Initialization segment"
577 << (audio_config
.IsValidConfig() ? " has" : " does not have")
578 << " an audio track, but the mimetype"
579 << (allow_audio
? " specifies" : " does not specify")
580 << " an audio codec.";
584 if (allow_video
!= video_config
.IsValidConfig()) {
585 MEDIA_LOG(ERROR
, log_cb_
)
586 << "Initialization segment"
587 << (video_config
.IsValidConfig() ? " has" : " does not have")
588 << " a video track, but the mimetype"
589 << (allow_video
? " specifies" : " does not specify")
590 << " a video codec.";
595 if (audio_config
.IsValidConfig()) {
597 media_log_
->SetBooleanProperty("found_audio_stream", true);
600 audio_
->audio_decoder_config().codec() != audio_config
.codec()) {
601 media_log_
->SetStringProperty("audio_codec_name",
602 audio_config
.GetHumanReadableCodecName());
606 audio_
= create_demuxer_stream_cb_
.Run(DemuxerStream::AUDIO
);
609 DVLOG(1) << "Failed to create an audio stream.";
613 if (!frame_processor_
->AddTrack(FrameProcessor::kAudioTrackId
, audio_
)) {
614 DVLOG(1) << "Failed to add audio track to frame processor.";
619 frame_processor_
->OnPossibleAudioConfigUpdate(audio_config
);
620 success
&= audio_
->UpdateAudioConfig(audio_config
, log_cb_
);
623 if (video_config
.IsValidConfig()) {
625 media_log_
->SetBooleanProperty("found_video_stream", true);
628 video_
->video_decoder_config().codec() != video_config
.codec()) {
629 media_log_
->SetStringProperty("video_codec_name",
630 video_config
.GetHumanReadableCodecName());
634 video_
= create_demuxer_stream_cb_
.Run(DemuxerStream::VIDEO
);
637 DVLOG(1) << "Failed to create a video stream.";
641 if (!frame_processor_
->AddTrack(FrameProcessor::kVideoTrackId
, video_
)) {
642 DVLOG(1) << "Failed to add video track to frame processor.";
647 success
&= video_
->UpdateVideoConfig(video_config
, log_cb_
);
650 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr
;
651 if (text_stream_map_
.empty()) {
652 for (TextConfigItr itr
= text_configs
.begin();
653 itr
!= text_configs
.end(); ++itr
) {
654 ChunkDemuxerStream
* const text_stream
=
655 create_demuxer_stream_cb_
.Run(DemuxerStream::TEXT
);
656 if (!frame_processor_
->AddTrack(itr
->first
, text_stream
)) {
658 MEDIA_LOG(ERROR
, log_cb_
) << "Failed to add text track ID "
659 << itr
->first
<< " to frame processor.";
662 text_stream
->UpdateTextConfig(itr
->second
, log_cb_
);
663 text_stream_map_
[itr
->first
] = text_stream
;
664 new_text_track_cb_
.Run(text_stream
, itr
->second
);
667 const size_t text_count
= text_stream_map_
.size();
668 if (text_configs
.size() != text_count
) {
670 MEDIA_LOG(ERROR
, log_cb_
) << "The number of text track configs changed.";
671 } else if (text_count
== 1) {
672 TextConfigItr config_itr
= text_configs
.begin();
673 TextStreamMap::iterator stream_itr
= text_stream_map_
.begin();
674 ChunkDemuxerStream
* text_stream
= stream_itr
->second
;
675 TextTrackConfig old_config
= text_stream
->text_track_config();
676 TextTrackConfig
new_config(config_itr
->second
.kind(),
677 config_itr
->second
.label(),
678 config_itr
->second
.language(),
680 if (!new_config
.Matches(old_config
)) {
682 MEDIA_LOG(ERROR
, log_cb_
)
683 << "New text track config does not match old one.";
685 StreamParser::TrackId old_id
= stream_itr
->first
;
686 StreamParser::TrackId new_id
= config_itr
->first
;
687 if (new_id
!= old_id
) {
688 if (frame_processor_
->UpdateTrack(old_id
, new_id
)) {
689 text_stream_map_
.clear();
690 text_stream_map_
[config_itr
->first
] = text_stream
;
693 MEDIA_LOG(ERROR
, log_cb_
)
694 << "Error remapping single text track number";
699 for (TextConfigItr config_itr
= text_configs
.begin();
700 config_itr
!= text_configs
.end(); ++config_itr
) {
701 TextStreamMap::iterator stream_itr
=
702 text_stream_map_
.find(config_itr
->first
);
703 if (stream_itr
== text_stream_map_
.end()) {
705 MEDIA_LOG(ERROR
, log_cb_
)
706 << "Unexpected text track configuration for track ID "
707 << config_itr
->first
;
711 const TextTrackConfig
& new_config
= config_itr
->second
;
712 ChunkDemuxerStream
* stream
= stream_itr
->second
;
713 TextTrackConfig old_config
= stream
->text_track_config();
714 if (!new_config
.Matches(old_config
)) {
716 MEDIA_LOG(ERROR
, log_cb_
) << "New text track config for track ID "
718 << " does not match old one.";
725 frame_processor_
->SetAllTrackBuffersNeedRandomAccessPoint();
727 DVLOG(1) << "OnNewConfigs() : " << (success
? "success" : "failed");
729 init_segment_received_cb_
.Run();
734 void SourceState::OnNewMediaSegment() {
735 DVLOG(2) << "OnNewMediaSegment()";
736 parsing_media_segment_
= true;
737 new_media_segment_
= true;
740 void SourceState::OnEndOfMediaSegment() {
741 DVLOG(2) << "OnEndOfMediaSegment()";
742 parsing_media_segment_
= false;
743 new_media_segment_
= false;
746 bool SourceState::OnNewBuffers(
747 const StreamParser::BufferQueue
& audio_buffers
,
748 const StreamParser::BufferQueue
& video_buffers
,
749 const StreamParser::TextBufferQueueMap
& text_map
) {
750 DVLOG(2) << "OnNewBuffers()";
751 DCHECK(timestamp_offset_during_append_
);
752 DCHECK(parsing_media_segment_
);
754 const TimeDelta timestamp_offset_before_processing
=
755 *timestamp_offset_during_append_
;
757 // Calculate the new timestamp offset for audio/video tracks if the stream
758 // parser has requested automatic updates.
759 TimeDelta new_timestamp_offset
= timestamp_offset_before_processing
;
760 if (auto_update_timestamp_offset_
) {
761 const bool have_audio_buffers
= !audio_buffers
.empty();
762 const bool have_video_buffers
= !video_buffers
.empty();
763 if (have_audio_buffers
&& have_video_buffers
) {
764 new_timestamp_offset
+=
765 std::min(EndTimestamp(audio_buffers
), EndTimestamp(video_buffers
));
766 } else if (have_audio_buffers
) {
767 new_timestamp_offset
+= EndTimestamp(audio_buffers
);
768 } else if (have_video_buffers
) {
769 new_timestamp_offset
+= EndTimestamp(video_buffers
);
773 if (!frame_processor_
->ProcessFrames(audio_buffers
,
776 append_window_start_during_append_
,
777 append_window_end_during_append_
,
779 timestamp_offset_during_append_
)) {
783 // Only update the timestamp offset if the frame processor hasn't already.
784 if (auto_update_timestamp_offset_
&&
785 timestamp_offset_before_processing
== *timestamp_offset_during_append_
) {
786 *timestamp_offset_during_append_
= new_timestamp_offset
;
792 void SourceState::OnSourceInitDone(const StreamParser::InitParameters
& params
) {
793 auto_update_timestamp_offset_
= params
.auto_update_timestamp_offset
;
794 base::ResetAndReturn(&init_cb_
).Run(params
);
797 ChunkDemuxerStream::ChunkDemuxerStream(Type type
,
798 bool splice_frames_enabled
)
800 liveness_(DemuxerStream::LIVENESS_UNKNOWN
),
801 state_(UNINITIALIZED
),
802 splice_frames_enabled_(splice_frames_enabled
),
803 partial_append_window_trimming_enabled_(false) {
806 void ChunkDemuxerStream::StartReturningData() {
807 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
808 base::AutoLock
auto_lock(lock_
);
809 DCHECK(read_cb_
.is_null());
810 ChangeState_Locked(RETURNING_DATA_FOR_READS
);
813 void ChunkDemuxerStream::AbortReads() {
814 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
815 base::AutoLock
auto_lock(lock_
);
816 ChangeState_Locked(RETURNING_ABORT_FOR_READS
);
817 if (!read_cb_
.is_null())
818 base::ResetAndReturn(&read_cb_
).Run(kAborted
, NULL
);
821 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
822 base::AutoLock
auto_lock(lock_
);
823 if (read_cb_
.is_null())
826 CompletePendingReadIfPossible_Locked();
829 void ChunkDemuxerStream::Shutdown() {
830 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
831 base::AutoLock
auto_lock(lock_
);
832 ChangeState_Locked(SHUTDOWN
);
834 // Pass an end of stream buffer to the pending callback to signal that no more
835 // data will be sent.
836 if (!read_cb_
.is_null()) {
837 base::ResetAndReturn(&read_cb_
).Run(DemuxerStream::kOk
,
838 StreamParserBuffer::CreateEOSBuffer());
842 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
843 base::AutoLock
auto_lock(lock_
);
845 // This method should not be called for text tracks. See the note in
846 // SourceState::IsSeekWaitingForData().
847 DCHECK_NE(type_
, DemuxerStream::TEXT
);
849 return stream_
->IsSeekPending();
852 void ChunkDemuxerStream::Seek(TimeDelta time
) {
853 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time
.InSecondsF() << ")";
854 base::AutoLock
auto_lock(lock_
);
855 DCHECK(read_cb_
.is_null());
856 DCHECK(state_
== UNINITIALIZED
|| state_
== RETURNING_ABORT_FOR_READS
)
862 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue
& buffers
) {
866 base::AutoLock
auto_lock(lock_
);
867 DCHECK_NE(state_
, SHUTDOWN
);
868 if (!stream_
->Append(buffers
)) {
869 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
873 if (!read_cb_
.is_null())
874 CompletePendingReadIfPossible_Locked();
879 void ChunkDemuxerStream::Remove(TimeDelta start
, TimeDelta end
,
880 TimeDelta duration
) {
881 base::AutoLock
auto_lock(lock_
);
882 stream_
->Remove(start
, end
, duration
);
885 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration
) {
886 base::AutoLock
auto_lock(lock_
);
887 stream_
->OnSetDuration(duration
);
890 Ranges
<TimeDelta
> ChunkDemuxerStream::GetBufferedRanges(
891 TimeDelta duration
) const {
892 base::AutoLock
auto_lock(lock_
);
895 // Since text tracks are discontinuous and the lack of cues should not block
896 // playback, report the buffered range for text tracks as [0, |duration|) so
897 // that intesections with audio & video tracks are computed correctly when
898 // no cues are present.
899 Ranges
<TimeDelta
> text_range
;
900 text_range
.Add(TimeDelta(), duration
);
904 Ranges
<TimeDelta
> range
= stream_
->GetBufferedTime();
906 if (range
.size() == 0u)
909 // Clamp the end of the stream's buffered ranges to fit within the duration.
910 // This can be done by intersecting the stream's range with the valid time
912 Ranges
<TimeDelta
> valid_time_range
;
913 valid_time_range
.Add(range
.start(0), duration
);
914 return range
.IntersectionWith(valid_time_range
);
917 TimeDelta
ChunkDemuxerStream::GetBufferedDuration() const {
918 return stream_
->GetBufferedDuration();
921 void ChunkDemuxerStream::OnNewMediaSegment(DecodeTimestamp start_timestamp
) {
922 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
923 << start_timestamp
.InSecondsF() << ")";
924 base::AutoLock
auto_lock(lock_
);
925 stream_
->OnNewMediaSegment(start_timestamp
);
928 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig
& config
,
929 const LogCB
& log_cb
) {
930 DCHECK(config
.IsValidConfig());
931 DCHECK_EQ(type_
, AUDIO
);
932 base::AutoLock
auto_lock(lock_
);
934 DCHECK_EQ(state_
, UNINITIALIZED
);
936 // On platforms which support splice frames, enable splice frames and
937 // partial append window support for most codecs (notably: not opus).
938 const bool codec_supported
= config
.codec() == kCodecMP3
||
939 config
.codec() == kCodecAAC
||
940 config
.codec() == kCodecVorbis
;
941 splice_frames_enabled_
= splice_frames_enabled_
&& codec_supported
;
942 partial_append_window_trimming_enabled_
=
943 splice_frames_enabled_
&& codec_supported
;
946 new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
950 return stream_
->UpdateAudioConfig(config
);
953 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig
& config
,
954 const LogCB
& log_cb
) {
955 DCHECK(config
.IsValidConfig());
956 DCHECK_EQ(type_
, VIDEO
);
957 base::AutoLock
auto_lock(lock_
);
960 DCHECK_EQ(state_
, UNINITIALIZED
);
962 new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
966 return stream_
->UpdateVideoConfig(config
);
969 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig
& config
,
970 const LogCB
& log_cb
) {
971 DCHECK_EQ(type_
, TEXT
);
972 base::AutoLock
auto_lock(lock_
);
974 DCHECK_EQ(state_
, UNINITIALIZED
);
975 stream_
.reset(new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
978 void ChunkDemuxerStream::MarkEndOfStream() {
979 base::AutoLock
auto_lock(lock_
);
980 stream_
->MarkEndOfStream();
983 void ChunkDemuxerStream::UnmarkEndOfStream() {
984 base::AutoLock
auto_lock(lock_
);
985 stream_
->UnmarkEndOfStream();
988 // DemuxerStream methods.
989 void ChunkDemuxerStream::Read(const ReadCB
& read_cb
) {
990 base::AutoLock
auto_lock(lock_
);
991 DCHECK_NE(state_
, UNINITIALIZED
);
992 DCHECK(read_cb_
.is_null());
994 read_cb_
= BindToCurrentLoop(read_cb
);
995 CompletePendingReadIfPossible_Locked();
998 DemuxerStream::Type
ChunkDemuxerStream::type() const { return type_
; }
1000 DemuxerStream::Liveness
ChunkDemuxerStream::liveness() const {
1001 base::AutoLock
auto_lock(lock_
);
1005 AudioDecoderConfig
ChunkDemuxerStream::audio_decoder_config() {
1006 CHECK_EQ(type_
, AUDIO
);
1007 base::AutoLock
auto_lock(lock_
);
1008 return stream_
->GetCurrentAudioDecoderConfig();
1011 VideoDecoderConfig
ChunkDemuxerStream::video_decoder_config() {
1012 CHECK_EQ(type_
, VIDEO
);
1013 base::AutoLock
auto_lock(lock_
);
1014 return stream_
->GetCurrentVideoDecoderConfig();
1017 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
1019 VideoRotation
ChunkDemuxerStream::video_rotation() {
1020 return VIDEO_ROTATION_0
;
1023 TextTrackConfig
ChunkDemuxerStream::text_track_config() {
1024 CHECK_EQ(type_
, TEXT
);
1025 base::AutoLock
auto_lock(lock_
);
1026 return stream_
->GetCurrentTextTrackConfig();
1029 void ChunkDemuxerStream::SetLiveness(Liveness liveness
) {
1030 base::AutoLock
auto_lock(lock_
);
1031 liveness_
= liveness
;
1034 void ChunkDemuxerStream::ChangeState_Locked(State state
) {
1035 lock_
.AssertAcquired();
1036 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1038 << " - " << state_
<< " -> " << state
;
1042 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1044 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1045 lock_
.AssertAcquired();
1046 DCHECK(!read_cb_
.is_null());
1048 DemuxerStream::Status status
;
1049 scoped_refptr
<StreamParserBuffer
> buffer
;
1055 case RETURNING_DATA_FOR_READS
:
1056 switch (stream_
->GetNextBuffer(&buffer
)) {
1057 case SourceBufferStream::kSuccess
:
1058 status
= DemuxerStream::kOk
;
1059 DVLOG(2) << __FUNCTION__
<< ": returning kOk, type " << type_
1060 << ", dts " << buffer
->GetDecodeTimestamp().InSecondsF()
1061 << ", pts " << buffer
->timestamp().InSecondsF()
1062 << ", dur " << buffer
->duration().InSecondsF()
1063 << ", key " << buffer
->is_key_frame();
1065 case SourceBufferStream::kNeedBuffer
:
1066 // Return early without calling |read_cb_| since we don't have
1067 // any data to return yet.
1068 DVLOG(2) << __FUNCTION__
<< ": returning kNeedBuffer, type "
1071 case SourceBufferStream::kEndOfStream
:
1072 status
= DemuxerStream::kOk
;
1073 buffer
= StreamParserBuffer::CreateEOSBuffer();
1074 DVLOG(2) << __FUNCTION__
<< ": returning kOk with EOS buffer, type "
1077 case SourceBufferStream::kConfigChange
:
1078 status
= kConfigChanged
;
1080 DVLOG(2) << __FUNCTION__
<< ": returning kConfigChange, type "
1085 case RETURNING_ABORT_FOR_READS
:
1086 // Null buffers should be returned in this state since we are waiting
1087 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1088 // because they are associated with the seek.
1089 status
= DemuxerStream::kAborted
;
1091 DVLOG(2) << __FUNCTION__
<< ": returning kAborted, type " << type_
;
1094 status
= DemuxerStream::kOk
;
1095 buffer
= StreamParserBuffer::CreateEOSBuffer();
1096 DVLOG(2) << __FUNCTION__
<< ": returning kOk with EOS buffer, type "
1101 base::ResetAndReturn(&read_cb_
).Run(status
, buffer
);
1104 ChunkDemuxer::ChunkDemuxer(
1105 const base::Closure
& open_cb
,
1106 const EncryptedMediaInitDataCB
& encrypted_media_init_data_cb
,
1107 const LogCB
& log_cb
,
1108 const scoped_refptr
<MediaLog
>& media_log
,
1109 bool splice_frames_enabled
)
1110 : state_(WAITING_FOR_INIT
),
1111 cancel_next_seek_(false),
1114 encrypted_media_init_data_cb_(encrypted_media_init_data_cb
),
1115 enable_text_(false),
1117 media_log_(media_log
),
1118 duration_(kNoTimestamp()),
1119 user_specified_duration_(-1),
1120 liveness_(DemuxerStream::LIVENESS_UNKNOWN
),
1121 splice_frames_enabled_(splice_frames_enabled
) {
1122 DCHECK(!open_cb_
.is_null());
1123 DCHECK(!encrypted_media_init_data_cb_
.is_null());
1126 void ChunkDemuxer::Initialize(
1128 const PipelineStatusCB
& cb
,
1129 bool enable_text_tracks
) {
1130 DVLOG(1) << "Init()";
1132 base::AutoLock
auto_lock(lock_
);
1134 // The |init_cb_| must only be run after this method returns, so always post.
1135 init_cb_
= BindToCurrentLoop(cb
);
1136 if (state_
== SHUTDOWN
) {
1137 base::ResetAndReturn(&init_cb_
).Run(DEMUXER_ERROR_COULD_NOT_OPEN
);
1140 DCHECK_EQ(state_
, WAITING_FOR_INIT
);
1142 enable_text_
= enable_text_tracks
;
1144 ChangeState_Locked(INITIALIZING
);
1146 base::ResetAndReturn(&open_cb_
).Run();
1149 void ChunkDemuxer::Stop() {
1150 DVLOG(1) << "Stop()";
1154 void ChunkDemuxer::Seek(TimeDelta time
, const PipelineStatusCB
& cb
) {
1155 DVLOG(1) << "Seek(" << time
.InSecondsF() << ")";
1156 DCHECK(time
>= TimeDelta());
1158 base::AutoLock
auto_lock(lock_
);
1159 DCHECK(seek_cb_
.is_null());
1161 seek_cb_
= BindToCurrentLoop(cb
);
1162 if (state_
!= INITIALIZED
&& state_
!= ENDED
) {
1163 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_INVALID_STATE
);
1167 if (cancel_next_seek_
) {
1168 cancel_next_seek_
= false;
1169 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1173 SeekAllSources(time
);
1174 StartReturningData();
1176 if (IsSeekWaitingForData_Locked()) {
1177 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1181 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1184 // Demuxer implementation.
1185 base::Time
ChunkDemuxer::GetTimelineOffset() const {
1186 return timeline_offset_
;
1189 DemuxerStream
* ChunkDemuxer::GetStream(DemuxerStream::Type type
) {
1190 DCHECK_NE(type
, DemuxerStream::TEXT
);
1191 base::AutoLock
auto_lock(lock_
);
1192 if (type
== DemuxerStream::VIDEO
)
1193 return video_
.get();
1195 if (type
== DemuxerStream::AUDIO
)
1196 return audio_
.get();
1201 TimeDelta
ChunkDemuxer::GetStartTime() const {
1205 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time
) {
1206 DVLOG(1) << "StartWaitingForSeek()";
1207 base::AutoLock
auto_lock(lock_
);
1208 DCHECK(state_
== INITIALIZED
|| state_
== ENDED
|| state_
== SHUTDOWN
||
1209 state_
== PARSE_ERROR
) << state_
;
1210 DCHECK(seek_cb_
.is_null());
1212 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1215 AbortPendingReads();
1216 SeekAllSources(seek_time
);
1218 // Cancel state set in CancelPendingSeek() since we want to
1219 // accept the next Seek().
1220 cancel_next_seek_
= false;
1223 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time
) {
1224 base::AutoLock
auto_lock(lock_
);
1225 DCHECK_NE(state_
, INITIALIZING
);
1226 DCHECK(seek_cb_
.is_null() || IsSeekWaitingForData_Locked());
1228 if (cancel_next_seek_
)
1231 AbortPendingReads();
1232 SeekAllSources(seek_time
);
1234 if (seek_cb_
.is_null()) {
1235 cancel_next_seek_
= true;
1239 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1242 ChunkDemuxer::Status
ChunkDemuxer::AddId(const std::string
& id
,
1243 const std::string
& type
,
1244 std::vector
<std::string
>& codecs
) {
1245 base::AutoLock
auto_lock(lock_
);
1247 if ((state_
!= WAITING_FOR_INIT
&& state_
!= INITIALIZING
) || IsValidId(id
))
1248 return kReachedIdLimit
;
1250 bool has_audio
= false;
1251 bool has_video
= false;
1252 scoped_ptr
<media::StreamParser
> stream_parser(
1253 StreamParserFactory::Create(type
, codecs
, log_cb_
,
1254 &has_audio
, &has_video
));
1257 return ChunkDemuxer::kNotSupported
;
1259 if ((has_audio
&& !source_id_audio_
.empty()) ||
1260 (has_video
&& !source_id_video_
.empty()))
1261 return kReachedIdLimit
;
1264 source_id_audio_
= id
;
1267 source_id_video_
= id
;
1269 scoped_ptr
<FrameProcessor
> frame_processor(
1270 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary
,
1271 base::Unretained(this))));
1273 scoped_ptr
<SourceState
> source_state(
1274 new SourceState(stream_parser
.Pass(),
1275 frame_processor
.Pass(), log_cb_
,
1276 base::Bind(&ChunkDemuxer::CreateDemuxerStream
,
1277 base::Unretained(this)),
1280 SourceState::NewTextTrackCB new_text_track_cb
;
1283 new_text_track_cb
= base::Bind(&ChunkDemuxer::OnNewTextTrack
,
1284 base::Unretained(this));
1288 base::Bind(&ChunkDemuxer::OnSourceInitDone
, base::Unretained(this)),
1289 has_audio
, has_video
, encrypted_media_init_data_cb_
, new_text_track_cb
);
1291 source_state_map_
[id
] = source_state
.release();
1295 void ChunkDemuxer::RemoveId(const std::string
& id
) {
1296 base::AutoLock
auto_lock(lock_
);
1297 CHECK(IsValidId(id
));
1299 delete source_state_map_
[id
];
1300 source_state_map_
.erase(id
);
1302 if (source_id_audio_
== id
)
1303 source_id_audio_
.clear();
1305 if (source_id_video_
== id
)
1306 source_id_video_
.clear();
1309 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges(const std::string
& id
) const {
1310 base::AutoLock
auto_lock(lock_
);
1311 DCHECK(!id
.empty());
1313 SourceStateMap::const_iterator itr
= source_state_map_
.find(id
);
1315 DCHECK(itr
!= source_state_map_
.end());
1316 return itr
->second
->GetBufferedRanges(duration_
, state_
== ENDED
);
1319 void ChunkDemuxer::AppendData(
1320 const std::string
& id
,
1323 TimeDelta append_window_start
,
1324 TimeDelta append_window_end
,
1325 TimeDelta
* timestamp_offset
,
1326 const InitSegmentReceivedCB
& init_segment_received_cb
) {
1327 DVLOG(1) << "AppendData(" << id
<< ", " << length
<< ")";
1329 DCHECK(!id
.empty());
1330 DCHECK(timestamp_offset
);
1331 DCHECK(!init_segment_received_cb
.is_null());
1333 Ranges
<TimeDelta
> ranges
;
1336 base::AutoLock
auto_lock(lock_
);
1337 DCHECK_NE(state_
, ENDED
);
1339 // Capture if any of the SourceBuffers are waiting for data before we start
1341 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1351 DCHECK(IsValidId(id
));
1352 if (!source_state_map_
[id
]->Append(data
, length
,
1353 append_window_start
,
1356 init_segment_received_cb
)) {
1357 ReportError_Locked(PIPELINE_ERROR_DECODE
);
1363 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1366 case WAITING_FOR_INIT
:
1369 DVLOG(1) << "AppendData(): called in unexpected state " << state_
;
1373 // Check to see if data was appended at the pending seek point. This
1374 // indicates we have parsed enough data to complete the seek.
1375 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1376 !seek_cb_
.is_null()) {
1377 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1380 ranges
= GetBufferedRanges_Locked();
1383 for (size_t i
= 0; i
< ranges
.size(); ++i
)
1384 host_
->AddBufferedTimeRange(ranges
.start(i
), ranges
.end(i
));
1387 void ChunkDemuxer::Abort(const std::string
& id
,
1388 TimeDelta append_window_start
,
1389 TimeDelta append_window_end
,
1390 TimeDelta
* timestamp_offset
) {
1391 DVLOG(1) << "Abort(" << id
<< ")";
1392 base::AutoLock
auto_lock(lock_
);
1393 DCHECK(!id
.empty());
1394 CHECK(IsValidId(id
));
1395 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1396 source_state_map_
[id
]->Abort(append_window_start
,
1399 // Abort can possibly emit some buffers.
1400 // Need to check whether seeking can be completed.
1401 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1402 !seek_cb_
.is_null()) {
1403 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1407 void ChunkDemuxer::Remove(const std::string
& id
, TimeDelta start
,
1409 DVLOG(1) << "Remove(" << id
<< ", " << start
.InSecondsF()
1410 << ", " << end
.InSecondsF() << ")";
1411 base::AutoLock
auto_lock(lock_
);
1413 DCHECK(!id
.empty());
1414 CHECK(IsValidId(id
));
1415 DCHECK(start
>= base::TimeDelta()) << start
.InSecondsF();
1416 DCHECK(start
< end
) << "start " << start
.InSecondsF()
1417 << " end " << end
.InSecondsF();
1418 DCHECK(duration_
!= kNoTimestamp());
1419 DCHECK(start
<= duration_
) << "start " << start
.InSecondsF()
1420 << " duration " << duration_
.InSecondsF();
1422 if (start
== duration_
)
1425 source_state_map_
[id
]->Remove(start
, end
, duration_
);
1428 double ChunkDemuxer::GetDuration() {
1429 base::AutoLock
auto_lock(lock_
);
1430 return GetDuration_Locked();
1433 double ChunkDemuxer::GetDuration_Locked() {
1434 lock_
.AssertAcquired();
1435 if (duration_
== kNoTimestamp())
1436 return std::numeric_limits
<double>::quiet_NaN();
1438 // Return positive infinity if the resource is unbounded.
1439 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1440 if (duration_
== kInfiniteDuration())
1441 return std::numeric_limits
<double>::infinity();
1443 if (user_specified_duration_
>= 0)
1444 return user_specified_duration_
;
1446 return duration_
.InSecondsF();
1449 void ChunkDemuxer::SetDuration(double duration
) {
1450 base::AutoLock
auto_lock(lock_
);
1451 DVLOG(1) << "SetDuration(" << duration
<< ")";
1452 DCHECK_GE(duration
, 0);
1454 if (duration
== GetDuration_Locked())
1457 // Compute & bounds check the TimeDelta representation of duration.
1458 // This can be different if the value of |duration| doesn't fit the range or
1459 // precision of TimeDelta.
1460 TimeDelta min_duration
= TimeDelta::FromInternalValue(1);
1461 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1462 TimeDelta max_duration
= TimeDelta::FromInternalValue(kint64max
- 1);
1463 double min_duration_in_seconds
= min_duration
.InSecondsF();
1464 double max_duration_in_seconds
= max_duration
.InSecondsF();
1466 TimeDelta duration_td
;
1467 if (duration
== std::numeric_limits
<double>::infinity()) {
1468 duration_td
= media::kInfiniteDuration();
1469 } else if (duration
< min_duration_in_seconds
) {
1470 duration_td
= min_duration
;
1471 } else if (duration
> max_duration_in_seconds
) {
1472 duration_td
= max_duration
;
1474 duration_td
= TimeDelta::FromMicroseconds(
1475 duration
* base::Time::kMicrosecondsPerSecond
);
1478 DCHECK(duration_td
> TimeDelta());
1480 user_specified_duration_
= duration
;
1481 duration_
= duration_td
;
1482 host_
->SetDuration(duration_
);
1484 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1485 itr
!= source_state_map_
.end(); ++itr
) {
1486 itr
->second
->OnSetDuration(duration_
);
1490 bool ChunkDemuxer::IsParsingMediaSegment(const std::string
& id
) {
1491 base::AutoLock
auto_lock(lock_
);
1492 DVLOG(1) << "IsParsingMediaSegment(" << id
<< ")";
1493 CHECK(IsValidId(id
));
1495 return source_state_map_
[id
]->parsing_media_segment();
1498 void ChunkDemuxer::SetSequenceMode(const std::string
& id
,
1499 bool sequence_mode
) {
1500 base::AutoLock
auto_lock(lock_
);
1501 DVLOG(1) << "SetSequenceMode(" << id
<< ", " << sequence_mode
<< ")";
1502 CHECK(IsValidId(id
));
1503 DCHECK_NE(state_
, ENDED
);
1505 source_state_map_
[id
]->SetSequenceMode(sequence_mode
);
1508 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1509 const std::string
& id
,
1510 base::TimeDelta timestamp_offset
) {
1511 base::AutoLock
auto_lock(lock_
);
1512 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id
<< ", "
1513 << timestamp_offset
.InSecondsF() << ")";
1514 CHECK(IsValidId(id
));
1515 DCHECK_NE(state_
, ENDED
);
1517 source_state_map_
[id
]->SetGroupStartTimestampIfInSequenceMode(
1522 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status
) {
1523 DVLOG(1) << "MarkEndOfStream(" << status
<< ")";
1524 base::AutoLock
auto_lock(lock_
);
1525 DCHECK_NE(state_
, WAITING_FOR_INIT
);
1526 DCHECK_NE(state_
, ENDED
);
1528 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1531 if (state_
== INITIALIZING
) {
1532 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1536 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1537 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1538 itr
!= source_state_map_
.end(); ++itr
) {
1539 itr
->second
->MarkEndOfStream();
1542 CompletePendingReadsIfPossible();
1544 // Give a chance to resume the pending seek process.
1545 if (status
!= PIPELINE_OK
) {
1546 ReportError_Locked(status
);
1550 ChangeState_Locked(ENDED
);
1551 DecreaseDurationIfNecessary();
1553 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1554 !seek_cb_
.is_null()) {
1555 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1559 void ChunkDemuxer::UnmarkEndOfStream() {
1560 DVLOG(1) << "UnmarkEndOfStream()";
1561 base::AutoLock
auto_lock(lock_
);
1562 DCHECK_EQ(state_
, ENDED
);
1564 ChangeState_Locked(INITIALIZED
);
1566 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1567 itr
!= source_state_map_
.end(); ++itr
) {
1568 itr
->second
->UnmarkEndOfStream();
1572 void ChunkDemuxer::Shutdown() {
1573 DVLOG(1) << "Shutdown()";
1574 base::AutoLock
auto_lock(lock_
);
1576 if (state_
== SHUTDOWN
)
1579 ShutdownAllStreams();
1581 ChangeState_Locked(SHUTDOWN
);
1583 if(!seek_cb_
.is_null())
1584 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_ABORT
);
1587 void ChunkDemuxer::SetMemoryLimits(DemuxerStream::Type type
, int memory_limit
) {
1588 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1589 itr
!= source_state_map_
.end(); ++itr
) {
1590 itr
->second
->SetMemoryLimits(type
, memory_limit
);
1594 void ChunkDemuxer::ChangeState_Locked(State new_state
) {
1595 lock_
.AssertAcquired();
1596 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1597 << state_
<< " -> " << new_state
;
1601 ChunkDemuxer::~ChunkDemuxer() {
1602 DCHECK_NE(state_
, INITIALIZED
);
1604 STLDeleteValues(&source_state_map_
);
1607 void ChunkDemuxer::ReportError_Locked(PipelineStatus error
) {
1608 DVLOG(1) << "ReportError_Locked(" << error
<< ")";
1609 lock_
.AssertAcquired();
1610 DCHECK_NE(error
, PIPELINE_OK
);
1612 ChangeState_Locked(PARSE_ERROR
);
1614 PipelineStatusCB cb
;
1616 if (!init_cb_
.is_null()) {
1617 std::swap(cb
, init_cb_
);
1619 if (!seek_cb_
.is_null())
1620 std::swap(cb
, seek_cb_
);
1622 ShutdownAllStreams();
1625 if (!cb
.is_null()) {
1630 base::AutoUnlock
auto_unlock(lock_
);
1631 host_
->OnDemuxerError(error
);
1634 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1635 lock_
.AssertAcquired();
1636 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1637 itr
!= source_state_map_
.end(); ++itr
) {
1638 if (itr
->second
->IsSeekWaitingForData())
1645 void ChunkDemuxer::OnSourceInitDone(
1646 const StreamParser::InitParameters
& params
) {
1647 DVLOG(1) << "OnSourceInitDone(" << params
.duration
.InSecondsF() << ")";
1648 lock_
.AssertAcquired();
1649 DCHECK_EQ(state_
, INITIALIZING
);
1650 if (!audio_
&& !video_
) {
1651 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1655 if (params
.duration
!= TimeDelta() && duration_
== kNoTimestamp())
1656 UpdateDuration(params
.duration
);
1658 if (!params
.timeline_offset
.is_null()) {
1659 if (!timeline_offset_
.is_null() &&
1660 params
.timeline_offset
!= timeline_offset_
) {
1661 MEDIA_LOG(ERROR
, log_cb_
)
1662 << "Timeline offset is not the same across all SourceBuffers.";
1663 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1667 timeline_offset_
= params
.timeline_offset
;
1670 if (params
.liveness
!= DemuxerStream::LIVENESS_UNKNOWN
) {
1672 audio_
->SetLiveness(params
.liveness
);
1674 video_
->SetLiveness(params
.liveness
);
1677 // Wait until all streams have initialized.
1678 if ((!source_id_audio_
.empty() && !audio_
) ||
1679 (!source_id_video_
.empty() && !video_
)) {
1683 SeekAllSources(GetStartTime());
1684 StartReturningData();
1686 if (duration_
== kNoTimestamp())
1687 duration_
= kInfiniteDuration();
1689 // The demuxer is now initialized after the |start_timestamp_| was set.
1690 ChangeState_Locked(INITIALIZED
);
1691 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
1695 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type
) {
1697 case DemuxerStream::AUDIO
:
1701 new ChunkDemuxerStream(DemuxerStream::AUDIO
, splice_frames_enabled_
));
1702 return audio_
.get();
1704 case DemuxerStream::VIDEO
:
1708 new ChunkDemuxerStream(DemuxerStream::VIDEO
, splice_frames_enabled_
));
1709 return video_
.get();
1711 case DemuxerStream::TEXT
: {
1712 return new ChunkDemuxerStream(DemuxerStream::TEXT
,
1713 splice_frames_enabled_
);
1716 case DemuxerStream::UNKNOWN
:
1717 case DemuxerStream::NUM_TYPES
:
1725 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
1726 const TextTrackConfig
& config
) {
1727 lock_
.AssertAcquired();
1728 DCHECK_NE(state_
, SHUTDOWN
);
1729 host_
->AddTextStream(text_stream
, config
);
1732 bool ChunkDemuxer::IsValidId(const std::string
& source_id
) const {
1733 lock_
.AssertAcquired();
1734 return source_state_map_
.count(source_id
) > 0u;
1737 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration
) {
1738 DCHECK(duration_
!= new_duration
);
1739 user_specified_duration_
= -1;
1740 duration_
= new_duration
;
1741 host_
->SetDuration(new_duration
);
1744 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration
) {
1745 DCHECK(new_duration
!= kNoTimestamp());
1746 DCHECK(new_duration
!= kInfiniteDuration());
1748 // Per April 1, 2014 MSE spec editor's draft:
1749 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1750 // media-source.html#sourcebuffer-coded-frame-processing
1751 // 5. If the media segment contains data beyond the current duration, then run
1752 // the duration change algorithm with new duration set to the maximum of
1753 // the current duration and the group end timestamp.
1755 if (new_duration
<= duration_
)
1758 DVLOG(2) << __FUNCTION__
<< ": Increasing duration: "
1759 << duration_
.InSecondsF() << " -> " << new_duration
.InSecondsF();
1761 UpdateDuration(new_duration
);
1764 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1765 lock_
.AssertAcquired();
1767 TimeDelta max_duration
;
1769 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1770 itr
!= source_state_map_
.end(); ++itr
) {
1771 max_duration
= std::max(max_duration
,
1772 itr
->second
->GetMaxBufferedDuration());
1775 if (max_duration
== TimeDelta())
1778 if (max_duration
< duration_
)
1779 UpdateDuration(max_duration
);
1782 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges() const {
1783 base::AutoLock
auto_lock(lock_
);
1784 return GetBufferedRanges_Locked();
1787 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges_Locked() const {
1788 lock_
.AssertAcquired();
1790 bool ended
= state_
== ENDED
;
1791 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1792 // we'll need to update this loop to only add ranges from active sources.
1793 RangesList ranges_list
;
1794 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1795 itr
!= source_state_map_
.end(); ++itr
) {
1796 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration_
, ended
));
1799 return ComputeIntersection(ranges_list
, ended
);
1802 void ChunkDemuxer::StartReturningData() {
1803 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1804 itr
!= source_state_map_
.end(); ++itr
) {
1805 itr
->second
->StartReturningData();
1809 void ChunkDemuxer::AbortPendingReads() {
1810 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1811 itr
!= source_state_map_
.end(); ++itr
) {
1812 itr
->second
->AbortReads();
1816 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time
) {
1817 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1818 itr
!= source_state_map_
.end(); ++itr
) {
1819 itr
->second
->Seek(seek_time
);
1823 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1824 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1825 itr
!= source_state_map_
.end(); ++itr
) {
1826 itr
->second
->CompletePendingReadIfPossible();
1830 void ChunkDemuxer::ShutdownAllStreams() {
1831 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1832 itr
!= source_state_map_
.end(); ++itr
) {
1833 itr
->second
->Shutdown();
1837 } // namespace media