OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "content/renderer/media/rtc_video_renderer.h" |
| 6 |
| 7 #include "base/bind.h" |
| 8 #include "base/location.h" |
| 9 #include "base/logging.h" |
| 10 #include "base/message_loop_proxy.h" |
| 11 #include "media/base/video_frame.h" |
| 12 #include "media/base/video_util.h" |
| 13 #include "third_party/libjingle/source/talk/base/timeutils.h" |
| 14 #include "third_party/libjingle/source/talk/media/base/videoframe.h" |
| 15 |
| 16 using media::CopyYPlane; |
| 17 using media::CopyUPlane; |
| 18 using media::CopyVPlane; |
| 19 |
| 20 namespace content { |
| 21 |
| 22 RTCVideoRenderer::RTCVideoRenderer( |
| 23 webrtc::VideoTrackInterface* video_track, |
| 24 const base::Closure& error_cb, |
| 25 const RepaintCB& repaint_cb) |
| 26 : error_cb_(error_cb), |
| 27 repaint_cb_(repaint_cb), |
| 28 message_loop_proxy_(base::MessageLoopProxy::current()), |
| 29 state_(kStopped), |
| 30 video_track_(video_track) { |
| 31 } |
| 32 |
| 33 RTCVideoRenderer::~RTCVideoRenderer() { |
| 34 } |
| 35 |
| 36 void RTCVideoRenderer::Start() { |
| 37 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 38 DCHECK_EQ(state_, kStopped); |
| 39 |
| 40 if (video_track_) |
| 41 video_track_->AddRenderer(this); |
| 42 state_ = kStarted; |
| 43 } |
| 44 |
| 45 void RTCVideoRenderer::Stop() { |
| 46 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 47 if (video_track_) { |
| 48 state_ = kStopped; |
| 49 video_track_->RemoveRenderer(this); |
| 50 video_track_ = NULL; |
| 51 } |
| 52 } |
| 53 |
| 54 void RTCVideoRenderer::Play() { |
| 55 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 56 if (video_track_ && state_ == kPaused) { |
| 57 state_ = kStarted; |
| 58 } |
| 59 } |
| 60 |
| 61 void RTCVideoRenderer::Pause() { |
| 62 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 63 if (video_track_ && state_ == kStarted) { |
| 64 state_ = kPaused; |
| 65 } |
| 66 } |
| 67 |
| 68 void RTCVideoRenderer::SetSize(int width, int height) { |
| 69 } |
| 70 |
| 71 void RTCVideoRenderer::RenderFrame(const cricket::VideoFrame* frame) { |
| 72 base::TimeDelta timestamp = base::TimeDelta::FromMilliseconds( |
| 73 frame->GetTimeStamp() / talk_base::kNumNanosecsPerMillisec); |
| 74 gfx::Size size(frame->GetWidth(), frame->GetHeight()); |
| 75 scoped_refptr<media::VideoFrame> video_frame = |
| 76 media::VideoFrame::CreateFrame(media::VideoFrame::YV12, |
| 77 size, |
| 78 size, |
| 79 timestamp); |
| 80 |
| 81 // Aspect ratio unsupported; DCHECK when there are non-square pixels. |
| 82 DCHECK_EQ(frame->GetPixelWidth(), 1u); |
| 83 DCHECK_EQ(frame->GetPixelHeight(), 1u); |
| 84 |
| 85 int y_rows = frame->GetHeight(); |
| 86 int uv_rows = frame->GetHeight() / 2; // YV12 format. |
| 87 CopyYPlane(frame->GetYPlane(), frame->GetYPitch(), y_rows, video_frame); |
| 88 CopyUPlane(frame->GetUPlane(), frame->GetUPitch(), uv_rows, video_frame); |
| 89 CopyVPlane(frame->GetVPlane(), frame->GetVPitch(), uv_rows, video_frame); |
| 90 |
| 91 message_loop_proxy_->PostTask( |
| 92 FROM_HERE, base::Bind(&RTCVideoRenderer::DoRenderFrameOnMainThread, |
| 93 this, video_frame)); |
| 94 } |
| 95 |
| 96 void RTCVideoRenderer::DoRenderFrameOnMainThread( |
| 97 scoped_refptr<media::VideoFrame> video_frame) { |
| 98 DCHECK(message_loop_proxy_->BelongsToCurrentThread()); |
| 99 |
| 100 if (state_ != kStarted) { |
| 101 return; |
| 102 } |
| 103 |
| 104 repaint_cb_.Run(video_frame); |
| 105 } |
| 106 |
| 107 } // namespace content |
OLD | NEW |