Skip to content

Instantly share code, notes, and snippets.

@xat
Created October 12, 2014 19:07
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xat/9ccf5eab2d0adb34fe8d to your computer and use it in GitHub Desktop.
Save xat/9ccf5eab2d0adb34fe8d to your computer and use it in GitHub Desktop.
start playback of a youtube clip on chromecast using castv2
var castv2Cli = require('castv2-client');
var inherits = require('util').inherits;
var Application = castv2Cli.Application;
var RequestResponseController = castv2Cli.RequestResponseController;
var extend = require('xtend');
var noop = function() {};
var slice = Array.prototype.slice;
var Api = function(client, session) {
var that = this;
Application.apply(this, arguments);
this.reqres = this.createController(RequestResponseController,
'urn:x-cast:com.google.cast.media');
this.yt = this.createController(RequestResponseController,
'urn:x-cast:com.google.youtube.mdx');
var onMessage = function(response, broadcast) {
if(response.type === 'MEDIA_STATUS' && broadcast) {
var status = response.status[0];
that.currentSession = status;
that.emit(status.playerState.toLowerCase(), status);
that.emit('status', status);
}
};
var onClose = function() {
that.removeListener('message', onMessage);
that.stop();
};
this.reqres.on('message', onMessage);
this.reqres.once('close', onClose);
};
Api.APP_ID = '233637DE';
inherits(Api, Application);
Api.prototype.getStatus = function(cb) {
var that = this;
this.reqres.request({ type: 'GET_STATUS' },
function(err, response) {
if(err) return callback(err);
var status = response.status[0];
that.currentSession = status;
cb(null, status);
}
);
};
Api.prototype.load = function(videoId, cb) {
var opts = {
type: 'flingVideo',
data: {
currentTime: 0,
videoId: videoId
}
};
this.yt.request(opts);
// TODO: find out why the request callback
// never gets fired... for now just call cb directly..
cb();
};
Api.prototype.getCurrentSession = function(cb) {
if (this.currentSession) return cb(null, this.currentSession);
this.getStatus(function(err, status) {
if (err) return cb(err);
cb(null, status);
});
};
Api.prototype.sessionRequest = function(data, cb) {
var that = this;
cb = cb || noop;
this.getCurrentSession(function(err, session) {
if (err) return cb(err);
var sessionId = session.mediaSessionId;
that.reqres.request(extend(data, { mediaSessionId: sessionId } ),
function(err, response) {
if(err) return cb(err);
cb(null, response.status[0]);
}
);
});
};
Api.prototype.play = function(cb) {
this.sessionRequest({ type: 'PLAY' }, cb);
};
Api.prototype.pause = function(cb) {
this.sessionRequest({ type: 'PAUSE' }, cb);
};
Api.prototype.stop = function(cb) {
this.sessionRequest({ type: 'STOP' }, cb);
};
Api.prototype.seek = function(currentTime, cb) {
this.sessionRequest({
type: 'SEEK',
currentTime: currentTime
}, cb);
};
module.exports = Api;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment