Chromium Code Reviews| 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 kRoundTripsNeeded = 4; | |
|
Alpha Left Google
2014/09/11 01:24:26
nit: This constant needs to be more descriptive or
hubbe
2014/09/11 19:17:19
I actually think the name is pretty good (but I'm
| |
| 23 | |
| 22 // Note, we use a fixed bitrate value when external video encoder is used. | 24 // Note, we use a fixed bitrate value when external video encoder is used. |
| 23 // Some hardware encoder shows bad behavior if we set the bitrate too | 25 // Some hardware encoder shows bad behavior if we set the bitrate too |
| 24 // frequently, e.g. quality drop, not abiding by target bitrate, etc. | 26 // frequently, e.g. quality drop, not abiding by target bitrate, etc. |
| 25 // See details: crbug.com/392086. | 27 // See details: crbug.com/392086. |
| 26 VideoSender::VideoSender( | 28 VideoSender::VideoSender( |
| 27 scoped_refptr<CastEnvironment> cast_environment, | 29 scoped_refptr<CastEnvironment> cast_environment, |
| 28 const VideoSenderConfig& video_config, | 30 const VideoSenderConfig& video_config, |
| 29 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, | 31 const CreateVideoEncodeAcceleratorCallback& create_vea_cb, |
| 30 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, | 32 const CreateVideoEncodeMemoryCallback& create_video_encode_mem_cb, |
| 31 CastTransportSender* const transport_sender) | 33 CastTransportSender* const transport_sender, |
| 34 const PlayoutDelayChangeCB& playout_delay_change_cb) | |
| 32 : FrameSender( | 35 : FrameSender( |
| 33 cast_environment, | 36 cast_environment, |
| 34 false, | 37 false, |
| 35 transport_sender, | 38 transport_sender, |
| 36 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), | 39 base::TimeDelta::FromMilliseconds(video_config.rtcp_interval), |
| 37 kVideoFrequency, | 40 kVideoFrequency, |
| 38 video_config.ssrc, | 41 video_config.ssrc, |
| 39 video_config.max_frame_rate, | 42 video_config.max_frame_rate, |
| 40 video_config.target_playout_delay, | 43 video_config.min_playout_delay, |
| 44 video_config.max_playout_delay, | |
| 41 NewFixedCongestionControl( | 45 NewFixedCongestionControl( |
| 42 (video_config.min_bitrate + video_config.max_bitrate) / 2)), | 46 (video_config.min_bitrate + video_config.max_bitrate) / 2)), |
| 43 frames_in_encoder_(0), | 47 frames_in_encoder_(0), |
| 44 last_bitrate_(0), | 48 last_bitrate_(0), |
| 49 playout_delay_change_cb_(playout_delay_change_cb), | |
| 45 weak_factory_(this) { | 50 weak_factory_(this) { |
| 46 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; | 51 cast_initialization_status_ = STATUS_VIDEO_UNINITIALIZED; |
| 47 VLOG(1) << "max_unacked_frames is " << max_unacked_frames_ | 52 VLOG(1) << "max_unacked_frames is " << max_unacked_frames_ |
| 48 << " for target_playout_delay=" | 53 << " for target_playout_delay=" |
| 49 << target_playout_delay_.InMilliseconds() << " ms" | 54 << target_playout_delay_.InMilliseconds() << " ms" |
| 50 << " and max_frame_rate=" << video_config.max_frame_rate; | 55 << " and max_frame_rate=" << video_config.max_frame_rate; |
| 51 DCHECK_GT(max_unacked_frames_, 0); | 56 DCHECK_GT(max_unacked_frames_, 0); |
| 52 | 57 |
| 53 if (video_config.use_external_encoder) { | 58 if (video_config.use_external_encoder) { |
| 54 video_encoder_.reset(new ExternalVideoEncoder(cast_environment, | 59 video_encoder_.reset(new ExternalVideoEncoder(cast_environment, |
| 55 video_config, | 60 video_config, |
| 56 create_vea_cb, | 61 create_vea_cb, |
| 57 create_video_encode_mem_cb)); | 62 create_video_encode_mem_cb)); |
| 58 } else { | 63 } else { |
| 59 congestion_control_.reset( | 64 congestion_control_.reset( |
| 60 NewAdaptiveCongestionControl(cast_environment->Clock(), | 65 NewAdaptiveCongestionControl(cast_environment->Clock(), |
| 61 video_config.max_bitrate, | 66 video_config.max_bitrate, |
| 62 video_config.min_bitrate, | 67 video_config.min_bitrate, |
| 63 max_unacked_frames_)); | 68 max_unacked_frames_)); |
| 64 video_encoder_.reset(new VideoEncoderImpl( | 69 video_encoder_.reset(new VideoEncoderImpl( |
| 65 cast_environment, video_config, max_unacked_frames_)); | 70 cast_environment, video_config, max_unacked_frames_)); |
| 66 } | 71 } |
| 67 cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; | 72 cast_initialization_status_ = STATUS_VIDEO_INITIALIZED; |
| 68 | 73 |
| 69 media::cast::CastTransportRtpConfig transport_config; | 74 media::cast::CastTransportRtpConfig transport_config; |
| 70 transport_config.ssrc = video_config.ssrc; | 75 transport_config.ssrc = video_config.ssrc; |
| 71 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; | 76 transport_config.feedback_ssrc = video_config.incoming_feedback_ssrc; |
| 72 transport_config.rtp_payload_type = video_config.rtp_payload_type; | 77 transport_config.rtp_payload_type = video_config.rtp_payload_type; |
| 73 transport_config.stored_frames = max_unacked_frames_; | 78 transport_config.stored_frames = |
| 79 std::min(kMaxUnackedFrames, | |
| 80 1 + static_cast<int>(max_playout_delay_ * | |
| 81 max_frame_rate_ / | |
| 82 base::TimeDelta::FromSeconds(1))); | |
| 74 transport_config.aes_key = video_config.aes_key; | 83 transport_config.aes_key = video_config.aes_key; |
| 75 transport_config.aes_iv_mask = video_config.aes_iv_mask; | 84 transport_config.aes_iv_mask = video_config.aes_iv_mask; |
| 76 | 85 |
| 77 transport_sender->InitializeVideo( | 86 transport_sender->InitializeVideo( |
| 78 transport_config, | 87 transport_config, |
| 79 base::Bind(&VideoSender::OnReceivedCastFeedback, | 88 base::Bind(&VideoSender::OnReceivedCastFeedback, |
| 80 weak_factory_.GetWeakPtr()), | 89 weak_factory_.GetWeakPtr()), |
| 81 base::Bind(&VideoSender::OnMeasuredRoundTripTime, | 90 base::Bind(&VideoSender::OnMeasuredRoundTripTime, |
| 82 weak_factory_.GetWeakPtr())); | 91 weak_factory_.GetWeakPtr())); |
| 83 } | 92 } |
| (...skipping 22 matching lines...) Expand all Loading... | |
| 106 kFrameIdUnknown); | 115 kFrameIdUnknown); |
| 107 | 116 |
| 108 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc | 117 // Used by chrome/browser/extension/api/cast_streaming/performance_test.cc |
| 109 TRACE_EVENT_INSTANT2( | 118 TRACE_EVENT_INSTANT2( |
| 110 "cast_perf_test", "InsertRawVideoFrame", | 119 "cast_perf_test", "InsertRawVideoFrame", |
| 111 TRACE_EVENT_SCOPE_THREAD, | 120 TRACE_EVENT_SCOPE_THREAD, |
| 112 "timestamp", capture_time.ToInternalValue(), | 121 "timestamp", capture_time.ToInternalValue(), |
| 113 "rtp_timestamp", rtp_timestamp); | 122 "rtp_timestamp", rtp_timestamp); |
| 114 | 123 |
| 115 if (ShouldDropNextFrame(capture_time)) { | 124 if (ShouldDropNextFrame(capture_time)) { |
| 125 base::TimeDelta new_target_delay = std::min( | |
| 126 AverageRTT() * kRoundTripsNeeded, | |
| 127 max_playout_delay_); | |
| 128 if (new_target_delay > target_playout_delay_) { | |
| 129 playout_delay_change_cb_.Run(new_target_delay); | |
| 130 } | |
| 116 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; | 131 VLOG(1) << "Dropping frame due to too many frames currently in-flight."; |
| 117 return; | 132 return; |
| 118 } | 133 } |
| 119 | 134 |
| 120 uint32 bitrate = congestion_control_->GetBitrate( | 135 uint32 bitrate = congestion_control_->GetBitrate( |
| 121 capture_time + target_playout_delay_, target_playout_delay_); | 136 capture_time + target_playout_delay_, target_playout_delay_); |
| 122 if (bitrate != last_bitrate_) { | 137 if (bitrate != last_bitrate_) { |
| 123 video_encoder_->SetBitRate(bitrate); | 138 video_encoder_->SetBitRate(bitrate); |
| 124 last_bitrate_ = bitrate; | 139 last_bitrate_ = bitrate; |
| 125 } | 140 } |
| (...skipping 24 matching lines...) Expand all Loading... | |
| 150 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); | 165 DCHECK(cast_environment_->CurrentlyOn(CastEnvironment::MAIN)); |
| 151 | 166 |
| 152 frames_in_encoder_--; | 167 frames_in_encoder_--; |
| 153 DCHECK_GE(frames_in_encoder_, 0); | 168 DCHECK_GE(frames_in_encoder_, 0); |
| 154 | 169 |
| 155 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); | 170 SendEncodedFrame(encoder_bitrate, encoded_frame.Pass()); |
| 156 } | 171 } |
| 157 | 172 |
| 158 } // namespace cast | 173 } // namespace cast |
| 159 } // namespace media | 174 } // namespace media |
| OLD | NEW |