-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvideo.html
251 lines (225 loc) · 7.62 KB
/
video.html
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" type="text/css" href="/css/bootstrap.min.css">
<link rel="stylesheet" type="text/css" href="/css/custom.css">
</head>
<body>
<main role="main" class="container mt-2">
<h1>Game Details</h1>
<h2>1. Pick a video:</h2>
<div class="form-group row">
<label for="local-selector" class="col-sm-3">Use a local file:</label>
<div class="col-sm-9">
<input type="file" id="local-selector" accept="video/*" class="form-control-file">
<small class="form-text text-muted">You can only upload video files (e.g. <code>mp4</code>, <code>ogg</code>, ...)</small>
</div>
</div>
<form id="direct-url-selector-form">
<div class="form-group row">
<label for="direct-url-selector" class="col-sm-3">A direct link to a video file:</label>
<input type="text" id="direct-url-selector" class="form-control col-sm-9" placeholder="https://example.org/video.mp4">
</div>
</form>
<form id="youtube-selector-form">
<div class="form-group row">
<label for="youtube-selector" class="col-sm-3">Or use a YouTube video:</label>
<input type="text" id="youtube-selector" class="form-control col-sm-9" placeholder="https://www.youtube.com/watch?v=9g3--WYH8SY">
</div>
</form>
<h2>2. Join using your phones by typing this URL:</h2>
<p><code><a id="joinurl">...</a></code></p>
<h2>3. Click the "fullscreen" button</h2>
<button type="button" id="fullscreen" class="btn btn-primary btn-lg mb-2">Fullscreen</button>
<div id="videocontainer">
<div id="video">Pick a video above...</div>
<div id="messagediv" style="display: none;">
<p id="message"></p>
</div>
</div>
<p>Players connected:</p>
<ul id="players">
<li class="empty">None yet</li>
</ul>
</main>
<script>
// Load YouTube API
// YouTube API uses a callback, adapt this to a Promise
var youtubeLoaded = new Promise(function(resolve, reject) {
window.onYouTubeIframeAPIReady = function() { resolve(); };
});
var youtubeLoading = false;
function loadYoutube() {
if(!youtubeLoading) {
var youtubeJs = document.createElement("script");
youtubeJs.src = "https://www.youtube.com/iframe_api";
document.body.appendChild(youtubeJs);
youtubeLoading = true;
}
return youtubeLoaded;
}
var currentPath = window.location.pathname; // "/video/1234"
var videoId = currentPath.substring(7); // "1234"
var videoContainer = document.getElementById("videocontainer");
var videoPlayer = undefined;
var playerList = document.getElementById("players");
var messageArea = document.getElementById("messagediv");
var messageElement = document.getElementById("message");
var fullscreenButton = document.getElementById("fullscreen");
function showYoutubeVideo(youtubeId) {
// Replace existing element with a div
var oldVideo = document.getElementById("video");
var newVideo = document.createElement("div");
newVideo.setAttribute("id", "video");
oldVideo.parentNode.replaceChild(newVideo, oldVideo);
// Load video, set videoPlayer when done
return loadYoutube().then(function() {
var yt = new YT.Player("video", {
height: "100%",
width: "100%",
videoId: youtubeId,
playerVars: {
fs: 0, // No full screen button
},
events: {
onReady: function() {
videoPlayer = {
play: function() {
yt.playVideo();
},
pause: function() {
yt.pauseVideo();
},
};
},
},
});
});
}
function showDirectUrlVideo(url) {
var videoElement = document.createElement("video");
videoElement.setAttribute("id", "video");
videoElement.setAttribute("controls", true);
videoElement.setAttribute("width", "100%");
videoElement.setAttribute("height", "100%");
videoElement.src = url;
var oldVideo = document.getElementById("video");
oldVideo.parentNode.replaceChild(videoElement, oldVideo);
// Set videoPlayer
videoPlayer = {
play: function() {
videoElement.play();
},
pause: function() {
videoElement.pause();
},
};
}
// Show a video file
function showVideoFile(file) {
var videoType = file.type;
var videoElement = document.createElement("video");
videoElement.setAttribute("id", "video");
videoElement.setAttribute("controls", true);
videoElement.setAttribute("width", "100%");
videoElement.setAttribute("height", "100%");
var canPlay = videoElement.canPlayType(videoType);
if(canPlay === "" || canPlay === "no") {
alert("Video format not supported");
return;
}
var fileURL = URL.createObjectURL(file);
videoElement.src = fileURL;
var oldVideo = document.getElementById("video");
oldVideo.parentNode.replaceChild(videoElement, oldVideo);
// Set videoPlayer
videoPlayer = {
play: function() {
videoElement.play();
},
pause: function() {
videoElement.pause();
},
};
}
var fileSelector = document.getElementById("local-selector");
fileSelector.addEventListener("change", function() {
showVideoFile(fileSelector.files[0]);
});
var youtubeSelector = document.getElementById("youtube-selector");
document.getElementById("youtube-selector-form").addEventListener("submit", function(e) {
e.preventDefault();
var m = youtubeSelector.value.match(new RegExp("(?:https?://)?(?:www\\.)?youtube\\.com/watch\\?v=([^/]+)"));
if(m) {
showYoutubeVideo(m[1]);
}
});
var directUrlSelector = document.getElementById("direct-url-selector");
document.getElementById("direct-url-selector-form").addEventListener("submit", function(e) {
e.preventDefault();
var m = directUrlSelector.value.match(new RegExp("https?://"));
if(m) {
showDirectUrlVideo(directUrlSelector.value);
}
});
// Go fullscreen
fullscreenButton.addEventListener("click", function() {
videoContainer.requestFullscreen();
});
// Show join URL
var joinUrl = document.getElementById("joinurl");
joinUrl.innerText = window.location.host + "/" + videoId;
joinUrl.href = window.location.protocol + "//" + window.location.host + "/" + videoId;
// Player list
players = {};
function updatePlayers() {
playerList.innerHTML = "";
var names = Object.keys(players);
names.sort();
for(var i = 0; i < names.length; ++i) {
var elem = document.createElement("li");
elem.innerText = names[i];
playerList.appendChild(elem);
}
}
var pauseTimeout = undefined;
function resume() {
messageElement.innerText = "";
messageArea.style.display = "none";
videoPlayer.play();
pauseTimeout = undefined;
}
// Receive buzzes
var wsProto = "wss:";
if(window.location.protocol === "http:") {
wsProto = "ws:";
}
var socket;
function connect() {
console.log("Connecting...");
socket = new WebSocket(wsProto + "//" + window.location.host + "/api/host/" + videoId);
socket.addEventListener("open", function() { console.log("Connected"); });
socket.addEventListener("message", function(e) {
console.log("Message: ", e.data);
if(e.data.startsWith("join ")) {
var name = e.data.substring(5);
players[name] = {};
updatePlayers();
} else if(e.data.startsWith("buzz ")) {
if(pauseTimeout === undefined) {
var name = e.data.substring(5);
messageElement.innerText = name + " buzzed!";
messageArea.style.display = "";
videoPlayer.pause();
pauseTimeout = setTimeout(resume, 5000);
}
}
});
socket.addEventListener("close", function() { setTimeout(connect, 2000); });
}
connect();
</script>
</body>
</html>