some rewriting

Esse commit está contido em:
Simon Kusterer
2015-01-24 20:55:38 +01:00
commit d658a66f15
8 arquivos alterados com 121 adições e 79 exclusões
+27 -4
Ver Arquivo
@@ -21,6 +21,8 @@ var castnow = function(opts) {
var uniqueId = utils.uniqueId(1);
var options = xtend(defaults, opts || {});
var app = express();
var flatteners = {};
var itemCreators = {};
var cn;
var flatten = function(input, cb) {
@@ -37,10 +39,9 @@ var castnow = function(opts) {
var resolver = function(input, cb) {
if (!cb) cb = noop;
var item = itemBuilder(uniqueId(), input);
cn.fire('resolve', { item: item }, function(err, ev) {
cn.fire('resolve', input, function(err, ev) {
if (err) return cb(err);
cb(null, ev.item);
cb(null, ev.item || null);
});
};
@@ -81,7 +82,7 @@ var castnow = function(opts) {
async.mapSeries(list, resolver, function(err, items) {
if (err) return cb(err);
items = items.filter(function(item) {
return !item.isDisabled();
return !!item;
});
cb(null, items);
});
@@ -92,6 +93,28 @@ var castnow = function(opts) {
return options;
},
createBlankItem: function() {
return itemBuilder(uniqueId());
},
createItem: function(name, options, cb) {
if (!itemCreators[name]) return cb(new Error('creator not found'));
itemCreators[name](options, cb);
},
flatten: function(name, options, cb) {
if (!flatteners[name]) return cb(new Error('flattener not found'));
flatteners[name](options, cb);
},
addItemCreator: function(name, fn) {
itemCreators[name] = fn;
},
addFlattener: function(name, fn) {
flattener[name] = fn;
},
// register a plugin
use: function(fn) {
fn(this);
-1
Ver Arquivo
@@ -103,7 +103,6 @@ var engine = function(opts) {
if (loading) {
return cb(new Error('load already in progress'));
}
loading = true;
async.waterfall([
+22 -19
Ver Arquivo
@@ -1,11 +1,10 @@
var player = require('chromecast-player');
var itemBuilder = function(id, source) {
var itemBuilder = function(id) {
var data = {};
var type = 'default'; // e.g. 'youtube'
var api = player.api;
var appId = api.APP_ID;
var args = {};
var disabled = true;
return {
@@ -13,8 +12,7 @@ var itemBuilder = function(id, source) {
return id;
},
setApi: function(t, a) {
type = t;
setApi: function(a) {
api = a;
},
@@ -22,8 +20,12 @@ var itemBuilder = function(id, source) {
return api;
},
getType: function() {
return type;
setAppId: function(id) {
appId = id;
},
getAppId: function() {
return appId;
},
setArgs: function(a) {
@@ -34,28 +36,29 @@ var itemBuilder = function(id, source) {
return args;
},
setSource: function(src) {
source = src;
},
getSource: function() {
return source;
},
enable: function() {
disabled = false;
},
disable: function() {
disabled = true;
},
isDisabled: function() {
return disabled;
},
set: function(key, val) {
data[key] = val;
},
get: function(key) {
return data[key];
},
load: function(controls, cb) {
controls.load(this.getArgs(), cb);
},
unload: function(controls, cb) {
if (controls.destroy) controls.destroy();
cb();
}
}
-1
Ver Arquivo
@@ -72,7 +72,6 @@ var guessIp = function(compare) {
}).address;
};
module.exports = {
unformatTime: unformatTime,
uniqueId: uniqueId,
+1 -1
Ver Arquivo
@@ -7,7 +7,7 @@
"castnow": "./bin.js"
},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "node test.js"
},
"author": "Simon Kusterer",
"license": "MIT",
+27 -8
Ver Arquivo
@@ -22,16 +22,27 @@ var localfile = function(castnow) {
serveMp4(req, res, item.getSource());
});
castnow.hook('resolve', function(ev, next, stop) {
var item = ev.item;
if (!isFile(item.getSource())) return next();
var url = 'http://' + ip + ':' + options.port + '/localfile/' + item.getId();
castnow.addItemCreator('localfile', function(o, cb) {
var opts = {};
var item;
var url;
debug('localfile detected: %s url: %s', item.getSource(), url);
if (typeof o === 'string') {
opts.source = o;
} else {
opts = o;
}
if (!isFile(opts.source)) return cb(new Error('not a valid file'));
item = castnow.createBlankItem();
item.setSource(opts.source);
url = 'http://' + ip + ':' + options.port + '/localfile/' + item.getId();
item.setArgs({
autoplay: true,
currentTime: 0,
currentTime: opts.start || 0,
media: {
contentId: url,
contentType: 'video/mp4',
@@ -41,9 +52,17 @@ var localfile = function(castnow) {
}
}
});
});
item.enable();
stop();
castnow.hook('resolve', function(ev, next, stop) {
var input = ev.input;
if (!isFile(input.source)) return next();
debug('localfile detected: %s', source);
castnow.createItem('localfile', input, function(err, item) {
if (err) return stop(err);
ev.item = item;
return stop();
});
}, 600);
};
+30 -9
Ver Arquivo
@@ -2,26 +2,47 @@ var isUrl = require('is-url');
var debug = require('debug')('castnow:url');
var url = function(castnow) {
var playlist = castnow.getPlaylist();
castnow.hook('resolve', function(ev, done, stop) {
var item = ev.item;
if (!isUrl(item.getSource())) return done();
castnow.addItemCreator('url', function(o, cb) {
var opts = {};
var item;
var url;
debug('url detected', item.getSource());
if (typeof o === 'string') {
opts.source = o;
} else {
opts = o;
}
if (!isUrl(opts.source)) return cb(new Error('not a valid url'));
item = castnow.createBlankItem();
item.setSource(opts.source);
item.setArgs({
autoplay: true,
currentTime: 0,
currentTime: opts.start || 0,
media: {
contentId: item.getSource(),
contentId: opts.source,
contentType: 'video/mp4',
streamType: 'BUFFERED'
}
});
item.enable();
stop();
return cb(null, item);
});
castnow.hook('resolve', function(ev, done, stop) {
var input = ev.input;
if (!isUrl(item.source)) return done();
debug('url detected', item.source);
castnow.createItem('url', input, function(err, item) {
if (err) return stop(err);
ev.item = item;
return stop();
});
}, 500);
};
+14 -36
Ver Arquivo
@@ -2,43 +2,15 @@ var test = require('tape');
var castnow = require('./castnow')();
var ENGINE_STATES = require('./lib/engine').STATES;
var scanner = require('chromecast-scanner');
var Api = require('chromecast-player').api;
var urlPlugin = require('./plugins/url');
var async = require('async');
var demo = 'http://commondatastorage.googleapis.com/gtv-videos-bucket/ED_1280.mp4';
castnow.use(urlPlugin);
test('castnow engine', function(t) {
var engine = castnow.getEngine();
var item = {
getAppId: function() {
return Api.APP_ID;
},
getApi: function() {
return Api;
},
load: function(controls, cb) {
controls.load({
autoplay: true,
currentTime: 0,
media: {
contentId: 'http://commondatastorage.googleapis.com/gtv-videos-bucket/ED_1280.mp4',
contentType: 'video/mp4',
streamType: 'BUFFERED'
}
}, cb);
},
unload: function(controls, cb) {
if (controls.destroy) controls.destroy();
t.pass('item unloaded');
cb();
}
};
async.waterfall([
function(next) {
scanner(function(err, service) {
@@ -56,14 +28,20 @@ test('castnow engine', function(t) {
},
function(next) {
engine.load(item, function(err, controls, item) {
t.equal(err, null, 'there should not be any load error');
t.equal(engine.getState(), ENGINE_STATES.launched, 'engine should be in launch state')
next();
castnow.createItem('url', demo, function(err, item) {
next(null, item);
});
},
function(next) {
function(item, next) {
engine.load(item, function(err) {
t.equal(err, null, 'there should not be any load error');
t.equal(engine.getState(), ENGINE_STATES.launched, 'engine should be in launch state')
next(null, item);
});
},
function(item, next) {
setTimeout(function() {
engine.load(item, function() {
next();