ovenlivekit 1.4.0 → 1.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +389 -381
- package/dist/OvenLiveKit.js +1 -1
- package/dist/OvenLiveKit.min.js +1 -1
- package/dist/OvenLiveKit.min.js.map +1 -1
- package/package.json +1 -1
- package/src/OvenLiveKit.js +1217 -1177
package/src/OvenLiveKit.js
CHANGED
|
@@ -1,1177 +1,1217 @@
|
|
|
1
|
-
const OvenLiveKit = {};
|
|
2
|
-
|
|
3
|
-
const version = '1.
|
|
4
|
-
const logHeader = 'OvenLiveKit.js :';
|
|
5
|
-
const logEventHeader = 'OvenLiveKit.js ====';
|
|
6
|
-
|
|
7
|
-
// private methods
|
|
8
|
-
function sendMessage(webSocket, message) {
|
|
9
|
-
|
|
10
|
-
if (webSocket) {
|
|
11
|
-
webSocket.send(JSON.stringify(message));
|
|
12
|
-
}
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function generateDomainFromUrl(url) {
|
|
16
|
-
let result = '';
|
|
17
|
-
let match;
|
|
18
|
-
if (match = url.match(/^(?:wss?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n\?\=]+)/im)) {
|
|
19
|
-
result = match[1];
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
return result;
|
|
23
|
-
}
|
|
24
|
-
|
|
25
|
-
function findIp(string) {
|
|
26
|
-
|
|
27
|
-
let result = '';
|
|
28
|
-
let match;
|
|
29
|
-
|
|
30
|
-
if (match = string.match(new RegExp('\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b', 'gi'))) {
|
|
31
|
-
result = match[0];
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
return result;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function getFormatNumber(sdp, format) {
|
|
38
|
-
|
|
39
|
-
const lines = sdp.split('\r\n');
|
|
40
|
-
let formatNumber = -1;
|
|
41
|
-
|
|
42
|
-
for (let i = 0; i < lines.length - 1; i++) {
|
|
43
|
-
|
|
44
|
-
lines[i] = lines[i].toLowerCase();
|
|
45
|
-
|
|
46
|
-
if (lines[i].indexOf('a=rtpmap') === 0 && lines[i].indexOf(format.toLowerCase()) > -1) {
|
|
47
|
-
// parsing "a=rtpmap:100 H264/90000" line
|
|
48
|
-
// a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters >]
|
|
49
|
-
formatNumber = lines[i].split(' ')[0].split(':')[1];
|
|
50
|
-
break;
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
|
|
54
|
-
return formatNumber;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function setPreferredVideoFormat(sdp, formatName) {
|
|
58
|
-
|
|
59
|
-
const formatNumber = getFormatNumber(sdp, formatName);
|
|
60
|
-
|
|
61
|
-
if (formatNumber === -1) {
|
|
62
|
-
return sdp;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
|
-
let newLines = [];
|
|
66
|
-
const lines = sdp.split('\r\n');
|
|
67
|
-
|
|
68
|
-
for (let i = 0; i < lines.length - 1; i++) {
|
|
69
|
-
|
|
70
|
-
const line = lines[i];
|
|
71
|
-
|
|
72
|
-
if (line.indexOf('m=video') === 0) {
|
|
73
|
-
|
|
74
|
-
// m=<media> <port>/<number of ports> <transport> <fmt list>
|
|
75
|
-
const others = line.split(' ').slice(0, 3);
|
|
76
|
-
const formats = line.split(' ').slice(3);
|
|
77
|
-
formats.sort(function (x, y) { return x == formatNumber ? -1 : y == formatNumber ? 1 : 0; });
|
|
78
|
-
newLines.push(others.concat(formats).join(' '));
|
|
79
|
-
} else {
|
|
80
|
-
newLines.push(line);
|
|
81
|
-
}
|
|
82
|
-
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
return newLines.join('\r\n') + '\r\n';
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
function removeFormat(sdp, formatNumber) {
|
|
89
|
-
let newLines = [];
|
|
90
|
-
let lines = sdp.split('\r\n');
|
|
91
|
-
|
|
92
|
-
for (let i = 0; i < lines.length; i++) {
|
|
93
|
-
|
|
94
|
-
if (lines[i].indexOf('m=video') === 0) {
|
|
95
|
-
newLines.push(lines[i].replace(' ' + formatNumber + '', ''));
|
|
96
|
-
} else if (lines[i].indexOf(formatNumber + '') > -1) {
|
|
97
|
-
|
|
98
|
-
} else {
|
|
99
|
-
newLines.push(lines[i]);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
return newLines.join('\r\n')
|
|
104
|
-
}
|
|
105
|
-
|
|
106
|
-
async function getStreamForDeviceCheck(type) {
|
|
107
|
-
|
|
108
|
-
// High resolution video constraints makes browser to get maximum resolution of video device.
|
|
109
|
-
const constraints = {
|
|
110
|
-
};
|
|
111
|
-
|
|
112
|
-
if (type === 'both') {
|
|
113
|
-
constraints.audio = true;
|
|
114
|
-
constraints.video = true;
|
|
115
|
-
} else if (type === 'audio') {
|
|
116
|
-
constraints.audio = true;
|
|
117
|
-
} else if (type === 'video') {
|
|
118
|
-
constraints.video = true;
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
return await navigator.mediaDevices.getUserMedia(constraints);
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
async function getDevices() {
|
|
125
|
-
|
|
126
|
-
return await navigator.mediaDevices.enumerateDevices();
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
function gotDevices(deviceInfos) {
|
|
130
|
-
|
|
131
|
-
let devices = {
|
|
132
|
-
'audioinput': [],
|
|
133
|
-
'audiooutput': [],
|
|
134
|
-
'videoinput': [],
|
|
135
|
-
'other': [],
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
for (let i = 0; i !== deviceInfos.length; ++i) {
|
|
139
|
-
|
|
140
|
-
const deviceInfo = deviceInfos[i];
|
|
141
|
-
|
|
142
|
-
let info = {};
|
|
143
|
-
|
|
144
|
-
info.deviceId = deviceInfo.deviceId;
|
|
145
|
-
|
|
146
|
-
if (deviceInfo.kind === 'audioinput') {
|
|
147
|
-
|
|
148
|
-
info.label = deviceInfo.label || `microphone ${devices.audioinput.length + 1}`;
|
|
149
|
-
devices.audioinput.push(info);
|
|
150
|
-
} else if (deviceInfo.kind === 'audiooutput') {
|
|
151
|
-
|
|
152
|
-
info.label = deviceInfo.label || `speaker ${devices.audiooutput.length + 1}`;
|
|
153
|
-
devices.audiooutput.push(info);
|
|
154
|
-
} else if (deviceInfo.kind === 'videoinput') {
|
|
155
|
-
|
|
156
|
-
info.label = deviceInfo.label || `camera ${devices.videoinput.length + 1}`;
|
|
157
|
-
devices.videoinput.push(info);
|
|
158
|
-
} else {
|
|
159
|
-
|
|
160
|
-
info.label = deviceInfo.label || `other ${devices.other.length + 1}`;
|
|
161
|
-
devices.other.push(info);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
|
|
165
|
-
return devices;
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function initConfig(instance, options) {
|
|
169
|
-
|
|
170
|
-
// webrtc or whip
|
|
171
|
-
instance.streamingMode = null;
|
|
172
|
-
|
|
173
|
-
instance.inputStream = null;
|
|
174
|
-
instance.webSocket = null;
|
|
175
|
-
instance.peerConnection = null;
|
|
176
|
-
instance.connectionConfig = {};
|
|
177
|
-
|
|
178
|
-
instance.videoElement = null;
|
|
179
|
-
instance.endpointUrl = null;
|
|
180
|
-
instance.resourceUrl = null;
|
|
181
|
-
|
|
182
|
-
if (options && options.callbacks) {
|
|
183
|
-
|
|
184
|
-
instance.callbacks = options.callbacks;
|
|
185
|
-
} else {
|
|
186
|
-
instance.callbacks = {};
|
|
187
|
-
}
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function addMethod(instance) {
|
|
191
|
-
|
|
192
|
-
function errorHandler(error) {
|
|
193
|
-
|
|
194
|
-
if (instance.callbacks.error) {
|
|
195
|
-
|
|
196
|
-
instance.callbacks.error(error);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
async function fetchWithRedirect(url, options) {
|
|
201
|
-
let fetched = await fetch(url, options);
|
|
202
|
-
|
|
203
|
-
while (fetched.redirected) {
|
|
204
|
-
url = fetched.url;
|
|
205
|
-
fetched = await fetch(url, options);
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return fetched;
|
|
209
|
-
}
|
|
210
|
-
|
|
211
|
-
function getUserMedia(constraints) {
|
|
212
|
-
|
|
213
|
-
if (!constraints) {
|
|
214
|
-
|
|
215
|
-
constraints = {
|
|
216
|
-
video: {
|
|
217
|
-
deviceId: undefined
|
|
218
|
-
},
|
|
219
|
-
audio: {
|
|
220
|
-
deviceId: undefined
|
|
221
|
-
}
|
|
222
|
-
};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
console.info(logHeader, 'Request Stream To Input Devices With Constraints', constraints);
|
|
226
|
-
|
|
227
|
-
return navigator.mediaDevices.getUserMedia(constraints)
|
|
228
|
-
.then(function (stream) {
|
|
229
|
-
|
|
230
|
-
console.info(logHeader, 'Received Media Stream From Input Device', stream);
|
|
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
|
-
message.
|
|
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
|
-
offer.sdp =
|
|
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
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
|
|
1081
|
-
|
|
1082
|
-
|
|
1083
|
-
|
|
1084
|
-
|
|
1085
|
-
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
|
|
1091
|
-
|
|
1092
|
-
|
|
1093
|
-
|
|
1094
|
-
|
|
1095
|
-
|
|
1096
|
-
|
|
1097
|
-
|
|
1098
|
-
|
|
1099
|
-
|
|
1100
|
-
if (
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1130
|
-
|
|
1131
|
-
|
|
1132
|
-
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1
|
+
const OvenLiveKit = {};
|
|
2
|
+
|
|
3
|
+
const version = '1.5.0';
|
|
4
|
+
const logHeader = 'OvenLiveKit.js :';
|
|
5
|
+
const logEventHeader = 'OvenLiveKit.js ====';
|
|
6
|
+
|
|
7
|
+
// private methods
|
|
8
|
+
function sendMessage(webSocket, message) {
|
|
9
|
+
|
|
10
|
+
if (webSocket) {
|
|
11
|
+
webSocket.send(JSON.stringify(message));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function generateDomainFromUrl(url) {
|
|
16
|
+
let result = '';
|
|
17
|
+
let match;
|
|
18
|
+
if (match = url.match(/^(?:wss?:\/\/)?(?:[^@\n]+@)?(?:www\.)?([^:\/\n\?\=]+)/im)) {
|
|
19
|
+
result = match[1];
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return result;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function findIp(string) {
|
|
26
|
+
|
|
27
|
+
let result = '';
|
|
28
|
+
let match;
|
|
29
|
+
|
|
30
|
+
if (match = string.match(new RegExp('\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\b', 'gi'))) {
|
|
31
|
+
result = match[0];
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function getFormatNumber(sdp, format) {
|
|
38
|
+
|
|
39
|
+
const lines = sdp.split('\r\n');
|
|
40
|
+
let formatNumber = -1;
|
|
41
|
+
|
|
42
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
43
|
+
|
|
44
|
+
lines[i] = lines[i].toLowerCase();
|
|
45
|
+
|
|
46
|
+
if (lines[i].indexOf('a=rtpmap') === 0 && lines[i].indexOf(format.toLowerCase()) > -1) {
|
|
47
|
+
// parsing "a=rtpmap:100 H264/90000" line
|
|
48
|
+
// a=rtpmap:<payload type> <encoding name>/<clock rate>[/<encoding parameters >]
|
|
49
|
+
formatNumber = lines[i].split(' ')[0].split(':')[1];
|
|
50
|
+
break;
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return formatNumber;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function setPreferredVideoFormat(sdp, formatName) {
|
|
58
|
+
|
|
59
|
+
const formatNumber = getFormatNumber(sdp, formatName);
|
|
60
|
+
|
|
61
|
+
if (formatNumber === -1) {
|
|
62
|
+
return sdp;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
let newLines = [];
|
|
66
|
+
const lines = sdp.split('\r\n');
|
|
67
|
+
|
|
68
|
+
for (let i = 0; i < lines.length - 1; i++) {
|
|
69
|
+
|
|
70
|
+
const line = lines[i];
|
|
71
|
+
|
|
72
|
+
if (line.indexOf('m=video') === 0) {
|
|
73
|
+
|
|
74
|
+
// m=<media> <port>/<number of ports> <transport> <fmt list>
|
|
75
|
+
const others = line.split(' ').slice(0, 3);
|
|
76
|
+
const formats = line.split(' ').slice(3);
|
|
77
|
+
formats.sort(function (x, y) { return x == formatNumber ? -1 : y == formatNumber ? 1 : 0; });
|
|
78
|
+
newLines.push(others.concat(formats).join(' '));
|
|
79
|
+
} else {
|
|
80
|
+
newLines.push(line);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return newLines.join('\r\n') + '\r\n';
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
function removeFormat(sdp, formatNumber) {
|
|
89
|
+
let newLines = [];
|
|
90
|
+
let lines = sdp.split('\r\n');
|
|
91
|
+
|
|
92
|
+
for (let i = 0; i < lines.length; i++) {
|
|
93
|
+
|
|
94
|
+
if (lines[i].indexOf('m=video') === 0) {
|
|
95
|
+
newLines.push(lines[i].replace(' ' + formatNumber + '', ''));
|
|
96
|
+
} else if (lines[i].indexOf(formatNumber + '') > -1) {
|
|
97
|
+
|
|
98
|
+
} else {
|
|
99
|
+
newLines.push(lines[i]);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
return newLines.join('\r\n')
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
async function getStreamForDeviceCheck(type) {
|
|
107
|
+
|
|
108
|
+
// High resolution video constraints makes browser to get maximum resolution of video device.
|
|
109
|
+
const constraints = {
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
if (type === 'both') {
|
|
113
|
+
constraints.audio = true;
|
|
114
|
+
constraints.video = true;
|
|
115
|
+
} else if (type === 'audio') {
|
|
116
|
+
constraints.audio = true;
|
|
117
|
+
} else if (type === 'video') {
|
|
118
|
+
constraints.video = true;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
return await navigator.mediaDevices.getUserMedia(constraints);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
async function getDevices() {
|
|
125
|
+
|
|
126
|
+
return await navigator.mediaDevices.enumerateDevices();
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
function gotDevices(deviceInfos) {
|
|
130
|
+
|
|
131
|
+
let devices = {
|
|
132
|
+
'audioinput': [],
|
|
133
|
+
'audiooutput': [],
|
|
134
|
+
'videoinput': [],
|
|
135
|
+
'other': [],
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
for (let i = 0; i !== deviceInfos.length; ++i) {
|
|
139
|
+
|
|
140
|
+
const deviceInfo = deviceInfos[i];
|
|
141
|
+
|
|
142
|
+
let info = {};
|
|
143
|
+
|
|
144
|
+
info.deviceId = deviceInfo.deviceId;
|
|
145
|
+
|
|
146
|
+
if (deviceInfo.kind === 'audioinput') {
|
|
147
|
+
|
|
148
|
+
info.label = deviceInfo.label || `microphone ${devices.audioinput.length + 1}`;
|
|
149
|
+
devices.audioinput.push(info);
|
|
150
|
+
} else if (deviceInfo.kind === 'audiooutput') {
|
|
151
|
+
|
|
152
|
+
info.label = deviceInfo.label || `speaker ${devices.audiooutput.length + 1}`;
|
|
153
|
+
devices.audiooutput.push(info);
|
|
154
|
+
} else if (deviceInfo.kind === 'videoinput') {
|
|
155
|
+
|
|
156
|
+
info.label = deviceInfo.label || `camera ${devices.videoinput.length + 1}`;
|
|
157
|
+
devices.videoinput.push(info);
|
|
158
|
+
} else {
|
|
159
|
+
|
|
160
|
+
info.label = deviceInfo.label || `other ${devices.other.length + 1}`;
|
|
161
|
+
devices.other.push(info);
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return devices;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function initConfig(instance, options) {
|
|
169
|
+
|
|
170
|
+
// webrtc or whip
|
|
171
|
+
instance.streamingMode = null;
|
|
172
|
+
|
|
173
|
+
instance.inputStream = null;
|
|
174
|
+
instance.webSocket = null;
|
|
175
|
+
instance.peerConnection = null;
|
|
176
|
+
instance.connectionConfig = {};
|
|
177
|
+
|
|
178
|
+
instance.videoElement = null;
|
|
179
|
+
instance.endpointUrl = null;
|
|
180
|
+
instance.resourceUrl = null;
|
|
181
|
+
|
|
182
|
+
if (options && options.callbacks) {
|
|
183
|
+
|
|
184
|
+
instance.callbacks = options.callbacks;
|
|
185
|
+
} else {
|
|
186
|
+
instance.callbacks = {};
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
function addMethod(instance) {
|
|
191
|
+
|
|
192
|
+
function errorHandler(error) {
|
|
193
|
+
|
|
194
|
+
if (instance.callbacks.error) {
|
|
195
|
+
|
|
196
|
+
instance.callbacks.error(error);
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
async function fetchWithRedirect(url, options) {
|
|
201
|
+
let fetched = await fetch(url, options);
|
|
202
|
+
|
|
203
|
+
while (fetched.redirected) {
|
|
204
|
+
url = fetched.url;
|
|
205
|
+
fetched = await fetch(url, options);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
return fetched;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
function getUserMedia(constraints) {
|
|
212
|
+
|
|
213
|
+
if (!constraints) {
|
|
214
|
+
|
|
215
|
+
constraints = {
|
|
216
|
+
video: {
|
|
217
|
+
deviceId: undefined
|
|
218
|
+
},
|
|
219
|
+
audio: {
|
|
220
|
+
deviceId: undefined
|
|
221
|
+
}
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
console.info(logHeader, 'Request Stream To Input Devices With Constraints', constraints);
|
|
226
|
+
|
|
227
|
+
return navigator.mediaDevices.getUserMedia(constraints)
|
|
228
|
+
.then(function (stream) {
|
|
229
|
+
|
|
230
|
+
console.info(logHeader, 'Received Media Stream From Input Device', stream);
|
|
231
|
+
|
|
232
|
+
stream.getVideoTracks().forEach(function (track) {
|
|
233
|
+
console.info(logHeader, 'Video Track from input stream', track.getSettings());
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
instance.inputStream = stream;
|
|
237
|
+
|
|
238
|
+
let elem = instance.videoElement;
|
|
239
|
+
|
|
240
|
+
// Attach stream to video element when video element is provided.
|
|
241
|
+
if (elem) {
|
|
242
|
+
|
|
243
|
+
elem.srcObject = stream;
|
|
244
|
+
|
|
245
|
+
elem.onloadedmetadata = function (e) {
|
|
246
|
+
|
|
247
|
+
elem.play();
|
|
248
|
+
};
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
return new Promise(function (resolve) {
|
|
252
|
+
|
|
253
|
+
resolve(stream);
|
|
254
|
+
});
|
|
255
|
+
})
|
|
256
|
+
.catch(function (error) {
|
|
257
|
+
|
|
258
|
+
console.error(logHeader, 'Can\'t Get Media Stream From Input Device', error);
|
|
259
|
+
errorHandler(error);
|
|
260
|
+
|
|
261
|
+
return new Promise(function (resolve, reject) {
|
|
262
|
+
reject(error);
|
|
263
|
+
});
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function getDisplayMedia(constraints) {
|
|
268
|
+
|
|
269
|
+
if (!constraints) {
|
|
270
|
+
constraints = {};
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
console.info(logHeader, 'Request Stream To Display With Constraints', constraints);
|
|
274
|
+
|
|
275
|
+
return navigator.mediaDevices.getDisplayMedia(constraints)
|
|
276
|
+
.then(function (stream) {
|
|
277
|
+
|
|
278
|
+
console.info(logHeader, 'Received Media Stream From Display', stream);
|
|
279
|
+
|
|
280
|
+
instance.inputStream = stream;
|
|
281
|
+
|
|
282
|
+
let elem = instance.videoElement;
|
|
283
|
+
|
|
284
|
+
// Attach stream to video element when video element is provided.
|
|
285
|
+
if (elem) {
|
|
286
|
+
|
|
287
|
+
elem.srcObject = stream;
|
|
288
|
+
|
|
289
|
+
elem.onloadedmetadata = function (e) {
|
|
290
|
+
|
|
291
|
+
elem.play();
|
|
292
|
+
};
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
return new Promise(function (resolve) {
|
|
296
|
+
|
|
297
|
+
resolve(stream);
|
|
298
|
+
});
|
|
299
|
+
})
|
|
300
|
+
.catch(function (error) {
|
|
301
|
+
|
|
302
|
+
console.error(logHeader, 'Can\'t Get Media Stream From Display', error);
|
|
303
|
+
errorHandler(error);
|
|
304
|
+
|
|
305
|
+
return new Promise(function (resolve, reject) {
|
|
306
|
+
reject(error);
|
|
307
|
+
});
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function setMediaStream(stream) {
|
|
312
|
+
// Check if a valid stream is provided
|
|
313
|
+
if (!stream || !(stream instanceof MediaStream)) {
|
|
314
|
+
|
|
315
|
+
const error = new Error("Invalid MediaStream provided");
|
|
316
|
+
console.error(logHeader, 'Invalid MediaStream', error);
|
|
317
|
+
errorHandler(error);
|
|
318
|
+
|
|
319
|
+
return new Promise(function (resolve, reject) {
|
|
320
|
+
reject(error);
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
console.info(logHeader, 'Received Media Stream', stream);
|
|
325
|
+
|
|
326
|
+
instance.inputStream = stream;
|
|
327
|
+
|
|
328
|
+
let elem = instance.videoElement;
|
|
329
|
+
|
|
330
|
+
// Attach stream to video element when video element is provided.
|
|
331
|
+
if (elem) {
|
|
332
|
+
elem.srcObject = stream;
|
|
333
|
+
|
|
334
|
+
elem.onloadedmetadata = function (e) {
|
|
335
|
+
elem.play();
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
|
|
339
|
+
return new Promise(function (resolve) {
|
|
340
|
+
resolve(stream);
|
|
341
|
+
});
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// From https://webrtchacks.com/limit-webrtc-bandwidth-sdp/
|
|
345
|
+
function setBitrateLimit(sdp, media, bitrate) {
|
|
346
|
+
|
|
347
|
+
let lines = sdp.split('\r\n');
|
|
348
|
+
let line = -1;
|
|
349
|
+
|
|
350
|
+
for (let i = 0; i < lines.length; i++) {
|
|
351
|
+
if (lines[i].indexOf('m=' + media) === 0) {
|
|
352
|
+
line = i;
|
|
353
|
+
break;
|
|
354
|
+
}
|
|
355
|
+
}
|
|
356
|
+
if (line === -1) {
|
|
357
|
+
// Could not find the m line for media
|
|
358
|
+
return sdp;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// Pass the m line
|
|
362
|
+
line++;
|
|
363
|
+
|
|
364
|
+
// Skip i and c lines
|
|
365
|
+
while (lines[line].indexOf('i=') === 0 || lines[line].indexOf('c=') === 0) {
|
|
366
|
+
|
|
367
|
+
line++;
|
|
368
|
+
}
|
|
369
|
+
|
|
370
|
+
// If we're on a b line, replace it
|
|
371
|
+
if (lines[line].indexOf('b') === 0) {
|
|
372
|
+
|
|
373
|
+
lines[line] = 'b=AS:' + bitrate;
|
|
374
|
+
|
|
375
|
+
return lines.join('\r\n');
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
// Add a new b line
|
|
379
|
+
let newLines = lines.slice(0, line)
|
|
380
|
+
|
|
381
|
+
newLines.push('b=AS:' + bitrate)
|
|
382
|
+
newLines = newLines.concat(lines.slice(line, lines.length))
|
|
383
|
+
|
|
384
|
+
return newLines.join('\r\n')
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
function initWebSocket(endpointUrl) {
|
|
388
|
+
|
|
389
|
+
if (!endpointUrl) {
|
|
390
|
+
errorHandler('endpointUrl is required');
|
|
391
|
+
return;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
let webSocket = null;
|
|
395
|
+
|
|
396
|
+
try {
|
|
397
|
+
|
|
398
|
+
webSocket = new WebSocket(endpointUrl);
|
|
399
|
+
} catch (error) {
|
|
400
|
+
|
|
401
|
+
errorHandler(error);
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
instance.webSocket = webSocket;
|
|
406
|
+
|
|
407
|
+
webSocket.onopen = function () {
|
|
408
|
+
|
|
409
|
+
// Request offer at the first time.
|
|
410
|
+
sendMessage(webSocket, {
|
|
411
|
+
command: 'request_offer'
|
|
412
|
+
});
|
|
413
|
+
};
|
|
414
|
+
|
|
415
|
+
webSocket.onmessage = function (e) {
|
|
416
|
+
|
|
417
|
+
let message = JSON.parse(e.data);
|
|
418
|
+
|
|
419
|
+
if (message.error) {
|
|
420
|
+
console.error('webSocket.onmessage', message.error);
|
|
421
|
+
errorHandler(message.error);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
if (message.command === 'offer') {
|
|
425
|
+
|
|
426
|
+
// OME returns offer. Start create peer connection.
|
|
427
|
+
createPeerConnection(
|
|
428
|
+
message.id,
|
|
429
|
+
message.peer_id,
|
|
430
|
+
message.sdp,
|
|
431
|
+
message.candidates,
|
|
432
|
+
message.ice_servers
|
|
433
|
+
);
|
|
434
|
+
}
|
|
435
|
+
};
|
|
436
|
+
|
|
437
|
+
webSocket.onerror = function (error) {
|
|
438
|
+
|
|
439
|
+
console.error('webSocket.onerror', error);
|
|
440
|
+
errorHandler(error);
|
|
441
|
+
};
|
|
442
|
+
|
|
443
|
+
webSocket.onclose = function (e) {
|
|
444
|
+
|
|
445
|
+
if (!instance.webSocketClosedByUser) {
|
|
446
|
+
|
|
447
|
+
if (instance.callbacks.connectionClosed) {
|
|
448
|
+
instance.callbacks.connectionClosed('websocket', e);
|
|
449
|
+
}
|
|
450
|
+
}
|
|
451
|
+
};
|
|
452
|
+
|
|
453
|
+
}
|
|
454
|
+
|
|
455
|
+
async function startWhip(endpointUrl) {
|
|
456
|
+
|
|
457
|
+
if (instance.peerConnection) {
|
|
458
|
+
console.error('Connection already established');
|
|
459
|
+
errorHandler('Connection already established');
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
|
|
463
|
+
const peerConnectionConfig = {
|
|
464
|
+
bundlePolicy: "max-bundle"
|
|
465
|
+
};
|
|
466
|
+
|
|
467
|
+
if (instance.connectionConfig.iceServers) {
|
|
468
|
+
|
|
469
|
+
// first priority using ice servers from local config.
|
|
470
|
+
peerConnectionConfig.iceServers = instance.connectionConfig.iceServers;
|
|
471
|
+
|
|
472
|
+
if (instance.connectionConfig.iceTransportPolicy) {
|
|
473
|
+
|
|
474
|
+
peerConnectionConfig.iceTransportPolicy = instance.connectionConfig.iceTransportPolicy;
|
|
475
|
+
}
|
|
476
|
+
} else {
|
|
477
|
+
// last priority using default ice servers.
|
|
478
|
+
|
|
479
|
+
if (instance.connectionConfig.iceTransportPolicy) {
|
|
480
|
+
|
|
481
|
+
peerConnectionConfig.iceTransportPolicy = instance.connectionConfig.iceTransportPolicy;
|
|
482
|
+
}
|
|
483
|
+
}
|
|
484
|
+
|
|
485
|
+
console.info(logHeader, 'Create Peer Connection With Config', peerConnectionConfig);
|
|
486
|
+
|
|
487
|
+
const peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
|
488
|
+
|
|
489
|
+
instance.peerConnection = peerConnection;
|
|
490
|
+
|
|
491
|
+
if (!instance.inputStream) {
|
|
492
|
+
console.error('No input stream in OvenLiveKit');
|
|
493
|
+
errorHandler('No input stream in OvenLiveKit');
|
|
494
|
+
return;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
for (const track of instance.inputStream.getTracks()) {
|
|
498
|
+
console.log(logHeader, 'Adding track: ', track);
|
|
499
|
+
|
|
500
|
+
const transceiverConfig = {
|
|
501
|
+
direction: 'sendonly'
|
|
502
|
+
};
|
|
503
|
+
|
|
504
|
+
// Add simulcast layers if configured
|
|
505
|
+
const simulcastConfig = instance.connectionConfig.simulcast;
|
|
506
|
+
|
|
507
|
+
if (track.kind === 'video' && simulcastConfig && simulcastConfig.length > 0) {
|
|
508
|
+
|
|
509
|
+
transceiverConfig.sendEncodings = [];
|
|
510
|
+
|
|
511
|
+
for (let i = 0; i < simulcastConfig.length; i++) {
|
|
512
|
+
|
|
513
|
+
const layer = {
|
|
514
|
+
rid: i,
|
|
515
|
+
active: true,
|
|
516
|
+
...simulcastConfig[i]
|
|
517
|
+
};
|
|
518
|
+
|
|
519
|
+
console.log(logHeader, `Adding simulcast layer to: ${track.kind}`, layer);
|
|
520
|
+
|
|
521
|
+
transceiverConfig.sendEncodings.push(layer);
|
|
522
|
+
}
|
|
523
|
+
}
|
|
524
|
+
|
|
525
|
+
peerConnection.addTransceiver(track, transceiverConfig);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
peerConnection.oniceconnectionstatechange = function (e) {
|
|
529
|
+
|
|
530
|
+
let state = peerConnection.iceConnectionState;
|
|
531
|
+
|
|
532
|
+
if (instance.callbacks.iceStateChange) {
|
|
533
|
+
|
|
534
|
+
console.info(logHeader, 'ICE State', '[' + state + ']');
|
|
535
|
+
instance.callbacks.iceStateChange(state);
|
|
536
|
+
}
|
|
537
|
+
|
|
538
|
+
if (state === 'connected') {
|
|
539
|
+
|
|
540
|
+
if (instance.callbacks.connected) {
|
|
541
|
+
instance.callbacks.connected(e);
|
|
542
|
+
}
|
|
543
|
+
}
|
|
544
|
+
|
|
545
|
+
if (state === 'failed') {
|
|
546
|
+
|
|
547
|
+
if (instance.callbacks.connectionClosed) {
|
|
548
|
+
console.error(logHeader, 'Ice connection failed', e);
|
|
549
|
+
instance.callbacks.errorHandler(e);
|
|
550
|
+
}
|
|
551
|
+
}
|
|
552
|
+
|
|
553
|
+
if (state === 'disconnected' || state === 'closed') {
|
|
554
|
+
|
|
555
|
+
if (instance.callbacks.connectionClosed) {
|
|
556
|
+
console.error(logHeader, 'Ice connection disconnected or closed', e);
|
|
557
|
+
instance.callbacks.connectionClosed('ice', e);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
const offer = await peerConnection.createOffer();
|
|
563
|
+
console.log(logHeader, 'Offer SDP: ', offer.sdp);
|
|
564
|
+
|
|
565
|
+
if (instance.connectionConfig.maxVideoBitrate) {
|
|
566
|
+
|
|
567
|
+
// if bandwidth limit is set. modify sdp from ome to limit acceptable bandwidth of ome
|
|
568
|
+
offer.sdp = setBitrateLimit(offer.sdp, 'video', instance.connectionConfig.maxVideoBitrate);
|
|
569
|
+
}
|
|
570
|
+
|
|
571
|
+
if (instance.connectionConfig.sdp && instance.connectionConfig.sdp.appendFmtp) {
|
|
572
|
+
|
|
573
|
+
offer.sdp = appendFmtp(offer.sdp);
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
if (instance.connectionConfig.preferredVideoFormat) {
|
|
577
|
+
offer.sdp = setPreferredVideoFormat(offer.sdp, instance.connectionConfig.preferredVideoFormat);
|
|
578
|
+
} else {
|
|
579
|
+
// default to H264
|
|
580
|
+
offer.sdp = setPreferredVideoFormat(offer.sdp, 'H264');
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
const headers = {
|
|
584
|
+
"Content-Type": "application/sdp"
|
|
585
|
+
};
|
|
586
|
+
|
|
587
|
+
// Set Oven-Capabilities header if not using simulcast
|
|
588
|
+
if (!instance.connectionConfig.simulcast || instance.connectionConfig.simulcast.length === 0) {
|
|
589
|
+
|
|
590
|
+
const videoTracks = instance.inputStream.getVideoTracks();
|
|
591
|
+
|
|
592
|
+
if (videoTracks && videoTracks.length === 1) {
|
|
593
|
+
|
|
594
|
+
for (let i = 0; i < videoTracks.length; i++) {
|
|
595
|
+
|
|
596
|
+
const track = videoTracks[i];
|
|
597
|
+
const settings = track.getSettings();
|
|
598
|
+
|
|
599
|
+
console.log(logHeader, 'Video track settings for Oven-Capabilities:', settings);
|
|
600
|
+
|
|
601
|
+
const width = settings.width;
|
|
602
|
+
const height = settings.height;
|
|
603
|
+
|
|
604
|
+
if (typeof width === 'number' && typeof height === 'number') {
|
|
605
|
+
console.log(logHeader, `Setting Oven-Capabilities header: max_width=${width}, max_height=${height}`);
|
|
606
|
+
headers['Oven-Capabilities'] = `max_width=${width}, max_height=${height}`;
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
} else {
|
|
610
|
+
|
|
611
|
+
if (!videoTracks || videoTracks.length === 0) {
|
|
612
|
+
console.log(logHeader, 'No video tracks found, skipping Oven-Capabilities header.');
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
if (videoTracks && videoTracks.length > 1) {
|
|
616
|
+
console.log(logHeader, `Multiple (${videoTracks.length}) video tracks found, skipping Oven-Capabilities header.`);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
} else {
|
|
620
|
+
console.log(logHeader, 'Simulcast is enabled, skipping Oven-Capabilities header.');
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
if (instance.connectionConfig.httpHeaders) {
|
|
624
|
+
Object.assign(headers, instance.connectionConfig.httpHeaders);
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
const fetched = await fetchWithRedirect(endpointUrl, {
|
|
628
|
+
method: "POST",
|
|
629
|
+
body: offer.sdp,
|
|
630
|
+
headers
|
|
631
|
+
});
|
|
632
|
+
|
|
633
|
+
if (!fetched.ok) {
|
|
634
|
+
console.error('Failed to fetch', fetched.status);
|
|
635
|
+
errorHandler(`Failed to fetch ${fetched.status}`);
|
|
636
|
+
closePeerConnection();
|
|
637
|
+
return;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
if (!fetched.headers.get("location")) {
|
|
641
|
+
console.error('No location header on answer response');
|
|
642
|
+
errorHandler('No location header on answer response');
|
|
643
|
+
return;
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// update endpointUrl
|
|
647
|
+
instance.endpointUrl = fetched.url;
|
|
648
|
+
console.log(logHeader, 'Updated endpointUrl: ', instance.endpointUrl);
|
|
649
|
+
|
|
650
|
+
const baseUrl = new URL(endpointUrl).origin;
|
|
651
|
+
instance.resourceUrl = baseUrl + fetched.headers.get("location");
|
|
652
|
+
|
|
653
|
+
const answer = await fetched.text();
|
|
654
|
+
console.log(logHeader, 'Answer SDP: ', answer);
|
|
655
|
+
|
|
656
|
+
try {
|
|
657
|
+
await peerConnection.setLocalDescription(offer);
|
|
658
|
+
} catch (error) {
|
|
659
|
+
console.error('peerConnection.setLocalDescription', error);
|
|
660
|
+
errorHandler(error);
|
|
661
|
+
}
|
|
662
|
+
|
|
663
|
+
try {
|
|
664
|
+
await peerConnection.setRemoteDescription({
|
|
665
|
+
type: "answer",
|
|
666
|
+
sdp: answer
|
|
667
|
+
});
|
|
668
|
+
} catch (error) {
|
|
669
|
+
console.error('peerConnection.setRemoteDescription', error);
|
|
670
|
+
errorHandler(error);
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
async function stopWhip() {
|
|
675
|
+
|
|
676
|
+
if (!instance.peerConnection) {
|
|
677
|
+
console.error('No connection to close');
|
|
678
|
+
errorHandler('No connection to close');
|
|
679
|
+
return;
|
|
680
|
+
}
|
|
681
|
+
|
|
682
|
+
closePeerConnection();
|
|
683
|
+
|
|
684
|
+
if (instance.resourceUrl) {
|
|
685
|
+
|
|
686
|
+
const headers = {
|
|
687
|
+
};
|
|
688
|
+
|
|
689
|
+
if (instance.connectionConfig.httpHeaders) {
|
|
690
|
+
Object.assign(headers, instance.connectionConfig.httpHeaders);
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
await fetchWithRedirect(instance.resourceUrl, {
|
|
694
|
+
method: "DELETE",
|
|
695
|
+
headers
|
|
696
|
+
});
|
|
697
|
+
}
|
|
698
|
+
}
|
|
699
|
+
|
|
700
|
+
function appendFmtp(sdp) {
|
|
701
|
+
|
|
702
|
+
const fmtpStr = instance.connectionConfig.sdp.appendFmtp;
|
|
703
|
+
|
|
704
|
+
const lines = sdp.split('\r\n');
|
|
705
|
+
const payloads = [];
|
|
706
|
+
|
|
707
|
+
for (let i = 0; i < lines.length; i++) {
|
|
708
|
+
|
|
709
|
+
if (lines[i].indexOf('m=video') === 0) {
|
|
710
|
+
|
|
711
|
+
let tokens = lines[i].split(' ')
|
|
712
|
+
|
|
713
|
+
for (let j = 3; j < tokens.length; j++) {
|
|
714
|
+
|
|
715
|
+
payloads.push(tokens[j]);
|
|
716
|
+
}
|
|
717
|
+
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
for (let i = 0; i < payloads.length; i++) {
|
|
723
|
+
|
|
724
|
+
let fmtpLineFound = false;
|
|
725
|
+
|
|
726
|
+
for (let j = 0; j < lines.length; j++) {
|
|
727
|
+
|
|
728
|
+
if (lines[j].indexOf('a=fmtp:' + payloads[i]) === 0) {
|
|
729
|
+
fmtpLineFound = true;
|
|
730
|
+
lines[j] += ';' + fmtpStr;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
|
|
734
|
+
if (!fmtpLineFound) {
|
|
735
|
+
|
|
736
|
+
for (let j = 0; j < lines.length; j++) {
|
|
737
|
+
|
|
738
|
+
if (lines[j].indexOf('a=rtpmap:' + payloads[i]) === 0) {
|
|
739
|
+
|
|
740
|
+
lines[j] += '\r\na=fmtp:' + payloads[i] + ' ' + fmtpStr;
|
|
741
|
+
}
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
}
|
|
745
|
+
|
|
746
|
+
return lines.join('\r\n')
|
|
747
|
+
}
|
|
748
|
+
|
|
749
|
+
function appendOrientation(sdp) {
|
|
750
|
+
|
|
751
|
+
const lines = sdp.split('\r\n');
|
|
752
|
+
const payloads = [];
|
|
753
|
+
|
|
754
|
+
for (let i = 0; i < lines.length; i++) {
|
|
755
|
+
|
|
756
|
+
if (lines[i].indexOf('m=video') === 0) {
|
|
757
|
+
|
|
758
|
+
let tokens = lines[i].split(' ')
|
|
759
|
+
|
|
760
|
+
for (let j = 3; j < tokens.length; j++) {
|
|
761
|
+
|
|
762
|
+
payloads.push(tokens[j]);
|
|
763
|
+
}
|
|
764
|
+
|
|
765
|
+
break;
|
|
766
|
+
}
|
|
767
|
+
}
|
|
768
|
+
|
|
769
|
+
for (let i = 0; i < payloads.length; i++) {
|
|
770
|
+
|
|
771
|
+
for (let j = 0; j < lines.length; j++) {
|
|
772
|
+
|
|
773
|
+
if (lines[j].indexOf('a=rtpmap:' + payloads[i]) === 0) {
|
|
774
|
+
|
|
775
|
+
lines[j] += '\r\na=extmap:' + payloads[i] + ' urn:3gpp:video-orientation';
|
|
776
|
+
}
|
|
777
|
+
}
|
|
778
|
+
}
|
|
779
|
+
|
|
780
|
+
return lines.join('\r\n')
|
|
781
|
+
}
|
|
782
|
+
|
|
783
|
+
function createPeerConnection(id, peerId, offer, candidates, iceServers) {
|
|
784
|
+
|
|
785
|
+
let peerConnectionConfig = {};
|
|
786
|
+
|
|
787
|
+
if (instance.connectionConfig.iceServers) {
|
|
788
|
+
|
|
789
|
+
// first priority using ice servers from local config.
|
|
790
|
+
peerConnectionConfig.iceServers = instance.connectionConfig.iceServers;
|
|
791
|
+
|
|
792
|
+
if (instance.connectionConfig.iceTransportPolicy) {
|
|
793
|
+
|
|
794
|
+
peerConnectionConfig.iceTransportPolicy = instance.connectionConfig.iceTransportPolicy;
|
|
795
|
+
}
|
|
796
|
+
} else if (iceServers) {
|
|
797
|
+
|
|
798
|
+
// second priority using ice servers from ome and force using TCP
|
|
799
|
+
peerConnectionConfig.iceServers = [];
|
|
800
|
+
|
|
801
|
+
for (let i = 0; i < iceServers.length; i++) {
|
|
802
|
+
|
|
803
|
+
let iceServer = iceServers[i];
|
|
804
|
+
|
|
805
|
+
let regIceServer = {};
|
|
806
|
+
|
|
807
|
+
regIceServer.urls = iceServer.urls;
|
|
808
|
+
|
|
809
|
+
let hasWebSocketUrl = false;
|
|
810
|
+
let webSocketUrl = generateDomainFromUrl(instance.endpointUrl);
|
|
811
|
+
|
|
812
|
+
for (let j = 0; j < regIceServer.urls.length; j++) {
|
|
813
|
+
|
|
814
|
+
let serverUrl = regIceServer.urls[j];
|
|
815
|
+
|
|
816
|
+
if (serverUrl.indexOf(webSocketUrl) > -1) {
|
|
817
|
+
hasWebSocketUrl = true;
|
|
818
|
+
break;
|
|
819
|
+
}
|
|
820
|
+
}
|
|
821
|
+
|
|
822
|
+
if (!hasWebSocketUrl) {
|
|
823
|
+
|
|
824
|
+
if (regIceServer.urls.length > 0) {
|
|
825
|
+
|
|
826
|
+
let cloneIceServer = regIceServer.urls[0];
|
|
827
|
+
let ip = findIp(cloneIceServer);
|
|
828
|
+
|
|
829
|
+
if (webSocketUrl && ip) {
|
|
830
|
+
regIceServer.urls.push(cloneIceServer.replace(ip, webSocketUrl));
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
}
|
|
834
|
+
|
|
835
|
+
regIceServer.username = iceServer.user_name;
|
|
836
|
+
regIceServer.credential = iceServer.credential;
|
|
837
|
+
|
|
838
|
+
peerConnectionConfig.iceServers.push(regIceServer);
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
if (instance.connectionConfig.iceTransportPolicy) {
|
|
842
|
+
|
|
843
|
+
peerConnectionConfig.iceTransportPolicy = instance.connectionConfig.iceTransportPolicy;
|
|
844
|
+
} else {
|
|
845
|
+
peerConnectionConfig.iceTransportPolicy = 'relay';
|
|
846
|
+
}
|
|
847
|
+
} else {
|
|
848
|
+
// last priority using default ice servers.
|
|
849
|
+
|
|
850
|
+
if (instance.connectionConfig.iceTransportPolicy) {
|
|
851
|
+
|
|
852
|
+
peerConnectionConfig.iceTransportPolicy = instance.connectionConfig.iceTransportPolicy;
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
let advancedSetting = {
|
|
857
|
+
optional: [
|
|
858
|
+
{
|
|
859
|
+
googHighStartBitrate: {
|
|
860
|
+
exact: !0
|
|
861
|
+
}
|
|
862
|
+
},
|
|
863
|
+
{
|
|
864
|
+
googPayloadPadding: {
|
|
865
|
+
exact: !0
|
|
866
|
+
}
|
|
867
|
+
},
|
|
868
|
+
{
|
|
869
|
+
googScreencastMinBitrate: {
|
|
870
|
+
exact: 500
|
|
871
|
+
}
|
|
872
|
+
},
|
|
873
|
+
{
|
|
874
|
+
enableDscp: {
|
|
875
|
+
exact: true
|
|
876
|
+
}
|
|
877
|
+
}
|
|
878
|
+
]
|
|
879
|
+
};
|
|
880
|
+
|
|
881
|
+
console.info(logHeader, 'Create Peer Connection With Config', peerConnectionConfig);
|
|
882
|
+
|
|
883
|
+
let peerConnection = new RTCPeerConnection(peerConnectionConfig);
|
|
884
|
+
|
|
885
|
+
instance.peerConnection = peerConnection;
|
|
886
|
+
|
|
887
|
+
// set local stream
|
|
888
|
+
instance.inputStream.getTracks().forEach(function (track) {
|
|
889
|
+
|
|
890
|
+
console.info(logHeader, 'Add Track To Peer Connection', track);
|
|
891
|
+
peerConnection.addTrack(track, instance.inputStream);
|
|
892
|
+
});
|
|
893
|
+
|
|
894
|
+
if (instance.connectionConfig.maxVideoBitrate) {
|
|
895
|
+
|
|
896
|
+
// if bandwith limit is set. modify sdp from ome to limit acceptable bandwidth of ome
|
|
897
|
+
offer.sdp = setBitrateLimit(offer.sdp, 'video', instance.connectionConfig.maxVideoBitrate);
|
|
898
|
+
}
|
|
899
|
+
|
|
900
|
+
if (instance.connectionConfig.sdp && instance.connectionConfig.sdp.appendFmtp) {
|
|
901
|
+
|
|
902
|
+
offer.sdp = appendFmtp(offer.sdp);
|
|
903
|
+
}
|
|
904
|
+
|
|
905
|
+
if (instance.connectionConfig.preferredVideoFormat) {
|
|
906
|
+
offer.sdp = setPreferredVideoFormat(offer.sdp, instance.connectionConfig.preferredVideoFormat)
|
|
907
|
+
}
|
|
908
|
+
|
|
909
|
+
|
|
910
|
+
// offer.sdp = appendOrientation(offer.sdp);
|
|
911
|
+
console.info(logHeader, 'Modified offer sdp\n\n' + offer.sdp);
|
|
912
|
+
|
|
913
|
+
peerConnection.setRemoteDescription(new RTCSessionDescription(offer))
|
|
914
|
+
.then(function () {
|
|
915
|
+
|
|
916
|
+
peerConnection.createAnswer()
|
|
917
|
+
.then(function (answer) {
|
|
918
|
+
|
|
919
|
+
if (instance.connectionConfig.sdp && instance.connectionConfig.sdp.appendFmtp) {
|
|
920
|
+
|
|
921
|
+
answer.sdp = appendFmtp(answer.sdp);
|
|
922
|
+
}
|
|
923
|
+
|
|
924
|
+
if (instance.connectionConfig.preferredVideoFormat) {
|
|
925
|
+
answer.sdp = setPreferredVideoFormat(answer.sdp, instance.connectionConfig.preferredVideoFormat)
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
console.info(logHeader, 'Modified answer sdp\n\n' + answer.sdp);
|
|
929
|
+
|
|
930
|
+
peerConnection.setLocalDescription(answer)
|
|
931
|
+
.then(function () {
|
|
932
|
+
|
|
933
|
+
sendMessage(instance.webSocket, {
|
|
934
|
+
id: id,
|
|
935
|
+
peer_id: peerId,
|
|
936
|
+
command: 'answer',
|
|
937
|
+
sdp: answer
|
|
938
|
+
});
|
|
939
|
+
})
|
|
940
|
+
.catch(function (error) {
|
|
941
|
+
|
|
942
|
+
console.error('peerConnection.setLocalDescription', error);
|
|
943
|
+
errorHandler(error);
|
|
944
|
+
});
|
|
945
|
+
})
|
|
946
|
+
.catch(function (error) {
|
|
947
|
+
|
|
948
|
+
console.error('peerConnection.createAnswer', error);
|
|
949
|
+
errorHandler(error);
|
|
950
|
+
});
|
|
951
|
+
})
|
|
952
|
+
.catch(function (error) {
|
|
953
|
+
|
|
954
|
+
console.error('peerConnection.setRemoteDescription', error);
|
|
955
|
+
errorHandler(error);
|
|
956
|
+
});
|
|
957
|
+
|
|
958
|
+
if (candidates) {
|
|
959
|
+
|
|
960
|
+
addIceCandidate(peerConnection, candidates);
|
|
961
|
+
}
|
|
962
|
+
|
|
963
|
+
peerConnection.onicecandidate = function (e) {
|
|
964
|
+
|
|
965
|
+
if (e.candidate && e.candidate.candidate) {
|
|
966
|
+
|
|
967
|
+
sendMessage(instance.webSocket, {
|
|
968
|
+
id: id,
|
|
969
|
+
peer_id: peerId,
|
|
970
|
+
command: 'candidate',
|
|
971
|
+
candidates: [e.candidate]
|
|
972
|
+
});
|
|
973
|
+
}
|
|
974
|
+
};
|
|
975
|
+
|
|
976
|
+
peerConnection.oniceconnectionstatechange = function (e) {
|
|
977
|
+
|
|
978
|
+
let state = peerConnection.iceConnectionState;
|
|
979
|
+
|
|
980
|
+
if (instance.callbacks.iceStateChange) {
|
|
981
|
+
|
|
982
|
+
console.info(logHeader, 'ICE State', '[' + state + ']');
|
|
983
|
+
instance.callbacks.iceStateChange(state);
|
|
984
|
+
}
|
|
985
|
+
|
|
986
|
+
if (state === 'connected') {
|
|
987
|
+
|
|
988
|
+
if (instance.callbacks.connected) {
|
|
989
|
+
instance.callbacks.connected(e);
|
|
990
|
+
}
|
|
991
|
+
}
|
|
992
|
+
|
|
993
|
+
if (state === 'failed' || state === 'disconnected' || state === 'closed') {
|
|
994
|
+
|
|
995
|
+
if (instance.callbacks.connectionClosed) {
|
|
996
|
+
console.error(logHeader, 'Ice connection closed', e);
|
|
997
|
+
instance.callbacks.connectionClosed('ice', e);
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
function addIceCandidate(peerConnection, candidates) {
|
|
1004
|
+
|
|
1005
|
+
for (let i = 0; i < candidates.length; i++) {
|
|
1006
|
+
|
|
1007
|
+
if (candidates[i] && candidates[i].candidate) {
|
|
1008
|
+
|
|
1009
|
+
let basicCandidate = candidates[i];
|
|
1010
|
+
|
|
1011
|
+
peerConnection.addIceCandidate(new RTCIceCandidate(basicCandidate))
|
|
1012
|
+
.then(function () {
|
|
1013
|
+
|
|
1014
|
+
})
|
|
1015
|
+
.catch(function (error) {
|
|
1016
|
+
|
|
1017
|
+
console.error('peerConnection.addIceCandidate', error);
|
|
1018
|
+
errorHandler(error);
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
}
|
|
1022
|
+
}
|
|
1023
|
+
|
|
1024
|
+
function closePeerConnection() {
|
|
1025
|
+
if (instance.peerConnection) {
|
|
1026
|
+
|
|
1027
|
+
// remove tracks from peer connection
|
|
1028
|
+
instance.peerConnection.getSenders().forEach(function (sender) {
|
|
1029
|
+
instance.peerConnection.removeTrack(sender);
|
|
1030
|
+
});
|
|
1031
|
+
|
|
1032
|
+
instance.peerConnection.close();
|
|
1033
|
+
instance.peerConnection = null;
|
|
1034
|
+
delete instance.peerConnection;
|
|
1035
|
+
}
|
|
1036
|
+
}
|
|
1037
|
+
|
|
1038
|
+
function closeWebSocket() {
|
|
1039
|
+
|
|
1040
|
+
if (instance.webSocket) {
|
|
1041
|
+
|
|
1042
|
+
instance.webSocket.close();
|
|
1043
|
+
instance.webSocket = null;
|
|
1044
|
+
delete instance.webSocket;
|
|
1045
|
+
}
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
function closeInputStream() {
|
|
1049
|
+
// release video, audio stream
|
|
1050
|
+
if (instance.inputStream) {
|
|
1051
|
+
|
|
1052
|
+
instance.inputStream.getTracks().forEach(track => {
|
|
1053
|
+
|
|
1054
|
+
track.stop();
|
|
1055
|
+
instance.inputStream.removeTrack(track);
|
|
1056
|
+
});
|
|
1057
|
+
|
|
1058
|
+
if (instance.videoElement) {
|
|
1059
|
+
instance.videoElement.srcObject = null;
|
|
1060
|
+
}
|
|
1061
|
+
|
|
1062
|
+
instance.inputStream = null;
|
|
1063
|
+
delete instance.inputStream;
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
|
|
1067
|
+
// instance methods
|
|
1068
|
+
instance.attachMedia = function (videoElement) {
|
|
1069
|
+
|
|
1070
|
+
instance.videoElement = videoElement;
|
|
1071
|
+
};
|
|
1072
|
+
|
|
1073
|
+
instance.getUserMedia = function (constraints) {
|
|
1074
|
+
|
|
1075
|
+
return getUserMedia(constraints);
|
|
1076
|
+
};
|
|
1077
|
+
|
|
1078
|
+
instance.getDisplayMedia = function (constraints) {
|
|
1079
|
+
|
|
1080
|
+
return getDisplayMedia(constraints);
|
|
1081
|
+
};
|
|
1082
|
+
|
|
1083
|
+
instance.setMediaStream = function (stream) {
|
|
1084
|
+
|
|
1085
|
+
return setMediaStream(stream);
|
|
1086
|
+
};
|
|
1087
|
+
|
|
1088
|
+
instance.startStreaming = function (endpointUrl, connectionConfig) {
|
|
1089
|
+
|
|
1090
|
+
console.info(logEventHeader, `Start Streaming to ${endpointUrl} with connectionConfig`, connectionConfig);
|
|
1091
|
+
|
|
1092
|
+
if (!endpointUrl) {
|
|
1093
|
+
console.error('endpointUrl is required');
|
|
1094
|
+
errorHandler('endpointUrl is required');
|
|
1095
|
+
return;
|
|
1096
|
+
}
|
|
1097
|
+
|
|
1098
|
+
instance.endpointUrl = endpointUrl;
|
|
1099
|
+
|
|
1100
|
+
if (connectionConfig) {
|
|
1101
|
+
instance.connectionConfig = connectionConfig;
|
|
1102
|
+
}
|
|
1103
|
+
|
|
1104
|
+
try {
|
|
1105
|
+
|
|
1106
|
+
const protocol = new URL(endpointUrl).protocol;
|
|
1107
|
+
|
|
1108
|
+
if (protocol === 'wss:' || protocol === 'ws:') {
|
|
1109
|
+
|
|
1110
|
+
instance.streamingMode = 'webrtc';
|
|
1111
|
+
initWebSocket(endpointUrl);
|
|
1112
|
+
} else if (protocol === 'https:' || protocol === 'http:') {
|
|
1113
|
+
|
|
1114
|
+
instance.streamingMode = 'whip';
|
|
1115
|
+
startWhip(endpointUrl);
|
|
1116
|
+
} else {
|
|
1117
|
+
console.error('Invalid protocol', error);
|
|
1118
|
+
errorHandler(error);
|
|
1119
|
+
}
|
|
1120
|
+
|
|
1121
|
+
} catch (error) {
|
|
1122
|
+
console.error('Cannot parse connection URL', error);
|
|
1123
|
+
errorHandler(error);
|
|
1124
|
+
}
|
|
1125
|
+
};
|
|
1126
|
+
|
|
1127
|
+
instance.stopStreaming = async function () {
|
|
1128
|
+
|
|
1129
|
+
if (instance.streamingMode === 'webrtc') {
|
|
1130
|
+
|
|
1131
|
+
instance.webSocketClosedByUser = true;
|
|
1132
|
+
|
|
1133
|
+
closeWebSocket();
|
|
1134
|
+
closePeerConnection();
|
|
1135
|
+
} else if (instance.streamingMode === 'whip') {
|
|
1136
|
+
|
|
1137
|
+
await stopWhip();
|
|
1138
|
+
}
|
|
1139
|
+
|
|
1140
|
+
if (instance.callbacks.connectionClosed) {
|
|
1141
|
+
console.log(logHeader, 'Connection closed by user');
|
|
1142
|
+
instance.callbacks.connectionClosed('user', 'Connection closed by user');
|
|
1143
|
+
}
|
|
1144
|
+
};
|
|
1145
|
+
|
|
1146
|
+
instance.remove = function () {
|
|
1147
|
+
|
|
1148
|
+
if (instance.streamingMode === 'webrtc') {
|
|
1149
|
+
|
|
1150
|
+
instance.webSocketClosedByUser = true;
|
|
1151
|
+
|
|
1152
|
+
closeWebSocket();
|
|
1153
|
+
closePeerConnection();
|
|
1154
|
+
} else if (instance.streamingMode === 'whip') {
|
|
1155
|
+
stopWhip();
|
|
1156
|
+
}
|
|
1157
|
+
|
|
1158
|
+
closeInputStream();
|
|
1159
|
+
|
|
1160
|
+
console.info(logEventHeader, 'Removed');
|
|
1161
|
+
|
|
1162
|
+
};
|
|
1163
|
+
}
|
|
1164
|
+
|
|
1165
|
+
OvenLiveKit.getVersion = function () {
|
|
1166
|
+
return version;
|
|
1167
|
+
}
|
|
1168
|
+
|
|
1169
|
+
// static methods
|
|
1170
|
+
OvenLiveKit.create = function (options) {
|
|
1171
|
+
|
|
1172
|
+
console.info(logEventHeader, 'Create WebRTC Input ' + version);
|
|
1173
|
+
|
|
1174
|
+
let instance = {};
|
|
1175
|
+
|
|
1176
|
+
instance.webSocketClosedByUser = false;
|
|
1177
|
+
|
|
1178
|
+
initConfig(instance, options);
|
|
1179
|
+
addMethod(instance);
|
|
1180
|
+
|
|
1181
|
+
return instance;
|
|
1182
|
+
};
|
|
1183
|
+
|
|
1184
|
+
OvenLiveKit.getDevices = async function (type = 'both') {
|
|
1185
|
+
|
|
1186
|
+
try {
|
|
1187
|
+
// First check both audio and video sources are available.
|
|
1188
|
+
await getStreamForDeviceCheck(type);
|
|
1189
|
+
} catch (e) {
|
|
1190
|
+
|
|
1191
|
+
console.warn(logHeader, 'Can not find Video and Audio devices', e);
|
|
1192
|
+
|
|
1193
|
+
let videoFound = null;
|
|
1194
|
+
let audioFound = null;
|
|
1195
|
+
|
|
1196
|
+
try {
|
|
1197
|
+
videoFound = await getStreamForDeviceCheck('video');
|
|
1198
|
+
} catch (e) {
|
|
1199
|
+
console.warn(logHeader, 'Can not find Video devices', e);
|
|
1200
|
+
}
|
|
1201
|
+
|
|
1202
|
+
try {
|
|
1203
|
+
audioFound = await getStreamForDeviceCheck('audio');
|
|
1204
|
+
} catch (e) {
|
|
1205
|
+
console.warn(logHeader, 'Can not find Audio devices', e);
|
|
1206
|
+
}
|
|
1207
|
+
|
|
1208
|
+
if (!videoFound && !audioFound) {
|
|
1209
|
+
throw new Error('No input devices were found.');
|
|
1210
|
+
}
|
|
1211
|
+
}
|
|
1212
|
+
|
|
1213
|
+
const deviceInfos = await getDevices();
|
|
1214
|
+
return gotDevices(deviceInfos)
|
|
1215
|
+
};
|
|
1216
|
+
|
|
1217
|
+
export default OvenLiveKit;
|