1 /*****************************************************************************
2 * alsa.c : Alsa input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2009 the VideoLAN team
7 * Authors: Benjamin Pracht <bigben at videolan dot org>
8 * Richard Hosking <richard at hovis dot net>
9 * Antoine Cellerier <dionoea at videolan d.t org>
10 * Dennis Lou <dlou99 at yahoo dot com>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
28 * ALSA support based on parts of
29 * http://www.equalarea.com/paul/alsa-audio.html
30 * and hints taken from alsa-utils (aplay/arecord)
31 * http://www.alsa-project.org
34 /*****************************************************************************
36 *****************************************************************************/
42 #include <vlc_common.h>
43 #include <vlc_plugin.h>
44 #include <vlc_access.h>
45 #include <vlc_demux.h>
46 #include <vlc_input.h>
51 #include <sys/ioctl.h>
54 #include <sys/soundcard.h>
56 #define ALSA_PCM_NEW_HW_PARAMS_API
57 #define ALSA_PCM_NEW_SW_PARAMS_API
58 #include <alsa/asoundlib.h>
62 /*****************************************************************************
64 *****************************************************************************/
66 static int DemuxOpen ( vlc_object_t * );
67 static void DemuxClose( vlc_object_t * );
69 #define STEREO_TEXT N_( "Stereo" )
70 #define STEREO_LONGTEXT N_( \
71 "Capture the audio stream in stereo." )
73 #define SAMPLERATE_TEXT N_( "Samplerate" )
74 #define SAMPLERATE_LONGTEXT N_( \
75 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
77 #define CACHING_TEXT N_("Caching value in ms")
78 #define CACHING_LONGTEXT N_( \
79 "Caching value for Alsa captures. This " \
80 "value should be set in milliseconds." )
82 #define ALSA_DEFAULT "hw"
83 #define CFG_PREFIX "alsa-"
86 set_shortname( N_("Alsa") )
87 set_description( N_("Alsa audio capture input") )
88 set_category( CAT_INPUT )
89 set_subcategory( SUBCAT_INPUT_ACCESS )
91 add_shortcut( "alsa" )
92 set_capability( "access_demux", 10 )
93 set_callbacks( DemuxOpen, DemuxClose )
95 add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
97 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
98 SAMPLERATE_LONGTEXT, true )
99 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
100 CACHING_TEXT, CACHING_LONGTEXT, true )
103 /*****************************************************************************
104 * Access: local prototypes
105 *****************************************************************************/
107 static int DemuxControl( demux_t *, int, va_list );
109 static int Demux( demux_t * );
111 static block_t* GrabAudio( demux_t *p_demux );
113 static int OpenAudioDev( demux_t * );
114 static bool ProbeAudioDevAlsa( demux_t *, const char *psz_device );
118 const char *psz_device; /* Alsa device from MRL */
122 unsigned int i_sample_rate;
124 size_t i_max_frame_size;
129 snd_pcm_t *p_alsa_pcm;
130 size_t i_alsa_frame_size;
131 int i_alsa_chunk_size;
133 int64_t i_next_demux_date; /* Used to handle alsa:// as input-slave properly */
136 static int FindMainDevice( demux_t *p_demux )
138 /* TODO: if using default device, loop through all alsa devices until
140 msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
141 if( ProbeAudioDevAlsa( p_demux, p_demux->p_sys->psz_device ) )
143 msg_Dbg( p_demux, "'%s' is an audio device",
144 p_demux->p_sys->psz_device );
145 OpenAudioDev( p_demux );
148 if( p_demux->p_sys->p_alsa_pcm == NULL )
153 static void ListAvailableDevices( demux_t *p_demux )
155 snd_ctl_card_info_t *p_info = NULL;
156 snd_ctl_card_info_alloca( &p_info );
158 snd_pcm_info_t *p_pcminfo = NULL;
159 snd_pcm_info_alloca( &p_pcminfo );
161 msg_Dbg( p_demux, "Available alsa capture devices:" );
163 while( !snd_card_next( &i_card ) && i_card >= 0 )
165 char psz_devname[10];
166 snprintf( psz_devname, 10, "hw:%d", i_card );
168 snd_ctl_t *p_ctl = NULL;
169 if( snd_ctl_open( &p_ctl, psz_devname, 0 ) < 0 ) continue;
171 snd_ctl_card_info( p_ctl, p_info );
172 msg_Dbg( p_demux, " %s (%s)",
173 snd_ctl_card_info_get_id( p_info ),
174 snd_ctl_card_info_get_name( p_info ) );
177 while( !snd_ctl_pcm_next_device( p_ctl, &i_dev ) && i_dev >= 0 )
179 snd_pcm_info_set_device( p_pcminfo, i_dev );
180 snd_pcm_info_set_subdevice( p_pcminfo, 0 );
181 snd_pcm_info_set_stream( p_pcminfo, SND_PCM_STREAM_CAPTURE );
182 if( snd_ctl_pcm_info( p_ctl, p_pcminfo ) < 0 ) continue;
184 msg_Dbg( p_demux, " hw:%d,%d : %s (%s)", i_card, i_dev,
185 snd_pcm_info_get_id( p_pcminfo ),
186 snd_pcm_info_get_name( p_pcminfo ) );
189 snd_ctl_close( p_ctl );
193 /*****************************************************************************
194 * DemuxOpen: opens alsa device, access_demux callback
195 *****************************************************************************
197 * url: <alsa device>::::
199 *****************************************************************************/
200 static int DemuxOpen( vlc_object_t *p_this )
202 demux_t *p_demux = (demux_t*)p_this;
205 /* Only when selected */
206 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
209 p_demux->pf_control = DemuxControl;
210 p_demux->pf_demux = Demux;
211 p_demux->info.i_update = 0;
212 p_demux->info.i_title = 0;
213 p_demux->info.i_seekpoint = 0;
215 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
216 if( p_sys == NULL ) return VLC_ENOMEM;
218 p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
219 p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
220 p_sys->i_cache = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
222 p_sys->p_block = NULL;
223 p_sys->i_next_demux_date = -1;
225 if( p_demux->psz_path && *p_demux->psz_path )
226 p_sys->psz_device = p_demux->psz_path;
229 p_sys->psz_device = ALSA_DEFAULT;
230 ListAvailableDevices( p_demux );
233 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
235 DemuxClose( p_this );
242 /*****************************************************************************
243 * Close: close device, free resources
244 *****************************************************************************/
245 static void DemuxClose( vlc_object_t *p_this )
247 demux_t *p_demux = (demux_t *)p_this;
248 demux_sys_t *p_sys = p_demux->p_sys;
250 if( p_sys->p_alsa_pcm )
252 snd_pcm_close( p_sys->p_alsa_pcm );
255 if( p_sys->p_block ) block_Release( p_sys->p_block );
260 /*****************************************************************************
262 *****************************************************************************/
263 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
265 demux_sys_t *p_sys = p_demux->p_sys;
269 /* Special for access_demux */
270 case DEMUX_CAN_PAUSE:
272 case DEMUX_SET_PAUSE_STATE:
273 case DEMUX_CAN_CONTROL_PACE:
274 *va_arg( args, bool * ) = false;
277 case DEMUX_GET_PTS_DELAY:
278 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_cache * 1000;
282 *va_arg( args, int64_t * ) = mdate();
285 case DEMUX_SET_NEXT_DEMUX_TIME:
286 p_sys->i_next_demux_date = va_arg( args, int64_t );
289 /* TODO implement others */
297 /*****************************************************************************
298 * Demux: Processes the audio frame
299 *****************************************************************************/
300 static int Demux( demux_t *p_demux )
302 demux_sys_t *p_sys = p_demux->p_sys;
304 block_t *p_block = NULL;
310 es_out_Send( p_demux->out, p_sys->p_es, p_block );
315 int i_wait = snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */
320 p_block = GrabAudio( p_demux );
322 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
325 /* FIXME: this is a copy paste from below. Shouldn't be needed
329 snd_pcm_prepare( p_sys->p_alsa_pcm );
334 int i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
335 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
340 } while( p_block && p_sys->i_next_demux_date > 0 &&
341 p_block->i_pts < p_sys->i_next_demux_date );
344 es_out_Send( p_demux->out, p_sys->p_es, p_block );
350 /*****************************************************************************
351 * GrabAudio: Grab an audio frame
352 *****************************************************************************/
353 static block_t* GrabAudio( demux_t *p_demux )
355 demux_sys_t *p_sys = p_demux->p_sys;
356 int i_read, i_correct;
359 if( p_sys->p_block ) p_block = p_sys->p_block;
360 else p_block = block_New( p_demux, p_sys->i_max_frame_size );
364 msg_Warn( p_demux, "cannot get block" );
368 p_sys->p_block = p_block;
371 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
381 snd_pcm_prepare( p_sys->p_alsa_pcm );
385 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
386 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
389 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
395 /* convert from frames to bytes */
396 i_read *= p_sys->i_alsa_frame_size;
399 if( i_read <= 0 ) return 0;
401 p_block->i_buffer = i_read;
404 /* Correct the date because of kernel buffering */
408 snd_pcm_sframes_t delay = 0;
409 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
411 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
412 /* Test for overrun */
413 if( i_correction_delta > p_sys->i_max_frame_size )
415 msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
416 i_correction_delta, p_sys->i_max_frame_size );
417 i_correction_delta = p_sys->i_max_frame_size;
418 snd_pcm_prepare( p_sys->p_alsa_pcm );
420 i_correct += i_correction_delta;
424 /* delay failed so reset */
425 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
426 snd_pcm_prepare( p_sys->p_alsa_pcm );
430 p_block->i_pts = p_block->i_dts =
431 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
432 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
437 /*****************************************************************************
438 * OpenAudioDev: open and set up the audio device and probe for capabilities
439 *****************************************************************************/
440 static int OpenAudioDevAlsa( demux_t *p_demux )
442 demux_sys_t *p_sys = p_demux->p_sys;
443 const char *psz_device = p_sys->psz_device;
444 p_sys->p_alsa_pcm = NULL;
445 snd_pcm_hw_params_t *p_hw_params = NULL;
446 snd_pcm_uframes_t buffer_size;
447 snd_pcm_uframes_t chunk_size;
452 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device,
453 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
455 msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)",
456 psz_device, snd_strerror( i_err ) );
460 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
462 msg_Err( p_demux, "Cannot set ALSA nonblock (%s)",
463 snd_strerror( i_err ) );
467 /* Begin setting hardware parameters */
469 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
472 "ALSA: cannot allocate hardware parameter structure (%s)",
473 snd_strerror( i_err ) );
477 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
480 "ALSA: cannot initialize hardware parameter structure (%s)",
481 snd_strerror( i_err ) );
485 /* Set Interleaved access */
486 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
488 msg_Err( p_demux, "ALSA: cannot set access type (%s)",
489 snd_strerror( i_err ) );
493 /* Set 16 bit little endian */
494 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
496 msg_Err( p_demux, "ALSA: cannot set sample format (%s)",
497 snd_strerror( i_err ) );
501 /* Set sample rate */
502 #ifdef HAVE_ALSA_NEW_API
503 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
505 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
509 msg_Err( p_demux, "ALSA: cannot set sample rate (%s)",
510 snd_strerror( i_err ) );
515 unsigned int channels = p_sys->b_stereo ? 2 : 1;
516 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
518 channels = ( channels==1 ) ? 2 : 1;
519 msg_Warn( p_demux, "ALSA: cannot set channel count (%s). "
520 "Trying with channels=%d",
521 snd_strerror( i_err ),
523 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
525 msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
526 snd_strerror( i_err ) );
529 p_sys->b_stereo = ( channels == 2 );
532 /* Set metrics for buffer calculations later */
533 unsigned int buffer_time;
534 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
536 msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)",
537 snd_strerror( i_err ) );
540 if( buffer_time > 500000 ) buffer_time = 500000;
542 /* Set period time */
543 unsigned int period_time = buffer_time / 4;
544 #ifdef HAVE_ALSA_NEW_API
545 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
547 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
551 msg_Err( p_demux, "ALSA: cannot set period time (%s)",
552 snd_strerror( i_err ) );
556 /* Set buffer time */
557 #ifdef HAVE_ALSA_NEW_API
558 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
560 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
564 msg_Err( p_demux, "ALSA: cannot set buffer time (%s)",
565 snd_strerror( i_err ) );
569 /* Apply new hardware parameters */
570 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
572 msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)",
573 snd_strerror( i_err ) );
577 /* Get various buffer metrics */
578 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
579 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
580 if( chunk_size == buffer_size )
583 "ALSA: period cannot equal buffer size (%lu == %lu)",
584 chunk_size, buffer_size);
588 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
589 int bits_per_frame = bits_per_sample * channels;
591 p_sys->i_alsa_chunk_size = chunk_size;
592 p_sys->i_alsa_frame_size = bits_per_frame / 8;
593 p_sys->i_max_frame_size = chunk_size * bits_per_frame / 8;
595 snd_pcm_hw_params_free( p_hw_params );
599 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
602 "ALSA: cannot prepare audio interface for use (%s)",
603 snd_strerror( i_err ) );
607 snd_pcm_start( p_sys->p_alsa_pcm );
613 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
614 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
615 p_sys->p_alsa_pcm = NULL;
621 static int OpenAudioDev( demux_t *p_demux )
623 demux_sys_t *p_sys = p_demux->p_sys;
624 if( OpenAudioDevAlsa( p_demux ) != VLC_SUCCESS )
627 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
628 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
629 p_sys->i_sample_rate );
632 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
634 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
635 fmt.audio.i_rate = p_sys->i_sample_rate;
636 fmt.audio.i_bitspersample = 16;
637 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
638 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
640 msg_Dbg( p_demux, "new audio es %d channels %dHz",
641 fmt.audio.i_channels, fmt.audio.i_rate );
643 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
648 /*****************************************************************************
649 * ProbeAudioDevAlsa: probe audio for capabilities
650 *****************************************************************************/
651 static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
654 snd_pcm_t *p_alsa_pcm;
656 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
658 msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
662 snd_pcm_close( p_alsa_pcm );