-
Notifications
You must be signed in to change notification settings - Fork 41
/
viewer.js
144 lines (126 loc) · 4.26 KB
/
viewer.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
/* eslint-disable object-shorthand */
(function () {
/**
* Options for adding OpenTok publisher and subscriber video elements
*/
const insertOptions = {
width: '100%',
height: '100%',
showControls: false,
};
/**
* Get our OpenTok API Key, Session ID, and Token from the JSON embedded
* in the HTML.
*/
const getCredentials = function () {
const el = document.getElementById('credentials');
const credentials = JSON.parse(el.getAttribute('data'));
el.remove();
return credentials;
};
/**
* Subscribe to a stream
* @returns {Object} A subscriber object
*/
const subscribe = function (session, stream) {
const name = stream.name;
const insertMode = name === 'Host' ? 'before' : 'after';
const properties = Object.assign({ name: name, insertMode: insertMode }, insertOptions);
return session.subscribe(stream, 'hostDivider', properties, function (error) {
if (error) {
console.log(error);
}
});
};
/** Ping the host to see if the broadcast has started */
const checkBroadcastStatus = function (session) {
session.signal({
type: 'broadcast',
data: 'status',
});
};
/**
* Update the banner based on the status of the broadcast (active or ended)
*/
const updateBanner = function (status) {
const banner = document.getElementById('banner');
const bannerText = document.getElementById('bannerText');
if (status === 'active') {
banner.classList.add('hidden');
} else if (status === 'ended') {
bannerText.classList.add('red');
bannerText.innerHTML = 'The Broadcast is Over';
banner.classList.remove('hidden');
}
};
/**
* Listen for events on the OpenTok session
*/
const setEventListeners = function (session) {
let streams = [];
const subscribers = [];
let broadcastActive = false;
/** Subscribe to new streams as they are published */
session.on('streamCreated', function (event) {
if (event.stream.name === 'EC') {
streams.push(event.stream);
}
if (broadcastActive) {
if (event.stream.name === 'EC' || event.stream.name === 'HostScreen') subscribers.push(subscribe(session, event.stream));
}
if (streams.length > 3) {
document.getElementById('videoContainer').classList.add('wrap');
}
});
session.on('streamDestroyed', function (event) {
const index = streams.indexOf(event.stream);
streams.splice(index, 1);
if (streams.length < 4) {
document.getElementById('videoContainer').classList.remove('wrap');
}
});
/** Listen for a broadcast status update from the host */
session.on('signal:broadcast', function (event) {
const status = event.data;
broadcastActive = status === 'active';
if (status === 'active') {
document.getElementById('back-hls').classList.remove('hidden');
document.getElementById('participate').classList.remove('hidden');
streams.forEach(function (stream) {
if (stream.name !== 'EC') return;
subscribers.push(subscribe(session, stream));
});
} else if (status === 'ended') {
subscribers.forEach(function (subscriber) {
session.unsubscribe(subscriber);
});
}
updateBanner(status);
});
};
const switchToHlsMode = function (e) {
window.location.href = `hls-viewer${window.location.search}`;
};
const switchToLiveMode = function (e) {
window.location.href = `guest${window.location.search}`;
};
const init = function () {
document.getElementById('participate-button').addEventListener('click', switchToLiveMode);
document.getElementById('go-hls-btn').addEventListener('click', switchToHlsMode);
const credentials = getCredentials();
const props = { connectionEventsSuppressed: true };
const session = OT.initSession(credentials.apiKey, credentials.sessionId, props);
session.connect(credentials.token, function (error) {
if (error) {
console.log(error);
if (error.name === 'OT_CONNECTION_LIMIT_EXCEEDED') {
switchToHlsMode();
}
} else {
setEventListeners(session);
checkBroadcastStatus(session);
}
});
};
document.addEventListener('DOMContentLoaded', init);
})();