pulse-server: split out extension handling

Part of !776.
This commit is contained in:
Barnabás Pőcze 2021-06-18 23:51:54 +02:00
parent d366487116
commit c49ae39888
5 changed files with 83 additions and 28 deletions

View file

@ -136,6 +136,7 @@ pipewire_module_protocol_pulse_sources = [
'module-protocol-pulse/client.c', 'module-protocol-pulse/client.c',
'module-protocol-pulse/collect.c', 'module-protocol-pulse/collect.c',
'module-protocol-pulse/dbus-name.c', 'module-protocol-pulse/dbus-name.c',
'module-protocol-pulse/extension.c',
'module-protocol-pulse/format.c', 'module-protocol-pulse/format.c',
'module-protocol-pulse/manager.c', 'module-protocol-pulse/manager.c',
'module-protocol-pulse/media-roles.c', 'module-protocol-pulse/media-roles.c',

View file

@ -22,21 +22,10 @@
* DEALINGS IN THE SOFTWARE. * DEALINGS IN THE SOFTWARE.
*/ */
#include <spa/utils/defs.h>
#include <spa/utils/string.h> #include <spa/utils/string.h>
struct extension_sub { #include "extension.h"
const char *name;
uint32_t command;
int (*process)(struct client *client, uint32_t command, uint32_t tag, struct message *m);
};
struct extension {
const char *name;
uint32_t idx;
int (*process)(struct client *client, uint32_t tag, struct message *m);
};
#include "ext-stream-restore.c"
static int do_extension_device_restore(struct client *client, uint32_t tag, struct message *m) static int do_extension_device_restore(struct client *client, uint32_t tag, struct message *m)
{ {
@ -48,13 +37,15 @@ static int do_extension_device_manager(struct client *client, uint32_t tag, stru
return -ENOTSUP; return -ENOTSUP;
} }
struct extension extensions[] = { #include "extensions/ext-stream-restore.c"
static const struct extension extensions[] = {
{ "module-stream-restore", 0 | EXTENSION_FLAG, do_extension_stream_restore, }, { "module-stream-restore", 0 | EXTENSION_FLAG, do_extension_stream_restore, },
{ "module-device-restore", 1 | EXTENSION_FLAG, do_extension_device_restore, }, { "module-device-restore", 1 | EXTENSION_FLAG, do_extension_device_restore, },
{ "module-device-manager", 2 | EXTENSION_FLAG, do_extension_device_manager, }, { "module-device-manager", 2 | EXTENSION_FLAG, do_extension_device_manager, },
}; };
static struct extension *find_extension(uint32_t idx, const char *name) const struct extension *extension_find(uint32_t idx, const char *name)
{ {
uint32_t i; uint32_t i;
for (i = 0; i < SPA_N_ELEMENTS(extensions); i++) { for (i = 0; i < SPA_N_ELEMENTS(extensions); i++) {

View file

@ -0,0 +1,47 @@
/* PipeWire
*
* Copyright © 2020 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.
*/
#ifndef PULSE_SERVER_EXTENSION_H
#define PULSE_SERVER_EXTENSION_H
#include <stdint.h>
struct client;
struct message;
struct extension_sub {
const char *name;
uint32_t command;
int (*process)(struct client *client, uint32_t command, uint32_t tag, struct message *m);
};
struct extension {
const char *name;
uint32_t idx;
int (*process)(struct client *client, uint32_t tag, struct message *m);
};
const struct extension *extension_find(uint32_t idx, const char *name);
#endif /* PULSE_SERVER_EXTENSION_H */

View file

@ -24,11 +24,28 @@
#define EXT_STREAM_RESTORE_VERSION 1 #define EXT_STREAM_RESTORE_VERSION 1
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <spa/utils/defs.h>
#include <spa/utils/dict.h>
#include <spa/utils/string.h> #include <spa/utils/string.h>
#include <spa/utils/json.h>
#include <pipewire/log.h>
#include <pipewire/properties.h>
#include "media-roles.h" #include "../client.h"
#include "../defs.h"
static const struct extension_sub ext_stream_restore[]; #include "../extension.h"
#include "../format.h"
#include "../manager.h"
#include "../media-roles.h"
#include "../message.h"
#include "../reply.h"
#include "../volume.h"
static int do_extension_stream_restore_test(struct client *client, uint32_t command, uint32_t tag, struct message *m) static int do_extension_stream_restore_test(struct client *client, uint32_t command, uint32_t tag, struct message *m)
{ {
@ -38,6 +55,7 @@ static int do_extension_stream_restore_test(struct client *client, uint32_t comm
message_put(reply, message_put(reply,
TAG_U32, EXT_STREAM_RESTORE_VERSION, TAG_U32, EXT_STREAM_RESTORE_VERSION,
TAG_INVALID); TAG_INVALID);
return client_queue_message(client, reply); return client_queue_message(client, reply);
} }
@ -167,7 +185,7 @@ static int do_extension_stream_restore_read(struct client *client, uint32_t comm
for (map.channels = 0; map.channels < CHANNELS_MAX; map.channels++) { for (map.channels = 0; map.channels < CHANNELS_MAX; map.channels++) {
char chname[16]; char chname[16];
if (spa_json_get_string(&it[2], chname, sizeof(chname)) <= 0) if (spa_json_get_string(&it[2], chname, sizeof(chname)) <= 0)
break; break;
map.map[map.channels] = channel_name2id(chname); map.map[map.channels] = channel_name2id(chname);
} }
@ -187,6 +205,7 @@ static int do_extension_stream_restore_read(struct client *client, uint32_t comm
TAG_BOOLEAN, mute, TAG_BOOLEAN, mute,
TAG_INVALID); TAG_INVALID);
} }
return client_queue_message(client, reply); return client_queue_message(client, reply);
} }
@ -249,13 +268,12 @@ static int do_extension_stream_restore_write(struct client *client, uint32_t com
fprintf(f, ", \"target-node\": \"%s\"", device_name); fprintf(f, ", \"target-node\": \"%s\"", device_name);
fprintf(f, " }"); fprintf(f, " }");
fclose(f); fclose(f);
if (key_from_name(name, key, sizeof(key)) >= 0) { if (key_from_name(name, key, sizeof(key)) >= 0) {
pw_log_debug("%s -> %s: %s", name, key, ptr); pw_log_debug("%s -> %s: %s", name, key, ptr);
if (pw_manager_set_metadata(client->manager, if (pw_manager_set_metadata(client->manager,
client->metadata_routes, client->metadata_routes,
PW_ID_CORE, key, "Spa:String:JSON", "%s", ptr) < 0) PW_ID_CORE, key, "Spa:String:JSON", "%s", ptr) < 0)
pw_log_warn(NAME ": failed to set metadata %s = %s", key, ptr); pw_log_warn("failed to set metadata %s = %s", key, ptr);
} }
free(ptr); free(ptr);
} }
@ -284,7 +302,6 @@ static const struct extension_sub ext_stream_restore[] = {
static int do_extension_stream_restore(struct client *client, uint32_t tag, struct message *m) static int do_extension_stream_restore(struct client *client, uint32_t tag, struct message *m)
{ {
struct impl *impl = client->impl;
uint32_t command; uint32_t command;
int res; int res;
@ -298,8 +315,8 @@ static int do_extension_stream_restore(struct client *client, uint32_t tag, stru
if (ext_stream_restore[command].process == NULL) if (ext_stream_restore[command].process == NULL)
return -EPROTO; return -EPROTO;
pw_log_info(NAME" %p: [%s] EXT_STREAM_RESTORE_%s tag:%u", impl, pw_log_info("client %p [%s]: EXT_STREAM_RESTORE_%s tag:%u",
client->name, ext_stream_restore[command].name, tag); client, client->name, ext_stream_restore[command].name, tag);
return ext_stream_restore[command].process(client, command, tag, m); return ext_stream_restore[command].process(client, command, tag, m);
} }

View file

@ -63,6 +63,7 @@
#include "commands.h" #include "commands.h"
#include "dbus-name.h" #include "dbus-name.h"
#include "defs.h" #include "defs.h"
#include "extension.h"
#include "format.h" #include "format.h"
#include "internal.h" #include "internal.h"
#include "message.h" #include "message.h"
@ -121,8 +122,6 @@ static struct sample *find_sample(struct impl *impl, uint32_t idx, const char *n
return NULL; return NULL;
} }
#include "extension.c"
static void broadcast_subscribe_event(struct impl *impl, uint32_t mask, uint32_t event, uint32_t id) static void broadcast_subscribe_event(struct impl *impl, uint32_t mask, uint32_t event, uint32_t id)
{ {
struct server *s; struct server *s;
@ -4225,7 +4224,7 @@ static int do_extension(struct client *client, uint32_t command, uint32_t tag, s
struct impl *impl = client->impl; struct impl *impl = client->impl;
uint32_t idx; uint32_t idx;
const char *name; const char *name;
struct extension *ext; const struct extension *ext;
if (message_get(m, if (message_get(m,
TAG_U32, &idx, TAG_U32, &idx,
@ -4240,7 +4239,7 @@ static int do_extension(struct client *client, uint32_t command, uint32_t tag, s
(idx != SPA_ID_INVALID && name != NULL)) (idx != SPA_ID_INVALID && name != NULL))
return -EINVAL; return -EINVAL;
ext = find_extension(idx, name); ext = extension_find(idx, name);
if (ext == NULL) if (ext == NULL)
return -ENOENT; return -ENOENT;