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 base::Callback
<void(
94 ChunkDemuxerStream
*, const TextTrackConfig
&)> NewTextTrackCB
;
97 scoped_ptr
<StreamParser
> stream_parser
,
98 scoped_ptr
<FrameProcessor
> frame_processor
, const LogCB
& log_cb
,
99 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
);
103 void Init(const StreamParser::InitCB
& init_cb
,
106 const StreamParser::NeedKeyCB
& need_key_cb
,
107 const NewTextTrackCB
& new_text_track_cb
);
109 // Appends new data to the StreamParser.
110 // Returns true if the data was successfully appended. Returns false if an
111 // error occurred. |*timestamp_offset| is used and possibly updated by the
112 // append. |append_window_start| and |append_window_end| correspond to the MSE
113 // spec's similarly named source buffer attributes that are used in coded
115 bool Append(const uint8
* data
, size_t length
,
116 TimeDelta append_window_start
,
117 TimeDelta append_window_end
,
118 TimeDelta
* timestamp_offset
);
120 // Aborts the current append sequence and resets the parser.
121 void Abort(TimeDelta append_window_start
,
122 TimeDelta append_window_end
,
123 TimeDelta
* timestamp_offset
);
125 // Calls Remove(|start|, |end|, |duration|) on all
126 // ChunkDemuxerStreams managed by this object.
127 void Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
);
129 // Returns true if currently parsing a media segment, or false otherwise.
130 bool parsing_media_segment() const { return parsing_media_segment_
; }
132 // Sets |frame_processor_|'s sequence mode to |sequence_mode|.
133 void SetSequenceMode(bool sequence_mode
);
135 // Signals the coded frame processor to update its group start timestamp to be
136 // |timestamp_offset| if it is in sequence append mode.
137 void SetGroupStartTimestampIfInSequenceMode(base::TimeDelta timestamp_offset
);
139 // Returns the range of buffered data in this source, capped at |duration|.
140 // |ended| - Set to true if end of stream has been signaled and the special
141 // end of stream range logic needs to be executed.
142 Ranges
<TimeDelta
> GetBufferedRanges(TimeDelta duration
, bool ended
) const;
144 // Returns the highest buffered duration across all streams managed
146 // Returns TimeDelta() if none of the streams contain buffered data.
147 TimeDelta
GetMaxBufferedDuration() const;
149 // Helper methods that call methods with similar names on all the
150 // ChunkDemuxerStreams managed by this object.
151 void StartReturningData();
153 void Seek(TimeDelta seek_time
);
154 void CompletePendingReadIfPossible();
155 void OnSetDuration(TimeDelta duration
);
156 void MarkEndOfStream();
157 void UnmarkEndOfStream();
159 // Sets the memory limit on each stream. |memory_limit| is the
160 // maximum number of bytes each stream is allowed to hold in its buffer.
161 void SetMemoryLimitsForTesting(int memory_limit
);
162 bool IsSeekWaitingForData() const;
165 // Called by the |stream_parser_| when a new initialization segment is
167 // Returns true on a successful call. Returns false if an error occurred while
168 // processing decoder configurations.
169 bool OnNewConfigs(bool allow_audio
, bool allow_video
,
170 const AudioDecoderConfig
& audio_config
,
171 const VideoDecoderConfig
& video_config
,
172 const StreamParser::TextTrackConfigMap
& text_configs
);
174 // Called by the |stream_parser_| at the beginning of a new media segment.
175 void OnNewMediaSegment();
177 // Called by the |stream_parser_| at the end of a media segment.
178 void OnEndOfMediaSegment();
180 // Called by the |stream_parser_| when new buffers have been parsed.
181 // It processes the new buffers using |frame_processor_|, which includes
182 // appending the processed frames to associated demuxer streams for each
184 // Returns true on a successful call. Returns false if an error occurred while
185 // processing the buffers.
186 bool OnNewBuffers(const StreamParser::BufferQueue
& audio_buffers
,
187 const StreamParser::BufferQueue
& video_buffers
,
188 const StreamParser::TextBufferQueueMap
& text_map
);
190 void OnSourceInitDone(bool success
,
191 const StreamParser::InitParameters
& params
);
193 CreateDemuxerStreamCB create_demuxer_stream_cb_
;
194 NewTextTrackCB new_text_track_cb_
;
196 // During Append(), if OnNewBuffers() coded frame processing updates the
197 // timestamp offset then |*timestamp_offset_during_append_| is also updated
198 // so Append()'s caller can know the new offset. This pointer is only non-NULL
199 // during the lifetime of an Append() call.
200 TimeDelta
* timestamp_offset_during_append_
;
202 // During Append(), coded frame processing triggered by OnNewBuffers()
203 // requires these two attributes. These are only valid during the lifetime of
205 TimeDelta append_window_start_during_append_
;
206 TimeDelta append_window_end_during_append_
;
208 // Set to true if the next buffers appended within the append window
209 // represent the start of a new media segment. This flag being set
210 // triggers a call to |new_segment_cb_| when the new buffers are
211 // appended. The flag is set on actual media segment boundaries and
212 // when the "append window" filtering causes discontinuities in the
214 // TODO(wolenetz/acolwell): Investigate if we need this, or if coded frame
215 // processing's discontinuity logic is enough. See http://crbug.com/351489.
216 bool new_media_segment_
;
218 // Keeps track of whether a media segment is being parsed.
219 bool parsing_media_segment_
;
221 // The object used to parse appended data.
222 scoped_ptr
<StreamParser
> stream_parser_
;
224 ChunkDemuxerStream
* audio_
; // Not owned by |this|.
225 ChunkDemuxerStream
* video_
; // Not owned by |this|.
227 typedef std::map
<StreamParser::TrackId
, ChunkDemuxerStream
*> TextStreamMap
;
228 TextStreamMap text_stream_map_
; // |this| owns the map's stream pointers.
230 scoped_ptr
<FrameProcessor
> frame_processor_
;
232 StreamParser::InitCB init_cb_
;
234 // Indicates that timestampOffset should be updated automatically during
235 // OnNewBuffers() based on the earliest end timestamp of the buffers provided.
236 // TODO(wolenetz): Refactor this function while integrating April 29, 2014
237 // changes to MSE spec. See http://crbug.com/371499.
238 bool auto_update_timestamp_offset_
;
240 DISALLOW_COPY_AND_ASSIGN(SourceState
);
243 SourceState::SourceState(scoped_ptr
<StreamParser
> stream_parser
,
244 scoped_ptr
<FrameProcessor
> frame_processor
,
246 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
)
247 : create_demuxer_stream_cb_(create_demuxer_stream_cb
),
248 timestamp_offset_during_append_(NULL
),
249 new_media_segment_(false),
250 parsing_media_segment_(false),
251 stream_parser_(stream_parser
.release()),
254 frame_processor_(frame_processor
.release()),
256 auto_update_timestamp_offset_(false) {
257 DCHECK(!create_demuxer_stream_cb_
.is_null());
258 DCHECK(frame_processor_
);
261 SourceState::~SourceState() {
264 STLDeleteValues(&text_stream_map_
);
267 void SourceState::Init(const StreamParser::InitCB
& init_cb
,
270 const StreamParser::NeedKeyCB
& need_key_cb
,
271 const NewTextTrackCB
& new_text_track_cb
) {
272 new_text_track_cb_
= new_text_track_cb
;
275 stream_parser_
->Init(
276 base::Bind(&SourceState::OnSourceInitDone
, base::Unretained(this)),
277 base::Bind(&SourceState::OnNewConfigs
,
278 base::Unretained(this),
281 base::Bind(&SourceState::OnNewBuffers
, base::Unretained(this)),
282 new_text_track_cb_
.is_null(),
284 base::Bind(&SourceState::OnNewMediaSegment
, base::Unretained(this)),
285 base::Bind(&SourceState::OnEndOfMediaSegment
, base::Unretained(this)),
289 void SourceState::SetSequenceMode(bool sequence_mode
) {
290 DCHECK(!parsing_media_segment_
);
292 frame_processor_
->SetSequenceMode(sequence_mode
);
295 void SourceState::SetGroupStartTimestampIfInSequenceMode(
296 base::TimeDelta timestamp_offset
) {
297 DCHECK(!parsing_media_segment_
);
299 frame_processor_
->SetGroupStartTimestampIfInSequenceMode(timestamp_offset
);
302 bool SourceState::Append(const uint8
* data
, size_t length
,
303 TimeDelta append_window_start
,
304 TimeDelta append_window_end
,
305 TimeDelta
* timestamp_offset
) {
306 DCHECK(timestamp_offset
);
307 DCHECK(!timestamp_offset_during_append_
);
308 append_window_start_during_append_
= append_window_start
;
309 append_window_end_during_append_
= append_window_end
;
310 timestamp_offset_during_append_
= timestamp_offset
;
312 // TODO(wolenetz/acolwell): Curry and pass a NewBuffersCB here bound with
313 // append window and timestamp offset pointer. See http://crbug.com/351454.
314 bool err
= stream_parser_
->Parse(data
, length
);
315 timestamp_offset_during_append_
= NULL
;
319 void SourceState::Abort(TimeDelta append_window_start
,
320 TimeDelta append_window_end
,
321 base::TimeDelta
* timestamp_offset
) {
322 DCHECK(timestamp_offset
);
323 DCHECK(!timestamp_offset_during_append_
);
324 timestamp_offset_during_append_
= timestamp_offset
;
325 append_window_start_during_append_
= append_window_start
;
326 append_window_end_during_append_
= append_window_end
;
328 stream_parser_
->Flush();
329 timestamp_offset_during_append_
= NULL
;
331 frame_processor_
->Reset();
332 parsing_media_segment_
= false;
335 void SourceState::Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
) {
337 audio_
->Remove(start
, end
, duration
);
340 video_
->Remove(start
, end
, duration
);
342 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
343 itr
!= text_stream_map_
.end(); ++itr
) {
344 itr
->second
->Remove(start
, end
, duration
);
348 Ranges
<TimeDelta
> SourceState::GetBufferedRanges(TimeDelta duration
,
350 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
351 // this code to only add ranges from active tracks.
352 RangesList ranges_list
;
354 ranges_list
.push_back(audio_
->GetBufferedRanges(duration
));
357 ranges_list
.push_back(video_
->GetBufferedRanges(duration
));
359 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
360 itr
!= text_stream_map_
.end(); ++itr
) {
361 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration
));
364 return ComputeIntersection(ranges_list
, ended
);
367 TimeDelta
SourceState::GetMaxBufferedDuration() const {
368 TimeDelta max_duration
;
371 max_duration
= std::max(max_duration
, audio_
->GetBufferedDuration());
374 max_duration
= std::max(max_duration
, video_
->GetBufferedDuration());
376 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
377 itr
!= text_stream_map_
.end(); ++itr
) {
378 max_duration
= std::max(max_duration
, itr
->second
->GetBufferedDuration());
384 void SourceState::StartReturningData() {
386 audio_
->StartReturningData();
389 video_
->StartReturningData();
391 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
392 itr
!= text_stream_map_
.end(); ++itr
) {
393 itr
->second
->StartReturningData();
397 void SourceState::AbortReads() {
399 audio_
->AbortReads();
402 video_
->AbortReads();
404 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
405 itr
!= text_stream_map_
.end(); ++itr
) {
406 itr
->second
->AbortReads();
410 void SourceState::Seek(TimeDelta seek_time
) {
412 audio_
->Seek(seek_time
);
415 video_
->Seek(seek_time
);
417 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
418 itr
!= text_stream_map_
.end(); ++itr
) {
419 itr
->second
->Seek(seek_time
);
423 void SourceState::CompletePendingReadIfPossible() {
425 audio_
->CompletePendingReadIfPossible();
428 video_
->CompletePendingReadIfPossible();
430 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
431 itr
!= text_stream_map_
.end(); ++itr
) {
432 itr
->second
->CompletePendingReadIfPossible();
436 void SourceState::OnSetDuration(TimeDelta duration
) {
438 audio_
->OnSetDuration(duration
);
441 video_
->OnSetDuration(duration
);
443 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
444 itr
!= text_stream_map_
.end(); ++itr
) {
445 itr
->second
->OnSetDuration(duration
);
449 void SourceState::MarkEndOfStream() {
451 audio_
->MarkEndOfStream();
454 video_
->MarkEndOfStream();
456 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
457 itr
!= text_stream_map_
.end(); ++itr
) {
458 itr
->second
->MarkEndOfStream();
462 void SourceState::UnmarkEndOfStream() {
464 audio_
->UnmarkEndOfStream();
467 video_
->UnmarkEndOfStream();
469 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
470 itr
!= text_stream_map_
.end(); ++itr
) {
471 itr
->second
->UnmarkEndOfStream();
475 void SourceState::Shutdown() {
482 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
483 itr
!= text_stream_map_
.end(); ++itr
) {
484 itr
->second
->Shutdown();
488 void SourceState::SetMemoryLimitsForTesting(int memory_limit
) {
490 audio_
->set_memory_limit_for_testing(memory_limit
);
493 video_
->set_memory_limit_for_testing(memory_limit
);
495 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
496 itr
!= text_stream_map_
.end(); ++itr
) {
497 itr
->second
->set_memory_limit_for_testing(memory_limit
);
501 bool SourceState::IsSeekWaitingForData() const {
502 if (audio_
&& audio_
->IsSeekWaitingForData())
505 if (video_
&& video_
->IsSeekWaitingForData())
508 // NOTE: We are intentionally not checking the text tracks
509 // because text tracks are discontinuous and may not have data
510 // for the seek position. This is ok and playback should not be
511 // stalled because we don't have cues. If cues, with timestamps after
512 // the seek time, eventually arrive they will be delivered properly
513 // in response to ChunkDemuxerStream::Read() calls.
518 bool SourceState::OnNewConfigs(
519 bool allow_audio
, bool allow_video
,
520 const AudioDecoderConfig
& audio_config
,
521 const VideoDecoderConfig
& video_config
,
522 const StreamParser::TextTrackConfigMap
& text_configs
) {
523 DVLOG(1) << "OnNewConfigs(" << allow_audio
<< ", " << allow_video
524 << ", " << audio_config
.IsValidConfig()
525 << ", " << video_config
.IsValidConfig() << ")";
527 if (!audio_config
.IsValidConfig() && !video_config
.IsValidConfig()) {
528 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
532 // Signal an error if we get configuration info for stream types that weren't
533 // specified in AddId() or more configs after a stream is initialized.
534 if (allow_audio
!= audio_config
.IsValidConfig()) {
536 << "Initialization segment"
537 << (audio_config
.IsValidConfig() ? " has" : " does not have")
538 << " an audio track, but the mimetype"
539 << (allow_audio
? " specifies" : " does not specify")
540 << " an audio codec.";
544 if (allow_video
!= video_config
.IsValidConfig()) {
546 << "Initialization segment"
547 << (video_config
.IsValidConfig() ? " has" : " does not have")
548 << " a video track, but the mimetype"
549 << (allow_video
? " specifies" : " does not specify")
550 << " a video codec.";
555 if (audio_config
.IsValidConfig()) {
557 audio_
= create_demuxer_stream_cb_
.Run(DemuxerStream::AUDIO
);
560 DVLOG(1) << "Failed to create an audio stream.";
564 if (!frame_processor_
->AddTrack(FrameProcessorBase::kAudioTrackId
,
566 DVLOG(1) << "Failed to add audio track to frame processor.";
571 frame_processor_
->OnPossibleAudioConfigUpdate(audio_config
);
572 success
&= audio_
->UpdateAudioConfig(audio_config
, log_cb_
);
575 if (video_config
.IsValidConfig()) {
577 video_
= create_demuxer_stream_cb_
.Run(DemuxerStream::VIDEO
);
580 DVLOG(1) << "Failed to create a video stream.";
584 if (!frame_processor_
->AddTrack(FrameProcessorBase::kVideoTrackId
,
586 DVLOG(1) << "Failed to add video track to frame processor.";
591 success
&= video_
->UpdateVideoConfig(video_config
, log_cb_
);
594 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr
;
595 if (text_stream_map_
.empty()) {
596 for (TextConfigItr itr
= text_configs
.begin();
597 itr
!= text_configs
.end(); ++itr
) {
598 ChunkDemuxerStream
* const text_stream
=
599 create_demuxer_stream_cb_
.Run(DemuxerStream::TEXT
);
600 if (!frame_processor_
->AddTrack(itr
->first
, text_stream
)) {
602 MEDIA_LOG(log_cb_
) << "Failed to add text track ID " << itr
->first
603 << " to frame processor.";
606 text_stream
->UpdateTextConfig(itr
->second
, log_cb_
);
607 text_stream_map_
[itr
->first
] = text_stream
;
608 new_text_track_cb_
.Run(text_stream
, itr
->second
);
611 const size_t text_count
= text_stream_map_
.size();
612 if (text_configs
.size() != text_count
) {
614 MEDIA_LOG(log_cb_
) << "The number of text track configs changed.";
615 } else if (text_count
== 1) {
616 TextConfigItr config_itr
= text_configs
.begin();
617 const TextTrackConfig
& new_config
= config_itr
->second
;
618 TextStreamMap::iterator stream_itr
= text_stream_map_
.begin();
619 ChunkDemuxerStream
* text_stream
= stream_itr
->second
;
620 TextTrackConfig old_config
= text_stream
->text_track_config();
621 if (!new_config
.Matches(old_config
)) {
623 MEDIA_LOG(log_cb_
) << "New text track config does not match old one.";
625 StreamParser::TrackId old_id
= stream_itr
->first
;
626 StreamParser::TrackId new_id
= config_itr
->first
;
627 if (new_id
!= old_id
) {
628 if (frame_processor_
->UpdateTrack(old_id
, new_id
)) {
629 text_stream_map_
.clear();
630 text_stream_map_
[config_itr
->first
] = text_stream
;
633 MEDIA_LOG(log_cb_
) << "Error remapping single text track number";
638 for (TextConfigItr config_itr
= text_configs
.begin();
639 config_itr
!= text_configs
.end(); ++config_itr
) {
640 TextStreamMap::iterator stream_itr
=
641 text_stream_map_
.find(config_itr
->first
);
642 if (stream_itr
== text_stream_map_
.end()) {
644 MEDIA_LOG(log_cb_
) << "Unexpected text track configuration "
646 << config_itr
->first
;
650 const TextTrackConfig
& new_config
= config_itr
->second
;
651 ChunkDemuxerStream
* stream
= stream_itr
->second
;
652 TextTrackConfig old_config
= stream
->text_track_config();
653 if (!new_config
.Matches(old_config
)) {
655 MEDIA_LOG(log_cb_
) << "New text track config for track ID "
657 << " does not match old one.";
664 frame_processor_
->SetAllTrackBuffersNeedRandomAccessPoint();
666 DVLOG(1) << "OnNewConfigs() : " << (success
? "success" : "failed");
670 void SourceState::OnNewMediaSegment() {
671 DVLOG(2) << "OnNewMediaSegment()";
672 parsing_media_segment_
= true;
673 new_media_segment_
= true;
676 void SourceState::OnEndOfMediaSegment() {
677 DVLOG(2) << "OnEndOfMediaSegment()";
678 parsing_media_segment_
= false;
679 new_media_segment_
= false;
682 bool SourceState::OnNewBuffers(
683 const StreamParser::BufferQueue
& audio_buffers
,
684 const StreamParser::BufferQueue
& video_buffers
,
685 const StreamParser::TextBufferQueueMap
& text_map
) {
686 DVLOG(2) << "OnNewBuffers()";
687 DCHECK(timestamp_offset_during_append_
);
688 DCHECK(parsing_media_segment_
);
690 const TimeDelta timestamp_offset_before_processing
=
691 *timestamp_offset_during_append_
;
693 // Calculate the new timestamp offset for audio/video tracks if the stream
694 // parser has requested automatic updates.
695 TimeDelta new_timestamp_offset
= timestamp_offset_before_processing
;
696 if (auto_update_timestamp_offset_
) {
697 const bool have_audio_buffers
= !audio_buffers
.empty();
698 const bool have_video_buffers
= !video_buffers
.empty();
699 if (have_audio_buffers
&& have_video_buffers
) {
700 new_timestamp_offset
+=
701 std::min(EndTimestamp(audio_buffers
), EndTimestamp(video_buffers
));
702 } else if (have_audio_buffers
) {
703 new_timestamp_offset
+= EndTimestamp(audio_buffers
);
704 } else if (have_video_buffers
) {
705 new_timestamp_offset
+= EndTimestamp(video_buffers
);
709 if (!frame_processor_
->ProcessFrames(audio_buffers
,
712 append_window_start_during_append_
,
713 append_window_end_during_append_
,
715 timestamp_offset_during_append_
)) {
719 // Only update the timestamp offset if the frame processor hasn't already.
720 if (auto_update_timestamp_offset_
&&
721 timestamp_offset_before_processing
== *timestamp_offset_during_append_
) {
722 *timestamp_offset_during_append_
= new_timestamp_offset
;
728 void SourceState::OnSourceInitDone(bool success
,
729 const StreamParser::InitParameters
& params
) {
730 auto_update_timestamp_offset_
= params
.auto_update_timestamp_offset
;
731 base::ResetAndReturn(&init_cb_
).Run(success
, params
);
734 ChunkDemuxerStream::ChunkDemuxerStream(Type type
, bool splice_frames_enabled
)
736 state_(UNINITIALIZED
),
737 splice_frames_enabled_(splice_frames_enabled
),
738 partial_append_window_trimming_enabled_(false) {
741 void ChunkDemuxerStream::StartReturningData() {
742 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
743 base::AutoLock
auto_lock(lock_
);
744 DCHECK(read_cb_
.is_null());
745 ChangeState_Locked(RETURNING_DATA_FOR_READS
);
748 void ChunkDemuxerStream::AbortReads() {
749 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
750 base::AutoLock
auto_lock(lock_
);
751 ChangeState_Locked(RETURNING_ABORT_FOR_READS
);
752 if (!read_cb_
.is_null())
753 base::ResetAndReturn(&read_cb_
).Run(kAborted
, NULL
);
756 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
757 base::AutoLock
auto_lock(lock_
);
758 if (read_cb_
.is_null())
761 CompletePendingReadIfPossible_Locked();
764 void ChunkDemuxerStream::Shutdown() {
765 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
766 base::AutoLock
auto_lock(lock_
);
767 ChangeState_Locked(SHUTDOWN
);
769 // Pass an end of stream buffer to the pending callback to signal that no more
770 // data will be sent.
771 if (!read_cb_
.is_null()) {
772 base::ResetAndReturn(&read_cb_
).Run(DemuxerStream::kOk
,
773 StreamParserBuffer::CreateEOSBuffer());
777 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
778 base::AutoLock
auto_lock(lock_
);
780 // This method should not be called for text tracks. See the note in
781 // SourceState::IsSeekWaitingForData().
782 DCHECK_NE(type_
, DemuxerStream::TEXT
);
784 return stream_
->IsSeekPending();
787 void ChunkDemuxerStream::Seek(TimeDelta time
) {
788 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time
.InSecondsF() << ")";
789 base::AutoLock
auto_lock(lock_
);
790 DCHECK(read_cb_
.is_null());
791 DCHECK(state_
== UNINITIALIZED
|| state_
== RETURNING_ABORT_FOR_READS
)
797 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue
& buffers
) {
801 base::AutoLock
auto_lock(lock_
);
802 DCHECK_NE(state_
, SHUTDOWN
);
803 if (!stream_
->Append(buffers
)) {
804 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
808 if (!read_cb_
.is_null())
809 CompletePendingReadIfPossible_Locked();
814 void ChunkDemuxerStream::Remove(TimeDelta start
, TimeDelta end
,
815 TimeDelta duration
) {
816 base::AutoLock
auto_lock(lock_
);
817 stream_
->Remove(start
, end
, duration
);
820 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration
) {
821 base::AutoLock
auto_lock(lock_
);
822 stream_
->OnSetDuration(duration
);
825 Ranges
<TimeDelta
> ChunkDemuxerStream::GetBufferedRanges(
826 TimeDelta duration
) const {
827 base::AutoLock
auto_lock(lock_
);
830 // Since text tracks are discontinuous and the lack of cues should not block
831 // playback, report the buffered range for text tracks as [0, |duration|) so
832 // that intesections with audio & video tracks are computed correctly when
833 // no cues are present.
834 Ranges
<TimeDelta
> text_range
;
835 text_range
.Add(TimeDelta(), duration
);
839 Ranges
<TimeDelta
> range
= stream_
->GetBufferedTime();
841 if (range
.size() == 0u)
844 // Clamp the end of the stream's buffered ranges to fit within the duration.
845 // This can be done by intersecting the stream's range with the valid time
847 Ranges
<TimeDelta
> valid_time_range
;
848 valid_time_range
.Add(range
.start(0), duration
);
849 return range
.IntersectionWith(valid_time_range
);
852 TimeDelta
ChunkDemuxerStream::GetBufferedDuration() const {
853 return stream_
->GetBufferedDuration();
856 void ChunkDemuxerStream::OnNewMediaSegment(TimeDelta start_timestamp
) {
857 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
858 << start_timestamp
.InSecondsF() << ")";
859 base::AutoLock
auto_lock(lock_
);
860 stream_
->OnNewMediaSegment(start_timestamp
);
863 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig
& config
,
864 const LogCB
& log_cb
) {
865 DCHECK(config
.IsValidConfig());
866 DCHECK_EQ(type_
, AUDIO
);
867 base::AutoLock
auto_lock(lock_
);
869 DCHECK_EQ(state_
, UNINITIALIZED
);
871 // On platforms which support splice frames, enable splice frames and
872 // partial append window support for most codecs (notably: not opus).
873 const bool codec_supported
= config
.codec() == kCodecMP3
||
874 config
.codec() == kCodecAAC
||
875 config
.codec() == kCodecVorbis
;
876 splice_frames_enabled_
= splice_frames_enabled_
&& codec_supported
;
877 partial_append_window_trimming_enabled_
=
878 splice_frames_enabled_
&& codec_supported
;
881 new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
885 return stream_
->UpdateAudioConfig(config
);
888 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig
& config
,
889 const LogCB
& log_cb
) {
890 DCHECK(config
.IsValidConfig());
891 DCHECK_EQ(type_
, VIDEO
);
892 base::AutoLock
auto_lock(lock_
);
895 DCHECK_EQ(state_
, UNINITIALIZED
);
897 new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
901 return stream_
->UpdateVideoConfig(config
);
904 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig
& config
,
905 const LogCB
& log_cb
) {
906 DCHECK_EQ(type_
, TEXT
);
907 base::AutoLock
auto_lock(lock_
);
909 DCHECK_EQ(state_
, UNINITIALIZED
);
910 stream_
.reset(new SourceBufferStream(config
, log_cb
, splice_frames_enabled_
));
913 void ChunkDemuxerStream::MarkEndOfStream() {
914 base::AutoLock
auto_lock(lock_
);
915 stream_
->MarkEndOfStream();
918 void ChunkDemuxerStream::UnmarkEndOfStream() {
919 base::AutoLock
auto_lock(lock_
);
920 stream_
->UnmarkEndOfStream();
923 // DemuxerStream methods.
924 void ChunkDemuxerStream::Read(const ReadCB
& read_cb
) {
925 base::AutoLock
auto_lock(lock_
);
926 DCHECK_NE(state_
, UNINITIALIZED
);
927 DCHECK(read_cb_
.is_null());
929 read_cb_
= BindToCurrentLoop(read_cb
);
930 CompletePendingReadIfPossible_Locked();
933 DemuxerStream::Type
ChunkDemuxerStream::type() { return type_
; }
935 void ChunkDemuxerStream::EnableBitstreamConverter() {}
937 AudioDecoderConfig
ChunkDemuxerStream::audio_decoder_config() {
938 CHECK_EQ(type_
, AUDIO
);
939 base::AutoLock
auto_lock(lock_
);
940 return stream_
->GetCurrentAudioDecoderConfig();
943 VideoDecoderConfig
ChunkDemuxerStream::video_decoder_config() {
944 CHECK_EQ(type_
, VIDEO
);
945 base::AutoLock
auto_lock(lock_
);
946 return stream_
->GetCurrentVideoDecoderConfig();
949 bool ChunkDemuxerStream::SupportsConfigChanges() { return true; }
951 TextTrackConfig
ChunkDemuxerStream::text_track_config() {
952 CHECK_EQ(type_
, TEXT
);
953 base::AutoLock
auto_lock(lock_
);
954 return stream_
->GetCurrentTextTrackConfig();
957 void ChunkDemuxerStream::ChangeState_Locked(State state
) {
958 lock_
.AssertAcquired();
959 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
961 << " - " << state_
<< " -> " << state
;
965 ChunkDemuxerStream::~ChunkDemuxerStream() {}
967 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
968 lock_
.AssertAcquired();
969 DCHECK(!read_cb_
.is_null());
971 DemuxerStream::Status status
;
972 scoped_refptr
<StreamParserBuffer
> buffer
;
978 case RETURNING_DATA_FOR_READS
:
979 switch (stream_
->GetNextBuffer(&buffer
)) {
980 case SourceBufferStream::kSuccess
:
981 status
= DemuxerStream::kOk
;
983 case SourceBufferStream::kNeedBuffer
:
984 // Return early without calling |read_cb_| since we don't have
985 // any data to return yet.
987 case SourceBufferStream::kEndOfStream
:
988 status
= DemuxerStream::kOk
;
989 buffer
= StreamParserBuffer::CreateEOSBuffer();
991 case SourceBufferStream::kConfigChange
:
992 DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
993 status
= kConfigChanged
;
998 case RETURNING_ABORT_FOR_READS
:
999 // Null buffers should be returned in this state since we are waiting
1000 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1001 // because they are associated with the seek.
1002 status
= DemuxerStream::kAborted
;
1006 status
= DemuxerStream::kOk
;
1007 buffer
= StreamParserBuffer::CreateEOSBuffer();
1011 base::ResetAndReturn(&read_cb_
).Run(status
, buffer
);
1014 ChunkDemuxer::ChunkDemuxer(const base::Closure
& open_cb
,
1015 const NeedKeyCB
& need_key_cb
,
1016 const LogCB
& log_cb
,
1017 bool splice_frames_enabled
)
1018 : state_(WAITING_FOR_INIT
),
1019 cancel_next_seek_(false),
1022 need_key_cb_(need_key_cb
),
1023 enable_text_(false),
1025 duration_(kNoTimestamp()),
1026 user_specified_duration_(-1),
1027 liveness_(LIVENESS_UNKNOWN
),
1028 splice_frames_enabled_(splice_frames_enabled
) {
1029 DCHECK(!open_cb_
.is_null());
1030 DCHECK(!need_key_cb_
.is_null());
1033 void ChunkDemuxer::Initialize(
1035 const PipelineStatusCB
& cb
,
1036 bool enable_text_tracks
) {
1037 DVLOG(1) << "Init()";
1039 base::AutoLock
auto_lock(lock_
);
1041 init_cb_
= BindToCurrentLoop(cb
);
1042 if (state_
== SHUTDOWN
) {
1043 base::ResetAndReturn(&init_cb_
).Run(DEMUXER_ERROR_COULD_NOT_OPEN
);
1046 DCHECK_EQ(state_
, WAITING_FOR_INIT
);
1048 enable_text_
= enable_text_tracks
;
1050 ChangeState_Locked(INITIALIZING
);
1052 base::ResetAndReturn(&open_cb_
).Run();
1055 void ChunkDemuxer::Stop(const base::Closure
& callback
) {
1056 DVLOG(1) << "Stop()";
1061 void ChunkDemuxer::Seek(TimeDelta time
, const PipelineStatusCB
& cb
) {
1062 DVLOG(1) << "Seek(" << time
.InSecondsF() << ")";
1063 DCHECK(time
>= TimeDelta());
1065 base::AutoLock
auto_lock(lock_
);
1066 DCHECK(seek_cb_
.is_null());
1068 seek_cb_
= BindToCurrentLoop(cb
);
1069 if (state_
!= INITIALIZED
&& state_
!= ENDED
) {
1070 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_INVALID_STATE
);
1074 if (cancel_next_seek_
) {
1075 cancel_next_seek_
= false;
1076 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1080 SeekAllSources(time
);
1081 StartReturningData();
1083 if (IsSeekWaitingForData_Locked()) {
1084 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1088 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1091 // Demuxer implementation.
1092 DemuxerStream
* ChunkDemuxer::GetStream(DemuxerStream::Type type
) {
1093 DCHECK_NE(type
, DemuxerStream::TEXT
);
1094 base::AutoLock
auto_lock(lock_
);
1095 if (type
== DemuxerStream::VIDEO
)
1096 return video_
.get();
1098 if (type
== DemuxerStream::AUDIO
)
1099 return audio_
.get();
1104 base::Time
ChunkDemuxer::GetTimelineOffset() const {
1105 return timeline_offset_
;
1108 Demuxer::Liveness
ChunkDemuxer::GetLiveness() const {
1112 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time
) {
1113 DVLOG(1) << "StartWaitingForSeek()";
1114 base::AutoLock
auto_lock(lock_
);
1115 DCHECK(state_
== INITIALIZED
|| state_
== ENDED
|| state_
== SHUTDOWN
||
1116 state_
== PARSE_ERROR
) << state_
;
1117 DCHECK(seek_cb_
.is_null());
1119 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1122 AbortPendingReads();
1123 SeekAllSources(seek_time
);
1125 // Cancel state set in CancelPendingSeek() since we want to
1126 // accept the next Seek().
1127 cancel_next_seek_
= false;
1130 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time
) {
1131 base::AutoLock
auto_lock(lock_
);
1132 DCHECK_NE(state_
, INITIALIZING
);
1133 DCHECK(seek_cb_
.is_null() || IsSeekWaitingForData_Locked());
1135 if (cancel_next_seek_
)
1138 AbortPendingReads();
1139 SeekAllSources(seek_time
);
1141 if (seek_cb_
.is_null()) {
1142 cancel_next_seek_
= true;
1146 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1149 ChunkDemuxer::Status
ChunkDemuxer::AddId(const std::string
& id
,
1150 const std::string
& type
,
1151 std::vector
<std::string
>& codecs
) {
1152 base::AutoLock
auto_lock(lock_
);
1154 if ((state_
!= WAITING_FOR_INIT
&& state_
!= INITIALIZING
) || IsValidId(id
))
1155 return kReachedIdLimit
;
1157 bool has_audio
= false;
1158 bool has_video
= false;
1159 scoped_ptr
<media::StreamParser
> stream_parser(
1160 StreamParserFactory::Create(type
, codecs
, log_cb_
,
1161 &has_audio
, &has_video
));
1164 return ChunkDemuxer::kNotSupported
;
1166 if ((has_audio
&& !source_id_audio_
.empty()) ||
1167 (has_video
&& !source_id_video_
.empty()))
1168 return kReachedIdLimit
;
1171 source_id_audio_
= id
;
1174 source_id_video_
= id
;
1176 scoped_ptr
<FrameProcessor
> frame_processor(
1177 new FrameProcessor(base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary
,
1178 base::Unretained(this))));
1180 scoped_ptr
<SourceState
> source_state(
1181 new SourceState(stream_parser
.Pass(),
1182 frame_processor
.Pass(), log_cb_
,
1183 base::Bind(&ChunkDemuxer::CreateDemuxerStream
,
1184 base::Unretained(this))));
1186 SourceState::NewTextTrackCB new_text_track_cb
;
1189 new_text_track_cb
= base::Bind(&ChunkDemuxer::OnNewTextTrack
,
1190 base::Unretained(this));
1194 base::Bind(&ChunkDemuxer::OnSourceInitDone
, base::Unretained(this)),
1200 source_state_map_
[id
] = source_state
.release();
1204 void ChunkDemuxer::RemoveId(const std::string
& id
) {
1205 base::AutoLock
auto_lock(lock_
);
1206 CHECK(IsValidId(id
));
1208 delete source_state_map_
[id
];
1209 source_state_map_
.erase(id
);
1211 if (source_id_audio_
== id
)
1212 source_id_audio_
.clear();
1214 if (source_id_video_
== id
)
1215 source_id_video_
.clear();
1218 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges(const std::string
& id
) const {
1219 base::AutoLock
auto_lock(lock_
);
1220 DCHECK(!id
.empty());
1222 SourceStateMap::const_iterator itr
= source_state_map_
.find(id
);
1224 DCHECK(itr
!= source_state_map_
.end());
1225 return itr
->second
->GetBufferedRanges(duration_
, state_
== ENDED
);
1228 void ChunkDemuxer::AppendData(const std::string
& id
,
1229 const uint8
* data
, size_t length
,
1230 TimeDelta append_window_start
,
1231 TimeDelta append_window_end
,
1232 TimeDelta
* timestamp_offset
) {
1233 DVLOG(1) << "AppendData(" << id
<< ", " << length
<< ")";
1235 DCHECK(!id
.empty());
1236 DCHECK(timestamp_offset
);
1238 Ranges
<TimeDelta
> ranges
;
1241 base::AutoLock
auto_lock(lock_
);
1242 DCHECK_NE(state_
, ENDED
);
1244 // Capture if any of the SourceBuffers are waiting for data before we start
1246 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1255 DCHECK(IsValidId(id
));
1256 if (!source_state_map_
[id
]->Append(data
, length
,
1257 append_window_start
,
1259 timestamp_offset
)) {
1260 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1266 DCHECK(IsValidId(id
));
1267 if (!source_state_map_
[id
]->Append(data
, length
,
1268 append_window_start
,
1270 timestamp_offset
)) {
1271 ReportError_Locked(PIPELINE_ERROR_DECODE
);
1277 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1280 case WAITING_FOR_INIT
:
1283 DVLOG(1) << "AppendData(): called in unexpected state " << state_
;
1287 // Check to see if data was appended at the pending seek point. This
1288 // indicates we have parsed enough data to complete the seek.
1289 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1290 !seek_cb_
.is_null()) {
1291 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1294 ranges
= GetBufferedRanges_Locked();
1297 for (size_t i
= 0; i
< ranges
.size(); ++i
)
1298 host_
->AddBufferedTimeRange(ranges
.start(i
), ranges
.end(i
));
1301 void ChunkDemuxer::Abort(const std::string
& id
,
1302 TimeDelta append_window_start
,
1303 TimeDelta append_window_end
,
1304 TimeDelta
* timestamp_offset
) {
1305 DVLOG(1) << "Abort(" << id
<< ")";
1306 base::AutoLock
auto_lock(lock_
);
1307 DCHECK(!id
.empty());
1308 CHECK(IsValidId(id
));
1309 source_state_map_
[id
]->Abort(append_window_start
,
1314 void ChunkDemuxer::Remove(const std::string
& id
, TimeDelta start
,
1316 DVLOG(1) << "Remove(" << id
<< ", " << start
.InSecondsF()
1317 << ", " << end
.InSecondsF() << ")";
1318 base::AutoLock
auto_lock(lock_
);
1320 DCHECK(!id
.empty());
1321 CHECK(IsValidId(id
));
1322 DCHECK(start
>= base::TimeDelta()) << start
.InSecondsF();
1323 DCHECK(start
< end
) << "start " << start
.InSecondsF()
1324 << " end " << end
.InSecondsF();
1325 DCHECK(duration_
!= kNoTimestamp());
1326 DCHECK(start
<= duration_
) << "start " << start
.InSecondsF()
1327 << " duration " << duration_
.InSecondsF();
1329 if (start
== duration_
)
1332 source_state_map_
[id
]->Remove(start
, end
, duration_
);
1335 double ChunkDemuxer::GetDuration() {
1336 base::AutoLock
auto_lock(lock_
);
1337 return GetDuration_Locked();
1340 double ChunkDemuxer::GetDuration_Locked() {
1341 lock_
.AssertAcquired();
1342 if (duration_
== kNoTimestamp())
1343 return std::numeric_limits
<double>::quiet_NaN();
1345 // Return positive infinity if the resource is unbounded.
1346 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1347 if (duration_
== kInfiniteDuration())
1348 return std::numeric_limits
<double>::infinity();
1350 if (user_specified_duration_
>= 0)
1351 return user_specified_duration_
;
1353 return duration_
.InSecondsF();
1356 void ChunkDemuxer::SetDuration(double duration
) {
1357 base::AutoLock
auto_lock(lock_
);
1358 DVLOG(1) << "SetDuration(" << duration
<< ")";
1359 DCHECK_GE(duration
, 0);
1361 if (duration
== GetDuration_Locked())
1364 // Compute & bounds check the TimeDelta representation of duration.
1365 // This can be different if the value of |duration| doesn't fit the range or
1366 // precision of TimeDelta.
1367 TimeDelta min_duration
= TimeDelta::FromInternalValue(1);
1368 // Don't use TimeDelta::Max() here, as we want the largest finite time delta.
1369 TimeDelta max_duration
= TimeDelta::FromInternalValue(kint64max
- 1);
1370 double min_duration_in_seconds
= min_duration
.InSecondsF();
1371 double max_duration_in_seconds
= max_duration
.InSecondsF();
1373 TimeDelta duration_td
;
1374 if (duration
== std::numeric_limits
<double>::infinity()) {
1375 duration_td
= media::kInfiniteDuration();
1376 } else if (duration
< min_duration_in_seconds
) {
1377 duration_td
= min_duration
;
1378 } else if (duration
> max_duration_in_seconds
) {
1379 duration_td
= max_duration
;
1381 duration_td
= TimeDelta::FromMicroseconds(
1382 duration
* base::Time::kMicrosecondsPerSecond
);
1385 DCHECK(duration_td
> TimeDelta());
1387 user_specified_duration_
= duration
;
1388 duration_
= duration_td
;
1389 host_
->SetDuration(duration_
);
1391 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1392 itr
!= source_state_map_
.end(); ++itr
) {
1393 itr
->second
->OnSetDuration(duration_
);
1397 bool ChunkDemuxer::IsParsingMediaSegment(const std::string
& id
) {
1398 base::AutoLock
auto_lock(lock_
);
1399 DVLOG(1) << "IsParsingMediaSegment(" << id
<< ")";
1400 CHECK(IsValidId(id
));
1402 return source_state_map_
[id
]->parsing_media_segment();
1405 void ChunkDemuxer::SetSequenceMode(const std::string
& id
,
1406 bool sequence_mode
) {
1407 base::AutoLock
auto_lock(lock_
);
1408 DVLOG(1) << "SetSequenceMode(" << id
<< ", " << sequence_mode
<< ")";
1409 CHECK(IsValidId(id
));
1410 DCHECK_NE(state_
, ENDED
);
1412 source_state_map_
[id
]->SetSequenceMode(sequence_mode
);
1415 void ChunkDemuxer::SetGroupStartTimestampIfInSequenceMode(
1416 const std::string
& id
,
1417 base::TimeDelta timestamp_offset
) {
1418 base::AutoLock
auto_lock(lock_
);
1419 DVLOG(1) << "SetGroupStartTimestampIfInSequenceMode(" << id
<< ", "
1420 << timestamp_offset
.InSecondsF() << ")";
1421 CHECK(IsValidId(id
));
1422 DCHECK_NE(state_
, ENDED
);
1424 source_state_map_
[id
]->SetGroupStartTimestampIfInSequenceMode(
1429 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status
) {
1430 DVLOG(1) << "MarkEndOfStream(" << status
<< ")";
1431 base::AutoLock
auto_lock(lock_
);
1432 DCHECK_NE(state_
, WAITING_FOR_INIT
);
1433 DCHECK_NE(state_
, ENDED
);
1435 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1438 if (state_
== INITIALIZING
) {
1439 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1443 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1444 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1445 itr
!= source_state_map_
.end(); ++itr
) {
1446 itr
->second
->MarkEndOfStream();
1449 CompletePendingReadsIfPossible();
1451 // Give a chance to resume the pending seek process.
1452 if (status
!= PIPELINE_OK
) {
1453 ReportError_Locked(status
);
1457 ChangeState_Locked(ENDED
);
1458 DecreaseDurationIfNecessary();
1460 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1461 !seek_cb_
.is_null()) {
1462 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1466 void ChunkDemuxer::UnmarkEndOfStream() {
1467 DVLOG(1) << "UnmarkEndOfStream()";
1468 base::AutoLock
auto_lock(lock_
);
1469 DCHECK_EQ(state_
, ENDED
);
1471 ChangeState_Locked(INITIALIZED
);
1473 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1474 itr
!= source_state_map_
.end(); ++itr
) {
1475 itr
->second
->UnmarkEndOfStream();
1479 void ChunkDemuxer::Shutdown() {
1480 DVLOG(1) << "Shutdown()";
1481 base::AutoLock
auto_lock(lock_
);
1483 if (state_
== SHUTDOWN
)
1486 ShutdownAllStreams();
1488 ChangeState_Locked(SHUTDOWN
);
1490 if(!seek_cb_
.is_null())
1491 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_ABORT
);
1494 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit
) {
1495 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1496 itr
!= source_state_map_
.end(); ++itr
) {
1497 itr
->second
->SetMemoryLimitsForTesting(memory_limit
);
1501 void ChunkDemuxer::ChangeState_Locked(State new_state
) {
1502 lock_
.AssertAcquired();
1503 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1504 << state_
<< " -> " << new_state
;
1508 ChunkDemuxer::~ChunkDemuxer() {
1509 DCHECK_NE(state_
, INITIALIZED
);
1511 STLDeleteValues(&source_state_map_
);
1514 void ChunkDemuxer::ReportError_Locked(PipelineStatus error
) {
1515 DVLOG(1) << "ReportError_Locked(" << error
<< ")";
1516 lock_
.AssertAcquired();
1517 DCHECK_NE(error
, PIPELINE_OK
);
1519 ChangeState_Locked(PARSE_ERROR
);
1521 PipelineStatusCB cb
;
1523 if (!init_cb_
.is_null()) {
1524 std::swap(cb
, init_cb_
);
1526 if (!seek_cb_
.is_null())
1527 std::swap(cb
, seek_cb_
);
1529 ShutdownAllStreams();
1532 if (!cb
.is_null()) {
1537 base::AutoUnlock
auto_unlock(lock_
);
1538 host_
->OnDemuxerError(error
);
1541 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1542 lock_
.AssertAcquired();
1543 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1544 itr
!= source_state_map_
.end(); ++itr
) {
1545 if (itr
->second
->IsSeekWaitingForData())
1552 void ChunkDemuxer::OnSourceInitDone(
1554 const StreamParser::InitParameters
& params
) {
1555 DVLOG(1) << "OnSourceInitDone(" << success
<< ", "
1556 << params
.duration
.InSecondsF() << ")";
1557 lock_
.AssertAcquired();
1558 DCHECK_EQ(state_
, INITIALIZING
);
1559 if (!success
|| (!audio_
&& !video_
)) {
1560 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1564 if (params
.duration
!= TimeDelta() && duration_
== kNoTimestamp())
1565 UpdateDuration(params
.duration
);
1567 if (!params
.timeline_offset
.is_null()) {
1568 if (!timeline_offset_
.is_null() &&
1569 params
.timeline_offset
!= timeline_offset_
) {
1571 << "Timeline offset is not the same across all SourceBuffers.";
1572 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1576 timeline_offset_
= params
.timeline_offset
;
1579 if (params
.liveness
!= LIVENESS_UNKNOWN
) {
1580 if (liveness_
!= LIVENESS_UNKNOWN
&& params
.liveness
!= liveness_
) {
1582 << "Liveness is not the same across all SourceBuffers.";
1583 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1587 liveness_
= params
.liveness
;
1590 // Wait until all streams have initialized.
1591 if ((!source_id_audio_
.empty() && !audio_
) ||
1592 (!source_id_video_
.empty() && !video_
)) {
1596 SeekAllSources(base::TimeDelta());
1597 StartReturningData();
1599 if (duration_
== kNoTimestamp())
1600 duration_
= kInfiniteDuration();
1602 // The demuxer is now initialized after the |start_timestamp_| was set.
1603 ChangeState_Locked(INITIALIZED
);
1604 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
1608 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type
) {
1610 case DemuxerStream::AUDIO
:
1614 new ChunkDemuxerStream(DemuxerStream::AUDIO
, splice_frames_enabled_
));
1615 return audio_
.get();
1617 case DemuxerStream::VIDEO
:
1621 new ChunkDemuxerStream(DemuxerStream::VIDEO
, splice_frames_enabled_
));
1622 return video_
.get();
1624 case DemuxerStream::TEXT
: {
1625 return new ChunkDemuxerStream(DemuxerStream::TEXT
,
1626 splice_frames_enabled_
);
1629 case DemuxerStream::UNKNOWN
:
1630 case DemuxerStream::NUM_TYPES
:
1638 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
1639 const TextTrackConfig
& config
) {
1640 lock_
.AssertAcquired();
1641 DCHECK_NE(state_
, SHUTDOWN
);
1642 host_
->AddTextStream(text_stream
, config
);
1645 bool ChunkDemuxer::IsValidId(const std::string
& source_id
) const {
1646 lock_
.AssertAcquired();
1647 return source_state_map_
.count(source_id
) > 0u;
1650 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration
) {
1651 DCHECK(duration_
!= new_duration
);
1652 user_specified_duration_
= -1;
1653 duration_
= new_duration
;
1654 host_
->SetDuration(new_duration
);
1657 void ChunkDemuxer::IncreaseDurationIfNecessary(TimeDelta new_duration
) {
1658 DCHECK(new_duration
!= kNoTimestamp());
1659 DCHECK(new_duration
!= kInfiniteDuration());
1661 // Per April 1, 2014 MSE spec editor's draft:
1662 // https://dvcs.w3.org/hg/html-media/raw-file/d471a4412040/media-source/
1663 // media-source.html#sourcebuffer-coded-frame-processing
1664 // 5. If the media segment contains data beyond the current duration, then run
1665 // the duration change algorithm with new duration set to the maximum of
1666 // the current duration and the group end timestamp.
1668 if (new_duration
<= duration_
)
1671 DVLOG(2) << __FUNCTION__
<< ": Increasing duration: "
1672 << duration_
.InSecondsF() << " -> " << new_duration
.InSecondsF();
1674 UpdateDuration(new_duration
);
1677 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1678 lock_
.AssertAcquired();
1680 TimeDelta max_duration
;
1682 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1683 itr
!= source_state_map_
.end(); ++itr
) {
1684 max_duration
= std::max(max_duration
,
1685 itr
->second
->GetMaxBufferedDuration());
1688 if (max_duration
== TimeDelta())
1691 if (max_duration
< duration_
)
1692 UpdateDuration(max_duration
);
1695 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges() const {
1696 base::AutoLock
auto_lock(lock_
);
1697 return GetBufferedRanges_Locked();
1700 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges_Locked() const {
1701 lock_
.AssertAcquired();
1703 bool ended
= state_
== ENDED
;
1704 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1705 // we'll need to update this loop to only add ranges from active sources.
1706 RangesList ranges_list
;
1707 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1708 itr
!= source_state_map_
.end(); ++itr
) {
1709 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration_
, ended
));
1712 return ComputeIntersection(ranges_list
, ended
);
1715 void ChunkDemuxer::StartReturningData() {
1716 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1717 itr
!= source_state_map_
.end(); ++itr
) {
1718 itr
->second
->StartReturningData();
1722 void ChunkDemuxer::AbortPendingReads() {
1723 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1724 itr
!= source_state_map_
.end(); ++itr
) {
1725 itr
->second
->AbortReads();
1729 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time
) {
1730 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1731 itr
!= source_state_map_
.end(); ++itr
) {
1732 itr
->second
->Seek(seek_time
);
1736 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1737 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1738 itr
!= source_state_map_
.end(); ++itr
) {
1739 itr
->second
->CompletePendingReadIfPossible();
1743 void ChunkDemuxer::ShutdownAllStreams() {
1744 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1745 itr
!= source_state_map_
.end(); ++itr
) {
1746 itr
->second
->Shutdown();
1750 } // namespace media