forked from fregante/iphone-inline-video
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
285 lines (250 loc) · 8 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
'use strict';
import Symbol from 'poor-mans-symbol';
import Intervalometer from './lib/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';
// iOS 10 adds support for native inline playback + silent autoplay
// Also adds unprefixed css-grid. This check essentially excludes
const isWhitelisted = /iPhone|iPod/i.test(navigator.userAgent) && document.head.style.grid === undefined;
const ಠ = Symbol();
const ಠevent = Symbol();
const ಠplay = Symbol('nativeplay');
const ಠpause = Symbol('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[ಠevent] = 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) {
// 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[ಠevent]; // allow timeupdate event
player.video.pause(true);
}
}
/**
* METHODS
*/
function play() {
// console.log('play');
const video = this;
const player = video[ಠ];
// if it's fullscreen, use the native player
if (video.webkitDisplayingFullscreen) {
video[ಠplay]();
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) {
// .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[ಠ];
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[ಠpause]();
}
if (player.paused && !forceEvents) {
return;
}
player.paused = true;
if (!player.hasAudio) {
dispatchEventAsync(video, 'pause');
}
if (video.ended) {
video[ಠevent] = true;
dispatchEventAsync(video, 'ended');
}
}
/**
* SETUP
*/
function addPlayer(video, hasAudio) {
const player = video[ಠ] = {};
player.paused = true; // track whether 'pause' events have been fired
player.hasAudio = hasAudio;
player.video = video;
player.updater = new Intervalometer(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) {
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[ಠplay]();
} else if (hasAudio && !player.driver.buffered.length) {
// 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 overloadAPI(video) {
const player = video[ಠ];
video[ಠplay] = video.play;
video[ಠpause] = 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);
preventEvent(video, 'seeking');
preventEvent(video, 'seeked');
preventEvent(video, 'timeupdate', ಠevent, false);
preventEvent(video, 'ended', ಠevent, false); // prevent occasional native ended events
}
function enableInlineVideo(video, hasAudio = true, onlyWhitelisted = true) {
if ((onlyWhitelisted && !isWhitelisted) || video[ಠ]) {
return;
}
addPlayer(video, hasAudio);
overloadAPI(video);
video.classList.add('IIV');
if (!hasAudio && video.autoplay) {
video.play();
}
if (navigator.platform === 'MacIntel' || navigator.platform === 'Windows') {
console.warn('iphone-inline-video is not guaranteed to work in emulated environments');
}
}
enableInlineVideo.isWhitelisted = isWhitelisted;
export default enableInlineVideo;