-
Notifications
You must be signed in to change notification settings - Fork 41
/
hls-viewer.js
146 lines (131 loc) · 4.34 KB
/
hls-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
145
146
/* 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;
};
const getBroadcastUrl = async function () {
const response = await fetch(`/broadcast/${window.location.search.split('=')[1]}`);
return response.json();
};
/** 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 videoContainer = document.getElementById('videoContainer');
const bannerText = document.getElementById('bannerText');
const playVideoContainer = document.getElementById('play-video-container');
if (status === 'active') {
banner.classList.add('hidden');
videoContainer.classList.remove('hidden');
playVideoContainer.classList.remove('hidden');
} else if (status === 'ended') {
bannerText.classList.add('red');
bannerText.innerHTML = 'The Broadcast is Over';
banner.classList.remove('hidden');
videoContainer.classList.add('hidden');
playVideoContainer.classList.add('hidden');
}
};
/**
* Listen for events on the OpenTok session
*/
const setEventListeners = function (session) {
const streams = [];
const subscribers = [];
let broadcastActive = false;
/** Listen for a broadcast status update from the host */
session.on('signal:broadcast-url', function (event) {
console.log('signal:broadcast-url', event);
const broadcastUrl = event.data;
var video = document.getElementById('video');
if (Hls.isSupported()) {
var hls = new Hls();
console.log('signal:broadcast-url - ', broadcastUrl);
hls.loadSource(broadcastUrl);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, function () {
playVideo();
});
hls.on(Hls.Events.BUFFER_EOS, function () {
updateBanner('ended');
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = broadcastUrl;
video.addEventListener('canplay', function () {
video.play();
});
}
updateBanner('active');
});
};
const playVideo = function () {
var video = document.getElementById('video');
if (video) {
video
.play()
.then((res) => {
console.log('Play Video Successfull', res);
const playVideoContainer = document.getElementById('play-video-container');
playVideoContainer.classList.add('hidden');
})
.catch((err) => {
console.log('Play Video error', err);
});
}
};
const switchToLiveMode = function () {
window.location.href = `viewer${window.location.search}`;
};
const addClickEventListeners = function () {
document.getElementById('play-video').addEventListener('click', playVideo);
document.getElementById('go-live-btn').addEventListener('click', switchToLiveMode);
};
const init = function () {
addClickEventListeners();
getBroadcastUrl()
.then((data) => {
updateBanner('active');
const { url } = data;
var video = document.getElementById('video');
if (Hls.isSupported()) {
var hls = new Hls();
console.log('broadcast-url - ', url);
hls.loadSource(url);
hls.attachMedia(video);
hls.on(Hls.Events.MANIFEST_PARSED, function () {
playVideo();
});
} else if (video.canPlayType('application/vnd.apple.mpegurl')) {
video.src = url;
video.addEventListener('canplay', function () {
video.play();
});
}
})
.catch((e) => console.log(e));
};
document.addEventListener('DOMContentLoaded', init);
})();