rygel: make server name configurable
[profile/ivi/pulseaudio-panda.git] / src / modules / module-rygel-media-server.c
1 /***
2   This file is part of PulseAudio.
3
4   Copyright 2005-2009 Lennart Poettering
5
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.
10
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.
15
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 ***/
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include <pulse/xmalloc.h>
36 #include <pulse/util.h>
37 #include <pulse/i18n.h>
38 #include <pulse/utf8.h>
39
40 #include <pulsecore/sink.h>
41 #include <pulsecore/source.h>
42 #include <pulsecore/core-util.h>
43 #include <pulsecore/log.h>
44 #include <pulsecore/modargs.h>
45 #include <pulsecore/dbus-shared.h>
46 #include <pulsecore/endianmacros.h>
47 #include <pulsecore/namereg.h>
48 #include <pulsecore/mime-type.h>
49 #include <pulsecore/strbuf.h>
50
51 #include "module-rygel-media-server-symdef.h"
52
53 PA_MODULE_AUTHOR("Lennart Poettering");
54 PA_MODULE_DESCRIPTION("UPnP MediaServer Plugin for Rygel");
55 PA_MODULE_VERSION(PACKAGE_VERSION);
56 PA_MODULE_LOAD_ONCE(TRUE);
57 PA_MODULE_USAGE(
58         "display_name=<UPnP Media Server name>");
59
60 /* This implements http://live.gnome.org/action/edit/Rygel/MediaProviderSpec */
61
62 #define SERVICE_NAME "org.Rygel.MediaServer1.PulseAudio"
63
64 #define OBJECT_ROOT "/org/Rygel/MediaServer1/PulseAudio"
65 #define OBJECT_SINKS "/org/Rygel/MediaServer1/PulseAudio/Sinks"
66 #define OBJECT_SOURCES "/org/Rygel/MediaServer1/PulseAudio/Sources"
67
68 #define CONTAINER_INTROSPECT_XML_PREFIX                                 \
69     DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                           \
70     "<node>"                                                            \
71     " <!-- If you are looking for documentation make sure to check out" \
72     "      http://live.gnome.org/Rygel/MediaProviderSpec -->"           \
73     " <interface name=\"org.Rygel.MediaContainer1\">"                   \
74     "  <method name=\"GetContainers\">"                                 \
75     "   <arg name=\"children\" type=\"ao\" direction=\"out\"/>"         \
76     "  </method>"                                                       \
77     "  <method name=\"GetItems\">"                                      \
78     "   <arg name=\"children\" type=\"ao\" direction=\"out\"/>"         \
79     "  </method>"                                                       \
80     "  <signal name=\"ItemAdded\">"                                     \
81     "   <arg name=\"path\" type=\"o\"/>"                                \
82     "  </signal>"                                                       \
83     "  <signal name=\"ItemRemoved\">"                                   \
84     "   <arg name=\"path\" type=\"o\"/>"                                \
85     "  </signal>"                                                       \
86     "  <signal name=\"ContainerAdded\">"                                \
87     "   <arg name=\"path\" type=\"o\"/>"                                \
88     "  </signal>"                                                       \
89     "  <signal name=\"ContainerRemoved\">"                              \
90     "   <arg name=\"path\" type=\"o\"/>"                                \
91     "  </signal>"                                                       \
92     " </interface>"                                                     \
93     " <interface name=\"org.Rygel.MediaObject1\">"                      \
94     "  <property name=\"display-name\" type=\"s\" access=\"read\"/>"    \
95     " </interface>"                                                     \
96     " <interface name=\"org.freedesktop.DBus.Properties\">"             \
97     "  <method name=\"Get\">"                                           \
98     "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>"          \
99     "   <arg name=\"property\" direction=\"in\" type=\"s\"/>"           \
100     "   <arg name=\"value\" direction=\"out\" type=\"v\"/>"             \
101     "  </method>"                                                       \
102     " </interface>"                                                     \
103     " <interface name=\"org.freedesktop.DBus.Introspectable\">"         \
104     "  <method name=\"Introspect\">"                                    \
105     "   <arg name=\"data\" type=\"s\" direction=\"out\"/>"              \
106     "  </method>"                                                       \
107     " </interface>"
108
109 #define CONTAINER_INTROSPECT_XML_POSTFIX                                \
110     "</node>"
111
112 #define ROOT_INTROSPECT_XML                                             \
113     CONTAINER_INTROSPECT_XML_PREFIX                                     \
114     "<node name=\"Sinks\"/>"                                            \
115     "<node name=\"Sources\"/>"                                          \
116     CONTAINER_INTROSPECT_XML_POSTFIX
117
118 #define ITEM_INTROSPECT_XML                                             \
119     DBUS_INTROSPECT_1_0_XML_DOCTYPE_DECL_NODE                           \
120     "<node>"                                                            \
121     " <!-- If you are looking for documentation make sure to check out" \
122     "      http://live.gnome.org/Rygel/MediaProviderSpec -->"           \
123     " <interface name=\"org.Rygel.MediaItem1\">"                        \
124     "  <property name=\"urls\" type=\"as\" access=\"read\"/>"           \
125     "  <property name=\"mime-type\" type=\"s\" access=\"read\"/>"       \
126     "  <property name=\"type\" type=\"s\" access=\"read\"/>"            \
127     " </interface>"                                                     \
128     " <interface name=\"org.Rygel.MediaObject1\">"                      \
129     "  <property name=\"display-name\" type=\"s\" access=\"read\"/>"    \
130     " </interface>"                                                     \
131     " <interface name=\"org.freedesktop.DBus.Properties\">"             \
132     "  <method name=\"Get\">"                                           \
133     "   <arg name=\"interface\" direction=\"in\" type=\"s\"/>"          \
134     "   <arg name=\"property\" direction=\"in\" type=\"s\"/>"           \
135     "   <arg name=\"value\" direction=\"out\" type=\"v\"/>"             \
136     "  </method>"                                                       \
137     " </interface>"                                                     \
138     " <interface name=\"org.freedesktop.DBus.Introspectable\">"         \
139     "  <method name=\"Introspect\">"                                    \
140     "   <arg name=\"data\" type=\"s\" direction=\"out\"/>"              \
141     "  </method>"                                                       \
142     " </interface>"                                                     \
143     "</node>"
144
145
146 static const char* const valid_modargs[] = {
147     "display_name",
148     NULL
149 };
150
151 struct userdata {
152     pa_core *core;
153     pa_module *module;
154
155     pa_dbus_connection *bus;
156     pa_bool_t got_name:1;
157
158     char *display_name;
159
160     pa_hook_slot *source_new_slot, *source_unlink_slot;
161 };
162
163 static void send_signal(struct userdata *u, pa_source *s, const char *name) {
164     DBusMessage *m;
165     char *child;
166     const char *parent;
167
168     pa_assert(u);
169     pa_source_assert_ref(s);
170
171     if (u->core->state == PA_CORE_SHUTDOWN)
172         return;
173
174     if (s->monitor_of) {
175         parent = OBJECT_SINKS;
176         child = pa_sprintf_malloc(OBJECT_SINKS "/%s", s->monitor_of->name);
177     } else {
178         parent = OBJECT_SOURCES;
179         child = pa_sprintf_malloc(OBJECT_SOURCES "/%s", s->name);
180     }
181
182     pa_assert_se(m = dbus_message_new_signal(parent, "org.Rygel.MediaContainer1", name));
183     pa_assert_se(dbus_message_append_args(m, DBUS_TYPE_OBJECT_PATH, &child, DBUS_TYPE_INVALID));
184     pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), m, NULL));
185
186     pa_xfree(child);
187
188     dbus_message_unref(m);
189 }
190
191 static pa_hook_result_t source_new_cb(pa_core *c, pa_source *s, struct userdata *u) {
192     pa_assert(c);
193     pa_source_assert_ref(s);
194
195     send_signal(u, s, "ItemAdded");
196
197     return PA_HOOK_OK;
198 }
199
200
201 static pa_hook_result_t source_unlink_cb(pa_core *c, pa_source *s, struct userdata *u) {
202     pa_assert(c);
203     pa_source_assert_ref(s);
204
205     send_signal(u, s, "ItemRemoved");
206
207     return PA_HOOK_OK;
208 }
209
210 static pa_bool_t message_is_property_get(DBusMessage *m, const char *interface, const char *property) {
211     char *i, *p;
212     DBusError error;
213
214     dbus_error_init(&error);
215
216     pa_assert(m);
217
218     if (!dbus_message_is_method_call(m, "org.freedesktop.DBus.Properties", "Get"))
219         return FALSE;
220
221     if (!dbus_message_get_args(m, &error, DBUS_TYPE_STRING, &i, DBUS_TYPE_STRING, &p, DBUS_TYPE_INVALID) || dbus_error_is_set(&error)) {
222         dbus_error_free(&error);
223         return FALSE;
224     }
225
226     return pa_streq(i, interface) && pa_streq(p, property);
227 }
228
229 static void append_variant_string(DBusMessage *m, const char *s) {
230     DBusMessageIter iter, sub;
231
232     pa_assert(m);
233     pa_assert(s);
234
235     dbus_message_iter_init_append(m, &iter);
236     pa_assert_se(dbus_message_iter_open_container(&iter, DBUS_TYPE_VARIANT, "s", &sub));
237     pa_assert_se(dbus_message_iter_append_basic(&sub, DBUS_TYPE_STRING, &s));
238     pa_assert_se(dbus_message_iter_close_container(&iter, &sub));
239 }
240
241 static DBusHandlerResult root_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
242     struct userdata *u = userdata;
243     DBusMessage *r = NULL;
244
245     pa_assert(u);
246
247     if (dbus_message_is_method_call(m, "org.Rygel.MediaContainer1", "GetContainers")) {
248         const char * array[] = { OBJECT_SINKS, OBJECT_SOURCES };
249         const char ** parray = array;
250
251         pa_assert_se(r = dbus_message_new_method_return(m));
252         pa_assert_se(dbus_message_append_args(
253                              r,
254                              DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &parray, 2,
255                              DBUS_TYPE_INVALID));
256
257     } else if (dbus_message_is_method_call(m, "org.Rygel.MediaContainer1", "GetItems")) {
258         const char * array[] = { };
259         const char **  parray = array;
260
261         pa_assert_se(r = dbus_message_new_method_return(m));
262         pa_assert_se(dbus_message_append_args(
263                              r,
264                              DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &parray, 0,
265                              DBUS_TYPE_INVALID));
266
267     } else if (message_is_property_get(m, "org.Rygel.MediaObject1", "display-name")) {
268         pa_assert_se(r = dbus_message_new_method_return(m));
269         append_variant_string(r, u->display_name);
270     } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
271         const char *xml = ROOT_INTROSPECT_XML;
272
273         pa_assert_se(r = dbus_message_new_method_return(m));
274         pa_assert_se(dbus_message_append_args(
275                              r,
276                              DBUS_TYPE_STRING, &xml,
277                              DBUS_TYPE_INVALID));
278
279     } else
280         return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
281
282     if (r) {
283         pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL));
284         dbus_message_unref(r);
285     }
286
287     return DBUS_HANDLER_RESULT_HANDLED;
288 }
289
290 static DBusHandlerResult sinks_and_sources_handler(DBusConnection *c, DBusMessage *m, void *userdata) {
291     struct userdata *u = userdata;
292     DBusMessage *r = NULL;
293     const char *path;
294
295     pa_assert(u);
296
297     path = dbus_message_get_path(m);
298
299     if (pa_streq(path, OBJECT_SINKS) || pa_streq(path, OBJECT_SOURCES)) {
300
301         /* Container nodes */
302
303         if (dbus_message_is_method_call(m, "org.Rygel.MediaContainer1", "GetContainers")) {
304
305             const char * array[] = { };
306             const char ** parray = array;
307
308             pa_assert_se(r = dbus_message_new_method_return(m));
309             pa_assert_se(dbus_message_append_args(
310                                  r,
311                                  DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &parray, 0,
312                                  DBUS_TYPE_INVALID));
313
314         } else if (dbus_message_is_method_call(m, "org.Rygel.MediaContainer1", "GetItems")) {
315             unsigned n, i = 0;
316             char ** array;
317             uint32_t idx;
318
319             if (pa_streq(path, OBJECT_SINKS))
320                 n = pa_idxset_size(u->core->sinks);
321             else
322                 n = pa_idxset_size(u->core->sources);
323
324             array = pa_xnew(char*, n);
325
326             if (pa_streq(path, OBJECT_SINKS)) {
327                 pa_sink *sink;
328
329                 PA_IDXSET_FOREACH(sink, u->core->sinks, idx)
330                     array[i++] = pa_sprintf_malloc(OBJECT_SINKS "/%u", sink->index);
331             } else {
332                 pa_source *source;
333
334                 PA_IDXSET_FOREACH(source, u->core->sources, idx)
335                     if (!source->monitor_of)
336                         array[i++] = pa_sprintf_malloc(OBJECT_SOURCES "/%u", source->index);
337             }
338
339             pa_assert(i <= n);
340
341             pa_assert_se(r = dbus_message_new_method_return(m));
342             pa_assert_se(dbus_message_append_args(
343                                  r,
344                                  DBUS_TYPE_ARRAY, DBUS_TYPE_OBJECT_PATH, &array, i,
345                                  DBUS_TYPE_INVALID));
346
347             for (; i >= 1; i--)
348                 pa_xfree(array[i-1]);
349
350             pa_xfree(array);
351
352         } else if (message_is_property_get(m, "org.Rygel.MediaObject1", "display-name")) {
353
354             if (pa_streq(path, OBJECT_SINKS)) {
355                 pa_assert_se(r = dbus_message_new_method_return(m));
356                 append_variant_string(r, _("Output Devices"));
357             } else {
358                 pa_assert_se(r = dbus_message_new_method_return(m));
359                 append_variant_string(r, _("Input Devices"));
360             }
361
362         } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
363             pa_strbuf *sb;
364             char *xml;
365             uint32_t idx;
366
367             sb = pa_strbuf_new();
368             pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_PREFIX);
369
370             if (pa_streq(path, OBJECT_SINKS)) {
371                 pa_sink *sink;
372
373                 PA_IDXSET_FOREACH(sink, u->core->sinks, idx)
374                     pa_strbuf_printf(sb, "<node name=\"%u\"/>", sink->index);
375             } else {
376                 pa_source *source;
377
378                 PA_IDXSET_FOREACH(source, u->core->sources, idx)
379                     if (!source->monitor_of)
380                         pa_strbuf_printf(sb, "<node name=\"%u\"/>", source->index);
381             }
382
383             pa_strbuf_puts(sb, CONTAINER_INTROSPECT_XML_POSTFIX);
384             xml = pa_strbuf_tostring_free(sb);
385
386             pa_assert_se(r = dbus_message_new_method_return(m));
387             pa_assert_se(dbus_message_append_args(
388                                  r,
389                                  DBUS_TYPE_STRING, &xml,
390                                  DBUS_TYPE_INVALID));
391
392             pa_xfree(xml);
393         }
394
395     } else {
396         pa_sink *sink = NULL;
397         pa_source *source = NULL;
398
399         /* Child nodes */
400
401         if (pa_startswith(path, OBJECT_SINKS "/"))
402             sink = pa_namereg_get(u->core, path + sizeof(OBJECT_SINKS), PA_NAMEREG_SINK);
403         else if (pa_startswith(path, OBJECT_SOURCES "/"))
404             source = pa_namereg_get(u->core, path + sizeof(OBJECT_SOURCES), PA_NAMEREG_SOURCE);
405
406         if (!sink && !source)
407             return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
408
409         if (message_is_property_get(m, "org.Rygel.MediaObject1", "display-name")) {
410             pa_assert_se(r = dbus_message_new_method_return(m));
411             append_variant_string(r, pa_strna(pa_proplist_gets(sink ? sink->proplist : source->proplist, PA_PROP_DEVICE_DESCRIPTION)));
412
413         } else if (message_is_property_get(m, "org.Rygel.MediaItem1", "type")) {
414             pa_assert_se(r = dbus_message_new_method_return(m));
415             append_variant_string(r, "audio");
416
417         } else if (message_is_property_get(m, "org.Rygel.MediaItem1", "mime-type")) {
418             char *t;
419
420             if (sink)
421                 t = pa_sample_spec_to_mime_type_mimefy(&sink->sample_spec, &sink->channel_map);
422             else
423                 t = pa_sample_spec_to_mime_type_mimefy(&source->sample_spec, &source->channel_map);
424
425             pa_assert_se(r = dbus_message_new_method_return(m));
426             append_variant_string(r, t);
427
428         } else if (message_is_property_get(m, "org.Rygel.MediaItem1", "urls")) {
429             char * array[1];
430             char ** parray = array;
431
432             pa_assert_se(r = dbus_message_new_method_return(m));
433
434             array[0] = pa_sprintf_malloc("http://@ADDRESS@:4714/listen/source/%s",
435                                          sink ? sink->monitor_source->name : source->name);
436
437             pa_assert_se(dbus_message_append_args(
438                                  r,
439                                  DBUS_TYPE_ARRAY, DBUS_TYPE_STRING, &parray, 1,
440                                  DBUS_TYPE_INVALID));
441
442             pa_xfree(array[0]);
443
444         } else if (dbus_message_is_method_call(m, "org.freedesktop.DBus.Introspectable", "Introspect")) {
445             const char *xml =
446                 ITEM_INTROSPECT_XML;
447
448             pa_assert_se(r = dbus_message_new_method_return(m));
449             pa_assert_se(dbus_message_append_args(
450                                  r,
451                                  DBUS_TYPE_STRING, &xml,
452                                  DBUS_TYPE_INVALID));
453
454         } else
455             return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
456     }
457
458     if (r) {
459         pa_assert_se(dbus_connection_send(pa_dbus_connection_get(u->bus), r, NULL));
460         dbus_message_unref(r);
461     }
462
463     return DBUS_HANDLER_RESULT_HANDLED;
464 }
465
466 int pa__init(pa_module *m) {
467
468     struct userdata *u;
469     pa_modargs *ma = NULL;
470     DBusError error;
471     const char *t;
472
473     static const DBusObjectPathVTable vtable_root = {
474         .message_function = root_handler,
475     };
476     static const DBusObjectPathVTable vtable_sinks_and_sources = {
477         .message_function = sinks_and_sources_handler,
478     };
479
480     dbus_error_init(&error);
481
482     if (!(ma = pa_modargs_new(m->argument, valid_modargs))) {
483         pa_log("Failed to parse module arguments.");
484         goto fail;
485     }
486
487     m->userdata = u = pa_xnew0(struct userdata, 1);
488     u->core = m->core;
489     u->module = m;
490
491     if ((t = pa_modargs_get_value(ma, "display_name", NULL)))
492         u->display_name = pa_utf8_filter(t);
493     else {
494         char *h;
495
496         h = pa_get_host_name_malloc();
497         u->display_name = pa_sprintf_malloc(_("Audio on %s"), h);
498         pa_xfree(h);
499     }
500
501     u->source_new_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_PUT], PA_HOOK_LATE, (pa_hook_cb_t) source_new_cb, u);
502     u->source_unlink_slot = pa_hook_connect(&m->core->hooks[PA_CORE_HOOK_SOURCE_UNLINK], PA_HOOK_LATE, (pa_hook_cb_t) source_unlink_cb, u);
503
504     if (!(u->bus = pa_dbus_bus_get(m->core, DBUS_BUS_SESSION, &error))) {
505         pa_log("Failed to get session bus connection: %s", error.message);
506         goto fail;
507     }
508
509     pa_assert_se(dbus_connection_register_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT, &vtable_root, u));
510     pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SINKS, &vtable_sinks_and_sources, u));
511     pa_assert_se(dbus_connection_register_fallback(pa_dbus_connection_get(u->bus), OBJECT_SOURCES, &vtable_sinks_and_sources, u));
512
513     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) {
514         pa_log("Failed to request service name " SERVICE_NAME ": %s", error.message);
515         goto fail;
516     }
517
518     u->got_name = TRUE;
519
520     pa_modargs_free(ma);
521
522     return 0;
523
524 fail:
525     pa__done(m);
526
527     if (ma)
528         pa_modargs_free(ma);
529
530     dbus_error_free(&error);
531
532     return -1;
533 }
534
535 void pa__done(pa_module*m) {
536     struct userdata*u;
537     pa_assert(m);
538
539     if (!(u = m->userdata))
540         return;
541
542     if (u->source_new_slot)
543         pa_hook_slot_free(u->source_new_slot);
544     if (u->source_unlink_slot)
545         pa_hook_slot_free(u->source_unlink_slot);
546
547     if (u->bus) {
548         DBusError error;
549
550         dbus_error_init(&error);
551
552         dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_ROOT);
553         dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SINKS);
554         dbus_connection_unregister_object_path(pa_dbus_connection_get(u->bus), OBJECT_SOURCES);
555
556         if (u->got_name) {
557             if (dbus_bus_release_name(pa_dbus_connection_get(u->bus), SERVICE_NAME, &error) != DBUS_RELEASE_NAME_REPLY_RELEASED) {
558                 pa_log("Failed to release service name " SERVICE_NAME ": %s", error.message);
559                 dbus_error_free(&error);
560             }
561         }
562
563         pa_dbus_connection_unref(u->bus);
564     }
565
566     pa_xfree(u->display_name);
567
568     pa_xfree(u);
569 }