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 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.
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.
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.
22 #include "gupnp-dlna-discoverer.h"
23 #include "gupnp-dlna-marshal.h"
24 #include "gupnp-dlna-load.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);
136 static void gupnp_dlna_discovered_cb (GstDiscoverer *discoverer,
137 GstDiscovererInformation *info,
140 GUPnPDLNAInformation *dlna = NULL;
141 GUPnPDLNADiscovererClass *klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
144 dlna = gupnp_dlna_information_new_from_discoverer_info (info,
145 klass->profiles_list);
147 g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
148 signals[DONE], 0, dlna, err);
151 g_object_unref (dlna);
155 gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
157 GObjectClass *object_class = G_OBJECT_CLASS (klass);
160 g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));
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;
168 * GUPnPDLNADiscoverer::relaxed-mode:
169 * @relaxed_mode: setting to true will enable relaxed mode
171 * The current release does not support relaxed mode yet
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 "
179 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
180 g_object_class_install_property (object_class,
181 PROP_DLNA_RELAXED_MODE,
185 * GUPnPDLNADiscoverer::extended-mode:
186 * @extended: setting true will enable extended profile support
188 * The current release does not support extended mode yet
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",
195 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
196 g_object_class_install_property (object_class,
197 PROP_DLNA_EXTENDED_MODE,
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
206 * Will be emitted when all information on a URI could be discovered.
208 * The reciever must unref @dlna with when done using it.
211 g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
213 G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
215 gupnp_dlna_marshal_VOID__OBJECT_BOXED,
216 G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
219 /* Load DLNA profiles from disk */
220 if (g_type_from_name ("GstElement"))
221 klass->profiles_list = gupnp_dlna_load_profiles_from_disk ();
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 "
231 gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
233 g_signal_connect (&self->parent,
235 G_CALLBACK (gupnp_dlna_discovered_cb),
240 * gupnp_dlna_discoverer_new:
241 * @timeout: default discovery timeout, in nanoseconds
243 * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
246 * Returns: A new #GUPnPDLNADiscoverer object.
249 gupnp_dlna_discoverer_new (GstClockTime timeout,
250 gboolean relaxed_mode,
251 gboolean extended_mode)
253 return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
255 "relaxed-mode", relaxed_mode,
256 "extended-mode", extended_mode,
260 /* Asynchronous API */
263 * gupnp_dlna_discoverer_start:
264 * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
266 * Allows asynchronous discovery of URIs to begin.
270 * gupnp_dlna_discoverer_stop:
271 * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
273 * Stops asynchronous discovery of URIs.
277 * gupnp_dlna_discoverer_discover_uri:
278 * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
279 * @uri: URI to gather metadata for
281 * Queues @uri for metadata discovery. When discovery is completed, the
282 * "discovered" signal is emitted on @discoverer.
284 * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
287 gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer, gchar *uri)
289 return gst_discoverer_append_uri (GST_DISCOVERER (discoverer), uri);
292 /* Synchronous API */
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
300 * Synchronously gathers metadata for @uri.
302 * Returns: a #GUPnPDLNAInformation with the metadata for @uri on success, NULL
305 GUPnPDLNAInformation *
306 gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
310 GstDiscovererInformation *info;
311 GUPnPDLNADiscovererClass *klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (discoverer);
313 info = gst_discoverer_discover_uri (GST_DISCOVERER (discoverer),
318 return gupnp_dlna_information_new_from_discoverer_info (info,
319 klass->profiles_list);
325 * gupnp_dlna_discoverer_get_profile:
326 * @self: The #GUPnPDLNADiscoverer object
327 * @name: The name of the DLNA profile to be retrieved
329 * Given @name, this finds the corresponding DLNA profile information (stored
330 * as a #GUPnPDLNAProfile).
332 * Returns: a #GUPnPDLNAProfile on success, NULL otherwise.
335 gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
339 GUPnPDLNADiscovererClass *klass;
341 g_return_val_if_fail (self != NULL, NULL);
342 klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
344 for (i = klass->profiles_list; i != NULL; i = i->next) {
345 GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;
347 if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
348 g_object_ref (profile);
357 * gupnp_dlna_discoverer_list_profiles:
358 * @self: The #GUPnPDLNADiscoverer whose profile list is required
360 * Retuns a list of the all the DLNA profiles supported by @self.
362 * Returns: a #GList of #GUPnPDLNAProfile on success, NULL otherwise.
365 gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
367 GUPnPDLNADiscovererClass *klass;
369 g_return_val_if_fail (self != NULL, NULL);
371 klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
373 return klass->profiles_list;
377 * gupnp_dlna_discoverer_get_relaxed_mode:
378 * @self: The #GUPnPDLNADiscoverer object
380 * Returns: true if relaxed mode is set and false otherwise
383 gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
385 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
386 return priv->relaxed_mode;
390 * gupnp_dlna_discoverer_get_extended_mode:
391 * @self: The #GUPnPDLNADiscoverer object
393 * Returns: true if application is using extended mode and false otherwise
396 gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
398 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
399 return priv->extended_mode;