Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(444)

Side by Side Diff: media/filters/adaptive_demuxer.cc

Issue 7044008: Initial implementation of stream switching in AdaptiveDemuxer. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Added documentation & a little cleanup Created 9 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "base/bind.h" 5 #include "base/bind.h"
6 #include "base/logging.h" 6 #include "base/logging.h"
7 #include "base/message_loop.h"
7 #include "base/string_number_conversions.h" 8 #include "base/string_number_conversions.h"
8 #include "base/string_split.h" 9 #include "base/string_split.h"
9 #include "base/string_util.h" 10 #include "base/string_util.h"
11 #include "media/ffmpeg/ffmpeg_common.h"
10 #include "media/filters/adaptive_demuxer.h" 12 #include "media/filters/adaptive_demuxer.h"
11 13
12 namespace media { 14 namespace media {
13 15
16 static const int64 kSwitchTimerPeriod = 5000; // In milliseconds.
17
18 // Object that decides when to switch streams.
19 class StreamSwitchManager
20 : public base::RefCountedThreadSafe<StreamSwitchManager> {
21
scherkus (not reviewing) 2011/05/23 18:26:29 nit: remove blank line
acolwell GONE FROM CHROMIUM 2011/05/23 22:45:45 Done.
22 public:
23 StreamSwitchManager();
24 ~StreamSwitchManager();
scherkus (not reviewing) 2011/05/23 18:26:29 virtual
acolwell GONE FROM CHROMIUM 2011/05/23 22:45:45 Done.
25
26 void Init(AdaptiveDemuxer* demuxer);
27
28 // Playback events. These methods are called when playback starts, pauses, or
29 // stops.
30 void Play();
31 void Pause();
32 void Stop();
33
34 private:
35 // Method called periodically to determine if we should switch
36 // streams.
37 void OnSwitchTimer();
38
39 // Called when the demuxer completes a stream switch.
40 void OnSwitchDone(PipelineStatus status);
41
42 // Helper method that schedules OnSwitchTimer() to be called.
43 void StartSwitchTimer_Locked();
44
45 // The demuxer that owns this object.
46 AdaptiveDemuxer* demuxer_;
47
48 // Guards the members below. Only held for simple variable reads/writes, not
49 // during async operation.
50 base::Lock lock_;
51
52 // Is clip playing or not?
53 bool playing_;
54
55 // Is a stream switch in progress?
56 bool switch_pending_;
57
58 // Has an OnSwitchTimer() call been scheduled?
59 bool switch_timer_running_;
60
61 // The stream IDs for the video streams.
62 AdaptiveDemuxer::StreamIdVector video_ids_;
63
64 // An index into |video_ids_| for the current video stream.
65 int current_id_index_;
66
67 DISALLOW_COPY_AND_ASSIGN(StreamSwitchManager);
68 };
69
70 StreamSwitchManager::StreamSwitchManager()
71 : demuxer_(NULL),
72 playing_(false),
73 switch_pending_(false),
74 switch_timer_running_(false),
75 current_id_index_(-1) {
76 }
77
78 StreamSwitchManager::~StreamSwitchManager() {}
79
80 void StreamSwitchManager::Init(AdaptiveDemuxer* demuxer) {
81 DCHECK(demuxer);
82 demuxer_ = demuxer;
83 video_ids_ = demuxer->GetVideoIds();
84 current_id_index_ = -1;
85
86 if (video_ids_.size() > 0) {
87 // Find the index in video_ids_ for the current video ID.
88 int current_id = demuxer->GetCurrentVideoId();
89 current_id_index_ = 0;
90 for (int i = 0; i < video_ids_.size(); i++) {
91 if (current_id == video_ids_[i]) {
92 current_id_index_ = i;
93 break;
94 }
95 }
96 }
97 }
98
99 void StreamSwitchManager::Play() {
100 base::AutoLock auto_lock(lock_);
scherkus (not reviewing) 2011/05/23 18:26:29 what are we locking ourselves from? which threads
acolwell GONE FROM CHROMIUM 2011/05/23 22:45:45 The pipeline & ffmpeg_demuxer threads were accessi
101
102 DCHECK(!playing_);
103
104 playing_ = true;
105
106 if (video_ids_.size() > 1 && !switch_timer_running_) {
107 StartSwitchTimer_Locked();
108 }
109 }
110
111 void StreamSwitchManager::Pause() {
112 base::AutoLock auto_lock(lock_);
113 DCHECK(playing_);
114 playing_ = false;
115 }
116
117 void StreamSwitchManager::Stop() {
118 base::AutoLock auto_lock(lock_);
119
120 DCHECK(!playing_);
121 demuxer_ = NULL;
122 }
123
124 void StreamSwitchManager::OnSwitchTimer() {
125 int new_stream_id = -1;
126 AdaptiveDemuxer* demuxer = NULL;
127
128 {
129 base::AutoLock auto_lock(lock_);
130 switch_timer_running_ = false;
131
132 if (!playing_)
133 return;
134
135 if (!switch_pending_) {
136 // Select the stream to switch to. For now we are just rotating
137 // through the available streams.
138 int new_id_index = (current_id_index_ + 1) % video_ids_.size();
139
140 // Did we select a new stream?
141 if (new_id_index != current_id_index_) {
142 current_id_index_ = new_id_index;
143 switch_pending_ = true;
144 demuxer = demuxer_;
145 new_stream_id = video_ids_[new_id_index];
146 }
147 }
148
149 StartSwitchTimer_Locked();
150 }
151
152 if (demuxer && new_stream_id != -1) {
153 demuxer->ChangeVideoStream(new_stream_id,
154 base::Bind(&StreamSwitchManager::OnSwitchDone,
155 this));
156 }
157 }
158
159 void StreamSwitchManager::OnSwitchDone(PipelineStatus status) {
160 base::AutoLock auto_lock(lock_);
161 switch_pending_ = false;
162 }
163
164 void StreamSwitchManager::StartSwitchTimer_Locked() {
165 lock_.AssertAcquired();
166
167 switch_timer_running_ = true;
168 MessageLoop::current()->PostDelayedTask(
169 FROM_HERE, NewRunnableMethod(this, &StreamSwitchManager::OnSwitchTimer),
170 kSwitchTimerPeriod);
171 }
172
14 // 173 //
15 // AdaptiveDemuxerStream 174 // AdaptiveDemuxerStream
16 // 175 //
17 AdaptiveDemuxerStream::AdaptiveDemuxerStream( 176 AdaptiveDemuxerStream::AdaptiveDemuxerStream(
18 StreamVector const& streams, int initial_stream) 177 StreamVector const& streams, int initial_stream)
19 : streams_(streams), current_stream_index_(initial_stream), 178 : streams_(streams), current_stream_index_(initial_stream),
20 bitstream_converter_enabled_(false) { 179 bitstream_converter_enabled_(false),
180 pending_reads_(0),
181 switch_index_(-1),
182 last_buffer_timestamp_(kNoTimestamp) {
21 DCheckSanity(); 183 DCheckSanity();
22 } 184 }
23 185
24 void AdaptiveDemuxerStream::DCheckSanity() { 186 void AdaptiveDemuxerStream::DCheckSanity() {
25 // We only carry out sanity checks in debug mode. 187 // We only carry out sanity checks in debug mode.
26 if (!logging::DEBUG_MODE) 188 if (!logging::DEBUG_MODE)
27 return; 189 return;
28 DCHECK(streams_[current_stream_index_].get()); 190 DCHECK(streams_[current_stream_index_].get());
29 191
30 bool non_null_stream_seen = false; 192 bool non_null_stream_seen = false;
(...skipping 19 matching lines...) Expand all
50 current_stream_index_ = -1; 212 current_stream_index_ = -1;
51 streams_.clear(); 213 streams_.clear();
52 } 214 }
53 215
54 DemuxerStream* AdaptiveDemuxerStream::current_stream() { 216 DemuxerStream* AdaptiveDemuxerStream::current_stream() {
55 base::AutoLock auto_lock(lock_); 217 base::AutoLock auto_lock(lock_);
56 return streams_[current_stream_index_]; 218 return streams_[current_stream_index_];
57 } 219 }
58 220
59 void AdaptiveDemuxerStream::Read(const ReadCallback& read_callback) { 221 void AdaptiveDemuxerStream::Read(const ReadCallback& read_callback) {
60 current_stream()->Read(read_callback); 222 DemuxerStream* stream = NULL;
223
224 {
225 base::AutoLock auto_lock(lock_);
226
227 read_cb_queue_.push_back(read_callback);
scherkus (not reviewing) 2011/05/23 18:26:29 want to raise it again since it seems like it migh
acolwell GONE FROM CHROMIUM 2011/05/23 22:45:45 It wouldn't remove queuing. It would just move the
228
229 // Check to make sure we aren't doing a stream switch. We only want to
230 // make calls on |streams_[current_stream_index_]| when we aren't
231 // in the middle of a stream switch. Since the callback is stored in
232 // |read_cb_queue_| we will issue the Read() on the new stream once
233 // the switch has completed.
234 if (!IsSwitchPending_Locked()) {
235 stream = streams_[current_stream_index_];
236 pending_reads_++;
237 }
238 }
239
240 if (stream)
241 stream->Read(base::Bind(&AdaptiveDemuxerStream::OnReadDone, this));
61 } 242 }
62 243
63 AVStream* AdaptiveDemuxerStream::GetAVStream() { 244 AVStream* AdaptiveDemuxerStream::GetAVStream() {
64 return current_stream()->GetAVStream(); 245 return current_stream()->GetAVStream();
65 } 246 }
66 247
67 DemuxerStream::Type AdaptiveDemuxerStream::type() { 248 DemuxerStream::Type AdaptiveDemuxerStream::type() {
68 return current_stream()->type(); 249 return current_stream()->type();
69 } 250 }
70 251
71 const MediaFormat& AdaptiveDemuxerStream::media_format() { 252 const MediaFormat& AdaptiveDemuxerStream::media_format() {
72 return current_stream()->media_format(); 253 return current_stream()->media_format();
73 } 254 }
74 255
75 void AdaptiveDemuxerStream::EnableBitstreamConverter() { 256 void AdaptiveDemuxerStream::EnableBitstreamConverter() {
76 { 257 {
77 base::AutoLock auto_lock(lock_); 258 base::AutoLock auto_lock(lock_);
78 bitstream_converter_enabled_ = true; 259 bitstream_converter_enabled_ = true;
79 } 260 }
80 current_stream()->EnableBitstreamConverter(); 261 current_stream()->EnableBitstreamConverter();
81 } 262 }
82 263
83 void AdaptiveDemuxerStream::ChangeCurrentStream(int index) { 264 void AdaptiveDemuxerStream::OnAdaptiveDemuxerSeek() {
84 bool needs_bitstream_converter_enabled; 265 base::AutoLock auto_lock(lock_);
85 { 266
86 base::AutoLock auto_lock(lock_); 267 last_buffer_timestamp_ = kNoTimestamp;
87 current_stream_index_ = index; 268
88 DCHECK(streams_[current_stream_index_]); 269 // XXAJC Figure out what to do if this happens during a stream switch.
scherkus (not reviewing) 2011/05/23 18:26:29 should these be TODOs / NOTIMPLEMENTED ?
acolwell GONE FROM CHROMIUM 2011/05/23 22:45:45 Done.
89 needs_bitstream_converter_enabled = bitstream_converter_enabled_; 270 }
90 } 271
272 // This method initiates a stream switch. The diagram below shows the steps
273 // involved.
274 //
275 // +-----------------------+
276 // ChangeCurrentStream() -> | Store stream switch |
277 // | index, seek_function, |
278 // | and switch_cb. |
279 // +-----------------------+
280 // |
281 // \|/
282 // +-------------------+ Yes
283 // | Are there pending | -----> (Wait for OnReadDone())
284 // | Read()s on the | |
285 // | current stream? | <-----+ \|/
286 // +-------------------+ +--- OnReadDone()
287 // | No
288 // \|/
289 // StartSwitch()
290 // |
291 // \|/
292 // +------------------------+ No
293 // | Have buffer timestamp? |-----+
294 // +------------------------+ |
295 // | Yes |
296 // \|/ |
297 // +-----------------------------+ |
298 // | Seek stream to timestamp | |
299 // | using switch_seek_function_ | |
300 // +-----------------------------+ |
301 // | |
302 // \|/ |
303 // OnSwitchSeekDone() <-----+
304 // |
305 // \|/
306 // +------------------+ No
307 // | Seek successful? |----------+
308 // +------------------+ |
309 // | Yes |
310 // \|/ |
311 // +------------------------------+ |
312 // | Update current_stream_index_ | |
313 // +------------------------------+ |
314 // | |
315 // \|/ |
316 // +---------------------------+ |
317 // | Call Read() on new stream |<---+
318 // | for deferred reads. |
319 // +---------------------------+
320 // |
321 // \|/
322 // switch_cb(OK | ERROR)
323 //
324 // NOTE: Any AdaptiveDemuxerStream::Read() calls that occur during the stream
325 // switch will be deferred until the switch has completed. The callbacks
326 // will be queued on |read_cb_queue_|, but no Read() will be issued on the
327 // current stream.
328 void AdaptiveDemuxerStream::ChangeCurrentStream(
329 int index,
330 const SeekFunction& seek_function,
331 const PipelineStatusCB& switch_cb) {
332 DCHECK_GE(index, 0);
333
334 PipelineStatusCB error_cb;
335 PipelineStatus error_status = PIPELINE_ERROR_INVALID_STATE;
336
337 bool start_switch = false;
338
339 {
340 base::AutoLock auto_lock(lock_);
341
342 DCHECK_LE(index, streams_.size());
343 DCHECK(streams_[index].get());
344 DCHECK(!IsSwitchPending_Locked());
345
346 // XXAJC - Still need to handle the case where the stream has ended.
347 if (index == current_stream_index_) {
348 error_cb = switch_cb;
349 error_status = PIPELINE_OK;
350 } else {
351 switch_cb_ = switch_cb;
352 switch_index_ = index;
353 switch_seek_function_ = seek_function;
354
355 start_switch = CanStartSwitch_Locked();
356 }
357 }
358
359 if (!error_cb.is_null()) {
360 error_cb.Run(error_status);
361 return;
362 }
363
364 if (start_switch)
365 StartSwitch();
366 }
367
368 void AdaptiveDemuxerStream::OnReadDone(Buffer* buffer) {
369 ReadCallback read_cb;
370 bool start_switch = false;
371
372 {
373 base::AutoLock auto_lock(lock_);
374
375 pending_reads_--;
376
377 DCHECK_GE(pending_reads_, 0);
378 DCHECK_GE(read_cb_queue_.size(), 0);
379
380 read_cb = read_cb_queue_.front();
381 read_cb_queue_.pop_front();
382
383 if (buffer && !buffer->IsEndOfStream())
384 last_buffer_timestamp_ = buffer->GetTimestamp();
385
386 start_switch = IsSwitchPending_Locked() && CanStartSwitch_Locked();
387 }
388
389 if (!read_cb.is_null())
390 read_cb.Run(buffer);
391
392 if (start_switch)
393 StartSwitch();
394 }
395
396 bool AdaptiveDemuxerStream::IsSwitchPending_Locked() const {
397 lock_.AssertAcquired();
398 return !switch_cb_.is_null();
399 }
400
401 bool AdaptiveDemuxerStream::CanStartSwitch_Locked() const {
402 lock_.AssertAcquired();
403 return (pending_reads_ == 0);
404 }
405
406 void AdaptiveDemuxerStream::StartSwitch() {
407 SeekFunction seek_function;
408 base::TimeDelta seek_point;
409
410 {
411 base::AutoLock auto_lock(lock_);
412 DCHECK(IsSwitchPending_Locked());
413 DCHECK_EQ(pending_reads_, 0);
414 DCHECK_GE(switch_index_, 0);
415
416 seek_point = last_buffer_timestamp_;
417 seek_function = switch_seek_function_;
418
419 // XXAJC add code to call switch_cb_ if we are at the end of the stream now.
420 }
421
422 if (seek_point == kNoTimestamp) {
423 // We haven't seen a buffer so there is no need to seek. Just move on to
424 // the next stage in the switch process.
425 OnSwitchSeekDone(PIPELINE_OK, kNoTimestamp);
426 return;
427 }
428
429 DCHECK(!seek_function.is_null());
430 seek_function.Run(seek_point,
431 base::Bind(&AdaptiveDemuxerStream::OnSwitchSeekDone, this));
432 }
433
434 void AdaptiveDemuxerStream::OnSwitchSeekDone(PipelineStatus status,
435 base::TimeDelta seek_time) {
436 DemuxerStream* stream = NULL;
437 PipelineStatusCB switch_cb;
438 int reads_to_request = 0;
439 bool needs_bitstream_converter_enabled = false;
440
441 {
442 base::AutoLock auto_lock(lock_);
443
444 if (status == PIPELINE_OK) {
445 DCHECK(streams_[switch_index_]);
446
447 current_stream_index_ = switch_index_;
448 needs_bitstream_converter_enabled = bitstream_converter_enabled_;
449 }
450
451 // Clear stream switch state.
452 switch_cb = switch_cb_;
453 switch_cb_.Reset();
454 switch_index_ = -1;
455 switch_seek_function_.Reset();
456
457 // Get the number of outstanding Read()s on this object.
458 reads_to_request = read_cb_queue_.size();
459
460 DCHECK_EQ(pending_reads_, 0);
461 pending_reads_ = reads_to_request;
462
463 stream = streams_[current_stream_index_];
464 }
465
91 if (needs_bitstream_converter_enabled) 466 if (needs_bitstream_converter_enabled)
92 EnableBitstreamConverter(); 467 EnableBitstreamConverter();
468
469 if (stream) {
470 // Make the Read() calls that were deferred during the stream switch.
471 for(;reads_to_request > 0; --reads_to_request)
472 stream->Read(base::Bind(&AdaptiveDemuxerStream::OnReadDone, this));
473 }
474
475 // Signal that the stream switch has completed.
476 if (!switch_cb.is_null())
477 switch_cb.Run(status);
93 } 478 }
94 479
95 // 480 //
96 // AdaptiveDemuxer 481 // AdaptiveDemuxer
97 // 482 //
98 483
99 AdaptiveDemuxer::AdaptiveDemuxer(DemuxerVector const& demuxers, 484 AdaptiveDemuxer::AdaptiveDemuxer(DemuxerVector const& demuxers,
100 int initial_audio_demuxer_index, 485 int initial_audio_demuxer_index,
101 int initial_video_demuxer_index) 486 int initial_video_demuxer_index)
102 : demuxers_(demuxers), 487 : demuxers_(demuxers),
103 current_audio_demuxer_index_(initial_audio_demuxer_index), 488 current_audio_demuxer_index_(initial_audio_demuxer_index),
104 current_video_demuxer_index_(initial_video_demuxer_index) { 489 current_video_demuxer_index_(initial_video_demuxer_index),
490 playback_rate_(0),
491 switch_pending_(false),
492 stream_switch_manager_(new StreamSwitchManager()){
105 DCHECK(!demuxers_.empty()); 493 DCHECK(!demuxers_.empty());
106 DCHECK_GE(current_audio_demuxer_index_, -1); 494 DCHECK_GE(current_audio_demuxer_index_, -1);
107 DCHECK_GE(current_video_demuxer_index_, -1); 495 DCHECK_GE(current_video_demuxer_index_, -1);
108 DCHECK_LT(current_audio_demuxer_index_, static_cast<int>(demuxers_.size())); 496 DCHECK_LT(current_audio_demuxer_index_, static_cast<int>(demuxers_.size()));
109 DCHECK_LT(current_video_demuxer_index_, static_cast<int>(demuxers_.size())); 497 DCHECK_LT(current_video_demuxer_index_, static_cast<int>(demuxers_.size()));
110 DCHECK(current_audio_demuxer_index_ != -1 || 498 DCHECK(current_audio_demuxer_index_ != -1 ||
111 current_video_demuxer_index_ != -1); 499 current_video_demuxer_index_ != -1);
112 AdaptiveDemuxerStream::StreamVector audio_streams, video_streams; 500 AdaptiveDemuxerStream::StreamVector audio_streams, video_streams;
113 for (size_t i = 0; i < demuxers_.size(); ++i) { 501 for (size_t i = 0; i < demuxers_.size(); ++i) {
114 audio_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::AUDIO)); 502 audio_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::AUDIO));
115 video_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::VIDEO)); 503 video_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::VIDEO));
116 } 504 }
117 if (current_audio_demuxer_index_ >= 0) { 505 if (current_audio_demuxer_index_ >= 0) {
118 audio_stream_ = new AdaptiveDemuxerStream( 506 audio_stream_ = new AdaptiveDemuxerStream(
119 audio_streams, current_audio_demuxer_index_); 507 audio_streams, current_audio_demuxer_index_);
120 } 508 }
121 if (current_video_demuxer_index_ >= 0) { 509 if (current_video_demuxer_index_ >= 0) {
122 video_stream_ = new AdaptiveDemuxerStream( 510 video_stream_ = new AdaptiveDemuxerStream(
123 video_streams, current_video_demuxer_index_); 511 video_streams, current_video_demuxer_index_);
124 } 512 }
125 513
514 stream_switch_manager_->Init(this);
515
126 // TODO(fischman): any streams in the underlying demuxers that aren't being 516 // TODO(fischman): any streams in the underlying demuxers that aren't being
127 // consumed currently need to be sent to /dev/null or else FFmpegDemuxer will 517 // consumed currently need to be sent to /dev/null or else FFmpegDemuxer will
128 // hold data for them in memory, waiting for them to get drained by a 518 // hold data for them in memory, waiting for them to get drained by a
129 // non-existent decoder. 519 // non-existent decoder.
130 } 520 }
131 521
132 AdaptiveDemuxer::~AdaptiveDemuxer() {} 522 AdaptiveDemuxer::~AdaptiveDemuxer() {}
133 523
134 void AdaptiveDemuxer::ChangeCurrentDemuxer(int audio_index, int video_index) { 524 // Switches the current video stream. The diagram below describes the switch
525 // process.
526 // +-------------------------------+
527 // ChangeVideoStream() ---> | video_index |
528 // | == | Yes.
529 // | current_video_demuxer_index_? |--> done_cb(OK)
530 // +-------------------------------+
531 // | No.
532 // \|/
533 // Call video_stream_->ChangeCurrentStream()
534 // |
535 // \|/
536 // (Wait for ChangeVideoStreamDone())
537 // |
538 // \|/
539 // ChangeVideoStreamDone()
540 // |
541 // \|/
542 // +------------------------+ No
543 // | Was switch successful? |---> done_cb(ERROR)
544 // +------------------------+
545 // |
546 // \|/
547 // Update current_video_demuxer_index_
548 // & SetPlaybackRate() on new stream.
549 // |
550 // \|/
551 // done_cb(OK).
552 //
553 void AdaptiveDemuxer::ChangeVideoStream(int video_index,
554 const PipelineStatusCB& done_cb) {
135 // TODO(fischman): this is currently broken because when a new Demuxer is to 555 // TODO(fischman): this is currently broken because when a new Demuxer is to
136 // be used we need to set_host(host()) it, and we need to set_host(NULL) the 556 // be used we need to set_host(host()) it, and we need to set_host(NULL) the
137 // current Demuxer if it's no longer being used. 557 // current Demuxer if it's no longer being used.
558 AdaptiveDemuxerStream* stream = NULL;
559 bool switching_to_current_stream = false;
560 AdaptiveDemuxerStream::SeekFunction seek_function;
561
562 {
563 base::AutoLock auto_lock(lock_);
564
565 DCHECK(video_stream_);
566 DCHECK(!switch_pending_);
567 if (current_video_demuxer_index_ == video_index) {
568 switching_to_current_stream = true;
569 } else {
570 stream = video_stream_;
571 switch_pending_ = true;
572 seek_function = base::Bind(&AdaptiveDemuxer::StartStreamSwitchSeek,
573 this,
574 DemuxerStream::VIDEO,
575 video_index);
576 }
577 }
578
579 if (switching_to_current_stream) {
580 done_cb.Run(PIPELINE_OK);
581 return;
582 }
583
584 DCHECK(stream);
585 stream->ChangeCurrentStream(
586 video_index, seek_function,
587 base::Bind(&AdaptiveDemuxer::ChangeVideoStreamDone, this, video_index,
588 done_cb));
589 }
590
591 int AdaptiveDemuxer::GetCurrentVideoId() const {
138 base::AutoLock auto_lock(lock_); 592 base::AutoLock auto_lock(lock_);
139 current_audio_demuxer_index_ = audio_index; 593 return current_video_demuxer_index_;
140 current_video_demuxer_index_ = video_index; 594 }
141 if (audio_stream_) 595
142 audio_stream_->ChangeCurrentStream(audio_index); 596 AdaptiveDemuxer::StreamIdVector AdaptiveDemuxer::GetVideoIds() const {
143 if (video_stream_) 597 StreamIdVector ids;
144 video_stream_->ChangeCurrentStream(video_index); 598 base::AutoLock auto_lock(lock_);
599
600 for (int i = 0; i < demuxers_.size(); i++) {
601 if (demuxers_[i]->GetStream(DemuxerStream::VIDEO))
602 ids.push_back(i);
603 }
604
605 return ids;
606 }
607
608 void AdaptiveDemuxer::ChangeVideoStreamDone(int new_stream_index,
609 const PipelineStatusCB& done_cb,
610 PipelineStatus status) {
611 {
612 base::AutoLock auto_lock(lock_);
613
614 switch_pending_ = false;
615
616 if (status == PIPELINE_OK) {
617 demuxers_[current_video_demuxer_index_]->SetPlaybackRate(0);
618 current_video_demuxer_index_ = new_stream_index;
619 demuxers_[current_video_demuxer_index_]->SetPlaybackRate(playback_rate_);
620 }
621 }
622
623 done_cb.Run(status);
145 } 624 }
146 625
147 Demuxer* AdaptiveDemuxer::current_demuxer(DemuxerStream::Type type) { 626 Demuxer* AdaptiveDemuxer::current_demuxer(DemuxerStream::Type type) {
148 base::AutoLock auto_lock(lock_); 627 base::AutoLock auto_lock(lock_);
149 switch (type) { 628 switch (type) {
150 case DemuxerStream::AUDIO: 629 case DemuxerStream::AUDIO:
151 return (current_audio_demuxer_index_ < 0) ? NULL : 630 return (current_audio_demuxer_index_ < 0) ? NULL :
152 demuxers_[current_audio_demuxer_index_]; 631 demuxers_[current_audio_demuxer_index_];
153 case DemuxerStream::VIDEO: 632 case DemuxerStream::VIDEO:
154 return (current_video_demuxer_index_ < 0) ? NULL : 633 return (current_video_demuxer_index_ < 0) ? NULL :
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 713
235 base::Lock lock_; 714 base::Lock lock_;
236 int remaining_count_; 715 int remaining_count_;
237 FilterStatusCB orig_cb_; 716 FilterStatusCB orig_cb_;
238 PipelineStatus overall_status_; 717 PipelineStatus overall_status_;
239 718
240 DISALLOW_COPY_AND_ASSIGN(CountingStatusCB); 719 DISALLOW_COPY_AND_ASSIGN(CountingStatusCB);
241 }; 720 };
242 721
243 void AdaptiveDemuxer::Stop(FilterCallback* callback) { 722 void AdaptiveDemuxer::Stop(FilterCallback* callback) {
723 stream_switch_manager_->Stop();
724
244 // Stop() must be called on all of the demuxers even though only one demuxer 725 // Stop() must be called on all of the demuxers even though only one demuxer
245 // is actively delivering audio and another one is delivering video. This 726 // is actively delivering audio and another one is delivering video. This
246 // just satisfies the contract that all demuxers must have Stop() called on 727 // just satisfies the contract that all demuxers must have Stop() called on
247 // them before they are destroyed. 728 // them before they are destroyed.
248 // 729 //
249 // TODO: Remove CountingCallback once Stop() is converted to FilterStatusCB. 730 // TODO: Remove CountingCallback once Stop() is converted to FilterStatusCB.
250 CountingCallback* wrapper = new CountingCallback(demuxers_.size(), callback); 731 CountingCallback* wrapper = new CountingCallback(demuxers_.size(), callback);
251 for (size_t i = 0; i < demuxers_.size(); ++i) 732 for (size_t i = 0; i < demuxers_.size(); ++i)
252 demuxers_[i]->Stop(wrapper->GetACallback()); 733 demuxers_[i]->Stop(wrapper->GetACallback());
253 } 734 }
254 735
255 void AdaptiveDemuxer::Seek(base::TimeDelta time, const FilterStatusCB& cb) { 736 void AdaptiveDemuxer::Seek(base::TimeDelta time, const FilterStatusCB& cb) {
737
738 if (audio_stream_)
739 audio_stream_->OnAdaptiveDemuxerSeek();
740
741 if (video_stream_)
742 video_stream_->OnAdaptiveDemuxerSeek();
743
256 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); 744 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO);
257 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); 745 Demuxer* video = current_demuxer(DemuxerStream::VIDEO);
258 int count = (audio ? 1 : 0) + (video && audio != video ? 1 : 0); 746 int count = (audio ? 1 : 0) + (video && audio != video ? 1 : 0);
259 CountingStatusCB* wrapper = new CountingStatusCB(count, cb); 747 CountingStatusCB* wrapper = new CountingStatusCB(count, cb);
260 if (audio) 748 if (audio)
261 audio->Seek(time, wrapper->GetACallback()); 749 audio->Seek(time, wrapper->GetACallback());
262 if (video && audio != video) 750 if (video && audio != video)
263 video->Seek(time, wrapper->GetACallback()); 751 video->Seek(time, wrapper->GetACallback());
264 } 752 }
265 753
266 void AdaptiveDemuxer::OnAudioRendererDisabled() { 754 void AdaptiveDemuxer::OnAudioRendererDisabled() {
267 for (size_t i = 0; i < demuxers_.size(); ++i) 755 for (size_t i = 0; i < demuxers_.size(); ++i)
268 demuxers_[i]->OnAudioRendererDisabled(); 756 demuxers_[i]->OnAudioRendererDisabled();
269 } 757 }
270 758
271 void AdaptiveDemuxer::set_host(FilterHost* filter_host) { 759 void AdaptiveDemuxer::set_host(FilterHost* filter_host) {
272 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); 760 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO);
273 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); 761 Demuxer* video = current_demuxer(DemuxerStream::VIDEO);
274 if (audio) audio->set_host(filter_host); 762 if (audio) audio->set_host(filter_host);
275 if (video && audio != video) video->set_host(filter_host); 763 if (video && audio != video) video->set_host(filter_host);
276 } 764 }
277 765
278 void AdaptiveDemuxer::SetPlaybackRate(float playback_rate) { 766 void AdaptiveDemuxer::SetPlaybackRate(float playback_rate) {
767 {
768 base::AutoLock auto_lock(lock_);
769 if (playback_rate_ == 0 && playback_rate > 0) {
770 stream_switch_manager_->Play();
771 } else if (playback_rate_ > 0 && playback_rate == 0) {
772 stream_switch_manager_->Pause();
773 }
774
775 playback_rate_ = playback_rate;
776 }
777
279 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); 778 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO);
280 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); 779 Demuxer* video = current_demuxer(DemuxerStream::VIDEO);
281 if (audio) audio->SetPlaybackRate(playback_rate); 780 if (audio) audio->SetPlaybackRate(playback_rate);
282 if (video && audio != video) video->SetPlaybackRate(playback_rate); 781 if (video && audio != video) video->SetPlaybackRate(playback_rate);
283 } 782 }
284 783
285 void AdaptiveDemuxer::SetPreload(Preload preload) { 784 void AdaptiveDemuxer::SetPreload(Preload preload) {
286 for (size_t i = 0; i < demuxers_.size(); ++i) 785 for (size_t i = 0; i < demuxers_.size(); ++i)
287 demuxers_[i]->SetPreload(preload); 786 demuxers_[i]->SetPreload(preload);
288 } 787 }
289 788
290 scoped_refptr<DemuxerStream> AdaptiveDemuxer::GetStream( 789 scoped_refptr<DemuxerStream> AdaptiveDemuxer::GetStream(
291 DemuxerStream::Type type) { 790 DemuxerStream::Type type) {
292 switch (type) { 791 switch (type) {
293 case DemuxerStream::AUDIO: 792 case DemuxerStream::AUDIO:
294 return audio_stream_; 793 return audio_stream_;
295 case DemuxerStream::VIDEO: 794 case DemuxerStream::VIDEO:
296 return video_stream_; 795 return video_stream_;
297 default: 796 default:
298 LOG(DFATAL) << "Unexpected type " << type; 797 LOG(DFATAL) << "Unexpected type " << type;
299 return NULL; 798 return NULL;
300 } 799 }
301 } 800 }
302 801
802 void AdaptiveDemuxer::StartStreamSwitchSeek(
803 DemuxerStream::Type type,
804 int stream_index,
805 base::TimeDelta seek_time,
806 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb) {
807 DCHECK_GE(stream_index, 0);
808 DCHECK(!seek_cb.is_null());
809
810 Demuxer* demuxer = NULL;
811 base::TimeDelta seek_point;
812 FilterStatusCB filter_cb;
813
814 {
815 base::AutoLock auto_lock(lock_);
816
817 demuxer = demuxers_[stream_index];
818
819 if (GetSeekTimeAfter(demuxer->GetStream(type)->GetAVStream(), seek_time,
820 &seek_point)) {
821 // We found a seek point.
822 filter_cb = base::Bind(&AdaptiveDemuxer::OnStreamSeekDone, this,
823 seek_point, seek_cb);
824 } else {
825 // We didn't find a seek point. Assume we don't have index data for it
826 // yet. Seek to the specified time to force index data to be loaded.
827 seek_point = seek_time;
828 filter_cb = base::Bind(&AdaptiveDemuxer::OnIndexSeekDone, this,
829 type, stream_index, seek_time, seek_cb);
830 }
831 }
832
833 DCHECK(demuxer);
834 demuxer->Seek(seek_point, filter_cb);
835 }
836
837 void AdaptiveDemuxer::OnIndexSeekDone(
838 DemuxerStream::Type type,
839 int stream_index,
840 base::TimeDelta seek_time,
841 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb,
842 PipelineStatus status) {
843 base::TimeDelta seek_point;
844 FilterStatusCB filter_cb;
845
846 Demuxer* demuxer = NULL;
847
848 if (status != PIPELINE_OK) {
849 seek_cb.Run(status, base::TimeDelta());
850 return;
851 }
852
853 {
854 base::AutoLock auto_lock(lock_);
855
856 demuxer = demuxers_[stream_index];
857 DCHECK(demuxer);
858
859 // Look for a seek point now that we have index data.
860 if (GetSeekTimeAfter(demuxer->GetStream(type)->GetAVStream(), seek_time,
861 &seek_point)) {
862 filter_cb = base::Bind(&AdaptiveDemuxer::OnStreamSeekDone, this,
863 seek_point, seek_cb);
864 } else {
865 // Failed again to find a seek point. Clear the demuxer so that
866 // a seek error will be reported.
867 demuxer = NULL;
868 }
869 }
870
871 if (!demuxer) {
872 seek_cb.Run(PIPELINE_ERROR_INITIALIZATION_FAILED, base::TimeDelta());
873 return;
874 }
875
876 demuxer->Seek(seek_point, filter_cb);
877 }
878
879 void AdaptiveDemuxer::OnStreamSeekDone(
880 base::TimeDelta seek_point,
881 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb,
882 PipelineStatus status) {
883 if (status != PIPELINE_OK) {
884 seek_cb.Run(status, base::TimeDelta());
885 return;
886 }
887
888 seek_cb.Run(PIPELINE_OK, seek_point);
889 }
890
891
303 // 892 //
304 // AdaptiveDemuxerFactory 893 // AdaptiveDemuxerFactory
305 // 894 //
306 895
307 AdaptiveDemuxerFactory::AdaptiveDemuxerFactory( 896 AdaptiveDemuxerFactory::AdaptiveDemuxerFactory(
308 DemuxerFactory* delegate_factory) 897 DemuxerFactory* delegate_factory)
309 : delegate_factory_(delegate_factory) { 898 : delegate_factory_(delegate_factory) {
310 DCHECK(delegate_factory); 899 DCHECK(delegate_factory);
311 } 900 }
312 901
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after
485 delete cb; 1074 delete cb;
486 return; 1075 return;
487 } 1076 }
488 DemuxerAccumulator* accumulator = new DemuxerAccumulator( 1077 DemuxerAccumulator* accumulator = new DemuxerAccumulator(
489 audio_index, video_index, urls.size(), cb); 1078 audio_index, video_index, urls.size(), cb);
490 for (size_t i = 0; i < urls.size(); ++i) 1079 for (size_t i = 0; i < urls.size(); ++i)
491 delegate_factory_->Build(urls[i], accumulator->GetNthCallback(i)); 1080 delegate_factory_->Build(urls[i], accumulator->GetNthCallback(i));
492 } 1081 }
493 1082
494 } // namespace media 1083 } // namespace media
OLDNEW
« media/filters/adaptive_demuxer.h ('K') | « media/filters/adaptive_demuxer.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698