core_proxy: prepare to rename pw_remote -> pw_core_proxy

The pw_remote object is really a wrapper around the pw_core_proxy.
The events it emits are also available in the core proxy and are
generally awkward to use.

With some clever new pw_core_proxy_* methods and a pw_core_connect
to create the core_proxy, we can convert all code away from pw_remote.

This is a first step in this conversion, using the pw_remote behind
the scenes. It leaks into some places because it really needs to become
its own struct in a next step.
This commit is contained in:
Wim Taymans 2019-12-06 11:48:40 +01:00
parent f8aabe69fe
commit 8a959ea7a1
37 changed files with 919 additions and 1185 deletions

View file

@ -28,6 +28,7 @@
#include <signal.h>
#include <string.h>
#include <spa/utils/result.h>
#include <spa/debug/pod.h>
#include <spa/debug/format.h>
@ -71,12 +72,11 @@ struct remote_data {
char *name;
uint32_t id;
struct pw_remote *remote;
struct spa_hook remote_listener;
int prompt_pending;
struct pw_core_proxy *core_proxy;
struct spa_hook core_listener;
struct spa_hook core_proxy_listener;
struct pw_registry_proxy *registry_proxy;
struct spa_hook registry_listener;
@ -370,7 +370,27 @@ static const struct pw_registry_proxy_events registry_events = {
.global_remove = registry_event_global_remove,
};
static void on_remote_destroy(void *_data)
static void on_core_error(void *_data, uint32_t id, int seq, int res, const char *message)
{
struct remote_data *rd = _data;
struct data *data = rd->data;
pw_log_error("remote %p: error id:%u seq:%d res:%d (%s): %s", rd,
id, seq, res, spa_strerror(res), message);
if (id == 0) {
pw_main_loop_quit(data->loop);
}
}
static const struct pw_core_proxy_events remote_core_events = {
PW_VERSION_CORE_PROXY_EVENTS,
.info = on_core_info,
.done = on_core_done,
.error = on_core_error,
};
static void on_core_destroy(void *_data)
{
struct remote_data *rd = _data;
struct data *data = rd->data;
@ -385,78 +405,49 @@ static void on_remote_destroy(void *_data)
free(rd->name);
}
static const struct pw_core_proxy_events remote_core_events = {
PW_VERSION_CORE_EVENTS,
.info = on_core_info,
.done = on_core_done,
static const struct pw_proxy_events core_proxy_events = {
PW_VERSION_PROXY_EVENTS,
.destroy = on_core_destroy,
};
static void on_state_changed(void *_data, enum pw_remote_state old,
enum pw_remote_state state, const char *error)
{
struct remote_data *rd = _data;
struct data *data = rd->data;
switch (state) {
case PW_REMOTE_STATE_ERROR:
fprintf(stderr, "remote %d error: %s\n", rd->id, error);
pw_main_loop_quit(data->loop);
break;
case PW_REMOTE_STATE_CONNECTED:
fprintf(stdout, "remote %d state: \"%s\"\n", rd->id, pw_remote_state_as_string(state));
rd->core_proxy = pw_remote_get_core_proxy(rd->remote);
pw_core_proxy_add_listener(rd->core_proxy,
&rd->core_listener,
&remote_core_events, rd);
rd->registry_proxy = pw_core_proxy_get_registry(rd->core_proxy,
PW_VERSION_REGISTRY_PROXY, 0);
pw_registry_proxy_add_listener(rd->registry_proxy,
&rd->registry_listener,
&registry_events, rd);
rd->prompt_pending = pw_core_proxy_sync(rd->core_proxy, 0, 0);
break;
default:
fprintf(stdout, "remote %d state: \"%s\"\n", rd->id, pw_remote_state_as_string(state));
break;
}
}
static const struct pw_remote_events remote_events = {
PW_VERSION_REMOTE_EVENTS,
.destroy = on_remote_destroy,
.state_changed = on_state_changed,
};
static bool do_connect(struct data *data, const char *cmd, char *args, char **error)
{
char *a[1];
int n;
struct pw_remote *remote;
struct pw_properties *props = NULL;
struct pw_core_proxy *core_proxy;
struct remote_data *rd;
n = pw_split_ip(args, WHITESPACE, 1, a);
if (n == 1) {
props = pw_properties_new(PW_KEY_REMOTE_NAME, a[0], NULL);
}
remote = pw_remote_new(data->core, props, sizeof(struct remote_data));
core_proxy = pw_core_connect(data->core, props, sizeof(struct remote_data));
if (core_proxy == NULL)
return false;
rd = pw_remote_get_user_data(remote);
rd->remote = remote;
rd = pw_proxy_get_user_data((struct pw_proxy*)core_proxy);
rd->core_proxy = core_proxy;
rd->data = data;
pw_map_init(&rd->globals, 64, 16);
rd->id = pw_map_insert_new(&data->vars, rd);
spa_list_append(&data->remotes, &rd->link);
fprintf(stdout, "%d = @remote:%p\n", rd->id, remote);
fprintf(stdout, "%d = @remote:%p\n", rd->id, rd->core_proxy);
data->current = rd;
pw_remote_add_listener(remote, &rd->remote_listener, &remote_events, rd);
if (pw_remote_connect(remote) < 0)
return false;
pw_core_proxy_add_listener(rd->core_proxy,
&rd->core_listener,
&remote_core_events, rd);
pw_proxy_add_listener((struct pw_proxy*)rd->core_proxy,
&rd->core_proxy_listener,
&core_proxy_events, rd);
rd->registry_proxy = pw_core_proxy_get_registry(rd->core_proxy,
PW_VERSION_REGISTRY_PROXY, 0);
pw_registry_proxy_add_listener(rd->registry_proxy,
&rd->registry_listener,
&registry_events, rd);
rd->prompt_pending = pw_core_proxy_sync(rd->core_proxy, 0, 0);
return true;
}
@ -476,8 +467,7 @@ static bool do_disconnect(struct data *data, const char *cmd, char *args, char *
goto no_remote;
}
pw_remote_disconnect(rd->remote);
pw_remote_destroy(rd->remote);
pw_core_proxy_disconnect(rd->core_proxy);
if (data->current == NULL) {
if (spa_list_is_empty(&data->remotes)) {
@ -498,7 +488,7 @@ static bool do_list_remotes(struct data *data, const char *cmd, char *args, char
struct remote_data *rd;
spa_list_for_each(rd, &data->remotes, link)
fprintf(stdout, "\t%d = @remote:%p '%s'\n", rd->id, rd->remote, rd->name);
fprintf(stdout, "\t%d = @remote:%p '%s'\n", rd->id, rd->core_proxy, rd->name);
return true;
}
@ -1428,7 +1418,7 @@ static bool do_export_node(struct data *data, const char *cmd, char *args, char
return false;
}
node = pw_global_get_object(global);
proxy = pw_remote_export(rd->remote, PW_TYPE_INTERFACE_Node, NULL, node, 0);
proxy = pw_core_proxy_export(rd->core_proxy, PW_TYPE_INTERFACE_Node, NULL, node, 0);
id = pw_map_insert_new(&data->vars, proxy);
fprintf(stdout, "%d = @proxy:%d\n", id, pw_proxy_get_id((struct pw_proxy*)proxy));

View file

@ -26,6 +26,7 @@
#include <signal.h>
#include <getopt.h>
#include <spa/utils/result.h>
#include <spa/debug/pod.h>
#include <spa/debug/format.h>
#include <spa/debug/types.h>
@ -48,9 +49,6 @@ struct data {
struct pw_main_loop *loop;
struct pw_core *core;
struct pw_remote *remote;
struct spa_hook remote_listener;
struct pw_core_proxy *core_proxy;
struct spa_hook core_listener;
@ -711,42 +709,22 @@ static void on_core_done(void *data, uint32_t id, int seq)
pw_main_loop_quit(d->loop);
}
static const struct pw_core_proxy_events core_events = {
PW_VERSION_CORE_EVENTS,
.done = on_core_done,
};
static void on_state_changed(void *_data, enum pw_remote_state old,
enum pw_remote_state state, const char *error)
static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
{
struct data *data = _data;
struct data *d = data;
switch (state) {
case PW_REMOTE_STATE_ERROR:
printf("remote error: %s\n", error);
pw_main_loop_quit(data->loop);
break;
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
id, seq, res, spa_strerror(res), message);
case PW_REMOTE_STATE_CONNECTED:
data->core_proxy = pw_remote_get_core_proxy(data->remote);
pw_core_proxy_add_listener(data->core_proxy,
&data->core_listener,
&core_events, data);
data->registry_proxy = pw_core_proxy_get_registry(data->core_proxy,
PW_VERSION_REGISTRY_PROXY, 0);
pw_registry_proxy_add_listener(data->registry_proxy,
&data->registry_listener,
&registry_events, data);
break;
default:
break;
if (id == 0) {
pw_main_loop_quit(d->loop);
}
}
static const struct pw_remote_events remote_events = {
PW_VERSION_REMOTE_EVENTS,
.state_changed = on_state_changed,
static const struct pw_core_proxy_events core_events = {
PW_VERSION_CORE_PROXY_EVENTS,
.done = on_core_done,
.error = on_core_error,
};
static void do_quit(void *data, int signal_number)
@ -843,12 +821,8 @@ int main(int argc, char *argv[])
if (remote_name)
props = pw_properties_new(PW_KEY_REMOTE_NAME, remote_name, NULL);
data.remote = pw_remote_new(data.core, props, 0);
if (data.remote == NULL)
return -1;
pw_remote_add_listener(data.remote, &data.remote_listener, &remote_events, &data);
if (pw_remote_connect(data.remote) < 0)
data.core_proxy = pw_core_connect(data.core, props, 0);
if (data.core_proxy == NULL)
return -1;
data.dot_str = dot_str_new();
@ -857,12 +831,20 @@ int main(int argc, char *argv[])
spa_list_init(&data.globals);
pw_core_proxy_add_listener(data.core_proxy,
&data.core_listener,
&core_events, &data);
data.registry_proxy = pw_core_proxy_get_registry(data.core_proxy,
PW_VERSION_REGISTRY_PROXY, 0);
pw_registry_proxy_add_listener(data.registry_proxy,
&data.registry_listener,
&registry_events, &data);
pw_main_loop_run(data.loop);
draw_graph(&data, dot_path);
dot_str_clear(&data.dot_str);
pw_remote_destroy(data.remote);
pw_core_destroy(data.core);
pw_main_loop_destroy(data.loop);

View file

@ -25,13 +25,13 @@
#include <stdio.h>
#include <signal.h>
#include <spa/utils/result.h>
#include <spa/debug/pod.h>
#include <spa/debug/format.h>
#include <spa/debug/types.h>
#include <pipewire/interfaces.h>
#include <pipewire/type.h>
#include <pipewire/remote.h>
#include <pipewire/main-loop.h>
#include <pipewire/pipewire.h>
@ -50,9 +50,6 @@ struct data {
struct pw_main_loop *loop;
struct pw_core *core;
struct pw_remote *remote;
struct spa_hook remote_listener;
struct pw_core_proxy *core_proxy;
struct spa_hook core_listener;
@ -670,46 +667,22 @@ static const struct pw_registry_proxy_events registry_events = {
.global_remove = registry_event_global_remove,
};
static const struct pw_core_proxy_events core_events = {
PW_VERSION_CORE_EVENTS,
.info = on_core_info,
.done = on_core_done,
};
static void on_state_changed(void *_data, enum pw_remote_state old,
enum pw_remote_state state, const char *error)
static void on_core_error(void *_data, uint32_t id, int seq, int res, const char *message)
{
struct data *data = _data;
switch (state) {
case PW_REMOTE_STATE_ERROR:
printf("remote error: %s\n", error);
pw_log_error("error id:%u seq:%d res:%d (%s): %s",
id, seq, res, spa_strerror(res), message);
if (id == 0) {
pw_main_loop_quit(data->loop);
break;
case PW_REMOTE_STATE_CONNECTED:
printf("remote state: \"%s\"\n", pw_remote_state_as_string(state));
data->core_proxy = pw_remote_get_core_proxy(data->remote);
pw_core_proxy_add_listener(data->core_proxy,
&data->core_listener,
&core_events, data);
data->registry_proxy = pw_core_proxy_get_registry(data->core_proxy,
PW_VERSION_REGISTRY_PROXY, 0);
pw_registry_proxy_add_listener(data->registry_proxy,
&data->registry_listener,
&registry_events, data);
break;
default:
printf("remote state: \"%s\"\n", pw_remote_state_as_string(state));
break;
}
}
static const struct pw_remote_events remote_events = {
PW_VERSION_REMOTE_EVENTS,
.state_changed = on_state_changed,
static const struct pw_core_proxy_events core_events = {
PW_VERSION_CORE_PROXY_EVENTS,
.info = on_core_info,
.done = on_core_done,
.error = on_core_error,
};
static void do_quit(void *data, int signal_number)
@ -741,19 +714,23 @@ int main(int argc, char *argv[])
if (argc > 1)
props = pw_properties_new(PW_KEY_REMOTE_NAME, argv[1], NULL);
data.remote = pw_remote_new(data.core, props, 0);
if (data.remote == NULL)
return -1;
pw_remote_add_listener(data.remote, &data.remote_listener, &remote_events, &data);
if (pw_remote_connect(data.remote) < 0)
return -1;
spa_list_init(&data.pending_list);
data.core_proxy = pw_core_connect(data.core, props, 0);
if (data.core_proxy == NULL)
return -1;
pw_core_proxy_add_listener(data.core_proxy,
&data.core_listener,
&core_events, &data);
data.registry_proxy = pw_core_proxy_get_registry(data.core_proxy,
PW_VERSION_REGISTRY_PROXY, 0);
pw_registry_proxy_add_listener(data.registry_proxy,
&data.registry_listener,
&registry_events, &data);
pw_main_loop_run(data.loop);
pw_remote_destroy(data.remote);
pw_core_destroy(data.core);
pw_main_loop_destroy(data.loop);