summaryrefslogtreecommitdiff
path: root/libgupnp-dlna/gupnp-dlna-discoverer.c
blob: faf21ff88102b85e494c6c6f2a31fce61b8759f9 (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
/*
 * Copyright (C) 2010 Nokia Corporation.
 *
 * Authors: Arun Raghavan <arun.raghavan@collabora.co.uk>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "gupnp-dlna-discoverer.h"
#include "gupnp-dlna-marshal.h"
#include "profile-loading.h"

/**
 * SECTION:gupnp-dlna-discoverer
 * @short_description: Utility API for discovering DLNA profile/mime type and
 * other metadata for given media.
 *
 * The GUPnPDLNADiscoverer object provides a light-weight wrapper over the
 * #GstDiscoverer API. The latter provides a simple interface to discover
 * media metadata given a URI. GUPnPDLNADiscoverer extends this API to also
 * provide a DLNA profile name and mime type for the media.
 *
 * The API provided corresponds very closely to the API provided by
 * #GstDiscoverer - both synchronous and asynchronous discovery of metadata
 * are possible.
 *
 * The asynchronous mode requires a running #GMainLoop in the default
 * #GMainContext, where one connects to the various signals, appends the
 * URIs to be processed and then asks for the discovery to begin.
 */
enum {
        DONE,
        SIGNAL_LAST
};

static guint signals[SIGNAL_LAST];


G_DEFINE_TYPE (GUPnPDLNADiscoverer, gupnp_dlna_discoverer, GST_TYPE_DISCOVERER)

#define GET_PRIVATE(o)                                                  \
        (G_TYPE_INSTANCE_GET_PRIVATE ((o),                              \
                                      GUPNP_TYPE_DLNA_DISCOVERER,       \
                                      GUPnPDLNADiscovererPrivate))

typedef struct _GUPnPDLNADiscovererPrivate GUPnPDLNADiscovererPrivate;

struct _GUPnPDLNADiscovererPrivate {
        gboolean  relaxed_mode;
        gboolean  extended_mode;
};

enum {
        PROP_0,
        PROP_DLNA_RELAXED_MODE,
        PROP_DLNA_EXTENDED_MODE,
};

static void
gupnp_dlna_discoverer_set_property (GObject      *object,
                                    guint        property_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
{
        GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);

        switch (property_id) {
                case PROP_DLNA_RELAXED_MODE:
                        priv->relaxed_mode = g_value_get_boolean (value);
                        break;

                case PROP_DLNA_EXTENDED_MODE:
                        priv->extended_mode = g_value_get_boolean (value);
                        break;

                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
                                                           property_id,
                                                           pspec);
                        break;
        }
}

static void
gupnp_dlna_discoverer_get_property (GObject    *object,
                                    guint      property_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
{
        GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);

        switch (property_id) {
                case PROP_DLNA_RELAXED_MODE:
                        g_value_set_boolean (value, priv->relaxed_mode);
                        break;

                case PROP_DLNA_EXTENDED_MODE:
                        g_value_set_boolean (value, priv->extended_mode);
                        break;

                default:
                        G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
                                                           property_id,
                                                           pspec);
                        break;
        }
}

static void
gupnp_dlna_discoverer_dispose (GObject *object)
{
        G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->dispose (object);
}

static void
gupnp_dlna_discoverer_finalize (GObject *object)
{
        G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->finalize (object);
}

static void
gupnp_dlna_discovered_cb (GstDiscoverer     *discoverer,
                          GstDiscovererInfo *info,
                          GError            *err)
{
        GUPnPDLNAInformation *dlna = NULL;
        GUPnPDLNADiscovererClass *klass =
                GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
        GUPnPDLNADiscovererPrivate *priv =
                GET_PRIVATE (GUPNP_DLNA_DISCOVERER (discoverer));
        gboolean relaxed = priv->relaxed_mode;
        gboolean extended = priv->extended_mode;

        if (info)
                dlna = gupnp_dlna_information_new_from_discoverer_info
                                        (info,
                                         klass->profiles_list
                                                  [relaxed][extended]);

        g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
                       signals[DONE], 0, dlna, err);

        if (dlna)
                g_object_unref (dlna);
}

static void
gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
{
        GObjectClass *object_class = G_OBJECT_CLASS (klass);
        GParamSpec *pspec;

        g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));

        object_class->get_property = gupnp_dlna_discoverer_get_property;
        object_class->set_property = gupnp_dlna_discoverer_set_property;
        object_class->dispose = gupnp_dlna_discoverer_dispose;
        object_class->finalize = gupnp_dlna_discoverer_finalize;

        /**
         * GUPnPDLNADiscoverer::relaxed-mode:
         * @relaxed_mode: setting to true will enable relaxed mode
         *
         * The current release does not support relaxed mode yet
         */
        pspec = g_param_spec_boolean ("relaxed-mode",
                                      "Relaxed mode property",
                                      "Indicates that profile matching should"
                                      "be strictly compliant with the DLNA "
                                      "specification",
                                      FALSE,
                                      G_PARAM_READWRITE |
                                      G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_DLNA_RELAXED_MODE,
                                         pspec);

        /**
         * GUPnPDLNADiscoverer::extended-mode:
         * @extended: setting true will enable extended profile support
         *
         * The current release does not support extended mode yet
         */
        pspec = g_param_spec_boolean ("extended-mode",
                                      "Extended mode property",
                                      "Indicates support for profiles that are "
                                      "not part of the DLNA specification",
                                      FALSE,
                                      G_PARAM_READWRITE |
                                      G_PARAM_CONSTRUCT_ONLY);
        g_object_class_install_property (object_class,
                                         PROP_DLNA_EXTENDED_MODE,
                                         pspec);

        /**
         * GUPnPDLNADiscoverer::done:
         * @discoverer: the #GUPnPDLNADiscoverer
         * @dlna: the results as #GUPnPDLNAInformation
         * @err: contains details of the error if discovery fails, else is NULL
         *
         * Will be emitted when all information on a URI could be discovered.
         *
         * The reciever must unref @dlna with when done using it.
         */
        signals[DONE] =
                g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
                              G_SIGNAL_RUN_LAST,
                              G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
                              NULL, NULL,
                              gupnp_dlna_marshal_VOID__OBJECT_BOXED,
                              G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
                              GST_TYPE_G_ERROR);

        /* Load DLNA profiles from disk */
        if (g_type_from_name ("GstElement")) {
                klass->profiles_list [0][0]
                        = gupnp_dlna_load_profiles_from_disk (FALSE,
                                                              FALSE);
                klass->profiles_list [0][1]
                        = gupnp_dlna_load_profiles_from_disk (FALSE,
                                                              TRUE);
                klass->profiles_list [1][0]
                        = gupnp_dlna_load_profiles_from_disk (TRUE,
                                                              FALSE);
                klass->profiles_list [1][1]
                        = gupnp_dlna_load_profiles_from_disk (TRUE,
                                                              TRUE);
        } else {
                klass->profiles_list [0][0] = NULL;
                klass->profiles_list [0][1] = NULL;
                klass->profiles_list [1][0] = NULL;
                klass->profiles_list [1][1] = NULL;
                g_warning ("GStreamer has not yet been initialised. You need "
                           "to call gst_init()/gst_init_check() for discovery "
                           "to work.");
        }
}

static void
gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
{
        g_signal_connect (&self->parent,
                          "discovered",
                          G_CALLBACK (gupnp_dlna_discovered_cb),
                          NULL);
}

/**
 * gupnp_dlna_discoverer_new:
 * @timeout: default discovery timeout, in nanoseconds
 * @relaxed_mode: set to TRUE, to enable relaxed mode support. FALSE otherwise
 * @extended_mode: set to TRUE, to enable extended mode support. FALSE otherwise
 *
 * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
 * value.
 *
 * Returns: A new #GUPnPDLNADiscoverer object.
 */
GUPnPDLNADiscoverer*
gupnp_dlna_discoverer_new (GstClockTime timeout,
                           gboolean     relaxed_mode,
                           gboolean     extended_mode)
{
        return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
                             "timeout", timeout,
                             "relaxed-mode", relaxed_mode,
                             "extended-mode", extended_mode,
                             NULL);
}

/* Asynchronous API */

/**
 * gupnp_dlna_discoverer_start:
 * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
 *
 * Allows asynchronous discovery of URIs to begin.
 */

/**
 * gupnp_dlna_discoverer_stop:
 * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
 *
 * Stops asynchronous discovery of URIs.
 */

/**
 * gupnp_dlna_discoverer_discover_uri:
 * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
 * @uri: URI to gather metadata for
 *
 * Queues @uri for metadata discovery. When discovery is completed, the
 * "discovered" signal is emitted on @discoverer.
 *
 * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
 */
gboolean
gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer,
                                    const gchar         *uri)
{
        return gst_discoverer_discover_uri_async (GST_DISCOVERER (discoverer),
                                                  uri);
}

/* Synchronous API */

/**
 * gupnp_dlna_discoverer_discover_uri_sync:
 * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
 * @uri: URI to gather metadata for
 * @err: contains details of the error if discovery fails, else is NULL
 *
 * Synchronously gathers metadata for @uri.
 *
 * Returns: (transfer full): a #GUPnPDLNAInformation with the metadata for @uri
 *          on success, NULL otherwise
 */
GUPnPDLNAInformation *
gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
                                         const gchar         *uri,
                                         GError              **err)
{
        GstDiscovererInfo *info;
        GUPnPDLNADiscovererClass *klass =
                GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (discoverer);
        gboolean relaxed = priv->relaxed_mode;
        gboolean extended = priv->extended_mode;

        info = gst_discoverer_discover_uri (GST_DISCOVERER (discoverer),
                                            uri,
                                            err);

        if (info)
                return gupnp_dlna_information_new_from_discoverer_info
                        (info, klass->profiles_list [relaxed][extended]);

        return NULL;
}

/**
 * gupnp_dlna_discoverer_get_profile:
 * @self: The #GUPnPDLNADiscoverer object
 * @name: The name of the DLNA profile to be retrieved
 *
 * Given @name, this finds the corresponding DLNA profile information (stored
 * as a #GUPnPDLNAProfile).
 *
 * Returns: (transfer full): a #GUPnPDLNAProfile on success, NULL otherwise.
 **/
GUPnPDLNAProfile *
gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
                                   const gchar         *name)
{
        GList *i;
        GUPnPDLNADiscovererClass *klass;
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
        gboolean relaxed = priv->relaxed_mode;
        gboolean extended = priv->extended_mode;

        g_return_val_if_fail (self != NULL, NULL);
        klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);

        for (i = klass->profiles_list [relaxed][extended];
             i != NULL;
             i = i->next) {
                GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;

                if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
                        g_object_ref (profile);
                        return profile;
                }
        }

        return NULL;
}

/**
 * gupnp_dlna_discoverer_list_profiles:
 * @self: The #GUPnPDLNADiscoverer whose profile list is required
 *
 * Retuns a list of the all the DLNA profiles supported by @self.
 *
 * Returns: (transfer none) (element-type GUPnPDLNAProfile*): a #GList of
 *          #GUPnPDLNAProfile on success, NULL otherwise.
 **/
const GList *
gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
{
        GUPnPDLNADiscovererClass *klass;
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
        gboolean relaxed = priv->relaxed_mode;
        gboolean extended = priv->extended_mode;

        g_return_val_if_fail (self != NULL, NULL);

        klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);

        return klass->profiles_list [relaxed][extended];
}

/**
 * gupnp_dlna_discoverer_get_relaxed_mode:
 * @self: The #GUPnPDLNADiscoverer object
 *
 * Returns: true if relaxed mode is set and false otherwise
 */
gboolean
gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
{
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);

        return priv->relaxed_mode;
}

/**
 * gupnp_dlna_discoverer_get_extended_mode:
 * @self: The #GUPnPDLNADiscoverer object
 *
 * Returns: true if application is using extended mode and false otherwise
 */
gboolean
gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
{
        GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);

        return priv->extended_mode;
}