This repository has been archived by the owner on Nov 20, 2021. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 297
/
index.js
323 lines (278 loc) · 8.99 KB
/
index.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
import {frameIntervalometer} from 'intervalometer';
import preventEvent from './lib/prevent-event';
import proxyProperty from './lib/proxy-property';
import proxyEvent from './lib/proxy-event';
import dispatchEventAsync from './lib/dispatch-event-async';
const iOS8or9 = typeof document === 'object' && 'object-fit' in document.head.style && !matchMedia('(-webkit-video-playable-inline)').matches;
const IIV = 'fregante:iphone-inline-video';
const IIVEvent = 'fregante:iphone-inline-video:event';
const IIVPlay = 'fregante:iphone-inline-video:nativeplay';
const IIVPause = 'fregante:iphone-inline-video:nativepause';
/**
* UTILS
*/
function getAudioFromVideo(video) {
const audio = new Audio();
proxyEvent(video, 'play', audio);
proxyEvent(video, 'playing', audio);
proxyEvent(video, 'pause', audio);
audio.crossOrigin = video.crossOrigin;
// 'data:' causes audio.networkState > 0
// which then allows to keep <audio> in a resumable playing state
// i.e. once you set a real src it will keep playing if it was if .play() was called
audio.src = video.src || video.currentSrc || 'data:';
// // if (audio.src === 'data:') {
// TODO: wait for video to be selected
// // }
return audio;
}
const lastRequests = [];
let requestIndex = 0;
let lastTimeupdateEvent;
function setTime(video, time, rememberOnly) {
// Allow one timeupdate event every 200+ ms
if ((lastTimeupdateEvent || 0) + 200 < Date.now()) {
video[IIVEvent] = true;
lastTimeupdateEvent = Date.now();
}
if (!rememberOnly) {
video.currentTime = time;
}
lastRequests[++requestIndex % 3] = time * 100 | 0 / 100;
}
function isPlayerEnded(player) {
return player.driver.currentTime >= player.video.duration;
}
function update(timeDiff) {
const player = this;
// // console.log('update', player.video.readyState, player.video.networkState, player.driver.readyState, player.driver.networkState, player.driver.paused);
if (player.video.readyState >= player.video.HAVE_FUTURE_DATA) {
if (!player.hasAudio) {
player.driver.currentTime = player.video.currentTime + ((timeDiff * player.video.playbackRate) / 1000);
if (player.video.loop && isPlayerEnded(player)) {
player.driver.currentTime = 0;
}
}
setTime(player.video, player.driver.currentTime);
} else if (player.video.networkState === player.video.NETWORK_IDLE && player.video.buffered.length === 0) {
// This should happen when the source is available but:
// - it's potentially playing (.paused === false)
// - it's not ready to play
// - it's not loading
// If it hasAudio, that will be loaded in the 'emptied' handler below
player.video.load();
// // console.log('Will load');
}
// // console.assert(player.video.currentTime === player.driver.currentTime, 'Video not updating!');
if (player.video.ended) {
delete player.video[IIVEvent]; // Allow timeupdate event
player.video.pause(true);
}
}
/**
* METHODS
*/
function play() {
// // console.log('play');
const video = this;
const player = video[IIV];
// If it's fullscreen, use the native player
if (video.webkitDisplayingFullscreen) {
video[IIVPlay]();
return;
}
if (player.driver.src !== 'data:' && player.driver.src !== video.src) {
// // console.log('src changed on play', video.src);
setTime(video, 0, true);
player.driver.src = video.src;
}
if (!video.paused) {
return;
}
player.paused = false;
if (video.buffered.length === 0) {
// .load() causes the emptied event
// the alternative is .play()+.pause() but that triggers play/pause events, even worse
// possibly the alternative is preventing this event only once
video.load();
}
player.driver.play();
player.updater.start();
if (!player.hasAudio) {
dispatchEventAsync(video, 'play');
if (player.video.readyState >= player.video.HAVE_ENOUGH_DATA) {
// // console.log('onplay');
dispatchEventAsync(video, 'playing');
}
}
}
function pause(forceEvents) {
// // console.log('pause');
const video = this;
const player = video[IIV];
player.driver.pause();
player.updater.stop();
// If it's fullscreen, the developer the native player.pause()
// This is at the end of pause() because it also
// needs to make sure that the simulation is paused
if (video.webkitDisplayingFullscreen) {
video[IIVPause]();
}
if (player.paused && !forceEvents) {
return;
}
player.paused = true;
if (!player.hasAudio) {
dispatchEventAsync(video, 'pause');
}
// Handle the 'ended' event only if it's not fullscreen
if (video.ended && !video.webkitDisplayingFullscreen) {
video[IIVEvent] = true;
dispatchEventAsync(video, 'ended');
}
}
/**
* SETUP
*/
function addPlayer(video, hasAudio) {
const player = {};
video[IIV] = player;
player.paused = true; // Track whether 'pause' events have been fired
player.hasAudio = hasAudio;
player.video = video;
player.updater = frameIntervalometer(update.bind(player));
if (hasAudio) {
player.driver = getAudioFromVideo(video);
} else {
video.addEventListener('canplay', () => {
if (!video.paused) {
// // console.log('oncanplay');
dispatchEventAsync(video, 'playing');
}
});
player.driver = {
src: video.src || video.currentSrc || 'data:',
muted: true,
paused: true,
pause: () => {
player.driver.paused = true;
},
play: () => {
player.driver.paused = false;
// Media automatically goes to 0 if .play() is called when it's done
if (isPlayerEnded(player)) {
setTime(video, 0);
}
},
get ended() {
return isPlayerEnded(player);
}
};
}
// .load() causes the emptied event
video.addEventListener('emptied', () => {
// // console.log('driver src is', player.driver.src);
const wasEmpty = !player.driver.src || player.driver.src === 'data:';
if (player.driver.src && player.driver.src !== video.src) {
// // console.log('src changed to', video.src);
setTime(video, 0, true);
player.driver.src = video.src;
// Playing videos will only keep playing if no src was present when .play()’ed
if (wasEmpty || (!hasAudio && video.autoplay)) {
player.driver.play();
} else {
player.updater.stop();
}
}
}, false);
// Stop programmatic player when OS takes over
video.addEventListener('webkitbeginfullscreen', () => {
if (!video.paused) {
// Make sure that the <audio> and the syncer/updater are stopped
video.pause();
// Play video natively
video[IIVPlay]();
} else if (hasAudio && player.driver.buffered.length === 0) {
// If the first play is native,
// the <audio> needs to be buffered manually
// so when the fullscreen ends, it can be set to the same current time
player.driver.load();
}
});
if (hasAudio) {
video.addEventListener('webkitendfullscreen', () => {
// Sync audio to new video position
player.driver.currentTime = video.currentTime;
// // console.assert(player.driver.currentTime === video.currentTime, 'Audio not synced');
});
// Allow seeking
video.addEventListener('seeking', () => {
if (lastRequests.indexOf(video.currentTime * 100 | 0 / 100) < 0) {
// // console.log('User-requested seeking');
player.driver.currentTime = video.currentTime;
}
});
}
}
function preventWithPropOrFullscreen(el) {
const isAllowed = el[IIVEvent];
delete el[IIVEvent];
return !el.webkitDisplayingFullscreen && !isAllowed;
}
function overloadAPI(video) {
const player = video[IIV];
video[IIVPlay] = video.play;
video[IIVPause] = video.pause;
video.play = play;
video.pause = pause;
proxyProperty(video, 'paused', player.driver);
proxyProperty(video, 'muted', player.driver, true);
proxyProperty(video, 'playbackRate', player.driver, true);
proxyProperty(video, 'ended', player.driver);
proxyProperty(video, 'loop', player.driver, true);
// IIV works by seeking 60 times per second.
// These events are now useless.
preventEvent(video, 'seeking', el => !el.webkitDisplayingFullscreen);
preventEvent(video, 'seeked', el => !el.webkitDisplayingFullscreen);
// Limit timeupdate events
preventEvent(video, 'timeupdate', preventWithPropOrFullscreen);
// Prevent occasional native ended events
preventEvent(video, 'ended', preventWithPropOrFullscreen);
}
export default function enableInlineVideo(video, opts = {}) {
// Stop if already enabled
if (video[IIV]) {
return;
}
// Allow the user to skip detection
if (!opts.everywhere) {
// Only iOS8 and 9 are supported
if (!iOS8or9) {
return;
}
// Stop if it's not an allowed device
if (!(opts.iPad || opts.ipad ? /iPhone|iPod|iPad/ : /iPhone|iPod/).test(navigator.userAgent)) {
return;
}
}
// Try to pause
video.pause();
// Prevent autoplay.
// An non-started autoplaying video can't be .pause()'d
const willAutoplay = video.autoplay;
video.autoplay = false;
addPlayer(video, !video.muted);
overloadAPI(video);
video.classList.add('IIV');
// Autoplay
if (video.muted && willAutoplay) {
video.play();
video.addEventListener('playing', function restoreAutoplay() {
video.autoplay = true;
video.removeEventListener('playing', restoreAutoplay);
});
}
if (!/iPhone|iPod|iPad/.test(navigator.platform)) {
console.warn('iphone-inline-video is not guaranteed to work in emulated environments');
}
}