]> git.0d.be Git - empathy.git/blob - src/empathy-audio-sink.c
Check and create video_preview outside camera control
[empathy.git] / src / empathy-audio-sink.c
1 /*
2  * empathy-gst-audio-sink.c - Source for EmpathyGstAudioSink
3  * Copyright (C) 2008 Collabora Ltd.
4  * @author Sjoerd Simons <sjoerd.simons@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.1 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 Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  */
20
21
22 #include <stdio.h>
23 #include <stdlib.h>
24
25 #include <gst/audio/audio.h>
26 #include <gst/interfaces/streamvolume.h>
27
28 #include <telepathy-glib/telepathy-glib.h>
29
30 #include <libempathy-gtk/empathy-call-utils.h>
31
32 #include "empathy-audio-sink.h"
33
34 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
35 #include <libempathy/empathy-debug.h>
36
37 G_DEFINE_TYPE(EmpathyGstAudioSink, empathy_audio_sink, GST_TYPE_BIN)
38
39 /* signal enum */
40 #if 0
41 enum
42 {
43     LAST_SIGNAL
44 };
45
46 static guint signals[LAST_SIGNAL] = {0};
47 #endif
48
49 static GstStaticPadTemplate sink_template = GST_STATIC_PAD_TEMPLATE(
50     "sink%d",
51     GST_PAD_SINK,
52     GST_PAD_REQUEST,
53     GST_STATIC_CAPS ( GST_AUDIO_INT_PAD_TEMPLATE_CAPS " ; "
54         GST_AUDIO_FLOAT_PAD_TEMPLATE_CAPS)
55 );
56
57 enum {
58   PROP_VOLUME = 1,
59 };
60
61 struct _EmpathyGstAudioSinkPrivate
62 {
63   GstElement *sink;
64 };
65
66 #define EMPATHY_GST_AUDIO_SINK_GET_PRIVATE(o) \
67   (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SINK, \
68   EmpathyGstAudioSinkPrivate))
69
70 static void
71 empathy_audio_sink_init (EmpathyGstAudioSink *self)
72 {
73   self->priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (self);
74 }
75
76 static GstPad * empathy_audio_sink_request_new_pad (GstElement *self,
77   GstPadTemplate *templ,
78   const gchar* name);
79
80 static void empathy_audio_sink_release_pad (GstElement *self,
81   GstPad *pad);
82
83 static void
84 empathy_audio_sink_set_property (GObject *object,
85   guint property_id, const GValue *value, GParamSpec *pspec)
86 {
87   switch (property_id)
88     {
89       case PROP_VOLUME:
90         empathy_audio_sink_set_volume (EMPATHY_GST_AUDIO_SINK (object),
91           g_value_get_double (value));
92         break;
93       default:
94         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
95     }
96 }
97
98 static void
99 empathy_audio_sink_get_property (GObject *object,
100   guint property_id, GValue *value, GParamSpec *pspec)
101 {
102   switch (property_id)
103     {
104       case PROP_VOLUME:
105         g_value_set_double (value,
106           empathy_audio_sink_get_volume (EMPATHY_GST_AUDIO_SINK (object)));
107         break;
108       default:
109         G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
110     }
111 }
112
113 static void
114 empathy_audio_sink_class_init (EmpathyGstAudioSinkClass
115   *empathy_audio_sink_class)
116 {
117   GObjectClass *object_class = G_OBJECT_CLASS (empathy_audio_sink_class);
118   GstElementClass *element_class =
119     GST_ELEMENT_CLASS (empathy_audio_sink_class);
120   GParamSpec *param_spec;
121
122   gst_element_class_add_pad_template (element_class,
123     gst_static_pad_template_get (&sink_template));
124
125   g_type_class_add_private (empathy_audio_sink_class,
126     sizeof (EmpathyGstAudioSinkPrivate));
127
128   object_class->set_property = empathy_audio_sink_set_property;
129   object_class->get_property = empathy_audio_sink_get_property;
130
131   element_class->request_new_pad = empathy_audio_sink_request_new_pad;
132   element_class->release_pad = empathy_audio_sink_release_pad;
133
134   param_spec = g_param_spec_double ("volume", "Volume", "volume control",
135     0.0, 5.0, 1.0,
136     G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS);
137   g_object_class_install_property (object_class, PROP_VOLUME, param_spec);
138 }
139
140 GstElement *
141 empathy_audio_sink_new (void)
142 {
143   static gboolean registered = FALSE;
144
145   if (!registered) {
146     if (!gst_element_register (NULL, "empathyaudiosink",
147             GST_RANK_NONE, EMPATHY_TYPE_GST_AUDIO_SINK))
148       return NULL;
149     registered = TRUE;
150   }
151   return gst_element_factory_make ("empathyaudiosink", NULL);
152 }
153
154 static gboolean
155 check_volume_support (EmpathyGstAudioSink *self)
156 {
157   gchar *name;
158
159   if (GST_IS_STREAM_VOLUME (self->priv->sink))
160     return TRUE;
161
162   name = gst_element_get_name (self->priv->sink);
163   DEBUG ("Element %s doesn't support volume", name);
164
165   g_free (name);
166   return FALSE;
167 }
168
169 void
170 empathy_audio_sink_set_volume (EmpathyGstAudioSink *sink, gdouble volume)
171 {
172   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
173
174   if (!check_volume_support (sink))
175       return;
176
177   g_object_set (priv->sink, "volume", volume, NULL);
178 }
179
180 gdouble
181 empathy_audio_sink_get_volume (EmpathyGstAudioSink *sink)
182 {
183   EmpathyGstAudioSinkPrivate *priv = EMPATHY_GST_AUDIO_SINK_GET_PRIVATE (sink);
184   gdouble volume;
185
186   if (!check_volume_support (sink))
187       return 1.0;
188
189   g_object_get (priv->sink, "volume", &volume, NULL);
190   return volume;
191 }
192
193 static GstElement *
194 create_sink (void)
195 {
196   GstElement *sink;
197   const gchar *description;
198
199   description = g_getenv ("EMPATHY_AUDIO_SINK");
200
201   if (description != NULL)
202     {
203       GError *error = NULL;
204
205       sink = gst_parse_bin_from_description (description, TRUE, &error);
206       if (sink == NULL)
207         {
208           DEBUG ("Failed to create bin %s: %s", description, error->message);
209           g_error_free (error);
210         }
211
212       return sink;
213     }
214
215   /* Use pulsesink as default */
216   sink = gst_element_factory_make ("pulsesink", NULL);
217   if (sink == NULL)
218     return NULL;
219
220   empathy_call_set_stream_properties (sink);
221
222   return sink;
223 }
224
225 static GstPad *
226 empathy_audio_sink_request_new_pad (GstElement *element,
227   GstPadTemplate *templ,
228   const gchar* name)
229 {
230   EmpathyGstAudioSink *self = EMPATHY_GST_AUDIO_SINK (element);
231   GstElement *bin, *volume, *resample, *audioconvert0, *audioconvert1;
232   GstPad *pad = NULL;
233   GstPad *subpad, *filterpad;
234
235   bin = gst_bin_new (NULL);
236
237   audioconvert0 = gst_element_factory_make ("audioconvert", NULL);
238   if (audioconvert0 == NULL)
239     goto error;
240
241   gst_bin_add (GST_BIN (bin), audioconvert0);
242
243   resample = gst_element_factory_make ("audioresample", NULL);
244   if (resample == NULL)
245     goto error;
246
247   gst_bin_add (GST_BIN (bin), resample);
248
249   audioconvert1 = gst_element_factory_make ("audioconvert", NULL);
250   if (audioconvert1 == NULL)
251     goto error;
252
253   gst_bin_add (GST_BIN (bin), audioconvert1);
254
255   volume = gst_element_factory_make ("volume", NULL);
256   if (volume == NULL)
257     goto error;
258
259   gst_bin_add (GST_BIN (bin), volume);
260
261   self->priv->sink = create_sink ();
262   if (self->priv->sink == NULL)
263     goto error;
264
265   gst_bin_add (GST_BIN (bin), self->priv->sink);
266
267   if (!gst_element_link_many (audioconvert0, resample, audioconvert1,
268       volume, self->priv->sink, NULL))
269     goto error;
270
271   filterpad = gst_element_get_static_pad (audioconvert0, "sink");
272
273   if (filterpad == NULL)
274     goto error;
275
276   subpad = gst_ghost_pad_new ("sink", filterpad);
277   if (!gst_element_add_pad (GST_ELEMENT (bin), subpad))
278     goto error;
279
280   gst_bin_add (GST_BIN (self), bin);
281
282   pad = gst_ghost_pad_new (name, subpad);
283   g_assert (pad != NULL);
284
285   if (!gst_element_sync_state_with_parent (bin))
286     goto error;
287
288   if (!gst_pad_set_active (pad, TRUE))
289     goto error;
290
291   if (!gst_element_add_pad (GST_ELEMENT (self), pad))
292     goto error;
293
294   return pad;
295
296 error:
297   if (pad != NULL)
298     {
299       gst_object_unref (pad);
300     }
301
302   gst_object_unref (bin);
303   g_warning ("Failed to create output subpipeline");
304   return NULL;
305 }
306
307 static void
308 empathy_audio_sink_release_pad (GstElement *element,
309   GstPad *pad)
310 {
311   gst_pad_set_active (pad, FALSE);
312   gst_element_remove_pad (element, pad);
313 }