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