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