-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (61 loc) · 2.38 KB
/
app.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
import { HMSReactiveStore, selectRemotePeers } from '@100mslive/hms-video-store';
const hms = new HMSReactiveStore();
// by default subscriber is notified about store changes post subscription only, this can be
// changed to call it right after subscribing too using this function.
hms.triggerOnSubscribe(); // optional, recommended
const actions = hms.getActions();
const store = hms.getStore();
const hmsNotifications = hms.getNotifications();
// Helper function to get URL parameters
function getUrlParam(param) {
const urlParams = new URLSearchParams(window.location.search);
const v = urlParams.get(param);
return v ? v : "";
}
async function run(authToken, toRender) {
await actions.join({
userName: "proctor",
authToken: authToken,
settings: {
isAudioMuted: true,
isVideoMuted: true,
}
});
store.subscribe(peers => {
const videosContainer = document.getElementById('videos');
const videoElements = videosContainer.getElementsByTagName('video');
for (let video of videoElements) {
const trackID = video.getAttribute('track_id');
actions.detachVideo(trackID, video);
console.log(`detachVideo(${trackID})`)
}
videosContainer.innerHTML = '';
peers.forEach(peer => {
let track;
if (toRender == 'video') {
track = peer.videoTrack;
} else if (toRender == 'screen' && peer.auxiliaryTracks.length > 0) {
track = peer.auxiliaryTracks[0];
}
if (track) {
const video = document.createElement('video');
video.autoplay = true;
video.controls = true;
video.setAttribute('track_id', track);
videosContainer.appendChild(video);
actions.attachVideo(track, video);
console.log(`attachVideo(${track})`)
} else {
console.error(`track not found for peer:${peer.id} toRender:${toRender}`);
}
});
}, selectRemotePeers);
}
// Automatically join the room if authToken is available
const token = getUrlParam('token');
const render = getUrlParam('type').toLowerCase();
if (token && (render === 'video' || render === 'screen')) {
run(token, render);
} else {
alert(`missing url param\ntype:${render}\ntoken:${token}`);
}