Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "video/video_send_stream_impl.h" |
| 11 | |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 12 | #include <stdio.h> |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 13 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 14 | #include <algorithm> |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 15 | #include <cstdint> |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 16 | #include <string> |
| 17 | #include <utility> |
| 18 | |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 19 | #include "absl/algorithm/container.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 20 | #include "api/crypto/crypto_options.h" |
| 21 | #include "api/rtp_parameters.h" |
Mirko Bonadei | d970807 | 2019-01-25 20:26:48 +0100 | [diff] [blame] | 22 | #include "api/scoped_refptr.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 23 | #include "api/video_codecs/video_codec.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 24 | #include "call/rtp_transport_controller_send_interface.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 25 | #include "call/video_send_stream.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 26 | #include "modules/pacing/paced_sender.h" |
Steve Anton | 10542f2 | 2019-01-11 09:11:00 -0800 | [diff] [blame] | 27 | #include "rtc_base/atomic_ops.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 28 | #include "rtc_base/checks.h" |
| 29 | #include "rtc_base/experiments/alr_experiment.h" |
Ying Wang | 8c5520c | 2019-09-03 15:25:21 +0000 | [diff] [blame] | 30 | #include "rtc_base/experiments/field_trial_parser.h" |
Elad Alon | 80f53b7 | 2019-10-11 16:19:43 +0200 | [diff] [blame] | 31 | #include "rtc_base/experiments/min_video_bitrate_experiment.h" |
Erik Språng | cd76eab | 2019-01-21 18:06:46 +0100 | [diff] [blame] | 32 | #include "rtc_base/experiments/rate_control_settings.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 33 | #include "rtc_base/logging.h" |
| 34 | #include "rtc_base/numerics/safe_conversions.h" |
Sebastian Jansson | b55015e | 2019-04-09 13:44:04 +0200 | [diff] [blame] | 35 | #include "rtc_base/synchronization/sequence_checker.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 36 | #include "rtc_base/thread_checker.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 37 | #include "rtc_base/trace_event.h" |
Yves Gerey | 3e70781 | 2018-11-28 16:47:49 +0100 | [diff] [blame] | 38 | #include "system_wrappers/include/clock.h" |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 39 | #include "system_wrappers/include/field_trial.h" |
| 40 | |
| 41 | namespace webrtc { |
| 42 | namespace internal { |
| 43 | namespace { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 44 | |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 45 | // Max positive size difference to treat allocations as "similar". |
| 46 | static constexpr int kMaxVbaSizeDifferencePercent = 10; |
| 47 | // Max time we will throttle similar video bitrate allocations. |
| 48 | static constexpr int64_t kMaxVbaThrottleTimeMs = 500; |
| 49 | |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 50 | constexpr TimeDelta kEncoderTimeOut = TimeDelta::Seconds(2); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 51 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 52 | bool TransportSeqNumExtensionConfigured(const VideoSendStream::Config& config) { |
| 53 | const std::vector<RtpExtension>& extensions = config.rtp.extensions; |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 54 | return absl::c_any_of(extensions, [](const RtpExtension& ext) { |
| 55 | return ext.uri == RtpExtension::kTransportSequenceNumberUri; |
| 56 | }); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 57 | } |
| 58 | |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 59 | // Calculate max padding bitrate for a multi layer codec. |
| 60 | int CalculateMaxPadBitrateBps(const std::vector<VideoStream>& streams, |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 61 | bool is_svc, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 62 | VideoEncoderConfig::ContentType content_type, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 63 | int min_transmit_bitrate_bps, |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 64 | bool pad_to_min_bitrate, |
| 65 | bool alr_probing) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 66 | int pad_up_to_bitrate_bps = 0; |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 67 | |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 68 | RTC_DCHECK(!is_svc || streams.size() <= 1) << "Only one stream is allowed in " |
| 69 | "SVC mode."; |
| 70 | |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 71 | // Filter out only the active streams; |
| 72 | std::vector<VideoStream> active_streams; |
| 73 | for (const VideoStream& stream : streams) { |
| 74 | if (stream.active) |
| 75 | active_streams.emplace_back(stream); |
| 76 | } |
| 77 | |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 78 | if (active_streams.size() > 1 || (!active_streams.empty() && is_svc)) { |
| 79 | // Simulcast or SVC is used. |
| 80 | // if SVC is used, stream bitrates should already encode svc bitrates: |
| 81 | // min_bitrate = min bitrate of a lowest svc layer. |
| 82 | // target_bitrate = sum of target bitrates of lower layers + min bitrate |
| 83 | // of the last one (as used in the calculations below). |
| 84 | // max_bitrate = sum of all active layers' max_bitrate. |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 85 | if (alr_probing) { |
| 86 | // With alr probing, just pad to the min bitrate of the lowest stream, |
| 87 | // probing will handle the rest of the rampup. |
| 88 | pad_up_to_bitrate_bps = active_streams[0].min_bitrate_bps; |
| 89 | } else { |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 90 | // Without alr probing, pad up to start bitrate of the |
| 91 | // highest active stream. |
| 92 | const double hysteresis_factor = |
| 93 | RateControlSettings::ParseFromFieldTrials() |
| 94 | .GetSimulcastHysteresisFactor(content_type); |
Erik Språng | 576db1b | 2020-06-08 13:32:20 +0200 | [diff] [blame] | 95 | if (is_svc) { |
| 96 | // For SVC, since there is only one "stream", the padding bitrate |
| 97 | // needed to enable the top spatial layer is stored in the |
| 98 | // |target_bitrate_bps| field. |
| 99 | // TODO(sprang): This behavior needs to die. |
| 100 | pad_up_to_bitrate_bps = static_cast<int>( |
| 101 | hysteresis_factor * active_streams[0].target_bitrate_bps + 0.5); |
| 102 | } else { |
| 103 | const size_t top_active_stream_idx = active_streams.size() - 1; |
| 104 | pad_up_to_bitrate_bps = std::min( |
| 105 | static_cast<int>( |
| 106 | hysteresis_factor * |
| 107 | active_streams[top_active_stream_idx].min_bitrate_bps + |
| 108 | 0.5), |
| 109 | active_streams[top_active_stream_idx].target_bitrate_bps); |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 110 | |
Erik Språng | 576db1b | 2020-06-08 13:32:20 +0200 | [diff] [blame] | 111 | // Add target_bitrate_bps of the lower active streams. |
| 112 | for (size_t i = 0; i < top_active_stream_idx; ++i) { |
| 113 | pad_up_to_bitrate_bps += active_streams[i].target_bitrate_bps; |
| 114 | } |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 115 | } |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 116 | } |
| 117 | } else if (!active_streams.empty() && pad_to_min_bitrate) { |
| 118 | pad_up_to_bitrate_bps = active_streams[0].min_bitrate_bps; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | pad_up_to_bitrate_bps = |
| 122 | std::max(pad_up_to_bitrate_bps, min_transmit_bitrate_bps); |
| 123 | |
| 124 | return pad_up_to_bitrate_bps; |
| 125 | } |
| 126 | |
Benjamin Wright | 192eeec | 2018-10-17 17:27:25 -0700 | [diff] [blame] | 127 | RtpSenderFrameEncryptionConfig CreateFrameEncryptionConfig( |
| 128 | const VideoSendStream::Config* config) { |
| 129 | RtpSenderFrameEncryptionConfig frame_encryption_config; |
| 130 | frame_encryption_config.frame_encryptor = config->frame_encryptor; |
| 131 | frame_encryption_config.crypto_options = config->crypto_options; |
| 132 | return frame_encryption_config; |
| 133 | } |
| 134 | |
Tommi | 8ae18ad | 2020-05-03 22:45:02 +0200 | [diff] [blame] | 135 | RtpSenderObservers CreateObservers(RtcpRttStats* call_stats, |
Elad Alon | 14d1c9d | 2019-04-08 14:16:17 +0200 | [diff] [blame] | 136 | EncoderRtcpFeedback* encoder_feedback, |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 137 | SendStatisticsProxy* stats_proxy, |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 138 | SendDelayStats* send_delay_stats) { |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 139 | RtpSenderObservers observers; |
| 140 | observers.rtcp_rtt_stats = call_stats; |
| 141 | observers.intra_frame_callback = encoder_feedback; |
Elad Alon | 0a8562e | 2019-04-09 11:55:13 +0200 | [diff] [blame] | 142 | observers.rtcp_loss_notification_observer = encoder_feedback; |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 143 | observers.rtcp_stats = stats_proxy; |
Henrik Boström | 87e3f9d | 2019-05-27 10:44:24 +0200 | [diff] [blame] | 144 | observers.report_block_data_observer = stats_proxy; |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 145 | observers.rtp_stats = stats_proxy; |
| 146 | observers.bitrate_observer = stats_proxy; |
| 147 | observers.frame_count_observer = stats_proxy; |
| 148 | observers.rtcp_type_observer = stats_proxy; |
| 149 | observers.send_delay_observer = stats_proxy; |
| 150 | observers.send_packet_observer = send_delay_stats; |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 151 | return observers; |
| 152 | } |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 153 | |
| 154 | absl::optional<AlrExperimentSettings> GetAlrSettings( |
| 155 | VideoEncoderConfig::ContentType content_type) { |
| 156 | if (content_type == VideoEncoderConfig::ContentType::kScreen) { |
| 157 | return AlrExperimentSettings::CreateFromFieldTrial( |
| 158 | AlrExperimentSettings::kScreenshareProbingBweExperimentName); |
| 159 | } |
| 160 | return AlrExperimentSettings::CreateFromFieldTrial( |
| 161 | AlrExperimentSettings::kStrictPacingAndProbingExperimentName); |
| 162 | } |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 163 | |
| 164 | bool SameStreamsEnabled(const VideoBitrateAllocation& lhs, |
| 165 | const VideoBitrateAllocation& rhs) { |
| 166 | for (size_t si = 0; si < kMaxSpatialLayers; ++si) { |
| 167 | for (size_t ti = 0; ti < kMaxTemporalStreams; ++ti) { |
| 168 | if (lhs.HasBitrate(si, ti) != rhs.HasBitrate(si, ti)) { |
| 169 | return false; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | return true; |
| 174 | } |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 175 | } // namespace |
| 176 | |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 177 | PacingConfig::PacingConfig() |
| 178 | : pacing_factor("factor", PacedSender::kDefaultPaceMultiplier), |
| 179 | max_pacing_delay("max_delay", |
Danil Chapovalov | 0c626af | 2020-02-10 11:16:00 +0100 | [diff] [blame] | 180 | TimeDelta::Millis(PacedSender::kMaxQueueLengthMs)) { |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 181 | ParseFieldTrial({&pacing_factor, &max_pacing_delay}, |
| 182 | field_trial::FindFullName("WebRTC-Video-Pacing")); |
| 183 | } |
| 184 | PacingConfig::PacingConfig(const PacingConfig&) = default; |
| 185 | PacingConfig::~PacingConfig() = default; |
| 186 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 187 | VideoSendStreamImpl::VideoSendStreamImpl( |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 188 | Clock* clock, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 189 | SendStatisticsProxy* stats_proxy, |
| 190 | rtc::TaskQueue* worker_queue, |
Tommi | 8ae18ad | 2020-05-03 22:45:02 +0200 | [diff] [blame] | 191 | RtcpRttStats* call_stats, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 192 | RtpTransportControllerSendInterface* transport, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 193 | BitrateAllocatorInterface* bitrate_allocator, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 194 | SendDelayStats* send_delay_stats, |
Sebastian Jansson | 652dc91 | 2018-04-19 17:09:15 +0200 | [diff] [blame] | 195 | VideoStreamEncoderInterface* video_stream_encoder, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 196 | RtcEventLog* event_log, |
| 197 | const VideoSendStream::Config* config, |
| 198 | int initial_encoder_max_bitrate, |
| 199 | double initial_encoder_bitrate_priority, |
| 200 | std::map<uint32_t, RtpState> suspended_ssrcs, |
| 201 | std::map<uint32_t, RtpPayloadState> suspended_payload_states, |
| 202 | VideoEncoderConfig::ContentType content_type, |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 203 | std::unique_ptr<FecController> fec_controller) |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 204 | : clock_(clock), |
| 205 | has_alr_probing_(config->periodic_alr_bandwidth_probing || |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 206 | GetAlrSettings(content_type)), |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 207 | pacing_config_(PacingConfig()), |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 208 | stats_proxy_(stats_proxy), |
| 209 | config_(config), |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 210 | worker_queue_(worker_queue), |
Erik Språng | cd76eab | 2019-01-21 18:06:46 +0100 | [diff] [blame] | 211 | timed_out_(false), |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 212 | transport_(transport), |
| 213 | bitrate_allocator_(bitrate_allocator), |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 214 | disable_padding_(true), |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 215 | max_padding_bitrate_(0), |
| 216 | encoder_min_bitrate_bps_(0), |
| 217 | encoder_target_rate_bps_(0), |
| 218 | encoder_bitrate_priority_(initial_encoder_bitrate_priority), |
| 219 | has_packet_feedback_(false), |
| 220 | video_stream_encoder_(video_stream_encoder), |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 221 | encoder_feedback_(clock, config_->rtp.ssrcs, video_stream_encoder), |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 222 | bandwidth_observer_(transport->GetBandwidthObserver()), |
Marina Ciocea | e77912b | 2020-02-27 16:16:55 +0100 | [diff] [blame] | 223 | rtp_video_sender_( |
| 224 | transport_->CreateRtpVideoSender(suspended_ssrcs, |
| 225 | suspended_payload_states, |
| 226 | config_->rtp, |
| 227 | config_->rtcp_report_interval_ms, |
| 228 | config_->send_transport, |
| 229 | CreateObservers(call_stats, |
| 230 | &encoder_feedback_, |
| 231 | stats_proxy_, |
| 232 | send_delay_stats), |
| 233 | event_log, |
| 234 | std::move(fec_controller), |
| 235 | CreateFrameEncryptionConfig(config_), |
| 236 | config->frame_transformer)), |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 237 | weak_ptr_factory_(this) { |
Elad Alon | 8f01c4e | 2019-06-28 15:19:43 +0200 | [diff] [blame] | 238 | video_stream_encoder->SetFecControllerOverride(rtp_video_sender_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 239 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 240 | RTC_LOG(LS_INFO) << "VideoSendStreamInternal: " << config_->ToString(); |
| 241 | weak_ptr_ = weak_ptr_factory_.GetWeakPtr(); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 242 | |
Elad Alon | b6ef99b | 2019-04-10 16:37:07 +0200 | [diff] [blame] | 243 | encoder_feedback_.SetRtpVideoSender(rtp_video_sender_); |
| 244 | |
Bjorn A Mellem | 7a9a092 | 2019-11-26 09:19:40 -0800 | [diff] [blame] | 245 | RTC_DCHECK(!config_->rtp.ssrcs.empty()); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 246 | RTC_DCHECK(transport_); |
| 247 | RTC_DCHECK_NE(initial_encoder_max_bitrate, 0); |
| 248 | |
| 249 | if (initial_encoder_max_bitrate > 0) { |
| 250 | encoder_max_bitrate_bps_ = |
| 251 | rtc::dchecked_cast<uint32_t>(initial_encoder_max_bitrate); |
| 252 | } else { |
| 253 | // TODO(srte): Make sure max bitrate is not set to negative values. We don't |
| 254 | // have any way to handle unset values in downstream code, such as the |
| 255 | // bitrate allocator. Previously -1 was implicitly casted to UINT32_MAX, a |
| 256 | // behaviour that is not safe. Converting to 10 Mbps should be safe for |
| 257 | // reasonable use cases as it allows adding the max of multiple streams |
| 258 | // without wrappping around. |
| 259 | const int kFallbackMaxBitrateBps = 10000000; |
| 260 | RTC_DLOG(LS_ERROR) << "ERROR: Initial encoder max bitrate = " |
| 261 | << initial_encoder_max_bitrate << " which is <= 0!"; |
| 262 | RTC_DLOG(LS_INFO) << "Using default encoder max bitrate = 10 Mbps"; |
| 263 | encoder_max_bitrate_bps_ = kFallbackMaxBitrateBps; |
| 264 | } |
| 265 | |
| 266 | RTC_CHECK(AlrExperimentSettings::MaxOneFieldTrialEnabled()); |
| 267 | // If send-side BWE is enabled, check if we should apply updated probing and |
| 268 | // pacing settings. |
| 269 | if (TransportSeqNumExtensionConfigured(*config_)) { |
| 270 | has_packet_feedback_ = true; |
| 271 | |
Erik Språng | b57ab38 | 2018-09-13 10:52:38 +0200 | [diff] [blame] | 272 | absl::optional<AlrExperimentSettings> alr_settings = |
| 273 | GetAlrSettings(content_type); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 274 | if (alr_settings) { |
| 275 | transport->EnablePeriodicAlrProbing(true); |
| 276 | transport->SetPacingFactor(alr_settings->pacing_factor); |
| 277 | configured_pacing_factor_ = alr_settings->pacing_factor; |
| 278 | transport->SetQueueTimeLimit(alr_settings->max_paced_queue_time); |
| 279 | } else { |
Erik Språng | cd76eab | 2019-01-21 18:06:46 +0100 | [diff] [blame] | 280 | RateControlSettings rate_control_settings = |
| 281 | RateControlSettings::ParseFromFieldTrials(); |
| 282 | |
| 283 | transport->EnablePeriodicAlrProbing( |
| 284 | rate_control_settings.UseAlrProbing()); |
| 285 | const double pacing_factor = |
| 286 | rate_control_settings.GetPacingFactor().value_or( |
| 287 | pacing_config_.pacing_factor); |
| 288 | transport->SetPacingFactor(pacing_factor); |
| 289 | configured_pacing_factor_ = pacing_factor; |
Christoffer Rodbro | 196c5ba | 2018-11-27 11:56:25 +0100 | [diff] [blame] | 290 | transport->SetQueueTimeLimit(pacing_config_.max_pacing_delay.Get().ms()); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 291 | } |
| 292 | } |
| 293 | |
| 294 | if (config_->periodic_alr_bandwidth_probing) { |
| 295 | transport->EnablePeriodicAlrProbing(true); |
| 296 | } |
| 297 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 298 | RTC_DCHECK_GE(config_->rtp.payload_type, 0); |
| 299 | RTC_DCHECK_LE(config_->rtp.payload_type, 127); |
| 300 | |
| 301 | video_stream_encoder_->SetStartBitrate( |
| 302 | bitrate_allocator_->GetStartBitrate(this)); |
| 303 | |
| 304 | // Only request rotation at the source when we positively know that the remote |
| 305 | // side doesn't support the rotation extension. This allows us to prepare the |
| 306 | // encoder in the expectation that rotation is supported - which is the common |
| 307 | // case. |
Steve Anton | bd631a0 | 2019-03-28 10:51:27 -0700 | [diff] [blame] | 308 | bool rotation_applied = absl::c_none_of( |
| 309 | config_->rtp.extensions, [](const RtpExtension& extension) { |
| 310 | return extension.uri == RtpExtension::kVideoRotationUri; |
| 311 | }); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 312 | |
| 313 | video_stream_encoder_->SetSink(this, rotation_applied); |
| 314 | } |
| 315 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 316 | VideoSendStreamImpl::~VideoSendStreamImpl() { |
| 317 | RTC_DCHECK_RUN_ON(worker_queue_); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 318 | RTC_DCHECK(!rtp_video_sender_->IsActive()) |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 319 | << "VideoSendStreamImpl::Stop not called"; |
| 320 | RTC_LOG(LS_INFO) << "~VideoSendStreamInternal: " << config_->ToString(); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 321 | transport_->DestroyRtpVideoSender(rtp_video_sender_); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | void VideoSendStreamImpl::RegisterProcessThread( |
| 325 | ProcessThread* module_process_thread) { |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 326 | rtp_video_sender_->RegisterProcessThread(module_process_thread); |
Stefan Holmer | dbdb3a0 | 2018-07-17 16:03:46 +0200 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void VideoSendStreamImpl::DeRegisterProcessThread() { |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 330 | rtp_video_sender_->DeRegisterProcessThread(); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Niels Möller | 8fb1a6a | 2019-03-05 14:29:42 +0100 | [diff] [blame] | 333 | void VideoSendStreamImpl::DeliverRtcp(const uint8_t* packet, size_t length) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 334 | // Runs on a network thread. |
| 335 | RTC_DCHECK(!worker_queue_->IsCurrent()); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 336 | rtp_video_sender_->DeliverRtcp(packet, length); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 337 | } |
| 338 | |
| 339 | void VideoSendStreamImpl::UpdateActiveSimulcastLayers( |
| 340 | const std::vector<bool> active_layers) { |
| 341 | RTC_DCHECK_RUN_ON(worker_queue_); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 342 | bool previously_active = rtp_video_sender_->IsActive(); |
| 343 | rtp_video_sender_->SetActiveModules(active_layers); |
| 344 | if (!rtp_video_sender_->IsActive() && previously_active) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 345 | // Payload router switched from active to inactive. |
| 346 | StopVideoSendStream(); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 347 | } else if (rtp_video_sender_->IsActive() && !previously_active) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 348 | // Payload router switched from inactive to active. |
| 349 | StartupVideoSendStream(); |
| 350 | } |
| 351 | } |
| 352 | |
| 353 | void VideoSendStreamImpl::Start() { |
| 354 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 355 | RTC_LOG(LS_INFO) << "VideoSendStream::Start"; |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 356 | if (rtp_video_sender_->IsActive()) |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 357 | return; |
| 358 | TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Start"); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 359 | rtp_video_sender_->SetActive(true); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 360 | StartupVideoSendStream(); |
| 361 | } |
| 362 | |
| 363 | void VideoSendStreamImpl::StartupVideoSendStream() { |
| 364 | RTC_DCHECK_RUN_ON(worker_queue_); |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 365 | bitrate_allocator_->AddObserver(this, GetAllocationConfig()); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 366 | // Start monitoring encoder activity. |
| 367 | { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 368 | RTC_DCHECK(!check_encoder_activity_task_.Running()); |
| 369 | |
| 370 | activity_ = false; |
| 371 | timed_out_ = false; |
Sebastian Jansson | cda86dd | 2019-03-11 17:26:36 +0100 | [diff] [blame] | 372 | check_encoder_activity_task_ = RepeatingTaskHandle::DelayedStart( |
| 373 | worker_queue_->Get(), kEncoderTimeOut, [this] { |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 374 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 375 | if (!activity_) { |
| 376 | if (!timed_out_) { |
| 377 | SignalEncoderTimedOut(); |
| 378 | } |
| 379 | timed_out_ = true; |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 380 | disable_padding_ = true; |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 381 | } else if (timed_out_) { |
| 382 | SignalEncoderActive(); |
| 383 | timed_out_ = false; |
| 384 | } |
| 385 | activity_ = false; |
| 386 | return kEncoderTimeOut; |
| 387 | }); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | video_stream_encoder_->SendKeyFrame(); |
| 391 | } |
| 392 | |
| 393 | void VideoSendStreamImpl::Stop() { |
| 394 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 395 | RTC_LOG(LS_INFO) << "VideoSendStream::Stop"; |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 396 | if (!rtp_video_sender_->IsActive()) |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 397 | return; |
| 398 | TRACE_EVENT_INSTANT0("webrtc", "VideoSendStream::Stop"); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 399 | rtp_video_sender_->SetActive(false); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 400 | StopVideoSendStream(); |
| 401 | } |
| 402 | |
| 403 | void VideoSendStreamImpl::StopVideoSendStream() { |
| 404 | bitrate_allocator_->RemoveObserver(this); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 405 | check_encoder_activity_task_.Stop(); |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 406 | video_stream_encoder_->OnBitrateUpdated(DataRate::Zero(), DataRate::Zero(), |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 407 | DataRate::Zero(), 0, 0, 0); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 408 | stats_proxy_->OnSetEncoderTargetRate(0); |
| 409 | } |
| 410 | |
| 411 | void VideoSendStreamImpl::SignalEncoderTimedOut() { |
| 412 | RTC_DCHECK_RUN_ON(worker_queue_); |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 413 | // If the encoder has not produced anything the last kEncoderTimeOut and it |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 414 | // is supposed to, deregister as BitrateAllocatorObserver. This can happen |
| 415 | // if a camera stops producing frames. |
| 416 | if (encoder_target_rate_bps_ > 0) { |
| 417 | RTC_LOG(LS_INFO) << "SignalEncoderTimedOut, Encoder timed out."; |
| 418 | bitrate_allocator_->RemoveObserver(this); |
| 419 | } |
| 420 | } |
| 421 | |
| 422 | void VideoSendStreamImpl::OnBitrateAllocationUpdated( |
Erik Språng | 566124a | 2018-04-23 12:32:22 +0200 | [diff] [blame] | 423 | const VideoBitrateAllocation& allocation) { |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 424 | if (!worker_queue_->IsCurrent()) { |
| 425 | auto ptr = weak_ptr_; |
| 426 | worker_queue_->PostTask([=] { |
| 427 | if (!ptr.get()) |
| 428 | return; |
| 429 | ptr->OnBitrateAllocationUpdated(allocation); |
| 430 | }); |
| 431 | return; |
| 432 | } |
| 433 | |
| 434 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 435 | |
Sebastian Jansson | 572c60f | 2019-03-04 18:30:41 +0100 | [diff] [blame] | 436 | int64_t now_ms = clock_->TimeInMilliseconds(); |
Erik Språng | f4ef2dd | 2018-09-11 12:37:51 +0200 | [diff] [blame] | 437 | if (encoder_target_rate_bps_ != 0) { |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 438 | if (video_bitrate_allocation_context_) { |
| 439 | // If new allocation is within kMaxVbaSizeDifferencePercent larger than |
| 440 | // the previously sent allocation and the same streams are still enabled, |
| 441 | // it is considered "similar". We do not want send similar allocations |
| 442 | // more once per kMaxVbaThrottleTimeMs. |
| 443 | const VideoBitrateAllocation& last = |
| 444 | video_bitrate_allocation_context_->last_sent_allocation; |
| 445 | const bool is_similar = |
| 446 | allocation.get_sum_bps() >= last.get_sum_bps() && |
| 447 | allocation.get_sum_bps() < |
| 448 | (last.get_sum_bps() * (100 + kMaxVbaSizeDifferencePercent)) / |
| 449 | 100 && |
| 450 | SameStreamsEnabled(allocation, last); |
| 451 | if (is_similar && |
| 452 | (now_ms - video_bitrate_allocation_context_->last_send_time_ms) < |
| 453 | kMaxVbaThrottleTimeMs) { |
| 454 | // This allocation is too similar, cache it and return. |
| 455 | video_bitrate_allocation_context_->throttled_allocation = allocation; |
| 456 | return; |
| 457 | } |
| 458 | } else { |
| 459 | video_bitrate_allocation_context_.emplace(); |
| 460 | } |
| 461 | |
| 462 | video_bitrate_allocation_context_->last_sent_allocation = allocation; |
| 463 | video_bitrate_allocation_context_->throttled_allocation.reset(); |
| 464 | video_bitrate_allocation_context_->last_send_time_ms = now_ms; |
| 465 | |
Erik Språng | f4ef2dd | 2018-09-11 12:37:51 +0200 | [diff] [blame] | 466 | // Send bitrate allocation metadata only if encoder is not paused. |
| 467 | rtp_video_sender_->OnBitrateAllocationUpdated(allocation); |
| 468 | } |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | void VideoSendStreamImpl::SignalEncoderActive() { |
| 472 | RTC_DCHECK_RUN_ON(worker_queue_); |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 473 | if (rtp_video_sender_->IsActive()) { |
| 474 | RTC_LOG(LS_INFO) << "SignalEncoderActive, Encoder is active."; |
| 475 | bitrate_allocator_->AddObserver(this, GetAllocationConfig()); |
| 476 | } |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | MediaStreamAllocationConfig VideoSendStreamImpl::GetAllocationConfig() const { |
| 480 | return MediaStreamAllocationConfig{ |
| 481 | static_cast<uint32_t>(encoder_min_bitrate_bps_), |
| 482 | encoder_max_bitrate_bps_, |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 483 | static_cast<uint32_t>(disable_padding_ ? 0 : max_padding_bitrate_), |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 484 | /* priority_bitrate */ 0, |
| 485 | !config_->suspend_below_min_bitrate, |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 486 | encoder_bitrate_priority_}; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | void VideoSendStreamImpl::OnEncoderConfigurationChanged( |
| 490 | std::vector<VideoStream> streams, |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 491 | bool is_svc, |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 492 | VideoEncoderConfig::ContentType content_type, |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 493 | int min_transmit_bitrate_bps) { |
| 494 | if (!worker_queue_->IsCurrent()) { |
| 495 | rtc::WeakPtr<VideoSendStreamImpl> send_stream = weak_ptr_; |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 496 | worker_queue_->PostTask([send_stream, streams, is_svc, content_type, |
Mirko Bonadei | 80a8687 | 2019-02-04 15:01:43 +0100 | [diff] [blame] | 497 | min_transmit_bitrate_bps]() mutable { |
| 498 | if (send_stream) { |
| 499 | send_stream->OnEncoderConfigurationChanged( |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 500 | std::move(streams), is_svc, content_type, min_transmit_bitrate_bps); |
Mirko Bonadei | 80a8687 | 2019-02-04 15:01:43 +0100 | [diff] [blame] | 501 | } |
| 502 | }); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 503 | return; |
| 504 | } |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 505 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 506 | RTC_DCHECK_GE(config_->rtp.ssrcs.size(), streams.size()); |
| 507 | TRACE_EVENT0("webrtc", "VideoSendStream::OnEncoderConfigurationChanged"); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 508 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 509 | |
Elad Alon | 80f53b7 | 2019-10-11 16:19:43 +0200 | [diff] [blame] | 510 | const VideoCodecType codec_type = |
| 511 | PayloadStringToCodecType(config_->rtp.payload_name); |
| 512 | |
| 513 | const absl::optional<DataRate> experimental_min_bitrate = |
| 514 | GetExperimentalMinVideoBitrate(codec_type); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 515 | encoder_min_bitrate_bps_ = |
Elad Alon | c67a4d6 | 2019-10-11 16:54:18 +0200 | [diff] [blame] | 516 | experimental_min_bitrate |
| 517 | ? experimental_min_bitrate->bps() |
| 518 | : std::max(streams[0].min_bitrate_bps, kDefaultMinVideoBitrateBps); |
| 519 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 520 | encoder_max_bitrate_bps_ = 0; |
| 521 | double stream_bitrate_priority_sum = 0; |
| 522 | for (const auto& stream : streams) { |
| 523 | // We don't want to allocate more bitrate than needed to inactive streams. |
| 524 | encoder_max_bitrate_bps_ += stream.active ? stream.max_bitrate_bps : 0; |
| 525 | if (stream.bitrate_priority) { |
| 526 | RTC_DCHECK_GT(*stream.bitrate_priority, 0); |
| 527 | stream_bitrate_priority_sum += *stream.bitrate_priority; |
| 528 | } |
| 529 | } |
| 530 | RTC_DCHECK_GT(stream_bitrate_priority_sum, 0); |
| 531 | encoder_bitrate_priority_ = stream_bitrate_priority_sum; |
| 532 | encoder_max_bitrate_bps_ = |
| 533 | std::max(static_cast<uint32_t>(encoder_min_bitrate_bps_), |
| 534 | encoder_max_bitrate_bps_); |
“Michael | 277a656 | 2018-06-01 14:09:19 -0500 | [diff] [blame] | 535 | |
Rasmus Brandt | c402dbe | 2019-02-04 11:09:46 +0100 | [diff] [blame] | 536 | // TODO(bugs.webrtc.org/10266): Query the VideoBitrateAllocator instead. |
Ilya Nikolaevskiy | 93be66c | 2020-04-02 14:10:27 +0200 | [diff] [blame] | 537 | max_padding_bitrate_ = CalculateMaxPadBitrateBps( |
| 538 | streams, is_svc, content_type, min_transmit_bitrate_bps, |
| 539 | config_->suspend_below_min_bitrate, has_alr_probing_); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 540 | |
| 541 | // Clear stats for disabled layers. |
| 542 | for (size_t i = streams.size(); i < config_->rtp.ssrcs.size(); ++i) { |
| 543 | stats_proxy_->OnInactiveSsrc(config_->rtp.ssrcs[i]); |
| 544 | } |
| 545 | |
| 546 | const size_t num_temporal_layers = |
| 547 | streams.back().num_temporal_layers.value_or(1); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 548 | |
| 549 | rtp_video_sender_->SetEncodingData(streams[0].width, streams[0].height, |
| 550 | num_temporal_layers); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 551 | |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 552 | if (rtp_video_sender_->IsActive()) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 553 | // The send stream is started already. Update the allocator with new bitrate |
| 554 | // limits. |
Sebastian Jansson | 464a557 | 2019-02-12 13:32:32 +0100 | [diff] [blame] | 555 | bitrate_allocator_->AddObserver(this, GetAllocationConfig()); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 556 | } |
| 557 | } |
| 558 | |
| 559 | EncodedImageCallback::Result VideoSendStreamImpl::OnEncodedImage( |
| 560 | const EncodedImage& encoded_image, |
Danil Chapovalov | 2549f17 | 2020-08-12 17:30:36 +0200 | [diff] [blame] | 561 | const CodecSpecificInfo* codec_specific_info) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 562 | // Encoded is called on whatever thread the real encoder implementation run |
| 563 | // on. In the case of hardware encoders, there might be several encoders |
| 564 | // running in parallel on different threads. |
Sebastian Jansson | ecb6897 | 2019-01-18 10:30:54 +0100 | [diff] [blame] | 565 | |
| 566 | // Indicate that there still is activity going on. |
| 567 | activity_ = true; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 568 | |
Ilya Nikolaevskiy | aa9aa57 | 2019-04-11 09:20:24 +0200 | [diff] [blame] | 569 | auto enable_padding_task = [this]() { |
| 570 | if (disable_padding_) { |
| 571 | RTC_DCHECK_RUN_ON(worker_queue_); |
| 572 | disable_padding_ = false; |
| 573 | // To ensure that padding bitrate is propagated to the bitrate allocator. |
| 574 | SignalEncoderActive(); |
| 575 | } |
| 576 | }; |
| 577 | if (!worker_queue_->IsCurrent()) { |
| 578 | worker_queue_->PostTask(enable_padding_task); |
| 579 | } else { |
| 580 | enable_padding_task(); |
| 581 | } |
| 582 | |
Niels Möller | 4687915 | 2019-01-07 15:54:47 +0100 | [diff] [blame] | 583 | EncodedImageCallback::Result result(EncodedImageCallback::Result::OK); |
Danil Chapovalov | 2549f17 | 2020-08-12 17:30:36 +0200 | [diff] [blame] | 584 | result = |
| 585 | rtp_video_sender_->OnEncodedImage(encoded_image, codec_specific_info); |
Erik Språng | 4e193e4 | 2018-09-14 19:01:58 +0200 | [diff] [blame] | 586 | // Check if there's a throttled VideoBitrateAllocation that we should try |
| 587 | // sending. |
| 588 | rtc::WeakPtr<VideoSendStreamImpl> send_stream = weak_ptr_; |
| 589 | auto update_task = [send_stream]() { |
| 590 | if (send_stream) { |
| 591 | RTC_DCHECK_RUN_ON(send_stream->worker_queue_); |
| 592 | auto& context = send_stream->video_bitrate_allocation_context_; |
| 593 | if (context && context->throttled_allocation) { |
| 594 | send_stream->OnBitrateAllocationUpdated(*context->throttled_allocation); |
| 595 | } |
| 596 | } |
| 597 | }; |
| 598 | if (!worker_queue_->IsCurrent()) { |
| 599 | worker_queue_->PostTask(update_task); |
| 600 | } else { |
| 601 | update_task(); |
| 602 | } |
| 603 | |
| 604 | return result; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 605 | } |
| 606 | |
Jakob Ivarsson | 159b417 | 2019-10-30 14:02:14 +0100 | [diff] [blame] | 607 | void VideoSendStreamImpl::OnDroppedFrame( |
| 608 | EncodedImageCallback::DropReason reason) { |
| 609 | activity_ = true; |
| 610 | } |
| 611 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 612 | std::map<uint32_t, RtpState> VideoSendStreamImpl::GetRtpStates() const { |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 613 | return rtp_video_sender_->GetRtpStates(); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 614 | } |
| 615 | |
| 616 | std::map<uint32_t, RtpPayloadState> VideoSendStreamImpl::GetRtpPayloadStates() |
| 617 | const { |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 618 | return rtp_video_sender_->GetRtpPayloadStates(); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 619 | } |
| 620 | |
Sebastian Jansson | c0e4d45 | 2018-10-25 15:08:32 +0200 | [diff] [blame] | 621 | uint32_t VideoSendStreamImpl::OnBitrateUpdated(BitrateAllocationUpdate update) { |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 622 | RTC_DCHECK_RUN_ON(worker_queue_); |
Stefan Holmer | 9416ef8 | 2018-07-19 10:34:38 +0200 | [diff] [blame] | 623 | RTC_DCHECK(rtp_video_sender_->IsActive()) |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 624 | << "VideoSendStream::Start has not been called."; |
| 625 | |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 626 | // When the BWE algorithm doesn't pass a stable estimate, we'll use the |
| 627 | // unstable one instead. |
| 628 | if (update.stable_target_bitrate.IsZero()) { |
| 629 | update.stable_target_bitrate = update.target_bitrate; |
| 630 | } |
| 631 | |
Sebastian Jansson | 82ed2e8 | 2019-10-15 15:58:37 +0200 | [diff] [blame] | 632 | rtp_video_sender_->OnBitrateUpdated(update, stats_proxy_->GetSendFrameRate()); |
Stefan Holmer | 64be7fa | 2018-10-04 15:21:55 +0200 | [diff] [blame] | 633 | encoder_target_rate_bps_ = rtp_video_sender_->GetPayloadBitrateBps(); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 634 | const uint32_t protection_bitrate_bps = |
| 635 | rtp_video_sender_->GetProtectionBitrateBps(); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 636 | DataRate link_allocation = DataRate::Zero(); |
| 637 | if (encoder_target_rate_bps_ > protection_bitrate_bps) { |
| 638 | link_allocation = |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 639 | DataRate::BitsPerSec(encoder_target_rate_bps_ - protection_bitrate_bps); |
Erik Språng | 610c763 | 2019-03-06 15:37:33 +0100 | [diff] [blame] | 640 | } |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 641 | DataRate overhead = |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 642 | update.target_bitrate - DataRate::BitsPerSec(encoder_target_rate_bps_); |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 643 | DataRate encoder_stable_target_rate = update.stable_target_bitrate; |
| 644 | if (encoder_stable_target_rate > overhead) { |
| 645 | encoder_stable_target_rate = encoder_stable_target_rate - overhead; |
| 646 | } else { |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 647 | encoder_stable_target_rate = DataRate::BitsPerSec(encoder_target_rate_bps_); |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 648 | } |
| 649 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 650 | encoder_target_rate_bps_ = |
| 651 | std::min(encoder_max_bitrate_bps_, encoder_target_rate_bps_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 652 | |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 653 | encoder_stable_target_rate = |
| 654 | std::min(DataRate::BitsPerSec(encoder_max_bitrate_bps_), |
| 655 | encoder_stable_target_rate); |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 656 | |
Danil Chapovalov | cad3e0e | 2020-02-17 18:46:07 +0100 | [diff] [blame] | 657 | DataRate encoder_target_rate = DataRate::BitsPerSec(encoder_target_rate_bps_); |
Erik Språng | 4c6ca30 | 2019-04-08 15:14:01 +0200 | [diff] [blame] | 658 | link_allocation = std::max(encoder_target_rate, link_allocation); |
Sebastian Jansson | 13e5903 | 2018-11-21 19:13:07 +0100 | [diff] [blame] | 659 | video_stream_encoder_->OnBitrateUpdated( |
Florent Castelli | a8336d3 | 2019-09-09 13:36:55 +0200 | [diff] [blame] | 660 | encoder_target_rate, encoder_stable_target_rate, link_allocation, |
Sebastian Jansson | 13e5903 | 2018-11-21 19:13:07 +0100 | [diff] [blame] | 661 | rtc::dchecked_cast<uint8_t>(update.packet_loss_ratio * 256), |
Ying Wang | 9b881ab | 2020-02-07 14:29:32 +0100 | [diff] [blame] | 662 | update.round_trip_time.ms(), update.cwnd_reduce_ratio); |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 663 | stats_proxy_->OnSetEncoderTargetRate(encoder_target_rate_bps_); |
Erik Språng | 2611164 | 2019-03-26 11:09:04 +0100 | [diff] [blame] | 664 | return protection_bitrate_bps; |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 665 | } |
| 666 | |
Sebastian Jansson | 8e0b15b | 2018-04-18 19:19:22 +0200 | [diff] [blame] | 667 | } // namespace internal |
| 668 | } // namespace webrtc |