forked from shaka-project/shaka-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controls.js
377 lines (319 loc) · 11.6 KB
/
controls.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
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/**
* Copyright 2015 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* @fileoverview Implements the video controls overlay.
*/
/** @class */
var playerControls = function() {};
/** @private {boolean} */
playerControls.isLive_;
/** @private {boolean} */
playerControls.isSeeking_;
/** @private {HTMLVideoElement} */
playerControls.video_;
/** @private {!{start: number, end: number}} */
playerControls.seekRange_ = {start: 0, end: 0};
/** @private {shaka.player.Player} */
playerControls.player_ = null;
/**
* Initializes the player controls.
* @param {!HTMLVideoElement} video
*/
playerControls.init = function(video) {
var videoContainer = document.getElementById('videoContainer');
var playButton = document.getElementById('playButton');
var pauseButton = document.getElementById('pauseButton');
var seekBar = document.getElementById('seekBar');
var muteButton = document.getElementById('muteButton');
var unmuteButton = document.getElementById('unmuteButton');
var volumeBar = document.getElementById('volumeBar');
var fullscreenButton = document.getElementById('fullscreenButton');
var currentTime = document.getElementById('currentTime');
var rewindButton = document.getElementById('rewindButton');
var fastForwardButton = document.getElementById('fastForwardButton');
playerControls.isLive_ = false;
playerControls.isSeeking_ = false;
playerControls.video_ = video;
// play
playButton.addEventListener('click', function() {
if (!video.src) return;
video.play();
});
video.addEventListener('play', function() {
playButton.style.display = 'none';
pauseButton.style.display = 'block';
});
// pause
pauseButton.addEventListener('click', function() {
video.pause();
});
video.addEventListener('pause', function() {
if (!playerControls.isSeeking_) {
pauseButton.style.display = 'none';
playButton.style.display = 'block';
}
});
// seek
var seekTimeoutId = null;
seekBar.addEventListener('mousedown', function() {
playerControls.isSeeking_ = true;
video.pause();
});
seekBar.addEventListener('input', function() {
if (!video.duration) {
// Can't seek. Ignore.
return;
}
// Update the UI right away.
playerControls.updateTimeAndSeekRange_();
// Collect input events and seek when things have been stable for 100ms.
if (seekTimeoutId) {
window.clearTimeout(seekTimeoutId);
seekTimeoutId = null;
}
seekTimeoutId = window.setTimeout(function() {
seekTimeoutId = null;
video.currentTime = seekBar.value;
}, 100);
});
seekBar.addEventListener('mouseup', function() {
video.play();
playerControls.isSeeking_ = false;
});
// initialize seek bar with 0
seekBar.value = 0;
// mute/unmute
muteButton.addEventListener('click', function() {
video.muted = true;
});
unmuteButton.addEventListener('click', function() {
video.muted = false;
});
// volume
volumeBar.addEventListener('input', function() {
video.volume = volumeBar.value;
video.muted = false;
});
// volume & mute updates
var onVolumeChange = function() {
if (video.muted) {
muteButton.style.display = 'none';
unmuteButton.style.display = 'block';
} else {
unmuteButton.style.display = 'none';
muteButton.style.display = 'block';
}
volumeBar.value = video.muted ? 0 : video.volume;
var gradient = ['to right'];
gradient.push('#ccc ' + (volumeBar.value * 100) + '%');
gradient.push('#000 ' + (volumeBar.value * 100) + '%');
gradient.push('#000 100%');
volumeBar.style.background =
'linear-gradient(' + gradient.join(',') + ')';
};
video.addEventListener('volumechange', onVolumeChange);
// initialize volume display with a fake event
onVolumeChange();
// current time & seek bar updates
video.addEventListener('timeupdate', function() {
if (!playerControls.isLive_) {
playerControls.updateTimeAndSeekRange_();
}
});
// fullscreen
fullscreenButton.addEventListener('click', function() {
if (document.fullscreenElement) {
document.exitFullscreen();
} else {
videoContainer.requestFullscreen();
}
});
// fullscreen updates
var normalSize = {};
document.addEventListener('fullscreenchange', function() {
if (document.fullscreenElement) {
// remember previous size
normalSize.w = videoContainer.style.width;
normalSize.h = videoContainer.style.height;
// expand the container
videoContainer.style.width = '100%';
videoContainer.style.height = '100%';
} else {
// restore the previous size
videoContainer.style.width = normalSize.w;
videoContainer.style.height = normalSize.h;
}
});
// Jump to LIVE if the user clicks on the current time.
currentTime.addEventListener('click', function() {
if (playerControls.isLive_) {
video.currentTime = seekBar.max;
}
});
// trick play
rewindButton.addEventListener('click', playerControls.onRewind);
fastForwardButton.addEventListener('click', playerControls.onFastForward);
};
/**
* Set the player. Is needed for trick play.
* @param {shaka.player.Player} player
*/
playerControls.setPlayer = function(player) {
playerControls.player_ = player;
};
/**
* Called by the application to set the buffering state.
* @param {boolean} bufferingState
*/
playerControls.onBuffering = function(bufferingState) {
var bufferingSpinner = document.getElementById('bufferingSpinner');
bufferingSpinner.style.display = bufferingState ? 'inherit' : 'none';
};
/**
* Called by the application when the seek range changes.
* @param {Event|{start: number, end: number}} event
*/
playerControls.onSeekRangeChanged = function(event) {
playerControls.seekRange_.start = event.start;
playerControls.seekRange_.end = event.end;
playerControls.updateTimeAndSeekRange_();
};
/**
* Called by the application to set the live playback state.
* @param {boolean} liveState True if the stream is live.
*/
playerControls.setLive = function(liveState) {
playerControls.isLive_ = liveState;
};
/**
* Called when rewind button is pressed. Will circle play between -1, -2, -4
* and -8 playback rates.
*/
playerControls.onRewind = function() {
if (!playerControls.player_) return;
var rate = playerControls.player_.getPlaybackRate();
playerControls.player_.setPlaybackRate(
rate > 0 || rate < -4 ? -1.0 : rate * 2);
};
/**
* Called when fastForward button is pressed. Will circle play between 1, 2,
* 4 and 8 playback rates.
*/
playerControls.onFastForward = function() {
if (!playerControls.player_) return;
var rate = playerControls.player_.getPlaybackRate();
playerControls.player_.setPlaybackRate(rate < 0 || rate > 4 ? 1.0 : rate * 2);
};
/**
* Called by the application to switch trick play controls and the seek bar.
* @param {boolean} enable True if trick play should be enabled, if false
* seekbar will be enabled.
*/
playerControls.enableTrickPlayButtons = function(enable) {;
var seekBar = document.getElementById('seekBar');
var rewindButton = document.getElementById('rewindButton');
var fastForwardButton = document.getElementById('fastForwardButton');
rewindButton.style.display = enable ? 'block' : 'none';
fastForwardButton.style.display = enable ? 'block' : 'none';
seekBar.style.display = enable ? 'none' : 'block';
};
/**
* Called when the seek range or current time need to be updated.
* @private
*/
playerControls.updateTimeAndSeekRange_ = function() {
var video = playerControls.video_;
var seekRange = playerControls.seekRange_;
var currentTime = document.getElementById('currentTime');
var seekBar = document.getElementById('seekBar');
var displayTime = video.currentTime;
if (playerControls.isSeeking_) {
var seekBar = document.getElementById('seekBar');
displayTime = seekBar.value;
}
// Set |currentTime|.
if (playerControls.isLive_) {
// The amount of time we are behind the live edge.
displayTime = Math.max(0, Math.floor(seekRange.end - displayTime));
var showHour = (seekRange.end - seekRange.start) >= 3600;
// Consider "LIVE" when 1 second or less behind the live-edge. Always show
// the full time string when seeking, including the leading '-'; otherwise,
// the time string "flickers" near the live-edge.
if ((displayTime > 1) || playerControls.isSeeking_) {
currentTime.textContent =
'- ' + playerControls.buildTimeString_(displayTime, showHour);
} else {
currentTime.textContent = 'LIVE';
}
seekBar.min = seekRange.start;
seekBar.max = seekRange.end;
seekBar.value = seekRange.end - displayTime;
} else {
var showHour = video.duration >= 3600;
currentTime.textContent =
playerControls.buildTimeString_(displayTime, showHour);
seekBar.min = 0;
seekBar.max = video.duration;
seekBar.value = displayTime;
}
var gradient = ['to right'];
var buffered = video.buffered;
if (buffered.length == 0) {
gradient.push('#000 0%');
} else {
// NOTE: the fallback to zero eliminates NaN.
var bufferStartFraction = (buffered.start(0) / video.duration) || 0;
var bufferEndFraction = (buffered.end(0) / video.duration) || 0;
var playheadFraction = (video.currentTime / video.duration) || 0;
if (playerControls.isLive_) {
var bufferStart = Math.max(buffered.start(0), seekRange.start);
var bufferEnd = Math.min(buffered.end(0), seekRange.end);
var seekRangeSize = seekRange.end - seekRange.start;
var bufferStartDistance = bufferStart - seekRange.start;
var bufferEndDistance = bufferEnd - seekRange.start;
var playheadDistance = video.currentTime - seekRange.start;
bufferStartFraction = (bufferStartDistance / seekRangeSize) || 0;
bufferEndFraction = (bufferEndDistance / seekRangeSize) || 0;
playheadFraction = (playheadDistance / seekRangeSize) || 0;
}
gradient.push('#000 ' + (bufferStartFraction * 100) + '%');
gradient.push('#ccc ' + (bufferStartFraction * 100) + '%');
gradient.push('#ccc ' + (playheadFraction * 100) + '%');
gradient.push('#444 ' + (playheadFraction * 100) + '%');
gradient.push('#444 ' + (bufferEndFraction * 100) + '%');
gradient.push('#000 ' + (bufferEndFraction * 100) + '%');
}
seekBar.style.background = 'linear-gradient(' + gradient.join(',') + ')';
};
/**
* Builds a time string, e.g., 01:04:23, from |displayTime|.
*
* @param {number} displayTime
* @param {boolean} showHour
* @return {string}
* @private
*/
playerControls.buildTimeString_ = function(displayTime, showHour) {
var h = Math.floor(displayTime / 3600);
var m = Math.floor((displayTime / 60) % 60);
var s = Math.floor(displayTime % 60);
if (s < 10) s = '0' + s;
var text = m + ':' + s;
if (showHour) {
if (m < 10) text = '0' + text;
text = h + ':' + text;
}
return text;
};