1 /* PipeWire
2  *
3  * Copyright © 2020 Wim Taymans
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the next
13  * paragraph) shall be included in all copies or substantial portions of the
14  * Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  */
24 
25 #include <stdio.h>
26 #include <signal.h>
27 #include <math.h>
28 #include <getopt.h>
29 
30 #include <spa/utils/result.h>
31 #include <spa/utils/string.h>
32 #include <spa/utils/defs.h>
33 
34 #include <pipewire/pipewire.h>
35 #include <pipewire/filter.h>
36 #include <pipewire/extensions/metadata.h>
37 
38 struct data {
39 	struct pw_main_loop *loop;
40 
41 	const char *opt_remote;
42 	const char *opt_name;
43 	bool opt_monitor;
44 	bool opt_delete;
45 	uint32_t opt_id;
46 	const char *opt_key;
47 	const char *opt_value;
48 	const char *opt_type;
49 
50 	struct pw_context *context;
51 
52 	struct pw_core *core;
53 	struct spa_hook core_listener;
54 
55 	struct pw_registry *registry;
56 	struct spa_hook registry_listener;
57 
58 	struct pw_metadata *metadata;
59 	struct spa_hook metadata_listener;
60 
61 	int sync;
62 };
63 
64 
metadata_property(void * data,uint32_t id,const char * key,const char * type,const char * value)65 static int metadata_property(void *data, uint32_t id,
66 		const char *key, const char *type, const char *value)
67 {
68 	struct data *d = data;
69 
70 	if ((d->opt_id == SPA_ID_INVALID || d->opt_id == id) &&
71 	    (d->opt_key == NULL || spa_streq(d->opt_key, key))) {
72 		if (key == NULL) {
73 			fprintf(stdout, "remove: id:%u all keys\n", id);
74 		} else if (value == NULL) {
75 			fprintf(stdout, "remove: id:%u key:'%s'\n", id, key);
76 		} else {
77 			fprintf(stdout, "update: id:%u key:'%s' value:'%s' type:'%s'\n", id, key, value, type);
78 		}
79 	}
80 
81 	return 0;
82 }
83 
84 static const struct pw_metadata_events metadata_events = {
85 	PW_VERSION_METADATA_EVENTS,
86 	.property = metadata_property
87 };
88 
registry_event_global(void * data,uint32_t id,uint32_t permissions,const char * type,uint32_t version,const struct spa_dict * props)89 static void registry_event_global(void *data, uint32_t id, uint32_t permissions,
90 				  const char *type, uint32_t version,
91 				  const struct spa_dict *props)
92 {
93 	struct data *d = data;
94 	const char *str;
95 
96 	if (!spa_streq(type, PW_TYPE_INTERFACE_Metadata))
97 		return;
98 
99 	if ((str = spa_dict_lookup(props, PW_KEY_METADATA_NAME)) != NULL &&
100 	    !spa_streq(str, d->opt_name))
101 		return;
102 
103 	if (d->metadata != NULL) {
104 		pw_log_warn("Multiple metadata: ignoring metadata %d", id);
105 		return;
106 	}
107 
108 	fprintf(stdout, "Found \"%s\" metadata %d\n", d->opt_name, id);
109 	d->metadata = pw_registry_bind(d->registry,
110 			id, type, PW_VERSION_METADATA, 0);
111 
112 	if (d->opt_delete) {
113 		if (d->opt_id != SPA_ID_INVALID) {
114 			if (d->opt_key != NULL)
115 				fprintf(stdout, "delete property: id:%u key:%s\n", d->opt_id, d->opt_key);
116 			else
117 				fprintf(stdout, "delete properties: id:%u\n", d->opt_id);
118 			pw_metadata_set_property(d->metadata, d->opt_id, d->opt_key, NULL, NULL);
119 		} else {
120 			fprintf(stdout, "delete all properties\n");
121 			pw_metadata_clear(d->metadata);
122 		}
123 	} else if (d->opt_id != SPA_ID_INVALID && d->opt_key != NULL && d->opt_value != NULL) {
124 		fprintf(stdout, "set property: id:%u key:%s value:%s type:%s\n",
125 				d->opt_id, d->opt_key, d->opt_value, d->opt_type);
126 		pw_metadata_set_property(d->metadata, d->opt_id, d->opt_key, d->opt_type, d->opt_value);
127 	} else {
128 		pw_metadata_add_listener(d->metadata,
129 				&d->metadata_listener,
130 				&metadata_events, d);
131 	}
132 
133 	d->sync = pw_core_sync(d->core, PW_ID_CORE, d->sync);
134 }
135 
136 
137 static const struct pw_registry_events registry_events = {
138 	PW_VERSION_REGISTRY_EVENTS,
139 	.global = registry_event_global,
140 };
141 
on_core_done(void * data,uint32_t id,int seq)142 static void on_core_done(void *data, uint32_t id, int seq)
143 {
144 	struct data *d = data;
145 	if (d->sync == seq && !d->opt_monitor)
146 		pw_main_loop_quit(d->loop);
147 }
148 
on_core_error(void * data,uint32_t id,int seq,int res,const char * message)149 static void on_core_error(void *data, uint32_t id, int seq, int res, const char *message)
150 {
151 	struct data *d = data;
152 
153 	pw_log_error("error id:%u seq:%d res:%d (%s): %s",
154 			id, seq, res, spa_strerror(res), message);
155 
156 	if (id == PW_ID_CORE && res == -EPIPE)
157 		pw_main_loop_quit(d->loop);
158 }
159 
160 static const struct pw_core_events core_events = {
161 	PW_VERSION_CORE_EVENTS,
162 	.done = on_core_done,
163 	.error = on_core_error,
164 };
165 
do_quit(void * userdata,int signal_number)166 static void do_quit(void *userdata, int signal_number)
167 {
168 	struct data *data = userdata;
169 	pw_main_loop_quit(data->loop);
170 }
171 
show_help(struct data * data,const char * name)172 static void show_help(struct data *data, const char *name)
173 {
174         fprintf(stdout, "%s [options] [ id [ key [ value [ type ] ] ] ]\n"
175 		"  -h, --help                            Show this help\n"
176 		"      --version                         Show version\n"
177 		"  -r, --remote                          Remote daemon name\n"
178 		"  -m, --monitor                         Monitor metadata\n"
179 		"  -d, --delete                          Delete metadata\n"
180 		"  -n, --name                            Metadata name (default: \"%s\")\n",
181 		name, data->opt_name);
182 }
183 
main(int argc,char * argv[])184 int main(int argc, char *argv[])
185 {
186 	struct data data = { 0, };
187 	int res = 0, c;
188 	static const struct option long_options[] = {
189 		{ "help",	no_argument,		NULL, 'h' },
190 		{ "version",	no_argument,		NULL, 'V' },
191 		{ "remote",	required_argument,	NULL, 'r' },
192 		{ "monitor",	no_argument,		NULL, 'm' },
193 		{ "delete",	no_argument,		NULL, 'd' },
194 		{ "name",	required_argument,	NULL, 'n' },
195 		{ NULL,	0, NULL, 0}
196 	};
197 
198 	pw_init(&argc, &argv);
199 
200 	data.opt_name = "default";
201 
202 	while ((c = getopt_long(argc, argv, "hVr:mdn:", long_options, NULL)) != -1) {
203 		switch (c) {
204 		case 'h':
205 			show_help(&data, argv[0]);
206 			return 0;
207 		case 'V':
208 			fprintf(stdout, "%s\n"
209 				"Compiled with libpipewire %s\n"
210 				"Linked with libpipewire %s\n",
211 				argv[0],
212 				pw_get_headers_version(),
213 				pw_get_library_version());
214 			return 0;
215 		case 'r':
216 			data.opt_remote = optarg;
217 			break;
218 		case 'm':
219 			data.opt_monitor = true;
220 			break;
221 		case 'd':
222 			data.opt_delete = true;
223 			break;
224 		case 'n':
225 			data.opt_name = optarg;
226 			break;
227 		default:
228 			show_help(&data, argv[0]);
229 			return -1;
230 		}
231 	}
232 
233 	data.opt_id = SPA_ID_INVALID;
234 	if (optind < argc)
235 		data.opt_id = atoi(argv[optind++]);
236 	if (optind < argc)
237 		data.opt_key = argv[optind++];
238 	if (optind < argc)
239 		data.opt_value = argv[optind++];
240 	if (optind < argc)
241 		data.opt_type = argv[optind++];
242 
243 	data.loop = pw_main_loop_new(NULL);
244 	if (data.loop == NULL) {
245 		fprintf(stderr, "can't create mainloop: %m\n");
246 		return -1;
247 	}
248 	pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGINT, do_quit, &data);
249 	pw_loop_add_signal(pw_main_loop_get_loop(data.loop), SIGTERM, do_quit, &data);
250 
251 	data.context = pw_context_new(pw_main_loop_get_loop(data.loop), NULL, 0);
252 	if (data.context == NULL) {
253 		fprintf(stderr, "can't create context: %m\n");
254 		return -1;
255 	}
256 
257 	data.core = pw_context_connect(data.context,
258 			pw_properties_new(
259 				PW_KEY_REMOTE_NAME, data.opt_remote,
260 				NULL),
261 			0);
262 	if (data.core == NULL) {
263 		fprintf(stderr, "can't connect: %m\n");
264 		return -1;
265 	}
266 
267 	pw_core_add_listener(data.core,
268 			&data.core_listener,
269 			&core_events, &data);
270 
271 	data.registry = pw_core_get_registry(data.core,
272 			PW_VERSION_REGISTRY, 0);
273 	pw_registry_add_listener(data.registry,
274 			&data.registry_listener,
275 			&registry_events, &data);
276 
277 	data.sync = pw_core_sync(data.core, PW_ID_CORE, data.sync);
278 
279 	pw_main_loop_run(data.loop);
280 
281 	if (data.metadata)
282 		pw_proxy_destroy((struct pw_proxy*)data.metadata);
283 	pw_proxy_destroy((struct pw_proxy*)data.registry);
284 	pw_core_disconnect(data.core);
285 	pw_context_destroy(data.context);
286 	pw_main_loop_destroy(data.loop);
287 	pw_deinit();
288 
289 	return res;
290 }
291