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