forked from phoboslab/jsmpeg
-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.js
324 lines (269 loc) · 7.94 KB
/
player.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
JSMpeg.Player = (function(){ "use strict";
var Player = function(url, options) {
this.options = options || {};
if (options.source) {
this.source = new options.source(url, options);
options.streaming = !!this.source.streaming;
}
else if (url.match(/^wss?:\/\//)) {
this.source = new JSMpeg.Source.WebSocket(url, options);
options.streaming = true;
}
else if (options.progressive !== false) {
this.source = new JSMpeg.Source.AjaxProgressive(url, options);
options.streaming = false;
}
else {
this.source = new JSMpeg.Source.Ajax(url, options);
options.streaming = false;
}
this.maxAudioLag = options.maxAudioLag || 0.25;
this.loop = options.loop !== false;
this.autoplay = !!options.autoplay || options.streaming;
this.demuxer = new JSMpeg.Demuxer.TS(options);
this.source.connect(this.demuxer);
if (!options.disableWebAssembly && JSMpeg.WASMModule.IsSupported()) {
this.wasmModule = JSMpeg.WASMModule.GetModule();
options.wasmModule = this.wasmModule;
}
if (options.video !== false) {
this.video = options.wasmModule
? new JSMpeg.Decoder.MPEG1VideoWASM(options)
: new JSMpeg.Decoder.MPEG1Video(options);
this.renderer = !options.disableGl && JSMpeg.Renderer.WebGL.IsSupported()
? new JSMpeg.Renderer.WebGL(options)
: new JSMpeg.Renderer.Canvas2D(options);
this.demuxer.connect(JSMpeg.Demuxer.TS.STREAM.VIDEO_1, this.video);
this.video.connect(this.renderer);
}
if (options.audio !== false && JSMpeg.AudioOutput.WebAudio.IsSupported()) {
this.audio = options.wasmModule
? new JSMpeg.Decoder.MP2AudioWASM(options)
: new JSMpeg.Decoder.MP2Audio(options);
this.audioOut = new JSMpeg.AudioOutput.WebAudio(options);
this.demuxer.connect(JSMpeg.Demuxer.TS.STREAM.AUDIO_1, this.audio);
this.audio.connect(this.audioOut);
}
Object.defineProperty(this, 'currentTime', {
get: this.getCurrentTime,
set: this.setCurrentTime
});
Object.defineProperty(this, 'volume', {
get: this.getVolume,
set: this.setVolume
});
this.paused = true;
this.unpauseOnShow = false;
if (options.pauseWhenHidden !== false) {
document.addEventListener('visibilitychange', this.showHide.bind(this));
}
// If we have WebAssembly support, wait until the module is compiled before
// loading the source. Otherwise the decoders won't know what to do with
// the source data.
if (this.wasmModule) {
if (this.wasmModule.ready) {
this.startLoading();
}
else if (JSMpeg.WASM_BINARY_INLINED) {
var wasm = JSMpeg.Base64ToArrayBuffer(JSMpeg.WASM_BINARY_INLINED);
this.wasmModule.loadFromBuffer(wasm, this.startLoading.bind(this));
}
else {
this.wasmModule.loadFromFile('jsmpeg.wasm', this.startLoading.bind(this));
}
}
else {
this.startLoading();
}
};
Player.prototype.startLoading = function() {
this.source.start();
if (this.autoplay) {
this.play();
}
};
Player.prototype.showHide = function(ev) {
if (document.visibilityState === 'hidden') {
this.unpauseOnShow = this.wantsToPlay;
this.pause();
}
else if (this.unpauseOnShow) {
this.play();
}
};
Player.prototype.play = function(ev) {
if (this.animationId) {
return;
}
this.animationId = requestAnimationFrame(this.update.bind(this));
this.wantsToPlay = true;
this.paused = false;
};
Player.prototype.pause = function(ev) {
if (this.paused) {
return;
}
cancelAnimationFrame(this.animationId);
this.animationId = null;
this.wantsToPlay = false;
this.isPlaying = false;
this.paused = true;
if (this.audio && this.audio.canPlay) {
// Seek to the currentTime again - audio may already be enqueued a bit
// further, so we have to rewind it.
this.audioOut.stop();
this.seek(this.currentTime);
}
if (this.options.onPause) {
this.options.onPause(this);
}
};
Player.prototype.getVolume = function() {
return this.audioOut ? this.audioOut.volume : 0;
};
Player.prototype.setVolume = function(volume) {
if (this.audioOut) {
this.audioOut.volume = volume;
}
};
Player.prototype.stop = function(ev) {
this.pause();
this.seek(0);
if (this.video && this.options.decodeFirstFrame !== false) {
this.video.decode();
}
};
Player.prototype.destroy = function() {
this.pause();
this.source.destroy();
this.video && this.video.destroy();
this.renderer && this.renderer.destroy();
this.audio && this.audio.destroy();
this.audioOut && this.audioOut.destroy();
};
Player.prototype.seek = function(time) {
var startOffset = this.audio && this.audio.canPlay
? this.audio.startTime
: this.video.startTime;
if (this.video) {
this.video.seek(time + startOffset);
}
if (this.audio) {
this.audio.seek(time + startOffset);
}
this.startTime = JSMpeg.Now() - time;
};
Player.prototype.getCurrentTime = function() {
return this.audio && this.audio.canPlay
? this.audio.currentTime - this.audio.startTime
: this.video.currentTime - this.video.startTime;
};
Player.prototype.setCurrentTime = function(time) {
this.seek(time);
};
Player.prototype.update = function() {
this.animationId = requestAnimationFrame(this.update.bind(this));
if (!this.source.established) {
if (this.renderer) {
this.renderer.renderProgress(this.source.progress);
}
return;
}
if (!this.isPlaying) {
this.isPlaying = true;
this.startTime = JSMpeg.Now() - this.currentTime;
if (this.options.onPlay) {
this.options.onPlay(this);
}
}
if (this.options.streaming) {
this.updateForStreaming();
}
else {
this.updateForStaticFile();
}
};
Player.prototype.updateForStreaming = function() {
// When streaming, immediately decode everything we have buffered up until
// now to minimize playback latency.
if (this.video) {
this.video.decode();
}
if (this.audio) {
var decoded = false;
do {
// If there's a lot of audio enqueued already, disable output and
// catch up with the encoding.
if (this.audioOut.enqueuedTime > this.maxAudioLag) {
this.audioOut.resetEnqueuedTime();
this.audioOut.enabled = false;
}
decoded = this.audio.decode();
} while (decoded);
this.audioOut.enabled = true;
}
};
Player.prototype.nextFrame = function() {
if (this.source.established && this.video) {
return this.video.decode();
}
return false;
};
Player.prototype.updateForStaticFile = function() {
var notEnoughData = false,
headroom = 0;
// If we have an audio track, we always try to sync the video to the audio.
// Gaps and discontinuities are far more percetable in audio than in video.
if (this.audio && this.audio.canPlay) {
// Do we have to decode and enqueue some more audio data?
while (
!notEnoughData &&
this.audio.decodedTime - this.audio.currentTime < 0.25
) {
notEnoughData = !this.audio.decode();
}
// Sync video to audio
if (this.video && this.video.currentTime < this.audio.currentTime) {
notEnoughData = !this.video.decode();
}
headroom = this.demuxer.currentTime - this.audio.currentTime;
}
else if (this.video) {
// Video only - sync it to player's wallclock
var targetTime = (JSMpeg.Now() - this.startTime) + this.video.startTime,
lateTime = targetTime - this.video.currentTime,
frameTime = 1/this.video.frameRate;
if (this.video && lateTime > 0) {
// If the video is too far behind (>2 frames), simply reset the
// target time to the next frame instead of trying to catch up.
if (lateTime > frameTime * 2) {
this.startTime += lateTime;
}
notEnoughData = !this.video.decode();
}
headroom = this.demuxer.currentTime - targetTime;
}
// Notify the source of the playhead headroom, so it can decide whether to
// continue loading further data.
this.source.resume(headroom);
// If we failed to decode and the source is complete, it means we reached
// the end of our data. We may want to loop.
if (notEnoughData && this.source.completed) {
if (this.loop) {
this.seek(0);
}
else {
this.pause();
if (this.options.onEnded) {
this.options.onEnded(this);
}
}
}
// If there's not enough data and the source is not completed, we have
// just stalled.
else if (notEnoughData && this.options.onStalled) {
this.options.onStalled(this);
}
};
return Player;
})();