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 seek_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_ = seek_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 std::swap(switch_cb, switch_cb_); |
| 423 switch_index_ = -1; |
| 424 switch_seek_function_.Reset(); |
| 425 |
| 426 // Get the number of outstanding Read()s on this object. |
| 427 reads_to_request = read_cb_queue_.size(); |
| 428 |
| 429 DCHECK_EQ(pending_reads_, 0); |
| 430 pending_reads_ = reads_to_request; |
| 431 |
| 432 stream = streams_[current_stream_index_]; |
| 433 } |
| 434 |
91 if (needs_bitstream_converter_enabled) | 435 if (needs_bitstream_converter_enabled) |
92 EnableBitstreamConverter(); | 436 EnableBitstreamConverter(); |
| 437 |
| 438 DCHECK(stream); |
| 439 DCHECK(!switch_cb.is_null()); |
| 440 |
| 441 // Make the Read() calls that were deferred during the stream switch. |
| 442 for (;reads_to_request > 0; --reads_to_request) |
| 443 stream->Read(base::Bind(&AdaptiveDemuxerStream::OnReadDone, this)); |
| 444 |
| 445 // Signal that the stream switch has completed. |
| 446 switch_cb.Run(status); |
93 } | 447 } |
94 | 448 |
95 // | 449 // |
96 // AdaptiveDemuxer | 450 // AdaptiveDemuxer |
97 // | 451 // |
98 | 452 |
99 AdaptiveDemuxer::AdaptiveDemuxer(DemuxerVector const& demuxers, | 453 AdaptiveDemuxer::AdaptiveDemuxer(DemuxerVector const& demuxers, |
100 int initial_audio_demuxer_index, | 454 int initial_audio_demuxer_index, |
101 int initial_video_demuxer_index) | 455 int initial_video_demuxer_index) |
102 : demuxers_(demuxers), | 456 : demuxers_(demuxers), |
103 current_audio_demuxer_index_(initial_audio_demuxer_index), | 457 current_audio_demuxer_index_(initial_audio_demuxer_index), |
104 current_video_demuxer_index_(initial_video_demuxer_index) { | 458 current_video_demuxer_index_(initial_video_demuxer_index), |
| 459 playback_rate_(0), |
| 460 switch_pending_(false) { |
105 DCHECK(!demuxers_.empty()); | 461 DCHECK(!demuxers_.empty()); |
106 DCHECK_GE(current_audio_demuxer_index_, -1); | 462 DCHECK_GE(current_audio_demuxer_index_, -1); |
107 DCHECK_GE(current_video_demuxer_index_, -1); | 463 DCHECK_GE(current_video_demuxer_index_, -1); |
108 DCHECK_LT(current_audio_demuxer_index_, static_cast<int>(demuxers_.size())); | 464 DCHECK_LT(current_audio_demuxer_index_, static_cast<int>(demuxers_.size())); |
109 DCHECK_LT(current_video_demuxer_index_, static_cast<int>(demuxers_.size())); | 465 DCHECK_LT(current_video_demuxer_index_, static_cast<int>(demuxers_.size())); |
110 DCHECK(current_audio_demuxer_index_ != -1 || | 466 DCHECK(current_audio_demuxer_index_ != -1 || |
111 current_video_demuxer_index_ != -1); | 467 current_video_demuxer_index_ != -1); |
112 AdaptiveDemuxerStream::StreamVector audio_streams, video_streams; | 468 AdaptiveDemuxerStream::StreamVector audio_streams, video_streams; |
| 469 StreamSwitchManager::StreamIdVector video_ids; |
113 for (size_t i = 0; i < demuxers_.size(); ++i) { | 470 for (size_t i = 0; i < demuxers_.size(); ++i) { |
| 471 DemuxerStream* video = demuxers_[i]->GetStream(DemuxerStream::VIDEO); |
114 audio_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::AUDIO)); | 472 audio_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::AUDIO)); |
115 video_streams.push_back(demuxers_[i]->GetStream(DemuxerStream::VIDEO)); | 473 video_streams.push_back(video); |
| 474 if (video) |
| 475 video_ids.push_back(i); |
116 } | 476 } |
117 if (current_audio_demuxer_index_ >= 0) { | 477 if (current_audio_demuxer_index_ >= 0) { |
118 audio_stream_ = new AdaptiveDemuxerStream( | 478 audio_stream_ = new AdaptiveDemuxerStream( |
119 audio_streams, current_audio_demuxer_index_); | 479 audio_streams, current_audio_demuxer_index_); |
120 } | 480 } |
121 if (current_video_demuxer_index_ >= 0) { | 481 if (current_video_demuxer_index_ >= 0) { |
122 video_stream_ = new AdaptiveDemuxerStream( | 482 video_stream_ = new AdaptiveDemuxerStream( |
123 video_streams, current_video_demuxer_index_); | 483 video_streams, current_video_demuxer_index_); |
124 } | 484 } |
125 | 485 |
| 486 stream_switch_manager_ = new StreamSwitchManager(this, video_ids); |
126 // TODO(fischman): any streams in the underlying demuxers that aren't being | 487 // 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 | 488 // 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 | 489 // hold data for them in memory, waiting for them to get drained by a |
129 // non-existent decoder. | 490 // non-existent decoder. |
130 } | 491 } |
131 | 492 |
132 AdaptiveDemuxer::~AdaptiveDemuxer() {} | 493 AdaptiveDemuxer::~AdaptiveDemuxer() {} |
133 | 494 |
134 void AdaptiveDemuxer::ChangeCurrentDemuxer(int audio_index, int video_index) { | 495 // Switches the current video stream. The diagram below describes the switch |
| 496 // process. |
| 497 // +-------------------------------+ |
| 498 // ChangeVideoStream() ---> | video_index | |
| 499 // | == | Yes. |
| 500 // | current_video_demuxer_index_? |--> done_cb(OK) |
| 501 // +-------------------------------+ |
| 502 // | No. |
| 503 // V |
| 504 // Call video_stream_->ChangeCurrentStream() |
| 505 // | |
| 506 // V |
| 507 // (Wait for ChangeVideoStreamDone()) |
| 508 // | |
| 509 // V |
| 510 // ChangeVideoStreamDone() |
| 511 // | |
| 512 // V |
| 513 // +------------------------+ No |
| 514 // | Was switch successful? |---> done_cb(ERROR) |
| 515 // +------------------------+ |
| 516 // | |
| 517 // V |
| 518 // Update current_video_demuxer_index_ |
| 519 // & SetPlaybackRate() on new stream. |
| 520 // | |
| 521 // V |
| 522 // done_cb(OK). |
| 523 // |
| 524 void AdaptiveDemuxer::ChangeVideoStream(int video_index, |
| 525 const PipelineStatusCB& done_cb) { |
135 // TODO(fischman): this is currently broken because when a new Demuxer is to | 526 // 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 | 527 // 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. | 528 // current Demuxer if it's no longer being used. |
| 529 bool switching_to_current_stream = false; |
| 530 AdaptiveDemuxerStream::SeekFunction seek_function; |
| 531 |
| 532 { |
| 533 base::AutoLock auto_lock(lock_); |
| 534 |
| 535 DCHECK(video_stream_); |
| 536 DCHECK(!switch_pending_); |
| 537 if (current_video_demuxer_index_ == video_index) { |
| 538 switching_to_current_stream = true; |
| 539 } else { |
| 540 switch_pending_ = true; |
| 541 seek_function = base::Bind(&AdaptiveDemuxer::StartStreamSwitchSeek, |
| 542 this, |
| 543 DemuxerStream::VIDEO, |
| 544 video_index); |
| 545 } |
| 546 } |
| 547 |
| 548 if (switching_to_current_stream) { |
| 549 done_cb.Run(PIPELINE_OK); |
| 550 return; |
| 551 } |
| 552 |
| 553 video_stream_->ChangeCurrentStream( |
| 554 video_index, seek_function, |
| 555 base::Bind(&AdaptiveDemuxer::ChangeVideoStreamDone, this, video_index, |
| 556 done_cb)); |
| 557 } |
| 558 |
| 559 int AdaptiveDemuxer::GetCurrentVideoId() const { |
138 base::AutoLock auto_lock(lock_); | 560 base::AutoLock auto_lock(lock_); |
139 current_audio_demuxer_index_ = audio_index; | 561 return current_video_demuxer_index_; |
140 current_video_demuxer_index_ = video_index; | 562 } |
141 if (audio_stream_) | 563 |
142 audio_stream_->ChangeCurrentStream(audio_index); | 564 void AdaptiveDemuxer::ChangeVideoStreamDone(int new_stream_index, |
143 if (video_stream_) | 565 const PipelineStatusCB& done_cb, |
144 video_stream_->ChangeCurrentStream(video_index); | 566 PipelineStatus status) { |
| 567 { |
| 568 base::AutoLock auto_lock(lock_); |
| 569 |
| 570 switch_pending_ = false; |
| 571 |
| 572 if (status == PIPELINE_OK) { |
| 573 demuxers_[current_video_demuxer_index_]->SetPlaybackRate(0); |
| 574 current_video_demuxer_index_ = new_stream_index; |
| 575 demuxers_[current_video_demuxer_index_]->SetPlaybackRate(playback_rate_); |
| 576 } |
| 577 } |
| 578 |
| 579 done_cb.Run(status); |
145 } | 580 } |
146 | 581 |
147 Demuxer* AdaptiveDemuxer::current_demuxer(DemuxerStream::Type type) { | 582 Demuxer* AdaptiveDemuxer::current_demuxer(DemuxerStream::Type type) { |
148 base::AutoLock auto_lock(lock_); | 583 base::AutoLock auto_lock(lock_); |
149 switch (type) { | 584 switch (type) { |
150 case DemuxerStream::AUDIO: | 585 case DemuxerStream::AUDIO: |
151 return (current_audio_demuxer_index_ < 0) ? NULL : | 586 return (current_audio_demuxer_index_ < 0) ? NULL : |
152 demuxers_[current_audio_demuxer_index_]; | 587 demuxers_[current_audio_demuxer_index_]; |
153 case DemuxerStream::VIDEO: | 588 case DemuxerStream::VIDEO: |
154 return (current_video_demuxer_index_ < 0) ? NULL : | 589 return (current_video_demuxer_index_ < 0) ? NULL : |
155 demuxers_[current_video_demuxer_index_]; | 590 demuxers_[current_video_demuxer_index_]; |
156 default: | 591 default: |
157 LOG(DFATAL) << "Unexpected type: " << type; | 592 LOG(DFATAL) << "Unexpected type: " << type; |
158 return NULL; | 593 return NULL; |
159 } | 594 } |
160 } | 595 } |
161 | 596 |
162 // Helper class that wraps a FilterCallback and expects to get called a set | 597 // 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). | 598 // number of times, after which the wrapped callback is fired (and deleted). |
164 // | 599 // |
165 // TODO: Remove this class once Stop() is converted to FilterStatusCB. | 600 // TODO(acolwell): Remove this class once Stop() is converted to FilterStatusCB. |
166 class CountingCallback { | 601 class CountingCallback { |
167 public: | 602 public: |
168 CountingCallback(int count, FilterCallback* orig_cb) | 603 CountingCallback(int count, FilterCallback* orig_cb) |
169 : remaining_count_(count), orig_cb_(orig_cb) { | 604 : remaining_count_(count), orig_cb_(orig_cb) { |
170 DCHECK_GT(remaining_count_, 0); | 605 DCHECK_GT(remaining_count_, 0); |
171 DCHECK(orig_cb); | 606 DCHECK(orig_cb); |
172 } | 607 } |
173 | 608 |
174 FilterCallback* GetACallback() { | 609 FilterCallback* GetACallback() { |
175 return NewCallback(this, &CountingCallback::OnChildCallbackDone); | 610 return NewCallback(this, &CountingCallback::OnChildCallbackDone); |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
234 | 669 |
235 base::Lock lock_; | 670 base::Lock lock_; |
236 int remaining_count_; | 671 int remaining_count_; |
237 FilterStatusCB orig_cb_; | 672 FilterStatusCB orig_cb_; |
238 PipelineStatus overall_status_; | 673 PipelineStatus overall_status_; |
239 | 674 |
240 DISALLOW_COPY_AND_ASSIGN(CountingStatusCB); | 675 DISALLOW_COPY_AND_ASSIGN(CountingStatusCB); |
241 }; | 676 }; |
242 | 677 |
243 void AdaptiveDemuxer::Stop(FilterCallback* callback) { | 678 void AdaptiveDemuxer::Stop(FilterCallback* callback) { |
| 679 stream_switch_manager_->Stop(); |
| 680 |
244 // Stop() must be called on all of the demuxers even though only one demuxer | 681 // 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 | 682 // is actively delivering audio and another one is delivering video. This |
246 // just satisfies the contract that all demuxers must have Stop() called on | 683 // just satisfies the contract that all demuxers must have Stop() called on |
247 // them before they are destroyed. | 684 // them before they are destroyed. |
248 // | 685 // |
249 // TODO: Remove CountingCallback once Stop() is converted to FilterStatusCB. | 686 // TODO(acolwell): Remove CountingCallback once Stop() is converted to |
| 687 // FilterStatusCB. |
250 CountingCallback* wrapper = new CountingCallback(demuxers_.size(), callback); | 688 CountingCallback* wrapper = new CountingCallback(demuxers_.size(), callback); |
251 for (size_t i = 0; i < demuxers_.size(); ++i) | 689 for (size_t i = 0; i < demuxers_.size(); ++i) |
252 demuxers_[i]->Stop(wrapper->GetACallback()); | 690 demuxers_[i]->Stop(wrapper->GetACallback()); |
253 } | 691 } |
254 | 692 |
255 void AdaptiveDemuxer::Seek(base::TimeDelta time, const FilterStatusCB& cb) { | 693 void AdaptiveDemuxer::Seek(base::TimeDelta time, const FilterStatusCB& cb) { |
| 694 if (audio_stream_) |
| 695 audio_stream_->OnAdaptiveDemuxerSeek(time); |
| 696 |
| 697 if (video_stream_) |
| 698 video_stream_->OnAdaptiveDemuxerSeek(time); |
| 699 |
256 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); | 700 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); |
257 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); | 701 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); |
258 int count = (audio ? 1 : 0) + (video && audio != video ? 1 : 0); | 702 int count = (audio ? 1 : 0) + (video && audio != video ? 1 : 0); |
259 CountingStatusCB* wrapper = new CountingStatusCB(count, cb); | 703 CountingStatusCB* wrapper = new CountingStatusCB(count, cb); |
260 if (audio) | 704 if (audio) |
261 audio->Seek(time, wrapper->GetACallback()); | 705 audio->Seek(time, wrapper->GetACallback()); |
262 if (video && audio != video) | 706 if (video && audio != video) |
263 video->Seek(time, wrapper->GetACallback()); | 707 video->Seek(time, wrapper->GetACallback()); |
264 } | 708 } |
265 | 709 |
266 void AdaptiveDemuxer::OnAudioRendererDisabled() { | 710 void AdaptiveDemuxer::OnAudioRendererDisabled() { |
267 for (size_t i = 0; i < demuxers_.size(); ++i) | 711 for (size_t i = 0; i < demuxers_.size(); ++i) |
268 demuxers_[i]->OnAudioRendererDisabled(); | 712 demuxers_[i]->OnAudioRendererDisabled(); |
269 } | 713 } |
270 | 714 |
271 void AdaptiveDemuxer::set_host(FilterHost* filter_host) { | 715 void AdaptiveDemuxer::set_host(FilterHost* filter_host) { |
272 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); | 716 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); |
273 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); | 717 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); |
274 if (audio) audio->set_host(filter_host); | 718 if (audio) audio->set_host(filter_host); |
275 if (video && audio != video) video->set_host(filter_host); | 719 if (video && audio != video) video->set_host(filter_host); |
276 } | 720 } |
277 | 721 |
278 void AdaptiveDemuxer::SetPlaybackRate(float playback_rate) { | 722 void AdaptiveDemuxer::SetPlaybackRate(float playback_rate) { |
| 723 { |
| 724 base::AutoLock auto_lock(lock_); |
| 725 if (playback_rate_ == 0 && playback_rate > 0) { |
| 726 stream_switch_manager_->Play(); |
| 727 } else if (playback_rate_ > 0 && playback_rate == 0) { |
| 728 stream_switch_manager_->Pause(); |
| 729 } |
| 730 |
| 731 playback_rate_ = playback_rate; |
| 732 } |
| 733 |
279 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); | 734 Demuxer* audio = current_demuxer(DemuxerStream::AUDIO); |
280 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); | 735 Demuxer* video = current_demuxer(DemuxerStream::VIDEO); |
281 if (audio) audio->SetPlaybackRate(playback_rate); | 736 if (audio) audio->SetPlaybackRate(playback_rate); |
282 if (video && audio != video) video->SetPlaybackRate(playback_rate); | 737 if (video && audio != video) video->SetPlaybackRate(playback_rate); |
283 } | 738 } |
284 | 739 |
285 void AdaptiveDemuxer::SetPreload(Preload preload) { | 740 void AdaptiveDemuxer::SetPreload(Preload preload) { |
286 for (size_t i = 0; i < demuxers_.size(); ++i) | 741 for (size_t i = 0; i < demuxers_.size(); ++i) |
287 demuxers_[i]->SetPreload(preload); | 742 demuxers_[i]->SetPreload(preload); |
288 } | 743 } |
289 | 744 |
290 scoped_refptr<DemuxerStream> AdaptiveDemuxer::GetStream( | 745 scoped_refptr<DemuxerStream> AdaptiveDemuxer::GetStream( |
291 DemuxerStream::Type type) { | 746 DemuxerStream::Type type) { |
292 switch (type) { | 747 switch (type) { |
293 case DemuxerStream::AUDIO: | 748 case DemuxerStream::AUDIO: |
294 return audio_stream_; | 749 return audio_stream_; |
295 case DemuxerStream::VIDEO: | 750 case DemuxerStream::VIDEO: |
296 return video_stream_; | 751 return video_stream_; |
297 default: | 752 default: |
298 LOG(DFATAL) << "Unexpected type " << type; | 753 LOG(DFATAL) << "Unexpected type " << type; |
299 return NULL; | 754 return NULL; |
300 } | 755 } |
301 } | 756 } |
302 | 757 |
| 758 void AdaptiveDemuxer::StartStreamSwitchSeek( |
| 759 DemuxerStream::Type type, |
| 760 int stream_index, |
| 761 base::TimeDelta seek_time, |
| 762 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb) { |
| 763 DCHECK_GE(stream_index, 0); |
| 764 DCHECK(!seek_cb.is_null()); |
| 765 |
| 766 Demuxer* demuxer = NULL; |
| 767 base::TimeDelta seek_point; |
| 768 FilterStatusCB filter_cb; |
| 769 |
| 770 { |
| 771 base::AutoLock auto_lock(lock_); |
| 772 |
| 773 demuxer = demuxers_[stream_index]; |
| 774 |
| 775 if (GetSeekTimeAfter(demuxer->GetStream(type)->GetAVStream(), seek_time, |
| 776 &seek_point)) { |
| 777 // We found a seek point. |
| 778 filter_cb = base::Bind(&AdaptiveDemuxer::OnStreamSeekDone, |
| 779 seek_cb, seek_point); |
| 780 } else { |
| 781 // We didn't find a seek point. Assume we don't have index data for it |
| 782 // yet. Seek to the specified time to force index data to be loaded. |
| 783 seek_point = seek_time; |
| 784 filter_cb = base::Bind(&AdaptiveDemuxer::OnIndexSeekDone, this, |
| 785 type, stream_index, seek_time, seek_cb); |
| 786 } |
| 787 } |
| 788 |
| 789 DCHECK(demuxer); |
| 790 demuxer->Seek(seek_point, filter_cb); |
| 791 } |
| 792 |
| 793 void AdaptiveDemuxer::OnIndexSeekDone( |
| 794 DemuxerStream::Type type, |
| 795 int stream_index, |
| 796 base::TimeDelta seek_time, |
| 797 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb, |
| 798 PipelineStatus status) { |
| 799 base::TimeDelta seek_point; |
| 800 FilterStatusCB filter_cb; |
| 801 |
| 802 Demuxer* demuxer = NULL; |
| 803 |
| 804 if (status != PIPELINE_OK) { |
| 805 seek_cb.Run(base::TimeDelta(), status); |
| 806 return; |
| 807 } |
| 808 |
| 809 { |
| 810 base::AutoLock auto_lock(lock_); |
| 811 |
| 812 demuxer = demuxers_[stream_index]; |
| 813 DCHECK(demuxer); |
| 814 |
| 815 // Look for a seek point now that we have index data. |
| 816 if (GetSeekTimeAfter(demuxer->GetStream(type)->GetAVStream(), seek_time, |
| 817 &seek_point)) { |
| 818 filter_cb = base::Bind(&AdaptiveDemuxer::OnStreamSeekDone, |
| 819 seek_cb, seek_point); |
| 820 } else { |
| 821 // Failed again to find a seek point. Clear the demuxer so that |
| 822 // a seek error will be reported. |
| 823 demuxer = NULL; |
| 824 } |
| 825 } |
| 826 |
| 827 if (!demuxer) { |
| 828 seek_cb.Run(base::TimeDelta(), PIPELINE_ERROR_INITIALIZATION_FAILED); |
| 829 return; |
| 830 } |
| 831 |
| 832 demuxer->Seek(seek_point, filter_cb); |
| 833 } |
| 834 |
| 835 // static |
| 836 void AdaptiveDemuxer::OnStreamSeekDone( |
| 837 const AdaptiveDemuxerStream::SeekFunctionCB& seek_cb, |
| 838 base::TimeDelta seek_point, |
| 839 PipelineStatus status) { |
| 840 seek_cb.Run(seek_point, status); |
| 841 } |
| 842 |
303 // | 843 // |
304 // AdaptiveDemuxerFactory | 844 // AdaptiveDemuxerFactory |
305 // | 845 // |
306 | 846 |
307 AdaptiveDemuxerFactory::AdaptiveDemuxerFactory( | 847 AdaptiveDemuxerFactory::AdaptiveDemuxerFactory( |
308 DemuxerFactory* delegate_factory) | 848 DemuxerFactory* delegate_factory) |
309 : delegate_factory_(delegate_factory) { | 849 : delegate_factory_(delegate_factory) { |
310 DCHECK(delegate_factory); | 850 DCHECK(delegate_factory); |
311 } | 851 } |
312 | 852 |
(...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
485 delete cb; | 1025 delete cb; |
486 return; | 1026 return; |
487 } | 1027 } |
488 DemuxerAccumulator* accumulator = new DemuxerAccumulator( | 1028 DemuxerAccumulator* accumulator = new DemuxerAccumulator( |
489 audio_index, video_index, urls.size(), cb); | 1029 audio_index, video_index, urls.size(), cb); |
490 for (size_t i = 0; i < urls.size(); ++i) | 1030 for (size_t i = 0; i < urls.size(); ++i) |
491 delegate_factory_->Build(urls[i], accumulator->GetNthCallback(i)); | 1031 delegate_factory_->Build(urls[i], accumulator->GetNthCallback(i)); |
492 } | 1032 } |
493 | 1033 |
494 } // namespace media | 1034 } // namespace media |
OLD | NEW |