Fix up according to Coding Style
[pulseaudio-mirror.git] / src / modules / module-rygel-media-server.c
blob9d23e8a46948a7ac1266f5f4e4b7392ab6c523d1
1 /***
2 This file is part of PulseAudio.
4 Copyright 2005-2009 Lennart Poettering
6 PulseAudio is free software; you can redistribute it and/or modify
7 it under the terms of the GNU Lesser General Public License as published
8 by the Free Software Foundation; either version 2.1 of the License,
9 or (at your option) any later version.
11 PulseAudio is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with PulseAudio; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19 USA.
20 ***/
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <unistd.h>
31 #include <pulse/xmalloc.h>
32 #include <pulse/util.h>
33 #include <pulse/i18n.h>
34 #include <pulse/utf8.h>
36 #include <pulsecore/sink.h>
37 #include <pulsecore/source.h>
38 #include <pulsecore/core-util.h>
39 #include <pulsecore/log.h>
40 #include <pulsecore/modargs.h>
41 #include <pulsecore/dbus-shared.h>
42 #include <pulsecore/endianmacros.h>
43 #include <pulsecore/namereg.h>
44 #include <pulsecore/mime-type.h>
45 #include <pulsecore/strbuf.h>
46 #include <pulsecore/protocol-http.h>
47 #include <pulsecore/parseaddr.h>
49 #include "module-rygel-media-server-symdef.h"
51 PA_MODULE_AUTHOR("Lennart Poettering");
52 PA_MODULE_DESCRIPTION("UPnP MediaServer Plugin for Rygel");
53 PA_MODULE_VERSION(PACKAGE_VERSION);
54 PA_MODULE_LOAD_ONCE(TRUE);
55 PA_MODULE_USAGE("display_name=<UPnP Media Server name>");
57 /* This implements http://live.gnome.org/Rygel/MediaServer2Spec */
59 #define SERVICE_NAME "org.gnome.UPnP.MediaServer2.PulseAudio"
61 #define OBJECT_ROOT "/org/gnome/UPnP/MediaServer2/PulseAudio"
62 #define OBJECT_SINKS "/org/gnome/UPnP/MediaServer2/PulseAudio/Sinks"
63 #define OBJECT_SOURCES "/org/gnome/UPnP/MediaServer2/PulseAudio/Sources"
65 #define CONTAINER_INTROSPECT_XML_PREFIX \
66 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
67 "<node>" \
68 " <!-- If you are looking for documentation make sure to check out" \
69 " http://live.gnome.org/Rygel/MediaServer2Spec -->" \
70 " <interface name=\"org.gnome.UPnP.MediaContainer2\">" \
71 " <method name='ListChildren'>" \
72 " <arg direction='in' name='offset' type='u' />" \
73 " <arg direction='in' name='max' type='u' />" \
74 " <arg direction='in' name='filter' type='as' />" \
75 " <arg direction='out' type='aa{sv}' />" \
76 " </method>" \
77 " <method name='ListContainers'>" \
78 " <arg direction='in' name='offset' type='u' />" \
79 " <arg direction='in' name='max' type='u' />" \
80 " <arg direction='in' name='filter' type='as' />" \
81 " <arg direction='out' type='aa{sv}' />" \
82 " </method>" \
83 " <method name='ListItems'>" \
84 " <arg direction='in' name='offset' type='u' />" \
85 " <arg direction='in' name='max' type='u' />" \
86 " <arg direction='in' name='filter' type='as' />" \
87 " <arg direction='out' type='aa{sv}' />" \
88 " </method>" \
89 " <signal name=\"Updated\">" \
90 " <arg name=\"path\" type=\"o\"/>" \
91 " </signal>" \
92 " <property name=\"ChildCount\" type=\"u\" access=\"read\"/>" \
93 " <property name=\"ItemCount\" type=\"u\" access=\"read\"/>" \
94 " <property name=\"ContainerCount\" type=\"u\" access=\"read\"/>" \
95 " <property name=\"Searchable\" type=\"b\" access=\"read\"/>" \
96 " </interface>" \
97 " <interface name=\"org.gnome.UPnP.MediaObject2\">" \
98 " <property name=\"Parent\" type=\"s\" access=\"read\"/>" \
99 " <property name=\"Type\" type=\"s\" access=\"read\"/>" \
100 " <property name=\"Path\" type=\"s\" access=\"read\"/>" \
101 " <property name=\"DisplayName\" type=\"s\" access=\"read\"/>" \
102 " </interface>" \
103 " <interface name=\"org.freedesktop.DBus.Properties\">" \
104 " <method name=\"Get\">" \
105 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
106 " <arg name=\"property\" direction=\"in\" type=\"s\"/>" \
107 " <arg name=\"value\" direction=\"out\" type=\"v\"/>" \
108 " </method>" \
109 " <method name=\"GetAll\">" \
110 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
111 " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>" \
112 " </method>" \
113 " </interface>" \
114 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
115 " <method name=\"Introspect\">" \
116 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
117 " </method>" \
118 " </interface>"
120 #define CONTAINER_INTROSPECT_XML_POSTFIX \
121 "</node>"
123 #define ROOT_INTROSPECT_XML \
124 CONTAINER_INTROSPECT_XML_PREFIX \
125 "<node name=\"Sinks\"/>" \
126 "<node name=\"Sources\"/>" \
127 CONTAINER_INTROSPECT_XML_POSTFIX
129 #define ITEM_INTROSPECT_XML \
130 DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE \
131 "<node>" \
132 " <!-- If you are looking for documentation make sure to check out" \
133 " http://live.gnome.org/Rygel/MediaProvider2Spec -->" \
134 " <interface name=\"org.gnome.UPnP.MediaItem2\">" \
135 " <property name=\"URLs\" type=\"as\" access=\"read\"/>" \
136 " <property name=\"MIMEType\" type=\"s\" access=\"read\"/>" \
137 " <interface name=\"org.gnome.UPnP.MediaObject2\">" \
138 " <property name=\"Parent\" type=\"s\" access=\"read\"/>" \
139 " <property name=\"Type\" type=\"s\" access=\"read\"/>" \
140 " <property name=\"Path\" type=\"s\" access=\"read\"/>" \
141 " <property name=\"DisplayName\" type=\"s\" access=\"read\"/>" \
142 " </interface>" \
143 " <interface name=\"org.freedesktop.DBus.Properties\">" \
144 " <method name=\"Get\">" \
145 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
146 " <arg name=\"property\" direction=\"in\" type=\"s\"/>" \
147 " <arg name=\"value\" direction=\"out\" type=\"v\"/>" \
148 " </method>" \
149 " <method name=\"GetAll\">" \
150 " <arg name=\"interface\" direction=\"in\" type=\"s\"/>" \
151 " <arg name=\"properties\" direction=\"out\" type=\"a{sv}\"/>" \
152 " </method>" \
153 " </interface>" \
154 " <interface name=\"org.freedesktop.DBus.Introspectable\">" \
155 " <method name=\"Introspect\">" \
156 " <arg name=\"data\" type=\"s\" direction=\"out\"/>" \
157 " </method>" \
158 " </interface>" \
159 "</node>"
162 static const char* const valid_modargs[] = {
163 "display_name",
164 NULL
167 struct userdata {
168 pa_core *core;
169 pa_module *module;
171 pa_dbus_connection *bus;
172 pa_bool_t got_name:1;
174 char *display_name;
176 pa_hook_slot *source_new_slot, *source_unlink_slot;
178 pa_http_protocol *http;
181 static char *compute_url(const struct userdata *u, const char *name);
183 static void send_signal(struct userdata *u, pa_source *s) {
184 DBusMessage *m;
185 const char *parent;
187 pa_assert(u);
188 pa_source_assert_ref(s);
190 if (u->core->state == PA_CORE_SHUTDOWN)
191 return;
193 if (s->monitor_of)
194 parent = OBJECT_SINKS;
195 else
196 parent = OBJECT_SOURCES;
198 pa_assert_se(m = dbus_message_new_signal(parent, "org.gnome.UPnP.MediaContainer2", "Updated"));
199 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), m, NULL));
201 dbus_message_unref(m);
204 static pa_hook_result_t source_new_or_unlink_cb(pa_core *c, pa_source *s, struct userdata *u) {
205 pa_assert(c);
206 pa_source_assert_ref(s);
208 send_signal(u, s);
210 return PA_HOOK_OK;
213 static pa_bool_t message_is_property_get(DBusMessage *m, const char *interface, const char *property) {
214 const char *i, *p;
215 DBusError error;
217 dbus_error_init(&error);
219 pa_assert(m);
221 if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get"))
222 return FALSE;
224 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_STRING, &p, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
225 dbus_error_free(&error);
226 return FALSE;
229 return pa_streq(i, interface) && pa_streq(p, property);
232 static pa_bool_t message_is_property_get_all(DBusMessage *m, const char *interface) {
233 const char *i;
234 DBusError error;
236 dbus_error_init(&error);
238 pa_assert(m);
240 if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "GetAll"))
241 return FALSE;
243 if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
244 dbus_error_free(&error);
245 return FALSE;
248 return pa_streq(i, interface);
251 static void append_variant_object_array(DBusMessage *m, DBusMessageIter *iter, const char *path[], unsigned n) {
252 DBusMessageIter _iter, variant, array;
253 unsigned c;
255 pa_assert(m);
256 pa_assert(path);
258 if (!iter) {
259 dbus_message_iter_init_append(m, &_iter);
260 iter = &_iter;
263 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "ao", &variant));
264 pa_assert_se(dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY, "o", &array));
266 for (c = 0; c < n; c++)
267 pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_OBJECT_PATH, path + c));
269 pa_assert_se(dbus_message_iter_close_container(&variant, &array));
270 pa_assert_se(dbus_message_iter_close_container(iter, &variant));
273 static void append_variant_string(DBusMessage *m, DBusMessageIter *iter, const char *s) {
274 DBusMessageIter _iter, sub;
276 pa_assert(m);
277 pa_assert(s);
279 if (!iter) {
280 dbus_message_iter_init_append(m, &_iter);
281 iter = &_iter;
284 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "s", &sub));
285 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &s));
286 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
289 static void append_variant_object(DBusMessage *m, DBusMessageIter *iter, const char *s) {
290 DBusMessageIter _iter, sub;
292 pa_assert(m);
293 pa_assert(s);
295 if (!iter) {
296 dbus_message_iter_init_append(m, &_iter);
297 iter = &_iter;
300 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "o", &sub));
301 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_OBJECT_PATH, &s));
302 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
305 static void append_variant_unsigned(DBusMessage *m, DBusMessageIter *iter, unsigned u) {
306 DBusMessageIter _iter, sub;
308 pa_assert(m);
310 if (!iter) {
311 dbus_message_iter_init_append(m, &_iter);
312 iter = &_iter;
315 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "u", &sub));
316 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_UINT32, &u));
317 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
320 static void append_variant_boolean(DBusMessage *m, DBusMessageIter *iter, dbus_bool_t b) {
321 DBusMessageIter _iter, sub;
323 pa_assert(m);
325 if (!iter) {
326 dbus_message_iter_init_append(m, &_iter);
327 iter = &_iter;
330 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "b", &sub));
331 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_BOOLEAN, &b));
332 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
335 static void append_variant_urls(DBusMessage *m, DBusMessageIter *iter, const struct userdata *u, pa_sink *sink, pa_source *source) {
336 DBusMessageIter _iter, sub, array;
337 char *url;
339 pa_assert(m);
340 pa_assert(u);
341 pa_assert(sink || source);
343 if (!iter) {
344 dbus_message_iter_init_append(m, &_iter);
345 iter = &_iter;
348 url = compute_url(u, sink ? sink->monitor_source->name : source->name);
350 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_VARIANT, "as", &sub));
351 pa_assert_se(dbus_message_iter_open_container(&sub, DBUS_TYPE_ARRAY, "s", &array));
352 pa_assert_se(dbus_message_iter_append_basic(&array, DBUS_TYPE_STRING, &url));
353 pa_assert_se(dbus_message_iter_close_container(&sub, &array));
354 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
356 pa_xfree(url);
359 static void append_variant_mime_type(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
360 char *mime_type;
362 pa_assert(sink || source);
364 if (sink)
365 mime_type = pa_sample_spec_to_mime_type_mimefy(&sink->sample_spec, &sink->channel_map);
366 else
367 mime_type = pa_sample_spec_to_mime_type_mimefy(&source->sample_spec, &source->channel_map);
369 append_variant_string(m, iter, mime_type);
371 pa_xfree(mime_type);
374 static void append_variant_item_display_name(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
375 const char *display_name;
377 pa_assert(sink || source);
379 display_name = pa_strna(pa_proplist_gets(sink ? sink->proplist : source->proplist, PA_PROP_DEVICE_DESCRIPTION));
380 append_variant_string(m, iter, display_name);
383 static void append_property_dict_entry_object_array(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *path[], unsigned n) {
384 DBusMessageIter sub;
386 pa_assert(iter);
388 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
389 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
390 append_variant_object_array(m, &sub, path, n);
391 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
394 static void append_property_dict_entry_string(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *value) {
395 DBusMessageIter sub;
397 pa_assert(iter);
399 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
400 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
401 append_variant_string(m, &sub, value);
402 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
405 static void append_property_dict_entry_object(DBusMessage *m, DBusMessageIter *iter, const char *name, const char *value) {
406 DBusMessageIter sub;
408 pa_assert(iter);
410 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
411 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
412 append_variant_object(m, &sub, value);
413 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
416 static void append_property_dict_entry_unsigned(DBusMessage *m, DBusMessageIter *iter, const char *name, unsigned u) {
417 DBusMessageIter sub;
419 pa_assert(iter);
421 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
422 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
423 append_variant_unsigned(m, &sub, u);
424 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
427 static void append_property_dict_entry_boolean(DBusMessage *m, DBusMessageIter *iter, const char *name, dbus_bool_t b) {
428 DBusMessageIter sub;
430 pa_assert(iter);
432 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
433 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &name));
434 append_variant_boolean(m, &sub, b);
435 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
438 static void append_property_dict_entry_urls(DBusMessage *m, DBusMessageIter *iter, const struct userdata *u, pa_sink *sink, pa_source *source) {
439 DBusMessageIter sub;
440 const char *property_name = "URLs";
442 pa_assert(iter);
444 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
445 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property_name));
446 append_variant_urls(m, &sub, u, sink, source);
447 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
450 static void append_property_dict_entry_mime_type(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
451 DBusMessageIter sub;
452 const char *property_name = "MIMEType";
454 pa_assert(iter);
456 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
457 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property_name));
458 append_variant_mime_type(m, &sub, sink, source);
459 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
462 static void append_property_dict_entry_item_display_name(DBusMessage *m, DBusMessageIter *iter, pa_sink *sink, pa_source *source) {
463 DBusMessageIter sub;
464 const char *property_name = "DisplayName";
466 pa_assert(iter);
468 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_DICT_ENTRY, NULL, &sub));
469 pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &property_name));
470 append_variant_item_display_name(m, &sub, sink, source);
471 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
474 static pa_bool_t get_mediacontainer2_list_args(DBusMessage *m, unsigned *offset, unsigned *max_entries, char ***filter, int *filter_len) {
475 DBusError error;
477 dbus_error_init(&error);
479 pa_assert(m);
480 pa_assert(offset);
481 pa_assert(max_entries);
482 pa_assert(filter);
484 if (!dbus_message_get_args(m, &error, DBUS_TYPE_UINT32, offset, DBUS_TYPE_UINT32, max_entries, DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, filter, filter_len, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
485 dbus_error_free(&error);
486 return FALSE;
489 return TRUE;
492 static unsigned get_sinks_or_sources_count(const char *path, const struct userdata *u) {
493 unsigned n, k;
495 n = pa_idxset_size(u->core->sinks);
496 k = pa_idxset_size(u->core->sources);
497 pa_assert(k >= n);
499 return pa_streq(path, OBJECT_SINKS) ? n : k - n;
502 static void append_sink_or_source_container_mediaobject2_properties(DBusMessage *r, DBusMessageIter *sub, const char *path) {
503 append_property_dict_entry_object(r, sub, "Parent", OBJECT_ROOT);
504 append_property_dict_entry_string(r, sub, "Type", "container");
505 append_property_dict_entry_object(r, sub, "Path", path);
506 append_property_dict_entry_string(r, sub, "DisplayName",
507 pa_streq(path, OBJECT_SINKS) ?
508 _("Output Devices") :
509 _("Input Devices"));
512 static void append_sink_or_source_container_properties(
513 DBusMessage *r, DBusMessageIter *iter,
514 const char *path, const struct userdata *user_data,
515 char * const * filter, int filter_len) {
517 DBusMessageIter sub;
519 pa_assert(r);
520 pa_assert(iter);
521 pa_assert(path);
522 pa_assert(filter);
524 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
526 if (filter_len == 1 && (*filter)[0] == '*' && (*filter)[1] == '\0') {
527 append_sink_or_source_container_mediaobject2_properties(r, &sub, path);
528 append_property_dict_entry_unsigned(r, &sub, "ChildCount", get_sinks_or_sources_count(path, user_data));
529 append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
531 else {
532 for (int i = 0; i < filter_len; ++i) {
533 const char *property_name = filter[i];
534 if (pa_streq(property_name, "Parent")) {
535 append_property_dict_entry_object(r, &sub, "Parent", OBJECT_ROOT);
537 else if (pa_streq(property_name, "Type")) {
538 append_property_dict_entry_string(r, &sub, "Type", "container");
540 else if (pa_streq(property_name, "Path")) {
541 append_property_dict_entry_object(r, &sub, "Path", path);
543 else if (pa_streq(property_name, "DisplayName")) {
544 append_property_dict_entry_string(r, &sub, "DisplayName",
545 pa_streq(path, OBJECT_SINKS) ?
546 _("Output Devices") :
547 _("Input Devices"));
549 else if (pa_streq(property_name, "ChildCount")) {
550 append_property_dict_entry_unsigned(r, &sub, "ChildCount", get_sinks_or_sources_count(path, user_data));
552 else if (pa_streq(property_name, "Searchable")) {
553 append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
558 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
561 static void append_sink_or_source_item_mediaobject2_properties(DBusMessage *r, DBusMessageIter *sub, const char *path, pa_sink *sink, pa_source *source) {
562 append_property_dict_entry_object(r, sub, "Parent", sink ? OBJECT_SINKS : OBJECT_SOURCES);
563 append_property_dict_entry_string(r, sub, "Type", "audio");
564 append_property_dict_entry_object(r, sub, "Path", path);
565 append_property_dict_entry_item_display_name(r, sub, sink, source);
568 static void append_sink_or_source_item_properties(
569 DBusMessage *r, DBusMessageIter *iter,
570 const char *path, const struct userdata *user_data,
571 pa_sink *sink, pa_source *source,
572 char * const * filter, int filter_len) {
574 DBusMessageIter sub;
576 pa_assert(r);
577 pa_assert(iter);
578 pa_assert(path);
579 pa_assert(filter);
580 pa_assert(sink || source);
582 pa_assert_se(dbus_message_iter_open_container(iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
584 if (filter_len == 1 && (*filter)[0] == '*' && (*filter)[1] == '\0') {
585 append_sink_or_source_item_mediaobject2_properties(r, &sub, path, sink, source);
586 append_property_dict_entry_urls(r, &sub, user_data, sink, source);
587 append_property_dict_entry_mime_type(r, &sub, sink, source);
589 else {
590 for (int i = 0; i < filter_len; ++i) {
591 const char *property_name = filter[i];
592 if (pa_streq(property_name, "Parent")) {
593 append_property_dict_entry_object(r, &sub, "Parent", sink ? OBJECT_SINKS : OBJECT_SOURCES);
595 else if (pa_streq(property_name, "Type")) {
596 append_property_dict_entry_string(r, &sub, "Type", "audio");
598 else if (pa_streq(property_name, "Path")) {
599 append_property_dict_entry_object(r, &sub, "Path", path);
601 else if (pa_streq(property_name, "DisplayName")) {
602 append_property_dict_entry_item_display_name(r, &sub, sink, source);
604 else if (pa_streq(property_name, "URLs")) {
605 append_property_dict_entry_urls(r, &sub, user_data, sink, source);
607 else if (pa_streq(property_name, "MIMEType")) {
608 append_property_dict_entry_mime_type(r, &sub, sink, source);
613 pa_assert_se(dbus_message_iter_close_container(iter, &sub));
616 static const char *array_root_containers[] = { OBJECT_SINKS, OBJECT_SOURCES };
617 static const char *array_no_children[] = { };
619 static DBusHandlerResult root_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
620 struct userdata *u = userdata;
621 DBusMessage *r = NULL;
623 pa_assert(u);
625 if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ChildCount")) {
626 pa_assert_se(r = dbus_message_new_method_return(m));
627 append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_root_containers));
629 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ItemCount")) {
630 pa_assert_se(r = dbus_message_new_method_return(m));
631 append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_no_children));
633 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ContainerCount")) {
634 pa_assert_se(r = dbus_message_new_method_return(m));
635 append_variant_unsigned(r, NULL, PA_ELEMENTSOF(array_root_containers));
637 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "Searchable")) {
638 pa_assert_se(r = dbus_message_new_method_return(m));
639 append_variant_boolean(r, NULL, FALSE);
641 } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaContainer2")) {
642 DBusMessageIter iter, sub;
644 pa_assert_se(r = dbus_message_new_method_return(m));
645 dbus_message_iter_init_append(r, &iter);
647 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
648 append_property_dict_entry_unsigned(r, &sub, "ChildCount", PA_ELEMENTSOF(array_root_containers));
649 append_property_dict_entry_unsigned(r, &sub, "ItemCount", PA_ELEMENTSOF(array_no_children));
650 append_property_dict_entry_unsigned(r, &sub, "ContainerCount", PA_ELEMENTSOF(array_root_containers));
651 append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
652 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
654 } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListChildren")
655 || dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListContainers")) {
656 DBusMessageIter iter, sub;
657 unsigned offset, max;
658 char ** filter;
659 int filter_len;
661 pa_assert_se(r = dbus_message_new_method_return(m));
663 dbus_message_iter_init_append(r, &iter);
664 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
666 if (get_mediacontainer2_list_args(m, &offset, &max, &filter, &filter_len)) {
667 unsigned end = (max != 0 && offset + max < PA_ELEMENTSOF(array_root_containers))
668 ? max + offset
669 : PA_ELEMENTSOF(array_root_containers);
671 for (unsigned i = offset; i < end; ++i) {
672 const char *container_path = array_root_containers[i];
673 append_sink_or_source_container_properties(r, &sub, container_path, u, filter, filter_len);
676 dbus_free_string_array(filter);
679 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
681 } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListItems")) {
682 DBusMessageIter iter, sub;
684 pa_assert_se(r = dbus_message_new_method_return(m));
686 dbus_message_iter_init_append(r, &iter);
687 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
688 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
690 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Parent")) {
691 pa_assert_se(r = dbus_message_new_method_return(m));
692 append_variant_object(r, NULL, OBJECT_ROOT);
694 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Type")) {
695 pa_assert_se(r = dbus_message_new_method_return(m));
696 append_variant_string(r, NULL, "container");
698 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Path")) {
699 const char *path = dbus_message_get_path(m);
701 pa_assert_se(r = dbus_message_new_method_return(m));
702 append_variant_object(r, NULL, path);
704 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "DisplayName")) {
705 pa_assert_se(r = dbus_message_new_method_return(m));
706 append_variant_string(r, NULL, u->display_name);
708 } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject2")) {
709 DBusMessageIter iter, sub;
710 const char *path = dbus_message_get_path(m);
712 pa_assert_se(r = dbus_message_new_method_return(m));
713 dbus_message_iter_init_append(r, &iter);
715 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
716 append_property_dict_entry_object(r, &sub, "Parent", OBJECT_ROOT);
717 append_property_dict_entry_string(r, &sub, "Type", "container");
718 append_property_dict_entry_object(r, &sub, "Path", path);
719 append_property_dict_entry_string(r, &sub, "DisplayName", u->display_name);
720 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
722 } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
723 const char *xml = ROOT_INTROSPECT_XML;
725 pa_assert_se(r = dbus_message_new_method_return(m));
726 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
728 } else
729 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
731 if (r) {
732 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL));
733 dbus_message_unref(r);
736 return DBUS_HANDLER_RESULT_HANDLED;
739 static char *compute_url(const struct userdata *u, const char *name) {
740 pa_strlist *i;
742 pa_assert(u);
743 pa_assert(name);
745 for (i = pa_http_protocol_servers(u->http); i; i = pa_strlist_next(i)) {
746 pa_parsed_address a;
748 if (pa_parse_address(pa_strlist_data(i), &a) >= 0 &&
749 (a.type == PA_PARSED_ADDRESS_TCP4 ||
750 a.type == PA_PARSED_ADDRESS_TCP6 ||
751 a.type == PA_PARSED_ADDRESS_TCP_AUTO)) {
753 const char *address;
754 char *s;
756 if (pa_is_ip_address(a.path_or_host))
757 address = a.path_or_host;
758 else
759 address = "@ADDRESS@";
761 if (a.port <= 0)
762 a.port = 4714;
764 s = pa_sprintf_malloc("http://%s:%u/listen/source/%s", address, a.port, name);
766 pa_xfree(a.path_or_host);
767 return s;
770 pa_xfree(a.path_or_host);
773 return pa_sprintf_malloc("http://@ADDRESS@:4714/listen/source/%s", name);
776 static DBusHandlerResult sinks_and_sources_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
777 struct userdata *u = userdata;
778 DBusMessage *r = NULL;
779 const char *path;
781 pa_assert(u);
783 path = dbus_message_get_path(m);
785 if (pa_streq(path, OBJECT_SINKS) || pa_streq(path, OBJECT_SOURCES)) {
787 /* Container nodes */
789 if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ChildCount")
790 || message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ItemCount")) {
791 pa_assert_se(r = dbus_message_new_method_return(m));
792 append_variant_unsigned(r, NULL, get_sinks_or_sources_count(path, u));
794 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "ContainerCount")) {
795 pa_assert_se(r = dbus_message_new_method_return(m));
796 append_variant_unsigned(r, NULL, 0);
798 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaContainer2", "Searchable")) {
799 pa_assert_se(r = dbus_message_new_method_return(m));
800 append_variant_boolean(r, NULL, FALSE);
802 } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaContainer2")) {
803 DBusMessageIter iter, sub;
804 unsigned item_count;
806 pa_assert_se(r = dbus_message_new_method_return(m));
807 dbus_message_iter_init_append(r, &iter);
809 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
811 item_count = get_sinks_or_sources_count(path, u);
813 append_property_dict_entry_unsigned(r, &sub, "ChildCount", item_count);
814 append_property_dict_entry_unsigned(r, &sub, "ItemCount", item_count);
815 append_property_dict_entry_unsigned(r, &sub, "ContainerCount", 0);
816 append_property_dict_entry_boolean(r, &sub, "Searchable", FALSE);
818 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
820 } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListChildren")
821 || dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListItems")) {
822 DBusMessageIter iter, sub;
823 unsigned offset, max;
824 char **filter;
825 int filter_len;
827 pa_assert_se(r = dbus_message_new_method_return(m));
829 dbus_message_iter_init_append(r, &iter);
830 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
832 if (get_mediacontainer2_list_args(m, &offset, &max, &filter, &filter_len)) {
833 unsigned end = (max != 0) ? max + offset : UINT_MAX;
835 if (pa_streq(path, OBJECT_SINKS)) {
836 pa_sink *sink;
837 char sink_path[sizeof(OBJECT_SINKS) + 32];
838 char *path_end = sink_path + sizeof(OBJECT_SINKS);
839 unsigned item_index = 0;
840 uint32_t idx;
842 strcpy(sink_path, OBJECT_SINKS "/");
844 PA_IDXSET_FOREACH(sink, u->core->sinks, idx) {
845 if (item_index >= offset && item_index < end) {
846 sprintf(path_end, "%u", sink->index);
847 append_sink_or_source_item_properties(r, &sub, sink_path, u, sink, NULL, filter, filter_len);
849 ++item_index;
851 } else {
852 pa_source *source;
853 char source_path[sizeof(OBJECT_SOURCES) + 32];
854 char *path_end = source_path + sizeof(OBJECT_SOURCES);
855 unsigned item_index = 0;
856 uint32_t idx;
858 strcpy(source_path, OBJECT_SOURCES "/");
860 PA_IDXSET_FOREACH(source, u->core->sources, idx)
861 if (!source->monitor_of) {
862 if (item_index >= offset && item_index < end) {
863 sprintf(path_end, "%u", source->index);
864 append_sink_or_source_item_properties(r, &sub, source_path, u, NULL, source, filter, filter_len);
866 ++item_index;
870 dbus_free_string_array(filter);
873 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
875 } else if (dbus_message_is_method_call(m, "org.gnome.UPnP.MediaContainer2", "ListContainers")) {
876 DBusMessageIter iter, sub;
878 pa_assert_se(r = dbus_message_new_method_return(m));
880 dbus_message_iter_init_append(r, &iter);
881 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "a{sv}", &sub));
882 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
884 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Parent")) {
885 pa_assert_se(r = dbus_message_new_method_return(m));
886 append_variant_object(r, NULL, OBJECT_ROOT);
888 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Type")) {
889 pa_assert_se(r = dbus_message_new_method_return(m));
890 append_variant_string(r, NULL, "container");
892 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Path")) {
893 pa_assert_se(r = dbus_message_new_method_return(m));
894 append_variant_object(r, NULL, path);
896 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "DisplayName")) {
897 pa_assert_se(r = dbus_message_new_method_return(m));
898 append_variant_string(r,
899 NULL,
900 pa_streq(path, OBJECT_SINKS) ?
901 _("Output Devices") :
902 _("Input Devices"));
904 } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject2")) {
905 DBusMessageIter iter, sub;
907 pa_assert_se(r = dbus_message_new_method_return(m));
909 dbus_message_iter_init_append(r, &iter);
910 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
911 append_sink_or_source_container_mediaobject2_properties(r, &sub, path);
913 } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
914 pa_strbuf *sb;
915 char *xml;
916 uint32_t idx;
918 sb = pa_strbuf_new();
919 pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_PREFIX);
921 if (pa_streq(path, OBJECT_SINKS)) {
922 pa_sink *sink;
924 PA_IDXSET_FOREACH(sink, u->core->sinks, idx)
925 pa_strbuf_printf(sb, "<node name=\"%u\"/>", sink->index);
926 } else {
927 pa_source *source;
929 PA_IDXSET_FOREACH(source, u->core->sources, idx)
930 if (!source->monitor_of)
931 pa_strbuf_printf(sb, "<node name=\"%u\"/>", source->index);
934 pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_POSTFIX);
935 xml = pa_strbuf_tostring_free(sb);
937 pa_assert_se(r = dbus_message_new_method_return(m));
938 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
940 pa_xfree(xml);
941 } else
942 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
944 } else {
945 pa_sink *sink = NULL;
946 pa_source *source = NULL;
948 /* Child nodes */
950 if (pa_startswith(path, OBJECT_SINKS "/"))
951 sink = pa_namereg_get(u->core, path + sizeof(OBJECT_SINKS), PA_NAMEREG_SINK);
952 else if (pa_startswith(path, OBJECT_SOURCES "/"))
953 source = pa_namereg_get(u->core, path + sizeof(OBJECT_SOURCES), PA_NAMEREG_SOURCE);
955 if (!sink && !source)
956 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
958 if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Parent")) {
959 pa_assert_se(r = dbus_message_new_method_return(m));
960 append_variant_object(r, NULL, sink ? OBJECT_SINKS : OBJECT_SOURCES);
962 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Type")) {
963 pa_assert_se(r = dbus_message_new_method_return(m));
964 append_variant_string(r, NULL, "audio");
966 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "Path")) {
967 pa_assert_se(r = dbus_message_new_method_return(m));
968 append_variant_object(r, NULL, path);
970 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaObject2", "DisplayName")) {
971 pa_assert_se(r = dbus_message_new_method_return(m));
972 append_variant_item_display_name(r, NULL, sink, source);
974 } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaObject2")) {
975 DBusMessageIter iter, sub;
977 pa_assert_se(r = dbus_message_new_method_return(m));
978 dbus_message_iter_init_append(r, &iter);
980 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
981 append_sink_or_source_item_mediaobject2_properties(r, &sub, path, sink, source);
983 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem2", "MIMEType")) {
984 pa_assert_se(r = dbus_message_new_method_return(m));
985 append_variant_mime_type(r, NULL, sink, source);
987 } else if (message_is_property_get(m, "org.gnome.UPnP.MediaItem2", "URLs")) {
988 pa_assert_se(r = dbus_message_new_method_return(m));
989 append_variant_urls(r, NULL, u, sink, source);
991 } else if (message_is_property_get_all(m, "org.gnome.UPnP.MediaItem2")) {
992 DBusMessageIter iter, sub;
994 pa_assert_se(r = dbus_message_new_method_return(m));
995 dbus_message_iter_init_append(r, &iter);
997 pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_ARRAY, "{sv}", &sub));
999 append_property_dict_entry_mime_type(r, &sub, sink, source);
1000 append_property_dict_entry_urls(r, &sub, u, sink, source);
1002 pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
1004 } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
1005 const char *xml =
1006 ITEM_INTROSPECT_XML;
1008 pa_assert_se(r = dbus_message_new_method_return(m));
1009 pa_assert_se(dbus_message_append_args(r, DBUS_TYPE_STRING, &xml, DBUS_TYPE_INVALID));
1011 } else
1012 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
1015 if (r) {
1016 pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL));
1017 dbus_message_unref(r);
1020 return DBUS_HANDLER_RESULT_HANDLED;
1023 int pa__init(pa_module *m) {
1025 struct userdata *u;
1026 pa_modargs *ma = NULL;
1027 DBusError error;
1028 const char *t;
1030 static const DBusObjectPathVTable vtable_root = {
1031 .message_function = root_handler,
1033 static const DBusObjectPathVTable vtable_sinks_and_sources = {
1034 .message_function = sinks_and_sources_handler,
1037 dbus_error_init(&error);
1039 if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
1040 pa_log("Failed to parse module arguments.");
1041 goto fail;
1044 m->userdata = u = pa_xnew0(struct userdata, 1);
1045 u->core = m->core;
1046 u->module = m;
1047 u->http = pa_http_protocol_get(u->core);
1049 if ((t = pa_modargs_get_value(ma, "display_name", NULL)))
1050 u->display_name = pa_utf8_filter(t);
1051 else
1052 u->display_name = pa_xstrdup(_("Audio on @HOSTNAME@"));
1054 u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_new_or_unlink_cb, u);
1055 u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_new_or_unlink_cb, u);
1057 if (!(u->bus = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error))) {
1058 pa_log("Failed to get session bus connection: %s", error.message);
1059 goto fail;
1062 pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT, &vtable_root, u));
1063 pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SINKS, &vtable_sinks_and_sources, u));
1064 pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SOURCES, &vtable_sinks_and_sources, u));
1066 if (dbus_bus_request_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, DBUS_NAME_FLAG_DO_NOT_QUEUE, &error) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER) {
1067 pa_log("Failed to request service name " SERVICE_NAME ": %s", error.message);
1068 goto fail;
1071 u->got_name = TRUE;
1073 pa_modargs_free(ma);
1075 return 0;
1077 fail:
1078 pa__done(m);
1080 if (ma)
1081 pa_modargs_free(ma);
1083 dbus_error_free(&error);
1085 return -1;
1088 void pa__done(pa_module*m) {
1089 struct userdata*u;
1090 pa_assert(m);
1092 if (!(u = m->userdata))
1093 return;
1095 if (u->source_new_slot)
1096 pa_hook_slot_free(u->source_new_slot);
1097 if (u->source_unlink_slot)
1098 pa_hook_slot_free(u->source_unlink_slot);
1100 if (u->bus) {
1101 DBusError error;
1103 dbus_error_init(&error);
1105 dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT);
1106 dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SINKS);
1107 dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SOURCES);
1109 if (u->got_name) {
1110 if (dbus_bus_release_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, &error) != DBUS_RELEASE_NAME_REPLY_RELEASED) {
1111 pa_log("Failed to release service name " SERVICE_NAME ": %s", error.message);
1112 dbus_error_free(&error);
1116 pa_dbus_connection_unref(u->bus);
1119 pa_xfree(u->display_name);
1121 if (u->http)
1122 pa_http_protocol_unref(u->http);
1124 pa_xfree(u);