Skip to content

Commit

Permalink
Add loadModule + unloadModuleByIndex
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib committed Nov 10, 2018
1 parent 0a42131 commit edb3b32
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 12 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
64 changes: 52 additions & 12 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};
Expand All @@ -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);
};
Expand Down Expand Up @@ -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');
Expand Down
29 changes: 29 additions & 0 deletions test/e2e/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
});

0 comments on commit edb3b32

Please sign in to comment.