This repository was archived by the owner on Aug 30, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
989 lines (810 loc) · 32.6 KB
/
script.js
File metadata and controls
989 lines (810 loc) · 32.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
const logMessage = (message) => {
console.log(`${new Date().toISOString()} - ${message}`);
$('#logs-area').val((_, text) => `${text}${new Date().toISOString()} - ${message}\r\n` );
// Scroll to the end
$('#logs-area').scrollTop($('#logs-area')[0].scrollHeight);
};
const logError = (message) => {
console.error(`${new Date().toISOString()} - ${message}`);
$('#logs-area').val((_, text) => `${text}${new Date().toISOString()} - ${message}\r\n` );
// Scroll to the end
$('#logs-area').scrollTop($('#logs-area')[0].scrollHeight);
};
// Do not configure your appKey and appSecret in this application,
// you must use your server for authentication purpose
// Please refer to this link https://docs.dolby.io/communications-apis/docs/guides-client-authentication
const appKey = "<APP_KEY>";
const appSecret = "<APP_SECRET>";
/**
* Initialize the SDK with a client access token
*/
$("#initialize-btn").click(() => {
const accessToken = $('#access-token-input').val();
initializeSDK(accessToken);
});
const initializeSDK = (accessToken) => {
const token = accessToken.split('.')[1];
const jwt = JSON.parse(window.atob(token));
accessTokenExpiration = new Date(jwt.exp * 1000);
if (accessTokenExpiration.getTime() <= new Date().getTime()) {
logError('The access token you have provided has expired.');
return;
}
logMessage(`Initialize the SDK with the Access Token: ${accessToken}`);
logMessage(`Access Token Expiration: ${accessTokenExpiration}`);
VoxeetSDK.initializeToken(accessToken, () => new Promise((resolve) => resolve(accessToken)));
$('#initialize-btn').attr('disabled', true);
$('#connect-btn').attr('disabled', false);
};
var conferenceId;
var conferenceAccessToken;
const getConstraints = (joinWithAudio, joinWithVideo) => {
if (!joinWithVideo) {
return {
constraints: {
audio: joinWithAudio,
video: joinWithVideo
}
};
}
let video = true;
let value = $('#webrtc-constraints').val();
if (value === "640") {
video = { width: 640, height: 360 };
} else if (value === "960") {
video = { width: 960, height: 540 };
} else if (value === "1280") {
video = { width: 1280, height: 720 };
} else if (value === "min640") {
video = { width: { min: 640 }, height: { min: 360 } };
} else if (value === "min960") {
video = { width: { min: 960 }, height: { min: 540 } };
} else if (value === "min1280") {
video = { width: { min: 1280 }, height: { min: 720 } };
}
return {
constraints: {
audio: joinWithAudio,
video: video
}
};
};
$('#btn-set-webrtc-constraints').click(() => {
VoxeetSDK.session.participant.streams.forEach(stream => {
if (stream.active && stream.type === "Camera") {
logMessage("VoxeetSDK.conference.stopVideo");
// Stop the video and restart it with the new constraints
VoxeetSDK.conference
.stopVideo(VoxeetSDK.session.participant)
.then(startVideo)
.catch((err) => logError(err));
return;
}
});
});
$('#connect-btn').click(() => {
const externalId = $('#external-id-input').val();
const username = $('#username-input').val();
const avatarUrl = $('#avatar-url-input').val();
// Open a session to the Dolby.io APIs
VoxeetSDK.session
.open({ name: username, externalId: externalId, avatarUrl: avatarUrl })
.then(() => {
// Update the login message with the name of the user
$('#title').text(`You are connected as ${username}`);
$('#conference-join-btn').attr('disabled', false);
$('#conference-listen-btn').attr('disabled', false);
$('#connect-btn').attr('disabled', true);
$('#external-id-input').attr('readonly', true);
$('#username-input').attr('readonly', true);
$('#avatar-url-input').attr('readonly', true);
})
.then(() => logMessage(`You are connected as ${username}`))
.catch((e) => logError(e));
});
function setDevices(name, listSelector, btnSelector, devices) {
console.log(name);
console.log(devices);
$(listSelector).empty();
devices.forEach(device => {
$(listSelector).append(new Option(device.label, device.deviceId));
});
$(btnSelector).attr('disabled', false);
}
async function updateDevices() {
if (VoxeetSDK.session.participant) {
// Load the Output Audio devices
let devices = await VoxeetSDK.mediaDevice.enumerateAudioDevices("output");
setDevices('Output Audio Devices', '#output-audio-devices', '#btn-set-output-audio-device', devices);
if (VoxeetSDK.session.participant.type !== 'listener') {
// Load the Input Audio devices
devices = await VoxeetSDK.mediaDevice.enumerateAudioDevices("input");
setDevices('Input Audio Devices', '#input-audio-devices', '#btn-set-input-audio-device', devices);
// Load the Video devices
devices = await VoxeetSDK.mediaDevice.enumerateVideoDevices("input");
setDevices('Video Devices', '#video-devices', '#btn-set-video-device', devices);
}
}
}
$('#conference-join-btn').click(async () => {
try {
const liveRecording = $('#chk-live-recording')[0].checked;
const joinWithAudio = $('#chk-join-with-audio')[0].checked;
const joinWithVideo = $('#chk-join-with-video')[0].checked;
// Default conference parameters
// See: https://docs.dolby.io/communications-apis/docs/js-client-sdk-model-conferenceparameters
const conferenceParams = {
liveRecording: liveRecording,
rtcpMode: "average", // worst, average, max
ttl: 0,
videoCodec: "H264", // H264, VP8
dolbyVoice: true
};
// See: https://docs.dolby.io/communications-apis/docs/js-client-sdk-model-conferenceoptions
const conferenceOptions = {
alias: $('#conference-alias-input').val(),
params: conferenceParams
};
// 1. Create a conference room with an alias
const conference = await VoxeetSDK.conference.create(conferenceOptions);
logMessage(`Conference id: ${conference.id} & Conference alias ${conference.alias}`);
conferenceId = conference.id;
// See: https://docs.dolby.io/communications-apis/docs/js-client-sdk-model-joinoptions
const joinOptions = getConstraints(joinWithAudio, joinWithVideo);
joinOptions.simulcast = false;
if (conferenceAccessToken) {
joinOptions.conferenceAccessToken = conferenceAccessToken;
}
logMessage("Join the conference with the options:");
logMessage(JSON.stringify(joinOptions));
// 2. Join the conference
await VoxeetSDK.conference.join(conference, joinOptions);
// Subscribe to the participant joined/left events
await VoxeetSDK.notification.subscribe([
{
type: "Participant.Left",
conferenceAlias: conference.alias
},
{
type: "Participant.Joined",
conferenceAlias: conference.alias
},
{
type: "Conference.ActiveParticipants",
conferenceAlias: conference.alias
}
]);
await updateDevices();
$('#btn-set-webrtc-constraints').attr('disabled', false);
$('#chk-live-recording').attr('disabled', true);
$('#conference-join-btn').attr('disabled', true);
$('#conference-listen-btn').attr('disabled', true);
$('#conference-leave-btn').attr('disabled', false);
$('#conference-alias-input').attr('readonly', true);
$('#start-video-btn').attr('disabled', joinWithVideo);
$('#stop-video-btn').attr('disabled', !joinWithVideo);
$('#start-audio-btn').attr('disabled', joinWithAudio);
$('#stop-audio-btn').attr('disabled', !joinWithAudio);
$('#mute-audio-btn').attr('disabled', !joinWithAudio);
$('#unmute-audio-btn').attr('disabled', joinWithAudio);
$('#start-screenshare-btn').attr('disabled', false);
$('#stop-screenshare-btn').attr('disabled', true);
$('#video-url-input').attr('readonly', false);
$("#video-start-btn").attr('disabled', false);
$("#video-stop-btn").attr('disabled', true);
$("#video-pause-btn").attr('disabled', true);
$("#video-play-btn").attr('disabled', true);
$("#start-recording-btn").attr('disabled', false);
$("#stop-recording-btn").attr('disabled', true);
$('#recording-status')
.removeClass('fa-circle').addClass('fa-stop-circle')
.removeClass('red').addClass('gray');
$('#rtmp-status').removeClass('red').addClass('gray');
$("#rtmp-url-input").attr('readonly', false);
$("#start-rtmp-btn").attr('disabled', false);
$("#stop-rtmp-btn").attr('disabled', true);
$('#lls-status').removeClass('red').addClass('gray');
$("#lls-stream-name-input").attr('readonly', false);
$("#lls-ptoken-input").attr('readonly', false);
$("#start-lls-btn").attr('disabled', false);
$("#stop-lls-btn").attr('disabled', true);
$('#send-message-btn').attr('disabled', false);
$('#send-invitation-btn').attr('disabled', false);
setRecordingState(VoxeetSDK.recording.current != null);
} catch (error) {
logError(error);
}
});
$('#conference-listen-btn').click(async () => {
try {
const liveRecording = $('#chk-live-recording')[0].checked;
// Default conference parameters
// See: https://docs.dolby.io/communications-apis/docs/js-client-sdk-model-conferenceparameters
const conferenceParams = {
liveRecording: liveRecording,
rtcpMode: "average", // worst, average, max
ttl: 0,
videoCodec: "H264", // H264, VP8
dolbyVoice: true
};
// See: https://docs.dolby.io/communications-apis/docs/js-client-sdk-model-conferenceoptions
const conferenceOptions = {
alias: $('#conference-alias-input').val(),
params: conferenceParams
};
// 1. Create a conference room with an alias
const conference = await VoxeetSDK.conference.create(conferenceOptions);
logMessage(`Conference id: ${conference.id} & Conference alias ${conference.alias}`);
conferenceId = conference.id;
const listenOptions = {};
if (conferenceAccessToken) {
listenOptions.conferenceAccessToken = conferenceAccessToken;
}
// 2. Join the conference
await VoxeetSDK.conference.listen(conference, listenOptions);
await updateDevices();
$('#btn-set-webrtc-constraints').attr('disabled', false);
$('#chk-live-recording').attr('disabled', true);
$('#conference-join-btn').attr('disabled', true);
$('#conference-listen-btn').attr('disabled', true);
$('#conference-leave-btn').attr('disabled', false);
$('#conference-alias-input').attr('readonly', true);
$('#start-video-btn').attr('disabled', true);
$('#stop-video-btn').attr('disabled', true);
$('#start-audio-btn').attr('disabled', true);
$('#stop-audio-btn').attr('disabled', true);
$('#mute-audio-btn').attr('disabled', true);
$('#unmute-audio-btn').attr('disabled', true);
$('#start-screenshare-btn').attr('disabled', true);
$('#stop-screenshare-btn').attr('disabled', true);
$('#video-url-input').attr('readonly', true);
$("#video-start-btn").attr('disabled', true);
$("#video-stop-btn").attr('disabled', true);
$("#video-pause-btn").attr('disabled', true);
$("#video-play-btn").attr('disabled', true);
$("#start-recording-btn").attr('disabled', true);
$("#stop-recording-btn").attr('disabled', true);
$('#recording-status')
.removeClass('fa-circle').addClass('fa-stop-circle')
.removeClass('red').addClass('gray');
$('#rtmp-status').removeClass('red').addClass('gray');
$("#rtmp-url-input").attr('readonly', false);
$("#start-rtmp-btn").attr('disabled', true);
$("#stop-rtmp-btn").attr('disabled', true);
$('#lls-status').removeClass('red').addClass('gray');
$("#lls-label-input").attr('readonly', false);
$("#lls-ptoken-input").attr('readonly', false);
$("#start-lls-btn").attr('disabled', true);
$("#stop-lls-btn").attr('disabled', true);
$('#send-message-btn').attr('disabled', false);
$('#send-invitation-btn').attr('disabled', false);
setRecordingState(VoxeetSDK.recording.current != null);
} catch (error) {
logError(error);
}
});
$('#conference-leave-btn').click(async () => {
try {
// Unsubscribe from the participant joined/left events
const alias = VoxeetSDK.conference.current.alias;
await VoxeetSDK.notification.unsubscribe([
{
type: "Participant.Left",
conferenceAlias: alias
},
{
type: "Participant.Joined",
conferenceAlias: alias
},
{
type: "Conference.ActiveParticipants",
conferenceAlias: alias
}]
);
// Leave the conference
await VoxeetSDK.conference.leave();
conferenceAccessToken = null;
$('#chk-live-recording').attr('disabled', false);
$('#btn-set-output-audio-device').attr('disabled', true);
$('#btn-set-input-audio-device').attr('disabled', true);
$('#btn-set-video-device').attr('disabled', true);
$('#btn-set-webrtc-constraints').attr('disabled', true);
$("#conference-join-btn").attr('disabled', false);
$("#conference-listen-btn").attr('disabled', false);
$("#conference-leave-btn").attr('disabled', true);
$('#conference-alias-input').attr('readonly', false);
$('[data-conference="on"] button').attr('disabled', true);
$('#video-url-input').attr('readonly', false);
$('#recording-status')
.removeClass('fa-circle').addClass('fa-stop-circle')
.removeClass('red').addClass('gray');
$('#rtmp-status').removeClass('red').addClass('gray');
$("#rtmp-url-input").attr('readonly', false);
// Empty the last video elements
$('#streams-containers').empty();
// Empty the list of participants
$('#participants-list').empty();
} catch (error) {
logError(error);
}
});
$('#btn-set-video-device').click(async () => {
await VoxeetSDK.mediaDevice.selectVideoInput($('#video-devices').val());
});
$('#btn-set-input-audio-device').click(async () => {
await VoxeetSDK.mediaDevice.selectAudioInput($('#input-audio-devices').val());
});
$('#btn-set-output-audio-device').click(async () => {
await VoxeetSDK.mediaDevice.selectAudioOutput($('#output-audio-devices').val());
});
const startVideo = () => {
const hasAudio = VoxeetSDK.session.participant.streams.length && VoxeetSDK.session.participant.streams[0].getAudioTracks().length > 0;
const payloadConstraints = getConstraints(hasAudio, true);
if (payloadConstraints.constraints.video == true) {
payloadConstraints.constraints.video = { deviceId: $('#video-devices').val() };
} else {
payloadConstraints.constraints.video.deviceId = $('#video-devices').val();
}
logMessage("VoxeetSDK.conference.startVideo with the options:");
logMessage(JSON.stringify(payloadConstraints.constraints.video));
// Start sharing the video with the other participants
VoxeetSDK.conference
.startVideo(VoxeetSDK.session.participant, payloadConstraints.constraints.video)
.then(() => {
$("#start-video-btn").attr('disabled', true);
$("#stop-video-btn").attr('disabled', false);
})
.catch((err) => logError(err));
};
$("#start-video-btn").click(startVideo);
$("#stop-video-btn").click(() => {
logMessage("VoxeetSDK.conference.stopVideo");
// Stop sharing the video with the other participants
VoxeetSDK.conference.stopVideo(VoxeetSDK.session.participant)
.then(() => {
$("#start-video-btn").attr('disabled', false);
$("#stop-video-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
// Add a video stream to the web page
const addVideoNode = (participant, stream) => {
let element = $(`#stream-${participant.id}`);
if (!element.length) {
let data = {
id: participant.id,
name: participant.info.name
};
let template = $.templates("#template-video");
element = $(template.render(data));
$("#streams-containers").append(element);
}
updateVideoMessage(participant, stream);
// Attach the video steam to the video element
let video = element.find('video')[0];
navigator.attachMediaStream(video, stream);
};
const updateVideoMessage = (participant, stream) => {
let element = $(`#stream-${participant.id}`);
if (element.length) {
let text = 'unknown resolution';
if (stream.getVideoTracks().length > 0) {
let streamSettings = stream.getVideoTracks()[0].getSettings();
if (streamSettings && streamSettings.width) {
text = `Resolution ${streamSettings.width} x ${streamSettings.height}`;
}
}
element.find('.resolution').text(text);
}
}
// Remove the video stream from the web page
const removeVideoNode = (participant) => {
const video = $(`#stream-${participant.id} video`);
if (video.length) {
video[0].srcObject = null; // Prevent memory leak in Chrome
}
$(`#stream-${participant.id}`).remove();
};
$("#start-audio-btn").click(() => {
logMessage("VoxeetSDK.conference.startAudio");
// Start sharing the audio with the other participants
VoxeetSDK.conference.startAudio(VoxeetSDK.session.participant)
.then(() => VoxeetSDK.mediaDevice.selectAudioInput($('#input-audio-devices').val()))
.then(() => {
$("#start-audio-btn").attr('disabled', true);
$("#stop-audio-btn").attr('disabled', false);
$("#mute-audio-btn").attr('disabled', false);
$("#unmute-audio-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
$("#stop-audio-btn").click(() => {
logMessage("VoxeetSDK.conference.stopAudio");
// Stop sharing the audio with the other participants
VoxeetSDK.conference.stopAudio(VoxeetSDK.session.participant)
.then(() => {
$("#start-audio-btn").attr('disabled', false);
$("#stop-audio-btn").attr('disabled', true);
$("#mute-audio-btn").attr('disabled', true);
$("#unmute-audio-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
$("#mute-audio-btn").click(() => {
logMessage("VoxeetSDK.conference.mute true");
VoxeetSDK.conference.mute(VoxeetSDK.session.participant, true);
$("#mute-audio-btn").attr('disabled', true);
$("#unmute-audio-btn").attr('disabled', false);
});
$("#unmute-audio-btn").click(() => {
logMessage("VoxeetSDK.conference.mute false");
VoxeetSDK.conference.mute(VoxeetSDK.session.participant, false);
$("#mute-audio-btn").attr('disabled', false);
$("#unmute-audio-btn").attr('disabled', true);
});
$("#start-screenshare-btn").click(() => {
logMessage('VoxeetSDK.conference.startScreenShare');
// Start screen sharing with the other participants
VoxeetSDK.conference.startScreenShare()
.then(() => {
$("#start-screenshare-btn").attr('disabled', true);
$("#stop-screenshare-btn").attr('disabled', false);
})
.catch((err) => logError(err));
});
$("#stop-screenshare-btn").click(() => {
logMessage("VoxeetSDK.conference.stopScreenShare");
// Stop screen sharing with the other participants
VoxeetSDK.conference.stopScreenShare()
.then(() => {
$("#start-screenshare-btn").attr('disabled', false);
$("#stop-screenshare-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
// Add a screen share stream to the web page
const addScreenShareNode = (participant, stream) => {
let element = $('#stream-screenshare');
if (!element.length) {
let data = {
name: participant.info.name
};
let template = $.templates("#template-screenshare");
element = $(template.render(data));
$("#streams-containers").append(element);
}
// Attach the video steam to the video element
let video = element.find('video')[0];
navigator.attachMediaStream(video, stream);
}
// Remove the screen share stream from the web page
const removeScreenShareNode = () => {
const video = $('#stream-screenshare video');
if (video.length) {
video[0].srcObject = null; // Prevent memory leak in Chrome
}
$('#stream-screenshare').remove();
$("#start-screenshare-btn").attr('disabled', false);
$("#stop-screenshare-btn").attr('disabled', true);
}
// Add a new participant to the list
const addUpdateParticipantNode = (participant) => {
let template = $.templates("#template-participant");
let elem = $(`#participant-${participant.id}`);
const element = $(template.render({
id: participant.id,
avatarUrl: participant.info.avatarUrl,
name: participant.info.name,
status: participant.status,
isLocal: participant.id === VoxeetSDK.session.participant.id,
}));
if (!elem.length) {
element.appendTo('#participants-list');
} else {
elem.replaceWith(element);
}
};
// Remove a participant from the list
const removeParticipantNode = (participant) => {
$(`#participant-${participant.id}`).remove();
};
$("#video-start-btn").click(() => {
const videoUrl = $('#video-url-input').val();
logMessage(`VoxeetSDK.videoPresentation.start ${videoUrl}`);
VoxeetSDK.videoPresentation
.start(videoUrl)
.then(() => {
$('#video-url-input').attr('readonly', true);
$("#video-start-btn").attr('disabled', true);
$("#video-stop-btn").attr('disabled', false);
$("#video-pause-btn").attr('disabled', false);
$("#video-play-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
$("#video-stop-btn").click(() => {
logMessage('VoxeetSDK.videoPresentation.stop');
VoxeetSDK.videoPresentation
.stop()
.then(() => {
$('#video-url-input').attr('readonly', false);
$("#video-start-btn").attr('disabled', false);
$("#video-stop-btn").attr('disabled', true);
$("#video-pause-btn").attr('disabled', true);
$("#video-play-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
$("#video-pause-btn").click(() => {
const timestamp = Math.round($(`#stream-video video`)[0].currentTime * 1000);
logMessage(`VoxeetSDK.videoPresentation.pause at ${timestamp}ms`);
VoxeetSDK.videoPresentation
.pause(timestamp)
.then(() => {
$("#video-pause-btn").attr('disabled', true);
$("#video-play-btn").attr('disabled', false);
})
.catch((err) => logError(err));
});
$("#video-play-btn").click(() => {
logMessage('VoxeetSDK.videoPresentation.play');
VoxeetSDK.videoPresentation
.play()
.then(() => {
$("#video-pause-btn").attr('disabled', false);
$("#video-play-btn").attr('disabled', true);
})
.catch((err) => logError(err));
});
const addVideoPlayer = (videoUrl) => {
let element = $(`#stream-video`);
if (!element.length) {
let data = {
url: videoUrl
};
let template = $.templates("#template-video-url");
element = $(template.render(data));
$("#streams-containers").append(element);
}
};
/**
* RECORDING
*/
const setRecordingState = (isRecording) => {
if (isRecording) {
$('#recording-status')
.removeClass('fa-stop-circle').addClass('fa-circle')
.removeClass('gray').addClass('red');
} else {
$('#recording-status')
.removeClass('fa-circle').addClass('fa-stop-circle')
.removeClass('red').addClass('gray');
}
$("#start-recording-btn").attr('disabled', isRecording);
$("#stop-recording-btn").attr('disabled', !isRecording);
};
$("#start-recording-btn").click(() => {
logMessage('VoxeetSDK.recording.start()');
// Start recording the conference
VoxeetSDK.recording.start()
.then(() => setRecordingState(true))
.catch((err) => logError(err));
});
$("#stop-recording-btn").click(() => {
logMessage('VoxeetSDK.recording.stop()');
// Stop recording the conference
VoxeetSDK.recording.stop()
.then(() => setRecordingState(false))
.catch((err) => logError(err));
});
/**
* Send a message
*/
$('#send-message-btn').click(() => {
logMessage('VoxeetSDK.command.send()');
VoxeetSDK.command
.send($("#message-input").val())
.then(() => {
$("#message-input").val();
})
.catch((err) => logError(err));
});
/**
* Send invitation
*/
$('#send-invitation-btn').click(() => {
const externalId = $("#invite-input").val();
logMessage(`VoxeetSDK.notification.invite('${externalId}')`);
var participants = [
{ externalId: externalId }
];
VoxeetSDK.notification
.invite(VoxeetSDK.conference.current, participants)
.then(() => {
logMessage(`Invitation sent to ${externalId}`);
})
.catch((err) => logError(err));
});
const isAppKeyConfigured = () => {
return appKey && appKey !== "<APP_KEY>" && appSecret && appSecret !== "<APP_SECRET>";
};
const getClientAccessToken = () => {
return new Promise((resolve, reject) => {
$.ajax({
async : true,
type: "POST",
url: "https://session.voxeet.com/v1/oauth2/token",
contentType: "application/x-www-form-urlencoded",
data: "grant_type=client_credentials",
headers: {
"Accept": "application/json",
"Cache-Control": "no-cache",
"Authorization": "Basic " + btoa(`${appKey}:${appSecret}`),
}
}).done(function (data) {
resolve(data);
}).fail(function (err) {
reject(err);
});
});
};
const getAPIToken = () => {
return new Promise((resolve, reject) => {
$.ajax({
async : true,
type: "POST",
url: "https://api.voxeet.com/v1/auth/token",
contentType: "application/x-www-form-urlencoded",
data: "grant_type=client_credentials",
headers: {
"Accept": "application/json",
"Cache-Control": "no-cache",
"Authorization": "Basic " + btoa(`${appKey}:${appSecret}`),
}
}).done(function (data) {
resolve(data);
}).fail(function (err) {
reject(err);
});
});
};
/**
* RTMP Streaming
*/
$("#start-rtmp-btn").click(async () => {
const rtmpUrl = $('#rtmp-url-input').val();
logMessage(`Start RTMP stream to ${rtmpUrl}`);
const jwt = await getAPIToken();
const url = `https://api.voxeet.com/v2/conferences/mix/${conferenceId}/rtmp/start`;
$.ajax({
async : true,
type: "POST",
url: url,
contentType: "application/json",
data: JSON.stringify({ uri: rtmpUrl }),
headers: {
"Authorization": "Bearer " + jwt.access_token
}
}).done(function () {
$('#rtmp-status').addClass('red').removeClass('gray');
$("#rtmp-url-input").attr('readonly', true);
$("#start-rtmp-btn").attr('disabled', true);
$("#stop-rtmp-btn").attr('disabled', false);
}).fail(function (err) {
logError(err);
});
});
$("#stop-rtmp-btn").click(async () => {
logMessage('Stop the RTMP stream');
const jwt = await getAPIToken();
const url = `https://api.voxeet.com/v2/conferences/mix/${conferenceId}/rtmp/stop`;
$.ajax({
async : true,
type: "POST",
url: url,
headers: {
"Authorization": "Bearer " + jwt.access_token
}
}).done(function () {
$('#rtmp-status').removeClass('red').addClass('gray');
$("#rtmp-url-input").attr('readonly', false);
$("#start-rtmp-btn").attr('disabled', false);
$("#stop-rtmp-btn").attr('disabled', true);
}).fail(function (err) {
logError(err);
});
});
/**
* Low Latency Streaming (LLS)
*/
$("#start-lls-btn").click(async () => {
const streamName = $('#lls-stream-name-input').val();
const pToken = $('#lls-ptoken-input').val();
logMessage(`Start LLS to ${streamName}`);
const jwt = await getAPIToken();
const url = `https://comms.api.dolby.io/v2/conferences/mix/${conferenceId}/lls/start`;
$.ajax({
async : true,
type: "POST",
url: url,
contentType: "application/json",
data: JSON.stringify({ streamName: streamName, publishingToken: pToken }),
headers: {
"Authorization": "Bearer " + jwt.access_token
}
}).done(function () {
logMessage('LLS start success!')
$('#lls-status').addClass('red').removeClass('gray');
$("#lls-stream-name-input").attr('readonly', true);
$("#lls-ptoken-input").attr('readonly', true);
$("#start-lls-btn").attr('disabled', true);
$("#stop-lls-btn").attr('disabled', false);
}).fail(function (err) {
logError(err);
});
});
$("#stop-lls-btn").click(async () => {
logMessage('Stop the LLS');
const jwt = await getAPIToken();
const url = `https://comms.api.dolby.io/v2/conferences/mix/${conferenceId}/lls/stop`;
$.ajax({
async : true,
type: "POST",
url: url,
contentType: "application/json",
data: JSON.stringify({}),
headers: {
"Authorization": "Bearer " + jwt.access_token
}
}).done(function () {
$('#lls-status').removeClass('red').addClass('gray');
$("#lls-stream-name-input").attr('readonly', false);
$("#lls-ptoken-input").attr('readonly', false);
$("#start-lls-btn").attr('disabled', false);
$("#stop-lls-btn").attr('disabled', true);
}).fail(function (err) {
logError(err);
});
});
$("#btn-use-sdk-versions").click(async () => {
const script = document.createElement('script');
const sdkVersion = $('#sdk-versions').val();
script.src = `https://cdn.jsdelivr.net/npm/@voxeet/voxeet-web-sdk@${sdkVersion}/dist/voxeet-sdk.js`;
script.addEventListener('load', async () => {
logMessage(`Dolby.io Communications SDK version ${sdkVersion} loaded from ${script.src}`);
const _isAppKeyConfigured = isAppKeyConfigured();
if (!_isAppKeyConfigured) {
// Hide backend operations when the API Key / Secret are not configured
$('[data-app-key-defined="yes"]').hide();
}
// Automatically try to load the Access Token
const urlParams = new URLSearchParams(window.location.search);
const accessToken = urlParams.get('token');
if (accessToken && accessToken.length > 0) {
$('#access-token-input').val(accessToken);
initializeSDK(accessToken);
} else if (_isAppKeyConfigured) {
const jwt = await getClientAccessToken();
$('#access-token-input').val(jwt.access_token);
initializeSDK(jwt.access_token);
} else {
$('#initialize-btn').attr('disabled', false);
}
// Set the Dolby.io SDK Version
$('#sdk-version').text(VoxeetSDK.version);
registerEvents();
});
// Append to the `head` element
document.head.appendChild(script);
$('#btn-use-sdk-versions').attr('disabled', true);
});
$(function() {
// Generate a random username
let rand = Math.round(Math.random() * 10000);
$('#external-id-input').val(`guest-${rand}`);
$('#username-input').val(`Guest ${rand}`);
$('#avatar-url-input').val(`https://gravatar.com/avatar/${rand}?s=200&d=identicon`);
// Generate a random conference alias
let conferenceAlias = "conf-" + Math.round(Math.random() * 10000);
$('#conference-alias-input').val(conferenceAlias);
});