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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
|
/* 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 "AudioConduit.h"
#include "common/browser_logging/CSFLog.h"
#include "MediaConduitControl.h"
#include "transport/SrtpFlow.h" // For SRTP_MAX_EXPANSION
#include "WebrtcCallWrapper.h"
#include "libwebrtcglue/FrameTransformer.h"
#include <vector>
#include "CodecConfig.h"
#include "mozilla/StateMirroring.h"
#include <vector>
#include "mozilla/MozPromise.h"
#include "mozilla/RefPtr.h"
#include "mozilla/RWLock.h"
// libwebrtc includes
#include "api/audio_codecs/builtin_audio_encoder_factory.h"
#include "audio/audio_receive_stream.h"
#include "media/base/media_constants.h"
#include "rtc_base/ref_counted_object.h"
#include "api/audio/audio_frame.h"
#include "api/audio/audio_mixer.h"
#include "api/audio_codecs/audio_format.h"
#include "api/call/transport.h"
#include "api/media_types.h"
#include "api/rtp_headers.h"
#include "api/rtp_parameters.h"
#include "api/transport/rtp/rtp_source.h"
#include <utility>
#include "call/audio_receive_stream.h"
#include "call/audio_send_stream.h"
#include "call/call_basic_stats.h"
#include "domstubs.h"
#include "jsapi/RTCStatsReport.h"
#include <limits>
#include "MainThreadUtils.h"
#include <map>
#include "MediaConduitErrors.h"
#include "MediaConduitInterface.h"
#include <memory>
#include "modules/rtp_rtcp/source/rtp_packet_received.h"
#include "mozilla/Assertions.h"
#include "mozilla/Atomics.h"
#include "mozilla/Maybe.h"
#include "mozilla/StateWatching.h"
#include "nsCOMPtr.h"
#include "nsError.h"
#include "nsISerialEventTarget.h"
#include "nsThreadUtils.h"
#include "rtc_base/copy_on_write_buffer.h"
#include "rtc_base/network/sent_packet.h"
#include <stdint.h>
#include <string>
#include "transport/mediapacket.h"
// for ntohs
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#elif defined XP_WIN
# include <winsock2.h>
#endif
#ifdef MOZ_WIDGET_ANDROID
# include "AndroidBridge.h"
#endif
namespace mozilla {
namespace {
static const char* acLogTag = "WebrtcAudioSessionConduit";
#ifdef LOGTAG
# undef LOGTAG
#endif
#define LOGTAG acLogTag
using namespace cricket;
using LocalDirection = MediaSessionConduitLocalDirection;
const char kCodecParamCbr[] = "cbr";
} // namespace
/**
* Factory Method for AudioConduit
*/
RefPtr<AudioSessionConduit> AudioSessionConduit::Create(
RefPtr<WebrtcCallWrapper> aCall,
nsCOMPtr<nsISerialEventTarget> aStsThread) {
CSFLogDebug(LOGTAG, "%s ", __FUNCTION__);
MOZ_ASSERT(NS_IsMainThread());
return MakeRefPtr<WebrtcAudioConduit>(std::move(aCall),
std::move(aStsThread));
}
#define INIT_MIRROR(name, val) \
name(aCallThread, val, "WebrtcAudioConduit::Control::" #name " (Mirror)")
WebrtcAudioConduit::Control::Control(const RefPtr<AbstractThread>& aCallThread)
: INIT_MIRROR(mReceiving, false),
INIT_MIRROR(mTransmitting, false),
INIT_MIRROR(mLocalSsrcs, Ssrcs()),
INIT_MIRROR(mLocalCname, std::string()),
INIT_MIRROR(mMid, std::string()),
INIT_MIRROR(mRemoteSsrc, 0),
INIT_MIRROR(mSyncGroup, std::string()),
INIT_MIRROR(mLocalRecvRtpExtensions, RtpExtList()),
INIT_MIRROR(mLocalSendRtpExtensions, RtpExtList()),
INIT_MIRROR(mSendCodec, Nothing()),
INIT_MIRROR(mRecvCodecs, std::vector<AudioCodecConfig>()),
INIT_MIRROR(mFrameTransformerProxySend, nullptr),
INIT_MIRROR(mFrameTransformerProxyRecv, nullptr) {}
#undef INIT_MIRROR
RefPtr<GenericPromise> WebrtcAudioConduit::Shutdown() {
MOZ_ASSERT(NS_IsMainThread());
mControl.mOnDtmfEventListener.DisconnectIfExists();
return InvokeAsync(
mCallThread, "WebrtcAudioConduit::Shutdown (main thread)",
[this, self = RefPtr<WebrtcAudioConduit>(this)] {
mControl.mReceiving.DisconnectIfConnected();
mControl.mTransmitting.DisconnectIfConnected();
mControl.mLocalSsrcs.DisconnectIfConnected();
mControl.mLocalCname.DisconnectIfConnected();
mControl.mMid.DisconnectIfConnected();
mControl.mRemoteSsrc.DisconnectIfConnected();
mControl.mSyncGroup.DisconnectIfConnected();
mControl.mLocalRecvRtpExtensions.DisconnectIfConnected();
mControl.mLocalSendRtpExtensions.DisconnectIfConnected();
mControl.mSendCodec.DisconnectIfConnected();
mControl.mRecvCodecs.DisconnectIfConnected();
mControl.mFrameTransformerProxySend.DisconnectIfConnected();
mControl.mFrameTransformerProxyRecv.DisconnectIfConnected();
mWatchManager.Shutdown();
{
AutoWriteLock lock(mLock);
DeleteSendStream();
DeleteRecvStream();
}
return GenericPromise::CreateAndResolve(
true, "WebrtcAudioConduit::Shutdown (call thread)");
});
}
WebrtcAudioConduit::WebrtcAudioConduit(
RefPtr<WebrtcCallWrapper> aCall, nsCOMPtr<nsISerialEventTarget> aStsThread)
: mCall(std::move(aCall)),
mSendTransport(this),
mRecvTransport(this),
mRecvStream(nullptr),
mSendStreamConfig(&mSendTransport),
mSendStream(nullptr),
mSendStreamRunning(false),
mRecvStreamRunning(false),
mDtmfEnabled(false),
mLock("WebrtcAudioConduit::mLock"),
mCallThread(mCall->mCallThread),
mStsThread(std::move(aStsThread)),
mControl(mCall->mCallThread),
mWatchManager(this, mCall->mCallThread) {
mRecvStreamConfig.rtcp_send_transport = &mRecvTransport;
mRecvStreamConfig.rtp.rtcp_event_observer = this;
}
/**
* Destruction defines for our super-classes
*/
WebrtcAudioConduit::~WebrtcAudioConduit() {
CSFLogDebug(LOGTAG, "%s ", __FUNCTION__);
MOZ_ASSERT(!mSendStream && !mRecvStream,
"Call DeleteStreams prior to ~WebrtcAudioConduit.");
}
#define CONNECT(aCanonical, aMirror) \
do { \
/* Ensure the watchmanager is wired up before the mirror receives its \
* initial mirrored value. */ \
mCall->mCallThread->DispatchStateChange( \
NS_NewRunnableFunction(__func__, [this, self = RefPtr(this)] { \
mWatchManager.Watch(aMirror, \
&WebrtcAudioConduit::OnControlConfigChange); \
})); \
(aCanonical).ConnectMirror(&(aMirror)); \
} while (0)
void WebrtcAudioConduit::InitControl(AudioConduitControlInterface* aControl) {
MOZ_ASSERT(NS_IsMainThread());
CONNECT(aControl->CanonicalReceiving(), mControl.mReceiving);
CONNECT(aControl->CanonicalTransmitting(), mControl.mTransmitting);
CONNECT(aControl->CanonicalLocalSsrcs(), mControl.mLocalSsrcs);
CONNECT(aControl->CanonicalLocalCname(), mControl.mLocalCname);
CONNECT(aControl->CanonicalMid(), mControl.mMid);
CONNECT(aControl->CanonicalRemoteSsrc(), mControl.mRemoteSsrc);
CONNECT(aControl->CanonicalSyncGroup(), mControl.mSyncGroup);
CONNECT(aControl->CanonicalLocalRecvRtpExtensions(),
mControl.mLocalRecvRtpExtensions);
CONNECT(aControl->CanonicalLocalSendRtpExtensions(),
mControl.mLocalSendRtpExtensions);
CONNECT(aControl->CanonicalAudioSendCodec(), mControl.mSendCodec);
CONNECT(aControl->CanonicalAudioRecvCodecs(), mControl.mRecvCodecs);
CONNECT(aControl->CanonicalFrameTransformerProxySend(),
mControl.mFrameTransformerProxySend);
CONNECT(aControl->CanonicalFrameTransformerProxyRecv(),
mControl.mFrameTransformerProxyRecv);
mControl.mOnDtmfEventListener = aControl->OnDtmfEvent().Connect(
mCall->mCallThread, this, &WebrtcAudioConduit::OnDtmfEvent);
}
#undef CONNECT
void WebrtcAudioConduit::OnDtmfEvent(const DtmfEvent& aEvent) {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mSendStream);
MOZ_ASSERT(mDtmfEnabled);
mSendStream->SendTelephoneEvent(aEvent.mPayloadType, aEvent.mPayloadFrequency,
aEvent.mEventCode, aEvent.mLengthMs);
}
void WebrtcAudioConduit::OnControlConfigChange() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
bool recvStreamReconfigureNeeded = false;
bool sendStreamReconfigureNeeded = false;
bool recvStreamRecreationNeeded = false;
bool sendStreamRecreationNeeded = false;
if (!mControl.mLocalSsrcs.Ref().empty()) {
if (mControl.mLocalSsrcs.Ref()[0] != mSendStreamConfig.rtp.ssrc) {
sendStreamRecreationNeeded = true;
// For now...
recvStreamRecreationNeeded = true;
}
mRecvStreamConfig.rtp.local_ssrc = mControl.mLocalSsrcs.Ref()[0];
mSendStreamConfig.rtp.ssrc = mControl.mLocalSsrcs.Ref()[0];
// In the future we can do this instead of recreating the recv stream:
// if (mRecvStream) {
// mCall->Call()->OnLocalSsrcUpdated(mRecvStream,
// mControl.mLocalSsrcs.Ref()[0]);
// }
}
if (mControl.mLocalCname.Ref() != mSendStreamConfig.rtp.c_name) {
mSendStreamConfig.rtp.c_name = mControl.mLocalCname.Ref();
sendStreamReconfigureNeeded = true;
}
if (mControl.mMid.Ref() != mSendStreamConfig.rtp.mid) {
mSendStreamConfig.rtp.mid = mControl.mMid.Ref();
sendStreamReconfigureNeeded = true;
}
if (mControl.mRemoteSsrc.Ref() != mControl.mConfiguredRemoteSsrc) {
mRecvStreamConfig.rtp.remote_ssrc = mControl.mConfiguredRemoteSsrc =
mControl.mRemoteSsrc.Ref();
recvStreamRecreationNeeded = true;
}
if (mControl.mSyncGroup.Ref() != mRecvStreamConfig.sync_group) {
mRecvStreamConfig.sync_group = mControl.mSyncGroup.Ref();
// For now...
recvStreamRecreationNeeded = true;
// In the future we can do this instead of recreating the recv stream:
// if (mRecvStream) {
// mCall->Call()->OnUpdateSyncGroup(mRecvStream,
// mRecvStreamConfig.sync_group);
// }
}
if (auto filteredExtensions = FilterExtensions(
LocalDirection::kSend, mControl.mLocalSendRtpExtensions);
filteredExtensions != mSendStreamConfig.rtp.extensions) {
// At the very least, we need a reconfigure. Recreation needed if the
// extmap for any extension has changed, but not for adding/removing
// extensions.
sendStreamReconfigureNeeded = true;
for (const auto& newExt : filteredExtensions) {
if (sendStreamRecreationNeeded) {
break;
}
for (const auto& oldExt : mSendStreamConfig.rtp.extensions) {
if (newExt.uri == oldExt.uri) {
if (newExt.id != oldExt.id) {
sendStreamRecreationNeeded = true;
}
// We're done handling newExt, one way or another
break;
}
}
}
mSendStreamConfig.rtp.extensions = std::move(filteredExtensions);
}
mControl.mSendCodec.Ref().apply([&](const auto& aConfig) {
if (mControl.mConfiguredSendCodec != mControl.mSendCodec.Ref()) {
mControl.mConfiguredSendCodec = mControl.mSendCodec;
if (ValidateCodecConfig(aConfig, true) == kMediaConduitNoError) {
mSendStreamConfig.encoder_factory =
webrtc::CreateBuiltinAudioEncoderFactory();
webrtc::AudioSendStream::Config::SendCodecSpec spec(
aConfig.mType, CodecConfigToLibwebrtcFormat(aConfig));
mSendStreamConfig.send_codec_spec = spec;
mDtmfEnabled = aConfig.mDtmfEnabled;
sendStreamReconfigureNeeded = true;
}
}
});
if (mControl.mConfiguredRecvCodecs != mControl.mRecvCodecs.Ref()) {
mControl.mConfiguredRecvCodecs = mControl.mRecvCodecs;
mRecvStreamConfig.decoder_factory = mCall->mAudioDecoderFactory;
mRecvStreamConfig.decoder_map.clear();
for (const auto& codec : mControl.mRecvCodecs.Ref()) {
if (ValidateCodecConfig(codec, false) != kMediaConduitNoError) {
continue;
}
mRecvStreamConfig.decoder_map.emplace(
codec.mType, CodecConfigToLibwebrtcFormat(codec));
}
recvStreamReconfigureNeeded = true;
}
if (mControl.mConfiguredFrameTransformerProxySend.get() !=
mControl.mFrameTransformerProxySend.Ref().get()) {
mControl.mConfiguredFrameTransformerProxySend =
mControl.mFrameTransformerProxySend.Ref();
if (!mSendStreamConfig.frame_transformer) {
mSendStreamConfig.frame_transformer =
new rtc::RefCountedObject<FrameTransformer>(false);
sendStreamRecreationNeeded = true;
}
static_cast<FrameTransformer*>(mSendStreamConfig.frame_transformer.get())
->SetProxy(mControl.mConfiguredFrameTransformerProxySend);
}
if (mControl.mConfiguredFrameTransformerProxyRecv.get() !=
mControl.mFrameTransformerProxyRecv.Ref().get()) {
mControl.mConfiguredFrameTransformerProxyRecv =
mControl.mFrameTransformerProxyRecv.Ref();
if (!mRecvStreamConfig.frame_transformer) {
mRecvStreamConfig.frame_transformer =
new rtc::RefCountedObject<FrameTransformer>(false);
recvStreamRecreationNeeded = true;
}
static_cast<FrameTransformer*>(mRecvStreamConfig.frame_transformer.get())
->SetProxy(mControl.mConfiguredFrameTransformerProxyRecv);
}
if (!recvStreamReconfigureNeeded && !sendStreamReconfigureNeeded &&
!recvStreamRecreationNeeded && !sendStreamRecreationNeeded &&
mControl.mReceiving == mRecvStreamRunning &&
mControl.mTransmitting == mSendStreamRunning) {
// No changes applied -- no need to lock.
return;
}
if (recvStreamRecreationNeeded) {
recvStreamReconfigureNeeded = false;
}
if (sendStreamRecreationNeeded) {
sendStreamReconfigureNeeded = false;
}
{
AutoWriteLock lock(mLock);
// Recreate/Stop/Start streams as needed.
if (recvStreamRecreationNeeded) {
DeleteRecvStream();
}
if (mControl.mReceiving) {
CreateRecvStream();
}
if (sendStreamRecreationNeeded) {
DeleteSendStream();
}
if (mControl.mTransmitting) {
CreateSendStream();
}
}
// We make sure to not hold the lock while stopping/starting/reconfiguring
// streams, so as to not cause deadlocks. These methods can cause our platform
// codecs to dispatch sync runnables to main, and main may grab the lock.
if (mRecvStream && recvStreamReconfigureNeeded) {
MOZ_ASSERT(!recvStreamRecreationNeeded);
mRecvStream->SetDecoderMap(mRecvStreamConfig.decoder_map);
}
if (mSendStream && sendStreamReconfigureNeeded) {
MOZ_ASSERT(!sendStreamRecreationNeeded);
// TODO: Pass a callback here, so we can react to RTCErrors thrown by
// libwebrtc.
mSendStream->Reconfigure(mSendStreamConfig, nullptr);
}
if (!mControl.mReceiving) {
StopReceiving();
}
if (!mControl.mTransmitting) {
StopTransmitting();
}
if (mControl.mReceiving) {
StartReceiving();
}
if (mControl.mTransmitting) {
StartTransmitting();
}
}
std::vector<uint32_t> WebrtcAudioConduit::GetLocalSSRCs() const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
return std::vector<uint32_t>(1, mRecvStreamConfig.rtp.local_ssrc);
}
bool WebrtcAudioConduit::OverrideRemoteSSRC(uint32_t aSsrc) {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
if (mRecvStreamConfig.rtp.remote_ssrc == aSsrc) {
return true;
}
mRecvStreamConfig.rtp.remote_ssrc = aSsrc;
const bool wasReceiving = mRecvStreamRunning;
const bool hadRecvStream = mRecvStream;
StopReceiving();
if (hadRecvStream) {
AutoWriteLock lock(mLock);
DeleteRecvStream();
CreateRecvStream();
}
if (wasReceiving) {
StartReceiving();
}
return true;
}
Maybe<Ssrc> WebrtcAudioConduit::GetRemoteSSRC() const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
// libwebrtc uses 0 to mean a lack of SSRC. That is not to spec.
return mRecvStreamConfig.rtp.remote_ssrc == 0
? Nothing()
: Some(mRecvStreamConfig.rtp.remote_ssrc);
}
Maybe<webrtc::AudioReceiveStreamInterface::Stats>
WebrtcAudioConduit::GetReceiverStats() const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
if (!mRecvStream) {
return Nothing();
}
return Some(mRecvStream->GetStats());
}
Maybe<webrtc::AudioSendStream::Stats> WebrtcAudioConduit::GetSenderStats()
const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
if (!mSendStream) {
return Nothing();
}
return Some(mSendStream->GetStats());
}
Maybe<webrtc::CallBasicStats> WebrtcAudioConduit::GetCallStats() const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
if (!mCall->Call()) {
return Nothing();
}
return Some(mCall->Call()->GetStats());
}
void WebrtcAudioConduit::OnRtcpBye() { mRtcpByeEvent.Notify(); }
void WebrtcAudioConduit::OnRtcpTimeout() { mRtcpTimeoutEvent.Notify(); }
void WebrtcAudioConduit::SetTransportActive(bool aActive) {
MOZ_ASSERT(mStsThread->IsOnCurrentThread());
if (mTransportActive == aActive) {
return;
}
// If false, This stops us from sending
mTransportActive = aActive;
// We queue this because there might be notifications to these listeners
// pending, and we don't want to drop them by letting this jump ahead of
// those notifications. We move the listeners into the lambda in case the
// transport comes back up before we disconnect them. (The Connect calls
// happen in MediaPipeline)
// We retain a strong reference to ourself, because the listeners are holding
// a non-refcounted reference to us, and moving them into the lambda could
// conceivably allow them to outlive us.
if (!aActive) {
MOZ_ALWAYS_SUCCEEDS(mCallThread->Dispatch(NS_NewRunnableFunction(
__func__,
[self = RefPtr<WebrtcAudioConduit>(this),
recvRtpListener = std::move(mReceiverRtpEventListener)]() mutable {
recvRtpListener.DisconnectIfExists();
})));
}
}
// AudioSessionConduit Implementation
MediaConduitErrorCode WebrtcAudioConduit::SendAudioFrame(
std::unique_ptr<webrtc::AudioFrame> frame) {
CSFLogDebug(LOGTAG, "%s ", __FUNCTION__);
// Following checks need to be performed
// 1. Non null audio buffer pointer, and
// 2. Valid sample rate, and
// 3. Appropriate Sample Length for 10 ms audio-frame. This represents the
// block size used upstream for processing.
// Ex: for 16000 sample rate , valid block-length is 160.
// Similarly for 32000 sample rate, valid block length is 320.
if (!frame->data() ||
(IsSamplingFreqSupported(frame->sample_rate_hz()) == false) ||
((frame->samples_per_channel() % (frame->sample_rate_hz() / 100) != 0))) {
CSFLogError(LOGTAG, "%s Invalid Parameters ", __FUNCTION__);
MOZ_ASSERT(PR_FALSE);
return kMediaConduitMalformedArgument;
}
// This is the AudioProxyThread, blocking it for a bit is fine.
AutoReadLock lock(mLock);
if (!mSendStreamRunning) {
CSFLogError(LOGTAG, "%s Engine not transmitting ", __FUNCTION__);
return kMediaConduitSessionNotInited;
}
mSendStream->SendAudioData(std::move(frame));
return kMediaConduitNoError;
}
MediaConduitErrorCode WebrtcAudioConduit::GetAudioFrame(
int32_t samplingFreqHz, webrtc::AudioFrame* frame) {
CSFLogDebug(LOGTAG, "%s ", __FUNCTION__);
// validate params
if (!frame) {
CSFLogError(LOGTAG, "%s Null Audio Buffer Pointer", __FUNCTION__);
MOZ_ASSERT(PR_FALSE);
return kMediaConduitMalformedArgument;
}
// Validate sample length
if (GetNum10msSamplesForFrequency(samplingFreqHz) == 0) {
CSFLogError(LOGTAG, "%s Invalid Sampling Frequency ", __FUNCTION__);
MOZ_ASSERT(PR_FALSE);
return kMediaConduitMalformedArgument;
}
// If the lock is taken, skip this chunk to avoid blocking the audio thread.
AutoTryReadLock tryLock(mLock);
if (!tryLock) {
CSFLogError(LOGTAG, "%s Conduit going through negotiation ", __FUNCTION__);
return kMediaConduitPlayoutError;
}
// Conduit should have reception enabled before we ask for decoded
// samples
if (!mRecvStreamRunning) {
CSFLogError(LOGTAG, "%s Engine not Receiving ", __FUNCTION__);
return kMediaConduitSessionNotInited;
}
// Unfortunate to have to cast to an internal class, but that looks like the
// only way short of interfacing with a layer above (which mixes all streams,
// which we don't want) or a layer below (which we try to avoid because it is
// less stable).
auto info = static_cast<webrtc::AudioReceiveStreamImpl*>(mRecvStream)
->GetAudioFrameWithInfo(samplingFreqHz, frame);
if (info == webrtc::AudioMixer::Source::AudioFrameInfo::kError) {
CSFLogError(LOGTAG, "%s Getting audio frame failed", __FUNCTION__);
return kMediaConduitPlayoutError;
}
CSFLogDebug(LOGTAG, "%s Got %zu channels of %zu samples", __FUNCTION__,
frame->num_channels(), frame->samples_per_channel());
return kMediaConduitNoError;
}
// Transport Layer Callbacks
void WebrtcAudioConduit::OnRtpReceived(webrtc::RtpPacketReceived&& aPacket,
webrtc::RTPHeader&& aHeader) {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
if (mAllowSsrcChange && mRecvStreamConfig.rtp.remote_ssrc != aHeader.ssrc) {
CSFLogDebug(LOGTAG, "%s: switching from SSRC %u to %u", __FUNCTION__,
mRecvStreamConfig.rtp.remote_ssrc, aHeader.ssrc);
OverrideRemoteSSRC(aHeader.ssrc);
}
CSFLogVerbose(LOGTAG, "%s: seq# %u, Len %zu, SSRC %u (0x%x) ", __FUNCTION__,
aPacket.SequenceNumber(), aPacket.size(), aPacket.Ssrc(),
aPacket.Ssrc());
// Libwebrtc commit cde4b67d9d now expect calls to
// SourceTracker::GetSources() to happen on the call thread. We'll
// grab the value now while on the call thread, and dispatch to main
// to store the cached value if we have new source information.
// See Bug 1845621.
std::vector<webrtc::RtpSource> sources;
if (mRecvStream) {
sources = mRecvStream->GetSources();
}
bool needsCacheUpdate = false;
{
AutoReadLock lock(mLock);
needsCacheUpdate = sources != mRtpSources;
}
// only dispatch to main if we have new data
if (needsCacheUpdate) {
GetMainThreadSerialEventTarget()->Dispatch(NS_NewRunnableFunction(
__func__, [this, rtpSources = std::move(sources),
self = RefPtr<WebrtcAudioConduit>(this)]() {
AutoWriteLock lock(mLock);
mRtpSources = rtpSources;
}));
}
mRtpPacketEvent.Notify();
if (mCall->Call()) {
mCall->Call()->Receiver()->DeliverRtpPacket(
webrtc::MediaType::AUDIO, std::move(aPacket),
[self = RefPtr<WebrtcAudioConduit>(this)](
const webrtc::RtpPacketReceived& packet) {
CSFLogVerbose(
LOGTAG,
"AudioConduit %p: failed demuxing packet, ssrc: %u seq: %u",
self.get(), packet.Ssrc(), packet.SequenceNumber());
return false;
});
}
}
Maybe<uint16_t> WebrtcAudioConduit::RtpSendBaseSeqFor(uint32_t aSsrc) const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
auto it = mRtpSendBaseSeqs.find(aSsrc);
if (it == mRtpSendBaseSeqs.end()) {
return Nothing();
}
return Some(it->second);
}
const dom::RTCStatsTimestampMaker& WebrtcAudioConduit::GetTimestampMaker()
const {
return mCall->GetTimestampMaker();
}
void WebrtcAudioConduit::StopTransmitting() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(!mLock.LockedForWritingByCurrentThread());
if (!mSendStreamRunning) {
return;
}
if (mSendStream) {
CSFLogDebug(LOGTAG, "%s Stopping send stream", __FUNCTION__);
mSendStream->Stop();
}
mSendStreamRunning = false;
}
void WebrtcAudioConduit::StartTransmitting() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mSendStream);
MOZ_ASSERT(!mLock.LockedForWritingByCurrentThread());
if (mSendStreamRunning) {
return;
}
CSFLogDebug(LOGTAG, "%s Starting send stream", __FUNCTION__);
mCall->Call()->SignalChannelNetworkState(webrtc::MediaType::AUDIO,
webrtc::kNetworkUp);
mSendStream->Start();
mSendStreamRunning = true;
}
void WebrtcAudioConduit::StopReceiving() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(!mLock.LockedForWritingByCurrentThread());
if (!mRecvStreamRunning) {
return;
}
if (mRecvStream) {
CSFLogDebug(LOGTAG, "%s Stopping recv stream", __FUNCTION__);
mRecvStream->Stop();
}
mRecvStreamRunning = false;
}
void WebrtcAudioConduit::StartReceiving() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mRecvStream);
MOZ_ASSERT(!mLock.LockedForWritingByCurrentThread());
if (mRecvStreamRunning) {
return;
}
CSFLogDebug(LOGTAG, "%s Starting receive stream (SSRC %u (0x%x))",
__FUNCTION__, mRecvStreamConfig.rtp.remote_ssrc,
mRecvStreamConfig.rtp.remote_ssrc);
mCall->Call()->SignalChannelNetworkState(webrtc::MediaType::AUDIO,
webrtc::kNetworkUp);
mRecvStream->Start();
mRecvStreamRunning = true;
}
bool WebrtcAudioConduit::SendRtp(const uint8_t* aData, size_t aLength,
const webrtc::PacketOptions& aOptions) {
MOZ_ASSERT(aLength >= 12);
const uint16_t seqno = ntohs(*((uint16_t*)&aData[2]));
const uint32_t ssrc = ntohl(*((uint32_t*)&aData[8]));
CSFLogVerbose(
LOGTAG,
"AudioConduit %p: Sending RTP Packet seq# %u, len %zu, SSRC %u (0x%x)",
this, seqno, aLength, ssrc, ssrc);
if (!mTransportActive) {
CSFLogError(LOGTAG, "AudioConduit %p: RTP Packet Send Failed ", this);
return false;
}
MediaPacket packet;
packet.Copy(aData, aLength, aLength + SRTP_MAX_EXPANSION);
packet.SetType(MediaPacket::RTP);
mSenderRtpSendEvent.Notify(std::move(packet));
// Parse the sequence number of the first rtp packet as base_seq.
const auto inserted = mRtpSendBaseSeqs_n.insert({ssrc, seqno}).second;
if (inserted || aOptions.packet_id >= 0) {
int64_t now_ms = PR_Now() / 1000;
MOZ_ALWAYS_SUCCEEDS(mCallThread->Dispatch(NS_NewRunnableFunction(
__func__, [this, self = RefPtr<WebrtcAudioConduit>(this),
packet_id = aOptions.packet_id, now_ms, ssrc, seqno] {
mRtpSendBaseSeqs.insert({ssrc, seqno});
if (packet_id >= 0) {
if (mCall->Call()) {
// TODO: This notification should ideally happen after the
// transport layer has sent the packet on the wire.
mCall->Call()->OnSentPacket({packet_id, now_ms});
}
}
})));
}
return true;
}
bool WebrtcAudioConduit::SendSenderRtcp(const uint8_t* aData, size_t aLength) {
CSFLogVerbose(
LOGTAG,
"AudioConduit %p: Sending RTCP SR Packet, len %zu, SSRC %u (0x%x)", this,
aLength, (uint32_t)ntohl(*((uint32_t*)&aData[4])),
(uint32_t)ntohl(*((uint32_t*)&aData[4])));
if (!mTransportActive) {
CSFLogError(LOGTAG, "%s RTCP SR Packet Send Failed ", __FUNCTION__);
return false;
}
MediaPacket packet;
packet.Copy(aData, aLength, aLength + SRTP_MAX_EXPANSION);
packet.SetType(MediaPacket::RTCP);
mSenderRtcpSendEvent.Notify(std::move(packet));
return true;
}
bool WebrtcAudioConduit::SendReceiverRtcp(const uint8_t* aData,
size_t aLength) {
CSFLogVerbose(
LOGTAG,
"AudioConduit %p: Sending RTCP RR Packet, len %zu, SSRC %u (0x%x)", this,
aLength, (uint32_t)ntohl(*((uint32_t*)&aData[4])),
(uint32_t)ntohl(*((uint32_t*)&aData[4])));
if (!mTransportActive) {
CSFLogError(LOGTAG, "AudioConduit %p: RTCP RR Packet Send Failed", this);
return false;
}
MediaPacket packet;
packet.Copy(aData, aLength, aLength + SRTP_MAX_EXPANSION);
packet.SetType(MediaPacket::RTCP);
mReceiverRtcpSendEvent.Notify(std::move(packet));
return true;
}
/**
* Supported Sampling Frequencies.
*/
bool WebrtcAudioConduit::IsSamplingFreqSupported(int freq) const {
return GetNum10msSamplesForFrequency(freq) != 0;
}
std::vector<webrtc::RtpSource> WebrtcAudioConduit::GetUpstreamRtpSources()
const {
MOZ_ASSERT(NS_IsMainThread());
return mRtpSources;
}
/* Return block-length of 10 ms audio frame in number of samples */
unsigned int WebrtcAudioConduit::GetNum10msSamplesForFrequency(
int samplingFreqHz) const {
switch (samplingFreqHz) {
case 16000:
return 160; // 160 samples
case 32000:
return 320; // 320 samples
case 44100:
return 441; // 441 samples
case 48000:
return 480; // 480 samples
default:
return 0; // invalid or unsupported
}
}
/**
* Perform validation on the codecConfig to be applied.
* Verifies if the codec is already applied.
*/
MediaConduitErrorCode WebrtcAudioConduit::ValidateCodecConfig(
const AudioCodecConfig& codecInfo, bool send) {
if (codecInfo.mName.empty()) {
CSFLogError(LOGTAG, "%s Empty Payload Name ", __FUNCTION__);
return kMediaConduitMalformedArgument;
}
// Only mono or stereo channels supported
if ((codecInfo.mChannels != 1) && (codecInfo.mChannels != 2)) {
CSFLogError(LOGTAG, "%s Channel Unsupported ", __FUNCTION__);
return kMediaConduitMalformedArgument;
}
return kMediaConduitNoError;
}
RtpExtList WebrtcAudioConduit::FilterExtensions(LocalDirection aDirection,
const RtpExtList& aExtensions) {
const bool isSend = aDirection == LocalDirection::kSend;
RtpExtList filteredExtensions;
for (const auto& extension : aExtensions) {
// ssrc-audio-level RTP header extension
if (extension.uri == webrtc::RtpExtension::kAudioLevelUri) {
filteredExtensions.push_back(
webrtc::RtpExtension(extension.uri, extension.id));
}
// csrc-audio-level RTP header extension
if (extension.uri == webrtc::RtpExtension::kCsrcAudioLevelsUri) {
if (isSend) {
continue;
}
filteredExtensions.push_back(
webrtc::RtpExtension(extension.uri, extension.id));
}
// MID RTP header extension
if (extension.uri == webrtc::RtpExtension::kMidUri) {
if (!isSend) {
// TODO: recv mid support, see also bug 1727211
continue;
}
filteredExtensions.push_back(
webrtc::RtpExtension(extension.uri, extension.id));
}
}
return filteredExtensions;
}
webrtc::SdpAudioFormat WebrtcAudioConduit::CodecConfigToLibwebrtcFormat(
const AudioCodecConfig& aConfig) {
webrtc::SdpAudioFormat::Parameters parameters;
if (aConfig.mName == kOpusCodecName) {
if (aConfig.mChannels == 2) {
parameters[kCodecParamStereo] = kParamValueTrue;
}
if (aConfig.mFECEnabled) {
parameters[kCodecParamUseInbandFec] = kParamValueTrue;
}
if (aConfig.mDTXEnabled) {
parameters[kCodecParamUseDtx] = kParamValueTrue;
}
if (aConfig.mMaxPlaybackRate) {
parameters[kCodecParamMaxPlaybackRate] =
std::to_string(aConfig.mMaxPlaybackRate);
}
if (aConfig.mMaxAverageBitrate) {
parameters[kCodecParamMaxAverageBitrate] =
std::to_string(aConfig.mMaxAverageBitrate);
}
if (aConfig.mFrameSizeMs) {
parameters[kCodecParamPTime] = std::to_string(aConfig.mFrameSizeMs);
}
if (aConfig.mMinFrameSizeMs) {
parameters[kCodecParamMinPTime] = std::to_string(aConfig.mMinFrameSizeMs);
}
if (aConfig.mMaxFrameSizeMs) {
parameters[kCodecParamMaxPTime] = std::to_string(aConfig.mMaxFrameSizeMs);
}
if (aConfig.mCbrEnabled) {
parameters[kCodecParamCbr] = kParamValueTrue;
}
}
return webrtc::SdpAudioFormat(aConfig.mName, aConfig.mFreq, aConfig.mChannels,
parameters);
}
void WebrtcAudioConduit::DeleteSendStream() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mLock.LockedForWritingByCurrentThread());
if (!mSendStream) {
return;
}
mCall->Call()->DestroyAudioSendStream(mSendStream);
mSendStreamRunning = false;
mSendStream = nullptr;
// Reset base_seqs in case ssrcs get re-used.
mRtpSendBaseSeqs.clear();
}
void WebrtcAudioConduit::CreateSendStream() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mLock.LockedForWritingByCurrentThread());
if (mSendStream) {
return;
}
mSendStream = mCall->Call()->CreateAudioSendStream(mSendStreamConfig);
}
void WebrtcAudioConduit::DeleteRecvStream() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mLock.LockedForWritingByCurrentThread());
if (!mRecvStream) {
return;
}
mCall->Call()->DestroyAudioReceiveStream(mRecvStream);
mRecvStreamRunning = false;
mRecvStream = nullptr;
}
void WebrtcAudioConduit::CreateRecvStream() {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
MOZ_ASSERT(mLock.LockedForWritingByCurrentThread());
if (mRecvStream) {
return;
}
mRecvStream = mCall->Call()->CreateAudioReceiveStream(mRecvStreamConfig);
// Ensure that we set the jitter buffer target on this stream.
mRecvStream->SetBaseMinimumPlayoutDelayMs(mJitterBufferTargetMs);
}
void WebrtcAudioConduit::SetJitterBufferTarget(DOMHighResTimeStamp aTargetMs) {
MOZ_RELEASE_ASSERT(aTargetMs <= std::numeric_limits<uint16_t>::max());
MOZ_RELEASE_ASSERT(aTargetMs >= 0);
MOZ_ALWAYS_SUCCEEDS(mCallThread->Dispatch(NS_NewRunnableFunction(
__func__,
[this, self = RefPtr<WebrtcAudioConduit>(this), targetMs = aTargetMs] {
mJitterBufferTargetMs = static_cast<uint16_t>(targetMs);
if (mRecvStream) {
mRecvStream->SetBaseMinimumPlayoutDelayMs(targetMs);
}
})));
}
void WebrtcAudioConduit::DeliverPacket(rtc::CopyOnWriteBuffer packet,
PacketType type) {
// Currently unused.
MOZ_ASSERT(false);
}
Maybe<int> WebrtcAudioConduit::ActiveSendPayloadType() const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
auto stats = GetSenderStats();
if (!stats) {
return Nothing();
}
if (!stats->codec_payload_type) {
return Nothing();
}
return Some(*stats->codec_payload_type);
}
Maybe<int> WebrtcAudioConduit::ActiveRecvPayloadType() const {
MOZ_ASSERT(mCallThread->IsOnCurrentThread());
auto stats = GetReceiverStats();
if (!stats) {
return Nothing();
}
if (!stats->codec_payload_type) {
return Nothing();
}
return Some(*stats->codec_payload_type);
}
} // namespace mozilla
|