Fix build break
[chromium-blink-merge.git] / content / renderer / media / rtc_video_renderer.cc
blob5087711a82071bd0dc99d5a45777b2e60334fc84
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.
5 #include "content/renderer/media/rtc_video_renderer.h"
7 #include "base/bind.h"
8 #include "base/debug/trace_event.h"
9 #include "base/location.h"
10 #include "base/logging.h"
11 #include "base/message_loop_proxy.h"
12 #include "media/base/video_frame.h"
13 #include "media/base/video_util.h"
14 #include "third_party/libjingle/source/talk/media/base/videoframe.h"
16 using media::CopyYPlane;
17 using media::CopyUPlane;
18 using media::CopyVPlane;
20 namespace content {
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) {
33 RTCVideoRenderer::~RTCVideoRenderer() {
36 void RTCVideoRenderer::Start() {
37 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
38 DCHECK_EQ(state_, kStopped);
40 if (video_track_) {
41 video_track_->AddRenderer(this);
42 video_track_->RegisterObserver(this);
44 state_ = kStarted;
45 MaybeRenderSignalingFrame();
48 void RTCVideoRenderer::Stop() {
49 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
50 if (video_track_) {
51 state_ = kStopped;
52 video_track_->RemoveRenderer(this);
53 video_track_->UnregisterObserver(this);
54 video_track_ = NULL;
58 void RTCVideoRenderer::Play() {
59 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
60 if (video_track_ && state_ == kPaused) {
61 state_ = kStarted;
65 void RTCVideoRenderer::Pause() {
66 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
67 if (video_track_ && state_ == kStarted) {
68 state_ = kPaused;
72 void RTCVideoRenderer::SetSize(int width, int height) {
75 void RTCVideoRenderer::RenderFrame(const cricket::VideoFrame* frame) {
76 TRACE_EVENT_INSTANT2("rtc_video_renderer",
77 "RenderFrame",
78 TRACE_EVENT_SCOPE_THREAD,
79 "elapsed time",
80 frame->GetElapsedTime(),
81 "timestamp",
82 frame->GetTimeStamp());
84 gfx::Size size(frame->GetWidth(), frame->GetHeight());
85 scoped_refptr<media::VideoFrame> video_frame =
86 media::VideoFrame::CreateFrame(media::VideoFrame::YV12,
87 size,
88 gfx::Rect(size),
89 size,
90 base::TimeDelta::FromMilliseconds(
91 frame->GetTimeStamp()));
93 // Aspect ratio unsupported; DCHECK when there are non-square pixels.
94 DCHECK_EQ(frame->GetPixelWidth(), 1u);
95 DCHECK_EQ(frame->GetPixelHeight(), 1u);
97 int y_rows = frame->GetHeight();
98 int uv_rows = frame->GetHeight() / 2; // YV12 format.
99 CopyYPlane(frame->GetYPlane(), frame->GetYPitch(), y_rows, video_frame);
100 CopyUPlane(frame->GetUPlane(), frame->GetUPitch(), uv_rows, video_frame);
101 CopyVPlane(frame->GetVPlane(), frame->GetVPitch(), uv_rows, video_frame);
103 message_loop_proxy_->PostTask(
104 FROM_HERE, base::Bind(&RTCVideoRenderer::DoRenderFrameOnMainThread,
105 this, video_frame));
108 void RTCVideoRenderer::OnChanged() {
109 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
110 MaybeRenderSignalingFrame();
113 void RTCVideoRenderer::MaybeRenderSignalingFrame() {
114 // Render a small black frame if the track transition to ended.
115 // This is necessary to make sure audio can play if the video tag src is
116 // a MediaStream video track that has been rejected or ended.
117 if (video_track_->state() == webrtc::MediaStreamTrackInterface::kEnded) {
118 const int kMinFrameSize = 2;
119 const gfx::Size size(kMinFrameSize, kMinFrameSize);
120 scoped_refptr<media::VideoFrame> video_frame =
121 media::VideoFrame::CreateBlackFrame(size);
122 DoRenderFrameOnMainThread(video_frame);
126 void RTCVideoRenderer::DoRenderFrameOnMainThread(
127 scoped_refptr<media::VideoFrame> video_frame) {
128 DCHECK(message_loop_proxy_->BelongsToCurrentThread());
130 if (state_ != kStarted) {
131 return;
134 TRACE_EVENT0("video", "DoRenderFrameOnMainThread");
135 repaint_cb_.Run(video_frame);
138 } // namespace content