1 /*
2 * Copyright (c) 2012 The WebRTC project authors. All Rights Reserved.
3 *
4 * Use of this source code is governed by a BSD-style license
5 * that can be found in the LICENSE file in the root of the source
6 * tree. An additional intellectual property rights grant can be found
7 * in the file PATENTS. All contributing project authors may
8 * be found in the AUTHORS file in the root of the source tree.
9 */
10
11 #include "modules/rtp_rtcp/source/rtp_sender.h"
12
13 #include <algorithm>
14 #include <limits>
15 #include <memory>
16 #include <string>
17 #include <utility>
18
19 #include "absl/strings/match.h"
20 #include "absl/strings/string_view.h"
21 #include "api/array_view.h"
22 #include "api/rtc_event_log/rtc_event_log.h"
23 #include "logging/rtc_event_log/events/rtc_event_rtp_packet_outgoing.h"
24 #include "modules/rtp_rtcp/include/rtp_cvo.h"
25 #include "modules/rtp_rtcp/source/byte_io.h"
26 #include "modules/rtp_rtcp/source/rtp_generic_frame_descriptor_extension.h"
27 #include "modules/rtp_rtcp/source/rtp_header_extensions.h"
28 #include "modules/rtp_rtcp/source/rtp_packet_to_send.h"
29 #include "modules/rtp_rtcp/source/time_util.h"
30 #include "rtc_base/arraysize.h"
31 #include "rtc_base/checks.h"
32 #include "rtc_base/experiments/field_trial_parser.h"
33 #include "rtc_base/logging.h"
34 #include "rtc_base/numerics/safe_minmax.h"
35 #include "rtc_base/rate_limiter.h"
36 #include "rtc_base/time_utils.h"
37
38 namespace webrtc {
39
40 namespace {
41 // Max in the RFC 3550 is 255 bytes, we limit it to be modulus 32 for SRTP.
42 constexpr size_t kMaxPaddingLength = 224;
43 constexpr size_t kMinAudioPaddingLength = 50;
44 constexpr size_t kRtpHeaderLength = 12;
45
46 // Min size needed to get payload padding from packet history.
47 constexpr int kMinPayloadPaddingBytes = 50;
48
49 // Determines how much larger a payload padding packet may be, compared to the
50 // requested padding size.
51 constexpr double kMaxPaddingSizeFactor = 3.0;
52
53 template <typename Extension>
CreateExtensionSize()54 constexpr RtpExtensionSize CreateExtensionSize() {
55 return {Extension::kId, Extension::kValueSizeBytes};
56 }
57
58 template <typename Extension>
CreateMaxExtensionSize()59 constexpr RtpExtensionSize CreateMaxExtensionSize() {
60 return {Extension::kId, Extension::kMaxValueSizeBytes};
61 }
62
63 // Size info for header extensions that might be used in padding or FEC packets.
64 constexpr RtpExtensionSize kFecOrPaddingExtensionSizes[] = {
65 CreateExtensionSize<AbsoluteSendTime>(),
66 CreateExtensionSize<TransmissionOffset>(),
67 CreateExtensionSize<TransportSequenceNumber>(),
68 CreateExtensionSize<PlayoutDelayLimits>(),
69 CreateMaxExtensionSize<RtpMid>(),
70 CreateExtensionSize<VideoTimingExtension>(),
71 };
72
73 // Size info for header extensions that might be used in video packets.
74 constexpr RtpExtensionSize kVideoExtensionSizes[] = {
75 CreateExtensionSize<AbsoluteSendTime>(),
76 CreateExtensionSize<AbsoluteCaptureTimeExtension>(),
77 CreateExtensionSize<TransmissionOffset>(),
78 CreateExtensionSize<TransportSequenceNumber>(),
79 CreateExtensionSize<PlayoutDelayLimits>(),
80 CreateExtensionSize<VideoOrientation>(),
81 CreateExtensionSize<VideoContentTypeExtension>(),
82 CreateExtensionSize<VideoTimingExtension>(),
83 CreateMaxExtensionSize<RtpStreamId>(),
84 CreateMaxExtensionSize<RepairedRtpStreamId>(),
85 CreateMaxExtensionSize<RtpMid>(),
86 {RtpGenericFrameDescriptorExtension00::kId,
87 RtpGenericFrameDescriptorExtension00::kMaxSizeBytes},
88 };
89
90 // Size info for header extensions that might be used in audio packets.
91 constexpr RtpExtensionSize kAudioExtensionSizes[] = {
92 CreateExtensionSize<AbsoluteSendTime>(),
93 CreateExtensionSize<AbsoluteCaptureTimeExtension>(),
94 CreateExtensionSize<AudioLevel>(),
95 CreateExtensionSize<InbandComfortNoiseExtension>(),
96 CreateExtensionSize<TransmissionOffset>(),
97 CreateExtensionSize<TransportSequenceNumber>(),
98 CreateMaxExtensionSize<RtpStreamId>(),
99 CreateMaxExtensionSize<RepairedRtpStreamId>(),
100 CreateMaxExtensionSize<RtpMid>(),
101 };
102
103 // Non-volatile extensions can be expected on all packets, if registered.
104 // Volatile ones, such as VideoContentTypeExtension which is only set on
105 // key-frames, are removed to simplify overhead calculations at the expense of
106 // some accuracy.
IsNonVolatile(RTPExtensionType type)107 bool IsNonVolatile(RTPExtensionType type) {
108 switch (type) {
109 case kRtpExtensionTransmissionTimeOffset:
110 case kRtpExtensionAudioLevel:
111 case kRtpExtensionCsrcAudioLevel:
112 case kRtpExtensionAbsoluteSendTime:
113 case kRtpExtensionTransportSequenceNumber:
114 case kRtpExtensionTransportSequenceNumber02:
115 case kRtpExtensionRtpStreamId:
116 case kRtpExtensionMid:
117 case kRtpExtensionGenericFrameDescriptor00:
118 case kRtpExtensionGenericFrameDescriptor02:
119 return true;
120 case kRtpExtensionInbandComfortNoise:
121 case kRtpExtensionAbsoluteCaptureTime:
122 case kRtpExtensionVideoRotation:
123 case kRtpExtensionPlayoutDelay:
124 case kRtpExtensionVideoContentType:
125 case kRtpExtensionVideoLayersAllocation:
126 case kRtpExtensionVideoTiming:
127 case kRtpExtensionRepairedRtpStreamId:
128 case kRtpExtensionColorSpace:
129 case kRtpExtensionVideoFrameTrackingId:
130 return false;
131 case kRtpExtensionNone:
132 case kRtpExtensionNumberOfExtensions:
133 RTC_DCHECK_NOTREACHED();
134 return false;
135 }
136 RTC_CHECK_NOTREACHED();
137 }
138
HasBweExtension(const RtpHeaderExtensionMap & extensions_map)139 bool HasBweExtension(const RtpHeaderExtensionMap& extensions_map) {
140 return extensions_map.IsRegistered(kRtpExtensionTransportSequenceNumber) ||
141 extensions_map.IsRegistered(kRtpExtensionTransportSequenceNumber02) ||
142 extensions_map.IsRegistered(kRtpExtensionAbsoluteSendTime) ||
143 extensions_map.IsRegistered(kRtpExtensionTransmissionTimeOffset);
144 }
145
146 } // namespace
147
RTPSender(const RtpRtcpInterface::Configuration & config,RtpPacketHistory * packet_history,RtpPacketSender * packet_sender)148 RTPSender::RTPSender(const RtpRtcpInterface::Configuration& config,
149 RtpPacketHistory* packet_history,
150 RtpPacketSender* packet_sender)
151 : clock_(config.clock),
152 random_(clock_->TimeInMicroseconds()),
153 audio_configured_(config.audio),
154 ssrc_(config.local_media_ssrc),
155 rtx_ssrc_(config.rtx_send_ssrc),
156 flexfec_ssrc_(config.fec_generator ? config.fec_generator->FecSsrc()
157 : absl::nullopt),
158 packet_history_(packet_history),
159 paced_sender_(packet_sender),
160 sending_media_(true), // Default to sending media.
161 max_packet_size_(IP_PACKET_SIZE - 28), // Default is IP-v4/UDP.
162 rtp_header_extension_map_(config.extmap_allow_mixed),
163 // RTP variables
164 rid_(config.rid),
165 always_send_mid_and_rid_(config.always_send_mid_and_rid),
166 ssrc_has_acked_(false),
167 rtx_ssrc_has_acked_(false),
168 csrcs_(),
169 rtx_(kRtxOff),
170 supports_bwe_extension_(false),
171 retransmission_rate_limiter_(config.retransmission_rate_limiter) {
172 // This random initialization is not intended to be cryptographic strong.
173 timestamp_offset_ = random_.Rand<uint32_t>();
174
175 RTC_DCHECK(paced_sender_);
176 RTC_DCHECK(packet_history_);
177 RTC_DCHECK_LE(rid_.size(), RtpStreamId::kMaxValueSizeBytes);
178
179 UpdateHeaderSizes();
180 }
181
~RTPSender()182 RTPSender::~RTPSender() {
183 // TODO(tommi): Use a thread checker to ensure the object is created and
184 // deleted on the same thread. At the moment this isn't possible due to
185 // voe::ChannelOwner in voice engine. To reproduce, run:
186 // voe_auto_test --automated --gtest_filter=*MixManyChannelsForStressOpus
187
188 // TODO(tommi,holmer): We don't grab locks in the dtor before accessing member
189 // variables but we grab them in all other methods. (what's the design?)
190 // Start documenting what thread we're on in what method so that it's easier
191 // to understand performance attributes and possibly remove locks.
192 }
193
FecExtensionSizes()194 rtc::ArrayView<const RtpExtensionSize> RTPSender::FecExtensionSizes() {
195 return rtc::MakeArrayView(kFecOrPaddingExtensionSizes,
196 arraysize(kFecOrPaddingExtensionSizes));
197 }
198
VideoExtensionSizes()199 rtc::ArrayView<const RtpExtensionSize> RTPSender::VideoExtensionSizes() {
200 return rtc::MakeArrayView(kVideoExtensionSizes,
201 arraysize(kVideoExtensionSizes));
202 }
203
AudioExtensionSizes()204 rtc::ArrayView<const RtpExtensionSize> RTPSender::AudioExtensionSizes() {
205 return rtc::MakeArrayView(kAudioExtensionSizes,
206 arraysize(kAudioExtensionSizes));
207 }
208
SetExtmapAllowMixed(bool extmap_allow_mixed)209 void RTPSender::SetExtmapAllowMixed(bool extmap_allow_mixed) {
210 MutexLock lock(&send_mutex_);
211 rtp_header_extension_map_.SetExtmapAllowMixed(extmap_allow_mixed);
212 }
213
RegisterRtpHeaderExtension(absl::string_view uri,int id)214 bool RTPSender::RegisterRtpHeaderExtension(absl::string_view uri, int id) {
215 MutexLock lock(&send_mutex_);
216 bool registered = rtp_header_extension_map_.RegisterByUri(id, uri);
217 supports_bwe_extension_ = HasBweExtension(rtp_header_extension_map_);
218 UpdateHeaderSizes();
219 return registered;
220 }
221
IsRtpHeaderExtensionRegistered(RTPExtensionType type) const222 bool RTPSender::IsRtpHeaderExtensionRegistered(RTPExtensionType type) const {
223 MutexLock lock(&send_mutex_);
224 return rtp_header_extension_map_.IsRegistered(type);
225 }
226
DeregisterRtpHeaderExtension(absl::string_view uri)227 void RTPSender::DeregisterRtpHeaderExtension(absl::string_view uri) {
228 MutexLock lock(&send_mutex_);
229 rtp_header_extension_map_.Deregister(uri);
230 supports_bwe_extension_ = HasBweExtension(rtp_header_extension_map_);
231 UpdateHeaderSizes();
232 }
233
SetMaxRtpPacketSize(size_t max_packet_size)234 void RTPSender::SetMaxRtpPacketSize(size_t max_packet_size) {
235 RTC_DCHECK_GE(max_packet_size, 100);
236 RTC_DCHECK_LE(max_packet_size, IP_PACKET_SIZE);
237 MutexLock lock(&send_mutex_);
238 max_packet_size_ = max_packet_size;
239 }
240
MaxRtpPacketSize() const241 size_t RTPSender::MaxRtpPacketSize() const {
242 return max_packet_size_;
243 }
244
SetRtxStatus(int mode)245 void RTPSender::SetRtxStatus(int mode) {
246 MutexLock lock(&send_mutex_);
247 if (mode != kRtxOff &&
248 (!rtx_ssrc_.has_value() || rtx_payload_type_map_.empty())) {
249 RTC_LOG(LS_ERROR)
250 << "Failed to enable RTX without RTX SSRC or payload types.";
251 return;
252 }
253 rtx_ = mode;
254 }
255
RtxStatus() const256 int RTPSender::RtxStatus() const {
257 MutexLock lock(&send_mutex_);
258 return rtx_;
259 }
260
SetRtxPayloadType(int payload_type,int associated_payload_type)261 void RTPSender::SetRtxPayloadType(int payload_type,
262 int associated_payload_type) {
263 MutexLock lock(&send_mutex_);
264 RTC_DCHECK_LE(payload_type, 127);
265 RTC_DCHECK_LE(associated_payload_type, 127);
266 if (payload_type < 0) {
267 RTC_LOG(LS_ERROR) << "Invalid RTX payload type: " << payload_type << ".";
268 return;
269 }
270
271 rtx_payload_type_map_[associated_payload_type] = payload_type;
272 }
273
ReSendPacket(uint16_t packet_id)274 int32_t RTPSender::ReSendPacket(uint16_t packet_id) {
275 int32_t packet_size = 0;
276 const bool rtx = (RtxStatus() & kRtxRetransmitted) > 0;
277
278 std::unique_ptr<RtpPacketToSend> packet =
279 packet_history_->GetPacketAndMarkAsPending(
280 packet_id, [&](const RtpPacketToSend& stored_packet) {
281 // Check if we're overusing retransmission bitrate.
282 // TODO(sprang): Add histograms for nack success or failure
283 // reasons.
284 packet_size = stored_packet.size();
285 std::unique_ptr<RtpPacketToSend> retransmit_packet;
286 if (retransmission_rate_limiter_ &&
287 !retransmission_rate_limiter_->TryUseRate(packet_size)) {
288 return retransmit_packet;
289 }
290 if (rtx) {
291 retransmit_packet = BuildRtxPacket(stored_packet);
292 } else {
293 retransmit_packet =
294 std::make_unique<RtpPacketToSend>(stored_packet);
295 }
296 if (retransmit_packet) {
297 retransmit_packet->set_retransmitted_sequence_number(
298 stored_packet.SequenceNumber());
299 }
300 return retransmit_packet;
301 });
302 if (packet_size == 0) {
303 // Packet not found or already queued for retransmission, ignore.
304 RTC_DCHECK(!packet);
305 return 0;
306 }
307 if (!packet) {
308 // Packet was found, but lambda helper above chose not to create
309 // `retransmit_packet` out of it.
310 return -1;
311 }
312 packet->set_packet_type(RtpPacketMediaType::kRetransmission);
313 packet->set_fec_protect_packet(false);
314 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
315 packets.emplace_back(std::move(packet));
316 paced_sender_->EnqueuePackets(std::move(packets));
317
318 return packet_size;
319 }
320
OnReceivedAckOnSsrc(int64_t extended_highest_sequence_number)321 void RTPSender::OnReceivedAckOnSsrc(int64_t extended_highest_sequence_number) {
322 MutexLock lock(&send_mutex_);
323 bool update_required = !ssrc_has_acked_;
324 ssrc_has_acked_ = true;
325 if (update_required) {
326 UpdateHeaderSizes();
327 }
328 }
329
OnReceivedAckOnRtxSsrc(int64_t extended_highest_sequence_number)330 void RTPSender::OnReceivedAckOnRtxSsrc(
331 int64_t extended_highest_sequence_number) {
332 MutexLock lock(&send_mutex_);
333 bool update_required = !rtx_ssrc_has_acked_;
334 rtx_ssrc_has_acked_ = true;
335 if (update_required) {
336 UpdateHeaderSizes();
337 }
338 }
339
OnReceivedNack(const std::vector<uint16_t> & nack_sequence_numbers,int64_t avg_rtt)340 void RTPSender::OnReceivedNack(
341 const std::vector<uint16_t>& nack_sequence_numbers,
342 int64_t avg_rtt) {
343 packet_history_->SetRtt(TimeDelta::Millis(5 + avg_rtt));
344 for (uint16_t seq_no : nack_sequence_numbers) {
345 const int32_t bytes_sent = ReSendPacket(seq_no);
346 if (bytes_sent < 0) {
347 // Failed to send one Sequence number. Give up the rest in this nack.
348 RTC_LOG(LS_WARNING) << "Failed resending RTP packet " << seq_no
349 << ", Discard rest of packets.";
350 break;
351 }
352 }
353 }
354
SupportsPadding() const355 bool RTPSender::SupportsPadding() const {
356 MutexLock lock(&send_mutex_);
357 return sending_media_ && supports_bwe_extension_;
358 }
359
SupportsRtxPayloadPadding() const360 bool RTPSender::SupportsRtxPayloadPadding() const {
361 MutexLock lock(&send_mutex_);
362 return sending_media_ && supports_bwe_extension_ &&
363 (rtx_ & kRtxRedundantPayloads);
364 }
365
GeneratePadding(size_t target_size_bytes,bool media_has_been_sent,bool can_send_padding_on_media_ssrc)366 std::vector<std::unique_ptr<RtpPacketToSend>> RTPSender::GeneratePadding(
367 size_t target_size_bytes,
368 bool media_has_been_sent,
369 bool can_send_padding_on_media_ssrc) {
370 // This method does not actually send packets, it just generates
371 // them and puts them in the pacer queue. Since this should incur
372 // low overhead, keep the lock for the scope of the method in order
373 // to make the code more readable.
374
375 std::vector<std::unique_ptr<RtpPacketToSend>> padding_packets;
376 size_t bytes_left = target_size_bytes;
377 if (SupportsRtxPayloadPadding()) {
378 while (bytes_left >= kMinPayloadPaddingBytes) {
379 std::unique_ptr<RtpPacketToSend> packet =
380 packet_history_->GetPayloadPaddingPacket(
381 [&](const RtpPacketToSend& packet)
382 -> std::unique_ptr<RtpPacketToSend> {
383 // Limit overshoot, generate <= `kMaxPaddingSizeFactor` *
384 // `target_size_bytes`.
385 const size_t max_overshoot_bytes = static_cast<size_t>(
386 ((kMaxPaddingSizeFactor - 1.0) * target_size_bytes) + 0.5);
387 if (packet.payload_size() + kRtxHeaderSize >
388 max_overshoot_bytes + bytes_left) {
389 return nullptr;
390 }
391 return BuildRtxPacket(packet);
392 });
393 if (!packet) {
394 break;
395 }
396
397 bytes_left -= std::min(bytes_left, packet->payload_size());
398 packet->set_packet_type(RtpPacketMediaType::kPadding);
399 padding_packets.push_back(std::move(packet));
400 }
401 }
402
403 MutexLock lock(&send_mutex_);
404 if (!sending_media_) {
405 return {};
406 }
407
408 size_t padding_bytes_in_packet;
409 const size_t max_payload_size =
410 max_packet_size_ - max_padding_fec_packet_header_;
411 if (audio_configured_) {
412 // Allow smaller padding packets for audio.
413 padding_bytes_in_packet = rtc::SafeClamp<size_t>(
414 bytes_left, kMinAudioPaddingLength,
415 rtc::SafeMin(max_payload_size, kMaxPaddingLength));
416 } else {
417 // Always send full padding packets. This is accounted for by the
418 // RtpPacketSender, which will make sure we don't send too much padding even
419 // if a single packet is larger than requested.
420 // We do this to avoid frequently sending small packets on higher bitrates.
421 padding_bytes_in_packet = rtc::SafeMin(max_payload_size, kMaxPaddingLength);
422 }
423
424 while (bytes_left > 0) {
425 auto padding_packet =
426 std::make_unique<RtpPacketToSend>(&rtp_header_extension_map_);
427 padding_packet->set_packet_type(RtpPacketMediaType::kPadding);
428 padding_packet->SetMarker(false);
429 if (rtx_ == kRtxOff) {
430 if (!can_send_padding_on_media_ssrc) {
431 break;
432 }
433 padding_packet->SetSsrc(ssrc_);
434 } else {
435 // Without abs-send-time or transport sequence number a media packet
436 // must be sent before padding so that the timestamps used for
437 // estimation are correct.
438 if (!media_has_been_sent &&
439 !(rtp_header_extension_map_.IsRegistered(AbsoluteSendTime::kId) ||
440 rtp_header_extension_map_.IsRegistered(
441 TransportSequenceNumber::kId))) {
442 break;
443 }
444
445 RTC_DCHECK(rtx_ssrc_);
446 RTC_DCHECK(!rtx_payload_type_map_.empty());
447 padding_packet->SetSsrc(*rtx_ssrc_);
448 padding_packet->SetPayloadType(rtx_payload_type_map_.begin()->second);
449 }
450
451 if (rtp_header_extension_map_.IsRegistered(TransportSequenceNumber::kId)) {
452 padding_packet->ReserveExtension<TransportSequenceNumber>();
453 }
454 if (rtp_header_extension_map_.IsRegistered(TransmissionOffset::kId)) {
455 padding_packet->ReserveExtension<TransmissionOffset>();
456 }
457 if (rtp_header_extension_map_.IsRegistered(AbsoluteSendTime::kId)) {
458 padding_packet->ReserveExtension<AbsoluteSendTime>();
459 }
460
461 padding_packet->SetPadding(padding_bytes_in_packet);
462 bytes_left -= std::min(bytes_left, padding_bytes_in_packet);
463 padding_packets.push_back(std::move(padding_packet));
464 }
465
466 return padding_packets;
467 }
468
SendToNetwork(std::unique_ptr<RtpPacketToSend> packet)469 bool RTPSender::SendToNetwork(std::unique_ptr<RtpPacketToSend> packet) {
470 RTC_DCHECK(packet);
471 auto packet_type = packet->packet_type();
472 RTC_CHECK(packet_type) << "Packet type must be set before sending.";
473
474 if (packet->capture_time() <= Timestamp::Zero()) {
475 packet->set_capture_time(clock_->CurrentTime());
476 }
477
478 std::vector<std::unique_ptr<RtpPacketToSend>> packets;
479 packets.emplace_back(std::move(packet));
480 paced_sender_->EnqueuePackets(std::move(packets));
481
482 return true;
483 }
484
EnqueuePackets(std::vector<std::unique_ptr<RtpPacketToSend>> packets)485 void RTPSender::EnqueuePackets(
486 std::vector<std::unique_ptr<RtpPacketToSend>> packets) {
487 RTC_DCHECK(!packets.empty());
488 Timestamp now = clock_->CurrentTime();
489 for (auto& packet : packets) {
490 RTC_DCHECK(packet);
491 RTC_CHECK(packet->packet_type().has_value())
492 << "Packet type must be set before sending.";
493 if (packet->capture_time() <= Timestamp::Zero()) {
494 packet->set_capture_time(now);
495 }
496 }
497
498 paced_sender_->EnqueuePackets(std::move(packets));
499 }
500
FecOrPaddingPacketMaxRtpHeaderLength() const501 size_t RTPSender::FecOrPaddingPacketMaxRtpHeaderLength() const {
502 MutexLock lock(&send_mutex_);
503 return max_padding_fec_packet_header_;
504 }
505
ExpectedPerPacketOverhead() const506 size_t RTPSender::ExpectedPerPacketOverhead() const {
507 MutexLock lock(&send_mutex_);
508 return max_media_packet_header_;
509 }
510
AllocatePacket() const511 std::unique_ptr<RtpPacketToSend> RTPSender::AllocatePacket() const {
512 MutexLock lock(&send_mutex_);
513 // TODO(danilchap): Find better motivator and value for extra capacity.
514 // RtpPacketizer might slightly miscalulate needed size,
515 // SRTP may benefit from extra space in the buffer and do encryption in place
516 // saving reallocation.
517 // While sending slightly oversized packet increase chance of dropped packet,
518 // it is better than crash on drop packet without trying to send it.
519 static constexpr int kExtraCapacity = 16;
520 auto packet = std::make_unique<RtpPacketToSend>(
521 &rtp_header_extension_map_, max_packet_size_ + kExtraCapacity);
522 packet->SetSsrc(ssrc_);
523 packet->SetCsrcs(csrcs_);
524 // Reserve extensions, if registered, RtpSender set in SendToNetwork.
525 packet->ReserveExtension<AbsoluteSendTime>();
526 packet->ReserveExtension<TransmissionOffset>();
527 packet->ReserveExtension<TransportSequenceNumber>();
528
529 // BUNDLE requires that the receiver "bind" the received SSRC to the values
530 // in the MID and/or (R)RID header extensions if present. Therefore, the
531 // sender can reduce overhead by omitting these header extensions once it
532 // knows that the receiver has "bound" the SSRC.
533 // This optimization can be configured by setting
534 // `always_send_mid_and_rid_` appropriately.
535 //
536 // The algorithm here is fairly simple: Always attach a MID and/or RID (if
537 // configured) to the outgoing packets until an RTCP receiver report comes
538 // back for this SSRC. That feedback indicates the receiver must have
539 // received a packet with the SSRC and header extension(s), so the sender
540 // then stops attaching the MID and RID.
541 if (always_send_mid_and_rid_ || !ssrc_has_acked_) {
542 // These are no-ops if the corresponding header extension is not registered.
543 if (!mid_.empty()) {
544 packet->SetExtension<RtpMid>(mid_);
545 }
546 if (!rid_.empty()) {
547 packet->SetExtension<RtpStreamId>(rid_);
548 }
549 }
550 return packet;
551 }
552
SetSendingMediaStatus(bool enabled)553 void RTPSender::SetSendingMediaStatus(bool enabled) {
554 MutexLock lock(&send_mutex_);
555 sending_media_ = enabled;
556 }
557
SendingMedia() const558 bool RTPSender::SendingMedia() const {
559 MutexLock lock(&send_mutex_);
560 return sending_media_;
561 }
562
IsAudioConfigured() const563 bool RTPSender::IsAudioConfigured() const {
564 return audio_configured_;
565 }
566
SetTimestampOffset(uint32_t timestamp)567 void RTPSender::SetTimestampOffset(uint32_t timestamp) {
568 MutexLock lock(&send_mutex_);
569 timestamp_offset_ = timestamp;
570 }
571
TimestampOffset() const572 uint32_t RTPSender::TimestampOffset() const {
573 MutexLock lock(&send_mutex_);
574 return timestamp_offset_;
575 }
576
SetMid(absl::string_view mid)577 void RTPSender::SetMid(absl::string_view mid) {
578 // This is configured via the API.
579 MutexLock lock(&send_mutex_);
580 RTC_DCHECK_LE(mid.length(), RtpMid::kMaxValueSizeBytes);
581 mid_ = std::string(mid);
582 UpdateHeaderSizes();
583 }
584
SetCsrcs(const std::vector<uint32_t> & csrcs)585 void RTPSender::SetCsrcs(const std::vector<uint32_t>& csrcs) {
586 RTC_DCHECK_LE(csrcs.size(), kRtpCsrcSize);
587 MutexLock lock(&send_mutex_);
588 csrcs_ = csrcs;
589 UpdateHeaderSizes();
590 }
591
CopyHeaderAndExtensionsToRtxPacket(const RtpPacketToSend & packet,RtpPacketToSend * rtx_packet)592 static void CopyHeaderAndExtensionsToRtxPacket(const RtpPacketToSend& packet,
593 RtpPacketToSend* rtx_packet) {
594 // Set the relevant fixed packet headers. The following are not set:
595 // * Payload type - it is replaced in rtx packets.
596 // * Sequence number - RTX has a separate sequence numbering.
597 // * SSRC - RTX stream has its own SSRC.
598 rtx_packet->SetMarker(packet.Marker());
599 rtx_packet->SetTimestamp(packet.Timestamp());
600
601 // Set the variable fields in the packet header:
602 // * CSRCs - must be set before header extensions.
603 // * Header extensions - replace Rid header with RepairedRid header.
604 const std::vector<uint32_t> csrcs = packet.Csrcs();
605 rtx_packet->SetCsrcs(csrcs);
606 for (int extension_num = kRtpExtensionNone + 1;
607 extension_num < kRtpExtensionNumberOfExtensions; ++extension_num) {
608 auto extension = static_cast<RTPExtensionType>(extension_num);
609
610 // Stream ID header extensions (MID, RSID) are sent per-SSRC. Since RTX
611 // operates on a different SSRC, the presence and values of these header
612 // extensions should be determined separately and not blindly copied.
613 if (extension == kRtpExtensionMid ||
614 extension == kRtpExtensionRtpStreamId) {
615 continue;
616 }
617
618 // Empty extensions should be supported, so not checking `source.empty()`.
619 if (!packet.HasExtension(extension)) {
620 continue;
621 }
622
623 rtc::ArrayView<const uint8_t> source = packet.FindExtension(extension);
624
625 rtc::ArrayView<uint8_t> destination =
626 rtx_packet->AllocateExtension(extension, source.size());
627
628 // Could happen if any:
629 // 1. Extension has 0 length.
630 // 2. Extension is not registered in destination.
631 // 3. Allocating extension in destination failed.
632 if (destination.empty() || source.size() != destination.size()) {
633 continue;
634 }
635
636 std::memcpy(destination.begin(), source.begin(), destination.size());
637 }
638 }
639
BuildRtxPacket(const RtpPacketToSend & packet)640 std::unique_ptr<RtpPacketToSend> RTPSender::BuildRtxPacket(
641 const RtpPacketToSend& packet) {
642 std::unique_ptr<RtpPacketToSend> rtx_packet;
643
644 // Add original RTP header.
645 {
646 MutexLock lock(&send_mutex_);
647 if (!sending_media_)
648 return nullptr;
649
650 RTC_DCHECK(rtx_ssrc_);
651
652 // Replace payload type.
653 auto kv = rtx_payload_type_map_.find(packet.PayloadType());
654 if (kv == rtx_payload_type_map_.end())
655 return nullptr;
656
657 rtx_packet = std::make_unique<RtpPacketToSend>(&rtp_header_extension_map_,
658 max_packet_size_);
659
660 rtx_packet->SetPayloadType(kv->second);
661
662 // Replace SSRC.
663 rtx_packet->SetSsrc(*rtx_ssrc_);
664
665 CopyHeaderAndExtensionsToRtxPacket(packet, rtx_packet.get());
666
667 // RTX packets are sent on an SSRC different from the main media, so the
668 // decision to attach MID and/or RRID header extensions is completely
669 // separate from that of the main media SSRC.
670 //
671 // Note that RTX packets must used the RepairedRtpStreamId (RRID) header
672 // extension instead of the RtpStreamId (RID) header extension even though
673 // the payload is identical.
674 if (always_send_mid_and_rid_ || !rtx_ssrc_has_acked_) {
675 // These are no-ops if the corresponding header extension is not
676 // registered.
677 if (!mid_.empty()) {
678 rtx_packet->SetExtension<RtpMid>(mid_);
679 }
680 if (!rid_.empty()) {
681 rtx_packet->SetExtension<RepairedRtpStreamId>(rid_);
682 }
683 }
684 }
685 RTC_DCHECK(rtx_packet);
686
687 uint8_t* rtx_payload =
688 rtx_packet->AllocatePayload(packet.payload_size() + kRtxHeaderSize);
689 if (rtx_payload == nullptr)
690 return nullptr;
691
692 // Add OSN (original sequence number).
693 ByteWriter<uint16_t>::WriteBigEndian(rtx_payload, packet.SequenceNumber());
694
695 // Add original payload data.
696 auto payload = packet.payload();
697 if (!payload.empty()) {
698 memcpy(rtx_payload + kRtxHeaderSize, payload.data(), payload.size());
699 }
700
701 // Add original additional data.
702 rtx_packet->set_additional_data(packet.additional_data());
703
704 // Copy capture time so e.g. TransmissionOffset is correctly set.
705 rtx_packet->set_capture_time(packet.capture_time());
706
707 return rtx_packet;
708 }
709
SetRtpState(const RtpState & rtp_state)710 void RTPSender::SetRtpState(const RtpState& rtp_state) {
711 MutexLock lock(&send_mutex_);
712
713 timestamp_offset_ = rtp_state.start_timestamp;
714 ssrc_has_acked_ = rtp_state.ssrc_has_acked;
715 UpdateHeaderSizes();
716 }
717
GetRtpState() const718 RtpState RTPSender::GetRtpState() const {
719 MutexLock lock(&send_mutex_);
720
721 RtpState state;
722 state.start_timestamp = timestamp_offset_;
723 state.ssrc_has_acked = ssrc_has_acked_;
724 return state;
725 }
726
SetRtxRtpState(const RtpState & rtp_state)727 void RTPSender::SetRtxRtpState(const RtpState& rtp_state) {
728 MutexLock lock(&send_mutex_);
729 rtx_ssrc_has_acked_ = rtp_state.ssrc_has_acked;
730 }
731
GetRtxRtpState() const732 RtpState RTPSender::GetRtxRtpState() const {
733 MutexLock lock(&send_mutex_);
734
735 RtpState state;
736 state.start_timestamp = timestamp_offset_;
737 state.ssrc_has_acked = rtx_ssrc_has_acked_;
738
739 return state;
740 }
741
UpdateHeaderSizes()742 void RTPSender::UpdateHeaderSizes() {
743 const size_t rtp_header_length =
744 kRtpHeaderLength + sizeof(uint32_t) * csrcs_.size();
745
746 max_padding_fec_packet_header_ =
747 rtp_header_length + RtpHeaderExtensionSize(kFecOrPaddingExtensionSizes,
748 rtp_header_extension_map_);
749
750 // RtpStreamId and Mid are treated specially in that we check if they
751 // currently are being sent. RepairedRtpStreamId is ignored because it is sent
752 // instead of RtpStreamId on rtx packets and require the same size.
753 const bool send_mid_rid_on_rtx =
754 rtx_ssrc_.has_value() && !rtx_ssrc_has_acked_;
755 const bool send_mid_rid =
756 always_send_mid_and_rid_ || !ssrc_has_acked_ || send_mid_rid_on_rtx;
757 std::vector<RtpExtensionSize> non_volatile_extensions;
758 for (auto& extension :
759 audio_configured_ ? AudioExtensionSizes() : VideoExtensionSizes()) {
760 if (IsNonVolatile(extension.type)) {
761 switch (extension.type) {
762 case RTPExtensionType::kRtpExtensionMid:
763 if (send_mid_rid && !mid_.empty()) {
764 non_volatile_extensions.push_back(extension);
765 }
766 break;
767 case RTPExtensionType::kRtpExtensionRtpStreamId:
768 if (send_mid_rid && !rid_.empty()) {
769 non_volatile_extensions.push_back(extension);
770 }
771 break;
772 default:
773 non_volatile_extensions.push_back(extension);
774 }
775 }
776 }
777 max_media_packet_header_ =
778 rtp_header_length + RtpHeaderExtensionSize(non_volatile_extensions,
779 rtp_header_extension_map_);
780 // Reserve extra bytes if packet might be resent in an rtx packet.
781 if (rtx_ssrc_.has_value()) {
782 max_media_packet_header_ += kRtxHeaderSize;
783 }
784 }
785 } // namespace webrtc
786