var browser = require('airplay').createBrowser();
var charm = require('charm');
var keypress = require('keypress');
var timespan = require('timespan');
keypress(process.stdin);
browser.on('deviceOnline', function(device) {
browser.stop();
playOnDevice(device);
});
browser.start();
function formatTime(seconds) {
function pad(n, z) {
z = z || '0';
n = n + '';
return n.length >= 2 ? n : new Array(2 - n.length + 1).join(z) + n;
}
var t = timespan.fromSeconds(seconds);
if (!t) return "";
var timeString = pad(t.minutes) + ':' + pad(t.seconds);
if (t.hours) {
timeString = t.hours + ':' + timeString;
}
return timeString;
}
function ProgressBar(device) {
this.device = device;
this.total = 100;
this.position = 0;
this.charm = charm();
this.charm.pipe(process.stdout);
process.stdin.on('keypress', function(ch, key) {
this.charm.delete('char', 1);
if (key && key.ctrl && key.name == 'c') {
console.log('\n');
process.exit(0);
}
var digit = parseInt(ch);
if (ch && (ch.length == 1) && digit > 0 && digit < 10) {
this.device.scrub(this.total / 10 * digit);
} else if (key.name == 'space') {
console.log(this.device);
if (this.device.rateValue !== 0) {
this.device.rate(0);
this.device.rateValue = 0;
} else {
this.device.rate(1);
this.device.rateValue = 1;
}
} else if (key.name == 'right') {
this.device.rateValue = Math.min(10, this.device.rateValue + 1);
this.device.rate(this.device.rateValue);
} else if (key.name == 'left') {
// todo: reverse
this.device.rateValue = Math.max(-10, this.device.rateValue - 1);
this.device.rate(this.device.rateValue);
}
this.draw();
}.bind(this));
process.stdin.setRawMode(true);
process.stdin.resume();
this.charm.write('\n\n\n');
}
ProgressBar.prototype.draw = function() {
this.charm.background('black');
this.charm.left(999);
this.charm.erase('line');
this.charm.write('Contols: 1-9 seek, t go to time, space pause');
this.charm.left(999);
this.charm.down(1);
this.charm.erase('line');
var current = formatTime(this.position);
this.charm.background('blue');
function getSymbol(pos, self) {
if (pos < Math.round(self.position / self.total * 100)) {
self.charm.background('blue');
self.charm.foreground('white');
} else {
self.charm.background('white');
self.charm.foreground('black');
}
if (!current || current.length <= 0) {
return " ";
}
var before = Math.ceil(current.length / 2);
var after = Math.floor(current.length / 2);
if ((pos > (50 - before)) && (pos < (50 + after))) {
var index = pos - (50 - before);
return current[pos - (50 - before)];
}
return " ";
}
for (var i = 0; i < 100; ++i)
this.charm.write(getSymbol(i, this));
this.charm.background('black');
this.charm.right(1);
this.charm.background('black');
this.charm.foreground('white');
if (this.total) {
this.charm.write(formatTime(this.total));
}
this.charm.down(1);
this.charm.left(999);
this.charm.erase('line');
this.charm.write('State: ');
this.charm.write('Playing');
this.charm.up(1);
this.charm.up(1);
this.charm.left(999);
};
function playOnDevice(device) {
var content = 'https://r15---sn-nwj7knlz.googlevideo.com/videoplayback?dur=581.520&sver=3&key=yt5&ms=au&cwbhb=yes&initcwndbps=1637500&mv=m&sparams=cwbhb%2Cdur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cratebypass%2Crequiressl%2Csource%2Cupn%2Cexpire&source=youtube&itag=22&upn=ywYBgMgOEx0&mm=31&signature=13A203BEAE899E7FF6A154B5F13A3C2ED7D86832.9660E1B0427375F5B088336D620340B7922C8F30&mt=1419669460&expire=1419691116&mime=video%2Fmp4&id=o-AAeH9gKlDHd03haEYJYmcDUI230ojFrdYsXwT9J_7TCk&fexp=900718%2C904732%2C904844%2C914951%2C924621%2C927622%2C932404%2C936117%2C941004%2C943607%2C943917%2C947209%2C947218%2C948124%2C952302%2C952605%2C952808%2C952901%2C955301%2C957103%2C957105%2C957201&ipbits=0&ratebypass=yes&requiressl=yes&ip=50.152.158.217';
//var content = 'http://cs518604v4.vk.me/u148196065/videos/2aede8152d.720.mp4?extra=eMUsc6gJD6MpId6zDLN8QCzL2uxck_scnoD-9-aQYMf58nqgaO5vdrBtN4pAZRdBOYuOegNKyBxqIxHmtEglzzPdnGFXEK1u0Q';
var startPosition = 0;
console.log(device);
device.play(content, startPosition, function(res) {
console.log(content);
if (res) {
} else {
console.error('failed to play file on device');
}
var bar = new ProgressBar(device);
setInterval(function() {
device.status(function(res) {
/*
{ duration: 8453.597,
position: 61.601224606,
rate: 1,
playbackBufferEmpty: true,
playbackBufferFull: false,
playbackLikelyToKeepUp: true,
readyToPlay: true,
loadedTimeRanges: [ { duration: 1242.302263006, start: 0.041736994 } ],
seekableTimeRanges: [ { duration: 8453.597, start: 0 } ] }
*/
device.lastStatus = res;
device.rateValue = res.rate;
bar.total = res.duration;
bar.position = res.position;
bar.draw();
});
}, 300);
});
}