diff --git a/clapper_src/player.js b/clapper_src/player.js index 776ed62c..be06fddc 100644 --- a/clapper_src/player.js +++ b/clapper_src/player.js @@ -639,6 +639,22 @@ class ClapperPlayer extends PlayerBase return true; } + _onWsData(server, action, value) + { + switch(action) { + case 'toggle-play': + this.toggle_play(); + break; + case 'play': + case 'pause': + this[action](); + break; + default: + super._onWsData(server, action, value); + break; + } + } + _onCloseRequest(window) { this._performCloseCleanup(window); diff --git a/clapper_src/playerBase.js b/clapper_src/playerBase.js index d703ff33..ceabf855 100644 --- a/clapper_src/playerBase.js +++ b/clapper_src/playerBase.js @@ -2,6 +2,8 @@ const { Gio, GLib, GObject, Gst, GstPlayer, Gtk } = imports.gi; const Debug = imports.clapper_src.debug; const Misc = imports.clapper_src.misc; +let WebServer; + /* PlayFlags are not exported through GI */ Gst.PlayFlags = { VIDEO: 1, @@ -66,6 +68,9 @@ class ClapperPlayerBase extends GstPlayer.Player this.state = GstPlayer.PlayerState.STOPPED; this.visualization_enabled = false; + this.webserver = null; + this.websocketSignal = null; + this.set_all_plugins_ranks(); this.set_initial_config(); this.set_and_bind_settings(); @@ -89,6 +94,7 @@ class ClapperPlayerBase extends GstPlayer.Player 'audio-offset', 'subtitle-offset', 'play-flags', + 'webserver-enabled', ]; for(let key of settingsToSet) @@ -175,6 +181,11 @@ class ClapperPlayerBase extends GstPlayer.Player this.widget.queue_render(); } + _onWsData(server, action, value) + { + debug(`unhandled WebSocket action: ${action}`); + } + _onSettingsKeyChanged(settings, key) { let root, value, action; @@ -267,6 +278,29 @@ class ClapperPlayerBase extends GstPlayer.Player this.pipeline.flags = settingsFlags; debug(`changed play flags: ${initialFlags} -> ${settingsFlags}`); break; + case 'webserver-enabled': + const webserverEnabled = settings.get_boolean(key); + + if(webserverEnabled) { + if(!WebServer) { + /* Probably most users will not use this, + * so conditional import for faster startup */ + WebServer = imports.clapper_src.webserver.WebServer; + } + + if(!this.webserver) + this.webserver = new WebServer(settings.get_int('webserver-port')); + + this.webserver.startListening(); + this.websocketSignal = this.webserver.connect( + 'websocket-data', this._onWsData.bind(this) + ); + } + else if(this.webserver) { + this.webserver.disconnect(this.websocketSignal); + this.webserver.stopListening(); + } + break; default: break; } diff --git a/data/com.github.rafostar.Clapper.gschema.xml b/data/com.github.rafostar.Clapper.gschema.xml index c18218b1..15f85b95 100644 --- a/data/com.github.rafostar.Clapper.gschema.xml +++ b/data/com.github.rafostar.Clapper.gschema.xml @@ -49,6 +49,16 @@ The subtitles font description + + + false + Enable WebSocket server for remote playback control + + + 6446 + Listening port to use for incoming WebSocket connections + + true