pipewire/src/tools/pipewire-monitor.c

357 lines
10 KiB
C
Raw Normal View History

2017-05-23 19:15:33 +02:00
/* PipeWire
2015-04-16 16:58:33 +02:00
* Copyright (C) 2015 Wim Taymans <wim.taymans@gmail.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
#include <stdio.h>
#include <spa/lib/debug.h>
2017-07-11 15:57:20 +02:00
#include <pipewire/pipewire.h>
#include <pipewire/sig.h>
2017-05-23 19:15:33 +02:00
struct data {
2017-05-26 08:05:01 +02:00
bool running;
struct pw_loop *loop;
struct pw_core *core;
struct pw_remote *remote;
struct pw_proxy *registry_proxy;
2015-04-16 16:58:33 +02:00
struct pw_listener on_info_changed;
2017-05-26 08:05:01 +02:00
struct pw_listener on_state_changed;
};
struct proxy_data {
void *info;
2017-05-23 19:15:33 +02:00
};
2015-04-16 16:58:33 +02:00
2017-05-26 08:05:01 +02:00
static void print_properties(struct spa_dict *props, char mark)
{
2017-05-26 08:05:01 +02:00
struct spa_dict_item *item;
2017-05-26 08:05:01 +02:00
if (props == NULL)
return;
2017-05-26 08:05:01 +02:00
printf("%c\tproperties:\n", mark);
spa_dict_for_each(item, props) {
printf("%c\t\t%s = \"%s\"\n", mark, item->key, item->value);
}
}
#define MARK_CHANGE(f) ((print_mark && ((info)->change_mask & (1 << (f)))) ? '*' : ' ')
static void on_info_changed(struct pw_listener *listener, struct pw_remote *remote)
{
struct pw_core_info *info = remote->info;
bool print_all = true, print_mark = false;
2017-05-26 08:05:01 +02:00
printf("\tid: %u\n", info->id);
printf("\ttype: %s\n", PIPEWIRE_TYPE__Core);
if (print_all) {
2017-05-26 08:05:01 +02:00
printf("%c\tuser-name: \"%s\"\n", MARK_CHANGE(0), info->user_name);
printf("%c\thost-name: \"%s\"\n", MARK_CHANGE(1), info->host_name);
printf("%c\tversion: \"%s\"\n", MARK_CHANGE(2), info->version);
printf("%c\tname: \"%s\"\n", MARK_CHANGE(3), info->name);
printf("%c\tcookie: %u\n", MARK_CHANGE(4), info->cookie);
print_properties(info->props, MARK_CHANGE(5));
}
2016-12-02 13:38:43 +01:00
}
static void module_event_info(void *object, struct pw_module_info *info)
2016-12-02 13:38:43 +01:00
{
struct pw_proxy *proxy = object;
struct proxy_data *data = proxy->user_data;
bool print_all, print_mark;
2016-12-02 13:38:43 +01:00
print_all = true;
if (data->info == NULL) {
printf("added:\n");
print_mark = false;
}
else {
printf("changed:\n");
print_mark = true;
}
info = data->info = pw_module_info_update(data->info, info);
2016-12-02 13:38:43 +01:00
2017-05-26 08:05:01 +02:00
printf("\tid: %u\n", info->id);
printf("\ttype: %s\n", PIPEWIRE_TYPE__Module);
if (print_all) {
printf("%c\tname: \"%s\"\n", MARK_CHANGE(0), info->name);
printf("%c\tfilename: \"%s\"\n", MARK_CHANGE(1), info->filename);
printf("%c\targs: \"%s\"\n", MARK_CHANGE(2), info->args);
print_properties(info->props, MARK_CHANGE(3));
2017-05-26 08:05:01 +02:00
}
}
static const struct pw_module_events module_events = {
&module_event_info,
};
static void node_event_info(void *object, struct pw_node_info *info)
{
struct pw_proxy *proxy = object;
struct proxy_data *data = proxy->user_data;
bool print_all, print_mark;
2017-05-26 08:05:01 +02:00
print_all = true;
if (data->info == NULL) {
printf("added:\n");
print_mark = false;
2017-05-26 08:05:01 +02:00
}
else {
printf("changed:\n");
print_mark = true;
}
info = data->info = pw_node_info_update(data->info, info);
2017-05-26 08:05:01 +02:00
printf("\tid: %u\n", info->id);
printf("\ttype: %s\n", PIPEWIRE_TYPE__Node);
if (print_all) {
2017-05-26 08:05:01 +02:00
int i;
printf("%c\tname: \"%s\"\n", MARK_CHANGE(0), info->name);
printf("%c\tinput ports: %u/%u\n", MARK_CHANGE(1), info->n_input_ports, info->max_input_ports);
2017-05-26 08:05:01 +02:00
printf("%c\tinput formats:\n", MARK_CHANGE(2));
for (i = 0; i < info->n_input_formats; i++)
spa_debug_format(info->input_formats[i]);
2017-05-26 08:05:01 +02:00
printf("%c\toutput ports: %u/%u\n", MARK_CHANGE(3), info->n_output_ports, info->max_output_ports);
2017-05-26 08:05:01 +02:00
printf("%c\toutput formats:\n", MARK_CHANGE(4));
for (i = 0; i < info->n_output_formats; i++)
spa_debug_format(info->output_formats[i]);
2017-05-26 08:05:01 +02:00
printf("%c\tstate: \"%s\"", MARK_CHANGE(5), pw_node_state_as_string(info->state));
if (info->state == PW_NODE_STATE_ERROR && info->error)
printf(" \"%s\"\n", info->error);
else
printf("\n");
print_properties(info->props, MARK_CHANGE(6));
}
}
static const struct pw_node_events node_events = {
&node_event_info
};
static void client_event_info(void *object, struct pw_client_info *info)
{
struct pw_proxy *proxy = object;
struct proxy_data *data = proxy->user_data;
bool print_all, print_mark;
2017-05-26 08:05:01 +02:00
print_all = true;
if (data->info == NULL) {
printf("added:\n");
print_mark = false;
}
else {
printf("changed:\n");
print_mark = true;
2017-05-26 08:05:01 +02:00
}
info = data->info = pw_client_info_update(data->info, info);
2017-05-26 08:05:01 +02:00
printf("\tid: %u\n", info->id);
printf("\ttype: %s\n", PIPEWIRE_TYPE__Client);
if (print_all) {
print_properties(info->props, MARK_CHANGE(0));
2017-05-26 08:05:01 +02:00
}
}
static const struct pw_client_events client_events = {
&client_event_info
};
static void link_event_info(void *object, struct pw_link_info *info)
{
struct pw_proxy *proxy = object;
struct proxy_data *data = proxy->user_data;
bool print_all, print_mark;
2017-05-26 08:05:01 +02:00
print_all = true;
if (data->info == NULL) {
printf("added:\n");
print_mark = false;
}
else {
printf("changed:\n");
print_mark = true;
2017-05-26 08:05:01 +02:00
}
info = data->info = pw_link_info_update(data->info, info);
2017-05-26 08:05:01 +02:00
printf("\tid: %u\n", info->id);
printf("\ttype: %s\n", PIPEWIRE_TYPE__Link);
if (print_all) {
2017-05-26 08:05:01 +02:00
printf("%c\toutput-node-id: %u\n", MARK_CHANGE(0), info->output_node_id);
printf("%c\toutput-port-id: %u\n", MARK_CHANGE(1), info->output_port_id);
printf("%c\tinput-node-id: %u\n", MARK_CHANGE(2), info->input_node_id);
printf("%c\tinput-port-id: %u\n", MARK_CHANGE(3), info->input_port_id);
printf("%c\tformat:\n", MARK_CHANGE(4));
if (info->format)
spa_debug_format(info->format);
else
printf("\t none\n");
2017-05-26 08:05:01 +02:00
}
}
static const struct pw_link_events link_events = {
&link_event_info
};
static void
destroy_proxy (void *data)
{
struct pw_proxy *proxy = data;
struct proxy_data *user_data = proxy->user_data;
struct pw_core *core = proxy->remote->core;
if (user_data->info == NULL)
return;
if (proxy->type == core->type.core) {
pw_core_info_free (user_data->info);
} else if (proxy->type == core->type.node) {
pw_node_info_free (user_data->info);
} else if (proxy->type == core->type.module) {
pw_module_info_free (user_data->info);
} else if (proxy->type == core->type.client) {
pw_client_info_free (user_data->info);
} else if (proxy->type == core->type.link) {
pw_link_info_free (user_data->info);
}
user_data->info = NULL;
}
2016-12-02 13:38:43 +01:00
static void registry_event_global(void *object, uint32_t id, const char *type, uint32_t version)
{
struct pw_proxy *registry_proxy = object;
struct data *data = registry_proxy->object;
struct pw_core *core = data->core;
struct pw_remote *remote = registry_proxy->remote;
struct pw_proxy *proxy = NULL;
uint32_t proxy_type, client_version;
const void *implementation;
if (!strcmp(type, PIPEWIRE_TYPE__Node)) {
proxy_type = core->type.node;
implementation = &node_events;
client_version = PW_VERSION_NODE;
} else if (!strcmp(type, PIPEWIRE_TYPE__Module)) {
proxy_type = core->type.module;
implementation = &module_events;
client_version = PW_VERSION_MODULE;
} else if (!strcmp(type, PIPEWIRE_TYPE__Client)) {
proxy_type = core->type.client;
implementation = &client_events;
client_version = PW_VERSION_CLIENT;
} else if (!strcmp(type, PIPEWIRE_TYPE__Link)) {
proxy_type = core->type.link;
implementation = &link_events;
client_version = PW_VERSION_LINK;
} else
return;
proxy = pw_proxy_new(remote, SPA_ID_INVALID, proxy_type, sizeof(struct proxy_data));
if (proxy == NULL)
goto no_mem;
pw_proxy_set_implementation(proxy, data, client_version, implementation, destroy_proxy);
pw_registry_do_bind(registry_proxy, id, version, proxy->id);
return;
no_mem:
printf("failed to create proxy");
return;
}
static void registry_event_global_remove(void *object, uint32_t id)
2015-04-16 16:58:33 +02:00
{
printf("removed:\n");
printf("\tid: %u\n", id);
2015-04-16 16:58:33 +02:00
}
static const struct pw_registry_events registry_events = {
registry_event_global,
registry_event_global_remove,
};
static void on_state_changed(struct pw_listener *listener, struct pw_remote *remote)
2015-04-16 16:58:33 +02:00
{
2017-05-26 08:05:01 +02:00
struct data *data = SPA_CONTAINER_OF(listener, struct data, on_state_changed);
switch (remote->state) {
2017-07-11 15:57:20 +02:00
case PW_REMOTE_STATE_ERROR:
printf("remote error: %s\n", remote->error);
2017-05-26 08:05:01 +02:00
data->running = false;
break;
2017-07-11 15:57:20 +02:00
case PW_REMOTE_STATE_CONNECTED:
printf("remote state: \"%s\"\n", pw_remote_state_as_string(remote->state));
data->registry_proxy = pw_proxy_new(data->remote,
SPA_ID_INVALID,
data->core->type.registry,
0);
pw_proxy_set_implementation(data->registry_proxy, data, PW_VERSION_REGISTRY,
&registry_events, NULL);
pw_core_do_get_registry(data->remote->core_proxy,
data->registry_proxy->id);
break;
2017-05-26 08:05:01 +02:00
default:
printf("remote state: \"%s\"\n", pw_remote_state_as_string(remote->state));
2017-05-26 08:05:01 +02:00
break;
}
2015-04-16 16:58:33 +02:00
}
2017-05-26 08:05:01 +02:00
int main(int argc, char *argv[])
2015-04-16 16:58:33 +02:00
{
struct data data = { 0 };
2015-04-16 16:58:33 +02:00
2017-05-26 08:05:01 +02:00
pw_init(&argc, &argv);
2015-04-16 16:58:33 +02:00
2017-05-26 08:05:01 +02:00
data.loop = pw_loop_new();
data.running = true;
data.core = pw_core_new(data.loop, NULL);
data.remote = pw_remote_new(data.core, NULL);
2015-04-16 16:58:33 +02:00
pw_signal_add(&data.remote->info_changed, &data.on_info_changed, on_info_changed);
pw_signal_add(&data.remote->state_changed, &data.on_state_changed, on_state_changed);
pw_remote_connect(data.remote);
2017-05-26 08:05:01 +02:00
pw_loop_enter(data.loop);
while (data.running) {
pw_loop_iterate(data.loop, -1);
}
pw_loop_leave(data.loop);
2015-04-16 16:58:33 +02:00
pw_remote_destroy(data.remote);
2017-05-26 08:05:01 +02:00
pw_loop_destroy(data.loop);
2015-04-16 16:58:33 +02:00
2017-05-26 08:05:01 +02:00
return 0;
2015-04-16 16:58:33 +02:00
}