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 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_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;
212 gobject_class = (GObjectClass *) klass;
213 gstelement_class = (GstElementClass *) klass;
214 gstbasesrc_class = (GstBaseSrcClass *) klass;
215 gstaudiosrc_class = (GstAudioSrcClass *) klass;
217 gobject_class->finalize = gst_alsasrc_finalize;
218 gobject_class->get_property = gst_alsasrc_get_property;
219 gobject_class->set_property = gst_alsasrc_set_property;
221 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_alsasrc_change_state);
223 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasrc_getcaps);
224 gstbasesrc_class->create = GST_DEBUG_FUNCPTR (gst_alsasrc_create);
226 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_alsasrc_open);
227 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasrc_prepare);
228 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasrc_unprepare);
229 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_alsasrc_close);
230 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_alsasrc_read);
231 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_alsasrc_delay);
232 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_alsasrc_reset);
234 g_object_class_install_property (gobject_class, PROP_DEVICE,
235 g_param_spec_string ("device", "Device",
236 "ALSA device, as defined in an asound configuration file",
237 DEFAULT_PROP_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
239 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
240 g_param_spec_string ("device-name", "Device name",
241 "Human-readable name of the sound device",
242 DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
244 g_object_class_install_property (gobject_class, PROP_CARD_NAME,
245 g_param_spec_string ("card-name", "Card name",
246 "Human-readable name of the sound card",
247 DEFAULT_PROP_CARD_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
251 gst_alsasrc_get_timestamp (GstAlsaSrc * src)
253 snd_pcm_status_t *status;
254 snd_htimestamp_t htstamp;
255 snd_timestamp_t tstamp;
256 GstClockTime timestamp;
257 snd_pcm_uframes_t availmax;
260 GST_DEBUG_OBJECT (src, "Getting alsa timestamp!");
263 GST_ERROR_OBJECT (src, "No alsa handle created yet !");
264 return GST_CLOCK_TIME_NONE;
267 if (snd_pcm_status_malloc (&status) != 0) {
268 GST_ERROR_OBJECT (src, "snd_pcm_status_malloc failed");
269 return GST_CLOCK_TIME_NONE;
272 if (snd_pcm_status (src->handle, status) != 0) {
273 GST_ERROR_OBJECT (src, "snd_pcm_status failed");
274 snd_pcm_status_free (status);
275 return GST_CLOCK_TIME_NONE;
278 /* get high resolution time stamp from driver */
279 snd_pcm_status_get_htstamp (status, &htstamp);
280 timestamp = GST_TIMESPEC_TO_TIME (htstamp);
282 GST_INFO_OBJECT (src,
283 "This alsa source does support high resolution timestamps");
284 snd_pcm_status_get_tstamp (status, &tstamp);
285 timestamp = GST_TIMEVAL_TO_TIME (tstamp);
287 GST_INFO_OBJECT (src,
288 "This alsa source does support low resolution timestamps");
289 timestamp = gst_util_get_timestamp ();
292 GST_DEBUG_OBJECT (src, "Base ts: %" GST_TIME_FORMAT,
293 GST_TIME_ARGS (timestamp));
294 if (timestamp == 0) {
295 /* This timestamp is supposed to represent the last sample, so 0 (which
296 can be returned on some ALSA setups (such as mine)) must mean that it
297 is invalid, unless there's just one sample, but we'll ignore that. */
298 GST_WARNING_OBJECT (src,
299 "No timestamp returned from snd_pcm_status_get_htstamp");
300 return GST_CLOCK_TIME_NONE;
303 /* Max available frames sets the depth of the buffer */
304 availmax = snd_pcm_status_get_avail_max (status);
306 /* Compensate the fact that the timestamp references the last sample */
307 offset = -gst_util_uint64_scale_int (availmax * 2, GST_SECOND, src->rate);
308 /* Compensate for the delay until the package is available */
309 offset += gst_util_uint64_scale_int (snd_pcm_status_get_delay (status),
310 GST_SECOND, src->rate);
312 snd_pcm_status_free (status);
314 /* just in case, should not happen */
315 if (-offset > timestamp)
320 /* Take first ts into account */
321 if (src->first_alsa_ts == GST_CLOCK_TIME_NONE) {
322 src->first_alsa_ts = timestamp;
324 timestamp -= src->first_alsa_ts;
326 GST_DEBUG_OBJECT (src, "ALSA timestamp : %" GST_TIME_FORMAT,
327 GST_TIME_ARGS (timestamp));
332 gst_alsasrc_set_property (GObject * object, guint prop_id,
333 const GValue * value, GParamSpec * pspec)
337 src = GST_ALSA_SRC (object);
341 g_free (src->device);
342 src->device = g_value_dup_string (value);
343 if (src->device == NULL) {
344 src->device = g_strdup (DEFAULT_PROP_DEVICE);
348 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
354 gst_alsasrc_get_property (GObject * object, guint prop_id,
355 GValue * value, GParamSpec * pspec)
359 src = GST_ALSA_SRC (object);
363 g_value_set_string (value, src->device);
365 case PROP_DEVICE_NAME:
366 g_value_take_string (value,
367 gst_alsa_find_device_name (GST_OBJECT_CAST (src),
368 src->device, src->handle, SND_PCM_STREAM_CAPTURE));
371 g_value_take_string (value,
372 gst_alsa_find_card_name (GST_OBJECT_CAST (src),
373 src->device, SND_PCM_STREAM_CAPTURE));
376 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 static GstStateChangeReturn
382 gst_alsasrc_change_state (GstElement * element, GstStateChange transition)
384 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
385 GstBaseAudioSrc *src = GST_BASE_AUDIO_SRC (element);
386 GstAlsaSrc *asrc = GST_ALSA_SRC (element);
389 switch (transition) {
390 /* Show the compiler that we care */
391 case GST_STATE_CHANGE_NULL_TO_READY:
392 case GST_STATE_CHANGE_READY_TO_PAUSED:
393 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
394 case GST_STATE_CHANGE_PAUSED_TO_READY:
395 case GST_STATE_CHANGE_READY_TO_NULL:
398 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
400 asrc->driver_timestamps = FALSE;
401 if (GST_IS_SYSTEM_CLOCK (clk)) {
403 g_object_get (clk, "clock-type", &clocktype, NULL);
404 if (clocktype == GST_CLOCK_TYPE_MONOTONIC) {
405 asrc->driver_timestamps = TRUE;
410 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
416 gst_alsasrc_create (GstBaseSrc * bsrc, guint64 offset, guint length,
419 GstFlowReturn ret = GST_FLOW_OK;
420 GstAlsaSrc *asrc = GST_ALSA_SRC (bsrc);
423 GST_BASE_SRC_CLASS (parent_class)->create (bsrc, offset, length, outbuf);
424 if (asrc->driver_timestamps == TRUE && *outbuf) {
425 GstClockTime ts = gst_alsasrc_get_timestamp (asrc);
426 if (GST_CLOCK_TIME_IS_VALID (ts)) {
427 GST_BUFFER_TIMESTAMP (*outbuf) = ts;
435 gst_alsasrc_init (GstAlsaSrc * alsasrc, GstAlsaSrcClass * g_class)
437 GST_DEBUG_OBJECT (alsasrc, "initializing");
439 alsasrc->device = g_strdup (DEFAULT_PROP_DEVICE);
440 alsasrc->cached_caps = NULL;
441 alsasrc->driver_timestamps = FALSE;
442 alsasrc->first_alsa_ts = GST_CLOCK_TIME_NONE;
444 alsasrc->alsa_lock = g_mutex_new ();
447 #define CHECK(call, error) \
449 if ((err = call) < 0) \
455 gst_alsasrc_getcaps (GstBaseSrc * bsrc)
457 GstElementClass *element_class;
458 GstPadTemplate *pad_template;
462 src = GST_ALSA_SRC (bsrc);
464 if (src->handle == NULL) {
465 GST_DEBUG_OBJECT (src, "device not open, using template caps");
466 return NULL; /* base class will get template caps for us */
469 if (src->cached_caps) {
470 GST_LOG_OBJECT (src, "Returning cached caps");
471 return gst_caps_ref (src->cached_caps);
474 element_class = GST_ELEMENT_GET_CLASS (src);
475 pad_template = gst_element_class_get_pad_template (element_class, "src");
476 g_return_val_if_fail (pad_template != NULL, NULL);
478 caps = gst_alsa_probe_supported_formats (GST_OBJECT (src), src->handle,
479 gst_pad_template_get_caps (pad_template));
482 src->cached_caps = gst_caps_ref (caps);
485 GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
491 set_hwparams (GstAlsaSrc * alsa)
495 snd_pcm_hw_params_t *params;
497 snd_pcm_hw_params_malloc (¶ms);
499 /* choose all parameters */
500 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
501 /* set the interleaved read/write format */
502 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
504 /* set the sample format */
505 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
507 /* set the count of channels */
508 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
510 /* set the stream rate */
512 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
514 if (rrate != alsa->rate)
517 if (alsa->buffer_time != -1) {
518 /* set the buffer time */
519 CHECK (snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
520 &alsa->buffer_time, NULL), buffer_time);
522 if (alsa->period_time != -1) {
523 /* set the period time */
524 CHECK (snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
525 &alsa->period_time, NULL), period_time);
528 /* write the parameters to device */
529 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
531 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
534 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
537 snd_pcm_hw_params_free (params);
543 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
544 ("Broken configuration for recording: no configurations available: %s",
545 snd_strerror (err)));
546 snd_pcm_hw_params_free (params);
551 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
552 ("Access type not available for recording: %s", snd_strerror (err)));
553 snd_pcm_hw_params_free (params);
558 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
559 ("Sample format not available for recording: %s", snd_strerror (err)));
560 snd_pcm_hw_params_free (params);
567 if ((alsa->channels) == 1)
568 msg = g_strdup (_("Could not open device for recording in mono mode."));
569 if ((alsa->channels) == 2)
570 msg = g_strdup (_("Could not open device for recording in stereo mode."));
571 if ((alsa->channels) > 2)
574 ("Could not open device for recording in %d-channel mode"),
576 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, ("%s", msg),
577 ("%s", snd_strerror (err)));
579 snd_pcm_hw_params_free (params);
584 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
585 ("Rate %iHz not available for recording: %s",
586 alsa->rate, snd_strerror (err)));
587 snd_pcm_hw_params_free (params);
592 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
593 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err));
594 snd_pcm_hw_params_free (params);
599 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
600 ("Unable to set buffer time %i for recording: %s",
601 alsa->buffer_time, snd_strerror (err)));
602 snd_pcm_hw_params_free (params);
607 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
608 ("Unable to get buffer size for recording: %s", snd_strerror (err)));
609 snd_pcm_hw_params_free (params);
614 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
615 ("Unable to set period time %i for recording: %s", alsa->period_time,
616 snd_strerror (err)));
617 snd_pcm_hw_params_free (params);
622 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
623 ("Unable to get period size for recording: %s", snd_strerror (err)));
624 snd_pcm_hw_params_free (params);
629 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
630 ("Unable to set hw params for recording: %s", snd_strerror (err)));
631 snd_pcm_hw_params_free (params);
637 set_swparams (GstAlsaSrc * alsa)
640 snd_pcm_sw_params_t *params;
642 snd_pcm_sw_params_malloc (¶ms);
644 /* get the current swparams */
645 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
646 /* allow the transfer when at least period_size samples can be processed */
647 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
648 alsa->period_size), set_avail);
649 /* start the transfer on first read */
650 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
651 0), start_threshold);
653 #if GST_CHECK_ALSA_VERSION(1,0,16)
654 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
656 /* align all transfers to 1 sample */
657 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
660 /* write the parameters to the recording device */
661 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
663 snd_pcm_sw_params_free (params);
669 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
670 ("Unable to determine current swparams for playback: %s",
671 snd_strerror (err)));
672 snd_pcm_sw_params_free (params);
677 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
678 ("Unable to set start threshold mode for playback: %s",
679 snd_strerror (err)));
680 snd_pcm_sw_params_free (params);
685 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
686 ("Unable to set avail min for playback: %s", snd_strerror (err)));
687 snd_pcm_sw_params_free (params);
690 #if !GST_CHECK_ALSA_VERSION(1,0,16)
693 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
694 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
695 snd_pcm_sw_params_free (params);
701 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
702 ("Unable to set sw params for playback: %s", snd_strerror (err)));
703 snd_pcm_sw_params_free (params);
709 alsasrc_parse_spec (GstAlsaSrc * alsa, GstRingBufferSpec * spec)
711 switch (spec->type) {
712 case GST_BUFTYPE_LINEAR:
713 alsa->format = snd_pcm_build_linear_format (spec->depth, spec->width,
714 spec->sign ? 0 : 1, spec->bigend ? 1 : 0);
716 case GST_BUFTYPE_FLOAT:
717 switch (spec->format) {
719 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
722 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
725 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
728 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
734 case GST_BUFTYPE_A_LAW:
735 alsa->format = SND_PCM_FORMAT_A_LAW;
737 case GST_BUFTYPE_MU_LAW:
738 alsa->format = SND_PCM_FORMAT_MU_LAW;
744 alsa->rate = spec->rate;
745 alsa->channels = spec->channels;
746 alsa->buffer_time = spec->buffer_time;
747 alsa->period_time = spec->latency_time;
748 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
760 gst_alsasrc_open (GstAudioSrc * asrc)
765 alsa = GST_ALSA_SRC (asrc);
767 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_CAPTURE,
768 SND_PCM_NONBLOCK), open_error);
771 alsa->mixer = gst_alsa_mixer_new (alsa->device, GST_ALSA_MIXER_CAPTURE);
779 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
780 (_("Could not open audio device for recording. "
781 "Device is being used by another application.")),
782 ("Device '%s' is busy", alsa->device));
784 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_READ,
785 (_("Could not open audio device for recording.")),
786 ("Recording open error on device '%s': %s", alsa->device,
787 snd_strerror (err)));
794 gst_alsasrc_prepare (GstAudioSrc * asrc, GstRingBufferSpec * spec)
799 alsa = GST_ALSA_SRC (asrc);
801 if (!alsasrc_parse_spec (alsa, spec))
804 CHECK (snd_pcm_nonblock (alsa->handle, 0), non_block);
806 CHECK (set_hwparams (alsa), hw_params_failed);
807 CHECK (set_swparams (alsa), sw_params_failed);
808 CHECK (snd_pcm_prepare (alsa->handle), prepare_failed);
810 alsa->bytes_per_sample = spec->bytes_per_sample;
811 spec->segsize = alsa->period_size * spec->bytes_per_sample;
812 spec->segtotal = alsa->buffer_size / alsa->period_size;
813 spec->silence_sample[0] = 0;
814 spec->silence_sample[1] = 0;
815 spec->silence_sample[2] = 0;
816 spec->silence_sample[3] = 0;
823 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
824 ("Error parsing spec"));
829 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
830 ("Could not set device to blocking: %s", snd_strerror (err)));
835 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
836 ("Setting of hwparams failed: %s", snd_strerror (err)));
841 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
842 ("Setting of swparams failed: %s", snd_strerror (err)));
847 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
848 ("Prepare failed: %s", snd_strerror (err)));
854 gst_alsasrc_unprepare (GstAudioSrc * asrc)
858 alsa = GST_ALSA_SRC (asrc);
860 snd_pcm_drop (alsa->handle);
861 snd_pcm_hw_free (alsa->handle);
862 snd_pcm_nonblock (alsa->handle, 1);
868 gst_alsasrc_close (GstAudioSrc * asrc)
870 GstAlsaSrc *alsa = GST_ALSA_SRC (asrc);
872 snd_pcm_close (alsa->handle);
876 gst_alsa_mixer_free (alsa->mixer);
880 gst_caps_replace (&alsa->cached_caps, NULL);
886 * Underrun and suspend recovery
889 xrun_recovery (GstAlsaSrc * alsa, snd_pcm_t * handle, gint err)
891 GST_DEBUG_OBJECT (alsa, "xrun recovery %d", err);
893 if (err == -EPIPE) { /* under-run */
894 err = snd_pcm_prepare (handle);
896 GST_WARNING_OBJECT (alsa,
897 "Can't recovery from underrun, prepare failed: %s",
900 } else if (err == -ESTRPIPE) {
901 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
902 g_usleep (100); /* wait until the suspend flag is released */
905 err = snd_pcm_prepare (handle);
907 GST_WARNING_OBJECT (alsa,
908 "Can't recovery from suspend, prepare failed: %s",
917 gst_alsasrc_read (GstAudioSrc * asrc, gpointer data, guint length)
924 alsa = GST_ALSA_SRC (asrc);
926 cptr = length / alsa->bytes_per_sample;
929 GST_ALSA_SRC_LOCK (asrc);
931 if ((err = snd_pcm_readi (alsa->handle, ptr, cptr)) < 0) {
932 if (err == -EAGAIN) {
933 GST_DEBUG_OBJECT (asrc, "Read error: %s", snd_strerror (err));
935 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
941 ptr += err * alsa->channels;
944 GST_ALSA_SRC_UNLOCK (asrc);
946 return length - (cptr * alsa->bytes_per_sample);
950 GST_ALSA_SRC_UNLOCK (asrc);
951 return length; /* skip one period */
956 gst_alsasrc_delay (GstAudioSrc * asrc)
959 snd_pcm_sframes_t delay;
962 alsa = GST_ALSA_SRC (asrc);
964 res = snd_pcm_delay (alsa->handle, &delay);
965 if (G_UNLIKELY (res < 0)) {
966 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
970 return CLAMP (delay, 0, alsa->buffer_size);
974 gst_alsasrc_reset (GstAudioSrc * asrc)
979 alsa = GST_ALSA_SRC (asrc);
981 GST_ALSA_SRC_LOCK (asrc);
982 GST_DEBUG_OBJECT (alsa, "drop");
983 CHECK (snd_pcm_drop (alsa->handle), drop_error);
984 GST_DEBUG_OBJECT (alsa, "prepare");
985 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
986 GST_DEBUG_OBJECT (alsa, "reset done");
987 alsa->first_alsa_ts = GST_CLOCK_TIME_NONE;
988 GST_ALSA_SRC_UNLOCK (asrc);
995 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
997 GST_ALSA_SRC_UNLOCK (asrc);
1002 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
1003 snd_strerror (err));
1004 GST_ALSA_SRC_UNLOCK (asrc);