From edb3b32093f824ca1da287071adf894c7dbd5d96 Mon Sep 17 00:00:00 2001 From: futpib Date: Sat, 10 Nov 2018 19:22:41 +0300 Subject: [PATCH] Add loadModule + unloadModuleByIndex --- README.md | 4 +++ lib/client.js | 64 ++++++++++++++++++++++++++++++++++++++--------- test/e2e/index.js | 29 +++++++++++++++++++++ 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/README.md b/README.md index 41eb493..3736e44 100644 --- a/README.md +++ b/README.md @@ -175,6 +175,10 @@ function getFnFromType(type) { * **killSourceOutputByIndex**(< _integer_ >index, < _function_ >callback) - _(void)_ - Terminates a source output. `callback` has 1 parameter: < _Error_ >err. +* **loadModule**(< _string_ >moduleName, < _string_ >argument, < _function_ >callback) - _(void)_ - Load a module. `callback` has 1 parameter: < _Error_ >err. + +* **unloadModuleByIndex**(< _integer_ >moduleIndex, < _function_ >callback) - _(void)_ - Unload a module. `callback` has 1 parameter: < _Error_ >err. + * **moveSinkInput**(< _integer_ >index, < _mixed_ >destSink, < _function_ >callback) - _(void)_ - Moves a sink input to a different sink identified by either its index (_integer_) or its name (_string_). `callback` has 1 parameter: < _Error_ >err. * **moveSourceOutput**(< _integer_ >index, < _mixed_ >destSink, < _function_ >callback) - _(void)_ - Moves a source output to a different source identified by either its index (_integer_) or its name (_string_). `callback` has 1 parameter: < _Error_ >err. diff --git a/lib/client.js b/lib/client.js index c1c0dea..d6d0205 100755 --- a/lib/client.js +++ b/lib/client.js @@ -1144,12 +1144,12 @@ function(sourceOutputIndex, destSource, cb) { const cmd = PA_COMMAND_MOVE_SOURCE_OUTPUT; const buf = new PacketBuilder() - .putu32(cmd) - .putu32(addReq(this, cmd, cb)) - .putu32(sourceOutputIndex) - .putu32(destSource) - .puts(destSource) - .toBuffer(); + .putu32(cmd) + .putu32(addReq(this, cmd, cb)) + .putu32(sourceOutputIndex) + .putu32(destSource) + .puts(destSource) + .toBuffer(); this._socket.write(buf); }; @@ -1168,12 +1168,12 @@ PAClient.prototype.setSinkPort = function(sink, portName, cb) { const cmd = PA_COMMAND_SET_SINK_PORT; const buf = new PacketBuilder() - .putu32(cmd) - .putu32(addReq(this, cmd, cb)) - .putu32(sink) - .puts(sink) - .puts(portName) - .toBuffer(); + .putu32(cmd) + .putu32(addReq(this, cmd, cb)) + .putu32(sink) + .puts(sink) + .puts(portName) + .toBuffer(); this._socket.write(buf); }; @@ -1385,6 +1385,46 @@ PAClient.prototype.removeClientProperties = function(keys, cb) { this._socket.write(buf); }; +PAClient.prototype.loadModule = function(name, argument, cb) { + if (typeof name !== 'string') { + throw new Error('Module name must be a string'); + } + if (typeof argument !== 'string') { + throw new Error('Module argument must be a string (at least pass an empty string)'); + } + if (this._index === -1) { + throw new Error('Not ready'); + } + + const cmd = PA_COMMAND_LOAD_MODULE; + const buf = new PacketBuilder() + .putu32(cmd) + .putu32(addReq(this, cmd, cb)) + .puts(name) + .puts(argument) + .toBuffer(); + + this._socket.write(buf); +}; + +PAClient.prototype.unloadModuleByIndex = function(index, cb) { + if (typeof index !== 'number') { + throw new Error('Module index must be a number'); + } + if (this._index === -1) { + throw new Error('Not ready'); + } + + const cmd = PA_COMMAND_UNLOAD_MODULE; + const buf = new PacketBuilder() + .putu32(cmd) + .putu32(addReq(this, cmd, cb)) + .putu32(index) + .toBuffer(); + + this._socket.write(buf); +}; + PAClient.prototype.subscribe = function(events, cb) { if (this._index === -1) throw new Error('Not ready'); diff --git a/test/e2e/index.js b/test/e2e/index.js index 8a5b911..bf92206 100644 --- a/test/e2e/index.js +++ b/test/e2e/index.js @@ -32,29 +32,58 @@ test.serial('connection', async t => { test.serial('moveSourceOutput (index, index)', async t => { const { pa, connect } = t.context; await connect(); + const sourceOutputs = await pify(pa).getSourceOutputs(); const sourceOutput = sourceOutputs.find(so => so.sourceIndex >= 0); await pify(pa).moveSourceOutput(sourceOutput.index, sourceOutput.sourceIndex); + t.pass(); }); test.serial('moveSourceOutput (index, name)', async t => { const { pa, connect } = t.context; await connect(); + const sourceOutputs = await pify(pa).getSourceOutputs(); const sources = await pify(pa).getSources(); const sourceOutput = sourceOutputs.find(so => so.sourceIndex >= 0); const source = sources.find(s => s.index === sourceOutput.sourceIndex); await pify(pa).moveSourceOutput(sourceOutput.index, source.name); + t.pass(); }); test.serial('setSinkPort (name, name)', async t => { const { pa, connect } = t.context; await connect(); + const sinks = await pify(pa).getSinks(); const sink = sinks.find(s => s.ports.length > 0); const { activePortName } = sink; await pify(pa).setSinkPort(sink.name, activePortName); + + t.pass(); +}); + +test.serial('loadModule + unloadModuleByIndex', async t => { + const { pa, connect } = t.context; + await connect(); + + const modulesBefore = await pify(pa).getModules(); + await pify(pa).loadModule('module-null-sink', ''); + const modulesAfter = await pify(pa).getModules(); + + t.is(modulesAfter.length, modulesBefore.length + 1); + + const lastModule = modulesAfter[modulesAfter.length - 1]; + + t.is(lastModule.name, 'module-null-sink'); + + await pify(pa).unloadModuleByIndex(lastModule.index); + + const modulesAfterKill = await pify(pa).getModules(); + + t.deepEqual(modulesAfterKill, modulesBefore); + t.pass(); });