Implement shell IPC and pass it to the player

This commit is contained in:
Vladimir Borisov 2022-07-26 13:40:54 +03:00
parent 2744e583cd
commit 1ba83f42bb
No known key found for this signature in database
GPG key ID: F9A584BE4FCB6603
2 changed files with 83 additions and 1 deletions

View file

@ -18,7 +18,7 @@ const useSettings = require('./useSettings');
const styles = require('./styles');
const Player = ({ urlParams, queryParams }) => {
const { core, chromecast } = useServices();
const { core, chromecast, shell } = useServices();
const [forceTranscoding, maxAudioChannels] = React.useMemo(() => {
return [
queryParams.has('forceTranscoding'),
@ -263,6 +263,7 @@ const Player = ({ urlParams, queryParams }) => {
}
}, {
chromecastTransport: chromecast.active ? chromecast.transport : null,
shell,
});
}
}, [streamingServer.baseUrl, player.selected, player.metaItem, forceTranscoding, maxAudioChannels, casting]);

View file

@ -59,6 +59,87 @@ function Shell() {
this.off = function(name, listener) {
events.off(name, listener);
};
this.props = {};
const shell = this;
window.initShellComm = function () {
var transport = window.qt && window.qt.webChannelTransport;
if (!transport) throw 'no viable transport found (qt.webChannelTransport)';
active = false;
starting = true;
error = null;
var QtMsgTypes = {
signal: 1,
propertyUpdate: 2,
init: 3,
idle: 4,
debug: 5,
invokeMethod: 6,
connectToSignal: 7,
disconnectFromSignal: 8,
setProperty: 9,
response: 10,
};
var QtObjId = 'transport'; // the ID of our transport object
var id = 0;
function send(msg) {
msg.id = id++;
transport.send(JSON.stringify(msg));
}
transport.onmessage = function (message) {
var msg = JSON.parse(message.data);
if (msg.id === 0) {
var obj = msg.data[QtObjId];
obj.properties.slice(1).forEach(function (prop) {
shell.props[prop[1]] = prop[3];
});
if (typeof shell.props.shellVersion === 'string') {
shell.shellVersionArr = (
shell.props.shellVersion.match(/(\d+)\.(\d+)\.(\d+)/) || []
)
.slice(1, 4)
.map(Number);
}
events.emit('received-props', shell.props);
obj.signals.forEach(function (sig) {
send({
type: QtMsgTypes.connectToSignal,
object: QtObjId,
signal: sig[1],
});
});
var onEvent = obj.methods.filter(function (x) {
return x[0] === 'onEvent';
})[0];
shell.send = function (ev, args) {
send({
type: QtMsgTypes.invokeMethod,
object: QtObjId,
method: onEvent[1],
args: [ev, args || {}],
});
};
starting = false;
active = true;
error = null;
shell.send('app-ready', {}); // signal that we're ready to take events
}
if (msg.object === QtObjId && msg.type === QtMsgTypes.signal)
events.emit(msg.args[0], msg.args[1]);
onStateChanged();
};
send({ type: QtMsgTypes.init });
};
}
module.exports = Shell;