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>
56 #define DEFAULT_DEVICE "default"
57 #define DEFAULT_DEVICE_NAME ""
58 #define DEFAULT_CARD_NAME ""
59 #define SPDIF_PERIOD_SIZE 1536
60 #define SPDIF_BUFFER_SIZE 15360
71 static void gst_alsasink_init_interfaces (GType type);
73 GST_BOILERPLATE_FULL (GstAlsaSink, gst_alsasink, GstAudioSink,
74 GST_TYPE_AUDIO_SINK, gst_alsasink_init_interfaces);
76 static void gst_alsasink_finalise (GObject * object);
77 static void gst_alsasink_set_property (GObject * object,
78 guint prop_id, const GValue * value, GParamSpec * pspec);
79 static void gst_alsasink_get_property (GObject * object,
80 guint prop_id, GValue * value, GParamSpec * pspec);
82 static GstCaps *gst_alsasink_getcaps (GstBaseSink * bsink);
84 static gboolean gst_alsasink_open (GstAudioSink * asink);
85 static gboolean gst_alsasink_prepare (GstAudioSink * asink,
86 GstRingBufferSpec * spec);
87 static gboolean gst_alsasink_unprepare (GstAudioSink * asink);
88 static gboolean gst_alsasink_close (GstAudioSink * asink);
89 static guint gst_alsasink_write (GstAudioSink * asink, gpointer data,
91 static guint gst_alsasink_delay (GstAudioSink * asink);
92 static void gst_alsasink_reset (GstAudioSink * asink);
94 static gint output_ref; /* 0 */
95 static snd_output_t *output; /* NULL */
96 static GStaticMutex output_mutex = G_STATIC_MUTEX_INIT;
99 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
100 # define ALSA_SINK_FACTORY_ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
102 # define ALSA_SINK_FACTORY_ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
105 static GstStaticPadTemplate alsasink_sink_factory =
106 GST_STATIC_PAD_TEMPLATE ("sink",
109 GST_STATIC_CAPS ("audio/x-raw-int, "
110 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
111 "signed = (boolean) { TRUE, FALSE }, "
114 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
116 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
117 "signed = (boolean) { TRUE, FALSE }, "
120 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
122 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
123 "signed = (boolean) { TRUE, FALSE }, "
126 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
128 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
129 "signed = (boolean) { TRUE, FALSE }, "
132 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
134 "signed = (boolean) { TRUE, FALSE }, "
137 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ];"
142 gst_alsasink_finalise (GObject * object)
144 GstAlsaSink *sink = GST_ALSA_SINK (object);
146 g_free (sink->device);
147 g_mutex_free (sink->alsa_lock);
149 g_static_mutex_lock (&output_mutex);
151 if (output_ref == 0) {
152 snd_output_close (output);
155 g_static_mutex_unlock (&output_mutex);
157 G_OBJECT_CLASS (parent_class)->finalize (object);
161 gst_alsasink_init_interfaces (GType type)
163 gst_alsa_type_add_device_property_probe_interface (type);
167 gst_alsasink_base_init (gpointer g_class)
169 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
171 gst_element_class_set_details_simple (element_class,
172 "Audio sink (ALSA)", "Sink/Audio",
173 "Output to a sound card via ALSA", "Wim Taymans <wim@fluendo.com>");
175 gst_element_class_add_pad_template (element_class,
176 gst_static_pad_template_get (&alsasink_sink_factory));
180 gst_alsasink_class_init (GstAlsaSinkClass * klass)
182 GObjectClass *gobject_class;
183 GstBaseSinkClass *gstbasesink_class;
184 GstAudioSinkClass *gstaudiosink_class;
186 gobject_class = (GObjectClass *) klass;
187 gstbasesink_class = (GstBaseSinkClass *) klass;
188 gstaudiosink_class = (GstAudioSinkClass *) klass;
190 parent_class = g_type_class_peek_parent (klass);
192 gobject_class->finalize = gst_alsasink_finalise;
193 gobject_class->get_property = gst_alsasink_get_property;
194 gobject_class->set_property = gst_alsasink_set_property;
196 gstbasesink_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasink_getcaps);
198 gstaudiosink_class->open = GST_DEBUG_FUNCPTR (gst_alsasink_open);
199 gstaudiosink_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasink_prepare);
200 gstaudiosink_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasink_unprepare);
201 gstaudiosink_class->close = GST_DEBUG_FUNCPTR (gst_alsasink_close);
202 gstaudiosink_class->write = GST_DEBUG_FUNCPTR (gst_alsasink_write);
203 gstaudiosink_class->delay = GST_DEBUG_FUNCPTR (gst_alsasink_delay);
204 gstaudiosink_class->reset = GST_DEBUG_FUNCPTR (gst_alsasink_reset);
206 g_object_class_install_property (gobject_class, PROP_DEVICE,
207 g_param_spec_string ("device", "Device",
208 "ALSA device, as defined in an asound configuration file",
209 DEFAULT_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
211 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
212 g_param_spec_string ("device-name", "Device name",
213 "Human-readable name of the sound device", DEFAULT_DEVICE_NAME,
214 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
216 g_object_class_install_property (gobject_class, PROP_CARD_NAME,
217 g_param_spec_string ("card-name", "Card name",
218 "Human-readable name of the sound card", DEFAULT_CARD_NAME,
219 G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
223 gst_alsasink_set_property (GObject * object, guint prop_id,
224 const GValue * value, GParamSpec * pspec)
228 sink = GST_ALSA_SINK (object);
232 g_free (sink->device);
233 sink->device = g_value_dup_string (value);
234 /* setting NULL restores the default device */
235 if (sink->device == NULL) {
236 sink->device = g_strdup (DEFAULT_DEVICE);
240 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
246 gst_alsasink_get_property (GObject * object, guint prop_id,
247 GValue * value, GParamSpec * pspec)
251 sink = GST_ALSA_SINK (object);
255 g_value_set_string (value, sink->device);
257 case PROP_DEVICE_NAME:
258 g_value_take_string (value,
259 gst_alsa_find_device_name (GST_OBJECT_CAST (sink),
260 sink->device, sink->handle, SND_PCM_STREAM_PLAYBACK));
263 g_value_take_string (value,
264 gst_alsa_find_card_name (GST_OBJECT_CAST (sink),
265 sink->device, SND_PCM_STREAM_PLAYBACK));
268 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
274 gst_alsasink_init (GstAlsaSink * alsasink, GstAlsaSinkClass * g_class)
276 GST_DEBUG_OBJECT (alsasink, "initializing alsasink");
278 alsasink->device = g_strdup (DEFAULT_DEVICE);
279 alsasink->handle = NULL;
280 alsasink->cached_caps = NULL;
281 alsasink->alsa_lock = g_mutex_new ();
283 g_static_mutex_lock (&output_mutex);
284 if (output_ref == 0) {
285 snd_output_stdio_attach (&output, stdout, 0);
288 g_static_mutex_unlock (&output_mutex);
291 #define CHECK(call, error) \
293 if ((err = call) < 0) \
298 gst_alsasink_getcaps (GstBaseSink * bsink)
300 GstElementClass *element_class;
301 GstPadTemplate *pad_template;
302 GstAlsaSink *sink = GST_ALSA_SINK (bsink);
305 if (sink->handle == NULL) {
306 GST_DEBUG_OBJECT (sink, "device not open, using template caps");
307 return NULL; /* base class will get template caps for us */
310 if (sink->cached_caps) {
311 GST_LOG_OBJECT (sink, "Returning cached caps");
312 return gst_caps_ref (sink->cached_caps);
315 element_class = GST_ELEMENT_GET_CLASS (sink);
316 pad_template = gst_element_class_get_pad_template (element_class, "sink");
317 g_return_val_if_fail (pad_template != NULL, NULL);
319 caps = gst_alsa_probe_supported_formats (GST_OBJECT (sink), sink->handle,
320 gst_pad_template_get_caps (pad_template));
323 sink->cached_caps = gst_caps_ref (caps);
326 GST_INFO_OBJECT (sink, "returning caps %" GST_PTR_FORMAT, caps);
332 set_hwparams (GstAlsaSink * alsa)
336 snd_pcm_hw_params_t *params;
337 guint period_time, buffer_time;
339 snd_pcm_hw_params_malloc (¶ms);
341 GST_DEBUG_OBJECT (alsa, "Negotiating to %d channels @ %d Hz (format = %s) "
342 "SPDIF (%d)", alsa->channels, alsa->rate,
343 snd_pcm_format_name (alsa->format), alsa->iec958);
345 /* start with requested values, if we cannot configure alsa for those values,
346 * we set these values to -1, which will leave the default alsa values */
347 buffer_time = alsa->buffer_time;
348 period_time = alsa->period_time;
351 /* choose all parameters */
352 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
353 /* set the interleaved read/write format */
354 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
356 /* set the sample format */
358 /* Try to use big endian first else fallback to le and swap bytes */
359 if (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format) < 0) {
360 alsa->format = SND_PCM_FORMAT_S16_LE;
361 alsa->need_swap = TRUE;
362 GST_DEBUG_OBJECT (alsa, "falling back to little endian with swapping");
364 alsa->need_swap = FALSE;
367 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
369 /* set the count of channels */
370 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
372 /* set the stream rate */
374 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
376 if (rrate != alsa->rate)
379 #ifndef GST_DISABLE_GST_DEBUG
380 /* get and dump some limits */
384 snd_pcm_hw_params_get_buffer_time_min (params, &min, NULL);
385 snd_pcm_hw_params_get_buffer_time_max (params, &max, NULL);
387 GST_DEBUG_OBJECT (alsa, "buffer time %u, min %u, max %u",
388 alsa->buffer_time, min, max);
390 snd_pcm_hw_params_get_period_time_min (params, &min, NULL);
391 snd_pcm_hw_params_get_period_time_max (params, &max, NULL);
393 GST_DEBUG_OBJECT (alsa, "period time %u, min %u, max %u",
394 alsa->period_time, min, max);
396 snd_pcm_hw_params_get_periods_min (params, &min, NULL);
397 snd_pcm_hw_params_get_periods_max (params, &max, NULL);
399 GST_DEBUG_OBJECT (alsa, "periods min %u, max %u", min, max);
403 /* now try to configure the buffer time and period time, if one
404 * of those fail, we fall back to the defaults and emit a warning. */
405 if (buffer_time != -1 && !alsa->iec958) {
406 /* set the buffer time */
407 if ((err = snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
408 &buffer_time, NULL)) < 0) {
409 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
410 ("Unable to set buffer time %i for playback: %s",
411 buffer_time, snd_strerror (err)));
412 /* disable buffer_time the next round */
416 GST_DEBUG_OBJECT (alsa, "buffer time %u", buffer_time);
418 if (period_time != -1 && !alsa->iec958) {
419 /* set the period time */
420 if ((err = snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
421 &period_time, NULL)) < 0) {
422 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
423 ("Unable to set period time %i for playback: %s",
424 period_time, snd_strerror (err)));
425 /* disable period_time the next round */
429 GST_DEBUG_OBJECT (alsa, "period time %u", period_time);
432 /* Set buffer size and period size manually for SPDIF */
433 if (G_UNLIKELY (alsa->iec958)) {
434 snd_pcm_uframes_t buffer_size = SPDIF_BUFFER_SIZE;
435 snd_pcm_uframes_t period_size = SPDIF_PERIOD_SIZE;
437 CHECK (snd_pcm_hw_params_set_buffer_size_near (alsa->handle, params,
438 &buffer_size), buffer_size);
439 CHECK (snd_pcm_hw_params_set_period_size_near (alsa->handle, params,
440 &period_size, NULL), period_size);
443 /* write the parameters to device */
444 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
446 /* now get the configured values */
447 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
449 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
452 GST_DEBUG_OBJECT (alsa, "buffer size %lu, period size %lu", alsa->buffer_size,
455 snd_pcm_hw_params_free (params);
461 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
462 ("Broken configuration for playback: no configurations available: %s",
463 snd_strerror (err)));
464 snd_pcm_hw_params_free (params);
469 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
470 ("Access type not available for playback: %s", snd_strerror (err)));
471 snd_pcm_hw_params_free (params);
476 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
477 ("Sample format not available for playback: %s", snd_strerror (err)));
478 snd_pcm_hw_params_free (params);
485 if ((alsa->channels) == 1)
486 msg = g_strdup (_("Could not open device for playback in mono mode."));
487 if ((alsa->channels) == 2)
488 msg = g_strdup (_("Could not open device for playback in stereo mode."));
489 if ((alsa->channels) > 2)
492 ("Could not open device for playback in %d-channel mode."),
494 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, ("%s", msg),
495 ("%s", snd_strerror (err)));
497 snd_pcm_hw_params_free (params);
502 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
503 ("Rate %iHz not available for playback: %s",
504 alsa->rate, snd_strerror (err)));
509 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
510 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err));
511 snd_pcm_hw_params_free (params);
516 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
517 ("Unable to get buffer size for playback: %s", snd_strerror (err)));
518 snd_pcm_hw_params_free (params);
523 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
524 ("Unable to get period size for playback: %s", snd_strerror (err)));
525 snd_pcm_hw_params_free (params);
530 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
531 ("Unable to set hw params for playback: %s", snd_strerror (err)));
532 snd_pcm_hw_params_free (params);
538 set_swparams (GstAlsaSink * alsa)
541 snd_pcm_sw_params_t *params;
543 snd_pcm_sw_params_malloc (¶ms);
545 /* get the current swparams */
546 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
547 /* start the transfer when the buffer is almost full: */
548 /* (buffer_size / avail_min) * avail_min */
549 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
550 (alsa->buffer_size / alsa->period_size) * alsa->period_size),
553 /* allow the transfer when at least period_size samples can be processed */
554 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
555 alsa->period_size), set_avail);
557 #if GST_CHECK_ALSA_VERSION(1,0,16)
558 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
560 /* align all transfers to 1 sample */
561 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
564 /* write the parameters to the playback device */
565 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
567 snd_pcm_sw_params_free (params);
573 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
574 ("Unable to determine current swparams 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 start threshold mode for playback: %s",
583 snd_strerror (err)));
584 snd_pcm_sw_params_free (params);
589 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
590 ("Unable to set avail min for playback: %s", snd_strerror (err)));
591 snd_pcm_sw_params_free (params);
594 #if !GST_CHECK_ALSA_VERSION(1,0,16)
597 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
598 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
599 snd_pcm_sw_params_free (params);
605 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
606 ("Unable to set sw params for playback: %s", snd_strerror (err)));
607 snd_pcm_sw_params_free (params);
613 alsasink_parse_spec (GstAlsaSink * alsa, GstRingBufferSpec * spec)
615 /* Initialize our boolean */
616 alsa->iec958 = FALSE;
618 switch (spec->type) {
619 case GST_BUFTYPE_LINEAR:
620 GST_DEBUG_OBJECT (alsa,
621 "Linear format : depth=%d, width=%d, sign=%d, bigend=%d", spec->depth,
622 spec->width, spec->sign, spec->bigend);
624 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width,
625 spec->sign ? 0 : 1, spec->bigend ? 1 : 0);
627 case GST_BUFTYPE_FLOAT:
628 switch (spec->format) {
630 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
633 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
636 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
639 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
645 case GST_BUFTYPE_A_LAW:
646 alsa->format = SND_PCM_FORMAT_A_LAW;
648 case GST_BUFTYPE_MU_LAW:
649 alsa->format = SND_PCM_FORMAT_MU_LAW;
651 case GST_BUFTYPE_IEC958:
652 alsa->format = SND_PCM_FORMAT_S16_BE;
659 alsa->rate = spec->rate;
660 alsa->channels = spec->channels;
661 alsa->buffer_time = spec->buffer_time;
662 alsa->period_time = spec->latency_time;
663 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
675 gst_alsasink_open (GstAudioSink * asink)
680 alsa = GST_ALSA_SINK (asink);
682 /* open in non-blocking mode, we'll use snd_pcm_wait() for space to become
684 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_PLAYBACK,
685 SND_PCM_NONBLOCK), open_error);
686 GST_LOG_OBJECT (alsa, "Opened device %s", alsa->device);
694 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
695 (_("Could not open audio device for playback. "
696 "Device is being used by another application.")),
697 ("Device '%s' is busy", alsa->device));
699 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE,
700 (_("Could not open audio device for playback.")),
701 ("Playback open error on device '%s': %s", alsa->device,
702 snd_strerror (err)));
709 gst_alsasink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
714 alsa = GST_ALSA_SINK (asink);
716 if (spec->format == GST_IEC958) {
717 snd_pcm_close (alsa->handle);
718 alsa->handle = gst_alsa_open_iec958_pcm (GST_OBJECT (alsa));
719 if (G_UNLIKELY (!alsa->handle)) {
724 if (!alsasink_parse_spec (alsa, spec))
727 CHECK (set_hwparams (alsa), hw_params_failed);
728 CHECK (set_swparams (alsa), sw_params_failed);
730 alsa->bytes_per_sample = spec->bytes_per_sample;
731 spec->segsize = alsa->period_size * spec->bytes_per_sample;
732 spec->segtotal = alsa->buffer_size / alsa->period_size;
735 snd_output_t *out_buf = NULL;
738 snd_output_buffer_open (&out_buf);
739 snd_pcm_dump_hw_setup (alsa->handle, out_buf);
740 snd_output_buffer_string (out_buf, &msg);
741 GST_DEBUG_OBJECT (alsa, "Hardware setup: \n%s", msg);
742 snd_output_close (out_buf);
743 snd_output_buffer_open (&out_buf);
744 snd_pcm_dump_sw_setup (alsa->handle, out_buf);
745 snd_output_buffer_string (out_buf, &msg);
746 GST_DEBUG_OBJECT (alsa, "Software setup: \n%s", msg);
747 snd_output_close (out_buf);
755 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE, (NULL),
756 ("Could not open IEC958 (SPDIF) device for playback"));
761 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
762 ("Error parsing spec"));
767 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
768 ("Setting of hwparams failed: %s", snd_strerror (err)));
773 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
774 ("Setting of swparams failed: %s", snd_strerror (err)));
780 gst_alsasink_unprepare (GstAudioSink * asink)
784 alsa = GST_ALSA_SINK (asink);
786 snd_pcm_drop (alsa->handle);
787 snd_pcm_hw_free (alsa->handle);
793 gst_alsasink_close (GstAudioSink * asink)
795 GstAlsaSink *alsa = GST_ALSA_SINK (asink);
798 snd_pcm_close (alsa->handle);
801 gst_caps_replace (&alsa->cached_caps, NULL);
808 * Underrun and suspend recovery
811 xrun_recovery (GstAlsaSink * alsa, snd_pcm_t * handle, gint err)
813 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err);
815 if (err == -EPIPE) { /* under-run */
816 err = snd_pcm_prepare (handle);
818 GST_WARNING_OBJECT (alsa,
819 "Can't recovery from underrun, prepare failed: %s",
822 } else if (err == -ESTRPIPE) {
823 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
824 g_usleep (100); /* wait until the suspend flag is released */
827 err = snd_pcm_prepare (handle);
829 GST_WARNING_OBJECT (alsa,
830 "Can't recovery from suspend, prepare failed: %s",
839 gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length)
846 alsa = GST_ALSA_SINK (asink);
848 if (alsa->iec958 && alsa->need_swap) {
851 GST_DEBUG_OBJECT (asink, "swapping bytes");
852 for (i = 0; i < length / 2; i++) {
853 ptr[i] = GUINT16_SWAP_LE_BE (ptr[i]);
857 GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length);
859 cptr = length / alsa->bytes_per_sample;
861 GST_ALSA_SINK_LOCK (asink);
863 /* start by doing a blocking wait for free space. Set the timeout
864 * to 4 times the period time */
865 err = snd_pcm_wait (alsa->handle, (4 * alsa->period_time / 1000));
867 GST_DEBUG_OBJECT (asink, "wait error, %d", err);
869 err = snd_pcm_writei (alsa->handle, ptr, cptr);
872 GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr);
874 GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err));
875 if (err == -EAGAIN) {
877 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
883 ptr += snd_pcm_frames_to_bytes (alsa->handle, err);
886 GST_ALSA_SINK_UNLOCK (asink);
888 return length - (cptr * alsa->bytes_per_sample);
892 GST_ALSA_SINK_UNLOCK (asink);
893 return length; /* skip one period */
898 gst_alsasink_delay (GstAudioSink * asink)
901 snd_pcm_sframes_t delay;
904 alsa = GST_ALSA_SINK (asink);
906 res = snd_pcm_delay (alsa->handle, &delay);
907 if (G_UNLIKELY (res < 0)) {
908 /* on errors, report 0 delay */
909 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
912 if (G_UNLIKELY (delay < 0)) {
913 /* make sure we never return a negative delay */
914 GST_WARNING_OBJECT (alsa, "snd_pcm_delay returned negative delay");
922 gst_alsasink_reset (GstAudioSink * asink)
927 alsa = GST_ALSA_SINK (asink);
929 GST_ALSA_SINK_LOCK (asink);
930 GST_DEBUG_OBJECT (alsa, "drop");
931 CHECK (snd_pcm_drop (alsa->handle), drop_error);
932 GST_DEBUG_OBJECT (alsa, "prepare");
933 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
934 GST_DEBUG_OBJECT (alsa, "reset done");
935 GST_ALSA_SINK_UNLOCK (asink);
942 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
944 GST_ALSA_SINK_UNLOCK (asink);
949 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
951 GST_ALSA_SINK_UNLOCK (asink);