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);
69 GST_BOILERPLATE_FULL (GstAlsaSrc, gst_alsasrc, GstAudioSrc,
70 GST_TYPE_AUDIO_SRC, gst_alsasrc_init_interfaces);
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);
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 GstClockTime gst_alsasrc_get_timestamp (GObject * object);
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_base_init (gpointer g_class)
194 GstElementClass *element_class = GST_ELEMENT_CLASS (g_class);
196 gst_element_class_set_details_simple (element_class,
197 "Audio source (ALSA)", "Source/Audio",
198 "Read from a sound card via ALSA", "Wim Taymans <wim@fluendo.com>");
200 gst_element_class_add_static_pad_template (element_class,
201 &alsasrc_src_factory);
205 gst_alsasrc_class_init (GstAlsaSrcClass * klass)
207 GObjectClass *gobject_class;
208 GstElementClass *gstelement_class;
209 GstBaseSrcClass *gstbasesrc_class;
210 GstAudioSrcClass *gstaudiosrc_class;
211 GstBaseAudioSrcClass *gstbaseaudiosrc_class;
213 gobject_class = (GObjectClass *) klass;
214 gstelement_class = (GstElementClass *) klass;
215 gstbasesrc_class = (GstBaseSrcClass *) klass;
216 gstaudiosrc_class = (GstAudioSrcClass *) klass;
217 gstbaseaudiosrc_class = (GstBaseAudioSrcClass *) klass;
219 gobject_class->finalize = gst_alsasrc_finalize;
220 gobject_class->get_property = gst_alsasrc_get_property;
221 gobject_class->set_property = gst_alsasrc_set_property;
223 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_alsasrc_change_state);
225 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasrc_getcaps);
226 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_alsasrc_create);
228 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_alsasrc_open);
229 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasrc_prepare);
230 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasrc_unprepare);
231 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_alsasrc_close);
232 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_alsasrc_read);
233 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_alsasrc_delay);
234 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_alsasrc_reset);
236 g_object_class_install_property (gobject_class, PROP_DEVICE,
237 g_param_spec_string ("device", "Device",
238 "ALSA device, as defined in an asound configuration file",
239 DEFAULT_PROP_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
241 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
242 g_param_spec_string ("device-name", "Device name",
243 "Human-readable name of the sound device",
244 DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
246 g_object_class_install_property (gobject_class, PROP_CARD_NAME,
247 g_param_spec_string ("card-name", "Card name",
248 "Human-readable name of the sound card",
249 DEFAULT_PROP_CARD_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
253 gst_alsasrc_get_timestamp (GstAlsaSrc * asrc)
255 snd_pcm_status_t *status;
256 snd_htimestamp_t tstamp;
257 GstClockTime timestamp;
258 snd_pcm_uframes_t availmax;
260 GST_DEBUG_OBJECT (object, "Getting alsa timestamp!");
263 GST_ERROR_OBJECT (asrc, "No alsa handle created yet !");
267 if (snd_pcm_status_malloc (&status) != 0) {
268 GST_ERROR_OBJECT (asrc, "snd_pcm_status_malloc failed");
271 if (snd_pcm_status (asrc->handle, status) != 0) {
272 GST_ERROR_OBJECT (asrc, "snd_pcm_status failed");
275 /* get high resolution time stamp from driver */
276 snd_pcm_status_get_htstamp (status, &tstamp);
277 timestamp = GST_TIMESPEC_TO_TIME (tstamp);
279 /* Max available frames sets the depth of the buffer */
280 availmax = snd_pcm_status_get_avail_max (status);
282 /* Compensate the fact that the timestamp references the last sample */
283 timestamp -= gst_util_uint64_scale_int (availmax * 2, GST_SECOND, asrc->rate);
284 /* Compensate for the delay until the package is available */
285 timestamp += gst_util_uint64_scale_int (snd_pcm_status_get_delay (status),
286 GST_SECOND, asrc->rate);
288 snd_pcm_status_free (status);
290 GST_DEBUG ("ALSA timestamp : %" GST_TIME_FORMAT, GST_TIME_ARGS (timestamp));
295 gst_alsasrc_set_property (GObject * object, guint prop_id,
296 const GValue * value, GParamSpec * pspec)
300 src = GST_ALSA_SRC (object);
304 g_free (src->device);
305 src->device = g_value_dup_string (value);
306 if (src->device == NULL) {
307 src->device = g_strdup (DEFAULT_PROP_DEVICE);
311 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
317 gst_alsasrc_get_property (GObject * object, guint prop_id,
318 GValue * value, GParamSpec * pspec)
322 src = GST_ALSA_SRC (object);
326 g_value_set_string (value, src->device);
328 case PROP_DEVICE_NAME:
329 g_value_take_string (value,
330 gst_alsa_find_device_name (GST_OBJECT_CAST (src),
331 src->device, src->handle, SND_PCM_STREAM_CAPTURE));
334 g_value_take_string (value,
335 gst_alsa_find_card_name (GST_OBJECT_CAST (src),
336 src->device, SND_PCM_STREAM_CAPTURE));
339 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 static GstStateChangeReturn
345 gst_alsasrc_change_state (GstElement * element, GstStateChange transition)
347 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
348 GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (element);
349 GstAlsaSrc *asrc = GST_ALSA_SRC (element);
352 switch (transition) {
353 /* Show the compiler that we care */
354 case GST_STATE_CHANGE_NULL_TO_READY:
355 case GST_STATE_CHANGE_READY_TO_PAUSED:
356 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
357 case GST_STATE_CHANGE_PAUSED_TO_READY:
358 case GST_STATE_CHANGE_READY_TO_NULL:
361 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
363 asrc->driver_timestamps = FALSE;
364 if (GST_IS_SYSTEM_CLOCK (clk)) {
366 g_object_get (clk, "clock-type", &clocktype, NULL);
367 if (clocktype == GST_CLOCK_TYPE_MONOTONIC) {
368 asrc->driver_timestamps = TRUE;
373 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
379 gst_alsasrc_create (GstBaseSrc * bsrc, guint64 offset, guint length,
382 GstFlowReturn ret = GST_FLOW_OK;
383 GstAlsaSrc *asrc = GST_ALSA_SRC (bsrc);
386 GST_BASE_SRC_CLASS (parent_class)->create (bsrc, offset, length, outbuf);
387 if (asrc->driver_timestamps == TRUE && *outbuf) {
388 GST_BUFFER_TIMESTAMP (*outbuf) =
389 gst_alsasrc_get_timestamp ((GObject *) bsrc);
396 gst_alsasrc_init (GstAlsaSrc * alsasrc, GstAlsaSrcClass * g_class)
398 GST_DEBUG_OBJECT (alsasrc, "initializing");
400 alsasrc->device = g_strdup (DEFAULT_PROP_DEVICE);
401 alsasrc->cached_caps = NULL;
402 alsasrc->driver_timestamps = FALSE;
404 alsasrc->alsa_lock = g_mutex_new ();
407 #define CHECK(call, error) \
409 if ((err = call) < 0) \
415 gst_alsasrc_getcaps (GstBaseSrc * bsrc)
417 GstElementClass *element_class;
418 GstPadTemplate *pad_template;
422 src = GST_ALSA_SRC (bsrc);
424 if (src->handle == NULL) {
425 GST_DEBUG_OBJECT (src, "device not open, using template caps");
426 return NULL; /* base class will get template caps for us */
429 if (src->cached_caps) {
430 GST_LOG_OBJECT (src, "Returning cached caps");
431 return gst_caps_ref (src->cached_caps);
434 element_class = GST_ELEMENT_GET_CLASS (src);
435 pad_template = gst_element_class_get_pad_template (element_class, "src");
436 g_return_val_if_fail (pad_template != NULL, NULL);
438 caps = gst_alsa_probe_supported_formats (GST_OBJECT (src), src->handle,
439 gst_pad_template_get_caps (pad_template));
442 src->cached_caps = gst_caps_ref (caps);
445 GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
451 set_hwparams (GstAlsaSrc * alsa)
455 snd_pcm_hw_params_t *params;
457 snd_pcm_hw_params_malloc (¶ms);
459 /* choose all parameters */
460 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
461 /* set the interleaved read/write format */
462 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
464 /* set the sample format */
465 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
467 /* set the count of channels */
468 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
470 /* set the stream rate */
472 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
474 if (rrate != alsa->rate)
477 if (alsa->buffer_time != -1) {
478 /* set the buffer time */
479 CHECK (snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
480 &alsa->buffer_time, NULL), buffer_time);
482 if (alsa->period_time != -1) {
483 /* set the period time */
484 CHECK (snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
485 &alsa->period_time, NULL), period_time);
488 /* write the parameters to device */
489 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
491 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
494 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
497 snd_pcm_hw_params_free (params);
503 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
504 ("Broken configuration for recording: no configurations available: %s",
505 snd_strerror (err)));
506 snd_pcm_hw_params_free (params);
511 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
512 ("Access type not available for recording: %s", snd_strerror (err)));
513 snd_pcm_hw_params_free (params);
518 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
519 ("Sample format not available for recording: %s", snd_strerror (err)));
520 snd_pcm_hw_params_free (params);
527 if ((alsa->channels) == 1)
528 msg = g_strdup (_("Could not open device for recording in mono mode."));
529 if ((alsa->channels) == 2)
530 msg = g_strdup (_("Could not open device for recording in stereo mode."));
531 if ((alsa->channels) > 2)
534 ("Could not open device for recording in %d-channel mode"),
536 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, ("%s", msg),
537 ("%s", snd_strerror (err)));
539 snd_pcm_hw_params_free (params);
544 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
545 ("Rate %iHz not available for recording: %s",
546 alsa->rate, snd_strerror (err)));
547 snd_pcm_hw_params_free (params);
552 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
553 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err));
554 snd_pcm_hw_params_free (params);
559 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
560 ("Unable to set buffer time %i for recording: %s",
561 alsa->buffer_time, snd_strerror (err)));
562 snd_pcm_hw_params_free (params);
567 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
568 ("Unable to get buffer size for recording: %s", snd_strerror (err)));
569 snd_pcm_hw_params_free (params);
574 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
575 ("Unable to set period time %i for recording: %s", alsa->period_time,
576 snd_strerror (err)));
577 snd_pcm_hw_params_free (params);
582 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
583 ("Unable to get period size for recording: %s", snd_strerror (err)));
584 snd_pcm_hw_params_free (params);
589 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
590 ("Unable to set hw params for recording: %s", snd_strerror (err)));
591 snd_pcm_hw_params_free (params);
597 set_swparams (GstAlsaSrc * alsa)
600 snd_pcm_sw_params_t *params;
602 snd_pcm_sw_params_malloc (¶ms);
604 /* get the current swparams */
605 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
606 /* allow the transfer when at least period_size samples can be processed */
607 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
608 alsa->period_size), set_avail);
609 /* start the transfer on first read */
610 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
611 0), start_threshold);
613 #if GST_CHECK_ALSA_VERSION(1,0,16)
614 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
616 /* align all transfers to 1 sample */
617 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
620 /* write the parameters to the recording device */
621 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
623 snd_pcm_sw_params_free (params);
629 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
630 ("Unable to determine current swparams for playback: %s",
631 snd_strerror (err)));
632 snd_pcm_sw_params_free (params);
637 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
638 ("Unable to set start threshold mode 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 avail min for playback: %s", snd_strerror (err)));
647 snd_pcm_sw_params_free (params);
650 #if !GST_CHECK_ALSA_VERSION(1,0,16)
653 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
654 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
655 snd_pcm_sw_params_free (params);
661 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
662 ("Unable to set sw params for playback: %s", snd_strerror (err)));
663 snd_pcm_sw_params_free (params);
669 alsasrc_parse_spec (GstAlsaSrc * alsa, GstRingBufferSpec * spec)
671 switch (spec->type) {
672 case GST_BUFTYPE_LINEAR:
673 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width,
674 spec->sign ? 0 : 1, spec->bigend ? 1 : 0);
676 case GST_BUFTYPE_FLOAT:
677 switch (spec->format) {
679 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
682 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
685 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
688 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
694 case GST_BUFTYPE_A_LAW:
695 alsa->format = SND_PCM_FORMAT_A_LAW;
697 case GST_BUFTYPE_MU_LAW:
698 alsa->format = SND_PCM_FORMAT_MU_LAW;
704 alsa->rate = spec->rate;
705 alsa->channels = spec->channels;
706 alsa->buffer_time = spec->buffer_time;
707 alsa->period_time = spec->latency_time;
708 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
720 gst_alsasrc_open (GstAudioSrc * asrc)
725 alsa = GST_ALSA_SRC (asrc);
727 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_CAPTURE,
728 SND_PCM_NONBLOCK), open_error);
731 alsa->mixer = gst_alsa_mixer_new (alsa->device, GST_ALSA_MIXER_CAPTURE);
739 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
740 (_("Could not open audio device for recording. "
741 "Device is being used by another application.")),
742 ("Device '%s' is busy", alsa->device));
744 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_READ,
745 (_("Could not open audio device for recording.")),
746 ("Recording open error on device '%s': %s", alsa->device,
747 snd_strerror (err)));
754 gst_alsasrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
759 alsa = GST_ALSA_SRC (asrc);
761 if (!alsasrc_parse_spec (alsa, spec))
764 CHECK (snd_pcm_nonblock (alsa->handle, 0), non_block);
766 CHECK (set_hwparams (alsa), hw_params_failed);
767 CHECK (set_swparams (alsa), sw_params_failed);
768 CHECK (snd_pcm_prepare (alsa->handle), prepare_failed);
770 alsa->bytes_per_sample = spec->bytes_per_sample;
771 spec->segsize = alsa->period_size * spec->bytes_per_sample;
772 spec->segtotal = alsa->buffer_size / alsa->period_size;
773 spec->silence_sample[0] = 0;
774 spec->silence_sample[1] = 0;
775 spec->silence_sample[2] = 0;
776 spec->silence_sample[3] = 0;
783 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
784 ("Error parsing spec"));
789 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
790 ("Could not set device to blocking: %s", snd_strerror (err)));
795 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
796 ("Setting of hwparams failed: %s", snd_strerror (err)));
801 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
802 ("Setting of swparams failed: %s", snd_strerror (err)));
807 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
808 ("Prepare failed: %s", snd_strerror (err)));
814 gst_alsasrc_unprepare (GstAudioSrc * asrc)
818 alsa = GST_ALSA_SRC (asrc);
820 snd_pcm_drop (alsa->handle);
821 snd_pcm_hw_free (alsa->handle);
822 snd_pcm_nonblock (alsa->handle, 1);
828 gst_alsasrc_close (GstAudioSrc * asrc)
830 GstAlsaSrc *alsa = GST_ALSA_SRC (asrc);
832 snd_pcm_close (alsa->handle);
836 gst_alsa_mixer_free (alsa->mixer);
840 gst_caps_replace (&alsa->cached_caps, NULL);
846 * Underrun and suspend recovery
849 xrun_recovery (GstAlsaSrc * alsa, snd_pcm_t * handle, gint err)
851 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err);
853 if (err == -EPIPE) { /* under-run */
854 err = snd_pcm_prepare (handle);
856 GST_WARNING_OBJECT (alsa,
857 "Can't recovery from underrun, prepare failed: %s",
860 } else if (err == -ESTRPIPE) {
861 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
862 g_usleep (100); /* wait until the suspend flag is released */
865 err = snd_pcm_prepare (handle);
867 GST_WARNING_OBJECT (alsa,
868 "Can't recovery from suspend, prepare failed: %s",
877 gst_alsasrc_read (GstAudioSrc * asrc, gpointer data, guint length)
884 alsa = GST_ALSA_SRC (asrc);
886 cptr = length / alsa->bytes_per_sample;
889 GST_ALSA_SRC_LOCK (asrc);
891 if ((err = snd_pcm_readi (alsa->handle, ptr, cptr)) < 0) {
892 if (err == -EAGAIN) {
893 GST_DEBUG_OBJECT (asrc, "Read error: %s", snd_strerror (err));
895 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
901 ptr += err * alsa->channels;
904 GST_ALSA_SRC_UNLOCK (asrc);
906 return length - (cptr * alsa->bytes_per_sample);
910 GST_ALSA_SRC_UNLOCK (asrc);
911 return length; /* skip one period */
916 gst_alsasrc_delay (GstAudioSrc * asrc)
919 snd_pcm_sframes_t delay;
922 alsa = GST_ALSA_SRC (asrc);
924 res = snd_pcm_delay (alsa->handle, &delay);
925 if (G_UNLIKELY (res < 0)) {
926 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
930 return CLAMP (delay, 0, alsa->buffer_size);
934 gst_alsasrc_reset (GstAudioSrc * asrc)
939 alsa = GST_ALSA_SRC (asrc);
941 GST_ALSA_SRC_LOCK (asrc);
942 GST_DEBUG_OBJECT (alsa, "drop");
943 CHECK (snd_pcm_drop (alsa->handle), drop_error);
944 GST_DEBUG_OBJECT (alsa, "prepare");
945 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
946 GST_DEBUG_OBJECT (alsa, "reset done");
947 GST_ALSA_SRC_UNLOCK (asrc);
954 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
956 GST_ALSA_SRC_UNLOCK (asrc);
961 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
963 GST_ALSA_SRC_UNLOCK (asrc);