michael@0: // -*- Mode: js2; tab-width: 2; indent-tabs-mode: nil; js2-basic-offset: 2; js2-skip-preprocessor-directives: t; -*- michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: "use strict"; michael@0: michael@0: this.EXPORTED_SYMBOLS = ["RokuApp"]; michael@0: michael@0: const { classes: Cc, interfaces: Ci, utils: Cu } = Components; michael@0: michael@0: Cu.import("resource://gre/modules/Services.jsm"); michael@0: michael@0: function log(msg) { michael@0: //Services.console.logStringMessage(msg); michael@0: } michael@0: michael@0: const PROTOCOL_VERSION = 1; michael@0: michael@0: /* RokuApp is a wrapper for interacting with a Roku channel. michael@0: * The basic interactions all use a REST API. michael@0: * spec: http://sdkdocs.roku.com/display/sdkdoc/External+Control+Guide michael@0: */ michael@0: function RokuApp(service, app) { michael@0: this.service = service; michael@0: this.resourceURL = this.service.location; michael@0: this.app = app; michael@0: this.appID = -1; michael@0: } michael@0: michael@0: RokuApp.prototype = { michael@0: status: function status(callback) { michael@0: // We have no way to know if the app is running, so just return "unknown" michael@0: // but we use this call to fetch the appID for the given app name michael@0: let url = this.resourceURL + "query/apps"; michael@0: let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest); michael@0: xhr.open("GET", url, true); michael@0: xhr.channel.loadFlags |= Ci.nsIRequest.INHIBIT_CACHING; michael@0: xhr.overrideMimeType("text/xml"); michael@0: michael@0: xhr.addEventListener("load", (function() { michael@0: if (xhr.status == 200) { michael@0: let doc = xhr.responseXML; michael@0: let apps = doc.querySelectorAll("app"); michael@0: for (let app of apps) { michael@0: if (app.textContent == this.app) { michael@0: this.appID = app.id; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // Since ECP has no way of telling us if an app is running, we always return "unknown" michael@0: if (callback) { michael@0: callback({ state: "unknown" }); michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.addEventListener("error", (function() { michael@0: if (callback) { michael@0: callback({ state: "unknown" }); michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.send(null); michael@0: }, michael@0: michael@0: start: function start(callback) { michael@0: // We need to make sure we have cached the appID michael@0: if (this.appID == -1) { michael@0: this.status(function() { michael@0: // If we found the appID, use it to make a new start call michael@0: if (this.appID != -1) { michael@0: this.start(callback); michael@0: } else { michael@0: // We failed to start the app, so let the caller know michael@0: callback(false); michael@0: } michael@0: }.bind(this)); michael@0: return; michael@0: } michael@0: michael@0: // Start a given app with any extra query data. Each app uses it's own data scheme. michael@0: // NOTE: Roku will also pass "source=external-control" as a param michael@0: let url = this.resourceURL + "launch/" + this.appID + "?version=" + parseInt(PROTOCOL_VERSION); michael@0: let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest); michael@0: xhr.open("POST", url, true); michael@0: xhr.overrideMimeType("text/plain"); michael@0: michael@0: xhr.addEventListener("load", (function() { michael@0: if (callback) { michael@0: callback(xhr.status === 200); michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.addEventListener("error", (function() { michael@0: if (callback) { michael@0: callback(false); michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.send(null); michael@0: }, michael@0: michael@0: stop: function stop(callback) { michael@0: // Roku doesn't seem to support stopping an app, so let's just go back to michael@0: // the Home screen michael@0: let url = this.resourceURL + "keypress/Home"; michael@0: let xhr = Cc["@mozilla.org/xmlextras/xmlhttprequest;1"].createInstance(Ci.nsIXMLHttpRequest); michael@0: xhr.open("POST", url, true); michael@0: xhr.overrideMimeType("text/plain"); michael@0: michael@0: xhr.addEventListener("load", (function() { michael@0: if (callback) { michael@0: callback(xhr.status === 200); michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.addEventListener("error", (function() { michael@0: if (callback) { michael@0: callback(false); michael@0: } michael@0: }).bind(this), false); michael@0: michael@0: xhr.send(null); michael@0: }, michael@0: michael@0: remoteMedia: function remoteMedia(callback, listener) { michael@0: if (this.appID != -1) { michael@0: if (callback) { michael@0: callback(new RemoteMedia(this.resourceURL, listener)); michael@0: } michael@0: } else { michael@0: if (callback) { michael@0: callback(); michael@0: } michael@0: } michael@0: } michael@0: } michael@0: michael@0: /* RemoteMedia provides a wrapper for using TCP socket to control Roku apps. michael@0: * The server implementation must be built into the Roku receiver app. michael@0: */ michael@0: function RemoteMedia(url, listener) { michael@0: this._url = url; michael@0: this._listener = listener; michael@0: this._status = "uninitialized"; michael@0: michael@0: let serverURI = Services.io.newURI(this._url , null, null); michael@0: this._socket = Cc["@mozilla.org/network/socket-transport-service;1"].getService(Ci.nsISocketTransportService).createTransport(null, 0, serverURI.host, 9191, null); michael@0: this._outputStream = this._socket.openOutputStream(0, 0, 0); michael@0: michael@0: this._scriptableStream = Cc["@mozilla.org/scriptableinputstream;1"].createInstance(Ci.nsIScriptableInputStream); michael@0: michael@0: this._inputStream = this._socket.openInputStream(0, 0, 0); michael@0: this._pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance(Ci.nsIInputStreamPump); michael@0: this._pump.init(this._inputStream, -1, -1, 0, 0, true); michael@0: this._pump.asyncRead(this, null); michael@0: } michael@0: michael@0: RemoteMedia.prototype = { michael@0: onStartRequest: function(request, context) { michael@0: }, michael@0: michael@0: onDataAvailable: function(request, context, stream, offset, count) { michael@0: this._scriptableStream.init(stream); michael@0: let data = this._scriptableStream.read(count); michael@0: if (!data) { michael@0: return; michael@0: } michael@0: michael@0: let msg = JSON.parse(data); michael@0: if (this._status === msg._s) { michael@0: return; michael@0: } michael@0: michael@0: this._status = msg._s; michael@0: michael@0: if (this._listener) { michael@0: // Check to see if we are getting the initial "connected" message michael@0: if (this._status == "connected" && "onRemoteMediaStart" in this._listener) { michael@0: this._listener.onRemoteMediaStart(this); michael@0: } michael@0: michael@0: if ("onRemoteMediaStatus" in this._listener) { michael@0: this._listener.onRemoteMediaStatus(this); michael@0: } michael@0: } michael@0: }, michael@0: michael@0: onStopRequest: function(request, context, result) { michael@0: if (this._listener && "onRemoteMediaStop" in this._listener) michael@0: this._listener.onRemoteMediaStop(this); michael@0: }, michael@0: michael@0: _sendMsg: function _sendMsg(data) { michael@0: if (!data) michael@0: return; michael@0: michael@0: // Add the protocol version michael@0: data["_v"] = PROTOCOL_VERSION; michael@0: michael@0: let raw = JSON.stringify(data); michael@0: this._outputStream.write(raw, raw.length); michael@0: }, michael@0: michael@0: shutdown: function shutdown() { michael@0: this._outputStream.close(); michael@0: this._inputStream.close(); michael@0: }, michael@0: michael@0: get active() { michael@0: return (this._socket && this._socket.isAlive()); michael@0: }, michael@0: michael@0: play: function play() { michael@0: // TODO: add position support michael@0: this._sendMsg({ type: "PLAY" }); michael@0: }, michael@0: michael@0: pause: function pause() { michael@0: this._sendMsg({ type: "STOP" }); michael@0: }, michael@0: michael@0: load: function load(aData) { michael@0: this._sendMsg({ type: "LOAD", title: aData.title, source: aData.source, poster: aData.poster }); michael@0: }, michael@0: michael@0: get status() { michael@0: return this._status; michael@0: } michael@0: }