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