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 "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, \
53 gupnp_dlna_discoverer, \
54 GST_TYPE_DISCOVERER_INTERNAL)
56 #define GET_PRIVATE(o) \
57 (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
58 GUPNP_TYPE_DLNA_DISCOVERER, \
59 GUPnPDLNADiscovererPrivate))
61 typedef struct _GUPnPDLNADiscovererPrivate GUPnPDLNADiscovererPrivate;
63 struct _GUPnPDLNADiscovererPrivate {
64 gboolean relaxed_mode;
65 gboolean extended_mode;
70 PROP_DLNA_RELAXED_MODE,
71 PROP_DLNA_EXTENDED_MODE,
75 gupnp_dlna_discoverer_set_property (GObject *object,
80 GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
81 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
83 switch (property_id) {
84 case PROP_DLNA_RELAXED_MODE:
85 priv->relaxed_mode = g_value_get_boolean (value);
88 case PROP_DLNA_EXTENDED_MODE:
89 priv->extended_mode = g_value_get_boolean (value);
93 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
101 gupnp_dlna_discoverer_get_property (GObject *object,
106 GUPnPDLNADiscoverer *self = GUPNP_DLNA_DISCOVERER (object);
107 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
109 switch (property_id) {
110 case PROP_DLNA_RELAXED_MODE:
111 g_value_set_boolean (value, priv->relaxed_mode);
114 case PROP_DLNA_EXTENDED_MODE:
115 g_value_set_boolean (value, priv->extended_mode);
119 G_OBJECT_WARN_INVALID_PROPERTY_ID (object,
127 gupnp_dlna_discoverer_dispose (GObject *object)
129 G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->dispose (object);
133 gupnp_dlna_discoverer_finalize (GObject *object)
135 G_OBJECT_CLASS (gupnp_dlna_discoverer_parent_class)->finalize (object);
138 static void gupnp_dlna_discovered_cb (GstDiscovererInternal *discoverer,
139 GstDiscovererInformation *info,
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;
149 dlna = gupnp_dlna_information_new_from_discoverer_info (info,
150 klass->profiles_list [relaxed][extended]);
152 g_signal_emit (GUPNP_DLNA_DISCOVERER (discoverer),
153 signals[DONE], 0, dlna, err);
156 g_object_unref (dlna);
160 gupnp_dlna_discoverer_class_init (GUPnPDLNADiscovererClass *klass)
162 GObjectClass *object_class = G_OBJECT_CLASS (klass);
165 g_type_class_add_private (klass, sizeof (GUPnPDLNADiscovererPrivate));
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;
173 * GUPnPDLNADiscoverer::relaxed-mode:
174 * @relaxed_mode: setting to true will enable relaxed mode
176 * The current release does not support relaxed mode yet
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 "
184 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
185 g_object_class_install_property (object_class,
186 PROP_DLNA_RELAXED_MODE,
190 * GUPnPDLNADiscoverer::extended-mode:
191 * @extended: setting true will enable extended profile support
193 * The current release does not support extended mode yet
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",
200 G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY);
201 g_object_class_install_property (object_class,
202 PROP_DLNA_EXTENDED_MODE,
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
211 * Will be emitted when all information on a URI could be discovered.
213 * The reciever must unref @dlna with when done using it.
216 g_signal_new ("done", G_TYPE_FROM_CLASS (klass),
218 G_STRUCT_OFFSET (GUPnPDLNADiscovererClass, done),
220 gupnp_dlna_marshal_VOID__OBJECT_BOXED,
221 G_TYPE_NONE, 2, GUPNP_TYPE_DLNA_INFORMATION,
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,
229 klass->profiles_list [0][1]
230 = gupnp_dlna_load_profiles_from_disk (FALSE,
232 klass->profiles_list [1][0]
233 = gupnp_dlna_load_profiles_from_disk (TRUE,
235 klass->profiles_list [1][1]
236 = gupnp_dlna_load_profiles_from_disk (TRUE,
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 "
250 gupnp_dlna_discoverer_init (GUPnPDLNADiscoverer *self)
252 g_signal_connect (&self->parent,
254 G_CALLBACK (gupnp_dlna_discovered_cb),
259 * gupnp_dlna_discoverer_new:
260 * @timeout: default discovery timeout, in nanoseconds
262 * Creates a new #GUPnPDLNADiscoverer object with the given default timeout
265 * Returns: A new #GUPnPDLNADiscoverer object.
268 gupnp_dlna_discoverer_new (GstClockTime timeout,
269 gboolean relaxed_mode,
270 gboolean extended_mode)
272 return g_object_new (GUPNP_TYPE_DLNA_DISCOVERER,
274 "relaxed-mode", relaxed_mode,
275 "extended-mode", extended_mode,
279 /* Asynchronous API */
282 * gupnp_dlna_discoverer_start:
283 * @discoverer: #GUPnPDLNADiscoverer object to start discovery on
285 * Allows asynchronous discovery of URIs to begin.
289 * gupnp_dlna_discoverer_stop:
290 * @discoverer: #GUPnPDLNADiscoverer object to stop discovery on
292 * Stops asynchronous discovery of URIs.
296 * gupnp_dlna_discoverer_discover_uri:
297 * @discoverer: #GUPnPDLNADiscoverer object to use for discovery
298 * @uri: URI to gather metadata for
300 * Queues @uri for metadata discovery. When discovery is completed, the
301 * "discovered" signal is emitted on @discoverer.
303 * Returns: TRUE if @uri was successfully queued, FALSE otherwise.
306 gupnp_dlna_discoverer_discover_uri (GUPnPDLNADiscoverer *discoverer, gchar *uri)
308 return gst_discoverer_internal_append_uri (
309 GST_DISCOVERER_INTERNAL (discoverer), uri);
312 /* Synchronous API */
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
320 * Synchronously gathers metadata for @uri.
322 * Returns: a #GUPnPDLNAInformation with the metadata for @uri on success, NULL
325 GUPnPDLNAInformation *
326 gupnp_dlna_discoverer_discover_uri_sync (GUPnPDLNADiscoverer *discoverer,
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;
336 info = gst_discoverer_internal_discover_uri (
337 GST_DISCOVERER_INTERNAL (discoverer),
342 return gupnp_dlna_information_new_from_discoverer_info (info,
343 klass->profiles_list [relaxed][extended]);
349 * gupnp_dlna_discoverer_get_profile:
350 * @self: The #GUPnPDLNADiscoverer object
351 * @name: The name of the DLNA profile to be retrieved
353 * Given @name, this finds the corresponding DLNA profile information (stored
354 * as a #GUPnPDLNAProfile).
356 * Returns: a #GUPnPDLNAProfile on success, NULL otherwise.
359 gupnp_dlna_discoverer_get_profile (GUPnPDLNADiscoverer *self,
363 GUPnPDLNADiscovererClass *klass;
364 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
365 gboolean relaxed = priv->relaxed_mode;
366 gboolean extended = priv->extended_mode;
368 g_return_val_if_fail (self != NULL, NULL);
369 klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
371 for (i = klass->profiles_list [relaxed][extended];
374 GUPnPDLNAProfile *profile = (GUPnPDLNAProfile *) i->data;
376 if (g_str_equal (gupnp_dlna_profile_get_name (profile), name)) {
377 g_object_ref (profile);
386 * gupnp_dlna_discoverer_list_profiles:
387 * @self: The #GUPnPDLNADiscoverer whose profile list is required
389 * Retuns a list of the all the DLNA profiles supported by @self.
391 * Returns: a #GList of #GUPnPDLNAProfile on success, NULL otherwise.
394 gupnp_dlna_discoverer_list_profiles (GUPnPDLNADiscoverer *self)
396 GUPnPDLNADiscovererClass *klass;
397 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
398 gboolean relaxed = priv->relaxed_mode;
399 gboolean extended = priv->extended_mode;
401 g_return_val_if_fail (self != NULL, NULL);
403 klass = GUPNP_DLNA_DISCOVERER_GET_CLASS (self);
405 return klass->profiles_list [relaxed][extended];
409 * gupnp_dlna_discoverer_get_relaxed_mode:
410 * @self: The #GUPnPDLNADiscoverer object
412 * Returns: true if relaxed mode is set and false otherwise
415 gupnp_dlna_discoverer_get_relaxed_mode (GUPnPDLNADiscoverer *self)
417 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
418 return priv->relaxed_mode;
422 * gupnp_dlna_discoverer_get_extended_mode:
423 * @self: The #GUPnPDLNADiscoverer object
425 * Returns: true if application is using extended mode and false otherwise
428 gupnp_dlna_discoverer_get_extended_mode (GUPnPDLNADiscoverer *self)
430 GUPnPDLNADiscovererPrivate *priv = GET_PRIVATE (self);
431 return priv->extended_mode;