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