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/threading/thread_checker.h"
11 #include "base/trace_event/trace_event.h"
12 #include "content/renderer/media/webrtc/track_observer.h"
13 #include "media/base/bind_to_current_loop.h"
14 #include "media/base/video_frame.h"
15 #include "media/base/video_util.h"
16 #include "third_party/libjingle/source/talk/media/base/videoframe.h"
20 // Internal class used for receiving frames from the webrtc track on a
21 // libjingle thread and forward it to the IO-thread.
22 class MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate
23 : public base::RefCountedThreadSafe
<RemoteVideoSourceDelegate
>,
24 public webrtc::VideoRendererInterface
{
26 RemoteVideoSourceDelegate(
27 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
28 const VideoCaptureDeliverFrameCB
& new_frame_callback
);
31 friend class base::RefCountedThreadSafe
<RemoteVideoSourceDelegate
>;
32 ~RemoteVideoSourceDelegate() override
;
34 // Implements webrtc::VideoRendererInterface used for receiving video frames
35 // from the PeerConnection video track. May be called on a libjingle internal
37 void RenderFrame(const cricket::VideoFrame
* frame
) override
;
39 void DoRenderFrameOnIOThread(
40 const scoped_refptr
<media::VideoFrame
>& video_frame
);
43 // Bound to the render thread.
44 base::ThreadChecker thread_checker_
;
46 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner_
;
48 // |frame_callback_| is accessed on the IO thread.
49 VideoCaptureDeliverFrameCB frame_callback_
;
52 MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::
53 RemoteVideoSourceDelegate(
54 scoped_refptr
<base::SingleThreadTaskRunner
> io_task_runner
,
55 const VideoCaptureDeliverFrameCB
& new_frame_callback
)
56 : io_task_runner_(io_task_runner
), frame_callback_(new_frame_callback
) {
59 MediaStreamRemoteVideoSource::
60 RemoteVideoSourceDelegate::~RemoteVideoSourceDelegate() {
63 void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::RenderFrame(
64 const cricket::VideoFrame
* incoming_frame
) {
65 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::RenderFrame");
66 base::TimeDelta timestamp
= base::TimeDelta::FromMicroseconds(
67 incoming_frame
->GetElapsedTime() / rtc::kNumNanosecsPerMicrosec
);
69 scoped_refptr
<media::VideoFrame
> video_frame
;
70 if (incoming_frame
->GetNativeHandle() != NULL
) {
72 static_cast<media::VideoFrame
*>(incoming_frame
->GetNativeHandle());
73 video_frame
->set_timestamp(timestamp
);
75 const cricket::VideoFrame
* frame
=
76 incoming_frame
->GetCopyWithRotationApplied();
78 gfx::Size
size(frame
->GetWidth(), frame
->GetHeight());
80 // Non-square pixels are unsupported.
81 DCHECK_EQ(frame
->GetPixelWidth(), 1u);
82 DCHECK_EQ(frame
->GetPixelHeight(), 1u);
84 // Make a shallow copy. Both |frame| and |video_frame| will share a single
85 // reference counted frame buffer. Const cast and hope no one will overwrite
87 // TODO(magjed): Update media::VideoFrame to support const data so we don't
88 // need to const cast here.
89 video_frame
= media::VideoFrame::WrapExternalYuvData(
90 media::PIXEL_FORMAT_YV12
, size
, gfx::Rect(size
), size
,
91 frame
->GetYPitch(), frame
->GetUPitch(), frame
->GetVPitch(),
92 const_cast<uint8_t*>(frame
->GetYPlane()),
93 const_cast<uint8_t*>(frame
->GetUPlane()),
94 const_cast<uint8_t*>(frame
->GetVPlane()), timestamp
);
95 video_frame
->AddDestructionObserver(
96 base::Bind(&base::DeletePointer
<cricket::VideoFrame
>, frame
->Copy()));
99 io_task_runner_
->PostTask(
100 FROM_HERE
, base::Bind(&RemoteVideoSourceDelegate::DoRenderFrameOnIOThread
,
104 void MediaStreamRemoteVideoSource::
105 RemoteVideoSourceDelegate::DoRenderFrameOnIOThread(
106 const scoped_refptr
<media::VideoFrame
>& video_frame
) {
107 DCHECK(io_task_runner_
->BelongsToCurrentThread());
108 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::DoRenderFrameOnIOThread");
109 // TODO(hclam): Give the estimated capture time.
110 frame_callback_
.Run(video_frame
, base::TimeTicks());
113 MediaStreamRemoteVideoSource::MediaStreamRemoteVideoSource(
114 scoped_ptr
<TrackObserver
> observer
)
115 : observer_(observer
.Pass()) {
116 // The callback will be automatically cleared when 'observer_' goes out of
117 // scope and no further callbacks will occur.
118 observer_
->SetCallback(base::Bind(&MediaStreamRemoteVideoSource::OnChanged
,
119 base::Unretained(this)));
122 MediaStreamRemoteVideoSource::~MediaStreamRemoteVideoSource() {
123 DCHECK(CalledOnValidThread());
126 void MediaStreamRemoteVideoSource::GetCurrentSupportedFormats(
127 int max_requested_width
,
128 int max_requested_height
,
129 double max_requested_frame_rate
,
130 const VideoCaptureDeviceFormatsCB
& callback
) {
131 DCHECK(CalledOnValidThread());
132 media::VideoCaptureFormats formats
;
133 // Since the remote end is free to change the resolution at any point in time
134 // the supported formats are unknown.
135 callback
.Run(formats
);
138 void MediaStreamRemoteVideoSource::StartSourceImpl(
139 const media::VideoCaptureFormat
& format
,
140 const blink::WebMediaConstraints
& constraints
,
141 const VideoCaptureDeliverFrameCB
& frame_callback
) {
142 DCHECK(CalledOnValidThread());
143 DCHECK(!delegate_
.get());
144 delegate_
= new RemoteVideoSourceDelegate(io_task_runner(), frame_callback
);
145 scoped_refptr
<webrtc::VideoTrackInterface
> video_track(
146 static_cast<webrtc::VideoTrackInterface
*>(observer_
->track().get()));
147 video_track
->AddRenderer(delegate_
.get());
148 OnStartDone(MEDIA_DEVICE_OK
);
151 void MediaStreamRemoteVideoSource::StopSourceImpl() {
152 DCHECK(CalledOnValidThread());
153 DCHECK(state() != MediaStreamVideoSource::ENDED
);
154 scoped_refptr
<webrtc::VideoTrackInterface
> video_track(
155 static_cast<webrtc::VideoTrackInterface
*>(observer_
->track().get()));
156 video_track
->RemoveRenderer(delegate_
.get());
159 webrtc::VideoRendererInterface
*
160 MediaStreamRemoteVideoSource::RenderInterfaceForTest() {
161 return delegate_
.get();
164 void MediaStreamRemoteVideoSource::OnChanged(
165 webrtc::MediaStreamTrackInterface::TrackState state
) {
166 DCHECK(CalledOnValidThread());
168 case webrtc::MediaStreamTrackInterface::kInitializing
:
169 // Ignore the kInitializing state since there is no match in
170 // WebMediaStreamSource::ReadyState.
172 case webrtc::MediaStreamTrackInterface::kLive
:
173 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive
);
175 case webrtc::MediaStreamTrackInterface::kEnded
:
176 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded
);
184 } // namespace content