summaryrefslogtreecommitdiff
path: root/src/gupnp-media-server.c
blob: a4a037c5de74cbcd79b64ba50871986bb77c424b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * Copyright (C) 2008 Zeeshan Ali <zeenix@gmail.com>.
 * Copyright (C) 2007 OpenedHand Ltd.
 *
 * Author: Zeeshan Ali <zeenix@gmail.com>
 *         Jorn Baayen <jorn@openedhand.com>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * 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.
 */

#include <string.h>
#include <libgupnp/gupnp.h>
#include <libgupnp-av/gupnp-av.h>

#include "gupnp-media-server.h"

#define HOME_DIR_ALIAS "/media"

G_DEFINE_TYPE (GUPnPMediaServer,
               gupnp_media_server,
               GUPNP_TYPE_ROOT_DEVICE);

struct _GUPnPMediaServerPrivate {
        guint32 system_update_id;

        GUPnPService *content_dir;

        GUPnPDIDLLiteWriter *didl_writer;
        GUPnPSearchCriteriaParser *search_parser;
};

/* Hard-coded items (mime, title, path)
 *
 * paths are relative to home directory
 * */
char *items[3][4] = {
        { "4000",
          "audio/mpeg",
          "Maa",
          "entertainment/songs/Maa.mp3" },
        { "4001",
          "audio/mpeg",
          "Hoo",
          "entertainment/songs/Ho.mp3" },
        { NULL }
};

/* GObject stuff */
static void
gupnp_media_server_dispose (GObject *object)
{
        GUPnPMediaServer *server;
        GObjectClass *object_class;

        server = GUPNP_MEDIA_SERVER (object);

        /* Free GUPnP resources */
        if (server->priv->search_parser) {
                g_object_unref (server->priv->search_parser);
                server->priv->search_parser = NULL;
        }

        if (server->priv->didl_writer) {
                g_object_unref (server->priv->didl_writer);
                server->priv->didl_writer = NULL;
        }
        if (server->priv->content_dir) {
                g_object_unref (server->priv->content_dir);
                server->priv->content_dir = NULL;
        }

        /* Call super */
        object_class = G_OBJECT_CLASS (gupnp_media_server_parent_class);
        object_class->dispose (object);
}

static void
gupnp_media_server_init (GUPnPMediaServer *server)
{
         server->priv = G_TYPE_INSTANCE_GET_PRIVATE (server,
                                                     GUPNP_TYPE_MEDIA_SERVER,
                                                     GUPnPMediaServerPrivate);

         /* Create a new DIDL-Lite writer */
        server->priv->didl_writer = gupnp_didl_lite_writer_new ();

        /* Create a new search criteria parser */
        server->priv->search_parser = gupnp_search_criteria_parser_new ();
}

static GObject *
gupnp_media_server_constructor (GType                  type,
                                guint                  n_construct_params,
                                GObjectConstructParam *construct_params)
{
        GObject *object;
        GObjectClass *object_class;
        GUPnPMediaServer *server;
        GUPnPServiceInfo *service;
        GUPnPContext *context;

        object_class = G_OBJECT_CLASS (gupnp_media_server_parent_class);
        object = object_class->constructor (type,
                                            n_construct_params,
                                            construct_params);

        if (object == NULL)
                return NULL;

        server = GUPNP_MEDIA_SERVER (object);

        /* Connect ContentDirectory signals */
        service = gupnp_device_info_get_service
                        (GUPNP_DEVICE_INFO (server),
                         "urn:schemas-upnp-org:service:ContentDirectory:2");
        if (service != NULL) {
                GError *error;

                server->priv->content_dir = GUPNP_SERVICE (service);

                error = NULL;
                gupnp_service_signals_autoconnect (server->priv->content_dir,
                                                   server,
                                                   &error);
                if (error) {
                        g_warning ("Error autoconnecting signals: %s",
                                   error->message);
                        g_error_free (error);
                }
        }

        context = gupnp_device_info_get_context (GUPNP_DEVICE_INFO (server));

        /* Host user's home dir */
        gupnp_context_host_path (context, g_get_home_dir (), HOME_DIR_ALIAS);

        return object;
}

static void
gupnp_media_server_class_init (GUPnPMediaServerClass *klass)
{
        GObjectClass *object_class;

        object_class = G_OBJECT_CLASS (klass);

        object_class->dispose = gupnp_media_server_dispose;
        object_class->constructor = gupnp_media_server_constructor;

        g_type_class_add_private (klass, sizeof (GUPnPMediaServerPrivate));
}

static void
add_root_container (GUPnPDIDLLiteWriter *didl_writer)
{
        guint child_count;

        /* Count items */
        for (child_count = 0; items[child_count][0]; child_count++);

        gupnp_didl_lite_writer_start_container (didl_writer,
                                                "0",
                                                "-1",
                                                child_count,
                                                FALSE,
                                                FALSE);

        gupnp_didl_lite_writer_add_string
                        (didl_writer,
                         "class",
                         GUPNP_DIDL_LITE_WRITER_NAMESPACE_UPNP,
                         NULL,
                         "object.container.storageFolder");

        /* End of Container */
        gupnp_didl_lite_writer_end_container (didl_writer);
}

static void
add_item (GUPnPContext        *context,
          GUPnPDIDLLiteWriter *didl_writer,
          const char          *id,
          const char          *parent_id,
          const char          *mime,
          const char          *title,
          const char          *path)
{
        GUPnPDIDLLiteResource res;

        gupnp_didl_lite_writer_start_item (didl_writer,
                                           id,
                                           parent_id,
                                           NULL,
                                           FALSE);

        /* Add fields */
        gupnp_didl_lite_writer_add_string (didl_writer,
                                           "title",
                                           GUPNP_DIDL_LITE_WRITER_NAMESPACE_DC,
                                           NULL,
                                           title);

        gupnp_didl_lite_writer_add_string
                        (didl_writer,
                         "class",
                         GUPNP_DIDL_LITE_WRITER_NAMESPACE_UPNP,
                         NULL,
                         "object.item.audioItem.musicTrack");

        gupnp_didl_lite_writer_add_string
                        (didl_writer,
                         "album",
                         GUPNP_DIDL_LITE_WRITER_NAMESPACE_UPNP,
                         NULL,
                         "Some album");

        /* Add resource data */
        gupnp_didl_lite_resource_reset (&res);

        /* URI */
        res.uri = g_strdup_printf ("http://%s:%d%s/%s",
                                   gupnp_context_get_host_ip (context),
                                   gupnp_context_get_port (context),
                                   HOME_DIR_ALIAS,
                                   path);

        /* Protocol info */
        res.protocol_info = g_strdup_printf ("http-get:*:%s:*", mime);

        gupnp_didl_lite_writer_add_res (didl_writer, &res);

        /* Cleanup */
        g_free (res.protocol_info);
        g_free (res.uri);

        /* End of item */
        gupnp_didl_lite_writer_end_item (didl_writer);
}

static char *
browse_direct_children (GUPnPMediaServer *server, guint *num_returned)
{
        GUPnPContext *context;
        const char *didl;
        char *result;
        guint i;

        context = gupnp_device_info_get_context (GUPNP_DEVICE_INFO (server));

        /* Start DIDL-Lite fragment */
        gupnp_didl_lite_writer_start_didl_lite (server->priv->didl_writer,
                                                NULL,
                                                NULL,
                                                TRUE);
        /* Add items */
        for (i = 0; items[i][0]; i++)
                add_item (context,
                          server->priv->didl_writer,
                          items[i][0],
                          "0",
                          items[i][1],
                          items[i][2],
                          items[i][3]);

        /* End DIDL-Lite fragment */
        gupnp_didl_lite_writer_end_didl_lite (server->priv->didl_writer);

        /* Retrieve generated string */
        didl = gupnp_didl_lite_writer_get_string (server->priv->didl_writer);
        result = g_strdup (didl);

        /* Reset the parser state */
        gupnp_didl_lite_writer_reset (server->priv->didl_writer);

        *num_returned = i;

        return result;
}

static char *
get_metadata (GUPnPMediaServer *server,
              const char       *object_id,
              guint            *num_returned)
{
        GUPnPContext *context;
        char *result;
        guint i;

        context = gupnp_device_info_get_context (GUPNP_DEVICE_INFO (server));

        /* Start DIDL-Lite fragment */
        gupnp_didl_lite_writer_start_didl_lite (server->priv->didl_writer,
                                                NULL,
                                                NULL,
                                                TRUE);
        *num_returned = 0;
        if (strcmp (object_id, "0") == 0) {
                        add_root_container (server->priv->didl_writer);

                        *num_returned = 1;
        } else {
                /* Find and add the item */
                for (i = 0; items[i][0]; i++) {
                        if (strcmp (object_id, items[i][0]) == 0) {
                                add_item (context,
                                          server->priv->didl_writer,
                                          items[i][0],
                                          "0",
                                          items[i][1],
                                          items[i][2],
                                          items[i][3]);

                                *num_returned = 1;

                                break;
                        }
                }
        }

        if (*num_returned != 0) {
                const char *didl;

                /* End DIDL-Lite fragment */
                gupnp_didl_lite_writer_end_didl_lite
                                        (server->priv->didl_writer);

                /* Retrieve generated string */
                didl = gupnp_didl_lite_writer_get_string
                                        (server->priv->didl_writer);
                result = g_strdup (didl);
        } else
                result = NULL;

        /* Reset the parser state */
        gupnp_didl_lite_writer_reset (server->priv->didl_writer);

        return result;
}

/* Browse action implementation */
void
browse_cb (GUPnPService       *service,
           GUPnPServiceAction *action,
           gpointer            user_data)
{
        GUPnPMediaServer *server;
        char *object_id, *browse_flag;
        gboolean browse_metadata;
        char *result;
        guint num_returned;

        server = GUPNP_MEDIA_SERVER (user_data);

        /* Handle incoming arguments */
        gupnp_service_action_get (action,
                                  "ObjectID",
                                        G_TYPE_STRING,
                                        &object_id,
                                  "BrowseFlag",
                                        G_TYPE_STRING,
                                        &browse_flag,
                                  NULL);

        /* BrowseFlag */
        if (browse_flag && !strcmp (browse_flag, "BrowseDirectChildren")) {
                browse_metadata = FALSE;
        } else if (browse_flag && !strcmp (browse_flag, "BrowseMetadata")) {
                browse_metadata = TRUE;
        } else {
                gupnp_service_action_return_error
                        (action, GUPNP_CONTROL_ERROR_INVALID_ARGS, NULL);

                goto OUT;
        }

        /* ObjectID */
        if (!object_id) {
                gupnp_service_action_return_error
                        (action, 701, "No such object");

                goto OUT;
        }

        if (browse_metadata) {
                result = get_metadata (server, object_id, &num_returned);

                if (result == NULL) {
                        gupnp_service_action_return_error
                                (action, 701, "No such object");

                        goto OUT;
                }
        } else {
                /* We only have a root object */
                if (strcmp (object_id, "0")) {
                        gupnp_service_action_return_error
                                (action, 701, "No such object");

                        goto OUT;
                }

                result = browse_direct_children (server, &num_returned);
        }

        /* Set action return arguments */
        gupnp_service_action_set (action,
                                  "Result",
                                        G_TYPE_STRING,
                                        result,
                                  "NumberReturned",
                                        G_TYPE_UINT,
                                        num_returned,
                                  "TotalMatches",
                                        G_TYPE_UINT,
                                        num_returned,
                                  "UpdateID",
                                        G_TYPE_UINT,
                                        server->priv->system_update_id,
                                  NULL);

        gupnp_service_action_return (action);

        g_free (result);
OUT:
        g_free (object_id);
}

GUPnPMediaServer *
gupnp_media_server_new (GUPnPContext *context,
                        xmlDoc       *description_doc,
                        const char   *relative_location)
{
        GUPnPResourceFactory *factory;

        factory = gupnp_resource_factory_get_default ();

        return g_object_new (GUPNP_TYPE_MEDIA_SERVER,
                             "context", context,
                             "resource-factory", factory,
                             "root-device", NULL,
                             "description-doc", description_doc,
                             "relative-location", relative_location,
                             NULL);
}