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 * @short_description: Capture audio from a PulseAudio sound server
25 * @see_also: pulsesink, pulsemixer
29 * This element captures audio from a PulseAudio sound server.
31 * <title>Example pipelines</title>
34 * gst-launch -v pulsesrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg
36 * Record from a sound card using ALSA and encode to Ogg/Vorbis.
48 #include <gst/base/gstbasesrc.h>
49 #include <gst/gsttaglist.h>
52 #include "pulseutil.h"
53 #include "pulsemixerctrl.h"
55 GST_DEBUG_CATEGORY_EXTERN (pulse_debug);
56 #define GST_CAT_DEFAULT pulse_debug
64 static GstAudioSrcClass *parent_class = NULL;
66 GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc)
68 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 void gst_pulsesrc_dispose (GObject * object);
80 static gboolean gst_pulsesrc_open (GstAudioSrc * asrc);
82 static gboolean gst_pulsesrc_close (GstAudioSrc * asrc);
84 static gboolean gst_pulsesrc_prepare (GstAudioSrc * asrc,
85 GstRingBufferSpec * spec);
86 static gboolean gst_pulsesrc_unprepare (GstAudioSrc * asrc);
88 static guint gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data,
90 static guint gst_pulsesrc_delay (GstAudioSrc * asrc);
92 static GstStateChangeReturn gst_pulsesrc_change_state (GstElement *
93 element, GstStateChange transition);
95 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
96 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
98 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
101 static gboolean gst_pulsesrc_interface_supported (GstImplementsInterface *
102 iface, GType interface_type)
104 GstPulseSrc *this = GST_PULSESRC (iface);
106 if (interface_type == GST_TYPE_MIXER && this->mixer)
113 gst_pulsesrc_implements_interface_init (GstImplementsInterfaceClass * klass)
115 klass->supported = gst_pulsesrc_interface_supported;
119 gst_pulsesrc_init_interfaces (GType type)
121 static const GInterfaceInfo implements_iface_info = {
122 (GInterfaceInitFunc) gst_pulsesrc_implements_interface_init,
126 static const GInterfaceInfo mixer_iface_info = {
127 (GInterfaceInitFunc) gst_pulsesrc_mixer_interface_init,
132 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
133 &implements_iface_info);
134 g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
138 gst_pulsesrc_base_init (gpointer g_class)
141 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
144 GST_STATIC_CAPS ("audio/x-raw-int, "
145 "endianness = (int) { " ENDIANNESS " }, "
146 "signed = (boolean) TRUE, "
149 "rate = (int) [ 1, MAX ], "
150 "channels = (int) [ 1, 16 ];"
152 "endianness = (int) { " ENDIANNESS " }, "
153 "signed = (boolean) TRUE, "
156 "rate = (int) [ 1, MAX ], "
157 "channels = (int) [ 1, 16 ];"
158 "audio/x-raw-float, "
159 "endianness = (int) { " ENDIANNESS " }, "
161 "rate = (int) [ 1, MAX ], "
162 "channels = (int) [ 1, 16 ];"
164 "signed = (boolean) FALSE, "
167 "rate = (int) [ 1, MAX ], "
168 "channels = (int) [ 1, 16 ];"
170 "rate = (int) [ 1, MAX], "
171 "channels = (int) [ 1, 16 ];"
173 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 16 ]")
176 static const GstElementDetails details =
177 GST_ELEMENT_DETAILS ("PulseAudio Audio Source",
179 "Captures audio from a PulseAudio server",
180 "Lennart Poettering");
182 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
184 gst_element_class_set_details (element_class, &details);
185 gst_element_class_add_pad_template (element_class,
186 gst_static_pad_template_get (&pad_template));
190 gst_pulsesrc_class_init (gpointer g_class, gpointer class_data)
193 GObjectClass *gobject_class = G_OBJECT_CLASS (g_class);
195 GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (g_class);
197 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (g_class);
199 parent_class = g_type_class_peek_parent (g_class);
201 gstelement_class->change_state =
202 GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
204 gobject_class->dispose = GST_DEBUG_FUNCPTR (gst_pulsesrc_dispose);
205 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_pulsesrc_finalize);
206 gobject_class->set_property = GST_DEBUG_FUNCPTR (gst_pulsesrc_set_property);
207 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_pulsesrc_get_property);
209 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
210 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
211 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
212 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
213 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
214 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
216 /* Overwrite GObject fields */
217 g_object_class_install_property (gobject_class,
219 g_param_spec_string ("server", "Server",
220 "The PulseAudio server to connect to", NULL, G_PARAM_READWRITE));
221 g_object_class_install_property (gobject_class, PROP_DEVICE,
222 g_param_spec_string ("device", "Source",
223 "The PulseAudio source device to connect to", NULL,
228 gst_pulsesrc_init (GTypeInstance * instance, gpointer g_class)
231 GstPulseSrc *pulsesrc = GST_PULSESRC (instance);
235 pulsesrc->server = pulsesrc->device = NULL;
237 pulsesrc->context = NULL;
238 pulsesrc->stream = NULL;
240 pulsesrc->read_buffer = NULL;
241 pulsesrc->read_buffer_length = 0;
243 pulsesrc->mainloop = pa_threaded_mainloop_new ();
244 g_assert (pulsesrc->mainloop);
246 e = pa_threaded_mainloop_start (pulsesrc->mainloop);
249 pulsesrc->mixer = NULL;
253 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
255 if (pulsesrc->stream) {
256 pa_stream_disconnect (pulsesrc->stream);
257 pa_stream_unref (pulsesrc->stream);
258 pulsesrc->stream = NULL;
263 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
266 gst_pulsesrc_destroy_stream (pulsesrc);
268 if (pulsesrc->context) {
269 pa_context_disconnect (pulsesrc->context);
270 pa_context_unref (pulsesrc->context);
271 pulsesrc->context = NULL;
276 gst_pulsesrc_finalize (GObject * object)
278 GstPulseSrc *pulsesrc = GST_PULSESRC (object);
280 pa_threaded_mainloop_stop (pulsesrc->mainloop);
282 gst_pulsesrc_destroy_context (pulsesrc);
284 g_free (pulsesrc->server);
285 g_free (pulsesrc->device);
287 pa_threaded_mainloop_free (pulsesrc->mainloop);
290 gst_pulsemixer_ctrl_free (pulsesrc->mixer);
292 G_OBJECT_CLASS (parent_class)->finalize (object);
296 gst_pulsesrc_dispose (GObject * object)
298 G_OBJECT_CLASS (parent_class)->dispose (object);
302 gst_pulsesrc_set_property (GObject * object,
303 guint prop_id, const GValue * value, GParamSpec * pspec)
306 GstPulseSrc *pulsesrc = GST_PULSESRC (object);
310 g_free (pulsesrc->server);
311 pulsesrc->server = g_value_dup_string (value);
315 g_free (pulsesrc->device);
316 pulsesrc->device = g_value_dup_string (value);
320 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
326 gst_pulsesrc_get_property (GObject * object,
327 guint prop_id, GValue * value, GParamSpec * pspec)
330 GstPulseSrc *pulsesrc = GST_PULSESRC (object);
334 g_value_set_string (value, pulsesrc->server);
338 g_value_set_string (value, pulsesrc->device);
342 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
348 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
350 GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
352 switch (pa_context_get_state (c)) {
353 case PA_CONTEXT_READY:
354 case PA_CONTEXT_TERMINATED:
355 case PA_CONTEXT_FAILED:
356 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
359 case PA_CONTEXT_UNCONNECTED:
360 case PA_CONTEXT_CONNECTING:
361 case PA_CONTEXT_AUTHORIZING:
362 case PA_CONTEXT_SETTING_NAME:
368 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
370 GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
372 switch (pa_stream_get_state (s)) {
374 case PA_STREAM_READY:
375 case PA_STREAM_FAILED:
376 case PA_STREAM_TERMINATED:
377 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
380 case PA_STREAM_UNCONNECTED:
381 case PA_STREAM_CREATING:
387 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
389 GstPulseSrc *pulsesrc = GST_PULSESRC (userdata);
391 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
395 gst_pulsesrc_open (GstAudioSrc * asrc)
397 GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
399 gchar *name = gst_pulse_client_name ();
401 pa_threaded_mainloop_lock (pulsesrc->mainloop);
403 if (!(pulsesrc->context =
404 pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
406 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
408 goto unlock_and_fail;
411 pa_context_set_state_callback (pulsesrc->context,
412 gst_pulsesrc_context_state_cb, pulsesrc);
414 if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
415 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
416 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
417 goto unlock_and_fail;
420 /* Wait until the context is ready */
421 pa_threaded_mainloop_wait (pulsesrc->mainloop);
423 if (pa_context_get_state (pulsesrc->context) != PA_CONTEXT_READY) {
424 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
425 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
426 goto unlock_and_fail;
429 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
436 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
443 gst_pulsesrc_close (GstAudioSrc * asrc)
445 GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
447 pa_threaded_mainloop_lock (pulsesrc->mainloop);
448 gst_pulsesrc_destroy_context (pulsesrc);
449 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
455 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
457 pa_buffer_attr buf_attr;
459 pa_channel_map channel_map;
461 GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
463 if (!gst_pulse_fill_sample_spec (spec, &pulsesrc->sample_spec)) {
464 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
465 ("Invalid sample specification."), (NULL));
466 goto unlock_and_fail;
469 pa_threaded_mainloop_lock (pulsesrc->mainloop);
471 if (!pulsesrc->context
472 || pa_context_get_state (pulsesrc->context) != PA_CONTEXT_READY) {
473 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context state: %s",
475 context ? pa_strerror (pa_context_errno (pulsesrc->context)) :
477 goto unlock_and_fail;
480 if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
482 &pulsesrc->sample_spec,
483 gst_pulse_gst_to_channel_map (&channel_map, spec)))) {
484 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
485 ("Failed to create stream: %s",
486 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
487 goto unlock_and_fail;
490 pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
492 pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
495 memset (&buf_attr, 0, sizeof (buf_attr));
496 buf_attr.maxlength = spec->segtotal * spec->segsize * 2;
497 buf_attr.fragsize = spec->segsize;
499 if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &buf_attr,
500 PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
501 PA_STREAM_NOT_MONOTONOUS) < 0) {
502 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
503 ("Failed to connect stream: %s",
504 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
505 goto unlock_and_fail;
508 /* Wait until the stream is ready */
509 pa_threaded_mainloop_wait (pulsesrc->mainloop);
511 if (pa_stream_get_state (pulsesrc->stream) != PA_STREAM_READY) {
512 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
513 ("Failed to connect stream: %s",
514 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
515 goto unlock_and_fail;
518 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
520 spec->bytes_per_sample = pa_frame_size (&pulsesrc->sample_spec);
521 memset (spec->silence_sample, 0, spec->bytes_per_sample);
527 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
532 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
534 GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
536 pa_threaded_mainloop_lock (pulsesrc->mainloop);
537 gst_pulsesrc_destroy_stream (pulsesrc);
539 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
541 pulsesrc->read_buffer = NULL;
542 pulsesrc->read_buffer_length = 0;
547 #define CHECK_DEAD_GOTO(pulsesrc, label) \
548 if (!(pulsesrc)->context || pa_context_get_state((pulsesrc)->context) != PA_CONTEXT_READY || \
549 !(pulsesrc)->stream || pa_stream_get_state((pulsesrc)->stream) != PA_STREAM_READY) { \
550 GST_ELEMENT_ERROR((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s", (pulsesrc)->context ? pa_strerror(pa_context_errno((pulsesrc)->context)) : NULL), (NULL)); \
555 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
557 GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
561 pa_threaded_mainloop_lock (pulsesrc->mainloop);
563 CHECK_DEAD_GOTO (pulsesrc, unlock_and_fail);
568 if (!pulsesrc->read_buffer) {
571 if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
572 &pulsesrc->read_buffer_length) < 0) {
573 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
574 ("pa_stream_peek() failed: %s",
575 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
576 goto unlock_and_fail;
579 if (pulsesrc->read_buffer)
582 pa_threaded_mainloop_wait (pulsesrc->mainloop);
584 CHECK_DEAD_GOTO (pulsesrc, unlock_and_fail);
588 g_assert (pulsesrc->read_buffer && pulsesrc->read_buffer_length);
590 l = pulsesrc->read_buffer_length >
591 length ? length : pulsesrc->read_buffer_length;
593 memcpy (data, pulsesrc->read_buffer, l);
595 pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
596 pulsesrc->read_buffer_length -= l;
598 data = (guint8 *) data + l;
603 if (pulsesrc->read_buffer_length <= 0) {
605 if (pa_stream_drop (pulsesrc->stream) < 0) {
606 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
607 ("pa_stream_drop() failed: %s",
608 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
609 goto unlock_and_fail;
612 pulsesrc->read_buffer = NULL;
613 pulsesrc->read_buffer_length = 0;
617 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
622 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
627 gst_pulsesrc_delay (GstAudioSrc * asrc)
629 GstPulseSrc *pulsesrc = GST_PULSESRC (asrc);
635 pa_threaded_mainloop_lock (pulsesrc->mainloop);
637 CHECK_DEAD_GOTO (pulsesrc, unlock_and_fail);
639 if (pa_stream_get_latency (pulsesrc->stream, &t, &negative) < 0) {
641 if (pa_context_errno (pulsesrc->context) != PA_ERR_NODATA) {
642 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
643 ("pa_stream_get_latency() failed: %s",
644 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
645 goto unlock_and_fail;
648 GST_WARNING ("Not data while querying latency");
653 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
655 return (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
659 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
663 static GstStateChangeReturn
664 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
666 GstPulseSrc *this = GST_PULSESRC (element);
668 switch (transition) {
669 case GST_STATE_CHANGE_NULL_TO_READY:
673 gst_pulsemixer_ctrl_new (this->server, this->device,
674 GST_PULSEMIXER_SOURCE);
678 case GST_STATE_CHANGE_READY_TO_NULL:
681 gst_pulsemixer_ctrl_free (this->mixer);
691 if (GST_ELEMENT_CLASS (parent_class)->change_state)
692 return GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
694 return GST_STATE_CHANGE_SUCCESS;
698 gst_pulsesrc_get_type (void)
700 static GType pulsesrc_type = 0;
702 if (!pulsesrc_type) {
704 static const GTypeInfo pulsesrc_info = {
705 sizeof (GstPulseSrcClass),
706 gst_pulsesrc_base_init,
708 gst_pulsesrc_class_init,
711 sizeof (GstPulseSrc),
716 pulsesrc_type = g_type_register_static (GST_TYPE_AUDIO_SRC,
717 "GstPulseSrc", &pulsesrc_info, 0);
719 gst_pulsesrc_init_interfaces (pulsesrc_type);
722 return pulsesrc_type;