OLD | NEW |
---|---|
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 "base/timer.h" | |
12 #include "media/ffmpeg/ffmpeg_common.h" | |
10 #include "media/filters/adaptive_demuxer.h" | 13 #include "media/filters/adaptive_demuxer.h" |
11 | 14 |
12 namespace media { | 15 namespace media { |
13 | 16 |
17 static const int64 kSwitchTimerPeriod = 5000; // In milliseconds. | |
18 | |
19 // Object that decides when to switch streams. | |
20 class StreamSwitchManager | |
21 : public base::RefCountedThreadSafe<StreamSwitchManager> { | |
22 public: | |
23 typedef std::vector<int> StreamIdVector; | |
24 | |
25 StreamSwitchManager(AdaptiveDemuxer* demuxer, | |
26 const StreamIdVector& video_ids); | |
27 virtual ~StreamSwitchManager(); | |
28 | |
29 // Playback events. These methods are called when playback starts, pauses, or | |
30 // stops. | |
31 void Play(); | |
32 void Pause(); | |
33 void Stop(); | |
34 | |
35 private: | |
36 // Method called periodically to determine if we should switch | |
37 // streams. | |
38 void OnSwitchTimer(); | |
39 | |
40 // Called when the demuxer completes a stream switch. | |
41 void OnSwitchDone(PipelineStatus status); | |
42 | |
43 // The demuxer that owns this object. | |
44 AdaptiveDemuxer* demuxer_; | |
45 | |
46 // Message loop this object runs on. | |
47 MessageLoop* message_loop_; | |
48 | |
49 // Is clip playing or not? | |
50 bool playing_; | |
51 | |
52 // Is a stream switch in progress? | |
53 bool switch_pending_; | |
54 | |
55 // Periodic timer that calls OnSwitchTimer(). | |
56 base::RepeatingTimer<StreamSwitchManager> timer_; | |
57 | |
58 // The stream IDs for the video streams. | |
59 StreamIdVector video_ids_; | |
60 | |
61 // An index into |video_ids_| for the current video stream. | |
62 int current_id_index_; | |
63 | |
64 DISALLOW_COPY_AND_ASSIGN(StreamSwitchManager); | |
65 }; | |
66 | |
67 StreamSwitchManager::StreamSwitchManager( | |
68 AdaptiveDemuxer* demuxer, | |
69 const StreamIdVector& video_ids) | |
70 : demuxer_(demuxer), | |
71 playing_(false), | |
72 switch_pending_(false), | |
73 video_ids_(video_ids), | |
74 current_id_index_(-1) { | |
75 DCHECK(demuxer); | |
76 message_loop_ = MessageLoop::current(); | |
77 demuxer_ = demuxer; | |
78 current_id_index_ = -1; | |
79 | |
80 if (video_ids_.size() > 0) { | |
81 // Find the index in video_ids_ for the current video ID. | |
82 int current_id = demuxer->GetCurrentVideoId(); | |
83 current_id_index_ = 0; | |
84 for (int i = 0; i < video_ids_.size(); i++) { | |
85 if (current_id == video_ids_[i]) { | |
86 current_id_index_ = i; | |
87 break; | |
88 } | |
89 } | |
90 } | |
91 } | |
92 | |
93 StreamSwitchManager::~StreamSwitchManager() {} | |
94 | |
95 void StreamSwitchManager::Play() { | |
96 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
97 DCHECK(!playing_); | |
98 playing_ = true; | |
99 | |
100 if (video_ids_.size() > 1) { | |
101 timer_.Start(base::TimeDelta::FromMilliseconds(kSwitchTimerPeriod), | |
102 this, &StreamSwitchManager::OnSwitchTimer); | |
103 } | |
104 } | |
105 | |
106 void StreamSwitchManager::Pause() { | |
107 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
108 DCHECK(playing_); | |
109 playing_ = false; | |
110 timer_.Stop(); | |
111 } | |
112 | |
113 void StreamSwitchManager::Stop() { | |
114 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
115 DCHECK(!playing_); | |
116 demuxer_ = NULL; | |
117 } | |
118 | |
119 void StreamSwitchManager::OnSwitchTimer() { | |
120 DCHECK_EQ(MessageLoop::current(), message_loop_); | |
121 | |
122 if (!playing_ || !demuxer_ || (video_ids_.size() < 2) || switch_pending_) | |
123 return; | |
124 | |
125 // Select the stream to switch to. For now we are just rotating | |
126 // through the available streams. | |
127 int new_id_index = (current_id_index_ + 1) % video_ids_.size(); | |
128 | |
129 current_id_index_ = new_id_index; | |
130 switch_pending_ = true; | |
131 demuxer_->ChangeVideoStream(video_ids_[new_id_index], | |
132 base::Bind(&StreamSwitchManager::OnSwitchDone, | |
133 this)); | |
134 } | |
135 | |
136 void StreamSwitchManager::OnSwitchDone(PipelineStatus status) { | |
137 if (MessageLoop::current() != message_loop_) { | |
138 message_loop_->PostTask( | |
139 FROM_HERE, | |
140 NewRunnableMethod(this, &StreamSwitchManager::OnSwitchDone, status)); | |
141 return; | |
142 } | |
143 | |
144 switch_pending_ = false; | |
145 } | |
146 | |
14 // | 147 // |
15 // AdaptiveDemuxerStream | 148 // AdaptiveDemuxerStream |
16 // | 149 // |
17 AdaptiveDemuxerStream::AdaptiveDemuxerStream( | 150 AdaptiveDemuxerStream::AdaptiveDemuxerStream( |
18 StreamVector const& streams, int initial_stream) | 151 StreamVector const& streams, int initial_stream) |
19 : streams_(streams), current_stream_index_(initial_stream), | 152 : streams_(streams), current_stream_index_(initial_stream), |
20 bitstream_converter_enabled_(false) { | 153 bitstream_converter_enabled_(false), |
154 pending_reads_(0), | |
155 switch_index_(-1), | |
156 last_buffer_timestamp_(kNoTimestamp) { | |
21 DCheckSanity(); | 157 DCheckSanity(); |
22 } | 158 } |
23 | 159 |
24 void AdaptiveDemuxerStream::DCheckSanity() { | 160 void AdaptiveDemuxerStream::DCheckSanity() { |
25 // We only carry out sanity checks in debug mode. | 161 // We only carry out sanity checks in debug mode. |
26 if (!logging::DEBUG_MODE) | 162 if (!logging::DEBUG_MODE) |
27 return; | 163 return; |
28 DCHECK(streams_[current_stream_index_].get()); | 164 DCHECK(streams_[current_stream_index_].get()); |
29 | 165 |
30 bool non_null_stream_seen = false; | 166 bool non_null_stream_seen = false; |
(...skipping 19 matching lines...) Expand all Loading... | |
50 current_stream_index_ = -1; | 186 current_stream_index_ = -1; |
51 streams_.clear(); | 187 streams_.clear(); |
52 } | 188 } |
53 | 189 |
54 DemuxerStream* AdaptiveDemuxerStream::current_stream() { | 190 DemuxerStream* AdaptiveDemuxerStream::current_stream() { |
55 base::AutoLock auto_lock(lock_); | 191 base::AutoLock auto_lock(lock_); |
56 return streams_[current_stream_index_]; | 192 return streams_[current_stream_index_]; |
57 } | 193 } |
58 | 194 |
59 void AdaptiveDemuxerStream::Read(const ReadCallback& read_callback) { | 195 void AdaptiveDemuxerStream::Read(const ReadCallback& read_callback) { |
60 current_stream()->Read(read_callback); | 196 DemuxerStream* stream = NULL; |
197 | |
198 { | |
199 base::AutoLock auto_lock(lock_); | |
200 | |
201 read_cb_queue_.push_back(read_callback); | |
202 | |
203 // Check to make sure we aren't doing a stream switch. We only want to | |
204 // make calls on |streams_[current_stream_index_]| when we aren't | |
205 // in the middle of a stream switch. Since the callback is stored in | |
206 // |read_cb_queue_| we will issue the Read() on the new stream once | |
207 // the switch has completed. | |
208 if (!IsSwitchPending_Locked()) { | |
209 stream = streams_[current_stream_index_]; | |
210 pending_reads_++; | |
211 } | |
212 } | |
213 | |
214 if (stream) | |
215 stream->Read(base::Bind(&AdaptiveDemuxerStream::OnReadDone, this)); | |
61 } | 216 } |
62 | 217 |
63 AVStream* AdaptiveDemuxerStream::GetAVStream() { | 218 AVStream* AdaptiveDemuxerStream::GetAVStream() { |
64 return current_stream()->GetAVStream(); | 219 return current_stream()->GetAVStream(); |
65 } | 220 } |
66 | 221 |
67 DemuxerStream::Type AdaptiveDemuxerStream::type() { | 222 DemuxerStream::Type AdaptiveDemuxerStream::type() { |
68 return current_stream()->type(); | 223 return current_stream()->type(); |
69 } | 224 } |
70 | 225 |
71 const MediaFormat& AdaptiveDemuxerStream::media_format() { | 226 const MediaFormat& AdaptiveDemuxerStream::media_format() { |
72 return current_stream()->media_format(); | 227 return current_stream()->media_format(); |
73 } | 228 } |
74 | 229 |
75 void AdaptiveDemuxerStream::EnableBitstreamConverter() { | 230 void AdaptiveDemuxerStream::EnableBitstreamConverter() { |
76 { | 231 { |
77 base::AutoLock auto_lock(lock_); | 232 base::AutoLock auto_lock(lock_); |
78 bitstream_converter_enabled_ = true; | 233 bitstream_converter_enabled_ = true; |
79 } | 234 } |
80 current_stream()->EnableBitstreamConverter(); | 235 current_stream()->EnableBitstreamConverter(); |
81 } | 236 } |
82 | 237 |
83 void AdaptiveDemuxerStream::ChangeCurrentStream(int index) { | 238 void AdaptiveDemuxerStream::OnAdaptiveDemuxerSeek(base::TimeDelta time) { |
84 bool needs_bitstream_converter_enabled; | 239 base::AutoLock auto_lock(lock_); |
85 { | 240 |
86 base::AutoLock auto_lock(lock_); | 241 last_buffer_timestamp_ = time; |
87 current_stream_index_ = index; | 242 |
88 DCHECK(streams_[current_stream_index_]); | 243 // TODO(acolwell): Figure out what to do if this happens during a stream |
89 needs_bitstream_converter_enabled = bitstream_converter_enabled_; | 244 // switch. |
90 } | 245 } |
246 | |
247 // This method initiates a stream switch. The diagram below shows the steps | |
248 // involved. | |
249 // | |
250 // +-----------------------+ | |
251 // ChangeCurrentStream() -> | Store stream switch | | |
252 // | index, seek_function, | | |
253 // | and switch_cb. | | |
254 // +-----------------------+ | |
255 // | | |
256 // V | |
257 // +-------------------+ Yes | |
258 // | Are there pending | -----> (Wait for OnReadDone()) | |
259 // | Read()s on the | | | |
260 // | current stream? | <-----+ V | |
261 // +-------------------+ +--- OnReadDone() | |
262 // | No | |
263 // V | |
264 // StartSwitch() | |
265 // | | |
266 // V | |
267 // +------------------------+ No | |
268 // | Have buffer timestamp? |-----+ | |
269 // +------------------------+ | | |
270 // | Yes | | |
271 // V | | |
272 // +-----------------------------+ | | |
273 // | Seek stream to timestamp | | | |
274 // | using switch_seek_function_ | | | |
275 // +-----------------------------+ | | |
276 // | | | |
277 // V | | |
278 // OnSwitchSeekDone() <-----+ | |
279 // | | |
280 // V | |
281 // +------------------+ No | |
282 // | Seek successful? |----------+ | |
283 // +------------------+ | | |
284 // | Yes | | |
285 // V | | |
286 // +------------------------------+ | | |
287 // | Update current_stream_index_ | | | |
288 // +------------------------------+ | | |
289 // | | | |
290 // V | | |
291 // +---------------------------+ | | |
292 // | Call Read() on new stream |<---+ | |
293 // | for deferred reads. | | |
294 // +---------------------------+ | |
295 // | | |
296 // V | |
297 // switch_cb(OK | ERROR) | |
298 // | |
299 // NOTE: Any AdaptiveDemuxerStream::Read() calls that occur during the stream | |
300 // switch will be deferred until the switch has completed. The callbacks | |
301 // will be queued on |read_cb_queue_|, but no Read() will be issued on the | |
302 // current stream. | |
303 void AdaptiveDemuxerStream::ChangeCurrentStream( | |
304 int index, | |
305 const SeekFunction& seek_function, | |
306 const PipelineStatusCB& switch_cb) { | |
307 DCHECK_GE(index, 0); | |
308 | |
309 bool start_switch = false; | |
310 | |
311 { | |
312 base::AutoLock auto_lock(lock_); | |
313 | |
314 DCHECK_LE(index, streams_.size()); | |
315 DCHECK(streams_[index].get()); | |
316 DCHECK_NE(index, current_stream_index_); | |
317 DCHECK(!IsSwitchPending_Locked()); | |
318 | |
319 // TODO(acolwell): Still need to handle the case where the stream has ended. | |
320 | |
321 switch_cb_ = switch_cb; | |
322 switch_index_ = index; | |
323 switch_seek_function_ = seek_function; | |
324 | |
325 start_switch = CanStartSwitch_Locked(); | |
326 } | |
327 | |
328 if (start_switch) | |
329 StartSwitch(); | |
330 } | |
331 | |
332 void AdaptiveDemuxerStream::OnReadDone(Buffer* buffer) { | |
333 ReadCallback read_cb; | |
334 bool start_switch = false; | |
335 | |
336 { | |
337 base::AutoLock auto_lock(lock_); | |
338 | |
339 pending_reads_--; | |
340 | |
341 DCHECK_GE(pending_reads_, 0); | |
342 DCHECK_GE(read_cb_queue_.size(), 0); | |
343 | |
344 read_cb = read_cb_queue_.front(); | |
345 read_cb_queue_.pop_front(); | |
346 | |
347 if (buffer && !buffer->IsEndOfStream()) | |
348 last_buffer_timestamp_ = buffer->GetTimestamp(); | |
349 | |
350 start_switch = IsSwitchPending_Locked() && CanStartSwitch_Locked(); | |
351 } | |
352 | |
353 if (!read_cb.is_null()) | |
354 read_cb.Run(buffer); | |
355 | |
356 if (start_switch) | |
357 StartSwitch(); | |
358 } | |
359 | |
360 bool AdaptiveDemuxerStream::IsSwitchPending_Locked() const { | |
361 lock_.AssertAcquired(); | |
362 return !switch_cb_.is_null(); | |
363 } | |
364 | |
365 bool AdaptiveDemuxerStream::CanStartSwitch_Locked() const { | |
366 lock_.AssertAcquired(); | |
367 | |
368 if (pending_reads_ == 0) { | |
369 DCHECK(IsSwitchPending_Locked()); | |
370 return true; | |
371 } | |
372 return false; | |
373 } | |
374 | |
375 void AdaptiveDemuxerStream::StartSwitch() { | |
376 SeekFunction seek_function; | |
377 base::TimeDelta seek_point; | |
378 | |
379 { | |
380 base::AutoLock auto_lock(lock_); | |
381 DCHECK(IsSwitchPending_Locked()); | |
382 DCHECK_EQ(pending_reads_, 0); | |
383 DCHECK_GE(switch_index_, 0); | |
384 | |
385 seek_point = last_buffer_timestamp_; | |
386 seek_function = switch_seek_function_; | |
387 | |
388 // TODO(acolwell): add code to call switch_cb_ if we are at the end of the | |
389 // stream now. | |
390 } | |
391 | |
392 if (seek_point == kNoTimestamp) { | |
393 // We haven't seen a buffer so there is no need to seek. Just move on to | |
394 // the next stage in the switch process. | |
395 OnSwitchSeekDone(kNoTimestamp, PIPELINE_OK); | |
396 return; | |
397 } | |
398 | |
399 DCHECK(!seek_function.is_null()); | |
400 seek_function.Run(seek_point, | |
401 base::Bind(&AdaptiveDemuxerStream::OnSwitchSeekDone, this)); | |
402 } | |
403 | |
404 void AdaptiveDemuxerStream::OnSwitchSeekDone(base::TimeDelta seek_time, | |
405 PipelineStatus status) { | |
406 DemuxerStream* stream = NULL; | |
407 PipelineStatusCB switch_cb; | |
408 int reads_to_request = 0; | |
409 bool needs_bitstream_converter_enabled = false; | |
410 | |
411 { | |
412 base::AutoLock auto_lock(lock_); | |
413 | |
414 if (status == PIPELINE_OK) { | |
415 DCHECK(streams_[switch_index_]); | |
416 | |
417 current_stream_index_ = switch_index_; | |
418 needs_bitstream_converter_enabled = bitstream_converter_enabled_; | |
419 } | |
420 | |
421 // Clear stream switch state. | |
422 switch_cb = switch_cb_; | |
423 switch_cb_.Reset(); | |
424 switch_index_ = -1; | |
425 switch_seek_function_.Reset(); | |
426 | |
427 // Get the number of outstanding Read()s on this object. | |
428 reads_to_request = read_cb_queue_.size(); | |
429 | |
430 DCHECK_EQ(pending_reads_, 0); | |
431 pending_reads_ = reads_to_request; | |
432 | |
433 stream = streams_[current_stream_index_]; | |
434 } | |
435 | |
91 if (needs_bitstream_converter_enabled) | 436 if (needs_bitstream_converter_enabled) |
92 EnableBitstreamConverter(); | 437 EnableBitstreamConverter(); |
438 | |
439 DCHECK(stream); | |
440 DCHECK(!switch_cb.is_null()); | |
441 | |
442 // Make the Read() calls that were deferred during the stream switch. | |
443 for(;reads_to_request > 0; --reads_to_request) | |
444 stream->Read(base::Bind(&AdaptiveDemuxerStream::OnReadDone, this)); | |
445 | |
446 // Signal that the stream switch has completed. | |
447 switch_cb.Run(status); | |
93 } | 448 } |
94 | 449 |
95 // | 450 // |
96 // AdaptiveDemuxer | 451 // AdaptiveDemuxer |
97 // | 452 // |
98 | 453 |
99 AdaptiveDemuxer::AdaptiveDemuxer(DemuxerVector const& demuxers, | 454 AdaptiveDemuxer::AdaptiveDemuxer(DemuxerVector const& demuxers, |
100 int initial_audio_demuxer_index, | 455 int initial_audio_demuxer_index, |
101 int initial_video_demuxer_index) | 456 int initial_video_demuxer_index) |
102 : demuxers_(demuxers), | 457 : demuxers_(demuxers), |
103 current_audio_demuxer_index_(initial_audio_demuxer_index), | 458 current_audio_demuxer_index_(initial_audio_demuxer_index), |
104 current_video_demuxer_index_(initial_video_demuxer_index) { | 459 current_video_demuxer_index_(initial_video_demuxer_index), |
460 playback_rate_(0), | |
461 switch_pending_(false), | |
462 stream_switch_manager_(NULL){ | |
Ami GONE FROM CHROMIUM
2011/05/25 05:39:01
Unnecessary?
acolwell GONE FROM CHROMIUM
2011/05/25 21:43:46
Done.
| |
105 DCHECK(!demuxers_.empty()); | 463 DCHECK(!demuxers_.empty()); |
106 DCHECK_GE(current_audio_demuxer_index_, -1); | 464 DCHECK_GE(current_audio_demuxer_index_, -1); |
107 DCHECK_GE(current_video_demuxer_index_, -1); | 465 DCHECK_GE(current_video_demuxer_index_, -1); |
108 DCHECK_LT(current_audio_demuxer_index_, static_cast<int>(demuxers_.size())); | 466 DCHECK_LT(current_audio_demuxer_index_, static_cast<int>(demuxers_.size())); |
109 DCHECK_LT(current_video_demuxer_index_, static_cast<int>(demuxers_.size())); | 467 DCHECK_LT(current_video_demuxer_index_, static_cast<int>(demuxers_.size())); |
110 DCHECK(current_audio_demuxer_index_ != -1 || | 468 DCHECK(current_audio_demuxer_index_ != -1 || |
111 current_video_demuxer_index_ != -1); | 469 current_video_demuxer_index_ != -1); |
112 AdaptiveDemuxerStream::StreamVector audio_streams, video_streams; | 470 AdaptiveDemuxerStream::StreamVector audio_streams, video_streams; |
471 StreamSwitchManager::StreamIdVector video_ids; | |
113 for (size_t i = 0; i < demuxers_.size(); ++i) { | 472 for (size_t i = 0; i < demuxers_.size(); ++i) { |
473 DemuxerStream* video = demuxers_[i]->GetStream(DemuxerStream::VIDEO); | |
114 audio_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::AUDIO)); | 474 audio_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::AUDIO)); |
115 video_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::VIDEO)); | 475 video_streams.push_back(video); |
476 if (video) | |
477 video_ids.push_back(i); | |
116 } | 478 } |
117 if (current_audio_demuxer_index_ >= 0) { | 479 if (current_audio_demuxer_index_ >= 0) { |
118 audio_stream_ = new AdaptiveDemuxerStream( | 480 audio_stream_ = new AdaptiveDemuxerStream( |
119 audio_streams, current_audio_demuxer_index_); | 481 audio_streams, current_audio_demuxer_index_); |
120 } | 482 } |
121 if (current_video_demuxer_index_ >= 0) { | 483 if (current_video_demuxer_index_ >= 0) { |
122 video_stream_ = new AdaptiveDemuxerStream( | 484 video_stream_ = new AdaptiveDemuxerStream( |
123 video_streams, current_video_demuxer_index_); | 485 video_streams, current_video_demuxer_index_); |
124 } | 486 } |
125 | 487 |
488 stream_switch_manager_ = new StreamSwitchManager(this, video_ids); | |
Ami GONE FROM CHROMIUM
2011/05/25 05:39:01
reset() more idiomatic for scoped_refptr?
acolwell GONE FROM CHROMIUM
2011/05/25 21:43:46
scoped_refptr doesn't have a reset().
| |
126 // TODO(fischman): any streams in the underlying demuxers that aren't being | 489 // 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 | 490 // 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 | 491 // hold data for them in memory, waiting for them to get drained by a |
129 // non-existent decoder. | 492 // non-existent decoder. |
130 } | 493 } |
131 | 494 |
132 AdaptiveDemuxer::~AdaptiveDemuxer() {} | 495 AdaptiveDemuxer::~AdaptiveDemuxer() {} |
133 | 496 |
134 void AdaptiveDemuxer::ChangeCurrentDemuxer(int audio_index, int video_index) { | 497 // Switches the current video stream. The diagram below describes the switch |
498 // process. | |
499 // +-------------------------------+ | |
500 // ChangeVideoStream() ---> | video_index | | |
501 // | == | Yes. | |
502 // | current_video_demuxer_index_? |--> done_cb(OK) | |
503 // +-------------------------------+ | |
504 // | No. | |
505 // V | |
506 // Call video_stream_->ChangeCurrentStream() | |
507 // | | |
508 // V | |
509 // (Wait for ChangeVideoStreamDone()) | |
510 // | | |
511 // V | |
512 // ChangeVideoStreamDone() | |
513 // | | |
514 // V | |
515 // +------------------------+ No | |
516 // | Was switch successful? |---> done_cb(ERROR) | |
517 // +------------------------+ | |
518 // | | |
519 // V | |
520 // Update current_video_demuxer_index_ | |
521 // & SetPlaybackRate() on new stream. | |
522 // | | |
523 // V | |
524 // done_cb(OK). | |
525 // | |
526 void AdaptiveDemuxer::ChangeVideoStream(int video_index, | |
527 const PipelineStatusCB& done_cb) { | |
135 // TODO(fischman): this is currently broken because when a new Demuxer is to | 528 // 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 | 529 // 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. | 530 // current Demuxer if it's no longer being used. |
531 bool switching_to_current_stream = false; | |
532 AdaptiveDemuxerStream::SeekFunction seek_function; | |
533 | |
534 { | |
535 base::AutoLock auto_lock(lock_); | |
536 | |
537 DCHECK(video_stream_); | |
538 DCHECK(!switch_pending_); | |
539 if (current_video_demuxer_index_ == video_index) { | |
540 switching_to_current_stream = true; | |
541 } else { | |
542 switch_pending_ = true; | |
543 seek_function = base::Bind(&AdaptiveDemuxer::StartStreamSwitchSeek, | |
544 this, | |
545 DemuxerStream::VIDEO, | |
546 video_index); | |
547 } | |
548 } | |
549 | |
550 if (switching_to_current_stream) { | |
551 done_cb.Run(PIPELINE_OK); | |
552 return; | |
553 } | |
554 | |
555 video_stream_->ChangeCurrentStream( | |
556 video_index, seek_function, | |
557 base::Bind(&AdaptiveDemuxer::ChangeVideoStreamDone, this, video_index, | |
558 done_cb)); | |
559 } | |
560 | |
561 int AdaptiveDemuxer::GetCurrentVideoId() const { | |
138 base::AutoLock auto_lock(lock_); | 562 base::AutoLock auto_lock(lock_); |
139 current_audio_demuxer_index_ = audio_index; | 563 return current_video_demuxer_index_; |
140 current_video_demuxer_index_ = video_index; | 564 } |
141 if (audio_stream_) | 565 |
142 audio_stream_->ChangeCurrentStream(audio_index); | 566 void AdaptiveDemuxer::ChangeVideoStreamDone(int new_stream_index, |
143 if (video_stream_) | 567 const PipelineStatusCB& done_cb, |
144 video_stream_->ChangeCurrentStream(video_index); | 568 PipelineStatus status) { |
569 { | |
570 base::AutoLock auto_lock(lock_); | |
571 | |
572 switch_pending_ = false; | |
573 | |
574 if (status == PIPELINE_OK) { | |
575 demuxers_[current_video_demuxer_index_]->SetPlaybackRate(0); | |
576 current_video_demuxer_index_ = new_stream_index; | |
577 demuxers_[current_video_demuxer_index_]->SetPlaybackRate(playback_rate_); | |
578 } | |
579 } | |
580 | |
581 done_cb.Run(status); | |
145 } | 582 } |
146 | 583 |
147 Demuxer* AdaptiveDemuxer::current_demuxer(DemuxerStream::Type type) { | 584 Demuxer* AdaptiveDemuxer::current_demuxer(DemuxerStream::Type type) { |
148 base::AutoLock auto_lock(lock_); | 585 base::AutoLock auto_lock(lock_); |
149 switch (type) { | 586 switch (type) { |
150 case DemuxerStream::AUDIO: | 587 case DemuxerStream::AUDIO: |
151 return (current_audio_demuxer_index_ < 0) ? NULL : | 588 return (current_audio_demuxer_index_ < 0) ? NULL : |
152 demuxers_[current_audio_demuxer_index_]; | 589 demuxers_[current_audio_demuxer_index_]; |
153 case DemuxerStream::VIDEO: | 590 case DemuxerStream::VIDEO: |
154 return (current_video_demuxer_index_ < 0) ? NULL : | 591 return (current_video_demuxer_index_ < 0) ? NULL : |
155 demuxers_[current_video_demuxer_index_]; | 592 demuxers_[current_video_demuxer_index_]; |
156 default: | 593 default: |
157 LOG(DFATAL) << "Unexpected type: " << type; | 594 LOG(DFATAL) << "Unexpected type: " << type; |
158 return NULL; | 595 return NULL; |
159 } | 596 } |
160 } | 597 } |
161 | 598 |
162 // Helper class that wraps a FilterCallback and expects to get called a set | 599 // Helper class that wraps a FilterCallback and expects to get called a set |
163 // number of times, after which the wrapped callback is fired (and deleted). | 600 // number of times, after which the wrapped callback is fired (and deleted). |
164 // | 601 // |
165 // TODO: Remove this class once Stop() is converted to FilterStatusCB. | 602 // TODO(acolwell): Remove this class once Stop() is converted to FilterStatusCB. |
166 class CountingCallback { | 603 class CountingCallback { |
167 public: | 604 public: |
168 CountingCallback(int count, FilterCallback* orig_cb) | 605 CountingCallback(int count, FilterCallback* orig_cb) |
169 : remaining_count_(count), orig_cb_(orig_cb) { | 606 : remaining_count_(count), orig_cb_(orig_cb) { |
170 DCHECK_GT(remaining_count_, 0); | 607 DCHECK_GT(remaining_count_, 0); |
171 DCHECK(orig_cb); | 608 DCHECK(orig_cb); |
172 } | 609 } |
173 | 610 |
174 FilterCallback* GetACallback() { | 611 FilterCallback* GetACallback() { |
175 return NewCallback(this, &CountingCallback::OnChildCallbackDone); | 612 return NewCallback(this, &CountingCallback::OnChildCallbackDone); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
234 | 671 |
235 base::Lock lock_; | 672 base::Lock lock_; |
236 int remaining_count_; | 673 int remaining_count_; |
237 FilterStatusCB orig_cb_; | 674 FilterStatusCB orig_cb_; |
238 PipelineStatus overall_status_; | 675 PipelineStatus overall_status_; |
239 | 676 |
240 DISALLOW_COPY_AND_ASSIGN(CountingStatusCB); | 677 DISALLOW_COPY_AND_ASSIGN(CountingStatusCB); |
241 }; | 678 }; |
242 | 679 |
243 void AdaptiveDemuxer::Stop(FilterCallback* callback) { | 680 void AdaptiveDemuxer::Stop(FilterCallback* callback) { |
681 stream_switch_manager_->Stop(); | |
682 | |
244 // Stop() must be called on all of the demuxers even though only one demuxer | 683 // 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 | 684 // is actively delivering audio and another one is delivering video. This |
246 // just satisfies the contract that all demuxers must have Stop() called on | 685 // just satisfies the contract that all demuxers must have Stop() called on |
247 // them before they are destroyed. | 686 // them before they are destroyed. |
248 // | 687 // |
249 // TODO: Remove CountingCallback once Stop() is converted to FilterStatusCB. | 688 // TODO(acolwell): Remove CountingCallback once Stop() is converted to |
689 // FilterStatusCB. | |
250 CountingCallback* wrapper = new CountingCallback(demuxers_.size(), callback); | 690 CountingCallback* wrapper = new CountingCallback(demuxers_.size(), callback); |
251 for (size_t i = 0; i < demuxers_.size(); ++i) | 691 for (size_t i = 0; i < demuxers_.size(); ++i) |
252 demuxers_[i]->Stop(wrapper->GetACallback()); | 692 demuxers_[i]->Stop(wrapper->GetACallback()); |
253 } | 693 } |
254 | 694 |
255 void AdaptiveDemuxer::Seek(base::TimeDelta time, const FilterStatusCB& cb) { | 695 void AdaptiveDemuxer::Seek(base::TimeDelta time, const FilterStatusCB& cb) { |
696 | |
697 if (audio_stream_) | |
698 audio_stream_->OnAdaptiveDemuxerSeek(time); | |
699 | |
700 if (video_stream_) | |
701 video_stream_->OnAdaptiveDemuxerSeek(time); | |
702 | |
256 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); | 703 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); |
257 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); | 704 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); |
258 int count = (audio ? 1 : 0) + (video && audio != video ? 1 : 0); | 705 int count = (audio ? 1 : 0) + (video && audio != video ? 1 : 0); |
259 CountingStatusCB* wrapper = new CountingStatusCB(count, cb); | 706 CountingStatusCB* wrapper = new CountingStatusCB(count, cb); |
260 if (audio) | 707 if (audio) |
261 audio->Seek(time, wrapper->GetACallback()); | 708 audio->Seek(time, wrapper->GetACallback()); |
262 if (video && audio != video) | 709 if (video && audio != video) |
263 video->Seek(time, wrapper->GetACallback()); | 710 video->Seek(time, wrapper->GetACallback()); |
264 } | 711 } |
265 | 712 |
266 void AdaptiveDemuxer::OnAudioRendererDisabled() { | 713 void AdaptiveDemuxer::OnAudioRendererDisabled() { |
267 for (size_t i = 0; i < demuxers_.size(); ++i) | 714 for (size_t i = 0; i < demuxers_.size(); ++i) |
268 demuxers_[i]->OnAudioRendererDisabled(); | 715 demuxers_[i]->OnAudioRendererDisabled(); |
269 } | 716 } |
270 | 717 |
271 void AdaptiveDemuxer::set_host(FilterHost* filter_host) { | 718 void AdaptiveDemuxer::set_host(FilterHost* filter_host) { |
272 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); | 719 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); |
273 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); | 720 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); |
274 if (audio) audio->set_host(filter_host); | 721 if (audio) audio->set_host(filter_host); |
275 if (video && audio != video) video->set_host(filter_host); | 722 if (video && audio != video) video->set_host(filter_host); |
276 } | 723 } |
277 | 724 |
278 void AdaptiveDemuxer::SetPlaybackRate(float playback_rate) { | 725 void AdaptiveDemuxer::SetPlaybackRate(float playback_rate) { |
726 { | |
727 base::AutoLock auto_lock(lock_); | |
728 if (playback_rate_ == 0 && playback_rate > 0) { | |
729 stream_switch_manager_->Play(); | |
730 } else if (playback_rate_ > 0 && playback_rate == 0) { | |
731 stream_switch_manager_->Pause(); | |
732 } | |
733 | |
734 playback_rate_ = playback_rate; | |
735 } | |
736 | |
279 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); | 737 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); |
280 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); | 738 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); |
281 if (audio) audio->SetPlaybackRate(playback_rate); | 739 if (audio) audio->SetPlaybackRate(playback_rate); |
282 if (video && audio != video) video->SetPlaybackRate(playback_rate); | 740 if (video && audio != video) video->SetPlaybackRate(playback_rate); |
283 } | 741 } |
284 | 742 |
285 void AdaptiveDemuxer::SetPreload(Preload preload) { | 743 void AdaptiveDemuxer::SetPreload(Preload preload) { |
286 for (size_t i = 0; i < demuxers_.size(); ++i) | 744 for (size_t i = 0; i < demuxers_.size(); ++i) |
287 demuxers_[i]->SetPreload(preload); | 745 demuxers_[i]->SetPreload(preload); |
288 } | 746 } |
289 | 747 |
290 scoped_refptr<DemuxerStream> AdaptiveDemuxer::GetStream( | 748 scoped_refptr<DemuxerStream> AdaptiveDemuxer::GetStream( |
291 DemuxerStream::Type type) { | 749 DemuxerStream::Type type) { |
292 switch (type) { | 750 switch (type) { |
293 case DemuxerStream::AUDIO: | 751 case DemuxerStream::AUDIO: |
294 return audio_stream_; | 752 return audio_stream_; |
295 case DemuxerStream::VIDEO: | 753 case DemuxerStream::VIDEO: |
296 return video_stream_; | 754 return video_stream_; |
297 default: | 755 default: |
298 LOG(DFATAL) << "Unexpected type " << type; | 756 LOG(DFATAL) << "Unexpected type " << type; |
299 return NULL; | 757 return NULL; |
300 } | 758 } |
301 } | 759 } |
302 | 760 |
761 void AdaptiveDemuxer::StartStreamSwitchSeek( | |
762 DemuxerStream::Type type, | |
763 int stream_index, | |
764 base::TimeDelta seek_time, | |
765 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb) { | |
766 DCHECK_GE(stream_index, 0); | |
767 DCHECK(!seek_cb.is_null()); | |
768 | |
769 Demuxer* demuxer = NULL; | |
770 base::TimeDelta seek_point; | |
771 FilterStatusCB filter_cb; | |
772 | |
773 { | |
774 base::AutoLock auto_lock(lock_); | |
775 | |
776 demuxer = demuxers_[stream_index]; | |
777 | |
778 if (GetSeekTimeAfter(demuxer->GetStream(type)->GetAVStream(), seek_time, | |
779 &seek_point)) { | |
780 // We found a seek point. | |
781 filter_cb = base::Bind(&AdaptiveDemuxer::OnStreamSeekDone, | |
782 seek_cb, seek_point); | |
783 } else { | |
784 // We didn't find a seek point. Assume we don't have index data for it | |
785 // yet. Seek to the specified time to force index data to be loaded. | |
786 seek_point = seek_time; | |
787 filter_cb = base::Bind(&AdaptiveDemuxer::OnIndexSeekDone, this, | |
788 type, stream_index, seek_time, seek_cb); | |
789 } | |
790 } | |
791 | |
792 DCHECK(demuxer); | |
793 demuxer->Seek(seek_point, filter_cb); | |
794 } | |
795 | |
796 void AdaptiveDemuxer::OnIndexSeekDone( | |
797 DemuxerStream::Type type, | |
798 int stream_index, | |
799 base::TimeDelta seek_time, | |
800 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb, | |
801 PipelineStatus status) { | |
802 base::TimeDelta seek_point; | |
803 FilterStatusCB filter_cb; | |
804 | |
805 Demuxer* demuxer = NULL; | |
806 | |
807 if (status != PIPELINE_OK) { | |
808 seek_cb.Run(base::TimeDelta(), status); | |
809 return; | |
810 } | |
811 | |
812 { | |
813 base::AutoLock auto_lock(lock_); | |
814 | |
815 demuxer = demuxers_[stream_index]; | |
816 DCHECK(demuxer); | |
817 | |
818 // Look for a seek point now that we have index data. | |
819 if (GetSeekTimeAfter(demuxer->GetStream(type)->GetAVStream(), seek_time, | |
820 &seek_point)) { | |
821 filter_cb = base::Bind(&AdaptiveDemuxer::OnStreamSeekDone, | |
822 seek_cb, seek_point); | |
823 } else { | |
824 // Failed again to find a seek point. Clear the demuxer so that | |
825 // a seek error will be reported. | |
826 demuxer = NULL; | |
827 } | |
828 } | |
829 | |
830 if (!demuxer) { | |
831 seek_cb.Run(base::TimeDelta(), PIPELINE_ERROR_INITIALIZATION_FAILED); | |
832 return; | |
833 } | |
834 | |
835 demuxer->Seek(seek_point, filter_cb); | |
836 } | |
837 | |
838 // static | |
839 void AdaptiveDemuxer::OnStreamSeekDone( | |
840 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb, | |
841 base::TimeDelta seek_point, | |
842 PipelineStatus status) { | |
843 seek_cb.Run(seek_point, status); | |
844 } | |
845 | |
303 // | 846 // |
304 // AdaptiveDemuxerFactory | 847 // AdaptiveDemuxerFactory |
305 // | 848 // |
306 | 849 |
307 AdaptiveDemuxerFactory::AdaptiveDemuxerFactory( | 850 AdaptiveDemuxerFactory::AdaptiveDemuxerFactory( |
308 DemuxerFactory* delegate_factory) | 851 DemuxerFactory* delegate_factory) |
309 : delegate_factory_(delegate_factory) { | 852 : delegate_factory_(delegate_factory) { |
310 DCHECK(delegate_factory); | 853 DCHECK(delegate_factory); |
311 } | 854 } |
312 | 855 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
485 delete cb; | 1028 delete cb; |
486 return; | 1029 return; |
487 } | 1030 } |
488 DemuxerAccumulator* accumulator = new DemuxerAccumulator( | 1031 DemuxerAccumulator* accumulator = new DemuxerAccumulator( |
489 audio_index, video_index, urls.size(), cb); | 1032 audio_index, video_index, urls.size(), cb); |
490 for (size_t i = 0; i < urls.size(); ++i) | 1033 for (size_t i = 0; i < urls.size(); ++i) |
491 delegate_factory_->Build(urls[i], accumulator->GetNthCallback(i)); | 1034 delegate_factory_->Build(urls[i], accumulator->GetNthCallback(i)); |
492 } | 1035 } |
493 | 1036 |
494 } // namespace media | 1037 } // namespace media |
OLD | NEW |