-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
133 lines (113 loc) · 3.94 KB
/
script.js
File metadata and controls
133 lines (113 loc) · 3.94 KB
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
const videoPublisherEl = document.querySelector('video-publisher');
const audioToggle = document.querySelector('#audio-toggle');
const videoToggle = document.querySelector('#video-toggle');
const videoSubscribersEl = document.querySelector('video-subscribers');
const screenShareEl = document.querySelector('screen-share');
const liveChatEl = document.querySelector('live-chat');
const livePollEl = document.querySelector('live-poll');
const livePollControlEl = document.querySelector('live-poll-control');
const videoSubscriberContainer = document.querySelector(
'#video-subscriber-container'
);
const videoSubscriberContainerCamera = document.querySelector('#camera');
const videoSubscriberContainerCustom = document.querySelector('#custom');
const videoSubscriberContainerScreen = document.querySelector('#screen');
let serverURL;
let applicationId = 'YOUR_APPLICATION_ID';
let sessionId = 'YOUR_SESSION_ID';
let token = 'YOUR_TOKEN';
function initializeSession() {
const session = OT.initSession(applicationId, sessionId);
console.log('session: ', session);
// Set session and token (and optionally properties) for Web Components
if (videoPublisherEl) {
videoPublisherEl.session = session;
videoPublisherEl.token = token;
videoPublisherEl.properties = {
fitMode: 'cover',
height: '100%',
resolution: '1920x1080',
videoContentHint: 'detail',
width: '100%',
};
audioToggle.addEventListener('click', () => {
console.log('audioToggle!');
videoPublisherEl.toggleAudio();
});
videoToggle.addEventListener('click', () => {
console.log('videoToggle!');
videoPublisherEl.toggleVideo();
});
}
if (videoSubscribersEl) {
videoSubscribersEl.session = session;
videoSubscribersEl.token = token;
}
if (videoSubscriberContainer) {
console.log('videoSubscriberContainer!');
session.on('streamCreated', function (event) {
console.log('streamCreated!', event.stream.videoType);
const videoSubscriberEl = document.createElement('video-subscriber');
videoSubscriberEl.setAttribute('id', `${event.stream.streamId}`);
videoSubscriberEl.properties = { width: '100%', height: '100%' };
// videoSubscriberEl.properties = { width: '100%', height: '100%' };
videoSubscriberEl.session = session;
videoSubscriberEl.stream = event.stream;
if (event.stream.videoType === 'camera') {
videoSubscriberContainerCamera.appendChild(videoSubscriberEl);
} else if (event.stream.videoType === 'screen') {
videoSubscriberContainerScreen.appendChild(videoSubscriberEl);
}
});
}
if (screenShareEl) {
screenShareEl.session = session;
screenShareEl.token = token;
}
if (liveChatEl) {
liveChatEl.session = session;
liveChatEl.token = token;
if (videoPublisherEl) {
liveChatEl.username = 'host';
} else {
liveChatEl.username = 'viewer';
}
}
if (livePollEl) {
livePollEl.session = session;
livePollEl.token = token;
livePollEl.addEventListener('poll-status', (event) => {
const pollStatus = event.detail.status;
if (pollStatus === 'started') {
livePollEl.style.display = 'inline';
}
if (pollStatus === 'closed') {
livePollEl.style.display = 'none';
}
});
}
if (livePollControlEl) {
livePollControlEl.session = session;
livePollControlEl.token = token;
}
}
if (serverURL) {
const fetchCredentials = async () => {
try {
const response = await fetch(serverURL + '/session');
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}
const credentials = await response.json();
applicationId = credentials.applicationId;
sessionId = credentials.sessionId;
token = credentials.token;
initializeSession();
} catch (err) {
console.error('Error getting credentials: ', err.message);
}
};
fetchCredentials();
} else {
initializeSession();
}