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
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>
46 #include <gst/audio/streamvolume.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_DEVICE_NAME NULL
58 #define DEFAULT_VOLUME 1.0
59 #define DEFAULT_MUTE FALSE
60 #define MAX_VOLUME 10.0
69 PROP_STREAM_PROPERTIES,
70 PROP_SOURCE_OUTPUT_INDEX,
76 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
77 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
79 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
80 const GValue * value, GParamSpec * pspec);
81 static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
82 GValue * value, GParamSpec * pspec);
83 static void gst_pulsesrc_finalize (GObject * object);
85 static gboolean gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked,
87 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
89 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
91 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
92 GstAudioRingBufferSpec * spec);
94 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
96 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
98 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
100 static void gst_pulsesrc_reset (GstAudioSrc * src);
102 static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
104 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
105 element, GstStateChange transition);
107 static GstClockTime gst_pulsesrc_get_time (GstClock * clock, GstPulseSrc * src);
109 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
110 # define FORMATS "{ S16LE, S16BE, F32LE, F32BE, S32LE, S32BE, U8 }"
112 # define FORMATS "{ S16BE, S16LE, F32BE, F32LE, S32BE, S32LE, U8 }"
115 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
118 GST_STATIC_CAPS ("audio/x-raw, "
119 "format = (string) " FORMATS ", "
120 "layout = (string) interleaved, "
121 "rate = (int) [ 1, MAX ], "
122 "channels = (int) [ 1, 32 ];"
124 "rate = (int) [ 1, MAX], "
125 "channels = (int) [ 1, 32 ];"
127 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
131 #define gst_pulsesrc_parent_class parent_class
132 G_DEFINE_TYPE_WITH_CODE (GstPulseSrc, gst_pulsesrc, GST_TYPE_AUDIO_SRC,
133 G_IMPLEMENT_INTERFACE (GST_TYPE_STREAM_VOLUME, NULL));
136 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
138 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
139 GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
140 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
141 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
144 gobject_class->finalize = gst_pulsesrc_finalize;
145 gobject_class->set_property = gst_pulsesrc_set_property;
146 gobject_class->get_property = gst_pulsesrc_get_property;
148 gstelement_class->change_state =
149 GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
151 gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
153 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
154 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
155 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
156 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
157 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
158 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
159 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
161 /* Overwrite GObject fields */
162 g_object_class_install_property (gobject_class,
164 g_param_spec_string ("server", "Server",
165 "The PulseAudio server to connect to", DEFAULT_SERVER,
166 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
168 g_object_class_install_property (gobject_class, PROP_DEVICE,
169 g_param_spec_string ("device", "Device",
170 "The PulseAudio source device to connect to", DEFAULT_DEVICE,
171 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173 g_object_class_install_property (gobject_class,
175 g_param_spec_string ("device-name", "Device name",
176 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
177 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
179 clientname = gst_pulse_client_name ();
181 * GstPulseSrc:client-name
183 * The PulseAudio client name to use.
185 g_object_class_install_property (gobject_class,
187 g_param_spec_string ("client-name", "Client Name",
188 "The PulseAudio client_name_to_use", clientname,
189 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
190 GST_PARAM_MUTABLE_READY));
194 * GstPulseSrc:stream-properties
196 * List of pulseaudio stream properties. A list of defined properties can be
197 * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
199 * Below is an example for registering as a music application to pulseaudio.
201 * GstStructure *props;
203 * props = gst_structure_from_string ("props,media.role=music", NULL);
204 * g_object_set (pulse, "stream-properties", props, NULL);
205 * gst_structure_free (props);
210 g_object_class_install_property (gobject_class,
211 PROP_STREAM_PROPERTIES,
212 g_param_spec_boxed ("stream-properties", "stream properties",
213 "list of pulseaudio stream properties",
214 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
216 * GstPulseSrc:source-output-index
218 * The index of the PulseAudio source output corresponding to this element.
222 g_object_class_install_property (gobject_class,
223 PROP_SOURCE_OUTPUT_INDEX,
224 g_param_spec_uint ("source-output-index", "source output index",
225 "The index of the PulseAudio source output corresponding to this "
226 "record stream", 0, G_MAXUINT, PA_INVALID_INDEX,
227 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
229 gst_element_class_set_static_metadata (gstelement_class,
230 "PulseAudio Audio Source",
232 "Captures audio from a PulseAudio server", "Lennart Poettering");
233 gst_element_class_add_pad_template (gstelement_class,
234 gst_static_pad_template_get (&pad_template));
239 * The volume of the record stream.
241 g_object_class_install_property (gobject_class,
242 PROP_VOLUME, g_param_spec_double ("volume", "Volume",
243 "Linear volume of this stream, 1.0=100%",
244 0.0, MAX_VOLUME, DEFAULT_VOLUME,
245 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
250 * Whether the stream is muted or not.
252 g_object_class_install_property (gobject_class,
253 PROP_MUTE, g_param_spec_boolean ("mute", "Mute",
254 "Mute state of this stream",
255 DEFAULT_MUTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
259 gst_pulsesrc_init (GstPulseSrc * pulsesrc)
261 pulsesrc->server = NULL;
262 pulsesrc->device = NULL;
263 pulsesrc->client_name = gst_pulse_client_name ();
264 pulsesrc->device_description = NULL;
266 pulsesrc->context = NULL;
267 pulsesrc->stream = NULL;
268 pulsesrc->stream_connected = FALSE;
269 pulsesrc->source_output_idx = PA_INVALID_INDEX;
271 pulsesrc->read_buffer = NULL;
272 pulsesrc->read_buffer_length = 0;
274 pa_sample_spec_init (&pulsesrc->sample_spec);
276 pulsesrc->operation_success = FALSE;
277 pulsesrc->paused = TRUE;
278 pulsesrc->in_read = FALSE;
280 pulsesrc->volume = DEFAULT_VOLUME;
281 pulsesrc->volume_set = FALSE;
283 pulsesrc->mute = DEFAULT_MUTE;
284 pulsesrc->mute_set = FALSE;
286 pulsesrc->notify = 0;
288 pulsesrc->properties = NULL;
289 pulsesrc->proplist = NULL;
291 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 */
293 /* this should be the default but it isn't yet */
294 gst_audio_base_src_set_slave_method (GST_AUDIO_BASE_SRC (pulsesrc),
295 GST_AUDIO_BASE_SRC_SLAVE_SKEW);
297 /* override with a custom clock */
298 if (GST_AUDIO_BASE_SRC (pulsesrc)->clock)
299 gst_object_unref (GST_AUDIO_BASE_SRC (pulsesrc)->clock);
301 GST_AUDIO_BASE_SRC (pulsesrc)->clock =
302 gst_audio_clock_new ("GstPulseSrcClock",
303 (GstAudioClockGetTimeFunc) gst_pulsesrc_get_time, pulsesrc, NULL);
307 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
309 if (pulsesrc->stream) {
310 pa_stream_disconnect (pulsesrc->stream);
311 pa_stream_unref (pulsesrc->stream);
312 pulsesrc->stream = NULL;
313 pulsesrc->stream_connected = FALSE;
314 pulsesrc->source_output_idx = PA_INVALID_INDEX;
315 g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
318 g_free (pulsesrc->device_description);
319 pulsesrc->device_description = NULL;
323 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
326 gst_pulsesrc_destroy_stream (pulsesrc);
328 if (pulsesrc->context) {
329 pa_context_disconnect (pulsesrc->context);
331 /* Make sure we don't get any further callbacks */
332 pa_context_set_state_callback (pulsesrc->context, NULL, NULL);
333 pa_context_set_subscribe_callback (pulsesrc->context, NULL, NULL);
335 pa_context_unref (pulsesrc->context);
337 pulsesrc->context = NULL;
342 gst_pulsesrc_finalize (GObject * object)
344 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
346 g_free (pulsesrc->server);
347 g_free (pulsesrc->device);
348 g_free (pulsesrc->client_name);
350 if (pulsesrc->properties)
351 gst_structure_free (pulsesrc->properties);
352 if (pulsesrc->proplist)
353 pa_proplist_free (pulsesrc->proplist);
355 if (pulsesrc->probe) {
356 gst_pulseprobe_free (pulsesrc->probe);
357 pulsesrc->probe = NULL;
360 G_OBJECT_CLASS (parent_class)->finalize (object);
363 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
364 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
367 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
369 if (!CONTEXT_OK (pulsesrc->context))
372 if (check_stream && !STREAM_OK (pulsesrc->stream))
379 const gchar *err_str = pulsesrc->context ?
380 pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
381 GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
388 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
391 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
396 g_free (pulsesrc->device_description);
397 pulsesrc->device_description = g_strdup (i->description);
400 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
404 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
406 pa_operation *o = NULL;
409 if (!pulsesrc->mainloop)
412 pa_threaded_mainloop_lock (pulsesrc->mainloop);
414 if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
415 pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
417 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
418 ("pa_stream_get_source_info() failed: %s",
419 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
423 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
425 if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
428 pa_threaded_mainloop_wait (pulsesrc->mainloop);
434 pa_operation_unref (o);
436 t = g_strdup (pulsesrc->device_description);
438 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
444 GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
450 gst_pulsesrc_source_output_info_cb (pa_context * c,
451 const pa_source_output_info * i, int eol, void *userdata)
455 psrc = GST_PULSESRC_CAST (userdata);
460 /* If the index doesn't match our current stream,
461 * it implies we just recreated the stream (caps change)
463 if (i->index == psrc->source_output_idx) {
464 psrc->volume = pa_sw_volume_to_linear (pa_cvolume_max (&i->volume));
465 psrc->mute = i->mute;
469 pa_threaded_mainloop_signal (psrc->mainloop, 0);
473 gst_pulsesrc_get_stream_volume (GstPulseSrc * pulsesrc)
475 pa_operation *o = NULL;
478 if (!pulsesrc->mainloop)
481 if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
484 pa_threaded_mainloop_lock (pulsesrc->mainloop);
486 if (!(o = pa_context_get_source_output_info (pulsesrc->context,
487 pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
491 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
492 pa_threaded_mainloop_wait (pulsesrc->mainloop);
493 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
498 v = pulsesrc->volume;
501 pa_operation_unref (o);
503 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
505 if (v > MAX_VOLUME) {
506 GST_WARNING_OBJECT (pulsesrc, "Clipped volume from %f to %f", v,
516 v = pulsesrc->volume;
517 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
522 v = pulsesrc->volume;
523 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
528 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
529 ("pa_context_get_source_output_info() failed: %s",
530 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
536 gst_pulsesrc_get_stream_mute (GstPulseSrc * pulsesrc)
538 pa_operation *o = NULL;
541 if (!pulsesrc->mainloop)
544 if (pulsesrc->source_output_idx == PA_INVALID_INDEX)
547 pa_threaded_mainloop_lock (pulsesrc->mainloop);
549 if (!(o = pa_context_get_source_output_info (pulsesrc->context,
550 pulsesrc->source_output_idx, gst_pulsesrc_source_output_info_cb,
554 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
555 pa_threaded_mainloop_wait (pulsesrc->mainloop);
556 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
561 mute = pulsesrc->mute;
564 pa_operation_unref (o);
566 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
573 mute = pulsesrc->mute;
574 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
579 mute = pulsesrc->mute;
580 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
585 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
586 ("pa_context_get_source_output_info() failed: %s",
587 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
593 gst_pulsesrc_set_stream_volume (GstPulseSrc * pulsesrc, gdouble volume)
596 pa_operation *o = NULL;
598 if (!pulsesrc->mainloop)
601 if (!pulsesrc->source_output_idx)
604 pa_threaded_mainloop_lock (pulsesrc->mainloop);
606 GST_DEBUG_OBJECT (pulsesrc, "setting volume to %f", volume);
608 gst_pulse_cvolume_from_linear (&v, pulsesrc->sample_spec.channels, volume);
610 if (!(o = pa_context_set_source_output_volume (pulsesrc->context,
611 pulsesrc->source_output_idx, &v, NULL, NULL)))
614 /* We don't really care about the result of this call */
618 pa_operation_unref (o);
620 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
627 pulsesrc->volume = volume;
628 pulsesrc->volume_set = TRUE;
629 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
634 pulsesrc->volume = volume;
635 pulsesrc->volume_set = TRUE;
636 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
641 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
642 ("pa_stream_set_source_output_volume() failed: %s",
643 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
649 gst_pulsesrc_set_stream_mute (GstPulseSrc * pulsesrc, gboolean mute)
651 pa_operation *o = NULL;
653 if (!pulsesrc->mainloop)
656 if (!pulsesrc->source_output_idx)
659 pa_threaded_mainloop_lock (pulsesrc->mainloop);
661 GST_DEBUG_OBJECT (pulsesrc, "setting mute state to %d", mute);
663 if (!(o = pa_context_set_source_output_mute (pulsesrc->context,
664 pulsesrc->source_output_idx, mute, NULL, NULL)))
667 /* We don't really care about the result of this call */
671 pa_operation_unref (o);
673 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
680 pulsesrc->mute = mute;
681 pulsesrc->mute_set = TRUE;
682 GST_DEBUG_OBJECT (pulsesrc, "we have no mainloop");
687 pulsesrc->mute = mute;
688 pulsesrc->mute_set = TRUE;
689 GST_DEBUG_OBJECT (pulsesrc, "we don't have a stream index");
694 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
695 ("pa_stream_set_source_output_mute() failed: %s",
696 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
702 gst_pulsesrc_set_property (GObject * object,
703 guint prop_id, const GValue * value, GParamSpec * pspec)
706 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
710 g_free (pulsesrc->server);
711 pulsesrc->server = g_value_dup_string (value);
713 gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
716 g_free (pulsesrc->device);
717 pulsesrc->device = g_value_dup_string (value);
719 case PROP_CLIENT_NAME:
720 g_free (pulsesrc->client_name);
721 if (!g_value_get_string (value)) {
722 GST_WARNING_OBJECT (pulsesrc,
723 "Empty PulseAudio client name not allowed. Resetting to default value");
724 pulsesrc->client_name = gst_pulse_client_name ();
726 pulsesrc->client_name = g_value_dup_string (value);
728 case PROP_STREAM_PROPERTIES:
729 if (pulsesrc->properties)
730 gst_structure_free (pulsesrc->properties);
731 pulsesrc->properties =
732 gst_structure_copy (gst_value_get_structure (value));
733 if (pulsesrc->proplist)
734 pa_proplist_free (pulsesrc->proplist);
735 pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
738 gst_pulsesrc_set_stream_volume (pulsesrc, g_value_get_double (value));
741 gst_pulsesrc_set_stream_mute (pulsesrc, g_value_get_boolean (value));
744 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
750 gst_pulsesrc_get_property (GObject * object,
751 guint prop_id, GValue * value, GParamSpec * pspec)
754 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
758 g_value_set_string (value, pulsesrc->server);
761 g_value_set_string (value, pulsesrc->device);
763 case PROP_DEVICE_NAME:
764 g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
766 case PROP_CLIENT_NAME:
767 g_value_set_string (value, pulsesrc->client_name);
769 case PROP_STREAM_PROPERTIES:
770 gst_value_set_structure (value, pulsesrc->properties);
772 case PROP_SOURCE_OUTPUT_INDEX:
773 g_value_set_uint (value, pulsesrc->source_output_idx);
776 g_value_set_double (value, gst_pulsesrc_get_stream_volume (pulsesrc));
779 g_value_set_boolean (value, gst_pulsesrc_get_stream_mute (pulsesrc));
782 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
788 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
790 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
792 switch (pa_context_get_state (c)) {
793 case PA_CONTEXT_READY:
794 case PA_CONTEXT_TERMINATED:
795 case PA_CONTEXT_FAILED:
796 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
799 case PA_CONTEXT_UNCONNECTED:
800 case PA_CONTEXT_CONNECTING:
801 case PA_CONTEXT_AUTHORIZING:
802 case PA_CONTEXT_SETTING_NAME:
808 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
810 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
812 switch (pa_stream_get_state (s)) {
814 case PA_STREAM_READY:
815 case PA_STREAM_FAILED:
816 case PA_STREAM_TERMINATED:
817 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
820 case PA_STREAM_UNCONNECTED:
821 case PA_STREAM_CREATING:
827 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
829 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
831 GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
833 if (pulsesrc->in_read) {
834 /* only signal when reading */
835 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
840 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
842 const pa_timing_info *info;
843 pa_usec_t source_usec;
845 info = pa_stream_get_timing_info (s);
848 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
849 "latency update (information unknown)");
852 source_usec = info->configured_source_usec;
854 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
855 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
856 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
857 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
858 info->write_index, info->read_index_corrupt, info->read_index,
859 info->source_usec, source_usec);
863 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
865 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
869 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
871 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
875 gst_pulsesrc_context_subscribe_cb (pa_context * c,
876 pa_subscription_event_type_t t, uint32_t idx, void *userdata)
878 GstPulseSrc *psrc = GST_PULSESRC (userdata);
880 if (t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_CHANGE)
881 && t != (PA_SUBSCRIPTION_EVENT_SOURCE_OUTPUT | PA_SUBSCRIPTION_EVENT_NEW))
884 if (idx != psrc->source_output_idx)
887 /* Actually this event is also triggered when other properties of the stream
888 * change that are unrelated to the volume. However it is probably cheaper to
889 * signal the change here and check for the volume when the GObject property
890 * is read instead of querying it always. */
892 /* inform streaming thread to notify */
893 g_atomic_int_compare_and_exchange (&psrc->notify, 0, 1);
897 gst_pulsesrc_open (GstAudioSrc * asrc)
899 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
901 pa_threaded_mainloop_lock (pulsesrc->mainloop);
903 g_assert (!pulsesrc->context);
904 g_assert (!pulsesrc->stream);
906 GST_DEBUG_OBJECT (pulsesrc, "opening device");
908 if (!(pulsesrc->context =
909 pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
910 pulsesrc->client_name))) {
911 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
913 goto unlock_and_fail;
916 pa_context_set_state_callback (pulsesrc->context,
917 gst_pulsesrc_context_state_cb, pulsesrc);
918 pa_context_set_subscribe_callback (pulsesrc->context,
919 gst_pulsesrc_context_subscribe_cb, pulsesrc);
921 GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
922 GST_STR_NULL (pulsesrc->server));
924 if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
925 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
926 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
927 goto unlock_and_fail;
931 pa_context_state_t state;
933 state = pa_context_get_state (pulsesrc->context);
935 if (!PA_CONTEXT_IS_GOOD (state)) {
936 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
937 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
938 goto unlock_and_fail;
941 if (state == PA_CONTEXT_READY)
944 /* Wait until the context is ready */
945 pa_threaded_mainloop_wait (pulsesrc->mainloop);
947 GST_DEBUG_OBJECT (pulsesrc, "connected");
949 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
956 gst_pulsesrc_destroy_context (pulsesrc);
958 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
965 gst_pulsesrc_close (GstAudioSrc * asrc)
967 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
969 pa_threaded_mainloop_lock (pulsesrc->mainloop);
970 gst_pulsesrc_destroy_context (pulsesrc);
971 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
977 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
979 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
981 pa_threaded_mainloop_lock (pulsesrc->mainloop);
982 gst_pulsesrc_destroy_stream (pulsesrc);
984 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
986 pulsesrc->read_buffer = NULL;
987 pulsesrc->read_buffer_length = 0;
993 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
995 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
998 if (g_atomic_int_compare_and_exchange (&pulsesrc->notify, 1, 0)) {
999 g_object_notify (G_OBJECT (pulsesrc), "volume");
1000 g_object_notify (G_OBJECT (pulsesrc), "mute");
1003 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1004 pulsesrc->in_read = TRUE;
1006 if (pulsesrc->paused)
1009 while (length > 0) {
1012 GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
1014 /*check if we have a leftover buffer */
1015 if (!pulsesrc->read_buffer) {
1017 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1018 goto unlock_and_fail;
1020 /* read all available data, we keep a pointer to the data and the length
1021 * and take from it what we need. */
1022 if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
1023 &pulsesrc->read_buffer_length) < 0)
1026 GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
1027 pulsesrc->read_buffer_length);
1029 /* if we have data, process if */
1030 if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
1033 /* now wait for more data to become available */
1034 GST_LOG_OBJECT (pulsesrc, "waiting for data");
1035 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1037 if (pulsesrc->paused)
1042 l = pulsesrc->read_buffer_length >
1043 length ? length : pulsesrc->read_buffer_length;
1045 memcpy (data, pulsesrc->read_buffer, l);
1047 pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
1048 pulsesrc->read_buffer_length -= l;
1050 data = (guint8 *) data + l;
1054 if (pulsesrc->read_buffer_length <= 0) {
1055 /* we copied all of the data, drop it now */
1056 if (pa_stream_drop (pulsesrc->stream) < 0)
1059 /* reset pointer to data */
1060 pulsesrc->read_buffer = NULL;
1061 pulsesrc->read_buffer_length = 0;
1065 pulsesrc->in_read = FALSE;
1066 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1073 GST_LOG_OBJECT (pulsesrc, "we are paused");
1074 goto unlock_and_fail;
1078 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1079 ("pa_stream_peek() failed: %s",
1080 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1081 goto unlock_and_fail;
1085 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1086 ("pa_stream_drop() failed: %s",
1087 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1088 goto unlock_and_fail;
1092 pulsesrc->in_read = FALSE;
1093 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1099 /* return the delay in samples */
1101 gst_pulsesrc_delay (GstAudioSrc * asrc)
1103 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1108 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1109 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1112 /* get the latency, this can fail when we don't have a latency update yet.
1113 * We don't want to wait for latency updates here but we just return 0. */
1114 res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
1116 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1119 GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
1125 result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
1132 GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
1133 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1139 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps ** caps)
1141 pa_channel_map channel_map;
1142 const pa_channel_map *m;
1144 gboolean need_channel_layout = FALSE;
1145 GstAudioRingBufferSpec spec;
1149 s = gst_caps_get_structure (*caps, 0);
1150 gst_structure_get_int (s, "channels", &spec.info.channels);
1151 if (!gst_structure_has_field (s, "channel-mask")) {
1152 if (spec.info.channels == 1) {
1153 pa_channel_map_init_mono (&channel_map);
1154 } else if (spec.info.channels == 2) {
1155 gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
1156 GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_LEFT) |
1157 GST_AUDIO_CHANNEL_POSITION_MASK (FRONT_RIGHT), NULL);
1158 pa_channel_map_init_stereo (&channel_map);
1160 need_channel_layout = TRUE;
1161 gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
1162 G_GUINT64_CONSTANT (0), NULL);
1166 memset (&spec, 0, sizeof (GstAudioRingBufferSpec));
1167 spec.latency_time = GST_SECOND;
1168 if (!gst_audio_ring_buffer_parse_caps (&spec, *caps))
1171 /* Keep the refcount of the caps at 1 to make them writable */
1172 gst_caps_unref (spec.caps);
1174 if (!need_channel_layout
1175 && !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
1176 need_channel_layout = TRUE;
1177 gst_structure_set (s, "channel-mask", GST_TYPE_BITMASK,
1178 G_GUINT64_CONSTANT (0), NULL);
1179 for (i = 0; i < G_N_ELEMENTS (spec.info.position); i++)
1180 spec.info.position[i] = GST_AUDIO_CHANNEL_POSITION_INVALID;
1183 if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec))
1186 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1188 if (!pulsesrc->context)
1191 name = "Record Stream";
1192 if (pulsesrc->proplist) {
1193 if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
1194 name, &pulsesrc->sample_spec,
1195 (need_channel_layout) ? NULL : &channel_map,
1196 pulsesrc->proplist)))
1199 } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
1200 name, &pulsesrc->sample_spec,
1201 (need_channel_layout) ? NULL : &channel_map)))
1204 m = pa_stream_get_channel_map (pulsesrc->stream);
1205 gst_pulse_channel_map_to_gst (m, &spec);
1206 gst_audio_channel_positions_to_valid_order (spec.info.position,
1207 spec.info.channels);
1208 gst_caps_unref (*caps);
1209 *caps = gst_audio_info_to_caps (&spec.info);
1211 GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, *caps);
1213 pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
1215 pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
1217 pa_stream_set_underflow_callback (pulsesrc->stream,
1218 gst_pulsesrc_stream_underflow_cb, pulsesrc);
1219 pa_stream_set_overflow_callback (pulsesrc->stream,
1220 gst_pulsesrc_stream_overflow_cb, pulsesrc);
1221 pa_stream_set_latency_update_callback (pulsesrc->stream,
1222 gst_pulsesrc_stream_latency_update_cb, pulsesrc);
1224 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1231 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1232 ("Can't parse caps."), (NULL));
1237 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
1238 ("Invalid sample specification."), (NULL));
1243 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
1244 goto unlock_and_fail;
1248 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1249 ("Failed to create stream: %s",
1250 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1251 goto unlock_and_fail;
1255 gst_pulsesrc_destroy_stream (pulsesrc);
1257 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1264 /* This is essentially gst_base_src_negotiate_default() but the caps
1265 * are guaranteed to have a channel layout for > 2 channels
1268 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
1270 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (basesrc);
1272 GstCaps *caps = NULL;
1273 GstCaps *peercaps = NULL;
1274 gboolean result = FALSE;
1276 /* first see what is possible on our source pad */
1277 thiscaps = gst_pad_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1278 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
1279 /* nothing or anything is allowed, we're done */
1280 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
1281 goto no_nego_needed;
1283 /* get the peer caps */
1284 peercaps = gst_pad_peer_query_caps (GST_BASE_SRC_PAD (basesrc), NULL);
1285 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
1287 /* get intersection */
1288 caps = gst_caps_intersect (thiscaps, peercaps);
1289 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
1290 gst_caps_unref (thiscaps);
1291 gst_caps_unref (peercaps);
1293 /* no peer, work with our own caps then */
1297 /* take first (and best, since they are sorted) possibility */
1298 caps = gst_caps_truncate (caps);
1301 if (!gst_caps_is_empty (caps)) {
1302 caps = GST_BASE_SRC_CLASS (parent_class)->fixate (basesrc, caps);
1303 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
1305 if (gst_caps_is_any (caps)) {
1306 /* hmm, still anything, so element can do anything and
1307 * nego is not needed */
1309 } else if (gst_caps_is_fixed (caps)) {
1310 /* yay, fixed caps, use those then */
1311 result = gst_pulsesrc_create_stream (pulsesrc, &caps);
1313 result = gst_base_src_set_caps (basesrc, caps);
1316 gst_caps_unref (caps);
1322 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1324 gst_caps_unref (thiscaps);
1330 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
1332 pa_buffer_attr wanted;
1333 const pa_buffer_attr *actual;
1334 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1335 pa_stream_flags_t flags;
1337 GstAudioClock *clock;
1339 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1342 GstAudioRingBufferSpec s = *spec;
1343 const pa_channel_map *m;
1345 m = pa_stream_get_channel_map (pulsesrc->stream);
1346 gst_pulse_channel_map_to_gst (m, &s);
1347 gst_audio_ring_buffer_set_channel_positions (GST_AUDIO_BASE_SRC
1348 (pulsesrc)->ringbuffer, s.info.position);
1351 /* enable event notifications */
1352 GST_LOG_OBJECT (pulsesrc, "subscribing to context events");
1353 if (!(o = pa_context_subscribe (pulsesrc->context,
1354 PA_SUBSCRIPTION_MASK_SOURCE_OUTPUT, NULL, NULL))) {
1355 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1356 ("pa_context_subscribe() failed: %s",
1357 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1358 goto unlock_and_fail;
1361 pa_operation_unref (o);
1363 wanted.maxlength = -1;
1364 wanted.tlength = -1;
1367 wanted.fragsize = spec->segsize;
1369 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
1370 GST_INFO_OBJECT (pulsesrc, "tlength: %d", wanted.tlength);
1371 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", wanted.prebuf);
1372 GST_INFO_OBJECT (pulsesrc, "minreq: %d", wanted.minreq);
1373 GST_INFO_OBJECT (pulsesrc, "fragsize: %d", wanted.fragsize);
1375 flags = PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
1376 PA_STREAM_NOT_MONOTONIC | PA_STREAM_ADJUST_LATENCY |
1377 PA_STREAM_START_CORKED;
1379 if (pulsesrc->mute_set && pulsesrc->mute)
1380 flags |= PA_STREAM_START_MUTED;
1382 if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
1384 goto connect_failed;
1387 /* our clock will now start from 0 again */
1388 clock = GST_AUDIO_CLOCK (GST_AUDIO_BASE_SRC (pulsesrc)->clock);
1389 gst_audio_clock_reset (clock, 0);
1391 pulsesrc->corked = TRUE;
1394 pa_stream_state_t state;
1396 state = pa_stream_get_state (pulsesrc->stream);
1398 if (!PA_STREAM_IS_GOOD (state))
1401 if (state == PA_STREAM_READY)
1404 /* Wait until the stream is ready */
1405 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1407 pulsesrc->stream_connected = TRUE;
1409 /* store the source output index so it can be accessed via a property */
1410 pulsesrc->source_output_idx = pa_stream_get_index (pulsesrc->stream);
1411 g_object_notify (G_OBJECT (pulsesrc), "source-output-index");
1413 if (pulsesrc->volume_set) {
1414 gst_pulsesrc_set_stream_volume (pulsesrc, pulsesrc->volume);
1415 pulsesrc->volume_set = FALSE;
1418 /* get the actual buffering properties now */
1419 actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1421 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1422 GST_INFO_OBJECT (pulsesrc, "tlength: %d (wanted: %d)",
1423 actual->tlength, wanted.tlength);
1424 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", actual->prebuf);
1425 GST_INFO_OBJECT (pulsesrc, "minreq: %d (wanted %d)", actual->minreq,
1427 GST_INFO_OBJECT (pulsesrc, "fragsize: %d (wanted %d)",
1428 actual->fragsize, wanted.fragsize);
1430 if (actual->fragsize >= wanted.fragsize) {
1431 spec->segsize = actual->fragsize;
1433 spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
1435 spec->segtotal = actual->maxlength / spec->segsize;
1437 if (!pulsesrc->paused) {
1438 GST_DEBUG_OBJECT (pulsesrc, "uncorking because we are playing");
1439 gst_pulsesrc_set_corked (pulsesrc, FALSE, FALSE);
1441 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1448 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1449 ("Failed to connect stream: %s",
1450 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1451 goto unlock_and_fail;
1455 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1456 ("Failed to connect stream: %s",
1457 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1458 goto unlock_and_fail;
1462 gst_pulsesrc_destroy_stream (pulsesrc);
1464 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1470 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1472 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1474 pulsesrc->operation_success = ! !success;
1475 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1479 gst_pulsesrc_reset (GstAudioSrc * asrc)
1481 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1482 pa_operation *o = NULL;
1484 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1485 GST_DEBUG_OBJECT (pulsesrc, "reset");
1487 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1488 goto unlock_and_fail;
1491 pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1493 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1494 ("pa_stream_flush() failed: %s",
1495 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1496 goto unlock_and_fail;
1499 pulsesrc->paused = TRUE;
1500 /* Inform anyone waiting in _write() call that it shall wakeup */
1501 if (pulsesrc->in_read) {
1502 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1505 pulsesrc->operation_success = FALSE;
1506 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1508 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1509 goto unlock_and_fail;
1511 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1514 if (!pulsesrc->operation_success) {
1515 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1516 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1517 goto unlock_and_fail;
1523 pa_operation_cancel (o);
1524 pa_operation_unref (o);
1527 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1530 /* update the corked state of a stream, must be called with the mainloop
1533 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1535 pa_operation *o = NULL;
1536 gboolean res = FALSE;
1538 GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1539 if (!psrc->stream_connected)
1542 if (psrc->corked != corked) {
1543 if (!(o = pa_stream_cork (psrc->stream, corked,
1544 gst_pulsesrc_success_cb, psrc)))
1547 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1548 pa_threaded_mainloop_wait (psrc->mainloop);
1549 if (gst_pulsesrc_is_dead (psrc, TRUE))
1552 psrc->corked = corked;
1554 GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1560 pa_operation_unref (o);
1567 GST_DEBUG_OBJECT (psrc, "the server is dead");
1572 GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1573 ("pa_stream_cork() failed: %s",
1574 pa_strerror (pa_context_errno (psrc->context))), (NULL));
1579 /* start/resume playback ASAP */
1581 gst_pulsesrc_play (GstPulseSrc * psrc)
1583 pa_threaded_mainloop_lock (psrc->mainloop);
1584 GST_DEBUG_OBJECT (psrc, "playing");
1585 psrc->paused = FALSE;
1586 gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1587 pa_threaded_mainloop_unlock (psrc->mainloop);
1592 /* pause/stop playback ASAP */
1594 gst_pulsesrc_pause (GstPulseSrc * psrc)
1596 pa_threaded_mainloop_lock (psrc->mainloop);
1597 GST_DEBUG_OBJECT (psrc, "pausing");
1598 /* make sure the commit method stops writing */
1599 psrc->paused = TRUE;
1600 if (psrc->in_read) {
1601 /* we are waiting in a read, signal */
1602 GST_DEBUG_OBJECT (psrc, "signal read");
1603 pa_threaded_mainloop_signal (psrc->mainloop, 0);
1605 pa_threaded_mainloop_unlock (psrc->mainloop);
1610 static GstStateChangeReturn
1611 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1613 GstStateChangeReturn ret;
1614 GstPulseSrc *this = GST_PULSESRC_CAST (element);
1616 switch (transition) {
1617 case GST_STATE_CHANGE_NULL_TO_READY:
1618 if (!(this->mainloop = pa_threaded_mainloop_new ()))
1619 goto mainloop_failed;
1620 if (pa_threaded_mainloop_start (this->mainloop) < 0) {
1621 pa_threaded_mainloop_free (this->mainloop);
1622 this->mainloop = NULL;
1623 goto mainloop_start_failed;
1626 case GST_STATE_CHANGE_READY_TO_PAUSED:
1627 gst_element_post_message (element,
1628 gst_message_new_clock_provide (GST_OBJECT_CAST (element),
1629 GST_AUDIO_BASE_SRC (this)->clock, TRUE));
1631 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1632 /* uncork and start recording */
1633 gst_pulsesrc_play (this);
1635 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1636 /* stop recording ASAP by corking */
1637 pa_threaded_mainloop_lock (this->mainloop);
1638 GST_DEBUG_OBJECT (this, "corking");
1639 gst_pulsesrc_set_corked (this, TRUE, FALSE);
1640 pa_threaded_mainloop_unlock (this->mainloop);
1646 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1648 switch (transition) {
1649 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1650 /* now make sure we get out of the _read method */
1651 gst_pulsesrc_pause (this);
1653 case GST_STATE_CHANGE_READY_TO_NULL:
1655 pa_threaded_mainloop_stop (this->mainloop);
1657 gst_pulsesrc_destroy_context (this);
1659 if (this->mainloop) {
1660 pa_threaded_mainloop_free (this->mainloop);
1661 this->mainloop = NULL;
1664 case GST_STATE_CHANGE_PAUSED_TO_READY:
1665 /* format_lost is reset in release() in baseaudiosink */
1666 gst_element_post_message (element,
1667 gst_message_new_clock_lost (GST_OBJECT_CAST (element),
1668 GST_AUDIO_BASE_SRC (this)->clock));
1679 GST_ELEMENT_ERROR (this, RESOURCE, FAILED,
1680 ("pa_threaded_mainloop_new() failed"), (NULL));
1681 return GST_STATE_CHANGE_FAILURE;
1683 mainloop_start_failed:
1685 GST_ELEMENT_ERROR (this, RESOURCE, FAILED,
1686 ("pa_threaded_mainloop_start() failed"), (NULL));
1687 return GST_STATE_CHANGE_FAILURE;
1692 gst_pulsesrc_get_time (GstClock * clock, GstPulseSrc * src)
1696 pa_threaded_mainloop_lock (src->mainloop);
1698 if (gst_pulsesrc_is_dead (src, TRUE)) {
1699 goto unlock_and_out;
1702 if (pa_stream_get_time (src->stream, &time) < 0) {
1703 GST_DEBUG_OBJECT (src, "could not get time");
1704 time = GST_CLOCK_TIME_NONE;
1711 pa_threaded_mainloop_unlock (src->mainloop);