Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / content / renderer / media / webrtc / media_stream_remote_video_source.cc
blobe325f797f6dee470e8e9c5c5b84e194b5d43716d
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"
7 #include "base/bind.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"
17 #include "third_party/webrtc/system_wrappers/interface/tick_util.h"
19 namespace content {
21 // Internal class used for receiving frames from the webrtc track on a
22 // libjingle thread and forward it to the IO-thread.
23 class MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate
24 : public base::RefCountedThreadSafe<RemoteVideoSourceDelegate>,
25 public webrtc::VideoRendererInterface {
26 public:
27 RemoteVideoSourceDelegate(
28 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
29 const VideoCaptureDeliverFrameCB& new_frame_callback);
31 protected:
32 friend class base::RefCountedThreadSafe<RemoteVideoSourceDelegate>;
33 ~RemoteVideoSourceDelegate() override;
35 // Implements webrtc::VideoRendererInterface used for receiving video frames
36 // from the PeerConnection video track. May be called on a libjingle internal
37 // thread.
38 void RenderFrame(const cricket::VideoFrame* frame) override;
40 void DoRenderFrameOnIOThread(
41 const scoped_refptr<media::VideoFrame>& video_frame);
43 private:
44 // Bound to the render thread.
45 base::ThreadChecker thread_checker_;
47 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner_;
49 // |frame_callback_| is accessed on the IO thread.
50 VideoCaptureDeliverFrameCB frame_callback_;
52 // WebRTC Chromium timestamp diff
53 int64_t time_diff_us_;
56 MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::
57 RemoteVideoSourceDelegate(
58 scoped_refptr<base::SingleThreadTaskRunner> io_task_runner,
59 const VideoCaptureDeliverFrameCB& new_frame_callback)
60 : io_task_runner_(io_task_runner), frame_callback_(new_frame_callback) {
61 // TODO(qiangchen): There can be two differences between clocks: 1)
62 // the offset, 2) the rate (i.e., one clock runs faster than the other).
63 // See http://crbug/516700
64 time_diff_us_ =
65 (base::TimeTicks::Now() - base::TimeTicks()).InMicroseconds() -
66 webrtc::TickTime::MicrosecondTimestamp();
69 MediaStreamRemoteVideoSource::
70 RemoteVideoSourceDelegate::~RemoteVideoSourceDelegate() {
73 void MediaStreamRemoteVideoSource::RemoteVideoSourceDelegate::RenderFrame(
74 const cricket::VideoFrame* incoming_frame) {
75 base::TimeTicks render_time =
76 base::TimeTicks() +
77 base::TimeDelta::FromMicroseconds(incoming_frame->GetTimeStamp() / 1000 +
78 time_diff_us_);
80 TRACE_EVENT1("webrtc", "RemoteVideoSourceDelegate::RenderFrame",
81 "Ideal Render Instant", render_time.ToInternalValue());
83 base::TimeDelta timestamp = base::TimeDelta::FromMicroseconds(
84 incoming_frame->GetElapsedTime() / rtc::kNumNanosecsPerMicrosec);
86 scoped_refptr<media::VideoFrame> video_frame;
87 if (incoming_frame->GetNativeHandle() != NULL) {
88 video_frame =
89 static_cast<media::VideoFrame*>(incoming_frame->GetNativeHandle());
90 video_frame->set_timestamp(timestamp);
91 } else {
92 const cricket::VideoFrame* frame =
93 incoming_frame->GetCopyWithRotationApplied();
95 gfx::Size size(frame->GetWidth(), frame->GetHeight());
97 // Non-square pixels are unsupported.
98 DCHECK_EQ(frame->GetPixelWidth(), 1u);
99 DCHECK_EQ(frame->GetPixelHeight(), 1u);
101 // Make a shallow copy. Both |frame| and |video_frame| will share a single
102 // reference counted frame buffer. Const cast and hope no one will overwrite
103 // the data.
104 // TODO(magjed): Update media::VideoFrame to support const data so we don't
105 // need to const cast here.
106 video_frame = media::VideoFrame::WrapExternalYuvData(
107 media::PIXEL_FORMAT_YV12, size, gfx::Rect(size), size,
108 frame->GetYPitch(), frame->GetUPitch(), frame->GetVPitch(),
109 const_cast<uint8_t*>(frame->GetYPlane()),
110 const_cast<uint8_t*>(frame->GetUPlane()),
111 const_cast<uint8_t*>(frame->GetVPlane()), timestamp);
112 video_frame->AddDestructionObserver(
113 base::Bind(&base::DeletePointer<cricket::VideoFrame>, frame->Copy()));
116 video_frame->metadata()->SetTimeTicks(
117 media::VideoFrameMetadata::REFERENCE_TIME, render_time);
119 io_task_runner_->PostTask(
120 FROM_HERE, base::Bind(&RemoteVideoSourceDelegate::DoRenderFrameOnIOThread,
121 this, video_frame));
124 void MediaStreamRemoteVideoSource::
125 RemoteVideoSourceDelegate::DoRenderFrameOnIOThread(
126 const scoped_refptr<media::VideoFrame>& video_frame) {
127 DCHECK(io_task_runner_->BelongsToCurrentThread());
128 TRACE_EVENT0("webrtc", "RemoteVideoSourceDelegate::DoRenderFrameOnIOThread");
129 // TODO(hclam): Give the estimated capture time.
130 frame_callback_.Run(video_frame, base::TimeTicks());
133 MediaStreamRemoteVideoSource::MediaStreamRemoteVideoSource(
134 scoped_ptr<TrackObserver> observer)
135 : observer_(observer.Pass()) {
136 // The callback will be automatically cleared when 'observer_' goes out of
137 // scope and no further callbacks will occur.
138 observer_->SetCallback(base::Bind(&MediaStreamRemoteVideoSource::OnChanged,
139 base::Unretained(this)));
142 MediaStreamRemoteVideoSource::~MediaStreamRemoteVideoSource() {
143 DCHECK(CalledOnValidThread());
146 void MediaStreamRemoteVideoSource::GetCurrentSupportedFormats(
147 int max_requested_width,
148 int max_requested_height,
149 double max_requested_frame_rate,
150 const VideoCaptureDeviceFormatsCB& callback) {
151 DCHECK(CalledOnValidThread());
152 media::VideoCaptureFormats formats;
153 // Since the remote end is free to change the resolution at any point in time
154 // the supported formats are unknown.
155 callback.Run(formats);
158 void MediaStreamRemoteVideoSource::StartSourceImpl(
159 const media::VideoCaptureFormat& format,
160 const blink::WebMediaConstraints& constraints,
161 const VideoCaptureDeliverFrameCB& frame_callback) {
162 DCHECK(CalledOnValidThread());
163 DCHECK(!delegate_.get());
164 delegate_ = new RemoteVideoSourceDelegate(io_task_runner(), frame_callback);
165 scoped_refptr<webrtc::VideoTrackInterface> video_track(
166 static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
167 video_track->AddRenderer(delegate_.get());
168 OnStartDone(MEDIA_DEVICE_OK);
171 void MediaStreamRemoteVideoSource::StopSourceImpl() {
172 DCHECK(CalledOnValidThread());
173 DCHECK(state() != MediaStreamVideoSource::ENDED);
174 scoped_refptr<webrtc::VideoTrackInterface> video_track(
175 static_cast<webrtc::VideoTrackInterface*>(observer_->track().get()));
176 video_track->RemoveRenderer(delegate_.get());
179 webrtc::VideoRendererInterface*
180 MediaStreamRemoteVideoSource::RenderInterfaceForTest() {
181 return delegate_.get();
184 void MediaStreamRemoteVideoSource::OnChanged(
185 webrtc::MediaStreamTrackInterface::TrackState state) {
186 DCHECK(CalledOnValidThread());
187 switch (state) {
188 case webrtc::MediaStreamTrackInterface::kInitializing:
189 // Ignore the kInitializing state since there is no match in
190 // WebMediaStreamSource::ReadyState.
191 break;
192 case webrtc::MediaStreamTrackInterface::kLive:
193 SetReadyState(blink::WebMediaStreamSource::ReadyStateLive);
194 break;
195 case webrtc::MediaStreamTrackInterface::kEnded:
196 SetReadyState(blink::WebMediaStreamSource::ReadyStateEnded);
197 break;
198 default:
199 NOTREACHED();
200 break;
204 } // namespace content