Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix setSinkPort, add module commands, add tests #4

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[{package.json,*.yml}]
indent_style = space
indent_size = 2
76 changes: 76 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Runtime data
pids
*.pid
*.seed
*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov

# Coverage directory used by tools like istanbul
coverage

# nyc test coverage
.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)
.grunt

# Bower dependency directory (https://bower.io/)
bower_components

# node-waf configuration
.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)
build/Release

# Dependency directories
node_modules/
jspm_packages/

# TypeScript v1 declaration files
typings/

# Optional npm cache directory
.npm

# Optional eslint cache
.eslintcache

# Optional REPL history
.node_repl_history

# Output of 'npm pack'
*.tgz

# Yarn Integrity file
.yarn-integrity

# dotenv environment variables file
.env

# parcel-bundler cache (https://parceljs.org/)
.cache

# next.js build output
.next

# nuxt.js build output
.nuxt

# vuepress build output
.vuepress/dist

# Serverless directories
.serverless

# FuseBox cache
.fusebox/
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
190 changes: 83 additions & 107 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,35 @@ const {
PA_COMMAND_GET_SINK_INPUT_INFO, PA_COMMAND_GET_SOURCE_OUTPUT_INFO,
PA_COMMAND_GET_SOURCE_INFO, PA_COMMAND_GET_SINK_INFO,
PA_COMMAND_GET_CLIENT_INFO, PA_COMMAND_GET_MODULE_INFO,
PA_COMMAND_GET_CARD_INFO, PA_COMMAND_GET_SERVER_INFO
PA_COMMAND_GET_CARD_INFO, PA_COMMAND_GET_SERVER_INFO,
PA_COMMAND_LOAD_MODULE, PA_COMMAND_UNLOAD_MODULE
} = require('./packet-types.js');

const PA_TAG_INVALID = 0;
const PA_TAG_STRING = 116;
const PA_TAG_STRING_NULL = 78;
const PA_TAG_U32 = 76;
const PA_TAG_U8 = 66;
const PA_TAG_U64 = 82;
const PA_TAG_S64 = 114;
const PA_TAG_SAMPLE_SPEC = 97;
const PA_TAG_ARBITRARY = 120;
const PA_TAG_BOOLEAN_TRUE = 49;
const PA_TAG_BOOLEAN_FALSE = 48;
const PA_TAG_BOOLEAN = PA_TAG_BOOLEAN_TRUE;
const PA_TAG_TIMEVAL = 84;
const PA_TAG_USEC = 85/* 64bit unsigned */;
const PA_TAG_CHANNEL_MAP = 109;
const PA_TAG_CVOLUME = 118;
const PA_TAG_PROPLIST = 80;
const PA_TAG_VOLUME = 86;
const PA_TAG_FORMAT_INFO = 102;
const {
PACKET_STATIC_FIELDS,

PA_TAG_INVALID,
PA_TAG_STRING,
PA_TAG_STRING_NULL,
PA_TAG_U32,
PA_TAG_U8,
PA_TAG_U64,
PA_TAG_S64,
PA_TAG_SAMPLE_SPEC,
PA_TAG_ARBITRARY,
PA_TAG_BOOLEAN_TRUE,
PA_TAG_BOOLEAN_FALSE,
PA_TAG_BOOLEAN,
PA_TAG_TIMEVAL,
PA_TAG_USEC,
PA_TAG_CHANNEL_MAP,
PA_TAG_CVOLUME,
PA_TAG_PROPLIST,
PA_TAG_VOLUME,
PA_TAG_FORMAT_INFO,

PacketBuilder,
} = require('./packet-builder.js');

const PA_SUBSCRIPTION_EVENT_SINK = 0x0000;
const PA_SUBSCRIPTION_EVENT_SOURCE = 0x0001;
Expand Down Expand Up @@ -87,12 +94,6 @@ const DNS_6ONLY = { family: 6 };
const DEFAULT_PORT = 4713;
const PROTOCOL_VERSION = 32;
const DEFAULT_CLIENT_PROPS = { application: { name: 'paclient.js' } };
const PACKET_STATIC_FIELDS = Buffer.from([
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00
]);



Expand Down Expand Up @@ -439,7 +440,7 @@ PAClient.prototype.getSinkIndexByName = function(name, cb) {
else
buf = Buffer.allocUnsafe(31 + Buffer.byteLength(name) + 1);

buf.writeUInt32BE(buf.length - 20, 0, true);
buf.writeUInt32BE(buf.length - 20, 0, true);
PACKET_STATIC_FIELDS.copy(buf, 4);

const cmd = PA_COMMAND_LOOKUP_SINK;
Expand Down Expand Up @@ -1141,42 +1142,14 @@ function(sourceOutputIndex, destSource, cb) {
if (this._index === -1)
throw new Error('Not ready');

var buf;
if (typeof destSource === 'number') {
buf = Buffer.allocUnsafe(41);
} else {
var len = Buffer.byteLength(destSource);
if (len > 0)
++len;
buf = Buffer.allocUnsafe(41 + len);
}

buf.writeUInt32BE(buf.length - 20, 0, true);
PACKET_STATIC_FIELDS.copy(buf, 4);

const cmd = PA_COMMAND_MOVE_SOURCE_OUTPUT;
buf[20] = PA_TAG_U32;
buf.writeUInt32BE(cmd, 21, true);
buf[25] = PA_TAG_U32;
buf.writeUInt32BE(addReq(this, cmd, cb), 26, true);

buf[30] = PA_TAG_U32;
buf.writeUInt32BE(sourceOutputIndex, 31, true);

buf[35] = PA_TAG_U32;
if (typeof destSource === 'number') {
buf.writeUInt32BE(destSource, 36, true);
buf[40] = PA_TAG_STRING_NULL;
} else {
buf.fill(0xFF, 36, 40);
if (destSource.length === 0) {
buf[40] = PA_TAG_STRING_NULL;
} else {
buf[40] = PA_TAG_STRING;
buf.write(destSource, 41);
buf[buf.length - 1] = 0;
}
}
const buf = new PacketBuilder()
.putu32(cmd)
.putu32(addReq(this, cmd, cb))
.putu32(sourceOutputIndex)
.putu32(destSource)
.puts(destSource)
.toBuffer();

this._socket.write(buf);
};
Expand All @@ -1193,51 +1166,14 @@ PAClient.prototype.setSinkPort = function(sink, portName, cb) {
if (this._index === -1)
throw new Error('Not ready');

var total = 36;
if (typeof sink !== 'number') {
var sinkLen = Buffer.byteLength(sink);
if (sinkLen > 0)
++sinkLen;
total += sinkLen;
}
if (portName.length > 0)
total += Buffer.byteLength(portName) + 1;
const buf = Buffer.allocUnsafe(total);

buf.writeUInt32BE(buf.length - 20, 0, true);
PACKET_STATIC_FIELDS.copy(buf, 4);

const cmd = PA_COMMAND_SET_SINK_PORT;
buf[20] = PA_TAG_U32;
buf.writeUInt32BE(cmd, 21, true);
buf[25] = PA_TAG_U32;
buf.writeUInt32BE(addReq(this, cmd, cb), 26, true);

buf[30] = PA_TAG_U32;
var p;
if (typeof sink === 'number') {
buf.writeUInt32BE(sink, 31, true);
buf[35] = PA_TAG_STRING_NULL;
p = 36;
} else {
buf.fill(0xFF, 31, 35);
if (sink.length === 0) {
buf[35] = PA_TAG_STRING_NULL;
p = 36;
} else {
buf[35] = PA_TAG_STRING;
buf.write(sink, 36);
buf[36 + sinkLen] = 0;
p = 36 + sinkLen + 1;
}
}

if (portName.length === 0) {
buf[p] = PA_TAG_STRING_NULL;
} else {
buf[p++] = PA_TAG_STRING;
buf.write(portName, p);
}
const buf = new PacketBuilder()
.putu32(cmd)
.putu32(addReq(this, cmd, cb))
.putu32(sink)
.puts(sink)
.puts(portName)
.toBuffer();

this._socket.write(buf);
};
Expand Down Expand Up @@ -1449,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 Expand Up @@ -2987,7 +2963,7 @@ function readCard(payload, p, len, serverVer) {
if (serverVer >= 27) {
if (p + 9 > len || payload[p++] !== PA_TAG_S64)
return;
var profileLatencyOffset
var profileLatencyOffset;
var sign;
if (payload[p] & 0x80) {
sign = -1;
Expand Down
Loading