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