added room 144 to live stream pages
[libreplanet-static.git] / 2017 / assets / js / stream.js
CommitLineData
fcf95e6d 1/**
2 * @licstart The following is the entire license notice for the JavaScript code in this page.
3 *
4 * IceCast Stream Monitor
5 * Copyright © 2015 David Thompson <davet@gnu.org>
6 *
7 * This program is free software: you can redistribute it and/or
8 * modify it under the terms of the GNU Affero General Public License
9 * as published by the Free Software Foundation, either version 3 of
10 * the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program. If not, see
19 * <http://www.gnu.org/licenses/>.
20 *
21 * @licend The above is the entire license notice for the JavaScript code in this page
22 */
23
24if (!Array.prototype.find) {
25 Array.prototype.find = function(predicate) {
26 if (this == null) {
27 throw new TypeError('Array.prototype.find called on null or undefined');
28 }
29 if (typeof predicate !== 'function') {
30 throw new TypeError('predicate must be a function');
31 }
32 var list = Object(this);
33 var length = list.length >>> 0;
34 var thisArg = arguments[1];
35 var value;
36
37 for (var i = 0; i < length; i++) {
38 value = list[i];
39 if (predicate.call(thisArg, value, i, list)) {
40 return value;
41 }
42 }
43 return undefined;
44 };
45}
46
47var app = {};
48
49app.icecastUrl = "http://live2.fsf.org";
50
51app.icecastApiUrl = "http://live2.fsf.org";
52
53app.scheduleEvery = function(duration, thunk) {
54 thunk();
55 setTimeout(function() {
56 app.scheduleEvery(duration, thunk);
57 }, duration);
58};
59
60app.nullStats = {
61 listeners: 0,
62 server_name: null,
63 server_description: null
64};
65
66app.publicApi = function(xhr) {
67 xhr.withCredentials = false;
68};
69
70app.streamStats = function(mount) {
71 var statsUrl = app.icecastApiUrl.concat('/status-json.xsl');
72
73 return m.request({
74 method: "GET",
75 url: statsUrl,
76 config: app.publicApi
77 }).then(function(data) {
78 // Match the end of the listen URL for the mount point.
79 var regexp = new RegExp(mount.concat('$'));
80
81 if(!data.icestats.source) {
82 return app.nullStats;
83 }
84
85 // Due to <https://trac.xiph.org/ticket/2174>, we must
86 // explicitly test if icestats.source is an array.
87 if(!(data.icestats.source instanceof Array)) {
88 data.icestats.source = [data.icestats.source];
89 }
90
91 var stats = data.icestats.source.find(function(source) {
92 return regexp.test(source.listenurl);
93 });
94
95 return stats || app.nullStats;
96 });
97};
98
99app.validStreamInfo = function(stats) {
100 var name = stats.server_name;
101 var desc = stats.server_description;
102
103 return name && desc && name !== "Unspecified name" &&
104 desc !== "Unspecified description";
105};
106
107app.mountToStreamUrl = function(mount) {
108 return app.icecastUrl.concat(mount);
109};
110
111app.changeVideoMount = function(video, mount) {
112 // This is quite hacky and doesn't feel like the Mithril way to do
113 // things, but we need to explicitly reload the video when the
114 // source URL changes.
115 video.src = app.mountToStreamUrl(mount);
116 video.load();
117 video.play();
118};
119
120app.withVideo = function(id, callback) {
121 return function() {
122 var video = document.getElementById(id);
123
124 if(video) {
125 callback(video);
126 }
127 };
128};
129
130app.streams = [
131 {
132 name: "Room 123",
133 speakerMount: "/stream-123.webm",
134 desktopMount: "/slides-123.webm",
135 ircChannel: "#libreplanet_room123"
136 }, {
137 name: "Room 141",
138 speakerMount: "/stream-141.webm",
139 desktopMount: "/slides-141.webm",
140 ircChannel: "#libreplanet_room141"
7bcaa6f6
AE
141 }, {
142 name: "Room 144",
143 speakerMount: "/stream-144.webm",
144 desktopMount: "/slides-144.webm",
145 ircChannel: "#libreplanet_room144"
fcf95e6d 146 }, {
147 name: "Room 155",
148 speakerMount: "/stream-155.webm",
149 desktopMount: "/slides-155.webm",
150 ircChannel: "#libreplanet_room155"
151 }
152];
153
154app.controller = function() {
155 this.stream = m.prop(app.streams[0]);
156 this.stats = m.prop(app.nullStats);
157 this.showDesktop = m.prop(false);
158
159 // Check stats every 10 seconds.
160 app.scheduleEvery(10000, this.updateStats.bind(this));
161};
162
163app.controller.prototype.updateStats = function() {
164 this.stats = app.streamStats(this.stream().speakerMount);
165};
166
167app.view = function(ctrl) {
168 var stream = ctrl.stream();
169 var stats = ctrl.stats() || app.nullStats;
170 var showDesktop = ctrl.showDesktop();
171
172 function renderSpeakerStream() {
173 return m("video.lp-video", {
174 id: "speaker-video",
175 controls: true,
176 autoplay: true,
177 // Sync desktop stream state as best we can.
178 onpause: app.withVideo("desktop-video", function(video) {
179 video.pause();
180 }),
181 onplay: app.withVideo("desktop-video", function(video) {
182 video.play();
183 })
184 }, [
185 m("source", {
186 src: app.mountToStreamUrl(stream.speakerMount)
187 }),
188 m("p",
189 m("em", [
190 "Your browser does not support the HTML5 video tag, ",
191 m("a", { href: "TODO" }, "[ please download ]"),
192 "the video instead"
193 ]))
194 ]);
195 }
196
197 function renderDesktopStream() {
198 return m("video.lp-video", {
199 id: "desktop-video",
200 autoplay: true
201 }, [
202 m("source", { src: app.mountToStreamUrl(stream.desktopMount) }),
203 m("p",
204 m("em", [
205 "Your browser does not support the HTML5 video tag, ",
206 m("a", { href: "TODO" }, "[ please download ]"),
207 "the video instead"
208 ]))
209 ]);
210 }
211
212 function renderToggleDesktopStream() {
213 var action = showDesktop ? "Hide desktop stream" : "Show desktop stream";
214
215 return m(".row", [
216 m(".col-sm-offset-4.col-sm-4",
217 m("button.btn.btn-block.btn-default", {
218 onclick: function() {
219 ctrl.showDesktop(!showDesktop);
220 }
221 }, action)
222 )
223 ]);
224 }
225
226 function renderRoomSelector() {
227 return m(".row",
228 m(".col-sm-offset-1.col-sm-10",
229 m("ol.breadcrumb.text-center", app.streams.map(function(s) {
230 return m("li", {
231 class: s === stream ? "active" : null,
232 onclick: function() {
233 var speakerVideo = document.getElementById("speaker-video");
234 var desktopVideo = document.getElementById("desktop-video");
235
236 app.changeVideoMount(speakerVideo, s.speakerMount);
237
238 // Video element doesn't exist when the user
239 // hasn't elected to show it.
240 if(desktopVideo) {
241 app.changeVideoMount(desktopVideo, s.desktopMount);
242 }
243
244 ctrl.stream(s);
245 ctrl.updateStats();
246
247 return false;
248 }
249 }, m("a.alt-a", { href: "#" }, s.name));
250 }))));
251 }
252
253 function renderStats() {
254 var info;
255
256 if(stats === app.nullStats) {
257 info = m("i", "not broadcasting");
258 } else {
259 info = m("i", "live");
260 }
261 // else if(app.validStreamInfo(stats)) {
262 // info = [
263 // m("strong", stats.server_name),
264 // " — ",
265 // m("i", stats.server_description)
266 // ];
267 // } else {
268 // info = null;
269 // }
270
271 return m(".row", [
272 m(".col-sm-8", info),
273 m(".col-sm-4.text-right", [
274 m("strong", stats.listeners),
275 " watching"
276 ])
277 ]);
278 }
279
280 return [
281 renderRoomSelector(),
282 m("h2", stream.name),
283 renderStats(),
284 renderSpeakerStream(),
285 showDesktop ? renderDesktopStream() : null,
286 renderToggleDesktopStream(),
287 m("h2", "IRC"),
288 m("p", "Join the discussion online!"),
289 m("ul", [
290 m("li", [
291 "Conference-wide Freenode IRC channel: ",
292 m("strong", "#libreplanet")
293 ]),
294 m("li", [
295 "Freenode IRC channel for ",
296 stream.name,
297 ": ",
298 m("strong", stream.ircChannel)
299 ]),
300 m("li", [
301 "Conference hashtag for ",
302 m("a", { href: "https://fsf.org/twitter" }, "microblogging"),
303 ": ",
a2df0d9d 304 m("strong", "#libreplanet")
fcf95e6d 305 ])
306 ])
307 ];
308};
309
310m.module(document.getElementById("stream"), app);