Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a039873
commit 328cb3c
Showing
47 changed files
with
9,303 additions
and
1,319 deletions.
There are no files selected for viewing
Empty file.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,163 +1,252 @@ | ||
var TVSTATUS = {} | ||
|
||
TVSTATUS.INIT = 0; | ||
TVSTATUS.SET = 1; | ||
TVSTATUS.PLAY = 2; | ||
TVSTATUS.PAUSE = 3; | ||
TVSTATUS.STOP = 4; | ||
TVSTATUS.TERMINATE = 5; | ||
|
||
var TVStat = function(){ | ||
this.status = TVSTATUS.INIT; | ||
this.mediatype = null; | ||
this.mediaurl = null; | ||
this.volume = -1; | ||
} | ||
|
||
TVStat.prototype.init = function(){ | ||
this.status = TVSTATUS.INIT; | ||
} | ||
TVStat.prototype.set = function(url, mediatype){ | ||
this.status = TVSTATUS.SET; | ||
this.mediaurl = url; | ||
this.mediatype = mediatype; | ||
} | ||
TVStat.prototype.play = function(){ | ||
this.status = TVSTATUS.PLAY; | ||
} | ||
TVStat.prototype.pause = function(){ | ||
this.status = TVSTATUS.PAUSE; | ||
} | ||
TVStat.prototype.stop = function(){ | ||
this.status = TVSTATUS.STOP; | ||
} | ||
TVStat.prototype.terminate = function(){ | ||
this.status = TVSTATUS.TERMINATE; | ||
this.meditatype = null; | ||
this.meditaurl = null; | ||
} | ||
|
||
TVStat.prototype.setVolume = function(level){ | ||
this.volume = level; | ||
} | ||
TVStat.prototype.stopped = function(){ | ||
return (this.status === TVSTATUS.STOP || this.status === TVSTATUS.TERMINATE); | ||
} | ||
|
||
var tvstat = new TVStat(); | ||
|
||
|
||
/** | ||
* UPnP | ||
* internal webserver | ||
*/ | ||
var UPnPDevices = []; | ||
|
||
Discovery.start(function(data){ | ||
console.dir(data); | ||
UPnPDevices.push(data); | ||
}); | ||
setTimeout(function(){ | ||
console.log("start discovery...") | ||
Discovery.start(function(data){ | ||
console.dir(data); | ||
UPnPDevices.push(data); | ||
}); | ||
}, 5000) | ||
|
||
var Controller = {"url": ""} | ||
, Proxy = {"url": "", "videourl": "", "videohost": "", "videopath": ""}; | ||
|
||
(function(){ | ||
var self = Controller; | ||
self.server = new Server(); | ||
|
||
|
||
self.server.get('/', function(req, res){ | ||
res.render("this is controller"); | ||
}); | ||
|
||
/** | ||
* internal webserver | ||
*/ | ||
self.server.get('/upnpdevices', function(req, res){ | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.send(JSON.stringify(UPnPDevices)) | ||
}) | ||
|
||
self.server.get('/set', function(req, res){ | ||
Proxy.videourl = req.params.url; | ||
|
||
// [TODO] Now assuming that protocol is 'http://', so if protocol schema | ||
// is other than it (ex, https://), it doesn't work. Ofcource, if this support | ||
// https, proxy feature has to support https features ( that's so hard thing ) | ||
var arr = Proxy.videourl.slice(7).split("/") | ||
Proxy.videohost = arr[0], Proxy.videopath = "/"+arr.slice(1).join("/") | ||
|
||
var ret = {} | ||
for(var key in Proxy) if(Proxy.hasOwnProperty(key)) { | ||
ret[key] = Proxy[key]; | ||
} | ||
|
||
var server = new Server(); | ||
|
||
|
||
var REQ = [ | ||
'GET {%path%} HTTP/1.1', | ||
'Host: {%host%}', | ||
'User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5', | ||
'Accept: */*', | ||
'','' | ||
] | ||
REQ = REQ.join("\r\n"); | ||
|
||
var youtubeurl = "", | ||
ytHost = "", | ||
ytPath = "" | ||
|
||
server.get('/', function(req, res){ | ||
res.render("hoge"); | ||
}); | ||
|
||
server.get('/upnpdevices', function(req, res){ | ||
res.setHeader('Content-Type', 'application/json'); | ||
res.send(JSON.stringify(UPnPDevices)) | ||
}) | ||
|
||
server.get('/set', function(req, res){ | ||
youtubeurl = req.params.url; | ||
var arr = youtubeurl.slice(7).split("/") | ||
ytHost = arr[0], ytPath = "/"+arr[1] | ||
|
||
res.render("set complete"); | ||
}); | ||
|
||
server.get('/start', function(req, res){ | ||
var avcontrol_url = req.params.avcontrol_url; | ||
var rendering_url = req.params.rendering_url; | ||
tvController.start(proxyurl+"/video.mp4", avcontrol_url, rendering_url) | ||
|
||
res.render("start") | ||
}) | ||
server.get('/play', function(req, res){ | ||
tvController.play(function(){}); | ||
res.render("play") | ||
}) | ||
server.get('/stop', function(req, res){ | ||
tvController.stop(function(){}); | ||
res.render("stop") | ||
}) | ||
server.get('/pause', function(req, res){ | ||
tvController.pause(function(){}); | ||
res.render("pause") | ||
}) | ||
server.get('/setVolume', function(req, res){ | ||
var level = req.params.level; | ||
tvController.setVolume(level, function(e){ | ||
res.setHeader('Content-Type', 'text/plain'); | ||
res.send(e.querySelector("CurrentVolume").textContent) | ||
res.setHeader("content-type", "application/json") | ||
res.send(JSON.stringify(ret)); | ||
}); | ||
}) | ||
server.get('/getVolume', function(req, res){ | ||
tvController.getVolume(function(e){ | ||
res.setHeader('Content-Type', 'text/plain'); | ||
res.send(e.querySelector("CurrentVolume").textContent) | ||
|
||
self.server.get('/start', function(req, res){ | ||
var avcontrol_url = req.params.avcontrol_url; | ||
var rendering_url = req.params.rendering_url; | ||
tvController.start(Proxy.url+"/video.mp4", avcontrol_url, rendering_url) | ||
|
||
tvstat.set(Proxy.videourl, "video/mp4"); | ||
|
||
var ret = { | ||
"avcontrol_url": avcontrol_url, | ||
"rendering_url": rendering_url, | ||
"proxy_url": Proxy.url | ||
} | ||
|
||
res.setHeader("content-type", "application/json") | ||
res.send(JSON.stringify(ret)); | ||
}) | ||
|
||
self.server.get('/play', function(req, res){ | ||
tvController.play(function(){}); | ||
tvstat.play(); | ||
res.render("play") | ||
}) | ||
|
||
self.server.get('/stop', function(req, res){ | ||
tvController.stop(function(){}); | ||
tvstat.stop(); | ||
res.render("stop") | ||
}) | ||
|
||
self.server.get('/pause', function(req, res){ | ||
tvController.pause(function(){}); | ||
tvstat.pause(); | ||
res.render("pause") | ||
}) | ||
|
||
self.server.get('/setVolume', function(req, res){ | ||
var level = req.params.level; | ||
tvController.setVolume(level, function(e){ | ||
res.setHeader('Content-Type', 'text/plain'); | ||
var level = e.querySelector("CurrentVolume").textContent | ||
tvstat.setVolume(parseInt(level)); | ||
res.send(level) | ||
}); | ||
}) | ||
|
||
self.server.get('/getVolume', function(req, res){ | ||
tvController.getVolume(function(e){ | ||
var level = e.querySelector("CurrentVolume").textContent | ||
res.setHeader('Content-Type', 'text/plain'); | ||
tvstat.setVolume(parseInt(level)); | ||
res.send(level) | ||
}); | ||
}) | ||
|
||
self.server.listen(0, function(err){ | ||
chrome.socket.getNetworkList(function(list){ | ||
var address = ""; | ||
console.log("=== detected network interfaces ===") | ||
console.dir(list); | ||
|
||
self.url = "http://localhost:"+self.server.port; | ||
list.forEach(function(if_){ | ||
if(if_.address.match(/^\d+\.\d+\.\d+\.\d+$/)) address = if_.address; | ||
}) | ||
if(!!address) self.url = self.url.replace('localhost', address); | ||
console.log('listening control url : '+self.url); | ||
}); | ||
}); | ||
}) | ||
}()); | ||
|
||
server.get('/video.mp4', function(req, res){ | ||
if(!!youtubeurl === false) { | ||
res.render("youtube url doesn't set"); | ||
return; | ||
} | ||
|
||
chrome.socket.create('tcp', {}, function(createInfo) { | ||
var sid = createInfo.socketId; | ||
chrome.socket.connect(sid, ytHost, 80, function(e) { | ||
var request = REQ.replace("{%path%}", ytPath).replace("{%host%}", ytHost); | ||
chrome.socket.write(sid, encodeToBuffer(request), function(e){ | ||
chrome.socket.read(sid, 65535, function(readInfo) { | ||
console.log("=== [PROXY] ===\n"+decodeFromBuffer(readInfo.data)); | ||
res.sendraw(readInfo.data); | ||
|
||
var read_ = function() { | ||
chrome.socket.read(sid, 65535, function(readInfo) { | ||
if(readInfo.resultCode > 0) { | ||
// console.dir("read", readInfo.resultCode); | ||
res.sendraw(readInfo.data); | ||
read_(); | ||
} else { | ||
/** | ||
* Proxy implementations | ||
* | ||
*/ | ||
(function(){ | ||
var self = Proxy; | ||
|
||
var REQ = [ | ||
'{%method%} {%path%} HTTP/1.1', | ||
'Host: {%host%}', | ||
'User-Agent: curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5', | ||
'Accept: */*', | ||
'','' | ||
], LFCR = "\r\n" | ||
REQ = REQ.join(LFCR); | ||
|
||
var proxy_ = function(method, res){ | ||
chrome.socket.create('tcp', {}, function(createInfo) { | ||
var sid = createInfo.socketId; | ||
chrome.socket.connect(sid, self.videohost, 80, function(e) { | ||
var request = REQ | ||
.replace("{%method%}", method) | ||
.replace("{%path%}", self.videopath) | ||
.replace("{%host%}", self.videohost); | ||
|
||
chrome.socket.write(sid, encodeToBuffer(request), function(e){ | ||
console.log("=== [PROXY] sent resquest header ==="); | ||
console.log(request) | ||
console.log("sendHeader completed", e); | ||
|
||
chrome.socket.read(sid, 65535, function(readInfo) { | ||
console.log("=== [PROXY] received response header ==="); | ||
console.log("method : "+method); | ||
console.log(decodeFromBuffer(readInfo.data)); | ||
|
||
res.sendraw(readInfo.data); | ||
|
||
var read_ = function() { | ||
if(tvstat.stopped()){ | ||
res.close(); | ||
chrome.socket.destroy(sid); | ||
} | ||
}); | ||
} | ||
read_(); | ||
return; | ||
} | ||
chrome.socket.read(sid, 65535, function(readInfo) { | ||
if(readInfo.resultCode > 0) { | ||
res.sendraw(readInfo.data); | ||
read_(); | ||
} else { | ||
chrome.socket.destroy(sid); | ||
} | ||
}); | ||
} | ||
read_(); | ||
}); | ||
}); | ||
console.log("write complete", e); | ||
}); | ||
}); | ||
} | ||
self.server = new Server(); | ||
|
||
self.server.get('/video.mp4', function(req, res){ | ||
if(!!self.videourl === false) { | ||
res.render("video url doesn't set"); | ||
return; | ||
} | ||
proxy_('GET', res) | ||
}); | ||
|
||
}); | ||
|
||
server.head('/video.mp4', function(req, res){ | ||
if(!!youtubeurl === false) { | ||
res.render("youtube url doesn't set"); | ||
return; | ||
} | ||
self.server.head('/video.mp4', function(req, res){ | ||
if(!!self.videourl === false) { | ||
res.render("video url doesn't set"); | ||
return; | ||
} | ||
proxy_('HEAD', res) | ||
}); | ||
|
||
console.log("=== [PROXY]receive HEAD method ==="); | ||
chrome.socket.create('tcp', {}, function(createInfo) { | ||
var sid = createInfo.socketId; | ||
chrome.socket.connect(sid, ytHost, 80, function(e) { | ||
var request = REQ.replace("{%path%}", ytPath).replace("{%host%}", ytHost).replace("GET", "HEAD") | ||
chrome.socket.write(sid, encodeToBuffer(request), function(e){ | ||
chrome.socket.read(sid, 65535, function(readInfo) { | ||
res.sendraw(readInfo.data); | ||
chrome.socket.destroy(sid); | ||
}); | ||
}); | ||
self.server.listen(0, function(err){ | ||
chrome.socket.getNetworkList(function(list){ | ||
var address = ""; | ||
console.log("=== detected network interfaces ===") | ||
console.dir(list); | ||
|
||
self.url = "http://localhost:"+self.server.port; | ||
list.forEach(function(if_){ | ||
if(if_.address.match(/^\d+\.\d+\.\d+\.\d+$/)) address = if_.address; | ||
}) | ||
if(!!address) self.url = self.url.replace('localhost', address); | ||
console.log('listening proxy url : '+self.url); | ||
}); | ||
}); | ||
}()) | ||
|
||
}); | ||
|
||
server.listen(0, function(err){ | ||
|
||
chrome.socket.getNetworkList(function(list){ | ||
console.dir(list); | ||
proxyurl = "http://localhost:"+server.port; | ||
list.forEach(function(if_){ | ||
if(if_.address.match(/^\d+\.\d+\.\d+\.\d+$/)) address = if_.address; | ||
console.log(address); | ||
}) | ||
if(!!address) proxyurl = proxyurl.replace('localhost', address); | ||
console.log('listening '+proxyurl); | ||
}); | ||
}); |
Oops, something went wrong.