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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
23 * SECTION:element-pulsesrc
24 * @see_also: pulsesink, pulsemixer
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 -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>
47 #include <gst/interfaces/streamvolume.h>
51 #include "pulseutil.h"
52 #include "pulsemixerctrl.h"
54 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
55 #define GST_CAT_DEFAULT pulse_debug
57 #define DEFAULT_SERVER NULL
58 #define DEFAULT_DEVICE NULL
59 #define DEFAULT_DEVICE_NAME NULL
62 #define DEFAULT_VOLUME 1.0
63 #define DEFAULT_MUTE FALSE
64 #define MAX_VOLUME 10.0
74 PROP_STREAM_PROPERTIES,
75 PROP_SOURCE_OUTPUT_INDEX,
83 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
84 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
86 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
87 const GValue * value, GParamSpec * pspec);
88 static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
89 GValue * value, GParamSpec * pspec);
90 static void gst_pulsesrc_finalize (GObject * object);
92 static gboolean gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked,
94 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
96 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
98 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
99 GstAudioRingBufferSpec * spec);
101 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
103 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
105 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
107 static void gst_pulsesrc_reset (GstAudioSrc * src);
109 static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
111 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
112 element, GstStateChange transition);
114 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
115 # define FORMATS "{ S16LE, S16BE, F32LE, F32BE, S32LE, S32BE, U8 }"
117 # define FORMATS "{ S16BE, S16LE, F32BE, F32LE, S32BE, S32LE, U8 }"
120 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
123 GST_STATIC_CAPS ("audio/x-raw, "
124 "format = (string) " FORMATS ", "
125 "rate = (int) [ 1, MAX ], "
126 "channels = (int) [ 1, 32 ];"
128 "rate = (int) [ 1, MAX], "
129 "channels = (int) [ 1, 32 ];"
131 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
135 GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
136 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
138 #define gst_pulsesrc_parent_class parent_class
139 G_DEFINE_TYPE_WITH_CODE (GstPulseSrc, gst_pulsesrc, GST_TYPE_AUDIO_SRC,
140 G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_pulsesrc_mixer_interface_init);
141 G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
142 gst_pulsesrc_property_probe_interface_init);
143 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
146 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
148 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
149 GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
150 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
151 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
154 gobject_class->finalize = gst_pulsesrc_finalize;
155 gobject_class->set_property = gst_pulsesrc_set_property;
156 gobject_class->get_property = gst_pulsesrc_get_property;
158 gstelement_class->change_state =
159 GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
161 gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
163 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
164 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
165 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
166 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
167 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
168 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
169 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
171 /* Overwrite GObject fields */
172 g_object_class_install_property (gobject_class,
174 g_param_spec_string ("server", "Server",
175 "The PulseAudio server to connect to", DEFAULT_SERVER,
176 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
178 g_object_class_install_property (gobject_class, PROP_DEVICE,
179 g_param_spec_string ("device", "Device",
180 "The PulseAudio source device to connect to", DEFAULT_DEVICE,
181 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
183 g_object_class_install_property (gobject_class,
185 g_param_spec_string ("device-name", "Device name",
186 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
187 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
189 clientname = gst_pulse_client_name ();
193 * The PulseAudio client name to use.
197 g_object_class_install_property (gobject_class,
199 g_param_spec_string ("client", "Client",
200 "The PulseAudio client_name_to_use", clientname,
201 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
202 GST_PARAM_MUTABLE_READY));
206 * GstPulseSrc:stream-properties
208 * List of pulseaudio stream properties. A list of defined properties can be
209 * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
211 * Below is an example for registering as a music application to pulseaudio.
213 * GstStructure *props;
215 * props = gst_structure_from_string ("props,media.role=music", NULL);
216 * g_object_set (pulse, "stream-properties", props, NULL);
217 * gst_structure_free (props);
222 g_object_class_install_property (gobject_class,
223 PROP_STREAM_PROPERTIES,
224 g_param_spec_boxed ("stream-properties", "stream properties",
225 "list of pulseaudio stream properties",
226 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
228 * GstPulseSrc:source-output-index
230 * The index of the PulseAudio source output corresponding to this element.
234 g_object_class_install_property (gobject_class,
235 PROP_SOURCE_OUTPUT_INDEX,
236 g_param_spec_uint ("source-output-index", "source output index",
237 "The index of the PulseAudio source output corresponding to this "
238 "record stream", 0, G_MAXUINT, PA_INVALID_INDEX,
239 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
241 gst_element_class_set_details_simple (gstelement_class,
242 "PulseAudio Audio Source",
244 "Captures audio from a PulseAudio server", "Lennart Poettering");
245 gst_element_class_add_pad_template (gstelement_class,
246 gst_static_pad_template_get (&pad_template));
248 #ifdef HAVE_PULSE_1_0
252 * The volume of the record stream. Only works when using PulseAudio 1.0 or
257 g_object_class_install_property (gobject_class,
258 PROP_VOLUME, g_param_spec_double ("volume", "Volume",
259 "Linear volume of this stream, 1.0=100%",
260 0.0, MAX_VOLUME, DEFAULT_VOLUME,
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
266 * Whether the stream is muted or not. Only works when using PulseAudio 1.0
271 g_object_class_install_property (gobject_class,
272 PROP_MUTE, g_param_spec_boolean ("mute", "Mute",
273 "Mute state of this stream",
274 DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
279 gst_pulsesrc_init (GstPulseSrc * pulsesrc)
281 pulsesrc->server = NULL;
282 pulsesrc->device = NULL;
283 pulsesrc->client_name = gst_pulse_client_name ();
284 pulsesrc->device_description = NULL;
286 pulsesrc->context = NULL;
287 pulsesrc->stream = NULL;
288 pulsesrc->stream_connected = FALSE;
289 pulsesrc->source_output_idx = PA_INVALID_INDEX;
291 pulsesrc->read_buffer = NULL;
292 pulsesrc->read_buffer_length = 0;
294 pa_sample_spec_init (&pulsesrc->sample_spec);
296 pulsesrc->operation_success = FALSE;
297 pulsesrc->paused = TRUE;
298 pulsesrc->in_read = FALSE;
300 #ifdef HAVE_PULSE_1_0
301 pulsesrc->volume = DEFAULT_VOLUME;
302 pulsesrc->volume_set = FALSE;
304 pulsesrc->mute = DEFAULT_MUTE;
305 pulsesrc->mute_set = FALSE;
307 pulsesrc->notify = 0;
310 pulsesrc->mixer = NULL;
312 pulsesrc->properties = NULL;
313 pulsesrc->proplist = NULL;
315 pulsesrc->probe = gst_pulseprobe_new (G_OBJECT (pulsesrc), G_OBJECT_GET_CLASS (pulsesrc), PROP_DEVICE, pulsesrc->server, FALSE, TRUE); /* FALSE for sinks, TRUE for sources */
317 /* this should be the default but it isn't yet */
318 gst_audio_base_src_set_slave_method (GST_AUDIO_BASE_SRC (pulsesrc),
319 GST_AUDIO_BASE_SRC_SLAVE_SKEW);
323 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
325 if (pulsesrc->stream) {
326 pa_stream_disconnect (pulsesrc->stream);
327 pa_stream_unref (pulsesrc->stream);
328 pulsesrc->stream = NULL;
329 pulsesrc->stream_connected = FALSE;
330 pulsesrc->source_output_idx = PA_INVALID_INDEX;
331 g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
334 g_free (pulsesrc->device_description);
335 pulsesrc->device_description = NULL;
339 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
342 gst_pulsesrc_destroy_stream (pulsesrc);
344 if (pulsesrc->context) {
345 pa_context_disconnect (pulsesrc->context);
347 /* Make sure we don't get any further callbacks */
348 pa_context_set_state_callback (pulsesrc->context, NULL, NULL);
349 #ifdef HAVE_PULSE_1_0
350 pa_context_set_subscribe_callback (pulsesrc->context, NULL, NULL);
353 pa_context_unref (pulsesrc->context);
355 pulsesrc->context = NULL;
360 gst_pulsesrc_finalize (GObject * object)
362 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
364 g_free (pulsesrc->server);
365 g_free (pulsesrc->device);
366 g_free (pulsesrc->client_name);
368 if (pulsesrc->properties)
369 gst_structure_free (pulsesrc->properties);
370 if (pulsesrc->proplist)
371 pa_proplist_free (pulsesrc->proplist);
373 if (pulsesrc->mixer) {
374 gst_pulsemixer_ctrl_free (pulsesrc->mixer);
375 pulsesrc->mixer = NULL;
378 if (pulsesrc->probe) {
379 gst_pulseprobe_free (pulsesrc->probe);
380 pulsesrc->probe = NULL;
383 G_OBJECT_CLASS (parent_class)->finalize (object);
386 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
387 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
390 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
392 if (!CONTEXT_OK (pulsesrc->context))
395 if (check_stream && !STREAM_OK (pulsesrc->stream))
402 const gchar *err_str = pulsesrc->context ?
403 pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
404 GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
411 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
414 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
419 g_free (pulsesrc->device_description);
420 pulsesrc->device_description = g_strdup (i->description);
423 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
427 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
429 pa_operation *o = NULL;
432 if (!pulsesrc->mainloop)
435 pa_threaded_mainloop_lock (pulsesrc->mainloop);
437 if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
438 pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
440 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
441 ("pa_stream_get_source_info() failed: %s",
442 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
446 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
448 if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
451 pa_threaded_mainloop_wait (pulsesrc->mainloop);
457 pa_operation_unref (o);
459 t = g_strdup (pulsesrc->device_description);
461 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
467 GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
472 #ifdef HAVE_PULSE_1_0
474 gst_pulsesrc_source_output_info_cb (pa_context * c,
475 const pa_source_output_info * i, int eol, void *userdata)
479 psrc = GST_PULSESRC_CAST (userdata);
484 /* If the index doesn't match our current stream,
485 * it implies we just recreated the stream (caps change)
487 if (i->index == psrc->source_output_idx) {
488 psrc->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
489 psrc->mute = i->mute;
493 pa_threaded_mainloop_signal (psrc->mainloop, 0);
497 gst_pulsesrc_get_stream_volume (GstPulseSrc * pulsesrc)
499 pa_operation *o = NULL;
502 if (!pulsesrc->mainloop)
505 if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
508 pa_threaded_mainloop_lock (pulsesrc->mainloop);
510 if (!(o = pa_context_get_source_output_info (pulsesrc->context,
511 pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
515 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
516 pa_threaded_mainloop_wait (pulsesrc->mainloop);
517 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
522 v = pulsesrc->volume;
525 pa_operation_unref (o);
527 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
529 if (v > MAX_VOLUME) {
530 GST_WARNING_OBJECT (pulsesrc, "Clipped volume from %f to %f", v,
540 v = pulsesrc->volume;
541 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
546 v = pulsesrc->volume;
547 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
552 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
553 ("pa_context_get_source_output_info() failed: %s",
554 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
560 gst_pulsesrc_get_stream_mute (GstPulseSrc * pulsesrc)
562 pa_operation *o = NULL;
565 if (!pulsesrc->mainloop)
568 if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
571 pa_threaded_mainloop_lock (pulsesrc->mainloop);
573 if (!(o = pa_context_get_source_output_info (pulsesrc->context,
574 pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
578 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
579 pa_threaded_mainloop_wait (pulsesrc->mainloop);
580 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
585 mute = pulsesrc->mute;
588 pa_operation_unref (o);
590 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
597 mute = pulsesrc->mute;
598 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
603 mute = pulsesrc->mute;
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));
727 gst_pulsesrc_set_property (GObject * object,
728 guint prop_id, const GValue * value, GParamSpec * pspec)
731 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
735 g_free (pulsesrc->server);
736 pulsesrc->server = g_value_dup_string (value);
738 gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
741 g_free (pulsesrc->device);
742 pulsesrc->device = g_value_dup_string (value);
745 g_free (pulsesrc->client_name);
746 if (!g_value_get_string (value)) {
747 GST_WARNING_OBJECT (pulsesrc,
748 "Empty PulseAudio client name not allowed. Resetting to default value");
749 pulsesrc->client_name = gst_pulse_client_name ();
751 pulsesrc->client_name = g_value_dup_string (value);
753 case PROP_STREAM_PROPERTIES:
754 if (pulsesrc->properties)
755 gst_structure_free (pulsesrc->properties);
756 pulsesrc->properties =
757 gst_structure_copy (gst_value_get_structure (value));
758 if (pulsesrc->proplist)
759 pa_proplist_free (pulsesrc->proplist);
760 pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
762 #ifdef HAVE_PULSE_1_0
764 gst_pulsesrc_set_stream_volume (pulsesrc, g_value_get_double (value));
767 gst_pulsesrc_set_stream_mute (pulsesrc, g_value_get_boolean (value));
771 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
777 gst_pulsesrc_get_property (GObject * object,
778 guint prop_id, GValue * value, GParamSpec * pspec)
781 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
785 g_value_set_string (value, pulsesrc->server);
788 g_value_set_string (value, pulsesrc->device);
790 case PROP_DEVICE_NAME:
791 g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
794 g_value_set_string (value, pulsesrc->client_name);
796 case PROP_STREAM_PROPERTIES:
797 gst_value_set_structure (value, pulsesrc->properties);
799 case PROP_SOURCE_OUTPUT_INDEX:
800 g_value_set_uint (value, pulsesrc->source_output_idx);
802 #ifdef HAVE_PULSE_1_0
804 g_value_set_double (value, gst_pulsesrc_get_stream_volume (pulsesrc));
807 g_value_set_boolean (value, gst_pulsesrc_get_stream_mute (pulsesrc));
811 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
817 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
819 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
821 switch (pa_context_get_state (c)) {
822 case PA_CONTEXT_READY:
823 case PA_CONTEXT_TERMINATED:
824 case PA_CONTEXT_FAILED:
825 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
828 case PA_CONTEXT_UNCONNECTED:
829 case PA_CONTEXT_CONNECTING:
830 case PA_CONTEXT_AUTHORIZING:
831 case PA_CONTEXT_SETTING_NAME:
837 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
839 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
841 switch (pa_stream_get_state (s)) {
843 case PA_STREAM_READY:
844 case PA_STREAM_FAILED:
845 case PA_STREAM_TERMINATED:
846 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
849 case PA_STREAM_UNCONNECTED:
850 case PA_STREAM_CREATING:
856 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
858 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
860 GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
862 if (pulsesrc->in_read) {
863 /* only signal when reading */
864 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
869 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
871 const pa_timing_info *info;
872 pa_usec_t source_usec;
874 info = pa_stream_get_timing_info (s);
877 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
878 "latency update (information unknown)");
881 source_usec = info->configured_source_usec;
883 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
884 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
885 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
886 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
887 info->write_index, info->read_index_corrupt, info->read_index,
888 info->source_usec, source_usec);
892 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
894 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
898 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
900 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
903 #ifdef HAVE_PULSE_1_0
905 gst_pulsesrc_context_subscribe_cb (pa_context * c,
906 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
908 GstPulseSrc *psrc = GST_PULSESRC (userdata);
910 if (t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE)
911 && t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_NEW))
914 if (idx != psrc->source_output_idx)
917 /* Actually this event is also triggered when other properties of the stream
918 * change that are unrelated to the volume. However it is probably cheaper to
919 * signal the change here and check for the volume when the GObject property
920 * is read instead of querying it always. */
922 /* inform streaming thread to notify */
923 g_atomic_int_compare_and_exchange (&psrc->notify, 0, 1);
928 gst_pulsesrc_open (GstAudioSrc * asrc)
930 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
932 pa_threaded_mainloop_lock (pulsesrc->mainloop);
934 g_assert (!pulsesrc->context);
935 g_assert (!pulsesrc->stream);
937 GST_DEBUG_OBJECT (pulsesrc, "opening device");
939 if (!(pulsesrc->context =
940 pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
941 pulsesrc->client_name))) {
942 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
944 goto unlock_and_fail;
947 pa_context_set_state_callback (pulsesrc->context,
948 gst_pulsesrc_context_state_cb, pulsesrc);
949 #ifdef HAVE_PULSE_1_0
950 pa_context_set_subscribe_callback (pulsesrc->context,
951 gst_pulsesrc_context_subscribe_cb, pulsesrc);
954 GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
955 GST_STR_NULL (pulsesrc->server));
957 if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
958 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
959 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
960 goto unlock_and_fail;
964 pa_context_state_t state;
966 state = pa_context_get_state (pulsesrc->context);
968 if (!PA_CONTEXT_IS_GOOD (state)) {
969 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
970 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
971 goto unlock_and_fail;
974 if (state == PA_CONTEXT_READY)
977 /* Wait until the context is ready */
978 pa_threaded_mainloop_wait (pulsesrc->mainloop);
980 GST_DEBUG_OBJECT (pulsesrc, "connected");
982 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
989 gst_pulsesrc_destroy_context (pulsesrc);
991 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
998 gst_pulsesrc_close (GstAudioSrc * asrc)
1000 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1002 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1003 gst_pulsesrc_destroy_context (pulsesrc);
1004 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1010 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
1012 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1014 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1015 gst_pulsesrc_destroy_stream (pulsesrc);
1017 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1019 pulsesrc->read_buffer = NULL;
1020 pulsesrc->read_buffer_length = 0;
1026 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
1028 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1031 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1032 pulsesrc->in_read = TRUE;
1034 #ifdef HAVE_PULSE_1_0
1035 if (g_atomic_int_compare_and_exchange (&pulsesrc->notify, 1, 0)) {
1036 g_object_notify (G_OBJECT (pulsesrc), "volume");
1037 g_object_notify (G_OBJECT (pulsesrc), "mute");
1041 if (pulsesrc->paused)
1044 while (length > 0) {
1047 GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
1049 /*check if we have a leftover buffer */
1050 if (!pulsesrc->read_buffer) {
1052 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1053 goto unlock_and_fail;
1055 /* read all available data, we keep a pointer to the data and the length
1056 * and take from it what we need. */
1057 if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
1058 &pulsesrc->read_buffer_length) < 0)
1061 GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
1062 pulsesrc->read_buffer_length);
1064 /* if we have data, process if */
1065 if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
1068 /* now wait for more data to become available */
1069 GST_LOG_OBJECT (pulsesrc, "waiting for data");
1070 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1072 if (pulsesrc->paused)
1077 l = pulsesrc->read_buffer_length >
1078 length ? length : pulsesrc->read_buffer_length;
1080 memcpy (data, pulsesrc->read_buffer, l);
1082 pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
1083 pulsesrc->read_buffer_length -= l;
1085 data = (guint8 *) data + l;
1089 if (pulsesrc->read_buffer_length <= 0) {
1090 /* we copied all of the data, drop it now */
1091 if (pa_stream_drop (pulsesrc->stream) < 0)
1094 /* reset pointer to data */
1095 pulsesrc->read_buffer = NULL;
1096 pulsesrc->read_buffer_length = 0;
1100 pulsesrc->in_read = FALSE;
1101 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1108 GST_LOG_OBJECT (pulsesrc, "we are paused");
1109 goto unlock_and_fail;
1113 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1114 ("pa_stream_peek() failed: %s",
1115 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1116 goto unlock_and_fail;
1120 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1121 ("pa_stream_drop() failed: %s",
1122 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1123 goto unlock_and_fail;
1127 pulsesrc->in_read = FALSE;
1128 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1134 /* return the delay in samples */
1136 gst_pulsesrc_delay (GstAudioSrc * asrc)
1138 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1143 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1144 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1147 /* get the latency, this can fail when we don't have a latency update yet.
1148 * We don't want to wait for latency updates here but we just return 0. */
1149 res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
1151 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1154 GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
1160 result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
1167 GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
1168 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1174 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
1176 pa_channel_map channel_map;
1178 gboolean need_channel_layout = FALSE;
1179 GstAudioRingBufferSpec spec;
1182 memset (&spec, 0, sizeof (GstAudioRingBufferSpec));
1183 spec.latency_time = GST_SECOND;
1184 if (!gst_audio_ring_buffer_parse_caps (&spec, caps))
1187 /* Keep the refcount of the caps at 1 to make them writable */
1188 gst_caps_unref (spec.caps);
1190 if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec))
1193 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1195 if (!pulsesrc->context)
1198 s = gst_caps_get_structure (caps, 0);
1199 if (!gst_structure_has_field (s, "channel-layout") ||
1200 !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
1201 if (spec.info.channels == 1)
1202 pa_channel_map_init_mono (&channel_map);
1203 else if (spec.info.channels == 2)
1204 pa_channel_map_init_stereo (&channel_map);
1206 need_channel_layout = TRUE;
1209 name = "Record Stream";
1210 if (pulsesrc->proplist) {
1211 if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
1212 name, &pulsesrc->sample_spec,
1213 (need_channel_layout) ? NULL : &channel_map,
1214 pulsesrc->proplist)))
1217 } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
1218 name, &pulsesrc->sample_spec,
1219 (need_channel_layout) ? NULL : &channel_map)))
1222 if (need_channel_layout) {
1223 const pa_channel_map *m = pa_stream_get_channel_map (pulsesrc->stream);
1225 gst_pulse_channel_map_to_gst (m, &spec);
1229 GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, caps);
1231 pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
1233 pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
1235 pa_stream_set_underflow_callback (pulsesrc->stream,
1236 gst_pulsesrc_stream_underflow_cb, pulsesrc);
1237 pa_stream_set_overflow_callback (pulsesrc->stream,
1238 gst_pulsesrc_stream_overflow_cb, pulsesrc);
1239 pa_stream_set_latency_update_callback (pulsesrc->stream,
1240 gst_pulsesrc_stream_latency_update_cb, pulsesrc);
1242 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1249 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1250 ("Can't parse caps."), (NULL));
1255 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1256 ("Invalid sample specification."), (NULL));
1261 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
1262 goto unlock_and_fail;
1266 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1267 ("Failed to create stream: %s",
1268 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1269 goto unlock_and_fail;
1273 gst_pulsesrc_destroy_stream (pulsesrc);
1275 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1282 /* This is essentially gst_base_src_negotiate_default() but the caps
1283 * are guaranteed to have a channel layout for > 2 channels
1286 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
1288 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (basesrc);
1290 GstCaps *caps = NULL;
1291 GstCaps *peercaps = NULL;
1292 gboolean result = FALSE;
1294 /* first see what is possible on our source pad */
1295 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1296 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1297 /* nothing or anything is allowed, we're done */
1298 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1299 goto no_nego_needed;
1301 /* get the peer caps */
1302 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1303 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1305 /* get intersection */
1306 caps = gst_caps_intersect (thiscaps, peercaps);
1307 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
1308 gst_caps_unref (thiscaps);
1309 gst_caps_unref (peercaps);
1311 /* no peer, work with our own caps then */
1315 /* take first (and best, since they are sorted) possibility */
1316 caps = gst_caps_make_writable (caps);
1317 gst_caps_truncate (caps);
1320 if (!gst_caps_is_empty (caps)) {
1321 GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
1322 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1324 if (gst_caps_is_any (caps)) {
1325 /* hmm, still anything, so element can do anything and
1326 * nego is not needed */
1328 } else if (gst_caps_is_fixed (caps)) {
1329 /* yay, fixed caps, use those then */
1330 result = gst_pulsesrc_create_stream (pulsesrc, caps);
1332 result = gst_base_src_set_caps (basesrc, caps);
1335 gst_caps_unref (caps);
1341 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1343 gst_caps_unref (thiscaps);
1349 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
1351 pa_buffer_attr wanted;
1352 const pa_buffer_attr *actual;
1353 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1354 pa_stream_flags_t flags;
1355 #ifdef HAVE_PULSE_1_0
1359 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1361 #ifdef HAVE_PULSE_1_0
1362 /* enable event notifications */
1363 GST_LOG_OBJECT (pulsesrc, "subscribing to context events");
1364 if (!(o = pa_context_subscribe (pulsesrc->context,
1365 PA_SUBSCRIPTION_MASK_SINK_INPUT, NULL, NULL))) {
1366 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1367 ("pa_context_subscribe() failed: %s",
1368 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1369 goto unlock_and_fail;
1372 pa_operation_unref (o);
1375 wanted.maxlength = -1;
1376 wanted.tlength = -1;
1379 wanted.fragsize = spec->segsize;
1381 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
1382 GST_INFO_OBJECT (pulsesrc, "tlength: %d", wanted.tlength);
1383 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", wanted.prebuf);
1384 GST_INFO_OBJECT (pulsesrc, "minreq: %d", wanted.minreq);
1385 GST_INFO_OBJECT (pulsesrc, "fragsize: %d", wanted.fragsize);
1387 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
1388 PA_STREAM_NOT_MONOTONIC | PA_STREAM_ADJUST_LATENCY |
1389 PA_STREAM_START_CORKED;
1391 #ifdef HAVE_PULSE_1_0
1392 if (pulsesrc->mute_set && pulsesrc->mute)
1393 flags |= PA_STREAM_START_MUTED;
1396 if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
1398 goto connect_failed;
1401 pulsesrc->corked = TRUE;
1404 pa_stream_state_t state;
1406 state = pa_stream_get_state (pulsesrc->stream);
1408 if (!PA_STREAM_IS_GOOD (state))
1411 if (state == PA_STREAM_READY)
1414 /* Wait until the stream is ready */
1415 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1417 pulsesrc->stream_connected = TRUE;
1419 /* store the source output index so it can be accessed via a property */
1420 pulsesrc->source_output_idx = pa_stream_get_index (pulsesrc->stream);
1421 g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
1423 #ifdef HAVE_PULSE_1_0
1424 if (pulsesrc->volume_set) {
1425 gst_pulsesrc_set_stream_volume (pulsesrc, pulsesrc->volume);
1426 pulsesrc->volume_set = FALSE;
1430 /* get the actual buffering properties now */
1431 actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1433 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1434 GST_INFO_OBJECT (pulsesrc, "tlength: %d (wanted: %d)",
1435 actual->tlength, wanted.tlength);
1436 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", actual->prebuf);
1437 GST_INFO_OBJECT (pulsesrc, "minreq: %d (wanted %d)", actual->minreq,
1439 GST_INFO_OBJECT (pulsesrc, "fragsize: %d (wanted %d)",
1440 actual->fragsize, wanted.fragsize);
1442 if (actual->fragsize >= wanted.fragsize) {
1443 spec->segsize = actual->fragsize;
1445 spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
1447 spec->segtotal = actual->maxlength / spec->segsize;
1449 if (!pulsesrc->paused) {
1450 GST_DEBUG_OBJECT (pulsesrc, "uncorking because we are playing");
1451 gst_pulsesrc_set_corked (pulsesrc, FALSE, FALSE);
1453 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1460 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1461 ("Failed to connect stream: %s",
1462 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1463 goto unlock_and_fail;
1467 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1468 ("Failed to connect stream: %s",
1469 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1470 goto unlock_and_fail;
1474 gst_pulsesrc_destroy_stream (pulsesrc);
1476 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1482 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1484 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1486 pulsesrc->operation_success = ! !success;
1487 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1491 gst_pulsesrc_reset (GstAudioSrc * asrc)
1493 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1494 pa_operation *o = NULL;
1496 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1497 GST_DEBUG_OBJECT (pulsesrc, "reset");
1499 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1500 goto unlock_and_fail;
1503 pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1505 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1506 ("pa_stream_flush() failed: %s",
1507 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1508 goto unlock_and_fail;
1511 pulsesrc->paused = TRUE;
1512 /* Inform anyone waiting in _write() call that it shall wakeup */
1513 if (pulsesrc->in_read) {
1514 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1517 pulsesrc->operation_success = FALSE;
1518 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1520 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1521 goto unlock_and_fail;
1523 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1526 if (!pulsesrc->operation_success) {
1527 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1528 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1529 goto unlock_and_fail;
1535 pa_operation_cancel (o);
1536 pa_operation_unref (o);
1539 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1542 /* update the corked state of a stream, must be called with the mainloop
1545 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1547 pa_operation *o = NULL;
1548 gboolean res = FALSE;
1550 GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1551 if (!psrc->stream_connected)
1554 if (psrc->corked != corked) {
1555 if (!(o = pa_stream_cork (psrc->stream, corked,
1556 gst_pulsesrc_success_cb, psrc)))
1559 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1560 pa_threaded_mainloop_wait (psrc->mainloop);
1561 if (gst_pulsesrc_is_dead (psrc, TRUE))
1564 psrc->corked = corked;
1566 GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1572 pa_operation_unref (o);
1579 GST_DEBUG_OBJECT (psrc, "the server is dead");
1584 GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1585 ("pa_stream_cork() failed: %s",
1586 pa_strerror (pa_context_errno (psrc->context))), (NULL));
1591 /* start/resume playback ASAP */
1593 gst_pulsesrc_play (GstPulseSrc * psrc)
1595 pa_threaded_mainloop_lock (psrc->mainloop);
1596 GST_DEBUG_OBJECT (psrc, "playing");
1597 psrc->paused = FALSE;
1598 gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1599 pa_threaded_mainloop_unlock (psrc->mainloop);
1604 /* pause/stop playback ASAP */
1606 gst_pulsesrc_pause (GstPulseSrc * psrc)
1608 pa_threaded_mainloop_lock (psrc->mainloop);
1609 GST_DEBUG_OBJECT (psrc, "pausing");
1610 /* make sure the commit method stops writing */
1611 psrc->paused = TRUE;
1612 if (psrc->in_read) {
1613 /* we are waiting in a read, signal */
1614 GST_DEBUG_OBJECT (psrc, "signal read");
1615 pa_threaded_mainloop_signal (psrc->mainloop, 0);
1617 pa_threaded_mainloop_unlock (psrc->mainloop);
1622 static GstStateChangeReturn
1623 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1625 GstStateChangeReturn ret;
1626 GstPulseSrc *this = GST_PULSESRC_CAST (element);
1628 switch (transition) {
1629 case GST_STATE_CHANGE_NULL_TO_READY:
1630 this->mainloop = pa_threaded_mainloop_new ();
1631 g_assert (this->mainloop);
1633 pa_threaded_mainloop_start (this->mainloop);
1637 gst_pulsemixer_ctrl_new (G_OBJECT (this), this->server,
1638 this->device, GST_PULSEMIXER_SOURCE);
1640 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1641 /* uncork and start recording */
1642 gst_pulsesrc_play (this);
1644 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1645 /* stop recording ASAP by corking */
1646 pa_threaded_mainloop_lock (this->mainloop);
1647 GST_DEBUG_OBJECT (this, "corking");
1648 gst_pulsesrc_set_corked (this, TRUE, FALSE);
1649 pa_threaded_mainloop_unlock (this->mainloop);
1655 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1657 switch (transition) {
1658 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1659 /* now make sure we get out of the _read method */
1660 gst_pulsesrc_pause (this);
1662 case GST_STATE_CHANGE_READY_TO_NULL:
1664 gst_pulsemixer_ctrl_free (this->mixer);
1669 pa_threaded_mainloop_stop (this->mainloop);
1671 gst_pulsesrc_destroy_context (this);
1673 if (this->mainloop) {
1674 pa_threaded_mainloop_free (this->mainloop);
1675 this->mainloop = NULL;