Skip to content

Commit

Permalink
优化节点调度算法
Browse files Browse the repository at this point in the history
  • Loading branch information
谢庭 authored and 谢庭 committed Jul 19, 2017
1 parent 174cc11 commit 5b041a5
Show file tree
Hide file tree
Showing 8 changed files with 104 additions and 41 deletions.
4 changes: 2 additions & 2 deletions dist/pear-player.min.js

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ var PearPlayer = require('PearPlayer');
algorithm: 'firstaid', //核心算法,默认firstaid
autoplay: true, //是否自动播放视频,默认true
interval: 5000, //滑动窗口的时间间隔,单位毫秒,默认10s
auto: false, //true为连续下载buffer,false则是只有当前播放时间与已缓冲时间小于slideInterval时下载buffer,默认true
auto: false, //true为连续下载buffer,false则是只有当前播放时间与已缓冲时间小于slideInterval时下载buffer,默认false
slideInterval: 15, //当前播放时间与已缓冲时间小于这个数值时触发窗口滑动,单位秒,默认20s
useDataChannel: true, //是否开启data channel,默认true
dataChannels: 4, //创建data channel的最大数量,默认3
dataChannels: 4, //创建data channel的最大数量,默认2
useMonitor: true //是否开启monitor,会稍微影响性能,默认false,只有开启useMonitor才能监听事件
}
```
Expand Down
6 changes: 3 additions & 3 deletions examples/test.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<body>
<div id="main">
<video id="pearvideo" src="https://qq.webrtc.win/tv/pear001.mp4" controls>
<!--<video id="pearvideo" src="https://qq.webrtc.win/free/planet.mp4" controls>-->
<!--<video id="pearvideo" src="https://qq.webrtc.win/tv/cqz/yxs-06.mp4" controls>-->
</video>
</div>
<!--<script src="https://cdn.jsdelivr.net/npm/pearplayer@latest/dist/pear-player.min.js"></script>-->
Expand All @@ -32,10 +32,10 @@
algorithm: 'firstaid', //核心算法,默认firstaid
autoplay: true, //是否自动播发视频,默认true
interval: 5000, //滑动窗口的时间间隔,单位毫秒,默认10s
auto: false, //true为连续下载buffer,false则是只有当前播放时间与已缓冲时间小于slideInterval时下载buffer,默认true
auto: false, //true为连续下载buffer,false则是只有当前播放时间与已缓冲时间小于slideInterval时下载buffer,默认false
slideInterval: 15, //当前播放时间与已缓冲时间小于这个数值时触发窗口滑动,单位秒,默认20s
useDataChannel: true, //是否开启data channel,默认true
dataChannels: 1, //创建data channel的最大数量,默认2
dataChannels: 3, //创建data channel的最大数量,默认2
useMonitor: true //是否开启monitor,会稍微影响性能,默认false
});

Expand Down
48 changes: 38 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ function PearPlayer(selector,token, opts) {
auto: opts.auto,
useMonitor: self.useMonitor
};
console.log('self.dispatcherConfig:'+self.dispatcherConfig.chunkSize);
// console.log('self.dispatcherConfig:'+self.dispatcherConfig.chunkSize);

self._start();

Expand Down Expand Up @@ -126,18 +126,21 @@ PearPlayer.prototype._getNodes = function (token, cb) {
} else {
var nodes = res.nodes;
var allNodes = [];
var isHTTP = location.protocol === 'http:' ? true : false;
for (var i=0; i<nodes.length; ++i){
var protocol = nodes[i].protocol;
var host = nodes[i].host;
var type = nodes[i].type;
var path = self.urlObj.host + self.urlObj.path;
var url = protocol+'://'+host+'/'+path;
if (!self.nodeSet.has(url)) {
allNodes.push({uri: url, type: type});
self.nodeSet.add(url);
if (isHTTP || protocol !== 'http') {
var host = nodes[i].host;
var type = nodes[i].type;
var path = self.urlObj.host + self.urlObj.path;
var url = protocol+'://'+host+'/'+path;
if (!self.nodeSet.has(url)) {
allNodes.push({uri: url, type: type});
self.nodeSet.add(url);
}
}
}

console.log('allNodes:'+JSON.stringify(allNodes));
// allNodes.push({uri: 'https://qq.webrtc.win/tv/pear001.mp4', type: 'node'}); //examples
// allNodes.push({uri: 'https://qq.webrtc.win/tv/pear001.mp4', type: 'node'}); //examples
// allNodes.push({uri: 'https://qq.webrtc.win/tv/pear001.mp4', type: 'node'}); //examples
Expand All @@ -150,8 +153,13 @@ PearPlayer.prototype._getNodes = function (token, cb) {
self.fileLength = fileLength;
console.log('nodeFilter fileLength:'+fileLength);
// self.nodes = nodes;
if (length === 1) {
if (length <= 2) {
// fallBack(nodes[0]);
nodes.push({uri: self.src, type: 'server'});
cb(nodes);
// self._fallBack(); //test
} else if (nodes.length >= 20){
nodes = nodes.slice(0, 20);
cb(nodes);
} else {
cb(nodes);
Expand All @@ -171,6 +179,7 @@ PearPlayer.prototype._getNodes = function (token, cb) {
};

PearPlayer.prototype._fallBack = function (url) {
var self = this;

if (this.isPlaying) return;
if (url) {
Expand All @@ -181,6 +190,25 @@ PearPlayer.prototype._fallBack = function (url) {
if (this.autoPlay) {
this.video.play();
}

// nodeFilter([{uri: this.src, type: 'server'}], function (nodes, fileLength) { //筛选出可用的节点,以及回调文件大小
//
// var length = nodes.length;
// console.log('nodes:'+JSON.stringify(nodes));
//
// if (length) {
// self.fileLength = fileLength;
// console.log('nodeFilter fileLength:'+fileLength);
// self._startPlaying(nodes);
// if (self.useDataChannel) {
// self._pearSignalHandshake();
// }
// } else {
// // self._fallBack();
// self.emit('exception', {errCode: 2, errMsg: 'Access video source fail'});
// }
// });

this.isPlaying = true;
};

Expand Down
14 changes: 9 additions & 5 deletions lib/dispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function Dispatcher(config) {
self.pieceLength = config.chunkSize || 1*1024*1024;
self.interval = config.interval || 10000;
self._slideInterval = config.slideInterval || 20;
self.auto = config.auto || true;
self.auto = config.auto || false;
self.useMonitor = config.useMonitor || false;
self.downloaded = 0;
self.fogDownloaded = 0; //通过data channel下载的字节数
Expand Down Expand Up @@ -207,7 +207,7 @@ Dispatcher.prototype._slide = function () {
var self = this;

if (self.done) return;
console.log('[dispatcher] slide window downloader length:'+self.downloaders.length);
// console.log('[dispatcher] slide window downloader length:'+self.downloaders.length);
self._fillWindow();
};

Expand Down Expand Up @@ -343,7 +343,7 @@ Dispatcher.prototype._fillWindow = function () {

var pair = self._calRange(index);
var node = self._getNodes(count); //prefetch
console.log('node type:'+node.type);
console.log('_getNodes node type:'+node.type);
node.select(pair[0],pair[1]);
count ++;
} else {
Expand Down Expand Up @@ -468,8 +468,9 @@ Dispatcher.prototype._setupDC = function (jd) {

jd.on('error', function () {
console.warn('jd error');
jd.close();
self.downloaders.removeObj(jd);

self._windowLength --;
self.checkoutDownloaders();

});
Expand All @@ -488,7 +489,10 @@ Dispatcher.prototype.checkoutDownloaders = function () {

Dispatcher.prototype.addDataChannel = function (dc) { //TODO:让每个新加入的节点都有至少一次下载机会

this.downloaders.push(dc);
// this.downloaders.push(dc);
this.downloaders.splice(this._windowLength,0,dc);
this._windowLength ++;
console.log('addDataChannel _windowLength:' + this._windowLength);
this._setupDC(dc);
console.log('addDataChannel now:'+this.downloaders.length);
for (var i=0;i<this.downloaders.length;++i) {
Expand Down
2 changes: 1 addition & 1 deletion lib/webrtc-downloader-bin.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ RTCDownloader.prototype._receive = function (chunk) {
// }

var headerInfo = self._getHeaderInfo(uint8);
console.log('headerInfo:'+JSON.stringify(headerInfo));
// console.log('headerInfo:'+JSON.stringify(headerInfo));
if (headerInfo) {

if (headerInfo.value){
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "pearplayer",
"version": "2.0.2",
"version": "2.0.3",
"description": "",
"main": "index.js",
"dependencies": {
Expand Down
65 changes: 48 additions & 17 deletions pear-player.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function PearPlayer(selector,token, opts) {
auto: opts.auto,
useMonitor: self.useMonitor
};
console.log('self.dispatcherConfig:'+self.dispatcherConfig.chunkSize);
// console.log('self.dispatcherConfig:'+self.dispatcherConfig.chunkSize);

self._start();

Expand Down Expand Up @@ -127,18 +127,21 @@ PearPlayer.prototype._getNodes = function (token, cb) {
} else {
var nodes = res.nodes;
var allNodes = [];
var isHTTP = location.protocol === 'http:' ? true : false;
for (var i=0; i<nodes.length; ++i){
var protocol = nodes[i].protocol;
var host = nodes[i].host;
var type = nodes[i].type;
var path = self.urlObj.host + self.urlObj.path;
var url = protocol+'://'+host+'/'+path;
if (!self.nodeSet.has(url)) {
allNodes.push({uri: url, type: type});
self.nodeSet.add(url);
if (isHTTP || protocol !== 'http') {
var host = nodes[i].host;
var type = nodes[i].type;
var path = self.urlObj.host + self.urlObj.path;
var url = protocol+'://'+host+'/'+path;
if (!self.nodeSet.has(url)) {
allNodes.push({uri: url, type: type});
self.nodeSet.add(url);
}
}
}

console.log('allNodes:'+JSON.stringify(allNodes));
// allNodes.push({uri: 'https://qq.webrtc.win/tv/pear001.mp4', type: 'node'}); //examples
// allNodes.push({uri: 'https://qq.webrtc.win/tv/pear001.mp4', type: 'node'}); //examples
// allNodes.push({uri: 'https://qq.webrtc.win/tv/pear001.mp4', type: 'node'}); //examples
Expand All @@ -151,8 +154,13 @@ PearPlayer.prototype._getNodes = function (token, cb) {
self.fileLength = fileLength;
console.log('nodeFilter fileLength:'+fileLength);
// self.nodes = nodes;
if (length === 1) {
if (length <= 2) {
// fallBack(nodes[0]);
nodes.push({uri: self.src, type: 'server'});
cb(nodes);
// self._fallBack(); //test
} else if (nodes.length >= 20){
nodes = nodes.slice(0, 20);
cb(nodes);
} else {
cb(nodes);
Expand All @@ -172,6 +180,7 @@ PearPlayer.prototype._getNodes = function (token, cb) {
};

PearPlayer.prototype._fallBack = function (url) {
var self = this;

if (this.isPlaying) return;
if (url) {
Expand All @@ -182,6 +191,25 @@ PearPlayer.prototype._fallBack = function (url) {
if (this.autoPlay) {
this.video.play();
}

// nodeFilter([{uri: this.src, type: 'server'}], function (nodes, fileLength) { //筛选出可用的节点,以及回调文件大小
//
// var length = nodes.length;
// console.log('nodes:'+JSON.stringify(nodes));
//
// if (length) {
// self.fileLength = fileLength;
// console.log('nodeFilter fileLength:'+fileLength);
// self._startPlaying(nodes);
// if (self.useDataChannel) {
// self._pearSignalHandshake();
// }
// } else {
// // self._fallBack();
// self.emit('exception', {errCode: 2, errMsg: 'Access video source fail'});
// }
// });

this.isPlaying = true;
};

Expand Down Expand Up @@ -443,7 +471,7 @@ function Dispatcher(config) {
self.pieceLength = config.chunkSize || 1*1024*1024;
self.interval = config.interval || 10000;
self._slideInterval = config.slideInterval || 20;
self.auto = config.auto || true;
self.auto = config.auto || false;
self.useMonitor = config.useMonitor || false;
self.downloaded = 0;
self.fogDownloaded = 0; //通过data channel下载的字节数
Expand Down Expand Up @@ -616,7 +644,7 @@ Dispatcher.prototype._slide = function () {
var self = this;

if (self.done) return;
console.log('[dispatcher] slide window downloader length:'+self.downloaders.length);
// console.log('[dispatcher] slide window downloader length:'+self.downloaders.length);
self._fillWindow();
};

Expand Down Expand Up @@ -752,7 +780,7 @@ Dispatcher.prototype._fillWindow = function () {

var pair = self._calRange(index);
var node = self._getNodes(count); //prefetch
console.log('node type:'+node.type);
console.log('_getNodes node type:'+node.type);
node.select(pair[0],pair[1]);
count ++;
} else {
Expand Down Expand Up @@ -877,8 +905,9 @@ Dispatcher.prototype._setupDC = function (jd) {

jd.on('error', function () {
console.warn('jd error');
jd.close();
self.downloaders.removeObj(jd);

self._windowLength --;
self.checkoutDownloaders();

});
Expand All @@ -897,11 +926,13 @@ Dispatcher.prototype.checkoutDownloaders = function () {

Dispatcher.prototype.addDataChannel = function (dc) { //TODO:让每个新加入的节点都有至少一次下载机会

this.downloaders.push(dc);
// this.downloaders.push(dc);
this.downloaders.splice(this._windowLength,0,dc);
this._windowLength ++;
console.log('addDataChannel _windowLength:' + this._windowLength);
this._setupDC(dc);
console.log('addDataChannel now:'+this.downloaders.length);
for (var i=0;i<this.downloaders.length;++i) {
console.log('666');
console.log('downloader type:'+this.downloaders[i].type);
}
};
Expand Down Expand Up @@ -1939,7 +1970,7 @@ RTCDownloader.prototype._receive = function (chunk) {
// }

var headerInfo = self._getHeaderInfo(uint8);
console.log('headerInfo:'+JSON.stringify(headerInfo));
// console.log('headerInfo:'+JSON.stringify(headerInfo));
if (headerInfo) {

if (headerInfo.value){
Expand Down

0 comments on commit 5b041a5

Please sign in to comment.