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 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
99 # define ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
101 # define ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
104 static GstStaticPadTemplate pad_template = GST_STATIC_PAD_TEMPLATE ("src",
107 GST_STATIC_CAPS ("audio/x-raw-int, "
108 "endianness = (int) { " ENDIANNESS " }, "
109 "signed = (boolean) TRUE, "
112 "rate = (int) [ 1, MAX ], "
113 "channels = (int) [ 1, 32 ];"
114 "audio/x-raw-float, "
115 "endianness = (int) { " ENDIANNESS " }, "
117 "rate = (int) [ 1, MAX ], "
118 "channels = (int) [ 1, 32 ];"
120 "endianness = (int) { " ENDIANNESS " }, "
121 "signed = (boolean) TRUE, "
124 "rate = (int) [ 1, MAX ], "
125 "channels = (int) [ 1, 32 ];"
127 "signed = (boolean) FALSE, "
130 "rate = (int) [ 1, MAX ], "
131 "channels = (int) [ 1, 32 ];"
133 "rate = (int) [ 1, MAX], "
134 "channels = (int) [ 1, 32 ];"
136 "rate = (int) [ 1, MAX], " "channels = (int) [ 1, 32 ]")
140 GST_IMPLEMENT_PULSEMIXER_CTRL_METHODS (GstPulseSrc, gst_pulsesrc);
141 GST_IMPLEMENT_PULSEPROBE_METHODS (GstPulseSrc, gst_pulsesrc);
143 #define gst_pulsesrc_parent_class parent_class
144 G_DEFINE_TYPE_WITH_CODE (GstPulseSrc, gst_pulsesrc, GST_TYPE_AUDIO_SRC,
145 G_IMPLEMENT_INTERFACE (GST_TYPE_MIXER, gst_pulsesrc_mixer_interface_init);
146 G_IMPLEMENT_INTERFACE (GST_TYPE_PROPERTY_PROBE,
147 gst_pulsesrc_property_probe_interface_init));
150 gst_pulsesrc_class_init (GstPulseSrcClass * klass)
152 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
153 GstAudioSrcClass *gstaudiosrc_class = GST_AUDIO_SRC_CLASS (klass);
154 GstBaseSrcClass *gstbasesrc_class = GST_BASE_SRC_CLASS (klass);
155 GstElementClass *gstelement_class = GST_ELEMENT_CLASS (klass);
157 gobject_class->finalize = gst_pulsesrc_finalize;
158 gobject_class->set_property = gst_pulsesrc_set_property;
159 gobject_class->get_property = gst_pulsesrc_get_property;
161 gstelement_class->change_state =
162 GST_DEBUG_FUNCPTR (gst_pulsesrc_change_state);
164 gstbasesrc_class->negotiate = GST_DEBUG_FUNCPTR (gst_pulsesrc_negotiate);
166 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_pulsesrc_open);
167 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_pulsesrc_close);
168 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_prepare);
169 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_pulsesrc_unprepare);
170 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_pulsesrc_read);
171 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_pulsesrc_delay);
172 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_pulsesrc_reset);
174 /* Overwrite GObject fields */
175 g_object_class_install_property (gobject_class,
177 g_param_spec_string ("server", "Server",
178 "The PulseAudio server to connect to", DEFAULT_SERVER,
179 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
181 g_object_class_install_property (gobject_class, PROP_DEVICE,
182 g_param_spec_string ("device", "Device",
183 "The PulseAudio source device to connect to", DEFAULT_DEVICE,
184 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186 g_object_class_install_property (gobject_class,
188 g_param_spec_string ("device-name", "Device name",
189 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
190 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
193 * GstPulseSink:client
195 * The PulseAudio client name to use.
199 g_object_class_install_property (gobject_class,
201 g_param_spec_string ("client", "Client",
202 "The PulseAudio client_name_to_use", gst_pulse_client_name (),
203 G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS |
204 GST_PARAM_MUTABLE_READY));
207 * GstPulseSrc:stream-properties
209 * List of pulseaudio stream properties. A list of defined properties can be
210 * found in the <ulink href="http://0pointer.de/lennart/projects/pulseaudio/doxygen/proplist_8h.html">pulseaudio api docs</ulink>.
212 * Below is an example for registering as a music application to pulseaudio.
214 * GstStructure *props;
216 * props = gst_structure_from_string ("props,media.role=music", NULL);
217 * g_object_set (pulse, "stream-properties", props, NULL);
218 * gst_structure_free (props);
223 g_object_class_install_property (gobject_class,
224 PROP_STREAM_PROPERTIES,
225 g_param_spec_boxed ("stream-properties", "stream properties",
226 "list of pulseaudio stream properties",
227 GST_TYPE_STRUCTURE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
229 gst_element_class_set_details_simple (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));
238 gst_pulsesrc_init (GstPulseSrc * pulsesrc)
240 pulsesrc->server = NULL;
241 pulsesrc->device = NULL;
242 pulsesrc->client_name = gst_pulse_client_name ();
243 pulsesrc->device_description = NULL;
245 pulsesrc->context = NULL;
246 pulsesrc->stream = NULL;
248 pulsesrc->read_buffer = NULL;
249 pulsesrc->read_buffer_length = 0;
251 pa_sample_spec_init (&pulsesrc->sample_spec);
253 pulsesrc->operation_success = FALSE;
254 pulsesrc->paused = FALSE;
255 pulsesrc->in_read = FALSE;
257 pulsesrc->mixer = NULL;
259 pulsesrc->properties = NULL;
260 pulsesrc->proplist = NULL;
262 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 */
264 /* this should be the default but it isn't yet */
265 gst_base_audio_src_set_slave_method (GST_BASE_AUDIO_SRC (pulsesrc),
266 GST_BASE_AUDIO_SRC_SLAVE_SKEW);
270 gst_pulsesrc_destroy_stream (GstPulseSrc * pulsesrc)
272 if (pulsesrc->stream) {
273 pa_stream_disconnect (pulsesrc->stream);
274 pa_stream_unref (pulsesrc->stream);
275 pulsesrc->stream = NULL;
278 g_free (pulsesrc->device_description);
279 pulsesrc->device_description = NULL;
283 gst_pulsesrc_destroy_context (GstPulseSrc * pulsesrc)
286 gst_pulsesrc_destroy_stream (pulsesrc);
288 if (pulsesrc->context) {
289 pa_context_disconnect (pulsesrc->context);
290 pa_context_unref (pulsesrc->context);
291 pulsesrc->context = NULL;
296 gst_pulsesrc_finalize (GObject * object)
298 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
300 g_free (pulsesrc->server);
301 g_free (pulsesrc->device);
302 g_free (pulsesrc->client_name);
304 if (pulsesrc->properties)
305 gst_structure_free (pulsesrc->properties);
306 if (pulsesrc->proplist)
307 pa_proplist_free (pulsesrc->proplist);
309 if (pulsesrc->mixer) {
310 gst_pulsemixer_ctrl_free (pulsesrc->mixer);
311 pulsesrc->mixer = NULL;
314 if (pulsesrc->probe) {
315 gst_pulseprobe_free (pulsesrc->probe);
316 pulsesrc->probe = NULL;
319 G_OBJECT_CLASS (parent_class)->finalize (object);
322 #define CONTEXT_OK(c) ((c) && PA_CONTEXT_IS_GOOD (pa_context_get_state ((c))))
323 #define STREAM_OK(s) ((s) && PA_STREAM_IS_GOOD (pa_stream_get_state ((s))))
326 gst_pulsesrc_is_dead (GstPulseSrc * pulsesrc, gboolean check_stream)
328 if (!CONTEXT_OK (pulsesrc->context))
331 if (check_stream && !STREAM_OK (pulsesrc->stream))
338 const gchar *err_str = pulsesrc->context ?
339 pa_strerror (pa_context_errno (pulsesrc->context)) : NULL;
340 GST_ELEMENT_ERROR ((pulsesrc), RESOURCE, FAILED, ("Disconnected: %s",
347 gst_pulsesrc_source_info_cb (pa_context * c, const pa_source_info * i, int eol,
350 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
355 g_free (pulsesrc->device_description);
356 pulsesrc->device_description = g_strdup (i->description);
359 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
363 gst_pulsesrc_device_description (GstPulseSrc * pulsesrc)
365 pa_operation *o = NULL;
368 if (!pulsesrc->mainloop)
371 pa_threaded_mainloop_lock (pulsesrc->mainloop);
373 if (!(o = pa_context_get_source_info_by_name (pulsesrc->context,
374 pulsesrc->device, gst_pulsesrc_source_info_cb, pulsesrc))) {
376 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
377 ("pa_stream_get_source_info() failed: %s",
378 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
382 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
384 if (gst_pulsesrc_is_dead (pulsesrc, FALSE))
387 pa_threaded_mainloop_wait (pulsesrc->mainloop);
393 pa_operation_unref (o);
395 t = g_strdup (pulsesrc->device_description);
397 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
403 GST_DEBUG_OBJECT (pulsesrc, "have no mainloop");
409 gst_pulsesrc_set_property (GObject * object,
410 guint prop_id, const GValue * value, GParamSpec * pspec)
413 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
417 g_free (pulsesrc->server);
418 pulsesrc->server = g_value_dup_string (value);
420 gst_pulseprobe_set_server (pulsesrc->probe, pulsesrc->server);
423 g_free (pulsesrc->device);
424 pulsesrc->device = g_value_dup_string (value);
427 g_free (pulsesrc->client_name);
428 if (!g_value_get_string (value)) {
429 GST_WARNING_OBJECT (pulsesrc,
430 "Empty PulseAudio client name not allowed. Resetting to default value");
431 pulsesrc->client_name = gst_pulse_client_name ();
433 pulsesrc->client_name = g_value_dup_string (value);
435 case PROP_STREAM_PROPERTIES:
436 if (pulsesrc->properties)
437 gst_structure_free (pulsesrc->properties);
438 pulsesrc->properties =
439 gst_structure_copy (gst_value_get_structure (value));
440 if (pulsesrc->proplist)
441 pa_proplist_free (pulsesrc->proplist);
442 pulsesrc->proplist = gst_pulse_make_proplist (pulsesrc->properties);
445 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
451 gst_pulsesrc_get_property (GObject * object,
452 guint prop_id, GValue * value, GParamSpec * pspec)
455 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (object);
459 g_value_set_string (value, pulsesrc->server);
462 g_value_set_string (value, pulsesrc->device);
464 case PROP_DEVICE_NAME:
465 g_value_take_string (value, gst_pulsesrc_device_description (pulsesrc));
468 g_value_set_string (value, pulsesrc->client_name);
470 case PROP_STREAM_PROPERTIES:
471 gst_value_set_structure (value, pulsesrc->properties);
474 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
480 gst_pulsesrc_context_state_cb (pa_context * c, void *userdata)
482 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
484 switch (pa_context_get_state (c)) {
485 case PA_CONTEXT_READY:
486 case PA_CONTEXT_TERMINATED:
487 case PA_CONTEXT_FAILED:
488 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
491 case PA_CONTEXT_UNCONNECTED:
492 case PA_CONTEXT_CONNECTING:
493 case PA_CONTEXT_AUTHORIZING:
494 case PA_CONTEXT_SETTING_NAME:
500 gst_pulsesrc_stream_state_cb (pa_stream * s, void *userdata)
502 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
504 switch (pa_stream_get_state (s)) {
506 case PA_STREAM_READY:
507 case PA_STREAM_FAILED:
508 case PA_STREAM_TERMINATED:
509 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
512 case PA_STREAM_UNCONNECTED:
513 case PA_STREAM_CREATING:
519 gst_pulsesrc_stream_request_cb (pa_stream * s, size_t length, void *userdata)
521 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
523 GST_LOG_OBJECT (pulsesrc, "got request for length %" G_GSIZE_FORMAT, length);
525 if (pulsesrc->in_read) {
526 /* only signal when reading */
527 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
532 gst_pulsesrc_stream_latency_update_cb (pa_stream * s, void *userdata)
534 const pa_timing_info *info;
535 pa_usec_t source_usec;
537 info = pa_stream_get_timing_info (s);
540 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
541 "latency update (information unknown)");
544 source_usec = info->configured_source_usec;
546 GST_LOG_OBJECT (GST_PULSESRC_CAST (userdata),
547 "latency_update, %" G_GUINT64_FORMAT ", %d:%" G_GINT64_FORMAT ", %d:%"
548 G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT ", %" G_GUINT64_FORMAT,
549 GST_TIMEVAL_TO_TIME (info->timestamp), info->write_index_corrupt,
550 info->write_index, info->read_index_corrupt, info->read_index,
551 info->source_usec, source_usec);
555 gst_pulsesrc_stream_underflow_cb (pa_stream * s, void *userdata)
557 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got underflow");
561 gst_pulsesrc_stream_overflow_cb (pa_stream * s, void *userdata)
563 GST_WARNING_OBJECT (GST_PULSESRC_CAST (userdata), "Got overflow");
567 gst_pulsesrc_open (GstAudioSrc * asrc)
569 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
571 pa_threaded_mainloop_lock (pulsesrc->mainloop);
573 g_assert (!pulsesrc->context);
574 g_assert (!pulsesrc->stream);
576 GST_DEBUG_OBJECT (pulsesrc, "opening device");
578 if (!(pulsesrc->context =
579 pa_context_new (pa_threaded_mainloop_get_api (pulsesrc->mainloop),
580 pulsesrc->client_name))) {
581 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to create context"),
583 goto unlock_and_fail;
586 pa_context_set_state_callback (pulsesrc->context,
587 gst_pulsesrc_context_state_cb, pulsesrc);
589 GST_DEBUG_OBJECT (pulsesrc, "connect to server %s",
590 GST_STR_NULL (pulsesrc->server));
592 if (pa_context_connect (pulsesrc->context, pulsesrc->server, 0, NULL) < 0) {
593 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
594 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
595 goto unlock_and_fail;
599 pa_context_state_t state;
601 state = pa_context_get_state (pulsesrc->context);
603 if (!PA_CONTEXT_IS_GOOD (state)) {
604 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Failed to connect: %s",
605 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
606 goto unlock_and_fail;
609 if (state == PA_CONTEXT_READY)
612 /* Wait until the context is ready */
613 pa_threaded_mainloop_wait (pulsesrc->mainloop);
615 GST_DEBUG_OBJECT (pulsesrc, "connected");
617 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
624 gst_pulsesrc_destroy_context (pulsesrc);
626 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
633 gst_pulsesrc_close (GstAudioSrc * asrc)
635 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
637 pa_threaded_mainloop_lock (pulsesrc->mainloop);
638 gst_pulsesrc_destroy_context (pulsesrc);
639 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
645 gst_pulsesrc_unprepare (GstAudioSrc * asrc)
647 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
649 pa_threaded_mainloop_lock (pulsesrc->mainloop);
650 gst_pulsesrc_destroy_stream (pulsesrc);
652 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
654 pulsesrc->read_buffer = NULL;
655 pulsesrc->read_buffer_length = 0;
661 gst_pulsesrc_read (GstAudioSrc * asrc, gpointer data, guint length)
663 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
666 pa_threaded_mainloop_lock (pulsesrc->mainloop);
667 pulsesrc->in_read = TRUE;
669 if (pulsesrc->paused)
675 GST_LOG_OBJECT (pulsesrc, "reading %u bytes", length);
677 /*check if we have a leftover buffer */
678 if (!pulsesrc->read_buffer) {
680 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
681 goto unlock_and_fail;
683 /* read all available data, we keep a pointer to the data and the length
684 * and take from it what we need. */
685 if (pa_stream_peek (pulsesrc->stream, &pulsesrc->read_buffer,
686 &pulsesrc->read_buffer_length) < 0)
689 GST_LOG_OBJECT (pulsesrc, "have data of %" G_GSIZE_FORMAT " bytes",
690 pulsesrc->read_buffer_length);
692 /* if we have data, process if */
693 if (pulsesrc->read_buffer && pulsesrc->read_buffer_length)
696 /* now wait for more data to become available */
697 GST_LOG_OBJECT (pulsesrc, "waiting for data");
698 pa_threaded_mainloop_wait (pulsesrc->mainloop);
700 if (pulsesrc->paused)
705 l = pulsesrc->read_buffer_length >
706 length ? length : pulsesrc->read_buffer_length;
708 memcpy (data, pulsesrc->read_buffer, l);
710 pulsesrc->read_buffer = (const guint8 *) pulsesrc->read_buffer + l;
711 pulsesrc->read_buffer_length -= l;
713 data = (guint8 *) data + l;
717 if (pulsesrc->read_buffer_length <= 0) {
718 /* we copied all of the data, drop it now */
719 if (pa_stream_drop (pulsesrc->stream) < 0)
722 /* reset pointer to data */
723 pulsesrc->read_buffer = NULL;
724 pulsesrc->read_buffer_length = 0;
728 pulsesrc->in_read = FALSE;
729 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
736 GST_LOG_OBJECT (pulsesrc, "we are paused");
737 goto unlock_and_fail;
741 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
742 ("pa_stream_peek() failed: %s",
743 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
744 goto unlock_and_fail;
748 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
749 ("pa_stream_drop() failed: %s",
750 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
751 goto unlock_and_fail;
755 pulsesrc->in_read = FALSE;
756 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
762 /* return the delay in samples */
764 gst_pulsesrc_delay (GstAudioSrc * asrc)
766 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
771 pa_threaded_mainloop_lock (pulsesrc->mainloop);
772 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
775 /* get the latency, this can fail when we don't have a latency update yet.
776 * We don't want to wait for latency updates here but we just return 0. */
777 res = pa_stream_get_latency (pulsesrc->stream, &t, &negative);
779 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
782 GST_DEBUG_OBJECT (pulsesrc, "could not get latency");
788 result = (guint) ((t * pulsesrc->sample_spec.rate) / 1000000LL);
795 GST_DEBUG_OBJECT (pulsesrc, "the server is dead");
796 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
802 gst_pulsesrc_create_stream (GstPulseSrc * pulsesrc, GstCaps * caps)
804 pa_channel_map channel_map;
806 gboolean need_channel_layout = FALSE;
807 GstRingBufferSpec spec;
810 memset (&spec, 0, sizeof (GstRingBufferSpec));
811 spec.latency_time = GST_SECOND;
812 if (!gst_ring_buffer_parse_caps (&spec, caps)) {
813 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
814 ("Can't parse caps."), (NULL));
817 /* Keep the refcount of the caps at 1 to make them writable */
818 gst_caps_unref (spec.caps);
820 if (!gst_pulse_fill_sample_spec (&spec, &pulsesrc->sample_spec)) {
821 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, SETTINGS,
822 ("Invalid sample specification."), (NULL));
826 pa_threaded_mainloop_lock (pulsesrc->mainloop);
828 if (!pulsesrc->context) {
829 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Bad context"), (NULL));
830 goto unlock_and_fail;
833 s = gst_caps_get_structure (caps, 0);
834 if (!gst_structure_has_field (s, "channel-layout") ||
835 !gst_pulse_gst_to_channel_map (&channel_map, &spec)) {
836 if (spec.channels == 1)
837 pa_channel_map_init_mono (&channel_map);
838 else if (spec.channels == 2)
839 pa_channel_map_init_stereo (&channel_map);
841 need_channel_layout = TRUE;
844 name = "Record Stream";
845 if (pulsesrc->proplist) {
846 if (!(pulsesrc->stream = pa_stream_new_with_proplist (pulsesrc->context,
847 name, &pulsesrc->sample_spec,
848 (need_channel_layout) ? NULL : &channel_map,
849 pulsesrc->proplist))) {
850 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
851 ("Failed to create stream: %s",
852 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
853 goto unlock_and_fail;
855 } else if (!(pulsesrc->stream = pa_stream_new (pulsesrc->context,
856 name, &pulsesrc->sample_spec,
857 (need_channel_layout) ? NULL : &channel_map))) {
858 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
859 ("Failed to create stream: %s",
860 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
861 goto unlock_and_fail;
864 if (need_channel_layout) {
865 const pa_channel_map *m = pa_stream_get_channel_map (pulsesrc->stream);
867 gst_pulse_channel_map_to_gst (m, &spec);
871 GST_DEBUG_OBJECT (pulsesrc, "Caps are %" GST_PTR_FORMAT, caps);
873 pa_stream_set_state_callback (pulsesrc->stream, gst_pulsesrc_stream_state_cb,
875 pa_stream_set_read_callback (pulsesrc->stream, gst_pulsesrc_stream_request_cb,
877 pa_stream_set_underflow_callback (pulsesrc->stream,
878 gst_pulsesrc_stream_underflow_cb, pulsesrc);
879 pa_stream_set_overflow_callback (pulsesrc->stream,
880 gst_pulsesrc_stream_overflow_cb, pulsesrc);
881 pa_stream_set_latency_update_callback (pulsesrc->stream,
882 gst_pulsesrc_stream_latency_update_cb, pulsesrc);
884 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
889 gst_pulsesrc_destroy_stream (pulsesrc);
891 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
897 /* This is essentially gst_base_src_negotiate_default() but the caps
898 * are guaranteed to have a channel layout for > 2 channels
901 gst_pulsesrc_negotiate (GstBaseSrc * basesrc)
904 GstCaps *caps = NULL;
905 GstCaps *peercaps = NULL;
906 gboolean result = FALSE;
908 /* first see what is possible on our source pad */
909 thiscaps = gst_pad_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
910 GST_DEBUG_OBJECT (basesrc, "caps of src: %" GST_PTR_FORMAT, thiscaps);
911 /* nothing or anything is allowed, we're done */
912 if (thiscaps == NULL || gst_caps_is_any (thiscaps))
915 /* get the peer caps */
916 peercaps = gst_pad_peer_get_caps (GST_BASE_SRC_PAD (basesrc), NULL);
917 GST_DEBUG_OBJECT (basesrc, "caps of peer: %" GST_PTR_FORMAT, peercaps);
919 /* get intersection */
920 caps = gst_caps_intersect (thiscaps, peercaps);
921 GST_DEBUG_OBJECT (basesrc, "intersect: %" GST_PTR_FORMAT, caps);
922 gst_caps_unref (thiscaps);
923 gst_caps_unref (peercaps);
925 /* no peer, work with our own caps then */
929 /* take first (and best, since they are sorted) possibility */
930 caps = gst_caps_make_writable (caps);
931 gst_caps_truncate (caps);
934 if (!gst_caps_is_empty (caps)) {
935 gst_pad_fixate_caps (GST_BASE_SRC_PAD (basesrc), caps);
936 GST_DEBUG_OBJECT (basesrc, "fixated to: %" GST_PTR_FORMAT, caps);
938 if (gst_caps_is_any (caps)) {
939 /* hmm, still anything, so element can do anything and
940 * nego is not needed */
942 } else if (gst_caps_is_fixed (caps)) {
943 /* yay, fixed caps, use those then */
944 result = gst_pulsesrc_create_stream (GST_PULSESRC_CAST (basesrc), caps);
946 result = gst_pad_set_caps (GST_BASE_SRC_PAD (basesrc), caps);
949 gst_caps_unref (caps);
955 GST_DEBUG_OBJECT (basesrc, "no negotiation needed");
957 gst_caps_unref (thiscaps);
963 gst_pulsesrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
965 pa_buffer_attr wanted;
966 const pa_buffer_attr *actual;
967 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
969 pa_threaded_mainloop_lock (pulsesrc->mainloop);
971 wanted.maxlength = -1;
975 wanted.fragsize = spec->segsize;
977 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", wanted.maxlength);
978 GST_INFO_OBJECT (pulsesrc, "tlength: %d", wanted.tlength);
979 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", wanted.prebuf);
980 GST_INFO_OBJECT (pulsesrc, "minreq: %d", wanted.minreq);
981 GST_INFO_OBJECT (pulsesrc, "fragsize: %d", wanted.fragsize);
983 if (pa_stream_connect_record (pulsesrc->stream, pulsesrc->device, &wanted,
984 PA_STREAM_INTERPOLATE_TIMING | PA_STREAM_AUTO_TIMING_UPDATE |
985 PA_STREAM_NOT_MONOTONIC | PA_STREAM_ADJUST_LATENCY |
986 PA_STREAM_START_CORKED) < 0) {
987 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
988 ("Failed to connect stream: %s",
989 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
990 goto unlock_and_fail;
993 pulsesrc->corked = TRUE;
996 pa_stream_state_t state;
998 state = pa_stream_get_state (pulsesrc->stream);
1000 if (!PA_STREAM_IS_GOOD (state)) {
1001 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1002 ("Failed to connect stream: %s",
1003 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1004 goto unlock_and_fail;
1007 if (state == PA_STREAM_READY)
1010 /* Wait until the stream is ready */
1011 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1014 /* get the actual buffering properties now */
1015 actual = pa_stream_get_buffer_attr (pulsesrc->stream);
1017 GST_INFO_OBJECT (pulsesrc, "maxlength: %d", actual->maxlength);
1018 GST_INFO_OBJECT (pulsesrc, "tlength: %d (wanted: %d)",
1019 actual->tlength, wanted.tlength);
1020 GST_INFO_OBJECT (pulsesrc, "prebuf: %d", actual->prebuf);
1021 GST_INFO_OBJECT (pulsesrc, "minreq: %d (wanted %d)", actual->minreq,
1023 GST_INFO_OBJECT (pulsesrc, "fragsize: %d (wanted %d)",
1024 actual->fragsize, wanted.fragsize);
1026 if (actual->fragsize >= wanted.fragsize) {
1027 spec->segsize = actual->fragsize;
1029 spec->segsize = actual->fragsize * (wanted.fragsize / actual->fragsize);
1031 spec->segtotal = actual->maxlength / spec->segsize;
1033 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1039 gst_pulsesrc_destroy_stream (pulsesrc);
1041 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1047 gst_pulsesrc_success_cb (pa_stream * s, int success, void *userdata)
1049 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (userdata);
1051 pulsesrc->operation_success = !!success;
1052 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1056 gst_pulsesrc_reset (GstAudioSrc * asrc)
1058 GstPulseSrc *pulsesrc = GST_PULSESRC_CAST (asrc);
1059 pa_operation *o = NULL;
1061 pa_threaded_mainloop_lock (pulsesrc->mainloop);
1062 GST_DEBUG_OBJECT (pulsesrc, "reset");
1064 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1065 goto unlock_and_fail;
1068 pa_stream_flush (pulsesrc->stream, gst_pulsesrc_success_cb,
1070 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED,
1071 ("pa_stream_flush() failed: %s",
1072 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1073 goto unlock_and_fail;
1076 pulsesrc->paused = TRUE;
1077 /* Inform anyone waiting in _write() call that it shall wakeup */
1078 if (pulsesrc->in_read) {
1079 pa_threaded_mainloop_signal (pulsesrc->mainloop, 0);
1082 pulsesrc->operation_success = FALSE;
1083 while (pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1085 if (gst_pulsesrc_is_dead (pulsesrc, TRUE))
1086 goto unlock_and_fail;
1088 pa_threaded_mainloop_wait (pulsesrc->mainloop);
1091 if (!pulsesrc->operation_success) {
1092 GST_ELEMENT_ERROR (pulsesrc, RESOURCE, FAILED, ("Flush failed: %s",
1093 pa_strerror (pa_context_errno (pulsesrc->context))), (NULL));
1094 goto unlock_and_fail;
1100 pa_operation_cancel (o);
1101 pa_operation_unref (o);
1104 pa_threaded_mainloop_unlock (pulsesrc->mainloop);
1107 /* update the corked state of a stream, must be called with the mainloop
1110 gst_pulsesrc_set_corked (GstPulseSrc * psrc, gboolean corked, gboolean wait)
1112 pa_operation *o = NULL;
1113 gboolean res = FALSE;
1115 GST_DEBUG_OBJECT (psrc, "setting corked state to %d", corked);
1116 if (psrc->corked != corked) {
1117 if (!(o = pa_stream_cork (psrc->stream, corked,
1118 gst_pulsesrc_success_cb, psrc)))
1121 while (wait && pa_operation_get_state (o) == PA_OPERATION_RUNNING) {
1122 pa_threaded_mainloop_wait (psrc->mainloop);
1123 if (gst_pulsesrc_is_dead (psrc, TRUE))
1126 psrc->corked = corked;
1128 GST_DEBUG_OBJECT (psrc, "skipping, already in requested state");
1134 pa_operation_unref (o);
1141 GST_DEBUG_OBJECT (psrc, "the server is dead");
1146 GST_ELEMENT_ERROR (psrc, RESOURCE, FAILED,
1147 ("pa_stream_cork() failed: %s",
1148 pa_strerror (pa_context_errno (psrc->context))), (NULL));
1153 /* start/resume playback ASAP */
1155 gst_pulsesrc_play (GstPulseSrc * psrc)
1157 pa_threaded_mainloop_lock (psrc->mainloop);
1158 GST_DEBUG_OBJECT (psrc, "playing");
1159 psrc->paused = FALSE;
1160 gst_pulsesrc_set_corked (psrc, FALSE, FALSE);
1161 pa_threaded_mainloop_unlock (psrc->mainloop);
1166 /* pause/stop playback ASAP */
1168 gst_pulsesrc_pause (GstPulseSrc * psrc)
1170 pa_threaded_mainloop_lock (psrc->mainloop);
1171 GST_DEBUG_OBJECT (psrc, "pausing");
1172 /* make sure the commit method stops writing */
1173 psrc->paused = TRUE;
1174 if (psrc->in_read) {
1175 /* we are waiting in a read, signal */
1176 GST_DEBUG_OBJECT (psrc, "signal read");
1177 pa_threaded_mainloop_signal (psrc->mainloop, 0);
1179 pa_threaded_mainloop_unlock (psrc->mainloop);
1184 static GstStateChangeReturn
1185 gst_pulsesrc_change_state (GstElement * element, GstStateChange transition)
1187 GstStateChangeReturn ret;
1188 GstPulseSrc *this = GST_PULSESRC_CAST (element);
1190 switch (transition) {
1191 case GST_STATE_CHANGE_NULL_TO_READY:
1192 this->mainloop = pa_threaded_mainloop_new ();
1193 g_assert (this->mainloop);
1195 pa_threaded_mainloop_start (this->mainloop);
1199 gst_pulsemixer_ctrl_new (G_OBJECT (this), this->server,
1200 this->device, GST_PULSEMIXER_SOURCE);
1202 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
1203 /* uncork and start recording */
1204 gst_pulsesrc_play (this);
1206 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1207 /* stop recording ASAP by corking */
1208 pa_threaded_mainloop_lock (this->mainloop);
1209 GST_DEBUG_OBJECT (this, "corking");
1210 gst_pulsesrc_set_corked (this, TRUE, FALSE);
1211 pa_threaded_mainloop_unlock (this->mainloop);
1217 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
1219 switch (transition) {
1220 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
1221 /* now make sure we get out of the _read method */
1222 gst_pulsesrc_pause (this);
1224 case GST_STATE_CHANGE_READY_TO_NULL:
1226 gst_pulsemixer_ctrl_free (this->mixer);
1231 pa_threaded_mainloop_stop (this->mainloop);
1233 gst_pulsesrc_destroy_context (this);
1235 if (this->mainloop) {
1236 pa_threaded_mainloop_free (this->mainloop);
1237 this->mainloop = NULL;