-
Notifications
You must be signed in to change notification settings - Fork 2
/
script.js
511 lines (505 loc) · 14.7 KB
/
script.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
const Toast = swal.mixin({
toast: true,
position: "top-right",
iconColor: "white",
customClass: {
popup: "colored-toast"
},
showConfirmButton: false,
timer: 1500,
timerProgressBar: true
});
var peer = new Peer({
config: {
iceServers: [
{ url: "stun:stun.l.google.com:19302" },
{ url: "stun:stun1.l.google.com:19302" },
{ url: "stun:stun2.l.google.com:19302" },
{ url: "stun:stun3.l.google.com:19302" }
]
} /* Sample servers, please use appropriate ones */
});
peer.on("close", () => {
document.querySelector(".buttons").classList.add("hide");
document.querySelector("video")?.remove();
swal.fire({
title: "Screen share ended",
icon: "info",
text: "The screen share ended, this can happen because of bad connection, but most of the time because the user sharing their screen stopped.",
confirmButtonText: "Ok. That was fun!",
reverseButtons: true,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
customClass: {
popup: "fullscreen"
}
}).then(() => {
location.reload();
})
})
const peerInitiated = new Promise((res) => peer.on("open", res));
const params = Object.fromEntries(
new URLSearchParams(location.search).entries()
);
if (params.id) {
join(params.id);
} else {
swal
.fire({
title: "Hello!",
text:
"Would you like to share your screen or view someone else's screen?",
confirmButtonText: `<span class="iconify-inline" data-icon="fa-solid:share"></span> Share my screen`,
denyButtonText: `<span class="iconify-inline" data-icon="grommet-icons:personal-computer"></span> View screen`,
showDenyButton: true,
denyButtonColor: "#0006",
reverseButtons: true,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
customClass: {
popup: "fullscreen"
}
})
.then(({ value }) => {
if (value === true /* Share screen */) {
init({ action: "share", peer });
} else {
new swal({
title: "Enter a meeting ID here",
input: "text",
inputLabel: "This should be a meeting ID that you copied",
inputAttributes: {
autofocus: true
},
customClass: {
popup: "fullscreen"
},
backdrop: false,
inputPlaceholder: "Meeting ID here",
showCancelButton: false,
confirmButtonText: `Join`,
showCloseButton: false,
focusConfirm: true,
allowEscapeKey: false,
allowOutsideClick: false
}).then(({ value }) => {
Toast.fire({
icon: "info",
title: "Joining...",
timer: 5000,
});
init({ action: "join", id: value, peer });
});
}
});
}
function join(id) {
Toast.fire({
icon: "info",
title: "Joining...",
timer: 5000,
});
swal
.fire({
title: "View screen",
text: "Press 'Join now' to view the screen being shared",
confirmButtonText: "Join now",
reverseButtons: true,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
customClass: {
popup: "fullscreen"
}
})
.then(() => {
init({ action: "join", id, peer });
});
}
async function init({ action, id, peer }) {
if (action === "join") {
const h = id;
console.log("Waiting for id");
await peerInitiated;
var conn = peer.connect(h);
var errorTimeout = setTimeout(() => {
new swal({
title: "Couldn't join meeting",
icon: "error",
text:
"This could be due to a variety of reasons, perhaps you're behind a firewall, perhaps whoever is sharing has stopped, or maybe the meeting ID's invalid. Sorry!",
customClass: {
popup: "fullscreen"
},
backdrop: false,
showCloseButton: false,
showConfirmButton: false,
allowEscapeKey: false,
allowOutsideClick: false
});
}, 4000);
console.log("Connecting to %o", h);
conn.on("open", function () {
console.log("Sending open message");
conn.send({ type: "share", id: peer.id });
conn.send({ type: "joined", id: peer.id });
window.onbeforeunload = () => conn.send({ type: "left", id: peer.id });
conn.on("data", (data) => {
console.log("Got data", data)
if (data.action === "ended"){
conn.send({type: "destroy"})
setTimeout(() => peer.destroy());
}
if (data.action === "paused"){
document.querySelector("video").classList.add("hide")
swal.fire({
title: "Screen share paused",
icon: "info",
text: "The person sharing their screen paused sharing, when they resume this will go away.",
showConfirmButton: false,
reverseButtons: true,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
customClass: {
popup: "fullscreen"
}
})
}
if (data.action === "resumed"){
swal.close();
document.querySelector("video").classList.remove("hide")
Toast.fire({
icon: "info",
title: "Stream resumed",
});
}
})
});
peer.on("call", (call) => {
clearTimeout(errorTimeout);
swal.close();
console.log("Got response call");
call.answer();
call.on("stream", function (stream) {
console.log("Got stream!!!", stream);
addStream(stream);
});
});
} else if (action === "share") {
await peerInitiated;
const mediaStream = await getMedia();
// If the user denies
if (!mediaStream) return location.reload();
const url = `${peer.id}`;
new swal({
title: "Copy this meeting ID and then send it to someone else",
input: "text",
inputValue: url,
inputAttributes: { readonly: true },
inputPlaceholder: "Copy this",
cancelButtonText: "No thanks",
showCancelButton: false,
confirmButtonText: `<span class="iconify" data-icon="akar-icons:copy"></span> Copy it`,
showCloseButton: true,
focusConfirm: true
}).then(async ({value}) => {
if (value !== true) return;
navigator.clipboard.writeText(url);
await Toast.fire({
icon: "success",
title: "Copied!"
});
});
addStream(mediaStream, true);
console.log("Got media stream");
var connections = [];
document.querySelector(".buttons").classList.remove("hide");
mediaStream.getVideoTracks()[0].onended = function () {
stop(mediaStream);
connections.forEach(i => {
i.send({action: "ended"});
})
Toast.fire({
icon: "info",
title: "Stream stopped",
timer: 5000,
});
setTimeout(() => peer.destroy(), 100);
};
document.querySelector("#stop").onclick = ({currentTarget: el}) => {
Toast.fire({
icon: "info",
title: "Stream stopped",
timer: 5000,
});
stop(mediaStream);
connections.forEach(i => {
i.send({action: "ended"});
})
setTimeout(() => peer.destroy(), 100);
}
document.querySelector("#copy_link").onclick = () => {
navigator.clipboard.writeText(`${location /*location already has '/' */}?id=${peer.id}`);
Toast.fire({
icon: "success",
title: "Copied meeting URL to clipboard!",
});
}
document.querySelector("#pause").onclick = ({currentTarget: el}) => {
if (el.getAttribute("data-paused") == "false"){
Toast.fire({
icon: "success",
title: "Paused!",
timer: 5000,
});
enable(mediaStream, false)
document.querySelector("video").classList.add("hide");
el.innerHTML = `<span class="iconify" data-icon="clarity:play-solid"></span>`;
el.setAttribute("data-paused", "true")
connections.forEach(i => {
console.log(i.send)
i.send({action: "paused"});
})
} else {
// This is gonna cause glitches when it hides for a different reason
document.querySelector("video").classList.remove("hide");
enable(mediaStream, true)
Toast.fire({
icon: "success",
title: "Resumed!",
timer: 5000,
});
el.innerHTML = `<span class="iconify" data-icon="akar-icons:pause"></span>`;
el.setAttribute("data-paused", "false")
connections.forEach(i => {
console.log(i.send)
i.send({action: "resumed"});
})
}
}
peer.on("connection", (connection) => {
console.log("Someone requested a connection");
connection.on("data", function (data) {
connection.send({action: 'test'})
connections.push(connection);
if (data.type == "share") {
console.log("Sharing media stream with", data.id);
peer.call(data.id, mediaStream);
}
if (data.type === "joined"){
Toast.fire({
icon: "info",
title: "Someone joined",
});
}
if (data.type === "destroy"){
peer.destroy();
}
if (data.type === "left"){
Toast.fire({
icon: "info",
title: "Someone left",
});
}
console.log("Received", data);
});
});
}
function addStream(stream, local) {
var v;
if (!document.querySelector("video")){
v = document.createElement("video");
v.setAttribute("autoplay", true);
if (local){
v.classList.add("local");
}
document.body.appendChild(v);
} else {
v = document.querySelector("video");
}
if (local){
v.setAttribute("muted", true)
}
v.srcObject = stream;
}
}
function stop(stream){
stream.getTracks().forEach(i => i.stop());
}
function enable(stream, enabled){
stream.getTracks().forEach(i => i.enabled = enabled);
}
async function getMedia(){
var mediaStream;
try {
var {value: options} = await swal.fire({
title: "Select your sources",
confirmButtonText: "Share!",
html: `
<label class='sourceSelect_label'><div><span class="iconify-inline" data-icon="ant-design:audio-filled"></span> Audio source:</div>
<select id="audio_select" class='select'>
<option value='display'>Audio from your computer</option>
<option value='system'>Audio from your microphone</option>
<option value='both'>Both computer and microphone audio</option>
<option value='none' selected>No audio</option>
</select>
</label>
<label class='sourceSelect_label'><div><span class="iconify-inline" data-icon="fluent:video-20-filled"></span> Video source:</div>
<select id="video_select" class='select'>
<option value='display'>Screen share</option>
<option value='system'>Your webcam</option>
</select>
</label>
`,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
customClass: {
popup: "fullscreen",
confirmButton: "confirmy"
},
preConfirm: ()=>{
return {
audio: document.querySelector("#audio_select").value,
video: document.querySelector("#video_select").value
};
}
,
});
mediaStream = await getStream(options);
} catch(e) {
var {value} = await swal.fire({
title: "Error",
icon: "error",
text:
e.message,
confirmButtonText: "Try again",
denyButtonText: "No thanks",
showDenyButton: true,
reverseButtons: true,
allowEscapeKey: false,
allowOutsideClick: false,
backdrop: false,
customClass: {
popup: "fullscreen"
}
})
try {
mediaStream.getTracks().forEach(i => i.stop())
}catch(e){}
if (value === true){
mediaStream = "retry";
} else {
mediaStream = false;
}
}
if (mediaStream === "retry"){
mediaStream = await getMedia();
}
return mediaStream;
}
async function getStream({audio, video}) {
var permissions = {
both: {},
system: {},
display: {},
none: {},
};
var streams = {};
if (video) {
//If video === 'system' then add {system: {video: true}}
permissions[video].video = true;
}
if (audio) {
permissions[audio].audio = true;
}
if (permissions.both.audio) {
permissions.system.audio = true;
permissions.display.audio = true;
}
console.log(permissions);
try {
if (Object.keys(permissions.system).length) {
streams.system = await navigator.mediaDevices.getUserMedia(permissions.system);
}
if (Object.keys(permissions.display).length) {
//getDisplayMedia requires video.
streams.display = await navigator.mediaDevices.getDisplayMedia({video: true, ...permissions.display});
}
} catch(e){
var _streams = Object.values(streams).map(i => i.getTracks()).flat();
//Stop all running streams
_streams.forEach(i => i.stop())
console.error(e);
throw new Error(`Permission denied.`)
return;
}
var audioStream, videoStream;
videoStream = get("video", streams.system) || get("video", streams.display);
audioStream = get("audio", streams.system) || get("audio", streams.display);
if (!audioStream && !permissions.none.audio) {
if (permissions.both.audio){
throw new Error(`You selected to mix audio from your microphone and the system audio, but no audio was given.`)
return;
}
throw new Error(`You selected ${audio === 'system' ? 'microphone' : 'system'} audio as the source but it was not provided.`);
return;
}
if (!videoStream) {
throw new Error(`You selected ${video === 'system' ? 'webcam' : 'display'} video as the source but it was not provided.`);
return;
}
if (permissions.both.audio) {
const audioStreams = [get("audio", streams.system), get("audio", streams.display)];
console.log(audioStreams, mixAudio(...audioStreams));
audioStream = mixAudio(...audioStreams)
if (!audioStream) {
throw new Error(`You selected to mix microphone audio and system audio but your ${get("audio", streams.system) ? "system" : "microphone"} audio was not given.`);
return;
}
return createStream(videoStream, ...audioStream)
}
return createStream(videoStream, audioStream);
}
function get(type, stream) {
if (type === "audio") {
return stream?.getAudioTracks()?.[0];
}
if (type === "video") {
return stream?.getVideoTracks()?.[0];
}
}
function mixAudio(desktopStream, voiceStream) {
// Return undefined if both are not avalible.
if (!(desktopStream && voiceStream))
return undefined;
console.log({desktopStream, voiceStream})
// desktopStream and voiceStream are tracks not streams here
desktopStream = createStream(desktopStream);
voiceStream = createStream(voiceStream);
const context = new AudioContext();
const source1 = context.createMediaStreamSource(desktopStream);
const source2 = context.createMediaStreamSource(voiceStream);
const destination = context.createMediaStreamDestination();
const desktopGain = context.createGain();
const voiceGain = context.createGain();
desktopGain.gain.value = 0.7;
voiceGain.gain.value = 0.7;
source1.connect(desktopGain).connect(destination);
source2.connect(voiceGain).connect(destination);
// Important to return tracks
return destination.stream.getAudioTracks();
}
function createStream(...tracks) {
if (Array.isArray(arguments[0])) {
tracks = arguments[0];
//Also allow [stream1, stream2] etc
}
tracks = tracks.filter(i => i);
let newStream = new MediaStream();
tracks.forEach(i=>newStream.addTrack(i));
return newStream;
}