| 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 base::Bind(&WebrtcVideoStream::CaptureNextFrame, base::Unretained(this))); |
| 128 base::Bind(&WebrtcVideoStream::SetKeyFrameRequest, | |
| 129 weak_factory_.GetWeakPtr()))); | |
| 130 | 110 |
| 131 // Register for target bitrate notifications. | 111 webrtc_transport_->video_encoder_factory()->SetNetworkStateObserver( |
| 132 webrtc_transport_->video_encoder_factory()->SetTargetBitrateCallback( | 112 scheduler_->GetNetworkStateObserver()); |
| 133 base::Bind(&PostTaskOnTaskRunnerWithParam<int>, | |
| 134 base::ThreadTaskRunnerHandle::Get(), | |
| 135 base::Bind(&WebrtcVideoStream::SetTargetBitrate, | |
| 136 weak_factory_.GetWeakPtr()))); | |
| 137 | 113 |
| 138 video_stats_dispatcher_.Init(webrtc_transport_->CreateOutgoingChannel( | 114 video_stats_dispatcher_.Init(webrtc_transport_->CreateOutgoingChannel( |
| 139 video_stats_dispatcher_.channel_name()), | 115 video_stats_dispatcher_.channel_name()), |
| 140 this); | 116 this); |
| 141 | |
| 142 scheduler_.reset(new WebrtcFrameSchedulerSimple()); | |
| 143 } | 117 } |
| 144 | 118 |
| 145 void WebrtcVideoStream::Pause(bool pause) { | 119 void WebrtcVideoStream::Pause(bool pause) { |
| 146 DCHECK(thread_checker_.CalledOnValidThread()); | 120 DCHECK(thread_checker_.CalledOnValidThread()); |
| 147 scheduler_->Pause(pause); | 121 scheduler_->Pause(pause); |
| 148 } | 122 } |
| 149 | 123 |
| 150 void WebrtcVideoStream::OnInputEventReceived(int64_t event_timestamp) { | 124 void WebrtcVideoStream::OnInputEventReceived(int64_t event_timestamp) { |
| 151 DCHECK(thread_checker_.CalledOnValidThread()); | 125 DCHECK(thread_checker_.CalledOnValidThread()); |
| 152 | 126 |
| 153 if (!next_frame_timestamps_) | 127 if (!next_frame_timestamps_) |
| 154 next_frame_timestamps_.reset(new FrameTimestamps()); | 128 next_frame_timestamps_.reset(new FrameTimestamps()); |
| 155 next_frame_timestamps_->input_event_client_timestamp = event_timestamp; | 129 next_frame_timestamps_->input_event_client_timestamp = event_timestamp; |
| 156 next_frame_timestamps_->input_event_received_time = base::TimeTicks::Now(); | 130 next_frame_timestamps_->input_event_received_time = base::TimeTicks::Now(); |
| 157 } | 131 } |
| 158 | 132 |
| 159 void WebrtcVideoStream::SetLosslessEncode(bool want_lossless) { | 133 void WebrtcVideoStream::SetLosslessEncode(bool want_lossless) { |
| 160 NOTIMPLEMENTED(); | 134 NOTIMPLEMENTED(); |
| 161 } | 135 } |
| 162 | 136 |
| 163 void WebrtcVideoStream::SetLosslessColor(bool want_lossless) { | 137 void WebrtcVideoStream::SetLosslessColor(bool want_lossless) { |
| 164 NOTIMPLEMENTED(); | 138 NOTIMPLEMENTED(); |
| 165 } | 139 } |
| 166 | 140 |
| 167 void WebrtcVideoStream::SetObserver(Observer* observer) { | 141 void WebrtcVideoStream::SetObserver(Observer* observer) { |
| 168 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 169 observer_ = observer; | 143 observer_ = observer; |
| 170 } | 144 } |
| 171 | 145 |
| 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( | 146 void WebrtcVideoStream::OnCaptureResult( |
| 193 webrtc::DesktopCapturer::Result result, | 147 webrtc::DesktopCapturer::Result result, |
| 194 std::unique_ptr<webrtc::DesktopFrame> frame) { | 148 std::unique_ptr<webrtc::DesktopFrame> frame) { |
| 195 DCHECK(thread_checker_.CalledOnValidThread()); | 149 DCHECK(thread_checker_.CalledOnValidThread()); |
| 196 | 150 |
| 197 // TODO(sergeyu): Handle ERROR_PERMANENT result here. | 151 // TODO(sergeyu): Handle ERROR_PERMANENT result here. |
| 198 | 152 |
| 199 webrtc::DesktopVector dpi = | 153 webrtc::DesktopVector dpi = |
| 200 frame->dpi().is_zero() ? webrtc::DesktopVector(kDefaultDpi, kDefaultDpi) | 154 frame->dpi().is_zero() ? webrtc::DesktopVector(kDefaultDpi, kDefaultDpi) |
| 201 : frame->dpi(); | 155 : frame->dpi(); |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 259 result.timestamps = std::move(timestamps); | 213 result.timestamps = std::move(timestamps); |
| 260 result.timestamps->encode_started_time = base::TimeTicks::Now(); | 214 result.timestamps->encode_started_time = base::TimeTicks::Now(); |
| 261 result.frame = encoder->Encode(*frame, params); | 215 result.frame = encoder->Encode(*frame, params); |
| 262 result.timestamps->encode_ended_time = base::TimeTicks::Now(); | 216 result.timestamps->encode_ended_time = base::TimeTicks::Now(); |
| 263 return result; | 217 return result; |
| 264 } | 218 } |
| 265 | 219 |
| 266 void WebrtcVideoStream::OnFrameEncoded(EncodedFrameWithTimestamps frame) { | 220 void WebrtcVideoStream::OnFrameEncoded(EncodedFrameWithTimestamps frame) { |
| 267 DCHECK(thread_checker_.CalledOnValidThread()); | 221 DCHECK(thread_checker_.CalledOnValidThread()); |
| 268 | 222 |
| 269 // Send the frame itself. | |
| 270 webrtc::EncodedImageCallback::Result result = | 223 webrtc::EncodedImageCallback::Result result = |
| 271 webrtc_transport_->video_encoder_factory()->SendEncodedFrame( | 224 webrtc_transport_->video_encoder_factory()->SendEncodedFrame( |
| 272 *frame.frame, frame.timestamps->capture_started_time); | 225 *frame.frame, frame.timestamps->capture_started_time); |
| 273 if (result.error != webrtc::EncodedImageCallback::Result::OK) { | 226 if (result.error != webrtc::EncodedImageCallback::Result::OK) { |
| 274 // TODO(sergeyu): Stop the stream. | 227 // TODO(sergeyu): Stop the stream. |
| 275 LOG(ERROR) << "Failed to send video frame."; | 228 LOG(ERROR) << "Failed to send video frame."; |
| 276 return; | 229 return; |
| 277 } | 230 } |
| 278 | 231 |
| 279 scheduler_->OnFrameEncoded(*frame.frame, result); | |
| 280 | |
| 281 // Send FrameStats message. | 232 // Send FrameStats message. |
| 282 if (video_stats_dispatcher_.is_connected()) { | 233 if (video_stats_dispatcher_.is_connected()) { |
| 283 HostFrameStats stats; | 234 HostFrameStats stats; |
| 284 stats.frame_size = frame.frame->data.size(); | 235 stats.frame_size = frame.frame->data.size(); |
| 285 | 236 |
| 286 if (!frame.timestamps->input_event_received_time.is_null()) { | 237 if (!frame.timestamps->input_event_received_time.is_null()) { |
| 287 stats.capture_pending_delay = frame.timestamps->capture_started_time - | 238 stats.capture_pending_delay = frame.timestamps->capture_started_time - |
| 288 frame.timestamps->input_event_received_time; | 239 frame.timestamps->input_event_received_time; |
| 289 stats.latest_event_timestamp = base::TimeTicks::FromInternalValue( | 240 stats.latest_event_timestamp = base::TimeTicks::FromInternalValue( |
| 290 frame.timestamps->input_event_client_timestamp); | 241 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 | 257 // TODO(sergeyu): Figure out how to measure send_pending time with WebRTC |
| 307 // and set it here. | 258 // and set it here. |
| 308 stats.send_pending_delay = base::TimeDelta(); | 259 stats.send_pending_delay = base::TimeDelta(); |
| 309 | 260 |
| 310 video_stats_dispatcher_.OnVideoFrameStats(result.frame_id, stats); | 261 video_stats_dispatcher_.OnVideoFrameStats(result.frame_id, stats); |
| 311 } | 262 } |
| 312 } | 263 } |
| 313 | 264 |
| 314 } // namespace protocol | 265 } // namespace protocol |
| 315 } // namespace remoting | 266 } // namespace remoting |
| OLD | NEW |