2 * empathy-gst-audio-src.c - Source for EmpathyGstAudioSrc
3 * Copyright (C) 2008 Collabora Ltd.
4 * @author Sjoerd Simons <sjoerd.simons@collabora.co.uk>
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.
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.
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
22 #include "empathy-audio-src.h"
24 #include <tp-account-widgets/tpaw-utils.h>
26 #include <gst/audio/streamvolume.h>
28 #include "empathy-audio-utils.h"
29 #include "empathy-mic-monitor.h"
30 #include "empathy-utils.h"
32 #define DEBUG_FLAG EMPATHY_DEBUG_VOIP
33 #include "empathy-debug.h"
35 G_DEFINE_TYPE(EmpathyGstAudioSrc
, empathy_audio_src
, GST_TYPE_BIN
)
43 /* private structure */
44 struct _EmpathyGstAudioSrcPrivate
46 gboolean dispose_has_run
;
48 GstElement
*volume_element
;
50 EmpathyMicMonitor
*mic_monitor
;
52 /* 0 if not known yet */
53 guint source_output_idx
;
54 /* G_MAXUINT if not known yet */
59 gboolean have_stream_volume
;
65 #define EMPATHY_GST_AUDIO_SRC_GET_PRIVATE(o) \
66 (G_TYPE_INSTANCE_GET_PRIVATE ((o), EMPATHY_TYPE_GST_AUDIO_SRC, \
67 EmpathyGstAudioSrcPrivate))
71 empathy_audio_src_volume_changed (GObject
*object
,
76 empathy_audio_set_hw_mute (EmpathyGstAudioSrc
*self
, gboolean mute
)
78 if (mute
== self
->priv
->mute
)
81 if (self
->priv
->have_stream_volume
)
82 g_object_set (self
->priv
->src
, "mute", mute
, NULL
);
84 /* Belt and braces: If for some reason the underlying src doesn't mute
85 * correctly or doesn't update us when it unmutes correctly enforce it using
86 * our own volume element. Our UI can in no circumstances be made to think
87 * the input is muted while it's not */
88 g_object_set (self
->priv
->volume_element
, "mute", mute
, NULL
);
90 self
->priv
->mute
= mute
;
94 empathy_audio_src_get_hw_mute (EmpathyGstAudioSrc
*self
)
97 g_object_get (self
->priv
->src
, "mute", &result
, NULL
);
103 empathy_audio_src_set_hw_volume (EmpathyGstAudioSrc
*self
,
106 if (volume
== self
->priv
->volume
)
109 if (self
->priv
->have_stream_volume
)
110 g_object_set (self
->priv
->src
, "volume", volume
, NULL
);
111 self
->priv
->volume
= volume
;
115 empathy_audio_src_get_hw_volume (EmpathyGstAudioSrc
*self
)
118 g_object_get (self
->priv
->src
, "volume", &result
, NULL
);
125 empathy_audio_src_supports_changing_mic (EmpathyGstAudioSrc
*self
)
127 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
128 GObjectClass
*object_class
;
130 object_class
= G_OBJECT_GET_CLASS (priv
->src
);
132 return (g_object_class_find_property (object_class
,
133 "source-output-index") != NULL
);
137 empathy_audio_src_get_mic_index (EmpathyGstAudioSrc
*self
)
139 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
140 guint audio_src_idx
= PA_INVALID_INDEX
;
142 if (empathy_audio_src_supports_changing_mic (self
))
143 g_object_get (priv
->src
,
144 "source-output-index", &audio_src_idx
,
147 return audio_src_idx
;
151 empathy_audio_src_microphone_changed_cb (EmpathyMicMonitor
*monitor
,
152 guint source_output_idx
,
156 EmpathyGstAudioSrc
*self
= user_data
;
157 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
160 audio_src_idx
= empathy_audio_src_get_mic_index (self
);
162 if (source_output_idx
== PA_INVALID_INDEX
163 || source_output_idx
!= audio_src_idx
)
166 if (priv
->source_idx
== source_idx
)
169 priv
->source_idx
= source_idx
;
170 g_object_notify (G_OBJECT (self
), "microphone");
174 empathy_audio_src_get_current_mic_cb (GObject
*source_object
,
175 GAsyncResult
*result
,
178 EmpathyMicMonitor
*monitor
= EMPATHY_MIC_MONITOR (source_object
);
179 EmpathyGstAudioSrc
*self
= user_data
;
180 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
182 GError
*error
= NULL
;
184 source_idx
= empathy_mic_monitor_get_current_mic_finish (monitor
, result
, &error
);
188 DEBUG ("Failed to get current mic: %s", error
->message
);
189 g_clear_error (&error
);
193 if (priv
->source_idx
== source_idx
)
196 priv
->source_idx
= source_idx
;
197 g_object_notify (G_OBJECT (self
), "microphone");
201 empathy_audio_src_source_output_index_notify (GObject
*object
,
203 EmpathyGstAudioSrc
*self
)
205 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
206 guint source_output_idx
;
208 source_output_idx
= empathy_audio_src_get_mic_index (self
);
210 if (source_output_idx
== PA_INVALID_INDEX
)
213 if (priv
->source_output_idx
== source_output_idx
)
216 /* It's actually changed. */
217 priv
->source_output_idx
= source_output_idx
;
219 empathy_mic_monitor_get_current_mic_async (priv
->mic_monitor
,
220 source_output_idx
, empathy_audio_src_get_current_mic_cb
, self
);
227 const gchar
*description
;
229 description
= g_getenv ("EMPATHY_AUDIO_SRC");
231 if (description
!= NULL
)
233 GError
*error
= NULL
;
235 src
= gst_parse_bin_from_description (description
, TRUE
, &error
);
238 DEBUG ("Failed to create bin %s: %s", description
, error
->message
);
239 g_error_free (error
);
245 /* Use pulsesrc as default */
246 src
= gst_element_factory_make ("pulsesrc", NULL
);
249 g_warning ("Missing 'pulsesrc' element");
253 empathy_audio_set_stream_properties (src
, TRUE
);
255 /* Set latency (buffering on the PulseAudio side) of 20ms */
256 g_object_set (src
, "buffer-time", (gint64
) 20000, NULL
);
262 empathy_audio_src_init (EmpathyGstAudioSrc
*obj
)
264 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (obj
);
268 g_mutex_init (&priv
->lock
);
272 priv
->src
= create_src ();
273 if (priv
->src
== NULL
)
276 if (GST_IS_STREAM_VOLUME (priv
->src
))
281 priv
->have_stream_volume
= TRUE
;
282 /* We can't do a bidirection bind as the ::notify comes from another
283 * thread, for other bits of empathy it's most simpler if it comes from
285 g_object_bind_property (obj
, "volume", priv
->src
, "volume",
287 g_object_bind_property (obj
, "mute", priv
->src
, "mute",
290 /* sync and callback for bouncing */
291 g_object_get (priv
->src
, "volume", &volume
, NULL
);
292 g_object_set (obj
, "volume", volume
, NULL
);
294 g_object_get (priv
->src
, "mute", &mute
, NULL
);
295 g_object_set (obj
, "mute", mute
, NULL
);
297 g_signal_connect (priv
->src
, "notify::volume",
298 G_CALLBACK (empathy_audio_src_volume_changed
), obj
);
299 g_signal_connect (priv
->src
, "notify::mute",
300 G_CALLBACK (empathy_audio_src_volume_changed
), obj
);
304 g_message ("No stream volume available :(, mute will work though");
305 priv
->have_stream_volume
= FALSE
;
308 gst_bin_add (GST_BIN (obj
), priv
->src
);
310 priv
->volume_element
= gst_element_factory_make ("volume", NULL
);
311 gst_bin_add (GST_BIN (obj
), priv
->volume_element
);
314 GstElement
*capsfilter
;
317 /* Explicitly state what format we want from pulsesrc. This pushes resampling
318 * and format conversion as early as possible, lowering the amount of data
319 * transferred and thus improving performance. When moving to GStreamer
320 * 0.11/1.0, this should change so that we actually request what the encoder
321 * wants downstream. */
322 caps
= gst_caps_new_simple ("audio/x-raw",
323 "channels", G_TYPE_INT
, 1,
324 "width", G_TYPE_INT
, 16,
325 "depth", G_TYPE_INT
, 16,
326 "rate", G_TYPE_INT
, 32000,
328 capsfilter
= gst_element_factory_make ("capsfilter", NULL
);
329 g_object_set (G_OBJECT (capsfilter
), "caps", caps
, NULL
);
330 gst_bin_add (GST_BIN (obj
), capsfilter
);
331 gst_element_link (priv
->src
, capsfilter
);
332 gst_element_link (capsfilter
, priv
->volume_element
);
335 src
= gst_element_get_static_pad (priv
->volume_element
, "src");
337 ghost
= gst_ghost_pad_new ("src", src
);
338 gst_element_add_pad (GST_ELEMENT (obj
), ghost
);
340 gst_object_unref (G_OBJECT (src
));
342 /* Listen to changes to GstPulseSrc:source-output-index so we know when
343 * it's no longer PA_INVALID_INDEX (starting for the first time) or if it
344 * changes (READY->NULL->READY...) */
345 g_signal_connect (priv
->src
, "notify::source-output-index",
346 G_CALLBACK (empathy_audio_src_source_output_index_notify
),
349 priv
->mic_monitor
= empathy_mic_monitor_new ();
350 g_signal_connect (priv
->mic_monitor
, "microphone-changed",
351 G_CALLBACK (empathy_audio_src_microphone_changed_cb
), obj
);
353 priv
->source_idx
= PA_INVALID_INDEX
;
356 static void empathy_audio_src_dispose (GObject
*object
);
357 static void empathy_audio_src_finalize (GObject
*object
);
360 empathy_audio_src_set_property (GObject
*object
,
361 guint property_id
, const GValue
*value
, GParamSpec
*pspec
)
366 empathy_audio_src_set_hw_volume (EMPATHY_GST_AUDIO_SRC (object
),
367 g_value_get_double (value
));
370 empathy_audio_set_hw_mute (EMPATHY_GST_AUDIO_SRC (object
),
371 g_value_get_boolean (value
));
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
379 empathy_audio_src_get_property (GObject
*object
,
380 guint property_id
, GValue
*value
, GParamSpec
*pspec
)
382 EmpathyGstAudioSrc
*self
= EMPATHY_GST_AUDIO_SRC (object
);
383 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
388 g_value_set_double (value
, priv
->volume
);
391 g_value_set_boolean (value
, priv
->mute
);
393 case PROP_MICROPHONE
:
394 g_value_set_uint (value
, priv
->source_idx
);
397 G_OBJECT_WARN_INVALID_PROPERTY_ID (object
, property_id
, pspec
);
402 empathy_audio_src_class_init (EmpathyGstAudioSrcClass
403 *empathy_audio_src_class
)
405 GObjectClass
*object_class
= G_OBJECT_CLASS (empathy_audio_src_class
);
406 GParamSpec
*param_spec
;
408 g_type_class_add_private (empathy_audio_src_class
,
409 sizeof (EmpathyGstAudioSrcPrivate
));
411 object_class
->dispose
= empathy_audio_src_dispose
;
412 object_class
->finalize
= empathy_audio_src_finalize
;
414 object_class
->set_property
= empathy_audio_src_set_property
;
415 object_class
->get_property
= empathy_audio_src_get_property
;
417 param_spec
= g_param_spec_double ("volume", "Volume", "volume contol",
419 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
420 g_object_class_install_property (object_class
, PROP_VOLUME
, param_spec
);
422 param_spec
= g_param_spec_boolean ("mute", "Mute", "mute contol",
424 G_PARAM_READWRITE
| G_PARAM_STATIC_STRINGS
);
425 g_object_class_install_property (object_class
, PROP_MUTE
, param_spec
);
427 param_spec
= g_param_spec_uint ("microphone", "microphone", "microphone",
428 0, G_MAXUINT
, G_MAXUINT
,
429 G_PARAM_READABLE
| G_PARAM_STATIC_STRINGS
);
430 g_object_class_install_property (object_class
, PROP_MICROPHONE
, param_spec
);
434 empathy_audio_src_dispose (GObject
*object
)
436 EmpathyGstAudioSrc
*self
= EMPATHY_GST_AUDIO_SRC (object
);
437 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
439 if (priv
->dispose_has_run
)
442 priv
->dispose_has_run
= TRUE
;
444 if (priv
->volume_idle_id
!= 0)
445 g_source_remove (priv
->volume_idle_id
);
446 priv
->volume_idle_id
= 0;
448 tp_clear_object (&priv
->mic_monitor
);
450 /* release any references held by the object here */
452 if (G_OBJECT_CLASS (empathy_audio_src_parent_class
)->dispose
)
453 G_OBJECT_CLASS (empathy_audio_src_parent_class
)->dispose (object
);
457 empathy_audio_src_finalize (GObject
*object
)
459 EmpathyGstAudioSrc
*self
= EMPATHY_GST_AUDIO_SRC (object
);
460 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
462 /* free any data held directly by the object here */
463 g_mutex_clear (&priv
->lock
);
465 G_OBJECT_CLASS (empathy_audio_src_parent_class
)->finalize (object
);
469 empathy_audio_src_volume_changed_idle (gpointer user_data
)
471 EmpathyGstAudioSrc
*self
= EMPATHY_GST_AUDIO_SRC (user_data
);
472 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (self
);
476 g_mutex_lock (&priv
->lock
);
477 priv
->volume_idle_id
= 0;
478 g_mutex_unlock (&priv
->lock
);
480 volume
= empathy_audio_src_get_hw_volume (self
);
482 if (volume
!= priv
->volume
)
484 priv
->volume
= volume
;
485 g_object_notify (G_OBJECT (self
), "volume");
488 mute
= empathy_audio_src_get_hw_mute (self
);
489 if (mute
!= priv
->mute
)
492 /* hw mute changed, follow with own volume */
493 g_object_set (self
->priv
->volume_element
, "mute", mute
, NULL
);
494 g_object_notify (G_OBJECT (self
), "mute");
501 empathy_audio_src_volume_changed (GObject
*object
,
505 EmpathyGstAudioSrc
*self
= EMPATHY_GST_AUDIO_SRC (user_data
);
507 g_mutex_lock (&self
->priv
->lock
);
508 if (self
->priv
->volume_idle_id
== 0)
509 self
->priv
->volume_idle_id
= g_idle_add (
510 empathy_audio_src_volume_changed_idle
, self
);
511 g_mutex_unlock (&self
->priv
->lock
);
517 empathy_audio_src_new (void)
519 static gboolean registered
= FALSE
;
522 if (!gst_element_register (NULL
, "empathyaudiosrc",
523 GST_RANK_NONE
, EMPATHY_TYPE_GST_AUDIO_SRC
))
527 return gst_element_factory_make ("empathyaudiosrc", NULL
);
531 empathy_audio_src_set_echo_cancel (EmpathyGstAudioSrc
*src
,
534 DEBUG ("Src echo cancellation setting: %s", enable
? "on" : "off");
535 empathy_audio_set_stream_properties (src
->priv
->src
, enable
);
539 empathy_audio_src_set_volume (EmpathyGstAudioSrc
*src
, gdouble volume
)
541 g_object_set (src
, "volume", volume
, NULL
);
545 empathy_audio_src_get_volume (EmpathyGstAudioSrc
*src
)
547 return src
->priv
->volume
;
551 empathy_audio_src_get_microphone (EmpathyGstAudioSrc
*src
)
553 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src
);
555 return priv
->source_idx
;
559 empathy_audio_src_change_microphone_cb (GObject
*source_object
,
560 GAsyncResult
*result
,
563 EmpathyMicMonitor
*monitor
= EMPATHY_MIC_MONITOR (source_object
);
564 GSimpleAsyncResult
*simple
= user_data
;
565 GError
*error
= NULL
;
567 if (!empathy_mic_monitor_change_microphone_finish (monitor
,
570 g_simple_async_result_take_error (simple
, error
);
573 g_simple_async_result_complete (simple
);
574 g_object_unref (simple
);
578 empathy_audio_src_change_microphone_async (EmpathyGstAudioSrc
*src
,
580 GAsyncReadyCallback callback
,
583 EmpathyGstAudioSrcPrivate
*priv
= EMPATHY_GST_AUDIO_SRC_GET_PRIVATE (src
);
584 guint source_output_idx
;
585 GSimpleAsyncResult
*simple
;
587 simple
= g_simple_async_result_new (G_OBJECT (src
), callback
, user_data
,
588 empathy_audio_src_change_microphone_async
);
590 if (!empathy_audio_src_supports_changing_mic (src
))
592 g_simple_async_result_set_error (simple
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
593 "pulsesrc is not new enough to support changing microphone");
594 g_simple_async_result_complete_in_idle (simple
);
595 g_object_unref (simple
);
599 source_output_idx
= empathy_audio_src_get_mic_index (src
);
601 if (source_output_idx
== PA_INVALID_INDEX
)
603 g_simple_async_result_set_error (simple
, G_IO_ERROR
, G_IO_ERROR_FAILED
,
604 "pulsesrc is not yet PLAYING");
605 g_simple_async_result_complete_in_idle (simple
);
606 g_object_unref (simple
);
610 empathy_mic_monitor_change_microphone_async (priv
->mic_monitor
,
611 source_output_idx
, microphone
, empathy_audio_src_change_microphone_cb
,
616 empathy_audio_src_change_microphone_finish (EmpathyGstAudioSrc
*src
,
617 GAsyncResult
*result
,
620 tpaw_implement_finish_void (src
,
621 empathy_audio_src_change_microphone_async
);
625 empathy_audio_src_set_mute (EmpathyGstAudioSrc
*self
,
628 empathy_audio_set_hw_mute (self
, mute
);
630 g_object_notify (G_OBJECT (self
), "mute");