-
Notifications
You must be signed in to change notification settings - Fork 50
/
index.player.js
97 lines (76 loc) · 3.25 KB
/
index.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
/**
* Created by xieting on 2017/11/8.
*/
module.exports = PearPlayer;
var inherits = require('inherits');
var render = require('render-media');
var PearDownloader = require('./index.downloader');
var WebTorrent = require('webtorrent');
inherits(PearPlayer, PearDownloader);
function PearPlayer(selector, token, opts) {
var self = this;
if (!(self instanceof PearPlayer)) return new PearPlayer(selector, token, opts);
if (typeof token === 'object') return PearPlayer(selector, '', token);
if (typeof selector !== 'string') throw new Error('video selector must be a string!');
self.video = document.querySelector(selector);
opts.selector = selector;
opts.render = render;
//monitor
self.canPlayDelayStart = (new Date()).getTime();
if (opts.BTMode && opts.magnetURI) {
var client = new WebTorrent();
return client.add(opts.magnetURI, function (torrent) {
// Got torrent metadata!
// console.log('Client is downloading:', torrent.infoHash)
torrent.files.forEach(function (file) {
render.render(file, opts.selector, {autoplay: opts.autoplay});
})
})
}
PearDownloader.call(self, opts.src || self.video.src, token, opts);
self.setupListeners();
}
PearPlayer.prototype.setupListeners = function () {
var self = this;
self.video.addEventListener('canplay', function () {
self.canPlayDelayEnd = (new Date()).getTime();
var canPlayDelay = (self.canPlayDelayEnd - self.canPlayDelayStart);
self.emit('canplay', canPlayDelay);
});
self.video.addEventListener('loadedmetadata', function () {
// console.info('loadedmetadata duration:' + self.video.duration);
// self.bitrate = Math.ceil(self.fileSize/self.video.duration);
// self._windowLength = Math.ceil(self.bitrate * 15 / self.pieceLength); //根据码率和时间间隔来计算窗口长度
// self.normalWindowLength = self._windowLength;
// if (self._windowLength < 3) {
// self._windowLength = 3;
// } else if (self._windowLength > 15) {
// self._windowLength = 15;
// }
// // self._colddown = 5/self._slideInterval*self._interval2BufPos + 5; //窗口滑动的冷却时间
// // self._colddown = self._windowLength*2;
// self._colddown = 5;
// self.emit('metadata', {'bitrate': self.bitrate, 'duration': self.video.duration});
// if (self.useTorrent && self.magnetURI) {
// var client = new WebTorrent();
// // client.on('error', function () {
// //
// // });
// console.log('magnetURI:'+self.magnetURI);
// client.add(self.magnetURI, {
// announce: self.trackers || [
// "wss://tracker.openwebtorrent.com",
// "wss://tracker.btorrent.xyz"
// ],
// store: d.store,
// bitfield: d.bitfield
// },
// function (torrent) {
// console.log('Torrent:', torrent);
//
// d.addTorrent(torrent);
// }
// );
// }
});
}