doc: Update NEWS for 0.4.1
[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 "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, \
53                gupnp_dlna_discoverer, \
54                GST_TYPE_DISCOVERER_INTERNAL)
55
56 #define GET_PRIVATE(o) \
57   (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
58                                 GUPNP_TYPE_DLNA_DISCOVERER, \
59                                 GUPnPDLNADiscovererPrivate))
60
61 typedef struct _GUPnPDLNADiscovererPrivate GUPnPDLNADiscovererPrivate;
62
63 struct _GUPnPDLNADiscovererPrivate {
64         gboolean  relaxed_mode;
65         gboolean  extended_mode;
66 };
67
68 enum {
69         PROP_0,
70         PROP_DLNA_RELAXED_MODE,
71         PROP_DLNA_EXTENDED_MODE,
72 };
73
74 static void
75 gupnp_dlna_discoverer_set_property (GObject      *object,
76                                     guint        property_id,
77                                     const GValue *value,
78                                     GParamSpec   *pspec)
79 {
80         GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
81         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
82
83         switch (property_id) {
84                 case PROP_DLNA_RELAXED_MODE:
85                         priv->relaxed_mode = g_value_get_boolean (value);
86                         break;
87
88                 case PROP_DLNA_EXTENDED_MODE:
89                         priv->extended_mode = g_value_get_boolean (value);
90                         break;
91
92                 default:
93                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
94                                                            property_id,
95                                                            pspec);
96                         break;
97         }
98 }
99
100 static void
101 gupnp_dlna_discoverer_get_property (GObject    *object,
102                                     guint      property_id,
103                                     GValue     *value,
104                                     GParamSpec *pspec)
105 {
106         GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
107         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
108
109         switch (property_id) {
110                 case PROP_DLNA_RELAXED_MODE:
111                         g_value_set_boolean (value, priv->relaxed_mode);
112                         break;
113
114                 case PROP_DLNA_EXTENDED_MODE:
115                         g_value_set_boolean (value, priv->extended_mode);
116                         break;
117
118                 default:
119                         G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
120                                                            property_id,
121                                                            pspec);
122                         break;
123         }
124 }
125
126 static void
127 gupnp_dlna_discoverer_dispose (GObject *object)
128 {
129         G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->dispose (object);
130 }
131
132 static void
133 gupnp_dlna_discoverer_finalize (GObject *object)
134 {
135         G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->finalize (object);
136 }
137
138 static void gupnp_dlna_discovered_cb (GstDiscovererInternal    *discoverer,
139                                       GstDiscovererInformation *info,
140                                       GError                   *err)
141 {
142         GUPnPDLNAInformation *dlna = NULL;
143         GUPnPDLNADiscovererClass *klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
144         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (GUPNP_DLNA_DISCOVERER (discoverer));
145         gboolean relaxed = priv->relaxed_mode;
146         gboolean extended = priv->extended_mode;
147
148         if (info)
149                 dlna = gupnp_dlna_information_new_from_discoverer_info (info,
150                                                  klass->profiles_list [relaxed][extended]);
151
152         g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
153                        signals[DONE], 0, dlna, err);
154
155         if (dlna)
156                 g_object_unref (dlna);
157 }
158
159 static void
160 gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
161 {
162         GObjectClass *object_class = G_OBJECT_CLASS (klass);
163         GParamSpec *pspec;
164
165         g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));
166
167         object_class->get_property = gupnp_dlna_discoverer_get_property;
168         object_class->set_property = gupnp_dlna_discoverer_set_property;
169         object_class->dispose = gupnp_dlna_discoverer_dispose;
170         object_class->finalize = gupnp_dlna_discoverer_finalize;
171
172         /**
173          * GUPnPDLNADiscoverer::relaxed-mode:
174          * @relaxed_mode: setting to true will enable relaxed mode
175          *
176          * The current release does not support relaxed mode yet
177          */
178         pspec = g_param_spec_boolean ("relaxed-mode",
179                                       "Relaxed mode property",
180                                       "Indicates that profile matching should"
181                                       "be strictly compliant with the DLNA "
182                                       "specification",
183                                       FALSE,
184                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
185         g_object_class_install_property (object_class,
186                                          PROP_DLNA_RELAXED_MODE,
187                                          pspec);
188
189         /**
190          * GUPnPDLNADiscoverer::extended-mode:
191          * @extended: setting true will enable extended profile support
192          *
193          * The current release does not support extended mode yet
194          */
195         pspec = g_param_spec_boolean ("extended-mode",
196                                       "Extended mode property",
197                                       "Indicates support for profiles that are "
198                                       "not part of the DLNA specification",
199                                       FALSE,
200                                       G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
201         g_object_class_install_property (object_class,
202                                          PROP_DLNA_EXTENDED_MODE,
203                                          pspec);
204
205         /**
206          * GUPnPDLNADiscoverer::done:
207          * @discoverer: the #GUPnPDLNADiscoverer
208          * @dlna: the results as #GUPnPDLNAInformation
209          * @err: contains details of the error if discovery fails, else is NULL
210          *
211          * Will be emitted when all information on a URI could be discovered.
212          *
213          * The reciever must unref @dlna with when done using it.
214          */
215         signals[DONE] =
216                 g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
217                               G_SIGNAL_RUN_LAST,
218                               G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
219                               NULL, NULL,
220                               gupnp_dlna_marshal_VOID__OBJECT_BOXED,
221                               G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
222                               GST_TYPE_G_ERROR);
223
224         /* Load DLNA profiles from disk */
225         if (g_type_from_name ("GstElement")) {
226                 klass->profiles_list [0][0]
227                         = gupnp_dlna_load_profiles_from_disk (FALSE,
228                                                               FALSE);
229                 klass->profiles_list [0][1]
230                         = gupnp_dlna_load_profiles_from_disk (FALSE,
231                                                               TRUE);
232                 klass->profiles_list [1][0]
233                         = gupnp_dlna_load_profiles_from_disk (TRUE,
234                                                               FALSE);
235                 klass->profiles_list [1][1]
236                         = gupnp_dlna_load_profiles_from_disk (TRUE,
237                                                               TRUE);
238         } else {
239                 klass->profiles_list [0][0] = NULL;
240                 klass->profiles_list [0][1] = NULL;
241                 klass->profiles_list [1][0] = NULL;
242                 klass->profiles_list [1][1] = NULL;
243                 g_warning ("GStreamer has not yet been initialised. You need "
244                            "to call gst_init()/gst_init_check() for discovery "
245                            "to work.");
246         }
247 }
248
249 static void
250 gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
251 {
252         g_signal_connect (&self->parent,
253                           "discovered",
254                           G_CALLBACK (gupnp_dlna_discovered_cb),
255                           NULL);
256 }
257
258 /**
259  * gupnp_dlna_discoverer_new:
260  * @timeout: default discovery timeout, in nanoseconds
261  *
262  * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
263  * value.
264  *
265  * Returns: A new #GUPnPDLNADiscoverer object.
266  */
267 GUPnPDLNADiscoverer*
268 gupnp_dlna_discoverer_new (GstClockTime timeout,
269                            gboolean     relaxed_mode,
270                            gboolean     extended_mode)
271 {
272         return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
273                              "timeout", timeout,
274                              "relaxed-mode", relaxed_mode,
275                              "extended-mode", extended_mode,
276                              NULL);
277 }
278
279 /* Asynchronous API */
280
281 /**
282  * gupnp_dlna_discoverer_start:
283  * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
284  *
285  * Allows asynchronous discovery of URIs to begin.
286  */
287
288 /**
289  * gupnp_dlna_discoverer_stop:
290  * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
291  *
292  * Stops asynchronous discovery of URIs.
293  */
294
295 /**
296  * gupnp_dlna_discoverer_discover_uri:
297  * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
298  * @uri: URI to gather metadata for
299  *
300  * Queues @uri for metadata discovery. When discovery is completed, the
301  * "discovered" signal is emitted on @discoverer.
302  *
303  * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
304  */
305 gboolean
306 gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer, gchar *uri)
307 {
308         return gst_discoverer_internal_append_uri (
309                         GST_DISCOVERER_INTERNAL (discoverer), uri);
310 }
311
312 /* Synchronous API */
313
314 /**
315  * gupnp_dlna_discoverer_discover_uri_sync:
316  * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
317  * @uri: URI to gather metadata for
318  * @err: contains details of the error if discovery fails, else is NULL
319  *
320  * Synchronously gathers metadata for @uri.
321  *
322  * Returns: a #GUPnPDLNAInformation with the metadata for @uri on success, NULL
323  *          otherwise
324  */
325 GUPnPDLNAInformation *
326 gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
327                                          gchar               *uri,
328                                          GError              **err)
329 {
330         GstDiscovererInformation *info;
331         GUPnPDLNADiscovererClass *klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
332         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (discoverer);
333         gboolean relaxed = priv->relaxed_mode;
334         gboolean extended = priv->extended_mode;
335
336         info = gst_discoverer_internal_discover_uri (
337                         GST_DISCOVERER_INTERNAL (discoverer),
338                         uri,
339                         err);
340
341         if (info)
342                 return gupnp_dlna_information_new_from_discoverer_info (info,
343                                      klass->profiles_list [relaxed][extended]);
344
345         return NULL;
346 }
347
348 /**
349  * gupnp_dlna_discoverer_get_profile:
350  * @self: The #GUPnPDLNADiscoverer object
351  * @name: The name of the DLNA profile to be retrieved
352  *
353  * Given @name, this finds the corresponding DLNA profile information (stored
354  * as a #GUPnPDLNAProfile).
355  *
356  * Returns: a #GUPnPDLNAProfile on success, NULL otherwise.
357  **/
358 GUPnPDLNAProfile *
359 gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
360                                    const gchar         *name)
361 {
362         GList *i;
363         GUPnPDLNADiscovererClass *klass;
364         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
365         gboolean relaxed = priv->relaxed_mode;
366         gboolean extended = priv->extended_mode;
367
368         g_return_val_if_fail (self != NULL, NULL);
369         klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
370
371         for (i = klass->profiles_list [relaxed][extended];
372              i != NULL;
373              i = i->next) {
374                 GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;
375
376                 if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
377                         g_object_ref (profile);
378                         return profile;
379                 }
380         }
381
382         return NULL;
383 }
384
385 /**
386  * gupnp_dlna_discoverer_list_profiles:
387  * @self: The #GUPnPDLNADiscoverer whose profile list is required
388  *
389  * Retuns a list of the all the DLNA profiles supported by @self.
390  *
391  * Returns: a #GList of #GUPnPDLNAProfile on success, NULL otherwise.
392  **/
393 const GList *
394 gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
395 {
396         GUPnPDLNADiscovererClass *klass;
397         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
398         gboolean relaxed = priv->relaxed_mode;
399         gboolean extended = priv->extended_mode;
400
401         g_return_val_if_fail (self != NULL, NULL);
402
403         klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
404
405         return klass->profiles_list [relaxed][extended];
406 }
407
408 /**
409  * gupnp_dlna_discoverer_get_relaxed_mode:
410  * @self: The #GUPnPDLNADiscoverer object
411  *
412  * Returns: true if relaxed mode is set and false otherwise
413  */
414 gboolean
415 gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
416 {
417         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
418         return priv->relaxed_mode;
419 }
420
421 /**
422  * gupnp_dlna_discoverer_get_extended_mode:
423  * @self: The #GUPnPDLNADiscoverer object
424  *
425  * Returns: true if application is using extended mode and false otherwise
426  */
427 gboolean
428 gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
429 {
430         GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
431         return priv->extended_mode;
432 }