radio/libjs/wController/player.js

375 lines
12 KiB
JavaScript
Raw Normal View History

2018-09-02 19:22:28 +00:00
"use strict";
var Uint64 = require("../wType/uint64");
var Address = require("../wType/address");
2018-09-02 19:22:28 +00:00
var Controller = require("./controller");
var Button = require("./button");
var ImageById = require("./imageById");
2018-09-02 19:22:28 +00:00
var Vocabulary = require("./vocabulary");
2018-12-17 17:15:58 +00:00
var Audio = require("./file/audio");
2018-09-02 19:22:28 +00:00
var Enum = require("../utils/enum");
2018-12-17 17:15:58 +00:00
var StateMachine = require("../utils/stateMachine");
2018-09-02 19:22:28 +00:00
var Source = AV.EventEmitter.extend(function() {
this.prototype.start = function(){}
this.prototype.pause = function(){}
this.prototype.reset = function(){}
});
2018-09-02 19:22:28 +00:00
var Player = Controller.inherit({
className: "Player",
constructor: function(addr) {
Controller.fn.constructor.call(this, addr);
this.controls = Object.create(null);
this.views = Object.create(null);
this.mode = PlayerMode.straight.playback;
2018-12-17 17:15:58 +00:00
this._audio = null;
this._createStateMachine();
this._createPlayingInfrastructure();
2018-09-02 19:22:28 +00:00
this.addHandler("get");
this.addHandler("viewsChange");
2018-12-17 17:15:58 +00:00
this.addHandler("play");
this.addHandler("pause");
},
destructor: function() {
this._fsm.destructor();
this._player.stop();
2018-12-17 17:15:58 +00:00
Controller.fn.destructor.call(this);
2018-09-02 19:22:28 +00:00
},
_addControl: function(type, address) {
var t = type.valueOf();
if (this.controls[t] !== undefined) {
throw new Error("An attempt to add multiple instances of " + ItemType.reversed[t] + " into Player");
}
if (ItemType.reversed[t] !== undefined) {
switch (t) {
case ItemType.straight.playPause:
case ItemType.straight.prev:
case ItemType.straight.next:
2018-09-02 19:22:28 +00:00
var btn = new Button(address.clone());
btn.itemType = t;
this.controls[t] = btn;
this.addController(btn);
2018-10-28 21:32:44 +00:00
this.trigger("newElement", btn, t);
2018-09-02 19:22:28 +00:00
break;
default:
this.trigger("serviceMessage", "An attempt to add ItemType " + ItemType.reversed[t] + " to controls of the Player, but it's not qualified to be a control", 1);
2018-09-02 19:22:28 +00:00
}
} else {
this.trigger("serviceMessage", "An unrecgnized item ItemType in Player: " + t, 1);
2018-09-02 19:22:28 +00:00
}
},
_addView: function(type, address) {
var t = type.valueOf();
var ctrl;
var supported = false;
2018-09-02 19:22:28 +00:00
if (this.views[t] !== undefined) {
throw new Error("An attempt to add multiple instances of " + ItemType.reversed[t] + " into Player");
}
if (ItemType.reversed[t] !== undefined) {
switch (t) {
case ItemType.straight.queue:
this.trigger("serviceMessage", "Queue is not supported yet in Player", 1);
2018-09-02 19:22:28 +00:00
break;
case ItemType.straight.currentPlayback:
ctrl = new Vocabulary(address.clone());
ctrl.on("newElement", this._onNewPlayBackElement, this);
ctrl.on("removeElement", this._onNewRemoveBackElement, this);
supported = true;
break;
case ItemType.straight.picture:
ctrl = new ImageById(null, address.back());
ctrl.ItemType = t;
this.views[t] = ctrl;
this.trigger("newElement", ctrl, t);
supported = false; //just to avoid adding with addController, since ImageById is not a controller
2018-10-28 21:32:44 +00:00
break;
2018-09-02 19:22:28 +00:00
default:
this.trigger("serviceMessage", "An attempt to add ItemType " + ItemType.reversed[t] + " to views of the Player, but it's not qualified to be a view", 1);
2018-09-02 19:22:28 +00:00
}
} else {
this.trigger("serviceMessage", "An unrecognized item ItemType in Player: " + t, 1);
2018-09-02 19:22:28 +00:00
}
if (supported) {
ctrl.ItemType = t;
this.views[t] = ctrl;
this.addController(ctrl);
this.trigger("newElement", ctrl, t);
}
2018-09-02 19:22:28 +00:00
},
_createPlayingInfrastructure() {
if (this._source) {
this._source.reset();
this._asset.stop();
this._player.stop();
}
this._source = new Source();
this._asset = new AV.Asset(this._source);
this._player = new AV.Player(this._asset);
},
2018-12-17 17:15:58 +00:00
_createStateMachine: function() {
this._fsm = new StateMachine("initial", graphs[this.mode]);
this._fsm.on("stateChanged", this._onStateChanged, this);
},
2018-09-02 19:22:28 +00:00
_h_get: function(ev) {
var data = ev.getData();
var controls = data.at("controls");
var views = data.at("views");
var mode = data.at("mode").valueOf();
2018-09-02 19:22:28 +00:00
var size, i, vc;
size = controls.length();
for (i = 0; i < size; ++i) {
vc = controls.at(i);
this._addControl(vc.at("type"), vc.at("address"));
}
size = views.length();
for (i = 0; i < size; ++i) {
vc = views.at(i);
2018-10-28 21:32:44 +00:00
this._addView(vc.at("type"), vc.at("address"));
2018-09-02 19:22:28 +00:00
}
2018-10-28 21:32:44 +00:00
if (this.mode !== mode) {
if (PlayerMode.reversed[mode] === undefined) {
throw new Error("Unsupported mode of player: " + mode);
}
this.mode = mode;
}
2018-10-28 21:32:44 +00:00
this.initialized = true;
this.trigger("data");
2018-09-02 19:22:28 +00:00
},
2018-12-17 17:15:58 +00:00
_h_pause: function(ev) {
this._fsm.manipulation("pause");
2018-12-17 17:15:58 +00:00
},
_h_play: function(ev) {
this._fsm.manipulation("play");
},
2018-09-02 19:22:28 +00:00
_h_viewsChange: function(ev) {
var data = ev.getData();
var add = data.at("add");
var remove = data.at("remove");
var size, i, vc;
size = remove.length();
for (i = 0; i < size; ++i) {
this._removeView(remove.at(i).valueOf());
2018-09-02 19:22:28 +00:00
}
size = add.length();
for (i = 0; i < size; ++i) {
vc = add.at(i);
2018-10-28 21:32:44 +00:00
this._addView(vc.at("type"), vc.at("address"));
2018-09-02 19:22:28 +00:00
}
},
2018-12-17 17:15:58 +00:00
_onAudioNewFrames: function(frames) {
var data = new Uint8Array(frames);
this._source.emit("data", new AV.Buffer(data));
2018-12-17 17:15:58 +00:00
this._fsm.manipulation("newFrames");
if (this._audio.hasMore()) {
this._audio.requestMore();
} else {
this._fsm.manipulation("noMoreFrames");
}
},
_onControllerReady: function() {
this._fsm.manipulation("controllerReady");
},
_onNewPlayBackElement: function(key, element) {
switch (key) {
case "image":
var address = new Address(["images", element.toString()]);
this._addView(new Uint64(ItemType.straight.picture), address);
address.destructor();
break;
2018-12-17 17:15:58 +00:00
case "audio":
if (this.mode === PlayerMode.straight.playback) {
this._audio = new Audio(new Address(["music", element.toString()]));
this.addForeignController("Corax", this._audio);
this._audio.on("newFrames", this._onAudioNewFrames, this);
this._audio.on("ready", this._onControllerReady, this);
2018-12-17 17:15:58 +00:00
this._fsm.manipulation("controller");
}
break;
}
},
_onNewRemoveBackElement: function(key) {
switch (key) {
case "image":
this._removeView(ItemType.straight.picture);
break;
2018-12-17 17:15:58 +00:00
case "audio":
this.removeForeignController(this._audio);
this._audio.destructor();
this._audio = null;
}
},
_onStateChanged: function(e) {
switch (e.newState) {
case "initial":
if (e.manipulation === "noController") {
this.removeForeignController(this._audio);
this._audio.destructor();
this._audio = null;
this._createPlayingInfrastructure();
}
2018-12-17 17:15:58 +00:00
break;
case "initialPlaying":
if (e.manipulation === "noController") {
this._player.pause();
this.removeForeignController(this._audio);
this.audio.destructor();
this._audio = null;
this._createPlayingInfrastructure();
}
2018-12-17 17:15:58 +00:00
break;
case "controllerNotReady":
break
case "controllerNotReadyPlaying":
break
2018-12-17 17:15:58 +00:00
case "hasController":
break;
case "hasControllerPlaying":
if (this._audio.hasMore()) {
this._audio.requestMore();
this._player.play(); //todo temporal
2018-12-17 17:15:58 +00:00
} else {
this._fsm.manipulation("noMoreFrames");
}
break;
case "paused":
switch (e.oldState) {
case "playing":
this._player.pause();
2018-12-17 17:15:58 +00:00
break;
}
break;
case "pausedAllLoaded":
switch (e.oldState) {
case "playingAllLoaded":
this._player.pause();
2018-12-17 17:15:58 +00:00
break;
}
break;
case "playing":
this._player.play();
2018-12-17 17:15:58 +00:00
break;
case "playingAllLoaded":
switch (e.oldState) {
case "pausedAllLoaded":
this._player.play();
2018-12-17 17:15:58 +00:00
break;
}
break;
}
},
2018-09-02 19:22:28 +00:00
_removeControl: function(type) {
var ctrl = this.controls[type];
if (ctrl !== undefined) {
this.trigger("removeElement", type);
this.removeController(ctrl);
ctrl.destructor();
}
2018-09-02 19:22:28 +00:00
},
_removeView: function(type) {
var view = this.views[type];
if (view !== undefined) {
this.trigger("removeElement", type);
if (type !== ItemType.straight.picture) {
this.removeController(view);
}
if (type === ItemType.straight.currentPlayback) {
if (this.views[ItemType.straight.picture]) {
this._removeView(ItemType.straight.picture);
}
this._fsm.manipulation("noController");
}
delete this.views[type];
view.destructor();
}
2018-09-02 19:22:28 +00:00
}
});
var ItemType = new Enum("ItemType");
ItemType.add("playPause");
ItemType.add("currentPlayback");
ItemType.add("queue");
ItemType.add("picture");
ItemType.add("prev");
ItemType.add("next");
var PlayerMode = new Enum("PlayerMode");
PlayerMode.add("playback");
2018-09-02 19:22:28 +00:00
Player.ItemType = ItemType;
2018-12-17 17:15:58 +00:00
var graphs = Object.create(null);
graphs[PlayerMode.straight.playback] = {
"initial": {
controller: "controllerNotReady",
2018-12-17 17:15:58 +00:00
play: "initialPlaying"
},
"initialPlaying": {
pause: "initial",
controller: "controllerNotReadyPlaying"
},
"controllerNotReady": {
play: "controllerNotReadyPlaying",
controllerReady: "hasController"
},
"controllerNotReadyPlaying": {
pause: "controllerNotReady",
controllerReady: "hasControllerPlaying"
2018-12-17 17:15:58 +00:00
},
"hasController": {
newFrames: "paused",
play: "hasControllerPlaying",
noController: "initial"
2018-12-17 17:15:58 +00:00
},
"hasControllerPlaying": {
newFrames: "playing",
pause: "hasController",
noController: "initialPlaying"
2018-12-17 17:15:58 +00:00
},
"paused": {
play: "playing",
noController: "initial",
2018-12-17 17:15:58 +00:00
noMoreFrames: "pausedAllLoaded"
},
"pausedAllLoaded": {
play: "playingAllLoaded",
noController: "initial"
2018-12-17 17:15:58 +00:00
},
"playing": {
pause: "paused",
noMoreFrames: "playingAllLoaded",
noController: "initialPlaying"
2018-12-17 17:15:58 +00:00
},
"playingAllLoaded": {
pause: "pausedAllLoaded",
noController: "initialPlaying"
2018-12-17 17:15:58 +00:00
}
}
2018-09-02 19:22:28 +00:00
module.exports = Player;