Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 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 "media/filters/video_renderer_base.h" | 5 #include "media/filters/video_renderer_base.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/callback.h" | 8 #include "base/callback.h" |
| 9 #include "base/callback_helpers.h" | 9 #include "base/callback_helpers.h" |
| 10 #include "base/message_loop.h" | |
| 10 #include "base/threading/platform_thread.h" | 11 #include "base/threading/platform_thread.h" |
| 11 #include "media/base/buffers.h" | 12 #include "media/base/buffers.h" |
| 12 #include "media/base/limits.h" | 13 #include "media/base/limits.h" |
| 13 #include "media/base/pipeline.h" | 14 #include "media/base/pipeline.h" |
| 14 #include "media/base/video_frame.h" | 15 #include "media/base/video_frame.h" |
| 15 | 16 |
| 16 namespace media { | 17 namespace media { |
| 17 | 18 |
| 18 base::TimeDelta VideoRendererBase::kMaxLastFrameDuration() { | 19 base::TimeDelta VideoRendererBase::kMaxLastFrameDuration() { |
| 19 return base::TimeDelta::FromMilliseconds(250); | 20 return base::TimeDelta::FromMilliseconds(250); |
| 20 } | 21 } |
| 21 | 22 |
| 22 VideoRendererBase::VideoRendererBase(const base::Closure& paint_cb, | 23 VideoRendererBase::VideoRendererBase( |
| 23 const SetOpaqueCB& set_opaque_cb, | 24 const base::Closure& paint_cb, |
| 24 bool drop_frames) | 25 const SetOpaqueCB& set_opaque_cb, |
| 26 bool drop_frames, | |
| 27 const scoped_refptr<base::MessageLoopProxy>& message_loop) | |
| 25 : frame_available_(&lock_), | 28 : frame_available_(&lock_), |
| 26 state_(kUninitialized), | 29 state_(kUninitialized), |
| 27 thread_(base::kNullThreadHandle), | 30 thread_(base::kNullThreadHandle), |
| 28 pending_read_(false), | 31 pending_read_(false), |
| 29 pending_paint_(false), | 32 pending_paint_(false), |
| 30 pending_paint_with_last_available_(false), | 33 pending_paint_with_last_available_(false), |
| 31 drop_frames_(drop_frames), | 34 drop_frames_(drop_frames), |
| 32 playback_rate_(0), | 35 playback_rate_(0), |
| 33 paint_cb_(paint_cb), | 36 paint_cb_(paint_cb), |
| 34 set_opaque_cb_(set_opaque_cb) { | 37 set_opaque_cb_(set_opaque_cb), |
| 38 decoder_message_loop_(message_loop) { | |
|
Ami GONE FROM CHROMIUM
2012/11/29 23:34:23
Could DCHECK that this is *not* the current loop,
| |
| 35 DCHECK(!paint_cb_.is_null()); | 39 DCHECK(!paint_cb_.is_null()); |
| 36 } | 40 } |
| 37 | 41 |
| 38 void VideoRendererBase::Play(const base::Closure& callback) { | 42 void VideoRendererBase::Play(const base::Closure& callback) { |
| 39 base::AutoLock auto_lock(lock_); | 43 base::AutoLock auto_lock(lock_); |
| 40 DCHECK_EQ(kPrerolled, state_); | 44 DCHECK_EQ(kPrerolled, state_); |
| 41 state_ = kPlaying; | 45 state_ = kPlaying; |
| 42 callback.Run(); | 46 callback.Run(); |
| 43 } | 47 } |
| 44 | 48 |
| 45 void VideoRendererBase::Pause(const base::Closure& callback) { | 49 void VideoRendererBase::Pause(const base::Closure& callback) { |
| 46 base::AutoLock auto_lock(lock_); | 50 base::AutoLock auto_lock(lock_); |
| 47 DCHECK(state_ != kUninitialized || state_ == kError); | 51 DCHECK(state_ != kUninitialized || state_ == kError); |
| 48 state_ = kPaused; | 52 state_ = kPaused; |
| 49 callback.Run(); | 53 callback.Run(); |
| 50 } | 54 } |
| 51 | 55 |
| 52 void VideoRendererBase::Flush(const base::Closure& callback) { | 56 void VideoRendererBase::Flush(const base::Closure& callback) { |
| 53 base::AutoLock auto_lock(lock_); | 57 base::AutoLock auto_lock(lock_); |
| 54 DCHECK_EQ(state_, kPaused); | 58 DCHECK_EQ(state_, kPaused); |
| 55 flush_cb_ = callback; | 59 flush_cb_ = callback; |
| 56 state_ = kFlushingDecoder; | 60 state_ = kFlushingDecoder; |
| 57 | 61 |
| 58 // We must unlock here because the callback might run within the Flush() | 62 decoder_message_loop_->PostTask(FROM_HERE, base::Bind( |
| 59 // call. | 63 &VideoDecoder::Reset, decoder_, base::Bind( |
| 60 // TODO: Remove this line when fixing http://crbug.com/125020 | 64 &VideoRendererBase::OnDecoderFlushDone, this))); |
| 61 base::AutoUnlock auto_unlock(lock_); | |
| 62 decoder_->Reset(base::Bind(&VideoRendererBase::OnDecoderFlushDone, this)); | |
| 63 } | 65 } |
| 64 | 66 |
| 65 void VideoRendererBase::Stop(const base::Closure& callback) { | 67 void VideoRendererBase::Stop(const base::Closure& callback) { |
| 66 if (state_ == kStopped) { | 68 if (state_ == kStopped) { |
| 67 callback.Run(); | 69 callback.Run(); |
| 68 return; | 70 return; |
| 69 } | 71 } |
| 70 | 72 |
| 71 base::PlatformThreadHandle thread_to_join = base::kNullThreadHandle; | 73 base::PlatformThreadHandle thread_to_join = base::kNullThreadHandle; |
| 72 { | 74 { |
| (...skipping 10 matching lines...) Expand all Loading... | |
| 83 // Signal the thread since it's possible to get stopped with the video | 85 // Signal the thread since it's possible to get stopped with the video |
| 84 // thread waiting for a read to complete. | 86 // thread waiting for a read to complete. |
| 85 frame_available_.Signal(); | 87 frame_available_.Signal(); |
| 86 thread_to_join = thread_; | 88 thread_to_join = thread_; |
| 87 thread_ = base::kNullThreadHandle; | 89 thread_ = base::kNullThreadHandle; |
| 88 } | 90 } |
| 89 } | 91 } |
| 90 if (thread_to_join != base::kNullThreadHandle) | 92 if (thread_to_join != base::kNullThreadHandle) |
| 91 base::PlatformThread::Join(thread_to_join); | 93 base::PlatformThread::Join(thread_to_join); |
| 92 | 94 |
| 93 decoder_->Stop(callback); | 95 decoder_message_loop_->PostTask(FROM_HERE, base::Bind( |
| 96 &VideoDecoder::Stop, decoder_, callback)); | |
| 94 } | 97 } |
| 95 | 98 |
| 96 void VideoRendererBase::SetPlaybackRate(float playback_rate) { | 99 void VideoRendererBase::SetPlaybackRate(float playback_rate) { |
| 97 base::AutoLock auto_lock(lock_); | 100 base::AutoLock auto_lock(lock_); |
| 98 playback_rate_ = playback_rate; | 101 playback_rate_ = playback_rate; |
| 99 } | 102 } |
| 100 | 103 |
| 101 void VideoRendererBase::Preroll(base::TimeDelta time, | 104 void VideoRendererBase::Preroll(base::TimeDelta time, |
| 102 const PipelineStatusCB& cb) { | 105 const PipelineStatusCB& cb) { |
| 103 base::AutoLock auto_lock(lock_); | 106 base::AutoLock auto_lock(lock_); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 188 base::ResetAndReturn(&init_cb_).Run(status); | 191 base::ResetAndReturn(&init_cb_).Run(status); |
| 189 return; | 192 return; |
| 190 } | 193 } |
| 191 | 194 |
| 192 // We're all good! Consider ourselves flushed. (ThreadMain() should never | 195 // We're all good! Consider ourselves flushed. (ThreadMain() should never |
| 193 // see us in the kUninitialized state). | 196 // see us in the kUninitialized state). |
| 194 // Since we had an initial Preroll(), we consider ourself flushed, because we | 197 // Since we had an initial Preroll(), we consider ourself flushed, because we |
| 195 // have not populated any buffers yet. | 198 // have not populated any buffers yet. |
| 196 state_ = kFlushed; | 199 state_ = kFlushed; |
| 197 | 200 |
| 201 // XXX we're taking advantage of the fact this is already on the right thread | |
| 198 set_opaque_cb_.Run(!decoder_->HasAlpha()); | 202 set_opaque_cb_.Run(!decoder_->HasAlpha()); |
| 199 set_opaque_cb_.Reset(); | 203 set_opaque_cb_.Reset(); |
| 200 | 204 |
| 201 // Create our video thread. | 205 // Create our video thread. |
| 202 if (!base::PlatformThread::Create(0, this, &thread_)) { | 206 if (!base::PlatformThread::Create(0, this, &thread_)) { |
| 203 NOTREACHED() << "Video thread creation failed"; | 207 NOTREACHED() << "Video thread creation failed"; |
| 204 state_ = kError; | 208 state_ = kError; |
| 205 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); | 209 base::ResetAndReturn(&init_cb_).Run(PIPELINE_ERROR_INITIALIZATION_FAILED); |
| 206 return; | 210 return; |
| 207 } | 211 } |
| (...skipping 356 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 564 | 568 |
| 565 if (pending_read_ || | 569 if (pending_read_ || |
| 566 NumFrames_Locked() == limits::kMaxVideoFrames || | 570 NumFrames_Locked() == limits::kMaxVideoFrames || |
| 567 (!ready_frames_.empty() && ready_frames_.back()->IsEndOfStream()) || | 571 (!ready_frames_.empty() && ready_frames_.back()->IsEndOfStream()) || |
| 568 state_ == kFlushingDecoder || | 572 state_ == kFlushingDecoder || |
| 569 state_ == kFlushing) { | 573 state_ == kFlushing) { |
| 570 return; | 574 return; |
| 571 } | 575 } |
| 572 | 576 |
| 573 pending_read_ = true; | 577 pending_read_ = true; |
| 574 decoder_->Read(base::Bind(&VideoRendererBase::FrameReady, this)); | 578 decoder_message_loop_->PostTask(FROM_HERE, base::Bind( |
| 579 &VideoDecoder::Read, decoder_, base::Bind( | |
| 580 &VideoRendererBase::FrameReady, this))); | |
| 575 } | 581 } |
| 576 | 582 |
| 577 void VideoRendererBase::OnDecoderFlushDone() { | 583 void VideoRendererBase::OnDecoderFlushDone() { |
| 578 base::AutoLock auto_lock(lock_); | 584 base::AutoLock auto_lock(lock_); |
| 579 DCHECK_EQ(kFlushingDecoder, state_); | 585 DCHECK_EQ(kFlushingDecoder, state_); |
| 580 DCHECK(!pending_read_); | 586 DCHECK(!pending_read_); |
| 581 | 587 |
| 582 state_ = kFlushing; | 588 state_ = kFlushing; |
| 583 AttemptFlush_Locked(); | 589 AttemptFlush_Locked(); |
| 584 } | 590 } |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 621 | 627 |
| 622 int VideoRendererBase::NumFrames_Locked() const { | 628 int VideoRendererBase::NumFrames_Locked() const { |
| 623 lock_.AssertAcquired(); | 629 lock_.AssertAcquired(); |
| 624 int outstanding_frames = | 630 int outstanding_frames = |
| 625 (current_frame_ ? 1 : 0) + (last_available_frame_ ? 1 : 0) + | 631 (current_frame_ ? 1 : 0) + (last_available_frame_ ? 1 : 0) + |
| 626 (current_frame_ && (current_frame_ == last_available_frame_) ? -1 : 0); | 632 (current_frame_ && (current_frame_ == last_available_frame_) ? -1 : 0); |
| 627 return ready_frames_.size() + outstanding_frames; | 633 return ready_frames_.size() + outstanding_frames; |
| 628 } | 634 } |
| 629 | 635 |
| 630 } // namespace media | 636 } // namespace media |
| OLD | NEW |