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., 51 Franklin St, Fifth Floor,
17 * Boston, MA 02110-1301, 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
gst_play_sink_audio_convert_add_conversion_elements(GstPlaySinkAudioConvert * self)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 if (prev) {
68 if (!gst_element_link_pads_full (prev, "src", el, "sink",
69 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
70 goto link_failed;
71 }
72 prev = el;
73 }
74 }
75
76 if (self->use_volume && self->volume) {
77 el = self->volume;
78 gst_play_sink_convert_bin_add_conversion_element (cbin, el);
79 if (prev) {
80 if (!gst_element_link_pads_full (prev, "src", el, "sink",
81 GST_PAD_LINK_CHECK_TEMPLATE_CAPS))
82 goto link_failed;
83 }
84 prev = el;
85 }
86
87 return TRUE;
88
89 link_failed:
90 return FALSE;
91 }
92
93 static void
gst_play_sink_audio_convert_finalize(GObject * object)94 gst_play_sink_audio_convert_finalize (GObject * object)
95 {
96 GstPlaySinkAudioConvert *self = GST_PLAY_SINK_AUDIO_CONVERT_CAST (object);
97
98 if (self->volume)
99 gst_object_unref (self->volume);
100
101 G_OBJECT_CLASS (parent_class)->finalize (object);
102 }
103
104 static void
gst_play_sink_audio_convert_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)105 gst_play_sink_audio_convert_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec)
107 {
108 GstPlaySinkAudioConvert *self = GST_PLAY_SINK_AUDIO_CONVERT_CAST (object);
109 gboolean v, changed = FALSE;
110
111 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
112 switch (prop_id) {
113 case PROP_USE_CONVERTERS:
114 v = g_value_get_boolean (value);
115 if (v != self->use_converters) {
116 self->use_converters = v;
117 changed = TRUE;
118 }
119 break;
120 case PROP_USE_VOLUME:
121 v = g_value_get_boolean (value);
122 if (v != self->use_volume) {
123 self->use_volume = v;
124 changed = TRUE;
125 }
126 break;
127 default:
128 break;
129 }
130
131 if (changed) {
132 GstPlaySinkConvertBin *cbin = GST_PLAY_SINK_CONVERT_BIN (self);
133 GST_DEBUG_OBJECT (self, "Rebuilding converter bin");
134 gst_play_sink_convert_bin_remove_elements (cbin);
135 gst_play_sink_audio_convert_add_conversion_elements (self);
136 gst_play_sink_convert_bin_add_identity (cbin);
137 gst_play_sink_convert_bin_cache_converter_caps (cbin);
138 }
139 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
140 }
141
142 static void
gst_play_sink_audio_convert_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)143 gst_play_sink_audio_convert_get_property (GObject * object, guint prop_id,
144 GValue * value, GParamSpec * pspec)
145 {
146 GstPlaySinkAudioConvert *self = GST_PLAY_SINK_AUDIO_CONVERT_CAST (object);
147
148 GST_PLAY_SINK_CONVERT_BIN_LOCK (self);
149 switch (prop_id) {
150 case PROP_USE_CONVERTERS:
151 g_value_set_boolean (value, self->use_converters);
152 break;
153 case PROP_USE_VOLUME:
154 g_value_set_boolean (value, self->use_volume);
155 break;
156 default:
157 break;
158 }
159 GST_PLAY_SINK_CONVERT_BIN_UNLOCK (self);
160 }
161
162 static void
gst_play_sink_audio_convert_class_init(GstPlaySinkAudioConvertClass * klass)163 gst_play_sink_audio_convert_class_init (GstPlaySinkAudioConvertClass * klass)
164 {
165 GObjectClass *gobject_class;
166 GstElementClass *gstelement_class;
167
168 GST_DEBUG_CATEGORY_INIT (gst_play_sink_audio_convert_debug,
169 "playsinkaudioconvert", 0, "play bin");
170
171 gobject_class = (GObjectClass *) klass;
172 gstelement_class = (GstElementClass *) klass;
173
174 gobject_class->finalize = gst_play_sink_audio_convert_finalize;
175 gobject_class->set_property = gst_play_sink_audio_convert_set_property;
176 gobject_class->get_property = gst_play_sink_audio_convert_get_property;
177
178 g_object_class_install_property (gobject_class, PROP_USE_CONVERTERS,
179 g_param_spec_boolean ("use-converters", "Use converters",
180 "Whether to use conversion elements", FALSE,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182
183 g_object_class_install_property (gobject_class, PROP_USE_VOLUME,
184 g_param_spec_boolean ("use-volume", "Use volume",
185 "Whether to use a volume element", FALSE,
186 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
187
188 gst_element_class_set_static_metadata (gstelement_class,
189 "Player Sink Audio Converter", "Audio/Bin/Converter",
190 "Convenience bin for audio conversion",
191 "Sebastian Dröge <sebastian.droege@collabora.co.uk>");
192 }
193
194 static void
gst_play_sink_audio_convert_init(GstPlaySinkAudioConvert * self)195 gst_play_sink_audio_convert_init (GstPlaySinkAudioConvert * self)
196 {
197 GstPlaySinkConvertBin *cbin = GST_PLAY_SINK_CONVERT_BIN (self);
198
199 cbin->audio = TRUE;
200
201 /* FIXME: Only create this on demand but for now we need
202 * it to always exist because of playsink's volume proxying
203 * logic.
204 */
205 self->volume = gst_element_factory_make ("volume", "volume");
206 if (self->volume)
207 gst_object_ref_sink (self->volume);
208
209 gst_play_sink_audio_convert_add_conversion_elements (self);
210 gst_play_sink_convert_bin_cache_converter_caps (cbin);
211 }
212