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"
12 #include "base/bind.h"
13 #include "base/callback_helpers.h"
14 #include "base/location.h"
15 #include "base/message_loop/message_loop_proxy.h"
16 #include "base/stl_util.h"
17 #include "media/base/audio_decoder_config.h"
18 #include "media/base/bind_to_current_loop.h"
19 #include "media/base/stream_parser_buffer.h"
20 #include "media/base/video_decoder_config.h"
21 #include "media/filters/stream_parser_factory.h"
23 using base::TimeDelta
;
27 // List of time ranges for each SourceBuffer.
28 typedef std::list
<Ranges
<TimeDelta
> > RangesList
;
29 static Ranges
<TimeDelta
> ComputeIntersection(const RangesList
& activeRanges
,
31 // Implementation of HTMLMediaElement.buffered algorithm in MSE spec.
32 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#dom-htmlmediaelement.buffered
34 // Step 1: If activeSourceBuffers.length equals 0 then return an empty
35 // TimeRanges object and abort these steps.
36 if (activeRanges
.empty())
37 return Ranges
<TimeDelta
>();
39 // Step 2: Let active ranges be the ranges returned by buffered for each
40 // SourceBuffer object in activeSourceBuffers.
41 // Step 3: Let highest end time be the largest range end time in the active
43 TimeDelta highest_end_time
;
44 for (RangesList::const_iterator itr
= activeRanges
.begin();
45 itr
!= activeRanges
.end(); ++itr
) {
49 highest_end_time
= std::max(highest_end_time
, itr
->end(itr
->size() - 1));
52 // Step 4: Let intersection ranges equal a TimeRange object containing a
53 // single range from 0 to highest end time.
54 Ranges
<TimeDelta
> intersection_ranges
;
55 intersection_ranges
.Add(TimeDelta(), highest_end_time
);
57 // Step 5: For each SourceBuffer object in activeSourceBuffers run the
59 for (RangesList::const_iterator itr
= activeRanges
.begin();
60 itr
!= activeRanges
.end(); ++itr
) {
61 // Step 5.1: Let source ranges equal the ranges returned by the buffered
62 // attribute on the current SourceBuffer.
63 Ranges
<TimeDelta
> source_ranges
= *itr
;
65 // Step 5.2: If readyState is "ended", then set the end time on the last
66 // range in source ranges to highest end time.
67 if (ended
&& source_ranges
.size() > 0u) {
68 source_ranges
.Add(source_ranges
.start(source_ranges
.size() - 1),
72 // Step 5.3: Let new intersection ranges equal the intersection between
73 // the intersection ranges and the source ranges.
74 // Step 5.4: Replace the ranges in intersection ranges with the new
75 // intersection ranges.
76 intersection_ranges
= intersection_ranges
.IntersectionWith(source_ranges
);
79 return intersection_ranges
;
82 // Contains state belonging to a source id.
85 // Callback signature used to create ChunkDemuxerStreams.
86 typedef base::Callback
<ChunkDemuxerStream
*(
87 DemuxerStream::Type
)> CreateDemuxerStreamCB
;
89 // Callback signature used to notify ChunkDemuxer of timestamps
90 // that may cause the duration to be updated.
91 typedef base::Callback
<void(
92 TimeDelta
, ChunkDemuxerStream
*)> IncreaseDurationCB
;
94 typedef base::Callback
<void(
95 ChunkDemuxerStream
*, const TextTrackConfig
&)> NewTextTrackCB
;
97 SourceState(scoped_ptr
<StreamParser
> stream_parser
, const LogCB
& log_cb
,
98 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
99 const IncreaseDurationCB
& increase_duration_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. Appending uses cached |timestamp_offset_| and may update
112 // |*timestamp_offset| if |timestamp_offset| is not NULL.
113 // TODO(wolenetz): Rework so |timestamp_offset_| is only valid during
114 // Append(). See http://crbug.com/347623.
115 bool Append(const uint8
* data
, size_t length
, double* timestamp_offset
);
117 // Aborts the current append sequence and resets the parser.
120 // Calls Remove(|start|, |end|, |duration|) on all
121 // ChunkDemuxerStreams managed by this object.
122 void Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
);
124 // Sets user-specified |timestamp_offset_| if possible.
125 // Returns true if the offset was set. Returns false if the offset could not
126 // be set at this time.
127 bool SetTimestampOffset(TimeDelta timestamp_offset
);
129 // Sets |sequence_mode_| to |sequence_mode| if possible.
130 // Returns true if the mode update was allowed. Returns false if the mode
131 // could not be updated at this time.
132 bool SetSequenceMode(bool sequence_mode
);
134 void set_append_window_start(TimeDelta start
) {
135 append_window_start_
= start
;
137 void set_append_window_end(TimeDelta end
) { append_window_end_
= end
; }
139 // Returns the range of buffered data in this source, capped at |duration|.
140 // |ended| - Set to true if end of stream has been signalled 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. It
181 // applies |timestamp_offset_| to all buffers in |audio_buffers|,
182 // |video_buffers| and |text_map| and then calls Append() with the modified
183 // buffers on |audio_|, |video_| and/or the text demuxer streams associated
184 // with the track numbers in |text_map|.
185 // Returns true on a successful call. Returns false if an error occurred while
186 // processing the buffers.
187 bool OnNewBuffers(const StreamParser::BufferQueue
& audio_buffers
,
188 const StreamParser::BufferQueue
& video_buffers
,
189 const StreamParser::TextBufferQueueMap
& text_map
);
191 // Helper function for OnNewBuffers() when new text buffers have been parsed.
192 // It applies |timestamp_offset_| to all buffers in |buffers| and then appends
193 // the (modified) buffers to the demuxer stream associated with
194 // the track having |text_track_id|.
195 // Returns true on a successful call. Returns false if an error occurred while
196 // processing the buffers.
197 bool OnTextBuffers(StreamParser::TrackId text_track_id
,
198 const StreamParser::BufferQueue
& buffers
);
200 // Helper function that appends |buffers| to |stream| and calls
201 // |increase_duration_cb_| to potentially update the duration.
202 // Returns true if the append was successful. Returns false if
203 // |stream| is NULL or something in |buffers| caused the append to fail.
204 bool AppendAndUpdateDuration(ChunkDemuxerStream
* stream
,
205 const StreamParser::BufferQueue
& buffers
);
207 // Helper function that adds |timestamp_offset_| to each buffer in |buffers|.
208 void AdjustBufferTimestamps(const StreamParser::BufferQueue
& buffers
);
210 // Filters out buffers that are outside of the append window
211 // [|append_window_start_|, |append_window_end_|).
212 // |needs_keyframe| is a pointer to the |xxx_need_keyframe_| flag
213 // associated with the |buffers|. Its state is read an updated as
214 // this method filters |buffers|.
215 // Buffers that are inside the append window are appended to the end
216 // of |filtered_buffers|.
217 void FilterWithAppendWindow(const StreamParser::BufferQueue
& buffers
,
218 bool* needs_keyframe
,
219 StreamParser::BufferQueue
* filtered_buffers
);
221 CreateDemuxerStreamCB create_demuxer_stream_cb_
;
222 IncreaseDurationCB increase_duration_cb_
;
223 NewTextTrackCB new_text_track_cb_
;
225 // The offset to apply to media segment timestamps.
226 TimeDelta timestamp_offset_
;
228 // Flag that tracks whether or not the current Append() operation changed
229 // |timestamp_offset_|.
230 bool timestamp_offset_updated_by_append_
;
232 // Tracks the mode by which appended media is processed. If true, then
233 // appended media is processed using "sequence" mode. Otherwise, appended
234 // media is processed using "segments" mode.
235 // TODO(wolenetz): Enable "sequence" mode logic. See http://crbug.com/249422
236 // and http://crbug.com/333437.
239 TimeDelta append_window_start_
;
240 TimeDelta append_window_end_
;
242 // Set to true if the next buffers appended within the append window
243 // represent the start of a new media segment. This flag being set
244 // triggers a call to |new_segment_cb_| when the new buffers are
245 // appended. The flag is set on actual media segment boundaries and
246 // when the "append window" filtering causes discontinuities in the
248 bool new_media_segment_
;
250 // Keeps track of whether |timestamp_offset_| or |sequence_mode_| can be
251 // updated. These cannot be updated if a media segment is being parsed.
252 bool parsing_media_segment_
;
254 // The object used to parse appended data.
255 scoped_ptr
<StreamParser
> stream_parser_
;
257 ChunkDemuxerStream
* audio_
;
258 bool audio_needs_keyframe_
;
260 ChunkDemuxerStream
* video_
;
261 bool video_needs_keyframe_
;
263 typedef std::map
<StreamParser::TrackId
, ChunkDemuxerStream
*> TextStreamMap
;
264 TextStreamMap text_stream_map_
;
268 DISALLOW_COPY_AND_ASSIGN(SourceState
);
271 class ChunkDemuxerStream
: public DemuxerStream
{
273 typedef std::deque
<scoped_refptr
<StreamParserBuffer
> > BufferQueue
;
275 explicit ChunkDemuxerStream(Type type
);
276 virtual ~ChunkDemuxerStream();
278 // ChunkDemuxerStream control methods.
279 void StartReturningData();
281 void CompletePendingReadIfPossible();
284 // SourceBufferStream manipulation methods.
285 void Seek(TimeDelta time
);
286 bool IsSeekWaitingForData() const;
288 // Add buffers to this stream. Buffers are stored in SourceBufferStreams,
289 // which handle ordering and overlap resolution.
290 // Returns true if buffers were successfully added.
291 bool Append(const StreamParser::BufferQueue
& buffers
);
293 // Removes buffers between |start| and |end| according to the steps
294 // in the "Coded Frame Removal Algorithm" in the Media Source
296 // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
298 // |duration| is the current duration of the presentation. It is
299 // required by the computation outlined in the spec.
300 void Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
);
302 // Signal to the stream that duration has changed to |duration|.
303 void OnSetDuration(TimeDelta duration
);
305 // Returns the range of buffered data in this stream, capped at |duration|.
306 Ranges
<TimeDelta
> GetBufferedRanges(TimeDelta duration
) const;
308 // Returns the duration of the buffered data.
309 // Returns TimeDelta() if the stream has no buffered data.
310 TimeDelta
GetBufferedDuration() const;
312 // Signal to the stream that buffers handed in through subsequent calls to
313 // Append() belong to a media segment that starts at |start_timestamp|.
314 void OnNewMediaSegment(TimeDelta start_timestamp
);
316 // Called when midstream config updates occur.
317 // Returns true if the new config is accepted.
318 // Returns false if the new config should trigger an error.
319 bool UpdateAudioConfig(const AudioDecoderConfig
& config
, const LogCB
& log_cb
);
320 bool UpdateVideoConfig(const VideoDecoderConfig
& config
, const LogCB
& log_cb
);
321 void UpdateTextConfig(const TextTrackConfig
& config
, const LogCB
& log_cb
);
323 void MarkEndOfStream();
324 void UnmarkEndOfStream();
326 // DemuxerStream methods.
327 virtual void Read(const ReadCB
& read_cb
) OVERRIDE
;
328 virtual Type
type() OVERRIDE
;
329 virtual void EnableBitstreamConverter() OVERRIDE
;
330 virtual AudioDecoderConfig
audio_decoder_config() OVERRIDE
;
331 virtual VideoDecoderConfig
video_decoder_config() OVERRIDE
;
333 // Returns the text track configuration. It is an error to call this method
334 // if type() != TEXT.
335 TextTrackConfig
text_track_config();
337 // Sets the memory limit, in bytes, on the SourceBufferStream.
338 void set_memory_limit_for_testing(int memory_limit
) {
339 stream_
->set_memory_limit_for_testing(memory_limit
);
345 RETURNING_DATA_FOR_READS
,
346 RETURNING_ABORT_FOR_READS
,
350 // Assigns |state_| to |state|
351 void ChangeState_Locked(State state
);
353 void CompletePendingReadIfPossible_Locked();
355 // Specifies the type of the stream.
358 scoped_ptr
<SourceBufferStream
> stream_
;
360 mutable base::Lock lock_
;
364 DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream
);
367 SourceState::SourceState(scoped_ptr
<StreamParser
> stream_parser
,
369 const CreateDemuxerStreamCB
& create_demuxer_stream_cb
,
370 const IncreaseDurationCB
& increase_duration_cb
)
371 : create_demuxer_stream_cb_(create_demuxer_stream_cb
),
372 increase_duration_cb_(increase_duration_cb
),
373 timestamp_offset_updated_by_append_(false),
374 sequence_mode_(false),
375 append_window_end_(kInfiniteDuration()),
376 new_media_segment_(false),
377 parsing_media_segment_(false),
378 stream_parser_(stream_parser
.release()),
380 audio_needs_keyframe_(true),
382 video_needs_keyframe_(true),
384 DCHECK(!create_demuxer_stream_cb_
.is_null());
385 DCHECK(!increase_duration_cb_
.is_null());
388 SourceState::~SourceState() {
391 STLDeleteValues(&text_stream_map_
);
394 void SourceState::Init(const StreamParser::InitCB
& init_cb
,
397 const StreamParser::NeedKeyCB
& need_key_cb
,
398 const NewTextTrackCB
& new_text_track_cb
) {
399 new_text_track_cb_
= new_text_track_cb
;
401 stream_parser_
->Init(init_cb
,
402 base::Bind(&SourceState::OnNewConfigs
,
403 base::Unretained(this),
406 base::Bind(&SourceState::OnNewBuffers
,
407 base::Unretained(this)),
408 new_text_track_cb_
.is_null(),
410 base::Bind(&SourceState::OnNewMediaSegment
,
411 base::Unretained(this)),
412 base::Bind(&SourceState::OnEndOfMediaSegment
,
413 base::Unretained(this)),
417 bool SourceState::SetTimestampOffset(TimeDelta timestamp_offset
) {
418 if (parsing_media_segment_
)
421 timestamp_offset_
= timestamp_offset
;
425 bool SourceState::SetSequenceMode(bool sequence_mode
) {
426 if (parsing_media_segment_
)
429 sequence_mode_
= sequence_mode
;
433 bool SourceState::Append(const uint8
* data
, size_t length
,
434 double* timestamp_offset
) {
435 timestamp_offset_updated_by_append_
= false;
436 bool err
= stream_parser_
->Parse(data
, length
);
438 if (timestamp_offset_updated_by_append_
&& timestamp_offset
)
439 *timestamp_offset
= timestamp_offset_
.InSecondsF();
444 void SourceState::Abort() {
445 stream_parser_
->Flush();
446 audio_needs_keyframe_
= true;
447 video_needs_keyframe_
= true;
448 parsing_media_segment_
= false;
451 void SourceState::Remove(TimeDelta start
, TimeDelta end
, TimeDelta duration
) {
453 audio_
->Remove(start
, end
, duration
);
456 video_
->Remove(start
, end
, duration
);
458 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
459 itr
!= text_stream_map_
.end(); ++itr
) {
460 itr
->second
->Remove(start
, end
, duration
);
464 Ranges
<TimeDelta
> SourceState::GetBufferedRanges(TimeDelta duration
,
466 // TODO(acolwell): When we start allowing disabled tracks we'll need to update
467 // this code to only add ranges from active tracks.
468 RangesList ranges_list
;
470 ranges_list
.push_back(audio_
->GetBufferedRanges(duration
));
473 ranges_list
.push_back(video_
->GetBufferedRanges(duration
));
475 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
476 itr
!= text_stream_map_
.end(); ++itr
) {
477 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration
));
480 return ComputeIntersection(ranges_list
, ended
);
483 TimeDelta
SourceState::GetMaxBufferedDuration() const {
484 TimeDelta max_duration
;
487 max_duration
= std::max(max_duration
, audio_
->GetBufferedDuration());
490 max_duration
= std::max(max_duration
, video_
->GetBufferedDuration());
492 for (TextStreamMap::const_iterator itr
= text_stream_map_
.begin();
493 itr
!= text_stream_map_
.end(); ++itr
) {
494 max_duration
= std::max(max_duration
, itr
->second
->GetBufferedDuration());
500 void SourceState::StartReturningData() {
502 audio_
->StartReturningData();
505 video_
->StartReturningData();
507 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
508 itr
!= text_stream_map_
.end(); ++itr
) {
509 itr
->second
->StartReturningData();
513 void SourceState::AbortReads() {
515 audio_
->AbortReads();
518 video_
->AbortReads();
520 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
521 itr
!= text_stream_map_
.end(); ++itr
) {
522 itr
->second
->AbortReads();
526 void SourceState::Seek(TimeDelta seek_time
) {
528 audio_
->Seek(seek_time
);
531 video_
->Seek(seek_time
);
533 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
534 itr
!= text_stream_map_
.end(); ++itr
) {
535 itr
->second
->Seek(seek_time
);
539 void SourceState::CompletePendingReadIfPossible() {
541 audio_
->CompletePendingReadIfPossible();
544 video_
->CompletePendingReadIfPossible();
546 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
547 itr
!= text_stream_map_
.end(); ++itr
) {
548 itr
->second
->CompletePendingReadIfPossible();
552 void SourceState::OnSetDuration(TimeDelta duration
) {
554 audio_
->OnSetDuration(duration
);
557 video_
->OnSetDuration(duration
);
559 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
560 itr
!= text_stream_map_
.end(); ++itr
) {
561 itr
->second
->OnSetDuration(duration
);
565 void SourceState::MarkEndOfStream() {
567 audio_
->MarkEndOfStream();
570 video_
->MarkEndOfStream();
572 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
573 itr
!= text_stream_map_
.end(); ++itr
) {
574 itr
->second
->MarkEndOfStream();
578 void SourceState::UnmarkEndOfStream() {
580 audio_
->UnmarkEndOfStream();
583 video_
->UnmarkEndOfStream();
585 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
586 itr
!= text_stream_map_
.end(); ++itr
) {
587 itr
->second
->UnmarkEndOfStream();
591 void SourceState::Shutdown() {
598 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
599 itr
!= text_stream_map_
.end(); ++itr
) {
600 itr
->second
->Shutdown();
604 void SourceState::SetMemoryLimitsForTesting(int memory_limit
) {
606 audio_
->set_memory_limit_for_testing(memory_limit
);
609 video_
->set_memory_limit_for_testing(memory_limit
);
611 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
612 itr
!= text_stream_map_
.end(); ++itr
) {
613 itr
->second
->set_memory_limit_for_testing(memory_limit
);
617 bool SourceState::IsSeekWaitingForData() const {
618 if (audio_
&& audio_
->IsSeekWaitingForData())
621 if (video_
&& video_
->IsSeekWaitingForData())
624 // NOTE: We are intentionally not checking the text tracks
625 // because text tracks are discontinuous and may not have data
626 // for the seek position. This is ok and playback should not be
627 // stalled because we don't have cues. If cues, with timestamps after
628 // the seek time, eventually arrive they will be delivered properly
629 // in response to ChunkDemuxerStream::Read() calls.
634 void SourceState::AdjustBufferTimestamps(
635 const StreamParser::BufferQueue
& buffers
) {
636 if (timestamp_offset_
== TimeDelta())
639 for (StreamParser::BufferQueue::const_iterator itr
= buffers
.begin();
640 itr
!= buffers
.end(); ++itr
) {
641 (*itr
)->SetDecodeTimestamp(
642 (*itr
)->GetDecodeTimestamp() + timestamp_offset_
);
643 (*itr
)->set_timestamp((*itr
)->timestamp() + timestamp_offset_
);
647 bool SourceState::OnNewConfigs(
648 bool allow_audio
, bool allow_video
,
649 const AudioDecoderConfig
& audio_config
,
650 const VideoDecoderConfig
& video_config
,
651 const StreamParser::TextTrackConfigMap
& text_configs
) {
652 DVLOG(1) << "OnNewConfigs(" << allow_audio
<< ", " << allow_video
653 << ", " << audio_config
.IsValidConfig()
654 << ", " << video_config
.IsValidConfig() << ")";
656 if (!audio_config
.IsValidConfig() && !video_config
.IsValidConfig()) {
657 DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
661 // Signal an error if we get configuration info for stream types that weren't
662 // specified in AddId() or more configs after a stream is initialized.
663 if (allow_audio
!= audio_config
.IsValidConfig()) {
665 << "Initialization segment"
666 << (audio_config
.IsValidConfig() ? " has" : " does not have")
667 << " an audio track, but the mimetype"
668 << (allow_audio
? " specifies" : " does not specify")
669 << " an audio codec.";
673 if (allow_video
!= video_config
.IsValidConfig()) {
675 << "Initialization segment"
676 << (video_config
.IsValidConfig() ? " has" : " does not have")
677 << " a video track, but the mimetype"
678 << (allow_video
? " specifies" : " does not specify")
679 << " a video codec.";
684 if (audio_config
.IsValidConfig()) {
686 audio_
= create_demuxer_stream_cb_
.Run(DemuxerStream::AUDIO
);
689 DVLOG(1) << "Failed to create an audio stream.";
694 success
&= audio_
->UpdateAudioConfig(audio_config
, log_cb_
);
697 if (video_config
.IsValidConfig()) {
699 video_
= create_demuxer_stream_cb_
.Run(DemuxerStream::VIDEO
);
702 DVLOG(1) << "Failed to create a video stream.";
707 success
&= video_
->UpdateVideoConfig(video_config
, log_cb_
);
710 typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr
;
711 if (text_stream_map_
.empty()) {
712 for (TextConfigItr itr
= text_configs
.begin();
713 itr
!= text_configs
.end(); ++itr
) {
714 ChunkDemuxerStream
* const text_stream
=
715 create_demuxer_stream_cb_
.Run(DemuxerStream::TEXT
);
716 text_stream
->UpdateTextConfig(itr
->second
, log_cb_
);
717 text_stream_map_
[itr
->first
] = text_stream
;
718 new_text_track_cb_
.Run(text_stream
, itr
->second
);
721 const size_t text_count
= text_stream_map_
.size();
722 if (text_configs
.size() != text_count
) {
724 MEDIA_LOG(log_cb_
) << "The number of text track configs changed.";
725 } else if (text_count
== 1) {
726 TextConfigItr config_itr
= text_configs
.begin();
727 const TextTrackConfig
& new_config
= config_itr
->second
;
728 TextStreamMap::iterator stream_itr
= text_stream_map_
.begin();
729 ChunkDemuxerStream
* text_stream
= stream_itr
->second
;
730 TextTrackConfig old_config
= text_stream
->text_track_config();
731 if (!new_config
.Matches(old_config
)) {
733 MEDIA_LOG(log_cb_
) << "New text track config does not match old one.";
735 text_stream_map_
.clear();
736 text_stream_map_
[config_itr
->first
] = text_stream
;
739 for (TextConfigItr config_itr
= text_configs
.begin();
740 config_itr
!= text_configs
.end(); ++config_itr
) {
741 TextStreamMap::iterator stream_itr
=
742 text_stream_map_
.find(config_itr
->first
);
743 if (stream_itr
== text_stream_map_
.end()) {
745 MEDIA_LOG(log_cb_
) << "Unexpected text track configuration "
747 << config_itr
->first
;
751 const TextTrackConfig
& new_config
= config_itr
->second
;
752 ChunkDemuxerStream
* stream
= stream_itr
->second
;
753 TextTrackConfig old_config
= stream
->text_track_config();
754 if (!new_config
.Matches(old_config
)) {
756 MEDIA_LOG(log_cb_
) << "New text track config for track ID "
758 << " does not match old one.";
765 DVLOG(1) << "OnNewConfigs() : " << (success
? "success" : "failed");
769 void SourceState::OnNewMediaSegment() {
770 DVLOG(2) << "OnNewMediaSegment()";
771 parsing_media_segment_
= true;
772 new_media_segment_
= true;
775 void SourceState::OnEndOfMediaSegment() {
776 DVLOG(2) << "OnEndOfMediaSegment()";
777 parsing_media_segment_
= false;
778 new_media_segment_
= false;
781 bool SourceState::OnNewBuffers(
782 const StreamParser::BufferQueue
& audio_buffers
,
783 const StreamParser::BufferQueue
& video_buffers
,
784 const StreamParser::TextBufferQueueMap
& text_map
) {
785 DCHECK(!audio_buffers
.empty() || !video_buffers
.empty() ||
788 // TODO(wolenetz): DCHECK + return false if any of these buffers have UNKNOWN
789 // type() in upcoming coded frame processing compliant implementation. See
790 // http://crbug.com/249422.
792 AdjustBufferTimestamps(audio_buffers
);
793 AdjustBufferTimestamps(video_buffers
);
795 StreamParser::BufferQueue filtered_audio
;
796 StreamParser::BufferQueue filtered_video
;
798 FilterWithAppendWindow(audio_buffers
, &audio_needs_keyframe_
,
801 FilterWithAppendWindow(video_buffers
, &video_needs_keyframe_
,
804 if ((!filtered_audio
.empty() || !filtered_video
.empty()) &&
805 new_media_segment_
) {
806 // Find the earliest timestamp in the filtered buffers and use that for the
807 // segment start timestamp.
808 TimeDelta segment_timestamp
= kNoTimestamp();
810 if (!filtered_audio
.empty())
811 segment_timestamp
= filtered_audio
.front()->GetDecodeTimestamp();
813 if (!filtered_video
.empty() &&
814 (segment_timestamp
== kNoTimestamp() ||
815 filtered_video
.front()->GetDecodeTimestamp() < segment_timestamp
)) {
816 segment_timestamp
= filtered_video
.front()->GetDecodeTimestamp();
819 new_media_segment_
= false;
822 audio_
->OnNewMediaSegment(segment_timestamp
);
825 video_
->OnNewMediaSegment(segment_timestamp
);
827 for (TextStreamMap::iterator itr
= text_stream_map_
.begin();
828 itr
!= text_stream_map_
.end(); ++itr
) {
829 itr
->second
->OnNewMediaSegment(segment_timestamp
);
833 if (!filtered_audio
.empty() &&
834 !AppendAndUpdateDuration(audio_
, filtered_audio
)) {
838 if (!filtered_video
.empty() &&
839 !AppendAndUpdateDuration(video_
, filtered_video
)) {
843 if (text_map
.empty())
846 // Process any buffers for each of the text tracks in the map.
847 bool all_text_buffers_empty
= true;
848 for (StreamParser::TextBufferQueueMap::const_iterator itr
= text_map
.begin();
849 itr
!= text_map
.end();
851 const StreamParser::BufferQueue text_buffers
= itr
->second
;
852 if (!text_buffers
.empty()) {
853 all_text_buffers_empty
= false;
854 if (!OnTextBuffers(itr
->first
, text_buffers
))
859 DCHECK(!all_text_buffers_empty
);
863 bool SourceState::OnTextBuffers(
864 StreamParser::TrackId text_track_id
,
865 const StreamParser::BufferQueue
& buffers
) {
866 DCHECK(!buffers
.empty());
868 TextStreamMap::iterator itr
= text_stream_map_
.find(text_track_id
);
869 if (itr
== text_stream_map_
.end())
872 AdjustBufferTimestamps(buffers
);
874 StreamParser::BufferQueue filtered_buffers
;
875 bool needs_keyframe
= false;
876 FilterWithAppendWindow(buffers
, &needs_keyframe
, &filtered_buffers
);
878 if (filtered_buffers
.empty())
881 return AppendAndUpdateDuration(itr
->second
, filtered_buffers
);
884 bool SourceState::AppendAndUpdateDuration(
885 ChunkDemuxerStream
* stream
,
886 const StreamParser::BufferQueue
& buffers
) {
887 DCHECK(!buffers
.empty());
889 if (!stream
|| !stream
->Append(buffers
))
892 increase_duration_cb_
.Run(buffers
.back()->timestamp(), stream
);
896 void SourceState::FilterWithAppendWindow(
897 const StreamParser::BufferQueue
& buffers
, bool* needs_keyframe
,
898 StreamParser::BufferQueue
* filtered_buffers
) {
899 DCHECK(needs_keyframe
);
900 DCHECK(filtered_buffers
);
902 // This loop implements steps 1.9, 1.10, & 1.11 of the "Coded frame
903 // processing loop" in the Media Source Extensions spec.
904 // These steps filter out buffers that are not within the "append
905 // window" and handles resyncing on the next random access point
906 // (i.e., next keyframe) if a buffer gets dropped.
907 for (StreamParser::BufferQueue::const_iterator itr
= buffers
.begin();
908 itr
!= buffers
.end(); ++itr
) {
909 // Filter out buffers that are outside the append window. Anytime
910 // a buffer gets dropped we need to set |*needs_keyframe| to true
911 // because we can only resume decoding at keyframes.
912 TimeDelta presentation_timestamp
= (*itr
)->timestamp();
914 // TODO(acolwell): Change |frame_end_timestamp| value to
915 // |presentation_timestamp + (*itr)->duration()|, like the spec
916 // requires, once frame durations are actually present in all buffers.
917 TimeDelta frame_end_timestamp
= presentation_timestamp
;
918 if (presentation_timestamp
< append_window_start_
||
919 frame_end_timestamp
> append_window_end_
) {
920 DVLOG(1) << "Dropping buffer outside append window."
921 << " presentation_timestamp "
922 << presentation_timestamp
.InSecondsF();
923 *needs_keyframe
= true;
925 // This triggers a discontinuity so we need to treat the next frames
926 // appended within the append window as if they were the beginning of a
928 new_media_segment_
= true;
932 // If |*needs_keyframe| is true then filter out buffers until we
933 // encounter the next keyframe.
934 if (*needs_keyframe
) {
935 if (!(*itr
)->IsKeyframe()) {
936 DVLOG(1) << "Dropping non-keyframe. presentation_timestamp "
937 << presentation_timestamp
.InSecondsF();
941 *needs_keyframe
= false;
944 filtered_buffers
->push_back(*itr
);
948 ChunkDemuxerStream::ChunkDemuxerStream(Type type
)
950 state_(UNINITIALIZED
) {
953 void ChunkDemuxerStream::StartReturningData() {
954 DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
955 base::AutoLock
auto_lock(lock_
);
956 DCHECK(read_cb_
.is_null());
957 ChangeState_Locked(RETURNING_DATA_FOR_READS
);
960 void ChunkDemuxerStream::AbortReads() {
961 DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
962 base::AutoLock
auto_lock(lock_
);
963 ChangeState_Locked(RETURNING_ABORT_FOR_READS
);
964 if (!read_cb_
.is_null())
965 base::ResetAndReturn(&read_cb_
).Run(kAborted
, NULL
);
968 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
969 base::AutoLock
auto_lock(lock_
);
970 if (read_cb_
.is_null())
973 CompletePendingReadIfPossible_Locked();
976 void ChunkDemuxerStream::Shutdown() {
977 DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
978 base::AutoLock
auto_lock(lock_
);
979 ChangeState_Locked(SHUTDOWN
);
981 // Pass an end of stream buffer to the pending callback to signal that no more
982 // data will be sent.
983 if (!read_cb_
.is_null()) {
984 base::ResetAndReturn(&read_cb_
).Run(DemuxerStream::kOk
,
985 StreamParserBuffer::CreateEOSBuffer());
989 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
990 base::AutoLock
auto_lock(lock_
);
992 // This method should not be called for text tracks. See the note in
993 // SourceState::IsSeekWaitingForData().
994 DCHECK_NE(type_
, DemuxerStream::TEXT
);
996 return stream_
->IsSeekPending();
999 void ChunkDemuxerStream::Seek(TimeDelta time
) {
1000 DVLOG(1) << "ChunkDemuxerStream::Seek(" << time
.InSecondsF() << ")";
1001 base::AutoLock
auto_lock(lock_
);
1002 DCHECK(read_cb_
.is_null());
1003 DCHECK(state_
== UNINITIALIZED
|| state_
== RETURNING_ABORT_FOR_READS
)
1006 stream_
->Seek(time
);
1009 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue
& buffers
) {
1010 if (buffers
.empty())
1013 base::AutoLock
auto_lock(lock_
);
1014 DCHECK_NE(state_
, SHUTDOWN
);
1015 if (!stream_
->Append(buffers
)) {
1016 DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
1020 if (!read_cb_
.is_null())
1021 CompletePendingReadIfPossible_Locked();
1026 void ChunkDemuxerStream::Remove(TimeDelta start
, TimeDelta end
,
1027 TimeDelta duration
) {
1028 base::AutoLock
auto_lock(lock_
);
1029 stream_
->Remove(start
, end
, duration
);
1032 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration
) {
1033 base::AutoLock
auto_lock(lock_
);
1034 stream_
->OnSetDuration(duration
);
1037 Ranges
<TimeDelta
> ChunkDemuxerStream::GetBufferedRanges(
1038 TimeDelta duration
) const {
1039 base::AutoLock
auto_lock(lock_
);
1041 if (type_
== TEXT
) {
1042 // Since text tracks are discontinuous and the lack of cues should not block
1043 // playback, report the buffered range for text tracks as [0, |duration|) so
1044 // that intesections with audio & video tracks are computed correctly when
1045 // no cues are present.
1046 Ranges
<TimeDelta
> text_range
;
1047 text_range
.Add(TimeDelta(), duration
);
1051 Ranges
<TimeDelta
> range
= stream_
->GetBufferedTime();
1053 if (range
.size() == 0u)
1056 // Clamp the end of the stream's buffered ranges to fit within the duration.
1057 // This can be done by intersecting the stream's range with the valid time
1059 Ranges
<TimeDelta
> valid_time_range
;
1060 valid_time_range
.Add(range
.start(0), duration
);
1061 return range
.IntersectionWith(valid_time_range
);
1064 TimeDelta
ChunkDemuxerStream::GetBufferedDuration() const {
1065 return stream_
->GetBufferedDuration();
1068 void ChunkDemuxerStream::OnNewMediaSegment(TimeDelta start_timestamp
) {
1069 DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
1070 << start_timestamp
.InSecondsF() << ")";
1071 base::AutoLock
auto_lock(lock_
);
1072 stream_
->OnNewMediaSegment(start_timestamp
);
1075 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig
& config
,
1076 const LogCB
& log_cb
) {
1077 DCHECK(config
.IsValidConfig());
1078 DCHECK_EQ(type_
, AUDIO
);
1079 base::AutoLock
auto_lock(lock_
);
1081 DCHECK_EQ(state_
, UNINITIALIZED
);
1082 stream_
.reset(new SourceBufferStream(config
, log_cb
));
1086 return stream_
->UpdateAudioConfig(config
);
1089 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig
& config
,
1090 const LogCB
& log_cb
) {
1091 DCHECK(config
.IsValidConfig());
1092 DCHECK_EQ(type_
, VIDEO
);
1093 base::AutoLock
auto_lock(lock_
);
1096 DCHECK_EQ(state_
, UNINITIALIZED
);
1097 stream_
.reset(new SourceBufferStream(config
, log_cb
));
1101 return stream_
->UpdateVideoConfig(config
);
1104 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig
& config
,
1105 const LogCB
& log_cb
) {
1106 DCHECK_EQ(type_
, TEXT
);
1107 base::AutoLock
auto_lock(lock_
);
1109 DCHECK_EQ(state_
, UNINITIALIZED
);
1110 stream_
.reset(new SourceBufferStream(config
, log_cb
));
1113 void ChunkDemuxerStream::MarkEndOfStream() {
1114 base::AutoLock
auto_lock(lock_
);
1115 stream_
->MarkEndOfStream();
1118 void ChunkDemuxerStream::UnmarkEndOfStream() {
1119 base::AutoLock
auto_lock(lock_
);
1120 stream_
->UnmarkEndOfStream();
1123 // DemuxerStream methods.
1124 void ChunkDemuxerStream::Read(const ReadCB
& read_cb
) {
1125 base::AutoLock
auto_lock(lock_
);
1126 DCHECK_NE(state_
, UNINITIALIZED
);
1127 DCHECK(read_cb_
.is_null());
1129 read_cb_
= BindToCurrentLoop(read_cb
);
1130 CompletePendingReadIfPossible_Locked();
1133 DemuxerStream::Type
ChunkDemuxerStream::type() { return type_
; }
1135 void ChunkDemuxerStream::EnableBitstreamConverter() {}
1137 AudioDecoderConfig
ChunkDemuxerStream::audio_decoder_config() {
1138 CHECK_EQ(type_
, AUDIO
);
1139 base::AutoLock
auto_lock(lock_
);
1140 return stream_
->GetCurrentAudioDecoderConfig();
1143 VideoDecoderConfig
ChunkDemuxerStream::video_decoder_config() {
1144 CHECK_EQ(type_
, VIDEO
);
1145 base::AutoLock
auto_lock(lock_
);
1146 return stream_
->GetCurrentVideoDecoderConfig();
1149 TextTrackConfig
ChunkDemuxerStream::text_track_config() {
1150 CHECK_EQ(type_
, TEXT
);
1151 base::AutoLock
auto_lock(lock_
);
1152 return stream_
->GetCurrentTextTrackConfig();
1155 void ChunkDemuxerStream::ChangeState_Locked(State state
) {
1156 lock_
.AssertAcquired();
1157 DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1159 << " - " << state_
<< " -> " << state
;
1163 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1165 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1166 lock_
.AssertAcquired();
1167 DCHECK(!read_cb_
.is_null());
1169 DemuxerStream::Status status
;
1170 scoped_refptr
<StreamParserBuffer
> buffer
;
1176 case RETURNING_DATA_FOR_READS
:
1177 switch (stream_
->GetNextBuffer(&buffer
)) {
1178 case SourceBufferStream::kSuccess
:
1179 status
= DemuxerStream::kOk
;
1181 case SourceBufferStream::kNeedBuffer
:
1182 // Return early without calling |read_cb_| since we don't have
1183 // any data to return yet.
1185 case SourceBufferStream::kEndOfStream
:
1186 status
= DemuxerStream::kOk
;
1187 buffer
= StreamParserBuffer::CreateEOSBuffer();
1189 case SourceBufferStream::kConfigChange
:
1190 DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
1191 status
= kConfigChanged
;
1196 case RETURNING_ABORT_FOR_READS
:
1197 // Null buffers should be returned in this state since we are waiting
1198 // for a seek. Any buffers in the SourceBuffer should NOT be returned
1199 // because they are associated with the seek.
1200 status
= DemuxerStream::kAborted
;
1204 status
= DemuxerStream::kOk
;
1205 buffer
= StreamParserBuffer::CreateEOSBuffer();
1209 base::ResetAndReturn(&read_cb_
).Run(status
, buffer
);
1212 ChunkDemuxer::ChunkDemuxer(const base::Closure
& open_cb
,
1213 const NeedKeyCB
& need_key_cb
,
1214 const LogCB
& log_cb
)
1215 : state_(WAITING_FOR_INIT
),
1216 cancel_next_seek_(false),
1219 need_key_cb_(need_key_cb
),
1220 enable_text_(false),
1222 duration_(kNoTimestamp()),
1223 user_specified_duration_(-1) {
1224 DCHECK(!open_cb_
.is_null());
1225 DCHECK(!need_key_cb_
.is_null());
1228 void ChunkDemuxer::Initialize(
1230 const PipelineStatusCB
& cb
,
1231 bool enable_text_tracks
) {
1232 DVLOG(1) << "Init()";
1234 base::AutoLock
auto_lock(lock_
);
1236 init_cb_
= BindToCurrentLoop(cb
);
1237 if (state_
== SHUTDOWN
) {
1238 base::ResetAndReturn(&init_cb_
).Run(DEMUXER_ERROR_COULD_NOT_OPEN
);
1241 DCHECK_EQ(state_
, WAITING_FOR_INIT
);
1243 enable_text_
= enable_text_tracks
;
1245 ChangeState_Locked(INITIALIZING
);
1247 base::ResetAndReturn(&open_cb_
).Run();
1250 void ChunkDemuxer::Stop(const base::Closure
& callback
) {
1251 DVLOG(1) << "Stop()";
1256 void ChunkDemuxer::Seek(TimeDelta time
, const PipelineStatusCB
& cb
) {
1257 DVLOG(1) << "Seek(" << time
.InSecondsF() << ")";
1258 DCHECK(time
>= TimeDelta());
1260 base::AutoLock
auto_lock(lock_
);
1261 DCHECK(seek_cb_
.is_null());
1263 seek_cb_
= BindToCurrentLoop(cb
);
1264 if (state_
!= INITIALIZED
&& state_
!= ENDED
) {
1265 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_INVALID_STATE
);
1269 if (cancel_next_seek_
) {
1270 cancel_next_seek_
= false;
1271 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1275 SeekAllSources(time
);
1276 StartReturningData();
1278 if (IsSeekWaitingForData_Locked()) {
1279 DVLOG(1) << "Seek() : waiting for more data to arrive.";
1283 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1286 void ChunkDemuxer::OnAudioRendererDisabled() {
1287 base::AutoLock
auto_lock(lock_
);
1289 disabled_audio_
= audio_
.Pass();
1292 // Demuxer implementation.
1293 DemuxerStream
* ChunkDemuxer::GetStream(DemuxerStream::Type type
) {
1294 DCHECK_NE(type
, DemuxerStream::TEXT
);
1295 base::AutoLock
auto_lock(lock_
);
1296 if (type
== DemuxerStream::VIDEO
)
1297 return video_
.get();
1299 if (type
== DemuxerStream::AUDIO
)
1300 return audio_
.get();
1305 TimeDelta
ChunkDemuxer::GetStartTime() const {
1309 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time
) {
1310 DVLOG(1) << "StartWaitingForSeek()";
1311 base::AutoLock
auto_lock(lock_
);
1312 DCHECK(state_
== INITIALIZED
|| state_
== ENDED
|| state_
== SHUTDOWN
||
1313 state_
== PARSE_ERROR
) << state_
;
1314 DCHECK(seek_cb_
.is_null());
1316 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1319 AbortPendingReads();
1320 SeekAllSources(seek_time
);
1322 // Cancel state set in CancelPendingSeek() since we want to
1323 // accept the next Seek().
1324 cancel_next_seek_
= false;
1327 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time
) {
1328 base::AutoLock
auto_lock(lock_
);
1329 DCHECK_NE(state_
, INITIALIZING
);
1330 DCHECK(seek_cb_
.is_null() || IsSeekWaitingForData_Locked());
1332 if (cancel_next_seek_
)
1335 AbortPendingReads();
1336 SeekAllSources(seek_time
);
1338 if (seek_cb_
.is_null()) {
1339 cancel_next_seek_
= true;
1343 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1346 ChunkDemuxer::Status
ChunkDemuxer::AddId(const std::string
& id
,
1347 const std::string
& type
,
1348 std::vector
<std::string
>& codecs
) {
1349 base::AutoLock
auto_lock(lock_
);
1351 if ((state_
!= WAITING_FOR_INIT
&& state_
!= INITIALIZING
) || IsValidId(id
))
1352 return kReachedIdLimit
;
1354 bool has_audio
= false;
1355 bool has_video
= false;
1356 scoped_ptr
<media::StreamParser
> stream_parser(
1357 StreamParserFactory::Create(type
, codecs
, log_cb_
,
1358 &has_audio
, &has_video
));
1361 return ChunkDemuxer::kNotSupported
;
1363 if ((has_audio
&& !source_id_audio_
.empty()) ||
1364 (has_video
&& !source_id_video_
.empty()))
1365 return kReachedIdLimit
;
1368 source_id_audio_
= id
;
1371 source_id_video_
= id
;
1373 scoped_ptr
<SourceState
> source_state(
1374 new SourceState(stream_parser
.Pass(), log_cb_
,
1375 base::Bind(&ChunkDemuxer::CreateDemuxerStream
,
1376 base::Unretained(this)),
1377 base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary
,
1378 base::Unretained(this))));
1380 SourceState::NewTextTrackCB new_text_track_cb
;
1383 new_text_track_cb
= base::Bind(&ChunkDemuxer::OnNewTextTrack
,
1384 base::Unretained(this));
1388 base::Bind(&ChunkDemuxer::OnSourceInitDone
, base::Unretained(this)),
1394 source_state_map_
[id
] = source_state
.release();
1398 void ChunkDemuxer::RemoveId(const std::string
& id
) {
1399 base::AutoLock
auto_lock(lock_
);
1400 CHECK(IsValidId(id
));
1402 delete source_state_map_
[id
];
1403 source_state_map_
.erase(id
);
1405 if (source_id_audio_
== id
)
1406 source_id_audio_
.clear();
1408 if (source_id_video_
== id
)
1409 source_id_video_
.clear();
1412 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges(const std::string
& id
) const {
1413 base::AutoLock
auto_lock(lock_
);
1414 DCHECK(!id
.empty());
1416 SourceStateMap::const_iterator itr
= source_state_map_
.find(id
);
1418 DCHECK(itr
!= source_state_map_
.end());
1419 return itr
->second
->GetBufferedRanges(duration_
, state_
== ENDED
);
1422 void ChunkDemuxer::AppendData(const std::string
& id
,
1423 const uint8
* data
, size_t length
,
1424 double* timestamp_offset
) {
1425 DVLOG(1) << "AppendData(" << id
<< ", " << length
<< ")";
1427 DCHECK(!id
.empty());
1429 Ranges
<TimeDelta
> ranges
;
1432 base::AutoLock
auto_lock(lock_
);
1433 DCHECK_NE(state_
, ENDED
);
1435 // Capture if any of the SourceBuffers are waiting for data before we start
1437 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1446 DCHECK(IsValidId(id
));
1447 if (!source_state_map_
[id
]->Append(data
, length
,
1448 timestamp_offset
)) {
1449 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1455 DCHECK(IsValidId(id
));
1456 if (!source_state_map_
[id
]->Append(data
, length
,
1457 timestamp_offset
)) {
1458 ReportError_Locked(PIPELINE_ERROR_DECODE
);
1464 DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1467 case WAITING_FOR_INIT
:
1470 DVLOG(1) << "AppendData(): called in unexpected state " << state_
;
1474 // Check to see if data was appended at the pending seek point. This
1475 // indicates we have parsed enough data to complete the seek.
1476 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1477 !seek_cb_
.is_null()) {
1478 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1481 ranges
= GetBufferedRanges_Locked();
1484 for (size_t i
= 0; i
< ranges
.size(); ++i
)
1485 host_
->AddBufferedTimeRange(ranges
.start(i
), ranges
.end(i
));
1488 void ChunkDemuxer::Abort(const std::string
& id
) {
1489 DVLOG(1) << "Abort(" << id
<< ")";
1490 base::AutoLock
auto_lock(lock_
);
1491 DCHECK(!id
.empty());
1492 CHECK(IsValidId(id
));
1493 source_state_map_
[id
]->Abort();
1496 void ChunkDemuxer::Remove(const std::string
& id
, TimeDelta start
,
1498 DVLOG(1) << "Remove(" << id
<< ", " << start
.InSecondsF()
1499 << ", " << end
.InSecondsF() << ")";
1500 base::AutoLock
auto_lock(lock_
);
1502 DCHECK(!id
.empty());
1503 CHECK(IsValidId(id
));
1504 source_state_map_
[id
]->Remove(start
, end
, duration_
);
1507 double ChunkDemuxer::GetDuration() {
1508 base::AutoLock
auto_lock(lock_
);
1509 return GetDuration_Locked();
1512 double ChunkDemuxer::GetDuration_Locked() {
1513 lock_
.AssertAcquired();
1514 if (duration_
== kNoTimestamp())
1515 return std::numeric_limits
<double>::quiet_NaN();
1517 // Return positive infinity if the resource is unbounded.
1518 // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1519 if (duration_
== kInfiniteDuration())
1520 return std::numeric_limits
<double>::infinity();
1522 if (user_specified_duration_
>= 0)
1523 return user_specified_duration_
;
1525 return duration_
.InSecondsF();
1528 void ChunkDemuxer::SetDuration(double duration
) {
1529 base::AutoLock
auto_lock(lock_
);
1530 DVLOG(1) << "SetDuration(" << duration
<< ")";
1531 DCHECK_GE(duration
, 0);
1533 if (duration
== GetDuration_Locked())
1536 // Compute & bounds check the TimeDelta representation of duration.
1537 // This can be different if the value of |duration| doesn't fit the range or
1538 // precision of TimeDelta.
1539 TimeDelta min_duration
= TimeDelta::FromInternalValue(1);
1540 TimeDelta max_duration
= TimeDelta::FromInternalValue(kint64max
- 1);
1541 double min_duration_in_seconds
= min_duration
.InSecondsF();
1542 double max_duration_in_seconds
= max_duration
.InSecondsF();
1544 TimeDelta duration_td
;
1545 if (duration
== std::numeric_limits
<double>::infinity()) {
1546 duration_td
= media::kInfiniteDuration();
1547 } else if (duration
< min_duration_in_seconds
) {
1548 duration_td
= min_duration
;
1549 } else if (duration
> max_duration_in_seconds
) {
1550 duration_td
= max_duration
;
1552 duration_td
= TimeDelta::FromMicroseconds(
1553 duration
* base::Time::kMicrosecondsPerSecond
);
1556 DCHECK(duration_td
> TimeDelta());
1558 user_specified_duration_
= duration
;
1559 duration_
= duration_td
;
1560 host_
->SetDuration(duration_
);
1562 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1563 itr
!= source_state_map_
.end(); ++itr
) {
1564 itr
->second
->OnSetDuration(duration_
);
1568 bool ChunkDemuxer::SetTimestampOffset(const std::string
& id
, TimeDelta offset
) {
1569 base::AutoLock
auto_lock(lock_
);
1570 DVLOG(1) << "SetTimestampOffset(" << id
<< ", " << offset
.InSecondsF() << ")";
1571 CHECK(IsValidId(id
));
1573 return source_state_map_
[id
]->SetTimestampOffset(offset
);
1576 bool ChunkDemuxer::SetSequenceMode(const std::string
& id
,
1577 bool sequence_mode
) {
1578 base::AutoLock
auto_lock(lock_
);
1579 DVLOG(1) << "SetSequenceMode(" << id
<< ", " << sequence_mode
<< ")";
1580 CHECK(IsValidId(id
));
1581 DCHECK_NE(state_
, ENDED
);
1583 return source_state_map_
[id
]->SetSequenceMode(sequence_mode
);
1586 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status
) {
1587 DVLOG(1) << "MarkEndOfStream(" << status
<< ")";
1588 base::AutoLock
auto_lock(lock_
);
1589 DCHECK_NE(state_
, WAITING_FOR_INIT
);
1590 DCHECK_NE(state_
, ENDED
);
1592 if (state_
== SHUTDOWN
|| state_
== PARSE_ERROR
)
1595 if (state_
== INITIALIZING
) {
1596 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1600 bool old_waiting_for_data
= IsSeekWaitingForData_Locked();
1601 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1602 itr
!= source_state_map_
.end(); ++itr
) {
1603 itr
->second
->MarkEndOfStream();
1606 CompletePendingReadsIfPossible();
1608 // Give a chance to resume the pending seek process.
1609 if (status
!= PIPELINE_OK
) {
1610 ReportError_Locked(status
);
1614 ChangeState_Locked(ENDED
);
1615 DecreaseDurationIfNecessary();
1617 if (old_waiting_for_data
&& !IsSeekWaitingForData_Locked() &&
1618 !seek_cb_
.is_null()) {
1619 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_OK
);
1623 void ChunkDemuxer::UnmarkEndOfStream() {
1624 DVLOG(1) << "UnmarkEndOfStream()";
1625 base::AutoLock
auto_lock(lock_
);
1626 DCHECK_EQ(state_
, ENDED
);
1628 ChangeState_Locked(INITIALIZED
);
1630 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1631 itr
!= source_state_map_
.end(); ++itr
) {
1632 itr
->second
->UnmarkEndOfStream();
1636 void ChunkDemuxer::SetAppendWindowStart(const std::string
& id
,
1638 base::AutoLock
auto_lock(lock_
);
1639 DVLOG(1) << "SetAppendWindowStart(" << id
<< ", "
1640 << start
.InSecondsF() << ")";
1641 CHECK(IsValidId(id
));
1642 source_state_map_
[id
]->set_append_window_start(start
);
1645 void ChunkDemuxer::SetAppendWindowEnd(const std::string
& id
, TimeDelta end
) {
1646 base::AutoLock
auto_lock(lock_
);
1647 DVLOG(1) << "SetAppendWindowEnd(" << id
<< ", " << end
.InSecondsF() << ")";
1648 CHECK(IsValidId(id
));
1649 source_state_map_
[id
]->set_append_window_end(end
);
1652 void ChunkDemuxer::Shutdown() {
1653 DVLOG(1) << "Shutdown()";
1654 base::AutoLock
auto_lock(lock_
);
1656 if (state_
== SHUTDOWN
)
1659 ShutdownAllStreams();
1661 ChangeState_Locked(SHUTDOWN
);
1663 if(!seek_cb_
.is_null())
1664 base::ResetAndReturn(&seek_cb_
).Run(PIPELINE_ERROR_ABORT
);
1667 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit
) {
1668 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1669 itr
!= source_state_map_
.end(); ++itr
) {
1670 itr
->second
->SetMemoryLimitsForTesting(memory_limit
);
1674 void ChunkDemuxer::ChangeState_Locked(State new_state
) {
1675 lock_
.AssertAcquired();
1676 DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1677 << state_
<< " -> " << new_state
;
1681 ChunkDemuxer::~ChunkDemuxer() {
1682 DCHECK_NE(state_
, INITIALIZED
);
1684 STLDeleteValues(&source_state_map_
);
1687 void ChunkDemuxer::ReportError_Locked(PipelineStatus error
) {
1688 DVLOG(1) << "ReportError_Locked(" << error
<< ")";
1689 lock_
.AssertAcquired();
1690 DCHECK_NE(error
, PIPELINE_OK
);
1692 ChangeState_Locked(PARSE_ERROR
);
1694 PipelineStatusCB cb
;
1696 if (!init_cb_
.is_null()) {
1697 std::swap(cb
, init_cb_
);
1699 if (!seek_cb_
.is_null())
1700 std::swap(cb
, seek_cb_
);
1702 ShutdownAllStreams();
1705 if (!cb
.is_null()) {
1710 base::AutoUnlock
auto_unlock(lock_
);
1711 host_
->OnDemuxerError(error
);
1714 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1715 lock_
.AssertAcquired();
1716 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1717 itr
!= source_state_map_
.end(); ++itr
) {
1718 if (itr
->second
->IsSeekWaitingForData())
1725 void ChunkDemuxer::OnSourceInitDone(bool success
, TimeDelta duration
) {
1726 DVLOG(1) << "OnSourceInitDone(" << success
<< ", "
1727 << duration
.InSecondsF() << ")";
1728 lock_
.AssertAcquired();
1729 DCHECK_EQ(state_
, INITIALIZING
);
1730 if (!success
|| (!audio_
&& !video_
)) {
1731 ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN
);
1735 if (duration
!= TimeDelta() && duration_
== kNoTimestamp())
1736 UpdateDuration(duration
);
1738 // Wait until all streams have initialized.
1739 if ((!source_id_audio_
.empty() && !audio_
) ||
1740 (!source_id_video_
.empty() && !video_
))
1743 SeekAllSources(GetStartTime());
1744 StartReturningData();
1746 if (duration_
== kNoTimestamp())
1747 duration_
= kInfiniteDuration();
1749 // The demuxer is now initialized after the |start_timestamp_| was set.
1750 ChangeState_Locked(INITIALIZED
);
1751 base::ResetAndReturn(&init_cb_
).Run(PIPELINE_OK
);
1755 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type
) {
1757 case DemuxerStream::AUDIO
:
1760 audio_
.reset(new ChunkDemuxerStream(DemuxerStream::AUDIO
));
1761 return audio_
.get();
1763 case DemuxerStream::VIDEO
:
1766 video_
.reset(new ChunkDemuxerStream(DemuxerStream::VIDEO
));
1767 return video_
.get();
1769 case DemuxerStream::TEXT
: {
1770 return new ChunkDemuxerStream(DemuxerStream::TEXT
);
1773 case DemuxerStream::UNKNOWN
:
1774 case DemuxerStream::NUM_TYPES
:
1782 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream
* text_stream
,
1783 const TextTrackConfig
& config
) {
1784 lock_
.AssertAcquired();
1785 DCHECK_NE(state_
, SHUTDOWN
);
1786 host_
->AddTextStream(text_stream
, config
);
1789 bool ChunkDemuxer::IsValidId(const std::string
& source_id
) const {
1790 lock_
.AssertAcquired();
1791 return source_state_map_
.count(source_id
) > 0u;
1794 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration
) {
1795 DCHECK(duration_
!= new_duration
);
1796 user_specified_duration_
= -1;
1797 duration_
= new_duration
;
1798 host_
->SetDuration(new_duration
);
1801 void ChunkDemuxer::IncreaseDurationIfNecessary(
1802 TimeDelta last_appended_buffer_timestamp
,
1803 ChunkDemuxerStream
* stream
) {
1804 DCHECK(last_appended_buffer_timestamp
!= kNoTimestamp());
1805 if (last_appended_buffer_timestamp
<= duration_
)
1808 TimeDelta stream_duration
= stream
->GetBufferedDuration();
1809 DCHECK(stream_duration
> TimeDelta());
1811 if (stream_duration
> duration_
)
1812 UpdateDuration(stream_duration
);
1815 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1816 lock_
.AssertAcquired();
1818 TimeDelta max_duration
;
1820 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1821 itr
!= source_state_map_
.end(); ++itr
) {
1822 max_duration
= std::max(max_duration
,
1823 itr
->second
->GetMaxBufferedDuration());
1826 if (max_duration
== TimeDelta())
1829 if (max_duration
< duration_
)
1830 UpdateDuration(max_duration
);
1833 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges() const {
1834 base::AutoLock
auto_lock(lock_
);
1835 return GetBufferedRanges_Locked();
1838 Ranges
<TimeDelta
> ChunkDemuxer::GetBufferedRanges_Locked() const {
1839 lock_
.AssertAcquired();
1841 bool ended
= state_
== ENDED
;
1842 // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1843 // we'll need to update this loop to only add ranges from active sources.
1844 RangesList ranges_list
;
1845 for (SourceStateMap::const_iterator itr
= source_state_map_
.begin();
1846 itr
!= source_state_map_
.end(); ++itr
) {
1847 ranges_list
.push_back(itr
->second
->GetBufferedRanges(duration_
, ended
));
1850 return ComputeIntersection(ranges_list
, ended
);
1853 void ChunkDemuxer::StartReturningData() {
1854 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1855 itr
!= source_state_map_
.end(); ++itr
) {
1856 itr
->second
->StartReturningData();
1860 void ChunkDemuxer::AbortPendingReads() {
1861 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1862 itr
!= source_state_map_
.end(); ++itr
) {
1863 itr
->second
->AbortReads();
1867 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time
) {
1868 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1869 itr
!= source_state_map_
.end(); ++itr
) {
1870 itr
->second
->Seek(seek_time
);
1874 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1875 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1876 itr
!= source_state_map_
.end(); ++itr
) {
1877 itr
->second
->CompletePendingReadIfPossible();
1881 void ChunkDemuxer::ShutdownAllStreams() {
1882 for (SourceStateMap::iterator itr
= source_state_map_
.begin();
1883 itr
!= source_state_map_
.end(); ++itr
) {
1884 itr
->second
->Shutdown();
1888 } // namespace media