2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3 * Copyright (C) 2006 Tim-Philipp Müller <tim centricular net>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public
18 * License along with this library; if not, write to the
19 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
24 * SECTION:element-alsasink
25 * @see_also: alsasrc, alsamixer
27 * This element renders raw audio samples using the ALSA api.
30 * <title>Example pipelines</title>
32 * gst-launch -v filesrc location=sine.ogg ! oggdemux ! vorbisdec ! audioconvert ! audioresample ! alsasink
33 * ]| Play an Ogg/Vorbis file.
36 * Last reviewed on 2006-03-01 (0.10.4)
42 #include <sys/ioctl.h>
48 #include <alsa/asoundlib.h>
51 #include "gstalsasink.h"
52 #include "gstalsadeviceprobe.h"
54 #include <gst/gst-i18n-plugin.h>
55 #include "gst/glib-compat-private.h"
57 #define DEFAULT_DEVICE "default"
58 #define DEFAULT_DEVICE_NAME ""
59 #define DEFAULT_CARD_NAME ""
60 #define SPDIF_PERIOD_SIZE 1536
61 #define SPDIF_BUFFER_SIZE 15360
72 static void gst_alsasink_init_interfaces (GType type);
74 GST_BOILERPLATE_FULL (GstAlsaSink, gst_alsasink, GstAudioSink,
75 GST_TYPE_AUDIO_SINK, gst_alsasink_init_interfaces);
77 static void gst_alsasink_finalise (GObject * object);
78 static void gst_alsasink_set_property (GObject * object,
79 guint prop_id, const GValue * value, GParamSpec * pspec);
80 static void gst_alsasink_get_property (GObject * object,
81 guint prop_id, GValue * value, GParamSpec * pspec);
83 static GstCaps *gst_alsasink_getcaps (GstBaseSink * bsink);
85 static gboolean gst_alsasink_open (GstAudioSink * asink);
86 static gboolean gst_alsasink_prepare (GstAudioSink * asink,
87 GstRingBufferSpec * spec);
88 static gboolean gst_alsasink_unprepare (GstAudioSink * asink);
89 static gboolean gst_alsasink_close (GstAudioSink * asink);
90 static guint gst_alsasink_write (GstAudioSink * asink, gpointer data,
92 static guint gst_alsasink_delay (GstAudioSink * asink);
93 static void gst_alsasink_reset (GstAudioSink * asink);
95 static gint output_ref; /* 0 */
96 static snd_output_t *output; /* NULL */
97 static GStaticMutex output_mutex = G_STATIC_MUTEX_INIT;
100 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
101 # define ALSA_SINK_FACTORY_ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
103 # define ALSA_SINK_FACTORY_ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
106 static GstStaticPadTemplate alsasink_sink_factory =
107 GST_STATIC_PAD_TEMPLATE ("sink",
110 GST_STATIC_CAPS ("audio/x-raw-int, "
111 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
112 "signed = (boolean) { TRUE, FALSE }, "
115 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
117 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
118 "signed = (boolean) { TRUE, FALSE }, "
121 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
123 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
124 "signed = (boolean) { TRUE, FALSE }, "
127 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
129 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
130 "signed = (boolean) { TRUE, FALSE }, "
133 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
135 "signed = (boolean) { TRUE, FALSE }, "
138 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ];"
143 gst_alsasink_finalise (GObject * object)
145 GstAlsaSink *sink = GST_ALSA_SINK (object);
147 g_free (sink->device);
148 g_mutex_free (sink->alsa_lock);
150 g_static_mutex_lock (&output_mutex);
152 if (output_ref == 0) {
153 snd_output_close (output);
156 g_static_mutex_unlock (&output_mutex);
158 G_OBJECT_CLASS (parent_class)->finalize (object);
162 gst_alsasink_init_interfaces (GType type)
164 gst_alsa_type_add_device_property_probe_interface (type);
168 gst_alsasink_base_init (gpointer g_class)
170 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
172 gst_element_class_set_details_simple (element_class,
173 "Audio sink (ALSA)", "Sink/Audio",
174 "Output to a sound card via ALSA", "Wim Taymans <wim@fluendo.com>");
176 gst_element_class_add_static_pad_template (element_class,
177 &alsasink_sink_factory);
181 gst_alsasink_class_init (GstAlsaSinkClass * klass)
183 GObjectClass *gobject_class;
184 GstBaseSinkClass *gstbasesink_class;
185 GstAudioSinkClass *gstaudiosink_class;
187 gobject_class = (GObjectClass *) klass;
188 gstbasesink_class = (GstBaseSinkClass *) klass;
189 gstaudiosink_class = (GstAudioSinkClass *) klass;
191 parent_class = g_type_class_peek_parent (klass);
193 gobject_class->finalize = gst_alsasink_finalise;
194 gobject_class->get_property = gst_alsasink_get_property;
195 gobject_class->set_property = gst_alsasink_set_property;
197 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasink_getcaps);
199 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_alsasink_open);
200 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasink_prepare);
201 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasink_unprepare);
202 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_alsasink_close);
203 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_alsasink_write);
204 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_alsasink_delay);
205 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_alsasink_reset);
207 g_object_class_install_property (gobject_class, PROP_DEVICE,
208 g_param_spec_string ("device", "Device",
209 "ALSA device, as defined in an asound configuration file",
210 DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
212 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
213 g_param_spec_string ("device-name", "Device name",
214 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
215 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
217 g_object_class_install_property (gobject_class, PROP_CARD_NAME,
218 g_param_spec_string ("card-name", "Card name",
219 "Human-readable name of the sound card", DEFAULT_CARD_NAME,
220 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
224 gst_alsasink_set_property (GObject * object, guint prop_id,
225 const GValue * value, GParamSpec * pspec)
229 sink = GST_ALSA_SINK (object);
233 g_free (sink->device);
234 sink->device = g_value_dup_string (value);
235 /* setting NULL restores the default device */
236 if (sink->device == NULL) {
237 sink->device = g_strdup (DEFAULT_DEVICE);
241 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
247 gst_alsasink_get_property (GObject * object, guint prop_id,
248 GValue * value, GParamSpec * pspec)
252 sink = GST_ALSA_SINK (object);
256 g_value_set_string (value, sink->device);
258 case PROP_DEVICE_NAME:
259 g_value_take_string (value,
260 gst_alsa_find_device_name (GST_OBJECT_CAST (sink),
261 sink->device, sink->handle, SND_PCM_STREAM_PLAYBACK));
264 g_value_take_string (value,
265 gst_alsa_find_card_name (GST_OBJECT_CAST (sink),
266 sink->device, SND_PCM_STREAM_PLAYBACK));
269 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
275 gst_alsasink_init (GstAlsaSink * alsasink, GstAlsaSinkClass * g_class)
277 GST_DEBUG_OBJECT (alsasink, "initializing alsasink");
279 alsasink->device = g_strdup (DEFAULT_DEVICE);
280 alsasink->handle = NULL;
281 alsasink->cached_caps = NULL;
282 alsasink->alsa_lock = g_mutex_new ();
284 g_static_mutex_lock (&output_mutex);
285 if (output_ref == 0) {
286 snd_output_stdio_attach (&output, stdout, 0);
289 g_static_mutex_unlock (&output_mutex);
292 #define CHECK(call, error) \
294 if ((err = call) < 0) \
299 gst_alsasink_getcaps (GstBaseSink * bsink)
301 GstElementClass *element_class;
302 GstPadTemplate *pad_template;
303 GstAlsaSink *sink = GST_ALSA_SINK (bsink);
306 if (sink->handle == NULL) {
307 GST_DEBUG_OBJECT (sink, "device not open, using template caps");
308 return NULL; /* base class will get template caps for us */
311 if (sink->cached_caps) {
312 GST_LOG_OBJECT (sink, "Returning cached caps");
313 return gst_caps_ref (sink->cached_caps);
316 element_class = GST_ELEMENT_GET_CLASS (sink);
317 pad_template = gst_element_class_get_pad_template (element_class, "sink");
318 g_return_val_if_fail (pad_template != NULL, NULL);
320 caps = gst_alsa_probe_supported_formats (GST_OBJECT (sink), sink->handle,
321 gst_pad_template_get_caps (pad_template));
324 sink->cached_caps = gst_caps_ref (caps);
327 GST_INFO_OBJECT (sink, "returning caps %" GST_PTR_FORMAT, caps);
333 set_hwparams (GstAlsaSink * alsa)
337 snd_pcm_hw_params_t *params;
338 guint period_time, buffer_time;
340 snd_pcm_hw_params_malloc (¶ms);
342 GST_DEBUG_OBJECT (alsa, "Negotiating to %d channels @ %d Hz (format = %s) "
343 "SPDIF (%d)", alsa->channels, alsa->rate,
344 snd_pcm_format_name (alsa->format), alsa->iec958);
346 /* start with requested values, if we cannot configure alsa for those values,
347 * we set these values to -1, which will leave the default alsa values */
348 buffer_time = alsa->buffer_time;
349 period_time = alsa->period_time;
352 /* choose all parameters */
353 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
354 /* set the interleaved read/write format */
355 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
357 /* set the sample format */
359 /* Try to use big endian first else fallback to le and swap bytes */
360 if (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format) < 0) {
361 alsa->format = SND_PCM_FORMAT_S16_LE;
362 alsa->need_swap = TRUE;
363 GST_DEBUG_OBJECT (alsa, "falling back to little endian with swapping");
365 alsa->need_swap = FALSE;
368 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
370 /* set the count of channels */
371 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
373 /* set the stream rate */
375 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
378 #ifndef GST_DISABLE_GST_DEBUG
379 /* get and dump some limits */
383 snd_pcm_hw_params_get_buffer_time_min (params, &min, NULL);
384 snd_pcm_hw_params_get_buffer_time_max (params, &max, NULL);
386 GST_DEBUG_OBJECT (alsa, "buffer time %u, min %u, max %u",
387 alsa->buffer_time, min, max);
389 snd_pcm_hw_params_get_period_time_min (params, &min, NULL);
390 snd_pcm_hw_params_get_period_time_max (params, &max, NULL);
392 GST_DEBUG_OBJECT (alsa, "period time %u, min %u, max %u",
393 alsa->period_time, min, max);
395 snd_pcm_hw_params_get_periods_min (params, &min, NULL);
396 snd_pcm_hw_params_get_periods_max (params, &max, NULL);
398 GST_DEBUG_OBJECT (alsa, "periods min %u, max %u", min, max);
402 /* now try to configure the buffer time and period time, if one
403 * of those fail, we fall back to the defaults and emit a warning. */
404 if (buffer_time != -1 && !alsa->iec958) {
405 /* set the buffer time */
406 if ((err = snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
407 &buffer_time, NULL)) < 0) {
408 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
409 ("Unable to set buffer time %i for playback: %s",
410 buffer_time, snd_strerror (err)));
411 /* disable buffer_time the next round */
415 GST_DEBUG_OBJECT (alsa, "buffer time %u", buffer_time);
417 if (period_time != -1 && !alsa->iec958) {
418 /* set the period time */
419 if ((err = snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
420 &period_time, NULL)) < 0) {
421 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
422 ("Unable to set period time %i for playback: %s",
423 period_time, snd_strerror (err)));
424 /* disable period_time the next round */
428 GST_DEBUG_OBJECT (alsa, "period time %u", period_time);
431 /* Set buffer size and period size manually for SPDIF */
432 if (G_UNLIKELY (alsa->iec958)) {
433 snd_pcm_uframes_t buffer_size = SPDIF_BUFFER_SIZE;
434 snd_pcm_uframes_t period_size = SPDIF_PERIOD_SIZE;
436 CHECK (snd_pcm_hw_params_set_buffer_size_near (alsa->handle, params,
437 &buffer_size), buffer_size);
438 CHECK (snd_pcm_hw_params_set_period_size_near (alsa->handle, params,
439 &period_size, NULL), period_size);
442 /* write the parameters to device */
443 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
445 /* now get the configured values */
446 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
448 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
451 GST_DEBUG_OBJECT (alsa, "buffer size %lu, period size %lu", alsa->buffer_size,
454 snd_pcm_hw_params_free (params);
460 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
461 ("Broken configuration for playback: no configurations available: %s",
462 snd_strerror (err)));
463 snd_pcm_hw_params_free (params);
468 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
469 ("Access type not available for playback: %s", snd_strerror (err)));
470 snd_pcm_hw_params_free (params);
475 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
476 ("Sample format not available for playback: %s", snd_strerror (err)));
477 snd_pcm_hw_params_free (params);
484 if ((alsa->channels) == 1)
485 msg = g_strdup (_("Could not open device for playback in mono mode."));
486 if ((alsa->channels) == 2)
487 msg = g_strdup (_("Could not open device for playback in stereo mode."));
488 if ((alsa->channels) > 2)
491 ("Could not open device for playback in %d-channel mode."),
493 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, ("%s", msg),
494 ("%s", snd_strerror (err)));
496 snd_pcm_hw_params_free (params);
501 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
502 ("Rate %iHz not available for playback: %s",
503 alsa->rate, snd_strerror (err)));
508 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
509 ("Unable to get buffer size for playback: %s", snd_strerror (err)));
510 snd_pcm_hw_params_free (params);
515 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
516 ("Unable to get period size for playback: %s", snd_strerror (err)));
517 snd_pcm_hw_params_free (params);
522 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
523 ("Unable to set hw params for playback: %s", snd_strerror (err)));
524 snd_pcm_hw_params_free (params);
530 set_swparams (GstAlsaSink * alsa)
533 snd_pcm_sw_params_t *params;
535 snd_pcm_sw_params_malloc (¶ms);
537 /* get the current swparams */
538 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
539 /* start the transfer when the buffer is almost full: */
540 /* (buffer_size / avail_min) * avail_min */
541 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
542 (alsa->buffer_size / alsa->period_size) * alsa->period_size),
545 /* allow the transfer when at least period_size samples can be processed */
546 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
547 alsa->period_size), set_avail);
549 #if GST_CHECK_ALSA_VERSION(1,0,16)
550 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
552 /* align all transfers to 1 sample */
553 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
556 /* write the parameters to the playback device */
557 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
559 snd_pcm_sw_params_free (params);
565 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
566 ("Unable to determine current swparams for playback: %s",
567 snd_strerror (err)));
568 snd_pcm_sw_params_free (params);
573 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
574 ("Unable to set start threshold mode for playback: %s",
575 snd_strerror (err)));
576 snd_pcm_sw_params_free (params);
581 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
582 ("Unable to set avail min for playback: %s", snd_strerror (err)));
583 snd_pcm_sw_params_free (params);
586 #if !GST_CHECK_ALSA_VERSION(1,0,16)
589 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
590 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
591 snd_pcm_sw_params_free (params);
597 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
598 ("Unable to set sw params for playback: %s", snd_strerror (err)));
599 snd_pcm_sw_params_free (params);
605 alsasink_parse_spec (GstAlsaSink * alsa, GstRingBufferSpec * spec)
607 /* Initialize our boolean */
608 alsa->iec958 = FALSE;
610 switch (spec->type) {
611 case GST_BUFTYPE_LINEAR:
612 GST_DEBUG_OBJECT (alsa,
613 "Linear format : depth=%d, width=%d, sign=%d, bigend=%d", spec->depth,
614 spec->width, spec->sign, spec->bigend);
616 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width,
617 spec->sign ? 0 : 1, spec->bigend ? 1 : 0);
619 case GST_BUFTYPE_FLOAT:
620 switch (spec->format) {
622 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
625 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
628 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
631 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
637 case GST_BUFTYPE_A_LAW:
638 alsa->format = SND_PCM_FORMAT_A_LAW;
640 case GST_BUFTYPE_MU_LAW:
641 alsa->format = SND_PCM_FORMAT_MU_LAW;
643 case GST_BUFTYPE_IEC958:
644 alsa->format = SND_PCM_FORMAT_S16_BE;
651 alsa->rate = spec->rate;
652 alsa->channels = spec->channels;
653 alsa->buffer_time = spec->buffer_time;
654 alsa->period_time = spec->latency_time;
655 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
667 gst_alsasink_open (GstAudioSink * asink)
672 alsa = GST_ALSA_SINK (asink);
674 /* open in non-blocking mode, we'll use snd_pcm_wait() for space to become
676 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_PLAYBACK,
677 SND_PCM_NONBLOCK), open_error);
678 GST_LOG_OBJECT (alsa, "Opened device %s", alsa->device);
686 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
687 (_("Could not open audio device for playback. "
688 "Device is being used by another application.")),
689 ("Device '%s' is busy", alsa->device));
691 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE,
692 (_("Could not open audio device for playback.")),
693 ("Playback open error on device '%s': %s", alsa->device,
694 snd_strerror (err)));
701 gst_alsasink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
706 alsa = GST_ALSA_SINK (asink);
708 if (spec->format == GST_IEC958) {
709 snd_pcm_close (alsa->handle);
710 alsa->handle = gst_alsa_open_iec958_pcm (GST_OBJECT (alsa));
711 if (G_UNLIKELY (!alsa->handle)) {
716 if (!alsasink_parse_spec (alsa, spec))
719 CHECK (set_hwparams (alsa), hw_params_failed);
720 CHECK (set_swparams (alsa), sw_params_failed);
722 alsa->bytes_per_sample = spec->bytes_per_sample;
723 spec->segsize = alsa->period_size * spec->bytes_per_sample;
724 spec->segtotal = alsa->buffer_size / alsa->period_size;
727 snd_output_t *out_buf = NULL;
730 snd_output_buffer_open (&out_buf);
731 snd_pcm_dump_hw_setup (alsa->handle, out_buf);
732 snd_output_buffer_string (out_buf, &msg);
733 GST_DEBUG_OBJECT (alsa, "Hardware setup: \n%s", msg);
734 snd_output_close (out_buf);
735 snd_output_buffer_open (&out_buf);
736 snd_pcm_dump_sw_setup (alsa->handle, out_buf);
737 snd_output_buffer_string (out_buf, &msg);
738 GST_DEBUG_OBJECT (alsa, "Software setup: \n%s", msg);
739 snd_output_close (out_buf);
747 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE, (NULL),
748 ("Could not open IEC958 (SPDIF) device for playback"));
753 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
754 ("Error parsing spec"));
759 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
760 ("Setting of hwparams failed: %s", snd_strerror (err)));
765 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
766 ("Setting of swparams failed: %s", snd_strerror (err)));
772 gst_alsasink_unprepare (GstAudioSink * asink)
776 alsa = GST_ALSA_SINK (asink);
778 snd_pcm_drop (alsa->handle);
779 snd_pcm_hw_free (alsa->handle);
785 gst_alsasink_close (GstAudioSink * asink)
787 GstAlsaSink *alsa = GST_ALSA_SINK (asink);
790 snd_pcm_close (alsa->handle);
793 gst_caps_replace (&alsa->cached_caps, NULL);
800 * Underrun and suspend recovery
803 xrun_recovery (GstAlsaSink * alsa, snd_pcm_t * handle, gint err)
805 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err);
807 if (err == -EPIPE) { /* under-run */
808 err = snd_pcm_prepare (handle);
810 GST_WARNING_OBJECT (alsa,
811 "Can't recovery from underrun, prepare failed: %s",
814 } else if (err == -ESTRPIPE) {
815 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
816 g_usleep (100); /* wait until the suspend flag is released */
819 err = snd_pcm_prepare (handle);
821 GST_WARNING_OBJECT (alsa,
822 "Can't recovery from suspend, prepare failed: %s",
831 gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length)
838 alsa = GST_ALSA_SINK (asink);
840 if (alsa->iec958 && alsa->need_swap) {
843 GST_DEBUG_OBJECT (asink, "swapping bytes");
844 for (i = 0; i < length / 2; i++) {
845 ptr[i] = GUINT16_SWAP_LE_BE (ptr[i]);
849 GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length);
851 cptr = length / alsa->bytes_per_sample;
853 GST_ALSA_SINK_LOCK (asink);
855 /* start by doing a blocking wait for free space. Set the timeout
856 * to 4 times the period time */
857 err = snd_pcm_wait (alsa->handle, (4 * alsa->period_time / 1000));
859 GST_DEBUG_OBJECT (asink, "wait error, %d", err);
861 err = snd_pcm_writei (alsa->handle, ptr, cptr);
864 GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr);
866 GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err));
867 if (err == -EAGAIN) {
869 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
875 ptr += snd_pcm_frames_to_bytes (alsa->handle, err);
878 GST_ALSA_SINK_UNLOCK (asink);
880 return length - (cptr * alsa->bytes_per_sample);
884 GST_ALSA_SINK_UNLOCK (asink);
885 return length; /* skip one period */
890 gst_alsasink_delay (GstAudioSink * asink)
893 snd_pcm_sframes_t delay;
896 alsa = GST_ALSA_SINK (asink);
898 res = snd_pcm_delay (alsa->handle, &delay);
899 if (G_UNLIKELY (res < 0)) {
900 /* on errors, report 0 delay */
901 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
904 if (G_UNLIKELY (delay < 0)) {
905 /* make sure we never return a negative delay */
906 GST_WARNING_OBJECT (alsa, "snd_pcm_delay returned negative delay");
914 gst_alsasink_reset (GstAudioSink * asink)
919 alsa = GST_ALSA_SINK (asink);
921 GST_ALSA_SINK_LOCK (asink);
922 GST_DEBUG_OBJECT (alsa, "drop");
923 CHECK (snd_pcm_drop (alsa->handle), drop_error);
924 GST_DEBUG_OBJECT (alsa, "prepare");
925 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
926 GST_DEBUG_OBJECT (alsa, "reset done");
927 GST_ALSA_SINK_UNLOCK (asink);
934 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
936 GST_ALSA_SINK_UNLOCK (asink);
941 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
943 GST_ALSA_SINK_UNLOCK (asink);