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
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
|
/* 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 "logging.h"
#include "nsIGfxInfo.h"
#include "nsServiceManagerUtils.h"
#include "PeerConnectionImpl.h"
#include "PeerConnectionMedia.h"
#include "MediaPipelineFactory.h"
#include "MediaPipelineFilter.h"
#include "transportflow.h"
#include "transportlayer.h"
#include "transportlayerdtls.h"
#include "transportlayerice.h"
#include "signaling/src/jsep/JsepTrack.h"
#include "signaling/src/jsep/JsepTransport.h"
#include "signaling/src/common/PtrVector.h"
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
#include "MediaStreamTrack.h"
#include "nsIPrincipal.h"
#include "nsIDocument.h"
#include "mozilla/Preferences.h"
#include "MediaEngine.h"
#endif
#include "GmpVideoCodec.h"
#ifdef MOZ_WEBRTC_OMX
#include "OMXVideoCodec.h"
#include "OMXCodecWrapper.h"
#endif
#ifdef MOZ_WEBRTC_MEDIACODEC
#include "MediaCodecVideoCodec.h"
#endif
#ifdef MOZILLA_INTERNAL_API
#include "mozilla/Preferences.h"
#endif
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
#include "WebrtcGmpVideoCodec.h"
#endif
#include <stdlib.h>
namespace mozilla {
MOZ_MTLOG_MODULE("MediaPipelineFactory")
static nsresult
JsepCodecDescToCodecConfig(const JsepCodecDescription& aCodec,
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 = new AudioCodecConfig(pt,
desc.mName,
desc.mClock,
desc.mPacketSize,
desc.mForceMono ? 1 : desc.mChannels,
desc.mBitrate,
desc.mFECEnabled);
(*aConfig)->mMaxPlaybackRate = desc.mMaxPlaybackRate;
(*aConfig)->mDtmfEnabled = desc.mDtmfEnabled;
return NS_OK;
}
static std::vector<JsepCodecDescription*>
GetCodecs(const JsepTrackNegotiatedDetails& aDetails)
{
// We do not try to handle cases where a codec is not used on the primary
// encoding.
if (aDetails.GetEncodingCount()) {
return aDetails.GetEncoding(0).GetCodecs();
}
return std::vector<JsepCodecDescription*>();
}
static nsresult
NegotiatedDetailsToAudioCodecConfigs(const JsepTrackNegotiatedDetails& aDetails,
PtrVector<AudioCodecConfig>* aConfigs)
{
std::vector<JsepCodecDescription*> codecs(GetCodecs(aDetails));
for (const JsepCodecDescription* codec : codecs) {
AudioCodecConfig* config;
if (NS_FAILED(JsepCodecDescToCodecConfig(*codec, &config))) {
return NS_ERROR_INVALID_ARG;
}
aConfigs->values.push_back(config);
}
return NS_OK;
}
static nsresult
JsepCodecDescToCodecConfig(const JsepCodecDescription& aCodec,
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. Issue 165.
}
VideoCodecConfig* configRaw;
configRaw = new VideoCodecConfig(
pt, desc.mName, desc.mConstraints, h264Config.get());
configRaw->mAckFbTypes = desc.mAckFbTypes;
configRaw->mNackFbTypes = desc.mNackFbTypes;
configRaw->mCcmFbTypes = desc.mCcmFbTypes;
configRaw->mRembFbSet = desc.RtcpFbRembIsSet();
configRaw->mFECFbSet = desc.mFECEnabled;
*aConfig = configRaw;
return NS_OK;
}
static nsresult
NegotiatedDetailsToVideoCodecConfigs(const JsepTrackNegotiatedDetails& aDetails,
PtrVector<VideoCodecConfig>* aConfigs)
{
std::vector<JsepCodecDescription*> codecs(GetCodecs(aDetails));
for (const JsepCodecDescription* codec : codecs) {
VideoCodecConfig* config;
if (NS_FAILED(JsepCodecDescToCodecConfig(*codec, &config))) {
return NS_ERROR_INVALID_ARG;
}
for (size_t i = 0; i < aDetails.GetEncodingCount(); ++i) {
const JsepTrackEncoding& jsepEncoding(aDetails.GetEncoding(i));
if (jsepEncoding.HasFormat(codec->mDefaultPt)) {
VideoCodecConfig::SimulcastEncoding encoding;
encoding.rid = jsepEncoding.mRid;
encoding.constraints = jsepEncoding.mConstraints;
config->mSimulcastEncodings.push_back(encoding);
}
}
aConfigs->values.push_back(config);
}
return NS_OK;
}
// Accessing the PCMedia should be safe here because we shouldn't
// have enqueued this function unless it was still active and
// the ICE data is destroyed on the STS.
static void
FinalizeTransportFlow_s(RefPtr<PeerConnectionMedia> aPCMedia,
RefPtr<TransportFlow> aFlow, size_t aLevel,
bool aIsRtcp,
nsAutoPtr<PtrVector<TransportLayer> > aLayerList)
{
TransportLayerIce* ice =
static_cast<TransportLayerIce*>(aLayerList->values.front());
ice->SetParameters(aPCMedia->ice_ctx(),
aPCMedia->ice_media_stream(aLevel),
aIsRtcp ? 2 : 1);
nsAutoPtr<std::queue<TransportLayer*> > layerQueue(
new std::queue<TransportLayer*>);
for (auto i = aLayerList->values.begin(); i != aLayerList->values.end();
++i) {
layerQueue->push(*i);
}
aLayerList->values.clear();
(void)aFlow->PushLayers(layerQueue); // TODO(bug 854518): Process errors.
}
static void
AddNewIceStreamForRestart_s(RefPtr<PeerConnectionMedia> aPCMedia,
RefPtr<TransportFlow> aFlow,
size_t aLevel,
bool aIsRtcp)
{
TransportLayerIce* ice =
static_cast<TransportLayerIce*>(aFlow->GetLayer("ice"));
ice->SetParameters(aPCMedia->ice_ctx(),
aPCMedia->ice_media_stream(aLevel),
aIsRtcp ? 2 : 1);
}
nsresult
MediaPipelineFactory::CreateOrGetTransportFlow(
size_t aLevel,
bool aIsRtcp,
const JsepTransport& aTransport,
RefPtr<TransportFlow>* aFlowOutparam)
{
nsresult rv;
RefPtr<TransportFlow> flow;
flow = mPCMedia->GetTransportFlow(aLevel, aIsRtcp);
if (flow) {
if (mPCMedia->IsIceRestarting()) {
MOZ_MTLOG(ML_INFO, "Flow[" << flow->id() << "]: "
<< "detected ICE restart - level: "
<< aLevel << " rtcp: " << aIsRtcp);
rv = mPCMedia->GetSTSThread()->Dispatch(
WrapRunnableNM(AddNewIceStreamForRestart_s,
mPCMedia, flow, aLevel, aIsRtcp),
NS_DISPATCH_NORMAL);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Failed to dispatch AddNewIceStreamForRestart_s");
return rv;
}
}
*aFlowOutparam = flow;
return NS_OK;
}
std::ostringstream osId;
osId << mPC->GetHandle() << ":" << aLevel << ","
<< (aIsRtcp ? "rtcp" : "rtp");
flow = new TransportFlow(osId.str());
// The media streams are made on STS so we need to defer setup.
auto ice = MakeUnique<TransportLayerIce>(mPC->GetHandle());
auto dtls = MakeUnique<TransportLayerDtls>();
dtls->SetRole(aTransport.mDtls->GetRole() ==
JsepDtlsTransport::kJsepDtlsClient
? TransportLayerDtls::CLIENT
: TransportLayerDtls::SERVER);
RefPtr<DtlsIdentity> pcid = mPC->Identity();
if (!pcid) {
MOZ_MTLOG(ML_ERROR, "Failed to get DTLS identity.");
return NS_ERROR_FAILURE;
}
dtls->SetIdentity(pcid);
const SdpFingerprintAttributeList& fingerprints =
aTransport.mDtls->GetFingerprints();
for (auto fp = fingerprints.mFingerprints.begin();
fp != fingerprints.mFingerprints.end();
++fp) {
std::ostringstream ss;
ss << fp->hashFunc;
rv = dtls->SetVerificationDigest(ss.str(), &fp->fingerprint[0],
fp->fingerprint.size());
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Could not set fingerprint");
return rv;
}
}
std::vector<uint16_t> srtpCiphers;
srtpCiphers.push_back(SRTP_AES128_CM_HMAC_SHA1_80);
srtpCiphers.push_back(SRTP_AES128_CM_HMAC_SHA1_32);
rv = dtls->SetSrtpCiphers(srtpCiphers);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Couldn't set SRTP ciphers");
return rv;
}
// Always permits negotiation of the confidential mode.
// Only allow non-confidential (which is an allowed default),
// if we aren't confidential.
std::set<std::string> alpn;
std::string alpnDefault = "";
alpn.insert("c-webrtc");
if (!mPC->PrivacyRequested()) {
alpnDefault = "webrtc";
alpn.insert(alpnDefault);
}
rv = dtls->SetAlpn(alpn, alpnDefault);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Couldn't set ALPN");
return rv;
}
nsAutoPtr<PtrVector<TransportLayer> > layers(new PtrVector<TransportLayer>);
layers->values.push_back(ice.release());
layers->values.push_back(dtls.release());
rv = mPCMedia->GetSTSThread()->Dispatch(
WrapRunnableNM(FinalizeTransportFlow_s, mPCMedia, flow, aLevel, aIsRtcp,
layers),
NS_DISPATCH_NORMAL);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Failed to dispatch FinalizeTransportFlow_s");
return rv;
}
mPCMedia->AddTransportFlow(aLevel, aIsRtcp, flow);
*aFlowOutparam = flow;
return NS_OK;
}
nsresult
MediaPipelineFactory::GetTransportParameters(
const JsepTrackPair& aTrackPair,
const JsepTrack& aTrack,
size_t* aLevelOut,
RefPtr<TransportFlow>* aRtpOut,
RefPtr<TransportFlow>* aRtcpOut,
nsAutoPtr<MediaPipelineFilter>* aFilterOut)
{
*aLevelOut = aTrackPair.mLevel;
size_t transportLevel = aTrackPair.mBundleLevel.isSome() ?
*aTrackPair.mBundleLevel :
aTrackPair.mLevel;
nsresult rv = CreateOrGetTransportFlow(
transportLevel, false, *aTrackPair.mRtpTransport, aRtpOut);
if (NS_FAILED(rv)) {
return rv;
}
MOZ_ASSERT(aRtpOut);
if (aTrackPair.mRtcpTransport) {
rv = CreateOrGetTransportFlow(
transportLevel, true, *aTrackPair.mRtcpTransport, aRtcpOut);
if (NS_FAILED(rv)) {
return rv;
}
MOZ_ASSERT(aRtcpOut);
}
if (aTrackPair.mBundleLevel.isSome()) {
bool receiving = aTrack.GetDirection() == sdp::kRecv;
*aFilterOut = new MediaPipelineFilter;
if (receiving) {
// Add remote SSRCs so we can distinguish which RTP packets actually
// belong to this pipeline (also RTCP sender reports).
for (auto i = aTrack.GetSsrcs().begin();
i != aTrack.GetSsrcs().end(); ++i) {
(*aFilterOut)->AddRemoteSSRC(*i);
}
// TODO(bug 1105005): Tell the filter about the mid for this track
// Add unique payload types as a last-ditch fallback
auto uniquePts = aTrack.GetNegotiatedDetails()->GetUniquePayloadTypes();
for (auto i = uniquePts.begin(); i != uniquePts.end(); ++i) {
(*aFilterOut)->AddUniquePT(*i);
}
}
}
return NS_OK;
}
nsresult
MediaPipelineFactory::CreateOrUpdateMediaPipeline(
const JsepTrackPair& aTrackPair,
const JsepTrack& aTrack)
{
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
// The GMP code is all the way on the other side of webrtc.org, and it is not
// feasible to plumb this information all the way through. So, we set it (for
// the duration of this call) in a global variable. This allows the GMP code
// to report errors to the PC.
WebrtcGmpPCHandleSetter setter(mPC->GetHandle());
#endif
MOZ_ASSERT(aTrackPair.mRtpTransport);
bool receiving = aTrack.GetDirection() == sdp::kRecv;
size_t level;
RefPtr<TransportFlow> rtpFlow;
RefPtr<TransportFlow> rtcpFlow;
nsAutoPtr<MediaPipelineFilter> filter;
nsresult rv = GetTransportParameters(aTrackPair,
aTrack,
&level,
&rtpFlow,
&rtcpFlow,
&filter);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Failed to get transport parameters for pipeline, rv="
<< static_cast<unsigned>(rv));
return rv;
}
if (aTrack.GetMediaType() == SdpMediaSection::kApplication) {
// GetTransportParameters has already done everything we need for
// datachannel.
return NS_OK;
}
// Find the stream we need
SourceStreamInfo* stream;
if (receiving) {
stream = mPCMedia->GetRemoteStreamById(aTrack.GetStreamId());
} else {
stream = mPCMedia->GetLocalStreamById(aTrack.GetStreamId());
}
if (!stream) {
MOZ_MTLOG(ML_ERROR, "Negotiated " << (receiving ? "recv" : "send")
<< " stream id " << aTrack.GetStreamId() << " was never added");
MOZ_ASSERT(false);
return NS_ERROR_FAILURE;
}
if (!stream->HasTrack(aTrack.GetTrackId())) {
MOZ_MTLOG(ML_ERROR, "Negotiated " << (receiving ? "recv" : "send")
<< " track id " << aTrack.GetTrackId() << " was never added");
MOZ_ASSERT(false);
return NS_ERROR_FAILURE;
}
RefPtr<MediaSessionConduit> conduit;
if (aTrack.GetMediaType() == SdpMediaSection::kAudio) {
rv = GetOrCreateAudioConduit(aTrackPair, aTrack, &conduit);
if (NS_FAILED(rv))
return rv;
} else if (aTrack.GetMediaType() == SdpMediaSection::kVideo) {
rv = GetOrCreateVideoConduit(aTrackPair, aTrack, &conduit);
if (NS_FAILED(rv))
return rv;
} else {
// We've created the TransportFlow, nothing else to do here.
return NS_OK;
}
if (aTrack.GetActive()) {
if (receiving) {
auto error = conduit->StartReceiving();
if (error) {
MOZ_MTLOG(ML_ERROR, "StartReceiving failed: " << error);
return NS_ERROR_FAILURE;
}
} else {
auto error = conduit->StartTransmitting();
if (error) {
MOZ_MTLOG(ML_ERROR, "StartTransmitting failed: " << error);
return NS_ERROR_FAILURE;
}
}
} else {
if (receiving) {
auto error = conduit->StopReceiving();
if (error) {
MOZ_MTLOG(ML_ERROR, "StopReceiving failed: " << error);
return NS_ERROR_FAILURE;
}
} else {
auto error = conduit->StopTransmitting();
if (error) {
MOZ_MTLOG(ML_ERROR, "StopTransmitting failed: " << error);
return NS_ERROR_FAILURE;
}
}
}
RefPtr<MediaPipeline> pipeline =
stream->GetPipelineByTrackId_m(aTrack.GetTrackId());
if (pipeline && pipeline->level() != static_cast<int>(level)) {
MOZ_MTLOG(ML_WARNING, "Track " << aTrack.GetTrackId() <<
" has moved from level " << pipeline->level() <<
" to level " << level <<
". This requires re-creating the MediaPipeline.");
RefPtr<dom::MediaStreamTrack> domTrack =
stream->GetTrackById(aTrack.GetTrackId());
MOZ_ASSERT(domTrack, "MediaPipeline existed for a track, but no MediaStreamTrack");
// Since we do not support changing the conduit on a pre-existing
// MediaPipeline
pipeline = nullptr;
stream->RemoveTrack(aTrack.GetTrackId());
stream->AddTrack(aTrack.GetTrackId(), domTrack);
}
if (pipeline) {
pipeline->UpdateTransport_m(level, rtpFlow, rtcpFlow, filter);
return NS_OK;
}
MOZ_MTLOG(ML_DEBUG,
"Creating media pipeline"
<< " m-line index=" << aTrackPair.mLevel
<< " type=" << aTrack.GetMediaType()
<< " direction=" << aTrack.GetDirection());
if (receiving) {
rv = CreateMediaPipelineReceiving(aTrackPair, aTrack,
level, rtpFlow, rtcpFlow, filter,
conduit);
if (NS_FAILED(rv))
return rv;
} else {
rv = CreateMediaPipelineSending(aTrackPair, aTrack,
level, rtpFlow, rtcpFlow, filter,
conduit);
if (NS_FAILED(rv))
return rv;
}
return NS_OK;
}
nsresult
MediaPipelineFactory::CreateMediaPipelineReceiving(
const JsepTrackPair& aTrackPair,
const JsepTrack& aTrack,
size_t aLevel,
RefPtr<TransportFlow> aRtpFlow,
RefPtr<TransportFlow> aRtcpFlow,
nsAutoPtr<MediaPipelineFilter> aFilter,
const RefPtr<MediaSessionConduit>& aConduit)
{
// We will error out earlier if this isn't here.
RefPtr<RemoteSourceStreamInfo> stream =
mPCMedia->GetRemoteStreamById(aTrack.GetStreamId());
RefPtr<MediaPipelineReceive> pipeline;
TrackID numericTrackId = stream->GetNumericTrackId(aTrack.GetTrackId());
MOZ_ASSERT(IsTrackIDExplicit(numericTrackId));
MOZ_MTLOG(ML_DEBUG, __FUNCTION__ << ": Creating pipeline for "
<< numericTrackId << " -> " << aTrack.GetTrackId());
if (aTrack.GetMediaType() == SdpMediaSection::kAudio) {
pipeline = new MediaPipelineReceiveAudio(
mPC->GetHandle(),
mPC->GetMainThread().get(),
mPC->GetSTSThread(),
stream->GetMediaStream()->GetInputStream()->AsSourceStream(),
aTrack.GetTrackId(),
numericTrackId,
aLevel,
static_cast<AudioSessionConduit*>(aConduit.get()), // Ugly downcast.
aRtpFlow,
aRtcpFlow,
aFilter);
} else if (aTrack.GetMediaType() == SdpMediaSection::kVideo) {
pipeline = new MediaPipelineReceiveVideo(
mPC->GetHandle(),
mPC->GetMainThread().get(),
mPC->GetSTSThread(),
stream->GetMediaStream()->GetInputStream()->AsSourceStream(),
aTrack.GetTrackId(),
numericTrackId,
aLevel,
static_cast<VideoSessionConduit*>(aConduit.get()), // Ugly downcast.
aRtpFlow,
aRtcpFlow,
aFilter);
} else {
MOZ_ASSERT(false);
MOZ_MTLOG(ML_ERROR, "Invalid media type in CreateMediaPipelineReceiving");
return NS_ERROR_FAILURE;
}
nsresult rv = pipeline->Init();
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Couldn't initialize receiving pipeline");
return rv;
}
rv = stream->StorePipeline(aTrack.GetTrackId(),
RefPtr<MediaPipeline>(pipeline));
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Couldn't store receiving pipeline " <<
static_cast<unsigned>(rv));
return rv;
}
stream->SyncPipeline(pipeline);
return NS_OK;
}
nsresult
MediaPipelineFactory::CreateMediaPipelineSending(
const JsepTrackPair& aTrackPair,
const JsepTrack& aTrack,
size_t aLevel,
RefPtr<TransportFlow> aRtpFlow,
RefPtr<TransportFlow> aRtcpFlow,
nsAutoPtr<MediaPipelineFilter> aFilter,
const RefPtr<MediaSessionConduit>& aConduit)
{
nsresult rv;
// This is checked earlier
RefPtr<LocalSourceStreamInfo> stream =
mPCMedia->GetLocalStreamById(aTrack.GetStreamId());
dom::MediaStreamTrack* track =
stream->GetTrackById(aTrack.GetTrackId());
MOZ_ASSERT(track);
// Now we have all the pieces, create the pipeline
RefPtr<MediaPipelineTransmit> pipeline = new MediaPipelineTransmit(
mPC->GetHandle(),
mPC->GetMainThread().get(),
mPC->GetSTSThread(),
track,
aTrack.GetTrackId(),
aLevel,
aConduit,
aRtpFlow,
aRtcpFlow,
aFilter);
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
// implement checking for peerIdentity (where failure == black/silence)
nsIDocument* doc = mPC->GetWindow()->GetExtantDoc();
if (doc) {
pipeline->UpdateSinkIdentity_m(track,
doc->NodePrincipal(),
mPC->GetPeerIdentity());
} else {
MOZ_MTLOG(ML_ERROR, "Cannot initialize pipeline without attached doc");
return NS_ERROR_FAILURE; // Don't remove this till we know it's safe.
}
#endif
rv = pipeline->Init();
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Couldn't initialize sending pipeline");
return rv;
}
rv = stream->StorePipeline(aTrack.GetTrackId(),
RefPtr<MediaPipeline>(pipeline));
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Couldn't store receiving pipeline " <<
static_cast<unsigned>(rv));
return rv;
}
return NS_OK;
}
nsresult
MediaPipelineFactory::GetOrCreateAudioConduit(
const JsepTrackPair& aTrackPair,
const JsepTrack& aTrack,
RefPtr<MediaSessionConduit>* aConduitp)
{
if (!aTrack.GetNegotiatedDetails()) {
MOZ_ASSERT(false, "Track is missing negotiated details");
return NS_ERROR_INVALID_ARG;
}
bool receiving = aTrack.GetDirection() == sdp::kRecv;
RefPtr<AudioSessionConduit> conduit =
mPCMedia->GetAudioConduit(aTrackPair.mLevel);
if (!conduit) {
conduit = AudioSessionConduit::Create();
if (!conduit) {
MOZ_MTLOG(ML_ERROR, "Could not create audio conduit");
return NS_ERROR_FAILURE;
}
mPCMedia->AddAudioConduit(aTrackPair.mLevel, conduit);
}
PtrVector<AudioCodecConfig> configs;
nsresult rv = NegotiatedDetailsToAudioCodecConfigs(
*aTrack.GetNegotiatedDetails(), &configs);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Failed to convert JsepCodecDescriptions to "
"AudioCodecConfigs.");
return rv;
}
if (configs.values.empty()) {
MOZ_MTLOG(ML_ERROR, "Can't set up a conduit with 0 codecs");
return NS_ERROR_FAILURE;
}
if (receiving) {
auto error = conduit->ConfigureRecvMediaCodecs(configs.values);
if (error) {
MOZ_MTLOG(ML_ERROR, "ConfigureRecvMediaCodecs failed: " << error);
return NS_ERROR_FAILURE;
}
if (!aTrackPair.mSending) {
// No send track, but we still need to configure an SSRC for receiver
// reports.
if (!conduit->SetLocalSSRC(aTrackPair.mRecvonlySsrc)) {
MOZ_MTLOG(ML_ERROR, "SetLocalSSRC failed");
return NS_ERROR_FAILURE;
}
}
} else {
// For now we only expect to have one ssrc per local track.
auto ssrcs = aTrack.GetSsrcs();
if (!ssrcs.empty()) {
if (!conduit->SetLocalSSRC(ssrcs.front())) {
MOZ_MTLOG(ML_ERROR, "SetLocalSSRC failed");
return NS_ERROR_FAILURE;
}
}
conduit->SetLocalCNAME(aTrack.GetCNAME().c_str());
if (configs.values.size() > 1
&& configs.values.back()->mName == "telephone-event") {
// we have a telephone event codec, so we need to make sure
// the dynamic pt is set properly
conduit->SetDtmfPayloadType(configs.values.back()->mType);
}
auto error = conduit->ConfigureSendMediaCodec(configs.values[0]);
if (error) {
MOZ_MTLOG(ML_ERROR, "ConfigureSendMediaCodec failed: " << error);
return NS_ERROR_FAILURE;
}
const SdpExtmapAttributeList::Extmap* audioLevelExt =
aTrack.GetNegotiatedDetails()->GetExt(
"urn:ietf:params:rtp-hdrext:ssrc-audio-level");
if (audioLevelExt) {
MOZ_MTLOG(ML_DEBUG, "Calling EnableAudioLevelExtension");
error = conduit->EnableAudioLevelExtension(true, audioLevelExt->entry);
if (error) {
MOZ_MTLOG(ML_ERROR, "EnableAudioLevelExtension failed: " << error);
return NS_ERROR_FAILURE;
}
}
}
*aConduitp = conduit;
return NS_OK;
}
nsresult
MediaPipelineFactory::GetOrCreateVideoConduit(
const JsepTrackPair& aTrackPair,
const JsepTrack& aTrack,
RefPtr<MediaSessionConduit>* aConduitp)
{
if (!aTrack.GetNegotiatedDetails()) {
MOZ_ASSERT(false, "Track is missing negotiated details");
return NS_ERROR_INVALID_ARG;
}
bool receiving = aTrack.GetDirection() == sdp::kRecv;
RefPtr<VideoSessionConduit> conduit =
mPCMedia->GetVideoConduit(aTrackPair.mLevel);
if (!conduit) {
conduit = VideoSessionConduit::Create();
if (!conduit) {
MOZ_MTLOG(ML_ERROR, "Could not create video conduit");
return NS_ERROR_FAILURE;
}
mPCMedia->AddVideoConduit(aTrackPair.mLevel, conduit);
}
PtrVector<VideoCodecConfig> configs;
nsresult rv = NegotiatedDetailsToVideoCodecConfigs(
*aTrack.GetNegotiatedDetails(), &configs);
if (NS_FAILED(rv)) {
MOZ_MTLOG(ML_ERROR, "Failed to convert JsepCodecDescriptions to "
"VideoCodecConfigs.");
return rv;
}
if (configs.values.empty()) {
MOZ_MTLOG(ML_ERROR, "Can't set up a conduit with 0 codecs");
return NS_ERROR_FAILURE;
}
if (receiving) {
if (aTrackPair.mSending) {
auto ssrcs = &aTrackPair.mSending->GetSsrcs();
if (!ssrcs->empty()) {
if (!conduit->SetLocalSSRC(ssrcs->front())) {
MOZ_MTLOG(ML_ERROR, "SetLocalSSRC failed(1)");
return NS_ERROR_FAILURE;
}
} else {
MOZ_MTLOG(ML_ERROR, "Sending without an SSRC??");
return NS_ERROR_FAILURE;
}
} else {
// No send track, but we still need to configure an SSRC for receiver
// reports.
if (!conduit->SetLocalSSRC(aTrackPair.mRecvonlySsrc)) {
MOZ_MTLOG(ML_ERROR, "SetLocalSSRC failed(2)");
return NS_ERROR_FAILURE;
}
}
// Prune out stuff we cannot actually do. We should work to eliminate the
// need for this.
bool configuredH264 = false;
for (size_t i = 0; i < configs.values.size();) {
// TODO(bug 1200768): We can only handle configuring one recv H264 codec
if (configuredH264 && (configs.values[i]->mName == "H264")) {
delete configs.values[i];
configs.values.erase(configs.values.begin() + i);
continue;
}
// TODO(bug 1018791): This really should be checked sooner
if (EnsureExternalCodec(*conduit, configs.values[i], false)) {
delete configs.values[i];
configs.values.erase(configs.values.begin() + i);
continue;
}
if (configs.values[i]->mName == "H264") {
configuredH264 = true;
}
++i;
}
auto error = conduit->ConfigureRecvMediaCodecs(configs.values);
if (error) {
MOZ_MTLOG(ML_ERROR, "ConfigureRecvMediaCodecs failed: " << error);
return NS_ERROR_FAILURE;
}
} else {
// For now we only expect to have one ssrc per local track.
auto ssrcs = aTrack.GetSsrcs();
if (!ssrcs.empty()) {
if (!conduit->SetLocalSSRC(ssrcs.front())) {
MOZ_MTLOG(ML_ERROR, "SetLocalSSRC failed");
return NS_ERROR_FAILURE;
}
}
conduit->SetLocalCNAME(aTrack.GetCNAME().c_str());
rv = ConfigureVideoCodecMode(aTrack,*conduit);
if (NS_FAILED(rv)) {
return rv;
}
// TODO(bug 1018791): This really should be checked sooner
if (EnsureExternalCodec(*conduit, configs.values[0], true)) {
MOZ_MTLOG(ML_ERROR, "External codec not available");
return NS_ERROR_FAILURE;
}
auto error = conduit->ConfigureSendMediaCodec(configs.values[0]);
const SdpExtmapAttributeList::Extmap* rtpStreamIdExt =
aTrack.GetNegotiatedDetails()->GetExt(
"urn:ietf:params:rtp-hdrext:sdes:rtp-stream-id");
if (rtpStreamIdExt) {
MOZ_MTLOG(ML_DEBUG, "Calling EnableRTPSenderIdExtension");
error = conduit->EnableRTPStreamIdExtension(true, rtpStreamIdExt->entry);
if (error) {
MOZ_MTLOG(ML_ERROR, "EnableRTPSenderIdExtension failed: " << error);
return NS_ERROR_FAILURE;
}
}
if (error) {
MOZ_MTLOG(ML_ERROR, "ConfigureSendMediaCodec failed: " << error);
return NS_ERROR_FAILURE;
}
}
*aConduitp = conduit;
return NS_OK;
}
nsresult
MediaPipelineFactory::ConfigureVideoCodecMode(const JsepTrack& aTrack,
VideoSessionConduit& aConduit)
{
#if !defined(MOZILLA_EXTERNAL_LINKAGE)
RefPtr<LocalSourceStreamInfo> stream =
mPCMedia->GetLocalStreamByTrackId(aTrack.GetTrackId());
//get video track
RefPtr<mozilla::dom::MediaStreamTrack> track =
stream->GetTrackById(aTrack.GetTrackId());
RefPtr<mozilla::dom::VideoStreamTrack> videotrack =
track->AsVideoStreamTrack();
if (!videotrack) {
MOZ_MTLOG(ML_ERROR, "video track not available");
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::Application:
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, "ConfigureCodecMode failed: " << error);
return NS_ERROR_FAILURE;
}
#endif
return NS_OK;
}
/*
* Add external H.264 video codec.
*/
MediaConduitErrorCode
MediaPipelineFactory::EnsureExternalCodec(VideoSessionConduit& aConduit,
VideoCodecConfig* aConfig,
bool aIsSend)
{
if (aConfig->mName == "VP8") {
#ifdef MOZ_WEBRTC_MEDIACODEC
if (aIsSend) {
#ifdef MOZILLA_INTERNAL_API
bool enabled = mozilla::Preferences::GetBool("media.navigator.hardware.vp8_encode.acceleration_enabled", false);
#else
bool enabled = false;
#endif
if (enabled) {
nsCOMPtr<nsIGfxInfo> gfxInfo = do_GetService("@mozilla.org/gfx/info;1");
if (gfxInfo) {
int32_t status;
nsCString discardFailureId;
if (NS_SUCCEEDED(gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_ENCODE, discardFailureId, &status))) {
if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
NS_WARNING("VP8 encoder hardware is not whitelisted: disabling.\n");
} else {
VideoEncoder* encoder = nullptr;
encoder = MediaCodecVideoCodec::CreateEncoder(MediaCodecVideoCodec::CodecType::CODEC_VP8);
if (encoder) {
return aConduit.SetExternalSendCodec(aConfig, encoder);
}
return kMediaConduitNoError;
}
}
}
}
} else {
#ifdef MOZILLA_INTERNAL_API
bool enabled = mozilla::Preferences::GetBool("media.navigator.hardware.vp8_decode.acceleration_enabled", false);
#else
bool enabled = false;
#endif
if (enabled) {
nsCOMPtr<nsIGfxInfo> gfxInfo = do_GetService("@mozilla.org/gfx/info;1");
if (gfxInfo) {
int32_t status;
nsCString discardFailureId;
if (NS_SUCCEEDED(gfxInfo->GetFeatureStatus(nsIGfxInfo::FEATURE_WEBRTC_HW_ACCELERATION_DECODE, discardFailureId, &status))) {
if (status != nsIGfxInfo::FEATURE_STATUS_OK) {
NS_WARNING("VP8 decoder hardware is not whitelisted: disabling.\n");
} else {
VideoDecoder* decoder;
decoder = MediaCodecVideoCodec::CreateDecoder(MediaCodecVideoCodec::CodecType::CODEC_VP8);
if (decoder) {
return aConduit.SetExternalRecvCodec(aConfig, decoder);
}
return kMediaConduitNoError;
}
}
}
}
}
#endif
return kMediaConduitNoError;
}
if (aConfig->mName == "VP9") {
return kMediaConduitNoError;
}
if (aConfig->mName == "H264") {
if (aConduit.CodecPluginID() != 0) {
return kMediaConduitNoError;
}
// Register H.264 codec.
if (aIsSend) {
VideoEncoder* encoder = nullptr;
#ifdef MOZ_WEBRTC_OMX
encoder =
OMXVideoCodec::CreateEncoder(OMXVideoCodec::CodecType::CODEC_H264);
#else
encoder = GmpVideoCodec::CreateEncoder();
#endif
if (encoder) {
return aConduit.SetExternalSendCodec(aConfig, encoder);
}
return kMediaConduitInvalidSendCodec;
}
VideoDecoder* decoder = nullptr;
#ifdef MOZ_WEBRTC_OMX
decoder =
OMXVideoCodec::CreateDecoder(OMXVideoCodec::CodecType::CODEC_H264);
#else
decoder = GmpVideoCodec::CreateDecoder();
#endif
if (decoder) {
return aConduit.SetExternalRecvCodec(aConfig, decoder);
}
return kMediaConduitInvalidReceiveCodec;
}
MOZ_MTLOG(ML_ERROR,
"Invalid video codec configured: " << aConfig->mName.c_str());
return aIsSend ? kMediaConduitInvalidSendCodec
: kMediaConduitInvalidReceiveCodec;
}
} // namespace mozilla
|