1 /* GStreamer
2 * Copyright (C) 2005 Wim Taymans <wim@fluendo.com>
3 *
4 * gstalsasrc.c:
5 *
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.
10 *
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.
15 *
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., 51 Franklin St, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
20 */
21
22 /**
23 * SECTION:element-alsasrc
24 * @title: alsasrc
25 * @see_also: alsasink
26 *
27 * This element reads data from an audio card using the ALSA API.
28 *
29 * ## Example pipelines
30 * |[
31 * gst-launch-1.0 -v alsasrc ! queue ! audioconvert ! vorbisenc ! oggmux ! filesink location=alsasrc.ogg
32 * ]|
33 * Record from a sound card using ALSA and encode to Ogg/Vorbis.
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40 #include <sys/ioctl.h>
41 #include <fcntl.h>
42 #include <errno.h>
43 #include <unistd.h>
44 #include <string.h>
45 #include <getopt.h>
46 #include <alsa/asoundlib.h>
47
48 #include "gstalsasrc.h"
49 #include "gstalsadeviceprobe.h"
50
51 #include <gst/gst-i18n-plugin.h>
52
53 #ifndef ESTRPIPE
54 #define ESTRPIPE EPIPE
55 #endif
56
57 #define DEFAULT_PROP_DEVICE "default"
58 #define DEFAULT_PROP_DEVICE_NAME ""
59 #define DEFAULT_PROP_CARD_NAME ""
60
61 enum
62 {
63 PROP_0,
64 PROP_DEVICE,
65 PROP_DEVICE_NAME,
66 PROP_CARD_NAME,
67 PROP_LAST
68 };
69
70 #define gst_alsasrc_parent_class parent_class
71 G_DEFINE_TYPE (GstAlsaSrc, gst_alsasrc, GST_TYPE_AUDIO_SRC);
72
73 static void gst_alsasrc_finalize (GObject * object);
74 static void gst_alsasrc_set_property (GObject * object,
75 guint prop_id, const GValue * value, GParamSpec * pspec);
76 static void gst_alsasrc_get_property (GObject * object,
77 guint prop_id, GValue * value, GParamSpec * pspec);
78 static GstStateChangeReturn gst_alsasrc_change_state (GstElement * element,
79 GstStateChange transition);
80 static GstCaps *gst_alsasrc_getcaps (GstBaseSrc * bsrc, GstCaps * filter);
81
82 static gboolean gst_alsasrc_open (GstAudioSrc * asrc);
83 static gboolean gst_alsasrc_prepare (GstAudioSrc * asrc,
84 GstAudioRingBufferSpec * spec);
85 static gboolean gst_alsasrc_unprepare (GstAudioSrc * asrc);
86 static gboolean gst_alsasrc_close (GstAudioSrc * asrc);
87 static guint gst_alsasrc_read
88 (GstAudioSrc * asrc, gpointer data, guint length, GstClockTime * timestamp);
89 static guint gst_alsasrc_delay (GstAudioSrc * asrc);
90 static void gst_alsasrc_reset (GstAudioSrc * asrc);
91
92 /* AlsaSrc signals and args */
93 enum
94 {
95 LAST_SIGNAL
96 };
97
98 #if (G_BYTE_ORDER == G_LITTLE_ENDIAN)
99 # define ALSA_SRC_FACTORY_ENDIANNESS "LITTLE_ENDIAN, BIG_ENDIAN"
100 #else
101 # define ALSA_SRC_FACTORY_ENDIANNESS "BIG_ENDIAN, LITTLE_ENDIAN"
102 #endif
103
104 static GstStaticPadTemplate alsasrc_src_factory =
105 GST_STATIC_PAD_TEMPLATE ("src",
106 GST_PAD_SRC,
107 GST_PAD_ALWAYS,
108 GST_STATIC_CAPS ("audio/x-raw, "
109 "format = (string) " GST_AUDIO_FORMATS_ALL ", "
110 "layout = (string) interleaved, "
111 "rate = (int) [ 1, MAX ], " "channels = (int) [ 1, MAX ]")
112 );
113
114 static void
gst_alsasrc_finalize(GObject * object)115 gst_alsasrc_finalize (GObject * object)
116 {
117 GstAlsaSrc *src = GST_ALSA_SRC (object);
118
119 g_free (src->device);
120 g_mutex_clear (&src->alsa_lock);
121
122 G_OBJECT_CLASS (parent_class)->finalize (object);
123 }
124
125 static void
gst_alsasrc_class_init(GstAlsaSrcClass * klass)126 gst_alsasrc_class_init (GstAlsaSrcClass * klass)
127 {
128 GObjectClass *gobject_class;
129 GstElementClass *gstelement_class;
130 GstBaseSrcClass *gstbasesrc_class;
131 GstAudioSrcClass *gstaudiosrc_class;
132
133 gobject_class = (GObjectClass *) klass;
134 gstelement_class = (GstElementClass *) klass;
135 gstbasesrc_class = (GstBaseSrcClass *) klass;
136 gstaudiosrc_class = (GstAudioSrcClass *) klass;
137
138 gobject_class->finalize = gst_alsasrc_finalize;
139 gobject_class->get_property = gst_alsasrc_get_property;
140 gobject_class->set_property = gst_alsasrc_set_property;
141
142 gst_element_class_set_static_metadata (gstelement_class,
143 "Audio source (ALSA)", "Source/Audio",
144 "Read from a sound card via ALSA", "Wim Taymans <wim@fluendo.com>");
145
146 gst_element_class_add_static_pad_template (gstelement_class,
147 &alsasrc_src_factory);
148
149 gstbasesrc_class->get_caps = GST_DEBUG_FUNCPTR (gst_alsasrc_getcaps);
150
151 gstaudiosrc_class->open = GST_DEBUG_FUNCPTR (gst_alsasrc_open);
152 gstaudiosrc_class->prepare = GST_DEBUG_FUNCPTR (gst_alsasrc_prepare);
153 gstaudiosrc_class->unprepare = GST_DEBUG_FUNCPTR (gst_alsasrc_unprepare);
154 gstaudiosrc_class->close = GST_DEBUG_FUNCPTR (gst_alsasrc_close);
155 gstaudiosrc_class->read = GST_DEBUG_FUNCPTR (gst_alsasrc_read);
156 gstaudiosrc_class->delay = GST_DEBUG_FUNCPTR (gst_alsasrc_delay);
157 gstaudiosrc_class->reset = GST_DEBUG_FUNCPTR (gst_alsasrc_reset);
158 gstelement_class->change_state = GST_DEBUG_FUNCPTR (gst_alsasrc_change_state);
159
160 g_object_class_install_property (gobject_class, PROP_DEVICE,
161 g_param_spec_string ("device", "Device",
162 "ALSA device, as defined in an asound configuration file",
163 DEFAULT_PROP_DEVICE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
164
165 g_object_class_install_property (gobject_class, PROP_DEVICE_NAME,
166 g_param_spec_string ("device-name", "Device name",
167 "Human-readable name of the sound device",
168 DEFAULT_PROP_DEVICE_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
169
170 g_object_class_install_property (gobject_class, PROP_CARD_NAME,
171 g_param_spec_string ("card-name", "Card name",
172 "Human-readable name of the sound card",
173 DEFAULT_PROP_CARD_NAME, G_PARAM_READABLE | G_PARAM_STATIC_STRINGS));
174 }
175
176 static void
gst_alsasrc_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)177 gst_alsasrc_set_property (GObject * object, guint prop_id,
178 const GValue * value, GParamSpec * pspec)
179 {
180 GstAlsaSrc *src;
181
182 src = GST_ALSA_SRC (object);
183
184 switch (prop_id) {
185 case PROP_DEVICE:
186 g_free (src->device);
187 src->device = g_value_dup_string (value);
188 if (src->device == NULL) {
189 src->device = g_strdup (DEFAULT_PROP_DEVICE);
190 }
191 break;
192 default:
193 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
194 break;
195 }
196 }
197
198 static void
gst_alsasrc_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)199 gst_alsasrc_get_property (GObject * object, guint prop_id,
200 GValue * value, GParamSpec * pspec)
201 {
202 GstAlsaSrc *src;
203
204 src = GST_ALSA_SRC (object);
205
206 switch (prop_id) {
207 case PROP_DEVICE:
208 g_value_set_string (value, src->device);
209 break;
210 case PROP_DEVICE_NAME:
211 g_value_take_string (value,
212 gst_alsa_find_device_name (GST_OBJECT_CAST (src),
213 src->device, src->handle, SND_PCM_STREAM_CAPTURE));
214 break;
215 case PROP_CARD_NAME:
216 g_value_take_string (value,
217 gst_alsa_find_card_name (GST_OBJECT_CAST (src),
218 src->device, SND_PCM_STREAM_CAPTURE));
219 break;
220 default:
221 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 break;
223 }
224 }
225
226 static GstStateChangeReturn
gst_alsasrc_change_state(GstElement * element,GstStateChange transition)227 gst_alsasrc_change_state (GstElement * element, GstStateChange transition)
228 {
229 GstStateChangeReturn ret = GST_STATE_CHANGE_SUCCESS;
230 GstAlsaSrc *alsa = GST_ALSA_SRC (element);
231 GstClock *clk;
232
233 switch (transition) {
234 /* show the compiler that we care */
235 case GST_STATE_CHANGE_NULL_TO_READY:
236 case GST_STATE_CHANGE_READY_TO_PAUSED:
237 case GST_STATE_CHANGE_PLAYING_TO_PAUSED:
238 case GST_STATE_CHANGE_PAUSED_TO_READY:
239 case GST_STATE_CHANGE_READY_TO_NULL:
240 case GST_STATE_CHANGE_NULL_TO_NULL:
241 case GST_STATE_CHANGE_READY_TO_READY:
242 case GST_STATE_CHANGE_PAUSED_TO_PAUSED:
243 case GST_STATE_CHANGE_PLAYING_TO_PLAYING:
244 break;
245 case GST_STATE_CHANGE_PAUSED_TO_PLAYING:
246 alsa->driver_timestamps = FALSE;
247
248 clk = gst_element_get_clock (element);
249 if (clk != NULL) {
250 if (GST_IS_SYSTEM_CLOCK (clk)) {
251 gint clocktype;
252 g_object_get (clk, "clock-type", &clocktype, NULL);
253 if (clocktype == GST_CLOCK_TYPE_MONOTONIC) {
254 GST_INFO ("Using driver timestamps !");
255 alsa->driver_timestamps = TRUE;
256 }
257 }
258
259 gst_object_unref (clk);
260 }
261 break;
262 }
263 ret = GST_ELEMENT_CLASS (parent_class)->change_state (element, transition);
264
265 return ret;
266 }
267
268 static void
gst_alsasrc_init(GstAlsaSrc * alsasrc)269 gst_alsasrc_init (GstAlsaSrc * alsasrc)
270 {
271 GST_DEBUG_OBJECT (alsasrc, "initializing");
272
273 alsasrc->device = g_strdup (DEFAULT_PROP_DEVICE);
274 alsasrc->cached_caps = NULL;
275 alsasrc->driver_timestamps = FALSE;
276
277 g_mutex_init (&alsasrc->alsa_lock);
278 }
279
280 #define CHECK(call, error) \
281 G_STMT_START { \
282 if ((err = call) < 0) \
283 goto error; \
284 } G_STMT_END;
285
286
287 static GstCaps *
gst_alsasrc_getcaps(GstBaseSrc * bsrc,GstCaps * filter)288 gst_alsasrc_getcaps (GstBaseSrc * bsrc, GstCaps * filter)
289 {
290 GstElementClass *element_class;
291 GstPadTemplate *pad_template;
292 GstAlsaSrc *src;
293 GstCaps *caps, *templ_caps;
294
295 src = GST_ALSA_SRC (bsrc);
296
297 if (src->handle == NULL) {
298 GST_DEBUG_OBJECT (src, "device not open, using template caps");
299 return GST_BASE_SRC_CLASS (parent_class)->get_caps (bsrc, filter);
300 }
301
302 if (src->cached_caps) {
303 GST_LOG_OBJECT (src, "Returning cached caps");
304 if (filter)
305 return gst_caps_intersect_full (filter, src->cached_caps,
306 GST_CAPS_INTERSECT_FIRST);
307 else
308 return gst_caps_ref (src->cached_caps);
309 }
310
311 element_class = GST_ELEMENT_GET_CLASS (src);
312 pad_template = gst_element_class_get_pad_template (element_class, "src");
313 g_return_val_if_fail (pad_template != NULL, NULL);
314
315 templ_caps = gst_pad_template_get_caps (pad_template);
316 GST_INFO_OBJECT (src, "template caps %" GST_PTR_FORMAT, templ_caps);
317
318 caps = gst_alsa_probe_supported_formats (GST_OBJECT (src),
319 src->device, src->handle, templ_caps);
320 gst_caps_unref (templ_caps);
321
322 if (caps) {
323 src->cached_caps = gst_caps_ref (caps);
324 }
325
326 GST_INFO_OBJECT (src, "returning caps %" GST_PTR_FORMAT, caps);
327
328 if (filter) {
329 GstCaps *intersection;
330
331 intersection =
332 gst_caps_intersect_full (filter, caps, GST_CAPS_INTERSECT_FIRST);
333 gst_caps_unref (caps);
334 return intersection;
335 } else {
336 return caps;
337 }
338 }
339
340 static int
set_hwparams(GstAlsaSrc * alsa)341 set_hwparams (GstAlsaSrc * alsa)
342 {
343 guint rrate;
344 gint err;
345 snd_pcm_hw_params_t *params;
346
347 snd_pcm_hw_params_malloc (¶ms);
348
349 /* choose all parameters */
350 CHECK (snd_pcm_hw_params_any (alsa->handle, params), no_config);
351 /* set the interleaved read/write format */
352 CHECK (snd_pcm_hw_params_set_access (alsa->handle, params, alsa->access),
353 wrong_access);
354 /* set the sample format */
355 CHECK (snd_pcm_hw_params_set_format (alsa->handle, params, alsa->format),
356 no_sample_format);
357 /* set the count of channels */
358 CHECK (snd_pcm_hw_params_set_channels (alsa->handle, params, alsa->channels),
359 no_channels);
360 /* set the stream rate */
361 rrate = alsa->rate;
362 CHECK (snd_pcm_hw_params_set_rate_near (alsa->handle, params, &rrate, NULL),
363 no_rate);
364 if (rrate != alsa->rate)
365 goto rate_match;
366
367 #ifndef GST_DISABLE_GST_DEBUG
368 /* get and dump some limits */
369 {
370 guint min, max;
371
372 snd_pcm_hw_params_get_buffer_time_min (params, &min, NULL);
373 snd_pcm_hw_params_get_buffer_time_max (params, &max, NULL);
374
375 GST_DEBUG_OBJECT (alsa, "buffer time %u, min %u, max %u",
376 alsa->buffer_time, min, max);
377
378 snd_pcm_hw_params_get_period_time_min (params, &min, NULL);
379 snd_pcm_hw_params_get_period_time_max (params, &max, NULL);
380
381 GST_DEBUG_OBJECT (alsa, "period time %u, min %u, max %u",
382 alsa->period_time, min, max);
383
384 snd_pcm_hw_params_get_periods_min (params, &min, NULL);
385 snd_pcm_hw_params_get_periods_max (params, &max, NULL);
386
387 GST_DEBUG_OBJECT (alsa, "periods min %u, max %u", min, max);
388 }
389 #endif
390
391 if (alsa->buffer_time != -1) {
392 /* set the buffer time */
393 CHECK (snd_pcm_hw_params_set_buffer_time_near (alsa->handle, params,
394 &alsa->buffer_time, NULL), buffer_time);
395 GST_DEBUG_OBJECT (alsa, "buffer time %u", alsa->buffer_time);
396 }
397 if (alsa->period_time != -1) {
398 /* set the period time */
399 CHECK (snd_pcm_hw_params_set_period_time_near (alsa->handle, params,
400 &alsa->period_time, NULL), period_time);
401 GST_DEBUG_OBJECT (alsa, "period time %u", alsa->period_time);
402 }
403
404 /* write the parameters to device */
405 CHECK (snd_pcm_hw_params (alsa->handle, params), set_hw_params);
406
407 CHECK (snd_pcm_hw_params_get_buffer_size (params, &alsa->buffer_size),
408 buffer_size);
409
410 CHECK (snd_pcm_hw_params_get_period_size (params, &alsa->period_size, NULL),
411 period_size);
412
413 snd_pcm_hw_params_free (params);
414 return 0;
415
416 /* ERRORS */
417 no_config:
418 {
419 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
420 ("Broken configuration for recording: no configurations available: %s",
421 snd_strerror (err)));
422 snd_pcm_hw_params_free (params);
423 return err;
424 }
425 wrong_access:
426 {
427 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
428 ("Access type not available for recording: %s", snd_strerror (err)));
429 snd_pcm_hw_params_free (params);
430 return err;
431 }
432 no_sample_format:
433 {
434 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
435 ("Sample format not available for recording: %s", snd_strerror (err)));
436 snd_pcm_hw_params_free (params);
437 return err;
438 }
439 no_channels:
440 {
441 gchar *msg = NULL;
442
443 if ((alsa->channels) == 1)
444 msg = g_strdup (_("Could not open device for recording in mono mode."));
445 if ((alsa->channels) == 2)
446 msg = g_strdup (_("Could not open device for recording in stereo mode."));
447 if ((alsa->channels) > 2)
448 msg =
449 g_strdup_printf (_
450 ("Could not open device for recording in %d-channel mode"),
451 alsa->channels);
452 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, ("%s", msg),
453 ("%s", snd_strerror (err)));
454 g_free (msg);
455 snd_pcm_hw_params_free (params);
456 return err;
457 }
458 no_rate:
459 {
460 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
461 ("Rate %iHz not available for recording: %s",
462 alsa->rate, snd_strerror (err)));
463 snd_pcm_hw_params_free (params);
464 return err;
465 }
466 rate_match:
467 {
468 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
469 ("Rate doesn't match (requested %iHz, get %iHz)", alsa->rate, err));
470 snd_pcm_hw_params_free (params);
471 return -EINVAL;
472 }
473 buffer_time:
474 {
475 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
476 ("Unable to set buffer time %i for recording: %s",
477 alsa->buffer_time, snd_strerror (err)));
478 snd_pcm_hw_params_free (params);
479 return err;
480 }
481 buffer_size:
482 {
483 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
484 ("Unable to get buffer size for recording: %s", snd_strerror (err)));
485 snd_pcm_hw_params_free (params);
486 return err;
487 }
488 period_time:
489 {
490 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
491 ("Unable to set period time %i for recording: %s", alsa->period_time,
492 snd_strerror (err)));
493 snd_pcm_hw_params_free (params);
494 return err;
495 }
496 period_size:
497 {
498 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
499 ("Unable to get period size for recording: %s", snd_strerror (err)));
500 snd_pcm_hw_params_free (params);
501 return err;
502 }
503 set_hw_params:
504 {
505 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
506 ("Unable to set hw params for recording: %s", snd_strerror (err)));
507 snd_pcm_hw_params_free (params);
508 return err;
509 }
510 }
511
512 static int
set_swparams(GstAlsaSrc * alsa)513 set_swparams (GstAlsaSrc * alsa)
514 {
515 int err;
516 snd_pcm_sw_params_t *params;
517
518 snd_pcm_sw_params_malloc (¶ms);
519
520 /* get the current swparams */
521 CHECK (snd_pcm_sw_params_current (alsa->handle, params), no_config);
522 /* allow the transfer when at least period_size samples can be processed */
523 CHECK (snd_pcm_sw_params_set_avail_min (alsa->handle, params,
524 alsa->period_size), set_avail);
525 /* start the transfer on first read */
526 CHECK (snd_pcm_sw_params_set_start_threshold (alsa->handle, params,
527 0), start_threshold);
528 /* use monotonic timestamping */
529 CHECK (snd_pcm_sw_params_set_tstamp_mode (alsa->handle, params,
530 SND_PCM_TSTAMP_MMAP), tstamp_mode);
531
532 #if GST_CHECK_ALSA_VERSION(1,0,16)
533 /* snd_pcm_sw_params_set_xfer_align() is deprecated, alignment is always 1 */
534 #else
535 /* align all transfers to 1 sample */
536 CHECK (snd_pcm_sw_params_set_xfer_align (alsa->handle, params, 1), set_align);
537 #endif
538
539 /* write the parameters to the recording device */
540 CHECK (snd_pcm_sw_params (alsa->handle, params), set_sw_params);
541
542 snd_pcm_sw_params_free (params);
543 return 0;
544
545 /* ERRORS */
546 no_config:
547 {
548 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
549 ("Unable to determine current swparams for playback: %s",
550 snd_strerror (err)));
551 snd_pcm_sw_params_free (params);
552 return err;
553 }
554 start_threshold:
555 {
556 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
557 ("Unable to set start threshold mode for playback: %s",
558 snd_strerror (err)));
559 snd_pcm_sw_params_free (params);
560 return err;
561 }
562 set_avail:
563 {
564 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
565 ("Unable to set avail min for playback: %s", snd_strerror (err)));
566 snd_pcm_sw_params_free (params);
567 return err;
568 }
569 tstamp_mode:
570 {
571 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
572 ("Unable to set tstamp mode for playback: %s", snd_strerror (err)));
573 snd_pcm_sw_params_free (params);
574 return err;
575 }
576 #if !GST_CHECK_ALSA_VERSION(1,0,16)
577 set_align:
578 {
579 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
580 ("Unable to set transfer align for playback: %s", snd_strerror (err)));
581 snd_pcm_sw_params_free (params);
582 return err;
583 }
584 #endif
585 set_sw_params:
586 {
587 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
588 ("Unable to set sw params for playback: %s", snd_strerror (err)));
589 snd_pcm_sw_params_free (params);
590 return err;
591 }
592 }
593
594 static gboolean
alsasrc_parse_spec(GstAlsaSrc * alsa,GstAudioRingBufferSpec * spec)595 alsasrc_parse_spec (GstAlsaSrc * alsa, GstAudioRingBufferSpec * spec)
596 {
597 switch (spec->type) {
598 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW:
599 switch (GST_AUDIO_INFO_FORMAT (&spec->info)) {
600 case GST_AUDIO_FORMAT_U8:
601 alsa->format = SND_PCM_FORMAT_U8;
602 break;
603 case GST_AUDIO_FORMAT_S8:
604 alsa->format = SND_PCM_FORMAT_S8;
605 break;
606 case GST_AUDIO_FORMAT_S16LE:
607 alsa->format = SND_PCM_FORMAT_S16_LE;
608 break;
609 case GST_AUDIO_FORMAT_S16BE:
610 alsa->format = SND_PCM_FORMAT_S16_BE;
611 break;
612 case GST_AUDIO_FORMAT_U16LE:
613 alsa->format = SND_PCM_FORMAT_U16_LE;
614 break;
615 case GST_AUDIO_FORMAT_U16BE:
616 alsa->format = SND_PCM_FORMAT_U16_BE;
617 break;
618 case GST_AUDIO_FORMAT_S24_32LE:
619 alsa->format = SND_PCM_FORMAT_S24_LE;
620 break;
621 case GST_AUDIO_FORMAT_S24_32BE:
622 alsa->format = SND_PCM_FORMAT_S24_BE;
623 break;
624 case GST_AUDIO_FORMAT_U24_32LE:
625 alsa->format = SND_PCM_FORMAT_U24_LE;
626 break;
627 case GST_AUDIO_FORMAT_U24_32BE:
628 alsa->format = SND_PCM_FORMAT_U24_BE;
629 break;
630 case GST_AUDIO_FORMAT_S32LE:
631 alsa->format = SND_PCM_FORMAT_S32_LE;
632 break;
633 case GST_AUDIO_FORMAT_S32BE:
634 alsa->format = SND_PCM_FORMAT_S32_BE;
635 break;
636 case GST_AUDIO_FORMAT_U32LE:
637 alsa->format = SND_PCM_FORMAT_U32_LE;
638 break;
639 case GST_AUDIO_FORMAT_U32BE:
640 alsa->format = SND_PCM_FORMAT_U32_BE;
641 break;
642 case GST_AUDIO_FORMAT_S24LE:
643 alsa->format = SND_PCM_FORMAT_S24_3LE;
644 break;
645 case GST_AUDIO_FORMAT_S24BE:
646 alsa->format = SND_PCM_FORMAT_S24_3BE;
647 break;
648 case GST_AUDIO_FORMAT_U24LE:
649 alsa->format = SND_PCM_FORMAT_U24_3LE;
650 break;
651 case GST_AUDIO_FORMAT_U24BE:
652 alsa->format = SND_PCM_FORMAT_U24_3BE;
653 break;
654 case GST_AUDIO_FORMAT_S20LE:
655 alsa->format = SND_PCM_FORMAT_S20_3LE;
656 break;
657 case GST_AUDIO_FORMAT_S20BE:
658 alsa->format = SND_PCM_FORMAT_S20_3BE;
659 break;
660 case GST_AUDIO_FORMAT_U20LE:
661 alsa->format = SND_PCM_FORMAT_U20_3LE;
662 break;
663 case GST_AUDIO_FORMAT_U20BE:
664 alsa->format = SND_PCM_FORMAT_U20_3BE;
665 break;
666 case GST_AUDIO_FORMAT_S18LE:
667 alsa->format = SND_PCM_FORMAT_S18_3LE;
668 break;
669 case GST_AUDIO_FORMAT_S18BE:
670 alsa->format = SND_PCM_FORMAT_S18_3BE;
671 break;
672 case GST_AUDIO_FORMAT_U18LE:
673 alsa->format = SND_PCM_FORMAT_U18_3LE;
674 break;
675 case GST_AUDIO_FORMAT_U18BE:
676 alsa->format = SND_PCM_FORMAT_U18_3BE;
677 break;
678 case GST_AUDIO_FORMAT_F32LE:
679 alsa->format = SND_PCM_FORMAT_FLOAT_LE;
680 break;
681 case GST_AUDIO_FORMAT_F32BE:
682 alsa->format = SND_PCM_FORMAT_FLOAT_BE;
683 break;
684 case GST_AUDIO_FORMAT_F64LE:
685 alsa->format = SND_PCM_FORMAT_FLOAT64_LE;
686 break;
687 case GST_AUDIO_FORMAT_F64BE:
688 alsa->format = SND_PCM_FORMAT_FLOAT64_BE;
689 break;
690 default:
691 goto error;
692 }
693 break;
694 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_A_LAW:
695 alsa->format = SND_PCM_FORMAT_A_LAW;
696 break;
697 case GST_AUDIO_RING_BUFFER_FORMAT_TYPE_MU_LAW:
698 alsa->format = SND_PCM_FORMAT_MU_LAW;
699 break;
700 default:
701 goto error;
702
703 }
704 alsa->rate = GST_AUDIO_INFO_RATE (&spec->info);
705 alsa->channels = GST_AUDIO_INFO_CHANNELS (&spec->info);
706 alsa->buffer_time = spec->buffer_time;
707 alsa->period_time = spec->latency_time;
708 alsa->access = SND_PCM_ACCESS_RW_INTERLEAVED;
709
710 if (spec->type == GST_AUDIO_RING_BUFFER_FORMAT_TYPE_RAW && alsa->channels < 9)
711 gst_audio_ring_buffer_set_channel_positions (GST_AUDIO_BASE_SRC
712 (alsa)->ringbuffer, alsa_position[alsa->channels - 1]);
713
714 return TRUE;
715
716 /* ERRORS */
717 error:
718 {
719 return FALSE;
720 }
721 }
722
723 static gboolean
gst_alsasrc_open(GstAudioSrc * asrc)724 gst_alsasrc_open (GstAudioSrc * asrc)
725 {
726 GstAlsaSrc *alsa;
727 gint err;
728
729 alsa = GST_ALSA_SRC (asrc);
730
731 CHECK (snd_pcm_open (&alsa->handle, alsa->device, SND_PCM_STREAM_CAPTURE,
732 (alsa->driver_timestamps) ? 0 : SND_PCM_NONBLOCK), open_error);
733
734 return TRUE;
735
736 /* ERRORS */
737 open_error:
738 {
739 if (err == -EBUSY) {
740 GST_ELEMENT_ERROR (alsa, RESOURCE, BUSY,
741 (_("Could not open audio device for recording. "
742 "Device is being used by another application.")),
743 ("Device '%s' is busy", alsa->device));
744 } else {
745 GST_ELEMENT_ERROR (alsa, RESOURCE, OPEN_READ,
746 (_("Could not open audio device for recording.")),
747 ("Recording open error on device '%s': %s", alsa->device,
748 snd_strerror (err)));
749 }
750 return FALSE;
751 }
752 }
753
754 static gboolean
gst_alsasrc_prepare(GstAudioSrc * asrc,GstAudioRingBufferSpec * spec)755 gst_alsasrc_prepare (GstAudioSrc * asrc, GstAudioRingBufferSpec * spec)
756 {
757 GstAlsaSrc *alsa;
758 gint err;
759
760 alsa = GST_ALSA_SRC (asrc);
761
762 if (!alsasrc_parse_spec (alsa, spec))
763 goto spec_parse;
764
765 CHECK (snd_pcm_nonblock (alsa->handle, 0), non_block);
766
767 CHECK (set_hwparams (alsa), hw_params_failed);
768 CHECK (set_swparams (alsa), sw_params_failed);
769 CHECK (snd_pcm_prepare (alsa->handle), prepare_failed);
770
771 alsa->bpf = GST_AUDIO_INFO_BPF (&spec->info);
772 spec->segsize = alsa->period_size * alsa->bpf;
773 spec->segtotal = alsa->buffer_size / alsa->period_size;
774
775 {
776 snd_output_t *out_buf = NULL;
777 char *msg = NULL;
778
779 snd_output_buffer_open (&out_buf);
780 snd_pcm_dump_hw_setup (alsa->handle, out_buf);
781 snd_output_buffer_string (out_buf, &msg);
782 GST_DEBUG_OBJECT (alsa, "Hardware setup: \n%s", msg);
783 snd_output_close (out_buf);
784 snd_output_buffer_open (&out_buf);
785 snd_pcm_dump_sw_setup (alsa->handle, out_buf);
786 snd_output_buffer_string (out_buf, &msg);
787 GST_DEBUG_OBJECT (alsa, "Software setup: \n%s", msg);
788 snd_output_close (out_buf);
789 }
790
791 #ifdef SND_CHMAP_API_VERSION
792 alsa_detect_channels_mapping (GST_OBJECT (alsa), alsa->handle, spec,
793 alsa->channels, GST_AUDIO_BASE_SRC (alsa)->ringbuffer);
794 #endif /* SND_CHMAP_API_VERSION */
795
796 return TRUE;
797
798 /* ERRORS */
799 spec_parse:
800 {
801 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
802 ("Error parsing spec"));
803 return FALSE;
804 }
805 non_block:
806 {
807 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
808 ("Could not set device to blocking: %s", snd_strerror (err)));
809 return FALSE;
810 }
811 hw_params_failed:
812 {
813 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
814 ("Setting of hwparams failed: %s", snd_strerror (err)));
815 return FALSE;
816 }
817 sw_params_failed:
818 {
819 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
820 ("Setting of swparams failed: %s", snd_strerror (err)));
821 return FALSE;
822 }
823 prepare_failed:
824 {
825 GST_ELEMENT_ERROR (alsa, RESOURCE, SETTINGS, (NULL),
826 ("Prepare failed: %s", snd_strerror (err)));
827 return FALSE;
828 }
829 }
830
831 static gboolean
gst_alsasrc_unprepare(GstAudioSrc * asrc)832 gst_alsasrc_unprepare (GstAudioSrc * asrc)
833 {
834 GstAlsaSrc *alsa;
835
836 alsa = GST_ALSA_SRC (asrc);
837
838 snd_pcm_drop (alsa->handle);
839 snd_pcm_hw_free (alsa->handle);
840 snd_pcm_nonblock (alsa->handle, 1);
841
842 return TRUE;
843 }
844
845 static gboolean
gst_alsasrc_close(GstAudioSrc * asrc)846 gst_alsasrc_close (GstAudioSrc * asrc)
847 {
848 GstAlsaSrc *alsa = GST_ALSA_SRC (asrc);
849
850 snd_pcm_close (alsa->handle);
851 alsa->handle = NULL;
852
853 gst_caps_replace (&alsa->cached_caps, NULL);
854
855 return TRUE;
856 }
857
858 /*
859 * Underrun and suspend recovery
860 */
861 static gint
xrun_recovery(GstAlsaSrc * alsa,snd_pcm_t * handle,gint err)862 xrun_recovery (GstAlsaSrc * alsa, snd_pcm_t * handle, gint err)
863 {
864 GST_WARNING_OBJECT (alsa, "xrun recovery %d: %s", err, g_strerror (-err));
865
866 if (err == -EPIPE) { /* under-run */
867 err = snd_pcm_prepare (handle);
868 if (err < 0)
869 GST_WARNING_OBJECT (alsa,
870 "Can't recover from underrun, prepare failed: %s",
871 snd_strerror (err));
872 return 0;
873 } else if (err == -ESTRPIPE) {
874 while ((err = snd_pcm_resume (handle)) == -EAGAIN)
875 g_usleep (100); /* wait until the suspend flag is released */
876
877 if (err < 0) {
878 err = snd_pcm_prepare (handle);
879 if (err < 0)
880 GST_WARNING_OBJECT (alsa,
881 "Can't recover from suspend, prepare failed: %s",
882 snd_strerror (err));
883 }
884 return 0;
885 }
886 return err;
887 }
888
889 static GstClockTime
gst_alsasrc_get_timestamp(GstAlsaSrc * asrc)890 gst_alsasrc_get_timestamp (GstAlsaSrc * asrc)
891 {
892 snd_pcm_status_t *status;
893 snd_htimestamp_t tstamp;
894 GstClockTime timestamp;
895 snd_pcm_uframes_t avail;
896 gint err = -EPIPE;
897
898 if (G_UNLIKELY (!asrc)) {
899 GST_ERROR_OBJECT (asrc, "No alsa handle created yet !");
900 return GST_CLOCK_TIME_NONE;
901 }
902
903 if (G_UNLIKELY (snd_pcm_status_malloc (&status) != 0)) {
904 GST_ERROR_OBJECT (asrc, "snd_pcm_status_malloc failed");
905 return GST_CLOCK_TIME_NONE;
906 }
907
908 if (G_UNLIKELY (snd_pcm_status (asrc->handle, status) != 0)) {
909 GST_ERROR_OBJECT (asrc, "snd_pcm_status failed");
910 return GST_CLOCK_TIME_NONE;
911 }
912
913 /* in case an xrun condition has occured we need to handle this */
914 if (snd_pcm_status_get_state (status) != SND_PCM_STATE_RUNNING) {
915 if (xrun_recovery (asrc, asrc->handle, err) < 0) {
916 GST_WARNING_OBJECT (asrc, "Could not recover from xrun condition !");
917 }
918 /* reload the status alsa status object, since recovery made it invalid */
919 if (G_UNLIKELY (snd_pcm_status (asrc->handle, status) != 0)) {
920 GST_ERROR_OBJECT (asrc, "snd_pcm_status failed");
921 }
922 }
923
924 /* get high resolution time stamp from driver */
925 snd_pcm_status_get_htstamp (status, &tstamp);
926
927 if (tstamp.tv_sec == 0 && tstamp.tv_nsec == 0)
928 return GST_CLOCK_TIME_NONE;
929
930 timestamp = GST_TIMESPEC_TO_TIME (tstamp);
931
932 /* max available frames sets the depth of the buffer */
933 avail = snd_pcm_status_get_avail (status);
934
935 /* calculate the timestamp of the next sample to be read */
936 timestamp -= gst_util_uint64_scale_int (avail, GST_SECOND, asrc->rate);
937
938 /* compensate for the fact that we really need the timestamp of the
939 * previously read data segment */
940 timestamp -= asrc->period_time * 1000;
941
942 snd_pcm_status_free (status);
943
944 GST_LOG_OBJECT (asrc, "ALSA timestamp : %" GST_TIME_FORMAT
945 ", delay %lu", GST_TIME_ARGS (timestamp), avail);
946
947 return timestamp;
948 }
949
950 static guint
gst_alsasrc_read(GstAudioSrc * asrc,gpointer data,guint length,GstClockTime * timestamp)951 gst_alsasrc_read (GstAudioSrc * asrc, gpointer data, guint length,
952 GstClockTime * timestamp)
953 {
954 GstAlsaSrc *alsa;
955 gint err;
956 gint cptr;
957 guint8 *ptr = data;
958
959 alsa = GST_ALSA_SRC (asrc);
960
961 cptr = length / alsa->bpf;
962
963 GST_ALSA_SRC_LOCK (asrc);
964 while (cptr > 0) {
965 if ((err = snd_pcm_readi (alsa->handle, ptr, cptr)) < 0) {
966 if (err == -EAGAIN) {
967 GST_DEBUG_OBJECT (asrc, "Read error: %s", snd_strerror (err));
968 continue;
969 } else if (err == -ENODEV) {
970 goto device_disappeared;
971 } else if (xrun_recovery (alsa, alsa->handle, err) < 0) {
972 goto read_error;
973 }
974 continue;
975 }
976
977 ptr += snd_pcm_frames_to_bytes (alsa->handle, err);
978 cptr -= err;
979 }
980 GST_ALSA_SRC_UNLOCK (asrc);
981
982 /* if driver timestamps are enabled we need to return this here */
983 if (alsa->driver_timestamps && timestamp)
984 *timestamp = gst_alsasrc_get_timestamp (alsa);
985
986 return length - (cptr * alsa->bpf);
987
988 read_error:
989 {
990 GST_ALSA_SRC_UNLOCK (asrc);
991 return length; /* skip one period */
992 }
993 device_disappeared:
994 {
995 GST_ELEMENT_ERROR (asrc, RESOURCE, READ,
996 (_("Error recording from audio device. "
997 "The device has been disconnected.")), (NULL));
998 GST_ALSA_SRC_UNLOCK (asrc);
999 return (guint) - 1;
1000 }
1001 }
1002
1003 static guint
gst_alsasrc_delay(GstAudioSrc * asrc)1004 gst_alsasrc_delay (GstAudioSrc * asrc)
1005 {
1006 GstAlsaSrc *alsa;
1007 snd_pcm_sframes_t delay;
1008 int res;
1009
1010 alsa = GST_ALSA_SRC (asrc);
1011
1012 res = snd_pcm_delay (alsa->handle, &delay);
1013 if (G_UNLIKELY (res < 0)) {
1014 GST_DEBUG_OBJECT (alsa, "snd_pcm_delay returned %d", res);
1015 delay = 0;
1016 }
1017
1018 return CLAMP (delay, 0, alsa->buffer_size);
1019 }
1020
1021 static void
gst_alsasrc_reset(GstAudioSrc * asrc)1022 gst_alsasrc_reset (GstAudioSrc * asrc)
1023 {
1024 GstAlsaSrc *alsa;
1025 gint err;
1026
1027 alsa = GST_ALSA_SRC (asrc);
1028
1029 GST_ALSA_SRC_LOCK (asrc);
1030 GST_DEBUG_OBJECT (alsa, "drop");
1031 CHECK (snd_pcm_drop (alsa->handle), drop_error);
1032 GST_DEBUG_OBJECT (alsa, "prepare");
1033 CHECK (snd_pcm_prepare (alsa->handle), prepare_error);
1034 GST_DEBUG_OBJECT (alsa, "reset done");
1035 GST_ALSA_SRC_UNLOCK (asrc);
1036
1037 return;
1038
1039 /* ERRORS */
1040 drop_error:
1041 {
1042 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm drop error: %s",
1043 snd_strerror (err));
1044 GST_ALSA_SRC_UNLOCK (asrc);
1045 return;
1046 }
1047 prepare_error:
1048 {
1049 GST_ERROR_OBJECT (alsa, "alsa-reset: pcm prepare error: %s",
1050 snd_strerror (err));
1051 GST_ALSA_SRC_UNLOCK (asrc);
1052 return;
1053 }
1054 }
1055