| 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 |
| 11 | #include "media/engine/webrtc_video_engine.h" |
| 12 |
| 13 | #include <stdio.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <set> |
| 17 | #include <string> |
| 18 | #include <utility> |
| 19 |
| 20 | #include "absl/algorithm/container.h" |
| 21 | #include "absl/strings/match.h" |
| 22 | #include "api/media_stream_interface.h" |
| 23 | #include "api/units/data_rate.h" |
| 24 | #include "api/video/video_codec_constants.h" |
| 25 | #include "api/video/video_codec_type.h" |
| 26 | #include "api/video_codecs/sdp_video_format.h" |
| 27 | #include "api/video_codecs/video_decoder_factory.h" |
| 28 | #include "api/video_codecs/video_encoder.h" |
| 29 | #include "api/video_codecs/video_encoder_factory.h" |
| 30 | #include "call/call.h" |
| 31 | #include "media/engine/simulcast.h" |
| 32 | #include "media/engine/webrtc_media_engine.h" |
| 33 | #include "media/engine/webrtc_voice_engine.h" |
| 34 | #include "rtc_base/copy_on_write_buffer.h" |
| 35 | #include "rtc_base/experiments/field_trial_parser.h" |
| 36 | #include "rtc_base/experiments/field_trial_units.h" |
| 37 | #include "rtc_base/experiments/min_video_bitrate_experiment.h" |
| 38 | #include "rtc_base/logging.h" |
| 39 | #include "rtc_base/numerics/safe_conversions.h" |
| 40 | #include "rtc_base/strings/string_builder.h" |
| 41 | #include "rtc_base/time_utils.h" |
| 42 | #include "rtc_base/trace_event.h" |
| 43 | #include "system_wrappers/include/field_trial.h" |
| 44 |
| 45 | namespace cricket { |
| 46 |
| 47 | namespace { |
| 48 |
| 49 | const int kMinLayerSize = 16; |
| 50 |
| 51 | const char* StreamTypeToString( |
| 52 | webrtc::VideoSendStream::StreamStats::StreamType type) { |
| 53 | switch (type) { |
| 54 | case webrtc::VideoSendStream::StreamStats::StreamType::kMedia: |
| 55 | return "kMedia"; |
| 56 | case webrtc::VideoSendStream::StreamStats::StreamType::kRtx: |
| 57 | return "kRtx"; |
| 58 | case webrtc::VideoSendStream::StreamStats::StreamType::kFlexfec: |
| 59 | return "kFlexfec"; |
| 60 | } |
| 61 | return nullptr; |
| 62 | } |
| 63 |
| 64 | // If this field trial is enabled, we will enable sending FlexFEC and disable |
| 65 | // sending ULPFEC whenever the former has been negotiated in the SDPs. |
| 66 | bool IsFlexfecFieldTrialEnabled() { |
| 67 | return webrtc::field_trial::IsEnabled("WebRTC-FlexFEC-03"); |
| 68 | } |
| 69 |
| 70 | // If this field trial is enabled, the "flexfec-03" codec will be advertised |
| 71 | // as being supported. This means that "flexfec-03" will appear in the default |
| 72 | // SDP offer, and we therefore need to be ready to receive FlexFEC packets from |
| 73 | // the remote. It also means that FlexFEC SSRCs will be generated by |
| 74 | // MediaSession and added as "a=ssrc:" and "a=ssrc-group:" lines in the local |
| 75 | // SDP. |
| 76 | bool IsFlexfecAdvertisedFieldTrialEnabled() { |
| 77 | return webrtc::field_trial::IsEnabled("WebRTC-FlexFEC-03-Advertised"); |
| 78 | } |
| 79 |
| 80 | void AddDefaultFeedbackParams(VideoCodec* codec) { |
| 81 | // Don't add any feedback params for RED and ULPFEC. |
| 82 | if (codec->name == kRedCodecName || codec->name == kUlpfecCodecName) |
| 83 | return; |
| 84 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamRemb, kParamValueEmpty)); |
| 85 | codec->AddFeedbackParam( |
| 86 | FeedbackParam(kRtcpFbParamTransportCc, kParamValueEmpty)); |
| 87 | // Don't add any more feedback params for FLEXFEC. |
| 88 | if (codec->name == kFlexfecCodecName) |
| 89 | return; |
| 90 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamCcm, kRtcpFbCcmParamFir)); |
| 91 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kParamValueEmpty)); |
| 92 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamNack, kRtcpFbNackParamPli)); |
| 93 | if (codec->name == kVp8CodecName && |
| 94 | webrtc::field_trial::IsEnabled("WebRTC-RtcpLossNotification")) { |
| 95 | codec->AddFeedbackParam(FeedbackParam(kRtcpFbParamLntf, kParamValueEmpty)); |
| 96 | } |
| 97 | } |
| 98 |
| 99 | // This function will assign dynamic payload types (in the range [96, 127]) to |
| 100 | // the input codecs, and also add ULPFEC, RED, FlexFEC, and associated RTX |
| 101 | // codecs for recognized codecs (VP8, VP9, H264, and RED). It will also add |
| 102 | // default feedback params to the codecs. |
| 103 | std::vector<VideoCodec> AssignPayloadTypesAndDefaultCodecs( |
| 104 | std::vector<webrtc::SdpVideoFormat> input_formats) { |
| 105 | if (input_formats.empty()) |
| 106 | return std::vector<VideoCodec>(); |
| 107 | static const int kFirstDynamicPayloadType = 96; |
| 108 | static const int kLastDynamicPayloadType = 127; |
| 109 | int payload_type = kFirstDynamicPayloadType; |
| 110 |
| 111 | input_formats.push_back(webrtc::SdpVideoFormat(kRedCodecName)); |
| 112 | input_formats.push_back(webrtc::SdpVideoFormat(kUlpfecCodecName)); |
| 113 |
| 114 | if (IsFlexfecAdvertisedFieldTrialEnabled()) { |
| 115 | webrtc::SdpVideoFormat flexfec_format(kFlexfecCodecName); |
| 116 | // This value is currently arbitrarily set to 10 seconds. (The unit |
| 117 | // is microseconds.) This parameter MUST be present in the SDP, but |
| 118 | // we never use the actual value anywhere in our code however. |
| 119 | // TODO(brandtr): Consider honouring this value in the sender and receiver. |
| 120 | flexfec_format.parameters = {{kFlexfecFmtpRepairWindow, "10000000"}}; |
| 121 | input_formats.push_back(flexfec_format); |
| 122 | } |
| 123 |
| 124 | std::vector<VideoCodec> output_codecs; |
| 125 | for (const webrtc::SdpVideoFormat& format : input_formats) { |
| 126 | VideoCodec codec(format); |
| 127 | codec.id = payload_type; |
| 128 | AddDefaultFeedbackParams(&codec); |
| 129 | output_codecs.push_back(codec); |
| 130 |
| 131 | // Increment payload type. |
| 132 | ++payload_type; |
| 133 | if (payload_type > kLastDynamicPayloadType) { |
| 134 | RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest."; |
| 135 | break; |
| 136 | } |
| 137 |
| 138 | // Add associated RTX codec for non-FEC codecs. |
| 139 | if (!absl::EqualsIgnoreCase(codec.name, kUlpfecCodecName) && |
| 140 | !absl::EqualsIgnoreCase(codec.name, kFlexfecCodecName)) { |
| 141 | output_codecs.push_back( |
| 142 | VideoCodec::CreateRtxCodec(payload_type, codec.id)); |
| 143 |
| 144 | // Increment payload type. |
| 145 | ++payload_type; |
| 146 | if (payload_type > kLastDynamicPayloadType) { |
| 147 | RTC_LOG(LS_ERROR) << "Out of dynamic payload types, skipping the rest."; |
| 148 | break; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | return output_codecs; |
| 153 | } |
| 154 |
| 155 | // is_decoder_factory is needed to keep track of the implict assumption that any |
| 156 | // H264 decoder also supports constrained base line profile. |
| 157 | // TODO(kron): Perhaps it better to move the implcit knowledge to the place |
| 158 | // where codecs are negotiated. |
| 159 | template <class T> |
| 160 | std::vector<VideoCodec> GetPayloadTypesAndDefaultCodecs( |
| 161 | const T* factory, |
| 162 | bool is_decoder_factory) { |
| 163 | if (!factory) { |
| 164 | return {}; |
| 165 | } |
| 166 |
| 167 | std::vector<webrtc::SdpVideoFormat> supported_formats = |
| 168 | factory->GetSupportedFormats(); |
| 169 | if (is_decoder_factory) { |
| 170 | AddH264ConstrainedBaselineProfileToSupportedFormats(&supported_formats); |
| 171 | } |
| 172 |
| 173 | return AssignPayloadTypesAndDefaultCodecs(std::move(supported_formats)); |
| 174 | } |
| 175 |
| 176 | bool IsTemporalLayersSupported(const std::string& codec_name) { |
| 177 | return absl::EqualsIgnoreCase(codec_name, kVp8CodecName) || |
| 178 | absl::EqualsIgnoreCase(codec_name, kVp9CodecName); |
| 179 | } |
| 180 |
| 181 | static std::string CodecVectorToString(const std::vector<VideoCodec>& codecs) { |
| 182 | rtc::StringBuilder out; |
| 183 | out << "{"; |
| 184 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 185 | out << codecs[i].ToString(); |
| 186 | if (i != codecs.size() - 1) { |
| 187 | out << ", "; |
| 188 | } |
| 189 | } |
| 190 | out << "}"; |
| 191 | return out.Release(); |
| 192 | } |
| 193 |
| 194 | static bool ValidateCodecFormats(const std::vector<VideoCodec>& codecs) { |
| 195 | bool has_video = false; |
| 196 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 197 | if (!codecs[i].ValidateCodecFormat()) { |
| 198 | return false; |
| 199 | } |
| 200 | if (codecs[i].GetCodecType() == VideoCodec::CODEC_VIDEO) { |
| 201 | has_video = true; |
| 202 | } |
| 203 | } |
| 204 | if (!has_video) { |
| 205 | RTC_LOG(LS_ERROR) << "Setting codecs without a video codec is invalid: " |
| 206 | << CodecVectorToString(codecs); |
| 207 | return false; |
| 208 | } |
| 209 | return true; |
| 210 | } |
| 211 |
| 212 | static bool ValidateStreamParams(const StreamParams& sp) { |
| 213 | if (sp.ssrcs.empty()) { |
| 214 | RTC_LOG(LS_ERROR) << "No SSRCs in stream parameters: " << sp.ToString(); |
| 215 | return false; |
| 216 | } |
| 217 |
| 218 | std::vector<uint32_t> primary_ssrcs; |
| 219 | sp.GetPrimarySsrcs(&primary_ssrcs); |
| 220 | std::vector<uint32_t> rtx_ssrcs; |
| 221 | sp.GetFidSsrcs(primary_ssrcs, &rtx_ssrcs); |
| 222 | for (uint32_t rtx_ssrc : rtx_ssrcs) { |
| 223 | bool rtx_ssrc_present = false; |
| 224 | for (uint32_t sp_ssrc : sp.ssrcs) { |
| 225 | if (sp_ssrc == rtx_ssrc) { |
| 226 | rtx_ssrc_present = true; |
| 227 | break; |
| 228 | } |
| 229 | } |
| 230 | if (!rtx_ssrc_present) { |
| 231 | RTC_LOG(LS_ERROR) << "RTX SSRC '" << rtx_ssrc |
| 232 | << "' missing from StreamParams ssrcs: " |
| 233 | << sp.ToString(); |
| 234 | return false; |
| 235 | } |
| 236 | } |
| 237 | if (!rtx_ssrcs.empty() && primary_ssrcs.size() != rtx_ssrcs.size()) { |
| 238 | RTC_LOG(LS_ERROR) |
| 239 | << "RTX SSRCs exist, but don't cover all SSRCs (unsupported): " |
| 240 | << sp.ToString(); |
| 241 | return false; |
| 242 | } |
| 243 |
| 244 | return true; |
| 245 | } |
| 246 |
| 247 | // Returns true if the given codec is disallowed from doing simulcast. |
| 248 | bool IsCodecDisabledForSimulcast(const std::string& codec_name) { |
| 249 | return !webrtc::field_trial::IsDisabled("WebRTC-H264Simulcast") |
| 250 | ? absl::EqualsIgnoreCase(codec_name, kVp9CodecName) |
| 251 | : absl::EqualsIgnoreCase(codec_name, kH264CodecName) || |
| 252 | absl::EqualsIgnoreCase(codec_name, kH265CodecName) || |
| 253 | absl::EqualsIgnoreCase(codec_name, kVp9CodecName); |
| 254 | } |
| 255 |
| 256 | // The selected thresholds for QVGA and VGA corresponded to a QP around 10. |
| 257 | // The change in QP declined above the selected bitrates. |
| 258 | static int GetMaxDefaultVideoBitrateKbps(int width, |
| 259 | int height, |
| 260 | bool is_screenshare) { |
| 261 | int max_bitrate; |
| 262 | if (width * height <= 320 * 240) { |
| 263 | max_bitrate = 600; |
| 264 | } else if (width * height <= 640 * 480) { |
| 265 | max_bitrate = 1700; |
| 266 | } else if (width * height <= 960 * 540) { |
| 267 | max_bitrate = 2000; |
| 268 | } else { |
| 269 | max_bitrate = 2500; |
| 270 | } |
| 271 | if (is_screenshare) |
| 272 | max_bitrate = std::max(max_bitrate, 1200); |
| 273 | return max_bitrate; |
| 274 | } |
| 275 |
| 276 | bool GetVp9LayersFromFieldTrialGroup(size_t* num_spatial_layers, |
| 277 | size_t* num_temporal_layers) { |
| 278 | std::string group = webrtc::field_trial::FindFullName("WebRTC-SupportVP9SVC"); |
| 279 | if (group.empty()) |
| 280 | return false; |
| 281 |
| 282 | if (sscanf(group.c_str(), "EnabledByFlag_%zuSL%zuTL", num_spatial_layers, |
| 283 | num_temporal_layers) != 2) { |
| 284 | return false; |
| 285 | } |
| 286 | if (*num_spatial_layers > webrtc::kMaxSpatialLayers || |
| 287 | *num_spatial_layers < 1) |
| 288 | return false; |
| 289 |
| 290 | const size_t kMaxTemporalLayers = 3; |
| 291 | if (*num_temporal_layers > kMaxTemporalLayers || *num_temporal_layers < 1) |
| 292 | return false; |
| 293 |
| 294 | return true; |
| 295 | } |
| 296 |
| 297 | absl::optional<size_t> GetVp9SpatialLayersFromFieldTrial() { |
| 298 | size_t num_sl; |
| 299 | size_t num_tl; |
| 300 | if (GetVp9LayersFromFieldTrialGroup(&num_sl, &num_tl)) { |
| 301 | return num_sl; |
| 302 | } |
| 303 | return absl::nullopt; |
| 304 | } |
| 305 |
| 306 | absl::optional<size_t> GetVp9TemporalLayersFromFieldTrial() { |
| 307 | size_t num_sl; |
| 308 | size_t num_tl; |
| 309 | if (GetVp9LayersFromFieldTrialGroup(&num_sl, &num_tl)) { |
| 310 | return num_tl; |
| 311 | } |
| 312 | return absl::nullopt; |
| 313 | } |
| 314 |
| 315 | // Returns its smallest positive argument. If neither argument is positive, |
| 316 | // returns an arbitrary nonpositive value. |
| 317 | int MinPositive(int a, int b) { |
| 318 | if (a <= 0) { |
| 319 | return b; |
| 320 | } |
| 321 | if (b <= 0) { |
| 322 | return a; |
| 323 | } |
| 324 | return std::min(a, b); |
| 325 | } |
| 326 |
| 327 | bool IsLayerActive(const webrtc::RtpEncodingParameters& layer) { |
| 328 | return layer.active && |
| 329 | (!layer.max_bitrate_bps || *layer.max_bitrate_bps > 0) && |
| 330 | (!layer.max_framerate || *layer.max_framerate > 0); |
| 331 | } |
| 332 |
| 333 | size_t FindRequiredActiveLayers( |
| 334 | const webrtc::VideoEncoderConfig& encoder_config) { |
| 335 | // Need enough layers so that at least the first active one is present. |
| 336 | for (size_t i = 0; i < encoder_config.number_of_streams; ++i) { |
| 337 | if (encoder_config.simulcast_layers[i].active) { |
| 338 | return i + 1; |
| 339 | } |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 |
| 344 | int NumActiveStreams(const webrtc::RtpParameters& rtp_parameters) { |
| 345 | int res = 0; |
| 346 | for (size_t i = 0; i < rtp_parameters.encodings.size(); ++i) { |
| 347 | if (rtp_parameters.encodings[i].active) { |
| 348 | ++res; |
| 349 | } |
| 350 | } |
| 351 | return res; |
| 352 | } |
| 353 |
| 354 | std::map<uint32_t, webrtc::VideoSendStream::StreamStats> |
| 355 | MergeInfoAboutOutboundRtpSubstreams( |
| 356 | const std::map<uint32_t, webrtc::VideoSendStream::StreamStats>& |
| 357 | substreams) { |
| 358 | std::map<uint32_t, webrtc::VideoSendStream::StreamStats> rtp_substreams; |
| 359 | // Add substreams for all RTP media streams. |
| 360 | for (const auto& pair : substreams) { |
| 361 | uint32_t ssrc = pair.first; |
| 362 | const webrtc::VideoSendStream::StreamStats& substream = pair.second; |
| 363 | switch (substream.type) { |
| 364 | case webrtc::VideoSendStream::StreamStats::StreamType::kMedia: |
| 365 | break; |
| 366 | case webrtc::VideoSendStream::StreamStats::StreamType::kRtx: |
| 367 | case webrtc::VideoSendStream::StreamStats::StreamType::kFlexfec: |
| 368 | continue; |
| 369 | } |
| 370 | rtp_substreams.insert(std::make_pair(ssrc, substream)); |
| 371 | } |
| 372 | // Complement the kMedia substream stats with the associated kRtx and kFlexfec |
| 373 | // substream stats. |
| 374 | for (const auto& pair : substreams) { |
| 375 | switch (pair.second.type) { |
| 376 | case webrtc::VideoSendStream::StreamStats::StreamType::kMedia: |
| 377 | continue; |
| 378 | case webrtc::VideoSendStream::StreamStats::StreamType::kRtx: |
| 379 | case webrtc::VideoSendStream::StreamStats::StreamType::kFlexfec: |
| 380 | break; |
| 381 | } |
| 382 | // The associated substream is an RTX or FlexFEC substream that is |
| 383 | // referencing an RTP media substream. |
| 384 | const webrtc::VideoSendStream::StreamStats& associated_substream = |
| 385 | pair.second; |
| 386 | RTC_DCHECK(associated_substream.referenced_media_ssrc.has_value()); |
| 387 | uint32_t media_ssrc = associated_substream.referenced_media_ssrc.value(); |
| 388 | if (substreams.find(media_ssrc) == substreams.end()) { |
| 389 | RTC_LOG(LS_WARNING) << "Substream [ssrc: " << pair.first << ", type: " |
| 390 | << StreamTypeToString(associated_substream.type) |
| 391 | << "] is associated with a media ssrc (" << media_ssrc |
| 392 | << ") that does not have StreamStats. Ignoring its " |
| 393 | << "RTP stats."; |
| 394 | continue; |
| 395 | } |
| 396 | webrtc::VideoSendStream::StreamStats& rtp_substream = |
| 397 | rtp_substreams[media_ssrc]; |
| 398 |
| 399 | // We only merge |rtp_stats|. All other metrics are not applicable for RTX |
| 400 | // and FlexFEC. |
| 401 | // TODO(hbos): kRtx and kFlexfec stats should use a separate struct to make |
| 402 | // it clear what is or is not applicable. |
| 403 | rtp_substream.rtp_stats.Add(associated_substream.rtp_stats); |
| 404 | } |
| 405 | return rtp_substreams; |
| 406 | } |
| 407 |
| 408 | } // namespace |
| 409 |
| 410 | // This constant is really an on/off, lower-level configurable NACK history |
| 411 | // duration hasn't been implemented. |
| 412 | static const int kNackHistoryMs = 1000; |
| 413 |
| 414 | static const int kDefaultRtcpReceiverReportSsrc = 1; |
| 415 |
| 416 | // Minimum time interval for logging stats. |
| 417 | static const int64_t kStatsLogIntervalMs = 10000; |
| 418 |
| 419 | std::map<uint32_t, webrtc::VideoSendStream::StreamStats> |
| 420 | MergeInfoAboutOutboundRtpSubstreamsForTesting( |
| 421 | const std::map<uint32_t, webrtc::VideoSendStream::StreamStats>& |
| 422 | substreams) { |
| 423 | return MergeInfoAboutOutboundRtpSubstreams(substreams); |
| 424 | } |
| 425 |
| 426 | rtc::scoped_refptr<webrtc::VideoEncoderConfig::EncoderSpecificSettings> |
| 427 | WebRtcVideoChannel::WebRtcVideoSendStream::ConfigureVideoEncoderSettings( |
| 428 | const VideoCodec& codec) { |
| 429 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 430 | bool is_screencast = parameters_.options.is_screencast.value_or(false); |
| 431 | // No automatic resizing when using simulcast or screencast, or when |
| 432 | // disabled by field trial flag. |
| 433 | bool automatic_resize = !disable_automatic_resize_ && !is_screencast && |
| 434 | (parameters_.config.rtp.ssrcs.size() == 1 || |
| 435 | NumActiveStreams(rtp_parameters_) == 1); |
| 436 |
| 437 | bool frame_dropping = !is_screencast; |
| 438 | bool denoising; |
| 439 | bool codec_default_denoising = false; |
| 440 | if (is_screencast) { |
| 441 | denoising = false; |
| 442 | } else { |
| 443 | // Use codec default if video_noise_reduction is unset. |
| 444 | codec_default_denoising = !parameters_.options.video_noise_reduction; |
| 445 | denoising = parameters_.options.video_noise_reduction.value_or(false); |
| 446 | } |
| 447 |
| 448 | if (absl::EqualsIgnoreCase(codec.name, kH264CodecName)) { |
| 449 | webrtc::VideoCodecH264 h264_settings = |
| 450 | webrtc::VideoEncoder::GetDefaultH264Settings(); |
| 451 | h264_settings.frameDroppingOn = frame_dropping; |
| 452 | return new rtc::RefCountedObject< |
| 453 | webrtc::VideoEncoderConfig::H264EncoderSpecificSettings>(h264_settings); |
| 454 | } |
| 455 | if (absl::EqualsIgnoreCase(codec.name, kVp8CodecName)) { |
| 456 | webrtc::VideoCodecVP8 vp8_settings = |
| 457 | webrtc::VideoEncoder::GetDefaultVp8Settings(); |
| 458 | vp8_settings.automaticResizeOn = automatic_resize; |
| 459 | // VP8 denoising is enabled by default. |
| 460 | vp8_settings.denoisingOn = codec_default_denoising ? true : denoising; |
| 461 | vp8_settings.frameDroppingOn = frame_dropping; |
| 462 | return new rtc::RefCountedObject< |
| 463 | webrtc::VideoEncoderConfig::Vp8EncoderSpecificSettings>(vp8_settings); |
| 464 | } |
| 465 | if (absl::EqualsIgnoreCase(codec.name, kVp9CodecName)) { |
| 466 | webrtc::VideoCodecVP9 vp9_settings = |
| 467 | webrtc::VideoEncoder::GetDefaultVp9Settings(); |
| 468 | const size_t default_num_spatial_layers = |
| 469 | parameters_.config.rtp.ssrcs.size(); |
| 470 | const size_t num_spatial_layers = |
| 471 | GetVp9SpatialLayersFromFieldTrial().value_or( |
| 472 | default_num_spatial_layers); |
| 473 |
| 474 | const size_t default_num_temporal_layers = |
| 475 | num_spatial_layers > 1 ? kConferenceDefaultNumTemporalLayers : 1; |
| 476 | const size_t num_temporal_layers = |
| 477 | GetVp9TemporalLayersFromFieldTrial().value_or( |
| 478 | default_num_temporal_layers); |
| 479 |
| 480 | vp9_settings.numberOfSpatialLayers = std::min<unsigned char>( |
| 481 | num_spatial_layers, kConferenceMaxNumSpatialLayers); |
| 482 | vp9_settings.numberOfTemporalLayers = std::min<unsigned char>( |
| 483 | num_temporal_layers, kConferenceMaxNumTemporalLayers); |
| 484 |
| 485 | // VP9 denoising is disabled by default. |
| 486 | vp9_settings.denoisingOn = codec_default_denoising ? true : denoising; |
| 487 | vp9_settings.automaticResizeOn = automatic_resize; |
| 488 | // Ensure frame dropping is always enabled. |
| 489 | RTC_DCHECK(vp9_settings.frameDroppingOn); |
| 490 | if (!is_screencast) { |
| 491 | webrtc::FieldTrialFlag interlayer_pred_experiment_enabled = |
| 492 | webrtc::FieldTrialFlag("Enabled"); |
| 493 | webrtc::FieldTrialEnum<webrtc::InterLayerPredMode> inter_layer_pred_mode( |
| 494 | "inter_layer_pred_mode", webrtc::InterLayerPredMode::kOnKeyPic, |
| 495 | {{"off", webrtc::InterLayerPredMode::kOff}, |
| 496 | {"on", webrtc::InterLayerPredMode::kOn}, |
| 497 | {"onkeypic", webrtc::InterLayerPredMode::kOnKeyPic}}); |
| 498 | webrtc::ParseFieldTrial( |
| 499 | {&interlayer_pred_experiment_enabled, &inter_layer_pred_mode}, |
| 500 | webrtc::field_trial::FindFullName("WebRTC-Vp9InterLayerPred")); |
| 501 | if (interlayer_pred_experiment_enabled) { |
| 502 | vp9_settings.interLayerPred = inter_layer_pred_mode; |
| 503 | } else { |
| 504 | // Limit inter-layer prediction to key pictures by default. |
| 505 | vp9_settings.interLayerPred = webrtc::InterLayerPredMode::kOnKeyPic; |
| 506 | } |
| 507 | } else { |
| 508 | // Multiple spatial layers vp9 screenshare needs flexible mode. |
| 509 | vp9_settings.flexibleMode = vp9_settings.numberOfSpatialLayers > 1; |
| 510 | vp9_settings.interLayerPred = webrtc::InterLayerPredMode::kOn; |
| 511 | } |
| 512 | return new rtc::RefCountedObject< |
| 513 | webrtc::VideoEncoderConfig::Vp9EncoderSpecificSettings>(vp9_settings); |
| 514 | } |
| 515 | return nullptr; |
| 516 | } |
| 517 |
| 518 | DefaultUnsignalledSsrcHandler::DefaultUnsignalledSsrcHandler() |
| 519 | : default_sink_(nullptr) {} |
| 520 |
| 521 | UnsignalledSsrcHandler::Action DefaultUnsignalledSsrcHandler::OnUnsignalledSsrc( |
| 522 | WebRtcVideoChannel* channel, |
| 523 | uint32_t ssrc) { |
| 524 | absl::optional<uint32_t> default_recv_ssrc = |
| 525 | channel->GetDefaultReceiveStreamSsrc(); |
| 526 |
| 527 | if (default_recv_ssrc) { |
| 528 | RTC_LOG(LS_INFO) << "Destroying old default receive stream for SSRC=" |
| 529 | << ssrc << "."; |
| 530 | channel->RemoveRecvStream(*default_recv_ssrc); |
| 531 | } |
| 532 |
| 533 | StreamParams sp = channel->unsignaled_stream_params(); |
| 534 | sp.ssrcs.push_back(ssrc); |
| 535 |
| 536 | RTC_LOG(LS_INFO) << "Creating default receive stream for SSRC=" << ssrc |
| 537 | << "."; |
| 538 | if (!channel->AddRecvStream(sp, /*default_stream=*/true)) { |
| 539 | RTC_LOG(LS_WARNING) << "Could not create default receive stream."; |
| 540 | } |
| 541 |
| 542 | // SSRC 0 returns default_recv_base_minimum_delay_ms. |
| 543 | const int unsignaled_ssrc = 0; |
| 544 | int default_recv_base_minimum_delay_ms = |
| 545 | channel->GetBaseMinimumPlayoutDelayMs(unsignaled_ssrc).value_or(0); |
| 546 | // Set base minimum delay if it was set before for the default receive stream. |
| 547 | channel->SetBaseMinimumPlayoutDelayMs(ssrc, |
| 548 | default_recv_base_minimum_delay_ms); |
| 549 | channel->SetSink(ssrc, default_sink_); |
| 550 | return kDeliverPacket; |
| 551 | } |
| 552 |
| 553 | rtc::VideoSinkInterface<webrtc::VideoFrame>* |
| 554 | DefaultUnsignalledSsrcHandler::GetDefaultSink() const { |
| 555 | return default_sink_; |
| 556 | } |
| 557 |
| 558 | void DefaultUnsignalledSsrcHandler::SetDefaultSink( |
| 559 | WebRtcVideoChannel* channel, |
| 560 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { |
| 561 | default_sink_ = sink; |
| 562 | absl::optional<uint32_t> default_recv_ssrc = |
| 563 | channel->GetDefaultReceiveStreamSsrc(); |
| 564 | if (default_recv_ssrc) { |
| 565 | channel->SetSink(*default_recv_ssrc, default_sink_); |
| 566 | } |
| 567 | } |
| 568 |
| 569 | WebRtcVideoEngine::WebRtcVideoEngine( |
| 570 | std::unique_ptr<webrtc::VideoEncoderFactory> video_encoder_factory, |
| 571 | std::unique_ptr<webrtc::VideoDecoderFactory> video_decoder_factory) |
| 572 | : decoder_factory_(std::move(video_decoder_factory)), |
| 573 | encoder_factory_(std::move(video_encoder_factory)) { |
| 574 | RTC_LOG(LS_INFO) << "WebRtcVideoEngine::WebRtcVideoEngine()"; |
| 575 | } |
| 576 |
| 577 | WebRtcVideoEngine::~WebRtcVideoEngine() { |
| 578 | RTC_LOG(LS_INFO) << "WebRtcVideoEngine::~WebRtcVideoEngine"; |
| 579 | } |
| 580 |
| 581 | VideoMediaChannel* WebRtcVideoEngine::CreateMediaChannel( |
| 582 | webrtc::Call* call, |
| 583 | const MediaConfig& config, |
| 584 | const VideoOptions& options, |
| 585 | const webrtc::CryptoOptions& crypto_options, |
| 586 | webrtc::VideoBitrateAllocatorFactory* video_bitrate_allocator_factory) { |
| 587 | RTC_LOG(LS_INFO) << "CreateMediaChannel. Options: " << options.ToString(); |
| 588 | return new WebRtcVideoChannel(call, config, options, crypto_options, |
| 589 | encoder_factory_.get(), decoder_factory_.get(), |
| 590 | video_bitrate_allocator_factory); |
| 591 | } |
| 592 | std::vector<VideoCodec> WebRtcVideoEngine::send_codecs() const { |
| 593 | return GetPayloadTypesAndDefaultCodecs(encoder_factory_.get(), |
| 594 | /*is_decoder_factory=*/false); |
| 595 | } |
| 596 |
| 597 | std::vector<VideoCodec> WebRtcVideoEngine::recv_codecs() const { |
| 598 | return GetPayloadTypesAndDefaultCodecs(decoder_factory_.get(), |
| 599 | /*is_decoder_factory=*/true); |
| 600 | } |
| 601 |
| 602 | std::vector<webrtc::RtpHeaderExtensionCapability> |
| 603 | WebRtcVideoEngine::GetRtpHeaderExtensions() const { |
| 604 | std::vector<webrtc::RtpHeaderExtensionCapability> result; |
| 605 | int id = 1; |
| 606 | for (const auto& uri : |
| 607 | {webrtc::RtpExtension::kTimestampOffsetUri, |
| 608 | webrtc::RtpExtension::kAbsSendTimeUri, |
| 609 | webrtc::RtpExtension::kVideoRotationUri, |
| 610 | webrtc::RtpExtension::kTransportSequenceNumberUri, |
| 611 | webrtc::RtpExtension::kPlayoutDelayUri, |
| 612 | webrtc::RtpExtension::kVideoContentTypeUri, |
| 613 | webrtc::RtpExtension::kVideoTimingUri, |
| 614 | webrtc::RtpExtension::kColorSpaceUri, webrtc::RtpExtension::kMidUri, |
| 615 | webrtc::RtpExtension::kRidUri, webrtc::RtpExtension::kRepairedRidUri}) { |
| 616 | result.emplace_back(uri, id++, webrtc::RtpTransceiverDirection::kSendRecv); |
| 617 | } |
| 618 | result.emplace_back( |
| 619 | webrtc::RtpExtension::kGenericFrameDescriptorUri00, id, |
| 620 | webrtc::field_trial::IsEnabled("WebRTC-GenericDescriptorAdvertised") |
| 621 | ? webrtc::RtpTransceiverDirection::kSendRecv |
| 622 | : webrtc::RtpTransceiverDirection::kStopped); |
| 623 | return result; |
| 624 | } |
| 625 |
| 626 | WebRtcVideoChannel::WebRtcVideoChannel( |
| 627 | webrtc::Call* call, |
| 628 | const MediaConfig& config, |
| 629 | const VideoOptions& options, |
| 630 | const webrtc::CryptoOptions& crypto_options, |
| 631 | webrtc::VideoEncoderFactory* encoder_factory, |
| 632 | webrtc::VideoDecoderFactory* decoder_factory, |
| 633 | webrtc::VideoBitrateAllocatorFactory* bitrate_allocator_factory) |
| 634 | : VideoMediaChannel(config), |
| 635 | worker_thread_(rtc::Thread::Current()), |
| 636 | call_(call), |
| 637 | unsignalled_ssrc_handler_(&default_unsignalled_ssrc_handler_), |
| 638 | video_config_(config.video), |
| 639 | encoder_factory_(encoder_factory), |
| 640 | decoder_factory_(decoder_factory), |
| 641 | bitrate_allocator_factory_(bitrate_allocator_factory), |
| 642 | default_send_options_(options), |
| 643 | last_stats_log_ms_(-1), |
| 644 | discard_unknown_ssrc_packets_(webrtc::field_trial::IsEnabled( |
| 645 | "WebRTC-Video-DiscardPacketsWithUnknownSsrc")), |
| 646 | crypto_options_(crypto_options), |
| 647 | unknown_ssrc_packet_buffer_( |
| 648 | webrtc::field_trial::IsEnabled( |
| 649 | "WebRTC-Video-BufferPacketsWithUnknownSsrc") |
| 650 | ? new UnhandledPacketsBuffer() |
| 651 | : nullptr) { |
| 652 | RTC_DCHECK(thread_checker_.IsCurrent()); |
| 653 |
| 654 | rtcp_receiver_report_ssrc_ = kDefaultRtcpReceiverReportSsrc; |
| 655 | sending_ = false; |
| 656 | recv_codecs_ = MapCodecs(GetPayloadTypesAndDefaultCodecs( |
| 657 | decoder_factory_, /*is_decoder_factory=*/true)); |
| 658 | recv_flexfec_payload_type_ = |
| 659 | recv_codecs_.empty() ? 0 : recv_codecs_.front().flexfec_payload_type; |
| 660 | } |
| 661 |
| 662 | WebRtcVideoChannel::~WebRtcVideoChannel() { |
| 663 | for (auto& kv : send_streams_) |
| 664 | delete kv.second; |
| 665 | for (auto& kv : receive_streams_) |
| 666 | delete kv.second; |
| 667 | } |
| 668 |
| 669 | std::vector<WebRtcVideoChannel::VideoCodecSettings> |
| 670 | WebRtcVideoChannel::SelectSendVideoCodecs( |
| 671 | const std::vector<VideoCodecSettings>& remote_mapped_codecs) const { |
| 672 | std::vector<webrtc::SdpVideoFormat> sdp_formats = |
| 673 | encoder_factory_ ? encoder_factory_->GetImplementations() |
| 674 | : std::vector<webrtc::SdpVideoFormat>(); |
| 675 |
| 676 | // The returned vector holds the VideoCodecSettings in term of preference. |
| 677 | // They are orderd by receive codec preference first and local implementation |
| 678 | // preference second. |
| 679 | std::vector<VideoCodecSettings> encoders; |
| 680 | for (const VideoCodecSettings& remote_codec : remote_mapped_codecs) { |
| 681 | for (auto format_it = sdp_formats.begin(); |
| 682 | format_it != sdp_formats.end();) { |
| 683 | // For H264, we will limit the encode level to the remote offered level |
| 684 | // regardless if level asymmetry is allowed or not. This is strictly not |
| 685 | // following the spec in https://tools.ietf.org/html/rfc6184#section-8.2.2 |
| 686 | // since we should limit the encode level to the lower of local and remote |
| 687 | // level when level asymmetry is not allowed. |
| 688 | if (IsSameCodec(format_it->name, format_it->parameters, |
| 689 | remote_codec.codec.name, remote_codec.codec.params)) { |
| 690 | encoders.push_back(remote_codec); |
| 691 |
| 692 | // To allow the VideoEncoderFactory to keep information about which |
| 693 | // implementation to instantitate when CreateEncoder is called the two |
| 694 | // parmeter sets are merged. |
| 695 | encoders.back().codec.params.insert(format_it->parameters.begin(), |
| 696 | format_it->parameters.end()); |
| 697 |
| 698 | format_it = sdp_formats.erase(format_it); |
| 699 | } else { |
| 700 | ++format_it; |
| 701 | } |
| 702 | } |
| 703 | } |
| 704 |
| 705 | return encoders; |
| 706 | } |
| 707 |
| 708 | bool WebRtcVideoChannel::NonFlexfecReceiveCodecsHaveChanged( |
| 709 | std::vector<VideoCodecSettings> before, |
| 710 | std::vector<VideoCodecSettings> after) { |
| 711 | // The receive codec order doesn't matter, so we sort the codecs before |
| 712 | // comparing. This is necessary because currently the |
| 713 | // only way to change the send codec is to munge SDP, which causes |
| 714 | // the receive codec list to change order, which causes the streams |
| 715 | // to be recreates which causes a "blink" of black video. In order |
| 716 | // to support munging the SDP in this way without recreating receive |
| 717 | // streams, we ignore the order of the received codecs so that |
| 718 | // changing the order doesn't cause this "blink". |
| 719 | auto comparison = [](const VideoCodecSettings& codec1, |
| 720 | const VideoCodecSettings& codec2) { |
| 721 | return codec1.codec.id > codec2.codec.id; |
| 722 | }; |
| 723 | absl::c_sort(before, comparison); |
| 724 | absl::c_sort(after, comparison); |
| 725 |
| 726 | // Changes in FlexFEC payload type are handled separately in |
| 727 | // WebRtcVideoChannel::GetChangedRecvParameters, so disregard FlexFEC in the |
| 728 | // comparison here. |
| 729 | return !absl::c_equal(before, after, |
| 730 | VideoCodecSettings::EqualsDisregardingFlexfec); |
| 731 | } |
| 732 |
| 733 | bool WebRtcVideoChannel::GetChangedSendParameters( |
| 734 | const VideoSendParameters& params, |
| 735 | ChangedSendParameters* changed_params) const { |
| 736 | if (!ValidateCodecFormats(params.codecs) || |
| 737 | !ValidateRtpExtensions(params.extensions)) { |
| 738 | return false; |
| 739 | } |
| 740 |
| 741 | std::vector<VideoCodecSettings> negotiated_codecs = |
| 742 | SelectSendVideoCodecs(MapCodecs(params.codecs)); |
| 743 |
| 744 | // We should only fail here if send direction is enabled. |
| 745 | if (params.is_stream_active && negotiated_codecs.empty()) { |
| 746 | RTC_LOG(LS_ERROR) << "No video codecs supported."; |
| 747 | return false; |
| 748 | } |
| 749 |
| 750 | // Never enable sending FlexFEC, unless we are in the experiment. |
| 751 | if (!IsFlexfecFieldTrialEnabled()) { |
| 752 | RTC_LOG(LS_INFO) << "WebRTC-FlexFEC-03 field trial is not enabled."; |
| 753 | for (VideoCodecSettings& codec : negotiated_codecs) |
| 754 | codec.flexfec_payload_type = -1; |
| 755 | } |
| 756 |
| 757 | if (negotiated_codecs_ != negotiated_codecs) { |
| 758 | if (negotiated_codecs.empty()) { |
| 759 | changed_params->send_codec = absl::nullopt; |
| 760 | } else if (send_codec_ != negotiated_codecs.front()) { |
| 761 | changed_params->send_codec = negotiated_codecs.front(); |
| 762 | } |
| 763 | changed_params->negotiated_codecs = std::move(negotiated_codecs); |
| 764 | } |
| 765 |
| 766 | // Handle RTP header extensions. |
| 767 | if (params.extmap_allow_mixed != ExtmapAllowMixed()) { |
| 768 | changed_params->extmap_allow_mixed = params.extmap_allow_mixed; |
| 769 | } |
| 770 | std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions( |
| 771 | params.extensions, webrtc::RtpExtension::IsSupportedForVideo, true); |
| 772 | if (!send_rtp_extensions_ || (*send_rtp_extensions_ != filtered_extensions)) { |
| 773 | changed_params->rtp_header_extensions = |
| 774 | absl::optional<std::vector<webrtc::RtpExtension>>(filtered_extensions); |
| 775 | } |
| 776 |
| 777 | if (params.mid != send_params_.mid) { |
| 778 | changed_params->mid = params.mid; |
| 779 | } |
| 780 |
| 781 | // Handle max bitrate. |
| 782 | if (params.max_bandwidth_bps != send_params_.max_bandwidth_bps && |
| 783 | params.max_bandwidth_bps >= -1) { |
| 784 | // 0 or -1 uncaps max bitrate. |
| 785 | // TODO(pbos): Reconsider how 0 should be treated. It is not mentioned as a |
| 786 | // special value and might very well be used for stopping sending. |
| 787 | changed_params->max_bandwidth_bps = |
| 788 | params.max_bandwidth_bps == 0 ? -1 : params.max_bandwidth_bps; |
| 789 | } |
| 790 |
| 791 | // Handle conference mode. |
| 792 | if (params.conference_mode != send_params_.conference_mode) { |
| 793 | changed_params->conference_mode = params.conference_mode; |
| 794 | } |
| 795 |
| 796 | // Handle RTCP mode. |
| 797 | if (params.rtcp.reduced_size != send_params_.rtcp.reduced_size) { |
| 798 | changed_params->rtcp_mode = params.rtcp.reduced_size |
| 799 | ? webrtc::RtcpMode::kReducedSize |
| 800 | : webrtc::RtcpMode::kCompound; |
| 801 | } |
| 802 |
| 803 | return true; |
| 804 | } |
| 805 |
| 806 | bool WebRtcVideoChannel::SetSendParameters(const VideoSendParameters& params) { |
| 807 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 808 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel::SetSendParameters"); |
| 809 | RTC_LOG(LS_INFO) << "SetSendParameters: " << params.ToString(); |
| 810 | ChangedSendParameters changed_params; |
| 811 | if (!GetChangedSendParameters(params, &changed_params)) { |
| 812 | return false; |
| 813 | } |
| 814 |
| 815 | if (changed_params.negotiated_codecs) { |
| 816 | for (const auto& send_codec : *changed_params.negotiated_codecs) |
| 817 | RTC_LOG(LS_INFO) << "Negotiated codec: " << send_codec.codec.ToString(); |
| 818 | } |
| 819 |
| 820 | send_params_ = params; |
| 821 | return ApplyChangedParams(changed_params); |
| 822 | } |
| 823 |
| 824 | void WebRtcVideoChannel::RequestEncoderFallback() { |
| 825 | invoker_.AsyncInvoke<void>( |
| 826 | RTC_FROM_HERE, worker_thread_, [this] { |
| 827 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 828 | if (negotiated_codecs_.size() <= 1) { |
| 829 | RTC_LOG(LS_WARNING) |
| 830 | << "Encoder failed but no fallback codec is available"; |
| 831 | return; |
| 832 | } |
| 833 |
| 834 | ChangedSendParameters params; |
| 835 | params.negotiated_codecs = negotiated_codecs_; |
| 836 | params.negotiated_codecs->erase(params.negotiated_codecs->begin()); |
| 837 | params.send_codec = params.negotiated_codecs->front(); |
| 838 | ApplyChangedParams(params); |
| 839 | }); |
| 840 | } |
| 841 |
| 842 | void WebRtcVideoChannel::RequestEncoderSwitch( |
| 843 | const EncoderSwitchRequestCallback::Config& conf) { |
| 844 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, [this, conf] { |
| 845 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 846 |
| 847 | if (!allow_codec_switching_) { |
| 848 | RTC_LOG(LS_INFO) << "Encoder switch requested but codec switching has" |
| 849 | " not been enabled yet."; |
| 850 | requested_encoder_switch_ = conf; |
| 851 | return; |
| 852 | } |
| 853 |
| 854 | for (const VideoCodecSettings& codec_setting : negotiated_codecs_) { |
| 855 | if (codec_setting.codec.name == conf.codec_name) { |
| 856 | if (conf.param) { |
| 857 | auto it = codec_setting.codec.params.find(*conf.param); |
| 858 |
| 859 | if (it == codec_setting.codec.params.end()) { |
| 860 | continue; |
| 861 | } |
| 862 |
| 863 | if (conf.value && it->second != *conf.value) { |
| 864 | continue; |
| 865 | } |
| 866 | } |
| 867 |
| 868 | if (send_codec_ == codec_setting) { |
| 869 | // Already using this codec, no switch required. |
| 870 | return; |
| 871 | } |
| 872 |
| 873 | ChangedSendParameters params; |
| 874 | params.send_codec = codec_setting; |
| 875 | ApplyChangedParams(params); |
| 876 | return; |
| 877 | } |
| 878 | } |
| 879 |
| 880 | RTC_LOG(LS_WARNING) << "Requested encoder with codec_name:" |
| 881 | << conf.codec_name |
| 882 | << ", param:" << conf.param.value_or("none") |
| 883 | << " and value:" << conf.value.value_or("none") |
| 884 | << "not found. No switch performed."; |
| 885 | }); |
| 886 | } |
| 887 |
| 888 | void WebRtcVideoChannel::RequestEncoderSwitch( |
| 889 | const webrtc::SdpVideoFormat& format) { |
| 890 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, [this, format] { |
| 891 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 892 |
| 893 | for (const VideoCodecSettings& codec_setting : negotiated_codecs_) { |
| 894 | if (IsSameCodec(format.name, format.parameters, codec_setting.codec.name, |
| 895 | codec_setting.codec.params)) { |
| 896 | VideoCodecSettings new_codec_setting = codec_setting; |
| 897 | for (const auto& kv : format.parameters) { |
| 898 | new_codec_setting.codec.params[kv.first] = kv.second; |
| 899 | } |
| 900 |
| 901 | if (send_codec_ == new_codec_setting) { |
| 902 | // Already using this codec, no switch required. |
| 903 | return; |
| 904 | } |
| 905 |
| 906 | ChangedSendParameters params; |
| 907 | params.send_codec = new_codec_setting; |
| 908 | ApplyChangedParams(params); |
| 909 | return; |
| 910 | } |
| 911 | } |
| 912 |
| 913 | RTC_LOG(LS_WARNING) << "Encoder switch failed: SdpVideoFormat " |
| 914 | << format.ToString() << " not negotiated."; |
| 915 | }); |
| 916 | } |
| 917 |
| 918 | bool WebRtcVideoChannel::ApplyChangedParams( |
| 919 | const ChangedSendParameters& changed_params) { |
| 920 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 921 | if (changed_params.negotiated_codecs) |
| 922 | negotiated_codecs_ = *changed_params.negotiated_codecs; |
| 923 |
| 924 | if (changed_params.send_codec) |
| 925 | send_codec_ = changed_params.send_codec; |
| 926 |
| 927 | if (changed_params.extmap_allow_mixed) { |
| 928 | SetExtmapAllowMixed(*changed_params.extmap_allow_mixed); |
| 929 | } |
| 930 | if (changed_params.rtp_header_extensions) { |
| 931 | send_rtp_extensions_ = changed_params.rtp_header_extensions; |
| 932 | } |
| 933 |
| 934 | if (changed_params.send_codec || changed_params.max_bandwidth_bps) { |
| 935 | if (send_params_.max_bandwidth_bps == -1) { |
| 936 | // Unset the global max bitrate (max_bitrate_bps) if max_bandwidth_bps is |
| 937 | // -1, which corresponds to no "b=AS" attribute in SDP. Note that the |
| 938 | // global max bitrate may be set below in GetBitrateConfigForCodec, from |
| 939 | // the codec max bitrate. |
| 940 | // TODO(pbos): This should be reconsidered (codec max bitrate should |
| 941 | // probably not affect global call max bitrate). |
| 942 | bitrate_config_.max_bitrate_bps = -1; |
| 943 | } |
| 944 |
| 945 | if (send_codec_) { |
| 946 | // TODO(holmer): Changing the codec parameters shouldn't necessarily mean |
| 947 | // that we change the min/max of bandwidth estimation. Reevaluate this. |
| 948 | bitrate_config_ = GetBitrateConfigForCodec(send_codec_->codec); |
| 949 | if (!changed_params.send_codec) { |
| 950 | // If the codec isn't changing, set the start bitrate to -1 which means |
| 951 | // "unchanged" so that BWE isn't affected. |
| 952 | bitrate_config_.start_bitrate_bps = -1; |
| 953 | } |
| 954 | } |
| 955 |
| 956 | if (send_params_.max_bandwidth_bps >= 0) { |
| 957 | // Note that max_bandwidth_bps intentionally takes priority over the |
| 958 | // bitrate config for the codec. This allows FEC to be applied above the |
| 959 | // codec target bitrate. |
| 960 | // TODO(pbos): Figure out whether b=AS means max bitrate for this |
| 961 | // WebRtcVideoChannel (in which case we're good), or per sender (SSRC), |
| 962 | // in which case this should not set a BitrateConstraints but rather |
| 963 | // reconfigure all senders. |
| 964 | bitrate_config_.max_bitrate_bps = send_params_.max_bandwidth_bps == 0 |
| 965 | ? -1 |
| 966 | : send_params_.max_bandwidth_bps; |
| 967 | } |
| 968 |
| 969 | call_->GetTransportControllerSend()->SetSdpBitrateParameters( |
| 970 | bitrate_config_); |
| 971 | } |
| 972 |
| 973 | for (auto& kv : send_streams_) { |
| 974 | kv.second->SetSendParameters(changed_params); |
| 975 | } |
| 976 | if (changed_params.send_codec || changed_params.rtcp_mode) { |
| 977 | // Update receive feedback parameters from new codec or RTCP mode. |
| 978 | RTC_LOG(LS_INFO) |
| 979 | << "SetFeedbackOptions on all the receive streams because the send " |
| 980 | "codec or RTCP mode has changed."; |
| 981 | for (auto& kv : receive_streams_) { |
| 982 | RTC_DCHECK(kv.second != nullptr); |
| 983 | kv.second->SetFeedbackParameters( |
| 984 | HasLntf(send_codec_->codec), HasNack(send_codec_->codec), |
| 985 | HasTransportCc(send_codec_->codec), |
| 986 | send_params_.rtcp.reduced_size ? webrtc::RtcpMode::kReducedSize |
| 987 | : webrtc::RtcpMode::kCompound); |
| 988 | } |
| 989 | } |
| 990 | return true; |
| 991 | } |
| 992 |
| 993 | webrtc::RtpParameters WebRtcVideoChannel::GetRtpSendParameters( |
| 994 | uint32_t ssrc) const { |
| 995 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 996 | auto it = send_streams_.find(ssrc); |
| 997 | if (it == send_streams_.end()) { |
| 998 | RTC_LOG(LS_WARNING) << "Attempting to get RTP send parameters for stream " |
| 999 | "with ssrc " |
| 1000 | << ssrc << " which doesn't exist."; |
| 1001 | return webrtc::RtpParameters(); |
| 1002 | } |
| 1003 |
| 1004 | webrtc::RtpParameters rtp_params = it->second->GetRtpParameters(); |
| 1005 | // Need to add the common list of codecs to the send stream-specific |
| 1006 | // RTP parameters. |
| 1007 | for (const VideoCodec& codec : send_params_.codecs) { |
| 1008 | rtp_params.codecs.push_back(codec.ToCodecParameters()); |
| 1009 | } |
| 1010 | return rtp_params; |
| 1011 | } |
| 1012 |
| 1013 | webrtc::RTCError WebRtcVideoChannel::SetRtpSendParameters( |
| 1014 | uint32_t ssrc, |
| 1015 | const webrtc::RtpParameters& parameters) { |
| 1016 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1017 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel::SetRtpSendParameters"); |
| 1018 | auto it = send_streams_.find(ssrc); |
| 1019 | if (it == send_streams_.end()) { |
| 1020 | RTC_LOG(LS_ERROR) << "Attempting to set RTP send parameters for stream " |
| 1021 | "with ssrc " |
| 1022 | << ssrc << " which doesn't exist."; |
| 1023 | return webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR); |
| 1024 | } |
| 1025 |
| 1026 | // TODO(deadbeef): Handle setting parameters with a list of codecs in a |
| 1027 | // different order (which should change the send codec). |
| 1028 | webrtc::RtpParameters current_parameters = GetRtpSendParameters(ssrc); |
| 1029 | if (current_parameters.codecs != parameters.codecs) { |
| 1030 | RTC_DLOG(LS_ERROR) << "Using SetParameters to change the set of codecs " |
| 1031 | "is not currently supported."; |
| 1032 | return webrtc::RTCError(webrtc::RTCErrorType::INTERNAL_ERROR); |
| 1033 | } |
| 1034 |
| 1035 | if (!parameters.encodings.empty()) { |
| 1036 | // Note that these values come from: |
| 1037 | // https://tools.ietf.org/html/draft-ietf-tsvwg-rtcweb-qos-16#section-5 |
| 1038 | // TODO(deadbeef): Change values depending on whether we are sending a |
| 1039 | // keyframe or non-keyframe. |
| 1040 | rtc::DiffServCodePoint new_dscp = rtc::DSCP_DEFAULT; |
| 1041 | switch (parameters.encodings[0].network_priority) { |
| 1042 | case webrtc::Priority::kVeryLow: |
| 1043 | new_dscp = rtc::DSCP_CS1; |
| 1044 | break; |
| 1045 | case webrtc::Priority::kLow: |
| 1046 | new_dscp = rtc::DSCP_DEFAULT; |
| 1047 | break; |
| 1048 | case webrtc::Priority::kMedium: |
| 1049 | new_dscp = rtc::DSCP_AF42; |
| 1050 | break; |
| 1051 | case webrtc::Priority::kHigh: |
| 1052 | new_dscp = rtc::DSCP_AF41; |
| 1053 | break; |
| 1054 | } |
| 1055 | SetPreferredDscp(new_dscp); |
| 1056 | } |
| 1057 |
| 1058 | return it->second->SetRtpParameters(parameters); |
| 1059 | } |
| 1060 |
| 1061 | webrtc::RtpParameters WebRtcVideoChannel::GetRtpReceiveParameters( |
| 1062 | uint32_t ssrc) const { |
| 1063 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1064 | webrtc::RtpParameters rtp_params; |
| 1065 | auto it = receive_streams_.find(ssrc); |
| 1066 | if (it == receive_streams_.end()) { |
| 1067 | RTC_LOG(LS_WARNING) |
| 1068 | << "Attempting to get RTP receive parameters for stream " |
| 1069 | "with SSRC " |
| 1070 | << ssrc << " which doesn't exist."; |
| 1071 | return webrtc::RtpParameters(); |
| 1072 | } |
| 1073 | rtp_params = it->second->GetRtpParameters(); |
| 1074 |
| 1075 | // Add codecs, which any stream is prepared to receive. |
| 1076 | for (const VideoCodec& codec : recv_params_.codecs) { |
| 1077 | rtp_params.codecs.push_back(codec.ToCodecParameters()); |
| 1078 | } |
| 1079 |
| 1080 | return rtp_params; |
| 1081 | } |
| 1082 |
| 1083 | webrtc::RtpParameters WebRtcVideoChannel::GetDefaultRtpReceiveParameters() |
| 1084 | const { |
| 1085 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1086 | webrtc::RtpParameters rtp_params; |
| 1087 | if (!default_unsignalled_ssrc_handler_.GetDefaultSink()) { |
| 1088 | RTC_LOG(LS_WARNING) << "Attempting to get RTP parameters for the default, " |
| 1089 | "unsignaled video receive stream, but not yet " |
| 1090 | "configured to receive such a stream."; |
| 1091 | return rtp_params; |
| 1092 | } |
| 1093 | rtp_params.encodings.emplace_back(); |
| 1094 |
| 1095 | // Add codecs, which any stream is prepared to receive. |
| 1096 | for (const VideoCodec& codec : recv_params_.codecs) { |
| 1097 | rtp_params.codecs.push_back(codec.ToCodecParameters()); |
| 1098 | } |
| 1099 |
| 1100 | return rtp_params; |
| 1101 | } |
| 1102 |
| 1103 | bool WebRtcVideoChannel::GetChangedRecvParameters( |
| 1104 | const VideoRecvParameters& params, |
| 1105 | ChangedRecvParameters* changed_params) const { |
| 1106 | if (!ValidateCodecFormats(params.codecs) || |
| 1107 | !ValidateRtpExtensions(params.extensions)) { |
| 1108 | return false; |
| 1109 | } |
| 1110 |
| 1111 | // Handle receive codecs. |
| 1112 | const std::vector<VideoCodecSettings> mapped_codecs = |
| 1113 | MapCodecs(params.codecs); |
| 1114 | if (mapped_codecs.empty()) { |
| 1115 | RTC_LOG(LS_ERROR) << "SetRecvParameters called without any video codecs."; |
| 1116 | return false; |
| 1117 | } |
| 1118 |
| 1119 | // Verify that every mapped codec is supported locally. |
| 1120 | if (params.is_stream_active) { |
| 1121 | const std::vector<VideoCodec> local_supported_codecs = |
| 1122 | GetPayloadTypesAndDefaultCodecs(decoder_factory_, |
| 1123 | /*is_decoder_factory=*/true); |
| 1124 | for (const VideoCodecSettings& mapped_codec : mapped_codecs) { |
| 1125 | if (!FindMatchingCodec(local_supported_codecs, mapped_codec.codec)) { |
| 1126 | RTC_LOG(LS_ERROR) |
| 1127 | << "SetRecvParameters called with unsupported video codec: " |
| 1128 | << mapped_codec.codec.ToString(); |
| 1129 | return false; |
| 1130 | } |
| 1131 | } |
| 1132 | } |
| 1133 |
| 1134 | if (NonFlexfecReceiveCodecsHaveChanged(recv_codecs_, mapped_codecs)) { |
| 1135 | changed_params->codec_settings = |
| 1136 | absl::optional<std::vector<VideoCodecSettings>>(mapped_codecs); |
| 1137 | } |
| 1138 |
| 1139 | // Handle RTP header extensions. |
| 1140 | std::vector<webrtc::RtpExtension> filtered_extensions = FilterRtpExtensions( |
| 1141 | params.extensions, webrtc::RtpExtension::IsSupportedForVideo, false); |
| 1142 | if (filtered_extensions != recv_rtp_extensions_) { |
| 1143 | changed_params->rtp_header_extensions = |
| 1144 | absl::optional<std::vector<webrtc::RtpExtension>>(filtered_extensions); |
| 1145 | } |
| 1146 |
| 1147 | int flexfec_payload_type = mapped_codecs.front().flexfec_payload_type; |
| 1148 | if (flexfec_payload_type != recv_flexfec_payload_type_) { |
| 1149 | changed_params->flexfec_payload_type = flexfec_payload_type; |
| 1150 | } |
| 1151 |
| 1152 | return true; |
| 1153 | } |
| 1154 |
| 1155 | bool WebRtcVideoChannel::SetRecvParameters(const VideoRecvParameters& params) { |
| 1156 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1157 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel::SetRecvParameters"); |
| 1158 | RTC_LOG(LS_INFO) << "SetRecvParameters: " << params.ToString(); |
| 1159 | ChangedRecvParameters changed_params; |
| 1160 | if (!GetChangedRecvParameters(params, &changed_params)) { |
| 1161 | return false; |
| 1162 | } |
| 1163 | if (changed_params.flexfec_payload_type) { |
| 1164 | RTC_LOG(LS_INFO) << "Changing FlexFEC payload type (recv) from " |
| 1165 | << recv_flexfec_payload_type_ << " to " |
| 1166 | << *changed_params.flexfec_payload_type; |
| 1167 | recv_flexfec_payload_type_ = *changed_params.flexfec_payload_type; |
| 1168 | } |
| 1169 | if (changed_params.rtp_header_extensions) { |
| 1170 | recv_rtp_extensions_ = *changed_params.rtp_header_extensions; |
| 1171 | } |
| 1172 | if (changed_params.codec_settings) { |
| 1173 | RTC_LOG(LS_INFO) << "Changing recv codecs from " |
| 1174 | << CodecSettingsVectorToString(recv_codecs_) << " to " |
| 1175 | << CodecSettingsVectorToString( |
| 1176 | *changed_params.codec_settings); |
| 1177 | recv_codecs_ = *changed_params.codec_settings; |
| 1178 | } |
| 1179 |
| 1180 | for (auto& kv : receive_streams_) { |
| 1181 | kv.second->SetRecvParameters(changed_params); |
| 1182 | } |
| 1183 | recv_params_ = params; |
| 1184 | return true; |
| 1185 | } |
| 1186 |
| 1187 | std::string WebRtcVideoChannel::CodecSettingsVectorToString( |
| 1188 | const std::vector<VideoCodecSettings>& codecs) { |
| 1189 | rtc::StringBuilder out; |
| 1190 | out << "{"; |
| 1191 | for (size_t i = 0; i < codecs.size(); ++i) { |
| 1192 | out << codecs[i].codec.ToString(); |
| 1193 | if (i != codecs.size() - 1) { |
| 1194 | out << ", "; |
| 1195 | } |
| 1196 | } |
| 1197 | out << "}"; |
| 1198 | return out.Release(); |
| 1199 | } |
| 1200 |
| 1201 | bool WebRtcVideoChannel::GetSendCodec(VideoCodec* codec) { |
| 1202 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1203 | if (!send_codec_) { |
| 1204 | RTC_LOG(LS_VERBOSE) << "GetSendCodec: No send codec set."; |
| 1205 | return false; |
| 1206 | } |
| 1207 | *codec = send_codec_->codec; |
| 1208 | return true; |
| 1209 | } |
| 1210 |
| 1211 | bool WebRtcVideoChannel::SetSend(bool send) { |
| 1212 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1213 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel::SetSend"); |
| 1214 | RTC_LOG(LS_VERBOSE) << "SetSend: " << (send ? "true" : "false"); |
| 1215 | if (send && !send_codec_) { |
| 1216 | RTC_DLOG(LS_ERROR) << "SetSend(true) called before setting codec."; |
| 1217 | return false; |
| 1218 | } |
| 1219 | for (const auto& kv : send_streams_) { |
| 1220 | kv.second->SetSend(send); |
| 1221 | } |
| 1222 | sending_ = send; |
| 1223 | return true; |
| 1224 | } |
| 1225 |
| 1226 | bool WebRtcVideoChannel::SetVideoSend( |
| 1227 | uint32_t ssrc, |
| 1228 | const VideoOptions* options, |
| 1229 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source) { |
| 1230 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1231 | TRACE_EVENT0("webrtc", "SetVideoSend"); |
| 1232 | RTC_DCHECK(ssrc != 0); |
| 1233 | RTC_LOG(LS_INFO) << "SetVideoSend (ssrc= " << ssrc << ", options: " |
| 1234 | << (options ? options->ToString() : "nullptr") |
| 1235 | << ", source = " << (source ? "(source)" : "nullptr") << ")"; |
| 1236 |
| 1237 | const auto& kv = send_streams_.find(ssrc); |
| 1238 | if (kv == send_streams_.end()) { |
| 1239 | // Allow unknown ssrc only if source is null. |
| 1240 | RTC_CHECK(source == nullptr); |
| 1241 | RTC_LOG(LS_ERROR) << "No sending stream on ssrc " << ssrc; |
| 1242 | return false; |
| 1243 | } |
| 1244 |
| 1245 | return kv->second->SetVideoSend(options, source); |
| 1246 | } |
| 1247 |
| 1248 | bool WebRtcVideoChannel::ValidateSendSsrcAvailability( |
| 1249 | const StreamParams& sp) const { |
| 1250 | for (uint32_t ssrc : sp.ssrcs) { |
| 1251 | if (send_ssrcs_.find(ssrc) != send_ssrcs_.end()) { |
| 1252 | RTC_LOG(LS_ERROR) << "Send stream with SSRC '" << ssrc |
| 1253 | << "' already exists."; |
| 1254 | return false; |
| 1255 | } |
| 1256 | } |
| 1257 | return true; |
| 1258 | } |
| 1259 |
| 1260 | bool WebRtcVideoChannel::ValidateReceiveSsrcAvailability( |
| 1261 | const StreamParams& sp) const { |
| 1262 | for (uint32_t ssrc : sp.ssrcs) { |
| 1263 | if (receive_ssrcs_.find(ssrc) != receive_ssrcs_.end()) { |
| 1264 | RTC_LOG(LS_ERROR) << "Receive stream with SSRC '" << ssrc |
| 1265 | << "' already exists."; |
| 1266 | return false; |
| 1267 | } |
| 1268 | } |
| 1269 | return true; |
| 1270 | } |
| 1271 |
| 1272 | bool WebRtcVideoChannel::AddSendStream(const StreamParams& sp) { |
| 1273 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1274 | RTC_LOG(LS_INFO) << "AddSendStream: " << sp.ToString(); |
| 1275 | if (!ValidateStreamParams(sp)) |
| 1276 | return false; |
| 1277 |
| 1278 | if (!ValidateSendSsrcAvailability(sp)) |
| 1279 | return false; |
| 1280 |
| 1281 | for (uint32_t used_ssrc : sp.ssrcs) |
| 1282 | send_ssrcs_.insert(used_ssrc); |
| 1283 |
| 1284 | webrtc::VideoSendStream::Config config(this); |
| 1285 |
| 1286 | for (const RidDescription& rid : sp.rids()) { |
| 1287 | config.rtp.rids.push_back(rid.rid); |
| 1288 | } |
| 1289 |
| 1290 | config.suspend_below_min_bitrate = video_config_.suspend_below_min_bitrate; |
| 1291 | config.periodic_alr_bandwidth_probing = |
| 1292 | video_config_.periodic_alr_bandwidth_probing; |
| 1293 | config.encoder_settings.experiment_cpu_load_estimator = |
| 1294 | video_config_.experiment_cpu_load_estimator; |
| 1295 | config.encoder_settings.encoder_factory = encoder_factory_; |
| 1296 | config.encoder_settings.bitrate_allocator_factory = |
| 1297 | bitrate_allocator_factory_; |
| 1298 | config.encoder_settings.encoder_switch_request_callback = this; |
| 1299 | config.crypto_options = crypto_options_; |
| 1300 | config.rtp.extmap_allow_mixed = ExtmapAllowMixed(); |
| 1301 | config.rtcp_report_interval_ms = video_config_.rtcp_report_interval_ms; |
| 1302 |
| 1303 | WebRtcVideoSendStream* stream = new WebRtcVideoSendStream( |
| 1304 | call_, sp, std::move(config), default_send_options_, |
| 1305 | video_config_.enable_cpu_adaptation, bitrate_config_.max_bitrate_bps, |
| 1306 | send_codec_, send_rtp_extensions_, send_params_); |
| 1307 |
| 1308 | uint32_t ssrc = sp.first_ssrc(); |
| 1309 | RTC_DCHECK(ssrc != 0); |
| 1310 | send_streams_[ssrc] = stream; |
| 1311 |
| 1312 | if (rtcp_receiver_report_ssrc_ == kDefaultRtcpReceiverReportSsrc) { |
| 1313 | rtcp_receiver_report_ssrc_ = ssrc; |
| 1314 | RTC_LOG(LS_INFO) |
| 1315 | << "SetLocalSsrc on all the receive streams because we added " |
| 1316 | "a send stream."; |
| 1317 | for (auto& kv : receive_streams_) |
| 1318 | kv.second->SetLocalSsrc(ssrc); |
| 1319 | } |
| 1320 | if (sending_) { |
| 1321 | stream->SetSend(true); |
| 1322 | } |
| 1323 |
| 1324 | return true; |
| 1325 | } |
| 1326 |
| 1327 | bool WebRtcVideoChannel::RemoveSendStream(uint32_t ssrc) { |
| 1328 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1329 | RTC_LOG(LS_INFO) << "RemoveSendStream: " << ssrc; |
| 1330 |
| 1331 | WebRtcVideoSendStream* removed_stream; |
| 1332 | std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = |
| 1333 | send_streams_.find(ssrc); |
| 1334 | if (it == send_streams_.end()) { |
| 1335 | return false; |
| 1336 | } |
| 1337 |
| 1338 | for (uint32_t old_ssrc : it->second->GetSsrcs()) |
| 1339 | send_ssrcs_.erase(old_ssrc); |
| 1340 |
| 1341 | removed_stream = it->second; |
| 1342 | send_streams_.erase(it); |
| 1343 |
| 1344 | // Switch receiver report SSRCs, the one in use is no longer valid. |
| 1345 | if (rtcp_receiver_report_ssrc_ == ssrc) { |
| 1346 | rtcp_receiver_report_ssrc_ = send_streams_.empty() |
| 1347 | ? kDefaultRtcpReceiverReportSsrc |
| 1348 | : send_streams_.begin()->first; |
| 1349 | RTC_LOG(LS_INFO) << "SetLocalSsrc on all the receive streams because the " |
| 1350 | "previous local SSRC was removed."; |
| 1351 |
| 1352 | for (auto& kv : receive_streams_) { |
| 1353 | kv.second->SetLocalSsrc(rtcp_receiver_report_ssrc_); |
| 1354 | } |
| 1355 | } |
| 1356 |
| 1357 | delete removed_stream; |
| 1358 |
| 1359 | return true; |
| 1360 | } |
| 1361 |
| 1362 | void WebRtcVideoChannel::DeleteReceiveStream( |
| 1363 | WebRtcVideoChannel::WebRtcVideoReceiveStream* stream) { |
| 1364 | for (uint32_t old_ssrc : stream->GetSsrcs()) |
| 1365 | receive_ssrcs_.erase(old_ssrc); |
| 1366 | delete stream; |
| 1367 | } |
| 1368 |
| 1369 | bool WebRtcVideoChannel::AddRecvStream(const StreamParams& sp) { |
| 1370 | return AddRecvStream(sp, false); |
| 1371 | } |
| 1372 |
| 1373 | bool WebRtcVideoChannel::AddRecvStream(const StreamParams& sp, |
| 1374 | bool default_stream) { |
| 1375 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1376 |
| 1377 | RTC_LOG(LS_INFO) << "AddRecvStream" |
| 1378 | << (default_stream ? " (default stream)" : "") << ": " |
| 1379 | << sp.ToString(); |
| 1380 | if (!sp.has_ssrcs()) { |
| 1381 | // This is a StreamParam with unsignaled SSRCs. Store it, so it can be used |
| 1382 | // later when we know the SSRC on the first packet arrival. |
| 1383 | unsignaled_stream_params_ = sp; |
| 1384 | return true; |
| 1385 | } |
| 1386 |
| 1387 | if (!ValidateStreamParams(sp)) |
| 1388 | return false; |
| 1389 |
| 1390 | uint32_t ssrc = sp.first_ssrc(); |
| 1391 |
| 1392 | // Remove running stream if this was a default stream. |
| 1393 | const auto& prev_stream = receive_streams_.find(ssrc); |
| 1394 | if (prev_stream != receive_streams_.end()) { |
| 1395 | if (default_stream || !prev_stream->second->IsDefaultStream()) { |
| 1396 | RTC_LOG(LS_ERROR) << "Receive stream for SSRC '" << ssrc |
| 1397 | << "' already exists."; |
| 1398 | return false; |
| 1399 | } |
| 1400 | DeleteReceiveStream(prev_stream->second); |
| 1401 | receive_streams_.erase(prev_stream); |
| 1402 | } |
| 1403 |
| 1404 | if (!ValidateReceiveSsrcAvailability(sp)) |
| 1405 | return false; |
| 1406 |
| 1407 | for (uint32_t used_ssrc : sp.ssrcs) |
| 1408 | receive_ssrcs_.insert(used_ssrc); |
| 1409 |
| 1410 | webrtc::VideoReceiveStream::Config config(this); |
| 1411 | webrtc::FlexfecReceiveStream::Config flexfec_config(this); |
| 1412 | ConfigureReceiverRtp(&config, &flexfec_config, sp); |
| 1413 |
| 1414 | config.crypto_options = crypto_options_; |
| 1415 | config.enable_prerenderer_smoothing = |
| 1416 | video_config_.enable_prerenderer_smoothing; |
| 1417 | if (!sp.stream_ids().empty()) { |
| 1418 | config.sync_group = sp.stream_ids()[0]; |
| 1419 | } |
| 1420 |
| 1421 | if (unsignaled_frame_transformer_ && !config.frame_transformer) |
| 1422 | config.frame_transformer = unsignaled_frame_transformer_; |
| 1423 |
| 1424 | receive_streams_[ssrc] = new WebRtcVideoReceiveStream( |
| 1425 | this, call_, sp, std::move(config), decoder_factory_, default_stream, |
| 1426 | recv_codecs_, flexfec_config); |
| 1427 |
| 1428 | return true; |
| 1429 | } |
| 1430 |
| 1431 | void WebRtcVideoChannel::ConfigureReceiverRtp( |
| 1432 | webrtc::VideoReceiveStream::Config* config, |
| 1433 | webrtc::FlexfecReceiveStream::Config* flexfec_config, |
| 1434 | const StreamParams& sp) const { |
| 1435 | uint32_t ssrc = sp.first_ssrc(); |
| 1436 |
| 1437 | config->rtp.remote_ssrc = ssrc; |
| 1438 | config->rtp.local_ssrc = rtcp_receiver_report_ssrc_; |
| 1439 |
| 1440 | // TODO(pbos): This protection is against setting the same local ssrc as |
| 1441 | // remote which is not permitted by the lower-level API. RTCP requires a |
| 1442 | // corresponding sender SSRC. Figure out what to do when we don't have |
| 1443 | // (receive-only) or know a good local SSRC. |
| 1444 | if (config->rtp.remote_ssrc == config->rtp.local_ssrc) { |
| 1445 | if (config->rtp.local_ssrc != kDefaultRtcpReceiverReportSsrc) { |
| 1446 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc; |
| 1447 | } else { |
| 1448 | config->rtp.local_ssrc = kDefaultRtcpReceiverReportSsrc + 1; |
| 1449 | } |
| 1450 | } |
| 1451 |
| 1452 | // Whether or not the receive stream sends reduced size RTCP is determined |
| 1453 | // by the send params. |
| 1454 | // TODO(deadbeef): Once we change "send_params" to "sender_params" and |
| 1455 | // "recv_params" to "receiver_params", we should get this out of |
| 1456 | // receiver_params_. |
| 1457 | config->rtp.rtcp_mode = send_params_.rtcp.reduced_size |
| 1458 | ? webrtc::RtcpMode::kReducedSize |
| 1459 | : webrtc::RtcpMode::kCompound; |
| 1460 |
| 1461 | config->rtp.transport_cc = |
| 1462 | send_codec_ ? HasTransportCc(send_codec_->codec) : false; |
| 1463 |
| 1464 | sp.GetFidSsrc(ssrc, &config->rtp.rtx_ssrc); |
| 1465 |
| 1466 | config->rtp.extensions = recv_rtp_extensions_; |
| 1467 |
| 1468 | // TODO(brandtr): Generalize when we add support for multistream protection. |
| 1469 | flexfec_config->payload_type = recv_flexfec_payload_type_; |
| 1470 | if (IsFlexfecAdvertisedFieldTrialEnabled() && |
| 1471 | sp.GetFecFrSsrc(ssrc, &flexfec_config->remote_ssrc)) { |
| 1472 | flexfec_config->protected_media_ssrcs = {ssrc}; |
| 1473 | flexfec_config->local_ssrc = config->rtp.local_ssrc; |
| 1474 | flexfec_config->rtcp_mode = config->rtp.rtcp_mode; |
| 1475 | // TODO(brandtr): We should be spec-compliant and set |transport_cc| here |
| 1476 | // based on the rtcp-fb for the FlexFEC codec, not the media codec. |
| 1477 | flexfec_config->transport_cc = config->rtp.transport_cc; |
| 1478 | flexfec_config->rtp_header_extensions = config->rtp.extensions; |
| 1479 | } |
| 1480 | } |
| 1481 |
| 1482 | bool WebRtcVideoChannel::RemoveRecvStream(uint32_t ssrc) { |
| 1483 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1484 | RTC_LOG(LS_INFO) << "RemoveRecvStream: " << ssrc; |
| 1485 |
| 1486 | std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator stream = |
| 1487 | receive_streams_.find(ssrc); |
| 1488 | if (stream == receive_streams_.end()) { |
| 1489 | RTC_LOG(LS_ERROR) << "Stream not found for ssrc: " << ssrc; |
| 1490 | return false; |
| 1491 | } |
| 1492 | DeleteReceiveStream(stream->second); |
| 1493 | receive_streams_.erase(stream); |
| 1494 |
| 1495 | return true; |
| 1496 | } |
| 1497 |
| 1498 | void WebRtcVideoChannel::ResetUnsignaledRecvStream() { |
| 1499 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1500 | RTC_LOG(LS_INFO) << "ResetUnsignaledRecvStream."; |
| 1501 | unsignaled_stream_params_ = StreamParams(); |
| 1502 |
| 1503 | // Delete any created default streams. This is needed to avoid SSRC collisions |
| 1504 | // in Call's RtpDemuxer, in the case that |this| has created a default video |
| 1505 | // receiver, and then some other WebRtcVideoChannel gets the SSRC signaled |
| 1506 | // in the corresponding Unified Plan "m=" section. |
| 1507 | auto it = receive_streams_.begin(); |
| 1508 | while (it != receive_streams_.end()) { |
| 1509 | if (it->second->IsDefaultStream()) { |
| 1510 | DeleteReceiveStream(it->second); |
| 1511 | receive_streams_.erase(it++); |
| 1512 | } else { |
| 1513 | ++it; |
| 1514 | } |
| 1515 | } |
| 1516 | } |
| 1517 |
| 1518 | bool WebRtcVideoChannel::SetSink( |
| 1519 | uint32_t ssrc, |
| 1520 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { |
| 1521 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1522 | RTC_LOG(LS_INFO) << "SetSink: ssrc:" << ssrc << " " |
| 1523 | << (sink ? "(ptr)" : "nullptr"); |
| 1524 |
| 1525 | std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = |
| 1526 | receive_streams_.find(ssrc); |
| 1527 | if (it == receive_streams_.end()) { |
| 1528 | return false; |
| 1529 | } |
| 1530 |
| 1531 | it->second->SetSink(sink); |
| 1532 | return true; |
| 1533 | } |
| 1534 |
| 1535 | void WebRtcVideoChannel::SetDefaultSink( |
| 1536 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { |
| 1537 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1538 | RTC_LOG(LS_INFO) << "SetDefaultSink: " << (sink ? "(ptr)" : "nullptr"); |
| 1539 | default_unsignalled_ssrc_handler_.SetDefaultSink(this, sink); |
| 1540 | } |
| 1541 |
| 1542 | bool WebRtcVideoChannel::GetStats(VideoMediaInfo* info) { |
| 1543 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1544 | TRACE_EVENT0("webrtc", "WebRtcVideoChannel::GetStats"); |
| 1545 |
| 1546 | // Log stats periodically. |
| 1547 | bool log_stats = false; |
| 1548 | int64_t now_ms = rtc::TimeMillis(); |
| 1549 | if (last_stats_log_ms_ == -1 || |
| 1550 | now_ms - last_stats_log_ms_ > kStatsLogIntervalMs) { |
| 1551 | last_stats_log_ms_ = now_ms; |
| 1552 | log_stats = true; |
| 1553 | } |
| 1554 |
| 1555 | info->Clear(); |
| 1556 | FillSenderStats(info, log_stats); |
| 1557 | FillReceiverStats(info, log_stats); |
| 1558 | FillSendAndReceiveCodecStats(info); |
| 1559 | // TODO(holmer): We should either have rtt available as a metric on |
| 1560 | // VideoSend/ReceiveStreams, or we should remove rtt from VideoSenderInfo. |
| 1561 | webrtc::Call::Stats stats = call_->GetStats(); |
| 1562 | if (stats.rtt_ms != -1) { |
| 1563 | for (size_t i = 0; i < info->senders.size(); ++i) { |
| 1564 | info->senders[i].rtt_ms = stats.rtt_ms; |
| 1565 | } |
| 1566 | for (size_t i = 0; i < info->aggregated_senders.size(); ++i) { |
| 1567 | info->aggregated_senders[i].rtt_ms = stats.rtt_ms; |
| 1568 | } |
| 1569 | } |
| 1570 |
| 1571 | if (log_stats) |
| 1572 | RTC_LOG(LS_INFO) << stats.ToString(now_ms); |
| 1573 |
| 1574 | return true; |
| 1575 | } |
| 1576 |
| 1577 | void WebRtcVideoChannel::FillSenderStats(VideoMediaInfo* video_media_info, |
| 1578 | bool log_stats) { |
| 1579 | for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator it = |
| 1580 | send_streams_.begin(); |
| 1581 | it != send_streams_.end(); ++it) { |
| 1582 | auto infos = it->second->GetPerLayerVideoSenderInfos(log_stats); |
| 1583 | if (infos.empty()) |
| 1584 | continue; |
| 1585 | video_media_info->aggregated_senders.push_back( |
| 1586 | it->second->GetAggregatedVideoSenderInfo(infos)); |
| 1587 | for (auto&& info : infos) { |
| 1588 | video_media_info->senders.push_back(info); |
| 1589 | } |
| 1590 | } |
| 1591 | } |
| 1592 |
| 1593 | void WebRtcVideoChannel::FillReceiverStats(VideoMediaInfo* video_media_info, |
| 1594 | bool log_stats) { |
| 1595 | for (std::map<uint32_t, WebRtcVideoReceiveStream*>::iterator it = |
| 1596 | receive_streams_.begin(); |
| 1597 | it != receive_streams_.end(); ++it) { |
| 1598 | video_media_info->receivers.push_back( |
| 1599 | it->second->GetVideoReceiverInfo(log_stats)); |
| 1600 | } |
| 1601 | } |
| 1602 |
| 1603 | void WebRtcVideoChannel::FillBitrateInfo(BandwidthEstimationInfo* bwe_info) { |
| 1604 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1605 | for (std::map<uint32_t, WebRtcVideoSendStream*>::iterator stream = |
| 1606 | send_streams_.begin(); |
| 1607 | stream != send_streams_.end(); ++stream) { |
| 1608 | stream->second->FillBitrateInfo(bwe_info); |
| 1609 | } |
| 1610 | } |
| 1611 |
| 1612 | void WebRtcVideoChannel::FillSendAndReceiveCodecStats( |
| 1613 | VideoMediaInfo* video_media_info) { |
| 1614 | for (const VideoCodec& codec : send_params_.codecs) { |
| 1615 | webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters(); |
| 1616 | video_media_info->send_codecs.insert( |
| 1617 | std::make_pair(codec_params.payload_type, std::move(codec_params))); |
| 1618 | } |
| 1619 | for (const VideoCodec& codec : recv_params_.codecs) { |
| 1620 | webrtc::RtpCodecParameters codec_params = codec.ToCodecParameters(); |
| 1621 | video_media_info->receive_codecs.insert( |
| 1622 | std::make_pair(codec_params.payload_type, std::move(codec_params))); |
| 1623 | } |
| 1624 | } |
| 1625 |
| 1626 | void WebRtcVideoChannel::OnPacketReceived(rtc::CopyOnWriteBuffer packet, |
| 1627 | int64_t packet_time_us) { |
| 1628 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1629 | const webrtc::PacketReceiver::DeliveryStatus delivery_result = |
| 1630 | call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, packet, |
| 1631 | packet_time_us); |
| 1632 | switch (delivery_result) { |
| 1633 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1634 | return; |
| 1635 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1636 | return; |
| 1637 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1638 | break; |
| 1639 | } |
| 1640 |
| 1641 | uint32_t ssrc = 0; |
| 1642 | if (!GetRtpSsrc(packet.cdata(), packet.size(), &ssrc)) { |
| 1643 | return; |
| 1644 | } |
| 1645 |
| 1646 | if (unknown_ssrc_packet_buffer_) { |
| 1647 | unknown_ssrc_packet_buffer_->AddPacket(ssrc, packet_time_us, packet); |
| 1648 | return; |
| 1649 | } |
| 1650 |
| 1651 | if (discard_unknown_ssrc_packets_) { |
| 1652 | return; |
| 1653 | } |
| 1654 |
| 1655 | int payload_type = 0; |
| 1656 | if (!GetRtpPayloadType(packet.cdata(), packet.size(), &payload_type)) { |
| 1657 | return; |
| 1658 | } |
| 1659 |
| 1660 | // See if this payload_type is registered as one that usually gets its own |
| 1661 | // SSRC (RTX) or at least is safe to drop either way (FEC). If it is, and |
| 1662 | // it wasn't handled above by DeliverPacket, that means we don't know what |
| 1663 | // stream it associates with, and we shouldn't ever create an implicit channel |
| 1664 | // for these. |
| 1665 | for (auto& codec : recv_codecs_) { |
| 1666 | if (payload_type == codec.rtx_payload_type || |
| 1667 | payload_type == codec.ulpfec.red_rtx_payload_type || |
| 1668 | payload_type == codec.ulpfec.ulpfec_payload_type) { |
| 1669 | return; |
| 1670 | } |
| 1671 | } |
| 1672 | if (payload_type == recv_flexfec_payload_type_) { |
| 1673 | return; |
| 1674 | } |
| 1675 |
| 1676 | switch (unsignalled_ssrc_handler_->OnUnsignalledSsrc(this, ssrc)) { |
| 1677 | case UnsignalledSsrcHandler::kDropPacket: |
| 1678 | return; |
| 1679 | case UnsignalledSsrcHandler::kDeliverPacket: |
| 1680 | break; |
| 1681 | } |
| 1682 |
| 1683 | if (call_->Receiver()->DeliverPacket(webrtc::MediaType::VIDEO, packet, |
| 1684 | packet_time_us) != |
| 1685 | webrtc::PacketReceiver::DELIVERY_OK) { |
| 1686 | RTC_LOG(LS_WARNING) << "Failed to deliver RTP packet on re-delivery."; |
| 1687 | return; |
| 1688 | } |
| 1689 | } |
| 1690 |
| 1691 | void WebRtcVideoChannel::BackfillBufferedPackets( |
| 1692 | rtc::ArrayView<const uint32_t> ssrcs) { |
| 1693 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1694 | if (!unknown_ssrc_packet_buffer_) { |
| 1695 | return; |
| 1696 | } |
| 1697 |
| 1698 | int delivery_ok_cnt = 0; |
| 1699 | int delivery_unknown_ssrc_cnt = 0; |
| 1700 | int delivery_packet_error_cnt = 0; |
| 1701 | webrtc::PacketReceiver* receiver = this->call_->Receiver(); |
| 1702 | unknown_ssrc_packet_buffer_->BackfillPackets( |
| 1703 | ssrcs, [&](uint32_t ssrc, int64_t packet_time_us, |
| 1704 | rtc::CopyOnWriteBuffer packet) { |
| 1705 | switch (receiver->DeliverPacket(webrtc::MediaType::VIDEO, packet, |
| 1706 | packet_time_us)) { |
| 1707 | case webrtc::PacketReceiver::DELIVERY_OK: |
| 1708 | delivery_ok_cnt++; |
| 1709 | break; |
| 1710 | case webrtc::PacketReceiver::DELIVERY_UNKNOWN_SSRC: |
| 1711 | delivery_unknown_ssrc_cnt++; |
| 1712 | break; |
| 1713 | case webrtc::PacketReceiver::DELIVERY_PACKET_ERROR: |
| 1714 | delivery_packet_error_cnt++; |
| 1715 | break; |
| 1716 | } |
| 1717 | }); |
| 1718 | rtc::StringBuilder out; |
| 1719 | out << "[ "; |
| 1720 | for (uint32_t ssrc : ssrcs) { |
| 1721 | out << std::to_string(ssrc) << " "; |
| 1722 | } |
| 1723 | out << "]"; |
| 1724 | auto level = rtc::LS_INFO; |
| 1725 | if (delivery_unknown_ssrc_cnt > 0 || delivery_packet_error_cnt > 0) { |
| 1726 | level = rtc::LS_ERROR; |
| 1727 | } |
| 1728 | int total = |
| 1729 | delivery_ok_cnt + delivery_unknown_ssrc_cnt + delivery_packet_error_cnt; |
| 1730 | RTC_LOG_V(level) << "Backfilled " << total |
| 1731 | << " packets for ssrcs: " << out.Release() |
| 1732 | << " ok: " << delivery_ok_cnt |
| 1733 | << " error: " << delivery_packet_error_cnt |
| 1734 | << " unknown: " << delivery_unknown_ssrc_cnt; |
| 1735 | } |
| 1736 |
| 1737 | void WebRtcVideoChannel::OnReadyToSend(bool ready) { |
| 1738 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1739 | RTC_LOG(LS_VERBOSE) << "OnReadyToSend: " << (ready ? "Ready." : "Not ready."); |
| 1740 | call_->SignalChannelNetworkState( |
| 1741 | webrtc::MediaType::VIDEO, |
| 1742 | ready ? webrtc::kNetworkUp : webrtc::kNetworkDown); |
| 1743 | } |
| 1744 |
| 1745 | void WebRtcVideoChannel::OnNetworkRouteChanged( |
| 1746 | const std::string& transport_name, |
| 1747 | const rtc::NetworkRoute& network_route) { |
| 1748 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1749 | call_->GetTransportControllerSend()->OnNetworkRouteChanged(transport_name, |
| 1750 | network_route); |
| 1751 | call_->GetTransportControllerSend()->OnTransportOverheadChanged( |
| 1752 | network_route.packet_overhead); |
| 1753 | } |
| 1754 |
| 1755 | void WebRtcVideoChannel::SetInterface(NetworkInterface* iface) { |
| 1756 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1757 | MediaChannel::SetInterface(iface); |
| 1758 | // Set the RTP recv/send buffer to a bigger size. |
| 1759 |
| 1760 | // The group should be a positive integer with an explicit size, in |
| 1761 | // which case that is used as UDP recevie buffer size. All other values shall |
| 1762 | // result in the default value being used. |
| 1763 | const std::string group_name = |
| 1764 | webrtc::field_trial::FindFullName("WebRTC-IncreasedReceivebuffers"); |
| 1765 | int recv_buffer_size = kVideoRtpRecvBufferSize; |
| 1766 | if (!group_name.empty() && |
| 1767 | (sscanf(group_name.c_str(), "%d", &recv_buffer_size) != 1 || |
| 1768 | recv_buffer_size <= 0)) { |
| 1769 | RTC_LOG(LS_WARNING) << "Invalid receive buffer size: " << group_name; |
| 1770 | recv_buffer_size = kVideoRtpRecvBufferSize; |
| 1771 | } |
| 1772 |
| 1773 | MediaChannel::SetOption(NetworkInterface::ST_RTP, rtc::Socket::OPT_RCVBUF, |
| 1774 | recv_buffer_size); |
| 1775 |
| 1776 | // Speculative change to increase the outbound socket buffer size. |
| 1777 | // In b/15152257, we are seeing a significant number of packets discarded |
| 1778 | // due to lack of socket buffer space, although it's not yet clear what the |
| 1779 | // ideal value should be. |
| 1780 | MediaChannel::SetOption(NetworkInterface::ST_RTP, rtc::Socket::OPT_SNDBUF, |
| 1781 | kVideoRtpSendBufferSize); |
| 1782 | } |
| 1783 |
| 1784 | void WebRtcVideoChannel::SetFrameDecryptor( |
| 1785 | uint32_t ssrc, |
| 1786 | rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) { |
| 1787 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1788 | auto matching_stream = receive_streams_.find(ssrc); |
| 1789 | if (matching_stream != receive_streams_.end()) { |
| 1790 | matching_stream->second->SetFrameDecryptor(frame_decryptor); |
| 1791 | } |
| 1792 | } |
| 1793 |
| 1794 | void WebRtcVideoChannel::SetFrameEncryptor( |
| 1795 | uint32_t ssrc, |
| 1796 | rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor) { |
| 1797 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1798 | auto matching_stream = send_streams_.find(ssrc); |
| 1799 | if (matching_stream != send_streams_.end()) { |
| 1800 | matching_stream->second->SetFrameEncryptor(frame_encryptor); |
| 1801 | } else { |
| 1802 | RTC_LOG(LS_ERROR) << "No stream found to attach frame encryptor"; |
| 1803 | } |
| 1804 | } |
| 1805 |
| 1806 | void WebRtcVideoChannel::SetVideoCodecSwitchingEnabled(bool enabled) { |
| 1807 | invoker_.AsyncInvoke<void>(RTC_FROM_HERE, worker_thread_, [this, enabled] { |
| 1808 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1809 | allow_codec_switching_ = enabled; |
| 1810 | if (allow_codec_switching_) { |
| 1811 | RTC_LOG(LS_INFO) << "Encoder switching enabled."; |
| 1812 | if (requested_encoder_switch_) { |
| 1813 | RTC_LOG(LS_INFO) << "Executing cached video encoder switch request."; |
| 1814 | RequestEncoderSwitch(*requested_encoder_switch_); |
| 1815 | requested_encoder_switch_.reset(); |
| 1816 | } |
| 1817 | } |
| 1818 | }); |
| 1819 | } |
| 1820 |
| 1821 | bool WebRtcVideoChannel::SetBaseMinimumPlayoutDelayMs(uint32_t ssrc, |
| 1822 | int delay_ms) { |
| 1823 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1824 | absl::optional<uint32_t> default_ssrc = GetDefaultReceiveStreamSsrc(); |
| 1825 |
| 1826 | // SSRC of 0 represents the default receive stream. |
| 1827 | if (ssrc == 0) { |
| 1828 | default_recv_base_minimum_delay_ms_ = delay_ms; |
| 1829 | } |
| 1830 |
| 1831 | if (ssrc == 0 && !default_ssrc) { |
| 1832 | return true; |
| 1833 | } |
| 1834 |
| 1835 | if (ssrc == 0 && default_ssrc) { |
| 1836 | ssrc = default_ssrc.value(); |
| 1837 | } |
| 1838 |
| 1839 | auto stream = receive_streams_.find(ssrc); |
| 1840 | if (stream != receive_streams_.end()) { |
| 1841 | stream->second->SetBaseMinimumPlayoutDelayMs(delay_ms); |
| 1842 | return true; |
| 1843 | } else { |
| 1844 | RTC_LOG(LS_ERROR) << "No stream found to set base minimum playout delay"; |
| 1845 | return false; |
| 1846 | } |
| 1847 | } |
| 1848 |
| 1849 | absl::optional<int> WebRtcVideoChannel::GetBaseMinimumPlayoutDelayMs( |
| 1850 | uint32_t ssrc) const { |
| 1851 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1852 | // SSRC of 0 represents the default receive stream. |
| 1853 | if (ssrc == 0) { |
| 1854 | return default_recv_base_minimum_delay_ms_; |
| 1855 | } |
| 1856 |
| 1857 | auto stream = receive_streams_.find(ssrc); |
| 1858 | if (stream != receive_streams_.end()) { |
| 1859 | return stream->second->GetBaseMinimumPlayoutDelayMs(); |
| 1860 | } else { |
| 1861 | RTC_LOG(LS_ERROR) << "No stream found to get base minimum playout delay"; |
| 1862 | return absl::nullopt; |
| 1863 | } |
| 1864 | } |
| 1865 |
| 1866 | absl::optional<uint32_t> WebRtcVideoChannel::GetDefaultReceiveStreamSsrc() { |
| 1867 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1868 | absl::optional<uint32_t> ssrc; |
| 1869 | for (auto it = receive_streams_.begin(); it != receive_streams_.end(); ++it) { |
| 1870 | if (it->second->IsDefaultStream()) { |
| 1871 | ssrc.emplace(it->first); |
| 1872 | break; |
| 1873 | } |
| 1874 | } |
| 1875 | return ssrc; |
| 1876 | } |
| 1877 |
| 1878 | std::vector<webrtc::RtpSource> WebRtcVideoChannel::GetSources( |
| 1879 | uint32_t ssrc) const { |
| 1880 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 1881 | auto it = receive_streams_.find(ssrc); |
| 1882 | if (it == receive_streams_.end()) { |
| 1883 | // TODO(bugs.webrtc.org/9781): Investigate standard compliance |
| 1884 | // with sources for streams that has been removed. |
| 1885 | RTC_LOG(LS_ERROR) << "Attempting to get contributing sources for SSRC:" |
| 1886 | << ssrc << " which doesn't exist."; |
| 1887 | return {}; |
| 1888 | } |
| 1889 | return it->second->GetSources(); |
| 1890 | } |
| 1891 |
| 1892 | bool WebRtcVideoChannel::SendRtp(const uint8_t* data, |
| 1893 | size_t len, |
| 1894 | const webrtc::PacketOptions& options) { |
| 1895 | rtc::CopyOnWriteBuffer packet(data, len, kMaxRtpPacketLen); |
| 1896 | rtc::PacketOptions rtc_options; |
| 1897 | rtc_options.packet_id = options.packet_id; |
| 1898 | if (DscpEnabled()) { |
| 1899 | rtc_options.dscp = PreferredDscp(); |
| 1900 | } |
| 1901 | rtc_options.info_signaled_after_sent.included_in_feedback = |
| 1902 | options.included_in_feedback; |
| 1903 | rtc_options.info_signaled_after_sent.included_in_allocation = |
| 1904 | options.included_in_allocation; |
| 1905 | return MediaChannel::SendPacket(&packet, rtc_options); |
| 1906 | } |
| 1907 |
| 1908 | bool WebRtcVideoChannel::SendRtcp(const uint8_t* data, size_t len) { |
| 1909 | rtc::CopyOnWriteBuffer packet(data, len, kMaxRtpPacketLen); |
| 1910 | rtc::PacketOptions rtc_options; |
| 1911 | if (DscpEnabled()) { |
| 1912 | rtc_options.dscp = PreferredDscp(); |
| 1913 | } |
| 1914 |
| 1915 | return MediaChannel::SendRtcp(&packet, rtc_options); |
| 1916 | } |
| 1917 |
| 1918 | WebRtcVideoChannel::WebRtcVideoSendStream::VideoSendStreamParameters:: |
| 1919 | VideoSendStreamParameters( |
| 1920 | webrtc::VideoSendStream::Config config, |
| 1921 | const VideoOptions& options, |
| 1922 | int max_bitrate_bps, |
| 1923 | const absl::optional<VideoCodecSettings>& codec_settings) |
| 1924 | : config(std::move(config)), |
| 1925 | options(options), |
| 1926 | max_bitrate_bps(max_bitrate_bps), |
| 1927 | conference_mode(false), |
| 1928 | codec_settings(codec_settings) {} |
| 1929 |
| 1930 | WebRtcVideoChannel::WebRtcVideoSendStream::WebRtcVideoSendStream( |
| 1931 | webrtc::Call* call, |
| 1932 | const StreamParams& sp, |
| 1933 | webrtc::VideoSendStream::Config config, |
| 1934 | const VideoOptions& options, |
| 1935 | bool enable_cpu_overuse_detection, |
| 1936 | int max_bitrate_bps, |
| 1937 | const absl::optional<VideoCodecSettings>& codec_settings, |
| 1938 | const absl::optional<std::vector<webrtc::RtpExtension>>& rtp_extensions, |
| 1939 | // TODO(deadbeef): Don't duplicate information between send_params, |
| 1940 | // rtp_extensions, options, etc. |
| 1941 | const VideoSendParameters& send_params) |
| 1942 | : worker_thread_(rtc::Thread::Current()), |
| 1943 | ssrcs_(sp.ssrcs), |
| 1944 | ssrc_groups_(sp.ssrc_groups), |
| 1945 | call_(call), |
| 1946 | enable_cpu_overuse_detection_(enable_cpu_overuse_detection), |
| 1947 | source_(nullptr), |
| 1948 | stream_(nullptr), |
| 1949 | encoder_sink_(nullptr), |
| 1950 | parameters_(std::move(config), options, max_bitrate_bps, codec_settings), |
| 1951 | rtp_parameters_(CreateRtpParametersWithEncodings(sp)), |
| 1952 | sending_(false), |
| 1953 | disable_automatic_resize_(webrtc::field_trial::IsEnabled( |
| 1954 | "WebRTC-Video-DisableAutomaticResize")) { |
| 1955 | // Maximum packet size may come in RtpConfig from external transport, for |
| 1956 | // example from QuicTransportInterface implementation, so do not exceed |
| 1957 | // given max_packet_size. |
| 1958 | parameters_.config.rtp.max_packet_size = |
| 1959 | std::min<size_t>(parameters_.config.rtp.max_packet_size, kVideoMtu); |
| 1960 | parameters_.conference_mode = send_params.conference_mode; |
| 1961 |
| 1962 | sp.GetPrimarySsrcs(¶meters_.config.rtp.ssrcs); |
| 1963 |
| 1964 | // ValidateStreamParams should prevent this from happening. |
| 1965 | RTC_CHECK(!parameters_.config.rtp.ssrcs.empty()); |
| 1966 | rtp_parameters_.encodings[0].ssrc = parameters_.config.rtp.ssrcs[0]; |
| 1967 |
| 1968 | // RTX. |
| 1969 | sp.GetFidSsrcs(parameters_.config.rtp.ssrcs, |
| 1970 | ¶meters_.config.rtp.rtx.ssrcs); |
| 1971 |
| 1972 | // FlexFEC SSRCs. |
| 1973 | // TODO(brandtr): This code needs to be generalized when we add support for |
| 1974 | // multistream protection. |
| 1975 | if (IsFlexfecFieldTrialEnabled()) { |
| 1976 | uint32_t flexfec_ssrc; |
| 1977 | bool flexfec_enabled = false; |
| 1978 | for (uint32_t primary_ssrc : parameters_.config.rtp.ssrcs) { |
| 1979 | if (sp.GetFecFrSsrc(primary_ssrc, &flexfec_ssrc)) { |
| 1980 | if (flexfec_enabled) { |
| 1981 | RTC_LOG(LS_INFO) |
| 1982 | << "Multiple FlexFEC streams in local SDP, but " |
| 1983 | "our implementation only supports a single FlexFEC " |
| 1984 | "stream. Will not enable FlexFEC for proposed " |
| 1985 | "stream with SSRC: " |
| 1986 | << flexfec_ssrc << "."; |
| 1987 | continue; |
| 1988 | } |
| 1989 |
| 1990 | flexfec_enabled = true; |
| 1991 | parameters_.config.rtp.flexfec.ssrc = flexfec_ssrc; |
| 1992 | parameters_.config.rtp.flexfec.protected_media_ssrcs = {primary_ssrc}; |
| 1993 | } |
| 1994 | } |
| 1995 | } |
| 1996 |
| 1997 | parameters_.config.rtp.c_name = sp.cname; |
| 1998 | if (rtp_extensions) { |
| 1999 | parameters_.config.rtp.extensions = *rtp_extensions; |
| 2000 | rtp_parameters_.header_extensions = *rtp_extensions; |
| 2001 | } |
| 2002 | parameters_.config.rtp.rtcp_mode = send_params.rtcp.reduced_size |
| 2003 | ? webrtc::RtcpMode::kReducedSize |
| 2004 | : webrtc::RtcpMode::kCompound; |
| 2005 | parameters_.config.rtp.mid = send_params.mid; |
| 2006 | rtp_parameters_.rtcp.reduced_size = send_params.rtcp.reduced_size; |
| 2007 |
| 2008 | if (codec_settings) { |
| 2009 | SetCodec(*codec_settings); |
| 2010 | } |
| 2011 | } |
| 2012 |
| 2013 | WebRtcVideoChannel::WebRtcVideoSendStream::~WebRtcVideoSendStream() { |
| 2014 | if (stream_ != NULL) { |
| 2015 | call_->DestroyVideoSendStream(stream_); |
| 2016 | } |
| 2017 | } |
| 2018 |
| 2019 | bool WebRtcVideoChannel::WebRtcVideoSendStream::SetVideoSend( |
| 2020 | const VideoOptions* options, |
| 2021 | rtc::VideoSourceInterface<webrtc::VideoFrame>* source) { |
| 2022 | TRACE_EVENT0("webrtc", "WebRtcVideoSendStream::SetVideoSend"); |
| 2023 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2024 |
| 2025 | if (options) { |
| 2026 | VideoOptions old_options = parameters_.options; |
| 2027 | parameters_.options.SetAll(*options); |
| 2028 | if (parameters_.options.is_screencast.value_or(false) != |
| 2029 | old_options.is_screencast.value_or(false) && |
| 2030 | parameters_.codec_settings) { |
| 2031 | // If screen content settings change, we may need to recreate the codec |
| 2032 | // instance so that the correct type is used. |
| 2033 |
| 2034 | SetCodec(*parameters_.codec_settings); |
| 2035 | // Mark screenshare parameter as being updated, then test for any other |
| 2036 | // changes that may require codec reconfiguration. |
| 2037 | old_options.is_screencast = options->is_screencast; |
| 2038 | } |
| 2039 | if (parameters_.options != old_options) { |
| 2040 | ReconfigureEncoder(); |
| 2041 | } |
| 2042 | } |
| 2043 |
| 2044 | if (source_ && stream_) { |
| 2045 | stream_->SetSource(nullptr, webrtc::DegradationPreference::DISABLED); |
| 2046 | } |
| 2047 | // Switch to the new source. |
| 2048 | source_ = source; |
| 2049 | if (source && stream_) { |
| 2050 | stream_->SetSource(this, GetDegradationPreference()); |
| 2051 | } |
| 2052 | return true; |
| 2053 | } |
| 2054 |
| 2055 | webrtc::DegradationPreference |
| 2056 | WebRtcVideoChannel::WebRtcVideoSendStream::GetDegradationPreference() const { |
| 2057 | // Do not adapt resolution for screen content as this will likely |
| 2058 | // result in blurry and unreadable text. |
| 2059 | // |this| acts like a VideoSource to make sure SinkWants are handled on the |
| 2060 | // correct thread. |
| 2061 | if (!enable_cpu_overuse_detection_) { |
| 2062 | return webrtc::DegradationPreference::DISABLED; |
| 2063 | } |
| 2064 |
| 2065 | webrtc::DegradationPreference degradation_preference; |
| 2066 | if (rtp_parameters_.degradation_preference.has_value()) { |
| 2067 | degradation_preference = *rtp_parameters_.degradation_preference; |
| 2068 | } else { |
| 2069 | if (parameters_.options.content_hint == |
| 2070 | webrtc::VideoTrackInterface::ContentHint::kFluid) { |
| 2071 | degradation_preference = |
| 2072 | webrtc::DegradationPreference::MAINTAIN_FRAMERATE; |
| 2073 | } else if (parameters_.options.is_screencast.value_or(false) || |
| 2074 | parameters_.options.content_hint == |
| 2075 | webrtc::VideoTrackInterface::ContentHint::kDetailed || |
| 2076 | parameters_.options.content_hint == |
| 2077 | webrtc::VideoTrackInterface::ContentHint::kText) { |
| 2078 | degradation_preference = |
| 2079 | webrtc::DegradationPreference::MAINTAIN_RESOLUTION; |
| 2080 | } else if (webrtc::field_trial::IsEnabled( |
| 2081 | "WebRTC-Video-BalancedDegradation")) { |
| 2082 | // Standard wants balanced by default, but it needs to be tuned first. |
| 2083 | degradation_preference = webrtc::DegradationPreference::BALANCED; |
| 2084 | } else { |
| 2085 | // Keep MAINTAIN_FRAMERATE by default until BALANCED has been tuned for |
| 2086 | // all codecs and launched. |
| 2087 | degradation_preference = |
| 2088 | webrtc::DegradationPreference::MAINTAIN_FRAMERATE; |
| 2089 | } |
| 2090 | } |
| 2091 |
| 2092 | return degradation_preference; |
| 2093 | } |
| 2094 |
| 2095 | const std::vector<uint32_t>& |
| 2096 | WebRtcVideoChannel::WebRtcVideoSendStream::GetSsrcs() const { |
| 2097 | return ssrcs_; |
| 2098 | } |
| 2099 |
| 2100 | void WebRtcVideoChannel::WebRtcVideoSendStream::SetCodec( |
| 2101 | const VideoCodecSettings& codec_settings) { |
| 2102 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2103 | parameters_.encoder_config = CreateVideoEncoderConfig(codec_settings.codec); |
| 2104 | RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0); |
| 2105 |
| 2106 | parameters_.config.rtp.payload_name = codec_settings.codec.name; |
| 2107 | parameters_.config.rtp.payload_type = codec_settings.codec.id; |
| 2108 | parameters_.config.rtp.raw_payload = |
| 2109 | codec_settings.codec.packetization == kPacketizationParamRaw; |
| 2110 | parameters_.config.rtp.ulpfec = codec_settings.ulpfec; |
| 2111 | parameters_.config.rtp.flexfec.payload_type = |
| 2112 | codec_settings.flexfec_payload_type; |
| 2113 |
| 2114 | // Set RTX payload type if RTX is enabled. |
| 2115 | if (!parameters_.config.rtp.rtx.ssrcs.empty()) { |
| 2116 | if (codec_settings.rtx_payload_type == -1) { |
| 2117 | RTC_LOG(LS_WARNING) |
| 2118 | << "RTX SSRCs configured but there's no configured RTX " |
| 2119 | "payload type. Ignoring."; |
| 2120 | parameters_.config.rtp.rtx.ssrcs.clear(); |
| 2121 | } else { |
| 2122 | parameters_.config.rtp.rtx.payload_type = codec_settings.rtx_payload_type; |
| 2123 | } |
| 2124 | } |
| 2125 |
| 2126 | const bool has_lntf = HasLntf(codec_settings.codec); |
| 2127 | parameters_.config.rtp.lntf.enabled = has_lntf; |
| 2128 | parameters_.config.encoder_settings.capabilities.loss_notification = has_lntf; |
| 2129 |
| 2130 | parameters_.config.rtp.nack.rtp_history_ms = |
| 2131 | HasNack(codec_settings.codec) ? kNackHistoryMs : 0; |
| 2132 |
| 2133 | parameters_.codec_settings = codec_settings; |
| 2134 |
| 2135 | // TODO(nisse): Avoid recreation, it should be enough to call |
| 2136 | // ReconfigureEncoder. |
| 2137 | RTC_LOG(LS_INFO) << "RecreateWebRtcStream (send) because of SetCodec."; |
| 2138 | RecreateWebRtcStream(); |
| 2139 | } |
| 2140 |
| 2141 | void WebRtcVideoChannel::WebRtcVideoSendStream::SetSendParameters( |
| 2142 | const ChangedSendParameters& params) { |
| 2143 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2144 | // |recreate_stream| means construction-time parameters have changed and the |
| 2145 | // sending stream needs to be reset with the new config. |
| 2146 | bool recreate_stream = false; |
| 2147 | if (params.rtcp_mode) { |
| 2148 | parameters_.config.rtp.rtcp_mode = *params.rtcp_mode; |
| 2149 | rtp_parameters_.rtcp.reduced_size = |
| 2150 | parameters_.config.rtp.rtcp_mode == webrtc::RtcpMode::kReducedSize; |
| 2151 | recreate_stream = true; |
| 2152 | } |
| 2153 | if (params.extmap_allow_mixed) { |
| 2154 | parameters_.config.rtp.extmap_allow_mixed = *params.extmap_allow_mixed; |
| 2155 | recreate_stream = true; |
| 2156 | } |
| 2157 | if (params.rtp_header_extensions) { |
| 2158 | parameters_.config.rtp.extensions = *params.rtp_header_extensions; |
| 2159 | rtp_parameters_.header_extensions = *params.rtp_header_extensions; |
| 2160 | recreate_stream = true; |
| 2161 | } |
| 2162 | if (params.mid) { |
| 2163 | parameters_.config.rtp.mid = *params.mid; |
| 2164 | recreate_stream = true; |
| 2165 | } |
| 2166 | if (params.max_bandwidth_bps) { |
| 2167 | parameters_.max_bitrate_bps = *params.max_bandwidth_bps; |
| 2168 | ReconfigureEncoder(); |
| 2169 | } |
| 2170 | if (params.conference_mode) { |
| 2171 | parameters_.conference_mode = *params.conference_mode; |
| 2172 | } |
| 2173 |
| 2174 | // Set codecs and options. |
| 2175 | if (params.send_codec) { |
| 2176 | SetCodec(*params.send_codec); |
| 2177 | recreate_stream = false; // SetCodec has already recreated the stream. |
| 2178 | } else if (params.conference_mode && parameters_.codec_settings) { |
| 2179 | SetCodec(*parameters_.codec_settings); |
| 2180 | recreate_stream = false; // SetCodec has already recreated the stream. |
| 2181 | } |
| 2182 | if (recreate_stream) { |
| 2183 | RTC_LOG(LS_INFO) |
| 2184 | << "RecreateWebRtcStream (send) because of SetSendParameters"; |
| 2185 | RecreateWebRtcStream(); |
| 2186 | } |
| 2187 | } |
| 2188 |
| 2189 | webrtc::RTCError WebRtcVideoChannel::WebRtcVideoSendStream::SetRtpParameters( |
| 2190 | const webrtc::RtpParameters& new_parameters) { |
| 2191 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2192 | webrtc::RTCError error = CheckRtpParametersInvalidModificationAndValues( |
| 2193 | rtp_parameters_, new_parameters); |
| 2194 | if (!error.ok()) { |
| 2195 | return error; |
| 2196 | } |
| 2197 |
| 2198 | bool new_param = false; |
| 2199 | for (size_t i = 0; i < rtp_parameters_.encodings.size(); ++i) { |
| 2200 | if ((new_parameters.encodings[i].min_bitrate_bps != |
| 2201 | rtp_parameters_.encodings[i].min_bitrate_bps) || |
| 2202 | (new_parameters.encodings[i].max_bitrate_bps != |
| 2203 | rtp_parameters_.encodings[i].max_bitrate_bps) || |
| 2204 | (new_parameters.encodings[i].max_framerate != |
| 2205 | rtp_parameters_.encodings[i].max_framerate) || |
| 2206 | (new_parameters.encodings[i].scale_resolution_down_by != |
| 2207 | rtp_parameters_.encodings[i].scale_resolution_down_by) || |
| 2208 | (new_parameters.encodings[i].num_temporal_layers != |
| 2209 | rtp_parameters_.encodings[i].num_temporal_layers)) { |
| 2210 | new_param = true; |
| 2211 | break; |
| 2212 | } |
| 2213 | } |
| 2214 |
| 2215 | bool new_degradation_preference = false; |
| 2216 | if (new_parameters.degradation_preference != |
| 2217 | rtp_parameters_.degradation_preference) { |
| 2218 | new_degradation_preference = true; |
| 2219 | } |
| 2220 |
| 2221 | // TODO(bugs.webrtc.org/8807): The bitrate priority really doesn't require an |
| 2222 | // entire encoder reconfiguration, it just needs to update the bitrate |
| 2223 | // allocator. |
| 2224 | bool reconfigure_encoder = |
| 2225 | new_param || (new_parameters.encodings[0].bitrate_priority != |
| 2226 | rtp_parameters_.encodings[0].bitrate_priority); |
| 2227 |
| 2228 | // TODO(bugs.webrtc.org/8807): The active field as well should not require |
| 2229 | // a full encoder reconfiguration, but it needs to update both the bitrate |
| 2230 | // allocator and the video bitrate allocator. |
| 2231 | bool new_send_state = false; |
| 2232 | for (size_t i = 0; i < rtp_parameters_.encodings.size(); ++i) { |
| 2233 | bool new_active = IsLayerActive(new_parameters.encodings[i]); |
| 2234 | bool old_active = IsLayerActive(rtp_parameters_.encodings[i]); |
| 2235 | if (new_active != old_active) { |
| 2236 | new_send_state = true; |
| 2237 | } |
| 2238 | } |
| 2239 | rtp_parameters_ = new_parameters; |
| 2240 | // Codecs are currently handled at the WebRtcVideoChannel level. |
| 2241 | rtp_parameters_.codecs.clear(); |
| 2242 | if (reconfigure_encoder || new_send_state) { |
| 2243 | ReconfigureEncoder(); |
| 2244 | } |
| 2245 | if (new_send_state) { |
| 2246 | UpdateSendState(); |
| 2247 | } |
| 2248 | if (new_degradation_preference) { |
| 2249 | if (source_ && stream_) { |
| 2250 | stream_->SetSource(this, GetDegradationPreference()); |
| 2251 | } |
| 2252 | } |
| 2253 | return webrtc::RTCError::OK(); |
| 2254 | } |
| 2255 |
| 2256 | webrtc::RtpParameters |
| 2257 | WebRtcVideoChannel::WebRtcVideoSendStream::GetRtpParameters() const { |
| 2258 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2259 | return rtp_parameters_; |
| 2260 | } |
| 2261 |
| 2262 | void WebRtcVideoChannel::WebRtcVideoSendStream::SetFrameEncryptor( |
| 2263 | rtc::scoped_refptr<webrtc::FrameEncryptorInterface> frame_encryptor) { |
| 2264 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2265 | parameters_.config.frame_encryptor = frame_encryptor; |
| 2266 | if (stream_) { |
| 2267 | RTC_LOG(LS_INFO) |
| 2268 | << "RecreateWebRtcStream (send) because of SetFrameEncryptor, ssrc=" |
| 2269 | << parameters_.config.rtp.ssrcs[0]; |
| 2270 | RecreateWebRtcStream(); |
| 2271 | } |
| 2272 | } |
| 2273 |
| 2274 | void WebRtcVideoChannel::WebRtcVideoSendStream::UpdateSendState() { |
| 2275 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2276 | if (sending_) { |
| 2277 | RTC_DCHECK(stream_ != nullptr); |
| 2278 | size_t num_layers = rtp_parameters_.encodings.size(); |
| 2279 | if (parameters_.encoder_config.number_of_streams == 1) { |
| 2280 | // SVC is used. Only one simulcast layer is present. |
| 2281 | num_layers = 1; |
| 2282 | } |
| 2283 | std::vector<bool> active_layers(num_layers); |
| 2284 | for (size_t i = 0; i < num_layers; ++i) { |
| 2285 | active_layers[i] = IsLayerActive(rtp_parameters_.encodings[i]); |
| 2286 | } |
| 2287 | if (parameters_.encoder_config.number_of_streams == 1 && |
| 2288 | rtp_parameters_.encodings.size() > 1) { |
| 2289 | // SVC is used. |
| 2290 | // The only present simulcast layer should be active if any of the |
| 2291 | // configured SVC layers is active. |
| 2292 | active_layers[0] = |
| 2293 | absl::c_any_of(rtp_parameters_.encodings, |
| 2294 | [](const auto& encoding) { return encoding.active; }); |
| 2295 | } |
| 2296 | // This updates what simulcast layers are sending, and possibly starts |
| 2297 | // or stops the VideoSendStream. |
| 2298 | stream_->UpdateActiveSimulcastLayers(active_layers); |
| 2299 | } else { |
| 2300 | if (stream_ != nullptr) { |
| 2301 | stream_->Stop(); |
| 2302 | } |
| 2303 | } |
| 2304 | } |
| 2305 |
| 2306 | webrtc::VideoEncoderConfig |
| 2307 | WebRtcVideoChannel::WebRtcVideoSendStream::CreateVideoEncoderConfig( |
| 2308 | const VideoCodec& codec) const { |
| 2309 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2310 | webrtc::VideoEncoderConfig encoder_config; |
| 2311 | encoder_config.codec_type = webrtc::PayloadStringToCodecType(codec.name); |
| 2312 | encoder_config.video_format = |
| 2313 | webrtc::SdpVideoFormat(codec.name, codec.params); |
| 2314 |
| 2315 | bool is_screencast = parameters_.options.is_screencast.value_or(false); |
| 2316 | if (is_screencast) { |
| 2317 | encoder_config.min_transmit_bitrate_bps = |
| 2318 | 1000 * parameters_.options.screencast_min_bitrate_kbps.value_or(0); |
| 2319 | encoder_config.content_type = |
| 2320 | webrtc::VideoEncoderConfig::ContentType::kScreen; |
| 2321 | } else { |
| 2322 | encoder_config.min_transmit_bitrate_bps = 0; |
| 2323 | encoder_config.content_type = |
| 2324 | webrtc::VideoEncoderConfig::ContentType::kRealtimeVideo; |
| 2325 | } |
| 2326 |
| 2327 | // By default, the stream count for the codec configuration should match the |
| 2328 | // number of negotiated ssrcs. But if the codec is disabled for simulcast |
| 2329 | // or a screencast (and not in simulcast screenshare experiment), only |
| 2330 | // configure a single stream. |
| 2331 | encoder_config.number_of_streams = parameters_.config.rtp.ssrcs.size(); |
| 2332 | if (IsCodecDisabledForSimulcast(codec.name)) { |
| 2333 | encoder_config.number_of_streams = 1; |
| 2334 | } |
| 2335 |
| 2336 | // parameters_.max_bitrate comes from the max bitrate set at the SDP |
| 2337 | // (m-section) level with the attribute "b=AS." Note that we override this |
| 2338 | // value below if the RtpParameters max bitrate set with |
| 2339 | // RtpSender::SetParameters has a lower value. |
| 2340 | int stream_max_bitrate = parameters_.max_bitrate_bps; |
| 2341 | // When simulcast is enabled (when there are multiple encodings), |
| 2342 | // encodings[i].max_bitrate_bps will be enforced by |
| 2343 | // encoder_config.simulcast_layers[i].max_bitrate_bps. Otherwise, it's |
| 2344 | // enforced by stream_max_bitrate, taking the minimum of the two maximums |
| 2345 | // (one coming from SDP, the other coming from RtpParameters). |
| 2346 | if (rtp_parameters_.encodings[0].max_bitrate_bps && |
| 2347 | rtp_parameters_.encodings.size() == 1) { |
| 2348 | stream_max_bitrate = |
| 2349 | MinPositive(*(rtp_parameters_.encodings[0].max_bitrate_bps), |
| 2350 | parameters_.max_bitrate_bps); |
| 2351 | } |
| 2352 |
| 2353 | // The codec max bitrate comes from the "x-google-max-bitrate" parameter |
| 2354 | // attribute set in the SDP for a specific codec. As done in |
| 2355 | // WebRtcVideoChannel::SetSendParameters, this value does not override the |
| 2356 | // stream max_bitrate set above. |
| 2357 | int codec_max_bitrate_kbps; |
| 2358 | if (codec.GetParam(kCodecParamMaxBitrate, &codec_max_bitrate_kbps) && |
| 2359 | stream_max_bitrate == -1) { |
| 2360 | stream_max_bitrate = codec_max_bitrate_kbps * 1000; |
| 2361 | } |
| 2362 | encoder_config.max_bitrate_bps = stream_max_bitrate; |
| 2363 |
| 2364 | // The encoder config's default bitrate priority is set to 1.0, |
| 2365 | // unless it is set through the sender's encoding parameters. |
| 2366 | // The bitrate priority, which is used in the bitrate allocation, is done |
| 2367 | // on a per sender basis, so we use the first encoding's value. |
| 2368 | encoder_config.bitrate_priority = |
| 2369 | rtp_parameters_.encodings[0].bitrate_priority; |
| 2370 |
| 2371 | // Application-controlled state is held in the encoder_config's |
| 2372 | // simulcast_layers. Currently this is used to control which simulcast layers |
| 2373 | // are active and for configuring the min/max bitrate and max framerate. |
| 2374 | // The encoder_config's simulcast_layers is also used for non-simulcast (when |
| 2375 | // there is a single layer). |
| 2376 | RTC_DCHECK_GE(rtp_parameters_.encodings.size(), |
| 2377 | encoder_config.number_of_streams); |
| 2378 | RTC_DCHECK_GT(encoder_config.number_of_streams, 0); |
| 2379 |
| 2380 | // Copy all provided constraints. |
| 2381 | encoder_config.simulcast_layers.resize(rtp_parameters_.encodings.size()); |
| 2382 | for (size_t i = 0; i < encoder_config.simulcast_layers.size(); ++i) { |
| 2383 | encoder_config.simulcast_layers[i].active = |
| 2384 | rtp_parameters_.encodings[i].active; |
| 2385 | if (rtp_parameters_.encodings[i].min_bitrate_bps) { |
| 2386 | encoder_config.simulcast_layers[i].min_bitrate_bps = |
| 2387 | *rtp_parameters_.encodings[i].min_bitrate_bps; |
| 2388 | } |
| 2389 | if (rtp_parameters_.encodings[i].max_bitrate_bps) { |
| 2390 | encoder_config.simulcast_layers[i].max_bitrate_bps = |
| 2391 | *rtp_parameters_.encodings[i].max_bitrate_bps; |
| 2392 | } |
| 2393 | if (rtp_parameters_.encodings[i].max_framerate) { |
| 2394 | encoder_config.simulcast_layers[i].max_framerate = |
| 2395 | *rtp_parameters_.encodings[i].max_framerate; |
| 2396 | } |
| 2397 | if (rtp_parameters_.encodings[i].scale_resolution_down_by) { |
| 2398 | encoder_config.simulcast_layers[i].scale_resolution_down_by = |
| 2399 | *rtp_parameters_.encodings[i].scale_resolution_down_by; |
| 2400 | } |
| 2401 | if (rtp_parameters_.encodings[i].num_temporal_layers) { |
| 2402 | encoder_config.simulcast_layers[i].num_temporal_layers = |
| 2403 | *rtp_parameters_.encodings[i].num_temporal_layers; |
| 2404 | } |
| 2405 | } |
| 2406 |
| 2407 | encoder_config.legacy_conference_mode = parameters_.conference_mode; |
| 2408 |
| 2409 | int max_qp = kDefaultQpMax; |
| 2410 | codec.GetParam(kCodecParamMaxQuantization, &max_qp); |
| 2411 | encoder_config.video_stream_factory = |
| 2412 | new rtc::RefCountedObject<EncoderStreamFactory>( |
| 2413 | codec.name, max_qp, is_screencast, parameters_.conference_mode); |
| 2414 | return encoder_config; |
| 2415 | } |
| 2416 |
| 2417 | void WebRtcVideoChannel::WebRtcVideoSendStream::ReconfigureEncoder() { |
| 2418 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2419 | if (!stream_) { |
| 2420 | // The webrtc::VideoSendStream |stream_| has not yet been created but other |
| 2421 | // parameters has changed. |
| 2422 | return; |
| 2423 | } |
| 2424 |
| 2425 | RTC_DCHECK_GT(parameters_.encoder_config.number_of_streams, 0); |
| 2426 |
| 2427 | RTC_CHECK(parameters_.codec_settings); |
| 2428 | VideoCodecSettings codec_settings = *parameters_.codec_settings; |
| 2429 |
| 2430 | webrtc::VideoEncoderConfig encoder_config = |
| 2431 | CreateVideoEncoderConfig(codec_settings.codec); |
| 2432 |
| 2433 | encoder_config.encoder_specific_settings = |
| 2434 | ConfigureVideoEncoderSettings(codec_settings.codec); |
| 2435 |
| 2436 | stream_->ReconfigureVideoEncoder(encoder_config.Copy()); |
| 2437 |
| 2438 | encoder_config.encoder_specific_settings = NULL; |
| 2439 |
| 2440 | parameters_.encoder_config = std::move(encoder_config); |
| 2441 | } |
| 2442 |
| 2443 | void WebRtcVideoChannel::WebRtcVideoSendStream::SetSend(bool send) { |
| 2444 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2445 | sending_ = send; |
| 2446 | UpdateSendState(); |
| 2447 | } |
| 2448 |
| 2449 | void WebRtcVideoChannel::WebRtcVideoSendStream::RemoveSink( |
| 2450 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { |
| 2451 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2452 | RTC_DCHECK(encoder_sink_ == sink); |
| 2453 | encoder_sink_ = nullptr; |
| 2454 | source_->RemoveSink(sink); |
| 2455 | } |
| 2456 |
| 2457 | void WebRtcVideoChannel::WebRtcVideoSendStream::AddOrUpdateSink( |
| 2458 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink, |
| 2459 | const rtc::VideoSinkWants& wants) { |
| 2460 | if (worker_thread_ == rtc::Thread::Current()) { |
| 2461 | // AddOrUpdateSink is called on |worker_thread_| if this is the first |
| 2462 | // registration of |sink|. |
| 2463 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2464 | encoder_sink_ = sink; |
| 2465 | source_->AddOrUpdateSink(encoder_sink_, wants); |
| 2466 | } else { |
| 2467 | // Subsequent calls to AddOrUpdateSink will happen on the encoder task |
| 2468 | // queue. |
| 2469 | invoker_.AsyncInvoke<void>( |
| 2470 | RTC_FROM_HERE, worker_thread_, [this, sink, wants] { |
| 2471 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2472 | // |sink| may be invalidated after this task was posted since |
| 2473 | // RemoveSink is called on the worker thread. |
| 2474 | bool encoder_sink_valid = (sink == encoder_sink_); |
| 2475 | if (source_ && encoder_sink_valid) { |
| 2476 | source_->AddOrUpdateSink(encoder_sink_, wants); |
| 2477 | } |
| 2478 | }); |
| 2479 | } |
| 2480 | } |
| 2481 | std::vector<VideoSenderInfo> |
| 2482 | WebRtcVideoChannel::WebRtcVideoSendStream::GetPerLayerVideoSenderInfos( |
| 2483 | bool log_stats) { |
| 2484 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2485 | VideoSenderInfo common_info; |
| 2486 | if (parameters_.codec_settings) { |
| 2487 | common_info.codec_name = parameters_.codec_settings->codec.name; |
| 2488 | common_info.codec_payload_type = parameters_.codec_settings->codec.id; |
| 2489 | } |
| 2490 | std::vector<VideoSenderInfo> infos; |
| 2491 | webrtc::VideoSendStream::Stats stats; |
| 2492 | if (stream_ == nullptr) { |
| 2493 | for (uint32_t ssrc : parameters_.config.rtp.ssrcs) { |
| 2494 | common_info.add_ssrc(ssrc); |
| 2495 | } |
| 2496 | infos.push_back(common_info); |
| 2497 | return infos; |
| 2498 | } else { |
| 2499 | stats = stream_->GetStats(); |
| 2500 | if (log_stats) |
| 2501 | RTC_LOG(LS_INFO) << stats.ToString(rtc::TimeMillis()); |
| 2502 |
| 2503 | // Metrics that are in common for all substreams. |
| 2504 | common_info.adapt_changes = stats.number_of_cpu_adapt_changes; |
| 2505 | common_info.adapt_reason = |
| 2506 | stats.cpu_limited_resolution ? ADAPTREASON_CPU : ADAPTREASON_NONE; |
| 2507 | common_info.has_entered_low_resolution = stats.has_entered_low_resolution; |
| 2508 |
| 2509 | // Get bandwidth limitation info from stream_->GetStats(). |
| 2510 | // Input resolution (output from video_adapter) can be further scaled down |
| 2511 | // or higher video layer(s) can be dropped due to bitrate constraints. |
| 2512 | // Note, adapt_changes only include changes from the video_adapter. |
| 2513 | if (stats.bw_limited_resolution) |
| 2514 | common_info.adapt_reason |= ADAPTREASON_BANDWIDTH; |
| 2515 |
| 2516 | common_info.quality_limitation_reason = stats.quality_limitation_reason; |
| 2517 | common_info.quality_limitation_durations_ms = |
| 2518 | stats.quality_limitation_durations_ms; |
| 2519 | common_info.quality_limitation_resolution_changes = |
| 2520 | stats.quality_limitation_resolution_changes; |
| 2521 | common_info.encoder_implementation_name = stats.encoder_implementation_name; |
| 2522 | common_info.ssrc_groups = ssrc_groups_; |
| 2523 | common_info.framerate_input = stats.input_frame_rate; |
| 2524 | common_info.avg_encode_ms = stats.avg_encode_time_ms; |
| 2525 | common_info.encode_usage_percent = stats.encode_usage_percent; |
| 2526 | common_info.nominal_bitrate = stats.media_bitrate_bps; |
| 2527 | common_info.content_type = stats.content_type; |
| 2528 | common_info.aggregated_framerate_sent = stats.encode_frame_rate; |
| 2529 | common_info.aggregated_huge_frames_sent = stats.huge_frames_sent; |
| 2530 |
| 2531 | // If we don't have any substreams, get the remaining metrics from |stats|. |
| 2532 | // Otherwise, these values are obtained from |sub_stream| below. |
| 2533 | if (stats.substreams.empty()) { |
| 2534 | for (uint32_t ssrc : parameters_.config.rtp.ssrcs) { |
| 2535 | common_info.add_ssrc(ssrc); |
| 2536 | } |
| 2537 | common_info.framerate_sent = stats.encode_frame_rate; |
| 2538 | common_info.frames_encoded = stats.frames_encoded; |
| 2539 | common_info.total_encode_time_ms = stats.total_encode_time_ms; |
| 2540 | common_info.total_encoded_bytes_target = stats.total_encoded_bytes_target; |
| 2541 | common_info.frames_sent = stats.frames_encoded; |
| 2542 | common_info.huge_frames_sent = stats.huge_frames_sent; |
| 2543 | infos.push_back(common_info); |
| 2544 | return infos; |
| 2545 | } |
| 2546 | } |
| 2547 | auto outbound_rtp_substreams = |
| 2548 | MergeInfoAboutOutboundRtpSubstreams(stats.substreams); |
| 2549 | for (const auto& pair : outbound_rtp_substreams) { |
| 2550 | auto info = common_info; |
| 2551 | info.add_ssrc(pair.first); |
| 2552 | info.rid = parameters_.config.rtp.GetRidForSsrc(pair.first); |
| 2553 | auto stream_stats = pair.second; |
| 2554 | RTC_DCHECK_EQ(stream_stats.type, |
| 2555 | webrtc::VideoSendStream::StreamStats::StreamType::kMedia); |
| 2556 | info.payload_bytes_sent = stream_stats.rtp_stats.transmitted.payload_bytes; |
| 2557 | info.header_and_padding_bytes_sent = |
| 2558 | stream_stats.rtp_stats.transmitted.header_bytes + |
| 2559 | stream_stats.rtp_stats.transmitted.padding_bytes; |
| 2560 | info.packets_sent = stream_stats.rtp_stats.transmitted.packets; |
| 2561 | info.total_packet_send_delay_ms += stream_stats.total_packet_send_delay_ms; |
| 2562 | info.send_frame_width = stream_stats.width; |
| 2563 | info.send_frame_height = stream_stats.height; |
| 2564 | info.key_frames_encoded = stream_stats.frame_counts.key_frames; |
| 2565 | info.framerate_sent = stream_stats.encode_frame_rate; |
| 2566 | info.frames_encoded = stream_stats.frames_encoded; |
| 2567 | info.frames_sent = stream_stats.frames_encoded; |
| 2568 | info.retransmitted_bytes_sent = |
| 2569 | stream_stats.rtp_stats.retransmitted.payload_bytes; |
| 2570 | info.retransmitted_packets_sent = |
| 2571 | stream_stats.rtp_stats.retransmitted.packets; |
| 2572 | info.packets_lost = stream_stats.rtcp_stats.packets_lost; |
| 2573 | info.firs_rcvd = stream_stats.rtcp_packet_type_counts.fir_packets; |
| 2574 | info.nacks_rcvd = stream_stats.rtcp_packet_type_counts.nack_packets; |
| 2575 | info.plis_rcvd = stream_stats.rtcp_packet_type_counts.pli_packets; |
| 2576 | if (stream_stats.report_block_data.has_value()) { |
| 2577 | info.report_block_datas.push_back(stream_stats.report_block_data.value()); |
| 2578 | } |
| 2579 | info.fraction_lost = |
| 2580 | static_cast<float>(stream_stats.rtcp_stats.fraction_lost) / (1 << 8); |
| 2581 | info.qp_sum = stream_stats.qp_sum; |
| 2582 | info.total_encode_time_ms = stream_stats.total_encode_time_ms; |
| 2583 | info.total_encoded_bytes_target = stream_stats.total_encoded_bytes_target; |
| 2584 | info.huge_frames_sent = stream_stats.huge_frames_sent; |
| 2585 | infos.push_back(info); |
| 2586 | } |
| 2587 | return infos; |
| 2588 | } |
| 2589 |
| 2590 | VideoSenderInfo |
| 2591 | WebRtcVideoChannel::WebRtcVideoSendStream::GetAggregatedVideoSenderInfo( |
| 2592 | const std::vector<VideoSenderInfo>& infos) const { |
| 2593 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2594 | RTC_CHECK(!infos.empty()); |
| 2595 | if (infos.size() == 1) { |
| 2596 | return infos[0]; |
| 2597 | } |
| 2598 | VideoSenderInfo info = infos[0]; |
| 2599 | info.local_stats.clear(); |
| 2600 | for (uint32_t ssrc : parameters_.config.rtp.ssrcs) { |
| 2601 | info.add_ssrc(ssrc); |
| 2602 | } |
| 2603 | info.framerate_sent = info.aggregated_framerate_sent; |
| 2604 | info.huge_frames_sent = info.aggregated_huge_frames_sent; |
| 2605 |
| 2606 | for (size_t i = 1; i < infos.size(); i++) { |
| 2607 | info.key_frames_encoded += infos[i].key_frames_encoded; |
| 2608 | info.payload_bytes_sent += infos[i].payload_bytes_sent; |
| 2609 | info.header_and_padding_bytes_sent += |
| 2610 | infos[i].header_and_padding_bytes_sent; |
| 2611 | info.packets_sent += infos[i].packets_sent; |
| 2612 | info.total_packet_send_delay_ms += infos[i].total_packet_send_delay_ms; |
| 2613 | info.retransmitted_bytes_sent += infos[i].retransmitted_bytes_sent; |
| 2614 | info.retransmitted_packets_sent += infos[i].retransmitted_packets_sent; |
| 2615 | info.packets_lost += infos[i].packets_lost; |
| 2616 | if (infos[i].send_frame_width > info.send_frame_width) |
| 2617 | info.send_frame_width = infos[i].send_frame_width; |
| 2618 | if (infos[i].send_frame_height > info.send_frame_height) |
| 2619 | info.send_frame_height = infos[i].send_frame_height; |
| 2620 | info.firs_rcvd += infos[i].firs_rcvd; |
| 2621 | info.nacks_rcvd += infos[i].nacks_rcvd; |
| 2622 | info.plis_rcvd += infos[i].plis_rcvd; |
| 2623 | if (infos[i].report_block_datas.size()) |
| 2624 | info.report_block_datas.push_back(infos[i].report_block_datas[0]); |
| 2625 | if (infos[i].qp_sum) { |
| 2626 | if (!info.qp_sum) { |
| 2627 | info.qp_sum = 0; |
| 2628 | } |
| 2629 | info.qp_sum = *info.qp_sum + *infos[i].qp_sum; |
| 2630 | } |
| 2631 | info.frames_encoded += infos[i].frames_encoded; |
| 2632 | info.frames_sent += infos[i].frames_sent; |
| 2633 | info.total_encode_time_ms += infos[i].total_encode_time_ms; |
| 2634 | info.total_encoded_bytes_target += infos[i].total_encoded_bytes_target; |
| 2635 | } |
| 2636 | return info; |
| 2637 | } |
| 2638 |
| 2639 | void WebRtcVideoChannel::WebRtcVideoSendStream::FillBitrateInfo( |
| 2640 | BandwidthEstimationInfo* bwe_info) { |
| 2641 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2642 | if (stream_ == NULL) { |
| 2643 | return; |
| 2644 | } |
| 2645 | webrtc::VideoSendStream::Stats stats = stream_->GetStats(); |
| 2646 | for (std::map<uint32_t, webrtc::VideoSendStream::StreamStats>::iterator it = |
| 2647 | stats.substreams.begin(); |
| 2648 | it != stats.substreams.end(); ++it) { |
| 2649 | bwe_info->transmit_bitrate += it->second.total_bitrate_bps; |
| 2650 | bwe_info->retransmit_bitrate += it->second.retransmit_bitrate_bps; |
| 2651 | } |
| 2652 | bwe_info->target_enc_bitrate += stats.target_media_bitrate_bps; |
| 2653 | bwe_info->actual_enc_bitrate += stats.media_bitrate_bps; |
| 2654 | } |
| 2655 |
| 2656 | void WebRtcVideoChannel::WebRtcVideoSendStream:: |
| 2657 | SetEncoderToPacketizerFrameTransformer( |
| 2658 | rtc::scoped_refptr<webrtc::FrameTransformerInterface> |
| 2659 | frame_transformer) { |
| 2660 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2661 | parameters_.config.frame_transformer = std::move(frame_transformer); |
| 2662 | if (stream_) |
| 2663 | RecreateWebRtcStream(); |
| 2664 | } |
| 2665 |
| 2666 | void WebRtcVideoChannel::WebRtcVideoSendStream::RecreateWebRtcStream() { |
| 2667 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 2668 | if (stream_ != NULL) { |
| 2669 | call_->DestroyVideoSendStream(stream_); |
| 2670 | } |
| 2671 |
| 2672 | RTC_CHECK(parameters_.codec_settings); |
| 2673 | RTC_DCHECK_EQ((parameters_.encoder_config.content_type == |
| 2674 | webrtc::VideoEncoderConfig::ContentType::kScreen), |
| 2675 | parameters_.options.is_screencast.value_or(false)) |
| 2676 | << "encoder content type inconsistent with screencast option"; |
| 2677 | parameters_.encoder_config.encoder_specific_settings = |
| 2678 | ConfigureVideoEncoderSettings(parameters_.codec_settings->codec); |
| 2679 |
| 2680 | webrtc::VideoSendStream::Config config = parameters_.config.Copy(); |
| 2681 | if (!config.rtp.rtx.ssrcs.empty() && config.rtp.rtx.payload_type == -1) { |
| 2682 | RTC_LOG(LS_WARNING) << "RTX SSRCs configured but there's no configured RTX " |
| 2683 | "payload type the set codec. Ignoring RTX."; |
| 2684 | config.rtp.rtx.ssrcs.clear(); |
| 2685 | } |
| 2686 | if (parameters_.encoder_config.number_of_streams == 1) { |
| 2687 | // SVC is used instead of simulcast. Remove unnecessary SSRCs. |
| 2688 | if (config.rtp.ssrcs.size() > 1) { |
| 2689 | config.rtp.ssrcs.resize(1); |
| 2690 | if (config.rtp.rtx.ssrcs.size() > 1) { |
| 2691 | config.rtp.rtx.ssrcs.resize(1); |
| 2692 | } |
| 2693 | } |
| 2694 | } |
| 2695 | stream_ = call_->CreateVideoSendStream(std::move(config), |
| 2696 | parameters_.encoder_config.Copy()); |
| 2697 |
| 2698 | parameters_.encoder_config.encoder_specific_settings = NULL; |
| 2699 |
| 2700 | if (source_) { |
| 2701 | stream_->SetSource(this, GetDegradationPreference()); |
| 2702 | } |
| 2703 |
| 2704 | // Call stream_->Start() if necessary conditions are met. |
| 2705 | UpdateSendState(); |
| 2706 | } |
| 2707 |
| 2708 | WebRtcVideoChannel::WebRtcVideoReceiveStream::WebRtcVideoReceiveStream( |
| 2709 | WebRtcVideoChannel* channel, |
| 2710 | webrtc::Call* call, |
| 2711 | const StreamParams& sp, |
| 2712 | webrtc::VideoReceiveStream::Config config, |
| 2713 | webrtc::VideoDecoderFactory* decoder_factory, |
| 2714 | bool default_stream, |
| 2715 | const std::vector<VideoCodecSettings>& recv_codecs, |
| 2716 | const webrtc::FlexfecReceiveStream::Config& flexfec_config) |
| 2717 | : channel_(channel), |
| 2718 | call_(call), |
| 2719 | stream_params_(sp), |
| 2720 | stream_(NULL), |
| 2721 | default_stream_(default_stream), |
| 2722 | config_(std::move(config)), |
| 2723 | flexfec_config_(flexfec_config), |
| 2724 | flexfec_stream_(nullptr), |
| 2725 | decoder_factory_(decoder_factory), |
| 2726 | sink_(NULL), |
| 2727 | first_frame_timestamp_(-1), |
| 2728 | estimated_remote_start_ntp_time_ms_(0) { |
| 2729 | config_.renderer = this; |
| 2730 | ConfigureCodecs(recv_codecs); |
| 2731 | ConfigureFlexfecCodec(flexfec_config.payload_type); |
| 2732 | MaybeRecreateWebRtcFlexfecStream(); |
| 2733 | RecreateWebRtcVideoStream(); |
| 2734 | } |
| 2735 |
| 2736 | WebRtcVideoChannel::WebRtcVideoReceiveStream::~WebRtcVideoReceiveStream() { |
| 2737 | if (flexfec_stream_) { |
| 2738 | MaybeDissociateFlexfecFromVideo(); |
| 2739 | call_->DestroyFlexfecReceiveStream(flexfec_stream_); |
| 2740 | } |
| 2741 | call_->DestroyVideoReceiveStream(stream_); |
| 2742 | } |
| 2743 |
| 2744 | const std::vector<uint32_t>& |
| 2745 | WebRtcVideoChannel::WebRtcVideoReceiveStream::GetSsrcs() const { |
| 2746 | return stream_params_.ssrcs; |
| 2747 | } |
| 2748 |
| 2749 | std::vector<webrtc::RtpSource> |
| 2750 | WebRtcVideoChannel::WebRtcVideoReceiveStream::GetSources() { |
| 2751 | RTC_DCHECK(stream_); |
| 2752 | return stream_->GetSources(); |
| 2753 | } |
| 2754 |
| 2755 | webrtc::RtpParameters |
| 2756 | WebRtcVideoChannel::WebRtcVideoReceiveStream::GetRtpParameters() const { |
| 2757 | webrtc::RtpParameters rtp_parameters; |
| 2758 |
| 2759 | std::vector<uint32_t> primary_ssrcs; |
| 2760 | stream_params_.GetPrimarySsrcs(&primary_ssrcs); |
| 2761 | for (uint32_t ssrc : primary_ssrcs) { |
| 2762 | rtp_parameters.encodings.emplace_back(); |
| 2763 | rtp_parameters.encodings.back().ssrc = ssrc; |
| 2764 | } |
| 2765 |
| 2766 | rtp_parameters.header_extensions = config_.rtp.extensions; |
| 2767 | rtp_parameters.rtcp.reduced_size = |
| 2768 | config_.rtp.rtcp_mode == webrtc::RtcpMode::kReducedSize; |
| 2769 |
| 2770 | return rtp_parameters; |
| 2771 | } |
| 2772 |
| 2773 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::ConfigureCodecs( |
| 2774 | const std::vector<VideoCodecSettings>& recv_codecs) { |
| 2775 | RTC_DCHECK(!recv_codecs.empty()); |
| 2776 | config_.decoders.clear(); |
| 2777 | config_.rtp.rtx_associated_payload_types.clear(); |
| 2778 | config_.rtp.raw_payload_types.clear(); |
| 2779 | config_.decoder_factory = decoder_factory_; |
| 2780 | for (const auto& recv_codec : recv_codecs) { |
| 2781 | webrtc::SdpVideoFormat video_format(recv_codec.codec.name, |
| 2782 | recv_codec.codec.params); |
| 2783 |
| 2784 | webrtc::VideoReceiveStream::Decoder decoder; |
| 2785 | decoder.video_format = video_format; |
| 2786 | decoder.payload_type = recv_codec.codec.id; |
| 2787 | decoder.video_format = |
| 2788 | webrtc::SdpVideoFormat(recv_codec.codec.name, recv_codec.codec.params); |
| 2789 | config_.decoders.push_back(decoder); |
| 2790 | config_.rtp.rtx_associated_payload_types[recv_codec.rtx_payload_type] = |
| 2791 | recv_codec.codec.id; |
| 2792 | if (recv_codec.codec.packetization == kPacketizationParamRaw) { |
| 2793 | config_.rtp.raw_payload_types.insert(recv_codec.codec.id); |
| 2794 | } |
| 2795 | } |
| 2796 |
| 2797 | const auto& codec = recv_codecs.front(); |
| 2798 | config_.rtp.ulpfec_payload_type = codec.ulpfec.ulpfec_payload_type; |
| 2799 | config_.rtp.red_payload_type = codec.ulpfec.red_payload_type; |
| 2800 |
| 2801 | config_.rtp.lntf.enabled = HasLntf(codec.codec); |
| 2802 | config_.rtp.nack.rtp_history_ms = HasNack(codec.codec) ? kNackHistoryMs : 0; |
| 2803 | config_.rtp.rtcp_xr.receiver_reference_time_report = HasRrtr(codec.codec); |
| 2804 | if (codec.ulpfec.red_rtx_payload_type != -1) { |
| 2805 | config_.rtp |
| 2806 | .rtx_associated_payload_types[codec.ulpfec.red_rtx_payload_type] = |
| 2807 | codec.ulpfec.red_payload_type; |
| 2808 | } |
| 2809 | } |
| 2810 |
| 2811 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::ConfigureFlexfecCodec( |
| 2812 | int flexfec_payload_type) { |
| 2813 | flexfec_config_.payload_type = flexfec_payload_type; |
| 2814 | } |
| 2815 |
| 2816 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::SetLocalSsrc( |
| 2817 | uint32_t local_ssrc) { |
| 2818 | // TODO(pbos): Consider turning this sanity check into a RTC_DCHECK. You |
| 2819 | // should not be able to create a sender with the same SSRC as a receiver, but |
| 2820 | // right now this can't be done due to unittests depending on receiving what |
| 2821 | // they are sending from the same MediaChannel. |
| 2822 | if (local_ssrc == config_.rtp.local_ssrc) { |
| 2823 | RTC_DLOG(LS_INFO) << "Ignoring call to SetLocalSsrc because parameters are " |
| 2824 | "unchanged; local_ssrc=" |
| 2825 | << local_ssrc; |
| 2826 | return; |
| 2827 | } |
| 2828 |
| 2829 | config_.rtp.local_ssrc = local_ssrc; |
| 2830 | flexfec_config_.local_ssrc = local_ssrc; |
| 2831 | RTC_LOG(LS_INFO) |
| 2832 | << "RecreateWebRtcStream (recv) because of SetLocalSsrc; local_ssrc=" |
| 2833 | << local_ssrc; |
| 2834 | MaybeRecreateWebRtcFlexfecStream(); |
| 2835 | RecreateWebRtcVideoStream(); |
| 2836 | } |
| 2837 |
| 2838 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::SetFeedbackParameters( |
| 2839 | bool lntf_enabled, |
| 2840 | bool nack_enabled, |
| 2841 | bool transport_cc_enabled, |
| 2842 | webrtc::RtcpMode rtcp_mode) { |
| 2843 | int nack_history_ms = nack_enabled ? kNackHistoryMs : 0; |
| 2844 | if (config_.rtp.lntf.enabled == lntf_enabled && |
| 2845 | config_.rtp.nack.rtp_history_ms == nack_history_ms && |
| 2846 | config_.rtp.transport_cc == transport_cc_enabled && |
| 2847 | config_.rtp.rtcp_mode == rtcp_mode) { |
| 2848 | RTC_LOG(LS_INFO) |
| 2849 | << "Ignoring call to SetFeedbackParameters because parameters are " |
| 2850 | "unchanged; lntf=" |
| 2851 | << lntf_enabled << ", nack=" << nack_enabled |
| 2852 | << ", transport_cc=" << transport_cc_enabled; |
| 2853 | return; |
| 2854 | } |
| 2855 | config_.rtp.lntf.enabled = lntf_enabled; |
| 2856 | config_.rtp.nack.rtp_history_ms = nack_history_ms; |
| 2857 | config_.rtp.transport_cc = transport_cc_enabled; |
| 2858 | config_.rtp.rtcp_mode = rtcp_mode; |
| 2859 | // TODO(brandtr): We should be spec-compliant and set |transport_cc| here |
| 2860 | // based on the rtcp-fb for the FlexFEC codec, not the media codec. |
| 2861 | flexfec_config_.transport_cc = config_.rtp.transport_cc; |
| 2862 | flexfec_config_.rtcp_mode = config_.rtp.rtcp_mode; |
| 2863 | RTC_LOG(LS_INFO) |
| 2864 | << "RecreateWebRtcStream (recv) because of SetFeedbackParameters; nack=" |
| 2865 | << nack_enabled << ", transport_cc=" << transport_cc_enabled; |
| 2866 | MaybeRecreateWebRtcFlexfecStream(); |
| 2867 | RecreateWebRtcVideoStream(); |
| 2868 | } |
| 2869 |
| 2870 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::SetRecvParameters( |
| 2871 | const ChangedRecvParameters& params) { |
| 2872 | bool video_needs_recreation = false; |
| 2873 | bool flexfec_needs_recreation = false; |
| 2874 | if (params.codec_settings) { |
| 2875 | ConfigureCodecs(*params.codec_settings); |
| 2876 | video_needs_recreation = true; |
| 2877 | } |
| 2878 | if (params.rtp_header_extensions) { |
| 2879 | config_.rtp.extensions = *params.rtp_header_extensions; |
| 2880 | flexfec_config_.rtp_header_extensions = *params.rtp_header_extensions; |
| 2881 | video_needs_recreation = true; |
| 2882 | flexfec_needs_recreation = true; |
| 2883 | } |
| 2884 | if (params.flexfec_payload_type) { |
| 2885 | ConfigureFlexfecCodec(*params.flexfec_payload_type); |
| 2886 | flexfec_needs_recreation = true; |
| 2887 | } |
| 2888 | if (flexfec_needs_recreation) { |
| 2889 | RTC_LOG(LS_INFO) << "MaybeRecreateWebRtcFlexfecStream (recv) because of " |
| 2890 | "SetRecvParameters"; |
| 2891 | MaybeRecreateWebRtcFlexfecStream(); |
| 2892 | } |
| 2893 | if (video_needs_recreation) { |
| 2894 | RTC_LOG(LS_INFO) |
| 2895 | << "RecreateWebRtcVideoStream (recv) because of SetRecvParameters"; |
| 2896 | RecreateWebRtcVideoStream(); |
| 2897 | } |
| 2898 | } |
| 2899 |
| 2900 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::RecreateWebRtcVideoStream() { |
| 2901 | absl::optional<int> base_minimum_playout_delay_ms; |
| 2902 | absl::optional<webrtc::VideoReceiveStream::RecordingState> recording_state; |
| 2903 | if (stream_) { |
| 2904 | base_minimum_playout_delay_ms = stream_->GetBaseMinimumPlayoutDelayMs(); |
| 2905 | recording_state = stream_->SetAndGetRecordingState( |
| 2906 | webrtc::VideoReceiveStream::RecordingState(), |
| 2907 | /*generate_key_frame=*/false); |
| 2908 | MaybeDissociateFlexfecFromVideo(); |
| 2909 | call_->DestroyVideoReceiveStream(stream_); |
| 2910 | stream_ = nullptr; |
| 2911 | } |
| 2912 | webrtc::VideoReceiveStream::Config config = config_.Copy(); |
| 2913 | config.rtp.protected_by_flexfec = (flexfec_stream_ != nullptr); |
| 2914 | config.stream_id = stream_params_.id; |
| 2915 | stream_ = call_->CreateVideoReceiveStream(std::move(config)); |
| 2916 | if (base_minimum_playout_delay_ms) { |
| 2917 | stream_->SetBaseMinimumPlayoutDelayMs( |
| 2918 | base_minimum_playout_delay_ms.value()); |
| 2919 | } |
| 2920 | if (recording_state) { |
| 2921 | stream_->SetAndGetRecordingState(std::move(*recording_state), |
| 2922 | /*generate_key_frame=*/false); |
| 2923 | } |
| 2924 | MaybeAssociateFlexfecWithVideo(); |
| 2925 | stream_->Start(); |
| 2926 |
| 2927 | if (webrtc::field_trial::IsEnabled( |
| 2928 | "WebRTC-Video-BufferPacketsWithUnknownSsrc")) { |
| 2929 | channel_->BackfillBufferedPackets(stream_params_.ssrcs); |
| 2930 | } |
| 2931 | } |
| 2932 |
| 2933 | void WebRtcVideoChannel::WebRtcVideoReceiveStream:: |
| 2934 | MaybeRecreateWebRtcFlexfecStream() { |
| 2935 | if (flexfec_stream_) { |
| 2936 | MaybeDissociateFlexfecFromVideo(); |
| 2937 | call_->DestroyFlexfecReceiveStream(flexfec_stream_); |
| 2938 | flexfec_stream_ = nullptr; |
| 2939 | } |
| 2940 | if (flexfec_config_.IsCompleteAndEnabled()) { |
| 2941 | flexfec_stream_ = call_->CreateFlexfecReceiveStream(flexfec_config_); |
| 2942 | MaybeAssociateFlexfecWithVideo(); |
| 2943 | } |
| 2944 | } |
| 2945 |
| 2946 | void WebRtcVideoChannel::WebRtcVideoReceiveStream:: |
| 2947 | MaybeAssociateFlexfecWithVideo() { |
| 2948 | if (stream_ && flexfec_stream_) { |
| 2949 | stream_->AddSecondarySink(flexfec_stream_); |
| 2950 | } |
| 2951 | } |
| 2952 |
| 2953 | void WebRtcVideoChannel::WebRtcVideoReceiveStream:: |
| 2954 | MaybeDissociateFlexfecFromVideo() { |
| 2955 | if (stream_ && flexfec_stream_) { |
| 2956 | stream_->RemoveSecondarySink(flexfec_stream_); |
| 2957 | } |
| 2958 | } |
| 2959 |
| 2960 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::OnFrame( |
| 2961 | const webrtc::VideoFrame& frame) { |
| 2962 | webrtc::MutexLock lock(&sink_lock_); |
| 2963 |
| 2964 | int64_t time_now_ms = rtc::TimeMillis(); |
| 2965 | if (first_frame_timestamp_ < 0) |
| 2966 | first_frame_timestamp_ = time_now_ms; |
| 2967 | int64_t elapsed_time_ms = time_now_ms - first_frame_timestamp_; |
| 2968 | if (frame.ntp_time_ms() > 0) |
| 2969 | estimated_remote_start_ntp_time_ms_ = frame.ntp_time_ms() - elapsed_time_ms; |
| 2970 |
| 2971 | if (sink_ == NULL) { |
| 2972 | RTC_LOG(LS_WARNING) << "VideoReceiveStream not connected to a VideoSink."; |
| 2973 | return; |
| 2974 | } |
| 2975 |
| 2976 | sink_->OnFrame(frame); |
| 2977 | } |
| 2978 |
| 2979 | bool WebRtcVideoChannel::WebRtcVideoReceiveStream::IsDefaultStream() const { |
| 2980 | return default_stream_; |
| 2981 | } |
| 2982 |
| 2983 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::SetFrameDecryptor( |
| 2984 | rtc::scoped_refptr<webrtc::FrameDecryptorInterface> frame_decryptor) { |
| 2985 | config_.frame_decryptor = frame_decryptor; |
| 2986 | if (stream_) { |
| 2987 | RTC_LOG(LS_INFO) |
| 2988 | << "Setting FrameDecryptor (recv) because of SetFrameDecryptor, " |
| 2989 | "remote_ssrc=" |
| 2990 | << config_.rtp.remote_ssrc; |
| 2991 | stream_->SetFrameDecryptor(frame_decryptor); |
| 2992 | } |
| 2993 | } |
| 2994 |
| 2995 | bool WebRtcVideoChannel::WebRtcVideoReceiveStream::SetBaseMinimumPlayoutDelayMs( |
| 2996 | int delay_ms) { |
| 2997 | return stream_ ? stream_->SetBaseMinimumPlayoutDelayMs(delay_ms) : false; |
| 2998 | } |
| 2999 |
| 3000 | int WebRtcVideoChannel::WebRtcVideoReceiveStream::GetBaseMinimumPlayoutDelayMs() |
| 3001 | const { |
| 3002 | return stream_ ? stream_->GetBaseMinimumPlayoutDelayMs() : 0; |
| 3003 | } |
| 3004 |
| 3005 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::SetSink( |
| 3006 | rtc::VideoSinkInterface<webrtc::VideoFrame>* sink) { |
| 3007 | webrtc::MutexLock lock(&sink_lock_); |
| 3008 | sink_ = sink; |
| 3009 | } |
| 3010 |
| 3011 | std::string |
| 3012 | WebRtcVideoChannel::WebRtcVideoReceiveStream::GetCodecNameFromPayloadType( |
| 3013 | int payload_type) { |
| 3014 | for (const webrtc::VideoReceiveStream::Decoder& decoder : config_.decoders) { |
| 3015 | if (decoder.payload_type == payload_type) { |
| 3016 | return decoder.video_format.name; |
| 3017 | } |
| 3018 | } |
| 3019 | return ""; |
| 3020 | } |
| 3021 |
| 3022 | VideoReceiverInfo |
| 3023 | WebRtcVideoChannel::WebRtcVideoReceiveStream::GetVideoReceiverInfo( |
| 3024 | bool log_stats) { |
| 3025 | VideoReceiverInfo info; |
| 3026 | info.ssrc_groups = stream_params_.ssrc_groups; |
| 3027 | info.add_ssrc(config_.rtp.remote_ssrc); |
| 3028 | webrtc::VideoReceiveStream::Stats stats = stream_->GetStats(); |
| 3029 | info.decoder_implementation_name = stats.decoder_implementation_name; |
| 3030 | if (stats.current_payload_type != -1) { |
| 3031 | info.codec_payload_type = stats.current_payload_type; |
| 3032 | } |
| 3033 | info.payload_bytes_rcvd = stats.rtp_stats.packet_counter.payload_bytes; |
| 3034 | info.header_and_padding_bytes_rcvd = |
| 3035 | stats.rtp_stats.packet_counter.header_bytes + |
| 3036 | stats.rtp_stats.packet_counter.padding_bytes; |
| 3037 | info.packets_rcvd = stats.rtp_stats.packet_counter.packets; |
| 3038 | info.packets_lost = stats.rtp_stats.packets_lost; |
| 3039 |
| 3040 | info.framerate_rcvd = stats.network_frame_rate; |
| 3041 | info.framerate_decoded = stats.decode_frame_rate; |
| 3042 | info.framerate_output = stats.render_frame_rate; |
| 3043 | info.frame_width = stats.width; |
| 3044 | info.frame_height = stats.height; |
| 3045 |
| 3046 | { |
| 3047 | webrtc::MutexLock frame_cs(&sink_lock_); |
| 3048 | info.capture_start_ntp_time_ms = estimated_remote_start_ntp_time_ms_; |
| 3049 | } |
| 3050 |
| 3051 | info.decode_ms = stats.decode_ms; |
| 3052 | info.max_decode_ms = stats.max_decode_ms; |
| 3053 | info.current_delay_ms = stats.current_delay_ms; |
| 3054 | info.target_delay_ms = stats.target_delay_ms; |
| 3055 | info.jitter_buffer_ms = stats.jitter_buffer_ms; |
| 3056 | info.jitter_buffer_delay_seconds = stats.jitter_buffer_delay_seconds; |
| 3057 | info.jitter_buffer_emitted_count = stats.jitter_buffer_emitted_count; |
| 3058 | info.min_playout_delay_ms = stats.min_playout_delay_ms; |
| 3059 | info.render_delay_ms = stats.render_delay_ms; |
| 3060 | info.frames_received = |
| 3061 | stats.frame_counts.key_frames + stats.frame_counts.delta_frames; |
| 3062 | info.frames_dropped = stats.frames_dropped; |
| 3063 | info.frames_decoded = stats.frames_decoded; |
| 3064 | info.key_frames_decoded = stats.frame_counts.key_frames; |
| 3065 | info.frames_rendered = stats.frames_rendered; |
| 3066 | info.qp_sum = stats.qp_sum; |
| 3067 | info.total_decode_time_ms = stats.total_decode_time_ms; |
| 3068 | info.last_packet_received_timestamp_ms = |
| 3069 | stats.rtp_stats.last_packet_received_timestamp_ms; |
| 3070 | info.estimated_playout_ntp_timestamp_ms = |
| 3071 | stats.estimated_playout_ntp_timestamp_ms; |
| 3072 | info.first_frame_received_to_decoded_ms = |
| 3073 | stats.first_frame_received_to_decoded_ms; |
| 3074 | info.total_inter_frame_delay = stats.total_inter_frame_delay; |
| 3075 | info.total_squared_inter_frame_delay = stats.total_squared_inter_frame_delay; |
| 3076 | info.interframe_delay_max_ms = stats.interframe_delay_max_ms; |
| 3077 | info.freeze_count = stats.freeze_count; |
| 3078 | info.pause_count = stats.pause_count; |
| 3079 | info.total_freezes_duration_ms = stats.total_freezes_duration_ms; |
| 3080 | info.total_pauses_duration_ms = stats.total_pauses_duration_ms; |
| 3081 | info.total_frames_duration_ms = stats.total_frames_duration_ms; |
| 3082 | info.sum_squared_frame_durations = stats.sum_squared_frame_durations; |
| 3083 |
| 3084 | info.content_type = stats.content_type; |
| 3085 |
| 3086 | info.codec_name = GetCodecNameFromPayloadType(stats.current_payload_type); |
| 3087 |
| 3088 | info.firs_sent = stats.rtcp_packet_type_counts.fir_packets; |
| 3089 | info.plis_sent = stats.rtcp_packet_type_counts.pli_packets; |
| 3090 | info.nacks_sent = stats.rtcp_packet_type_counts.nack_packets; |
| 3091 | // TODO(bugs.webrtc.org/10662): Add stats for LNTF. |
| 3092 |
| 3093 | info.timing_frame_info = stats.timing_frame_info; |
| 3094 |
| 3095 | if (log_stats) |
| 3096 | RTC_LOG(LS_INFO) << stats.ToString(rtc::TimeMillis()); |
| 3097 |
| 3098 | return info; |
| 3099 | } |
| 3100 |
| 3101 | void WebRtcVideoChannel::WebRtcVideoReceiveStream:: |
| 3102 | SetRecordableEncodedFrameCallback( |
| 3103 | std::function<void(const webrtc::RecordableEncodedFrame&)> callback) { |
| 3104 | if (stream_) { |
| 3105 | stream_->SetAndGetRecordingState( |
| 3106 | webrtc::VideoReceiveStream::RecordingState(std::move(callback)), |
| 3107 | /*generate_key_frame=*/true); |
| 3108 | } else { |
| 3109 | RTC_LOG(LS_ERROR) << "Absent receive stream; ignoring setting encoded " |
| 3110 | "frame sink"; |
| 3111 | } |
| 3112 | } |
| 3113 |
| 3114 | void WebRtcVideoChannel::WebRtcVideoReceiveStream:: |
| 3115 | ClearRecordableEncodedFrameCallback() { |
| 3116 | if (stream_) { |
| 3117 | stream_->SetAndGetRecordingState( |
| 3118 | webrtc::VideoReceiveStream::RecordingState(), |
| 3119 | /*generate_key_frame=*/false); |
| 3120 | } else { |
| 3121 | RTC_LOG(LS_ERROR) << "Absent receive stream; ignoring clearing encoded " |
| 3122 | "frame sink"; |
| 3123 | } |
| 3124 | } |
| 3125 |
| 3126 | void WebRtcVideoChannel::WebRtcVideoReceiveStream::GenerateKeyFrame() { |
| 3127 | if (stream_) { |
| 3128 | stream_->GenerateKeyFrame(); |
| 3129 | } else { |
| 3130 | RTC_LOG(LS_ERROR) |
| 3131 | << "Absent receive stream; ignoring key frame generation request."; |
| 3132 | } |
| 3133 | } |
| 3134 |
| 3135 | void WebRtcVideoChannel::WebRtcVideoReceiveStream:: |
| 3136 | SetDepacketizerToDecoderFrameTransformer( |
| 3137 | rtc::scoped_refptr<webrtc::FrameTransformerInterface> |
| 3138 | frame_transformer) { |
| 3139 | config_.frame_transformer = frame_transformer; |
| 3140 | if (stream_) |
| 3141 | stream_->SetDepacketizerToDecoderFrameTransformer(frame_transformer); |
| 3142 | } |
| 3143 |
| 3144 | WebRtcVideoChannel::VideoCodecSettings::VideoCodecSettings() |
| 3145 | : flexfec_payload_type(-1), rtx_payload_type(-1) {} |
| 3146 |
| 3147 | bool WebRtcVideoChannel::VideoCodecSettings::operator==( |
| 3148 | const WebRtcVideoChannel::VideoCodecSettings& other) const { |
| 3149 | return codec == other.codec && ulpfec == other.ulpfec && |
| 3150 | flexfec_payload_type == other.flexfec_payload_type && |
| 3151 | rtx_payload_type == other.rtx_payload_type; |
| 3152 | } |
| 3153 |
| 3154 | bool WebRtcVideoChannel::VideoCodecSettings::EqualsDisregardingFlexfec( |
| 3155 | const WebRtcVideoChannel::VideoCodecSettings& a, |
| 3156 | const WebRtcVideoChannel::VideoCodecSettings& b) { |
| 3157 | return a.codec == b.codec && a.ulpfec == b.ulpfec && |
| 3158 | a.rtx_payload_type == b.rtx_payload_type; |
| 3159 | } |
| 3160 |
| 3161 | bool WebRtcVideoChannel::VideoCodecSettings::operator!=( |
| 3162 | const WebRtcVideoChannel::VideoCodecSettings& other) const { |
| 3163 | return !(*this == other); |
| 3164 | } |
| 3165 |
| 3166 | std::vector<WebRtcVideoChannel::VideoCodecSettings> |
| 3167 | WebRtcVideoChannel::MapCodecs(const std::vector<VideoCodec>& codecs) { |
| 3168 | if (codecs.empty()) { |
| 3169 | return {}; |
| 3170 | } |
| 3171 |
| 3172 | std::vector<VideoCodecSettings> video_codecs; |
| 3173 | std::map<int, VideoCodec::CodecType> payload_codec_type; |
| 3174 | // |rtx_mapping| maps video payload type to rtx payload type. |
| 3175 | std::map<int, int> rtx_mapping; |
| 3176 |
| 3177 | webrtc::UlpfecConfig ulpfec_config; |
| 3178 | absl::optional<int> flexfec_payload_type; |
| 3179 |
| 3180 | for (const VideoCodec& in_codec : codecs) { |
| 3181 | const int payload_type = in_codec.id; |
| 3182 |
| 3183 | if (payload_codec_type.find(payload_type) != payload_codec_type.end()) { |
| 3184 | RTC_LOG(LS_ERROR) << "Payload type already registered: " |
| 3185 | << in_codec.ToString(); |
| 3186 | return {}; |
| 3187 | } |
| 3188 | payload_codec_type[payload_type] = in_codec.GetCodecType(); |
| 3189 |
| 3190 | switch (in_codec.GetCodecType()) { |
| 3191 | case VideoCodec::CODEC_RED: { |
| 3192 | if (ulpfec_config.red_payload_type != -1) { |
| 3193 | RTC_LOG(LS_ERROR) |
| 3194 | << "Duplicate RED codec: ignoring PT=" << payload_type |
| 3195 | << " in favor of PT=" << ulpfec_config.red_payload_type |
| 3196 | << " which was specified first."; |
| 3197 | break; |
| 3198 | } |
| 3199 | ulpfec_config.red_payload_type = payload_type; |
| 3200 | break; |
| 3201 | } |
| 3202 |
| 3203 | case VideoCodec::CODEC_ULPFEC: { |
| 3204 | if (ulpfec_config.ulpfec_payload_type != -1) { |
| 3205 | RTC_LOG(LS_ERROR) |
| 3206 | << "Duplicate ULPFEC codec: ignoring PT=" << payload_type |
| 3207 | << " in favor of PT=" << ulpfec_config.ulpfec_payload_type |
| 3208 | << " which was specified first."; |
| 3209 | break; |
| 3210 | } |
| 3211 | ulpfec_config.ulpfec_payload_type = payload_type; |
| 3212 | break; |
| 3213 | } |
| 3214 |
| 3215 | case VideoCodec::CODEC_FLEXFEC: { |
| 3216 | if (flexfec_payload_type) { |
| 3217 | RTC_LOG(LS_ERROR) |
| 3218 | << "Duplicate FLEXFEC codec: ignoring PT=" << payload_type |
| 3219 | << " in favor of PT=" << *flexfec_payload_type |
| 3220 | << " which was specified first."; |
| 3221 | break; |
| 3222 | } |
| 3223 | flexfec_payload_type = payload_type; |
| 3224 | break; |
| 3225 | } |
| 3226 |
| 3227 | case VideoCodec::CODEC_RTX: { |
| 3228 | int associated_payload_type; |
| 3229 | if (!in_codec.GetParam(kCodecParamAssociatedPayloadType, |
| 3230 | &associated_payload_type) || |
| 3231 | !IsValidRtpPayloadType(associated_payload_type)) { |
| 3232 | RTC_LOG(LS_ERROR) |
| 3233 | << "RTX codec with invalid or no associated payload type: " |
| 3234 | << in_codec.ToString(); |
| 3235 | return {}; |
| 3236 | } |
| 3237 | rtx_mapping[associated_payload_type] = payload_type; |
| 3238 | break; |
| 3239 | } |
| 3240 |
| 3241 | case VideoCodec::CODEC_VIDEO: { |
| 3242 | video_codecs.emplace_back(); |
| 3243 | video_codecs.back().codec = in_codec; |
| 3244 | break; |
| 3245 | } |
| 3246 | } |
| 3247 | } |
| 3248 |
| 3249 | // One of these codecs should have been a video codec. Only having FEC |
| 3250 | // parameters into this code is a logic error. |
| 3251 | RTC_DCHECK(!video_codecs.empty()); |
| 3252 |
| 3253 | for (const auto& entry : rtx_mapping) { |
| 3254 | const int associated_payload_type = entry.first; |
| 3255 | const int rtx_payload_type = entry.second; |
| 3256 | auto it = payload_codec_type.find(associated_payload_type); |
| 3257 | if (it == payload_codec_type.end()) { |
| 3258 | RTC_LOG(LS_ERROR) << "RTX codec (PT=" << rtx_payload_type |
| 3259 | << ") mapped to PT=" << associated_payload_type |
| 3260 | << " which is not in the codec list."; |
| 3261 | return {}; |
| 3262 | } |
| 3263 | const VideoCodec::CodecType associated_codec_type = it->second; |
| 3264 | if (associated_codec_type != VideoCodec::CODEC_VIDEO && |
| 3265 | associated_codec_type != VideoCodec::CODEC_RED) { |
| 3266 | RTC_LOG(LS_ERROR) |
| 3267 | << "RTX PT=" << rtx_payload_type |
| 3268 | << " not mapped to regular video codec or RED codec (PT=" |
| 3269 | << associated_payload_type << ")."; |
| 3270 | return {}; |
| 3271 | } |
| 3272 |
| 3273 | if (associated_payload_type == ulpfec_config.red_payload_type) { |
| 3274 | ulpfec_config.red_rtx_payload_type = rtx_payload_type; |
| 3275 | } |
| 3276 | } |
| 3277 |
| 3278 | for (VideoCodecSettings& codec_settings : video_codecs) { |
| 3279 | const int payload_type = codec_settings.codec.id; |
| 3280 | codec_settings.ulpfec = ulpfec_config; |
| 3281 | codec_settings.flexfec_payload_type = flexfec_payload_type.value_or(-1); |
| 3282 | auto it = rtx_mapping.find(payload_type); |
| 3283 | if (it != rtx_mapping.end()) { |
| 3284 | const int rtx_payload_type = it->second; |
| 3285 | codec_settings.rtx_payload_type = rtx_payload_type; |
| 3286 | } |
| 3287 | } |
| 3288 |
| 3289 | return video_codecs; |
| 3290 | } |
| 3291 |
| 3292 | WebRtcVideoChannel::WebRtcVideoReceiveStream* |
| 3293 | WebRtcVideoChannel::FindReceiveStream(uint32_t ssrc) { |
| 3294 | if (ssrc == 0) { |
| 3295 | absl::optional<uint32_t> default_ssrc = GetDefaultReceiveStreamSsrc(); |
| 3296 | if (!default_ssrc) { |
| 3297 | return nullptr; |
| 3298 | } |
| 3299 | ssrc = *default_ssrc; |
| 3300 | } |
| 3301 | auto it = receive_streams_.find(ssrc); |
| 3302 | if (it != receive_streams_.end()) { |
| 3303 | return it->second; |
| 3304 | } |
| 3305 | return nullptr; |
| 3306 | } |
| 3307 |
| 3308 | void WebRtcVideoChannel::SetRecordableEncodedFrameCallback( |
| 3309 | uint32_t ssrc, |
| 3310 | std::function<void(const webrtc::RecordableEncodedFrame&)> callback) { |
| 3311 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 3312 | WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc); |
| 3313 | if (stream) { |
| 3314 | stream->SetRecordableEncodedFrameCallback(std::move(callback)); |
| 3315 | } else { |
| 3316 | RTC_LOG(LS_ERROR) << "Absent receive stream; ignoring setting encoded " |
| 3317 | "frame sink for ssrc " |
| 3318 | << ssrc; |
| 3319 | } |
| 3320 | } |
| 3321 |
| 3322 | void WebRtcVideoChannel::ClearRecordableEncodedFrameCallback(uint32_t ssrc) { |
| 3323 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 3324 | WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc); |
| 3325 | if (stream) { |
| 3326 | stream->ClearRecordableEncodedFrameCallback(); |
| 3327 | } else { |
| 3328 | RTC_LOG(LS_ERROR) << "Absent receive stream; ignoring clearing encoded " |
| 3329 | "frame sink for ssrc " |
| 3330 | << ssrc; |
| 3331 | } |
| 3332 | } |
| 3333 |
| 3334 | void WebRtcVideoChannel::GenerateKeyFrame(uint32_t ssrc) { |
| 3335 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 3336 | WebRtcVideoReceiveStream* stream = FindReceiveStream(ssrc); |
| 3337 | if (stream) { |
| 3338 | stream->GenerateKeyFrame(); |
| 3339 | } else { |
| 3340 | RTC_LOG(LS_ERROR) |
| 3341 | << "Absent receive stream; ignoring key frame generation for ssrc " |
| 3342 | << ssrc; |
| 3343 | } |
| 3344 | } |
| 3345 |
| 3346 | void WebRtcVideoChannel::SetEncoderToPacketizerFrameTransformer( |
| 3347 | uint32_t ssrc, |
| 3348 | rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) { |
| 3349 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 3350 | auto matching_stream = send_streams_.find(ssrc); |
| 3351 | if (matching_stream != send_streams_.end()) { |
| 3352 | matching_stream->second->SetEncoderToPacketizerFrameTransformer( |
| 3353 | std::move(frame_transformer)); |
| 3354 | } |
| 3355 | } |
| 3356 |
| 3357 | void WebRtcVideoChannel::SetDepacketizerToDecoderFrameTransformer( |
| 3358 | uint32_t ssrc, |
| 3359 | rtc::scoped_refptr<webrtc::FrameTransformerInterface> frame_transformer) { |
| 3360 | RTC_DCHECK(frame_transformer); |
| 3361 | RTC_DCHECK_RUN_ON(&thread_checker_); |
| 3362 | if (ssrc == 0) { |
| 3363 | // If the receiver is unsignaled, save the frame transformer and set it when |
| 3364 | // the stream is associated with an ssrc. |
| 3365 | unsignaled_frame_transformer_ = std::move(frame_transformer); |
| 3366 | return; |
| 3367 | } |
| 3368 |
| 3369 | auto matching_stream = receive_streams_.find(ssrc); |
| 3370 | if (matching_stream != receive_streams_.end()) { |
| 3371 | matching_stream->second->SetDepacketizerToDecoderFrameTransformer( |
| 3372 | std::move(frame_transformer)); |
| 3373 | } |
| 3374 | } |
| 3375 |
| 3376 | // TODO(bugs.webrtc.org/8785): Consider removing max_qp as member of |
| 3377 | // EncoderStreamFactory and instead set this value individually for each stream |
| 3378 | // in the VideoEncoderConfig.simulcast_layers. |
| 3379 | EncoderStreamFactory::EncoderStreamFactory(std::string codec_name, |
| 3380 | int max_qp, |
| 3381 | bool is_screenshare, |
| 3382 | bool conference_mode) |
| 3383 |
| 3384 | : codec_name_(codec_name), |
| 3385 | max_qp_(max_qp), |
| 3386 | is_screenshare_(is_screenshare), |
| 3387 | conference_mode_(conference_mode) {} |
| 3388 |
| 3389 | std::vector<webrtc::VideoStream> EncoderStreamFactory::CreateEncoderStreams( |
| 3390 | int width, |
| 3391 | int height, |
| 3392 | const webrtc::VideoEncoderConfig& encoder_config) { |
| 3393 | RTC_DCHECK_GT(encoder_config.number_of_streams, 0); |
| 3394 | RTC_DCHECK_GE(encoder_config.simulcast_layers.size(), |
| 3395 | encoder_config.number_of_streams); |
| 3396 |
| 3397 | const absl::optional<webrtc::DataRate> experimental_min_bitrate = |
| 3398 | GetExperimentalMinVideoBitrate(encoder_config.codec_type); |
| 3399 |
| 3400 | if (encoder_config.number_of_streams > 1 || |
| 3401 | ((absl::EqualsIgnoreCase(codec_name_, kVp8CodecName) || |
| 3402 | absl::EqualsIgnoreCase(codec_name_, kH264CodecName) || |
| 3403 | absl::EqualsIgnoreCase(codec_name_, kH265CodecName)) && |
| 3404 | is_screenshare_ && conference_mode_)) { |
| 3405 | return CreateSimulcastOrConferenceModeScreenshareStreams( |
| 3406 | width, height, encoder_config, experimental_min_bitrate); |
| 3407 | } |
| 3408 |
| 3409 | return CreateDefaultVideoStreams(width, height, encoder_config, |
| 3410 | experimental_min_bitrate); |
| 3411 | } |
| 3412 |
| 3413 | std::vector<webrtc::VideoStream> |
| 3414 | EncoderStreamFactory::CreateDefaultVideoStreams( |
| 3415 | int width, |
| 3416 | int height, |
| 3417 | const webrtc::VideoEncoderConfig& encoder_config, |
| 3418 | const absl::optional<webrtc::DataRate>& experimental_min_bitrate) const { |
| 3419 | std::vector<webrtc::VideoStream> layers; |
| 3420 |
| 3421 | // For unset max bitrates set default bitrate for non-simulcast. |
| 3422 | int max_bitrate_bps = |
| 3423 | (encoder_config.max_bitrate_bps > 0) |
| 3424 | ? encoder_config.max_bitrate_bps |
| 3425 | : GetMaxDefaultVideoBitrateKbps(width, height, is_screenshare_) * |
| 3426 | 1000; |
| 3427 |
| 3428 | int min_bitrate_bps = |
| 3429 | experimental_min_bitrate |
| 3430 | ? rtc::saturated_cast<int>(experimental_min_bitrate->bps()) |
| 3431 | : webrtc::kDefaultMinVideoBitrateBps; |
| 3432 | if (encoder_config.simulcast_layers[0].min_bitrate_bps > 0) { |
| 3433 | // Use set min bitrate. |
| 3434 | min_bitrate_bps = encoder_config.simulcast_layers[0].min_bitrate_bps; |
| 3435 | // If only min bitrate is configured, make sure max is above min. |
| 3436 | if (encoder_config.max_bitrate_bps <= 0) |
| 3437 | max_bitrate_bps = std::max(min_bitrate_bps, max_bitrate_bps); |
| 3438 | } |
| 3439 | int max_framerate = (encoder_config.simulcast_layers[0].max_framerate > 0) |
| 3440 | ? encoder_config.simulcast_layers[0].max_framerate |
| 3441 | : kDefaultVideoMaxFramerate; |
| 3442 |
| 3443 | webrtc::VideoStream layer; |
| 3444 | layer.width = width; |
| 3445 | layer.height = height; |
| 3446 | layer.max_framerate = max_framerate; |
| 3447 |
| 3448 | if (encoder_config.simulcast_layers[0].scale_resolution_down_by > 1.) { |
| 3449 | layer.width = std::max<size_t>( |
| 3450 | layer.width / |
| 3451 | encoder_config.simulcast_layers[0].scale_resolution_down_by, |
| 3452 | kMinLayerSize); |
| 3453 | layer.height = std::max<size_t>( |
| 3454 | layer.height / |
| 3455 | encoder_config.simulcast_layers[0].scale_resolution_down_by, |
| 3456 | kMinLayerSize); |
| 3457 | } |
| 3458 |
| 3459 | // In the case that the application sets a max bitrate that's lower than the |
| 3460 | // min bitrate, we adjust it down (see bugs.webrtc.org/9141). |
| 3461 | layer.min_bitrate_bps = std::min(min_bitrate_bps, max_bitrate_bps); |
| 3462 | if (encoder_config.simulcast_layers[0].target_bitrate_bps <= 0) { |
| 3463 | layer.target_bitrate_bps = max_bitrate_bps; |
| 3464 | } else { |
| 3465 | layer.target_bitrate_bps = |
| 3466 | encoder_config.simulcast_layers[0].target_bitrate_bps; |
| 3467 | } |
| 3468 | layer.max_bitrate_bps = max_bitrate_bps; |
| 3469 | layer.max_qp = max_qp_; |
| 3470 | layer.bitrate_priority = encoder_config.bitrate_priority; |
| 3471 |
| 3472 | if (absl::EqualsIgnoreCase(codec_name_, kVp9CodecName)) { |
| 3473 | RTC_DCHECK(encoder_config.encoder_specific_settings); |
| 3474 | // Use VP9 SVC layering from codec settings which might be initialized |
| 3475 | // though field trial in ConfigureVideoEncoderSettings. |
| 3476 | webrtc::VideoCodecVP9 vp9_settings; |
| 3477 | encoder_config.encoder_specific_settings->FillVideoCodecVp9(&vp9_settings); |
| 3478 | layer.num_temporal_layers = vp9_settings.numberOfTemporalLayers; |
| 3479 | } |
| 3480 |
| 3481 | if (IsTemporalLayersSupported(codec_name_)) { |
| 3482 | // Use configured number of temporal layers if set. |
| 3483 | if (encoder_config.simulcast_layers[0].num_temporal_layers) { |
| 3484 | layer.num_temporal_layers = |
| 3485 | *encoder_config.simulcast_layers[0].num_temporal_layers; |
| 3486 | } |
| 3487 | } |
| 3488 |
| 3489 | layers.push_back(layer); |
| 3490 | return layers; |
| 3491 | } |
| 3492 |
| 3493 | std::vector<webrtc::VideoStream> |
| 3494 | EncoderStreamFactory::CreateSimulcastOrConferenceModeScreenshareStreams( |
| 3495 | int width, |
| 3496 | int height, |
| 3497 | const webrtc::VideoEncoderConfig& encoder_config, |
| 3498 | const absl::optional<webrtc::DataRate>& experimental_min_bitrate) const { |
| 3499 | std::vector<webrtc::VideoStream> layers; |
| 3500 |
| 3501 | const bool temporal_layers_supported = |
| 3502 | absl::EqualsIgnoreCase(codec_name_, kVp8CodecName) || |
| 3503 | absl::EqualsIgnoreCase(codec_name_, kH264CodecName); |
| 3504 | // Use legacy simulcast screenshare if conference mode is explicitly enabled |
| 3505 | // or use the regular simulcast configuration path which is generic. |
| 3506 | layers = GetSimulcastConfig(FindRequiredActiveLayers(encoder_config), |
| 3507 | encoder_config.number_of_streams, width, height, |
| 3508 | encoder_config.bitrate_priority, max_qp_, |
| 3509 | is_screenshare_ && conference_mode_, |
| 3510 | temporal_layers_supported); |
| 3511 | // Allow an experiment to override the minimum bitrate for the lowest |
| 3512 | // spatial layer. The experiment's configuration has the lowest priority. |
| 3513 | if (experimental_min_bitrate) { |
| 3514 | layers[0].min_bitrate_bps = |
| 3515 | rtc::saturated_cast<int>(experimental_min_bitrate->bps()); |
| 3516 | } |
| 3517 | // Update the active simulcast layers and configured bitrates. |
| 3518 | bool is_highest_layer_max_bitrate_configured = false; |
| 3519 | const bool has_scale_resolution_down_by = absl::c_any_of( |
| 3520 | encoder_config.simulcast_layers, [](const webrtc::VideoStream& layer) { |
| 3521 | return layer.scale_resolution_down_by != -1.; |
| 3522 | }); |
| 3523 | const int normalized_width = |
| 3524 | NormalizeSimulcastSize(width, encoder_config.number_of_streams); |
| 3525 | const int normalized_height = |
| 3526 | NormalizeSimulcastSize(height, encoder_config.number_of_streams); |
| 3527 | for (size_t i = 0; i < layers.size(); ++i) { |
| 3528 | layers[i].active = encoder_config.simulcast_layers[i].active; |
| 3529 | // Update with configured num temporal layers if supported by codec. |
| 3530 | if (encoder_config.simulcast_layers[i].num_temporal_layers && |
| 3531 | IsTemporalLayersSupported(codec_name_)) { |
| 3532 | layers[i].num_temporal_layers = |
| 3533 | *encoder_config.simulcast_layers[i].num_temporal_layers; |
| 3534 | } |
| 3535 | if (encoder_config.simulcast_layers[i].max_framerate > 0) { |
| 3536 | layers[i].max_framerate = |
| 3537 | encoder_config.simulcast_layers[i].max_framerate; |
| 3538 | } |
| 3539 | if (has_scale_resolution_down_by) { |
| 3540 | const double scale_resolution_down_by = std::max( |
| 3541 | encoder_config.simulcast_layers[i].scale_resolution_down_by, 1.0); |
| 3542 | layers[i].width = std::max( |
| 3543 | static_cast<int>(normalized_width / scale_resolution_down_by), |
| 3544 | kMinLayerSize); |
| 3545 | layers[i].height = std::max( |
| 3546 | static_cast<int>(normalized_height / scale_resolution_down_by), |
| 3547 | kMinLayerSize); |
| 3548 | } |
| 3549 | // Update simulcast bitrates with configured min and max bitrate. |
| 3550 | if (encoder_config.simulcast_layers[i].min_bitrate_bps > 0) { |
| 3551 | layers[i].min_bitrate_bps = |
| 3552 | encoder_config.simulcast_layers[i].min_bitrate_bps; |
| 3553 | } |
| 3554 | if (encoder_config.simulcast_layers[i].max_bitrate_bps > 0) { |
| 3555 | layers[i].max_bitrate_bps = |
| 3556 | encoder_config.simulcast_layers[i].max_bitrate_bps; |
| 3557 | } |
| 3558 | if (encoder_config.simulcast_layers[i].target_bitrate_bps > 0) { |
| 3559 | layers[i].target_bitrate_bps = |
| 3560 | encoder_config.simulcast_layers[i].target_bitrate_bps; |
| 3561 | } |
| 3562 | if (encoder_config.simulcast_layers[i].min_bitrate_bps > 0 && |
| 3563 | encoder_config.simulcast_layers[i].max_bitrate_bps > 0) { |
| 3564 | // Min and max bitrate are configured. |
| 3565 | // Set target to 3/4 of the max bitrate (or to max if below min). |
| 3566 | if (encoder_config.simulcast_layers[i].target_bitrate_bps <= 0) |
| 3567 | layers[i].target_bitrate_bps = layers[i].max_bitrate_bps * 3 / 4; |
| 3568 | if (layers[i].target_bitrate_bps < layers[i].min_bitrate_bps) |
| 3569 | layers[i].target_bitrate_bps = layers[i].max_bitrate_bps; |
| 3570 | } else if (encoder_config.simulcast_layers[i].min_bitrate_bps > 0) { |
| 3571 | // Only min bitrate is configured, make sure target/max are above min. |
| 3572 | layers[i].target_bitrate_bps = |
| 3573 | std::max(layers[i].target_bitrate_bps, layers[i].min_bitrate_bps); |
| 3574 | layers[i].max_bitrate_bps = |
| 3575 | std::max(layers[i].max_bitrate_bps, layers[i].min_bitrate_bps); |
| 3576 | } else if (encoder_config.simulcast_layers[i].max_bitrate_bps > 0) { |
| 3577 | // Only max bitrate is configured, make sure min/target are below max. |
| 3578 | layers[i].min_bitrate_bps = |
| 3579 | std::min(layers[i].min_bitrate_bps, layers[i].max_bitrate_bps); |
| 3580 | layers[i].target_bitrate_bps = |
| 3581 | std::min(layers[i].target_bitrate_bps, layers[i].max_bitrate_bps); |
| 3582 | } |
| 3583 | if (i == layers.size() - 1) { |
| 3584 | is_highest_layer_max_bitrate_configured = |
| 3585 | encoder_config.simulcast_layers[i].max_bitrate_bps > 0; |
| 3586 | } |
| 3587 | } |
| 3588 | if (!is_screenshare_ && !is_highest_layer_max_bitrate_configured && |
| 3589 | encoder_config.max_bitrate_bps > 0) { |
| 3590 | // No application-configured maximum for the largest layer. |
| 3591 | // If there is bitrate leftover, give it to the largest layer. |
| 3592 | BoostMaxSimulcastLayer( |
| 3593 | webrtc::DataRate::BitsPerSec(encoder_config.max_bitrate_bps), &layers); |
| 3594 | } |
| 3595 | return layers; |
| 3596 | } |
| 3597 |
| 3598 | } // namespace cricket |
| 3599 |