OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/protocol/webrtc_video_stream.h" | 5 #include "remoting/protocol/webrtc_video_stream.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "base/single_thread_task_runner.h" | 8 #include "base/single_thread_task_runner.h" |
9 #include "base/task_runner_util.h" | 9 #include "base/task_runner_util.h" |
10 #include "base/threading/thread_task_runner_handle.h" | 10 #include "base/threading/thread_task_runner_handle.h" |
11 #include "remoting/base/constants.h" | 11 #include "remoting/base/constants.h" |
12 #include "remoting/codec/webrtc_video_encoder_vpx.h" | 12 #include "remoting/codec/webrtc_video_encoder_vpx.h" |
13 #include "remoting/protocol/frame_stats.h" | 13 #include "remoting/protocol/frame_stats.h" |
14 #include "remoting/protocol/host_video_stats_dispatcher.h" | 14 #include "remoting/protocol/host_video_stats_dispatcher.h" |
15 #include "remoting/protocol/webrtc_dummy_video_capturer.h" | 15 #include "remoting/protocol/webrtc_dummy_video_capturer.h" |
16 #include "remoting/protocol/webrtc_frame_scheduler_simple.h" | 16 #include "remoting/protocol/webrtc_frame_scheduler_simple.h" |
17 #include "remoting/protocol/webrtc_transport.h" | 17 #include "remoting/protocol/webrtc_transport.h" |
18 #include "third_party/webrtc/api/mediastreaminterface.h" | 18 #include "third_party/webrtc/api/mediastreaminterface.h" |
19 #include "third_party/webrtc/api/peerconnectioninterface.h" | 19 #include "third_party/webrtc/api/peerconnectioninterface.h" |
20 #include "third_party/webrtc/api/test/fakeconstraints.h" | 20 #include "third_party/webrtc/api/test/fakeconstraints.h" |
21 #include "third_party/webrtc/media/base/videocapturer.h" | 21 #include "third_party/webrtc/media/base/videocapturer.h" |
22 | 22 |
23 namespace remoting { | 23 namespace remoting { |
24 namespace protocol { | 24 namespace protocol { |
25 | 25 |
26 namespace { | |
27 | |
28 void PostTaskOnTaskRunner( | |
29 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
30 const base::Closure& task) { | |
31 task_runner->PostTask(FROM_HERE, task); | |
32 } | |
33 | |
34 template <typename ParamType> | |
35 void PostTaskOnTaskRunnerWithParam( | |
36 scoped_refptr<base::SingleThreadTaskRunner> task_runner, | |
37 const base::Callback<void(ParamType param)>& task, | |
38 ParamType param) { | |
39 task_runner->PostTask(FROM_HERE, base::Bind(task, param)); | |
40 } | |
41 | |
42 } // namespace | |
43 | |
44 const char kStreamLabel[] = "screen_stream"; | 26 const char kStreamLabel[] = "screen_stream"; |
45 const char kVideoLabel[] = "screen_video"; | 27 const char kVideoLabel[] = "screen_video"; |
46 | 28 |
47 struct WebrtcVideoStream::FrameTimestamps { | 29 struct WebrtcVideoStream::FrameTimestamps { |
48 // The following two fields are set only for one frame after each incoming | 30 // The following two fields are set only for one frame after each incoming |
49 // input event. |input_event_client_timestamp| is event timestamp | 31 // input event. |input_event_client_timestamp| is event timestamp |
50 // received from the client. |input_event_received_time| is local time when | 32 // received from the client. |input_event_received_time| is local time when |
51 // the event was received. | 33 // the event was received. |
52 int64_t input_event_client_timestamp = -1; | 34 int64_t input_event_client_timestamp = -1; |
53 base::TimeTicks input_event_received_time; | 35 base::TimeTicks input_event_received_time; |
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
115 // AddTrack() may fail only if there is another track with the same name, | 97 // AddTrack() may fail only if there is another track with the same name, |
116 // which is impossible because it's a brand new stream. | 98 // which is impossible because it's a brand new stream. |
117 bool result = stream_->AddTrack(video_track.get()); | 99 bool result = stream_->AddTrack(video_track.get()); |
118 DCHECK(result); | 100 DCHECK(result); |
119 | 101 |
120 // AddStream() may fail if there is another stream with the same name or when | 102 // AddStream() may fail if there is another stream with the same name or when |
121 // the PeerConnection is closed, neither is expected. | 103 // the PeerConnection is closed, neither is expected. |
122 result = peer_connection_->AddStream(stream_.get()); | 104 result = peer_connection_->AddStream(stream_.get()); |
123 DCHECK(result); | 105 DCHECK(result); |
124 | 106 |
125 // Register for PLI requests. | 107 scheduler_.reset(new WebrtcFrameSchedulerSimple()); |
126 webrtc_transport_->video_encoder_factory()->SetKeyFrameRequestCallback( | 108 scheduler_->Start( |
127 base::Bind(&PostTaskOnTaskRunner, base::ThreadTaskRunnerHandle::Get(), | 109 webrtc_transport_->video_encoder_factory(), |
128 base::Bind(&WebrtcVideoStream::SetKeyFrameRequest, | 110 base::Bind(&WebrtcVideoStream::CaptureNextFrame, base::Unretained(this))); |
129 weak_factory_.GetWeakPtr()))); | |
130 | |
131 // Register for target bitrate notifications. | |
132 webrtc_transport_->video_encoder_factory()->SetTargetBitrateCallback( | |
133 base::Bind(&PostTaskOnTaskRunnerWithParam<int>, | |
134 base::ThreadTaskRunnerHandle::Get(), | |
135 base::Bind(&WebrtcVideoStream::SetTargetBitrate, | |
136 weak_factory_.GetWeakPtr()))); | |
137 | 111 |
138 video_stats_dispatcher_.Init(webrtc_transport_->CreateOutgoingChannel( | 112 video_stats_dispatcher_.Init(webrtc_transport_->CreateOutgoingChannel( |
139 video_stats_dispatcher_.channel_name()), | 113 video_stats_dispatcher_.channel_name()), |
140 this); | 114 this); |
141 | |
142 scheduler_.reset(new WebrtcFrameSchedulerSimple()); | |
143 } | 115 } |
144 | 116 |
145 void WebrtcVideoStream::Pause(bool pause) { | 117 void WebrtcVideoStream::Pause(bool pause) { |
146 DCHECK(thread_checker_.CalledOnValidThread()); | 118 DCHECK(thread_checker_.CalledOnValidThread()); |
147 scheduler_->Pause(pause); | 119 scheduler_->Pause(pause); |
148 } | 120 } |
149 | 121 |
150 void WebrtcVideoStream::OnInputEventReceived(int64_t event_timestamp) { | 122 void WebrtcVideoStream::OnInputEventReceived(int64_t event_timestamp) { |
151 DCHECK(thread_checker_.CalledOnValidThread()); | 123 DCHECK(thread_checker_.CalledOnValidThread()); |
152 | 124 |
153 if (!next_frame_timestamps_) | 125 if (!next_frame_timestamps_) |
154 next_frame_timestamps_.reset(new FrameTimestamps()); | 126 next_frame_timestamps_.reset(new FrameTimestamps()); |
155 next_frame_timestamps_->input_event_client_timestamp = event_timestamp; | 127 next_frame_timestamps_->input_event_client_timestamp = event_timestamp; |
156 next_frame_timestamps_->input_event_received_time = base::TimeTicks::Now(); | 128 next_frame_timestamps_->input_event_received_time = base::TimeTicks::Now(); |
157 } | 129 } |
158 | 130 |
159 void WebrtcVideoStream::SetLosslessEncode(bool want_lossless) { | 131 void WebrtcVideoStream::SetLosslessEncode(bool want_lossless) { |
160 NOTIMPLEMENTED(); | 132 NOTIMPLEMENTED(); |
161 } | 133 } |
162 | 134 |
163 void WebrtcVideoStream::SetLosslessColor(bool want_lossless) { | 135 void WebrtcVideoStream::SetLosslessColor(bool want_lossless) { |
164 NOTIMPLEMENTED(); | 136 NOTIMPLEMENTED(); |
165 } | 137 } |
166 | 138 |
167 void WebrtcVideoStream::SetObserver(Observer* observer) { | 139 void WebrtcVideoStream::SetObserver(Observer* observer) { |
168 DCHECK(thread_checker_.CalledOnValidThread()); | 140 DCHECK(thread_checker_.CalledOnValidThread()); |
169 observer_ = observer; | 141 observer_ = observer; |
170 } | 142 } |
171 | 143 |
172 void WebrtcVideoStream::SetKeyFrameRequest() { | |
173 DCHECK(thread_checker_.CalledOnValidThread()); | |
174 | |
175 scheduler_->SetKeyFrameRequest(); | |
176 | |
177 // Create capture scheduler when the first key frame request is received. | |
178 if (!received_first_frame_request_) { | |
179 received_first_frame_request_ = true; | |
180 scheduler_->Start(base::Bind(&WebrtcVideoStream::CaptureNextFrame, | |
181 base::Unretained(this))); | |
182 } | |
183 } | |
184 | |
185 void WebrtcVideoStream::SetTargetBitrate(int target_bitrate_kbps) { | |
186 DCHECK(thread_checker_.CalledOnValidThread()); | |
187 | |
188 VLOG(1) << "Set Target bitrate " << target_bitrate_kbps; | |
189 scheduler_->SetTargetBitrate(target_bitrate_kbps); | |
190 } | |
191 | |
192 void WebrtcVideoStream::OnCaptureResult( | 144 void WebrtcVideoStream::OnCaptureResult( |
193 webrtc::DesktopCapturer::Result result, | 145 webrtc::DesktopCapturer::Result result, |
194 std::unique_ptr<webrtc::DesktopFrame> frame) { | 146 std::unique_ptr<webrtc::DesktopFrame> frame) { |
195 DCHECK(thread_checker_.CalledOnValidThread()); | 147 DCHECK(thread_checker_.CalledOnValidThread()); |
196 | 148 |
197 // TODO(sergeyu): Handle ERROR_PERMANENT result here. | 149 // TODO(sergeyu): Handle ERROR_PERMANENT result here. |
198 | 150 |
199 webrtc::DesktopVector dpi = | 151 webrtc::DesktopVector dpi = |
200 frame->dpi().is_zero() ? webrtc::DesktopVector(kDefaultDpi, kDefaultDpi) | 152 frame->dpi().is_zero() ? webrtc::DesktopVector(kDefaultDpi, kDefaultDpi) |
201 : frame->dpi(); | 153 : frame->dpi(); |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 result.timestamps = std::move(timestamps); | 211 result.timestamps = std::move(timestamps); |
260 result.timestamps->encode_started_time = base::TimeTicks::Now(); | 212 result.timestamps->encode_started_time = base::TimeTicks::Now(); |
261 result.frame = encoder->Encode(*frame, params); | 213 result.frame = encoder->Encode(*frame, params); |
262 result.timestamps->encode_ended_time = base::TimeTicks::Now(); | 214 result.timestamps->encode_ended_time = base::TimeTicks::Now(); |
263 return result; | 215 return result; |
264 } | 216 } |
265 | 217 |
266 void WebrtcVideoStream::OnFrameEncoded(EncodedFrameWithTimestamps frame) { | 218 void WebrtcVideoStream::OnFrameEncoded(EncodedFrameWithTimestamps frame) { |
267 DCHECK(thread_checker_.CalledOnValidThread()); | 219 DCHECK(thread_checker_.CalledOnValidThread()); |
268 | 220 |
269 // Send the frame itself. | |
270 webrtc::EncodedImageCallback::Result result = | 221 webrtc::EncodedImageCallback::Result result = |
271 webrtc_transport_->video_encoder_factory()->SendEncodedFrame( | 222 webrtc_transport_->video_encoder_factory()->SendEncodedFrame( |
272 *frame.frame, frame.timestamps->capture_started_time); | 223 *frame.frame, frame.timestamps->capture_started_time); |
273 if (result.error != webrtc::EncodedImageCallback::Result::OK) { | 224 if (result.error != webrtc::EncodedImageCallback::Result::OK) { |
274 // TODO(sergeyu): Stop the stream. | 225 // TODO(sergeyu): Stop the stream. |
275 LOG(ERROR) << "Failed to send video frame."; | 226 LOG(ERROR) << "Failed to send video frame."; |
276 return; | 227 return; |
277 } | 228 } |
278 | 229 |
279 scheduler_->OnFrameEncoded(*frame.frame, result); | |
280 | |
281 // Send FrameStats message. | 230 // Send FrameStats message. |
282 if (video_stats_dispatcher_.is_connected()) { | 231 if (video_stats_dispatcher_.is_connected()) { |
283 HostFrameStats stats; | 232 HostFrameStats stats; |
284 stats.frame_size = frame.frame->data.size(); | 233 stats.frame_size = frame.frame->data.size(); |
285 | 234 |
286 if (!frame.timestamps->input_event_received_time.is_null()) { | 235 if (!frame.timestamps->input_event_received_time.is_null()) { |
287 stats.capture_pending_delay = frame.timestamps->capture_started_time - | 236 stats.capture_pending_delay = frame.timestamps->capture_started_time - |
288 frame.timestamps->input_event_received_time; | 237 frame.timestamps->input_event_received_time; |
289 stats.latest_event_timestamp = base::TimeTicks::FromInternalValue( | 238 stats.latest_event_timestamp = base::TimeTicks::FromInternalValue( |
290 frame.timestamps->input_event_client_timestamp); | 239 frame.timestamps->input_event_client_timestamp); |
(...skipping 15 matching lines...) Expand all Loading... |
306 // TODO(sergeyu): Figure out how to measure send_pending time with WebRTC | 255 // TODO(sergeyu): Figure out how to measure send_pending time with WebRTC |
307 // and set it here. | 256 // and set it here. |
308 stats.send_pending_delay = base::TimeDelta(); | 257 stats.send_pending_delay = base::TimeDelta(); |
309 | 258 |
310 video_stats_dispatcher_.OnVideoFrameStats(result.frame_id, stats); | 259 video_stats_dispatcher_.OnVideoFrameStats(result.frame_id, stats); |
311 } | 260 } |
312 } | 261 } |
313 | 262 |
314 } // namespace protocol | 263 } // namespace protocol |
315 } // namespace remoting | 264 } // namespace remoting |
OLD | NEW |