2 * GStreamer pulseaudio plugin
4 * Copyright (c) 2004-2008 Lennart Poettering
6 * gst-pulse is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 2.1 of the
9 * License, or (at your option) any later version.
11 * gst-pulse is distributed in the hope that it will be useful, but
12 * 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 gst-pulse; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
23 * SECTION:element-pulsesrc
24 * @see_also: pulsesink
26 * This element captures audio from a
27 * <ulink href="http://www.pulseaudio.org">PulseAudio sound server</ulink>.
30 * <title>Example pipelines</title>
32 * gst-launch-1.0 -v pulsesrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg
33 * ]| Record from a sound card using pulseaudio and encode to Ogg/Vorbis.
44 #include <gst/base/gstbasesrc.h>
45 #include <gst/gsttaglist.h>
46 #include <gst/audio/audio.h>
49 #include "pulseutil.h"
51 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
52 #define GST_CAT_DEFAULT pulse_debug
54 #define DEFAULT_SERVER NULL
55 #define DEFAULT_DEVICE NULL
56 #define DEFAULT_CURRENT_DEVICE NULL
57 #define DEFAULT_DEVICE_NAME NULL
59 #define DEFAULT_VOLUME 1.0
60 #define DEFAULT_MUTE FALSE
61 #define MAX_VOLUME 10.0
63 /* See the pulsesink code for notes on how we interact with the PA mainloop
74 PROP_STREAM_PROPERTIES,
75 PROP_SOURCE_OUTPUT_INDEX,
81 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
82 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
84 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
85 const GValue * value, GParamSpec * pspec);
86 static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
87 GValue * value, GParamSpec * pspec);
88 static void gst_pulsesrc_finalize (GObject * object);
90 static gboolean gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked,
92 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
94 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
96 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
97 GstAudioRingBufferSpec * spec);
99 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
101 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
102 guint length, GstClockTime * timestamp);
103 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
105 static void gst_pulsesrc_reset (GstAudioSrc * src);
107 static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
108 static gboolean gst_pulsesrc_event (GstBaseSrc * basesrc, GstEvent * event);
110 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
111 element, GstStateChange transition);
113 static GstClockTime gst_pulsesrc_get_time (GstClock * clock, GstPulseSrc * src);
115 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
118 GST_STATIC_CAPS (_PULSE_CAPS_PCM)
121 #define gst_pulsesrc_parent_class parent_class
122 G_DEFINE_TYPE_WITH_CODE (GstPulseSrc, gst_pulsesrc, GST_TYPE_AUDIO_SRC,
123 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
126 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
128 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
129 GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
130 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
131 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
134 gobject_class->finalize = gst_pulsesrc_finalize;
135 gobject_class->set_property = gst_pulsesrc_set_property;
136 gobject_class->get_property = gst_pulsesrc_get_property;
138 gstelement_class->change_state =
139 GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
141 gstbasesrc_class->event = GST_DEBUG_FUNCPTR (gst_pulsesrc_event);
142 gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
144 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
145 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
146 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
147 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
148 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
149 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
150 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
152 /* Overwrite GObject fields */
153 g_object_class_install_property (gobject_class,
155 g_param_spec_string ("server", "Server",
156 "The PulseAudio server to connect to", DEFAULT_SERVER,
157 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
159 g_object_class_install_property (gobject_class, PROP_DEVICE,
160 g_param_spec_string ("device", "Device",
161 "The PulseAudio source device to connect to", DEFAULT_DEVICE,
162 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164 g_object_class_install_property (gobject_class, PROP_CURRENT_DEVICE,
165 g_param_spec_string ("current-device", "Current Device",
166 "The current PulseAudio source device", DEFAULT_CURRENT_DEVICE,
167 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
169 g_object_class_install_property (gobject_class,
171 g_param_spec_string ("device-name", "Device name",
172 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
173 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
175 clientname = gst_pulse_client_name ();
177 * GstPulseSrc:client-name
179 * The PulseAudio client name to use.
181 g_object_class_install_property (gobject_class,
183 g_param_spec_string ("client-name", "Client Name",
184 "The PulseAudio client_name_to_use", clientname,
185 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
186 GST_PARAM_MUTABLE_READY));
190 * GstPulseSrc:stream-properties:
192 * List of pulseaudio stream properties. A list of defined properties can be
193 * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
195 * Below is an example for registering as a music application to pulseaudio.
197 * GstStructure *props;
199 * props = gst_structure_from_string ("props,media.role=music", NULL);
200 * g_object_set (pulse, "stream-properties", props, NULL);
201 * gst_structure_free (props);
204 g_object_class_install_property (gobject_class,
205 PROP_STREAM_PROPERTIES,
206 g_param_spec_boxed ("stream-properties", "stream properties",
207 "list of pulseaudio stream properties",
208 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
210 * GstPulseSrc:source-output-index:
212 * The index of the PulseAudio source output corresponding to this element.
214 g_object_class_install_property (gobject_class,
215 PROP_SOURCE_OUTPUT_INDEX,
216 g_param_spec_uint ("source-output-index", "source output index",
217 "The index of the PulseAudio source output corresponding to this "
218 "record stream", 0, G_MAXUINT, PA_INVALID_INDEX,
219 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
221 gst_element_class_set_static_metadata (gstelement_class,
222 "PulseAudio Audio Source",
224 "Captures audio from a PulseAudio server", "Lennart Poettering");
225 gst_element_class_add_static_pad_template (gstelement_class, &pad_template);
228 * GstPulseSrc:volume:
230 * The volume of the record stream.
232 g_object_class_install_property (gobject_class,
233 PROP_VOLUME, g_param_spec_double ("volume", "Volume",
234 "Linear volume of this stream, 1.0=100%",
235 0.0, MAX_VOLUME, DEFAULT_VOLUME,
236 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 * Whether the stream is muted or not.
243 g_object_class_install_property (gobject_class,
244 PROP_MUTE, g_param_spec_boolean ("mute", "Mute",
245 "Mute state of this stream",
246 DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 gst_pulsesrc_init (GstPulseSrc * pulsesrc)
252 pulsesrc->server = NULL;
253 pulsesrc->device = NULL;
254 pulsesrc->client_name = gst_pulse_client_name ();
255 pulsesrc->device_description = NULL;
257 pulsesrc->context = NULL;
258 pulsesrc->stream = NULL;
259 pulsesrc->stream_connected = FALSE;
260 pulsesrc->source_output_idx = PA_INVALID_INDEX;
262 pulsesrc->read_buffer = NULL;
263 pulsesrc->read_buffer_length = 0;
265 pa_sample_spec_init (&pulsesrc->sample_spec);
267 pulsesrc->operation_success = FALSE;
268 pulsesrc->paused = TRUE;
269 pulsesrc->in_read = FALSE;
271 pulsesrc->volume = DEFAULT_VOLUME;
272 pulsesrc->volume_set = FALSE;
274 pulsesrc->mute = DEFAULT_MUTE;
275 pulsesrc->mute_set = FALSE;
277 pulsesrc->notify = 0;
279 pulsesrc->properties = NULL;
280 pulsesrc->proplist = NULL;
282 /* this should be the default but it isn't yet */
283 gst_audio_base_src_set_slave_method (GST_AUDIO_BASE_SRC (pulsesrc),
284 GST_AUDIO_BASE_SRC_SLAVE_SKEW);
286 /* override with a custom clock */
287 if (GST_AUDIO_BASE_SRC (pulsesrc)->clock)
288 gst_object_unref (GST_AUDIO_BASE_SRC (pulsesrc)->clock);
290 GST_AUDIO_BASE_SRC (pulsesrc)->clock =
291 gst_audio_clock_new ("GstPulseSrcClock",
292 (GstAudioClockGetTimeFunc) gst_pulsesrc_get_time, pulsesrc, NULL);
296 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
298 if (pulsesrc->stream) {
299 pa_stream_disconnect (pulsesrc->stream);
300 pa_stream_unref (pulsesrc->stream);
301 pulsesrc->stream = NULL;
302 pulsesrc->stream_connected = FALSE;
303 pulsesrc->source_output_idx = PA_INVALID_INDEX;
304 g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
307 g_free (pulsesrc->device_description);
308 pulsesrc->device_description = NULL;
312 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
315 gst_pulsesrc_destroy_stream (pulsesrc);
317 if (pulsesrc->context) {
318 pa_context_disconnect (pulsesrc->context);
320 /* Make sure we don't get any further callbacks */
321 pa_context_set_state_callback (pulsesrc->context, NULL, NULL);
322 pa_context_set_subscribe_callback (pulsesrc->context, NULL, NULL);
324 pa_context_unref (pulsesrc->context);
326 pulsesrc->context = NULL;
331 gst_pulsesrc_finalize (GObject * object)
333 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
335 g_free (pulsesrc->server);
336 g_free (pulsesrc->device);
337 g_free (pulsesrc->client_name);
338 g_free (pulsesrc->current_source_name);
340 if (pulsesrc->properties)
341 gst_structure_free (pulsesrc->properties);
342 if (pulsesrc->proplist)
343 pa_proplist_free (pulsesrc->proplist);
345 G_OBJECT_CLASS (parent_class)->finalize (object);
348 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
349 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
352 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
354 if (!pulsesrc->stream_connected)
357 if (!CONTEXT_OK (pulsesrc->context))
360 if (check_stream && !STREAM_OK (pulsesrc->stream))
367 const gchar *err_str = pulsesrc->context ?
368 pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
369 GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
376 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
379 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
384 g_free (pulsesrc->device_description);
385 pulsesrc->device_description = g_strdup (i->description);
388 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
392 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
394 pa_operation *o = NULL;
397 if (!pulsesrc->mainloop)
400 pa_threaded_mainloop_lock (pulsesrc->mainloop);
402 if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
403 pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
405 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
406 ("pa_stream_get_source_info() failed: %s",
407 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
411 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
413 if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
416 pa_threaded_mainloop_wait (pulsesrc->mainloop);
422 pa_operation_unref (o);
424 t = g_strdup (pulsesrc->device_description);
426 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
432 GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
438 gst_pulsesrc_source_output_info_cb (pa_context * c,
439 const pa_source_output_info * i, int eol, void *userdata)
443 psrc = GST_PULSESRC_CAST (userdata);
448 /* If the index doesn't match our current stream,
449 * it implies we just recreated the stream (caps change)
451 if (i->index == psrc->source_output_idx) {
452 psrc->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
453 psrc->mute = i->mute;
454 psrc->current_source_idx = i->source;
456 if (G_UNLIKELY (psrc->volume > MAX_VOLUME)) {
457 GST_WARNING_OBJECT (psrc, "Clipped volume from %f to %f",
458 psrc->volume, MAX_VOLUME);
459 psrc->volume = MAX_VOLUME;
464 pa_threaded_mainloop_signal (psrc->mainloop, 0);
468 gst_pulsesrc_get_source_output_info (GstPulseSrc * pulsesrc, gdouble * volume,
471 pa_operation *o = NULL;
473 if (!pulsesrc->mainloop)
476 if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
479 pa_threaded_mainloop_lock (pulsesrc->mainloop);
481 if (!(o = pa_context_get_source_output_info (pulsesrc->context,
482 pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
486 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
487 pa_threaded_mainloop_wait (pulsesrc->mainloop);
488 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
495 *volume = pulsesrc->volume;
497 *mute = pulsesrc->mute;
500 pa_operation_unref (o);
502 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
509 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
511 *volume = pulsesrc->volume;
513 *mute = pulsesrc->mute;
518 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
520 *volume = pulsesrc->volume;
522 *mute = pulsesrc->mute;
527 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
528 ("pa_context_get_source_output_info() failed: %s",
529 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
535 gst_pulsesrc_current_source_info_cb (pa_context * c, const pa_source_info * i,
536 int eol, void *userdata)
540 psrc = GST_PULSESRC_CAST (userdata);
545 /* If the index doesn't match our current stream,
546 * it implies we just recreated the stream (caps change)
548 if (i->index == psrc->current_source_idx) {
549 g_free (psrc->current_source_name);
550 psrc->current_source_name = g_strdup (i->name);
554 pa_threaded_mainloop_signal (psrc->mainloop, 0);
558 gst_pulsesrc_get_current_device (GstPulseSrc * pulsesrc)
560 pa_operation *o = NULL;
563 if (!pulsesrc->mainloop)
566 if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
569 gst_pulsesrc_get_source_output_info (pulsesrc, NULL, NULL);
571 pa_threaded_mainloop_lock (pulsesrc->mainloop);
574 if (!(o = pa_context_get_source_info_by_index (pulsesrc->context,
575 pulsesrc->current_source_idx, gst_pulsesrc_current_source_info_cb,
579 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
580 pa_threaded_mainloop_wait (pulsesrc->mainloop);
581 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
587 current_src = g_strdup (pulsesrc->current_source_name);
590 pa_operation_unref (o);
592 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
599 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
604 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
609 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
610 ("pa_context_get_source_output_info() failed: %s",
611 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
617 gst_pulsesrc_set_stream_volume (GstPulseSrc * pulsesrc, gdouble volume)
620 pa_operation *o = NULL;
622 if (!pulsesrc->mainloop)
625 if (!pulsesrc->source_output_idx)
628 pa_threaded_mainloop_lock (pulsesrc->mainloop);
630 GST_DEBUG_OBJECT (pulsesrc, "setting volume to %f", volume);
632 gst_pulse_cvolume_from_linear (&v, pulsesrc->sample_spec.channels, volume);
634 if (!(o = pa_context_set_source_output_volume (pulsesrc->context,
635 pulsesrc->source_output_idx, &v, NULL, NULL)))
638 /* We don't really care about the result of this call */
642 pa_operation_unref (o);
644 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
651 pulsesrc->volume = volume;
652 pulsesrc->volume_set = TRUE;
653 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
658 pulsesrc->volume = volume;
659 pulsesrc->volume_set = TRUE;
660 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
665 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
666 ("pa_stream_set_source_output_volume() failed: %s",
667 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
673 gst_pulsesrc_set_stream_mute (GstPulseSrc * pulsesrc, gboolean mute)
675 pa_operation *o = NULL;
677 if (!pulsesrc->mainloop)
680 if (!pulsesrc->source_output_idx)
683 pa_threaded_mainloop_lock (pulsesrc->mainloop);
685 GST_DEBUG_OBJECT (pulsesrc, "setting mute state to %d", mute);
687 if (!(o = pa_context_set_source_output_mute (pulsesrc->context,
688 pulsesrc->source_output_idx, mute, NULL, NULL)))
691 /* We don't really care about the result of this call */
695 pa_operation_unref (o);
697 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
704 pulsesrc->mute = mute;
705 pulsesrc->mute_set = TRUE;
706 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
711 pulsesrc->mute = mute;
712 pulsesrc->mute_set = TRUE;
713 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
718 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
719 ("pa_stream_set_source_output_mute() failed: %s",
720 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
726 gst_pulsesrc_set_stream_device (GstPulseSrc * pulsesrc, const gchar * device)
728 pa_operation *o = NULL;
730 if (!pulsesrc->mainloop)
733 if (!pulsesrc->source_output_idx)
736 pa_threaded_mainloop_lock (pulsesrc->mainloop);
738 GST_DEBUG_OBJECT (pulsesrc, "setting stream device to %s", device);
740 if (!(o = pa_context_move_source_output_by_name (pulsesrc->context,
741 pulsesrc->source_output_idx, device, NULL, NULL)))
747 pa_operation_unref (o);
749 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
756 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
761 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
766 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
767 ("pa_context_move_source_output_by_name(%s) failed: %s",
768 device, pa_strerror (pa_context_errno (pulsesrc->context))),
775 gst_pulsesrc_set_property (GObject * object,
776 guint prop_id, const GValue * value, GParamSpec * pspec)
779 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
783 g_free (pulsesrc->server);
784 pulsesrc->server = g_value_dup_string (value);
787 g_free (pulsesrc->device);
788 pulsesrc->device = g_value_dup_string (value);
789 gst_pulsesrc_set_stream_device (pulsesrc, pulsesrc->device);
791 case PROP_CLIENT_NAME:
792 g_free (pulsesrc->client_name);
793 if (!g_value_get_string (value)) {
794 GST_WARNING_OBJECT (pulsesrc,
795 "Empty PulseAudio client name not allowed. Resetting to default value");
796 pulsesrc->client_name = gst_pulse_client_name ();
798 pulsesrc->client_name = g_value_dup_string (value);
800 case PROP_STREAM_PROPERTIES:
801 if (pulsesrc->properties)
802 gst_structure_free (pulsesrc->properties);
803 pulsesrc->properties =
804 gst_structure_copy (gst_value_get_structure (value));
805 if (pulsesrc->proplist)
806 pa_proplist_free (pulsesrc->proplist);
807 pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
810 gst_pulsesrc_set_stream_volume (pulsesrc, g_value_get_double (value));
813 gst_pulsesrc_set_stream_mute (pulsesrc, g_value_get_boolean (value));
816 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
822 gst_pulsesrc_get_property (GObject * object,
823 guint prop_id, GValue * value, GParamSpec * pspec)
826 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
830 g_value_set_string (value, pulsesrc->server);
833 g_value_set_string (value, pulsesrc->device);
835 case PROP_CURRENT_DEVICE:
837 gchar *current_device = gst_pulsesrc_get_current_device (pulsesrc);
839 g_value_take_string (value, current_device);
841 g_value_set_string (value, "");
844 case PROP_DEVICE_NAME:
845 g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
847 case PROP_CLIENT_NAME:
848 g_value_set_string (value, pulsesrc->client_name);
850 case PROP_STREAM_PROPERTIES:
851 gst_value_set_structure (value, pulsesrc->properties);
853 case PROP_SOURCE_OUTPUT_INDEX:
854 g_value_set_uint (value, pulsesrc->source_output_idx);
859 gst_pulsesrc_get_source_output_info (pulsesrc, &volume, NULL);
860 g_value_set_double (value, volume);
866 gst_pulsesrc_get_source_output_info (pulsesrc, NULL, &mute);
867 g_value_set_boolean (value, mute);
871 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
877 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
879 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
881 switch (pa_context_get_state (c)) {
882 case PA_CONTEXT_READY:
883 case PA_CONTEXT_TERMINATED:
884 case PA_CONTEXT_FAILED:
885 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
888 case PA_CONTEXT_UNCONNECTED:
889 case PA_CONTEXT_CONNECTING:
890 case PA_CONTEXT_AUTHORIZING:
891 case PA_CONTEXT_SETTING_NAME:
897 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
899 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
901 switch (pa_stream_get_state (s)) {
903 case PA_STREAM_READY:
904 case PA_STREAM_FAILED:
905 case PA_STREAM_TERMINATED:
906 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
909 case PA_STREAM_UNCONNECTED:
910 case PA_STREAM_CREATING:
916 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
918 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
920 GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
922 if (pulsesrc->in_read) {
923 /* only signal when reading */
924 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
929 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
931 const pa_timing_info *info;
932 pa_usec_t source_usec;
934 info = pa_stream_get_timing_info (s);
937 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
938 "latency update (information unknown)");
941 source_usec = info->configured_source_usec;
943 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
944 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
945 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
946 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
947 info->write_index, info->read_index_corrupt, info->read_index,
948 info->source_usec, source_usec);
952 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
954 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
958 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
960 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
964 gst_pulsesrc_context_subscribe_cb (pa_context * c,
965 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
967 GstPulseSrc *psrc = GST_PULSESRC (userdata);
969 if (t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE)
970 && t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_NEW))
973 if (idx != psrc->source_output_idx)
976 /* Actually this event is also triggered when other properties of the stream
977 * change that are unrelated to the volume. However it is probably cheaper to
978 * signal the change here and check for the volume when the GObject property
979 * is read instead of querying it always. */
981 /* inform streaming thread to notify */
982 g_atomic_int_compare_and_exchange (&psrc->notify, 0, 1);
986 gst_pulsesrc_open (GstAudioSrc * asrc)
988 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
990 pa_threaded_mainloop_lock (pulsesrc->mainloop);
992 g_assert (!pulsesrc->context);
993 g_assert (!pulsesrc->stream);
995 GST_DEBUG_OBJECT (pulsesrc, "opening device");
997 if (!(pulsesrc->context =
998 pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
999 pulsesrc->client_name))) {
1000 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
1002 goto unlock_and_fail;
1005 pa_context_set_state_callback (pulsesrc->context,
1006 gst_pulsesrc_context_state_cb, pulsesrc);
1007 pa_context_set_subscribe_callback (pulsesrc->context,
1008 gst_pulsesrc_context_subscribe_cb, pulsesrc);
1010 GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
1011 GST_STR_NULL (pulsesrc->server));
1013 if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
1014 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
1015 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1016 goto unlock_and_fail;
1020 pa_context_state_t state;
1022 state = pa_context_get_state (pulsesrc->context);
1024 if (!PA_CONTEXT_IS_GOOD (state)) {
1025 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
1026 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1027 goto unlock_and_fail;
1030 if (state == PA_CONTEXT_READY)
1033 /* Wait until the context is ready */
1034 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1036 GST_DEBUG_OBJECT (pulsesrc, "connected");
1038 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1045 gst_pulsesrc_destroy_context (pulsesrc);
1047 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1054 gst_pulsesrc_close (GstAudioSrc * asrc)
1056 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1058 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1059 gst_pulsesrc_destroy_context (pulsesrc);
1060 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1066 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
1068 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1070 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1071 gst_pulsesrc_destroy_stream (pulsesrc);
1073 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1075 pulsesrc->read_buffer = NULL;
1076 pulsesrc->read_buffer_length = 0;
1082 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length,
1083 GstClockTime * timestamp)
1085 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1088 if (g_atomic_int_compare_and_exchange (&pulsesrc->notify, 1, 0)) {
1089 g_object_notify (G_OBJECT (pulsesrc), "volume");
1090 g_object_notify (G_OBJECT (pulsesrc), "mute");
1091 g_object_notify (G_OBJECT (pulsesrc), "current-device");
1094 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1095 pulsesrc->in_read = TRUE;
1097 if (!pulsesrc->stream_connected)
1100 if (pulsesrc->paused)
1103 while (length > 0) {
1106 GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
1108 /*check if we have a leftover buffer */
1109 if (!pulsesrc->read_buffer) {
1111 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1112 goto unlock_and_fail;
1114 /* read all available data, we keep a pointer to the data and the length
1115 * and take from it what we need. */
1116 if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
1117 &pulsesrc->read_buffer_length) < 0)
1120 GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
1121 pulsesrc->read_buffer_length);
1123 /* if we have data, process if */
1124 if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
1127 /* now wait for more data to become available */
1128 GST_LOG_OBJECT (pulsesrc, "waiting for data");
1129 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1131 if (pulsesrc->paused)
1136 l = pulsesrc->read_buffer_length >
1137 length ? length : pulsesrc->read_buffer_length;
1139 memcpy (data, pulsesrc->read_buffer, l);
1141 pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
1142 pulsesrc->read_buffer_length -= l;
1144 data = (guint8 *) data + l;
1148 if (pulsesrc->read_buffer_length <= 0) {
1149 /* we copied all of the data, drop it now */
1150 if (pa_stream_drop (pulsesrc->stream) < 0)
1153 /* reset pointer to data */
1154 pulsesrc->read_buffer = NULL;
1155 pulsesrc->read_buffer_length = 0;
1159 pulsesrc->in_read = FALSE;
1160 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1167 GST_LOG_OBJECT (pulsesrc, "we are not connected");
1168 goto unlock_and_fail;
1172 GST_LOG_OBJECT (pulsesrc, "we are paused");
1173 goto unlock_and_fail;
1177 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1178 ("pa_stream_peek() failed: %s",
1179 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1180 goto unlock_and_fail;
1184 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1185 ("pa_stream_drop() failed: %s",
1186 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1187 goto unlock_and_fail;
1191 pulsesrc->in_read = FALSE;
1192 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1198 /* return the delay in samples */
1200 gst_pulsesrc_delay (GstAudioSrc * asrc)
1202 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1207 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1208 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1211 /* get the latency, this can fail when we don't have a latency update yet.
1212 * We don't want to wait for latency updates here but we just return 0. */
1213 res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
1215 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1218 GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
1224 result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
1231 GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
1232 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1238 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps ** caps,
1239 GstAudioRingBufferSpec * rspec)
1241 pa_channel_map channel_map;
1242 const pa_channel_map *m;
1244 gboolean need_channel_layout = FALSE;
1245 GstAudioRingBufferSpec new_spec, *spec = NULL;
1249 /* If we already have a stream (renegotiation), free it first */
1250 if (pulsesrc->stream)
1251 gst_pulsesrc_destroy_stream (pulsesrc);
1254 /* Post-negotiation, we already have a ringbuffer spec, so we just need to
1255 * use it to create a stream. */
1258 /* At this point, we expect the channel-mask to be set in caps, so we just
1260 if (!gst_pulse_gst_to_channel_map (&channel_map, spec))
1264 /* At negotiation time, we get a fixed caps and use it to set up a stream */
1265 s = gst_caps_get_structure (*caps, 0);
1266 gst_structure_get_int (s, "channels", &new_spec.info.channels);
1267 if (!gst_structure_has_field (s, "channel-mask")) {
1268 if (new_spec.info.channels == 1) {
1269 pa_channel_map_init_mono (&channel_map);
1270 } else if (new_spec.info.channels == 2) {
1271 pa_channel_map_init_stereo (&channel_map);
1273 need_channel_layout = TRUE;
1274 gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
1275 G_GUINT64_CONSTANT (0), NULL);
1279 memset (&new_spec, 0, sizeof (GstAudioRingBufferSpec));
1280 new_spec.latency_time = GST_SECOND;
1281 if (!gst_audio_ring_buffer_parse_caps (&new_spec, *caps))
1284 /* Keep the refcount of the caps at 1 to make them writable */
1285 gst_caps_unref (new_spec.caps);
1287 if (!need_channel_layout
1288 && !gst_pulse_gst_to_channel_map (&channel_map, &new_spec)) {
1289 need_channel_layout = TRUE;
1290 gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
1291 G_GUINT64_CONSTANT (0), NULL);
1292 for (i = 0; i < G_N_ELEMENTS (new_spec.info.position); i++)
1293 new_spec.info.position[i] = GST_AUDIO_CHANNEL_POSITION_INVALID;
1298 /* !rspec && !caps */
1299 g_assert_not_reached ();
1302 if (!gst_pulse_fill_sample_spec (spec, &pulsesrc->sample_spec))
1305 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1307 if (!pulsesrc->context)
1310 name = "Record Stream";
1311 if (pulsesrc->proplist) {
1312 if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
1313 name, &pulsesrc->sample_spec,
1314 (need_channel_layout) ? NULL : &channel_map,
1315 pulsesrc->proplist)))
1318 } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
1319 name, &pulsesrc->sample_spec,
1320 (need_channel_layout) ? NULL : &channel_map)))
1324 m = pa_stream_get_channel_map (pulsesrc->stream);
1325 gst_pulse_channel_map_to_gst (m, &new_spec);
1326 gst_audio_channel_positions_to_valid_order (new_spec.info.position,
1327 new_spec.info.channels);
1328 gst_caps_unref (*caps);
1329 *caps = gst_audio_info_to_caps (&new_spec.info);
1331 GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, *caps);
1335 pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
1337 pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
1339 pa_stream_set_underflow_callback (pulsesrc->stream,
1340 gst_pulsesrc_stream_underflow_cb, pulsesrc);
1341 pa_stream_set_overflow_callback (pulsesrc->stream,
1342 gst_pulsesrc_stream_overflow_cb, pulsesrc);
1343 pa_stream_set_latency_update_callback (pulsesrc->stream,
1344 gst_pulsesrc_stream_latency_update_cb, pulsesrc);
1346 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1353 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1354 ("Can't parse caps."), (NULL));
1359 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1360 ("Invalid sample specification."), (NULL));
1365 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
1366 goto unlock_and_fail;
1370 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1371 ("Failed to create stream: %s",
1372 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1373 goto unlock_and_fail;
1377 gst_pulsesrc_destroy_stream (pulsesrc);
1379 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1387 gst_pulsesrc_event (GstBaseSrc * basesrc, GstEvent * event)
1389 GST_DEBUG_OBJECT (basesrc, "handle event %" GST_PTR_FORMAT, event);
1391 switch (GST_EVENT_TYPE (event)) {
1392 case GST_EVENT_RECONFIGURE:
1393 gst_pad_check_reconfigure (GST_BASE_SRC_PAD (basesrc));
1398 return GST_BASE_SRC_CLASS (parent_class)->event (basesrc, event);
1401 /* This is essentially gst_base_src_negotiate_default() but the caps
1402 * are guaranteed to have a channel layout for > 2 channels
1405 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
1407 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (basesrc);
1409 GstCaps *caps = NULL;
1410 GstCaps *peercaps = NULL;
1411 gboolean result = FALSE;
1413 /* first see what is possible on our source pad */
1414 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1415 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1416 /* nothing or anything is allowed, we're done */
1417 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1418 goto no_nego_needed;
1420 /* get the peer caps */
1421 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1422 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1424 /* get intersection */
1425 caps = gst_caps_intersect (thiscaps, peercaps);
1426 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
1427 gst_caps_unref (thiscaps);
1428 gst_caps_unref (peercaps);
1430 /* no peer, work with our own caps then */
1434 /* take first (and best, since they are sorted) possibility */
1435 caps = gst_caps_truncate (caps);
1438 if (!gst_caps_is_empty (caps)) {
1439 caps = GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
1440 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1442 if (gst_caps_is_any (caps)) {
1443 /* hmm, still anything, so element can do anything and
1444 * nego is not needed */
1446 } else if (gst_caps_is_fixed (caps)) {
1447 /* yay, fixed caps, use those then */
1448 result = gst_pulsesrc_create_stream (pulsesrc, &caps, NULL);
1450 result = gst_base_src_set_caps (basesrc, caps);
1453 gst_caps_unref (caps);
1459 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1461 gst_caps_unref (thiscaps);
1467 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
1469 pa_buffer_attr wanted;
1470 const pa_buffer_attr *actual;
1471 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1472 pa_stream_flags_t flags;
1474 GstAudioClock *clock;
1476 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1478 if (!pulsesrc->stream)
1479 gst_pulsesrc_create_stream (pulsesrc, NULL, spec);
1482 GstAudioRingBufferSpec s = *spec;
1483 const pa_channel_map *m;
1485 m = pa_stream_get_channel_map (pulsesrc->stream);
1486 gst_pulse_channel_map_to_gst (m, &s);
1487 gst_audio_ring_buffer_set_channel_positions (GST_AUDIO_BASE_SRC
1488 (pulsesrc)->ringbuffer, s.info.position);
1491 /* enable event notifications */
1492 GST_LOG_OBJECT (pulsesrc, "subscribing to context events");
1493 if (!(o = pa_context_subscribe (pulsesrc->context,
1494 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, NULL, NULL))) {
1495 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1496 ("pa_context_subscribe() failed: %s",
1497 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1498 goto unlock_and_fail;
1501 pa_operation_unref (o);
1503 /* There's a bit of a disconnect here between the audio ringbuffer and what
1504 * PulseAudio provides. The audio ringbuffer provide a total of buffer_time
1505 * worth of buffering, divided into segments of latency_time size. We're
1506 * asking PulseAudio to provide a total latency of latency_time, which, with
1507 * PA_STREAM_ADJUST_LATENCY, effectively sets itself up as a ringbuffer with
1508 * one segment being the hardware buffer, and the other the software buffer.
1509 * This segment size is returned as the fragsize.
1511 * Since the two concepts don't map very well, what we do is keep segsize as
1512 * it is (unless fragsize is even larger, in which case we use that). We'll
1513 * get data from PulseAudio in smaller chunks than we want to pass on as an
1514 * element, and we coalesce those chunks in the ringbuffer memory and pass it
1515 * on in the expected chunk size. */
1516 wanted.maxlength = spec->segsize * spec->segtotal;
1517 wanted.tlength = -1;
1520 wanted.fragsize = spec->segsize;
1522 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
1523 GST_INFO_OBJECT (pulsesrc, "tlength: %d", wanted.tlength);
1524 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", wanted.prebuf);
1525 GST_INFO_OBJECT (pulsesrc, "minreq: %d", wanted.minreq);
1526 GST_INFO_OBJECT (pulsesrc, "fragsize: %d", wanted.fragsize);
1528 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
1529 PA_STREAM_NOT_MONOTONIC | PA_STREAM_ADJUST_LATENCY |
1530 PA_STREAM_START_CORKED;
1532 if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
1534 goto connect_failed;
1537 /* our clock will now start from 0 again */
1538 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SRC (pulsesrc)->clock);
1539 gst_audio_clock_reset (clock, 0);
1541 pulsesrc->corked = TRUE;
1544 pa_stream_state_t state;
1546 state = pa_stream_get_state (pulsesrc->stream);
1548 if (!PA_STREAM_IS_GOOD (state))
1551 if (state == PA_STREAM_READY)
1554 /* Wait until the stream is ready */
1555 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1557 pulsesrc->stream_connected = TRUE;
1559 /* store the source output index so it can be accessed via a property */
1560 pulsesrc->source_output_idx = pa_stream_get_index (pulsesrc->stream);
1561 g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
1563 /* Although source output stream muting is supported, there is a bug in
1564 * PulseAudio that doesn't allow us to do this at startup, so we mute
1565 * manually post-connect. This should be moved back pre-connect once things
1566 * are fixed on the PulseAudio side. */
1567 if (pulsesrc->mute_set && pulsesrc->mute) {
1568 gst_pulsesrc_set_stream_mute (pulsesrc, pulsesrc->mute);
1569 pulsesrc->mute_set = FALSE;
1572 if (pulsesrc->volume_set) {
1573 gst_pulsesrc_set_stream_volume (pulsesrc, pulsesrc->volume);
1574 pulsesrc->volume_set = FALSE;
1577 /* get the actual buffering properties now */
1578 actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1580 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1581 GST_INFO_OBJECT (pulsesrc, "tlength: %d (wanted: %d)",
1582 actual->tlength, wanted.tlength);
1583 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", actual->prebuf);
1584 GST_INFO_OBJECT (pulsesrc, "minreq: %d (wanted %d)", actual->minreq,
1586 GST_INFO_OBJECT (pulsesrc, "fragsize: %d (wanted %d)",
1587 actual->fragsize, wanted.fragsize);
1589 if (actual->fragsize >= spec->segsize) {
1590 spec->segsize = actual->fragsize;
1592 /* fragsize is smaller than what we wanted, so let the read function
1593 * coalesce the smaller chunks as they come in */
1596 /* Fix up the total ringbuffer size based on what we actually got */
1597 spec->segtotal = actual->maxlength / spec->segsize;
1598 /* Don't buffer less than 2 segments as the ringbuffer can't deal with it */
1599 if (spec->segtotal < 2)
1602 if (!pulsesrc->paused) {
1603 GST_DEBUG_OBJECT (pulsesrc, "uncorking because we are playing");
1604 gst_pulsesrc_set_corked (pulsesrc, FALSE, FALSE);
1606 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1613 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1614 ("Failed to connect stream: %s",
1615 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1616 goto unlock_and_fail;
1620 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1621 ("Failed to connect stream: %s",
1622 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1623 goto unlock_and_fail;
1627 gst_pulsesrc_destroy_stream (pulsesrc);
1629 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1635 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1637 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1639 pulsesrc->operation_success = ! !success;
1640 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1644 gst_pulsesrc_reset (GstAudioSrc * asrc)
1646 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1647 pa_operation *o = NULL;
1649 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1650 GST_DEBUG_OBJECT (pulsesrc, "reset");
1652 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1653 goto unlock_and_fail;
1656 pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1658 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1659 ("pa_stream_flush() failed: %s",
1660 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1661 goto unlock_and_fail;
1664 pulsesrc->paused = TRUE;
1665 /* Inform anyone waiting in _write() call that it shall wakeup */
1666 if (pulsesrc->in_read) {
1667 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1670 pulsesrc->operation_success = FALSE;
1671 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1673 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1674 goto unlock_and_fail;
1676 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1679 if (!pulsesrc->operation_success) {
1680 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1681 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1682 goto unlock_and_fail;
1688 pa_operation_cancel (o);
1689 pa_operation_unref (o);
1692 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1695 /* update the corked state of a stream, must be called with the mainloop
1698 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1700 pa_operation *o = NULL;
1701 gboolean res = FALSE;
1703 GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1704 if (!psrc->stream_connected)
1707 if (psrc->corked != corked) {
1708 if (!(o = pa_stream_cork (psrc->stream, corked,
1709 gst_pulsesrc_success_cb, psrc)))
1712 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1713 pa_threaded_mainloop_wait (psrc->mainloop);
1714 if (gst_pulsesrc_is_dead (psrc, TRUE))
1717 psrc->corked = corked;
1719 GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1725 pa_operation_unref (o);
1732 GST_DEBUG_OBJECT (psrc, "the server is dead");
1737 GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1738 ("pa_stream_cork() failed: %s",
1739 pa_strerror (pa_context_errno (psrc->context))), (NULL));
1744 /* start/resume playback ASAP */
1746 gst_pulsesrc_play (GstPulseSrc * psrc)
1748 pa_threaded_mainloop_lock (psrc->mainloop);
1749 GST_DEBUG_OBJECT (psrc, "playing");
1750 psrc->paused = FALSE;
1751 gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1752 pa_threaded_mainloop_unlock (psrc->mainloop);
1757 /* pause/stop playback ASAP */
1759 gst_pulsesrc_pause (GstPulseSrc * psrc)
1761 pa_threaded_mainloop_lock (psrc->mainloop);
1762 GST_DEBUG_OBJECT (psrc, "pausing");
1763 /* make sure the commit method stops writing */
1764 psrc->paused = TRUE;
1765 if (psrc->in_read) {
1766 /* we are waiting in a read, signal */
1767 GST_DEBUG_OBJECT (psrc, "signal read");
1768 pa_threaded_mainloop_signal (psrc->mainloop, 0);
1770 pa_threaded_mainloop_unlock (psrc->mainloop);
1775 static GstStateChangeReturn
1776 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1778 GstStateChangeReturn ret;
1779 GstPulseSrc *this = GST_PULSESRC_CAST (element);
1781 switch (transition) {
1782 case GST_STATE_CHANGE_NULL_TO_READY:
1783 if (!(this->mainloop = pa_threaded_mainloop_new ()))
1784 goto mainloop_failed;
1785 if (pa_threaded_mainloop_start (this->mainloop) < 0) {
1786 pa_threaded_mainloop_free (this->mainloop);
1787 this->mainloop = NULL;
1788 goto mainloop_start_failed;
1791 case GST_STATE_CHANGE_READY_TO_PAUSED:
1792 gst_element_post_message (element,
1793 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
1794 GST_AUDIO_BASE_SRC (this)->clock, TRUE));
1796 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1797 /* uncork and start recording */
1798 gst_pulsesrc_play (this);
1800 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1801 /* stop recording ASAP by corking */
1802 pa_threaded_mainloop_lock (this->mainloop);
1803 GST_DEBUG_OBJECT (this, "corking");
1804 gst_pulsesrc_set_corked (this, TRUE, FALSE);
1805 pa_threaded_mainloop_unlock (this->mainloop);
1811 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1813 switch (transition) {
1814 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1815 /* now make sure we get out of the _read method */
1816 gst_pulsesrc_pause (this);
1818 case GST_STATE_CHANGE_READY_TO_NULL:
1820 pa_threaded_mainloop_stop (this->mainloop);
1822 gst_pulsesrc_destroy_context (this);
1824 if (this->mainloop) {
1825 pa_threaded_mainloop_free (this->mainloop);
1826 this->mainloop = NULL;
1829 case GST_STATE_CHANGE_PAUSED_TO_READY:
1830 /* format_lost is reset in release() in baseaudiosink */
1831 gst_element_post_message (element,
1832 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
1833 GST_AUDIO_BASE_SRC (this)->clock));
1844 GST_ELEMENT_ERROR (this, RESOURCE, FAILED,
1845 ("pa_threaded_mainloop_new() failed"), (NULL));
1846 return GST_STATE_CHANGE_FAILURE;
1848 mainloop_start_failed:
1850 GST_ELEMENT_ERROR (this, RESOURCE, FAILED,
1851 ("pa_threaded_mainloop_start() failed"), (NULL));
1852 return GST_STATE_CHANGE_FAILURE;
1857 gst_pulsesrc_get_time (GstClock * clock, GstPulseSrc * src)
1861 if (src->mainloop == NULL)
1864 pa_threaded_mainloop_lock (src->mainloop);
1866 goto unlock_and_out;
1868 if (gst_pulsesrc_is_dead (src, TRUE))
1869 goto unlock_and_out;
1871 if (pa_stream_get_time (src->stream, &time) < 0) {
1872 GST_DEBUG_OBJECT (src, "could not get time");
1873 time = GST_CLOCK_TIME_NONE;
1880 pa_threaded_mainloop_unlock (src->mainloop);