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
|
/* 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 http://mozilla.org/MPL/2.0/. */
#include "jsapi/TransceiverImpl.h"
#include "mozilla/UniquePtr.h"
#include <string>
#include <vector>
#include "libwebrtcglue/AudioConduit.h"
#include "libwebrtcglue/VideoConduit.h"
#include "MediaTrackGraph.h"
#include "transportbridge/MediaPipeline.h"
#include "transportbridge/MediaPipelineFilter.h"
#include "jsep/JsepTrack.h"
#include "sdp/SdpHelper.h"
#include "MediaTrackGraphImpl.h"
#include "transport/logging.h"
#include "MediaEngine.h"
#include "nsIPrincipal.h"
#include "MediaSegment.h"
#include "RemoteTrackSource.h"
#include "libwebrtcglue/RtpRtcpConfig.h"
#include "MediaTransportHandler.h"
#include "mozilla/dom/RTCRtpReceiverBinding.h"
#include "mozilla/dom/RTCRtpSenderBinding.h"
#include "mozilla/dom/RTCRtpTransceiverBinding.h"
#include "mozilla/dom/TransceiverImplBinding.h"
#include "RTCDtlsTransport.h"
#include "RTCRtpReceiver.h"
#include "RTCDTMFSender.h"
#include "libwebrtcglue/WebrtcGmpVideoCodec.h"
namespace mozilla {
using namespace dom;
MOZ_MTLOG_MODULE("transceiverimpl")
using LocalDirection = MediaSessionConduitLocalDirection;
NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(TransceiverImpl, mWindow, mSendTrack,
mReceiver, mDtmf, mDtlsTransport,
mLastStableDtlsTransport)
NS_IMPL_CYCLE_COLLECTING_ADDREF(TransceiverImpl)
NS_IMPL_CYCLE_COLLECTING_RELEASE(TransceiverImpl)
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(TransceiverImpl)
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END
TransceiverImpl::TransceiverImpl(
nsPIDOMWindowInner* aWindow, bool aPrivacyNeeded,
const std::string& aPCHandle, MediaTransportHandler* aTransportHandler,
JsepTransceiver* aJsepTransceiver, nsISerialEventTarget* aMainThread,
nsISerialEventTarget* aStsThread, dom::MediaStreamTrack* aSendTrack,
WebRtcCallWrapper* aCallWrapper)
: mWindow(aWindow),
mPCHandle(aPCHandle),
mTransportHandler(aTransportHandler),
mJsepTransceiver(aJsepTransceiver),
mHaveSetupTransport(false),
mMainThread(aMainThread),
mStsThread(aStsThread),
mSendTrack(aSendTrack),
mCallWrapper(aCallWrapper) {
if (IsVideo()) {
InitVideo();
} else {
InitAudio();
}
if (!IsValid()) {
return;
}
mConduit->SetPCHandle(mPCHandle);
mReceiver = new RTCRtpReceiver(aWindow, aPrivacyNeeded, aPCHandle,
aTransportHandler, aJsepTransceiver,
aMainThread, aStsThread, mConduit, this);
if (!IsVideo()) {
mDtmf = new RTCDTMFSender(
aWindow, this, static_cast<AudioSessionConduit*>(mConduit.get()));
}
mTransmitPipeline =
new MediaPipelineTransmit(mPCHandle, mTransportHandler, mMainThread.get(),
mStsThread.get(), IsVideo(), mConduit);
mTransmitPipeline->SetTrack(mSendTrack);
auto self = nsMainThreadPtrHandle<TransceiverImpl>(
new nsMainThreadPtrHolder<TransceiverImpl>(
"TransceiverImpl::TransceiverImpl::self", this, false));
mStsThread->Dispatch(
NS_NewRunnableFunction("TransceiverImpl::TransceiverImpl", [self] {
self->mTransportHandler->SignalStateChange.connect(
self.get(), &TransceiverImpl::UpdateDtlsTransportState);
self->mTransportHandler->SignalRtcpStateChange.connect(
self.get(), &TransceiverImpl::UpdateDtlsTransportState);
}));
}
TransceiverImpl::~TransceiverImpl() = default;
void TransceiverImpl::SetDtlsTransport(dom::RTCDtlsTransport* aDtlsTransport,
bool aStable) {
mDtlsTransport = aDtlsTransport;
if (aStable) {
mLastStableDtlsTransport = mDtlsTransport;
}
}
void TransceiverImpl::RollbackToStableDtlsTransport() {
mDtlsTransport = mLastStableDtlsTransport;
}
void TransceiverImpl::UpdateDtlsTransportState(const std::string& aTransportId,
TransportLayer::State aState) {
if (!mMainThread->IsOnCurrentThread()) {
mMainThread->Dispatch(
WrapRunnable(this, &TransceiverImpl::UpdateDtlsTransportState,
aTransportId, aState),
NS_DISPATCH_NORMAL);
return;
}
if (!mDtlsTransport) {
return;
}
mDtlsTransport->UpdateState(aState);
}
void TransceiverImpl::InitAudio() {
mConduit = AudioSessionConduit::Create(mCallWrapper, mStsThread);
if (!mConduit) {
MOZ_MTLOG(ML_ERROR, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< ": Failed to create AudioSessionConduit");
// TODO(bug 1422897): We need a way to record this when it happens in the
// wild.
}
}
void TransceiverImpl::InitVideo() {
mConduit = VideoSessionConduit::Create(mCallWrapper, mStsThread);
if (!mConduit) {
MOZ_MTLOG(ML_ERROR, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< ": Failed to create VideoSessionConduit");
// TODO(bug 1422897): We need a way to record this when it happens in the
// wild.
}
}
nsresult TransceiverImpl::UpdateSinkIdentity(
const dom::MediaStreamTrack* aTrack, nsIPrincipal* aPrincipal,
const PeerIdentity* aSinkIdentity) {
if (mJsepTransceiver->IsStopped()) {
return NS_OK;
}
mTransmitPipeline->UpdateSinkIdentity_m(aTrack, aPrincipal, aSinkIdentity);
return NS_OK;
}
void TransceiverImpl::Shutdown_m() {
// Called via PCImpl::Close -> PCImpl::CloseInt -> PCImpl::ShutdownMedia ->
// PCMedia::SelfDestruct. Satisfies step 7 of
// https://w3c.github.io/webrtc-pc/#dom-rtcpeerconnection-close
if (mDtlsTransport) {
mDtlsTransport->UpdateState(TransportLayer::TS_CLOSED);
}
Stop();
mTransmitPipeline = nullptr;
auto self = nsMainThreadPtrHandle<TransceiverImpl>(
new nsMainThreadPtrHolder<TransceiverImpl>(
"TransceiverImpl::Shutdown_m::self", this, false));
mStsThread->Dispatch(NS_NewRunnableFunction(__func__, [self] {
self->disconnect_all();
self->mTransportHandler = nullptr;
}));
}
nsresult TransceiverImpl::UpdateSendTrack(dom::MediaStreamTrack* aSendTrack) {
if (mJsepTransceiver->IsStopped()) {
return NS_ERROR_UNEXPECTED;
}
MOZ_MTLOG(ML_DEBUG, mPCHandle << "[" << mMid << "]: " << __FUNCTION__ << "("
<< aSendTrack << ")");
mSendTrack = aSendTrack;
return mTransmitPipeline->SetTrack(mSendTrack);
}
nsresult TransceiverImpl::UpdateTransport() {
if (!mJsepTransceiver->HasLevel() || mJsepTransceiver->IsStopped()) {
return NS_OK;
}
mReceiver->UpdateTransport();
if (!mHaveSetupTransport) {
mTransmitPipeline->SetLevel(mJsepTransceiver->GetLevel());
mHaveSetupTransport = true;
}
mTransmitPipeline->UpdateTransport_m(
mJsepTransceiver->mTransport.mTransportId, nullptr);
return NS_OK;
}
nsresult TransceiverImpl::UpdateConduit() {
if (mJsepTransceiver->IsStopped()) {
return NS_OK;
}
if (mJsepTransceiver->IsAssociated()) {
mMid = mJsepTransceiver->GetMid();
} else {
mMid.clear();
}
mReceiver->Stop();
mTransmitPipeline->Stop();
// NOTE(pkerr) - the Call API requires the both local_ssrc and remote_ssrc be
// set to a non-zero value or the CreateVideo...Stream call will fail.
if (mJsepTransceiver->mSendTrack.GetSsrcs().empty()) {
MOZ_MTLOG(ML_ERROR,
mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " No local SSRC set! (Should be set regardless of "
"whether we're sending RTP; we need a local SSRC in "
"all cases)");
return NS_ERROR_FAILURE;
}
if (!mConduit->SetLocalSSRCs(mJsepTransceiver->mSendTrack.GetSsrcs(),
mJsepTransceiver->mSendTrack.GetRtxSsrcs())) {
MOZ_MTLOG(ML_ERROR, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " SetLocalSSRCs failed");
return NS_ERROR_FAILURE;
}
mConduit->SetLocalCNAME(mJsepTransceiver->mSendTrack.GetCNAME().c_str());
mConduit->SetLocalMID(mMid);
nsresult rv;
mReceiver->UpdateConduit();
// TODO(bug 1616937): Move this stuff into RTCRtpSender.
if (IsVideo()) {
rv = UpdateVideoConduit();
} else {
rv = UpdateAudioConduit();
}
if (NS_FAILED(rv)) {
return rv;
}
if (mJsepTransceiver->mRecvTrack.GetActive()) {
mReceiver->Start();
}
if (mJsepTransceiver->mSendTrack.GetActive()) {
if (!mSendTrack) {
MOZ_MTLOG(ML_WARNING,
mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " Starting transmit conduit without send track!");
}
mTransmitPipeline->Start();
}
return NS_OK;
}
void TransceiverImpl::ResetSync() {
if (mConduit) {
mConduit->SetSyncGroup("");
}
}
nsresult TransceiverImpl::SyncWithMatchingVideoConduits(
std::vector<RefPtr<TransceiverImpl>>& transceivers) {
if (mJsepTransceiver->IsStopped()) {
return NS_OK;
}
if (IsVideo()) {
MOZ_MTLOG(ML_ERROR, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " called when transceiver is not "
"video! This should never happen.");
MOZ_CRASH();
return NS_ERROR_UNEXPECTED;
}
std::set<std::string> myReceiveStreamIds;
myReceiveStreamIds.insert(mJsepTransceiver->mRecvTrack.GetStreamIds().begin(),
mJsepTransceiver->mRecvTrack.GetStreamIds().end());
for (RefPtr<TransceiverImpl>& transceiver : transceivers) {
if (!transceiver->IsValid()) {
continue;
}
if (!transceiver->IsVideo()) {
// |this| is an audio transceiver, so we skip other audio transceivers
continue;
}
// Maybe could make this more efficient by cacheing this set, but probably
// not worth it.
for (const std::string& streamId :
transceiver->mJsepTransceiver->mRecvTrack.GetStreamIds()) {
if (myReceiveStreamIds.count(streamId)) {
// Ok, we have one video, one non-video - cross the streams!
mConduit->SetSyncGroup(streamId);
transceiver->mConduit->SetSyncGroup(streamId);
MOZ_MTLOG(ML_DEBUG, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " Syncing " << mConduit.get() << " to "
<< transceiver->mConduit.get());
// The sync code in call.cc only permits sync between audio stream and
// one video stream. They take the first match, so there's no point in
// continuing here. If we want to change the default, we should sort
// video streams here and only call SetSyncGroup on the chosen stream.
break;
}
}
}
return NS_OK;
}
bool TransceiverImpl::ConduitHasPluginID(uint64_t aPluginID) {
return mConduit ? mConduit->CodecPluginID() == aPluginID : false;
}
bool TransceiverImpl::HasSendTrack(
const dom::MediaStreamTrack* aSendTrack) const {
if (!mSendTrack) {
return false;
}
if (!aSendTrack) {
return true;
}
return mSendTrack.get() == aSendTrack;
}
void TransceiverImpl::SyncWithJS(dom::RTCRtpTransceiver& aJsTransceiver,
ErrorResult& aRv) {
MOZ_MTLOG(ML_DEBUG, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " Syncing with JS transceiver");
if (!mTransmitPipeline) {
// Shutdown_m has already been called, probably due to pc.close(). Just
// nod and smile.
return;
}
// Update stopped, both ways, since either JSEP or JS can stop these
if (mJsepTransceiver->IsStopped()) {
// We don't call RTCRtpTransceiver::Stop(), because that causes another sync
aJsTransceiver.SetStopped(aRv);
Stop();
} else if (aJsTransceiver.GetStopped(aRv)) {
mJsepTransceiver->Stop();
Stop();
}
// Lots of this in here for simple getters that should never fail. Lame.
// Just propagate the exception and let JS log it.
if (aRv.Failed()) {
return;
}
// Update direction from JS only
dom::RTCRtpTransceiverDirection direction = aJsTransceiver.GetDirection(aRv);
if (aRv.Failed()) {
return;
}
switch (direction) {
case dom::RTCRtpTransceiverDirection::Sendrecv:
mJsepTransceiver->mJsDirection =
SdpDirectionAttribute::Direction::kSendrecv;
break;
case dom::RTCRtpTransceiverDirection::Sendonly:
mJsepTransceiver->mJsDirection =
SdpDirectionAttribute::Direction::kSendonly;
break;
case dom::RTCRtpTransceiverDirection::Recvonly:
mJsepTransceiver->mJsDirection =
SdpDirectionAttribute::Direction::kRecvonly;
break;
case dom::RTCRtpTransceiverDirection::Inactive:
mJsepTransceiver->mJsDirection =
SdpDirectionAttribute::Direction::kInactive;
break;
default:
MOZ_ASSERT(false);
aRv = NS_ERROR_INVALID_ARG;
return;
}
// Update send track ids in JSEP
RefPtr<dom::RTCRtpSender> sender = aJsTransceiver.GetSender(aRv);
if (aRv.Failed()) {
return;
}
nsTArray<RefPtr<DOMMediaStream>> streams;
sender->GetStreams(streams, aRv);
if (aRv.Failed()) {
return;
}
std::vector<std::string> streamIds;
for (const auto& stream : streams) {
nsString wideStreamId;
stream->GetId(wideStreamId);
std::string streamId = NS_ConvertUTF16toUTF8(wideStreamId).get();
MOZ_ASSERT(!streamId.empty());
streamIds.push_back(streamId);
}
mJsepTransceiver->mSendTrack.UpdateStreamIds(streamIds);
// Update RTCRtpParameters
// TODO: Both ways for things like ssrc, codecs, header extensions, etc
dom::RTCRtpParameters parameters;
sender->GetParameters(parameters, aRv);
if (aRv.Failed()) {
return;
}
std::vector<JsepTrack::JsConstraints> constraints;
if (parameters.mEncodings.WasPassed()) {
for (auto& encoding : parameters.mEncodings.Value()) {
JsepTrack::JsConstraints constraint;
if (encoding.mRid.WasPassed()) {
// TODO: Either turn on the RID RTP header extension in JsepSession, or
// just leave that extension on all the time?
constraint.rid = NS_ConvertUTF16toUTF8(encoding.mRid.Value()).get();
}
if (encoding.mMaxBitrate.WasPassed()) {
constraint.constraints.maxBr = encoding.mMaxBitrate.Value();
}
constraint.constraints.scaleDownBy = encoding.mScaleResolutionDownBy;
constraints.push_back(constraint);
}
}
if (mJsepTransceiver->mSendTrack.SetJsConstraints(constraints)) {
if (mTransmitPipeline->Transmitting()) {
WebrtcGmpPCHandleSetter setter(mPCHandle);
DebugOnly<nsresult> rv = UpdateConduit();
MOZ_ASSERT(NS_SUCCEEDED(rv));
}
}
// If a SRD has unset the receive bit, stop the receive pipeline so incoming
// RTP does not unmute the receive track.
if (!mJsepTransceiver->mRecvTrack.GetRemoteSetSendBit() ||
!mJsepTransceiver->mRecvTrack.GetActive()) {
mReceiver->Stop();
}
// mid from JSEP
if (mJsepTransceiver->IsAssociated()) {
aJsTransceiver.SetMid(
NS_ConvertUTF8toUTF16(mJsepTransceiver->GetMid().c_str()), aRv);
} else {
aJsTransceiver.UnsetMid(aRv);
}
if (aRv.Failed()) {
return;
}
// currentDirection from JSEP, but not if "this transceiver has never been
// represented in an offer/answer exchange"
if (mJsepTransceiver->HasLevel() && mJsepTransceiver->IsNegotiated()) {
if (IsReceiving()) {
if (IsSending()) {
aJsTransceiver.SetCurrentDirection(
dom::RTCRtpTransceiverDirection::Sendrecv, aRv);
} else {
aJsTransceiver.SetCurrentDirection(
dom::RTCRtpTransceiverDirection::Recvonly, aRv);
}
} else {
if (IsSending()) {
aJsTransceiver.SetCurrentDirection(
dom::RTCRtpTransceiverDirection::Sendonly, aRv);
} else {
aJsTransceiver.SetCurrentDirection(
dom::RTCRtpTransceiverDirection::Inactive, aRv);
}
}
if (aRv.Failed()) {
return;
}
}
// AddTrack magic from JS
if (aJsTransceiver.GetAddTrackMagic(aRv)) {
mJsepTransceiver->SetAddTrackMagic();
}
if (aRv.Failed()) {
return;
}
if (mJsepTransceiver->IsRemoved()) {
aJsTransceiver.SetShouldRemove(true, aRv);
}
}
bool TransceiverImpl::CanSendDTMF() const {
// Spec says: "If connection's RTCPeerConnectionState is not "connected"
// return false." We don't support that right now. This is supposed to be
// true once ICE is complete, and _all_ DTLS handshakes are also complete. We
// don't really have access to the state of _all_ of our DTLS states either.
// Our pipeline _does_ know whether SRTP/SRTCP is ready, which happens
// immediately after our transport finishes DTLS (unless there was an error),
// so this is pretty close.
// TODO (bug 1265827): Base this on RTCPeerConnectionState instead.
// TODO (bug 1623193): Tighten this up
if (!IsSending() || !mSendTrack) {
return false;
}
// Ok, it looks like the connection is up and sending. Did we negotiate
// telephone-event?
JsepTrackNegotiatedDetails* details =
mJsepTransceiver->mSendTrack.GetNegotiatedDetails();
if (NS_WARN_IF(!details || !details->GetEncodingCount())) {
// What?
return false;
}
for (size_t i = 0; i < details->GetEncodingCount(); ++i) {
const auto& encoding = details->GetEncoding(i);
for (const auto& codec : encoding.GetCodecs()) {
if (codec->mName == "telephone-event") {
return true;
}
}
}
return false;
}
JSObject* TransceiverImpl::WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) {
return dom::TransceiverImpl_Binding::Wrap(aCx, this, aGivenProto);
}
nsPIDOMWindowInner* TransceiverImpl::GetParentObject() const { return mWindow; }
RefPtr<MediaPipelineTransmit> TransceiverImpl::GetSendPipeline() {
return mTransmitPipeline;
}
static nsresult JsepCodecDescToAudioCodecConfig(
const JsepCodecDescription& aCodec, UniquePtr<AudioCodecConfig>* aConfig) {
MOZ_ASSERT(aCodec.mType == SdpMediaSection::kAudio);
if (aCodec.mType != SdpMediaSection::kAudio) return NS_ERROR_INVALID_ARG;
const JsepAudioCodecDescription& desc =
static_cast<const JsepAudioCodecDescription&>(aCodec);
uint16_t pt;
if (!desc.GetPtAsInt(&pt)) {
MOZ_MTLOG(ML_ERROR, "Invalid payload type: " << desc.mDefaultPt);
return NS_ERROR_INVALID_ARG;
}
aConfig->reset(new AudioCodecConfig(pt, desc.mName, desc.mClock,
desc.mForceMono ? 1 : desc.mChannels,
desc.mFECEnabled));
(*aConfig)->mMaxPlaybackRate = desc.mMaxPlaybackRate;
(*aConfig)->mDtmfEnabled = desc.mDtmfEnabled;
(*aConfig)->mDTXEnabled = desc.mDTXEnabled;
(*aConfig)->mMaxAverageBitrate = desc.mMaxAverageBitrate;
(*aConfig)->mFrameSizeMs = desc.mFrameSizeMs;
(*aConfig)->mMinFrameSizeMs = desc.mMinFrameSizeMs;
(*aConfig)->mMaxFrameSizeMs = desc.mMaxFrameSizeMs;
(*aConfig)->mCbrEnabled = desc.mCbrEnabled;
return NS_OK;
}
// TODO: Maybe move this someplace else?
/*static*/
nsresult TransceiverImpl::NegotiatedDetailsToAudioCodecConfigs(
const JsepTrackNegotiatedDetails& aDetails,
std::vector<UniquePtr<AudioCodecConfig>>* aConfigs) {
UniquePtr<AudioCodecConfig> telephoneEvent;
if (aDetails.GetEncodingCount()) {
for (const auto& codec : aDetails.GetEncoding(0).GetCodecs()) {
UniquePtr<AudioCodecConfig> config;
if (NS_FAILED(JsepCodecDescToAudioCodecConfig(*codec, &config))) {
return NS_ERROR_INVALID_ARG;
}
if (config->mName == "telephone-event") {
telephoneEvent = std::move(config);
} else {
aConfigs->push_back(std::move(config));
}
}
}
// Put telephone event at the back, because webrtc.org crashes if we don't
// If we need to do even more sorting, we should use std::sort.
if (telephoneEvent) {
aConfigs->push_back(std::move(telephoneEvent));
}
if (aConfigs->empty()) {
MOZ_MTLOG(ML_ERROR, "Can't set up a conduit with 0 codecs");
return NS_ERROR_FAILURE;
}
return NS_OK;
}
nsresult TransceiverImpl::UpdateAudioConduit() {
MOZ_ASSERT(IsValid());
RefPtr<AudioSessionConduit> conduit =
static_cast<AudioSessionConduit*>(mConduit.get());
if (mJsepTransceiver->mSendTrack.GetNegotiatedDetails() &&
mJsepTransceiver->mSendTrack.GetActive()) {
const auto& details(*mJsepTransceiver->mSendTrack.GetNegotiatedDetails());
std::vector<UniquePtr<AudioCodecConfig>> configs;
nsresult rv = TransceiverImpl::NegotiatedDetailsToAudioCodecConfigs(
details, &configs);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, mPCHandle
<< "[" << mMid << "]: " << __FUNCTION__
<< " Failed to convert JsepCodecDescriptions to "
"AudioCodecConfigs (send).");
return rv;
}
for (const auto& value : configs) {
if (value->mName == "telephone-event") {
// we have a telephone event codec, so we need to make sure
// the dynamic pt is set properly
conduit->SetDtmfPayloadType(value->mType, value->mFreq);
break;
}
}
auto error = conduit->ConfigureSendMediaCodec(configs[0].get());
if (error) {
MOZ_MTLOG(ML_ERROR, mPCHandle
<< "[" << mMid << "]: " << __FUNCTION__
<< " ConfigureSendMediaCodec failed: " << error);
return NS_ERROR_FAILURE;
}
UpdateConduitRtpExtmap(*conduit, details, LocalDirection::kSend);
}
return NS_OK;
}
static nsresult JsepCodecDescToVideoCodecConfig(
const JsepCodecDescription& aCodec, UniquePtr<VideoCodecConfig>* aConfig) {
MOZ_ASSERT(aCodec.mType == SdpMediaSection::kVideo);
if (aCodec.mType != SdpMediaSection::kVideo) {
MOZ_ASSERT(false, "JsepCodecDescription has wrong type");
return NS_ERROR_INVALID_ARG;
}
const JsepVideoCodecDescription& desc =
static_cast<const JsepVideoCodecDescription&>(aCodec);
uint16_t pt;
if (!desc.GetPtAsInt(&pt)) {
MOZ_MTLOG(ML_ERROR, "Invalid payload type: " << desc.mDefaultPt);
return NS_ERROR_INVALID_ARG;
}
UniquePtr<VideoCodecConfigH264> h264Config;
if (desc.mName == "H264") {
h264Config = MakeUnique<VideoCodecConfigH264>();
size_t spropSize = sizeof(h264Config->sprop_parameter_sets);
strncpy(h264Config->sprop_parameter_sets, desc.mSpropParameterSets.c_str(),
spropSize);
h264Config->sprop_parameter_sets[spropSize - 1] = '\0';
h264Config->packetization_mode = desc.mPacketizationMode;
h264Config->profile_level_id = desc.mProfileLevelId;
h264Config->tias_bw = 0; // TODO(bug 1403206)
}
aConfig->reset(new VideoCodecConfig(pt, desc.mName, desc.mConstraints,
h264Config.get()));
(*aConfig)->mAckFbTypes = desc.mAckFbTypes;
(*aConfig)->mNackFbTypes = desc.mNackFbTypes;
(*aConfig)->mCcmFbTypes = desc.mCcmFbTypes;
(*aConfig)->mRembFbSet = desc.RtcpFbRembIsSet();
(*aConfig)->mFECFbSet = desc.mFECEnabled;
(*aConfig)->mTransportCCFbSet = desc.RtcpFbTransportCCIsSet();
if (desc.mFECEnabled) {
uint16_t pt;
if (SdpHelper::GetPtAsInt(desc.mREDPayloadType, &pt)) {
(*aConfig)->mREDPayloadType = pt;
}
if (SdpHelper::GetPtAsInt(desc.mULPFECPayloadType, &pt)) {
(*aConfig)->mULPFECPayloadType = pt;
}
}
if (desc.mRtxEnabled) {
uint16_t pt;
if (SdpHelper::GetPtAsInt(desc.mRtxPayloadType, &pt)) {
(*aConfig)->mRTXPayloadType = pt;
}
}
return NS_OK;
}
// TODO: Maybe move this someplace else?
/*static*/
nsresult TransceiverImpl::NegotiatedDetailsToVideoCodecConfigs(
const JsepTrackNegotiatedDetails& aDetails,
std::vector<UniquePtr<VideoCodecConfig>>* aConfigs) {
if (aDetails.GetEncodingCount()) {
for (const auto& codec : aDetails.GetEncoding(0).GetCodecs()) {
UniquePtr<VideoCodecConfig> config;
if (NS_FAILED(JsepCodecDescToVideoCodecConfig(*codec, &config))) {
return NS_ERROR_INVALID_ARG;
}
config->mTias = aDetails.GetTias();
for (size_t i = 0; i < aDetails.GetEncodingCount(); ++i) {
const JsepTrackEncoding& jsepEncoding(aDetails.GetEncoding(i));
if (jsepEncoding.HasFormat(codec->mDefaultPt)) {
VideoCodecConfig::Encoding encoding;
encoding.rid = jsepEncoding.mRid;
encoding.constraints = jsepEncoding.mConstraints;
config->mEncodings.push_back(encoding);
}
}
aConfigs->push_back(std::move(config));
}
}
return NS_OK;
}
nsresult TransceiverImpl::UpdateVideoConduit() {
MOZ_ASSERT(IsValid());
RefPtr<VideoSessionConduit> conduit =
static_cast<VideoSessionConduit*>(mConduit.get());
// It is possible for SDP to signal that there is a send track, but there not
// actually be a send track, according to the specification; all that needs to
// happen is for the transceiver to be configured to send...
if (mJsepTransceiver->mSendTrack.GetNegotiatedDetails() &&
mJsepTransceiver->mSendTrack.GetActive() && mSendTrack) {
const auto& details(*mJsepTransceiver->mSendTrack.GetNegotiatedDetails());
UpdateConduitRtpExtmap(*conduit, details, LocalDirection::kSend);
nsresult rv = ConfigureVideoCodecMode(*conduit);
if (NS_FAILED(rv)) {
return rv;
}
std::vector<UniquePtr<VideoCodecConfig>> configs;
rv = TransceiverImpl::NegotiatedDetailsToVideoCodecConfigs(details,
&configs);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, mPCHandle
<< "[" << mMid << "]: " << __FUNCTION__
<< " Failed to convert JsepCodecDescriptions to "
"VideoCodecConfigs (send).");
return rv;
}
if (configs.empty()) {
MOZ_MTLOG(ML_INFO, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " No codecs were negotiated (send).");
return NS_OK;
}
auto error = conduit->ConfigureSendMediaCodec(configs[0].get(),
details.GetRtpRtcpConfig());
if (error) {
MOZ_MTLOG(ML_ERROR, mPCHandle
<< "[" << mMid << "]: " << __FUNCTION__
<< " ConfigureSendMediaCodec failed: " << error);
return NS_ERROR_FAILURE;
}
}
return NS_OK;
}
nsresult TransceiverImpl::ConfigureVideoCodecMode(
VideoSessionConduit& aConduit) {
RefPtr<mozilla::dom::VideoStreamTrack> videotrack =
mSendTrack->AsVideoStreamTrack();
if (!videotrack) {
MOZ_MTLOG(
ML_ERROR, mPCHandle
<< "[" << mMid << "]: " << __FUNCTION__
<< " mSendTrack is not video! This should never happen!");
MOZ_CRASH();
return NS_ERROR_FAILURE;
}
dom::MediaSourceEnum source = videotrack->GetSource().GetMediaSource();
webrtc::VideoCodecMode mode = webrtc::kRealtimeVideo;
switch (source) {
case dom::MediaSourceEnum::Browser:
case dom::MediaSourceEnum::Screen:
case dom::MediaSourceEnum::Window:
mode = webrtc::kScreensharing;
break;
case dom::MediaSourceEnum::Camera:
default:
mode = webrtc::kRealtimeVideo;
break;
}
auto error = aConduit.ConfigureCodecMode(mode);
if (error) {
MOZ_MTLOG(ML_ERROR, mPCHandle << "[" << mMid << "]: " << __FUNCTION__
<< " ConfigureCodecMode failed: " << error);
return NS_ERROR_FAILURE;
}
return NS_OK;
}
void TransceiverImpl::UpdateConduitRtpExtmap(
MediaSessionConduit& aConduit, const JsepTrackNegotiatedDetails& aDetails,
const LocalDirection aDirection) {
std::vector<webrtc::RtpExtension> extmaps;
// @@NG read extmap from track
aDetails.ForEachRTPHeaderExtension(
[&extmaps](const SdpExtmapAttributeList::Extmap& extmap) {
extmaps.emplace_back(extmap.extensionname, extmap.entry);
});
if (!extmaps.empty()) {
aConduit.SetLocalRTPExtensions(aDirection, extmaps);
}
}
void TransceiverImpl::Stop() {
mTransmitPipeline->Shutdown_m();
mReceiver->Shutdown();
// Make sure that stats queries stop working on this transceiver.
UpdateSendTrack(nullptr);
if (mConduit) {
mConduit->DeleteStreams();
}
mConduit = nullptr;
if (mDtmf) {
mDtmf->StopPlayout();
}
}
bool TransceiverImpl::IsVideo() const {
return mJsepTransceiver->GetMediaType() == SdpMediaSection::MediaType::kVideo;
}
} // namespace mozilla
|