tizen 2.0 init
[framework/multimedia/gst-plugins-base0.10.git] / gst / playback / gstplaysinkaudioconvert.c
1 /* GStreamer
2  * Copyright (C) <2011> Sebastian Dröge <sebastian.droege@collabora.co.uk>
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
23
24 #include "gstplaysinkaudioconvert.h"
25
26 #include <gst/pbutils/pbutils.h>
27 #include <gst/gst-i18n-plugin.h>
28
29 GST_DEBUG_CATEGORY_STATIC (gst_play_sink_audio_convert_debug);
30 #define GST_CAT_DEFAULT gst_play_sink_audio_convert_debug
31
32 #define parent_class gst_play_sink_audio_convert_parent_class
33
34 G_DEFINE_TYPE (GstPlaySinkAudioConvert, gst_play_sink_audio_convert,
35     GST_TYPE_PLAY_SINK_CONVERT_BIN);
36
37 enum
38 {
39   PROP_0,
40   PROP_USE_CONVERTERS,
41   PROP_USE_VOLUME,
42 };
43
44 static gboolean
45 gst_play_sink_audio_convert_add_conversion_elements (GstPlaySinkAudioConvert *
46     self)
47 {
48   GstPlaySinkConvertBin *cbin = GST_PLAY_SINK_CONVERT_BIN (self);
49   GstElement *el, *prev = NULL;
50
51   g_assert (cbin->conversion_elements == NULL);
52
53   GST_DEBUG_OBJECT (self,
54       "Building audio conversion with use-converters %d, use-volume %d",
55       self->use_converters, self->use_volume);
56
57   if (self->use_converters) {
58     el = gst_play_sink_convert_bin_add_conversion_element_factory (cbin,
59         "audioconvert", "conv");
60     if (el) {
61       prev = el;
62     }
63
64     el = gst_play_sink_convert_bin_add_conversion_element_factory (cbin,
65         "audioresample", "resample");
66     if (el) {
67
68       if (prev) {
69         if (!gst_element_link_pads_full (prev, "src", el, "sink",
70                 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
71           goto link_failed;
72       }
73       prev = el;
74     }
75   }
76
77   if (self->use_volume && self->volume) {
78     el = self->volume;
79     gst_play_sink_convert_bin_add_conversion_element (cbin, el);
80     if (prev) {
81       if (!gst_element_link_pads_full (prev, "src", el, "sink",
82               GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
83         goto link_failed;
84     }
85     prev = el;
86   }
87
88   return TRUE;
89
90 link_failed:
91   return FALSE;
92 }
93
94 static void
95 gst_play_sink_audio_convert_finalize (GObject * object)
96 {
97   GstPlaySinkAudioConvert *self = GST_PLAY_SINK_AUDIO_CONVERT_CAST (object);
98
99   if (self->volume)
100     gst_object_unref (self->volume);
101
102   G_OBJECT_CLASS (parent_class)->finalize (object);
103 }
104
105 static void
106 gst_play_sink_audio_convert_set_property (GObject * object, guint prop_id,
107     const GValue * value, GParamSpec * pspec)
108 {
109   GstPlaySinkAudioConvert *self = GST_PLAY_SINK_AUDIO_CONVERT_CAST (object);
110   gboolean v, changed = FALSE;
111
112   GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
113   switch (prop_id) {
114     case PROP_USE_CONVERTERS:
115       v = g_value_get_boolean (value);
116       if (v != self->use_converters) {
117         self->use_converters = v;
118         changed = TRUE;
119       }
120       break;
121     case PROP_USE_VOLUME:
122       v = g_value_get_boolean (value);
123       if (v != self->use_volume) {
124         self->use_volume = v;
125         changed = TRUE;
126       }
127       break;
128     default:
129       break;
130   }
131
132   if (changed) {
133     GstPlaySinkConvertBin *cbin = GST_PLAY_SINK_CONVERT_BIN (self);
134     GST_DEBUG_OBJECT (self, "Rebuilding converter bin");
135     gst_play_sink_convert_bin_remove_elements (cbin);
136     gst_play_sink_audio_convert_add_conversion_elements (self);
137     gst_play_sink_convert_bin_add_identity (cbin);
138     gst_play_sink_convert_bin_cache_converter_caps (cbin);
139   }
140   GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
141 }
142
143 static void
144 gst_play_sink_audio_convert_get_property (GObject * object, guint prop_id,
145     GValue * value, GParamSpec * pspec)
146 {
147   GstPlaySinkAudioConvert *self = GST_PLAY_SINK_AUDIO_CONVERT_CAST (object);
148
149   GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
150   switch (prop_id) {
151     case PROP_USE_CONVERTERS:
152       g_value_set_boolean (value, self->use_converters);
153       break;
154     case PROP_USE_VOLUME:
155       g_value_set_boolean (value, self->use_volume);
156       break;
157     default:
158       break;
159   }
160   GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
161 }
162
163 static void
164 gst_play_sink_audio_convert_class_init (GstPlaySinkAudioConvertClass * klass)
165 {
166   GObjectClass *gobject_class;
167   GstElementClass *gstelement_class;
168
169   GST_DEBUG_CATEGORY_INIT (gst_play_sink_audio_convert_debug,
170       "playsinkaudioconvert", 0, "play bin");
171
172   gobject_class = (GObjectClass *) klass;
173   gstelement_class = (GstElementClass *) klass;
174
175   gobject_class->finalize = gst_play_sink_audio_convert_finalize;
176   gobject_class->set_property = gst_play_sink_audio_convert_set_property;
177   gobject_class->get_property = gst_play_sink_audio_convert_get_property;
178
179   g_object_class_install_property (gobject_class, PROP_USE_CONVERTERS,
180       g_param_spec_boolean ("use-converters", "Use converters",
181           "Whether to use conversion elements", FALSE,
182           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183
184   g_object_class_install_property (gobject_class, PROP_USE_VOLUME,
185       g_param_spec_boolean ("use-volume", "Use volume",
186           "Whether to use a volume element", FALSE,
187           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
188
189   gst_element_class_set_details_simple (gstelement_class,
190       "Player Sink Audio Converter", "Audio/Bin/Converter",
191       "Convenience bin for audio conversion",
192       "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
193 }
194
195 static void
196 gst_play_sink_audio_convert_init (GstPlaySinkAudioConvert * self)
197 {
198   GstPlaySinkConvertBin *cbin = GST_PLAY_SINK_CONVERT_BIN (self);
199
200   cbin->audio = TRUE;
201
202   /* FIXME: Only create this on demand but for now we need
203    * it to always exist because of playsink's volume proxying
204    * logic.
205    */
206   self->volume = gst_element_factory_make ("volume", "volume");
207   if (self->volume)
208     gst_object_ref_sink (self->volume);
209
210   gst_play_sink_audio_convert_add_conversion_elements (self);
211   gst_play_sink_convert_bin_cache_converter_caps (cbin);
212 }