diff --git a/src/modules/module-protocol-pulse/extension.c b/src/modules/module-protocol-pulse/extension.c index 2bfb564d0..8f3ea225b 100644 --- a/src/modules/module-protocol-pulse/extension.c +++ b/src/modules/module-protocol-pulse/extension.c @@ -27,16 +27,12 @@ #include "extension.h" -static int do_extension_device_restore(struct client *client, uint32_t tag, struct message *m) -{ - return -ENOTSUP; -} - static int do_extension_device_manager(struct client *client, uint32_t tag, struct message *m) { return -ENOTSUP; } +#include "extensions/ext-device-restore.c" #include "extensions/ext-stream-restore.c" static const struct extension extensions[] = { diff --git a/src/modules/module-protocol-pulse/extensions/ext-device-restore.c b/src/modules/module-protocol-pulse/extensions/ext-device-restore.c new file mode 100644 index 000000000..0527d4caf --- /dev/null +++ b/src/modules/module-protocol-pulse/extensions/ext-device-restore.c @@ -0,0 +1,257 @@ +/* PipeWire + * + * Copyright © 2021 Wim Taymans + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice (including the next + * paragraph) shall be included in all copies or substantial portions of the + * Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. + */ + +#define EXT_DEVICE_RESTORE_VERSION 1 + +#include +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "../client.h" +#include "../collect.h" +#include "../defs.h" +#include "../extension.h" +#include "../format.h" +#include "../manager.h" +#include "../media-roles.h" +#include "../message.h" +#include "../reply.h" +#include "../volume.h" + +#define DEVICE_TYPE_SINK 0 +#define DEVICE_TYPE_SOURCE 1 + +static int do_extension_device_restore_test(struct client *client, uint32_t command, uint32_t tag, struct message *m) +{ + struct message *reply; + + reply = reply_new(client, tag); + message_put(reply, + TAG_U32, EXT_DEVICE_RESTORE_VERSION, + TAG_INVALID); + + return client_queue_message(client, reply); +} + +static int do_extension_device_restore_subscribe(struct client *client, uint32_t command, uint32_t tag, struct message *m) +{ + return reply_simple_ack(client, tag); +} + + +struct format_data { + struct client *client; + struct message *reply; +}; + +static int do_sink_read_format(void *data, struct pw_manager_object *o) +{ + struct format_data *d = data; + struct pw_manager_param *p; + struct format_info info[32]; + uint32_t i, n_info = 0; + + if (!pw_manager_object_is_sink(o)) + return 0; + + spa_list_for_each(p, &o->param_list, link) { + uint32_t index = 0; + + if (p->id != SPA_PARAM_EnumFormat) + continue; + + while (n_info < SPA_N_ELEMENTS(info)) { + spa_zero(info[n_info]); + if (format_info_from_param(&info[n_info], p->param, index++) < 0) + break; + if (info[n_info].encoding == ENCODING_ANY) + continue; + n_info++; + } + } + message_put(d->reply, + TAG_U32, DEVICE_TYPE_SINK, + TAG_U32, o->id, + TAG_U8, n_info, /* n_formats */ + TAG_INVALID); + for (i = 0; i < n_info; i++) { + message_put(d->reply, + TAG_FORMAT_INFO, &info[i], + TAG_INVALID); + } + return 0; +} + +static int do_extension_device_restore_read_formats_all(struct client *client, + uint32_t command, uint32_t tag, struct message *m) +{ + struct pw_manager *manager = client->manager; + struct format_data data; + + spa_zero(data); + data.client = client; + data.reply = reply_new(client, tag); + + pw_manager_for_each_object(manager, do_sink_read_format, &data); + + return client_queue_message(client, data.reply); +} + +static int do_extension_device_restore_read_formats(struct client *client, + uint32_t command, uint32_t tag, struct message *m) +{ + struct pw_manager *manager = client->manager; + struct format_data data; + uint32_t type, sink_index; + struct selector sel; + struct pw_manager_object *o; + int res; + + if ((res = message_get(m, + TAG_U32, &type, + TAG_U32, &sink_index, + TAG_INVALID)) < 0) + return -EPROTO; + + if (type != DEVICE_TYPE_SINK) { + pw_log_info("Device format reading is only supported on sinks"); + return -ENOTSUP; + } + + spa_zero(sel); + sel.id = sink_index; + sel.type = pw_manager_object_is_sink; + + o = select_object(manager, &sel); + if (o == NULL) + return -ENOENT; + + spa_zero(data); + data.client = client; + data.reply = reply_new(client, tag); + + do_sink_read_format(&data, o); + + return client_queue_message(client, data.reply); +} + +static int do_extension_device_restore_save_formats(struct client *client, + uint32_t command, uint32_t tag, struct message *m) +{ + struct pw_manager *manager = client->manager; + struct selector sel; + struct pw_manager_object *o; + int res; + uint32_t type, sink_index; + uint8_t i, n_formats; + uint32_t codec, iec958codecs[32]; + uint32_t n_codecs = 0; + char buf[1024]; + struct spa_pod_builder b; + struct spa_pod *param; + + if ((res = message_get(m, + TAG_U32, &type, + TAG_U32, &sink_index, + TAG_U8, &n_formats, + TAG_INVALID)) < 0) + return -EPROTO; + if (n_formats < 1) + return -EPROTO; + + for (i = 0; i < n_formats; ++i) { + struct format_info format; + if (message_get(m, + TAG_FORMAT_INFO, &format, + TAG_INVALID) < 0) + return -EPROTO; + + codec = format_encoding2id(format.encoding); + if (codec != SPA_ID_INVALID && n_codecs < SPA_N_ELEMENTS(iec958codecs)) + iec958codecs[n_codecs++] = codec; + } + if (n_codecs == 0) + return -ENOTSUP; + + spa_zero(sel); + sel.id = sink_index; + sel.type = pw_manager_object_is_sink; + + o = select_object(manager, &sel); + if (o == NULL) + return -ENOENT; + + spa_pod_builder_init(&b, buf, sizeof(buf)); + param = spa_pod_builder_add_object(&b, + SPA_TYPE_OBJECT_Props, SPA_PARAM_Props, + SPA_PROP_iec958Codecs, SPA_POD_Array(sizeof(uint32_t), + SPA_TYPE_Id, n_codecs, iec958codecs)); + + spa_debug_pod(0, NULL, param); + + pw_node_set_param((struct pw_node*)o->proxy, + SPA_PARAM_Props, 0, param); + + return reply_simple_ack(client, tag); +} + +static const struct extension_sub ext_device_restore[] = { + { "TEST", 0, do_extension_device_restore_test, }, + { "SUBSCRIBE", 1, do_extension_device_restore_subscribe, }, + { "EVENT", 2, }, + { "READ_FORMATS_ALL", 3, do_extension_device_restore_read_formats_all, }, + { "READ_FORMATS", 4, do_extension_device_restore_read_formats, }, + { "SAVE_FORMATS", 5, do_extension_device_restore_save_formats, }, +}; + +static int do_extension_device_restore(struct client *client, uint32_t tag, struct message *m) +{ + uint32_t command; + int res; + + if ((res = message_get(m, + TAG_U32, &command, + TAG_INVALID)) < 0) + return -EPROTO; + + if (command >= SPA_N_ELEMENTS(ext_device_restore)) + return -ENOTSUP; + if (ext_device_restore[command].process == NULL) + return -EPROTO; + + pw_log_info("client %p [%s]: EXT_DEVICE_RESTORE_%s tag:%u", + client, client->name, ext_device_restore[command].name, tag); + + return ext_device_restore[command].process(client, command, tag, m); +} diff --git a/src/modules/module-protocol-pulse/format.c b/src/modules/module-protocol-pulse/format.c index 949f64432..216ef98c6 100644 --- a/src/modules/module-protocol-pulse/format.c +++ b/src/modules/module-protocol-pulse/format.c @@ -406,7 +406,7 @@ const char *format_encoding2name(enum encoding enc) return encoding_names[enc].name; return "INVALID"; } -static uint32_t format_encoding2id(enum encoding enc) +uint32_t format_encoding2id(enum encoding enc) { if (enc >= 0 && enc < (int)SPA_N_ELEMENTS(encoding_names) && encoding_names[enc].name != NULL) @@ -632,6 +632,7 @@ static int format_info_iec958_from_param(struct format_info *info, struct spa_po default: return -ENOTSUP; } + if ((info->props = pw_properties_new(NULL, NULL)) == NULL) return -errno; diff --git a/src/modules/module-protocol-pulse/format.h b/src/modules/module-protocol-pulse/format.h index 0acdce349..7f9cf4c93 100644 --- a/src/modules/module-protocol-pulse/format.h +++ b/src/modules/module-protocol-pulse/format.h @@ -189,6 +189,7 @@ uint32_t format_paname2id(const char *name, size_t size); enum sample_format format_id2pa(uint32_t id); const char *format_id2paname(uint32_t id); const char *format_encoding2name(enum encoding enc); +uint32_t format_encoding2id(enum encoding enc); uint32_t sample_spec_frame_size(const struct sample_spec *ss); bool sample_spec_valid(const struct sample_spec *ss);