1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "content/renderer/media/rtc_video_renderer.h"
#include "base/bind.h"
#include "base/debug/trace_event.h"
#include "base/location.h"
#include "base/logging.h"
#include "base/message_loop/message_loop_proxy.h"
#include "content/renderer/media/native_handle_impl.h"
#include "media/base/video_frame.h"
#include "media/base/video_util.h"
#include "third_party/libjingle/source/talk/media/base/videoframe.h"
using media::CopyYPlane;
using media::CopyUPlane;
using media::CopyVPlane;
namespace content {
RTCVideoRenderer::RTCVideoRenderer(
webrtc::VideoTrackInterface* video_track,
const base::Closure& error_cb,
const RepaintCB& repaint_cb)
: error_cb_(error_cb),
repaint_cb_(repaint_cb),
message_loop_proxy_(base::MessageLoopProxy::current()),
state_(kStopped),
video_track_(video_track) {
}
RTCVideoRenderer::~RTCVideoRenderer() {
}
void RTCVideoRenderer::Start() {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
DCHECK_EQ(state_, kStopped);
if (video_track_.get()) {
video_track_->AddRenderer(this);
video_track_->RegisterObserver(this);
}
state_ = kStarted;
MaybeRenderSignalingFrame();
}
void RTCVideoRenderer::Stop() {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
if (video_track_.get()) {
state_ = kStopped;
video_track_->RemoveRenderer(this);
video_track_->UnregisterObserver(this);
video_track_ = NULL;
}
}
void RTCVideoRenderer::Play() {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
if (video_track_.get() && state_ == kPaused) {
state_ = kStarted;
}
}
void RTCVideoRenderer::Pause() {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
if (video_track_.get() && state_ == kStarted) {
state_ = kPaused;
}
}
void RTCVideoRenderer::SetSize(int width, int height) {
}
void RTCVideoRenderer::RenderFrame(const cricket::VideoFrame* frame) {
base::TimeDelta timestamp = base::TimeDelta::FromMilliseconds(
frame->GetTimeStamp() / talk_base::kNumNanosecsPerMillisec);
TRACE_EVENT_INSTANT2("rtc_video_renderer",
"RenderFrame",
TRACE_EVENT_SCOPE_THREAD,
"elapsed time",
frame->GetElapsedTime(),
"timestamp_ms",
timestamp.InMilliseconds());
scoped_refptr<media::VideoFrame> video_frame;
if (frame->GetNativeHandle() != NULL) {
NativeHandleImpl* handle =
static_cast<NativeHandleImpl*>(frame->GetNativeHandle());
video_frame = static_cast<media::VideoFrame*>(handle->GetHandle());
video_frame->SetTimestamp(timestamp);
} else {
gfx::Size size(frame->GetWidth(), frame->GetHeight());
video_frame = media::VideoFrame::CreateFrame(
media::VideoFrame::YV12, size, gfx::Rect(size), size, timestamp);
// Aspect ratio unsupported; DCHECK when there are non-square pixels.
DCHECK_EQ(frame->GetPixelWidth(), 1u);
DCHECK_EQ(frame->GetPixelHeight(), 1u);
int y_rows = frame->GetHeight();
int uv_rows = frame->GetHeight() / 2; // YV12 format.
CopyYPlane(
frame->GetYPlane(), frame->GetYPitch(), y_rows, video_frame.get());
CopyUPlane(
frame->GetUPlane(), frame->GetUPitch(), uv_rows, video_frame.get());
CopyVPlane(
frame->GetVPlane(), frame->GetVPitch(), uv_rows, video_frame.get());
}
message_loop_proxy_->PostTask(
FROM_HERE, base::Bind(&RTCVideoRenderer::DoRenderFrameOnMainThread,
this, video_frame));
}
void RTCVideoRenderer::OnChanged() {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
MaybeRenderSignalingFrame();
}
void RTCVideoRenderer::MaybeRenderSignalingFrame() {
// Render a small black frame if the track transition to ended.
// This is necessary to make sure audio can play if the video tag src is
// a MediaStream video track that has been rejected or ended.
if (video_track_->state() == webrtc::MediaStreamTrackInterface::kEnded) {
const int kMinFrameSize = 2;
const gfx::Size size(kMinFrameSize, kMinFrameSize);
scoped_refptr<media::VideoFrame> video_frame =
media::VideoFrame::CreateBlackFrame(size);
DoRenderFrameOnMainThread(video_frame);
}
}
void RTCVideoRenderer::DoRenderFrameOnMainThread(
scoped_refptr<media::VideoFrame> video_frame) {
DCHECK(message_loop_proxy_->BelongsToCurrentThread());
if (state_ != kStarted) {
return;
}
TRACE_EVENT0("video", "DoRenderFrameOnMainThread");
repaint_cb_.Run(video_frame);
}
} // namespace content
|