forked from otalk/jingle-media-session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
892 lines (734 loc) · 29.8 KB
/
index.js
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
// can add and remove stream but doing so every other time you publish it has an error.
var util = require('util');
var extend = require('extend-object');
var BaseSession = require('jingle-session');
var RTCPeerConnection = require('rtcpeerconnection');
var queue = require('queue');
function filterContentSources(content, stream) {
if (content.application.applicationType !== 'rtp') {
return;
}
delete content.transport;
delete content.application.payloads;
delete content.application.headerExtensions;
content.application.mux = false;
if (content.application.sources) {
content.application.sources = content.application.sources.filter(function (source) {
// if there's no msid, ignore it
if (source.parameters.length < 2) {
return false;
}
return (stream.id === source.parameters[1].value.split(' ')[0]
|| stream.label === source.parameters[1].value.split(' ')[0]);
});
}
// remove source groups not related to this stream
if (content.application.sourceGroups) {
content.application.sourceGroups = content.application.sourceGroups.filter(function (group) {
var found = false;
for (var i = 0; i < content.application.sources.length; i++) {
if (content.application.sources[i].ssrc === group.sources[0]) {
found = true;
break;
}
}
return found;
});
}
}
function filterMsidFromRecvonlySources(description) {
description.contents.forEach(function(content) {
content.application.sources.forEach(function(source) {
source.parameters = source.parameters.filter(function(param) {
if (param.key === 'msid' && (content.senders ==='initiator' || content.senders ==='none')) {
return false;
}
return true;
});
});
});
return description;
}
function getRtpContentsWithSources(content) {
return content && content.application && content.application.applicationType === 'rtp'
&& content.application.sources
&& content.application.sources.length;
}
function generateDifferenceOfSources(oldLocalDescription, newLocalDescription) {
const oldContents = oldLocalDescription.contents.filter(getRtpContentsWithSources);
const newContents = newLocalDescription.contents.filter(getRtpContentsWithSources);
const sourceMap = {};
const sourcesRemoved = [];
const sourcesModified = [];
//creating map of sources and directions
for (var i = 0; i < newContents.length; i++) {
for(var j = 0; j < newContents[i].application.sources.length; j++) {
sourceMap[newContents[i].application.sources[j].ssrc] = {
source: newContents[i].application.sources[j],
direction: newContents[i].senders,
};
}
};
for (var i = 0; i < oldContents.length; i++) {
for(var j = 0; j < oldContents[i].application.sources.length; j++) {
if (!sourceMap[oldContents[i].application.sources[j].ssrc]) {
// this IS a removed ssource
sourcesRemoved.push(oldContents[i].application.sources[j].ssrc);
}
else {
// this is a possible modified source
const oldContentHasMsid = sourceHasMsid(oldContents[i].application.sources[j]);
const oldContentSourceDirection = oldContents[i].senders;
const newContentHasMsid = sourceHasMsid(sourceMap[oldContents[i].application.sources[j].ssrc].source);
const newContentSourceDirection = sourceMap[oldContents[i].application.sources[j].ssrc].direction;
if ((newContentSourceDirection !== oldContentSourceDirection)) {
if (newContentSourceDirection === 'both') {
if (newContentHasMsid) {
sourcesModified.push(oldContents[i].application.sources[j].ssrc);
}
} else if (newContentSourceDirection === 'initiator') {
if (oldContentHasMsid) {
sourcesModified.push(oldContents[i].application.sources[j].ssrc);
}
} else if (newContentSourceDirection === 'responder') {
if (newContentHasMsid) {
sourcesModified.push(oldContents[i].application.sources[j].ssrc);
}
} else if (newContentSourceDirection === 'none') {
sourcesModified.push(oldContents[i].application.sources[j].ssrc);
}
}
}
delete sourceMap[oldContents[i].application.sources[j].ssrc];
}
}
const sourcesAdded = Object.keys(sourceMap);
return { sourcesRemoved: sourcesRemoved, sourcesAdded: sourcesAdded, sourcesModified: sourcesModified};
}
function getCleanedContentBlockRelatedToSSRCS(contents, ssrcList) {
const properContents = [];
for (var i = 0; i < contents.length; i++) {
const filteredSsrcs =
contents[i].application.sources.filter(function(source) {
return ssrcList.indexOf(source.ssrc) > -1;
});
if (filteredSsrcs.length) {
contents[i].application.sources = filteredSsrcs;
delete contents[i].transport;
delete contents[i].application.ssrc;
delete contents[i].application.payloads;
contents[i].application.mux = false;
delete contents[i].application.headerExtensions;
properContents.push(contents[i]);
}
}
return properContents;
}
function filterUnusedLabels(content) {
// Remove mslabel and label ssrc-specific attributes
var sources = content.application.sources || [];
sources.forEach(function (source) {
source.parameters = source.parameters.filter(function (parameter) {
return !(parameter.key === 'mslabel' || parameter.key === 'label');
});
});
}
function findMatchingSource(baseSource, compareSources) {
compareSources = compareSources || [];
for (var i = 0; i < compareSources.length; i++) {
var compareSource = compareSources[i];
if (baseSource.ssrc === compareSource.ssrc) {
return compareSource;
}
}
return null;
}
function sourceHasMsid(source) {
return source.parameters && source.parameters.some(function(param) { return param.key === 'msid'; });
}
function changeSendersIfNoMsids(content) {
if (!content.application) {
return;
}
// remove sources that are missing an msid (they are recvonly)
var sources = content.application.sources || [];
var hasSourcesWithMsids = sources.some(sourceHasMsid);
if (!hasSourcesWithMsids) {
content.senders = 'both';
}
}
function MediaSession(opts) {
BaseSession.call(this, opts);
var sdpSemantics = opts.parent && opts.parent.config && opts.parent.config.sdpSemantics;
this.pc = new RTCPeerConnection({
iceServers: opts.iceServers || [],
useJingle: true,
sdpSemantics: sdpSemantics
}, opts.constraints || {});
this.q = queue({
autostart: true,
concurrency: 1
});
this.pc.on('ice', this.onIceCandidate.bind(this, opts));
this.pc.on('endOfCandidates', this.onIceEndOfCandidates.bind(this, opts));
this.pc.on('iceConnectionStateChange', this.onIceStateChange.bind(this));
this.pc.on('addStream', this.onAddStream.bind(this));
this.pc.on('removeStream', this.onRemoveStream.bind(this));
this.pc.on('addChannel', this.onAddChannel.bind(this));
if (opts.stream) {
this.addStream(opts.stream);
}
this._ringing = false;
}
function queueOfferAnswer(self, errorMsg, jingleDesc, cb) {
self.q.push(function(qCb) {
function done(err, answer) {
qCb();
return (err ? cb(err) : cb(null, answer));
}
self.pc.handleOffer({
type: 'offer',
jingle: jingleDesc
}, function (err) {
if (err) {
self._log('error', 'Could not create offer for ' + errorMsg);
return done(err);
}
self.pc.answer(self.constraints, function (err, answer) {
if (err) {
self._log('error', 'Could not create answer for ' + errorMsg);
return done(err);
}
// call the remaing logic in the cb
done(null, answer);
});
});
});
}
function queueOffer(self, errorMsg, jingleDesc, cb) {
self.q.push(function(qCb) {
function done(err) {
qCb();
return (err ? cb(err) : cb(null));
}
self.pc.handleOffer({
type: 'offer',
jingle: jingleDesc
}, function (err) {
if (err) {
self._log('error', errorMsg);
return done(err);
}
// call the remaing logic in the cb
done();
});
});
}
function queueAnswer(self, errorMsg, jingleDesc, cb) {
self.q.push(function(qCb) {
function done(err, answer) {
qCb();
return (err ? cb(err) : cb(null, answer));
}
self.pc.handleAnswer({
type: 'answer',
jingle: jingleDesc
}, function (err) {
if (err) {
self._log('error', errorMsg);
return done(err);
}
// call the remaing logic in the cb
done();
});
});
}
util.inherits(MediaSession, BaseSession);
Object.defineProperties(MediaSession.prototype, {
ringing: {
get: function () {
return this._ringing;
},
set: function (value) {
if (value !== this._ringing) {
this._ringing = value;
this.emit('change:ringing', value);
}
}
},
streams: {
get: function () {
if (this.pc.signalingState !== 'closed') {
return this.pc.getRemoteStreams();
}
return [];
}
}
});
MediaSession.prototype = extend(MediaSession.prototype, {
// ----------------------------------------------------------------
// Session control methods
// ----------------------------------------------------------------
start: function (offerOptions, next) {
var self = this;
this.state = 'pending';
next = next || function () {};
this.pc.isInitiator = true;
self.q.push(function(qCb) {
self.pc.offer(offerOptions, function (err, offer) {
if (err) {
self._log('error', 'Could not create WebRTC offer', err);
return self.end('failed-application', true);
}
// a workaround for missing a=sendonly
// https://code.google.com/p/webrtc/issues/detail?id=1553
if (offerOptions && offerOptions.mandatory) {
offer.jingle.contents.forEach(function (content) {
var mediaType = content.application.media;
if (!content.description || content.application.applicationType !== 'rtp') {
return;
}
if (!offerOptions.mandatory.OfferToReceiveAudio && mediaType === 'audio') {
content.senders = 'initiator';
}
if (!offerOptions.mandatory.OfferToReceiveVideo && mediaType === 'video') {
content.senders = 'initiator';
}
});
}
offer.jingle.contents.forEach(filterUnusedLabels);
self.send('session-initiate', offer.jingle);
next();
qCb();
});
});
},
accept: function (opts, next) {
var self = this;
// support calling with accept(next) or accept(opts, next)
if (arguments.length === 1 && typeof opts === 'function') {
next = opts;
opts = {};
}
next = next || function () {};
opts = opts || {};
self.constraints = opts.constraints || {
mandatory: {
OfferToReceiveAudio: true,
OfferToReceiveVideo: true
}
};
self._log('info', 'Accepted incoming session');
self.state = 'active';
self.q.push(function(qCb) {
self.pc.answer(self.constraints, function (err, answer) {
if (err) {
self._log('error', 'Could not create WebRTC answer', err);
return self.end('failed-application');
}
answer.jingle.contents.forEach(filterUnusedLabels);
answer.jingle.contents.forEach(changeSendersIfNoMsids);
self.send('session-accept', answer.jingle);
next();
qCb();
});
});
},
end: function (reason, silent) {
var self = this;
this.streams.forEach(function (stream) {
self.onRemoveStream({stream: stream});
});
this.pc.close();
BaseSession.prototype.end.call(this, reason, silent);
},
ring: function () {
this._log('info', 'Ringing on incoming session');
this.ringing = true;
this.send('session-info', {ringing: true});
},
mute: function (creator, name) {
this._log('info', 'Muting', name);
this.send('session-info', {
mute: {
creator: creator,
name: name
}
});
},
unmute: function (creator, name) {
this._log('info', 'Unmuting', name);
this.send('session-info', {
unmute: {
creator: creator,
name: name
}
});
},
hold: function () {
this._log('info', 'Placing on hold');
this.send('session-info', {hold: true});
},
resume: function () {
this._log('info', 'Resuming from hold');
this.send('session-info', {active: true});
},
// ----------------------------------------------------------------
// Stream control methods
// ----------------------------------------------------------------
addStream: function (stream, renegotiate, cb) {
var self = this;
var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
cb = cb || function () {};
this.pc.addStream(stream);
if (!renegotiate) {
return;
} else if (typeof renegotiate === 'object') {
self.constraints = renegotiate;
}
var errorMsg = 'adding new stream';
queueOfferAnswer(this, errorMsg, self.pc.remoteDescription, function(err, answer) {
if (err) {
self._log('error', 'Could not create offer for ' + errorMsg);
return cb(err);
}
var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription);
return cb();
});
},
addStream2: function (stream, cb) {
this.addStream(stream, true, cb);
},
removeStream: function (stream, renegotiate, cb) {
var self = this;
var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
cb = cb || function () {};
if (!renegotiate) {
this.pc.removeStream(stream);
return;
} else if (typeof renegotiate === 'object') {
self.constraints = renegotiate;
}
this.pc.removeStream(stream);
var errorMsg = 'removing stream';
queueOfferAnswer(self, errorMsg, this.pc.remoteDescription, function(err) {
if (err) {
return cb(err);
}
var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription);
cb();
});
},
removeStream2: function (stream, cb) {
this.removeStream(stream, true, cb);
},
// Justin's new functions
_determineDifferencesAndSignal: function(oldLocalDescription, newLocalDescription) {
const oldDescCopy = JSON.parse(JSON.stringify(oldLocalDescription));
const newDescCopy = JSON.parse(JSON.stringify(newLocalDescription));
const diffObject = generateDifferenceOfSources(oldDescCopy, newDescCopy);
this._signalDifferenceInSources({sourcesRemoved: diffObject.sourcesRemoved, sourcesAdded: diffObject.sourcesAdded, sourcesModified: diffObject.sourcesModified,
oldLocalDescription: oldDescCopy, newLocalDescription: newDescCopy });
},
_signalDifferenceInSources: function(diffObject){
const oldContents = diffObject.oldLocalDescription.contents.filter(getRtpContentsWithSources);
const newContents = diffObject.newLocalDescription.contents.filter(getRtpContentsWithSources);
delete diffObject.oldLocalDescription.groups;
delete diffObject.newLocalDescription.groups;
if (diffObject.sourcesAdded.length) {
diffObject.newLocalDescription.contents = getCleanedContentBlockRelatedToSSRCS(newContents, diffObject.sourcesAdded);
const filteredNewDesc = filterMsidFromRecvonlySources(diffObject.newLocalDescription);
this._log('info', 'sending source add', diffObject.newLocalDescription);
this.send('source-add', filteredNewDesc);
}
if (diffObject.sourcesRemoved.length) {
diffObject.oldLocalDescription.contents = getCleanedContentBlockRelatedToSSRCS(oldContents, diffObject.sourcesRemoved);
const filteredDesc = filterMsidFromRecvonlySources(diffObject.oldLocalDescription);
this._log('info', 'sending source remove', diffObject.oldLocalDescription);
this.send('source-remove', filteredDesc);
}
if (diffObject.sourcesModified.length) {
diffObject.oldLocalDescription.contents = getCleanedContentBlockRelatedToSSRCS(oldContents, diffObject.sourcesModified);
diffObject.newLocalDescription.contents = getCleanedContentBlockRelatedToSSRCS(newContents, diffObject.sourcesModified);;
const filteredDesc = filterMsidFromRecvonlySources(diffObject.oldLocalDescription);
const filteredNewDesc = filterMsidFromRecvonlySources(diffObject.newLocalDescription);
this._log('info', 'sending source remove', filteredDesc);
this.send('source-remove', filteredDesc);
// Hack. Do not send the bridge messages for the same channel too fast. Remove once
// bridge has been fixed
var that = this;
setTimeout(function() {
that._log('info', 'sending source add', filteredNewDesc);
that.send('source-add', filteredNewDesc);
}, 250);
}
},
switchStream: function (oldStream, newStream, cb) {
var self = this;
cb = cb || function () {};
var desc = this.pc.localDescription;
desc.contents.forEach(function (content) {
delete content.transport;
delete content.application.payloads;
});
this.pc.removeStream(oldStream);
this.send('source-remove', desc);
this.pc.addStream(newStream);
var errorMsg = 'switching streams';
queueOfferAnswer(self, errorMsg, this.pc.remoteDescription, function(err, answer) {
if (err) {
self._log('error', 'Could not create offer for ' + errorMsg);
return cb(err);
}
answer.jingle.contents.forEach(function (content) {
delete content.transport;
delete content.application.payloads;
});
self.send('source-add', answer.jingle);
return err ? cb(err) : cb();
});
},
// ----------------------------------------------------------------
// ICE action handers
// ----------------------------------------------------------------
onIceCandidate: function (opts, candidate) {
this._log('info', 'Discovered new ICE candidate', candidate.jingle);
this.send('transport-info', candidate.jingle);
if (opts.signalEndOfCandidates) {
this.lastCandidate = candidate;
}
},
onIceEndOfCandidates: function (opts) {
this._log('info', 'ICE end of candidates');
if (opts.signalEndOfCandidates) {
var endOfCandidates = this.lastCandidate.jingle;
endOfCandidates.contents[0].transport = {
transportType: endOfCandidates.contents[0].transport.transportType,
gatheringComplete: true
};
this.lastCandidate = null;
this.send('transport-info', endOfCandidates);
}
},
onIceStateChange: function () {
switch (this.pc.iceConnectionState) {
case 'checking':
this.connectionState = 'connecting';
break;
case 'completed':
case 'connected':
this.connectionState = 'connected';
break;
case 'disconnected':
if (this.pc.signalingState === 'stable') {
this.connectionState = 'interrupted';
} else {
this.connectionState = 'disconnected';
}
break;
case 'failed':
this.connectionState = 'failed';
this.end('failed-transport');
break;
case 'closed':
this.connectionState = 'disconnected';
break;
}
},
// ----------------------------------------------------------------
// Stream event handlers
// ----------------------------------------------------------------
onAddStream: function (event) {
this._log('info', 'Stream added');
this.emit('peerStreamAdded', this, event.stream);
},
onRemoveStream: function (event) {
this._log('info', 'Stream removed');
this.emit('peerStreamRemoved', this, event.stream);
},
// ----------------------------------------------------------------
// Jingle action handers
// ----------------------------------------------------------------
onSessionInitiate: function (changes, cb) {
this._log('info', 'Initiating incoming session');
this.state = 'pending';
this.pc.isInitiator = false;
var errorMsg = 'Could not create WebRTC answer';
queueOffer(this, errorMsg, changes, function(err) {
return err ? cb({condition: 'general-error'}) : cb();
});
},
onSessionAccept: function (changes, cb) {
var self = this;
this.state = 'active';
var errorMsg = 'Could not process WebRTC answer';
queueAnswer(this, errorMsg, changes, function(err) {
if (err) {
return cb({condition: 'general-error'});
}
self.emit('accepted', self);
cb();
});
},
onSessionTerminate: function (changes, cb) {
var self = this;
this._log('info', 'Terminating session');
this.streams.forEach(function (stream) {
self.onRemoveStream({stream: stream});
});
this.pc.close();
BaseSession.prototype.end.call(this, changes.reason, true);
cb();
},
onSessionInfo: function (info, cb) {
if (info.ringing) {
this._log('info', 'Outgoing session is ringing');
this.ringing = true;
this.emit('ringing', this);
return cb();
}
if (info.hold) {
this._log('info', 'On hold');
this.emit('hold', this);
return cb();
}
if (info.active) {
this._log('info', 'Resuming from hold');
this.emit('resumed', this);
return cb();
}
if (info.mute) {
this._log('info', 'Muting', info.mute);
this.emit('mute', this, info.mute);
return cb();
}
if (info.unmute) {
this._log('info', 'Unmuting', info.unmute);
this.emit('unmute', this, info.unmute);
return cb();
}
cb();
},
onTransportInfo: function (changes, cb) {
var self = this;
self.q.push(function(qCb) {
function done() {
qCb();
return cb();
}
self.pc.processIce(changes, function () {
done();
});
});
},
onSourceAdd: function (changes, cb) {
var self = this;
this._log('info', 'Adding new stream source');
var newDesc = this.pc.remoteDescription;
this.pc.remoteDescription.contents.forEach(function (content, idx) {
var desc = content.application;
var ssrcs = desc && desc.sources || [];
var groups = desc && desc.sourceGroups || [];
if (!changes.contents) {
return;
}
changes.contents.forEach(function (newContent) {
if (content.name !== newContent.name) {
return;
}
var newContentDesc = newContent.application;
var newSSRCs = newContentDesc.sources || [];
ssrcs = ssrcs.concat(newSSRCs);
newDesc.contents[idx].application.sources = JSON.parse(JSON.stringify(ssrcs));
var newGroups = newContentDesc.sourceGroups || [];
groups = groups.concat(newGroups);
newDesc.contents[idx].application.sourceGroups = JSON.parse(JSON.stringify(groups));
});
});
var errorMsg = 'adding new stream source';
var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
queueOfferAnswer(self, errorMsg, newDesc, function(err) {
if (err) {
return cb({condition: 'general-error'});
}
var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription, true);
return cb();
});
},
onSourceRemove: function (changes, cb) {
var self = this;
this._log('info', 'Removing stream source');
var newDesc = this.pc.remoteDescription;
this.pc.remoteDescription.contents.forEach(function (content, idx) {
var desc = content.application;
var ssrcs = desc && desc.sources || [];
var groups = desc && desc.sourceGroups || [];
changes.contents.forEach(function (newContent) {
if (content.name !== newContent.name) {
return;
}
var newContentDesc = newContent.application;
var newSSRCs = newContentDesc.sources || [];
var newGroups = newContentDesc.sourceGroups || [];
var found, i, j, k;
for (i = 0; i < newSSRCs.length; i++) {
found = -1;
for (j = 0; j < ssrcs.length; j++) {
if (newSSRCs[i].ssrc === ssrcs[j].ssrc) {
found = j;
break;
}
}
if (found > -1) {
ssrcs.splice(found, 1);
newDesc.contents[idx].application.sources = JSON.parse(JSON.stringify(ssrcs));
}
}
// Remove ssrc-groups that are no longer needed
for (i = 0; i < newGroups.length; i++) {
found = -1;
for (j = 0; j < groups.length; j++) {
if (newGroups[i].semantics === groups[j].semantics &&
newGroups[i].sources.length === groups[j].sources.length) {
var same = true;
for (k = 0; k < newGroups[i].sources.length; k++) {
if (newGroups[i].sources[k] !== groups[j].sources[k]) {
same = false;
break;
}
}
if (same) {
found = j;
break;
}
}
}
if (found > -1) {
groups.splice(found, 1);
newDesc.contents[idx].application.sourceGroups = JSON.parse(JSON.stringify(groups));
}
}
});
});
var errorMsg = 'removing stream source';
var oldLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
queueOfferAnswer(this, errorMsg, newDesc, function(err) {
if (err) {
return cb({condition: 'general-error'});
}
var newLocalDescription = JSON.parse(JSON.stringify(self.pc.localDescription));
self._determineDifferencesAndSignal(oldLocalDescription, newLocalDescription);
return cb();
});
},
// ----------------------------------------------------------------
// DataChannels
// ----------------------------------------------------------------
onAddChannel: function (channel) {
this.emit('addChannel', channel);
}
});
module.exports = MediaSession;