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