-
Notifications
You must be signed in to change notification settings - Fork 8
/
lib.js
795 lines (635 loc) · 24.9 KB
/
lib.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
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
'use strict';
/**************************************************
* Initialization *
***************************************************/
let receiverOnly = false;
let showVideo = false;
let isElectron = (navigator.userAgent.toLowerCase().indexOf(' electron/') > -1);
// Set up media stream constant and parameters.
// Audio is mono right now
const mediaStreamConstraints = {
audio: {
mandatory: {
chromeMediaSource: 'desktop',
echoCancellation: false,
googEchoCancellation: false,
googAutoGainControl: false,
googNoiseSuppression: false,
googHighpassFilter: false,
googTypingNoiseDetection: false,
googAudioMirroring: false,
googAudioMirroring: false,
googNoiseReduction: false,
}
},
video: {
mandatory: {
chromeMediaSource: 'desktop'
}
}
};
// Mac and Linux have to disable audio
// if you want to stream video.
// Receiver only will work fine either way
//mediaStreamConstraints.audio = false;
// Set up to exchange audio/video
const offerOptions = {
offerToReceiveAudio: 1,
offerToReceiveVideo: (showVideo) ? 1 : 0
};
// Setup Web Audio components
window.AudioContext = (window.AudioContext || window.webkitAudioContext);
let context = new AudioContext();
let localStreamNode;
let incomingRemoteStreamNode;
let outgoingRemoteStreamNode = context.createMediaStreamDestination();
let incomingRemoteGainNode = context.createGain();
let outgoingRemoteGainNode = context.createGain();
incomingRemoteGainNode.connect(context.destination);
outgoingRemoteGainNode.connect(outgoingRemoteStreamNode);
if (false) {
// Listen to what's going out in the left for reference
let panL = context.createStereoPanner();
panL.pan.value = -1;
panL.connect(context.destination);
outgoingRemoteGainNode.connect(panL);
// Listen to what's coming in in the right ear
let panR = context.createStereoPanner();
panR.pan.value = 1;
panR.connect(context.destination);
incomingRemoteGainNode.disconnect();
incomingRemoteGainNode.connect(panR);
}
// Visualizer canvas
const visualizerCanvas = document.getElementById('visualizer');
const vizCtx = visualizerCanvas.getContext('2d');
visualizerCanvas.addEventListener('dblclick', () => {
if (visualizerCanvas.requestFullscreen) {
visualizerCanvas.requestFullscreen();
} else if (visualizerCanvas.webkitRequestFullscreen) {
visualizerCanvas.webkitRequestFullscreen();
} else if (visualizerCanvas.mozRequestFullScreen) {
visualizerCanvas.mozRequestFullScreen();
} else if (visualizerCanvas.msRequestFullscreen) {
visualizerCanvas.msRequestFullscreen();
}
});
// Define media elements.
const localMedia = document.getElementById('localMedia');
const localVideo = document.getElementById('localVideo');
// Container for remote media elements
const remoteMedia = document.getElementById('remoteMedia');
// Socket ID element
const socketIdElem = document.getElementById('socketId');
// Stream from options
const streamFromElem = document.getElementById('streamFrom');
// Hide video elements
if (showVideo === false) {
hideVideoElements();
}
// Prevent file:// protocol issues
if (isElectron === false && location.href.includes('file://')) {
enableReceiverOnly();
}
// Prevent screen capture issues
if (isElectron === false) {
document.getElementById('fromDesktop').remove();
}
const servers = null; // Allows for RTC server configuration.
let videoStream = null;
/**************************************************
* Stream related functions *
***************************************************/
// Setup local media streams
async function setupLocalMediaStreams() {
return new Promise((resolve, reject) => {
navigator.mediaDevices.getUserMedia(mediaStreamConstraints)
.then((stream) => {
if (showVideo) {
gotLocalVideoMediaStream(stream);
}
gotLocalMediaStream(stream);
resolve();
})
.catch((e) => {
console.warn(`Failed to obtain local media stream: ${e}`);
// We weren't able to get a local media stream
// Become a receiver
enableReceiverOnly();
reject();
});
});
}
async function setupLocalMediaStreamsFromFile(filepath) {
return new Promise(async (resolve, reject) => {
// AudioContext gets suspended if created before
// a user interaction https://goo.gl/7K7WLu
context.resume();
if (receiverOnly) {
resolve();
return;
}
if (showVideo) {
// This will grab video and audio.
// We'll overwrite the audio once it's done
await setupLocalMediaStreams();
}
// Create media source
// This is attached to the HTML audio element and can be fed arbitrary buffers of audio
// TODO: Make sure we can support MIME other than audio/mpeg
let mediaSource = new MediaSource();
trace('Created MediaSource.');
console.dir(mediaSource);
// Can't call addSourceBuffer until it's open
mediaSource.addEventListener('sourceopen', async () => {
trace('MediaSource open.');
// Corner case for file:// protocol since fetch won't like it
if (isElectron === false && location.href.includes('file://')) {
// TODO: Audio still wouldn't transmit
// URL.revokeObjectURL(localAudio.src);
// localAudio.src = './test_file.mp3';
} else {
let buffer = mediaSource.addSourceBuffer('audio/mpeg');
trace('Fetching data...');
let data;
let resp = await fetch(filepath);
data = await resp.arrayBuffer();
console.dir(data);
buffer.appendBuffer(data);
trace('Data loaded.');
}
});
// We need a media stream for WebRTC, so run
// our MediaSource through a muted HTML audio element
// and grab its stream via captureStream()
let localAudio = new Audio();
localAudio.autoplay = true;
localAudio.muted = true;
// Only grab stream after it has loaded; won't have tracks if grabbed too early
localAudio.addEventListener('canplaythrough', () => {
try {
let localStream = localAudio.captureStream();
gotLocalMediaStream(localStream);
} catch (e) {
console.warn(`Failed to captureStream() on audio elem. Assuming unsupported. Switching to receiver only.`, e);
enableReceiverOnly();
}
resolve();
});
localMedia.appendChild(localAudio);
// srcObject doesn't work here ?
localAudio.src = URL.createObjectURL(mediaSource);
localAudio.load();
});
}
function gotLocalMediaStream(mediaStream) {
let videoTracks = mediaStream.getVideoTracks();
if (videoTracks.length > 0) {
localVideo.srcObject = mediaStream;
}
// Disconnect our old one if we get a new one
// This will get called twice if we want a video stream
// and a different audio source
if (localStreamNode) {
localStreamNode.disconnect();
}
localStreamNode = context.createMediaStreamSource(mediaStream);
localStreamNode.connect(outgoingRemoteGainNode);
trace('Connected localStreamNode.');
}
function gotLocalVideoMediaStream(mediaStream) {
videoStream = mediaStream;
trace('Received local video stream.');
}
/**************************************************
* DOM related functions *
***************************************************/
function enableReceiverOnly() {
receiverOnly = true;
localMedia.innerHTML = 'Receiver only';
streamFromElem.style.display = 'none';
trace('Switched to receiver only.');
}
function hideVideoElements() {
localVideo.style.display = 'none';
// Hide all remote video elements
let remoteVideos = document.getElementsByClassName('remoteVideo');
for (let i = 0; i < remoteVideos.length; i++) {
remoteVideos[i].style.display = 'none';
}
}
function handleError(e) {
console.error(e);
console.dir(e);
console.trace(e);
}
// Logs an action (text) and the time when it happened on the console.
function trace(text) {
text = text.trim();
const now = (performance.now() / 1000).toFixed(3);
console.log(now, text);
}
/**************************************************
* WebRTC connections *
***************************************************/
class Peer {
constructor(id, socket) {
this.id = id;
this.socket = socket; // This is our class wrapped socket. Not socket.io socket
this.initiated = false;
this.offered = false;
this.answered = false;
this.conn = null;
this.sendChannel = null;
this.recvChannel = null;
this.iceCandidates = [];
this.remoteStream = null;
this.titleElem = null;
this.audioElem = null;
this.videoElem = null;
this.audioNode = null;
this.gainNode = null;
this.muteButton = null;
this.conn = new RTCPeerConnection(servers, { });
trace('Created local peer connection object localPeerConnection.');
// Use arrow function so that 'this' is available in class methods
this.conn.addEventListener('icecandidate', (event) => {
this.handleIceCandidates(event);
});
this.conn.addEventListener('iceconnectionstatechange', (event) => {
this.handleConnectionChange(event);
});
this.conn.addEventListener('track', (event) => {
this.gotRemoteMediaStream(event);
});
// Set up additional data channel to pass messages peer-to-peer
// There is a separate channel for sending and receiving
this.sendChannel = this.conn.createDataChannel('session-info');
this.sendChannel.addEventListener('open', (event) => {
trace(`Data channel to ${this.id} opened.`);
});
this.conn.addEventListener('datachannel', (event) => {
trace(`Received data channel '${event.channel.label}' from ${this.id}.`);
this.recvChannel = event.channel;
this.recvChannel.addEventListener('message', (event) => {
trace(`Message received from ${this.id}:`);
console.dir(JSON.parse(event.data));
});
// Send an initial message
this.sendChannel.send(JSON.stringify({ type: 'msg', contents: 'hello' }));
});
}
cleanup() {
if (this.titleElem) {
this.titleElem.remove();
}
if (this.audioElem) {
this.audioElem.remove();
}
if (this.audioNode) {
this.audioNode.disconnect();
}
if (this.gainNode) {
this.gainNode.disconnect();
}
if (this.videoElem) {
this.videoElem.remove();
}
if (this.muteButton) {
this.muteButton.remove();
}
this.iceCandidates = [];
}
reconnect() {
this.cleanup();
}
disconnect() {
this.conn.close();
this.sendChannel.close();
this.recvChannel.close();
this.cleanup();
// TODO: This is meh coupling
this.socket.disconnected(this.id);
trace(`Disconnected from ${this.id}.`);
}
// Connects with new peer candidate.
handleIceCandidates(event) {
if (event.candidate) {
this.socket.socket.emit('candidate', event.candidate, this.id);
trace(`Sent ICE candidate to ${this.id}.`);
}
}
// Logs changes to the connection state.
handleConnectionChange(event) {
trace(`ICE state changed to: ${event.target.iceConnectionState}.`);
if (event.target.iceConnectionState === 'disconnected') { // || event.target.iceConnectionState === 'closed' || event.target.iceConnectionState === 'failed') {
this.disconnect();
}
}
uncacheICECandidates() {
if (!(this.conn && this.conn.remoteDescription && this.conn.remoteDescription.type)) {
console.warn(`Connection was not in a state for uncaching.`);
return;
}
this.iceCandidates.forEach((candidate) => {
trace(`Added cached ICE candidate`);
this.conn.addIceCandidate(candidate);
});
this.iceCandidates = [];
}
// Handles remote MediaStream success by adding it as the remoteVideo src.
gotRemoteMediaStream(event) {
this.remoteStream = event.streams[0];
let videoTracks = this.remoteStream.getVideoTracks();
let audioTracks = this.remoteStream.getAudioTracks();
// If we have a video stream and separate audio stream,
// we'll get multiple 'track' events
// Make sure the title only gets added once
if (!this.titleElem) {
this.titleElem = document.createElement('h3');
this.titleElem.innerHTML = `${this.id}:`;
remoteMedia.appendChild(this.titleElem);
}
// Make sure we actually have audio tracks
if (audioTracks.length > 0) {
// TODO: This needs more investigation
// The MediaStream node doesn't produce audio until an HTML audio element is attached to the stream
// Pause and remove the element after loading since we only need it to trigger the stream
// See https://stackoverflow.com/questions/24287054/chrome-wont-play-webaudio-getusermedia-via-webrtc-peer-js
// and https://bugs.chromium.org/p/chromium/issues/detail?id=121673#c121
let audioElem = new Audio();
audioElem.autoplay = true;
audioElem.controls = true;
audioElem.muted = true;
audioElem.srcObject = this.remoteStream;
audioElem.addEventListener('canplaythrough', () => {
audioElem.pause();
audioElem = null;
});
// Gain node for this stream only
// Connected to gain node for all remote streams
this.gainNode = context.createGain();
this.gainNode.connect(incomingRemoteGainNode);
this.audioNode = context.createMediaStreamSource(this.remoteStream);
this.audioNode.connect(this.gainNode);
// Setup mute button logic
this.muteButton = document.createElement('button');
this.muteButton.innerHTML = 'Mute';
this.muteButton.addEventListener('click', () => {
if (this.muteButton.innerHTML === 'Mute') {
this.gainNode.gain.value = 0;
this.muteButton.innerHTML = 'Unmute';
} else {
this.gainNode.gain.value = 1;
this.muteButton.innerHTML = 'Mute';
}
});
remoteMedia.appendChild(this.muteButton);
// AudioContext gets suspended if created before
// a user interaction https://goo.gl/7K7WLu
context.resume();
}
// Do video if we should
if (showVideo && videoTracks.length > 0) {
this.videoElem = document.createElement('video');
this.videoElem.classList.add('remoteVideo');
this.videoElem.autoplay = true;
this.videoElem.controls = true;
this.videoElem.muted = true;
this.videoElem.srcObject = this.remoteStream;
remoteMedia.appendChild(this.videoElem);
}
trace(`Received remote stream from ${this.id}.`);
}
}
/**
* Factory function for creating a new Peer and connecting streams to it.
* @param {string} id
*/
async function createPeer(id, socket) {
trace(`Starting connection to ${id}...`);
// Mask global localStream on purpose
// Easily revertible to old style streams from WebAudio changes
let localStream = outgoingRemoteStreamNode.stream;
let peer = null;
let videoTracks = null;
let audioTracks = null;
if (receiverOnly === false) {
if (showVideo && videoStream) {
videoTracks = videoStream.getVideoTracks();
}
audioTracks = localStream.getAudioTracks();
trace(`Audio tracks:`);
console.dir(audioTracks);
if (showVideo && videoTracks.length > 0) {
trace(`Using video device: ${videoTracks[0].label}.`);
}
if (audioTracks.length > 0) {
trace(`Using audio device: ${audioTracks[0].label}.`);
}
}
// Create peer connections and add behavior.
peer = new Peer(id, socket);
// Add local stream to connection and create offer to connect.
if (receiverOnly === false && showVideo && videoTracks[0]) {
peer.conn.addTrack(videoTracks[0], videoStream);
}
if (receiverOnly === false && audioTracks[0]) {
peer.conn.addTrack(audioTracks[0], localStream);
}
return peer;
}
/**************************************************
* Socket.io signaling *
***************************************************/
class Socket {
constructor(ip, port) {
this.ip = ip;
this.port = port;
this.rooms = [];
this.peers = {};
this.socket = io.connect(`http://${this.ip}:${this.port}`);
trace(`Created socket.`);
console.dir(this.socket);
// This is emitted when this socket successfully creates
this.socket.on('created', (room, socketId) => {
trace(`${socketId} successfully created ${room}.`);
socketIdElem.innerHTML = this.socket.id;
this.rooms.push(room);
});
// This is emitted when this socket successfully joins
this.socket.on('joined', (room, socketId) => {
trace(`${socketId} successfully joined ${room}.`);
socketIdElem.innerHTML = this.socket.id;
this.rooms.push(room);
});
this.socket.on('full', (room) => {
console.warn(`Room ${room} is full.`);
});
this.socket.on('ipaddr', (ipaddr) => {
trace(`Server IP address: ${ipaddr}`);
});
// This is emitted when someone else joins
this.socket.on('join', async (socketId) => {
// Have to ignore our own join
if (socketId === this.socket.id) {
return;
}
let peer = this.peers[socketId];
trace(`'${socketId}' joined.`);
// Connection already existed
// Close old one
if (peer) {
this.handleDisconnect(peer.id);
}
peer = await createPeer(socketId, this);
this.peers[peer.id] = peer;
peer.offered = true;
trace(`createOffer to ${socketId} started.`);
let offer = await peer.conn.createOffer(offerOptions);
await peer.conn.setLocalDescription(offer);
console.log(peer);
this.socket.emit('offer', offer, peer.id);
});
this.socket.on('offer', async (offer, socketId) => {
let peer = this.peers[socketId];
trace(`Offer received from ${socketId}:`);
console.dir(offer);
// Peer might exist because of ICE candidates
if (peer) {
console.warn(`Peer already existed at offer.`);
peer.reconnect();
} else {
peer = await createPeer(socketId, this);
this.peers[peer.id] = peer;
}
peer.answered = true;
await peer.conn.setRemoteDescription(offer);
let answer = await peer.conn.createAnswer(offerOptions);
await peer.conn.setLocalDescription(answer);
this.socket.emit('answer', answer, socketId);
// Restore any cached ICE candidates
peer.uncacheICECandidates();
});
this.socket.on('answer', async (answer, socketId) => {
let peer = this.peers[socketId];
// Make sure we're expecting an answer
if (!(peer && peer.offered)) {
console.warn(`Unexpected answer from ${socketId} to ${this.socket.id}.`);
return;
}
trace(`Answer received from ${socketId}:`);
console.dir(answer);
await peer.conn.setRemoteDescription(answer);
// Restore any cached ICE candidates
peer.uncacheICECandidates();
});
this.socket.on('candidate', async (candidate, ownerId) => {
let peer = this.peers[ownerId];
// Make sure we're expecting candidates
if (!(peer && (peer.offered || peer.answered))) {
console.warn(`Unexpected ICE candidates from ${ownerId} to ${this.socket.id}.`);
return;
}
trace(`Received ICE candidate for ${ownerId}.`);
let iceCandidate = new RTCIceCandidate(candidate);
// Cache ICE candidates if the connection isn't ready yet
if (peer.conn && peer.conn.remoteDescription && peer.conn.remoteDescription.type) {
await peer.conn.addIceCandidate(iceCandidate);
} else {
trace(`Cached ICE candidate`);
peer.iceCandidates.push(iceCandidate);
}
});
this.socket.on('leave', (room, socketId) => {
let peer = this.peers[socketId];
if (peer) {
trace(`${socketId} left ${room}.`);
peer.disconnect();
}
this.peers[socketId] = null;
});
}
joinRoom(room) {
trace(`Entering room '${room}'...`);
this.socket.emit('join', room);
}
leaveRoom(room) {
trace(`Leaving room ${room}...`);
this.socket.emit('leave', room, this.socket.id);
this.rooms = this.rooms.filter((val) => val !== room);
}
leaveAllRooms() {
this.rooms.forEach((val) => {
this.leaveRoom(val);
});
}
disconnected(id) {
this.peers[id] = null;
trace(`Removed ${id} from peer list.`);
}
}
// Not in use yet
class Room {
constructor(name) {
this.name = name;
this.peers = {};
}
}
/**************************************************
* Other WebAudio stuff *
***************************************************/
// Visualizer
// Based on: https://stackoverflow.com/a/49371349/6798110
// and https://github.com/webrtc/samples/blob/gh-pages/src/content/peerconnection/webaudio-output/js/main.js
let analyzerNode = context.createAnalyser();
analyzerNode.smoothingTimeConstant = 0.6;
analyzerNode.fftSize = 2048;
analyzerNode.minDecibels = -100;
analyzerNode.maxDecibels = -10;
let vizFreqDomainData = new Uint8Array(analyzerNode.frequencyBinCount);
let vizAnimationFrameId = requestAnimationFrame(updateVizualizer);
outgoingRemoteGainNode.connect(analyzerNode);
incomingRemoteGainNode.connect(analyzerNode);
console.log(analyzerNode.frequencyBinCount);
let vizualizerOpt = document.getElementById('vizualizerOptions');
function updateVizualizer() {
analyzerNode.getByteFrequencyData(vizFreqDomainData);
let width = visualizerCanvas.width;
let height = visualizerCanvas.height;
let barWidth = (width / (analyzerNode.frequencyBinCount / 9.3)); // Estimation for now
// Clear old points
vizCtx.clearRect(0, 0, width, height);
vizCtx.fillStyle = 'black';
vizCtx.fillRect(0, 0, width, height);
vizCtx.strokeStyle = 'yellow';
vizCtx.fillStyle = 'yellow';
vizCtx.beginPath();
vizCtx.moveTo(0, height);
let x = 0;
let t = 1;
let next = 1;
for (let i = 0; i < analyzerNode.frequencyBinCount; i += next) {
// Rounding doesn't go so well...
next += i / (analyzerNode.frequencyBinCount / 16);
next = next - (next % 1);
if (vizualizerOpt.value === 'bar') {
vizCtx.fillRect(x, height - vizFreqDomainData[i], barWidth, vizFreqDomainData[i]);
} else {
let p0 = (i > 0) ? { x: x - barWidth, y: height - vizFreqDomainData[i - 1] } : { x: 0, y: 0 };
let p1 = { x: x, y: height - vizFreqDomainData[i] };
let p2 = (i < analyzerNode.frequencyBinCount - 1) ? { x: x + barWidth, y: height - vizFreqDomainData[i + 1] } : p1;
let p3 = (i < analyzerNode.frequencyBinCount - 2) ? { x: x + 2 * barWidth, y: height - vizFreqDomainData[i + 2] } : p1;
let cp1x = p1.x + (p2.x - p0.x) / 6 * t;
let cp1y = p1.y + (p2.y - p0.y) / 6 * t;
let cp2x = p2.x - (p3.x - p1.x) / 6 * t;
let cp2y = p2.y - (p3.y - p1.y) / 6 * t;
vizCtx.bezierCurveTo(cp1x, cp1y, cp2x, cp2y, p2.x, p2.y);
}
x += barWidth + 1;
}
vizCtx.stroke();
setTimeout(() => {
vizAnimationFrameId = requestAnimationFrame(updateVizualizer);
}, 20);
}