| 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/video_frame_pump.h" | 5 #include "remoting/protocol/video_frame_pump.h" |
| 6 | 6 |
| 7 #include <algorithm> | 7 #include <algorithm> |
| 8 #include <memory> |
| 8 #include <utility> | 9 #include <utility> |
| 9 | 10 |
| 10 #include "base/bind.h" | 11 #include "base/bind.h" |
| 11 #include "base/callback.h" | 12 #include "base/callback.h" |
| 12 #include "base/logging.h" | 13 #include "base/logging.h" |
| 13 #include "base/memory/scoped_ptr.h" | 14 #include "base/memory/ptr_util.h" |
| 14 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 15 #include "base/task_runner_util.h" | 16 #include "base/task_runner_util.h" |
| 16 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 17 #include "remoting/base/constants.h" | 18 #include "remoting/base/constants.h" |
| 18 #include "remoting/proto/control.pb.h" | 19 #include "remoting/proto/control.pb.h" |
| 19 #include "remoting/proto/video.pb.h" | 20 #include "remoting/proto/video.pb.h" |
| 20 #include "remoting/protocol/video_stub.h" | 21 #include "remoting/protocol/video_stub.h" |
| 21 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" | 22 #include "third_party/webrtc/modules/desktop_capture/desktop_frame.h" |
| 22 | 23 |
| 23 namespace remoting { | 24 namespace remoting { |
| 24 namespace protocol { | 25 namespace protocol { |
| 25 | 26 |
| 26 // Interval between empty keep-alive frames. These frames are sent only when the | 27 // Interval between empty keep-alive frames. These frames are sent only when the |
| 27 // stream is paused or inactive for some other reason (e.g. when blocked on | 28 // stream is paused or inactive for some other reason (e.g. when blocked on |
| 28 // capturer). To prevent PseudoTCP from resetting congestion window this value | 29 // capturer). To prevent PseudoTCP from resetting congestion window this value |
| 29 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms. | 30 // must be smaller than the minimum RTO used in PseudoTCP, which is 250ms. |
| 30 static const int kKeepAlivePacketIntervalMs = 200; | 31 static const int kKeepAlivePacketIntervalMs = 200; |
| 31 | 32 |
| 32 static bool g_enable_timestamps = false; | 33 static bool g_enable_timestamps = false; |
| 33 | 34 |
| 34 VideoFramePump::FrameTimestamps::FrameTimestamps() {} | 35 VideoFramePump::FrameTimestamps::FrameTimestamps() {} |
| 35 VideoFramePump::FrameTimestamps::~FrameTimestamps() {} | 36 VideoFramePump::FrameTimestamps::~FrameTimestamps() {} |
| 36 | 37 |
| 37 VideoFramePump::PacketWithTimestamps::PacketWithTimestamps( | 38 VideoFramePump::PacketWithTimestamps::PacketWithTimestamps( |
| 38 scoped_ptr<VideoPacket> packet, | 39 std::unique_ptr<VideoPacket> packet, |
| 39 scoped_ptr<FrameTimestamps> timestamps) | 40 std::unique_ptr<FrameTimestamps> timestamps) |
| 40 : packet(std::move(packet)), timestamps(std::move(timestamps)) {} | 41 : packet(std::move(packet)), timestamps(std::move(timestamps)) {} |
| 41 | 42 |
| 42 VideoFramePump::PacketWithTimestamps::~PacketWithTimestamps() {} | 43 VideoFramePump::PacketWithTimestamps::~PacketWithTimestamps() {} |
| 43 | 44 |
| 44 // static | 45 // static |
| 45 void VideoFramePump::EnableTimestampsForTests() { | 46 void VideoFramePump::EnableTimestampsForTests() { |
| 46 g_enable_timestamps = true; | 47 g_enable_timestamps = true; |
| 47 } | 48 } |
| 48 | 49 |
| 49 VideoFramePump::VideoFramePump( | 50 VideoFramePump::VideoFramePump( |
| 50 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 51 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
| 51 scoped_ptr<webrtc::DesktopCapturer> capturer, | 52 std::unique_ptr<webrtc::DesktopCapturer> capturer, |
| 52 scoped_ptr<VideoEncoder> encoder, | 53 std::unique_ptr<VideoEncoder> encoder, |
| 53 protocol::VideoStub* video_stub) | 54 protocol::VideoStub* video_stub) |
| 54 : encode_task_runner_(encode_task_runner), | 55 : encode_task_runner_(encode_task_runner), |
| 55 capturer_(std::move(capturer)), | 56 capturer_(std::move(capturer)), |
| 56 encoder_(std::move(encoder)), | 57 encoder_(std::move(encoder)), |
| 57 video_stub_(video_stub), | 58 video_stub_(video_stub), |
| 58 keep_alive_timer_( | 59 keep_alive_timer_( |
| 59 FROM_HERE, | 60 FROM_HERE, |
| 60 base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), | 61 base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), |
| 61 base::Bind(&VideoFramePump::SendKeepAlivePacket, | 62 base::Bind(&VideoFramePump::SendKeepAlivePacket, |
| 62 base::Unretained(this)), | 63 base::Unretained(this)), |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 134 size_callback_.Run(frame_size_, frame_dpi_); | 135 size_callback_.Run(frame_size_, frame_dpi_); |
| 135 } | 136 } |
| 136 } | 137 } |
| 137 | 138 |
| 138 // Even when |frame| is nullptr we still need to post it to the encode thread | 139 // Even when |frame| is nullptr we still need to post it to the encode thread |
| 139 // to make sure frames are freed in the same order they are received and | 140 // to make sure frames are freed in the same order they are received and |
| 140 // that we don't start capturing frame n+2 before frame n is freed. | 141 // that we don't start capturing frame n+2 before frame n is freed. |
| 141 base::PostTaskAndReplyWithResult( | 142 base::PostTaskAndReplyWithResult( |
| 142 encode_task_runner_.get(), FROM_HERE, | 143 encode_task_runner_.get(), FROM_HERE, |
| 143 base::Bind(&VideoFramePump::EncodeFrame, encoder_.get(), | 144 base::Bind(&VideoFramePump::EncodeFrame, encoder_.get(), |
| 144 base::Passed(make_scoped_ptr(frame)), | 145 base::Passed(base::WrapUnique(frame)), |
| 145 base::Passed(&captured_frame_timestamps_)), | 146 base::Passed(&captured_frame_timestamps_)), |
| 146 base::Bind(&VideoFramePump::OnFrameEncoded, weak_factory_.GetWeakPtr())); | 147 base::Bind(&VideoFramePump::OnFrameEncoded, weak_factory_.GetWeakPtr())); |
| 147 } | 148 } |
| 148 | 149 |
| 149 void VideoFramePump::CaptureNextFrame() { | 150 void VideoFramePump::CaptureNextFrame() { |
| 150 DCHECK(thread_checker_.CalledOnValidThread()); | 151 DCHECK(thread_checker_.CalledOnValidThread()); |
| 151 | 152 |
| 152 // |next_frame_timestamps_| is not set if no input events were received since | 153 // |next_frame_timestamps_| is not set if no input events were received since |
| 153 // the previous frame. In that case create FrameTimestamps instance without | 154 // the previous frame. In that case create FrameTimestamps instance without |
| 154 // setting |input_event_client_timestamp| and |input_event_received_time|. | 155 // setting |input_event_client_timestamp| and |input_event_received_time|. |
| 155 if (!next_frame_timestamps_) | 156 if (!next_frame_timestamps_) |
| 156 next_frame_timestamps_.reset(new FrameTimestamps()); | 157 next_frame_timestamps_.reset(new FrameTimestamps()); |
| 157 | 158 |
| 158 captured_frame_timestamps_ = std::move(next_frame_timestamps_); | 159 captured_frame_timestamps_ = std::move(next_frame_timestamps_); |
| 159 captured_frame_timestamps_->capture_started_time = base::TimeTicks::Now(); | 160 captured_frame_timestamps_->capture_started_time = base::TimeTicks::Now(); |
| 160 | 161 |
| 161 capturer_->Capture(webrtc::DesktopRegion()); | 162 capturer_->Capture(webrtc::DesktopRegion()); |
| 162 } | 163 } |
| 163 | 164 |
| 164 // static | 165 // static |
| 165 scoped_ptr<VideoFramePump::PacketWithTimestamps> VideoFramePump::EncodeFrame( | 166 std::unique_ptr<VideoFramePump::PacketWithTimestamps> |
| 166 VideoEncoder* encoder, | 167 VideoFramePump::EncodeFrame(VideoEncoder* encoder, |
| 167 scoped_ptr<webrtc::DesktopFrame> frame, | 168 std::unique_ptr<webrtc::DesktopFrame> frame, |
| 168 scoped_ptr<FrameTimestamps> timestamps) { | 169 std::unique_ptr<FrameTimestamps> timestamps) { |
| 169 timestamps->encode_started_time = base::TimeTicks::Now(); | 170 timestamps->encode_started_time = base::TimeTicks::Now(); |
| 170 | 171 |
| 171 scoped_ptr<VideoPacket> packet; | 172 std::unique_ptr<VideoPacket> packet; |
| 172 // If |frame| is non-NULL then let the encoder process it. | 173 // If |frame| is non-NULL then let the encoder process it. |
| 173 if (frame) | 174 if (frame) |
| 174 packet = encoder->Encode(*frame); | 175 packet = encoder->Encode(*frame); |
| 175 | 176 |
| 176 // If |frame| is NULL, or the encoder returned nothing, return an empty | 177 // If |frame| is NULL, or the encoder returned nothing, return an empty |
| 177 // packet. | 178 // packet. |
| 178 if (!packet) | 179 if (!packet) |
| 179 packet.reset(new VideoPacket()); | 180 packet.reset(new VideoPacket()); |
| 180 | 181 |
| 181 if (frame) | 182 if (frame) |
| 182 packet->set_capture_time_ms(frame->capture_time_ms()); | 183 packet->set_capture_time_ms(frame->capture_time_ms()); |
| 183 | 184 |
| 184 timestamps->encode_ended_time = base::TimeTicks::Now(); | 185 timestamps->encode_ended_time = base::TimeTicks::Now(); |
| 185 packet->set_encode_time_ms( | 186 packet->set_encode_time_ms( |
| 186 (timestamps->encode_ended_time - timestamps->encode_started_time) | 187 (timestamps->encode_ended_time - timestamps->encode_started_time) |
| 187 .InMilliseconds()); | 188 .InMilliseconds()); |
| 188 | 189 |
| 189 return make_scoped_ptr( | 190 return base::WrapUnique( |
| 190 new PacketWithTimestamps(std::move(packet), std::move(timestamps))); | 191 new PacketWithTimestamps(std::move(packet), std::move(timestamps))); |
| 191 } | 192 } |
| 192 | 193 |
| 193 void VideoFramePump::OnFrameEncoded(scoped_ptr<PacketWithTimestamps> packet) { | 194 void VideoFramePump::OnFrameEncoded( |
| 195 std::unique_ptr<PacketWithTimestamps> packet) { |
| 194 DCHECK(thread_checker_.CalledOnValidThread()); | 196 DCHECK(thread_checker_.CalledOnValidThread()); |
| 195 | 197 |
| 196 capture_scheduler_.OnFrameEncoded(packet->packet.get()); | 198 capture_scheduler_.OnFrameEncoded(packet->packet.get()); |
| 197 | 199 |
| 198 if (send_pending_) { | 200 if (send_pending_) { |
| 199 pending_packets_.push_back(std::move(packet)); | 201 pending_packets_.push_back(std::move(packet)); |
| 200 } else { | 202 } else { |
| 201 SendPacket(std::move(packet)); | 203 SendPacket(std::move(packet)); |
| 202 } | 204 } |
| 203 } | 205 } |
| 204 | 206 |
| 205 void VideoFramePump::SendPacket(scoped_ptr<PacketWithTimestamps> packet) { | 207 void VideoFramePump::SendPacket(std::unique_ptr<PacketWithTimestamps> packet) { |
| 206 DCHECK(thread_checker_.CalledOnValidThread()); | 208 DCHECK(thread_checker_.CalledOnValidThread()); |
| 207 DCHECK(!send_pending_); | 209 DCHECK(!send_pending_); |
| 208 | 210 |
| 209 packet->timestamps->can_send_time = base::TimeTicks::Now(); | 211 packet->timestamps->can_send_time = base::TimeTicks::Now(); |
| 210 UpdateFrameTimers(packet->packet.get(), packet->timestamps.get()); | 212 UpdateFrameTimers(packet->packet.get(), packet->timestamps.get()); |
| 211 | 213 |
| 212 send_pending_ = true; | 214 send_pending_ = true; |
| 213 video_stub_->ProcessVideoPacket(std::move(packet->packet), | 215 video_stub_->ProcessVideoPacket(std::move(packet->packet), |
| 214 base::Bind(&VideoFramePump::OnVideoPacketSent, | 216 base::Bind(&VideoFramePump::OnVideoPacketSent, |
| 215 weak_factory_.GetWeakPtr())); | 217 weak_factory_.GetWeakPtr())); |
| (...skipping 29 matching lines...) Expand all Loading... |
| 245 | 247 |
| 246 void VideoFramePump::OnVideoPacketSent() { | 248 void VideoFramePump::OnVideoPacketSent() { |
| 247 DCHECK(thread_checker_.CalledOnValidThread()); | 249 DCHECK(thread_checker_.CalledOnValidThread()); |
| 248 | 250 |
| 249 send_pending_ = false; | 251 send_pending_ = false; |
| 250 capture_scheduler_.OnFrameSent(); | 252 capture_scheduler_.OnFrameSent(); |
| 251 keep_alive_timer_.Reset(); | 253 keep_alive_timer_.Reset(); |
| 252 | 254 |
| 253 // Send next packet if any. | 255 // Send next packet if any. |
| 254 if (!pending_packets_.empty()) { | 256 if (!pending_packets_.empty()) { |
| 255 scoped_ptr<PacketWithTimestamps> next(pending_packets_.front()); | 257 std::unique_ptr<PacketWithTimestamps> next(pending_packets_.front()); |
| 256 pending_packets_.weak_erase(pending_packets_.begin()); | 258 pending_packets_.weak_erase(pending_packets_.begin()); |
| 257 SendPacket(std::move(next)); | 259 SendPacket(std::move(next)); |
| 258 } | 260 } |
| 259 } | 261 } |
| 260 | 262 |
| 261 void VideoFramePump::SendKeepAlivePacket() { | 263 void VideoFramePump::SendKeepAlivePacket() { |
| 262 DCHECK(thread_checker_.CalledOnValidThread()); | 264 DCHECK(thread_checker_.CalledOnValidThread()); |
| 263 | 265 |
| 264 video_stub_->ProcessVideoPacket( | 266 video_stub_->ProcessVideoPacket( |
| 265 make_scoped_ptr(new VideoPacket()), | 267 base::WrapUnique(new VideoPacket()), |
| 266 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, | 268 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, |
| 267 weak_factory_.GetWeakPtr())); | 269 weak_factory_.GetWeakPtr())); |
| 268 } | 270 } |
| 269 | 271 |
| 270 void VideoFramePump::OnKeepAlivePacketSent() { | 272 void VideoFramePump::OnKeepAlivePacketSent() { |
| 271 DCHECK(thread_checker_.CalledOnValidThread()); | 273 DCHECK(thread_checker_.CalledOnValidThread()); |
| 272 | 274 |
| 273 keep_alive_timer_.Reset(); | 275 keep_alive_timer_.Reset(); |
| 274 } | 276 } |
| 275 | 277 |
| 276 } // namespace protocol | 278 } // namespace protocol |
| 277 } // namespace remoting | 279 } // namespace remoting |
| OLD | NEW |