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