forked from paullouisageneau/libdatachannel
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpeerconnection.cpp
1318 lines (1093 loc) · 42.3 KB
/
peerconnection.cpp
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
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2019 Paul-Louis Ageneau
* Copyright (c) 2020 Filip Klembara (in2core)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
#include "peerconnection.hpp"
#include "certificate.hpp"
#include "dtlstransport.hpp"
#include "icetransport.hpp"
#include "internals.hpp"
#include "logcounter.hpp"
#include "peerconnection.hpp"
#include "processor.hpp"
#include "rtp.hpp"
#include "sctptransport.hpp"
#include "utils.hpp"
#if RTC_ENABLE_MEDIA
#include "dtlssrtptransport.hpp"
#endif
#include <algorithm>
#include <array>
#include <iomanip>
#include <set>
#include <sstream>
#include <thread>
using namespace std::placeholders;
namespace rtc::impl {
static LogCounter COUNTER_MEDIA_TRUNCATED(plog::warning,
"Number of truncated RTP packets over past second");
static LogCounter COUNTER_SRTP_DECRYPT_ERROR(plog::warning,
"Number of SRTP decryption errors over past second");
static LogCounter COUNTER_SRTP_ENCRYPT_ERROR(plog::warning,
"Number of SRTP encryption errors over past second");
static LogCounter
COUNTER_UNKNOWN_PACKET_TYPE(plog::warning,
"Number of unknown RTCP packet types over past second");
PeerConnection::PeerConnection(Configuration config_)
: config(std::move(config_)), mCertificate(make_certificate(config.certificateType)) {
PLOG_VERBOSE << "Creating PeerConnection";
if (config.portRangeEnd && config.portRangeBegin > config.portRangeEnd)
throw std::invalid_argument("Invalid port range");
if (config.mtu) {
if (*config.mtu < 576) // Min MTU for IPv4
throw std::invalid_argument("Invalid MTU value");
if (*config.mtu > 1500) { // Standard Ethernet
PLOG_WARNING << "MTU set to " << *config.mtu;
} else {
PLOG_VERBOSE << "MTU set to " << *config.mtu;
}
}
}
PeerConnection::~PeerConnection() {
PLOG_VERBOSE << "Destroying PeerConnection";
mProcessor.join();
}
void PeerConnection::close() {
negotiationNeeded = false;
if (!closing.exchange(true)) {
PLOG_VERBOSE << "Closing PeerConnection";
if (auto transport = std::atomic_load(&mSctpTransport))
transport->stop();
else
remoteClose();
}
}
void PeerConnection::remoteClose() {
close();
if (state.load() != State::Closed) {
// Close data channels and tracks asynchronously
mProcessor.enqueue(&PeerConnection::closeDataChannels, shared_from_this());
mProcessor.enqueue(&PeerConnection::closeTracks, shared_from_this());
closeTransports();
}
}
optional<Description> PeerConnection::localDescription() const {
std::lock_guard lock(mLocalDescriptionMutex);
return mLocalDescription;
}
optional<Description> PeerConnection::remoteDescription() const {
std::lock_guard lock(mRemoteDescriptionMutex);
return mRemoteDescription;
}
size_t PeerConnection::remoteMaxMessageSize() const {
const size_t localMax = config.maxMessageSize.value_or(DEFAULT_LOCAL_MAX_MESSAGE_SIZE);
size_t remoteMax = DEFAULT_REMOTE_MAX_MESSAGE_SIZE;
std::lock_guard lock(mRemoteDescriptionMutex);
if (mRemoteDescription)
if (auto *application = mRemoteDescription->application())
if (auto max = application->maxMessageSize()) {
// RFC 8841: If the SDP "max-message-size" attribute contains a maximum message
// size value of zero, it indicates that the SCTP endpoint will handle messages
// of any size, subject to memory capacity, etc.
remoteMax = *max > 0 ? *max : std::numeric_limits<size_t>::max();
}
return std::min(remoteMax, localMax);
}
// Helper for PeerConnection::initXTransport methods: start and emplace the transport
template <typename T>
shared_ptr<T> emplaceTransport(PeerConnection *pc, shared_ptr<T> *member, shared_ptr<T> transport) {
std::atomic_store(member, transport);
try {
transport->start();
} catch (...) {
std::atomic_store(member, decltype(transport)(nullptr));
throw;
}
if (pc->closing.load() || pc->state.load() == PeerConnection::State::Closed) {
std::atomic_store(member, decltype(transport)(nullptr));
transport->stop();
return nullptr;
}
return transport;
}
shared_ptr<IceTransport> PeerConnection::initIceTransport() {
try {
if (auto transport = std::atomic_load(&mIceTransport))
return transport;
PLOG_VERBOSE << "Starting ICE transport";
auto transport = std::make_shared<IceTransport>(
config, weak_bind(&PeerConnection::processLocalCandidate, this, _1),
[this, weak_this = weak_from_this()](IceTransport::State transportState) {
auto shared_this = weak_this.lock();
if (!shared_this)
return;
switch (transportState) {
case IceTransport::State::Connecting:
changeIceState(IceState::Checking);
changeState(State::Connecting);
break;
case IceTransport::State::Connected:
changeIceState(IceState::Connected);
initDtlsTransport();
break;
case IceTransport::State::Completed:
changeIceState(IceState::Completed);
break;
case IceTransport::State::Failed:
changeIceState(IceState::Failed);
changeState(State::Failed);
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
break;
case IceTransport::State::Disconnected:
changeIceState(IceState::Disconnected);
changeState(State::Disconnected);
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
break;
default:
// Ignore
break;
}
},
[this, weak_this = weak_from_this()](IceTransport::GatheringState gatheringState) {
auto shared_this = weak_this.lock();
if (!shared_this)
return;
switch (gatheringState) {
case IceTransport::GatheringState::InProgress:
changeGatheringState(GatheringState::InProgress);
break;
case IceTransport::GatheringState::Complete:
endLocalCandidates();
changeGatheringState(GatheringState::Complete);
break;
default:
// Ignore
break;
}
});
return emplaceTransport(this, &mIceTransport, std::move(transport));
} catch (const std::exception &e) {
PLOG_ERROR << e.what();
changeState(State::Failed);
throw std::runtime_error("ICE transport initialization failed");
}
}
shared_ptr<DtlsTransport> PeerConnection::initDtlsTransport() {
try {
if (auto transport = std::atomic_load(&mDtlsTransport))
return transport;
PLOG_VERBOSE << "Starting DTLS transport";
auto fingerprintAlgorithm = CertificateFingerprint::Algorithm::Sha256;
if (auto remote = remoteDescription(); remote && remote->fingerprint()) {
fingerprintAlgorithm = remote->fingerprint()->algorithm;
}
auto lower = std::atomic_load(&mIceTransport);
if (!lower)
throw std::logic_error("No underlying ICE transport for DTLS transport");
auto certificate = mCertificate.get();
auto verifierCallback = weak_bind(&PeerConnection::checkFingerprint, this, _1);
auto dtlsStateChangeCallback =
[this, weak_this = weak_from_this()](DtlsTransport::State transportState) {
auto shared_this = weak_this.lock();
if (!shared_this)
return;
switch (transportState) {
case DtlsTransport::State::Connected:
if (auto remote = remoteDescription(); remote && remote->hasApplication())
initSctpTransport();
else
changeState(State::Connected);
mProcessor.enqueue(&PeerConnection::openTracks, shared_from_this());
break;
case DtlsTransport::State::Failed:
changeState(State::Failed);
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
break;
case DtlsTransport::State::Disconnected:
changeState(State::Disconnected);
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
break;
default:
// Ignore
break;
}
};
shared_ptr<DtlsTransport> transport;
auto local = localDescription();
if (config.forceMediaTransport || (local && local->hasAudioOrVideo())) {
#if RTC_ENABLE_MEDIA
PLOG_INFO << "This connection requires media support";
// DTLS-SRTP
transport = std::make_shared<DtlsSrtpTransport>(
lower, certificate, config.mtu, fingerprintAlgorithm, verifierCallback,
weak_bind(&PeerConnection::forwardMedia, this, _1), dtlsStateChangeCallback);
#else
PLOG_WARNING << "Ignoring media support (not compiled with media support)";
#endif
}
if (!transport) {
// DTLS only
transport = std::make_shared<DtlsTransport>(lower, certificate, config.mtu,
fingerprintAlgorithm, verifierCallback,
dtlsStateChangeCallback);
}
return emplaceTransport(this, &mDtlsTransport, std::move(transport));
} catch (const std::exception &e) {
PLOG_ERROR << e.what();
changeState(State::Failed);
throw std::runtime_error("DTLS transport initialization failed");
}
}
shared_ptr<SctpTransport> PeerConnection::initSctpTransport() {
try {
if (auto transport = std::atomic_load(&mSctpTransport))
return transport;
PLOG_VERBOSE << "Starting SCTP transport";
auto lower = std::atomic_load(&mDtlsTransport);
if (!lower)
throw std::logic_error("No underlying DTLS transport for SCTP transport");
auto local = localDescription();
if (!local || !local->application())
throw std::logic_error("Starting SCTP transport without local application description");
auto remote = remoteDescription();
if (!remote || !remote->application())
throw std::logic_error(
"Starting SCTP transport without remote application description");
SctpTransport::Ports ports = {};
ports.local = local->application()->sctpPort().value_or(DEFAULT_SCTP_PORT);
ports.remote = remote->application()->sctpPort().value_or(DEFAULT_SCTP_PORT);
auto transport = std::make_shared<SctpTransport>(
lower, config, std::move(ports), weak_bind(&PeerConnection::forwardMessage, this, _1),
weak_bind(&PeerConnection::forwardBufferedAmount, this, _1, _2),
[this, weak_this = weak_from_this()](SctpTransport::State transportState) {
auto shared_this = weak_this.lock();
if (!shared_this)
return;
switch (transportState) {
case SctpTransport::State::Connected:
changeState(State::Connected);
assignDataChannels();
mProcessor.enqueue(&PeerConnection::openDataChannels, shared_from_this());
break;
case SctpTransport::State::Failed:
changeState(State::Failed);
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
break;
case SctpTransport::State::Disconnected:
changeState(State::Disconnected);
mProcessor.enqueue(&PeerConnection::remoteClose, shared_from_this());
break;
default:
// Ignore
break;
}
});
return emplaceTransport(this, &mSctpTransport, std::move(transport));
} catch (const std::exception &e) {
PLOG_ERROR << e.what();
changeState(State::Failed);
throw std::runtime_error("SCTP transport initialization failed");
}
}
shared_ptr<IceTransport> PeerConnection::getIceTransport() const {
return std::atomic_load(&mIceTransport);
}
shared_ptr<DtlsTransport> PeerConnection::getDtlsTransport() const {
return std::atomic_load(&mDtlsTransport);
}
shared_ptr<SctpTransport> PeerConnection::getSctpTransport() const {
return std::atomic_load(&mSctpTransport);
}
void PeerConnection::closeTransports() {
PLOG_VERBOSE << "Closing transports";
// Change ICE state to sink state Closed
changeIceState(IceState::Closed);
// Change state to sink state Closed
if (!changeState(State::Closed))
return; // already closed
// Reset interceptor and callbacks now that state is changed
setMediaHandler(nullptr);
resetCallbacks();
// Pass the pointers to a thread, allowing to terminate a transport from its own thread
auto sctp = std::atomic_exchange(&mSctpTransport, decltype(mSctpTransport)(nullptr));
auto dtls = std::atomic_exchange(&mDtlsTransport, decltype(mDtlsTransport)(nullptr));
auto ice = std::atomic_exchange(&mIceTransport, decltype(mIceTransport)(nullptr));
if (sctp) {
sctp->onRecv(nullptr);
sctp->onBufferedAmount(nullptr);
}
using array = std::array<shared_ptr<Transport>, 3>;
array transports{std::move(sctp), std::move(dtls), std::move(ice)};
for (const auto &t : transports)
if (t)
t->onStateChange(nullptr);
TearDownProcessor::Instance().enqueue(
[transports = std::move(transports), token = Init::Instance().token()]() mutable {
for (const auto &t : transports) {
if (t) {
t->stop();
break;
}
}
for (auto &t : transports)
t.reset();
});
}
void PeerConnection::endLocalCandidates() {
std::lock_guard lock(mLocalDescriptionMutex);
if (mLocalDescription)
mLocalDescription->endCandidates();
}
void PeerConnection::rollbackLocalDescription() {
PLOG_DEBUG << "Rolling back pending local description";
std::unique_lock lock(mLocalDescriptionMutex);
if (mCurrentLocalDescription) {
std::vector<Candidate> existingCandidates;
if (mLocalDescription)
existingCandidates = mLocalDescription->extractCandidates();
mLocalDescription.emplace(std::move(*mCurrentLocalDescription));
mLocalDescription->addCandidates(std::move(existingCandidates));
mCurrentLocalDescription.reset();
}
}
bool PeerConnection::checkFingerprint(const std::string &fingerprint) const {
std::lock_guard lock(mRemoteDescriptionMutex);
if (!mRemoteDescription || !mRemoteDescription->fingerprint())
return false;
auto expectedFingerprint = mRemoteDescription->fingerprint()->value;
if (expectedFingerprint == fingerprint) {
PLOG_VERBOSE << "Valid fingerprint \"" << fingerprint << "\"";
return true;
}
PLOG_ERROR << "Invalid fingerprint \"" << fingerprint << "\", expected \"" << expectedFingerprint << "\"";
return false;
}
void PeerConnection::forwardMessage(message_ptr message) {
if (!message) {
remoteCloseDataChannels();
return;
}
auto iceTransport = std::atomic_load(&mIceTransport);
auto sctpTransport = std::atomic_load(&mSctpTransport);
if (!iceTransport || !sctpTransport)
return;
const uint16_t stream = uint16_t(message->stream);
auto [channel, found] = findDataChannel(stream);
if (DataChannel::IsOpenMessage(message)) {
if (found) {
// The stream is already used, the receiver must close the DataChannel
PLOG_WARNING << "Got open message on already used stream " << stream;
if (channel && !channel->isClosed())
channel->close();
else
sctpTransport->closeStream(message->stream);
return;
}
const uint16_t remoteParity = (iceTransport->role() == Description::Role::Active) ? 1 : 0;
if (stream % 2 != remoteParity) {
// The odd/even rule is violated, the receiver must close the DataChannel
PLOG_WARNING << "Got open message violating the odd/even rule on stream " << stream;
sctpTransport->closeStream(message->stream);
return;
}
channel = std::make_shared<IncomingDataChannel>(weak_from_this(), sctpTransport);
channel->assignStream(stream);
channel->openCallback =
weak_bind(&PeerConnection::triggerDataChannel, this, weak_ptr<DataChannel>{channel});
std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
mDataChannels.emplace(stream, channel);
} else if (!found) {
if (message->type == Message::Reset)
return; // ignore
// Invalid, close the DataChannel
PLOG_WARNING << "Got unexpected message on stream " << stream;
sctpTransport->closeStream(message->stream);
return;
}
if (message->type == Message::Reset) {
// Incoming stream is reset, unregister it
removeDataChannel(stream);
}
if (channel) {
// Forward the message
channel->incoming(message);
} else {
// DataChannel was destroyed, ignore
PLOG_DEBUG << "Ignored message on stream " << stream << ", DataChannel is destroyed";
}
}
void PeerConnection::forwardMedia([[maybe_unused]] message_ptr message) {
#if RTC_ENABLE_MEDIA
if (!message)
return;
// TODO: outgoing
if (auto handler = getMediaHandler()) {
message_vector messages{std::move(message)};
handler->incoming(messages, [this](message_ptr message) {
auto transport = std::atomic_load(&mDtlsTransport);
if (auto srtpTransport = std::dynamic_pointer_cast<DtlsSrtpTransport>(transport))
srtpTransport->send(std::move(message));
});
for (auto &m : messages)
dispatchMedia(std::move(m));
} else {
dispatchMedia(std::move(message));
}
#endif
}
void PeerConnection::dispatchMedia([[maybe_unused]] message_ptr message) {
#if RTC_ENABLE_MEDIA
std::shared_lock lock(mTracksMutex); // read-only
if (mTrackLines.size()==1) {
if (auto track = mTrackLines.front().lock())
track->incoming(message);
return;
}
// Browsers like to compound their packets with a random SSRC.
// we have to do this monstrosity to distribute the report blocks
if (message->type == Message::Control) {
std::set<uint32_t> ssrcs;
size_t offset = 0;
while ((sizeof(RtcpHeader) + offset) <= message->size()) {
auto header = reinterpret_cast<RtcpHeader *>(message->data() + offset);
if (header->lengthInBytes() > message->size() - offset) {
COUNTER_MEDIA_TRUNCATED++;
break;
}
offset += header->lengthInBytes();
if (header->payloadType() == 205 || header->payloadType() == 206) {
auto rtcpfb = reinterpret_cast<RtcpFbHeader *>(header);
ssrcs.insert(rtcpfb->packetSenderSSRC());
ssrcs.insert(rtcpfb->mediaSourceSSRC());
} else if (header->payloadType() == 200) {
auto rtcpsr = reinterpret_cast<RtcpSr *>(header);
ssrcs.insert(rtcpsr->senderSSRC());
for (int i = 0; i < rtcpsr->header.reportCount(); ++i)
ssrcs.insert(rtcpsr->getReportBlock(i)->getSSRC());
} else if (header->payloadType() == 201) {
auto rtcprr = reinterpret_cast<RtcpRr *>(header);
ssrcs.insert(rtcprr->senderSSRC());
for (int i = 0; i < rtcprr->header.reportCount(); ++i)
ssrcs.insert(rtcprr->getReportBlock(i)->getSSRC());
} else if (header->payloadType() == 202) {
auto sdes = reinterpret_cast<RtcpSdes *>(header);
if (!sdes->isValid()) {
PLOG_WARNING << "RTCP SDES packet is invalid";
continue;
}
for (unsigned int i = 0; i < sdes->chunksCount(); i++) {
auto chunk = sdes->getChunk(i);
ssrcs.insert(chunk->ssrc());
}
} else {
// PT=203 == Goodbye
// PT=204 == Application Specific
// PT=207 == Extended Report
if (header->payloadType() != 203 && header->payloadType() != 204 &&
header->payloadType() != 207) {
COUNTER_UNKNOWN_PACKET_TYPE++;
}
}
}
if (!ssrcs.empty()) {
for (uint32_t ssrc : ssrcs) {
if (auto it = mTracksBySsrc.find(ssrc); it != mTracksBySsrc.end()) {
if (auto track = it->second.lock())
track->incoming(message);
}
}
return;
}
}
uint32_t ssrc = uint32_t(message->stream);
if (auto it = mTracksBySsrc.find(ssrc); it != mTracksBySsrc.end()) {
if (auto track = it->second.lock())
track->incoming(message);
} else {
/*
* TODO: So the problem is that when stop sending streams, we stop getting report blocks for
* those streams Therefore when we get compound RTCP packets, they are empty, and we can't
* forward them. Therefore, it is expected that we don't know where to forward packets. Is
* this ideal? No! Do I know how to fix it? No!
*/
// PLOG_WARNING << "Track not found for SSRC " << ssrc << ", dropping";
return;
}
#endif
}
void PeerConnection::forwardBufferedAmount(uint16_t stream, size_t amount) {
[[maybe_unused]] auto [channel, found] = findDataChannel(stream);
if (channel)
channel->triggerBufferedAmount(amount);
}
shared_ptr<DataChannel> PeerConnection::emplaceDataChannel(string label, DataChannelInit init) {
std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
// If the DataChannel is user-negotiated, do not negotiate it in-band
auto channel =
init.negotiated
? std::make_shared<DataChannel>(weak_from_this(), std::move(label),
std::move(init.protocol), std::move(init.reliability))
: std::make_shared<OutgoingDataChannel>(weak_from_this(), std::move(label),
std::move(init.protocol),
std::move(init.reliability));
// If the user supplied a stream id, use it, otherwise assign it later
if (init.id) {
uint16_t stream = *init.id;
if (stream > maxDataChannelStream())
throw std::invalid_argument("DataChannel stream id is too high");
channel->assignStream(stream);
mDataChannels.emplace(std::make_pair(stream, channel));
} else {
mUnassignedDataChannels.push_back(channel);
}
lock.unlock(); // we are going to call assignDataChannels()
// If SCTP is connected, assign and open now
auto sctpTransport = std::atomic_load(&mSctpTransport);
if (sctpTransport && sctpTransport->state() == SctpTransport::State::Connected) {
assignDataChannels();
channel->open(sctpTransport);
}
return channel;
}
std::pair<shared_ptr<DataChannel>, bool> PeerConnection::findDataChannel(uint16_t stream) {
std::shared_lock lock(mDataChannelsMutex); // read-only
if (auto it = mDataChannels.find(stream); it != mDataChannels.end())
return std::make_pair(it->second.lock(), true);
else
return std::make_pair(nullptr, false);
}
bool PeerConnection::removeDataChannel(uint16_t stream) {
std::unique_lock lock(mDataChannelsMutex); // we are going to erase
return mDataChannels.erase(stream) != 0;
}
uint16_t PeerConnection::maxDataChannelStream() const {
auto sctpTransport = std::atomic_load(&mSctpTransport);
return sctpTransport ? sctpTransport->maxStream() : (MAX_SCTP_STREAMS_COUNT - 1);
}
void PeerConnection::assignDataChannels() {
std::unique_lock lock(mDataChannelsMutex); // we are going to emplace
auto iceTransport = std::atomic_load(&mIceTransport);
if (!iceTransport)
throw std::logic_error("Attempted to assign DataChannels without ICE transport");
const uint16_t maxStream = maxDataChannelStream();
for (auto it = mUnassignedDataChannels.begin(); it != mUnassignedDataChannels.end(); ++it) {
auto channel = it->lock();
if (!channel)
continue;
// RFC 8832: The peer that initiates opening a data channel selects a stream identifier
// for which the corresponding incoming and outgoing streams are unused. If the side is
// acting as the DTLS client, it MUST choose an even stream identifier; if the side is
// acting as the DTLS server, it MUST choose an odd one. See
// https://www.rfc-editor.org/rfc/rfc8832.html#section-6
uint16_t stream = (iceTransport->role() == Description::Role::Active) ? 0 : 1;
while (true) {
if (stream > maxStream)
throw std::runtime_error("Too many DataChannels");
if (mDataChannels.find(stream) == mDataChannels.end())
break;
stream += 2;
}
PLOG_DEBUG << "Assigning stream " << stream << " to DataChannel";
channel->assignStream(stream);
mDataChannels.emplace(std::make_pair(stream, channel));
}
mUnassignedDataChannels.clear();
}
void PeerConnection::iterateDataChannels(
std::function<void(shared_ptr<DataChannel> channel)> func) {
std::vector<shared_ptr<DataChannel>> locked;
{
std::shared_lock lock(mDataChannelsMutex); // read-only
locked.reserve(mDataChannels.size());
for(auto it = mDataChannels.begin(); it != mDataChannels.end(); ++it) {
auto channel = it->second.lock();
if (channel && !channel->isClosed())
locked.push_back(std::move(channel));
}
}
for (auto &channel : locked) {
try {
func(std::move(channel));
} catch (const std::exception &e) {
PLOG_WARNING << e.what();
}
}
}
void PeerConnection::openDataChannels() {
if (auto transport = std::atomic_load(&mSctpTransport))
iterateDataChannels([&](shared_ptr<DataChannel> channel) {
if (!channel->isOpen())
channel->open(transport);
});
}
void PeerConnection::closeDataChannels() {
iterateDataChannels([&](shared_ptr<DataChannel> channel) { channel->close(); });
}
void PeerConnection::remoteCloseDataChannels() {
iterateDataChannels([&](shared_ptr<DataChannel> channel) { channel->remoteClose(); });
}
shared_ptr<Track> PeerConnection::emplaceTrack(Description::Media description) {
std::unique_lock lock(mTracksMutex); // we are going to emplace
#if !RTC_ENABLE_MEDIA
// No media support, mark as removed
PLOG_WARNING << "Tracks are disabled (not compiled with media support)";
description.markRemoved();
#endif
shared_ptr<Track> track;
if (auto it = mTracks.find(description.mid()); it != mTracks.end())
if (auto t = it->second.lock(); t && !t->isClosed())
track = std::move(t);
if (track) {
track->setDescription(std::move(description));
} else {
track = std::make_shared<Track>(weak_from_this(), std::move(description));
mTracks.emplace(std::make_pair(track->mid(), track));
mTrackLines.emplace_back(track);
}
auto handler = getMediaHandler();
if (handler)
handler->media(track->description());
if (track->description().isRemoved())
track->close();
return track;
}
void PeerConnection::iterateTracks(std::function<void(shared_ptr<Track> track)> func) {
std::vector<shared_ptr<Track>> locked;
{
std::shared_lock lock(mTracksMutex); // read-only
locked.reserve(mTrackLines.size());
for(auto it = mTrackLines.begin(); it != mTrackLines.end(); ++it) {
auto track = it->lock();
if (track && !track->isClosed())
locked.push_back(std::move(track));
}
}
for (auto &track : locked) {
try {
func(std::move(track));
} catch (const std::exception &e) {
PLOG_WARNING << e.what();
}
}
}
void PeerConnection::openTracks() {
#if RTC_ENABLE_MEDIA
if (auto transport = std::atomic_load(&mDtlsTransport)) {
auto srtpTransport = std::dynamic_pointer_cast<DtlsSrtpTransport>(transport);
iterateTracks([&](const shared_ptr<Track> &track) {
if (!track->isOpen()) {
if (srtpTransport) {
track->open(srtpTransport);
} else {
// A track was added during a latter renegotiation, whereas SRTP transport was
// not initialized. This is an optimization to use the library with data
// channels only. Set forceMediaTransport to true to initialize the transport
// before dynamically adding tracks.
auto errorMsg = "The connection has no media transport";
PLOG_ERROR << errorMsg;
track->triggerError(errorMsg);
}
}
});
}
#endif
}
void PeerConnection::closeTracks() {
std::shared_lock lock(mTracksMutex); // read-only
iterateTracks([&](shared_ptr<Track> track) { track->close(); });
}
void PeerConnection::validateRemoteDescription(const Description &description) {
if (!description.iceUfrag())
throw std::invalid_argument("Remote description has no ICE user fragment");
if (!description.icePwd())
throw std::invalid_argument("Remote description has no ICE password");
if (!description.fingerprint())
throw std::invalid_argument("Remote description has no valid fingerprint");
if (description.mediaCount() == 0)
throw std::invalid_argument("Remote description has no media line");
int activeMediaCount = 0;
for (unsigned int i = 0; i < description.mediaCount(); ++i)
std::visit(rtc::overloaded{[&](const Description::Application *application) {
if (!application->isRemoved())
++activeMediaCount;
},
[&](const Description::Media *media) {
if (!media->isRemoved() ||
media->direction() != Description::Direction::Inactive)
++activeMediaCount;
}},
description.media(i));
if (activeMediaCount == 0)
throw std::invalid_argument("Remote description has no active media");
PLOG_VERBOSE << "Remote description looks valid";
}
void PeerConnection::processLocalDescription(Description description) {
const uint16_t localSctpPort = DEFAULT_SCTP_PORT;
const size_t localMaxMessageSize =
config.maxMessageSize.value_or(DEFAULT_LOCAL_MAX_MESSAGE_SIZE);
// Clean up the application entry the ICE transport might have added already (libnice)
description.clearMedia();
if (auto remote = remoteDescription()) {
// Reciprocate remote description
for (unsigned int i = 0; i < remote->mediaCount(); ++i)
std::visit( // reciprocate each media
rtc::overloaded{
[&](Description::Application *remoteApp) {
std::shared_lock lock(mDataChannelsMutex);
if (!mDataChannels.empty() || !mUnassignedDataChannels.empty()) {
// Prefer local description
Description::Application app(remoteApp->mid());
app.setSctpPort(localSctpPort);
app.setMaxMessageSize(localMaxMessageSize);
PLOG_DEBUG << "Adding application to local description, mid=\""
<< app.mid() << "\"";
description.addMedia(std::move(app));
return;
}
auto reciprocated = remoteApp->reciprocate();
reciprocated.hintSctpPort(localSctpPort);
reciprocated.setMaxMessageSize(localMaxMessageSize);
PLOG_DEBUG << "Reciprocating application in local description, mid=\""
<< reciprocated.mid() << "\"";
description.addMedia(std::move(reciprocated));
},
[&](Description::Media *remoteMedia) {
std::unique_lock lock(mTracksMutex); // we may emplace a track
if (auto it = mTracks.find(remoteMedia->mid()); it != mTracks.end()) {
// Prefer local description
if (auto track = it->second.lock()) {
auto media = track->description();
PLOG_DEBUG << "Adding media to local description, mid=\""
<< media.mid() << "\", removed=" << std::boolalpha
<< media.isRemoved();
description.addMedia(std::move(media));
} else {
auto reciprocated = remoteMedia->reciprocate();
reciprocated.markRemoved();
PLOG_DEBUG << "Adding media to local description, mid=\""
<< reciprocated.mid()
<< "\", removed=true (track is destroyed)";
description.addMedia(std::move(reciprocated));
}
return;
}
auto reciprocated = remoteMedia->reciprocate();
#if !RTC_ENABLE_MEDIA
if (!reciprocated.isRemoved()) {
// No media support, mark as removed
PLOG_WARNING << "Rejecting track (not compiled with media support)";
reciprocated.markRemoved();
}
#endif
PLOG_DEBUG << "Reciprocating media in local description, mid=\""
<< reciprocated.mid() << "\", removed=" << std::boolalpha
<< reciprocated.isRemoved();
// Create incoming track
auto track =
std::make_shared<Track>(weak_from_this(), std::move(reciprocated));
mTracks.emplace(std::make_pair(track->mid(), track));
mTrackLines.emplace_back(track);
triggerTrack(track); // The user may modify the track description
auto handler = getMediaHandler();
if (handler)
handler->media(track->description());
if (track->description().isRemoved())
track->close();
description.addMedia(track->description());
},
},
remote->media(i));
// We need to update the SSRC cache for newly-created incoming tracks
updateTrackSsrcCache(*remote);
}
if (description.type() == Description::Type::Offer) {
// This is an offer, add locally created data channels and tracks
// Add media for local tracks
std::shared_lock lock(mTracksMutex);
for (auto it = mTrackLines.begin(); it != mTrackLines.end(); ++it) {
if (auto track = it->lock()) {
if (description.hasMid(track->mid()))
continue;
auto media = track->description();
PLOG_DEBUG << "Adding media to local description, mid=\"" << media.mid()
<< "\", removed=" << std::boolalpha << media.isRemoved();
description.addMedia(std::move(media));
}
}
// Add application for data channels
if (!description.hasApplication()) {
std::shared_lock lock(mDataChannelsMutex);
if (!mDataChannels.empty() || !mUnassignedDataChannels.empty()) {
// Prevents mid collision with remote or local tracks
unsigned int m = 0;
while (description.hasMid(std::to_string(m)))
++m;
Description::Application app(std::to_string(m));
app.setSctpPort(localSctpPort);
app.setMaxMessageSize(localMaxMessageSize);
PLOG_DEBUG << "Adding application to local description, mid=\"" << app.mid()
<< "\"";
description.addMedia(std::move(app));
}
}