-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVidyoConnector.js
575 lines (521 loc) · 24.4 KB
/
VidyoConnector.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
function ShowRenderer(vidyoConnector) {
var rndr = document.getElementById('renderer');
vidyoConnector.ShowViewAt("renderer", rndr.offsetLeft, rndr.offsetTop, rndr.offsetWidth, rndr.offsetHeight);
}
// Run StartVidyoConnector when the VidyoClient is successfully loaded
function StartVidyoConnector(VC, webrtc, webrtcExtensionPath, configParams) {
var vidyoConnector;
var cameras = {};
var microphones = {};
var speakers = {};
var cameraPrivacy = false;
var microphonePrivacy = false;
window.onresize = function()
{
ShowRenderer(vidyoConnector);
};
VC.CreateVidyoConnector({
viewId: "renderer", // Div ID where the composited video will be rendered, see VidyoConnector.html;
viewStyle: "VIDYO_CONNECTORVIEWSTYLE_Default", // Visual style of the composited renderer
remoteParticipants: 15, // Maximum number of participants to render
logFileFilter: "warning info@VidyoClient info@VidyoConnector",
logFileName:"",
userData:""
}).then(function(vc) {
vidyoConnector = vc;
ShowRenderer(vidyoConnector);
registerDeviceListeners(vidyoConnector, cameras, microphones, speakers);
handleDeviceChange(vidyoConnector, cameras, microphones, speakers);
handleParticipantChange(vidyoConnector);
handleSharing(vidyoConnector, webrtc, webrtcExtensionPath);
// Populate the connectionStatus with the client version
vidyoConnector.GetVersion().then(function(version) {
$("#clientVersion").html("v " + version);
}).catch(function() {
console.error("GetVersion failed");
});
// If enableDebug is configured then enable debugging
if (configParams.enableDebug === "1") {
vidyoConnector.EnableDebug({port:7776, logFilter: "warning info@VidyoClient info@VidyoConnector"}).then(function() {
console.log("EnableDebug success");
}).catch(function() {
console.error("EnableDebug failed");
});
}
// Handle camera privacy and microphone privacy initial state
if (configParams.cameraPrivacy === "1") {
$("#cameraButton").click();
}
if (configParams.microphonePrivacy === "1") {
$("#microphoneButton").click();
}
// Join the conference if the autoJoin URL parameter was enabled
if (configParams.autoJoin === "1") {
joinLeave();
} else {
// Handle the join in the toolbar button being clicked by the end user.
$("#joinLeaveButton").one("click", joinLeave);
}
}).catch(function(err) {
console.error("CreateVidyoConnector Failed " + err);
});
// Handle the camera privacy button, toggle between show and hide.
$("#cameraButton").click(function() {
// CameraPrivacy button clicked
cameraPrivacy = !cameraPrivacy;
vidyoConnector.SetCameraPrivacy({
privacy: cameraPrivacy
}).then(function() {
if (cameraPrivacy) {
$("#cameraButton").addClass("cameraOff").removeClass("cameraOn");
} else {
$("#cameraButton").addClass("cameraOn").removeClass("cameraOff");
}
console.log("SetCameraPrivacy Success");
}).catch(function() {
console.error("SetCameraPrivacy Failed");
});
});
// Handle the microphone mute button, toggle between mute and unmute audio.
$("#microphoneButton").click(function() {
// MicrophonePrivacy button clicked
microphonePrivacy = !microphonePrivacy;
vidyoConnector.SetMicrophonePrivacy({
privacy: microphonePrivacy
}).then(function() {
if (microphonePrivacy) {
$("#microphoneButton").addClass("microphoneOff").removeClass("microphoneOn");
} else {
$("#microphoneButton").addClass("microphoneOn").removeClass("microphoneOff");
}
console.log("SetMicrophonePrivacy Success");
}).catch(function() {
console.error("SetMicrophonePrivacy Failed");
});
});
// Handle the options visibility button, toggle between show and hide options.
$("#optionsVisibilityButton").click(function() {
// OptionsVisibility button clicked
if ($("#optionsVisibilityButton").hasClass("hideOptions")) {
$("#options").addClass("hidden");
$("#optionsVisibilityButton").addClass("showOptions").removeClass("hideOptions");
$("#renderer").addClass("rendererFullScreen").removeClass("rendererWithOptions");
} else {
$("#options").removeClass("hidden");
$("#optionsVisibilityButton").addClass("hideOptions").removeClass("showOptions");
$("#renderer").removeClass("rendererFullScreen").addClass("rendererWithOptions");
}
});
function joinLeave() {
// join or leave dependent on the joinLeaveButton, whether it
// contains the class callStart or callEnd.
if ($("#joinLeaveButton").hasClass("callStart")) {
$("#connectionStatus").html("Connecting...");
$("#joinLeaveButton").removeClass("callStart").addClass("callEnd");
$('#joinLeaveButton').prop('title', 'Leave Conference');
connectToConference(vidyoConnector);
} else {
$("#connectionStatus").html("Disconnecting...");
vidyoConnector.Disconnect().then(function() {
console.log("Disconnect Success");
}).catch(function() {
console.error("Disconnect Failure");
});
}
$("#joinLeaveButton").one("click", joinLeave);
}
}
function registerDeviceListeners(vidyoConnector, cameras, microphones, speakers) {
// Map the "None" option (whose value is 0) in the camera, microphone, and speaker drop-down menus to null since
// a null argument to SelectLocalCamera, SelectLocalMicrophone, and SelectLocalSpeaker releases the resource.
cameras[0] = null;
microphones[0] = null;
speakers[0] = null;
// Handle appearance and disappearance of camera devices in the system
vidyoConnector.RegisterLocalCameraEventListener({
onAdded: function(localCamera) {
// New camera is available
$("#cameras").append("<option value='" + window.btoa(localCamera.id) + "'>" + localCamera.name + "</option>");
cameras[window.btoa(localCamera.id)] = localCamera;
},
onRemoved: function(localCamera) {
// Existing camera became unavailable
$("#cameras option[value='" + window.btoa(localCamera.id) + "']").remove();
delete cameras[window.btoa(localCamera.id)];
},
onSelected: function(localCamera) {
// Camera was selected/unselected by you or automatically
if(localCamera) {
$("#cameras option[value='" + window.btoa(localCamera.id) + "']").prop('selected', true);
}
},
onStateUpdated: function(localCamera, state) {
// Camera state was updated
}
}).then(function() {
console.log("RegisterLocalCameraEventListener Success");
}).catch(function() {
console.error("RegisterLocalCameraEventListener Failed");
});
// Handle appearance and disappearance of microphone devices in the system
vidyoConnector.RegisterLocalMicrophoneEventListener({
onAdded: function(localMicrophone) {
// New microphone is available
$("#microphones").append("<option value='" + window.btoa(localMicrophone.id) + "'>" + localMicrophone.name + "</option>");
microphones[window.btoa(localMicrophone.id)] = localMicrophone;
},
onRemoved: function(localMicrophone) {
// Existing microphone became unavailable
$("#microphones option[value='" + window.btoa(localMicrophone.id) + "']").remove();
delete microphones[window.btoa(localMicrophone.id)];
},
onSelected: function(localMicrophone) {
// Microphone was selected/unselected by you or automatically
if(localMicrophone)
$("#microphones option[value='" + window.btoa(localMicrophone.id) + "']").prop('selected', true);
},
onStateUpdated: function(localMicrophone, state) {
// Microphone state was updated
}
}).then(function() {
console.log("RegisterLocalMicrophoneEventListener Success");
}).catch(function() {
console.error("RegisterLocalMicrophoneEventListener Failed");
});
// Handle appearance and disappearance of speaker devices in the system
vidyoConnector.RegisterLocalSpeakerEventListener({
onAdded: function(localSpeaker) {
// New speaker is available
$("#speakers").append("<option value='" + window.btoa(localSpeaker.id) + "'>" + localSpeaker.name + "</option>");
speakers[window.btoa(localSpeaker.id)] = localSpeaker;
},
onRemoved: function(localSpeaker) {
// Existing speaker became unavailable
$("#speakers option[value='" + window.btoa(localSpeaker.id) + "']").remove();
delete speakers[window.btoa(localSpeaker.id)];
},
onSelected: function(localSpeaker) {
// Speaker was selected/unselected by you or automatically
if(localSpeaker)
$("#speakers option[value='" + window.btoa(localSpeaker.id) + "']").prop('selected', true);
},
onStateUpdated: function(localSpeaker, state) {
// Speaker state was updated
}
}).then(function() {
console.log("RegisterLocalSpeakerEventListener Success");
}).catch(function() {
console.error("RegisterLocalSpeakerEventListener Failed");
});
}
function handleDeviceChange(vidyoConnector, cameras, microphones, speakers) {
// Hook up camera selector functions for each of the available cameras
$("#cameras").change(function() {
// Camera selected from the drop-down menu
$("#cameras option:selected").each(function() {
camera = cameras[$(this).val()];
vidyoConnector.SelectLocalCamera({
localCamera: camera
}).then(function() {
console.log("SelectCamera Success");
}).catch(function() {
console.error("SelectCamera Failed");
});
});
});
// Hook up microphone selector functions for each of the available microphones
$("#microphones").change(function() {
// Microphone selected from the drop-down menu
$("#microphones option:selected").each(function() {
microphone = microphones[$(this).val()];
vidyoConnector.SelectLocalMicrophone({
localMicrophone: microphone
}).then(function() {
console.log("SelectMicrophone Success");
}).catch(function() {
console.error("SelectMicrophone Failed");
});
});
});
// Hook up speaker selector functions for each of the available speakers
$("#speakers").change(function() {
// Speaker selected from the drop-down menu
$("#speakers option:selected").each(function() {
speaker = speakers[$(this).val()];
vidyoConnector.SelectLocalSpeaker({
localSpeaker: speaker
}).then(function() {
console.log("SelectSpeaker Success");
}).catch(function() {
console.error("SelectSpeaker Failed");
});
});
});
}
function handleSharing(vidyoConnector, webrtc, webrtcExtensionPath) {
var monitorShares = {};
var windowShares = {};
var isSharingWindow = false; // Flag indicating whether a window is currently being shared
var webrtcMode = (webrtc === "true"); // Whether the app is running in plugin or webrtc mode
// The monitorShares & windowShares associative arrays hold a handle to each window/monitor that are available for sharing.
// The element with key "0" contains a value of null, which is used to stop sharing.
monitorShares[0] = null;
windowShares[0] = null;
// Check if app is running in plugin or webrtc mode
if (webrtcMode === false) {
// In plugin mode, start window and monitor sharing
StartWindowShare();
StartMonitorShare();
} else {
// In webrtc mode, start window sharing only.
// StartWindowShare needs to be called each time a new share is initiated
// so perform this action when the "Window Share" drop-down list is clicked.
$("#windowShares").mousedown(function() {
console.log("*** Window Share drop-down clicked. isSharingWindow = " + isSharingWindow);
// Initiate the share selection process only if not already sharing
if (isSharingWindow === false) {
// Re-initialize the windowShares array
windowShares = {};
windowShares[0] = null;
// Clear all of the drop-down items other than the first ("None"), which is used to stop sharing
$("#windowShares").find('option').not(':first').remove();
// Start window sharing (in WebRTC mode, this includes monitors)
StartWindowShare();
}
});
}
function StartWindowShare() {
// Register for window share status updates, which operates differently in plugin vs webrtc:
// plugin: onAdded and onRemoved callbacks are received for each available window
// webrtc: a popup is displayed (an extension to Firefox/Chrome) which allows the user to
// select a share; once selected, that share will trigger an onAdded event
vidyoConnector.RegisterLocalWindowShareEventListener({
onAdded: function(localWindowShare) {
// In webrtc mode, select the share which triggered this callback
if (webrtcMode) {
vidyoConnector.SelectLocalWindowShare({
localWindowShare: localWindowShare
}).then(function() {
console.log("SelectLocalWindowShare Success");
}).catch(function() {
console.error("SelectLocalWindowShare Failed");
});
}
// New share is available so add it to the windowShares array and the drop-down list
if (localWindowShare.name != "") {
var shareVal;
if (webrtcMode) {
shareVal = "Selected Share";
} else {
shareVal = localWindowShare.applicationName + " : " + localWindowShare.name;
}
$("#windowShares").append("<option value='" + window.btoa(localWindowShare.id) + "'>" + shareVal + "</option>");
windowShares[window.btoa(localWindowShare.id)] = localWindowShare;
console.log("Window share added, name : " + localWindowShare.name + " | id : " + window.btoa(localWindowShare.id));
}
},
onRemoved: function(localWindowShare) {
// Existing share became unavailable
$("#windowShares option[value='" + window.btoa(localWindowShare.id) + "']").remove();
delete windowShares[window.btoa(localWindowShare.id)];
},
onSelected: function(localWindowShare) {
// Share was selected/unselected by you or automatically
if (localWindowShare) {
$("#windowShares option[value='" + window.btoa(localWindowShare.id) + "']").prop('selected', true);
isSharingWindow = true;
console.log("Window share selected : " + localWindowShare.name);
} else {
isSharingWindow = false;
}
},
onStateUpdated: function(localWindowShare, state) {
// localWindowShare state was updated
}
}).then(function(result) {
if (result) {
console.log("RegisterLocalWindowShareEventListener Success");
} else {
console.error("RegisterLocalWindowShareEventListener Failed");
if (webrtcExtensionPath.length === 0) {
alert("Error: cannot initiate window sharing.");
} else if (webrtcMode) {
prompt("An extension is needed to initiate window sharing. Navigate to the URL below to install.", webrtcExtensionPath);
}
}
}).catch(function() {
console.error("RegisterLocalWindowShareEventListener Failed");
});
}
function StartMonitorShare() {
// Register for monitor share status updates
vidyoConnector.RegisterLocalMonitorEventListener({
onAdded: function(localMonitorShare) {
// New share is available so add it to the monitorShares array and the drop-down list
if (localMonitorShare.name != "") {
$("#monitorShares").append("<option value='" + window.btoa(localMonitorShare.id) + "'>" + localMonitorShare.name + "</option>");
monitorShares[window.btoa(localMonitorShare.id)] = localMonitorShare;
console.log("Monitor share added, name : " + localMonitorShare.name + " | id : " + window.btoa(localMonitorShare.id));
}
},
onRemoved: function(localMonitorShare) {
// Existing share became unavailable
$("#monitorShares option[value='" + window.btoa(localMonitorShare.id) + "']").remove();
delete monitorShares[window.btoa(localMonitorShare.id)];
},
onSelected: function(localMonitorShare) {
// Share was selected/unselected by you or automatically
if (localMonitorShare) {
$("#monitorShares option[value='" + window.btoa(localMonitorShare.id) + "']").prop('selected', true);
console.log("Monitor share selected : " + localMonitorShare.name);
}
},
onStateUpdated: function(localMonitorShare, state) {
// localMonitorShare state was updated
}
}).then(function() {
console.log("RegisterLocalMonitorShareEventListener Success");
}).catch(function() {
console.error("RegisterLocalMonitorShareEventListener Failed");
});
}
// A monitor was selected from the "Monitor Share" drop-down list (plugin mode only).
$("#monitorShares").change(function() {
console.log("*** Monitor shares change called");
// Find the share selected from the drop-down list
$("#monitorShares option:selected").each(function() {
share = monitorShares[$(this).val()];
// Select the local monitor
vidyoConnector.SelectLocalMonitor({
localMonitor: share
}).then(function() {
console.log("SelectLocalMonitor Success");
}).catch(function() {
console.error("SelectLocalMonitor Failed");
});
});
});
// A window was selected from the "Window Share" drop-down list.
// Note: in webrtc mode, this is only called for the "None" option (to stop the share) since
// the share is selected in the onAdded callback of the LocalWindowShareEventListener.
$("#windowShares").change(function() {
console.log("*** Window shares change called");
// Find the share selected from the drop-down list
$("#windowShares option:selected").each(function() {
share = windowShares[$(this).val()];
// Select the local window share
vidyoConnector.SelectLocalWindowShare({
localWindowShare: share
}).then(function() {
console.log("SelectLocalWindowShare Success");
}).catch(function() {
console.error("SelectLocalWindowShare Failed");
});
});
});
}
function getParticipantName(participant, cb) {
if (!participant) {
cb("Undefined");
return;
}
if (participant.name) {
cb(participant.name);
return;
}
participant.GetName().then(function(name) {
cb(name);
}).catch(function() {
cb("GetNameFailed");
});
}
function handleParticipantChange(vidyoConnector) {
vidyoConnector.RegisterParticipantEventListener({
onJoined: function(participant) {
getParticipantName(participant, function(name) {
$("#participantStatus").html("" + name + " Joined");
});
},
onLeft: function(participant) {
getParticipantName(participant, function(name) {
$("#participantStatus").html("" + name + " Left");
});
},
onDynamicChanged: function(participants, cameras) {
// Order of participants changed
},
onLoudestChanged: function(participant, audioOnly) {
getParticipantName(participant, function(name) {
$("#participantStatus").html("" + name + " Speaking");
});
}
}).then(function() {
console.log("RegisterParticipantEventListener Success");
}).catch(function() {
console.err("RegisterParticipantEventListener Failed");
});
}
// Attempt to connect to the conference
// We will also handle connection failures
// and network or server-initiated disconnects.
function connectToConference(vidyoConnector) {
// Abort the Connect call if resourceId is invalid. It cannot contain empty spaces or "@".
// Clear messages
$("#error").html("");
$("#message").html("<h3 class='blink'>CONNECTING...</h3>");
var displayName_is='M&S';
var token_is='cHJvdmlzaW9uAGZhaGFkQDczYjVjOS52aWR5by5pbwA2MzY4ODY2MjkxNAAAMTFkMjU0NzdkN2RkOTQzZjBiMmUwZTlmN2EwNGZlODY2ZWI0ZmIwNDViNmU5NjZiMTUwNmQzOThiMDliZTI1MTJhZTBlMDFhMjUzOWVhNmVmNjgxZWZlMDdlYTI5N2E3';
vidyoConnector.Connect({
// Take input from options form
host: 'prod.vidyo.io',
token: token_is,
displayName: displayName_is,
resourceId: 'demoRoom2',
// Define handlers for connection events.
onSuccess: function() {
// Connected
console.log("vidyoConnector.Connect : onSuccess callback received");
$("#connectionStatus").html("Connected");
$("#options").addClass("hidden");
$("#optionsVisibilityButton").addClass("showOptions").removeClass("hideOptions");
$("#renderer").addClass("rendererFullScreen").removeClass("rendererWithOptions");
ShowRenderer(vidyoConnector);
$("#message").html("");
},
onFailure: function(reason) {
// Failed
console.error("vidyoConnector.Connect : onFailure callback received");
connectorDisconnected("Failed", "");
$("#error").html("<h3>Call Failed: " + reason + "</h3>");
},
onDisconnected: function(reason) {
// Disconnected
console.log("vidyoConnector.Connect : onDisconnected callback received");
connectorDisconnected("Disconnected", "Call Disconnected: " + reason);
$("#options").removeClass("hidden");
$("#optionsVisibilityButton").addClass("hideOptions").removeClass("showOptions");
$("#renderer").removeClass("rendererFullScreen").addClass("rendererWithOptions");
ShowRenderer(vidyoConnector);
}
}).then(function(status) {
if (status) {
console.log("Connect Success");
} else {
console.error("Connect Failed");
connectorDisconnected("Failed", "");
$("#error").html("<h3>Call Failed" + "</h3>");
}
}).catch(function() {
console.error("Connect Failed");
connectorDisconnected("Failed", "");
$("#error").html("<h3>Call Failed" + "</h3>");
});
}
// Connector either fails to connect or a disconnect completed, update UI elements
function connectorDisconnected(connectionStatus, message) {
$("#connectionStatus").html(connectionStatus);
$("#message").html(message);
$("#participantStatus").html("");
$("#joinLeaveButton").removeClass("callEnd").addClass("callStart");
$('#joinLeaveButton').prop('title', 'Join Conference');
}