protocol-native: add message sending capability

This patch adds the PA_COMMAND_SEND_OBJECT_MESSAGE command to protocol-native
so that clients can use the messaging feature introduced in the previous patch.

Sending messages can in effect replace the extension system for modules. The
approach is more flexible than the extension interface because a generic string
format is used to exchange information. Furthermore the messaging system can be
used for any object, not only for modules, and is easier to implement than
extensions.

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

View file

@ -2205,3 +2205,67 @@ pa_operation* pa_context_suspend_source_by_index(pa_context *c, uint32_t idx, in
return o;
}
/** Object response string processing **/
static void context_string_callback(pa_pdispatch *pd, uint32_t command, uint32_t tag, pa_tagstruct *t, void *userdata) {
pa_operation *o = userdata;
const char *response;
int success = 1;
pa_assert(pd);
pa_assert(o);
pa_assert(PA_REFCNT_VALUE(o) >= 1);
if (!o->context)
goto finish;
if (command != PA_COMMAND_REPLY) {
if (pa_context_handle_error(o->context, command, t, false) < 0)
goto finish;
success = 0;
response = "";
} else if (pa_tagstruct_gets(t, &response) < 0 ||
!pa_tagstruct_eof(t)) {
pa_context_fail(o->context, PA_ERR_PROTOCOL);
goto finish;
}
if (!response)
response = "";
if (o->callback) {
pa_context_string_cb_t cb = (pa_context_string_cb_t) o->callback;
cb(o->context, success, response, o->userdata);
}
finish:
pa_operation_done(o);
pa_operation_unref(o);
}
pa_operation* pa_context_send_message_to_object(pa_context *c, const char *object_path, const char *message, const char *message_parameters, pa_context_string_cb_t cb, void *userdata) {
pa_operation *o;
pa_tagstruct *t;
uint32_t tag;
pa_assert(c);
pa_assert(PA_REFCNT_VALUE(c) >= 1);
PA_CHECK_VALIDITY_RETURN_NULL(c, !pa_detect_fork(), PA_ERR_FORKED);
PA_CHECK_VALIDITY_RETURN_NULL(c, c->state == PA_CONTEXT_READY, PA_ERR_BADSTATE);
o = pa_operation_new(c, NULL, (pa_operation_cb_t) cb, userdata);
t = pa_tagstruct_command(c, PA_COMMAND_SEND_OBJECT_MESSAGE, &tag);
pa_tagstruct_puts(t, object_path);
pa_tagstruct_puts(t, message);
pa_tagstruct_puts(t, message_parameters);
pa_pstream_send_tagstruct(c->pstream, t);
pa_pdispatch_register_reply(c->pdispatch, tag, DEFAULT_TIMEOUT, context_string_callback, pa_operation_ref(o), (pa_free_cb_t) pa_operation_unref);
return o;
}

View file

@ -204,6 +204,12 @@
* Server modules can be remotely loaded and unloaded using
* pa_context_load_module() and pa_context_unload_module().
*
* \subsection message_subsec Messages
*
* Server objects like sinks, sink inputs or modules can register a message
* handler to communicate with clients. A message can be sent to a named
* message handler using pa_context_send_message_to_object().
*
* \subsection client_subsec Clients
*
* The only operation supported on clients is the possibility of kicking
@ -489,6 +495,16 @@ pa_operation* pa_context_unload_module(pa_context *c, uint32_t idx, pa_context_s
/** @} */
/** @{ \name Messages */
/** 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);
/** Send a message to an object that registered a message handler. \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);
/** @} */
/** @{ \name Clients */
/** Stores information about clients. Please note that this structure