|
| 1 | +// -*- indent-tabs-mode: nil; js-indent-level: 2 -*- |
| 2 | +/* This Source Code Form is subject to the terms of the Mozilla Public |
| 3 | + * License, v. 2.0. If a copy of the MPL was not distributed with this file, |
| 4 | + * You can obtain one at http://mozilla.org/MPL/2.0/. */ |
| 5 | + |
| 6 | +"use strict"; |
| 7 | + |
| 8 | +this.EXPORTED_SYMBOLS = ["PresentationApp"]; |
| 9 | + |
| 10 | +const { classes: Cc, interfaces: Ci, utils: Cu } = Components; |
| 11 | + |
| 12 | +Cu.import("resource://gre/modules/Services.jsm"); |
| 13 | +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); |
| 14 | + |
| 15 | +XPCOMUtils.defineLazyGetter(this, "sysInfo", () => { |
| 16 | + return Cc["@mozilla.org/system-info;1"].getService(Ci.nsIPropertyBag2); |
| 17 | +}); |
| 18 | + |
| 19 | +const DEBUG = false; |
| 20 | + |
| 21 | +const STATE_UNINIT = "uninitialized" // RemoteMedia status |
| 22 | +const STATE_STARTED = "started"; // RemoteMedia status |
| 23 | +const STATE_PAUSED = "paused"; // RemoteMedia status |
| 24 | +const STATE_SHUTDOWN = "shutdown"; // RemoteMedia status |
| 25 | + |
| 26 | +function debug(msg) { |
| 27 | + Services.console.logStringMessage("PresentationApp: " + msg); |
| 28 | +} |
| 29 | + |
| 30 | +// PresentationApp is a wrapper for interacting with a Presentation Receiver Device. |
| 31 | +function PresentationApp(service, request) { |
| 32 | + this.service = service; |
| 33 | + this.request = request; |
| 34 | +} |
| 35 | + |
| 36 | +PresentationApp.prototype = { |
| 37 | + start: function start(callback) { |
| 38 | + this.request.startWithDevice(this.service.uuid) |
| 39 | + .then((session) => { |
| 40 | + this._session = session; |
| 41 | + if (callback) { |
| 42 | + callback(true); |
| 43 | + } |
| 44 | + }, () => { |
| 45 | + if (callback) { |
| 46 | + callback(false); |
| 47 | + } |
| 48 | + }); |
| 49 | + }, |
| 50 | + |
| 51 | + stop: function stop(callback) { |
| 52 | + if (this._session && this._session.state === "connected") { |
| 53 | + this._session.terminate(); |
| 54 | + } |
| 55 | + |
| 56 | + delete this._session; |
| 57 | + |
| 58 | + if (callback) { |
| 59 | + callback(true); |
| 60 | + } |
| 61 | + }, |
| 62 | + |
| 63 | + remoteMedia: function remoteMedia(callback, listener) { |
| 64 | + if (callback) { |
| 65 | + if (!this._session) { |
| 66 | + callback(); |
| 67 | + return; |
| 68 | + } |
| 69 | + |
| 70 | + callback(new RemoteMedia(this._session, listener)); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +/* RemoteMedia provides a wrapper for using Presentation API to control Firefox TV app. |
| 76 | + * The server implementation must be built into the Firefox TV receiver app. |
| 77 | + * see https://github.com/mozilla-b2g/gaia/tree/master/tv_apps/fling-player |
| 78 | + */ |
| 79 | +function RemoteMedia(session, listener) { |
| 80 | + this._session = session ; |
| 81 | + this._listener = listener; |
| 82 | + this._status = STATE_UNINIT; |
| 83 | + |
| 84 | + this._session.addEventListener("message", this); |
| 85 | + this._session.addEventListener("statechange", this); |
| 86 | + |
| 87 | + if (this._listener && "onRemoteMediaStart" in this._listener) { |
| 88 | + Services.tm.mainThread.dispatch((function() { |
| 89 | + this._listener.onRemoteMediaStart(this); |
| 90 | + }).bind(this), Ci.nsIThread.DISPATCH_NORMAL); |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +RemoteMedia.prototype = { |
| 95 | + _seq: 0, |
| 96 | + |
| 97 | + handleEvent: function(e) { |
| 98 | + switch (e.type) { |
| 99 | + case "message": |
| 100 | + this._onmessage(e); |
| 101 | + break; |
| 102 | + case "statechange": |
| 103 | + this._onstatechange(e); |
| 104 | + break; |
| 105 | + } |
| 106 | + }, |
| 107 | + |
| 108 | + _onmessage: function(e) { |
| 109 | + DEBUG && debug("onmessage: " + e.data); |
| 110 | + if (this.status === STATE_SHUTDOWN) { |
| 111 | + return; |
| 112 | + } |
| 113 | + |
| 114 | + if (e.data.indexOf("stopped") > -1) { |
| 115 | + if (this.status !== STATE_PAUSED) { |
| 116 | + this._status = STATE_PAUSED; |
| 117 | + if (this._listener && "onRemoteMediaStatus" in this._listener) { |
| 118 | + this._listener.onRemoteMediaStatus(this); |
| 119 | + } |
| 120 | + } |
| 121 | + } else if (e.data.indexOf("playing") > -1) { |
| 122 | + if (this.status !== STATE_STARTED) { |
| 123 | + this._status = STATE_STARTED; |
| 124 | + if (this._listener && "onRemoteMediaStatus" in this._listener) { |
| 125 | + this._listener.onRemoteMediaStatus(this); |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + }, |
| 130 | + |
| 131 | + _onstatechange: function(e) { |
| 132 | + DEBUG && debug("onstatechange: " + this._session.state); |
| 133 | + if (this._session.state !== "connected") { |
| 134 | + this._status = STATE_SHUTDOWN; |
| 135 | + if (this._listener && "onRemoteMediaStop" in this._listener) { |
| 136 | + this._listener.onRemoteMediaStop(this); |
| 137 | + } |
| 138 | + } |
| 139 | + }, |
| 140 | + |
| 141 | + _sendCommand: function(command, data) { |
| 142 | + let msg = { |
| 143 | + 'type': command, |
| 144 | + 'seq': ++this._seq |
| 145 | + }; |
| 146 | + |
| 147 | + if (data) { |
| 148 | + for (var k in data) { |
| 149 | + msg[k] = data[k]; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + let raw = JSON.stringify(msg); |
| 154 | + DEBUG && debug("send command: " + raw); |
| 155 | + |
| 156 | + this._session.send(raw); |
| 157 | + }, |
| 158 | + |
| 159 | + shutdown: function shutdown() { |
| 160 | + DEBUG && debug("RemoteMedia - shutdown"); |
| 161 | + this._sendCommand("close"); |
| 162 | + }, |
| 163 | + |
| 164 | + play: function play() { |
| 165 | + DEBUG && debug("RemoteMedia - play"); |
| 166 | + this._sendCommand("play"); |
| 167 | + }, |
| 168 | + |
| 169 | + pause: function pause() { |
| 170 | + DEBUG && debug("RemoteMedia - pause"); |
| 171 | + this._sendCommand("pause"); |
| 172 | + }, |
| 173 | + |
| 174 | + load: function load(data) { |
| 175 | + DEBUG && debug("RemoteMedia - load: " + data); |
| 176 | + this._sendCommand("load", { "url": data.source }); |
| 177 | + |
| 178 | + let deviceName; |
| 179 | + if (Services.appinfo.widgetToolkit == "android") { |
| 180 | + deviceName = sysInfo.get("device"); |
| 181 | + } else { |
| 182 | + deviceName = sysInfo.get("host"); |
| 183 | + } |
| 184 | + this._sendCommand("device-info", { "displayName": deviceName }); |
| 185 | + }, |
| 186 | + |
| 187 | + get status() { |
| 188 | + return this._status; |
| 189 | + } |
| 190 | +} |
0 commit comments