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>
48 #include "pulseutil.h"
49 #include "pulsemixerctrl.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
65 PROP_STREAM_PROPERTIES,
69 static void gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc);
70 static void gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc);
72 static void gst_pulsesrc_set_property (GObject * object, guint prop_id,
73 const GValue * value, GParamSpec * pspec);
74 static void gst_pulsesrc_get_property (GObject * object, guint prop_id,
75 GValue * value, GParamSpec * pspec);
76 static void gst_pulsesrc_finalize (GObject * object);
78 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
80 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
82 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
83 GstRingBufferSpec * spec);
85 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
87 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
89 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
91 static void gst_pulsesrc_reset (GstAudioSrc * src);
93 static gboolean gst_pulsesrc_negotiate (GstBaseSrc * basesrc);
95 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
96 element, GstStateChange transition);
98 static void gst_pulsesrc_init_interfaces (GType type);
100 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
101 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
103 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
106 GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
107 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
108 GST_BOILERPLATE_FULL (GstPulseSrc, gst_pulsesrc, GstAudioSrc,
109 GST_TYPE_AUDIO_SRC, gst_pulsesrc_init_interfaces);
112 gst_pulsesrc_interface_supported (GstImplementsInterface *
113 iface, GType interface_type)
115 GstPulseSrc *this = GST_PULSESRC_CAST (iface);
117 if (interface_type == GST_TYPE_MIXER && this->mixer)
120 if (interface_type == GST_TYPE_PROPERTY_PROBE && this->probe)
127 gst_pulsesrc_implements_interface_init (GstImplementsInterfaceClass * klass)
129 klass->supported = gst_pulsesrc_interface_supported;
133 gst_pulsesrc_init_interfaces (GType type)
135 static const GInterfaceInfo implements_iface_info = {
136 (GInterfaceInitFunc) gst_pulsesrc_implements_interface_init,
140 static const GInterfaceInfo mixer_iface_info = {
141 (GInterfaceInitFunc) gst_pulsesrc_mixer_interface_init,
145 static const GInterfaceInfo probe_iface_info = {
146 (GInterfaceInitFunc) gst_pulsesrc_property_probe_interface_init,
151 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
152 &implements_iface_info);
153 g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
154 g_type_add_interface_static (type, GST_TYPE_PROPERTY_PROBE,
159 gst_pulsesrc_base_init (gpointer g_class)
162 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
165 GST_STATIC_CAPS ("audio/x-raw-int, "
166 "endianness = (int) { " ENDIANNESS " }, "
167 "signed = (boolean) TRUE, "
170 "rate = (int) [ 1, MAX ], "
171 "channels = (int) [ 1, 32 ];"
172 "audio/x-raw-float, "
173 "endianness = (int) { " ENDIANNESS " }, "
175 "rate = (int) [ 1, MAX ], "
176 "channels = (int) [ 1, 32 ];"
178 "endianness = (int) { " ENDIANNESS " }, "
179 "signed = (boolean) TRUE, "
182 "rate = (int) [ 1, MAX ], "
183 "channels = (int) [ 1, 32 ];"
185 "signed = (boolean) FALSE, "
188 "rate = (int) [ 1, MAX ], "
189 "channels = (int) [ 1, 32 ];"
191 "rate = (int) [ 1, MAX], "
192 "channels = (int) [ 1, 32 ];"
194 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
197 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
199 gst_element_class_set_details_simple (element_class,
200 "PulseAudio Audio Source",
202 "Captures audio from a PulseAudio server", "Lennart Poettering");
203 gst_element_class_add_pad_template (element_class,
204 gst_static_pad_template_get (&pad_template));
208 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
210 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
211 GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
212 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
213 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
215 gobject_class->finalize = gst_pulsesrc_finalize;
216 gobject_class->set_property = gst_pulsesrc_set_property;
217 gobject_class->get_property = gst_pulsesrc_get_property;
219 gstelement_class->change_state =
220 GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
222 gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
224 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
225 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
226 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
227 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
228 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
229 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
230 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
232 /* Overwrite GObject fields */
233 g_object_class_install_property (gobject_class,
235 g_param_spec_string ("server", "Server",
236 "The PulseAudio server to connect to", DEFAULT_SERVER,
237 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_DEVICE,
240 g_param_spec_string ("device", "Device",
241 "The PulseAudio source device to connect to", DEFAULT_DEVICE,
242 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class,
246 g_param_spec_string ("device-name", "Device name",
247 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
248 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
251 * GstPulseSink:client
253 * The PulseAudio client name to use.
257 g_object_class_install_property (gobject_class,
259 g_param_spec_string ("client", "Client",
260 "The PulseAudio client_name_to_use", gst_pulse_client_name (),
261 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
262 GST_PARAM_MUTABLE_READY));
265 * GstPulseSrc:stream-properties
267 * List of pulseaudio stream properties. A list of defined properties can be
268 * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
270 * Below is an example for registering as a music application to pulseaudio.
272 * GstStructure *props;
274 * props = gst_structure_from_string ("props,media.role=music", NULL);
275 * g_object_set (pulse, "stream-properties", props, NULL);
276 * gst_structure_free (props);
281 g_object_class_install_property (gobject_class,
282 PROP_STREAM_PROPERTIES,
283 g_param_spec_boxed ("stream-properties", "stream properties",
284 "list of pulseaudio stream properties",
285 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
289 gst_pulsesrc_init (GstPulseSrc * pulsesrc, GstPulseSrcClass * klass)
291 pulsesrc->server = NULL;
292 pulsesrc->device = NULL;
293 pulsesrc->client_name = gst_pulse_client_name ();
294 pulsesrc->device_description = NULL;
296 pulsesrc->context = NULL;
297 pulsesrc->stream = NULL;
299 pulsesrc->read_buffer = NULL;
300 pulsesrc->read_buffer_length = 0;
302 #ifdef HAVE_PULSE_0_9_13
303 pa_sample_spec_init (&pulsesrc->sample_spec);
305 pulsesrc->sample_spec.format = PA_SAMPLE_INVALID;
306 pulsesrc->sample_spec.rate = 0;
307 pulsesrc->sample_spec.channels = 0;
310 pulsesrc->operation_success = FALSE;
311 pulsesrc->paused = FALSE;
312 pulsesrc->in_read = FALSE;
314 pulsesrc->mixer = NULL;
316 pulsesrc->properties = NULL;
317 pulsesrc->proplist = NULL;
319 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 */
321 /* this should be the default but it isn't yet */
322 gst_base_audio_src_set_slave_method (GST_BASE_AUDIO_SRC (pulsesrc),
323 GST_BASE_AUDIO_SRC_SLAVE_SKEW);
327 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
329 if (pulsesrc->stream) {
330 pa_stream_disconnect (pulsesrc->stream);
331 pa_stream_unref (pulsesrc->stream);
332 pulsesrc->stream = NULL;
335 g_free (pulsesrc->device_description);
336 pulsesrc->device_description = NULL;
340 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
343 gst_pulsesrc_destroy_stream (pulsesrc);
345 if (pulsesrc->context) {
346 pa_context_disconnect (pulsesrc->context);
347 pa_context_unref (pulsesrc->context);
348 pulsesrc->context = NULL;
353 gst_pulsesrc_finalize (GObject * object)
355 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
357 g_free (pulsesrc->server);
358 g_free (pulsesrc->device);
359 g_free (pulsesrc->client_name);
361 if (pulsesrc->properties)
362 gst_structure_free (pulsesrc->properties);
363 if (pulsesrc->proplist)
364 pa_proplist_free (pulsesrc->proplist);
366 if (pulsesrc->mixer) {
367 gst_pulsemixer_ctrl_free (pulsesrc->mixer);
368 pulsesrc->mixer = NULL;
371 if (pulsesrc->probe) {
372 gst_pulseprobe_free (pulsesrc->probe);
373 pulsesrc->probe = NULL;
376 G_OBJECT_CLASS (parent_class)->finalize (object);
379 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
380 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
383 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
385 if (!CONTEXT_OK (pulsesrc->context))
388 if (check_stream && !STREAM_OK (pulsesrc->stream))
395 const gchar *err_str = pulsesrc->context ?
396 pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
397 GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
404 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
407 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
412 g_free (pulsesrc->device_description);
413 pulsesrc->device_description = g_strdup (i->description);
416 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
420 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
422 pa_operation *o = NULL;
425 if (!pulsesrc->mainloop)
428 pa_threaded_mainloop_lock (pulsesrc->mainloop);
430 if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
431 pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
433 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
434 ("pa_stream_get_source_info() failed: %s",
435 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
439 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
441 if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
444 pa_threaded_mainloop_wait (pulsesrc->mainloop);
450 pa_operation_unref (o);
452 t = g_strdup (pulsesrc->device_description);
454 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
460 GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
466 gst_pulsesrc_set_property (GObject * object,
467 guint prop_id, const GValue * value, GParamSpec * pspec)
470 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
474 g_free (pulsesrc->server);
475 pulsesrc->server = g_value_dup_string (value);
477 gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
480 g_free (pulsesrc->device);
481 pulsesrc->device = g_value_dup_string (value);
484 g_free (pulsesrc->client_name);
485 if (!g_value_get_string (value)) {
486 GST_WARNING_OBJECT (pulsesrc,
487 "Empty PulseAudio client name not allowed. Resetting to default value");
488 pulsesrc->client_name = gst_pulse_client_name ();
490 pulsesrc->client_name = g_value_dup_string (value);
492 case PROP_STREAM_PROPERTIES:
493 if (pulsesrc->properties)
494 gst_structure_free (pulsesrc->properties);
495 pulsesrc->properties =
496 gst_structure_copy (gst_value_get_structure (value));
497 if (pulsesrc->proplist)
498 pa_proplist_free (pulsesrc->proplist);
499 pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
502 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
508 gst_pulsesrc_get_property (GObject * object,
509 guint prop_id, GValue * value, GParamSpec * pspec)
512 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
516 g_value_set_string (value, pulsesrc->server);
519 g_value_set_string (value, pulsesrc->device);
521 case PROP_DEVICE_NAME:
522 g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
525 g_value_set_string (value, pulsesrc->client_name);
527 case PROP_STREAM_PROPERTIES:
528 gst_value_set_structure (value, pulsesrc->properties);
531 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
537 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
539 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
541 switch (pa_context_get_state (c)) {
542 case PA_CONTEXT_READY:
543 case PA_CONTEXT_TERMINATED:
544 case PA_CONTEXT_FAILED:
545 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
548 case PA_CONTEXT_UNCONNECTED:
549 case PA_CONTEXT_CONNECTING:
550 case PA_CONTEXT_AUTHORIZING:
551 case PA_CONTEXT_SETTING_NAME:
557 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
559 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
561 switch (pa_stream_get_state (s)) {
563 case PA_STREAM_READY:
564 case PA_STREAM_FAILED:
565 case PA_STREAM_TERMINATED:
566 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
569 case PA_STREAM_UNCONNECTED:
570 case PA_STREAM_CREATING:
576 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
578 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
580 GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
582 if (pulsesrc->in_read) {
583 /* only signal when reading */
584 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
589 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
591 const pa_timing_info *info;
592 pa_usec_t source_usec;
594 info = pa_stream_get_timing_info (s);
597 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
598 "latency update (information unknown)");
601 #ifdef HAVE_PULSE_0_9_11
602 source_usec = info->configured_source_usec;
607 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
608 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
609 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
610 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
611 info->write_index, info->read_index_corrupt, info->read_index,
612 info->source_usec, source_usec);
616 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
618 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
622 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
624 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
628 gst_pulsesrc_open (GstAudioSrc * asrc)
630 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
632 pa_threaded_mainloop_lock (pulsesrc->mainloop);
634 g_assert (!pulsesrc->context);
635 g_assert (!pulsesrc->stream);
637 GST_DEBUG_OBJECT (pulsesrc, "opening device");
639 if (!(pulsesrc->context =
640 pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
641 pulsesrc->client_name))) {
642 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
644 goto unlock_and_fail;
647 pa_context_set_state_callback (pulsesrc->context,
648 gst_pulsesrc_context_state_cb, pulsesrc);
650 GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
651 GST_STR_NULL (pulsesrc->server));
653 if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
654 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
655 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
656 goto unlock_and_fail;
660 pa_context_state_t state;
662 state = pa_context_get_state (pulsesrc->context);
664 if (!PA_CONTEXT_IS_GOOD (state)) {
665 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
666 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
667 goto unlock_and_fail;
670 if (state == PA_CONTEXT_READY)
673 /* Wait until the context is ready */
674 pa_threaded_mainloop_wait (pulsesrc->mainloop);
676 GST_DEBUG_OBJECT (pulsesrc, "connected");
678 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
685 gst_pulsesrc_destroy_context (pulsesrc);
687 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
694 gst_pulsesrc_close (GstAudioSrc * asrc)
696 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
698 pa_threaded_mainloop_lock (pulsesrc->mainloop);
699 gst_pulsesrc_destroy_context (pulsesrc);
700 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
706 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
708 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
710 pa_threaded_mainloop_lock (pulsesrc->mainloop);
711 gst_pulsesrc_destroy_stream (pulsesrc);
713 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
715 pulsesrc->read_buffer = NULL;
716 pulsesrc->read_buffer_length = 0;
722 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
724 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
727 pa_threaded_mainloop_lock (pulsesrc->mainloop);
728 pulsesrc->in_read = TRUE;
730 if (pulsesrc->paused)
736 GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
738 /*check if we have a leftover buffer */
739 if (!pulsesrc->read_buffer) {
741 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
742 goto unlock_and_fail;
744 /* read all available data, we keep a pointer to the data and the length
745 * and take from it what we need. */
746 if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
747 &pulsesrc->read_buffer_length) < 0)
750 GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
751 pulsesrc->read_buffer_length);
753 /* if we have data, process if */
754 if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
757 /* now wait for more data to become available */
758 GST_LOG_OBJECT (pulsesrc, "waiting for data");
759 pa_threaded_mainloop_wait (pulsesrc->mainloop);
761 if (pulsesrc->paused)
766 l = pulsesrc->read_buffer_length >
767 length ? length : pulsesrc->read_buffer_length;
769 memcpy (data, pulsesrc->read_buffer, l);
771 pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
772 pulsesrc->read_buffer_length -= l;
774 data = (guint8 *) data + l;
778 if (pulsesrc->read_buffer_length <= 0) {
779 /* we copied all of the data, drop it now */
780 if (pa_stream_drop (pulsesrc->stream) < 0)
783 /* reset pointer to data */
784 pulsesrc->read_buffer = NULL;
785 pulsesrc->read_buffer_length = 0;
789 pulsesrc->in_read = FALSE;
790 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
797 GST_LOG_OBJECT (pulsesrc, "we are paused");
798 goto unlock_and_fail;
802 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
803 ("pa_stream_peek() failed: %s",
804 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
805 goto unlock_and_fail;
809 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
810 ("pa_stream_drop() failed: %s",
811 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
812 goto unlock_and_fail;
816 pulsesrc->in_read = FALSE;
817 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
823 /* return the delay in samples */
825 gst_pulsesrc_delay (GstAudioSrc * asrc)
827 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
832 pa_threaded_mainloop_lock (pulsesrc->mainloop);
833 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
836 /* get the latency, this can fail when we don't have a latency update yet.
837 * We don't want to wait for latency updates here but we just return 0. */
838 res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
840 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
843 GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
849 result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
856 GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
857 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
863 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
865 pa_channel_map channel_map;
867 gboolean need_channel_layout = FALSE;
868 GstRingBufferSpec spec;
871 memset (&spec, 0, sizeof (GstRingBufferSpec));
872 spec.latency_time = GST_SECOND;
873 if (!gst_ring_buffer_parse_caps (&spec, caps)) {
874 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
875 ("Can't parse caps."), (NULL));
878 /* Keep the refcount of the caps at 1 to make them writable */
879 gst_caps_unref (spec.caps);
881 if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec)) {
882 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
883 ("Invalid sample specification."), (NULL));
887 pa_threaded_mainloop_lock (pulsesrc->mainloop);
889 if (!pulsesrc->context) {
890 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
891 goto unlock_and_fail;
894 s = gst_caps_get_structure (caps, 0);
895 if (!gst_structure_has_field (s, "channel-layout") ||
896 !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
897 if (spec.channels == 1)
898 pa_channel_map_init_mono (&channel_map);
899 else if (spec.channels == 2)
900 pa_channel_map_init_stereo (&channel_map);
902 need_channel_layout = TRUE;
905 name = "Record Stream";
906 if (pulsesrc->proplist) {
907 if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
908 name, &pulsesrc->sample_spec,
909 (need_channel_layout) ? NULL : &channel_map,
910 pulsesrc->proplist))) {
911 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
912 ("Failed to create stream: %s",
913 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
914 goto unlock_and_fail;
916 } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
917 name, &pulsesrc->sample_spec,
918 (need_channel_layout) ? NULL : &channel_map))) {
919 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
920 ("Failed to create stream: %s",
921 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
922 goto unlock_and_fail;
925 if (need_channel_layout) {
926 const pa_channel_map *m = pa_stream_get_channel_map (pulsesrc->stream);
928 gst_pulse_channel_map_to_gst (m, &spec);
932 GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, caps);
934 pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
936 pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
938 pa_stream_set_underflow_callback (pulsesrc->stream,
939 gst_pulsesrc_stream_underflow_cb, pulsesrc);
940 pa_stream_set_overflow_callback (pulsesrc->stream,
941 gst_pulsesrc_stream_overflow_cb, pulsesrc);
942 pa_stream_set_latency_update_callback (pulsesrc->stream,
943 gst_pulsesrc_stream_latency_update_cb, pulsesrc);
945 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
950 gst_pulsesrc_destroy_stream (pulsesrc);
952 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
958 /* This is essentially gst_base_src_negotiate_default() but the caps
959 * are guaranteed to have a channel layout for > 2 channels
962 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
965 GstCaps *caps = NULL;
966 GstCaps *peercaps = NULL;
967 gboolean result = FALSE;
969 /* first see what is possible on our source pad */
970 thiscaps = gst_pad_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
971 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
972 /* nothing or anything is allowed, we're done */
973 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
976 /* get the peer caps */
977 peercaps = gst_pad_peer_get_caps_reffed (GST_BASE_SRC_PAD (basesrc));
978 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
980 /* get intersection */
981 caps = gst_caps_intersect (thiscaps, peercaps);
982 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
983 gst_caps_unref (thiscaps);
984 gst_caps_unref (peercaps);
986 /* no peer, work with our own caps then */
990 /* take first (and best, since they are sorted) possibility */
991 caps = gst_caps_make_writable (caps);
992 gst_caps_truncate (caps);
995 if (!gst_caps_is_empty (caps)) {
996 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
997 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
999 if (gst_caps_is_any (caps)) {
1000 /* hmm, still anything, so element can do anything and
1001 * nego is not needed */
1003 } else if (gst_caps_is_fixed (caps)) {
1004 /* yay, fixed caps, use those then */
1005 result = gst_pulsesrc_create_stream (GST_PULSESRC_CAST (basesrc), caps);
1007 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
1010 gst_caps_unref (caps);
1016 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
1018 gst_caps_unref (thiscaps);
1024 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
1026 pa_buffer_attr wanted;
1027 const pa_buffer_attr *actual;
1028 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1030 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1032 wanted.maxlength = -1;
1033 wanted.tlength = -1;
1036 wanted.fragsize = spec->segsize;
1038 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
1039 GST_INFO_OBJECT (pulsesrc, "tlength: %d", wanted.tlength);
1040 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", wanted.prebuf);
1041 GST_INFO_OBJECT (pulsesrc, "minreq: %d", wanted.minreq);
1042 GST_INFO_OBJECT (pulsesrc, "fragsize: %d", wanted.fragsize);
1044 if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
1045 PA_STREAM_INTERPOLATE_TIMING |
1046 PA_STREAM_AUTO_TIMING_UPDATE | PA_STREAM_NOT_MONOTONOUS |
1047 #ifdef HAVE_PULSE_0_9_11
1048 PA_STREAM_ADJUST_LATENCY |
1050 PA_STREAM_START_CORKED) < 0) {
1051 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1052 ("Failed to connect stream: %s",
1053 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1054 goto unlock_and_fail;
1057 pulsesrc->corked = TRUE;
1060 pa_stream_state_t state;
1062 state = pa_stream_get_state (pulsesrc->stream);
1064 if (!PA_STREAM_IS_GOOD (state)) {
1065 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1066 ("Failed to connect stream: %s",
1067 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1068 goto unlock_and_fail;
1071 if (state == PA_STREAM_READY)
1074 /* Wait until the stream is ready */
1075 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1078 /* get the actual buffering properties now */
1079 actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1081 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1082 GST_INFO_OBJECT (pulsesrc, "tlength: %d (wanted: %d)",
1083 actual->tlength, wanted.tlength);
1084 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", actual->prebuf);
1085 GST_INFO_OBJECT (pulsesrc, "minreq: %d (wanted %d)", actual->minreq,
1087 GST_INFO_OBJECT (pulsesrc, "fragsize: %d (wanted %d)",
1088 actual->fragsize, wanted.fragsize);
1090 if (actual->fragsize >= wanted.fragsize) {
1091 spec->segsize = actual->fragsize;
1093 spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
1095 spec->segtotal = actual->maxlength / spec->segsize;
1097 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1103 gst_pulsesrc_destroy_stream (pulsesrc);
1105 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1111 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1113 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1115 pulsesrc->operation_success = !!success;
1116 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1120 gst_pulsesrc_reset (GstAudioSrc * asrc)
1122 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1123 pa_operation *o = NULL;
1125 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1126 GST_DEBUG_OBJECT (pulsesrc, "reset");
1128 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1129 goto unlock_and_fail;
1132 pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1134 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1135 ("pa_stream_flush() failed: %s",
1136 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1137 goto unlock_and_fail;
1140 pulsesrc->paused = TRUE;
1141 /* Inform anyone waiting in _write() call that it shall wakeup */
1142 if (pulsesrc->in_read) {
1143 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1146 pulsesrc->operation_success = FALSE;
1147 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1149 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1150 goto unlock_and_fail;
1152 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1155 if (!pulsesrc->operation_success) {
1156 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1157 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1158 goto unlock_and_fail;
1164 pa_operation_cancel (o);
1165 pa_operation_unref (o);
1168 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1171 /* update the corked state of a stream, must be called with the mainloop
1174 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1176 pa_operation *o = NULL;
1177 gboolean res = FALSE;
1179 GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1180 if (psrc->corked != corked) {
1181 if (!(o = pa_stream_cork (psrc->stream, corked,
1182 gst_pulsesrc_success_cb, psrc)))
1185 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1186 pa_threaded_mainloop_wait (psrc->mainloop);
1187 if (gst_pulsesrc_is_dead (psrc, TRUE))
1190 psrc->corked = corked;
1192 GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1198 pa_operation_unref (o);
1205 GST_DEBUG_OBJECT (psrc, "the server is dead");
1210 GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1211 ("pa_stream_cork() failed: %s",
1212 pa_strerror (pa_context_errno (psrc->context))), (NULL));
1217 /* start/resume playback ASAP */
1219 gst_pulsesrc_play (GstPulseSrc * psrc)
1221 pa_threaded_mainloop_lock (psrc->mainloop);
1222 GST_DEBUG_OBJECT (psrc, "playing");
1223 psrc->paused = FALSE;
1224 gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1225 pa_threaded_mainloop_unlock (psrc->mainloop);
1230 /* pause/stop playback ASAP */
1232 gst_pulsesrc_pause (GstPulseSrc * psrc)
1234 pa_threaded_mainloop_lock (psrc->mainloop);
1235 GST_DEBUG_OBJECT (psrc, "pausing");
1236 /* make sure the commit method stops writing */
1237 psrc->paused = TRUE;
1238 if (psrc->in_read) {
1239 /* we are waiting in a read, signal */
1240 GST_DEBUG_OBJECT (psrc, "signal read");
1241 pa_threaded_mainloop_signal (psrc->mainloop, 0);
1243 pa_threaded_mainloop_unlock (psrc->mainloop);
1248 static GstStateChangeReturn
1249 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1251 GstStateChangeReturn ret;
1252 GstPulseSrc *this = GST_PULSESRC_CAST (element);
1255 switch (transition) {
1256 case GST_STATE_CHANGE_NULL_TO_READY:
1257 this->mainloop = pa_threaded_mainloop_new ();
1258 g_assert (this->mainloop);
1260 e = pa_threaded_mainloop_start (this->mainloop);
1265 gst_pulsemixer_ctrl_new (G_OBJECT (this), this->server,
1266 this->device, GST_PULSEMIXER_SOURCE);
1268 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1269 /* uncork and start recording */
1270 gst_pulsesrc_play (this);
1272 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1273 /* stop recording ASAP by corking */
1274 pa_threaded_mainloop_lock (this->mainloop);
1275 GST_DEBUG_OBJECT (this, "corking");
1276 gst_pulsesrc_set_corked (this, TRUE, FALSE);
1277 pa_threaded_mainloop_unlock (this->mainloop);
1283 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1285 switch (transition) {
1286 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1287 /* now make sure we get out of the _read method */
1288 gst_pulsesrc_pause (this);
1290 case GST_STATE_CHANGE_READY_TO_NULL:
1292 gst_pulsemixer_ctrl_free (this->mixer);
1297 pa_threaded_mainloop_stop (this->mainloop);
1299 gst_pulsesrc_destroy_context (this);
1301 if (this->mainloop) {
1302 pa_threaded_mainloop_free (this->mainloop);
1303 this->mainloop = NULL;