Fix return types for getter functions
[gupnp:gupnp-dlna.git] / libgupnp-dlna / gupnp-dlna-profile.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-profile.h"
23 #include "gupnp-dlna-profile-private.h"
24 #include <gst/gstminiobject.h>
25
26 /**
27  * SECTION:gupnp-dlna-profile
28  * @short_description: Object representing a DLNA profile
29  *
30  * The #GUPnPDLNADiscoverer object provides a few APIs that return
31  * #GUPnPDLNAProfile objects. These represent a single DLNA profile. Each
32  * #GUPnPDLNAProfile has a name (the name of the DLNA profile), the
33  * corresponding MIME type, and a #GstEncodingProfile which represents the
34  * various audio/video/container restrictions specified for that DLNA profile.
35  */
36 G_DEFINE_TYPE (GUPnPDLNAProfile, gupnp_dlna_profile, G_TYPE_OBJECT)
37
38 #define GET_PRIVATE(o)                                          \
39         (G_TYPE_INSTANCE_GET_PRIVATE ((o),                      \
40                                       GUPNP_TYPE_DLNA_PROFILE,  \
41                                       GUPnPDLNAProfilePrivate))
42
43 typedef struct _GUPnPDLNAProfilePrivate GUPnPDLNAProfilePrivate;
44
45 struct _GUPnPDLNAProfilePrivate {
46         gchar              *name;
47         gchar              *mime;
48         GstCaps            *container_caps;
49         GstCaps            *video_caps;
50         GstCaps            *audio_caps;
51         gboolean           extended;
52         GstEncodingProfile *enc_profile;
53 };
54
55 enum {
56         PROP_0,
57         PROP_DLNA_NAME,
58         PROP_DLNA_MIME,
59         PROP_ENCODING_PROFILE,
60         PROP_DLNA_EXTENDED,
61 };
62
63 static void
64 gupnp_dlna_profile_get_property (GObject    *object,
65                                  guint       property_id,
66                                  GValue     *value,
67                                  GParamSpec *pspec)
68 {
69         GUPnPDLNAProfile *self = GUPNP_DLNA_PROFILE (object);
70         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
71
72         switch (property_id) {
73                 case PROP_DLNA_NAME:
74                         g_value_set_string (value, priv->name);
75                         break;
76
77                 case PROP_DLNA_MIME:
78                         g_value_set_string (value, priv->mime);
79                         break;
80
81                 case PROP_ENCODING_PROFILE:
82                         gst_value_set_mini_object (value,
83                                         GST_MINI_OBJECT (priv->enc_profile));
84                         break;
85
86                 case PROP_DLNA_EXTENDED:
87                         g_value_set_boolean (value, priv->extended);
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_profile_set_property (GObject      *object,
100                                  guint         property_id,
101                                  const GValue *value,
102                                  GParamSpec   *pspec)
103 {
104         GUPnPDLNAProfile *self = GUPNP_DLNA_PROFILE (object);
105         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
106
107         switch (property_id) {
108                 case PROP_DLNA_NAME:
109                         g_free (priv->name);
110                         priv->name = g_value_dup_string (value);
111                         break;
112
113                 case PROP_DLNA_MIME:
114                         g_free (priv->mime);
115                         priv->mime = g_value_dup_string (value);
116                         break;
117
118                 case PROP_DLNA_EXTENDED:
119                         priv->extended = g_value_get_boolean (value);
120                         break;
121
122                 default:
123                         G_OBJECT_WARN_INVALID_PROPERTY_ID
124                                 (object, property_id, pspec);
125                         break;
126         }
127 }
128
129 static void
130 gupnp_dlna_profile_finalize (GObject *object)
131 {
132         GUPnPDLNAProfile *self = GUPNP_DLNA_PROFILE (object);
133         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
134
135         g_free (priv->name);
136         g_free (priv->mime);
137
138         if (priv->container_caps)
139                 gst_caps_unref (priv->container_caps);
140         if (priv->audio_caps)
141                 gst_caps_unref (priv->audio_caps);
142         if (priv->video_caps)
143                 gst_caps_unref (priv->video_caps);
144
145         if (priv->enc_profile)
146                 gst_encoding_profile_unref (priv->enc_profile);
147
148         G_OBJECT_CLASS (gupnp_dlna_profile_parent_class)->finalize (object);
149 }
150
151 static void
152 gupnp_dlna_profile_class_init (GUPnPDLNAProfileClass *klass)
153 {
154         GObjectClass *object_class = G_OBJECT_CLASS (klass);
155         GParamSpec *pspec;
156
157         g_type_class_add_private (klass, sizeof (GUPnPDLNAProfilePrivate));
158
159         object_class->get_property = gupnp_dlna_profile_get_property;
160         object_class->set_property = gupnp_dlna_profile_set_property;
161         object_class->finalize = gupnp_dlna_profile_finalize;
162
163         pspec = g_param_spec_string ("name",
164                                      "DLNA profile name",
165                                      "The name of the DLNA profile ",
166                                      NULL,
167                                      G_PARAM_READWRITE |
168                                      G_PARAM_CONSTRUCT_ONLY);
169         g_object_class_install_property (object_class, PROP_DLNA_NAME, pspec);
170
171         pspec = g_param_spec_string ("mime",
172                                      "DLNA profile MIME type",
173                                      "The MIME type of the DLNA profile",
174                                      NULL,
175                                      G_PARAM_READWRITE |
176                                      G_PARAM_CONSTRUCT_ONLY);
177         g_object_class_install_property (object_class, PROP_DLNA_MIME, pspec);
178
179         pspec = gst_param_spec_mini_object ("encoding-profile",
180                                             "Encoding Profile for the "
181                                             "DLNA Profile",
182                                             "GstEncodingProfile object"
183                                             "corresponding to the DLNA profile",
184                                             GST_TYPE_ENCODING_PROFILE,
185                                             G_PARAM_READABLE);
186         g_object_class_install_property (object_class,
187                                          PROP_ENCODING_PROFILE,
188                                          pspec);
189
190         pspec = g_param_spec_boolean ("extended",
191                                       "Extended mode property",
192                                       "Indicates that this profile is not "
193                                       "part of the DLNA specification",
194                                       FALSE,
195                                       G_PARAM_READWRITE |
196                                       G_PARAM_CONSTRUCT_ONLY);
197         g_object_class_install_property (object_class,
198                                          PROP_DLNA_EXTENDED,
199                                          pspec);
200
201 }
202
203 static void
204 gupnp_dlna_profile_init (GUPnPDLNAProfile *self)
205 {
206 }
207
208 const GstCaps *
209 gupnp_dlna_profile_get_container_caps (GUPnPDLNAProfile *self)
210 {
211         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
212         return priv->container_caps;
213 }
214
215 const GstCaps *
216 gupnp_dlna_profile_get_video_caps (GUPnPDLNAProfile *self)
217 {
218         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
219         return priv->video_caps;
220 }
221
222 const GstCaps *
223 gupnp_dlna_profile_get_audio_caps (GUPnPDLNAProfile *self)
224 {
225         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
226         return priv->audio_caps;
227 }
228
229 void
230 gupnp_dlna_profile_set_container_caps (GUPnPDLNAProfile *self, GstCaps *caps)
231 {
232         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
233
234         if (priv->container_caps)
235                 gst_caps_unref (priv->container_caps);
236         priv->container_caps = gst_caps_copy (caps);
237 }
238
239 void
240 gupnp_dlna_profile_set_video_caps (GUPnPDLNAProfile *self, GstCaps *caps)
241 {
242         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
243
244         if (priv->video_caps)
245                 gst_caps_unref (priv->video_caps);
246         priv->video_caps = gst_caps_copy (caps);
247 }
248
249 void
250 gupnp_dlna_profile_set_audio_caps (GUPnPDLNAProfile *self, GstCaps *caps)
251 {
252         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
253
254         if (priv->audio_caps)
255                 gst_caps_unref (priv->audio_caps);
256         priv->audio_caps = gst_caps_copy (caps);
257 }
258
259 GUPnPDLNAProfile *
260 gupnp_dlna_profile_new (gchar    *name,
261                         gchar    *mime,
262                         GstCaps  *container_caps,
263                         GstCaps  *video_caps,
264                         GstCaps  *audio_caps,
265                         gboolean extended)
266 {
267         GUPnPDLNAProfile *prof;
268
269         prof =  g_object_new (GUPNP_TYPE_DLNA_PROFILE,
270                               "name", name,
271                               "mime", mime,
272                               "extended", extended,
273                               NULL);
274
275         gupnp_dlna_profile_set_container_caps (prof, container_caps);
276         gupnp_dlna_profile_set_video_caps (prof, video_caps);
277         gupnp_dlna_profile_set_audio_caps (prof, audio_caps);
278
279         return prof;
280 }
281
282 /**
283  * gupnp_dlna_profile_get_name:
284  * @self: The #GUPnPDLNAProfile object
285  *
286  * Returns: the name of the DLNA profile represented by @self
287  */
288 const gchar *
289 gupnp_dlna_profile_get_name (GUPnPDLNAProfile *self)
290 {
291         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
292         return priv->name;
293 }
294
295 /**
296  * gupnp_dlna_profile_get_mime:
297  * @self: The #GUPnPDLNAProfile object
298  *
299  * Returns: the DLNA MIME type of the DLNA profile represented by @self
300  */
301 const gchar *
302 gupnp_dlna_profile_get_mime (GUPnPDLNAProfile *self)
303 {
304         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
305         return priv->mime;
306 }
307
308 /**
309  * gupnp_dlna_profile_get_encoding_profile:
310  * @self: The #GUPnPDLNAProfile object
311  *
312  * Returns: a #GstEncodingProfile object that, in a future version, can be used
313  *          to transcode a given stream to match the DLNA profile represented
314  *          by @self.
315  *          The receiver must unref the returned #GstEncodingProfile when done
316  *          using it.
317  */
318 GstEncodingProfile *
319 gupnp_dlna_profile_get_encoding_profile (GUPnPDLNAProfile *self)
320 {
321         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
322
323         /* create an encoding-profile if we don't have one */
324         if (!priv->enc_profile) {
325                 GstEncodingContainerProfile *container = NULL;
326                 GstEncodingAudioProfile *audio_profile = NULL;
327                 GstEncodingVideoProfile *video_profile = NULL;
328
329                 if (GST_IS_CAPS (priv->video_caps) &&
330                     !gst_caps_is_empty (priv->video_caps))
331                         video_profile = gst_encoding_video_profile_new
332                                         (priv->video_caps,NULL, NULL, 0);
333
334                 if (GST_IS_CAPS (priv->audio_caps) &&
335                     !gst_caps_is_empty (priv->audio_caps))
336                         audio_profile = gst_encoding_audio_profile_new
337                                         (priv->audio_caps,NULL, NULL, 0);
338
339                 if (GST_IS_CAPS (priv->container_caps)) {
340                         container = gst_encoding_container_profile_new
341                                         (priv->name,
342                                          priv->mime,
343                                          priv->container_caps,
344                                          NULL);
345
346                         if (video_profile)
347                                 gst_encoding_container_profile_add_profile
348                                         (container,
349                                          (GstEncodingProfile *)video_profile);
350
351                         if (audio_profile)
352                                 gst_encoding_container_profile_add_profile
353                                         (container,
354                                          (GstEncodingProfile *) audio_profile);
355
356                         priv->enc_profile = (GstEncodingProfile *)container;
357                 } else {
358                         if(audio_profile)
359                                 /* Container-less audio */
360                                 priv->enc_profile =
361                                         (GstEncodingProfile *)audio_profile;
362
363                         if (video_profile)
364                                 /* Container-less video isn't a possibility
365                                    yet */
366                                 g_assert_not_reached ();
367                 }
368         }
369
370         gst_encoding_profile_ref (priv->enc_profile);
371
372         return priv->enc_profile;
373 }
374
375 /**
376  * gupnp_dlna_profile_get_extended:
377  * @self: The #GUPnPDLNAProfile object
378  *
379  * Returns: true if application is using extended mode and false otherwise
380  */
381 gboolean
382 gupnp_dlna_profile_get_extended (GUPnPDLNAProfile *self)
383 {
384         GUPnPDLNAProfilePrivate *priv = GET_PRIVATE (self);
385         return priv->extended;
386 }