Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / media / filters / chunk_demuxer.cc
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.
4
5 #include "media/filters/chunk_demuxer.h"
6
7 #include <algorithm>
8 #include <deque>
9 #include <limits>
10 #include <list>
11
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"
22
23 using base::TimeDelta;
24
25 namespace media {
26
27 // List of time ranges for each SourceBuffer.
28 typedef std::list<Ranges<TimeDelta> > RangesList;
29 static Ranges<TimeDelta> ComputeIntersection(const RangesList& activeRanges,
30                                              bool ended) {
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
33
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>();
38
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
42   //  ranges.
43   TimeDelta highest_end_time;
44   for (RangesList::const_iterator itr = activeRanges.begin();
45        itr != activeRanges.end(); ++itr) {
46     if (!itr->size())
47       continue;
48
49     highest_end_time = std::max(highest_end_time, itr->end(itr->size() - 1));
50   }
51
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);
56
57   // Step 5: For each SourceBuffer object in activeSourceBuffers run the
58   //  following steps:
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;
64
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),
69                         highest_end_time);
70     }
71
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);
77   }
78
79   return intersection_ranges;
80 }
81
82 // Contains state belonging to a source id.
83 class SourceState {
84  public:
85   // Callback signature used to create ChunkDemuxerStreams.
86   typedef base::Callback<ChunkDemuxerStream*(
87       DemuxerStream::Type)> CreateDemuxerStreamCB;
88
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;
93
94   typedef base::Callback<void(
95       ChunkDemuxerStream*, const TextTrackConfig&)> NewTextTrackCB;
96
97   SourceState(scoped_ptr<StreamParser> stream_parser, const LogCB& log_cb,
98               const CreateDemuxerStreamCB& create_demuxer_stream_cb,
99               const IncreaseDurationCB& increase_duration_cb);
100
101   ~SourceState();
102
103   void Init(const StreamParser::InitCB& init_cb,
104             bool allow_audio,
105             bool allow_video,
106             const StreamParser::NeedKeyCB& need_key_cb,
107             const NewTextTrackCB& new_text_track_cb);
108
109   // Appends new data to the StreamParser.
110   // Returns true if the data was successfully appended. Returns false if an
111   // error occurred.
112   bool Append(const uint8* data, size_t length);
113
114   // Aborts the current append sequence and resets the parser.
115   void Abort();
116
117   // Calls Remove(|start|, |end|, |duration|) on all
118   // ChunkDemuxerStreams managed by this object.
119   void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
120
121   // Sets |timestamp_offset_| if possible.
122   // Returns if the offset was set. Returns false if the offset could not be
123   // updated at this time.
124   bool SetTimestampOffset(TimeDelta timestamp_offset);
125
126   // Sets |sequence_mode_| to |sequence_mode| if possible.
127   // Returns true if the mode update was allowed. Returns false if the mode
128   // could not be updated at this time.
129   bool SetSequenceMode(bool sequence_mode);
130
131   TimeDelta timestamp_offset() const { return timestamp_offset_; }
132
133   void set_append_window_start(TimeDelta start) {
134     append_window_start_ = start;
135   }
136   void set_append_window_end(TimeDelta end) { append_window_end_ = end; }
137
138   // Returns the range of buffered data in this source, capped at |duration|.
139   // |ended| - Set to true if end of stream has been signalled and the special
140   // end of stream range logic needs to be executed.
141   Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration, bool ended) const;
142
143   // Returns the highest buffered duration across all streams managed
144   // by this object.
145   // Returns TimeDelta() if none of the streams contain buffered data.
146   TimeDelta GetMaxBufferedDuration() const;
147
148   // Helper methods that call methods with similar names on all the
149   // ChunkDemuxerStreams managed by this object.
150   void StartReturningData();
151   void AbortReads();
152   void Seek(TimeDelta seek_time);
153   void CompletePendingReadIfPossible();
154   void OnSetDuration(TimeDelta duration);
155   void MarkEndOfStream();
156   void UnmarkEndOfStream();
157   void Shutdown();
158   // Sets the memory limit on each stream. |memory_limit| is the
159   // maximum number of bytes each stream is allowed to hold in its buffer.
160   void SetMemoryLimitsForTesting(int memory_limit);
161   bool IsSeekWaitingForData() const;
162
163  private:
164   // Called by the |stream_parser_| when a new initialization segment is
165   // encountered.
166   // Returns true on a successful call. Returns false if an error occurred while
167   // processing decoder configurations.
168   bool OnNewConfigs(bool allow_audio, bool allow_video,
169                     const AudioDecoderConfig& audio_config,
170                     const VideoDecoderConfig& video_config,
171                     const StreamParser::TextTrackConfigMap& text_configs);
172
173   // Called by the |stream_parser_| at the beginning of a new media segment.
174   void OnNewMediaSegment();
175
176   // Called by the |stream_parser_| at the end of a media segment.
177   void OnEndOfMediaSegment();
178
179   // Called by the |stream_parser_| when new buffers have been parsed. It
180   // applies |timestamp_offset_| to all buffers in |audio_buffers|,
181   // |video_buffers| and |text_map| and then calls Append() with the modified
182   // buffers on |audio_|, |video_| and/or the text demuxer streams associated
183   // with the track numbers in |text_map|.
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);
189
190   // Helper function for OnNewBuffers() when new text buffers have been parsed.
191   // It applies |timestamp_offset_| to all buffers in |buffers| and then appends
192   // the (modified) buffers to the demuxer stream associated with
193   // the track having |text_track_id|.
194   // Returns true on a successful call. Returns false if an error occurred while
195   // processing the buffers.
196   bool OnTextBuffers(StreamParser::TrackId text_track_id,
197                      const StreamParser::BufferQueue& buffers);
198
199   // Helper function that appends |buffers| to |stream| and calls
200   // |increase_duration_cb_| to potentially update the duration.
201   // Returns true if the append was successful. Returns false if
202   // |stream| is NULL or something in |buffers| caused the append to fail.
203   bool AppendAndUpdateDuration(ChunkDemuxerStream* stream,
204                                const StreamParser::BufferQueue& buffers);
205
206   // Helper function that adds |timestamp_offset_| to each buffer in |buffers|.
207   void AdjustBufferTimestamps(const StreamParser::BufferQueue& buffers);
208
209   // Filters out buffers that are outside of the append window
210   // [|append_window_start_|, |append_window_end_|).
211   // |needs_keyframe| is a pointer to the |xxx_need_keyframe_| flag
212   // associated with the |buffers|. Its state is read an updated as
213   // this method filters |buffers|.
214   // Buffers that are inside the append window are appended to the end
215   // of |filtered_buffers|.
216   void FilterWithAppendWindow(const StreamParser::BufferQueue& buffers,
217                               bool* needs_keyframe,
218                               StreamParser::BufferQueue* filtered_buffers);
219
220   CreateDemuxerStreamCB create_demuxer_stream_cb_;
221   IncreaseDurationCB increase_duration_cb_;
222   NewTextTrackCB new_text_track_cb_;
223
224   // The offset to apply to media segment timestamps.
225   TimeDelta timestamp_offset_;
226
227   // Tracks the mode by which appended media is processed. If true, then
228   // appended media is processed using "sequence" mode. Otherwise, appended
229   // media is processed using "segments" mode.
230   // TODO(wolenetz): Enable "sequence" mode logic. See http://crbug.com/249422
231   // and http://crbug.com/333437.
232   bool sequence_mode_;
233
234   TimeDelta append_window_start_;
235   TimeDelta append_window_end_;
236
237   // Set to true if the next buffers appended within the append window
238   // represent the start of a new media segment. This flag being set
239   // triggers a call to |new_segment_cb_| when the new buffers are
240   // appended. The flag is set on actual media segment boundaries and
241   // when the "append window" filtering causes discontinuities in the
242   // appended data.
243   bool new_media_segment_;
244
245   // Keeps track of whether |timestamp_offset_| or |sequence_mode_| can be
246   // updated. These cannot be updated if a media segment is being parsed.
247   bool parsing_media_segment_;
248
249   // The object used to parse appended data.
250   scoped_ptr<StreamParser> stream_parser_;
251
252   ChunkDemuxerStream* audio_;
253   bool audio_needs_keyframe_;
254
255   ChunkDemuxerStream* video_;
256   bool video_needs_keyframe_;
257
258   typedef std::map<StreamParser::TrackId, ChunkDemuxerStream*> TextStreamMap;
259   TextStreamMap text_stream_map_;
260
261   LogCB log_cb_;
262
263   DISALLOW_COPY_AND_ASSIGN(SourceState);
264 };
265
266 class ChunkDemuxerStream : public DemuxerStream {
267  public:
268   typedef std::deque<scoped_refptr<StreamParserBuffer> > BufferQueue;
269
270   explicit ChunkDemuxerStream(Type type);
271   virtual ~ChunkDemuxerStream();
272
273   // ChunkDemuxerStream control methods.
274   void StartReturningData();
275   void AbortReads();
276   void CompletePendingReadIfPossible();
277   void Shutdown();
278
279   // SourceBufferStream manipulation methods.
280   void Seek(TimeDelta time);
281   bool IsSeekWaitingForData() const;
282
283   // Add buffers to this stream.  Buffers are stored in SourceBufferStreams,
284   // which handle ordering and overlap resolution.
285   // Returns true if buffers were successfully added.
286   bool Append(const StreamParser::BufferQueue& buffers);
287
288   // Removes buffers between |start| and |end| according to the steps
289   // in the "Coded Frame Removal Algorithm" in the Media Source
290   // Extensions Spec.
291   // https://dvcs.w3.org/hg/html-media/raw-file/default/media-source/media-source.html#sourcebuffer-coded-frame-removal
292   //
293   // |duration| is the current duration of the presentation. It is
294   // required by the computation outlined in the spec.
295   void Remove(TimeDelta start, TimeDelta end, TimeDelta duration);
296
297   // Signal to the stream that duration has changed to |duration|.
298   void OnSetDuration(TimeDelta duration);
299
300   // Returns the range of buffered data in this stream, capped at |duration|.
301   Ranges<TimeDelta> GetBufferedRanges(TimeDelta duration) const;
302
303   // Returns the duration of the buffered data.
304   // Returns TimeDelta() if the stream has no buffered data.
305   TimeDelta GetBufferedDuration() const;
306
307   // Signal to the stream that buffers handed in through subsequent calls to
308   // Append() belong to a media segment that starts at |start_timestamp|.
309   void OnNewMediaSegment(TimeDelta start_timestamp);
310
311   // Called when midstream config updates occur.
312   // Returns true if the new config is accepted.
313   // Returns false if the new config should trigger an error.
314   bool UpdateAudioConfig(const AudioDecoderConfig& config, const LogCB& log_cb);
315   bool UpdateVideoConfig(const VideoDecoderConfig& config, const LogCB& log_cb);
316   void UpdateTextConfig(const TextTrackConfig& config, const LogCB& log_cb);
317
318   void MarkEndOfStream();
319   void UnmarkEndOfStream();
320
321   // DemuxerStream methods.
322   virtual void Read(const ReadCB& read_cb) OVERRIDE;
323   virtual Type type() OVERRIDE;
324   virtual void EnableBitstreamConverter() OVERRIDE;
325   virtual AudioDecoderConfig audio_decoder_config() OVERRIDE;
326   virtual VideoDecoderConfig video_decoder_config() OVERRIDE;
327
328   // Returns the text track configuration.  It is an error to call this method
329   // if type() != TEXT.
330   TextTrackConfig text_track_config();
331
332   // Sets the memory limit, in bytes, on the SourceBufferStream.
333   void set_memory_limit_for_testing(int memory_limit) {
334     stream_->set_memory_limit_for_testing(memory_limit);
335   }
336
337  private:
338   enum State {
339     UNINITIALIZED,
340     RETURNING_DATA_FOR_READS,
341     RETURNING_ABORT_FOR_READS,
342     SHUTDOWN,
343   };
344
345   // Assigns |state_| to |state|
346   void ChangeState_Locked(State state);
347
348   void CompletePendingReadIfPossible_Locked();
349
350   // Specifies the type of the stream.
351   Type type_;
352
353   scoped_ptr<SourceBufferStream> stream_;
354
355   mutable base::Lock lock_;
356   State state_;
357   ReadCB read_cb_;
358
359   DISALLOW_IMPLICIT_CONSTRUCTORS(ChunkDemuxerStream);
360 };
361
362 SourceState::SourceState(scoped_ptr<StreamParser> stream_parser,
363                          const LogCB& log_cb,
364                          const CreateDemuxerStreamCB& create_demuxer_stream_cb,
365                          const IncreaseDurationCB& increase_duration_cb)
366     : create_demuxer_stream_cb_(create_demuxer_stream_cb),
367       increase_duration_cb_(increase_duration_cb),
368       sequence_mode_(false),
369       append_window_end_(kInfiniteDuration()),
370       new_media_segment_(false),
371       parsing_media_segment_(false),
372       stream_parser_(stream_parser.release()),
373       audio_(NULL),
374       audio_needs_keyframe_(true),
375       video_(NULL),
376       video_needs_keyframe_(true),
377       log_cb_(log_cb) {
378   DCHECK(!create_demuxer_stream_cb_.is_null());
379   DCHECK(!increase_duration_cb_.is_null());
380 }
381
382 SourceState::~SourceState() {
383   Shutdown();
384
385   STLDeleteValues(&text_stream_map_);
386 }
387
388 void SourceState::Init(const StreamParser::InitCB& init_cb,
389                        bool allow_audio,
390                        bool allow_video,
391                        const StreamParser::NeedKeyCB& need_key_cb,
392                        const NewTextTrackCB& new_text_track_cb) {
393   new_text_track_cb_ = new_text_track_cb;
394
395   stream_parser_->Init(init_cb,
396                        base::Bind(&SourceState::OnNewConfigs,
397                                   base::Unretained(this),
398                                   allow_audio,
399                                   allow_video),
400                        base::Bind(&SourceState::OnNewBuffers,
401                                   base::Unretained(this)),
402                        new_text_track_cb_.is_null(),
403                        need_key_cb,
404                        base::Bind(&SourceState::OnNewMediaSegment,
405                                   base::Unretained(this)),
406                        base::Bind(&SourceState::OnEndOfMediaSegment,
407                                   base::Unretained(this)),
408                        log_cb_);
409 }
410
411 bool SourceState::SetTimestampOffset(TimeDelta timestamp_offset) {
412   if (parsing_media_segment_)
413     return false;
414
415   timestamp_offset_ = timestamp_offset;
416   return true;
417 }
418
419 bool SourceState::SetSequenceMode(bool sequence_mode) {
420   if (parsing_media_segment_)
421     return false;
422
423   sequence_mode_ = sequence_mode;
424   return true;
425 }
426
427
428 bool SourceState::Append(const uint8* data, size_t length) {
429   return stream_parser_->Parse(data, length);
430 }
431
432 void SourceState::Abort() {
433   stream_parser_->Flush();
434   audio_needs_keyframe_ = true;
435   video_needs_keyframe_ = true;
436   parsing_media_segment_ = false;
437 }
438
439 void SourceState::Remove(TimeDelta start, TimeDelta end, TimeDelta duration) {
440   if (audio_)
441     audio_->Remove(start, end, duration);
442
443   if (video_)
444     video_->Remove(start, end, duration);
445
446   for (TextStreamMap::iterator itr = text_stream_map_.begin();
447        itr != text_stream_map_.end(); ++itr) {
448     itr->second->Remove(start, end, duration);
449   }
450 }
451
452 Ranges<TimeDelta> SourceState::GetBufferedRanges(TimeDelta duration,
453                                                  bool ended) const {
454   // TODO(acolwell): When we start allowing disabled tracks we'll need to update
455   // this code to only add ranges from active tracks.
456   RangesList ranges_list;
457   if (audio_)
458     ranges_list.push_back(audio_->GetBufferedRanges(duration));
459
460   if (video_)
461     ranges_list.push_back(video_->GetBufferedRanges(duration));
462
463   for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
464        itr != text_stream_map_.end(); ++itr) {
465     ranges_list.push_back(itr->second->GetBufferedRanges(duration));
466   }
467
468   return ComputeIntersection(ranges_list, ended);
469 }
470
471 TimeDelta SourceState::GetMaxBufferedDuration() const {
472   TimeDelta max_duration;
473
474   if (audio_)
475     max_duration = std::max(max_duration, audio_->GetBufferedDuration());
476
477   if (video_)
478     max_duration = std::max(max_duration, video_->GetBufferedDuration());
479
480   for (TextStreamMap::const_iterator itr = text_stream_map_.begin();
481        itr != text_stream_map_.end(); ++itr) {
482     max_duration = std::max(max_duration, itr->second->GetBufferedDuration());
483   }
484
485   return max_duration;
486 }
487
488 void SourceState::StartReturningData() {
489   if (audio_)
490     audio_->StartReturningData();
491
492   if (video_)
493     video_->StartReturningData();
494
495   for (TextStreamMap::iterator itr = text_stream_map_.begin();
496        itr != text_stream_map_.end(); ++itr) {
497     itr->second->StartReturningData();
498   }
499 }
500
501 void SourceState::AbortReads() {
502   if (audio_)
503     audio_->AbortReads();
504
505   if (video_)
506     video_->AbortReads();
507
508   for (TextStreamMap::iterator itr = text_stream_map_.begin();
509        itr != text_stream_map_.end(); ++itr) {
510     itr->second->AbortReads();
511   }
512 }
513
514 void SourceState::Seek(TimeDelta seek_time) {
515   if (audio_)
516     audio_->Seek(seek_time);
517
518   if (video_)
519     video_->Seek(seek_time);
520
521   for (TextStreamMap::iterator itr = text_stream_map_.begin();
522        itr != text_stream_map_.end(); ++itr) {
523     itr->second->Seek(seek_time);
524   }
525 }
526
527 void SourceState::CompletePendingReadIfPossible() {
528   if (audio_)
529     audio_->CompletePendingReadIfPossible();
530
531   if (video_)
532     video_->CompletePendingReadIfPossible();
533
534   for (TextStreamMap::iterator itr = text_stream_map_.begin();
535        itr != text_stream_map_.end(); ++itr) {
536     itr->second->CompletePendingReadIfPossible();
537   }
538 }
539
540 void SourceState::OnSetDuration(TimeDelta duration) {
541   if (audio_)
542     audio_->OnSetDuration(duration);
543
544   if (video_)
545     video_->OnSetDuration(duration);
546
547   for (TextStreamMap::iterator itr = text_stream_map_.begin();
548        itr != text_stream_map_.end(); ++itr) {
549     itr->second->OnSetDuration(duration);
550   }
551 }
552
553 void SourceState::MarkEndOfStream() {
554   if (audio_)
555     audio_->MarkEndOfStream();
556
557   if (video_)
558     video_->MarkEndOfStream();
559
560   for (TextStreamMap::iterator itr = text_stream_map_.begin();
561        itr != text_stream_map_.end(); ++itr) {
562     itr->second->MarkEndOfStream();
563   }
564 }
565
566 void SourceState::UnmarkEndOfStream() {
567   if (audio_)
568     audio_->UnmarkEndOfStream();
569
570   if (video_)
571     video_->UnmarkEndOfStream();
572
573   for (TextStreamMap::iterator itr = text_stream_map_.begin();
574        itr != text_stream_map_.end(); ++itr) {
575     itr->second->UnmarkEndOfStream();
576   }
577 }
578
579 void SourceState::Shutdown() {
580   if (audio_)
581     audio_->Shutdown();
582
583   if (video_)
584     video_->Shutdown();
585
586   for (TextStreamMap::iterator itr = text_stream_map_.begin();
587        itr != text_stream_map_.end(); ++itr) {
588     itr->second->Shutdown();
589   }
590 }
591
592 void SourceState::SetMemoryLimitsForTesting(int memory_limit) {
593   if (audio_)
594     audio_->set_memory_limit_for_testing(memory_limit);
595
596   if (video_)
597     video_->set_memory_limit_for_testing(memory_limit);
598
599   for (TextStreamMap::iterator itr = text_stream_map_.begin();
600        itr != text_stream_map_.end(); ++itr) {
601     itr->second->set_memory_limit_for_testing(memory_limit);
602   }
603 }
604
605 bool SourceState::IsSeekWaitingForData() const {
606   if (audio_ && audio_->IsSeekWaitingForData())
607     return true;
608
609   if (video_ && video_->IsSeekWaitingForData())
610     return true;
611
612   // NOTE: We are intentionally not checking the text tracks
613   // because text tracks are discontinuous and may not have data
614   // for the seek position. This is ok and playback should not be
615   // stalled because we don't have cues. If cues, with timestamps after
616   // the seek time, eventually arrive they will be delivered properly
617   // in response to ChunkDemuxerStream::Read() calls.
618
619   return false;
620 }
621
622 void SourceState::AdjustBufferTimestamps(
623     const StreamParser::BufferQueue& buffers) {
624   if (timestamp_offset_ == TimeDelta())
625     return;
626
627   for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
628        itr != buffers.end(); ++itr) {
629     (*itr)->SetDecodeTimestamp(
630         (*itr)->GetDecodeTimestamp() + timestamp_offset_);
631     (*itr)->set_timestamp((*itr)->timestamp() + timestamp_offset_);
632   }
633 }
634
635 bool SourceState::OnNewConfigs(
636     bool allow_audio, bool allow_video,
637     const AudioDecoderConfig& audio_config,
638     const VideoDecoderConfig& video_config,
639     const StreamParser::TextTrackConfigMap& text_configs) {
640   DVLOG(1) << "OnNewConfigs(" << allow_audio << ", " << allow_video
641            << ", " << audio_config.IsValidConfig()
642            << ", " << video_config.IsValidConfig() << ")";
643
644   if (!audio_config.IsValidConfig() && !video_config.IsValidConfig()) {
645     DVLOG(1) << "OnNewConfigs() : Audio & video config are not valid!";
646     return false;
647   }
648
649   // Signal an error if we get configuration info for stream types that weren't
650   // specified in AddId() or more configs after a stream is initialized.
651   if (allow_audio != audio_config.IsValidConfig()) {
652     MEDIA_LOG(log_cb_)
653         << "Initialization segment"
654         << (audio_config.IsValidConfig() ? " has" : " does not have")
655         << " an audio track, but the mimetype"
656         << (allow_audio ? " specifies" : " does not specify")
657         << " an audio codec.";
658     return false;
659   }
660
661   if (allow_video != video_config.IsValidConfig()) {
662     MEDIA_LOG(log_cb_)
663         << "Initialization segment"
664         << (video_config.IsValidConfig() ? " has" : " does not have")
665         << " a video track, but the mimetype"
666         << (allow_video ? " specifies" : " does not specify")
667         << " a video codec.";
668     return false;
669   }
670
671   bool success = true;
672   if (audio_config.IsValidConfig()) {
673     if (!audio_) {
674       audio_ = create_demuxer_stream_cb_.Run(DemuxerStream::AUDIO);
675
676       if (!audio_) {
677         DVLOG(1) << "Failed to create an audio stream.";
678         return false;
679       }
680     }
681
682     success &= audio_->UpdateAudioConfig(audio_config, log_cb_);
683   }
684
685   if (video_config.IsValidConfig()) {
686     if (!video_) {
687       video_ = create_demuxer_stream_cb_.Run(DemuxerStream::VIDEO);
688
689       if (!video_) {
690         DVLOG(1) << "Failed to create a video stream.";
691         return false;
692       }
693     }
694
695     success &= video_->UpdateVideoConfig(video_config, log_cb_);
696   }
697
698   typedef StreamParser::TextTrackConfigMap::const_iterator TextConfigItr;
699   if (text_stream_map_.empty()) {
700     for (TextConfigItr itr = text_configs.begin();
701          itr != text_configs.end(); ++itr) {
702       ChunkDemuxerStream* const text_stream =
703           create_demuxer_stream_cb_.Run(DemuxerStream::TEXT);
704       text_stream->UpdateTextConfig(itr->second, log_cb_);
705       text_stream_map_[itr->first] = text_stream;
706       new_text_track_cb_.Run(text_stream, itr->second);
707     }
708   } else {
709     const size_t text_count = text_stream_map_.size();
710     if (text_configs.size() != text_count) {
711       success &= false;
712       MEDIA_LOG(log_cb_) << "The number of text track configs changed.";
713     } else if (text_count == 1) {
714       TextConfigItr config_itr = text_configs.begin();
715       const TextTrackConfig& new_config = config_itr->second;
716       TextStreamMap::iterator stream_itr = text_stream_map_.begin();
717       ChunkDemuxerStream* text_stream = stream_itr->second;
718       TextTrackConfig old_config = text_stream->text_track_config();
719       if (!new_config.Matches(old_config)) {
720         success &= false;
721         MEDIA_LOG(log_cb_) << "New text track config does not match old one.";
722       } else {
723         text_stream_map_.clear();
724         text_stream_map_[config_itr->first] = text_stream;
725       }
726     } else {
727       for (TextConfigItr config_itr = text_configs.begin();
728            config_itr != text_configs.end(); ++config_itr) {
729         TextStreamMap::iterator stream_itr =
730             text_stream_map_.find(config_itr->first);
731         if (stream_itr == text_stream_map_.end()) {
732           success &= false;
733           MEDIA_LOG(log_cb_) << "Unexpected text track configuration "
734                                 "for track ID "
735                              << config_itr->first;
736           break;
737         }
738
739         const TextTrackConfig& new_config = config_itr->second;
740         ChunkDemuxerStream* stream = stream_itr->second;
741         TextTrackConfig old_config = stream->text_track_config();
742         if (!new_config.Matches(old_config)) {
743           success &= false;
744           MEDIA_LOG(log_cb_) << "New text track config for track ID "
745                              << config_itr->first
746                              << " does not match old one.";
747           break;
748         }
749       }
750     }
751   }
752
753   DVLOG(1) << "OnNewConfigs() : " << (success ? "success" : "failed");
754   return success;
755 }
756
757 void SourceState::OnNewMediaSegment() {
758   DVLOG(2) << "OnNewMediaSegment()";
759   parsing_media_segment_ = true;
760   new_media_segment_ = true;
761 }
762
763 void SourceState::OnEndOfMediaSegment() {
764   DVLOG(2) << "OnEndOfMediaSegment()";
765   parsing_media_segment_ = false;
766   new_media_segment_ = false;
767 }
768
769 bool SourceState::OnNewBuffers(
770     const StreamParser::BufferQueue& audio_buffers,
771     const StreamParser::BufferQueue& video_buffers,
772     const StreamParser::TextBufferQueueMap& text_map) {
773   DCHECK(!audio_buffers.empty() || !video_buffers.empty() ||
774          !text_map.empty());
775
776   // TODO(wolenetz): DCHECK + return false if any of these buffers have UNKNOWN
777   // type() in upcoming coded frame processing compliant implementation. See
778   // http://crbug.com/249422.
779
780   AdjustBufferTimestamps(audio_buffers);
781   AdjustBufferTimestamps(video_buffers);
782
783   StreamParser::BufferQueue filtered_audio;
784   StreamParser::BufferQueue filtered_video;
785
786   FilterWithAppendWindow(audio_buffers, &audio_needs_keyframe_,
787                          &filtered_audio);
788
789   FilterWithAppendWindow(video_buffers, &video_needs_keyframe_,
790                          &filtered_video);
791
792   if ((!filtered_audio.empty() || !filtered_video.empty()) &&
793       new_media_segment_) {
794     // Find the earliest timestamp in the filtered buffers and use that for the
795     // segment start timestamp.
796     TimeDelta segment_timestamp = kNoTimestamp();
797
798     if (!filtered_audio.empty())
799       segment_timestamp = filtered_audio.front()->GetDecodeTimestamp();
800
801     if (!filtered_video.empty() &&
802         (segment_timestamp == kNoTimestamp() ||
803          filtered_video.front()->GetDecodeTimestamp() < segment_timestamp)) {
804       segment_timestamp = filtered_video.front()->GetDecodeTimestamp();
805     }
806
807     new_media_segment_ = false;
808
809     if (audio_)
810       audio_->OnNewMediaSegment(segment_timestamp);
811
812     if (video_)
813       video_->OnNewMediaSegment(segment_timestamp);
814
815     for (TextStreamMap::iterator itr = text_stream_map_.begin();
816          itr != text_stream_map_.end(); ++itr) {
817       itr->second->OnNewMediaSegment(segment_timestamp);
818     }
819   }
820
821   if (!filtered_audio.empty() &&
822       !AppendAndUpdateDuration(audio_, filtered_audio)) {
823     return false;
824   }
825
826   if (!filtered_video.empty() &&
827       !AppendAndUpdateDuration(video_, filtered_video)) {
828     return false;
829   }
830
831   if (text_map.empty())
832     return true;
833
834   // Process any buffers for each of the text tracks in the map.
835   bool all_text_buffers_empty = true;
836   for (StreamParser::TextBufferQueueMap::const_iterator itr = text_map.begin();
837        itr != text_map.end();
838        ++itr) {
839     const StreamParser::BufferQueue text_buffers = itr->second;
840     if (!text_buffers.empty()) {
841       all_text_buffers_empty = false;
842       if (!OnTextBuffers(itr->first, text_buffers))
843         return false;
844     }
845   }
846
847   DCHECK(!all_text_buffers_empty);
848   return true;
849 }
850
851 bool SourceState::OnTextBuffers(
852     StreamParser::TrackId text_track_id,
853     const StreamParser::BufferQueue& buffers) {
854   DCHECK(!buffers.empty());
855
856   TextStreamMap::iterator itr = text_stream_map_.find(text_track_id);
857   if (itr == text_stream_map_.end())
858     return false;
859
860   AdjustBufferTimestamps(buffers);
861
862   StreamParser::BufferQueue filtered_buffers;
863   bool needs_keyframe = false;
864   FilterWithAppendWindow(buffers, &needs_keyframe, &filtered_buffers);
865
866   if (filtered_buffers.empty())
867     return true;
868
869   return AppendAndUpdateDuration(itr->second, filtered_buffers);
870 }
871
872 bool SourceState::AppendAndUpdateDuration(
873     ChunkDemuxerStream* stream,
874     const StreamParser::BufferQueue& buffers) {
875   DCHECK(!buffers.empty());
876
877   if (!stream || !stream->Append(buffers))
878     return false;
879
880   increase_duration_cb_.Run(buffers.back()->timestamp(), stream);
881   return true;
882 }
883
884 void SourceState::FilterWithAppendWindow(
885     const StreamParser::BufferQueue& buffers, bool* needs_keyframe,
886     StreamParser::BufferQueue* filtered_buffers) {
887   DCHECK(needs_keyframe);
888   DCHECK(filtered_buffers);
889
890   // This loop implements steps 1.9, 1.10, & 1.11 of the "Coded frame
891   // processing loop" in the Media Source Extensions spec.
892   // These steps filter out buffers that are not within the "append
893   // window" and handles resyncing on the next random access point
894   // (i.e., next keyframe) if a buffer gets dropped.
895   for (StreamParser::BufferQueue::const_iterator itr = buffers.begin();
896        itr != buffers.end(); ++itr) {
897     // Filter out buffers that are outside the append window. Anytime
898     // a buffer gets dropped we need to set |*needs_keyframe| to true
899     // because we can only resume decoding at keyframes.
900     TimeDelta presentation_timestamp = (*itr)->timestamp();
901
902     // TODO(acolwell): Change |frame_end_timestamp| value to
903     // |presentation_timestamp + (*itr)->duration()|, like the spec
904     // requires, once frame durations are actually present in all buffers.
905     TimeDelta frame_end_timestamp = presentation_timestamp;
906     if (presentation_timestamp < append_window_start_ ||
907         frame_end_timestamp > append_window_end_) {
908       DVLOG(1) << "Dropping buffer outside append window."
909                << " presentation_timestamp "
910                << presentation_timestamp.InSecondsF();
911       *needs_keyframe = true;
912
913       // This triggers a discontinuity so we need to treat the next frames
914       // appended within the append window as if they were the beginning of a
915       // new segment.
916       new_media_segment_ = true;
917       continue;
918     }
919
920     // If |*needs_keyframe| is true then filter out buffers until we
921     // encounter the next keyframe.
922     if (*needs_keyframe) {
923       if (!(*itr)->IsKeyframe()) {
924         DVLOG(1) << "Dropping non-keyframe. presentation_timestamp "
925                  << presentation_timestamp.InSecondsF();
926         continue;
927       }
928
929       *needs_keyframe = false;
930     }
931
932     filtered_buffers->push_back(*itr);
933   }
934 }
935
936 ChunkDemuxerStream::ChunkDemuxerStream(Type type)
937     : type_(type),
938       state_(UNINITIALIZED) {
939 }
940
941 void ChunkDemuxerStream::StartReturningData() {
942   DVLOG(1) << "ChunkDemuxerStream::StartReturningData()";
943   base::AutoLock auto_lock(lock_);
944   DCHECK(read_cb_.is_null());
945   ChangeState_Locked(RETURNING_DATA_FOR_READS);
946 }
947
948 void ChunkDemuxerStream::AbortReads() {
949   DVLOG(1) << "ChunkDemuxerStream::AbortReads()";
950   base::AutoLock auto_lock(lock_);
951   ChangeState_Locked(RETURNING_ABORT_FOR_READS);
952   if (!read_cb_.is_null())
953     base::ResetAndReturn(&read_cb_).Run(kAborted, NULL);
954 }
955
956 void ChunkDemuxerStream::CompletePendingReadIfPossible() {
957   base::AutoLock auto_lock(lock_);
958   if (read_cb_.is_null())
959     return;
960
961   CompletePendingReadIfPossible_Locked();
962 }
963
964 void ChunkDemuxerStream::Shutdown() {
965   DVLOG(1) << "ChunkDemuxerStream::Shutdown()";
966   base::AutoLock auto_lock(lock_);
967   ChangeState_Locked(SHUTDOWN);
968
969   // Pass an end of stream buffer to the pending callback to signal that no more
970   // data will be sent.
971   if (!read_cb_.is_null()) {
972     base::ResetAndReturn(&read_cb_).Run(DemuxerStream::kOk,
973                                         StreamParserBuffer::CreateEOSBuffer());
974   }
975 }
976
977 bool ChunkDemuxerStream::IsSeekWaitingForData() const {
978   base::AutoLock auto_lock(lock_);
979
980   // This method should not be called for text tracks. See the note in
981   // SourceState::IsSeekWaitingForData().
982   DCHECK_NE(type_, DemuxerStream::TEXT);
983
984   return stream_->IsSeekPending();
985 }
986
987 void ChunkDemuxerStream::Seek(TimeDelta time) {
988   DVLOG(1) << "ChunkDemuxerStream::Seek(" << time.InSecondsF() << ")";
989   base::AutoLock auto_lock(lock_);
990   DCHECK(read_cb_.is_null());
991   DCHECK(state_ == UNINITIALIZED || state_ == RETURNING_ABORT_FOR_READS)
992       << state_;
993
994   stream_->Seek(time);
995 }
996
997 bool ChunkDemuxerStream::Append(const StreamParser::BufferQueue& buffers) {
998   if (buffers.empty())
999     return false;
1000
1001   base::AutoLock auto_lock(lock_);
1002   DCHECK_NE(state_, SHUTDOWN);
1003   if (!stream_->Append(buffers)) {
1004     DVLOG(1) << "ChunkDemuxerStream::Append() : stream append failed";
1005     return false;
1006   }
1007
1008   if (!read_cb_.is_null())
1009     CompletePendingReadIfPossible_Locked();
1010
1011   return true;
1012 }
1013
1014 void ChunkDemuxerStream::Remove(TimeDelta start, TimeDelta end,
1015                                 TimeDelta duration) {
1016   base::AutoLock auto_lock(lock_);
1017   stream_->Remove(start, end, duration);
1018 }
1019
1020 void ChunkDemuxerStream::OnSetDuration(TimeDelta duration) {
1021   base::AutoLock auto_lock(lock_);
1022   stream_->OnSetDuration(duration);
1023 }
1024
1025 Ranges<TimeDelta> ChunkDemuxerStream::GetBufferedRanges(
1026     TimeDelta duration) const {
1027   base::AutoLock auto_lock(lock_);
1028
1029   if (type_ == TEXT) {
1030     // Since text tracks are discontinuous and the lack of cues should not block
1031     // playback, report the buffered range for text tracks as [0, |duration|) so
1032     // that intesections with audio & video tracks are computed correctly when
1033     // no cues are present.
1034     Ranges<TimeDelta> text_range;
1035     text_range.Add(TimeDelta(), duration);
1036     return text_range;
1037   }
1038
1039   Ranges<TimeDelta> range = stream_->GetBufferedTime();
1040
1041   if (range.size() == 0u)
1042     return range;
1043
1044   // Clamp the end of the stream's buffered ranges to fit within the duration.
1045   // This can be done by intersecting the stream's range with the valid time
1046   // range.
1047   Ranges<TimeDelta> valid_time_range;
1048   valid_time_range.Add(range.start(0), duration);
1049   return range.IntersectionWith(valid_time_range);
1050 }
1051
1052 TimeDelta ChunkDemuxerStream::GetBufferedDuration() const {
1053   return stream_->GetBufferedDuration();
1054 }
1055
1056 void ChunkDemuxerStream::OnNewMediaSegment(TimeDelta start_timestamp) {
1057   DVLOG(2) << "ChunkDemuxerStream::OnNewMediaSegment("
1058            << start_timestamp.InSecondsF() << ")";
1059   base::AutoLock auto_lock(lock_);
1060   stream_->OnNewMediaSegment(start_timestamp);
1061 }
1062
1063 bool ChunkDemuxerStream::UpdateAudioConfig(const AudioDecoderConfig& config,
1064                                            const LogCB& log_cb) {
1065   DCHECK(config.IsValidConfig());
1066   DCHECK_EQ(type_, AUDIO);
1067   base::AutoLock auto_lock(lock_);
1068   if (!stream_) {
1069     DCHECK_EQ(state_, UNINITIALIZED);
1070     stream_.reset(new SourceBufferStream(config, log_cb));
1071     return true;
1072   }
1073
1074   return stream_->UpdateAudioConfig(config);
1075 }
1076
1077 bool ChunkDemuxerStream::UpdateVideoConfig(const VideoDecoderConfig& config,
1078                                            const LogCB& log_cb) {
1079   DCHECK(config.IsValidConfig());
1080   DCHECK_EQ(type_, VIDEO);
1081   base::AutoLock auto_lock(lock_);
1082
1083   if (!stream_) {
1084     DCHECK_EQ(state_, UNINITIALIZED);
1085     stream_.reset(new SourceBufferStream(config, log_cb));
1086     return true;
1087   }
1088
1089   return stream_->UpdateVideoConfig(config);
1090 }
1091
1092 void ChunkDemuxerStream::UpdateTextConfig(const TextTrackConfig& config,
1093                                           const LogCB& log_cb) {
1094   DCHECK_EQ(type_, TEXT);
1095   base::AutoLock auto_lock(lock_);
1096   DCHECK(!stream_);
1097   DCHECK_EQ(state_, UNINITIALIZED);
1098   stream_.reset(new SourceBufferStream(config, log_cb));
1099 }
1100
1101 void ChunkDemuxerStream::MarkEndOfStream() {
1102   base::AutoLock auto_lock(lock_);
1103   stream_->MarkEndOfStream();
1104 }
1105
1106 void ChunkDemuxerStream::UnmarkEndOfStream() {
1107   base::AutoLock auto_lock(lock_);
1108   stream_->UnmarkEndOfStream();
1109 }
1110
1111 // DemuxerStream methods.
1112 void ChunkDemuxerStream::Read(const ReadCB& read_cb) {
1113   base::AutoLock auto_lock(lock_);
1114   DCHECK_NE(state_, UNINITIALIZED);
1115   DCHECK(read_cb_.is_null());
1116
1117   read_cb_ = BindToCurrentLoop(read_cb);
1118   CompletePendingReadIfPossible_Locked();
1119 }
1120
1121 DemuxerStream::Type ChunkDemuxerStream::type() { return type_; }
1122
1123 void ChunkDemuxerStream::EnableBitstreamConverter() {}
1124
1125 AudioDecoderConfig ChunkDemuxerStream::audio_decoder_config() {
1126   CHECK_EQ(type_, AUDIO);
1127   base::AutoLock auto_lock(lock_);
1128   return stream_->GetCurrentAudioDecoderConfig();
1129 }
1130
1131 VideoDecoderConfig ChunkDemuxerStream::video_decoder_config() {
1132   CHECK_EQ(type_, VIDEO);
1133   base::AutoLock auto_lock(lock_);
1134   return stream_->GetCurrentVideoDecoderConfig();
1135 }
1136
1137 TextTrackConfig ChunkDemuxerStream::text_track_config() {
1138   CHECK_EQ(type_, TEXT);
1139   base::AutoLock auto_lock(lock_);
1140   return stream_->GetCurrentTextTrackConfig();
1141 }
1142
1143 void ChunkDemuxerStream::ChangeState_Locked(State state) {
1144   lock_.AssertAcquired();
1145   DVLOG(1) << "ChunkDemuxerStream::ChangeState_Locked() : "
1146            << "type " << type_
1147            << " - " << state_ << " -> " << state;
1148   state_ = state;
1149 }
1150
1151 ChunkDemuxerStream::~ChunkDemuxerStream() {}
1152
1153 void ChunkDemuxerStream::CompletePendingReadIfPossible_Locked() {
1154   lock_.AssertAcquired();
1155   DCHECK(!read_cb_.is_null());
1156
1157   DemuxerStream::Status status;
1158   scoped_refptr<StreamParserBuffer> buffer;
1159
1160   switch (state_) {
1161     case UNINITIALIZED:
1162       NOTREACHED();
1163       return;
1164     case RETURNING_DATA_FOR_READS:
1165       switch (stream_->GetNextBuffer(&buffer)) {
1166         case SourceBufferStream::kSuccess:
1167           status = DemuxerStream::kOk;
1168           break;
1169         case SourceBufferStream::kNeedBuffer:
1170           // Return early without calling |read_cb_| since we don't have
1171           // any data to return yet.
1172           return;
1173         case SourceBufferStream::kEndOfStream:
1174           status = DemuxerStream::kOk;
1175           buffer = StreamParserBuffer::CreateEOSBuffer();
1176           break;
1177         case SourceBufferStream::kConfigChange:
1178           DVLOG(2) << "Config change reported to ChunkDemuxerStream.";
1179           status = kConfigChanged;
1180           buffer = NULL;
1181           break;
1182       }
1183       break;
1184     case RETURNING_ABORT_FOR_READS:
1185       // Null buffers should be returned in this state since we are waiting
1186       // for a seek. Any buffers in the SourceBuffer should NOT be returned
1187       // because they are associated with the seek.
1188       status = DemuxerStream::kAborted;
1189       buffer = NULL;
1190       break;
1191     case SHUTDOWN:
1192       status = DemuxerStream::kOk;
1193       buffer = StreamParserBuffer::CreateEOSBuffer();
1194       break;
1195   }
1196
1197   base::ResetAndReturn(&read_cb_).Run(status, buffer);
1198 }
1199
1200 ChunkDemuxer::ChunkDemuxer(const base::Closure& open_cb,
1201                            const NeedKeyCB& need_key_cb,
1202                            const LogCB& log_cb)
1203     : state_(WAITING_FOR_INIT),
1204       cancel_next_seek_(false),
1205       host_(NULL),
1206       open_cb_(open_cb),
1207       need_key_cb_(need_key_cb),
1208       enable_text_(false),
1209       log_cb_(log_cb),
1210       duration_(kNoTimestamp()),
1211       user_specified_duration_(-1) {
1212   DCHECK(!open_cb_.is_null());
1213   DCHECK(!need_key_cb_.is_null());
1214 }
1215
1216 void ChunkDemuxer::Initialize(
1217     DemuxerHost* host,
1218     const PipelineStatusCB& cb,
1219     bool enable_text_tracks) {
1220   DVLOG(1) << "Init()";
1221
1222   base::AutoLock auto_lock(lock_);
1223
1224   init_cb_ = BindToCurrentLoop(cb);
1225   if (state_ == SHUTDOWN) {
1226     base::ResetAndReturn(&init_cb_).Run(DEMUXER_ERROR_COULD_NOT_OPEN);
1227     return;
1228   }
1229   DCHECK_EQ(state_, WAITING_FOR_INIT);
1230   host_ = host;
1231   enable_text_ = enable_text_tracks;
1232
1233   ChangeState_Locked(INITIALIZING);
1234
1235   base::ResetAndReturn(&open_cb_).Run();
1236 }
1237
1238 void ChunkDemuxer::Stop(const base::Closure& callback) {
1239   DVLOG(1) << "Stop()";
1240   Shutdown();
1241   callback.Run();
1242 }
1243
1244 void ChunkDemuxer::Seek(TimeDelta time, const PipelineStatusCB& cb) {
1245   DVLOG(1) << "Seek(" << time.InSecondsF() << ")";
1246   DCHECK(time >= TimeDelta());
1247
1248   base::AutoLock auto_lock(lock_);
1249   DCHECK(seek_cb_.is_null());
1250
1251   seek_cb_ = BindToCurrentLoop(cb);
1252   if (state_ != INITIALIZED && state_ != ENDED) {
1253     base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_INVALID_STATE);
1254     return;
1255   }
1256
1257   if (cancel_next_seek_) {
1258     cancel_next_seek_ = false;
1259     base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1260     return;
1261   }
1262
1263   SeekAllSources(time);
1264   StartReturningData();
1265
1266   if (IsSeekWaitingForData_Locked()) {
1267     DVLOG(1) << "Seek() : waiting for more data to arrive.";
1268     return;
1269   }
1270
1271   base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1272 }
1273
1274 void ChunkDemuxer::OnAudioRendererDisabled() {
1275   base::AutoLock auto_lock(lock_);
1276   audio_->Shutdown();
1277   disabled_audio_ = audio_.Pass();
1278 }
1279
1280 // Demuxer implementation.
1281 DemuxerStream* ChunkDemuxer::GetStream(DemuxerStream::Type type) {
1282   DCHECK_NE(type, DemuxerStream::TEXT);
1283   base::AutoLock auto_lock(lock_);
1284   if (type == DemuxerStream::VIDEO)
1285     return video_.get();
1286
1287   if (type == DemuxerStream::AUDIO)
1288     return audio_.get();
1289
1290   return NULL;
1291 }
1292
1293 TimeDelta ChunkDemuxer::GetStartTime() const {
1294   return TimeDelta();
1295 }
1296
1297 void ChunkDemuxer::StartWaitingForSeek(TimeDelta seek_time) {
1298   DVLOG(1) << "StartWaitingForSeek()";
1299   base::AutoLock auto_lock(lock_);
1300   DCHECK(state_ == INITIALIZED || state_ == ENDED || state_ == SHUTDOWN ||
1301          state_ == PARSE_ERROR) << state_;
1302   DCHECK(seek_cb_.is_null());
1303
1304   if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1305     return;
1306
1307   AbortPendingReads();
1308   SeekAllSources(seek_time);
1309
1310   // Cancel state set in CancelPendingSeek() since we want to
1311   // accept the next Seek().
1312   cancel_next_seek_ = false;
1313 }
1314
1315 void ChunkDemuxer::CancelPendingSeek(TimeDelta seek_time) {
1316   base::AutoLock auto_lock(lock_);
1317   DCHECK_NE(state_, INITIALIZING);
1318   DCHECK(seek_cb_.is_null() || IsSeekWaitingForData_Locked());
1319
1320   if (cancel_next_seek_)
1321     return;
1322
1323   AbortPendingReads();
1324   SeekAllSources(seek_time);
1325
1326   if (seek_cb_.is_null()) {
1327     cancel_next_seek_ = true;
1328     return;
1329   }
1330
1331   base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1332 }
1333
1334 ChunkDemuxer::Status ChunkDemuxer::AddId(const std::string& id,
1335                                          const std::string& type,
1336                                          std::vector<std::string>& codecs) {
1337   base::AutoLock auto_lock(lock_);
1338
1339   if ((state_ != WAITING_FOR_INIT && state_ != INITIALIZING) || IsValidId(id))
1340     return kReachedIdLimit;
1341
1342   bool has_audio = false;
1343   bool has_video = false;
1344   scoped_ptr<media::StreamParser> stream_parser(
1345       StreamParserFactory::Create(type, codecs, log_cb_,
1346                                   &has_audio, &has_video));
1347
1348   if (!stream_parser)
1349     return ChunkDemuxer::kNotSupported;
1350
1351   if ((has_audio && !source_id_audio_.empty()) ||
1352       (has_video && !source_id_video_.empty()))
1353     return kReachedIdLimit;
1354
1355   if (has_audio)
1356     source_id_audio_ = id;
1357
1358   if (has_video)
1359     source_id_video_ = id;
1360
1361   scoped_ptr<SourceState> source_state(
1362       new SourceState(stream_parser.Pass(), log_cb_,
1363                       base::Bind(&ChunkDemuxer::CreateDemuxerStream,
1364                                  base::Unretained(this)),
1365                       base::Bind(&ChunkDemuxer::IncreaseDurationIfNecessary,
1366                                  base::Unretained(this))));
1367
1368   SourceState::NewTextTrackCB new_text_track_cb;
1369
1370   if (enable_text_) {
1371     new_text_track_cb = base::Bind(&ChunkDemuxer::OnNewTextTrack,
1372                                    base::Unretained(this));
1373   }
1374
1375   source_state->Init(
1376       base::Bind(&ChunkDemuxer::OnSourceInitDone, base::Unretained(this)),
1377       has_audio,
1378       has_video,
1379       need_key_cb_,
1380       new_text_track_cb);
1381
1382   source_state_map_[id] = source_state.release();
1383   return kOk;
1384 }
1385
1386 void ChunkDemuxer::RemoveId(const std::string& id) {
1387   base::AutoLock auto_lock(lock_);
1388   CHECK(IsValidId(id));
1389
1390   delete source_state_map_[id];
1391   source_state_map_.erase(id);
1392
1393   if (source_id_audio_ == id)
1394     source_id_audio_.clear();
1395
1396   if (source_id_video_ == id)
1397     source_id_video_.clear();
1398 }
1399
1400 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges(const std::string& id) const {
1401   base::AutoLock auto_lock(lock_);
1402   DCHECK(!id.empty());
1403
1404   SourceStateMap::const_iterator itr = source_state_map_.find(id);
1405
1406   DCHECK(itr != source_state_map_.end());
1407   return itr->second->GetBufferedRanges(duration_, state_ == ENDED);
1408 }
1409
1410 void ChunkDemuxer::AppendData(const std::string& id,
1411                               const uint8* data,
1412                               size_t length) {
1413   DVLOG(1) << "AppendData(" << id << ", " << length << ")";
1414
1415   DCHECK(!id.empty());
1416
1417   Ranges<TimeDelta> ranges;
1418
1419   {
1420     base::AutoLock auto_lock(lock_);
1421     DCHECK_NE(state_, ENDED);
1422
1423     // Capture if any of the SourceBuffers are waiting for data before we start
1424     // parsing.
1425     bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1426
1427     if (length == 0u)
1428       return;
1429
1430     DCHECK(data);
1431
1432     switch (state_) {
1433       case INITIALIZING:
1434         DCHECK(IsValidId(id));
1435         if (!source_state_map_[id]->Append(data, length)) {
1436           ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1437           return;
1438         }
1439         break;
1440
1441       case INITIALIZED: {
1442         DCHECK(IsValidId(id));
1443         if (!source_state_map_[id]->Append(data, length)) {
1444           ReportError_Locked(PIPELINE_ERROR_DECODE);
1445           return;
1446         }
1447       } break;
1448
1449       case PARSE_ERROR:
1450         DVLOG(1) << "AppendData(): Ignoring data after a parse error.";
1451         return;
1452
1453       case WAITING_FOR_INIT:
1454       case ENDED:
1455       case SHUTDOWN:
1456         DVLOG(1) << "AppendData(): called in unexpected state " << state_;
1457         return;
1458     }
1459
1460     // Check to see if data was appended at the pending seek point. This
1461     // indicates we have parsed enough data to complete the seek.
1462     if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1463         !seek_cb_.is_null()) {
1464       base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1465     }
1466
1467     ranges = GetBufferedRanges_Locked();
1468   }
1469
1470   for (size_t i = 0; i < ranges.size(); ++i)
1471     host_->AddBufferedTimeRange(ranges.start(i), ranges.end(i));
1472 }
1473
1474 void ChunkDemuxer::Abort(const std::string& id) {
1475   DVLOG(1) << "Abort(" << id << ")";
1476   base::AutoLock auto_lock(lock_);
1477   DCHECK(!id.empty());
1478   CHECK(IsValidId(id));
1479   source_state_map_[id]->Abort();
1480 }
1481
1482 void ChunkDemuxer::Remove(const std::string& id, TimeDelta start,
1483                           TimeDelta end) {
1484   DVLOG(1) << "Remove(" << id << ", " << start.InSecondsF()
1485            << ", " << end.InSecondsF() << ")";
1486   base::AutoLock auto_lock(lock_);
1487
1488   DCHECK(!id.empty());
1489   CHECK(IsValidId(id));
1490   source_state_map_[id]->Remove(start, end, duration_);
1491 }
1492
1493 double ChunkDemuxer::GetDuration() {
1494   base::AutoLock auto_lock(lock_);
1495   return GetDuration_Locked();
1496 }
1497
1498 double ChunkDemuxer::GetDuration_Locked() {
1499   lock_.AssertAcquired();
1500   if (duration_ == kNoTimestamp())
1501     return std::numeric_limits<double>::quiet_NaN();
1502
1503   // Return positive infinity if the resource is unbounded.
1504   // http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#dom-media-duration
1505   if (duration_ == kInfiniteDuration())
1506     return std::numeric_limits<double>::infinity();
1507
1508   if (user_specified_duration_ >= 0)
1509     return user_specified_duration_;
1510
1511   return duration_.InSecondsF();
1512 }
1513
1514 void ChunkDemuxer::SetDuration(double duration) {
1515   base::AutoLock auto_lock(lock_);
1516   DVLOG(1) << "SetDuration(" << duration << ")";
1517   DCHECK_GE(duration, 0);
1518
1519   if (duration == GetDuration_Locked())
1520     return;
1521
1522   // Compute & bounds check the TimeDelta representation of duration.
1523   // This can be different if the value of |duration| doesn't fit the range or
1524   // precision of TimeDelta.
1525   TimeDelta min_duration = TimeDelta::FromInternalValue(1);
1526   TimeDelta max_duration = TimeDelta::FromInternalValue(kint64max - 1);
1527   double min_duration_in_seconds = min_duration.InSecondsF();
1528   double max_duration_in_seconds = max_duration.InSecondsF();
1529
1530   TimeDelta duration_td;
1531   if (duration == std::numeric_limits<double>::infinity()) {
1532     duration_td = media::kInfiniteDuration();
1533   } else if (duration < min_duration_in_seconds) {
1534     duration_td = min_duration;
1535   } else if (duration > max_duration_in_seconds) {
1536     duration_td = max_duration;
1537   } else {
1538     duration_td = TimeDelta::FromMicroseconds(
1539         duration * base::Time::kMicrosecondsPerSecond);
1540   }
1541
1542   DCHECK(duration_td > TimeDelta());
1543
1544   user_specified_duration_ = duration;
1545   duration_ = duration_td;
1546   host_->SetDuration(duration_);
1547
1548   for (SourceStateMap::iterator itr = source_state_map_.begin();
1549        itr != source_state_map_.end(); ++itr) {
1550     itr->second->OnSetDuration(duration_);
1551   }
1552 }
1553
1554 bool ChunkDemuxer::SetTimestampOffset(const std::string& id, TimeDelta offset) {
1555   base::AutoLock auto_lock(lock_);
1556   DVLOG(1) << "SetTimestampOffset(" << id << ", " << offset.InSecondsF() << ")";
1557   CHECK(IsValidId(id));
1558
1559   return source_state_map_[id]->SetTimestampOffset(offset);
1560 }
1561
1562 bool ChunkDemuxer::SetSequenceMode(const std::string& id,
1563                                    bool sequence_mode) {
1564   base::AutoLock auto_lock(lock_);
1565   DVLOG(1) << "SetSequenceMode(" << id << ", " << sequence_mode << ")";
1566   CHECK(IsValidId(id));
1567   DCHECK_NE(state_, ENDED);
1568
1569   return source_state_map_[id]->SetSequenceMode(sequence_mode);
1570 }
1571
1572 void ChunkDemuxer::MarkEndOfStream(PipelineStatus status) {
1573   DVLOG(1) << "MarkEndOfStream(" << status << ")";
1574   base::AutoLock auto_lock(lock_);
1575   DCHECK_NE(state_, WAITING_FOR_INIT);
1576   DCHECK_NE(state_, ENDED);
1577
1578   if (state_ == SHUTDOWN || state_ == PARSE_ERROR)
1579     return;
1580
1581   if (state_ == INITIALIZING) {
1582     ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1583     return;
1584   }
1585
1586   bool old_waiting_for_data = IsSeekWaitingForData_Locked();
1587   for (SourceStateMap::iterator itr = source_state_map_.begin();
1588        itr != source_state_map_.end(); ++itr) {
1589     itr->second->MarkEndOfStream();
1590   }
1591
1592   CompletePendingReadsIfPossible();
1593
1594   // Give a chance to resume the pending seek process.
1595   if (status != PIPELINE_OK) {
1596     ReportError_Locked(status);
1597     return;
1598   }
1599
1600   ChangeState_Locked(ENDED);
1601   DecreaseDurationIfNecessary();
1602
1603   if (old_waiting_for_data && !IsSeekWaitingForData_Locked() &&
1604       !seek_cb_.is_null()) {
1605     base::ResetAndReturn(&seek_cb_).Run(PIPELINE_OK);
1606   }
1607 }
1608
1609 void ChunkDemuxer::UnmarkEndOfStream() {
1610   DVLOG(1) << "UnmarkEndOfStream()";
1611   base::AutoLock auto_lock(lock_);
1612   DCHECK_EQ(state_, ENDED);
1613
1614   ChangeState_Locked(INITIALIZED);
1615
1616   for (SourceStateMap::iterator itr = source_state_map_.begin();
1617        itr != source_state_map_.end(); ++itr) {
1618     itr->second->UnmarkEndOfStream();
1619   }
1620 }
1621
1622 void ChunkDemuxer::SetAppendWindowStart(const std::string& id,
1623                                         TimeDelta start) {
1624   base::AutoLock auto_lock(lock_);
1625   DVLOG(1) << "SetAppendWindowStart(" << id << ", "
1626            << start.InSecondsF() << ")";
1627   CHECK(IsValidId(id));
1628   source_state_map_[id]->set_append_window_start(start);
1629 }
1630
1631 void ChunkDemuxer::SetAppendWindowEnd(const std::string& id, TimeDelta end) {
1632   base::AutoLock auto_lock(lock_);
1633   DVLOG(1) << "SetAppendWindowEnd(" << id << ", " << end.InSecondsF() << ")";
1634   CHECK(IsValidId(id));
1635   source_state_map_[id]->set_append_window_end(end);
1636 }
1637
1638 void ChunkDemuxer::Shutdown() {
1639   DVLOG(1) << "Shutdown()";
1640   base::AutoLock auto_lock(lock_);
1641
1642   if (state_ == SHUTDOWN)
1643     return;
1644
1645   ShutdownAllStreams();
1646
1647   ChangeState_Locked(SHUTDOWN);
1648
1649   if(!seek_cb_.is_null())
1650     base::ResetAndReturn(&seek_cb_).Run(PIPELINE_ERROR_ABORT);
1651 }
1652
1653 void ChunkDemuxer::SetMemoryLimitsForTesting(int memory_limit) {
1654   for (SourceStateMap::iterator itr = source_state_map_.begin();
1655        itr != source_state_map_.end(); ++itr) {
1656     itr->second->SetMemoryLimitsForTesting(memory_limit);
1657   }
1658 }
1659
1660 void ChunkDemuxer::ChangeState_Locked(State new_state) {
1661   lock_.AssertAcquired();
1662   DVLOG(1) << "ChunkDemuxer::ChangeState_Locked() : "
1663            << state_ << " -> " << new_state;
1664   state_ = new_state;
1665 }
1666
1667 ChunkDemuxer::~ChunkDemuxer() {
1668   DCHECK_NE(state_, INITIALIZED);
1669
1670   STLDeleteValues(&source_state_map_);
1671 }
1672
1673 void ChunkDemuxer::ReportError_Locked(PipelineStatus error) {
1674   DVLOG(1) << "ReportError_Locked(" << error << ")";
1675   lock_.AssertAcquired();
1676   DCHECK_NE(error, PIPELINE_OK);
1677
1678   ChangeState_Locked(PARSE_ERROR);
1679
1680   PipelineStatusCB cb;
1681
1682   if (!init_cb_.is_null()) {
1683     std::swap(cb, init_cb_);
1684   } else {
1685     if (!seek_cb_.is_null())
1686       std::swap(cb, seek_cb_);
1687
1688     ShutdownAllStreams();
1689   }
1690
1691   if (!cb.is_null()) {
1692     cb.Run(error);
1693     return;
1694   }
1695
1696   base::AutoUnlock auto_unlock(lock_);
1697   host_->OnDemuxerError(error);
1698 }
1699
1700 bool ChunkDemuxer::IsSeekWaitingForData_Locked() const {
1701   lock_.AssertAcquired();
1702   for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1703        itr != source_state_map_.end(); ++itr) {
1704     if (itr->second->IsSeekWaitingForData())
1705       return true;
1706   }
1707
1708   return false;
1709 }
1710
1711 void ChunkDemuxer::OnSourceInitDone(bool success, TimeDelta duration) {
1712   DVLOG(1) << "OnSourceInitDone(" << success << ", "
1713            << duration.InSecondsF() << ")";
1714   lock_.AssertAcquired();
1715   DCHECK_EQ(state_, INITIALIZING);
1716   if (!success || (!audio_ && !video_)) {
1717     ReportError_Locked(DEMUXER_ERROR_COULD_NOT_OPEN);
1718     return;
1719   }
1720
1721   if (duration != TimeDelta() && duration_ == kNoTimestamp())
1722     UpdateDuration(duration);
1723
1724   // Wait until all streams have initialized.
1725   if ((!source_id_audio_.empty() && !audio_) ||
1726       (!source_id_video_.empty() && !video_))
1727     return;
1728
1729   SeekAllSources(GetStartTime());
1730   StartReturningData();
1731
1732   if (duration_ == kNoTimestamp())
1733     duration_ = kInfiniteDuration();
1734
1735   // The demuxer is now initialized after the |start_timestamp_| was set.
1736   ChangeState_Locked(INITIALIZED);
1737   base::ResetAndReturn(&init_cb_).Run(PIPELINE_OK);
1738 }
1739
1740 ChunkDemuxerStream*
1741 ChunkDemuxer::CreateDemuxerStream(DemuxerStream::Type type) {
1742   switch (type) {
1743     case DemuxerStream::AUDIO:
1744       if (audio_)
1745         return NULL;
1746       audio_.reset(new ChunkDemuxerStream(DemuxerStream::AUDIO));
1747       return audio_.get();
1748       break;
1749     case DemuxerStream::VIDEO:
1750       if (video_)
1751         return NULL;
1752       video_.reset(new ChunkDemuxerStream(DemuxerStream::VIDEO));
1753       return video_.get();
1754       break;
1755     case DemuxerStream::TEXT: {
1756       return new ChunkDemuxerStream(DemuxerStream::TEXT);
1757       break;
1758     }
1759     case DemuxerStream::UNKNOWN:
1760     case DemuxerStream::NUM_TYPES:
1761       NOTREACHED();
1762       return NULL;
1763   }
1764   NOTREACHED();
1765   return NULL;
1766 }
1767
1768 void ChunkDemuxer::OnNewTextTrack(ChunkDemuxerStream* text_stream,
1769                                   const TextTrackConfig& config) {
1770   lock_.AssertAcquired();
1771   DCHECK_NE(state_, SHUTDOWN);
1772   host_->AddTextStream(text_stream, config);
1773 }
1774
1775 bool ChunkDemuxer::IsValidId(const std::string& source_id) const {
1776   lock_.AssertAcquired();
1777   return source_state_map_.count(source_id) > 0u;
1778 }
1779
1780 void ChunkDemuxer::UpdateDuration(TimeDelta new_duration) {
1781   DCHECK(duration_ != new_duration);
1782   user_specified_duration_ = -1;
1783   duration_ = new_duration;
1784   host_->SetDuration(new_duration);
1785 }
1786
1787 void ChunkDemuxer::IncreaseDurationIfNecessary(
1788     TimeDelta last_appended_buffer_timestamp,
1789     ChunkDemuxerStream* stream) {
1790   DCHECK(last_appended_buffer_timestamp != kNoTimestamp());
1791   if (last_appended_buffer_timestamp <= duration_)
1792     return;
1793
1794   TimeDelta stream_duration = stream->GetBufferedDuration();
1795   DCHECK(stream_duration > TimeDelta());
1796
1797   if (stream_duration > duration_)
1798     UpdateDuration(stream_duration);
1799 }
1800
1801 void ChunkDemuxer::DecreaseDurationIfNecessary() {
1802   lock_.AssertAcquired();
1803
1804   TimeDelta max_duration;
1805
1806   for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1807        itr != source_state_map_.end(); ++itr) {
1808     max_duration = std::max(max_duration,
1809                             itr->second->GetMaxBufferedDuration());
1810   }
1811
1812   if (max_duration == TimeDelta())
1813     return;
1814
1815   if (max_duration < duration_)
1816     UpdateDuration(max_duration);
1817 }
1818
1819 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges() const {
1820   base::AutoLock auto_lock(lock_);
1821   return GetBufferedRanges_Locked();
1822 }
1823
1824 Ranges<TimeDelta> ChunkDemuxer::GetBufferedRanges_Locked() const {
1825   lock_.AssertAcquired();
1826
1827   bool ended = state_ == ENDED;
1828   // TODO(acolwell): When we start allowing SourceBuffers that are not active,
1829   // we'll need to update this loop to only add ranges from active sources.
1830   RangesList ranges_list;
1831   for (SourceStateMap::const_iterator itr = source_state_map_.begin();
1832        itr != source_state_map_.end(); ++itr) {
1833     ranges_list.push_back(itr->second->GetBufferedRanges(duration_, ended));
1834   }
1835
1836   return ComputeIntersection(ranges_list, ended);
1837 }
1838
1839 void ChunkDemuxer::StartReturningData() {
1840   for (SourceStateMap::iterator itr = source_state_map_.begin();
1841        itr != source_state_map_.end(); ++itr) {
1842     itr->second->StartReturningData();
1843   }
1844 }
1845
1846 void ChunkDemuxer::AbortPendingReads() {
1847   for (SourceStateMap::iterator itr = source_state_map_.begin();
1848        itr != source_state_map_.end(); ++itr) {
1849     itr->second->AbortReads();
1850   }
1851 }
1852
1853 void ChunkDemuxer::SeekAllSources(TimeDelta seek_time) {
1854   for (SourceStateMap::iterator itr = source_state_map_.begin();
1855        itr != source_state_map_.end(); ++itr) {
1856     itr->second->Seek(seek_time);
1857   }
1858 }
1859
1860 void ChunkDemuxer::CompletePendingReadsIfPossible() {
1861   for (SourceStateMap::iterator itr = source_state_map_.begin();
1862        itr != source_state_map_.end(); ++itr) {
1863     itr->second->CompletePendingReadIfPossible();
1864   }
1865 }
1866
1867 void ChunkDemuxer::ShutdownAllStreams() {
1868   for (SourceStateMap::iterator itr = source_state_map_.begin();
1869        itr != source_state_map_.end(); ++itr) {
1870     itr->second->Shutdown();
1871   }
1872 }
1873
1874 }  // namespace media