2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Library General Public License for more details.
16 * You should have received a copy of the GNU Library General Public
17 * License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
23 * SECTION:element-alsasrc
24 * @see_also: alsasink, alsamixer
26 * This element reads data from an audio card using the ALSA API.
29 * <title>Example pipelines</title>
31 * gst-launch -v alsasrc ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg
32 * ]| Record from a sound card using ALSA and encode to Ogg/Vorbis.
35 * Last reviewed on 2006-03-01 (0.10.4)
41 #include <sys/ioctl.h>
47 #include <alsa/asoundlib.h>
49 #include "gstalsasrc.h"
50 #include "gstalsadeviceprobe.h"
52 #include <gst/gst-i18n-plugin.h>
54 #define DEFAULT_PROP_DEVICE "default"
55 #define DEFAULT_PROP_DEVICE_NAME ""
56 #define DEFAULT_PROP_CARD_NAME ""
67 static void gst_alsasrc_init_interfaces (GType type);
68 #define gst_alsasrc_parent_class parent_class
69 G_DEFINE_TYPE_WITH_CODE (GstAlsaSrc, gst_alsasrc,
70 GST_TYPE_AUDIO_SRC, gst_alsasrc_init_interfaces (g_define_type_id));
72 GST_IMPLEMENT_ALSA_MIXER_METHODS (GstAlsaSrc, gst_alsasrc_mixer);
74 static void gst_alsasrc_finalize (GObject * object);
75 static void gst_alsasrc_set_property (GObject * object,
76 guint prop_id, const GValue * value, GParamSpec * pspec);
77 static void gst_alsasrc_get_property (GObject * object,
78 guint prop_id, GValue * value, GParamSpec * pspec);
80 static GstCaps *gst_alsasrc_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
82 static gboolean gst_alsasrc_open (GstAudioSrc * asrc);
83 static gboolean gst_alsasrc_prepare (GstAudioSrc * asrc,
84 GstRingBufferSpec * spec);
85 static gboolean gst_alsasrc_unprepare (GstAudioSrc * asrc);
86 static gboolean gst_alsasrc_close (GstAudioSrc * asrc);
87 static guint gst_alsasrc_read (GstAudioSrc * asrc, gpointer data, guint length);
88 static guint gst_alsasrc_delay (GstAudioSrc * asrc);
89 static void gst_alsasrc_reset (GstAudioSrc * asrc);
90 static GstStateChangeReturn gst_alsasrc_change_state (GstElement * element,
91 GstStateChange transition);
92 static GstFlowReturn gst_alsasrc_create (GstBaseSrc * bsrc, guint64 offset,
93 guint length, GstBuffer ** outbuf);
94 static GstClockTime gst_alsasrc_get_timestamp (GstAlsaSrc * src);
97 /* AlsaSrc signals and args */
103 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
104 # define ALSA_SRC_FACTORY_ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
106 # define ALSA_SRC_FACTORY_ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
109 static GstStaticPadTemplate alsasrc_src_factory =
110 GST_STATIC_PAD_TEMPLATE ("src",
113 GST_STATIC_CAPS ("audio/x-raw-int, "
114 "endianness = (int) { " ALSA_SRC_FACTORY_ENDIANNESS " }, "
115 "signed = (boolean) { TRUE, FALSE }, "
118 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
120 "endianness = (int) { " ALSA_SRC_FACTORY_ENDIANNESS " }, "
121 "signed = (boolean) { TRUE, FALSE }, "
124 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
126 "endianness = (int) { " ALSA_SRC_FACTORY_ENDIANNESS " }, "
127 "signed = (boolean) { TRUE, FALSE }, "
130 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]; "
132 "endianness = (int) { " ALSA_SRC_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 ]")
145 gst_alsasrc_finalize (GObject * object)
147 GstAlsaSrc *src = GST_ALSA_SRC (object);
149 g_free (src->device);
150 g_mutex_free (src->alsa_lock);
152 G_OBJECT_CLASS (parent_class)->finalize (object);
156 gst_alsasrc_interface_supported (GstAlsaSrc * this, GType interface_type)
158 /* only support this one interface (wrapped by GstImplementsInterface) */
159 g_assert (interface_type == GST_TYPE_MIXER);
161 return gst_alsasrc_mixer_supported (this, interface_type);
165 gst_implements_interface_init (GstImplementsInterfaceClass * klass)
167 klass->supported = (gpointer) gst_alsasrc_interface_supported;
171 gst_alsasrc_init_interfaces (GType type)
173 static const GInterfaceInfo implements_iface_info = {
174 (GInterfaceInitFunc) gst_implements_interface_init,
178 static const GInterfaceInfo mixer_iface_info = {
179 (GInterfaceInitFunc) gst_alsasrc_mixer_interface_init,
184 g_type_add_interface_static (type, GST_TYPE_IMPLEMENTS_INTERFACE,
185 &implements_iface_info);
186 g_type_add_interface_static (type, GST_TYPE_MIXER, &mixer_iface_info);
188 gst_alsa_type_add_device_property_probe_interface (type);
192 gst_alsasrc_class_init (GstAlsaSrcClass * klass)
194 GObjectClass *gobject_class;
195 GstElementClass *gstelement_class;
196 GstBaseSrcClass *gstbasesrc_class;
197 GstAudioSrcClass *gstaudiosrc_class;
199 gobject_class = (GObjectClass *) klass;
200 gstelement_class = (GstElementClass *) klass;
201 gstbasesrc_class = (GstBaseSrcClass *) klass;
202 gstaudiosrc_class = (GstAudioSrcClass *) klass;
204 gobject_class->finalize = gst_alsasrc_finalize;
205 gobject_class->get_property = gst_alsasrc_get_property;
206 gobject_class->set_property = gst_alsasrc_set_property;
208 gst_element_class_set_details_simple (gstelement_class,
209 "Audio source (ALSA)", "Source/Audio",
210 "Read from a sound card via ALSA", "Wim Taymans <wim@fluendo.com>");
212 gst_element_class_add_pad_template (gstelement_class,
213 gst_static_pad_template_get (&alsasrc_src_factory));
215 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_alsasrc_change_state);
217 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasrc_getcaps);
218 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_alsasrc_create);
220 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_alsasrc_open);
221 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasrc_prepare);
222 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasrc_unprepare);
223 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_alsasrc_close);
224 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_alsasrc_read);
225 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_alsasrc_delay);
226 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_alsasrc_reset);
228 g_object_class_install_property (gobject_class, PROP_DEVICE,
229 g_param_spec_string ("device", "Device",
230 "ALSA device, as defined in an asound configuration file",
231 DEFAULT_PROP_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
233 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
234 g_param_spec_string ("device-name", "Device name",
235 "Human-readable name of the sound device",
236 DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
238 g_object_class_install_property (gobject_class, PROP_CARD_NAME,
239 g_param_spec_string ("card-name", "Card name",
240 "Human-readable name of the sound card",
241 DEFAULT_PROP_CARD_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
245 gst_alsasrc_get_timestamp (GstAlsaSrc * src)
247 snd_pcm_status_t *status;
248 snd_htimestamp_t tstamp;
249 GstClockTime timestamp;
250 snd_pcm_uframes_t availmax;
252 GST_DEBUG_OBJECT (src, "Getting alsa timestamp!");
255 GST_ERROR_OBJECT (src, "No alsa handle created yet !");
259 if (snd_pcm_status_malloc (&status) != 0) {
260 GST_ERROR_OBJECT (src, "snd_pcm_status_malloc failed");
263 if (snd_pcm_status (src->handle, status) != 0) {
264 GST_ERROR_OBJECT (src, "snd_pcm_status failed");
267 /* get high resolution time stamp from driver */
268 snd_pcm_status_get_htstamp (status, &tstamp);
269 timestamp = GST_TIMESPEC_TO_TIME (tstamp);
271 /* Max available frames sets the depth of the buffer */
272 availmax = snd_pcm_status_get_avail_max (status);
274 /* Compensate the fact that the timestamp references the last sample */
275 timestamp -= gst_util_uint64_scale_int (availmax * 2, GST_SECOND, src->rate);
276 /* Compensate for the delay until the package is available */
277 timestamp += gst_util_uint64_scale_int (snd_pcm_status_get_delay (status),
278 GST_SECOND, src->rate);
280 snd_pcm_status_free (status);
282 GST_DEBUG_OBJECT (src, "ALSA timestamp : %" GST_TIME_FORMAT,
283 GST_TIME_ARGS (timestamp));
288 gst_alsasrc_set_property (GObject * object, guint prop_id,
289 const GValue * value, GParamSpec * pspec)
293 src = GST_ALSA_SRC (object);
297 g_free (src->device);
298 src->device = g_value_dup_string (value);
299 if (src->device == NULL) {
300 src->device = g_strdup (DEFAULT_PROP_DEVICE);
304 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
310 gst_alsasrc_get_property (GObject * object, guint prop_id,
311 GValue * value, GParamSpec * pspec)
315 src = GST_ALSA_SRC (object);
319 g_value_set_string (value, src->device);
321 case PROP_DEVICE_NAME:
322 g_value_take_string (value,
323 gst_alsa_find_device_name (GST_OBJECT_CAST (src),
324 src->device, src->handle, SND_PCM_STREAM_CAPTURE));
327 g_value_take_string (value,
328 gst_alsa_find_card_name (GST_OBJECT_CAST (src),
329 src->device, SND_PCM_STREAM_CAPTURE));
332 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
337 static GstStateChangeReturn
338 gst_alsasrc_change_state (GstElement * element, GstStateChange transition)
340 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
341 GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (element);
342 GstAlsaSrc *asrc = GST_ALSA_SRC (element);
345 switch (transition) {
346 /* Show the compiler that we care */
347 case GST_STATE_CHANGE_NULL_TO_READY:
348 case GST_STATE_CHANGE_READY_TO_PAUSED:
349 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
350 case GST_STATE_CHANGE_PAUSED_TO_READY:
351 case GST_STATE_CHANGE_READY_TO_NULL:
354 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
356 asrc->driver_timestamps = FALSE;
357 if (GST_IS_SYSTEM_CLOCK (clk)) {
359 g_object_get (clk, "clock-type", &clocktype, NULL);
360 if (clocktype == GST_CLOCK_TYPE_MONOTONIC) {
361 asrc->driver_timestamps = TRUE;
366 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
372 gst_alsasrc_create (GstBaseSrc * bsrc, guint64 offset, guint length,
375 GstFlowReturn ret = GST_FLOW_OK;
376 GstAlsaSrc *asrc = GST_ALSA_SRC (bsrc);
379 GST_BASE_SRC_CLASS (parent_class)->create (bsrc, offset, length, outbuf);
380 if (asrc->driver_timestamps == TRUE && *outbuf) {
381 GST_BUFFER_TIMESTAMP (*outbuf) =
382 gst_alsasrc_get_timestamp ((GstAlsaSrc *) bsrc);
389 gst_alsasrc_init (GstAlsaSrc * alsasrc)
391 GST_DEBUG_OBJECT (alsasrc, "initializing");
393 alsasrc->device = g_strdup (DEFAULT_PROP_DEVICE);
394 alsasrc->cached_caps = NULL;
395 alsasrc->driver_timestamps = FALSE;
397 alsasrc->alsa_lock = g_mutex_new ();
400 #define CHECK(call, error) \
402 if ((err = call) < 0) \
408 gst_alsasrc_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
410 GstElementClass *element_class;
411 GstPadTemplate *pad_template;
413 GstCaps *caps, *templ_caps;
415 src = GST_ALSA_SRC (bsrc);
417 if (src->handle == NULL) {
418 GST_DEBUG_OBJECT (src, "device not open, using template caps");
419 return NULL; /* base class will get template caps for us */
422 if (src->cached_caps) {
423 GST_LOG_OBJECT (src, "Returning cached caps");
425 return gst_caps_intersect_full (filter, src->cached_caps,
426 GST_CAPS_INTERSECT_FIRST);
428 return gst_caps_ref (src->cached_caps);
431 element_class = GST_ELEMENT_GET_CLASS (src);
432 pad_template = gst_element_class_get_pad_template (element_class, "src");
433 g_return_val_if_fail (pad_template != NULL, NULL);
435 templ_caps = gst_pad_template_get_caps (pad_template);
436 caps = gst_alsa_probe_supported_formats (GST_OBJECT (src), src->handle,
438 gst_caps_unref (templ_caps);
441 src->cached_caps = gst_caps_ref (caps);
444 GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
447 GstCaps *intersection;
450 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
451 gst_caps_unref (caps);
459 set_hwparams (GstAlsaSrc * alsa)
463 snd_pcm_hw_params_t *params;
465 snd_pcm_hw_params_malloc (¶ms);
467 /* choose all parameters */
468 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
469 /* set the interleaved read/write format */
470 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
472 /* set the sample format */
473 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
475 /* set the count of channels */
476 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
478 /* set the stream rate */
480 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
482 if (rrate != alsa->rate)
485 if (alsa->buffer_time != -1) {
486 /* set the buffer time */
487 CHECK (snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
488 &alsa->buffer_time, NULL), buffer_time);
490 if (alsa->period_time != -1) {
491 /* set the period time */
492 CHECK (snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
493 &alsa->period_time, NULL), period_time);
496 /* write the parameters to device */
497 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
499 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
502 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
505 snd_pcm_hw_params_free (params);
511 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
512 ("Broken configuration for recording: no configurations available: %s",
513 snd_strerror (err)));
514 snd_pcm_hw_params_free (params);
519 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
520 ("Access type not available for recording: %s", snd_strerror (err)));
521 snd_pcm_hw_params_free (params);
526 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
527 ("Sample format not available for recording: %s", snd_strerror (err)));
528 snd_pcm_hw_params_free (params);
535 if ((alsa->channels) == 1)
536 msg = g_strdup (_("Could not open device for recording in mono mode."));
537 if ((alsa->channels) == 2)
538 msg = g_strdup (_("Could not open device for recording in stereo mode."));
539 if ((alsa->channels) > 2)
542 ("Could not open device for recording in %d-channel mode"),
544 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, ("%s", msg),
545 ("%s", snd_strerror (err)));
547 snd_pcm_hw_params_free (params);
552 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
553 ("Rate %iHz not available for recording: %s",
554 alsa->rate, snd_strerror (err)));
555 snd_pcm_hw_params_free (params);
560 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
561 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err));
562 snd_pcm_hw_params_free (params);
567 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
568 ("Unable to set buffer time %i for recording: %s",
569 alsa->buffer_time, snd_strerror (err)));
570 snd_pcm_hw_params_free (params);
575 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
576 ("Unable to get buffer size for recording: %s", snd_strerror (err)));
577 snd_pcm_hw_params_free (params);
582 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
583 ("Unable to set period time %i for recording: %s", alsa->period_time,
584 snd_strerror (err)));
585 snd_pcm_hw_params_free (params);
590 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
591 ("Unable to get period size for recording: %s", snd_strerror (err)));
592 snd_pcm_hw_params_free (params);
597 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
598 ("Unable to set hw params for recording: %s", snd_strerror (err)));
599 snd_pcm_hw_params_free (params);
605 set_swparams (GstAlsaSrc * alsa)
608 snd_pcm_sw_params_t *params;
610 snd_pcm_sw_params_malloc (¶ms);
612 /* get the current swparams */
613 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
614 /* allow the transfer when at least period_size samples can be processed */
615 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
616 alsa->period_size), set_avail);
617 /* start the transfer on first read */
618 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
619 0), start_threshold);
621 #if GST_CHECK_ALSA_VERSION(1,0,16)
622 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
624 /* align all transfers to 1 sample */
625 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
628 /* write the parameters to the recording device */
629 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
631 snd_pcm_sw_params_free (params);
637 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
638 ("Unable to determine current swparams for playback: %s",
639 snd_strerror (err)));
640 snd_pcm_sw_params_free (params);
645 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
646 ("Unable to set start threshold mode for playback: %s",
647 snd_strerror (err)));
648 snd_pcm_sw_params_free (params);
653 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
654 ("Unable to set avail min for playback: %s", snd_strerror (err)));
655 snd_pcm_sw_params_free (params);
658 #if !GST_CHECK_ALSA_VERSION(1,0,16)
661 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
662 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
663 snd_pcm_sw_params_free (params);
669 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
670 ("Unable to set sw params for playback: %s", snd_strerror (err)));
671 snd_pcm_sw_params_free (params);
677 alsasrc_parse_spec (GstAlsaSrc * alsa, GstRingBufferSpec * spec)
679 switch (spec->type) {
680 case GST_BUFTYPE_LINEAR:
681 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width,
682 spec->sign ? 0 : 1, spec->bigend ? 1 : 0);
684 case GST_BUFTYPE_FLOAT:
685 switch (spec->format) {
687 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
690 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
693 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
696 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
702 case GST_BUFTYPE_A_LAW:
703 alsa->format = SND_PCM_FORMAT_A_LAW;
705 case GST_BUFTYPE_MU_LAW:
706 alsa->format = SND_PCM_FORMAT_MU_LAW;
712 alsa->rate = spec->rate;
713 alsa->channels = spec->channels;
714 alsa->buffer_time = spec->buffer_time;
715 alsa->period_time = spec->latency_time;
716 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
728 gst_alsasrc_open (GstAudioSrc * asrc)
733 alsa = GST_ALSA_SRC (asrc);
735 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_CAPTURE,
736 SND_PCM_NONBLOCK), open_error);
739 alsa->mixer = gst_alsa_mixer_new (alsa->device, GST_ALSA_MIXER_CAPTURE);
747 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
748 (_("Could not open audio device for recording. "
749 "Device is being used by another application.")),
750 ("Device '%s' is busy", alsa->device));
752 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_READ,
753 (_("Could not open audio device for recording.")),
754 ("Recording open error on device '%s': %s", alsa->device,
755 snd_strerror (err)));
762 gst_alsasrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
767 alsa = GST_ALSA_SRC (asrc);
769 if (!alsasrc_parse_spec (alsa, spec))
772 CHECK (snd_pcm_nonblock (alsa->handle, 0), non_block);
774 CHECK (set_hwparams (alsa), hw_params_failed);
775 CHECK (set_swparams (alsa), sw_params_failed);
776 CHECK (snd_pcm_prepare (alsa->handle), prepare_failed);
778 alsa->bytes_per_sample = spec->bytes_per_sample;
779 spec->segsize = alsa->period_size * spec->bytes_per_sample;
780 spec->segtotal = alsa->buffer_size / alsa->period_size;
781 spec->silence_sample[0] = 0;
782 spec->silence_sample[1] = 0;
783 spec->silence_sample[2] = 0;
784 spec->silence_sample[3] = 0;
791 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
792 ("Error parsing spec"));
797 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
798 ("Could not set device to blocking: %s", snd_strerror (err)));
803 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
804 ("Setting of hwparams failed: %s", snd_strerror (err)));
809 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
810 ("Setting of swparams failed: %s", snd_strerror (err)));
815 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
816 ("Prepare failed: %s", snd_strerror (err)));
822 gst_alsasrc_unprepare (GstAudioSrc * asrc)
826 alsa = GST_ALSA_SRC (asrc);
828 snd_pcm_drop (alsa->handle);
829 snd_pcm_hw_free (alsa->handle);
830 snd_pcm_nonblock (alsa->handle, 1);
836 gst_alsasrc_close (GstAudioSrc * asrc)
838 GstAlsaSrc *alsa = GST_ALSA_SRC (asrc);
840 snd_pcm_close (alsa->handle);
844 gst_alsa_mixer_free (alsa->mixer);
848 gst_caps_replace (&alsa->cached_caps, NULL);
854 * Underrun and suspend recovery
857 xrun_recovery (GstAlsaSrc * alsa, snd_pcm_t * handle, gint err)
859 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err);
861 if (err == -EPIPE) { /* under-run */
862 err = snd_pcm_prepare (handle);
864 GST_WARNING_OBJECT (alsa,
865 "Can't recovery from underrun, prepare failed: %s",
868 } else if (err == -ESTRPIPE) {
869 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
870 g_usleep (100); /* wait until the suspend flag is released */
873 err = snd_pcm_prepare (handle);
875 GST_WARNING_OBJECT (alsa,
876 "Can't recovery from suspend, prepare failed: %s",
885 gst_alsasrc_read (GstAudioSrc * asrc, gpointer data, guint length)
892 alsa = GST_ALSA_SRC (asrc);
894 cptr = length / alsa->bytes_per_sample;
897 GST_ALSA_SRC_LOCK (asrc);
899 if ((err = snd_pcm_readi (alsa->handle, ptr, cptr)) < 0) {
900 if (err == -EAGAIN) {
901 GST_DEBUG_OBJECT (asrc, "Read error: %s", snd_strerror (err));
903 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
909 ptr += err * alsa->channels;
912 GST_ALSA_SRC_UNLOCK (asrc);
914 return length - (cptr * alsa->bytes_per_sample);
918 GST_ALSA_SRC_UNLOCK (asrc);
919 return length; /* skip one period */
924 gst_alsasrc_delay (GstAudioSrc * asrc)
927 snd_pcm_sframes_t delay;
930 alsa = GST_ALSA_SRC (asrc);
932 res = snd_pcm_delay (alsa->handle, &delay);
933 if (G_UNLIKELY (res < 0)) {
934 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
938 return CLAMP (delay, 0, alsa->buffer_size);
942 gst_alsasrc_reset (GstAudioSrc * asrc)
947 alsa = GST_ALSA_SRC (asrc);
949 GST_ALSA_SRC_LOCK (asrc);
950 GST_DEBUG_OBJECT (alsa, "drop");
951 CHECK (snd_pcm_drop (alsa->handle), drop_error);
952 GST_DEBUG_OBJECT (alsa, "prepare");
953 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
954 GST_DEBUG_OBJECT (alsa, "reset done");
955 GST_ALSA_SRC_UNLOCK (asrc);
962 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
964 GST_ALSA_SRC_UNLOCK (asrc);
969 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
971 GST_ALSA_SRC_UNLOCK (asrc);