libgupnp-dlna: Don't free the profiles list
[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 Library 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  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library 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 "gupnp-dlna-load.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 gupnp_dlna_discovered_cb (GstDiscoverer            *discoverer,
137                                       GstDiscovererInformation *info,
138                                       GError                   *err)
139 {
140         GUPnPDLNAInformation *dlna = NULL;
141         GUPnPDLNADiscovererClass *klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
142
143         if (info)
144                 dlna = gupnp_dlna_information_new_from_discoverer_info (info,
145                                                                         klass->profiles_list);
146
147         g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
148                        signals[DONE], 0, dlna, err);
149
150         if (dlna)
151                 g_object_unref (dlna);
152 }
153
154 static void
155 gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
156 {
157         GObjectClass *object_class = G_OBJECT_CLASS (klass);
158         GParamSpec *pspec;
159
160         g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));
161
162         object_class->get_property = gupnp_dlna_discoverer_get_property;
163         object_class->set_property = gupnp_dlna_discoverer_set_property;
164         object_class->dispose = gupnp_dlna_discoverer_dispose;
165         object_class->finalize = gupnp_dlna_discoverer_finalize;
166
167         /**
168          * GUPnPDLNADiscoverer::relaxed-mode:
169          * @relaxed_mode: setting to true will enable relaxed mode
170          *
171          * The current release does not support relaxed mode yet
172          */
173         pspec = g_param_spec_boolean ("relaxed-mode",
174                                       "Relaxed mode property",
175                                       "Indicates that profile matching should"
176                                       "be strictly compliant with the DLNA "
177                                       "specification",
178                                       FALSE,
179                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
180         g_object_class_install_property (object_class,
181                                          PROP_DLNA_RELAXED_MODE,
182                                          pspec);
183
184         /**
185          * GUPnPDLNADiscoverer::extended-mode:
186          * @extended: setting true will enable extended profile support
187          *
188          * The current release does not support extended mode yet
189          */
190         pspec = g_param_spec_boolean ("extended-mode",
191                                       "Extended mode property",
192                                       "Indicates support for profiles that are "
193                                       "not part of the DLNA specification",
194                                       FALSE,
195                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
196         g_object_class_install_property (object_class,
197                                          PROP_DLNA_EXTENDED_MODE,
198                                          pspec);
199
200         /**
201          * GUPnPDLNADiscoverer::done:
202          * @discoverer: the #GUPnPDLNADiscoverer
203          * @dlna: the results as #GUPnPDLNAInformation
204          * @err: contains details of the error if discovery fails, else is NULL
205          *
206          * Will be emitted when all information on a URI could be discovered.
207          *
208          * The reciever must unref @dlna with when done using it.
209          */
210         signals[DONE] =
211                 g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
212                               G_SIGNAL_RUN_LAST,
213                               G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
214                               NULL, NULL,
215                               gupnp_dlna_marshal_VOID__OBJECT_BOXED,
216                               G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
217                               GST_TYPE_G_ERROR);
218
219         /* Load DLNA profiles from disk */
220         if (g_type_from_name ("GstElement"))
221                 klass->profiles_list = gupnp_dlna_load_profiles_from_disk ();
222         else {
223                 klass->profiles_list = NULL;
224                 g_warning ("GStreamer has not yet been initialised. You need "
225                            "to call gst_init()/gst_init_check() for discovery "
226                            "to work.");
227         }
228 }
229
230 static void
231 gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
232 {
233         g_signal_connect (&self->parent,
234                           "discovered",
235                           G_CALLBACK (gupnp_dlna_discovered_cb),
236                           NULL);
237 }
238
239 /**
240  * gupnp_dlna_discoverer_new:
241  * @timeout: default discovery timeout, in nanoseconds
242  *
243  * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
244  * value.
245  *
246  * Returns: A new #GUPnPDLNADiscoverer object.
247  */
248 GUPnPDLNADiscoverer*
249 gupnp_dlna_discoverer_new (GstClockTime timeout,
250                            gboolean     relaxed_mode,
251                            gboolean     extended_mode)
252 {
253         return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
254                              "timeout", timeout,
255                              "relaxed-mode", relaxed_mode,
256                              "extended-mode", extended_mode,
257                              NULL);
258 }
259
260 /* Asynchronous API */
261
262 /**
263  * gupnp_dlna_discoverer_start:
264  * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
265  *
266  * Allows asynchronous discovery of URIs to begin.
267  */
268
269 /**
270  * gupnp_dlna_discoverer_stop:
271  * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
272  *
273  * Stops asynchronous discovery of URIs.
274  */
275
276 /**
277  * gupnp_dlna_discoverer_discover_uri:
278  * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
279  * @uri: URI to gather metadata for
280  *
281  * Queues @uri for metadata discovery. When discovery is completed, the
282  * "discovered" signal is emitted on @discoverer.
283  *
284  * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
285  */
286 gboolean
287 gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer, gchar *uri)
288 {
289         return gst_discoverer_append_uri (GST_DISCOVERER (discoverer), uri);
290 }
291
292 /* Synchronous API */
293
294 /**
295  * gupnp_dlna_discoverer_discover_uri_sync:
296  * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
297  * @uri: URI to gather metadata for
298  * @err: contains details of the error if discovery fails, else is NULL
299  *
300  * Synchronously gathers metadata for @uri.
301  *
302  * Returns: a #GUPnPDLNAInformation with the metadata for @uri on success, NULL
303  *          otherwise
304  */
305 GUPnPDLNAInformation *
306 gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
307                                          gchar               *uri,
308                                          GError              **err)
309 {
310         GstDiscovererInformation *info;
311         GUPnPDLNADiscovererClass *klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
312
313         info = gst_discoverer_discover_uri (GST_DISCOVERER (discoverer),
314                                             uri,
315                                             err);
316
317         if (info)
318                 return gupnp_dlna_information_new_from_discoverer_info (info,
319                                                                         klass->profiles_list);
320
321         return NULL;
322 }
323
324 /**
325  * gupnp_dlna_discoverer_get_profile:
326  * @self: The #GUPnPDLNADiscoverer object
327  * @name: The name of the DLNA profile to be retrieved
328  *
329  * Given @name, this finds the corresponding DLNA profile information (stored
330  * as a #GUPnPDLNAProfile).
331  *
332  * Returns: a #GUPnPDLNAProfile on success, NULL otherwise.
333  **/
334 GUPnPDLNAProfile *
335 gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
336                                    const gchar         *name)
337 {
338         GList *i;
339         GUPnPDLNADiscovererClass *klass;
340
341         g_return_val_if_fail (self != NULL, NULL);
342         klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
343
344         for (i = klass->profiles_list; i != NULL; i = i->next) {
345                 GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;
346
347                 if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
348                         g_object_ref (profile);
349                         return profile;
350                 }
351         }
352
353         return NULL;
354 }
355
356 /**
357  * gupnp_dlna_discoverer_list_profiles:
358  * @self: The #GUPnPDLNADiscoverer whose profile list is required
359  *
360  * Retuns a list of the all the DLNA profiles supported by @self.
361  *
362  * Returns: a #GList of #GUPnPDLNAProfile on success, NULL otherwise.
363  **/
364 const GList *
365 gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
366 {
367         GUPnPDLNADiscovererClass *klass;
368
369         g_return_val_if_fail (self != NULL, NULL);
370
371         klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
372
373         return klass->profiles_list;
374 }
375
376 /**
377  * gupnp_dlna_discoverer_get_relaxed_mode:
378  * @self: The #GUPnPDLNADiscoverer object
379  *
380  * Returns: true if relaxed mode is set and false otherwise
381  */
382 gboolean
383 gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
384 {
385         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
386         return priv->relaxed_mode;
387 }
388
389 /**
390  * gupnp_dlna_discoverer_get_extended_mode:
391  * @self: The #GUPnPDLNADiscoverer object
392  *
393  * Returns: true if application is using extended mode and false otherwise
394  */
395 gboolean
396 gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
397 {
398         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
399         return priv->extended_mode;
400 }