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 /* elementfactory information */
57 static const GstElementDetails gst_alsasink_details =
58 GST_ELEMENT_DETAILS ("Audio sink (ALSA)",
60 "Output to a sound card via ALSA",
61 "Wim Taymans <wim@fluendo.com>");
63 #define DEFAULT_DEVICE "default"
64 #define DEFAULT_DEVICE_NAME ""
65 #define SPDIF_PERIOD_SIZE 1536
66 #define SPDIF_BUFFER_SIZE 15360
75 static void gst_alsasink_init_interfaces (GType type);
77 GST_BOILERPLATE_FULL (GstAlsaSink, gst_alsasink, GstAudioSink,
78 GST_TYPE_AUDIO_SINK, gst_alsasink_init_interfaces);
80 static void gst_alsasink_finalise (GObject * object);
81 static void gst_alsasink_set_property (GObject * object,
82 guint prop_id, const GValue * value, GParamSpec * pspec);
83 static void gst_alsasink_get_property (GObject * object,
84 guint prop_id, GValue * value, GParamSpec * pspec);
86 static GstCaps *gst_alsasink_getcaps (GstBaseSink * bsink);
88 static gboolean gst_alsasink_open (GstAudioSink * asink);
89 static gboolean gst_alsasink_prepare (GstAudioSink * asink,
90 GstRingBufferSpec * spec);
91 static gboolean gst_alsasink_unprepare (GstAudioSink * asink);
92 static gboolean gst_alsasink_close (GstAudioSink * asink);
93 static guint gst_alsasink_write (GstAudioSink * asink, gpointer data,
95 static guint gst_alsasink_delay (GstAudioSink * asink);
96 static void gst_alsasink_reset (GstAudioSink * asink);
98 static gint output_ref; /* 0 */
99 static snd_output_t *output; /* NULL */
100 static GStaticMutex output_mutex = G_STATIC_MUTEX_INIT;
103 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
104 # define ALSA_SINK_FACTORY_ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
106 # define ALSA_SINK_FACTORY_ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
109 static GstStaticPadTemplate alsasink_sink_factory =
110 GST_STATIC_PAD_TEMPLATE ("sink",
113 GST_STATIC_CAPS ("audio/x-raw-int, "
114 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
115 "signed = (boolean) { TRUE, FALSE }, "
118 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
120 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
121 "signed = (boolean) { TRUE, FALSE }, "
124 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
126 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
127 "signed = (boolean) { TRUE, FALSE }, "
130 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
132 "endianness = (int) { " ALSA_SINK_FACTORY_ENDIANNESS " }, "
133 "signed = (boolean) { TRUE, FALSE }, "
136 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
138 "signed = (boolean) { TRUE, FALSE }, "
141 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ];"
146 gst_alsasink_finalise (GObject * object)
148 GstAlsaSink *sink = GST_ALSA_SINK (object);
150 g_free (sink->device);
151 g_mutex_free (sink->alsa_lock);
153 g_static_mutex_lock (&output_mutex);
155 if (output_ref == 0) {
156 snd_output_close (output);
159 g_static_mutex_unlock (&output_mutex);
161 G_OBJECT_CLASS (parent_class)->finalize (object);
165 gst_alsasink_init_interfaces (GType type)
167 gst_alsa_type_add_device_property_probe_interface (type);
171 gst_alsasink_base_init (gpointer g_class)
173 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
175 gst_element_class_set_details (element_class, &gst_alsasink_details);
177 gst_element_class_add_pad_template (element_class,
178 gst_static_pad_template_get (&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_DEBUG_FUNCPTR (gst_alsasink_finalise);
194 gobject_class->get_property = GST_DEBUG_FUNCPTR (gst_alsasink_get_property);
195 gobject_class->set_property = GST_DEBUG_FUNCPTR (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));
219 gst_alsasink_set_property (GObject * object, guint prop_id,
220 const GValue * value, GParamSpec * pspec)
224 sink = GST_ALSA_SINK (object);
228 g_free (sink->device);
229 sink->device = g_value_dup_string (value);
230 /* setting NULL restores the default device */
231 if (sink->device == NULL) {
232 sink->device = g_strdup (DEFAULT_DEVICE);
236 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 gst_alsasink_get_property (GObject * object, guint prop_id,
243 GValue * value, GParamSpec * pspec)
247 sink = GST_ALSA_SINK (object);
251 g_value_set_string (value, sink->device);
253 case PROP_DEVICE_NAME:
254 g_value_take_string (value,
255 gst_alsa_find_device_name (GST_OBJECT_CAST (sink),
256 sink->device, sink->handle, SND_PCM_STREAM_PLAYBACK));
259 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
265 gst_alsasink_init (GstAlsaSink * alsasink, GstAlsaSinkClass * g_class)
267 GST_DEBUG_OBJECT (alsasink, "initializing alsasink");
269 alsasink->device = g_strdup (DEFAULT_DEVICE);
270 alsasink->handle = NULL;
271 alsasink->cached_caps = NULL;
272 alsasink->alsa_lock = g_mutex_new ();
274 g_static_mutex_lock (&output_mutex);
275 if (output_ref == 0) {
276 snd_output_stdio_attach (&output, stdout, 0);
279 g_static_mutex_unlock (&output_mutex);
282 #define CHECK(call, error) \
284 if ((err = call) < 0) \
289 gst_alsasink_getcaps (GstBaseSink * bsink)
291 GstElementClass *element_class;
292 GstPadTemplate *pad_template;
293 GstAlsaSink *sink = GST_ALSA_SINK (bsink);
296 if (sink->handle == NULL) {
297 GST_DEBUG_OBJECT (sink, "device not open, using template caps");
298 return NULL; /* base class will get template caps for us */
301 if (sink->cached_caps) {
302 GST_LOG_OBJECT (sink, "Returning cached caps");
303 return gst_caps_ref (sink->cached_caps);
306 element_class = GST_ELEMENT_GET_CLASS (sink);
307 pad_template = gst_element_class_get_pad_template (element_class, "sink");
308 g_return_val_if_fail (pad_template != NULL, NULL);
310 caps = gst_alsa_probe_supported_formats (GST_OBJECT (sink), sink->handle,
311 gst_pad_template_get_caps (pad_template));
314 sink->cached_caps = gst_caps_ref (caps);
317 GST_INFO_OBJECT (sink, "returning caps %" GST_PTR_FORMAT, caps);
323 set_hwparams (GstAlsaSink * alsa)
327 snd_pcm_hw_params_t *params;
328 guint period_time, buffer_time;
330 snd_pcm_hw_params_malloc (¶ms);
332 GST_DEBUG_OBJECT (alsa, "Negotiating to %d channels @ %d Hz (format = %s) "
333 "SPDIF (%d)", alsa->channels, alsa->rate,
334 snd_pcm_format_name (alsa->format), alsa->iec958);
336 /* start with requested values, if we cannot configure alsa for those values,
337 * we set these values to -1, which will leave the default alsa values */
338 buffer_time = alsa->buffer_time;
339 period_time = alsa->period_time;
342 /* choose all parameters */
343 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
344 /* set the interleaved read/write format */
345 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
347 /* set the sample format */
349 /* Try to use big endian first else fallback to le and swap bytes */
350 if (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format) < 0) {
351 alsa->format = SND_PCM_FORMAT_S16_LE;
352 alsa->need_swap = TRUE;
353 GST_DEBUG_OBJECT (alsa, "falling back to little endian with swapping");
355 alsa->need_swap = FALSE;
358 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
360 /* set the count of channels */
361 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
363 /* set the stream rate */
365 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
367 if (rrate != alsa->rate)
370 /* get and dump some limits */
374 snd_pcm_hw_params_get_buffer_time_min (params, &min, &dir);
375 snd_pcm_hw_params_get_buffer_time_max (params, &max, &dir);
377 GST_DEBUG_OBJECT (alsa, "buffer time %u, min %u, max %u",
378 alsa->buffer_time, min, max);
380 snd_pcm_hw_params_get_period_time_min (params, &min, &dir);
381 snd_pcm_hw_params_get_period_time_max (params, &max, &dir);
383 GST_DEBUG_OBJECT (alsa, "period time %u, min %u, max %u",
384 alsa->period_time, min, max);
386 snd_pcm_hw_params_get_periods_min (params, &min, &dir);
387 snd_pcm_hw_params_get_periods_max (params, &max, &dir);
389 GST_DEBUG_OBJECT (alsa, "periods min %u, max %u", min, max);
392 /* now try to configure the buffer time and period time, if one
393 * of those fail, we fall back to the defaults and emit a warning. */
394 if (buffer_time != -1 && !alsa->iec958) {
395 /* set the buffer time */
396 if ((err = snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
397 &buffer_time, &dir)) < 0) {
398 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
399 ("Unable to set buffer time %i for playback: %s",
400 buffer_time, snd_strerror (err)));
401 /* disable buffer_time the next round */
405 GST_DEBUG_OBJECT (alsa, "buffer time %u", buffer_time);
407 if (period_time != -1 && !alsa->iec958) {
408 /* set the period time */
409 if ((err = snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
410 &period_time, &dir)) < 0) {
411 GST_ELEMENT_WARNING (alsa, RESOURCE, SETTINGS, (NULL),
412 ("Unable to set period time %i for playback: %s",
413 period_time, snd_strerror (err)));
414 /* disable period_time the next round */
418 GST_DEBUG_OBJECT (alsa, "period time %u", period_time);
421 /* Set buffer size and period size manually for SPDIF */
422 if (G_UNLIKELY (alsa->iec958)) {
423 snd_pcm_uframes_t buffer_size = SPDIF_BUFFER_SIZE;
424 snd_pcm_uframes_t period_size = SPDIF_PERIOD_SIZE;
426 CHECK (snd_pcm_hw_params_set_buffer_size_near (alsa->handle, params,
427 &buffer_size), buffer_size);
428 CHECK (snd_pcm_hw_params_set_period_size_near (alsa->handle, params,
429 &period_size, NULL), period_size);
432 /* write the parameters to device */
433 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
435 /* now get the configured values */
436 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
438 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, &dir),
441 GST_DEBUG_OBJECT (alsa, "buffer size %lu, period size %lu", alsa->buffer_size,
444 snd_pcm_hw_params_free (params);
450 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
451 ("Broken configuration for playback: no configurations available: %s",
452 snd_strerror (err)));
453 snd_pcm_hw_params_free (params);
458 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
459 ("Access type not available for playback: %s", snd_strerror (err)));
460 snd_pcm_hw_params_free (params);
465 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
466 ("Sample format not available for playback: %s", snd_strerror (err)));
467 snd_pcm_hw_params_free (params);
474 if ((alsa->channels) == 1)
475 msg = g_strdup (_("Could not open device for playback in mono mode."));
476 if ((alsa->channels) == 2)
477 msg = g_strdup (_("Could not open device for playback in stereo mode."));
478 if ((alsa->channels) > 2)
481 ("Could not open device for playback in %d-channel mode."),
483 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (msg), (snd_strerror (err)));
485 snd_pcm_hw_params_free (params);
490 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
491 ("Rate %iHz not available for playback: %s",
492 alsa->rate, snd_strerror (err)));
497 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
498 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err));
499 snd_pcm_hw_params_free (params);
504 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
505 ("Unable to get buffer size for playback: %s", snd_strerror (err)));
506 snd_pcm_hw_params_free (params);
511 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
512 ("Unable to get period size for playback: %s", snd_strerror (err)));
513 snd_pcm_hw_params_free (params);
518 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
519 ("Unable to set hw params for playback: %s", snd_strerror (err)));
520 snd_pcm_hw_params_free (params);
526 set_swparams (GstAlsaSink * alsa)
529 snd_pcm_sw_params_t *params;
531 snd_pcm_sw_params_malloc (¶ms);
533 /* get the current swparams */
534 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
535 /* start the transfer when the buffer is almost full: */
536 /* (buffer_size / avail_min) * avail_min */
537 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
538 (alsa->buffer_size / alsa->period_size) * alsa->period_size),
541 /* allow the transfer when at least period_size samples can be processed */
542 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
543 alsa->period_size), set_avail);
545 #if GST_CHECK_ALSA_VERSION(1,0,16)
546 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
548 /* align all transfers to 1 sample */
549 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
552 /* write the parameters to the playback device */
553 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
555 snd_pcm_sw_params_free (params);
561 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
562 ("Unable to determine current swparams for playback: %s",
563 snd_strerror (err)));
564 snd_pcm_sw_params_free (params);
569 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
570 ("Unable to set start threshold mode for playback: %s",
571 snd_strerror (err)));
572 snd_pcm_sw_params_free (params);
577 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
578 ("Unable to set avail min for playback: %s", snd_strerror (err)));
579 snd_pcm_sw_params_free (params);
582 #if !GST_CHECK_ALSA_VERSION(1,0,16)
585 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
586 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
587 snd_pcm_sw_params_free (params);
593 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
594 ("Unable to set sw params for playback: %s", snd_strerror (err)));
595 snd_pcm_sw_params_free (params);
601 alsasink_parse_spec (GstAlsaSink * alsa, GstRingBufferSpec * spec)
603 /* Initialize our boolean */
604 alsa->iec958 = FALSE;
606 switch (spec->type) {
607 case GST_BUFTYPE_LINEAR:
608 GST_DEBUG_OBJECT (alsa,
609 "Linear format : depth=%d, width=%d, sign=%d, bigend=%d", spec->depth,
610 spec->width, spec->sign, spec->bigend);
612 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width,
613 spec->sign ? 0 : 1, spec->bigend ? 1 : 0);
615 case GST_BUFTYPE_FLOAT:
616 switch (spec->format) {
618 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
621 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
624 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
627 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
633 case GST_BUFTYPE_A_LAW:
634 alsa->format = SND_PCM_FORMAT_A_LAW;
636 case GST_BUFTYPE_MU_LAW:
637 alsa->format = SND_PCM_FORMAT_MU_LAW;
639 case GST_BUFTYPE_IEC958:
640 alsa->format = SND_PCM_FORMAT_S16_BE;
647 alsa->rate = spec->rate;
648 alsa->channels = spec->channels;
649 alsa->buffer_time = spec->buffer_time;
650 alsa->period_time = spec->latency_time;
651 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
663 gst_alsasink_open (GstAudioSink * asink)
668 alsa = GST_ALSA_SINK (asink);
670 /* open in non-blocking mode, we'll use snd_pcm_wait() for space to become
672 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_PLAYBACK,
673 SND_PCM_NONBLOCK), open_error);
674 GST_LOG_OBJECT (alsa, "Opened device %s", alsa->device);
682 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
683 (_("Could not open audio device for playback. "
684 "Device is being used by another application.")),
685 ("Device '%s' is busy", alsa->device));
687 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE,
688 (_("Could not open audio device for playback.")),
689 ("Playback open error on device '%s': %s", alsa->device,
690 snd_strerror (err)));
697 gst_alsasink_prepare (GstAudioSink * asink, GstRingBufferSpec * spec)
702 alsa = GST_ALSA_SINK (asink);
704 if (spec->format == GST_IEC958) {
705 snd_pcm_close (alsa->handle);
706 alsa->handle = gst_alsa_open_iec958_pcm (GST_OBJECT (alsa));
707 if (G_UNLIKELY (!alsa->handle)) {
712 if (!alsasink_parse_spec (alsa, spec))
715 CHECK (set_hwparams (alsa), hw_params_failed);
716 CHECK (set_swparams (alsa), sw_params_failed);
718 alsa->bytes_per_sample = spec->bytes_per_sample;
719 spec->segsize = alsa->period_size * spec->bytes_per_sample;
720 spec->segtotal = alsa->buffer_size / alsa->period_size;
723 snd_output_t *out_buf = NULL;
726 snd_output_buffer_open (&out_buf);
727 snd_pcm_dump_hw_setup (alsa->handle, out_buf);
728 snd_output_buffer_string (out_buf, &msg);
729 GST_DEBUG_OBJECT (alsa, "Hardware setup: \n%s", msg);
730 snd_output_close (out_buf);
731 snd_output_buffer_open (&out_buf);
732 snd_pcm_dump_sw_setup (alsa->handle, out_buf);
733 snd_output_buffer_string (out_buf, &msg);
734 GST_DEBUG_OBJECT (alsa, "Software setup: \n%s", msg);
735 snd_output_close (out_buf);
743 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_WRITE, (NULL),
744 ("Could not open IEC958 (SPDIF) device for playback"));
749 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
750 ("Error parsing spec"));
755 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
756 ("Setting of hwparams failed: %s", snd_strerror (err)));
761 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
762 ("Setting of swparams failed: %s", snd_strerror (err)));
768 gst_alsasink_unprepare (GstAudioSink * asink)
773 alsa = GST_ALSA_SINK (asink);
775 CHECK (snd_pcm_drop (alsa->handle), drop);
777 CHECK (snd_pcm_hw_free (alsa->handle), hw_free);
784 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
785 ("Could not drop samples: %s", snd_strerror (err)));
790 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
791 ("Could not free hw params: %s", snd_strerror (err)));
797 gst_alsasink_close (GstAudioSink * asink)
799 GstAlsaSink *alsa = GST_ALSA_SINK (asink);
803 CHECK (snd_pcm_close (alsa->handle), close_error);
806 gst_caps_replace (&alsa->cached_caps, NULL);
813 GST_ELEMENT_ERROR (alsa, RESOURCE, CLOSE, (NULL),
814 ("Playback close error: %s", snd_strerror (err)));
821 * Underrun and suspend recovery
824 xrun_recovery (GstAlsaSink * alsa, snd_pcm_t * handle, gint err)
826 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err);
828 if (err == -EPIPE) { /* under-run */
829 err = snd_pcm_prepare (handle);
831 GST_WARNING_OBJECT (alsa,
832 "Can't recovery from underrun, prepare failed: %s",
835 } else if (err == -ESTRPIPE) {
836 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
837 g_usleep (100); /* wait until the suspend flag is released */
840 err = snd_pcm_prepare (handle);
842 GST_WARNING_OBJECT (alsa,
843 "Can't recovery from suspend, prepare failed: %s",
852 gst_alsasink_write (GstAudioSink * asink, gpointer data, guint length)
859 alsa = GST_ALSA_SINK (asink);
861 if (alsa->iec958 && alsa->need_swap) {
864 GST_DEBUG_OBJECT (asink, "swapping bytes");
865 for (i = 0; i < length / 2; i++) {
866 ptr[i] = GUINT16_SWAP_LE_BE (ptr[i]);
870 GST_LOG_OBJECT (asink, "received audio samples buffer of %u bytes", length);
872 cptr = length / alsa->bytes_per_sample;
874 GST_ALSA_SINK_LOCK (asink);
876 /* start by doing a blocking wait for free space. Set the timeout
877 * to 4 times the period time */
878 err = snd_pcm_wait (alsa->handle, (4 * alsa->period_time / 1000));
880 GST_DEBUG_OBJECT (asink, "wait error, %d", err);
882 err = snd_pcm_writei (alsa->handle, ptr, cptr);
885 GST_DEBUG_OBJECT (asink, "written %d frames out of %d", err, cptr);
887 GST_DEBUG_OBJECT (asink, "Write error: %s", snd_strerror (err));
888 if (err == -EAGAIN) {
890 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
896 ptr += snd_pcm_frames_to_bytes (alsa->handle, err);
899 GST_ALSA_SINK_UNLOCK (asink);
901 return length - (cptr * alsa->bytes_per_sample);
905 GST_ALSA_SINK_UNLOCK (asink);
906 return length; /* skip one period */
911 gst_alsasink_delay (GstAudioSink * asink)
914 snd_pcm_sframes_t delay;
917 alsa = GST_ALSA_SINK (asink);
919 res = snd_pcm_delay (alsa->handle, &delay);
920 if (G_UNLIKELY (res < 0)) {
921 /* on errors, report 0 delay */
922 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
925 if (G_UNLIKELY (delay < 0)) {
926 /* make sure we never return a negative delay */
927 GST_WARNING_OBJECT (alsa, "snd_pcm_delay returned negative delay");
935 gst_alsasink_reset (GstAudioSink * asink)
940 alsa = GST_ALSA_SINK (asink);
942 GST_ALSA_SINK_LOCK (asink);
943 GST_DEBUG_OBJECT (alsa, "drop");
944 CHECK (snd_pcm_drop (alsa->handle), drop_error);
945 GST_DEBUG_OBJECT (alsa, "prepare");
946 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
947 GST_DEBUG_OBJECT (alsa, "reset done");
948 GST_ALSA_SINK_UNLOCK (asink);
955 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
957 GST_ALSA_SINK_UNLOCK (asink);
962 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
964 GST_ALSA_SINK_UNLOCK (asink);