pactl, pacmd, cli-command: Add send-message command

Part-of: <https://gitlab.freedesktop.org/pulseaudio/pulseaudio/-/merge_requests/51>
This commit is contained in:
Georg Chini 2020-01-14 10:24:36 +01:00 committed by Tanu Kaskinen
parent 4cdc0053c0
commit 68f2f1395d
9 changed files with 122 additions and 4 deletions

15
doc/messaging_api.txt Normal file
View file

@ -0,0 +1,15 @@
Message API reference
The message API allows any object within pulseaudio to register a message
handler. A message handler is a function that can be called by clients using
PA_COMMAND_SEND_OBJECT_MESSAGE. A message consists at least of an object path
and a message command, both specified as strings. Additional parameters can
be specified using a single string, but are not mandatory. The message handler
returns an error number as defined in def.h and also returns a string in
the "response" variable. The following reference lists available messages,
their parameters and return values.
Recipient:
Message:
Parameters:
Return value:

View file

@ -253,6 +253,13 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
for possible encodings. </p></optdesc> for possible encodings. </p></optdesc>
</option> </option>
<option>
<p><opt>send-message</opt> <arg>RECIPIENT</arg> <arg>MESSAGE</arg> <arg>MESSAGE_PARAMETERS</arg></p>
<optdesc><p>Send a message to the specified recipient object. If applicable an additional string containing
message parameters can be specified. A string is returned as a response to the message. For available messages
see https://cgit.freedesktop.org/pulseaudio/pulseaudio/tree/doc/messaging_api.txt.</p></optdesc>
</option>
<option> <option>
<p><opt>subscribe</opt></p> <p><opt>subscribe</opt></p>
<optdesc><p>Subscribe to events, pactl does not exit by itself, but keeps waiting for new events.</p></optdesc> <optdesc><p>Subscribe to events, pactl does not exit by itself, but keeps waiting for new events.</p></optdesc>

View file

@ -306,6 +306,13 @@ License along with PulseAudio; if not, see <http://www.gnu.org/licenses/>.
<optdesc><p>Debug: Show shared properties.</p></optdesc> <optdesc><p>Debug: Show shared properties.</p></optdesc>
</option> </option>
<option>
<p><opt>send-message</opt> <arg>recipient</arg> <arg>message</arg> <arg>message_parameters</arg></p>
<optdesc><p>Send a message to the specified recipient object. If applicable an additional string containing
message parameters can be specified. A string is returned as a response to the message. For available messages
see https://cgit.freedesktop.org/pulseaudio/pulseaudio/tree/doc/messaging_api.txt.</p></optdesc>
</option>
<option> <option>
<p><opt>exit</opt></p> <p><opt>exit</opt></p>
<optdesc><p>Terminate the daemon. If you want to terminate a CLI <optdesc><p>Terminate the daemon. If you want to terminate a CLI

View file

@ -121,7 +121,7 @@ _pactl() {
set-source-volume set-sink-input-volume set-source-output-volume set-source-volume set-sink-input-volume set-source-output-volume
set-sink-mute set-source-mute set-sink-input-mute set-sink-mute set-source-mute set-sink-input-mute
set-source-output-mute set-sink-formats set-port-latency-offset set-source-output-mute set-sink-formats set-port-latency-offset
subscribe help) subscribe send-message help)
_init_completion -n = || return _init_completion -n = || return
preprev=${words[$cword-2]} preprev=${words[$cword-2]}
@ -271,7 +271,7 @@ _pacmd() {
move-sink-input move-source-output suspend-sink suspend-source move-sink-input move-source-output suspend-sink suspend-source
suspend set-card-profile set-sink-port set-source-port suspend set-card-profile set-sink-port set-source-port
set-port-latency-offset set-log-target set-log-level set-log-meta set-port-latency-offset set-log-target set-log-level set-log-meta
set-log-time set-log-backtrace) set-log-time set-log-backtrace send-message)
_init_completion -n = || return _init_completion -n = || return
preprev=${words[$cword-2]} preprev=${words[$cword-2]}

View file

@ -263,6 +263,7 @@ _pactl_completion() {
'set-sink-input-mute: mute a stream' 'set-sink-input-mute: mute a stream'
'set-source-output-mute: mute a recording stream' 'set-source-output-mute: mute a recording stream'
'set-sink-formats: set supported formats of a sink' 'set-sink-formats: set supported formats of a sink'
'send-message: send a message to a pulseaudio object'
'subscribe: subscribe to events' 'subscribe: subscribe to events'
) )
@ -561,6 +562,7 @@ _pacmd_completion() {
'dump: show daemon configuration' 'dump: show daemon configuration'
'dump-volumes: show the state of all volumes' 'dump-volumes: show the state of all volumes'
'shared: show shared properties' 'shared: show shared properties'
'send-message: send a message to a pulseaudio object'
'exit: ask the PulseAudio daemon to exit' 'exit: ask the PulseAudio daemon to exit'
) )
_describe 'pacmd commands' _pacmd_commands _describe 'pacmd commands' _pacmd_commands

View file

@ -500,7 +500,8 @@ pa_operation* pa_context_unload_module(pa_context *c, uint32_t idx, pa_context_s
/** Callback prototype for pa_context_send_message_to_object() \since 15.0 */ /** Callback prototype for pa_context_send_message_to_object() \since 15.0 */
typedef void (*pa_context_string_cb_t)(pa_context *c, int success, const char *response, void *userdata); typedef void (*pa_context_string_cb_t)(pa_context *c, int success, const char *response, void *userdata);
/** Send a message to an object that registered a message handler. \since 15.0 */ /** Send a message to an object that registered a message handler. For more information
* see https://cgit.freedesktop.org/pulseaudio/pulseaudio/tree/doc/messaging_api.txt. \since 15.0 */
pa_operation* pa_context_send_message_to_object(pa_context *c, const char *recipient_name, const char *message, const char *message_parameters, pa_context_string_cb_t cb, void *userdata); pa_operation* pa_context_send_message_to_object(pa_context *c, const char *recipient_name, const char *message, const char *message_parameters, pa_context_string_cb_t cb, void *userdata);
/** @} */ /** @} */

View file

@ -53,6 +53,7 @@
#include <pulsecore/sound-file-stream.h> #include <pulsecore/sound-file-stream.h>
#include <pulsecore/shared.h> #include <pulsecore/shared.h>
#include <pulsecore/core-util.h> #include <pulsecore/core-util.h>
#include <pulsecore/message-handler.h>
#include <pulsecore/core-error.h> #include <pulsecore/core-error.h>
#include <pulsecore/modinfo.h> #include <pulsecore/modinfo.h>
#include <pulsecore/dynarray.h> #include <pulsecore/dynarray.h>
@ -135,6 +136,7 @@ static int pa_cli_command_sink_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf,
static int pa_cli_command_source_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail); static int pa_cli_command_source_port(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
static int pa_cli_command_port_offset(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail); static int pa_cli_command_port_offset(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
static int pa_cli_command_dump_volumes(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail); static int pa_cli_command_dump_volumes(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
static int pa_cli_command_send_message_to_object(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail);
/* A method table for all available commands */ /* A method table for all available commands */
@ -191,6 +193,7 @@ static const struct command commands[] = {
{ "set-log-meta", pa_cli_command_log_meta, "Show source code location in log messages (args: bool)", 2}, { "set-log-meta", pa_cli_command_log_meta, "Show source code location in log messages (args: bool)", 2},
{ "set-log-time", pa_cli_command_log_time, "Show timestamps in log messages (args: bool)", 2}, { "set-log-time", pa_cli_command_log_time, "Show timestamps in log messages (args: bool)", 2},
{ "set-log-backtrace", pa_cli_command_log_backtrace, "Show backtrace in log messages (args: frames)", 2}, { "set-log-backtrace", pa_cli_command_log_backtrace, "Show backtrace in log messages (args: frames)", 2},
{ "send-message", pa_cli_command_send_message_to_object, "Send a message to an object (args: recipient, message, message_parameters)", 4},
{ "play-file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3}, { "play-file", pa_cli_command_play_file, "Play a sound file (args: filename, sink|index)", 3},
{ "dump", pa_cli_command_dump, "Dump daemon configuration", 1}, { "dump", pa_cli_command_dump, "Dump daemon configuration", 1},
{ "dump-volumes", pa_cli_command_dump_volumes, "Debug: Show the state of all volumes", 1 }, { "dump-volumes", pa_cli_command_dump_volumes, "Debug: Show the state of all volumes", 1 },
@ -1784,6 +1787,47 @@ static int pa_cli_command_port_offset(pa_core *c, pa_tokenizer *t, pa_strbuf *bu
return 0; return 0;
} }
static int pa_cli_command_send_message_to_object(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) {
const char *object_path, *message, *message_parameters;
char *response = NULL;
int ret;
pa_core_assert_ref(c);
pa_assert(t);
pa_assert(buf);
pa_assert(fail);
if (!(object_path = pa_tokenizer_get(t, 1))) {
pa_strbuf_puts(buf, "You need to specify an object path as recipient for the message.\n");
return -1;
}
if (!(message = pa_tokenizer_get(t, 2))) {
pa_strbuf_puts(buf, "You need to specify a message name.\n");
return -1;
}
/* parameters may be NULL */
message_parameters = pa_tokenizer_get(t, 3);
ret = pa_message_handler_send_message(c, object_path, message, message_parameters, &response);
if (ret < 0) {
pa_strbuf_printf(buf, "Send message failed: %s\n", pa_strerror(ret));
ret = -1;
} else {
if (response)
pa_strbuf_puts(buf, response);
pa_strbuf_puts(buf, "\n");
ret = 0;
}
pa_xfree(response);
return ret;
}
static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) { static int pa_cli_command_dump(pa_core *c, pa_tokenizer *t, pa_strbuf *buf, bool *fail) {
pa_module *m; pa_module *m;
pa_sink *sink; pa_sink *sink;

View file

@ -77,6 +77,7 @@ static void help(const char *argv0) {
printf("%s %s %s\n", argv0, "set-log-meta", _("1|0")); printf("%s %s %s\n", argv0, "set-log-meta", _("1|0"));
printf("%s %s %s\n", argv0, "set-log-time", _("1|0")); printf("%s %s %s\n", argv0, "set-log-time", _("1|0"));
printf("%s %s %s\n", argv0, "set-log-backtrace", _("FRAMES")); printf("%s %s %s\n", argv0, "set-log-backtrace", _("FRAMES"));
printf("%s %s %s\n", argv0, "send-message", _("RECIPIENT MESSAGE [MESSAGE_PARAMETERS]"));
printf(_("\n" printf(_("\n"
" -h, --help Show this help\n" " -h, --help Show this help\n"

View file

@ -56,7 +56,10 @@ static char
*card_name = NULL, *card_name = NULL,
*profile_name = NULL, *profile_name = NULL,
*port_name = NULL, *port_name = NULL,
*formats = NULL; *formats = NULL,
*object_path = NULL,
*message = NULL,
*message_args = NULL;
static uint32_t static uint32_t
sink_input_idx = PA_INVALID_INDEX, sink_input_idx = PA_INVALID_INDEX,
@ -130,6 +133,7 @@ static enum {
SET_SOURCE_OUTPUT_MUTE, SET_SOURCE_OUTPUT_MUTE,
SET_SINK_FORMATS, SET_SINK_FORMATS,
SET_PORT_LATENCY_OFFSET, SET_PORT_LATENCY_OFFSET,
SEND_MESSAGE,
SUBSCRIBE SUBSCRIBE
} action = NONE; } action = NONE;
@ -878,6 +882,19 @@ static void index_callback(pa_context *c, uint32_t idx, void *userdata) {
complete_action(); complete_action();
} }
static void send_message_callback(pa_context *c, int success, const char *response, void *userdata) {
if (!success) {
pa_log(_("Send message failed: %s"), pa_strerror(pa_context_errno(c)));
quit(1);
return;
}
printf("%s\n", response);
complete_action();
}
static void volume_relative_adjust(pa_cvolume *cv) { static void volume_relative_adjust(pa_cvolume *cv) {
pa_assert(volume_flags & VOL_RELATIVE); pa_assert(volume_flags & VOL_RELATIVE);
@ -1450,6 +1467,10 @@ static void context_state_callback(pa_context *c, void *userdata) {
o = pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL); o = pa_context_set_port_latency_offset(c, card_name, port_name, latency_offset, simple_callback, NULL);
break; break;
case SEND_MESSAGE:
o = pa_context_send_message_to_object(c, object_path, message, message_args, send_message_callback, NULL);
break;
case SUBSCRIBE: case SUBSCRIBE:
pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL); pa_context_set_subscribe_callback(c, context_subscribe_callback, NULL);
@ -1626,6 +1647,7 @@ static void help(const char *argv0) {
printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle")); printf("%s %s %s %s\n", argv0, _("[options]"), "set-(sink-input|source-output)-mute", _("#N 1|0|toggle"));
printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS")); printf("%s %s %s %s\n", argv0, _("[options]"), "set-sink-formats", _("#N FORMATS"));
printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET")); printf("%s %s %s %s\n", argv0, _("[options]"), "set-port-latency-offset", _("CARD-NAME|CARD-#N PORT OFFSET"));
printf("%s %s %s %s\n", argv0, _("[options]"), "send-message", _("RECIPIENT MESSAGE [MESSAGE_PARAMETERS]"));
printf("%s %s %s\n", argv0, _("[options]"), "subscribe"); printf("%s %s %s\n", argv0, _("[options]"), "subscribe");
printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n" printf(_("\nThe special names @DEFAULT_SINK@, @DEFAULT_SOURCE@ and @DEFAULT_MONITOR@\n"
"can be used to specify the default sink, source and monitor.\n")); "can be used to specify the default sink, source and monitor.\n"));
@ -2061,6 +2083,22 @@ int main(int argc, char *argv[]) {
goto quit; goto quit;
} }
} else if (pa_streq(argv[optind], "send-message")) {
action = SEND_MESSAGE;
if (argc < optind+3) {
pa_log(_("You have to specify at least an object path and a message name"));
goto quit;
}
object_path = pa_xstrdup(argv[optind + 1]);
message = pa_xstrdup(argv[optind + 2]);
if (argc >= optind+4)
message_args = pa_xstrdup(argv[optind + 3]);
if (argc > optind+4)
pa_log(_("Excess arguments given, they will be ignored. Note that all message parameters must be given as a single string."));
} else if (pa_streq(argv[optind], "subscribe")) } else if (pa_streq(argv[optind], "subscribe"))
action = SUBSCRIBE; action = SUBSCRIBE;
@ -2154,6 +2192,9 @@ quit:
pa_xfree(profile_name); pa_xfree(profile_name);
pa_xfree(port_name); pa_xfree(port_name);
pa_xfree(formats); pa_xfree(formats);
pa_xfree(object_path);
pa_xfree(message);
pa_xfree(message_args);
if (sndfile) if (sndfile)
sf_close(sndfile); sf_close(sndfile);