-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuddle.js
142 lines (121 loc) · 3.86 KB
/
huddle.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
const client = new HuddleWebCore.HuddleClient({
projectId: 'TxG-OolMwGeCoZPzX660e65wwuU2MP83',
options: {
activeSpeakers: {
// Number of active speaker visible in the grid, by default 8
size: 10,
},
},
});
let token = '';
let roomId = '';
let displayName = '';
document.querySelector('#audio').addEventListener('click', async () => {
const audioRef = document.querySelector('#audio');
if (audioRef.textContent == 'Disable Audio') {
await client.localPeer.disableAudio();
audioRef.textContent = 'Enable Audio';
} else {
await client.localPeer.enableAudio();
audioRef.textContent = 'Disable Audio';
}
});
document.querySelector('#video').addEventListener('click', async () => {
const videoRef = document.querySelector('#videoRef');
if (videoRef.srcObject) {
client.localPeer.disableVideo();
document.querySelector('#video').textContent = 'Enable Video';
return;
}
const stream = await client.localPeer.enableVideo();
console.log('stream', stream);
videoRef.srcObject = stream;
videoRef.onloadedmetadata = async () => {
console.warn('videoCard() | Metadata loaded...');
try {
await videoRef.play();
document.querySelector('#video').textContent = 'Disable Video';
} catch (error) {
console.error(error);
}
};
videoRef.onerror = () => {
console.error('videoCard() | Error is hapenning...');
};
});
document.querySelector('#screen').addEventListener('click', async () => {
const screenRef = document.querySelector('#screenRef');
if (screenRef.srcObject) {
client.localPeer.stopScreenShare();
screenRef.srcObject = null;
document.querySelector('#screen').textContent = 'Share Screen';
return;
}
const stream = await client.localPeer.startScreenShare();
console.log('stream', stream);
screenRef.srcObject = stream;
screenRef.onloadedmetadata = async () => {
console.warn('videoCard() | Metadata loaded...');
try {
await screenRef.play();
document.querySelector('#screen').textContent = 'Stop Sharing';
} catch (error) {
console.error(error);
}
};
screenRef.onerror = () => {
console.error('videoCard() | Error is hapenning...');
};
});
document.querySelector('#roomId').addEventListener('change', (e) => {
roomId = e.target.value;
});
document.querySelector('#accessToken').addEventListener('change', (e) => {
token = e.target.value;
});
document.querySelector('#displayName').addEventListener('change', (e) => {
displayName = e.target.value;
});
document.querySelector('#joinRoom').addEventListener('click', async () => {
const room = await client.joinRoom({
roomId,
token,
});
room.updateMetadata({
displayName: displayName,
});
document.querySelectorAll('input').forEach((input) => {
input.hidden = true;
});
document.querySelector('#joinRoom').hidden = true;
});
client.room.on('stream-added', ({ peerId, label }) => {
console.log(
'remote',
client.room.getRemotePeerById(peerId)?.getConsumer(label)?.track,
label
);
const container = document.querySelector('#remotePeers');
let mediaRef = document.createElement('video');
if (label == 'audio') {
mediaRef = document.createElement('audio');
}
const remoteTrack = client.room
.getRemotePeerById(peerId)
?.getConsumer(label)?.track;
mediaRef.srcObject = new MediaStream([remoteTrack]);
mediaRef.id = `${peerId}-${label}`;
mediaRef.autoplay = true;
if (label == 'video') {
mediaRef.muted = true;
}
mediaRef.className = 'border-2 rounded-xl border-white-400 aspect-video';
container.appendChild(mediaRef);
});
client.room.on('stream-closed', ({ peerId, label }) => {
console.log('stream-closed', peerId, label);
const mediaRef = document.querySelector(`#${peerId}-${label}`);
mediaRef.srcObject.getTracks().forEach((track) => track.stop());
mediaRef.srcObject = null;
mediaRef.remove();
});