|
|
@@ -0,0 +1,669 @@
|
|
|
1
|
+/*jslint plusplus: true */
|
|
|
2
|
+/*jslint nomen: true*/
|
|
|
3
|
+
|
|
|
4
|
+/**
|
|
|
5
|
+ * Created by gp on 11/08/14.
|
|
|
6
|
+ */
|
|
|
7
|
+function Simulcast() {
|
|
|
8
|
+ "use strict";
|
|
|
9
|
+
|
|
|
10
|
+ // TODO(gp) split the Simulcast class in two classes : NativeSimulcast and ClassicSimulcast.
|
|
|
11
|
+ this.debugLvl = 1;
|
|
|
12
|
+}
|
|
|
13
|
+
|
|
|
14
|
+(function () {
|
|
|
15
|
+ "use strict";
|
|
|
16
|
+ // global state for all transformers.
|
|
|
17
|
+ var localExplosionMap = {}, localVideoSourceCache, emptyCompoundIndex,
|
|
|
18
|
+ remoteMaps = {
|
|
|
19
|
+ msid2Quality: {},
|
|
|
20
|
+ ssrc2Msid: {},
|
|
|
21
|
+ receivingVideoStreams: {}
|
|
|
22
|
+ }, localMaps = {
|
|
|
23
|
+ msids: [],
|
|
|
24
|
+ msid2ssrc: {}
|
|
|
25
|
+ };
|
|
|
26
|
+
|
|
|
27
|
+ Simulcast.prototype._generateGuid = (function () {
|
|
|
28
|
+ function s4() {
|
|
|
29
|
+ return Math.floor((1 + Math.random()) * 0x10000)
|
|
|
30
|
+ .toString(16)
|
|
|
31
|
+ .substring(1);
|
|
|
32
|
+ }
|
|
|
33
|
+
|
|
|
34
|
+ return function () {
|
|
|
35
|
+ return s4() + s4() + '-' + s4() + '-' + s4() + '-' +
|
|
|
36
|
+ s4() + '-' + s4() + s4() + s4();
|
|
|
37
|
+ };
|
|
|
38
|
+ }());
|
|
|
39
|
+
|
|
|
40
|
+ Simulcast.prototype._cacheVideoSources = function (lines) {
|
|
|
41
|
+ localVideoSourceCache = this._getVideoSources(lines);
|
|
|
42
|
+ };
|
|
|
43
|
+
|
|
|
44
|
+ Simulcast.prototype._restoreVideoSources = function (lines) {
|
|
|
45
|
+ this._replaceVideoSources(lines, localVideoSourceCache);
|
|
|
46
|
+ };
|
|
|
47
|
+
|
|
|
48
|
+ Simulcast.prototype._replaceVideoSources = function (lines, videoSources) {
|
|
|
49
|
+
|
|
|
50
|
+ var i, inVideo = false, index = -1, howMany = 0;
|
|
|
51
|
+
|
|
|
52
|
+ if (this.debugLvl) {
|
|
|
53
|
+ console.info('Replacing video sources...');
|
|
|
54
|
+ }
|
|
|
55
|
+
|
|
|
56
|
+ for (i = 0; i < lines.length; i++) {
|
|
|
57
|
+ if (inVideo && lines[i].substring(0, 'm='.length) === 'm=') {
|
|
|
58
|
+ // Out of video.
|
|
|
59
|
+ break;
|
|
|
60
|
+ }
|
|
|
61
|
+
|
|
|
62
|
+ if (!inVideo && lines[i].substring(0, 'm=video '.length) === 'm=video ') {
|
|
|
63
|
+ // In video.
|
|
|
64
|
+ inVideo = true;
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ if (inVideo && (lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:'
|
|
|
68
|
+ || lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:')) {
|
|
|
69
|
+
|
|
|
70
|
+ if (index === -1) {
|
|
|
71
|
+ index = i;
|
|
|
72
|
+ }
|
|
|
73
|
+
|
|
|
74
|
+ howMany++;
|
|
|
75
|
+ }
|
|
|
76
|
+ }
|
|
|
77
|
+
|
|
|
78
|
+ // efficiency baby ;)
|
|
|
79
|
+ lines.splice.apply(lines,
|
|
|
80
|
+ [index, howMany].concat(videoSources));
|
|
|
81
|
+
|
|
|
82
|
+ };
|
|
|
83
|
+
|
|
|
84
|
+ Simulcast.prototype._getVideoSources = function (lines) {
|
|
|
85
|
+ var i, inVideo = false, sb = [];
|
|
|
86
|
+
|
|
|
87
|
+ if (this.debugLvl) {
|
|
|
88
|
+ console.info('Getting video sources...');
|
|
|
89
|
+ }
|
|
|
90
|
+
|
|
|
91
|
+ for (i = 0; i < lines.length; i++) {
|
|
|
92
|
+ if (inVideo && lines[i].substring(0, 'm='.length) === 'm=') {
|
|
|
93
|
+ // Out of video.
|
|
|
94
|
+ break;
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ if (!inVideo && lines[i].substring(0, 'm=video '.length) === 'm=video ') {
|
|
|
98
|
+ // In video.
|
|
|
99
|
+ inVideo = true;
|
|
|
100
|
+ }
|
|
|
101
|
+
|
|
|
102
|
+ if (inVideo && lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:') {
|
|
|
103
|
+ // In SSRC.
|
|
|
104
|
+ sb.push(lines[i]);
|
|
|
105
|
+ }
|
|
|
106
|
+
|
|
|
107
|
+ if (inVideo && lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:') {
|
|
|
108
|
+ sb.push(lines[i]);
|
|
|
109
|
+ }
|
|
|
110
|
+ }
|
|
|
111
|
+
|
|
|
112
|
+ return sb;
|
|
|
113
|
+ };
|
|
|
114
|
+
|
|
|
115
|
+ Simulcast.prototype._parseMedia = function (lines, mediatypes) {
|
|
|
116
|
+ var i, res = [], type, cur_media, idx, ssrcs, cur_ssrc, ssrc,
|
|
|
117
|
+ ssrc_attribute, group, semantics, skip;
|
|
|
118
|
+
|
|
|
119
|
+ if (this.debugLvl) {
|
|
|
120
|
+ console.info('Parsing media sources...');
|
|
|
121
|
+ }
|
|
|
122
|
+
|
|
|
123
|
+ for (i = 0; i < lines.length; i++) {
|
|
|
124
|
+ if (lines[i].substring(0, 'm='.length) === 'm=') {
|
|
|
125
|
+
|
|
|
126
|
+ type = lines[i]
|
|
|
127
|
+ .substr('m='.length, lines[i].indexOf(' ') - 'm='.length);
|
|
|
128
|
+ skip = mediatypes !== undefined && mediatypes.indexOf(type) === -1;
|
|
|
129
|
+
|
|
|
130
|
+ if (!skip) {
|
|
|
131
|
+ cur_media = {
|
|
|
132
|
+ 'type': type,
|
|
|
133
|
+ 'sources': {},
|
|
|
134
|
+ 'groups': []
|
|
|
135
|
+ };
|
|
|
136
|
+
|
|
|
137
|
+ res.push(cur_media);
|
|
|
138
|
+ }
|
|
|
139
|
+
|
|
|
140
|
+ } else if (!skip && lines[i].substring(0, 'a=ssrc:'.length) === 'a=ssrc:') {
|
|
|
141
|
+
|
|
|
142
|
+ idx = lines[i].indexOf(' ');
|
|
|
143
|
+ ssrc = lines[i].substring('a=ssrc:'.length, idx);
|
|
|
144
|
+ if (cur_media.sources[ssrc] === undefined) {
|
|
|
145
|
+ cur_ssrc = {'ssrc': ssrc};
|
|
|
146
|
+ cur_media.sources[ssrc] = cur_ssrc;
|
|
|
147
|
+ }
|
|
|
148
|
+
|
|
|
149
|
+ ssrc_attribute = lines[i].substr(idx + 1).split(':', 2)[0];
|
|
|
150
|
+ cur_ssrc[ssrc_attribute] = lines[i].substr(idx + 1).split(':', 2)[1];
|
|
|
151
|
+
|
|
|
152
|
+ if (cur_media.base === undefined) {
|
|
|
153
|
+ cur_media.base = cur_ssrc;
|
|
|
154
|
+ }
|
|
|
155
|
+
|
|
|
156
|
+ } else if (!skip && lines[i].substring(0, 'a=ssrc-group:'.length) === 'a=ssrc-group:') {
|
|
|
157
|
+ idx = lines[i].indexOf(' ');
|
|
|
158
|
+ semantics = lines[i].substr(0, idx).substr('a=ssrc-group:'.length);
|
|
|
159
|
+ ssrcs = lines[i].substr(idx).trim().split(' ');
|
|
|
160
|
+ group = {
|
|
|
161
|
+ 'semantics': semantics,
|
|
|
162
|
+ 'ssrcs': ssrcs
|
|
|
163
|
+ };
|
|
|
164
|
+ cur_media.groups.push(group);
|
|
|
165
|
+ } else if (!skip && (lines[i].substring(0, 'a=sendrecv'.length) === 'a=sendrecv' ||
|
|
|
166
|
+ lines[i].substring(0, 'a=recvonly'.length) === 'a=recvonly' ||
|
|
|
167
|
+ lines[i].substring(0, 'a=sendonly'.length) === 'a=sendonly' ||
|
|
|
168
|
+ lines[i].substring(0, 'a=inactive'.length) === 'a=inactive')) {
|
|
|
169
|
+
|
|
|
170
|
+ cur_media.direction = lines[i].substring('a='.length, 8);
|
|
|
171
|
+ }
|
|
|
172
|
+ }
|
|
|
173
|
+
|
|
|
174
|
+ return res;
|
|
|
175
|
+ };
|
|
|
176
|
+
|
|
|
177
|
+ // Returns a random integer between min (included) and max (excluded)
|
|
|
178
|
+ // Using Math.round() will give you a non-uniform distribution!
|
|
|
179
|
+ Simulcast.prototype._generateRandomSSRC = function () {
|
|
|
180
|
+ var min = 0, max = 0xffffffff;
|
|
|
181
|
+ return Math.floor(Math.random() * (max - min)) + min;
|
|
|
182
|
+ };
|
|
|
183
|
+
|
|
|
184
|
+ function CompoundIndex(obj) {
|
|
|
185
|
+ if (obj !== undefined) {
|
|
|
186
|
+ this.row = obj.row;
|
|
|
187
|
+ this.column = obj.column;
|
|
|
188
|
+ }
|
|
|
189
|
+ }
|
|
|
190
|
+
|
|
|
191
|
+ emptyCompoundIndex = new CompoundIndex();
|
|
|
192
|
+
|
|
|
193
|
+ Simulcast.prototype._indexOfArray = function (needle, haystack, start) {
|
|
|
194
|
+ var length = haystack.length, idx, i;
|
|
|
195
|
+
|
|
|
196
|
+ if (!start) {
|
|
|
197
|
+ start = 0;
|
|
|
198
|
+ }
|
|
|
199
|
+
|
|
|
200
|
+ for (i = start; i < length; i++) {
|
|
|
201
|
+ idx = haystack[i].indexOf(needle);
|
|
|
202
|
+ if (idx !== -1) {
|
|
|
203
|
+ return new CompoundIndex({row: i, column: idx});
|
|
|
204
|
+ }
|
|
|
205
|
+ }
|
|
|
206
|
+ return emptyCompoundIndex;
|
|
|
207
|
+ };
|
|
|
208
|
+
|
|
|
209
|
+ Simulcast.prototype._removeSimulcastGroup = function (lines) {
|
|
|
210
|
+ var i;
|
|
|
211
|
+
|
|
|
212
|
+ for (i = lines.length - 1; i >= 0; i--) {
|
|
|
213
|
+ if (lines[i].indexOf('a=ssrc-group:SIM') !== -1) {
|
|
|
214
|
+ lines.splice(i, 1);
|
|
|
215
|
+ }
|
|
|
216
|
+ }
|
|
|
217
|
+ };
|
|
|
218
|
+
|
|
|
219
|
+ Simulcast.prototype._explodeLocalSimulcastSources = function (lines) {
|
|
|
220
|
+ var sb, msid, sid, tid, videoSources, self;
|
|
|
221
|
+
|
|
|
222
|
+ if (this.debugLvl) {
|
|
|
223
|
+ console.info('Exploding local video sources...');
|
|
|
224
|
+ }
|
|
|
225
|
+
|
|
|
226
|
+ videoSources = this._parseMedia(lines, ['video'])[0];
|
|
|
227
|
+
|
|
|
228
|
+ self = this;
|
|
|
229
|
+ if (videoSources.groups && videoSources.groups.length !== 0) {
|
|
|
230
|
+ videoSources.groups.forEach(function (group) {
|
|
|
231
|
+ if (group.semantics === 'SIM') {
|
|
|
232
|
+ group.ssrcs.forEach(function (ssrc) {
|
|
|
233
|
+
|
|
|
234
|
+ // Get the msid for this ssrc..
|
|
|
235
|
+ if (localExplosionMap[ssrc]) {
|
|
|
236
|
+ // .. either from the explosion map..
|
|
|
237
|
+ msid = localExplosionMap[ssrc];
|
|
|
238
|
+ } else {
|
|
|
239
|
+ // .. or generate a new one (msid).
|
|
|
240
|
+ sid = videoSources.sources[ssrc].msid
|
|
|
241
|
+ .substring(0, videoSources.sources[ssrc].msid.indexOf(' '));
|
|
|
242
|
+
|
|
|
243
|
+ tid = self._generateGuid();
|
|
|
244
|
+ msid = [sid, tid].join(' ');
|
|
|
245
|
+ localExplosionMap[ssrc] = msid;
|
|
|
246
|
+ }
|
|
|
247
|
+
|
|
|
248
|
+ // Assign it to the source object.
|
|
|
249
|
+ videoSources.sources[ssrc].msid = msid;
|
|
|
250
|
+
|
|
|
251
|
+ // TODO(gp) Change the msid of associated sources.
|
|
|
252
|
+ });
|
|
|
253
|
+ }
|
|
|
254
|
+ });
|
|
|
255
|
+ }
|
|
|
256
|
+
|
|
|
257
|
+ sb = this._compileVideoSources(videoSources);
|
|
|
258
|
+
|
|
|
259
|
+ this._replaceVideoSources(lines, sb);
|
|
|
260
|
+ };
|
|
|
261
|
+
|
|
|
262
|
+ Simulcast.prototype._groupLocalVideoSources = function (lines) {
|
|
|
263
|
+ var sb, videoSources, ssrcs = [], ssrc;
|
|
|
264
|
+
|
|
|
265
|
+ if (this.debugLvl) {
|
|
|
266
|
+ console.info('Grouping local video sources...');
|
|
|
267
|
+ }
|
|
|
268
|
+
|
|
|
269
|
+ videoSources = this._parseMedia(lines, ['video'])[0];
|
|
|
270
|
+
|
|
|
271
|
+ for (ssrc in videoSources.sources) {
|
|
|
272
|
+ // jitsi-meet destroys/creates streams at various places causing
|
|
|
273
|
+ // the original local stream ids to change. The only thing that
|
|
|
274
|
+ // remains unchanged is the trackid.
|
|
|
275
|
+ localMaps.msid2ssrc[videoSources.sources[ssrc].msid.split(' ')[1]] = ssrc;
|
|
|
276
|
+ }
|
|
|
277
|
+
|
|
|
278
|
+ // TODO(gp) add only "free" sources.
|
|
|
279
|
+ localMaps.msids.forEach(function (msid) {
|
|
|
280
|
+ ssrcs.push(localMaps.msid2ssrc[msid]);
|
|
|
281
|
+ });
|
|
|
282
|
+
|
|
|
283
|
+ if (!videoSources.groups) {
|
|
|
284
|
+ videoSources.groups = [];
|
|
|
285
|
+ }
|
|
|
286
|
+
|
|
|
287
|
+ videoSources.groups.push({
|
|
|
288
|
+ 'semantics': 'SIM',
|
|
|
289
|
+ 'ssrcs': ssrcs
|
|
|
290
|
+ });
|
|
|
291
|
+
|
|
|
292
|
+ sb = this._compileVideoSources(videoSources);
|
|
|
293
|
+
|
|
|
294
|
+ this._replaceVideoSources(lines, sb);
|
|
|
295
|
+ };
|
|
|
296
|
+
|
|
|
297
|
+ Simulcast.prototype._appendSimulcastGroup = function (lines) {
|
|
|
298
|
+ var videoSources, ssrcGroup, simSSRC, numOfSubs = 3, i, sb, msid;
|
|
|
299
|
+
|
|
|
300
|
+ if (this.debugLvl) {
|
|
|
301
|
+ console.info('Appending simulcast group...');
|
|
|
302
|
+ }
|
|
|
303
|
+
|
|
|
304
|
+ // Get the primary SSRC information.
|
|
|
305
|
+ videoSources = this._parseMedia(lines, ['video'])[0];
|
|
|
306
|
+
|
|
|
307
|
+ // Start building the SIM SSRC group.
|
|
|
308
|
+ ssrcGroup = ['a=ssrc-group:SIM'];
|
|
|
309
|
+
|
|
|
310
|
+ // The video source buffer.
|
|
|
311
|
+ sb = [];
|
|
|
312
|
+
|
|
|
313
|
+ // Create the simulcast sub-streams.
|
|
|
314
|
+ for (i = 0; i < numOfSubs; i++) {
|
|
|
315
|
+ // TODO(gp) prevent SSRC collision.
|
|
|
316
|
+ simSSRC = this._generateRandomSSRC();
|
|
|
317
|
+ ssrcGroup.push(simSSRC);
|
|
|
318
|
+
|
|
|
319
|
+ sb.splice.apply(sb, [sb.length, 0].concat(
|
|
|
320
|
+ [["a=ssrc:", simSSRC, " cname:", videoSources.base.cname].join(''),
|
|
|
321
|
+ ["a=ssrc:", simSSRC, " msid:", videoSources.base.msid].join('')]
|
|
|
322
|
+ ));
|
|
|
323
|
+
|
|
|
324
|
+ if (this.debugLvl) {
|
|
|
325
|
+ console.info(['Generated substream ', i, ' with SSRC ', simSSRC, '.'].join(''));
|
|
|
326
|
+ }
|
|
|
327
|
+ }
|
|
|
328
|
+
|
|
|
329
|
+ // Add the group sim layers.
|
|
|
330
|
+ sb.splice(0, 0, ssrcGroup.join(' '))
|
|
|
331
|
+
|
|
|
332
|
+ this._replaceVideoSources(lines, sb);
|
|
|
333
|
+ };
|
|
|
334
|
+
|
|
|
335
|
+ // Does the actual patching.
|
|
|
336
|
+ Simulcast.prototype._ensureSimulcastGroup = function (lines) {
|
|
|
337
|
+ if (this.debugLvl) {
|
|
|
338
|
+ console.info('Ensuring simulcast group...');
|
|
|
339
|
+ }
|
|
|
340
|
+
|
|
|
341
|
+ if (this._indexOfArray('a=ssrc-group:SIM', lines) === emptyCompoundIndex) {
|
|
|
342
|
+ this._appendSimulcastGroup(lines);
|
|
|
343
|
+ this._cacheVideoSources(lines);
|
|
|
344
|
+ } else {
|
|
|
345
|
+ // verify that the ssrcs participating in the SIM group are present
|
|
|
346
|
+ // in the SDP (needed for presence).
|
|
|
347
|
+ this._restoreVideoSources(lines);
|
|
|
348
|
+ }
|
|
|
349
|
+ };
|
|
|
350
|
+
|
|
|
351
|
+ Simulcast.prototype._ensureGoogConference = function (lines) {
|
|
|
352
|
+ var sb;
|
|
|
353
|
+ if (this.debugLvl) {
|
|
|
354
|
+ console.info('Ensuring x-google-conference flag...')
|
|
|
355
|
+ }
|
|
|
356
|
+
|
|
|
357
|
+ if (this._indexOfArray('a=x-google-flag:conference', lines) === emptyCompoundIndex) {
|
|
|
358
|
+ // Add the google conference flag
|
|
|
359
|
+ sb = this._getVideoSources(lines);
|
|
|
360
|
+ sb = ['a=x-google-flag:conference'].concat(sb);
|
|
|
361
|
+ this._replaceVideoSources(lines, sb);
|
|
|
362
|
+ }
|
|
|
363
|
+ };
|
|
|
364
|
+
|
|
|
365
|
+ Simulcast.prototype._compileVideoSources = function (videoSources) {
|
|
|
366
|
+ var sb = [], ssrc, addedSSRCs = [];
|
|
|
367
|
+
|
|
|
368
|
+ if (this.debugLvl) {
|
|
|
369
|
+ console.info('Compiling video sources...');
|
|
|
370
|
+ }
|
|
|
371
|
+
|
|
|
372
|
+ // Add the groups
|
|
|
373
|
+ if (videoSources.groups && videoSources.groups.length !== 0) {
|
|
|
374
|
+ videoSources.groups.forEach(function (group) {
|
|
|
375
|
+ if (group.ssrcs && group.ssrcs.length !== 0) {
|
|
|
376
|
+ sb.push([['a=ssrc-group:', group.semantics].join(''), group.ssrcs.join(' ')].join(' '));
|
|
|
377
|
+
|
|
|
378
|
+ // if (group.semantics !== 'SIM') {
|
|
|
379
|
+ group.ssrcs.forEach(function (ssrc) {
|
|
|
380
|
+ addedSSRCs.push(ssrc);
|
|
|
381
|
+ sb.splice.apply(sb, [sb.length, 0].concat([
|
|
|
382
|
+ ["a=ssrc:", ssrc, " cname:", videoSources.sources[ssrc].cname].join(''),
|
|
|
383
|
+ ["a=ssrc:", ssrc, " msid:", videoSources.sources[ssrc].msid].join('')]));
|
|
|
384
|
+ });
|
|
|
385
|
+ //}
|
|
|
386
|
+ }
|
|
|
387
|
+ });
|
|
|
388
|
+ }
|
|
|
389
|
+
|
|
|
390
|
+ // Then add any free sources.
|
|
|
391
|
+ if (videoSources.sources) {
|
|
|
392
|
+ for (ssrc in videoSources.sources) {
|
|
|
393
|
+ if (addedSSRCs.indexOf(ssrc) === -1) {
|
|
|
394
|
+ sb.splice.apply(sb, [sb.length, 0].concat([
|
|
|
395
|
+ ["a=ssrc:", ssrc, " cname:", videoSources.sources[ssrc].cname].join(''),
|
|
|
396
|
+ ["a=ssrc:", ssrc, " msid:", videoSources.sources[ssrc].msid].join('')]));
|
|
|
397
|
+ }
|
|
|
398
|
+ }
|
|
|
399
|
+ }
|
|
|
400
|
+
|
|
|
401
|
+ return sb;
|
|
|
402
|
+ };
|
|
|
403
|
+
|
|
|
404
|
+ Simulcast.prototype.transformAnswer = function (desc) {
|
|
|
405
|
+ if (config.enableSimulcast && config.useNativeSimulcast) {
|
|
|
406
|
+
|
|
|
407
|
+ var sb = desc.sdp.split('\r\n');
|
|
|
408
|
+
|
|
|
409
|
+ // Even if we have enabled native simulcasting previously
|
|
|
410
|
+ // (with a call to SLD with an appropriate SDP, for example),
|
|
|
411
|
+ // createAnswer seems to consistently generate incomplete SDP
|
|
|
412
|
+ // with missing SSRCS.
|
|
|
413
|
+ //
|
|
|
414
|
+ // So, subsequent calls to SLD will have missing SSRCS and presence
|
|
|
415
|
+ // won't have the complete list of SRCs.
|
|
|
416
|
+ this._ensureSimulcastGroup(sb);
|
|
|
417
|
+
|
|
|
418
|
+ desc = new RTCSessionDescription({
|
|
|
419
|
+ type: desc.type,
|
|
|
420
|
+ sdp: sb.join('\r\n')
|
|
|
421
|
+ });
|
|
|
422
|
+
|
|
|
423
|
+ if (this.debugLvl && this.debugLvl > 1) {
|
|
|
424
|
+ console.info('Transformed answer');
|
|
|
425
|
+ console.info(desc.sdp);
|
|
|
426
|
+ }
|
|
|
427
|
+ }
|
|
|
428
|
+
|
|
|
429
|
+ return desc;
|
|
|
430
|
+ };
|
|
|
431
|
+
|
|
|
432
|
+ Simulcast.prototype.makeLocalDescriptionPublic = function (desc) {
|
|
|
433
|
+ var sb;
|
|
|
434
|
+
|
|
|
435
|
+ if (!desc || desc == null)
|
|
|
436
|
+ return desc;
|
|
|
437
|
+
|
|
|
438
|
+ if (config.enableSimulcast) {
|
|
|
439
|
+
|
|
|
440
|
+ if (config.useNativeSimulcast) {
|
|
|
441
|
+ sb = desc.sdp.split('\r\n');
|
|
|
442
|
+
|
|
|
443
|
+ this._explodeLocalSimulcastSources(sb);
|
|
|
444
|
+
|
|
|
445
|
+ desc = new RTCSessionDescription({
|
|
|
446
|
+ type: desc.type,
|
|
|
447
|
+ sdp: sb.join('\r\n')
|
|
|
448
|
+ });
|
|
|
449
|
+
|
|
|
450
|
+ if (this.debugLvl && this.debugLvl > 1) {
|
|
|
451
|
+ console.info('Exploded local video sources');
|
|
|
452
|
+ console.info(desc.sdp);
|
|
|
453
|
+ }
|
|
|
454
|
+ } else {
|
|
|
455
|
+ sb = desc.sdp.split('\r\n');
|
|
|
456
|
+
|
|
|
457
|
+ this._groupLocalVideoSources(sb);
|
|
|
458
|
+
|
|
|
459
|
+ desc = new RTCSessionDescription({
|
|
|
460
|
+ type: desc.type,
|
|
|
461
|
+ sdp: sb.join('\r\n')
|
|
|
462
|
+ });
|
|
|
463
|
+
|
|
|
464
|
+ if (this.debugLvl && this.debugLvl > 1) {
|
|
|
465
|
+ console.info('Grouped local video sources');
|
|
|
466
|
+ console.info(desc.sdp);
|
|
|
467
|
+ }
|
|
|
468
|
+ }
|
|
|
469
|
+ }
|
|
|
470
|
+
|
|
|
471
|
+ return desc;
|
|
|
472
|
+ };
|
|
|
473
|
+
|
|
|
474
|
+ Simulcast.prototype._ensureOrder = function (lines) {
|
|
|
475
|
+ var videoSources, sb;
|
|
|
476
|
+
|
|
|
477
|
+ videoSources = this._parseMedia(lines, ['video'])[0];
|
|
|
478
|
+ sb = this._compileVideoSources(videoSources);
|
|
|
479
|
+
|
|
|
480
|
+ this._replaceVideoSources(lines, sb);
|
|
|
481
|
+ };
|
|
|
482
|
+
|
|
|
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
|
+ Simulcast.prototype._updateRemoteMaps = function (lines) {
|
|
|
505
|
+ var remoteVideoSources = this._parseMedia(lines, ['video'])[0], videoSource, quality;
|
|
|
506
|
+
|
|
|
507
|
+ if (remoteVideoSources.groups && remoteVideoSources.groups.length !== 0) {
|
|
|
508
|
+ remoteVideoSources.groups.forEach(function (group) {
|
|
|
509
|
+ if (group.semantics === 'SIM' && group.ssrcs && group.ssrcs.length !== 0) {
|
|
|
510
|
+ quality = 0;
|
|
|
511
|
+ group.ssrcs.forEach(function (ssrc) {
|
|
|
512
|
+ videoSource = remoteVideoSources.sources[ssrc];
|
|
|
513
|
+ remoteMaps.msid2Quality[videoSource.msid] = quality++;
|
|
|
514
|
+ remoteMaps.ssrc2Msid[videoSource.ssrc] = videoSource.msid;
|
|
|
515
|
+ });
|
|
|
516
|
+ }
|
|
|
517
|
+ });
|
|
|
518
|
+ }
|
|
|
519
|
+ };
|
|
|
520
|
+
|
|
|
521
|
+ Simulcast.prototype.transformLocalDescription = function (desc) {
|
|
|
522
|
+ if (config.enableSimulcast && !config.useNativeSimulcast) {
|
|
|
523
|
+
|
|
|
524
|
+ var sb = desc.sdp.split('\r\n');
|
|
|
525
|
+
|
|
|
526
|
+ this._removeSimulcastGroup(sb);
|
|
|
527
|
+
|
|
|
528
|
+ desc = new RTCSessionDescription({
|
|
|
529
|
+ type: desc.type,
|
|
|
530
|
+ sdp: sb.join('\r\n')
|
|
|
531
|
+ });
|
|
|
532
|
+
|
|
|
533
|
+ if (this.debugLvl && this.debugLvl > 1) {
|
|
|
534
|
+ console.info('Transformed local description');
|
|
|
535
|
+ console.info(desc.sdp);
|
|
|
536
|
+ }
|
|
|
537
|
+ }
|
|
|
538
|
+
|
|
|
539
|
+ return desc;
|
|
|
540
|
+ };
|
|
|
541
|
+
|
|
|
542
|
+ Simulcast.prototype.transformRemoteDescription = function (desc) {
|
|
|
543
|
+ if (config.enableSimulcast) {
|
|
|
544
|
+
|
|
|
545
|
+ var sb = desc.sdp.split('\r\n');
|
|
|
546
|
+
|
|
|
547
|
+ this._updateRemoteMaps(sb);
|
|
|
548
|
+ this._removeSimulcastGroup(sb); // NOTE(gp) this needs to be called after updateRemoteMaps!
|
|
|
549
|
+ this._ensureGoogConference(sb);
|
|
|
550
|
+
|
|
|
551
|
+ desc = new RTCSessionDescription({
|
|
|
552
|
+ type: desc.type,
|
|
|
553
|
+ sdp: sb.join('\r\n')
|
|
|
554
|
+ });
|
|
|
555
|
+
|
|
|
556
|
+ if (this.debugLvl && this.debugLvl > 1) {
|
|
|
557
|
+ console.info('Transformed remote description');
|
|
|
558
|
+ console.info(desc.sdp);
|
|
|
559
|
+ }
|
|
|
560
|
+ }
|
|
|
561
|
+
|
|
|
562
|
+ return desc;
|
|
|
563
|
+ };
|
|
|
564
|
+
|
|
|
565
|
+ Simulcast.prototype.setReceivingVideoStream = function (ssrc) {
|
|
|
566
|
+ var receivingTrack = remoteMaps.ssrc2Msid[ssrc],
|
|
|
567
|
+ msidParts = receivingTrack.split(' ');
|
|
|
568
|
+
|
|
|
569
|
+ remoteMaps.receivingVideoStreams[msidParts[0]] = msidParts[1];
|
|
|
570
|
+ };
|
|
|
571
|
+
|
|
|
572
|
+ Simulcast.prototype.getReceivingVideoStream = function (stream) {
|
|
|
573
|
+ var tracks, track, i, electedTrack, msid, quality = 1, receivingTrackId;
|
|
|
574
|
+
|
|
|
575
|
+ if (config.enableSimulcast) {
|
|
|
576
|
+
|
|
|
577
|
+ if (remoteMaps.receivingVideoStreams[stream.id])
|
|
|
578
|
+ {
|
|
|
579
|
+ receivingTrackId = remoteMaps.receivingVideoStreams[stream.id];
|
|
|
580
|
+ tracks = stream.getVideoTracks();
|
|
|
581
|
+ for (i = 0; i < tracks.length; i++) {
|
|
|
582
|
+ if (receivingTrackId === tracks[i].id) {
|
|
|
583
|
+ electedTrack = tracks[i];
|
|
|
584
|
+ break;
|
|
|
585
|
+ }
|
|
|
586
|
+ }
|
|
|
587
|
+ }
|
|
|
588
|
+
|
|
|
589
|
+ if (!electedTrack) {
|
|
|
590
|
+ tracks = stream.getVideoTracks();
|
|
|
591
|
+ for (i = 0; i < tracks.length; i++) {
|
|
|
592
|
+ track = tracks[i];
|
|
|
593
|
+ msid = [stream.id, track.id].join(' ');
|
|
|
594
|
+ if (remoteMaps.msid2Quality[msid] === quality) {
|
|
|
595
|
+ electedTrack = track;
|
|
|
596
|
+ break;
|
|
|
597
|
+ }
|
|
|
598
|
+ }
|
|
|
599
|
+ }
|
|
|
600
|
+ }
|
|
|
601
|
+
|
|
|
602
|
+ return (electedTrack)
|
|
|
603
|
+ ? new webkitMediaStream([electedTrack])
|
|
|
604
|
+ : stream;
|
|
|
605
|
+ };
|
|
|
606
|
+
|
|
|
607
|
+ Simulcast.prototype.getUserMedia = function (constraints, success, err) {
|
|
|
608
|
+
|
|
|
609
|
+ // TODO(gp) what if we request a resolution not supported by the hardware?
|
|
|
610
|
+ // TODO(gp) make the lq stream configurable; although this wouldn't work with native simulcast
|
|
|
611
|
+ var lqConstraints = {
|
|
|
612
|
+ audio: false,
|
|
|
613
|
+ video: {
|
|
|
614
|
+ mandatory: {
|
|
|
615
|
+ maxWidth: 320,
|
|
|
616
|
+ maxHeight: 180
|
|
|
617
|
+ }
|
|
|
618
|
+ }
|
|
|
619
|
+ };
|
|
|
620
|
+
|
|
|
621
|
+ if (config.enableSimulcast && !config.useNativeSimulcast) {
|
|
|
622
|
+
|
|
|
623
|
+ // NOTE(gp) if we request the lq stream first webkitGetUserMedia fails randomly. Tested with Chrome 37.
|
|
|
624
|
+
|
|
|
625
|
+ navigator.webkitGetUserMedia(constraints, function (hqStream) {
|
|
|
626
|
+
|
|
|
627
|
+ // reset local maps.
|
|
|
628
|
+ localMaps.msids = [];
|
|
|
629
|
+ localMaps.msid2ssrc = {};
|
|
|
630
|
+
|
|
|
631
|
+ // add hq trackid to local map
|
|
|
632
|
+ localMaps.msids.push(hqStream.getVideoTracks()[0].id);
|
|
|
633
|
+
|
|
|
634
|
+ navigator.webkitGetUserMedia(lqConstraints, function (lqStream) {
|
|
|
635
|
+
|
|
|
636
|
+ // add lq trackid to local map
|
|
|
637
|
+ localMaps.msids.push(lqStream.getVideoTracks()[0].id);
|
|
|
638
|
+
|
|
|
639
|
+ hqStream.addTrack(lqStream.getVideoTracks()[0]);
|
|
|
640
|
+ success(hqStream);
|
|
|
641
|
+ }, err);
|
|
|
642
|
+ }, err);
|
|
|
643
|
+ } else {
|
|
|
644
|
+
|
|
|
645
|
+ // There's nothing special to do for native simulcast, so just do a normal GUM.
|
|
|
646
|
+
|
|
|
647
|
+ navigator.webkitGetUserMedia(constraints, function (hqStream) {
|
|
|
648
|
+
|
|
|
649
|
+ // reset local maps.
|
|
|
650
|
+ localMaps.msids = [];
|
|
|
651
|
+ localMaps.msid2ssrc = {};
|
|
|
652
|
+
|
|
|
653
|
+ // add hq stream to local map
|
|
|
654
|
+ localMaps.msids.push(hqStream.getVideoTracks()[0].id);
|
|
|
655
|
+
|
|
|
656
|
+ success(hqStream);
|
|
|
657
|
+ }, err);
|
|
|
658
|
+ }
|
|
|
659
|
+ };
|
|
|
660
|
+
|
|
|
661
|
+ Simulcast.prototype.getRemoteVideoStreamIdBySSRC = function (primarySSRC) {
|
|
|
662
|
+ return remoteMaps.ssrc2Msid[primarySSRC];
|
|
|
663
|
+ };
|
|
|
664
|
+
|
|
|
665
|
+ Simulcast.prototype.parseMedia = function (desc, mediatypes) {
|
|
|
666
|
+ var lines = desc.sdp.split('\r\n');
|
|
|
667
|
+ return this._parseMedia(lines, mediatypes);
|
|
|
668
|
+ };
|
|
|
669
|
+}());
|