-
Notifications
You must be signed in to change notification settings - Fork 36
/
index.js
613 lines (534 loc) · 14.4 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
// TODO
// [OK] youku support
// [OK] tudou support
// [OK] video player shortcut
// [OK] double buffered problem: http://www.bilibili.com/video/av4376362/index_3.html at 360.0s
// discontinous audio problem: http://www.bilibili.com/video/av3067286/ at 97.806,108.19
// discontinous audio problem: http://www.bilibili.com/video/av1965365/index_6.html at 51.806
// [OK] fast start
// [OK] open twice
// [OK] http://www.bilibili.com/video/av3659561/index_57.html: Error: empty range, maybe video end
// [OK] http://www.bilibili.com/video/av3659561/index_56.html: First segment too small
// [OK] double buffered problem: http://www.bilibili.com/video/av4467810/
// [OK] double buffered problem: http://www.bilibili.com/video/av3791945/
// [[2122.957988,2162.946522],[2163.041988,2173.216033]]
// [OK] video reset problem: http://www.bilibili.com/video/av314/
// [OK] video stuck problem: http://www.tudou.com/albumplay/-3O0GyT_JkQ/Az5cnjgva4k.html 16:11
// [OK] InitSegment invalid: http://www.bilibili.com/video/av1753789
// EOF error at index 67 http://www.bilibili.com/video/av4593775/
// EOF error at index 166,168 http://www.tudou.com/albumplay/92J2xqpSxWY/m4dBe7EG-7Q.html
// Test needed for safari:
// xhr cross origin, change referer header, pass arraybuffer efficiency,
// mse playing
'use strict'
let localhost = 'http://localhost:6060/'
let mediaSource = require('./mediaSource');
let Nanobar = require('nanobar');
let bilibili = require('./bilibili');
let youku = require('./youku');
let tudou = require('./tudou');
let createPlayer = require('./player');
let flashBlocker = require('./flashBlocker');
let flvdemux = require('./flvdemux');
let FastDamoo = require('./damoo');
let nanobar = new Nanobar();
let style = document.createElement('style');
let themeColor = '#DF6558';
style.innerHTML = `
.nanobar .bar {
background: ${themeColor};
}
.nanobar {
z-index: 1000001;
left: 0px;
top: 0px;
}
.mama-toolbar {
position: absolute;
z-index: 1;
bottom: 20px;
right: 20px;
}
.mama-toolbar svg {
width: 17px;
color: #fff;
fill: currentColor;
cursor: pointer;
}
.mama-toolbar {
display: flex;
padding: 5px;
padding-left: 15px;
padding-right: 15px;
border-radius: 5px;
align-items: center;
background: #333;
}
.mama-toolbar input[type=range]:focus {
outline: none;
}
.mama-toolbar .selected {
color: ${themeColor};
}
.mama-toolbar input[type=range] {
-webkit-appearance: none;
height: 9px;
width: 75px;
border-radius: 3px;
margin: 0;
margin-right: 8px;
}
.mama-toolbar input[type=range]::-webkit-slider-thumb {
-webkit-appearance: none;
height: 13px;
width: 5px;
background: ${themeColor};
border-radius: 1px;
}
`
document.head.appendChild(style);
mediaSource.debug = true;
let getSeeker = url => {
let seekers = [bilibili, youku, tudou];
let found = seekers.filter(s => s.testUrl(url));
return found[0];
}
let playVideo = res => {
let player = createPlayer();
let media = mediaSource.bindVideo({
video:player.video,
src:res.src,
duration:res.duration,
});
player.streams = media.streams;
return {player, media};
}
let handleDamoo = (vres, player, seeker, media) => {
let mode;
if (seeker.getAllDamoo) {
mode = 'all';
} else if (seeker.getDamooProgressive) {
mode = 'progressive';
}
if (!mode)
return;
let damoos = [];
(() => {
if (mode == 'all') {
return seeker.getAllDamoo(vres).then(res => {
damoos = res;
});
} else if (mode == 'progressive') {
return new Promise((fulfill, reject) => {
seeker.getDamooProgressive(vres, res => {
damoos = damoos.concat(res);
//console.log(`damoo: loaded n=${damoos.length}`);
fulfill();
})
});
}
})().then(() => {
let video = player.video;
let updating;
let cur = 0;
let emitter;
let update = () => {
let time = video.currentTime+1.0;
if (cur < damoos.length && time > damoos[cur].time) {
for (; cur < damoos.length && damoos[cur].time <= time; cur++) {
let d = damoos[cur];
//console.log('damoo: emit', `${Math.floor(d.time/60)}:${Math.floor(d.time%60)}`, d.text);
emitter.emit({text: d.text, pos: d.pos, shadow: {color: '#000'}, color: d.color});
}
}
updating = setTimeout(update, 1000);
};
let stopUpdate = () => {
if (updating) {
clearTimeout(updating);
updating = null;
}
}
let startUpdate = () => {
if (!updating)
update();
}
let resetCur = () => {
let time;
for (cur = 0; cur < damoos.length; cur++) {
if (damoos[cur].time > video.currentTime) {
time = damoos[cur].time;
break;
}
}
console.log(`damoo: cur=${cur}/${damoos.length} time=${time}`);
}
media.onSeek.push(() => {
emitter.clear();
resetCur();
})
player.onResume.push(() => {
if (emitter == null) {
emitter = new FastDamoo({container:player.damoo, fontSize:20});
let setDamooOpts = () => {
player.damoo.style.opacity = player.damooOpacity;
if (player.damooEnabled) {
emitter.show();
} else {
emitter.hide();
}
}
player.onDamooOptsChange.push(() => setDamooOpts());
setDamooOpts();
}
emitter.synctime(video.currentTime);
emitter.resume()
startUpdate();
});
player.onSuspend.push(() => {
emitter.synctime(video.currentTime);
emitter.suspend()
stopUpdate();
});
});
}
let playUrl = url => {
return new Promise((fulfill, reject) => {
let seeker = getSeeker(url)
if (seeker) {
flashBlocker();
nanobar.go(30);
seeker.getVideos(url).then(res => {
console.log('getVideosResult:', res);
if (res) {
let ctrl = playVideo(res);
ctrl.player.onStarted.push(() => nanobar.go(100));
handleDamoo(res, ctrl.player, seeker, ctrl.media);
nanobar.go(60)
fulfill(ctrl);
} else {
throw new Error('getVideosResult: invalid')
}
}).catch(e => {
nanobar.go(100);
throw e;
});
} else {
throw new Error('seeker not found');
}
});
}
let cmd = {};
cmd.youku = youku;
cmd.tudou = tudou;
cmd.bilibili = bilibili;
cmd.testDanmuLayer = () => {
let danmu = createDamnuLayer(document.body);
}
cmd.fetchSingleFlvMediaSegments = (url, duration, indexStart, indexEnd) => {
let streams = new mediaSource.Streams({
urls: [localhost+url],
fakeDuration: duration,
});
streams.onProbeProgress = (stream, i) => {
if (i == 0) {
streams.fetchMediaSegmentsByIndex(indexStart, indexEnd);
}
};
streams.probeOneByOne();
}
cmd.playSingleFlv = (url, duration, pos) => {
cmd.ctrl = playVideo({
src:[
localhost+url,
],
duration,
});
if (pos)
setTimeout(() => cmd.ctrl.player.video.currentTime = pos, 500);
}
cmd.getVideos = url => {
let seeker = getSeeker(url);
if (!seeker) {
console.log('seeker not found');
return;
}
seeker.getVideos(url).then(res => console.log(res))
}
cmd.playUrl = playUrl;
cmd.testXhr = () => {
var xhr = new XMLHttpRequest();
xhr.open('GET', localhost+'projectindex-0.flv');
setTimeout(() => xhr.abort(), 100);
xhr.onload = function(e) {
console.log(this.status);
console.log(this.response.length);
}
xhr.onerror = function() {
console.log('onerror')
}
xhr.send();
}
cmd.testWriteFile = () => {
let errfunc = e => console.error(e);
webkitRequestFileSystem(TEMPORARY, 1*1024*1024*1024, fs => {
fs.root.getFile('tmp.bin', {create:true}, file => {
file.createWriter(writer => {
writer.onwrittend = () => console.log('write complete');
//writer.truncate(1024*1024);
for (let i = 0; i < 1024*1024*10; i++) {
let u8 = new Uint8Array([0x65,0x65,0x65,0x65]);
writer.write(new Blob([u8]));
}
let a = document.createElement('a');
a.href = file.toURL();
a.download = 'a.txt';
document.body.appendChild(a);
a.click();
});
}, errfunc);
}, errfunc);
}
cmd.testfetch = () => {
let dbp = console.log.bind(console)
let parser = new flvdemux.InitSegmentParser();
let total = 0;
let pump = reader => {
return reader.read().then(res => {
if (res.done) {
dbp('parser: EOF');
return;
}
let chunk = res.value;
total += chunk.byteLength;
dbp(`parser: incoming ${chunk.byteLength}`);
let done = parser.push(chunk);
if (done) {
dbp('parser: finished', done);
reader.cancel();
return done;
} else {
return pump(reader);
}
});
}
let headers = new Headers();
headers.append('Range', 'bytes=0-400000');
fetch(`http://27.221.48.172/youku/65723A1CDA44683D499698466F/030001290051222DE95D6C055EEB3EBFDE3F09-E65E-1E0A-218C-3CDFACC4F973.flv`, {headers}).then(res => pump(res.body.getReader()))
.then(res => console.log(res));
}
cmd.testInitSegment = () => {
let dbp = console.log.bind(console)
let meta;
let fetchseg = seg => {
return fetch(localhost+'test-fragmented.mp4', {headers: {
Range: `bytes=${seg.offset}-${seg.offset+seg.size-1}`,
}}).then(res=>res.arrayBuffer());
}
fetch(localhost+'test-fragmented-manifest.json').then(res=>res.json()).then(res => {
meta = res;
dbp('meta', meta);
}).then(res => {
res = new Uint8Array(res);
let mediaSource = new MediaSource();
let video = document.createElement('video');
document.body.appendChild(video);
video.src = URL.createObjectURL(mediaSource);
video.autoplay = true;
video.addEventListener('loadedmetadata', () => {
dbp('loadedmetadata', video.duration);
});
let sourceBuffer;
mediaSource.addEventListener('sourceopen', e => {
dbp('sourceopen');
if (mediaSource.sourceBuffers.length > 0)
return;
let codecType = meta.type;
sourceBuffer = mediaSource.addSourceBuffer(codecType);
sourceBuffer.mode = 'sequence';
sourceBuffer.addEventListener('error', () => dbp('sourceBuffer: error'));
sourceBuffer.addEventListener('abort', () => dbp('sourceBuffer: abort'));
sourceBuffer.addEventListener('update', () => {
dbp('sourceBuffer: update');
})
sourceBuffer.addEventListener('updateend', () => {
let ranges = [];
let buffered = sourceBuffer.buffered;
for (let i = 0; i < buffered.length; i++) {
ranges.push([buffered.start(i), buffered.end(i)]);
}
dbp('sourceBuffer: updateend');
dbp('buffered', JSON.stringify(ranges), 'duration', video.duration);
});
fetchseg(meta.init).then(() => {
sourceBuffer.appendBuffer(res);
return fetchseg(meta.media[1]).then(res => {
dbp(res.byteLength);
sourceBuffer.appendBuffer(res);
});
});
})
mediaSource.addEventListener('sourceended', () => dbp('mediaSource: sourceended'))
mediaSource.addEventListener('sourceclose', () => dbp('mediaSource: sourceclose'))
}).catch(e => {
console.error(e);
});
}
cmd.testPlayer = () => {
let player = createPlayer();
player.video.src = localhost+'projectindex.mp4';
player.video.muted = true;
}
//cmd.testPlayer();
cmd.testCanvasSpeed = () => {
let canvas = document.createElement('canvas');
canvas.width = 1280;
canvas.height = 800;
let ctx = canvas.getContext('2d');
let line = [];
for (let i = 0; i < 3; i++) {
let c = document.createElement('canvas');
c.width = 100;
c.height = 100;
line.push(c);
}
console.log('canvas', canvas.width, canvas.height);
var _RAF = window.requestAnimationFrame ||
window.mozRequestAnimationFrame ||
window.webkitRequestAnimationFrame ||
window.msRequestAnimationFrame ||
window.oRequestAnimationFrame ||
function(cb) { return setTimeout(cb, 17); };
setInterval(() => {
line.forEach(c => {
ctx.drawImage(c, 0, 0);
console.log('draw');
});
}, 1000/24);
}
cmd.testCssTransition = () => {
let freelist = [];
const FONTSIZE = 25;
const ROWS = 50;
let currow = 0;
let container = document.createElement('div');
container.style.width = '100%';
container.style.height = '100%';
document.body.appendChild(container);
for (let i = 0; i < 200; i++) {
let p = document.createElement('canvas');
p.width = 1;
p.height = 1;
p.style.position = 'absolute';
p.style.backgroundColor = 'transparent';
container.appendChild(p);
freelist.push(p);
}
let emit = ({text, pos, color, shadow}) => {
if (freelist.length == 0)
return;
currow++;
if (currow > ROWS)
currow = 0;
color = color || '#fff';
shadow = shadow || {color: '#000'};
pos = pos || 'normal';
let p = freelist[0];
freelist = freelist.slice(1);
let size = FONTSIZE;
let ctx = p.getContext('2d');
ctx.font = `${size}px Arail`;
p.width = ctx.measureText(text).width;
p.height = size*1.5;
ctx.font = `${size}px Arail`;
ctx.fillStyle = color;
ctx.textAlign = "start";
ctx.textBaseline = "top";
if (shadow) {
ctx.shadowOffsetX = 1;
ctx.shadowOffsetY = 1;
ctx.shadowColor = shadow.color;
}
ctx.fillText(text, 0, 0);
let time = 5;
let movew = container.offsetWidth+p.width;
p.style.top = `${currow*FONTSIZE}px`;
if (pos == 'top') {
p.style.left = `${(container.offsetWidth-p.width)/2}px`;
} else {
p.style.right = `${-p.width}px`;
}
p.style.display = 'block';
setTimeout(() => {
p.style.display = 'none';
freelist.push(p);
}, time*1000);
if (pos == 'normal') {
setTimeout(() => {
p.style.transition = `transform ${time}s linear`;
p.style.transform = `translate(-${movew}px,0)`;
}, 50);
}
}
emit({text:'哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩哔哩'});
emit({text:'我占了中间位置', color:'#f00', pos:'top'});
//setInterval(reset, 2000);
}
cmd.testDamoo = () => {
let div = document.createElement('div');
document.body.style.margin = '0';
let resize = () => {
div.style.height = window.innerHeight+'px';
div.style.width = window.innerWidth+'px';
}
window.addEventListener('resize', () => resize());
resize();
div.innerHTML = `
<h1>Background</h1>
`;
div.style.background = '#eee';
document.body.appendChild(div);
let dm = new FastDamoo({container:div, fontSize:20});
dm.show();
dm.resume();
dm.emit({text:'小小小的文字', color:'#000'});
dm.emit({text:'小小小的文字', color:'#000', pos:'bottom'});
dm.emit({text:'稍微长一点的文字2333333333333333333', color:'#000', pos:'top'});
document.body.addEventListener('keydown', (e) => {
switch (e.code) {
case "KeyR": {
dm.resume();
} break;
case "KeyP": {
dm.suspend();
} break;
case "KeyS": {
dm.show();
} break;
case "KeyH": {
dm.hide();
} break;
}
});
let i = 0;
let timer = setInterval(() => {
i++;
if (i > 300) {
clearInterval(timer);
return;
}
let text = '哔哩哔哩';
for (let i = 0; i < 4; i++)
text = text+text;
dm.emit({
text,
color: '#f00',
});
}, 10);
}
//cmd.testDamoo()
if (location.href.substr(0,6) != 'chrome') {
playUrl(location.href);
} else {
window.cmd = cmd;
}