| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 |
| 11 | #include "call/rtp_video_sender.h" |
| 12 |
| 13 | #include <algorithm> |
| 14 | #include <memory> |
| 15 | #include <string> |
| 16 | #include <utility> |
| 17 |
| 18 | #include "absl/algorithm/container.h" |
| 19 | #include "absl/strings/match.h" |
| 20 | #include "absl/strings/string_view.h" |
| 21 | #include "api/array_view.h" |
| 22 | #include "api/task_queue/task_queue_factory.h" |
| 23 | #include "api/transport/field_trial_based_config.h" |
| 24 | #include "api/units/time_delta.h" |
| 25 | #include "api/video_codecs/video_codec.h" |
| 26 | #include "call/rtp_transport_controller_send_interface.h" |
| 27 | #include "modules/pacing/packet_router.h" |
| 28 | #include "modules/rtp_rtcp/include/rtp_rtcp_defines.h" |
| 29 | #include "modules/rtp_rtcp/source/rtp_rtcp_impl2.h" |
| 30 | #include "modules/rtp_rtcp/source/rtp_sender.h" |
| 31 | #include "modules/video_coding/include/video_codec_interface.h" |
| 32 | #include "rtc_base/checks.h" |
| 33 | #include "rtc_base/logging.h" |
| 34 | #include "rtc_base/task_queue.h" |
| 35 | #include "rtc_base/trace_event.h" |
| 36 |
| 37 | namespace webrtc { |
| 38 |
| 39 | namespace webrtc_internal_rtp_video_sender { |
| 40 |
| 41 | RtpStreamSender::RtpStreamSender( |
| 42 | std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp, |
| 43 | std::unique_ptr<RTPSenderVideo> sender_video, |
| 44 | std::unique_ptr<VideoFecGenerator> fec_generator) |
| 45 | : rtp_rtcp(std::move(rtp_rtcp)), |
| 46 | sender_video(std::move(sender_video)), |
| 47 | fec_generator(std::move(fec_generator)) {} |
| 48 |
| 49 | RtpStreamSender::~RtpStreamSender() = default; |
| 50 |
| 51 | } // namespace webrtc_internal_rtp_video_sender |
| 52 |
| 53 | namespace { |
| 54 | static const int kMinSendSidePacketHistorySize = 600; |
| 55 | // We don't do MTU discovery, so assume that we have the standard ethernet MTU. |
| 56 | static const size_t kPathMTU = 1500; |
| 57 |
| 58 | using webrtc_internal_rtp_video_sender::RtpStreamSender; |
| 59 |
| 60 | bool PayloadTypeSupportsSkippingFecPackets(absl::string_view payload_name, |
| 61 | const FieldTrialsView& trials) { |
| 62 | const VideoCodecType codecType = |
| 63 | PayloadStringToCodecType(std::string(payload_name)); |
| 64 | if (codecType == kVideoCodecVP8 || codecType == kVideoCodecVP9) { |
| 65 | return true; |
| 66 | } |
| 67 | if (codecType == kVideoCodecGeneric && |
| 68 | absl::StartsWith(trials.Lookup("WebRTC-GenericPictureId"), "Enabled")) { |
| 69 | return true; |
| 70 | } |
| 71 | return false; |
| 72 | } |
| 73 |
| 74 | bool ShouldDisableRedAndUlpfec(bool flexfec_enabled, |
| 75 | const RtpConfig& rtp_config, |
| 76 | const FieldTrialsView& trials) { |
| 77 | // Consistency of NACK and RED+ULPFEC parameters is checked in this function. |
| 78 | const bool nack_enabled = rtp_config.nack.rtp_history_ms > 0; |
| 79 |
| 80 | // Shorthands. |
| 81 | auto IsRedEnabled = [&]() { return rtp_config.ulpfec.red_payload_type >= 0; }; |
| 82 | auto IsUlpfecEnabled = [&]() { |
| 83 | return rtp_config.ulpfec.ulpfec_payload_type >= 0; |
| 84 | }; |
| 85 |
| 86 | bool should_disable_red_and_ulpfec = false; |
| 87 |
| 88 | if (absl::StartsWith(trials.Lookup("WebRTC-DisableUlpFecExperiment"), |
| 89 | "Enabled")) { |
| 90 | RTC_LOG(LS_INFO) << "Experiment to disable sending ULPFEC is enabled."; |
| 91 | should_disable_red_and_ulpfec = true; |
| 92 | } |
| 93 |
| 94 | // If enabled, FlexFEC takes priority over RED+ULPFEC. |
| 95 | if (flexfec_enabled) { |
| 96 | if (IsUlpfecEnabled()) { |
| 97 | RTC_LOG(LS_INFO) |
| 98 | << "Both FlexFEC and ULPFEC are configured. Disabling ULPFEC."; |
| 99 | } |
| 100 | should_disable_red_and_ulpfec = true; |
| 101 | } |
| 102 |
| 103 | // Payload types without picture ID cannot determine that a stream is complete |
| 104 | // without retransmitting FEC, so using ULPFEC + NACK for H.264 (for instance) |
| 105 | // is a waste of bandwidth since FEC packets still have to be transmitted. |
| 106 | // Note that this is not the case with FlexFEC. |
| 107 | if (nack_enabled && IsUlpfecEnabled() && |
| 108 | !PayloadTypeSupportsSkippingFecPackets(rtp_config.payload_name, trials)) { |
| 109 | RTC_LOG(LS_WARNING) |
| 110 | << "Transmitting payload type without picture ID using " |
| 111 | "NACK+ULPFEC is a waste of bandwidth since ULPFEC packets " |
| 112 | "also have to be retransmitted. Disabling ULPFEC."; |
| 113 | should_disable_red_and_ulpfec = true; |
| 114 | } |
| 115 |
| 116 | // Verify payload types. |
| 117 | if (IsUlpfecEnabled() ^ IsRedEnabled()) { |
| 118 | RTC_LOG(LS_WARNING) |
| 119 | << "Only RED or only ULPFEC enabled, but not both. Disabling both."; |
| 120 | should_disable_red_and_ulpfec = true; |
| 121 | } |
| 122 |
| 123 | return should_disable_red_and_ulpfec; |
| 124 | } |
| 125 |
| 126 | // TODO(brandtr): Update this function when we support multistream protection. |
| 127 | std::unique_ptr<VideoFecGenerator> MaybeCreateFecGenerator( |
| 128 | Clock* clock, |
| 129 | const RtpConfig& rtp, |
| 130 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 131 | int simulcast_index, |
| 132 | const FieldTrialsView& trials) { |
| 133 | // If flexfec is configured that takes priority. |
| 134 | if (rtp.flexfec.payload_type >= 0) { |
| 135 | RTC_DCHECK_GE(rtp.flexfec.payload_type, 0); |
| 136 | RTC_DCHECK_LE(rtp.flexfec.payload_type, 127); |
| 137 | if (rtp.flexfec.ssrc == 0) { |
| 138 | RTC_LOG(LS_WARNING) << "FlexFEC is enabled, but no FlexFEC SSRC given. " |
| 139 | "Therefore disabling FlexFEC."; |
| 140 | return nullptr; |
| 141 | } |
| 142 | if (rtp.flexfec.protected_media_ssrcs.empty()) { |
| 143 | RTC_LOG(LS_WARNING) |
| 144 | << "FlexFEC is enabled, but no protected media SSRC given. " |
| 145 | "Therefore disabling FlexFEC."; |
| 146 | return nullptr; |
| 147 | } |
| 148 |
| 149 | if (rtp.flexfec.protected_media_ssrcs.size() > 1) { |
| 150 | RTC_LOG(LS_WARNING) |
| 151 | << "The supplied FlexfecConfig contained multiple protected " |
| 152 | "media streams, but our implementation currently only " |
| 153 | "supports protecting a single media stream. " |
| 154 | "To avoid confusion, disabling FlexFEC completely."; |
| 155 | return nullptr; |
| 156 | } |
| 157 |
| 158 | if (absl::c_find(rtp.flexfec.protected_media_ssrcs, |
| 159 | rtp.ssrcs[simulcast_index]) == |
| 160 | rtp.flexfec.protected_media_ssrcs.end()) { |
| 161 | // Media SSRC not among flexfec protected SSRCs. |
| 162 | return nullptr; |
| 163 | } |
| 164 |
| 165 | const RtpState* rtp_state = nullptr; |
| 166 | auto it = suspended_ssrcs.find(rtp.flexfec.ssrc); |
| 167 | if (it != suspended_ssrcs.end()) { |
| 168 | rtp_state = &it->second; |
| 169 | } |
| 170 |
| 171 | RTC_DCHECK_EQ(1U, rtp.flexfec.protected_media_ssrcs.size()); |
| 172 | return std::make_unique<FlexfecSender>( |
| 173 | rtp.flexfec.payload_type, rtp.flexfec.ssrc, |
| 174 | rtp.flexfec.protected_media_ssrcs[0], rtp.mid, rtp.extensions, |
| 175 | RTPSender::FecExtensionSizes(), rtp_state, clock); |
| 176 | } else if (rtp.ulpfec.red_payload_type >= 0 && |
| 177 | rtp.ulpfec.ulpfec_payload_type >= 0 && |
| 178 | !ShouldDisableRedAndUlpfec(/*flexfec_enabled=*/false, rtp, |
| 179 | trials)) { |
| 180 | // Flexfec not configured, but ulpfec is and is not disabled. |
| 181 | return std::make_unique<UlpfecGenerator>( |
| 182 | rtp.ulpfec.red_payload_type, rtp.ulpfec.ulpfec_payload_type, clock); |
| 183 | } |
| 184 |
| 185 | // Not a single FEC is given. |
| 186 | return nullptr; |
| 187 | } |
| 188 |
| 189 | std::vector<RtpStreamSender> CreateRtpStreamSenders( |
| 190 | Clock* clock, |
| 191 | const RtpConfig& rtp_config, |
| 192 | const RtpSenderObservers& observers, |
| 193 | int rtcp_report_interval_ms, |
| 194 | Transport* send_transport, |
| 195 | RtpTransportControllerSendInterface* transport, |
| 196 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 197 | RtcEventLog* event_log, |
| 198 | RateLimiter* retransmission_rate_limiter, |
| 199 | FrameEncryptorInterface* frame_encryptor, |
| 200 | const CryptoOptions& crypto_options, |
| 201 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer, |
| 202 | const FieldTrialsView& trials, |
| 203 | TaskQueueFactory* task_queue_factory) { |
| 204 | RTC_DCHECK_GT(rtp_config.ssrcs.size(), 0); |
| 205 | RTC_DCHECK(task_queue_factory); |
| 206 |
| 207 | RtpRtcpInterface::Configuration configuration; |
| 208 | configuration.clock = clock; |
| 209 | configuration.audio = false; |
| 210 | configuration.receiver_only = false; |
| 211 | configuration.outgoing_transport = send_transport; |
| 212 | configuration.intra_frame_callback = observers.intra_frame_callback; |
| 213 | configuration.rtcp_loss_notification_observer = |
| 214 | observers.rtcp_loss_notification_observer; |
| 215 | configuration.network_link_rtcp_observer = transport->GetRtcpObserver(); |
| 216 | configuration.network_state_estimate_observer = |
| 217 | transport->network_state_estimate_observer(); |
| 218 | configuration.transport_feedback_callback = |
| 219 | transport->transport_feedback_observer(); |
| 220 | configuration.rtt_stats = observers.rtcp_rtt_stats; |
| 221 | configuration.rtcp_packet_type_counter_observer = |
| 222 | observers.rtcp_type_observer; |
| 223 | configuration.report_block_data_observer = |
| 224 | observers.report_block_data_observer; |
| 225 | configuration.paced_sender = transport->packet_sender(); |
| 226 | configuration.send_bitrate_observer = observers.bitrate_observer; |
| 227 | configuration.send_packet_observer = observers.send_packet_observer; |
| 228 | configuration.event_log = event_log; |
| 229 | if (trials.IsDisabled("WebRTC-DisableRtxRateLimiter")) { |
| 230 | configuration.retransmission_rate_limiter = retransmission_rate_limiter; |
| 231 | } |
| 232 | configuration.rtp_stats_callback = observers.rtp_stats; |
| 233 | configuration.frame_encryptor = frame_encryptor; |
| 234 | configuration.require_frame_encryption = |
| 235 | crypto_options.sframe.require_frame_encryption; |
| 236 | configuration.extmap_allow_mixed = rtp_config.extmap_allow_mixed; |
| 237 | configuration.rtcp_report_interval_ms = rtcp_report_interval_ms; |
| 238 | configuration.field_trials = &trials; |
| 239 | configuration.enable_send_packet_batching = |
| 240 | rtp_config.enable_send_packet_batching; |
| 241 |
| 242 | std::vector<RtpStreamSender> rtp_streams; |
| 243 |
| 244 | RTC_DCHECK(rtp_config.rtx.ssrcs.empty() || |
| 245 | rtp_config.rtx.ssrcs.size() == rtp_config.ssrcs.size()); |
| 246 |
| 247 | // Some streams could have been disabled, but the rids are still there. |
| 248 | // This will occur when simulcast has been disabled for a codec (e.g. VP9) |
| 249 | RTC_DCHECK(rtp_config.rids.empty() || |
| 250 | rtp_config.rids.size() >= rtp_config.ssrcs.size()); |
| 251 |
| 252 | for (size_t i = 0; i < rtp_config.ssrcs.size(); ++i) { |
| 253 | RTPSenderVideo::Config video_config; |
| 254 | configuration.local_media_ssrc = rtp_config.ssrcs[i]; |
| 255 |
| 256 | std::unique_ptr<VideoFecGenerator> fec_generator = |
| 257 | MaybeCreateFecGenerator(clock, rtp_config, suspended_ssrcs, i, trials); |
| 258 | configuration.fec_generator = fec_generator.get(); |
| 259 |
| 260 | configuration.rtx_send_ssrc = |
| 261 | rtp_config.GetRtxSsrcAssociatedWithMediaSsrc(rtp_config.ssrcs[i]); |
| 262 | RTC_DCHECK_EQ(configuration.rtx_send_ssrc.has_value(), |
| 263 | !rtp_config.rtx.ssrcs.empty()); |
| 264 |
| 265 | configuration.rid = (i < rtp_config.rids.size()) ? rtp_config.rids[i] : ""; |
| 266 |
| 267 | configuration.need_rtp_packet_infos = rtp_config.lntf.enabled; |
| 268 |
| 269 | std::unique_ptr<ModuleRtpRtcpImpl2> rtp_rtcp( |
| 270 | ModuleRtpRtcpImpl2::Create(configuration)); |
| 271 | rtp_rtcp->SetSendingStatus(false); |
| 272 | rtp_rtcp->SetSendingMediaStatus(false); |
| 273 | rtp_rtcp->SetRTCPStatus(RtcpMode::kCompound); |
| 274 | // Set NACK. |
| 275 | rtp_rtcp->SetStorePacketsStatus(true, kMinSendSidePacketHistorySize); |
| 276 |
| 277 | video_config.clock = configuration.clock; |
| 278 | video_config.rtp_sender = rtp_rtcp->RtpSender(); |
| 279 | video_config.frame_encryptor = frame_encryptor; |
| 280 | video_config.require_frame_encryption = |
| 281 | crypto_options.sframe.require_frame_encryption; |
| 282 | video_config.field_trials = &trials; |
| 283 | video_config.enable_retransmit_all_layers = |
| 284 | !video_config.field_trials->IsDisabled( |
| 285 | "WebRTC-Video-EnableRetransmitAllLayers"); |
| 286 |
| 287 | const bool using_flexfec = |
| 288 | fec_generator && |
| 289 | fec_generator->GetFecType() == VideoFecGenerator::FecType::kFlexFec; |
| 290 | const bool should_disable_red_and_ulpfec = |
| 291 | ShouldDisableRedAndUlpfec(using_flexfec, rtp_config, trials); |
| 292 | if (!should_disable_red_and_ulpfec && |
| 293 | rtp_config.ulpfec.red_payload_type != -1) { |
| 294 | video_config.red_payload_type = rtp_config.ulpfec.red_payload_type; |
| 295 | } |
| 296 | if (fec_generator) { |
| 297 | video_config.fec_type = fec_generator->GetFecType(); |
| 298 | video_config.fec_overhead_bytes = fec_generator->MaxPacketOverhead(); |
| 299 | } |
| 300 | video_config.frame_transformer = frame_transformer; |
| 301 | video_config.task_queue_factory = task_queue_factory; |
| 302 | auto sender_video = std::make_unique<RTPSenderVideo>(video_config); |
| 303 | rtp_streams.emplace_back(std::move(rtp_rtcp), std::move(sender_video), |
| 304 | std::move(fec_generator)); |
| 305 | } |
| 306 | return rtp_streams; |
| 307 | } |
| 308 |
| 309 | absl::optional<VideoCodecType> GetVideoCodecType(const RtpConfig& config) { |
| 310 | if (config.raw_payload) { |
| 311 | return absl::nullopt; |
| 312 | } |
| 313 | return PayloadStringToCodecType(config.payload_name); |
| 314 | } |
| 315 | bool TransportSeqNumExtensionConfigured(const RtpConfig& config) { |
| 316 | return absl::c_any_of(config.extensions, [](const RtpExtension& ext) { |
| 317 | return ext.uri == RtpExtension::kTransportSequenceNumberUri; |
| 318 | }); |
| 319 | } |
| 320 |
| 321 | // Returns true when some coded video sequence can be decoded starting with |
| 322 | // this frame without requiring any previous frames. |
| 323 | // e.g. it is the same as a key frame when spatial scalability is not used. |
| 324 | // When spatial scalability is used, then it is true for layer frames of |
| 325 | // a key frame without inter-layer dependencies. |
| 326 | bool IsFirstFrameOfACodedVideoSequence( |
| 327 | const EncodedImage& encoded_image, |
| 328 | const CodecSpecificInfo* codec_specific_info) { |
| 329 | if (encoded_image._frameType != VideoFrameType::kVideoFrameKey) { |
| 330 | return false; |
| 331 | } |
| 332 |
| 333 | if (codec_specific_info != nullptr) { |
| 334 | if (codec_specific_info->generic_frame_info.has_value()) { |
| 335 | // This function is used before |
| 336 | // `codec_specific_info->generic_frame_info->frame_diffs` are calculated, |
| 337 | // so need to use a more complicated way to check for presence of the |
| 338 | // dependencies. |
| 339 | return absl::c_none_of( |
| 340 | codec_specific_info->generic_frame_info->encoder_buffers, |
| 341 | [](const CodecBufferUsage& buffer) { return buffer.referenced; }); |
| 342 | } |
| 343 |
| 344 | if (codec_specific_info->codecType == VideoCodecType::kVideoCodecVP8 || |
| 345 | codec_specific_info->codecType == VideoCodecType::kVideoCodecH264 || |
| 346 | codec_specific_info->codecType == VideoCodecType::kVideoCodecGeneric) { |
| 347 | // These codecs do not support intra picture dependencies, so a frame |
| 348 | // marked as a key frame should be a key frame. |
| 349 | return true; |
| 350 | } |
| 351 | } |
| 352 |
| 353 | // Without depenedencies described in generic format do an educated guess. |
| 354 | // It might be wrong for VP9 with spatial layer 0 skipped or higher spatial |
| 355 | // layer not depending on the spatial layer 0. This corner case is unimportant |
| 356 | // for current usage of this helper function. |
| 357 |
| 358 | // Use <= to accept both 0 (i.e. the first) and nullopt (i.e. the only). |
| 359 | return encoded_image.SpatialIndex() <= 0; |
| 360 | } |
| 361 |
| 362 | } // namespace |
| 363 |
| 364 | RtpVideoSender::RtpVideoSender( |
| 365 | Clock* clock, |
| 366 | const std::map<uint32_t, RtpState>& suspended_ssrcs, |
| 367 | const std::map<uint32_t, RtpPayloadState>& states, |
| 368 | const RtpConfig& rtp_config, |
| 369 | int rtcp_report_interval_ms, |
| 370 | Transport* send_transport, |
| 371 | const RtpSenderObservers& observers, |
| 372 | RtpTransportControllerSendInterface* transport, |
| 373 | RtcEventLog* event_log, |
| 374 | RateLimiter* retransmission_limiter, |
| 375 | std::unique_ptr<FecController> fec_controller, |
| 376 | FrameEncryptorInterface* frame_encryptor, |
| 377 | const CryptoOptions& crypto_options, |
| 378 | rtc::scoped_refptr<FrameTransformerInterface> frame_transformer, |
| 379 | const FieldTrialsView& field_trials, |
| 380 | TaskQueueFactory* task_queue_factory) |
| 381 | : field_trials_(field_trials), |
| 382 | use_frame_rate_for_overhead_(absl::StartsWith( |
| 383 | field_trials_.Lookup("WebRTC-Video-UseFrameRateForOverhead"), |
| 384 | "Enabled")), |
| 385 | has_packet_feedback_(TransportSeqNumExtensionConfigured(rtp_config)), |
| 386 | active_(false), |
| 387 | fec_controller_(std::move(fec_controller)), |
| 388 | fec_allowed_(true), |
| 389 | rtp_streams_(CreateRtpStreamSenders(clock, |
| 390 | rtp_config, |
| 391 | observers, |
| 392 | rtcp_report_interval_ms, |
| 393 | send_transport, |
| 394 | transport, |
| 395 | suspended_ssrcs, |
| 396 | event_log, |
| 397 | retransmission_limiter, |
| 398 | frame_encryptor, |
| 399 | crypto_options, |
| 400 | std::move(frame_transformer), |
| 401 | field_trials_, |
| 402 | task_queue_factory)), |
| 403 | rtp_config_(rtp_config), |
| 404 | codec_type_(GetVideoCodecType(rtp_config)), |
| 405 | transport_(transport), |
| 406 | transport_overhead_bytes_per_packet_(0), |
| 407 | encoder_target_rate_bps_(0), |
| 408 | frame_counts_(rtp_config.ssrcs.size()), |
| 409 | frame_count_observer_(observers.frame_count_observer) { |
| 410 | transport_checker_.Detach(); |
| 411 | RTC_DCHECK_EQ(rtp_config_.ssrcs.size(), rtp_streams_.size()); |
| 412 | if (has_packet_feedback_) |
| 413 | transport_->IncludeOverheadInPacedSender(); |
| 414 | // SSRCs are assumed to be sorted in the same order as `rtp_modules`. |
| 415 | for (uint32_t ssrc : rtp_config_.ssrcs) { |
| 416 | // Restore state if it previously existed. |
| 417 | const RtpPayloadState* state = nullptr; |
| 418 | auto it = states.find(ssrc); |
| 419 | if (it != states.end()) { |
| 420 | state = &it->second; |
| 421 | shared_frame_id_ = std::max(shared_frame_id_, state->shared_frame_id); |
| 422 | } |
| 423 | params_.push_back(RtpPayloadParams(ssrc, state, field_trials_)); |
| 424 | } |
| 425 |
| 426 | // RTP/RTCP initialization. |
| 427 |
| 428 | for (size_t i = 0; i < rtp_config_.extensions.size(); ++i) { |
| 429 | const std::string& extension = rtp_config_.extensions[i].uri; |
| 430 | int id = rtp_config_.extensions[i].id; |
| 431 | RTC_DCHECK(RtpExtension::IsSupportedForVideo(extension)); |
| 432 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 433 | stream.rtp_rtcp->RegisterRtpHeaderExtension(extension, id); |
| 434 | } |
| 435 | } |
| 436 |
| 437 | ConfigureSsrcs(suspended_ssrcs); |
| 438 |
| 439 | if (!rtp_config_.mid.empty()) { |
| 440 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 441 | stream.rtp_rtcp->SetMid(rtp_config_.mid); |
| 442 | } |
| 443 | } |
| 444 |
| 445 | bool fec_enabled = false; |
| 446 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 447 | // Simulcast has one module for each layer. Set the CNAME on all modules. |
| 448 | stream.rtp_rtcp->SetCNAME(rtp_config_.c_name.c_str()); |
| 449 | stream.rtp_rtcp->SetMaxRtpPacketSize(rtp_config_.max_packet_size); |
| 450 | stream.rtp_rtcp->RegisterSendPayloadFrequency(rtp_config_.payload_type, |
| 451 | kVideoPayloadTypeFrequency); |
| 452 | if (stream.fec_generator != nullptr) { |
| 453 | fec_enabled = true; |
| 454 | } |
| 455 | } |
| 456 | // Currently, both ULPFEC and FlexFEC use the same FEC rate calculation logic, |
| 457 | // so enable that logic if either of those FEC schemes are enabled. |
| 458 | fec_controller_->SetProtectionMethod(fec_enabled, NackEnabled()); |
| 459 |
| 460 | fec_controller_->SetProtectionCallback(this); |
| 461 |
| 462 | // Construction happens on the worker thread (see Call::CreateVideoSendStream) |
| 463 | // but subseqeuent calls to the RTP state will happen on one of two threads: |
| 464 | // * The pacer thread for actually sending packets. |
| 465 | // * The transport thread when tearing down and quering GetRtpState(). |
| 466 | // Detach thread checkers. |
| 467 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 468 | stream.rtp_rtcp->OnPacketSendingThreadSwitched(); |
| 469 | } |
| 470 | } |
| 471 |
| 472 | RtpVideoSender::~RtpVideoSender() { |
| 473 | RTC_DCHECK_RUN_ON(&transport_checker_); |
| 474 | SetActiveModulesLocked( |
| 475 | /*sending=*/false); |
| 476 | } |
| 477 |
| 478 | void RtpVideoSender::SetSending(bool enabled) { |
| 479 | RTC_DCHECK_RUN_ON(&transport_checker_); |
| 480 | MutexLock lock(&mutex_); |
| 481 | if (enabled == active_) { |
| 482 | return; |
| 483 | } |
| 484 | SetActiveModulesLocked(/*sending=*/enabled); |
| 485 | } |
| 486 |
| 487 | void RtpVideoSender::SetActiveModulesLocked(bool sending) { |
| 488 | RTC_DCHECK_RUN_ON(&transport_checker_); |
| 489 | if (active_ == sending) { |
| 490 | return; |
| 491 | } |
| 492 | active_ = sending; |
| 493 | for (size_t i = 0; i < rtp_streams_.size(); ++i) { |
| 494 | RtpRtcpInterface& rtp_module = *rtp_streams_[i].rtp_rtcp; |
| 495 | rtp_module.SetSendingStatus(sending); |
| 496 | rtp_module.SetSendingMediaStatus(sending); |
| 497 | if (sending) { |
| 498 | transport_->RegisterSendingRtpStream(rtp_module); |
| 499 | } else { |
| 500 | transport_->DeRegisterSendingRtpStream(rtp_module); |
| 501 | } |
| 502 | } |
| 503 | auto* feedback_provider = transport_->GetStreamFeedbackProvider(); |
| 504 | if (!sending) { |
| 505 | feedback_provider->DeRegisterStreamFeedbackObserver(this); |
| 506 | } else { |
| 507 | feedback_provider->RegisterStreamFeedbackObserver(rtp_config_.ssrcs, this); |
| 508 | } |
| 509 | } |
| 510 |
| 511 | bool RtpVideoSender::IsActive() { |
| 512 | RTC_DCHECK_RUN_ON(&transport_checker_); |
| 513 | MutexLock lock(&mutex_); |
| 514 | return IsActiveLocked(); |
| 515 | } |
| 516 |
| 517 | bool RtpVideoSender::IsActiveLocked() { |
| 518 | return active_ && !rtp_streams_.empty(); |
| 519 | } |
| 520 |
| 521 | EncodedImageCallback::Result RtpVideoSender::OnEncodedImage( |
| 522 | const EncodedImage& encoded_image, |
| 523 | const CodecSpecificInfo* codec_specific_info) { |
| 524 | fec_controller_->UpdateWithEncodedData(encoded_image.size(), |
| 525 | encoded_image._frameType); |
| 526 | MutexLock lock(&mutex_); |
| 527 | RTC_DCHECK(!rtp_streams_.empty()); |
| 528 | if (!active_) |
| 529 | return Result(Result::ERROR_SEND_FAILED); |
| 530 |
| 531 | shared_frame_id_++; |
| 532 | size_t simulcast_index = encoded_image.SimulcastIndex().value_or(0); |
| 533 | RTC_DCHECK_LT(simulcast_index, rtp_streams_.size()); |
| 534 |
| 535 | uint32_t rtp_timestamp = |
| 536 | encoded_image.RtpTimestamp() + |
| 537 | rtp_streams_[simulcast_index].rtp_rtcp->StartTimestamp(); |
| 538 |
| 539 | // RTCPSender has it's own copy of the timestamp offset, added in |
| 540 | // RTCPSender::BuildSR, hence we must not add the in the offset for this call. |
| 541 | // TODO(nisse): Delete RTCPSender:timestamp_offset_, and see if we can confine |
| 542 | // knowledge of the offset to a single place. |
| 543 | if (!rtp_streams_[simulcast_index].rtp_rtcp->OnSendingRtpFrame( |
| 544 | encoded_image.RtpTimestamp(), encoded_image.capture_time_ms_, |
| 545 | rtp_config_.payload_type, |
| 546 | encoded_image._frameType == VideoFrameType::kVideoFrameKey)) { |
| 547 | // The payload router could be active but this module isn't sending. |
| 548 | return Result(Result::ERROR_SEND_FAILED); |
| 549 | } |
| 550 |
| 551 | TimeDelta expected_retransmission_time = TimeDelta::PlusInfinity(); |
| 552 | if (encoded_image.RetransmissionAllowed()) { |
| 553 | expected_retransmission_time = |
| 554 | rtp_streams_[simulcast_index].rtp_rtcp->ExpectedRetransmissionTime(); |
| 555 | } |
| 556 |
| 557 | if (IsFirstFrameOfACodedVideoSequence(encoded_image, codec_specific_info)) { |
| 558 | // In order to use the dependency descriptor RTP header extension: |
| 559 | // - Pass along any `FrameDependencyStructure` templates produced by the |
| 560 | // encoder adapter. |
| 561 | // - If none were produced the `RtpPayloadParams::*ToGeneric` for the |
| 562 | // particular codec have simulated a dependency structure, so provide a |
| 563 | // minimal set of templates. |
| 564 | // - Otherwise, don't pass along any templates at all which will disable |
| 565 | // the generation of a dependency descriptor. |
| 566 | RTPSenderVideo& sender_video = *rtp_streams_[simulcast_index].sender_video; |
| 567 | if (codec_specific_info && codec_specific_info->template_structure) { |
| 568 | sender_video.SetVideoStructure(&*codec_specific_info->template_structure); |
| 569 | } else if (absl::optional<FrameDependencyStructure> structure = |
| 570 | params_[simulcast_index].GenericStructure( |
| 571 | codec_specific_info)) { |
| 572 | sender_video.SetVideoStructure(&*structure); |
| 573 | } else { |
| 574 | sender_video.SetVideoStructure(nullptr); |
| 575 | } |
| 576 | } |
| 577 |
| 578 | bool send_result = |
| 579 | rtp_streams_[simulcast_index].sender_video->SendEncodedImage( |
| 580 | rtp_config_.payload_type, codec_type_, rtp_timestamp, encoded_image, |
| 581 | params_[simulcast_index].GetRtpVideoHeader( |
| 582 | encoded_image, codec_specific_info, shared_frame_id_), |
| 583 | expected_retransmission_time); |
| 584 | if (frame_count_observer_) { |
| 585 | FrameCounts& counts = frame_counts_[simulcast_index]; |
| 586 | if (encoded_image._frameType == VideoFrameType::kVideoFrameKey) { |
| 587 | ++counts.key_frames; |
| 588 | } else if (encoded_image._frameType == VideoFrameType::kVideoFrameDelta) { |
| 589 | ++counts.delta_frames; |
| 590 | } else { |
| 591 | RTC_DCHECK(encoded_image._frameType == VideoFrameType::kEmptyFrame); |
| 592 | } |
| 593 | frame_count_observer_->FrameCountUpdated( |
| 594 | counts, rtp_config_.ssrcs[simulcast_index]); |
| 595 | } |
| 596 | if (!send_result) |
| 597 | return Result(Result::ERROR_SEND_FAILED); |
| 598 |
| 599 | return Result(Result::OK, rtp_timestamp); |
| 600 | } |
| 601 |
| 602 | void RtpVideoSender::OnBitrateAllocationUpdated( |
| 603 | const VideoBitrateAllocation& bitrate) { |
| 604 | RTC_DCHECK_RUN_ON(&transport_checker_); |
| 605 | MutexLock lock(&mutex_); |
| 606 | if (IsActiveLocked()) { |
| 607 | if (rtp_streams_.size() == 1) { |
| 608 | // If spatial scalability is enabled, it is covered by a single stream. |
| 609 | rtp_streams_[0].rtp_rtcp->SetVideoBitrateAllocation(bitrate); |
| 610 | } else { |
| 611 | std::vector<absl::optional<VideoBitrateAllocation>> layer_bitrates = |
| 612 | bitrate.GetSimulcastAllocations(); |
| 613 | // Simulcast is in use, split the VideoBitrateAllocation into one struct |
| 614 | // per rtp stream, moving over the temporal layer allocation. |
| 615 | for (size_t i = 0; i < rtp_streams_.size(); ++i) { |
| 616 | // The next spatial layer could be used if the current one is |
| 617 | // inactive. |
| 618 | if (layer_bitrates[i]) { |
| 619 | rtp_streams_[i].rtp_rtcp->SetVideoBitrateAllocation( |
| 620 | *layer_bitrates[i]); |
| 621 | } else { |
| 622 | // Signal a 0 bitrate on a simulcast stream. |
| 623 | rtp_streams_[i].rtp_rtcp->SetVideoBitrateAllocation( |
| 624 | VideoBitrateAllocation()); |
| 625 | } |
| 626 | } |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | void RtpVideoSender::OnVideoLayersAllocationUpdated( |
| 631 | const VideoLayersAllocation& allocation) { |
| 632 | MutexLock lock(&mutex_); |
| 633 | if (IsActiveLocked()) { |
| 634 | for (size_t i = 0; i < rtp_streams_.size(); ++i) { |
| 635 | VideoLayersAllocation stream_allocation = allocation; |
| 636 | stream_allocation.rtp_stream_index = i; |
| 637 | rtp_streams_[i].sender_video->SetVideoLayersAllocation( |
| 638 | std::move(stream_allocation)); |
| 639 | // Only send video frames on the rtp module if the encoder is configured |
| 640 | // to send. This is to prevent stray frames to be sent after an encoder |
| 641 | // has been reconfigured. |
| 642 | rtp_streams_[i].rtp_rtcp->SetSendingMediaStatus( |
| 643 | absl::c_any_of(allocation.active_spatial_layers, |
| 644 | [&i](const VideoLayersAllocation::SpatialLayer layer) { |
| 645 | return layer.rtp_stream_index == static_cast<int>(i); |
| 646 | })); |
| 647 | } |
| 648 | } |
| 649 | } |
| 650 |
| 651 | bool RtpVideoSender::NackEnabled() const { |
| 652 | const bool nack_enabled = rtp_config_.nack.rtp_history_ms > 0; |
| 653 | return nack_enabled; |
| 654 | } |
| 655 |
| 656 | DataRate RtpVideoSender::GetPostEncodeOverhead() const { |
| 657 | DataRate post_encode_overhead = DataRate::Zero(); |
| 658 | for (size_t i = 0; i < rtp_streams_.size(); ++i) { |
| 659 | if (rtp_streams_[i].rtp_rtcp->SendingMedia()) { |
| 660 | post_encode_overhead += |
| 661 | rtp_streams_[i].sender_video->PostEncodeOverhead(); |
| 662 | } |
| 663 | } |
| 664 | return post_encode_overhead; |
| 665 | } |
| 666 |
| 667 | void RtpVideoSender::DeliverRtcp(const uint8_t* packet, size_t length) { |
| 668 | // Runs on a network thread. |
| 669 | for (const RtpStreamSender& stream : rtp_streams_) |
| 670 | stream.rtp_rtcp->IncomingRtcpPacket(rtc::MakeArrayView(packet, length)); |
| 671 | } |
| 672 |
| 673 | void RtpVideoSender::ConfigureSsrcs( |
| 674 | const std::map<uint32_t, RtpState>& suspended_ssrcs) { |
| 675 | // Configure regular SSRCs. |
| 676 | RTC_CHECK(ssrc_to_rtp_module_.empty()); |
| 677 | for (size_t i = 0; i < rtp_config_.ssrcs.size(); ++i) { |
| 678 | uint32_t ssrc = rtp_config_.ssrcs[i]; |
| 679 | RtpRtcpInterface* const rtp_rtcp = rtp_streams_[i].rtp_rtcp.get(); |
| 680 |
| 681 | // Restore RTP state if previous existed. |
| 682 | auto it = suspended_ssrcs.find(ssrc); |
| 683 | if (it != suspended_ssrcs.end()) |
| 684 | rtp_rtcp->SetRtpState(it->second); |
| 685 |
| 686 | ssrc_to_rtp_module_[ssrc] = rtp_rtcp; |
| 687 | } |
| 688 |
| 689 | // Set up RTX if available. |
| 690 | if (rtp_config_.rtx.ssrcs.empty()) |
| 691 | return; |
| 692 |
| 693 | RTC_DCHECK_EQ(rtp_config_.rtx.ssrcs.size(), rtp_config_.ssrcs.size()); |
| 694 | for (size_t i = 0; i < rtp_config_.rtx.ssrcs.size(); ++i) { |
| 695 | uint32_t ssrc = rtp_config_.rtx.ssrcs[i]; |
| 696 | RtpRtcpInterface* const rtp_rtcp = rtp_streams_[i].rtp_rtcp.get(); |
| 697 | auto it = suspended_ssrcs.find(ssrc); |
| 698 | if (it != suspended_ssrcs.end()) |
| 699 | rtp_rtcp->SetRtxState(it->second); |
| 700 | } |
| 701 |
| 702 | // Configure RTX payload types. |
| 703 | RTC_DCHECK_GE(rtp_config_.rtx.payload_type, 0); |
| 704 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 705 | stream.rtp_rtcp->SetRtxSendPayloadType(rtp_config_.rtx.payload_type, |
| 706 | rtp_config_.payload_type); |
| 707 | stream.rtp_rtcp->SetRtxSendStatus(kRtxRetransmitted | |
| 708 | kRtxRedundantPayloads); |
| 709 | } |
| 710 | if (rtp_config_.ulpfec.red_payload_type != -1 && |
| 711 | rtp_config_.ulpfec.red_rtx_payload_type != -1) { |
| 712 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 713 | stream.rtp_rtcp->SetRtxSendPayloadType( |
| 714 | rtp_config_.ulpfec.red_rtx_payload_type, |
| 715 | rtp_config_.ulpfec.red_payload_type); |
| 716 | } |
| 717 | } |
| 718 | } |
| 719 |
| 720 | void RtpVideoSender::OnNetworkAvailability(bool network_available) { |
| 721 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 722 | stream.rtp_rtcp->SetRTCPStatus(network_available ? rtp_config_.rtcp_mode |
| 723 | : RtcpMode::kOff); |
| 724 | } |
| 725 | } |
| 726 |
| 727 | std::map<uint32_t, RtpState> RtpVideoSender::GetRtpStates() const { |
| 728 | std::map<uint32_t, RtpState> rtp_states; |
| 729 |
| 730 | for (size_t i = 0; i < rtp_config_.ssrcs.size(); ++i) { |
| 731 | uint32_t ssrc = rtp_config_.ssrcs[i]; |
| 732 | RTC_DCHECK_EQ(ssrc, rtp_streams_[i].rtp_rtcp->SSRC()); |
| 733 | rtp_states[ssrc] = rtp_streams_[i].rtp_rtcp->GetRtpState(); |
| 734 |
| 735 | // Only happens during shutdown, when RTP module is already inactive, |
| 736 | // so OK to call fec generator here. |
| 737 | if (rtp_streams_[i].fec_generator) { |
| 738 | absl::optional<RtpState> fec_state = |
| 739 | rtp_streams_[i].fec_generator->GetRtpState(); |
| 740 | if (fec_state) { |
| 741 | uint32_t ssrc = rtp_config_.flexfec.ssrc; |
| 742 | rtp_states[ssrc] = *fec_state; |
| 743 | } |
| 744 | } |
| 745 | } |
| 746 |
| 747 | for (size_t i = 0; i < rtp_config_.rtx.ssrcs.size(); ++i) { |
| 748 | uint32_t ssrc = rtp_config_.rtx.ssrcs[i]; |
| 749 | rtp_states[ssrc] = rtp_streams_[i].rtp_rtcp->GetRtxState(); |
| 750 | } |
| 751 |
| 752 | return rtp_states; |
| 753 | } |
| 754 |
| 755 | std::map<uint32_t, RtpPayloadState> RtpVideoSender::GetRtpPayloadStates() |
| 756 | const { |
| 757 | MutexLock lock(&mutex_); |
| 758 | std::map<uint32_t, RtpPayloadState> payload_states; |
| 759 | for (const auto& param : params_) { |
| 760 | payload_states[param.ssrc()] = param.state(); |
| 761 | payload_states[param.ssrc()].shared_frame_id = shared_frame_id_; |
| 762 | } |
| 763 | return payload_states; |
| 764 | } |
| 765 |
| 766 | void RtpVideoSender::OnTransportOverheadChanged( |
| 767 | size_t transport_overhead_bytes_per_packet) { |
| 768 | MutexLock lock(&mutex_); |
| 769 | transport_overhead_bytes_per_packet_ = transport_overhead_bytes_per_packet; |
| 770 |
| 771 | size_t max_rtp_packet_size = |
| 772 | std::min(rtp_config_.max_packet_size, |
| 773 | kPathMTU - transport_overhead_bytes_per_packet_); |
| 774 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 775 | stream.rtp_rtcp->SetMaxRtpPacketSize(max_rtp_packet_size); |
| 776 | } |
| 777 | } |
| 778 |
| 779 | void RtpVideoSender::OnBitrateUpdated(BitrateAllocationUpdate update, |
| 780 | int framerate) { |
| 781 | // Substract overhead from bitrate. |
| 782 | MutexLock lock(&mutex_); |
| 783 | size_t num_active_streams = 0; |
| 784 | size_t overhead_bytes_per_packet = 0; |
| 785 | for (const auto& stream : rtp_streams_) { |
| 786 | if (stream.rtp_rtcp->SendingMedia()) { |
| 787 | overhead_bytes_per_packet += stream.rtp_rtcp->ExpectedPerPacketOverhead(); |
| 788 | ++num_active_streams; |
| 789 | } |
| 790 | } |
| 791 | if (num_active_streams > 1) { |
| 792 | overhead_bytes_per_packet /= num_active_streams; |
| 793 | } |
| 794 |
| 795 | DataSize packet_overhead = DataSize::Bytes( |
| 796 | overhead_bytes_per_packet + transport_overhead_bytes_per_packet_); |
| 797 | DataSize max_total_packet_size = DataSize::Bytes( |
| 798 | rtp_config_.max_packet_size + transport_overhead_bytes_per_packet_); |
| 799 | uint32_t payload_bitrate_bps = update.target_bitrate.bps(); |
| 800 | if (has_packet_feedback_) { |
| 801 | DataRate overhead_rate = |
| 802 | CalculateOverheadRate(update.target_bitrate, max_total_packet_size, |
| 803 | packet_overhead, Frequency::Hertz(framerate)); |
| 804 | // TODO(srte): We probably should not accept 0 payload bitrate here. |
| 805 | payload_bitrate_bps = rtc::saturated_cast<uint32_t>(payload_bitrate_bps - |
| 806 | overhead_rate.bps()); |
| 807 | } |
| 808 |
| 809 | // Get the encoder target rate. It is the estimated network rate - |
| 810 | // protection overhead. |
| 811 | // TODO(srte): We should multiply with 255 here. |
| 812 | encoder_target_rate_bps_ = fec_controller_->UpdateFecRates( |
| 813 | payload_bitrate_bps, framerate, |
| 814 | rtc::saturated_cast<uint8_t>(update.packet_loss_ratio * 256), |
| 815 | loss_mask_vector_, update.round_trip_time.ms()); |
| 816 | if (!fec_allowed_) { |
| 817 | encoder_target_rate_bps_ = payload_bitrate_bps; |
| 818 | // fec_controller_->UpdateFecRates() was still called so as to allow |
| 819 | // `fec_controller_` to update whatever internal state it might have, |
| 820 | // since `fec_allowed_` may be toggled back on at any moment. |
| 821 | } |
| 822 |
| 823 | // Subtract post encode overhead from the encoder target. If target rate |
| 824 | // is really low, cap the overhead at 50%. This also avoids the case where |
| 825 | // `encoder_target_rate_bps_` is 0 due to encoder pause event while the |
| 826 | // packetization rate is positive since packets are still flowing. |
| 827 | uint32_t post_encode_overhead_bps = std::min( |
| 828 | GetPostEncodeOverhead().bps<uint32_t>(), encoder_target_rate_bps_ / 2); |
| 829 | encoder_target_rate_bps_ -= post_encode_overhead_bps; |
| 830 |
| 831 | loss_mask_vector_.clear(); |
| 832 |
| 833 | uint32_t encoder_overhead_rate_bps = 0; |
| 834 | if (has_packet_feedback_) { |
| 835 | // TODO(srte): The packet size should probably be the same as in the |
| 836 | // CalculateOverheadRate call above (just max_total_packet_size), it doesn't |
| 837 | // make sense to use different packet rates for different overhead |
| 838 | // calculations. |
| 839 | DataRate encoder_overhead_rate = CalculateOverheadRate( |
| 840 | DataRate::BitsPerSec(encoder_target_rate_bps_), |
| 841 | max_total_packet_size - DataSize::Bytes(overhead_bytes_per_packet), |
| 842 | packet_overhead, Frequency::Hertz(framerate)); |
| 843 | encoder_overhead_rate_bps = std::min( |
| 844 | encoder_overhead_rate.bps<uint32_t>(), |
| 845 | update.target_bitrate.bps<uint32_t>() - encoder_target_rate_bps_); |
| 846 | } |
| 847 | const uint32_t media_rate = encoder_target_rate_bps_ + |
| 848 | encoder_overhead_rate_bps + |
| 849 | post_encode_overhead_bps; |
| 850 | RTC_DCHECK_GE(update.target_bitrate, DataRate::BitsPerSec(media_rate)); |
| 851 | // `protection_bitrate_bps_` includes overhead. |
| 852 | protection_bitrate_bps_ = update.target_bitrate.bps() - media_rate; |
| 853 | } |
| 854 |
| 855 | uint32_t RtpVideoSender::GetPayloadBitrateBps() const { |
| 856 | return encoder_target_rate_bps_; |
| 857 | } |
| 858 |
| 859 | uint32_t RtpVideoSender::GetProtectionBitrateBps() const { |
| 860 | return protection_bitrate_bps_; |
| 861 | } |
| 862 |
| 863 | std::vector<RtpSequenceNumberMap::Info> RtpVideoSender::GetSentRtpPacketInfos( |
| 864 | uint32_t ssrc, |
| 865 | rtc::ArrayView<const uint16_t> sequence_numbers) const { |
| 866 | for (const auto& rtp_stream : rtp_streams_) { |
| 867 | if (ssrc == rtp_stream.rtp_rtcp->SSRC()) { |
| 868 | return rtp_stream.rtp_rtcp->GetSentRtpPacketInfos(sequence_numbers); |
| 869 | } |
| 870 | } |
| 871 | return std::vector<RtpSequenceNumberMap::Info>(); |
| 872 | } |
| 873 |
| 874 | int RtpVideoSender::ProtectionRequest(const FecProtectionParams* delta_params, |
| 875 | const FecProtectionParams* key_params, |
| 876 | uint32_t* sent_video_rate_bps, |
| 877 | uint32_t* sent_nack_rate_bps, |
| 878 | uint32_t* sent_fec_rate_bps) { |
| 879 | *sent_video_rate_bps = 0; |
| 880 | *sent_nack_rate_bps = 0; |
| 881 | *sent_fec_rate_bps = 0; |
| 882 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 883 | stream.rtp_rtcp->SetFecProtectionParams(*delta_params, *key_params); |
| 884 |
| 885 | auto send_bitrate = stream.rtp_rtcp->GetSendRates(); |
| 886 | *sent_video_rate_bps += send_bitrate[RtpPacketMediaType::kVideo].bps(); |
| 887 | *sent_fec_rate_bps += |
| 888 | send_bitrate[RtpPacketMediaType::kForwardErrorCorrection].bps(); |
| 889 | *sent_nack_rate_bps += |
| 890 | send_bitrate[RtpPacketMediaType::kRetransmission].bps(); |
| 891 | } |
| 892 | return 0; |
| 893 | } |
| 894 |
| 895 | void RtpVideoSender::SetRetransmissionMode(int retransmission_mode) { |
| 896 | MutexLock lock(&mutex_); |
| 897 | for (const RtpStreamSender& stream : rtp_streams_) { |
| 898 | stream.sender_video->SetRetransmissionSetting(retransmission_mode); |
| 899 | } |
| 900 | } |
| 901 |
| 902 | void RtpVideoSender::SetFecAllowed(bool fec_allowed) { |
| 903 | MutexLock lock(&mutex_); |
| 904 | fec_allowed_ = fec_allowed; |
| 905 | } |
| 906 |
| 907 | void RtpVideoSender::OnPacketFeedbackVector( |
| 908 | std::vector<StreamPacketInfo> packet_feedback_vector) { |
| 909 | if (fec_controller_->UseLossVectorMask()) { |
| 910 | MutexLock lock(&mutex_); |
| 911 | for (const StreamPacketInfo& packet : packet_feedback_vector) { |
| 912 | loss_mask_vector_.push_back(!packet.received); |
| 913 | } |
| 914 | } |
| 915 |
| 916 | // Map from SSRC to all acked packets for that RTP module. |
| 917 | std::map<uint32_t, std::vector<uint16_t>> acked_packets_per_ssrc; |
| 918 | for (const StreamPacketInfo& packet : packet_feedback_vector) { |
| 919 | if (packet.received && packet.ssrc) { |
| 920 | acked_packets_per_ssrc[*packet.ssrc].push_back( |
| 921 | packet.rtp_sequence_number); |
| 922 | } |
| 923 | } |
| 924 |
| 925 | // Map from SSRC to vector of RTP sequence numbers that are indicated as |
| 926 | // lost by feedback, without being trailed by any received packets. |
| 927 | std::map<uint32_t, std::vector<uint16_t>> early_loss_detected_per_ssrc; |
| 928 |
| 929 | for (const StreamPacketInfo& packet : packet_feedback_vector) { |
| 930 | // Only include new media packets, not retransmissions/padding/fec. |
| 931 | if (!packet.received && packet.ssrc && !packet.is_retransmission) { |
| 932 | // Last known lost packet, might not be detectable as lost by remote |
| 933 | // jitter buffer. |
| 934 | early_loss_detected_per_ssrc[*packet.ssrc].push_back( |
| 935 | packet.rtp_sequence_number); |
| 936 | } else { |
| 937 | // Packet received, so any loss prior to this is already detectable. |
| 938 | early_loss_detected_per_ssrc.erase(*packet.ssrc); |
| 939 | } |
| 940 | } |
| 941 |
| 942 | for (const auto& kv : early_loss_detected_per_ssrc) { |
| 943 | const uint32_t ssrc = kv.first; |
| 944 | auto it = ssrc_to_rtp_module_.find(ssrc); |
| 945 | RTC_CHECK(it != ssrc_to_rtp_module_.end()); |
| 946 | RTPSender* rtp_sender = it->second->RtpSender(); |
| 947 | for (uint16_t sequence_number : kv.second) { |
| 948 | rtp_sender->ReSendPacket(sequence_number); |
| 949 | } |
| 950 | } |
| 951 |
| 952 | for (const auto& kv : acked_packets_per_ssrc) { |
| 953 | const uint32_t ssrc = kv.first; |
| 954 | auto it = ssrc_to_rtp_module_.find(ssrc); |
| 955 | if (it == ssrc_to_rtp_module_.end()) { |
| 956 | // No media, likely FEC or padding. Ignore since there's no RTP history to |
| 957 | // clean up anyway. |
| 958 | continue; |
| 959 | } |
| 960 | rtc::ArrayView<const uint16_t> rtp_sequence_numbers(kv.second); |
| 961 | it->second->OnPacketsAcknowledged(rtp_sequence_numbers); |
| 962 | } |
| 963 | } |
| 964 |
| 965 | void RtpVideoSender::SetEncodingData(size_t width, |
| 966 | size_t height, |
| 967 | size_t num_temporal_layers) { |
| 968 | fec_controller_->SetEncodingData(width, height, num_temporal_layers, |
| 969 | rtp_config_.max_packet_size); |
| 970 | } |
| 971 |
| 972 | DataRate RtpVideoSender::CalculateOverheadRate(DataRate data_rate, |
| 973 | DataSize packet_size, |
| 974 | DataSize overhead_per_packet, |
| 975 | Frequency framerate) const { |
| 976 | Frequency packet_rate = data_rate / packet_size; |
| 977 | if (use_frame_rate_for_overhead_) { |
| 978 | framerate = std::max(framerate, Frequency::Hertz(1)); |
| 979 | DataSize frame_size = data_rate / framerate; |
| 980 | int packets_per_frame = ceil(frame_size / packet_size); |
| 981 | packet_rate = packets_per_frame * framerate; |
| 982 | } |
| 983 | return packet_rate.RoundUpTo(Frequency::Hertz(1)) * overhead_per_packet; |
| 984 | } |
| 985 |
| 986 | } // namespace webrtc |
| 987 |