|
@@ -15,7 +15,7 @@ function Simulcast() {
|
15
|
15
|
"use strict";
|
16
|
16
|
// global state for all transformers.
|
17
|
17
|
var localExplosionMap = {}, localVideoSourceCache, emptyCompoundIndex,
|
18
|
|
- remoteMaps = {
|
|
18
|
+ remoteVideoSourceCache, remoteMaps = {
|
19
|
19
|
msid2Quality: {},
|
20
|
20
|
ssrc2Msid: {},
|
21
|
21
|
receivingVideoStreams: {}
|
|
@@ -45,6 +45,14 @@ function Simulcast() {
|
45
|
45
|
this._replaceVideoSources(lines, localVideoSourceCache);
|
46
|
46
|
};
|
47
|
47
|
|
|
48
|
+ Simulcast.prototype._cacheRemoteVideoSources = function (lines) {
|
|
49
|
+ remoteVideoSourceCache = this._getVideoSources(lines);
|
|
50
|
+ };
|
|
51
|
+
|
|
52
|
+ Simulcast.prototype._restoreRemoteVideoSources = function (lines) {
|
|
53
|
+ this._replaceVideoSources(lines, remoteVideoSourceCache);
|
|
54
|
+ };
|
|
55
|
+
|
48
|
56
|
Simulcast.prototype._replaceVideoSources = function (lines, videoSources) {
|
49
|
57
|
|
50
|
58
|
var i, inVideo = false, index = -1, howMany = 0;
|
|
@@ -216,6 +224,12 @@ function Simulcast() {
|
216
|
224
|
}
|
217
|
225
|
};
|
218
|
226
|
|
|
227
|
+ /**
|
|
228
|
+ * Produces a single stream with multiple tracks for local video sources.
|
|
229
|
+ *
|
|
230
|
+ * @param lines
|
|
231
|
+ * @private
|
|
232
|
+ */
|
219
|
233
|
Simulcast.prototype._explodeLocalSimulcastSources = function (lines) {
|
220
|
234
|
var sb, msid, sid, tid, videoSources, self;
|
221
|
235
|
|
|
@@ -259,6 +273,12 @@ function Simulcast() {
|
259
|
273
|
this._replaceVideoSources(lines, sb);
|
260
|
274
|
};
|
261
|
275
|
|
|
276
|
+ /**
|
|
277
|
+ * Groups local video sources together in the ssrc-group:SIM group.
|
|
278
|
+ *
|
|
279
|
+ * @param lines
|
|
280
|
+ * @private
|
|
281
|
+ */
|
262
|
282
|
Simulcast.prototype._groupLocalVideoSources = function (lines) {
|
263
|
283
|
var sb, videoSources, ssrcs = [], ssrc;
|
264
|
284
|
|
|
@@ -401,6 +421,13 @@ function Simulcast() {
|
401
|
421
|
return sb;
|
402
|
422
|
};
|
403
|
423
|
|
|
424
|
+ /**
|
|
425
|
+ * Ensures that the simulcast group is present in the answer, _if_ native
|
|
426
|
+ * simulcast is enabled,
|
|
427
|
+ *
|
|
428
|
+ * @param desc
|
|
429
|
+ * @returns {*}
|
|
430
|
+ */
|
404
|
431
|
Simulcast.prototype.transformAnswer = function (desc) {
|
405
|
432
|
if (config.enableSimulcast && config.useNativeSimulcast) {
|
406
|
433
|
|
|
@@ -429,11 +456,53 @@ function Simulcast() {
|
429
|
456
|
return desc;
|
430
|
457
|
};
|
431
|
458
|
|
432
|
|
- Simulcast.prototype.makeLocalDescriptionPublic = function (desc) {
|
|
459
|
+ Simulcast.prototype._restoreSimulcastGroups = function (sb) {
|
|
460
|
+ this._restoreRemoteVideoSources(sb);
|
|
461
|
+ };
|
|
462
|
+
|
|
463
|
+ /**
|
|
464
|
+ * Restores the simulcast groups of the remote description. In
|
|
465
|
+ * transformRemoteDescription we remove those in order for the set remote
|
|
466
|
+ * description to succeed. The focus needs the signal the groups to new
|
|
467
|
+ * participants.
|
|
468
|
+ *
|
|
469
|
+ * @param desc
|
|
470
|
+ * @returns {*}
|
|
471
|
+ */
|
|
472
|
+ Simulcast.prototype.reverseTransformRemoteDescription = function (desc) {
|
433
|
473
|
var sb;
|
434
|
474
|
|
435
|
|
- if (!desc || desc == null)
|
|
475
|
+ if (!desc || desc == null) {
|
436
|
476
|
return desc;
|
|
477
|
+ }
|
|
478
|
+
|
|
479
|
+ if (config.enableSimulcast) {
|
|
480
|
+ sb = desc.sdp.split('\r\n');
|
|
481
|
+
|
|
482
|
+ this._restoreSimulcastGroups(sb);
|
|
483
|
+
|
|
484
|
+ desc = new RTCSessionDescription({
|
|
485
|
+ type: desc.type,
|
|
486
|
+ sdp: sb.join('\r\n')
|
|
487
|
+ });
|
|
488
|
+ }
|
|
489
|
+
|
|
490
|
+ return desc;
|
|
491
|
+ };
|
|
492
|
+
|
|
493
|
+ /**
|
|
494
|
+ * Prepares the local description for public usage (i.e. to be signaled
|
|
495
|
+ * through Jingle to the focus).
|
|
496
|
+ *
|
|
497
|
+ * @param desc
|
|
498
|
+ * @returns {RTCSessionDescription}
|
|
499
|
+ */
|
|
500
|
+ Simulcast.prototype.reverseTransformLocalDescription = function (desc) {
|
|
501
|
+ var sb;
|
|
502
|
+
|
|
503
|
+ if (!desc || desc == null) {
|
|
504
|
+ return desc;
|
|
505
|
+ }
|
437
|
506
|
|
438
|
507
|
if (config.enableSimulcast) {
|
439
|
508
|
|
|
@@ -480,30 +549,16 @@ function Simulcast() {
|
480
|
549
|
this._replaceVideoSources(lines, sb);
|
481
|
550
|
};
|
482
|
551
|
|
483
|
|
- Simulcast.prototype.transformBridgeDescription = function (desc) {
|
484
|
|
- if (config.enableSimulcast && config.useNativeSimulcast) {
|
485
|
|
-
|
486
|
|
- var sb = desc.sdp.split('\r\n');
|
487
|
|
-
|
488
|
|
- this._ensureGoogConference(sb);
|
489
|
|
-
|
490
|
|
- desc = new RTCSessionDescription({
|
491
|
|
- type: desc.type,
|
492
|
|
- sdp: sb.join('\r\n')
|
493
|
|
- });
|
494
|
|
-
|
495
|
|
- if (this.debugLvl && this.debugLvl > 1) {
|
496
|
|
- console.info('Transformed bridge description');
|
497
|
|
- console.info(desc.sdp);
|
498
|
|
- }
|
499
|
|
- }
|
500
|
|
-
|
501
|
|
- return desc;
|
502
|
|
- };
|
503
|
|
-
|
504
|
552
|
Simulcast.prototype._updateRemoteMaps = function (lines) {
|
505
|
553
|
var remoteVideoSources = this._parseMedia(lines, ['video'])[0], videoSource, quality;
|
506
|
554
|
|
|
555
|
+ // (re) initialize the remote maps.
|
|
556
|
+ remoteMaps = {
|
|
557
|
+ msid2Quality: {},
|
|
558
|
+ ssrc2Msid: {},
|
|
559
|
+ receivingVideoStreams: {}
|
|
560
|
+ };
|
|
561
|
+
|
507
|
562
|
if (remoteVideoSources.groups && remoteVideoSources.groups.length !== 0) {
|
508
|
563
|
remoteVideoSources.groups.forEach(function (group) {
|
509
|
564
|
if (group.semantics === 'SIM' && group.ssrcs && group.ssrcs.length !== 0) {
|
|
@@ -518,6 +573,12 @@ function Simulcast() {
|
518
|
573
|
}
|
519
|
574
|
};
|
520
|
575
|
|
|
576
|
+ /**
|
|
577
|
+ *
|
|
578
|
+ *
|
|
579
|
+ * @param desc
|
|
580
|
+ * @returns {*}
|
|
581
|
+ */
|
521
|
582
|
Simulcast.prototype.transformLocalDescription = function (desc) {
|
522
|
583
|
if (config.enableSimulcast && !config.useNativeSimulcast) {
|
523
|
584
|
|
|
@@ -539,14 +600,28 @@ function Simulcast() {
|
539
|
600
|
return desc;
|
540
|
601
|
};
|
541
|
602
|
|
|
603
|
+ /**
|
|
604
|
+ * Removes the ssrc-group:SIM from the remote description bacause Chrome
|
|
605
|
+ * either gets confused and thinks this is an FID group or, if an FID group
|
|
606
|
+ * is already present, it fails to set the remote description.
|
|
607
|
+ *
|
|
608
|
+ * @param desc
|
|
609
|
+ * @returns {*}
|
|
610
|
+ */
|
542
|
611
|
Simulcast.prototype.transformRemoteDescription = function (desc) {
|
543
|
612
|
if (config.enableSimulcast) {
|
544
|
613
|
|
545
|
614
|
var sb = desc.sdp.split('\r\n');
|
546
|
615
|
|
547
|
616
|
this._updateRemoteMaps(sb);
|
548
|
|
- this._removeSimulcastGroup(sb); // NOTE(gp) this needs to be called after updateRemoteMaps!
|
549
|
|
- this._ensureGoogConference(sb);
|
|
617
|
+ this._cacheRemoteVideoSources(sb);
|
|
618
|
+ this._removeSimulcastGroup(sb); // NOTE(gp) this needs to be called after updateRemoteMaps because we need the simulcast group in the _updateRemoteMaps() method.
|
|
619
|
+
|
|
620
|
+ if (config.useNativeSimulcast) {
|
|
621
|
+ // We don't need the goog conference flag if we're not doing
|
|
622
|
+ // native simulcast.
|
|
623
|
+ this._ensureGoogConference(sb);
|
|
624
|
+ }
|
550
|
625
|
|
551
|
626
|
desc = new RTCSessionDescription({
|
552
|
627
|
type: desc.type,
|
|
@@ -562,20 +637,28 @@ function Simulcast() {
|
562
|
637
|
return desc;
|
563
|
638
|
};
|
564
|
639
|
|
565
|
|
- Simulcast.prototype.setReceivingVideoStream = function (ssrc) {
|
|
640
|
+ Simulcast.prototype._setReceivingVideoStream = function (ssrc) {
|
566
|
641
|
var receivingTrack = remoteMaps.ssrc2Msid[ssrc],
|
567
|
642
|
msidParts = receivingTrack.split(' ');
|
568
|
643
|
|
569
|
644
|
remoteMaps.receivingVideoStreams[msidParts[0]] = msidParts[1];
|
570
|
645
|
};
|
571
|
646
|
|
|
647
|
+ /**
|
|
648
|
+ * Returns a stream with single video track, the one currently being
|
|
649
|
+ * received by this endpoint.
|
|
650
|
+ *
|
|
651
|
+ * @param stream the remote simulcast stream.
|
|
652
|
+ * @returns {webkitMediaStream}
|
|
653
|
+ */
|
572
|
654
|
Simulcast.prototype.getReceivingVideoStream = function (stream) {
|
573
|
|
- var tracks, track, i, electedTrack, msid, quality = 1, receivingTrackId;
|
|
655
|
+ var tracks, i, electedTrack, msid, quality = 1, receivingTrackId;
|
574
|
656
|
|
575
|
657
|
if (config.enableSimulcast) {
|
576
|
658
|
|
577
|
659
|
if (remoteMaps.receivingVideoStreams[stream.id])
|
578
|
660
|
{
|
|
661
|
+ // the bridge has signaled us to receive a specific track.
|
579
|
662
|
receivingTrackId = remoteMaps.receivingVideoStreams[stream.id];
|
580
|
663
|
tracks = stream.getVideoTracks();
|
581
|
664
|
for (i = 0; i < tracks.length; i++) {
|
|
@@ -587,15 +670,18 @@ function Simulcast() {
|
587
|
670
|
}
|
588
|
671
|
|
589
|
672
|
if (!electedTrack) {
|
|
673
|
+ // we don't have an elected track, choose by initial quality.
|
590
|
674
|
tracks = stream.getVideoTracks();
|
591
|
675
|
for (i = 0; i < tracks.length; i++) {
|
592
|
|
- track = tracks[i];
|
593
|
|
- msid = [stream.id, track.id].join(' ');
|
|
676
|
+ msid = [stream.id, tracks[i].id].join(' ');
|
594
|
677
|
if (remoteMaps.msid2Quality[msid] === quality) {
|
595
|
|
- electedTrack = track;
|
|
678
|
+ electedTrack = tracks[i];
|
596
|
679
|
break;
|
597
|
680
|
}
|
598
|
681
|
}
|
|
682
|
+
|
|
683
|
+ // TODO(gp) if the initialQuality could not be satisfied, lower
|
|
684
|
+ // the requirement and try again.
|
599
|
685
|
}
|
600
|
686
|
}
|
601
|
687
|
|
|
@@ -604,6 +690,15 @@ function Simulcast() {
|
604
|
690
|
: stream;
|
605
|
691
|
};
|
606
|
692
|
|
|
693
|
+ var stream;
|
|
694
|
+
|
|
695
|
+ /**
|
|
696
|
+ * GUM for simulcast.
|
|
697
|
+ *
|
|
698
|
+ * @param constraints
|
|
699
|
+ * @param success
|
|
700
|
+ * @param err
|
|
701
|
+ */
|
607
|
702
|
Simulcast.prototype.getUserMedia = function (constraints, success, err) {
|
608
|
703
|
|
609
|
704
|
// TODO(gp) what if we request a resolution not supported by the hardware?
|
|
@@ -620,7 +715,10 @@ function Simulcast() {
|
620
|
715
|
|
621
|
716
|
if (config.enableSimulcast && !config.useNativeSimulcast) {
|
622
|
717
|
|
623
|
|
- // NOTE(gp) if we request the lq stream first webkitGetUserMedia fails randomly. Tested with Chrome 37.
|
|
718
|
+ // NOTE(gp) if we request the lq stream first webkitGetUserMedia
|
|
719
|
+ // fails randomly. Tested with Chrome 37. As fippo suggested, the
|
|
720
|
+ // reason appears to be that Chrome only acquires the cam once and
|
|
721
|
+ // then downscales the picture (https://code.google.com/p/chromium/issues/detail?id=346616#c11)
|
624
|
722
|
|
625
|
723
|
navigator.webkitGetUserMedia(constraints, function (hqStream) {
|
626
|
724
|
|
|
@@ -641,6 +739,7 @@ function Simulcast() {
|
641
|
739
|
localMaps.msids.splice(0, 0, lqStream.getVideoTracks()[0].id);
|
642
|
740
|
|
643
|
741
|
hqStream.addTrack(lqStream.getVideoTracks()[0]);
|
|
742
|
+ stream = hqStream;
|
644
|
743
|
success(hqStream);
|
645
|
744
|
}, err);
|
646
|
745
|
}, err);
|
|
@@ -656,18 +755,95 @@ function Simulcast() {
|
656
|
755
|
|
657
|
756
|
// add hq stream to local map
|
658
|
757
|
localMaps.msids.push(hqStream.getVideoTracks()[0].id);
|
659
|
|
-
|
|
758
|
+ stream = hqStream;
|
660
|
759
|
success(hqStream);
|
661
|
760
|
}, err);
|
662
|
761
|
}
|
663
|
762
|
};
|
664
|
763
|
|
665
|
|
- Simulcast.prototype.getRemoteVideoStreamIdBySSRC = function (primarySSRC) {
|
666
|
|
- return remoteMaps.ssrc2Msid[primarySSRC];
|
|
764
|
+ /**
|
|
765
|
+ * Gets the fully qualified msid (stream.id + track.id) associated to the
|
|
766
|
+ * SSRC.
|
|
767
|
+ *
|
|
768
|
+ * @param ssrc
|
|
769
|
+ * @returns {*}
|
|
770
|
+ */
|
|
771
|
+ Simulcast.prototype.getRemoteVideoStreamIdBySSRC = function (ssrc) {
|
|
772
|
+ return remoteMaps.ssrc2Msid[ssrc];
|
667
|
773
|
};
|
668
|
774
|
|
669
|
775
|
Simulcast.prototype.parseMedia = function (desc, mediatypes) {
|
670
|
776
|
var lines = desc.sdp.split('\r\n');
|
671
|
777
|
return this._parseMedia(lines, mediatypes);
|
672
|
778
|
};
|
|
779
|
+
|
|
780
|
+ Simulcast.prototype._startLocalVideoStream = function (ssrc) {
|
|
781
|
+ var trackid;
|
|
782
|
+
|
|
783
|
+ Object.keys(localMaps.msid2ssrc).some(function (tid) {
|
|
784
|
+ if (localMaps.msid2ssrc[tid] == ssrc)
|
|
785
|
+ {
|
|
786
|
+ trackid = tid;
|
|
787
|
+ return true;
|
|
788
|
+ }
|
|
789
|
+ });
|
|
790
|
+
|
|
791
|
+ stream.getVideoTracks().some(function(track) {
|
|
792
|
+ if (track.id === trackid) {
|
|
793
|
+ track.enabled = true;
|
|
794
|
+ return true;
|
|
795
|
+ }
|
|
796
|
+ });
|
|
797
|
+ };
|
|
798
|
+
|
|
799
|
+ Simulcast.prototype._stopLocalVideoStream = function (ssrc) {
|
|
800
|
+ var trackid;
|
|
801
|
+
|
|
802
|
+ Object.keys(localMaps.msid2ssrc).some(function (tid) {
|
|
803
|
+ if (localMaps.msid2ssrc[tid] == ssrc)
|
|
804
|
+ {
|
|
805
|
+ trackid = tid;
|
|
806
|
+ return true;
|
|
807
|
+ }
|
|
808
|
+ });
|
|
809
|
+
|
|
810
|
+ stream.getVideoTracks().some(function(track) {
|
|
811
|
+ if (track.id === trackid) {
|
|
812
|
+ track.enabled = false;
|
|
813
|
+ return true;
|
|
814
|
+ }
|
|
815
|
+ });
|
|
816
|
+ };
|
|
817
|
+
|
|
818
|
+ Simulcast.prototype.getLocalVideoStream = function() {
|
|
819
|
+ var track;
|
|
820
|
+
|
|
821
|
+ stream.getVideoTracks().some(function(t) {
|
|
822
|
+ if ((track = t).enabled) {
|
|
823
|
+ return true;
|
|
824
|
+ }
|
|
825
|
+ });
|
|
826
|
+
|
|
827
|
+ return new webkitMediaStream([track]);
|
|
828
|
+ };
|
|
829
|
+
|
|
830
|
+ $(document).bind('simulcastlayerschanged', function (event, endpointSimulcastLayers) {
|
|
831
|
+ endpointSimulcastLayers.forEach(function (esl) {
|
|
832
|
+ var ssrc = esl.simulcastLayer.primarySSRC;
|
|
833
|
+ var simulcast = new Simulcast();
|
|
834
|
+ simulcast._setReceivingVideoStream(ssrc);
|
|
835
|
+ });
|
|
836
|
+ });
|
|
837
|
+
|
|
838
|
+ $(document).bind('startsimulcastlayer', function(event, simulcastLayer) {
|
|
839
|
+ var ssrc = simulcastLayer.primarySSRC;
|
|
840
|
+ var simulcast = new Simulcast();
|
|
841
|
+ simulcast._startLocalVideoStream(ssrc);
|
|
842
|
+ });
|
|
843
|
+
|
|
844
|
+ $(document).bind('stopsimulcastlayer', function(event, simulcastLayer) {
|
|
845
|
+ var ssrc = simulcastLayer.primarySSRC;
|
|
846
|
+ var simulcast = new Simulcast();
|
|
847
|
+ simulcast._stopLocalVideoStream(ssrc);
|
|
848
|
+ });
|
673
|
849
|
}());
|