Rename gupnp-dlna-load.[hc] to profile-loading.[hc]
[gupnp:gupnp-dlna.git] / libgupnp-dlna / gupnp-dlna-discoverer.c
1 /*
2  * Copyright (C) 2010 Nokia Corporation.
3  *
4  * Authors: Arun Raghavan <arun.raghavan@collabora.co.uk>
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "gupnp-dlna-discoverer.h"
23 #include "gupnp-dlna-marshal.h"
24 #include "profile-loading.h"
25
26 /**
27  * SECTION:gupnp-dlna-discoverer
28  * @short_description: Utility API for discovering DLNA profile/mime type and
29  * other metadata for given media.
30  *
31  * The GUPnPDLNADiscoverer object provides a light-weight wrapper over the
32  * #GstDiscoverer API. The latter provides a simple interface to discover
33  * media metadata given a URI. GUPnPDLNADiscoverer extends this API to also
34  * provide a DLNA profile name and mime type for the media.
35  *
36  * The API provided corresponds very closely to the API provided by
37  * #GstDiscoverer - both synchronous and asynchronous discovery of metadata
38  * are possible.
39  *
40  * The asynchronous mode requires a running #GMainLoop in the default
41  * #GMainContext, where one connects to the various signals, appends the
42  * URIs to be processed and then asks for the discovery to begin.
43  */
44 enum {
45         DONE,
46         SIGNAL_LAST
47 };
48
49 static guint signals[SIGNAL_LAST];
50
51
52 G_DEFINE_TYPE (GUPnPDLNADiscoverer, gupnp_dlna_discoverer, GST_TYPE_DISCOVERER)
53
54 #define GET_PRIVATE(o)                                                  \
55         (G_TYPE_INSTANCE_GET_PRIVATE ((o),                              \
56                                       GUPNP_TYPE_DLNA_DISCOVERER,       \
57                                       GUPnPDLNADiscovererPrivate))
58
59 typedef struct _GUPnPDLNADiscovererPrivate GUPnPDLNADiscovererPrivate;
60
61 struct _GUPnPDLNADiscovererPrivate {
62         gboolean  relaxed_mode;
63         gboolean  extended_mode;
64 };
65
66 enum {
67         PROP_0,
68         PROP_DLNA_RELAXED_MODE,
69         PROP_DLNA_EXTENDED_MODE,
70 };
71
72 static void
73 gupnp_dlna_discoverer_set_property (GObject      *object,
74                                     guint        property_id,
75                                     const GValue *value,
76                                     GParamSpec   *pspec)
77 {
78         GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
79         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
80
81         switch (property_id) {
82                 case PROP_DLNA_RELAXED_MODE:
83                         priv->relaxed_mode = g_value_get_boolean (value);
84                         break;
85
86                 case PROP_DLNA_EXTENDED_MODE:
87                         priv->extended_mode = g_value_get_boolean (value);
88                         break;
89
90                 default:
91                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
92                                                            property_id,
93                                                            pspec);
94                         break;
95         }
96 }
97
98 static void
99 gupnp_dlna_discoverer_get_property (GObject    *object,
100                                     guint      property_id,
101                                     GValue     *value,
102                                     GParamSpec *pspec)
103 {
104         GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
105         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
106
107         switch (property_id) {
108                 case PROP_DLNA_RELAXED_MODE:
109                         g_value_set_boolean (value, priv->relaxed_mode);
110                         break;
111
112                 case PROP_DLNA_EXTENDED_MODE:
113                         g_value_set_boolean (value, priv->extended_mode);
114                         break;
115
116                 default:
117                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
118                                                            property_id,
119                                                            pspec);
120                         break;
121         }
122 }
123
124 static void
125 gupnp_dlna_discoverer_dispose (GObject *object)
126 {
127         G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->dispose (object);
128 }
129
130 static void
131 gupnp_dlna_discoverer_finalize (GObject *object)
132 {
133         G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->finalize (object);
134 }
135
136 static void
137 gupnp_dlna_discovered_cb (GstDiscoverer     *discoverer,
138                           GstDiscovererInfo *info,
139                           GError            *err)
140 {
141         GUPnPDLNAInformation *dlna = NULL;
142         GUPnPDLNADiscovererClass *klass =
143                 GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
144         GUPnPDLNADiscovererPrivate *priv =
145                 GET_PRIVATE (GUPNP_DLNA_DISCOVERER (discoverer));
146         gboolean relaxed = priv->relaxed_mode;
147         gboolean extended = priv->extended_mode;
148
149         if (info)
150                 dlna = gupnp_dlna_information_new_from_discoverer_info
151                                         (info,
152                                          klass->profiles_list
153                                                   [relaxed][extended]);
154
155         g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
156                        signals[DONE], 0, dlna, err);
157
158         if (dlna)
159                 g_object_unref (dlna);
160 }
161
162 static void
163 gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
164 {
165         GObjectClass *object_class = G_OBJECT_CLASS (klass);
166         GParamSpec *pspec;
167
168         g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));
169
170         object_class->get_property = gupnp_dlna_discoverer_get_property;
171         object_class->set_property = gupnp_dlna_discoverer_set_property;
172         object_class->dispose = gupnp_dlna_discoverer_dispose;
173         object_class->finalize = gupnp_dlna_discoverer_finalize;
174
175         /**
176          * GUPnPDLNADiscoverer::relaxed-mode:
177          * @relaxed_mode: setting to true will enable relaxed mode
178          *
179          * The current release does not support relaxed mode yet
180          */
181         pspec = g_param_spec_boolean ("relaxed-mode",
182                                       "Relaxed mode property",
183                                       "Indicates that profile matching should"
184                                       "be strictly compliant with the DLNA "
185                                       "specification",
186                                       FALSE,
187                                       G_PARAM_READWRITE |
188                                       G_PARAM_CONSTRUCT_ONLY);
189         g_object_class_install_property (object_class,
190                                          PROP_DLNA_RELAXED_MODE,
191                                          pspec);
192
193         /**
194          * GUPnPDLNADiscoverer::extended-mode:
195          * @extended: setting true will enable extended profile support
196          *
197          * The current release does not support extended mode yet
198          */
199         pspec = g_param_spec_boolean ("extended-mode",
200                                       "Extended mode property",
201                                       "Indicates support for profiles that are "
202                                       "not part of the DLNA specification",
203                                       FALSE,
204                                       G_PARAM_READWRITE |
205                                       G_PARAM_CONSTRUCT_ONLY);
206         g_object_class_install_property (object_class,
207                                          PROP_DLNA_EXTENDED_MODE,
208                                          pspec);
209
210         /**
211          * GUPnPDLNADiscoverer::done:
212          * @discoverer: the #GUPnPDLNADiscoverer
213          * @dlna: the results as #GUPnPDLNAInformation
214          * @err: contains details of the error if discovery fails, else is NULL
215          *
216          * Will be emitted when all information on a URI could be discovered.
217          *
218          * The reciever must unref @dlna with when done using it.
219          */
220         signals[DONE] =
221                 g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
222                               G_SIGNAL_RUN_LAST,
223                               G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
224                               NULL, NULL,
225                               gupnp_dlna_marshal_VOID__OBJECT_BOXED,
226                               G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
227                               GST_TYPE_G_ERROR);
228
229         /* Load DLNA profiles from disk */
230         if (g_type_from_name ("GstElement")) {
231                 klass->profiles_list [0][0]
232                         = gupnp_dlna_load_profiles_from_disk (FALSE,
233                                                               FALSE);
234                 klass->profiles_list [0][1]
235                         = gupnp_dlna_load_profiles_from_disk (FALSE,
236                                                               TRUE);
237                 klass->profiles_list [1][0]
238                         = gupnp_dlna_load_profiles_from_disk (TRUE,
239                                                               FALSE);
240                 klass->profiles_list [1][1]
241                         = gupnp_dlna_load_profiles_from_disk (TRUE,
242                                                               TRUE);
243         } else {
244                 klass->profiles_list [0][0] = NULL;
245                 klass->profiles_list [0][1] = NULL;
246                 klass->profiles_list [1][0] = NULL;
247                 klass->profiles_list [1][1] = NULL;
248                 g_warning ("GStreamer has not yet been initialised. You need "
249                            "to call gst_init()/gst_init_check() for discovery "
250                            "to work.");
251         }
252 }
253
254 static void
255 gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
256 {
257         g_signal_connect (&self->parent,
258                           "discovered",
259                           G_CALLBACK (gupnp_dlna_discovered_cb),
260                           NULL);
261 }
262
263 /**
264  * gupnp_dlna_discoverer_new:
265  * @timeout: default discovery timeout, in nanoseconds
266  * @relaxed_mode: set to TRUE, to enable relaxed mode support. FALSE otherwise
267  * @extended_mode: set to TRUE, to enable extended mode support. FALSE otherwise
268  *
269  * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
270  * value.
271  *
272  * Returns: A new #GUPnPDLNADiscoverer object.
273  */
274 GUPnPDLNADiscoverer*
275 gupnp_dlna_discoverer_new (GstClockTime timeout,
276                            gboolean     relaxed_mode,
277                            gboolean     extended_mode)
278 {
279         return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
280                              "timeout", timeout,
281                              "relaxed-mode", relaxed_mode,
282                              "extended-mode", extended_mode,
283                              NULL);
284 }
285
286 /* Asynchronous API */
287
288 /**
289  * gupnp_dlna_discoverer_start:
290  * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
291  *
292  * Allows asynchronous discovery of URIs to begin.
293  */
294
295 /**
296  * gupnp_dlna_discoverer_stop:
297  * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
298  *
299  * Stops asynchronous discovery of URIs.
300  */
301
302 /**
303  * gupnp_dlna_discoverer_discover_uri:
304  * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
305  * @uri: URI to gather metadata for
306  *
307  * Queues @uri for metadata discovery. When discovery is completed, the
308  * "discovered" signal is emitted on @discoverer.
309  *
310  * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
311  */
312 gboolean
313 gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer,
314                                     const gchar         *uri)
315 {
316         return gst_discoverer_discover_uri_async (GST_DISCOVERER (discoverer),
317                                                   uri);
318 }
319
320 /* Synchronous API */
321
322 /**
323  * gupnp_dlna_discoverer_discover_uri_sync:
324  * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
325  * @uri: URI to gather metadata for
326  * @err: contains details of the error if discovery fails, else is NULL
327  *
328  * Synchronously gathers metadata for @uri.
329  *
330  * Returns: a #GUPnPDLNAInformation with the metadata for @uri on success, NULL
331  *          otherwise
332  */
333 GUPnPDLNAInformation *
334 gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
335                                          const gchar         *uri,
336                                          GError              **err)
337 {
338         GstDiscovererInfo *info;
339         GUPnPDLNADiscovererClass *klass =
340                 GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
341         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (discoverer);
342         gboolean relaxed = priv->relaxed_mode;
343         gboolean extended = priv->extended_mode;
344
345         info = gst_discoverer_discover_uri (GST_DISCOVERER (discoverer),
346                                             uri,
347                                             err);
348
349         if (info)
350                 return gupnp_dlna_information_new_from_discoverer_info
351                         (info, klass->profiles_list [relaxed][extended]);
352
353         return NULL;
354 }
355
356 /**
357  * gupnp_dlna_discoverer_get_profile:
358  * @self: The #GUPnPDLNADiscoverer object
359  * @name: The name of the DLNA profile to be retrieved
360  *
361  * Given @name, this finds the corresponding DLNA profile information (stored
362  * as a #GUPnPDLNAProfile).
363  *
364  * Returns: a #GUPnPDLNAProfile on success, NULL otherwise.
365  **/
366 GUPnPDLNAProfile *
367 gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
368                                    const gchar         *name)
369 {
370         GList *i;
371         GUPnPDLNADiscovererClass *klass;
372         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
373         gboolean relaxed = priv->relaxed_mode;
374         gboolean extended = priv->extended_mode;
375
376         g_return_val_if_fail (self != NULL, NULL);
377         klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
378
379         for (i = klass->profiles_list [relaxed][extended];
380              i != NULL;
381              i = i->next) {
382                 GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;
383
384                 if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
385                         g_object_ref (profile);
386                         return profile;
387                 }
388         }
389
390         return NULL;
391 }
392
393 /**
394  * gupnp_dlna_discoverer_list_profiles:
395  * @self: The #GUPnPDLNADiscoverer whose profile list is required
396  *
397  * Retuns a list of the all the DLNA profiles supported by @self.
398  *
399  * Returns: a #GList of #GUPnPDLNAProfile on success, NULL otherwise.
400  **/
401 const GList *
402 gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
403 {
404         GUPnPDLNADiscovererClass *klass;
405         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
406         gboolean relaxed = priv->relaxed_mode;
407         gboolean extended = priv->extended_mode;
408
409         g_return_val_if_fail (self != NULL, NULL);
410
411         klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
412
413         return klass->profiles_list [relaxed][extended];
414 }
415
416 /**
417  * gupnp_dlna_discoverer_get_relaxed_mode:
418  * @self: The #GUPnPDLNADiscoverer object
419  *
420  * Returns: true if relaxed mode is set and false otherwise
421  */
422 gboolean
423 gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
424 {
425         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
426
427         return priv->relaxed_mode;
428 }
429
430 /**
431  * gupnp_dlna_discoverer_get_extended_mode:
432  * @self: The #GUPnPDLNADiscoverer object
433  *
434  * Returns: true if application is using extended mode and false otherwise
435  */
436 gboolean
437 gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
438 {
439         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
440
441         return priv->extended_mode;
442 }