Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
send desire to swarm 2 times instead of only 1 (refs #19)
Browse files Browse the repository at this point in the history
  • Loading branch information
flavioribeiro committed Feb 21, 2014
1 parent 74bfc78 commit 1988b8c
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 16 deletions.
28 changes: 20 additions & 8 deletions html/js/bemtv.bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var utils = _dereq_('./Utils.js');
BEMTV_ROOM_DISCOVER_URL = "http://server.bem.tv:9000/room"
BEMTV_SERVER = "http://server.bem.tv:8080"
ICE_SERVERS = freeice();
DESIRE_TIMEOUT = 1 // in seconds
DESIRE_TIMEOUT = 0.7 // in seconds
REQ_TIMEOUT = 4 // in seconds
MAX_CACHE_SIZE = 4;

Expand Down Expand Up @@ -42,6 +42,7 @@ BemTV.prototype = {
this.bufferedChannel = undefined;
this.requestTimeout = undefined;
this.currentState = PEER_IDLE;
this.totalDesireSent = 0;
},

setupPeerConnection: function(room) {
Expand Down Expand Up @@ -79,16 +80,18 @@ BemTV.prototype = {
console.log("RECEIVED DESACK, SENDING REQ " + id + ":" + resource);
clearTimeout(self.requestTimeout);
self.currentState = PEER_DOWNLOADING;
self.totalDesireSent = 0;
var reqMessage = utils.createMessage(CHUNK_REQ, resource);
self.swarm[id].send(reqMessage);
this.requestTimeout = setTimeout(function() { self.getFromCDN(resource); }, REQ_TIMEOUT * 1000);
this.requestTimeout = setTimeout(function() { self.getFromCDN(resource); }, REQ_TIMEOUT *1000);

} else if (self.isReq(parsedData)) { // covering only the happy path. What happens if chunk is poped from the cache on this negotiation step?
} else if (self.isReq(parsedData)) { // what happens if the chunk is removed from cache on this step?
console.log("RECEIVED REQ, SENDING CHUNK " + id + ":" + resource);
var offerMessage = utils.createMessage(CHUNK_OFFER, resource, self.chunksCache[resource]);
self.swarm[id].send(offerMessage);
utils.incrementCounter("chunksToP2P");
self.currentState = PEER_IDLE;
self.totalDesireSent = 0;

} else if (self.isOffer(parsedData) && resource == self.currentUrl) {
console.log("RECEIVED OFFER, GETTING CHUNK " + id + ":" + resource);
Expand Down Expand Up @@ -143,10 +146,19 @@ BemTV.prototype = {
},

getFromP2P: function(url) {
this.currentState = PEER_DESIRING;
var desMessage = utils.createMessage(CHUNK_DESIRE, url);
this.broadcast(desMessage);
this.requestTimeout = setTimeout(function() { self.getFromCDN(url); }, DESIRE_TIMEOUT * 1000);
if (this.totalDesireSent < 2) {
console.log("SENDING DESIRE FOR " + url + " attempt: " + this.totalDesireSent);
this.currentState = PEER_DESIRING;
this.totalDesireSent += 1;
var desMessage = utils.createMessage(CHUNK_DESIRE, url);
this.broadcast(desMessage);
this.requestTimeout = setTimeout(function() { self.getFromP2P(url); }, DESIRE_TIMEOUT * Math.random() * 2000);
} else {
this.totalDesireSent = 0;
console.log("Giving up, let's get from CDN");
self.currentState = PEER_DOWNLOADING;
self.getFromCDN(url);
}
},

getFromCDN: function(url) {
Expand All @@ -172,7 +184,7 @@ BemTV.prototype = {
},

sendToPlayer: function(data) {
var bemtvPlayer = document.getElementsByTagName("embed")[0] || document.getElementById("BemTVplayer");;
var bemtvPlayer = document.getElementsByTagName("embed")[0] || document.getElementById("BemTVplayer");
self.chunksCache[self.currentUrl] = data;
self.currentUrl = undefined;
bemtvPlayer.resourceLoaded(data);
Expand Down
Binary file modified html/static/HLSPlayer.swf
Binary file not shown.
1 change: 1 addition & 0 deletions html/vkthread
Submodule vkthread added at fb4d51
28 changes: 20 additions & 8 deletions src/BemTV.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ var utils = require('./Utils.js');
BEMTV_ROOM_DISCOVER_URL = "http://server.bem.tv:9000/room"
BEMTV_SERVER = "http://server.bem.tv:8080"
ICE_SERVERS = freeice();
DESIRE_TIMEOUT = 1 // in seconds
DESIRE_TIMEOUT = 0.7 // in seconds
REQ_TIMEOUT = 4 // in seconds
MAX_CACHE_SIZE = 4;

Expand Down Expand Up @@ -41,6 +41,7 @@ BemTV.prototype = {
this.bufferedChannel = undefined;
this.requestTimeout = undefined;
this.currentState = PEER_IDLE;
this.totalDesireSent = 0;
},

setupPeerConnection: function(room) {
Expand Down Expand Up @@ -78,16 +79,18 @@ BemTV.prototype = {
console.log("RECEIVED DESACK, SENDING REQ " + id + ":" + resource);
clearTimeout(self.requestTimeout);
self.currentState = PEER_DOWNLOADING;
self.totalDesireSent = 0;
var reqMessage = utils.createMessage(CHUNK_REQ, resource);
self.swarm[id].send(reqMessage);
this.requestTimeout = setTimeout(function() { self.getFromCDN(resource); }, REQ_TIMEOUT * 1000);
this.requestTimeout = setTimeout(function() { self.getFromCDN(resource); }, REQ_TIMEOUT *1000);

} else if (self.isReq(parsedData)) { // covering only the happy path. What happens if chunk is poped from the cache on this negotiation step?
} else if (self.isReq(parsedData)) { // what happens if the chunk is removed from cache on this step?
console.log("RECEIVED REQ, SENDING CHUNK " + id + ":" + resource);
var offerMessage = utils.createMessage(CHUNK_OFFER, resource, self.chunksCache[resource]);
self.swarm[id].send(offerMessage);
utils.incrementCounter("chunksToP2P");
self.currentState = PEER_IDLE;
self.totalDesireSent = 0;

} else if (self.isOffer(parsedData) && resource == self.currentUrl) {
console.log("RECEIVED OFFER, GETTING CHUNK " + id + ":" + resource);
Expand Down Expand Up @@ -142,10 +145,19 @@ BemTV.prototype = {
},

getFromP2P: function(url) {
this.currentState = PEER_DESIRING;
var desMessage = utils.createMessage(CHUNK_DESIRE, url);
this.broadcast(desMessage);
this.requestTimeout = setTimeout(function() { self.getFromCDN(url); }, DESIRE_TIMEOUT * 1000);
if (this.totalDesireSent < 2) {
console.log("SENDING DESIRE FOR " + url + " attempt: " + this.totalDesireSent);
this.currentState = PEER_DESIRING;
this.totalDesireSent += 1;
var desMessage = utils.createMessage(CHUNK_DESIRE, url);
this.broadcast(desMessage);
this.requestTimeout = setTimeout(function() { self.getFromP2P(url); }, DESIRE_TIMEOUT * Math.random() * 2000);
} else {
this.totalDesireSent = 0;
console.log("Giving up, let's get from CDN");
self.currentState = PEER_DOWNLOADING;
self.getFromCDN(url);
}
},

getFromCDN: function(url) {
Expand All @@ -171,7 +183,7 @@ BemTV.prototype = {
},

sendToPlayer: function(data) {
var bemtvPlayer = document.getElementsByTagName("embed")[0] || document.getElementById("BemTVplayer");;
var bemtvPlayer = document.getElementsByTagName("embed")[0] || document.getElementById("BemTVplayer");
self.chunksCache[self.currentUrl] = data;
self.currentUrl = undefined;
bemtvPlayer.resourceLoaded(data);
Expand Down

0 comments on commit 1988b8c

Please sign in to comment.