| 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 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/callback.h" | 10 #include "base/callback.h" |
| (...skipping 17 matching lines...) Expand all Loading... |
| 28 static const int kKeepAlivePacketIntervalMs = 200; | 28 static const int kKeepAlivePacketIntervalMs = 200; |
| 29 | 29 |
| 30 static bool g_enable_timestamps = false; | 30 static bool g_enable_timestamps = false; |
| 31 | 31 |
| 32 VideoFramePump::FrameTimestamps::FrameTimestamps() {} | 32 VideoFramePump::FrameTimestamps::FrameTimestamps() {} |
| 33 VideoFramePump::FrameTimestamps::~FrameTimestamps() {} | 33 VideoFramePump::FrameTimestamps::~FrameTimestamps() {} |
| 34 | 34 |
| 35 VideoFramePump::PacketWithTimestamps::PacketWithTimestamps( | 35 VideoFramePump::PacketWithTimestamps::PacketWithTimestamps( |
| 36 scoped_ptr<VideoPacket> packet, | 36 scoped_ptr<VideoPacket> packet, |
| 37 scoped_ptr<FrameTimestamps> timestamps) | 37 scoped_ptr<FrameTimestamps> timestamps) |
| 38 : packet(packet.Pass()), timestamps(timestamps.Pass()) {} | 38 : packet(std::move(packet)), timestamps(std::move(timestamps)) {} |
| 39 | 39 |
| 40 VideoFramePump::PacketWithTimestamps::~PacketWithTimestamps() {} | 40 VideoFramePump::PacketWithTimestamps::~PacketWithTimestamps() {} |
| 41 | 41 |
| 42 // static | 42 // static |
| 43 void VideoFramePump::EnableTimestampsForTests() { | 43 void VideoFramePump::EnableTimestampsForTests() { |
| 44 g_enable_timestamps = true; | 44 g_enable_timestamps = true; |
| 45 } | 45 } |
| 46 | 46 |
| 47 VideoFramePump::VideoFramePump( | 47 VideoFramePump::VideoFramePump( |
| 48 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, | 48 scoped_refptr<base::SingleThreadTaskRunner> encode_task_runner, |
| 49 scoped_ptr<webrtc::DesktopCapturer> capturer, | 49 scoped_ptr<webrtc::DesktopCapturer> capturer, |
| 50 scoped_ptr<VideoEncoder> encoder, | 50 scoped_ptr<VideoEncoder> encoder, |
| 51 protocol::VideoStub* video_stub) | 51 protocol::VideoStub* video_stub) |
| 52 : encode_task_runner_(encode_task_runner), | 52 : encode_task_runner_(encode_task_runner), |
| 53 capturer_(capturer.Pass()), | 53 capturer_(std::move(capturer)), |
| 54 encoder_(encoder.Pass()), | 54 encoder_(std::move(encoder)), |
| 55 video_stub_(video_stub), | 55 video_stub_(video_stub), |
| 56 keep_alive_timer_( | 56 keep_alive_timer_( |
| 57 FROM_HERE, | 57 FROM_HERE, |
| 58 base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), | 58 base::TimeDelta::FromMilliseconds(kKeepAlivePacketIntervalMs), |
| 59 base::Bind(&VideoFramePump::SendKeepAlivePacket, | 59 base::Bind(&VideoFramePump::SendKeepAlivePacket, |
| 60 base::Unretained(this)), | 60 base::Unretained(this)), |
| 61 false), | 61 false), |
| 62 capture_scheduler_(base::Bind(&VideoFramePump::CaptureNextFrame, | 62 capture_scheduler_(base::Bind(&VideoFramePump::CaptureNextFrame, |
| 63 base::Unretained(this))), | 63 base::Unretained(this))), |
| 64 weak_factory_(this) { | 64 weak_factory_(this) { |
| (...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 140 | 140 |
| 141 void VideoFramePump::CaptureNextFrame() { | 141 void VideoFramePump::CaptureNextFrame() { |
| 142 DCHECK(thread_checker_.CalledOnValidThread()); | 142 DCHECK(thread_checker_.CalledOnValidThread()); |
| 143 | 143 |
| 144 // |next_frame_timestamps_| is not set if no input events were received since | 144 // |next_frame_timestamps_| is not set if no input events were received since |
| 145 // the previous frame. In that case create FrameTimestamps instance without | 145 // the previous frame. In that case create FrameTimestamps instance without |
| 146 // setting |input_event_client_timestamp| and |input_event_received_time|. | 146 // setting |input_event_client_timestamp| and |input_event_received_time|. |
| 147 if (!next_frame_timestamps_) | 147 if (!next_frame_timestamps_) |
| 148 next_frame_timestamps_.reset(new FrameTimestamps()); | 148 next_frame_timestamps_.reset(new FrameTimestamps()); |
| 149 | 149 |
| 150 captured_frame_timestamps_ = next_frame_timestamps_.Pass(); | 150 captured_frame_timestamps_ = std::move(next_frame_timestamps_); |
| 151 captured_frame_timestamps_->capture_started_time = base::TimeTicks::Now(); | 151 captured_frame_timestamps_->capture_started_time = base::TimeTicks::Now(); |
| 152 | 152 |
| 153 capturer_->Capture(webrtc::DesktopRegion()); | 153 capturer_->Capture(webrtc::DesktopRegion()); |
| 154 } | 154 } |
| 155 | 155 |
| 156 // static | 156 // static |
| 157 scoped_ptr<VideoFramePump::PacketWithTimestamps> VideoFramePump::EncodeFrame( | 157 scoped_ptr<VideoFramePump::PacketWithTimestamps> VideoFramePump::EncodeFrame( |
| 158 VideoEncoder* encoder, | 158 VideoEncoder* encoder, |
| 159 scoped_ptr<webrtc::DesktopFrame> frame, | 159 scoped_ptr<webrtc::DesktopFrame> frame, |
| 160 scoped_ptr<FrameTimestamps> timestamps) { | 160 scoped_ptr<FrameTimestamps> timestamps) { |
| (...skipping 11 matching lines...) Expand all Loading... |
| 172 | 172 |
| 173 if (frame) | 173 if (frame) |
| 174 packet->set_capture_time_ms(frame->capture_time_ms()); | 174 packet->set_capture_time_ms(frame->capture_time_ms()); |
| 175 | 175 |
| 176 timestamps->encode_ended_time = base::TimeTicks::Now(); | 176 timestamps->encode_ended_time = base::TimeTicks::Now(); |
| 177 packet->set_encode_time_ms( | 177 packet->set_encode_time_ms( |
| 178 (timestamps->encode_ended_time - timestamps->encode_started_time) | 178 (timestamps->encode_ended_time - timestamps->encode_started_time) |
| 179 .InMilliseconds()); | 179 .InMilliseconds()); |
| 180 | 180 |
| 181 return make_scoped_ptr( | 181 return make_scoped_ptr( |
| 182 new PacketWithTimestamps(packet.Pass(), timestamps.Pass())); | 182 new PacketWithTimestamps(std::move(packet), std::move(timestamps))); |
| 183 } | 183 } |
| 184 | 184 |
| 185 void VideoFramePump::OnFrameEncoded(scoped_ptr<PacketWithTimestamps> packet) { | 185 void VideoFramePump::OnFrameEncoded(scoped_ptr<PacketWithTimestamps> packet) { |
| 186 DCHECK(thread_checker_.CalledOnValidThread()); | 186 DCHECK(thread_checker_.CalledOnValidThread()); |
| 187 | 187 |
| 188 capture_scheduler_.OnFrameEncoded(packet->packet.get()); | 188 capture_scheduler_.OnFrameEncoded(packet->packet.get()); |
| 189 | 189 |
| 190 if (send_pending_) { | 190 if (send_pending_) { |
| 191 pending_packets_.push_back(packet.Pass()); | 191 pending_packets_.push_back(std::move(packet)); |
| 192 } else { | 192 } else { |
| 193 SendPacket(packet.Pass()); | 193 SendPacket(std::move(packet)); |
| 194 } | 194 } |
| 195 } | 195 } |
| 196 | 196 |
| 197 void VideoFramePump::SendPacket(scoped_ptr<PacketWithTimestamps> packet) { | 197 void VideoFramePump::SendPacket(scoped_ptr<PacketWithTimestamps> packet) { |
| 198 DCHECK(thread_checker_.CalledOnValidThread()); | 198 DCHECK(thread_checker_.CalledOnValidThread()); |
| 199 DCHECK(!send_pending_); | 199 DCHECK(!send_pending_); |
| 200 | 200 |
| 201 packet->timestamps->can_send_time = base::TimeTicks::Now(); | 201 packet->timestamps->can_send_time = base::TimeTicks::Now(); |
| 202 UpdateFrameTimers(packet->packet.get(), packet->timestamps.get()); | 202 UpdateFrameTimers(packet->packet.get(), packet->timestamps.get()); |
| 203 | 203 |
| 204 send_pending_ = true; | 204 send_pending_ = true; |
| 205 video_stub_->ProcessVideoPacket(packet->packet.Pass(), | 205 video_stub_->ProcessVideoPacket(std::move(packet->packet), |
| 206 base::Bind(&VideoFramePump::OnVideoPacketSent, | 206 base::Bind(&VideoFramePump::OnVideoPacketSent, |
| 207 weak_factory_.GetWeakPtr())); | 207 weak_factory_.GetWeakPtr())); |
| 208 } | 208 } |
| 209 | 209 |
| 210 void VideoFramePump::UpdateFrameTimers(VideoPacket* packet, | 210 void VideoFramePump::UpdateFrameTimers(VideoPacket* packet, |
| 211 FrameTimestamps* timestamps) { | 211 FrameTimestamps* timestamps) { |
| 212 if (g_enable_timestamps) | 212 if (g_enable_timestamps) |
| 213 packet->set_timestamp(timestamps->capture_ended_time.ToInternalValue()); | 213 packet->set_timestamp(timestamps->capture_ended_time.ToInternalValue()); |
| 214 | 214 |
| 215 | 215 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 239 DCHECK(thread_checker_.CalledOnValidThread()); | 239 DCHECK(thread_checker_.CalledOnValidThread()); |
| 240 | 240 |
| 241 send_pending_ = false; | 241 send_pending_ = false; |
| 242 capture_scheduler_.OnFrameSent(); | 242 capture_scheduler_.OnFrameSent(); |
| 243 keep_alive_timer_.Reset(); | 243 keep_alive_timer_.Reset(); |
| 244 | 244 |
| 245 // Send next packet if any. | 245 // Send next packet if any. |
| 246 if (!pending_packets_.empty()) { | 246 if (!pending_packets_.empty()) { |
| 247 scoped_ptr<PacketWithTimestamps> next(pending_packets_.front()); | 247 scoped_ptr<PacketWithTimestamps> next(pending_packets_.front()); |
| 248 pending_packets_.weak_erase(pending_packets_.begin()); | 248 pending_packets_.weak_erase(pending_packets_.begin()); |
| 249 SendPacket(next.Pass()); | 249 SendPacket(std::move(next)); |
| 250 } | 250 } |
| 251 } | 251 } |
| 252 | 252 |
| 253 void VideoFramePump::SendKeepAlivePacket() { | 253 void VideoFramePump::SendKeepAlivePacket() { |
| 254 DCHECK(thread_checker_.CalledOnValidThread()); | 254 DCHECK(thread_checker_.CalledOnValidThread()); |
| 255 | 255 |
| 256 video_stub_->ProcessVideoPacket( | 256 video_stub_->ProcessVideoPacket( |
| 257 make_scoped_ptr(new VideoPacket()), | 257 make_scoped_ptr(new VideoPacket()), |
| 258 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, | 258 base::Bind(&VideoFramePump::OnKeepAlivePacketSent, |
| 259 weak_factory_.GetWeakPtr())); | 259 weak_factory_.GetWeakPtr())); |
| 260 } | 260 } |
| 261 | 261 |
| 262 void VideoFramePump::OnKeepAlivePacketSent() { | 262 void VideoFramePump::OnKeepAlivePacketSent() { |
| 263 DCHECK(thread_checker_.CalledOnValidThread()); | 263 DCHECK(thread_checker_.CalledOnValidThread()); |
| 264 | 264 |
| 265 keep_alive_timer_.Reset(); | 265 keep_alive_timer_.Reset(); |
| 266 } | 266 } |
| 267 | 267 |
| 268 } // namespace protocol | 268 } // namespace protocol |
| 269 } // namespace remoting | 269 } // namespace remoting |
| OLD | NEW |