1 // Copyright 2014 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/webrtc/media_stream_remote_video_source.h"
8 #include "base/callback_helpers.h"
9 #include "base/location.h"
10 #include "base/message_loop/message_loop_proxy.h"
11 #include "base/threading/thread_checker.h"
12 #include "base/trace_event/trace_event.h"
13 #include "content/renderer/media/native_handle_impl.h"
14 #include "content/renderer/media/webrtc/track_observer.h"
15 #include "media/base/bind_to_current_loop.h"
16 #include "media/base/video_frame.h"
17 #include "media/base/video_util.h"
18 #include "third_party/libjingle/source/talk/media/base/videoframe.h"
22 // Internal class used for receiving frames from the webrtc track on a
23 // libjingle thread and forward it to the IO-thread.
24 class MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate
25 : public base::RefCountedThreadSafe
<RemoteVideoSourceDelegate
>,
26 public webrtc::VideoRendererInterface
{
28 RemoteVideoSourceDelegate(
29 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
,
30 const VideoCaptureDeliverFrameCB
& new_frame_callback
);
33 friend class base::RefCountedThreadSafe
<RemoteVideoSourceDelegate
>;
34 ~RemoteVideoSourceDelegate() override
;
36 // Implements webrtc::VideoRendererInterface used for receiving video frames
37 // from the PeerConnection video track. May be called on a libjingle internal
39 void RenderFrame(const cricket::VideoFrame
* frame
) override
;
41 void DoRenderFrameOnIOThread(
42 const scoped_refptr
<media::VideoFrame
>& video_frame
);
45 // Bound to the render thread.
46 base::ThreadChecker thread_checker_
;
48 scoped_refptr
<base::MessageLoopProxy
> io_message_loop_
;
50 // |frame_callback_| is accessed on the IO thread.
51 VideoCaptureDeliverFrameCB frame_callback_
;
54 MediaStreamRemoteVideoSource::
55 RemoteVideoSourceDelegate::RemoteVideoSourceDelegate(
56 const scoped_refptr
<base::MessageLoopProxy
>& io_message_loop
,
57 const VideoCaptureDeliverFrameCB
& new_frame_callback
)
58 : io_message_loop_(io_message_loop
),
59 frame_callback_(new_frame_callback
) {
62 MediaStreamRemoteVideoSource::
63 RemoteVideoSourceDelegate::~RemoteVideoSourceDelegate() {
66 void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::RenderFrame(
67 const cricket::VideoFrame
* incoming_frame
) {
68 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::RenderFrame");
69 base::TimeDelta timestamp
= base::TimeDelta::FromMicroseconds(
70 incoming_frame
->GetElapsedTime() / rtc::kNumNanosecsPerMicrosec
);
72 scoped_refptr
<media::VideoFrame
> video_frame
;
73 if (incoming_frame
->GetNativeHandle() != NULL
) {
74 NativeHandleImpl
* handle
=
75 static_cast<NativeHandleImpl
*>(incoming_frame
->GetNativeHandle());
76 video_frame
= static_cast<media::VideoFrame
*>(handle
->GetHandle());
77 video_frame
->set_timestamp(timestamp
);
79 const cricket::VideoFrame
* frame
=
80 incoming_frame
->GetCopyWithRotationApplied();
82 gfx::Size
size(frame
->GetWidth(), frame
->GetHeight());
84 // Non-square pixels are unsupported.
85 DCHECK_EQ(frame
->GetPixelWidth(), 1u);
86 DCHECK_EQ(frame
->GetPixelHeight(), 1u);
88 // Make a shallow copy. Both |frame| and |video_frame| will share a single
89 // reference counted frame buffer. Const cast and hope no one will overwrite
91 // TODO(magjed): Update media::VideoFrame to support const data so we don't
92 // need to const cast here.
93 video_frame
= media::VideoFrame::WrapExternalYuvData(
94 media::VideoFrame::YV12
, size
, gfx::Rect(size
), size
,
95 frame
->GetYPitch(), frame
->GetUPitch(), frame
->GetVPitch(),
96 const_cast<uint8_t*>(frame
->GetYPlane()),
97 const_cast<uint8_t*>(frame
->GetUPlane()),
98 const_cast<uint8_t*>(frame
->GetVPlane()), timestamp
,
99 base::Bind(&base::DeletePointer
<cricket::VideoFrame
>, frame
->Copy()));
102 io_message_loop_
->PostTask(
104 base::Bind(&RemoteVideoSourceDelegate::DoRenderFrameOnIOThread
,
108 void MediaStreamRemoteVideoSource::
109 RemoteVideoSourceDelegate::DoRenderFrameOnIOThread(
110 const scoped_refptr
<media::VideoFrame
>& video_frame
) {
111 DCHECK(io_message_loop_
->BelongsToCurrentThread());
112 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::DoRenderFrameOnIOThread");
113 // TODO(hclam): Give the estimated capture time.
114 frame_callback_
.Run(video_frame
, base::TimeTicks());
117 MediaStreamRemoteVideoSource::MediaStreamRemoteVideoSource(
118 scoped_ptr
<TrackObserver
> observer
)
119 : observer_(observer
.Pass()) {
120 // The callback will be automatically cleared when 'observer_' goes out of
121 // scope and no further callbacks will occur.
122 observer_
->SetCallback(base::Bind(&MediaStreamRemoteVideoSource::OnChanged
,
123 base::Unretained(this)));
126 MediaStreamRemoteVideoSource::~MediaStreamRemoteVideoSource() {
127 DCHECK(CalledOnValidThread());
130 void MediaStreamRemoteVideoSource::GetCurrentSupportedFormats(
131 int max_requested_width
,
132 int max_requested_height
,
133 double max_requested_frame_rate
,
134 const VideoCaptureDeviceFormatsCB
& callback
) {
135 DCHECK(CalledOnValidThread());
136 media::VideoCaptureFormats formats
;
137 // Since the remote end is free to change the resolution at any point in time
138 // the supported formats are unknown.
139 callback
.Run(formats
);
142 void MediaStreamRemoteVideoSource::StartSourceImpl(
143 const media::VideoCaptureFormat
& format
,
144 const VideoCaptureDeliverFrameCB
& frame_callback
) {
145 DCHECK(CalledOnValidThread());
146 DCHECK(!delegate_
.get());
147 delegate_
= new RemoteVideoSourceDelegate(io_message_loop(), frame_callback
);
148 scoped_refptr
<webrtc::VideoTrackInterface
> video_track(
149 static_cast<webrtc::VideoTrackInterface
*>(observer_
->track().get()));
150 video_track
->AddRenderer(delegate_
.get());
151 OnStartDone(MEDIA_DEVICE_OK
);
154 void MediaStreamRemoteVideoSource::StopSourceImpl() {
155 DCHECK(CalledOnValidThread());
156 DCHECK(state() != MediaStreamVideoSource::ENDED
);
157 scoped_refptr
<webrtc::VideoTrackInterface
> video_track(
158 static_cast<webrtc::VideoTrackInterface
*>(observer_
->track().get()));
159 video_track
->RemoveRenderer(delegate_
.get());
162 webrtc::VideoRendererInterface
*
163 MediaStreamRemoteVideoSource::RenderInterfaceForTest() {
164 return delegate_
.get();
167 void MediaStreamRemoteVideoSource::OnChanged(
168 webrtc::MediaStreamTrackInterface::TrackState state
) {
169 DCHECK(CalledOnValidThread());
171 case webrtc::MediaStreamTrackInterface::kInitializing
:
172 // Ignore the kInitializing state since there is no match in
173 // WebMediaStreamSource::ReadyState.
175 case webrtc::MediaStreamTrackInterface::kLive
:
176 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive
);
178 case webrtc::MediaStreamTrackInterface::kEnded
:
179 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded
);
187 } // namespace content