1 Star 0 Fork 172

tjc/PearPlayer.js 流媒体播放器

forked from Pear/PearPlayer.js 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
index.player.js 3.40 KB
一键复制 编辑 原始数据 按行查看 历史
DIYgod 提交于 2018-01-19 18:38 . remove useless code
/**
* Created by xieting on 2017/11/8.
*/
module.exports = PearPlayer;
var debug = require('debug')('pear:player');
var inherits = require('inherits');
var render = require('render-media');
var PearDownloader = require('./src/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 (!opts) opts = {};
if (typeof selector === 'string') {
self.video = document.querySelector(selector);
} else if (Object.prototype.toString.call(selector) === '[object HTMLVideoElement]') {
self.video = selector;
} else {
throw new Error('illegal video selector');
}
opts.selector = selector;
opts.render = render;
opts.sequencial = true; //player必须有序下载buffer
opts.interval = 3000;
if (!opts.algorithm) opts.algorithm = 'pull'; //algorithm默认‘pull’
//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!
// debug('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 () {
var dispatcher = self.dispatcher;
if (dispatcher) {
var bitrate = Math.ceil(dispatcher.fileSize/self.video.duration);
var windowLength = Math.ceil(bitrate * 15 / dispatcher.pieceLength); //根据码率和时间间隔来计算窗口长度
// console.warn('dispatcher._windowLength:'+dispatcher._windowLength);
// self.normalWindowLength = self._windowLength;
if (windowLength < 3) {
windowLength = 3;
} else if (self._windowLength > 15) {
windowLength = 15;
}
dispatcher._windowLength = windowLength;
dispatcher.interval = 5000;
// console.warn('dispatcher._windowLength:'+dispatcher._windowLength);
// self._colddown = 5/self._slideInterval*self._interval2BufPos + 5; //窗口滑动的冷却时间
// self._colddown = self._windowLength*2;
// self._colddown = 5;
self.emit('metadata', {'bitrate': bitrate, 'duration': self.video.duration});
}
});
}
PearPlayer.isWebRTCSupported = function () {
return PearDownloader.isWebRTCSupported();
};
PearPlayer.isMSESupported = function () {
return isMSESupported();
};
function isMSESupported() {
return !!(window['MediaSource'] || window['WebKitMediaSource']);
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/t2018/PearPlayer.js.git
git@gitee.com:t2018/PearPlayer.js.git
t2018
PearPlayer.js
PearPlayer.js 流媒体播放器
master

搜索帮助