2 * Copyright (C) 2010 Nokia Corporation.
4 * Authors: Arun Raghavan <arun.raghavan@collabora.co.uk>
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.
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.
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.
22 #include "gupnp-dlna-discoverer.h"
23 #include "gupnp-dlna-marshal.h"
24 #include "profile-loading.h"
27 * SECTION:gupnp-dlna-discoverer
28 * @short_description: Utility API for discovering DLNA profile/mime type and
29 * other metadata for given media.
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.
36 * The API provided corresponds very closely to the API provided by
37 * #GstDiscoverer - both synchronous and asynchronous discovery of metadata
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.
49 static guint signals[SIGNAL_LAST];
52 G_DEFINE_TYPE (GUPnPDLNADiscoverer, gupnp_dlna_discoverer, GST_TYPE_DISCOVERER)
54 #define GET_PRIVATE(o) \
55 (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
56 GUPNP_TYPE_DLNA_DISCOVERER, \
57 GUPnPDLNADiscovererPrivate))
59 typedef struct _GUPnPDLNADiscovererPrivate GUPnPDLNADiscovererPrivate;
61 struct _GUPnPDLNADiscovererPrivate {
62 gboolean relaxed_mode;
63 gboolean extended_mode;
68 PROP_DLNA_RELAXED_MODE,
69 PROP_DLNA_EXTENDED_MODE,
73 gupnp_dlna_discoverer_set_property (GObject *object,
78 GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
79 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
81 switch (property_id) {
82 case PROP_DLNA_RELAXED_MODE:
83 priv->relaxed_mode = g_value_get_boolean (value);
86 case PROP_DLNA_EXTENDED_MODE:
87 priv->extended_mode = g_value_get_boolean (value);
91 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
99 gupnp_dlna_discoverer_get_property (GObject *object,
104 GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
105 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
107 switch (property_id) {
108 case PROP_DLNA_RELAXED_MODE:
109 g_value_set_boolean (value, priv->relaxed_mode);
112 case PROP_DLNA_EXTENDED_MODE:
113 g_value_set_boolean (value, priv->extended_mode);
117 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
125 gupnp_dlna_discoverer_dispose (GObject *object)
127 G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->dispose (object);
131 gupnp_dlna_discoverer_finalize (GObject *object)
133 G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->finalize (object);
137 gupnp_dlna_discovered_cb (GstDiscoverer *discoverer,
138 GstDiscovererInfo *info,
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;
150 dlna = gupnp_dlna_information_new_from_discoverer_info
153 [relaxed][extended]);
155 g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
156 signals[DONE], 0, dlna, err);
159 g_object_unref (dlna);
163 gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
165 GObjectClass *object_class = G_OBJECT_CLASS (klass);
168 g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));
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;
176 * GUPnPDLNADiscoverer::relaxed-mode:
177 * @relaxed_mode: setting to true will enable relaxed mode
179 * The current release does not support relaxed mode yet
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 "
188 G_PARAM_CONSTRUCT_ONLY);
189 g_object_class_install_property (object_class,
190 PROP_DLNA_RELAXED_MODE,
194 * GUPnPDLNADiscoverer::extended-mode:
195 * @extended: setting true will enable extended profile support
197 * The current release does not support extended mode yet
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",
205 G_PARAM_CONSTRUCT_ONLY);
206 g_object_class_install_property (object_class,
207 PROP_DLNA_EXTENDED_MODE,
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
216 * Will be emitted when all information on a URI could be discovered.
218 * The reciever must unref @dlna with when done using it.
221 g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
223 G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
225 gupnp_dlna_marshal_VOID__OBJECT_BOXED,
226 G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
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,
234 klass->profiles_list [0][1]
235 = gupnp_dlna_load_profiles_from_disk (FALSE,
237 klass->profiles_list [1][0]
238 = gupnp_dlna_load_profiles_from_disk (TRUE,
240 klass->profiles_list [1][1]
241 = gupnp_dlna_load_profiles_from_disk (TRUE,
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 "
255 gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
257 g_signal_connect (&self->parent,
259 G_CALLBACK (gupnp_dlna_discovered_cb),
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
269 * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
272 * Returns: A new #GUPnPDLNADiscoverer object.
275 gupnp_dlna_discoverer_new (GstClockTime timeout,
276 gboolean relaxed_mode,
277 gboolean extended_mode)
279 return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
281 "relaxed-mode", relaxed_mode,
282 "extended-mode", extended_mode,
286 /* Asynchronous API */
289 * gupnp_dlna_discoverer_start:
290 * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
292 * Allows asynchronous discovery of URIs to begin.
296 * gupnp_dlna_discoverer_stop:
297 * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
299 * Stops asynchronous discovery of URIs.
303 * gupnp_dlna_discoverer_discover_uri:
304 * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
305 * @uri: URI to gather metadata for
307 * Queues @uri for metadata discovery. When discovery is completed, the
308 * "discovered" signal is emitted on @discoverer.
310 * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
313 gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer,
316 return gst_discoverer_discover_uri_async (GST_DISCOVERER (discoverer),
320 /* Synchronous API */
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
328 * Synchronously gathers metadata for @uri.
330 * Returns: a #GUPnPDLNAInformation with the metadata for @uri on success, NULL
333 GUPnPDLNAInformation *
334 gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
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;
345 info = gst_discoverer_discover_uri (GST_DISCOVERER (discoverer),
350 return gupnp_dlna_information_new_from_discoverer_info
351 (info, klass->profiles_list [relaxed][extended]);
357 * gupnp_dlna_discoverer_get_profile:
358 * @self: The #GUPnPDLNADiscoverer object
359 * @name: The name of the DLNA profile to be retrieved
361 * Given @name, this finds the corresponding DLNA profile information (stored
362 * as a #GUPnPDLNAProfile).
364 * Returns: a #GUPnPDLNAProfile on success, NULL otherwise.
367 gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
371 GUPnPDLNADiscovererClass *klass;
372 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
373 gboolean relaxed = priv->relaxed_mode;
374 gboolean extended = priv->extended_mode;
376 g_return_val_if_fail (self != NULL, NULL);
377 klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
379 for (i = klass->profiles_list [relaxed][extended];
382 GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;
384 if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
385 g_object_ref (profile);
394 * gupnp_dlna_discoverer_list_profiles:
395 * @self: The #GUPnPDLNADiscoverer whose profile list is required
397 * Retuns a list of the all the DLNA profiles supported by @self.
399 * Returns: a #GList of #GUPnPDLNAProfile on success, NULL otherwise.
402 gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
404 GUPnPDLNADiscovererClass *klass;
405 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
406 gboolean relaxed = priv->relaxed_mode;
407 gboolean extended = priv->extended_mode;
409 g_return_val_if_fail (self != NULL, NULL);
411 klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
413 return klass->profiles_list [relaxed][extended];
417 * gupnp_dlna_discoverer_get_relaxed_mode:
418 * @self: The #GUPnPDLNADiscoverer object
420 * Returns: true if relaxed mode is set and false otherwise
423 gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
425 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
427 return priv->relaxed_mode;
431 * gupnp_dlna_discoverer_get_extended_mode:
432 * @self: The #GUPnPDLNADiscoverer object
434 * Returns: true if application is using extended mode and false otherwise
437 gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
439 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
441 return priv->extended_mode;