OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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 "media/cast/sender/video_sender.h" | 5 #include "media/cast/sender/video_sender.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 #include <cstring> | 8 #include <cstring> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/debug/trace_event.h" | 11 #include "base/debug/trace_event.h" |
12 #include "base/logging.h" | 12 #include "base/logging.h" |
13 #include "base/message_loop/message_loop.h" | 13 #include "base/message_loop/message_loop.h" |
14 #include "media/cast/cast_defines.h" | 14 #include "media/cast/cast_defines.h" |
15 #include "media/cast/net/cast_transport_config.h" | 15 #include "media/cast/net/cast_transport_config.h" |
16 #include "media/cast/sender/external_video_encoder.h" | 16 #include "media/cast/sender/external_video_encoder.h" |
17 #include "media/cast/sender/video_encoder_impl.h" | 17 #include "media/cast/sender/video_encoder_impl.h" |
18 | 18 |
19 namespace media { | 19 namespace media { |
20 namespace cast { | 20 namespace cast { |
21 | 21 |
22 const int kNumAggressiveReportsSentAtStart = 100; | 22 const int kNumAggressiveReportsSentAtStart = 100; |
23 const int kMinSchedulingDelayMs = 1; | |
24 | 23 |
25 namespace { | 24 namespace { |
26 | 25 |
27 // Returns a fixed bitrate value when external video encoder is used. | 26 // Returns a fixed bitrate value when external video encoder is used. |
28 // Some hardware encoder shows bad behavior if we set the bitrate too | 27 // Some hardware encoder shows bad behavior if we set the bitrate too |
29 // frequently, e.g. quality drop, not abiding by target bitrate, etc. | 28 // frequently, e.g. quality drop, not abiding by target bitrate, etc. |
30 // See details: crbug.com/392086. | 29 // See details: crbug.com/392086. |
31 size_t GetFixedBitrate(const VideoSenderConfig& video_config) { | 30 size_t GetFixedBitrate(const VideoSenderConfig& video_config) { |
32 if (!video_config.use_external_encoder) | 31 if (!video_config.use_external_encoder) |
33 return 0; | 32 return 0; |
(...skipping 10 matching lines...) Expand all Loading... |
44 CastTransportSender* const transport_sender) | 43 CastTransportSender* const transport_sender) |
45 : FrameSender( | 44 : FrameSender( |
46 cast_environment, | 45 cast_environment, |
47 transport_sender, | 46 transport_sender, |
48 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), | 47 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), |
49 kVideoFrequency, | 48 kVideoFrequency, |
50 video_config.ssrc, | 49 video_config.ssrc, |
51 video_config.max_frame_rate, | 50 video_config.max_frame_rate, |
52 video_config.target_playout_delay), | 51 video_config.target_playout_delay), |
53 fixed_bitrate_(GetFixedBitrate(video_config)), | 52 fixed_bitrate_(GetFixedBitrate(video_config)), |
54 num_aggressive_rtcp_reports_sent_(0), | |
55 frames_in_encoder_(0), | 53 frames_in_encoder_(0), |
56 last_sent_frame_id_(0), | |
57 latest_acked_frame_id_(0), | |
58 duplicate_ack_counter_(0), | |
59 congestion_control_(cast_environment->Clock(), | 54 congestion_control_(cast_environment->Clock(), |
60 video_config.max_bitrate, | 55 video_config.max_bitrate, |
61 video_config.min_bitrate, | 56 video_config.min_bitrate, |
62 max_unacked_frames_), | 57 max_unacked_frames_), |
63 cast_initialization_status_(STATUS_VIDEO_UNINITIALIZED), | |
64 weak_factory_(this) { | 58 weak_factory_(this) { |
| 59 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; |
65 VLOG(1) << "max_unacked_frames is " << max_unacked_frames_ | 60 VLOG(1) << "max_unacked_frames is " << max_unacked_frames_ |
66 << " for target_playout_delay=" | 61 << " for target_playout_delay=" |
67 << target_playout_delay_.InMilliseconds() << " ms" | 62 << target_playout_delay_.InMilliseconds() << " ms" |
68 << " and max_frame_rate=" << video_config.max_frame_rate; | 63 << " and max_frame_rate=" << video_config.max_frame_rate; |
69 DCHECK_GT(max_unacked_frames_, 0); | 64 DCHECK_GT(max_unacked_frames_, 0); |
70 | 65 |
71 if (video_config.use_external_encoder) { | 66 if (video_config.use_external_encoder) { |
72 video_encoder_.reset(new ExternalVideoEncoder(cast_environment, | 67 video_encoder_.reset(new ExternalVideoEncoder(cast_environment, |
73 video_config, | 68 video_config, |
74 create_vea_cb, | 69 create_vea_cb, |
(...skipping 10 matching lines...) Expand all Loading... |
85 transport_config.rtp_payload_type = video_config.rtp_payload_type; | 80 transport_config.rtp_payload_type = video_config.rtp_payload_type; |
86 transport_config.stored_frames = max_unacked_frames_; | 81 transport_config.stored_frames = max_unacked_frames_; |
87 transport_config.aes_key = video_config.aes_key; | 82 transport_config.aes_key = video_config.aes_key; |
88 transport_config.aes_iv_mask = video_config.aes_iv_mask; | 83 transport_config.aes_iv_mask = video_config.aes_iv_mask; |
89 | 84 |
90 transport_sender->InitializeVideo( | 85 transport_sender->InitializeVideo( |
91 transport_config, | 86 transport_config, |
92 base::Bind(&VideoSender::OnReceivedCastFeedback, | 87 base::Bind(&VideoSender::OnReceivedCastFeedback, |
93 weak_factory_.GetWeakPtr()), | 88 weak_factory_.GetWeakPtr()), |
94 base::Bind(&VideoSender::OnReceivedRtt, weak_factory_.GetWeakPtr())); | 89 base::Bind(&VideoSender::OnReceivedRtt, weak_factory_.GetWeakPtr())); |
95 | |
96 memset(frame_id_to_rtp_timestamp_, 0, sizeof(frame_id_to_rtp_timestamp_)); | |
97 } | 90 } |
98 | 91 |
99 VideoSender::~VideoSender() { | 92 VideoSender::~VideoSender() { |
100 } | 93 } |
101 | 94 |
102 void VideoSender::InsertRawVideoFrame( | 95 void VideoSender::InsertRawVideoFrame( |
103 const scoped_refptr<media::VideoFrame>& video_frame, | 96 const scoped_refptr<media::VideoFrame>& video_frame, |
104 const base::TimeTicks& capture_time) { | 97 const base::TimeTicks& capture_time) { |
105 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 98 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
106 if (cast_initialization_status_ != STATUS_VIDEO_INITIALIZED) { | 99 if (cast_initialization_status_ != STATUS_VIDEO_INITIALIZED) { |
(...skipping 109 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
216 congestion_control_.SendFrameToTransport( | 209 congestion_control_.SendFrameToTransport( |
217 frame_id, encoded_frame->data.size() * 8, last_send_time_); | 210 frame_id, encoded_frame->data.size() * 8, last_send_time_); |
218 | 211 |
219 if (send_target_playout_delay_) { | 212 if (send_target_playout_delay_) { |
220 encoded_frame->new_playout_delay_ms = | 213 encoded_frame->new_playout_delay_ms = |
221 target_playout_delay_.InMilliseconds(); | 214 target_playout_delay_.InMilliseconds(); |
222 } | 215 } |
223 transport_sender_->InsertCodedVideoFrame(*encoded_frame); | 216 transport_sender_->InsertCodedVideoFrame(*encoded_frame); |
224 } | 217 } |
225 | 218 |
226 void VideoSender::ResendCheck() { | |
227 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
228 DCHECK(!last_send_time_.is_null()); | |
229 const base::TimeDelta time_since_last_send = | |
230 cast_environment_->Clock()->NowTicks() - last_send_time_; | |
231 if (time_since_last_send > target_playout_delay_) { | |
232 if (latest_acked_frame_id_ == last_sent_frame_id_) { | |
233 // Last frame acked, no point in doing anything | |
234 } else { | |
235 VLOG(1) << "ACK timeout; last acked frame: " << latest_acked_frame_id_; | |
236 ResendForKickstart(); | |
237 } | |
238 } | |
239 ScheduleNextResendCheck(); | |
240 } | |
241 | |
242 void VideoSender::ScheduleNextResendCheck() { | |
243 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
244 DCHECK(!last_send_time_.is_null()); | |
245 base::TimeDelta time_to_next = | |
246 last_send_time_ - cast_environment_->Clock()->NowTicks() + | |
247 target_playout_delay_; | |
248 time_to_next = std::max( | |
249 time_to_next, base::TimeDelta::FromMilliseconds(kMinSchedulingDelayMs)); | |
250 cast_environment_->PostDelayedTask( | |
251 CastEnvironment::MAIN, | |
252 FROM_HERE, | |
253 base::Bind(&VideoSender::ResendCheck, weak_factory_.GetWeakPtr()), | |
254 time_to_next); | |
255 } | |
256 | |
257 void VideoSender::OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) { | 219 void VideoSender::OnReceivedCastFeedback(const RtcpCastMessage& cast_feedback) { |
258 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 220 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
259 | 221 |
260 base::TimeDelta rtt; | 222 base::TimeDelta rtt; |
261 base::TimeDelta avg_rtt; | 223 base::TimeDelta avg_rtt; |
262 base::TimeDelta min_rtt; | 224 base::TimeDelta min_rtt; |
263 base::TimeDelta max_rtt; | 225 base::TimeDelta max_rtt; |
264 if (is_rtt_available()) { | 226 if (is_rtt_available()) { |
265 rtt = rtt_; | 227 rtt = rtt_; |
266 avg_rtt = avg_rtt_; | 228 avg_rtt = avg_rtt_; |
(...skipping 78 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 frames_in_flight += | 307 frames_in_flight += |
346 static_cast<int32>(last_sent_frame_id_ - latest_acked_frame_id_); | 308 static_cast<int32>(last_sent_frame_id_ - latest_acked_frame_id_); |
347 } | 309 } |
348 VLOG(2) << frames_in_flight | 310 VLOG(2) << frames_in_flight |
349 << " frames in flight; last sent: " << last_sent_frame_id_ | 311 << " frames in flight; last sent: " << last_sent_frame_id_ |
350 << " latest acked: " << latest_acked_frame_id_ | 312 << " latest acked: " << latest_acked_frame_id_ |
351 << " frames in encoder: " << frames_in_encoder_; | 313 << " frames in encoder: " << frames_in_encoder_; |
352 return frames_in_flight >= max_unacked_frames_; | 314 return frames_in_flight >= max_unacked_frames_; |
353 } | 315 } |
354 | 316 |
355 void VideoSender::ResendForKickstart() { | |
356 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | |
357 DCHECK(!last_send_time_.is_null()); | |
358 VLOG(1) << "Resending last packet of frame " << last_sent_frame_id_ | |
359 << " to kick-start."; | |
360 last_send_time_ = cast_environment_->Clock()->NowTicks(); | |
361 transport_sender_->ResendFrameForKickstart(ssrc_, last_sent_frame_id_); | |
362 } | |
363 | |
364 } // namespace cast | 317 } // namespace cast |
365 } // namespace media | 318 } // namespace media |
OLD | NEW |