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 HELP_TEXT N_( \
83 "Use alsa:// to open the default audio input. If multiple audio " \
84 "inputs are available, they will be listed in the vlc debug output. " \
85 "To select hw:0,1 , use alsa://hw:0,1 ." )
87 #define ALSA_DEFAULT "hw"
88 #define CFG_PREFIX "alsa-"
91 set_shortname( N_("Alsa") )
92 set_description( N_("Alsa audio capture input") )
93 set_category( CAT_INPUT )
94 set_subcategory( SUBCAT_INPUT_ACCESS )
97 add_shortcut( "alsa" )
98 set_capability( "access_demux", 10 )
99 set_callbacks( DemuxOpen, DemuxClose )
101 add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
103 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
104 SAMPLERATE_LONGTEXT, true )
105 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
106 CACHING_TEXT, CACHING_LONGTEXT, true )
109 /*****************************************************************************
110 * Access: local prototypes
111 *****************************************************************************/
113 static int DemuxControl( demux_t *, int, va_list );
115 static int Demux( demux_t * );
117 static block_t* GrabAudio( demux_t *p_demux );
119 static int OpenAudioDev( demux_t * );
120 static bool ProbeAudioDevAlsa( demux_t *, const char *psz_device );
124 const char *psz_device; /* Alsa device from MRL */
128 unsigned int i_sample_rate;
130 size_t i_max_frame_size;
135 snd_pcm_t *p_alsa_pcm;
136 size_t i_alsa_frame_size;
137 int i_alsa_chunk_size;
139 int64_t i_next_demux_date; /* Used to handle alsa:// as input-slave properly */
142 static int FindMainDevice( demux_t *p_demux )
144 /* TODO: if using default device, loop through all alsa devices until
146 msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
147 if( ProbeAudioDevAlsa( p_demux, p_demux->p_sys->psz_device ) )
149 msg_Dbg( p_demux, "'%s' is an audio device",
150 p_demux->p_sys->psz_device );
151 OpenAudioDev( p_demux );
154 if( p_demux->p_sys->p_alsa_pcm == NULL )
159 static void ListAvailableDevices( demux_t *p_demux )
161 snd_ctl_card_info_t *p_info = NULL;
162 snd_ctl_card_info_alloca( &p_info );
164 snd_pcm_info_t *p_pcminfo = NULL;
165 snd_pcm_info_alloca( &p_pcminfo );
167 msg_Dbg( p_demux, "Available alsa capture devices:" );
169 while( !snd_card_next( &i_card ) && i_card >= 0 )
171 char psz_devname[10];
172 snprintf( psz_devname, 10, "hw:%d", i_card );
174 snd_ctl_t *p_ctl = NULL;
175 if( snd_ctl_open( &p_ctl, psz_devname, 0 ) < 0 ) continue;
177 snd_ctl_card_info( p_ctl, p_info );
178 msg_Dbg( p_demux, " %s (%s)",
179 snd_ctl_card_info_get_id( p_info ),
180 snd_ctl_card_info_get_name( p_info ) );
183 while( !snd_ctl_pcm_next_device( p_ctl, &i_dev ) && i_dev >= 0 )
185 snd_pcm_info_set_device( p_pcminfo, i_dev );
186 snd_pcm_info_set_subdevice( p_pcminfo, 0 );
187 snd_pcm_info_set_stream( p_pcminfo, SND_PCM_STREAM_CAPTURE );
188 if( snd_ctl_pcm_info( p_ctl, p_pcminfo ) < 0 ) continue;
190 msg_Dbg( p_demux, " hw:%d,%d : %s (%s)", i_card, i_dev,
191 snd_pcm_info_get_id( p_pcminfo ),
192 snd_pcm_info_get_name( p_pcminfo ) );
195 snd_ctl_close( p_ctl );
199 /*****************************************************************************
200 * DemuxOpen: opens alsa device, access_demux callback
201 *****************************************************************************
203 * url: <alsa device>::::
205 *****************************************************************************/
206 static int DemuxOpen( vlc_object_t *p_this )
208 demux_t *p_demux = (demux_t*)p_this;
211 /* Only when selected */
212 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
215 p_demux->pf_control = DemuxControl;
216 p_demux->pf_demux = Demux;
217 p_demux->info.i_update = 0;
218 p_demux->info.i_title = 0;
219 p_demux->info.i_seekpoint = 0;
221 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
222 if( p_sys == NULL ) return VLC_ENOMEM;
224 p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
225 p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
226 p_sys->i_cache = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
228 p_sys->p_block = NULL;
229 p_sys->i_next_demux_date = -1;
231 if( p_demux->psz_path && *p_demux->psz_path )
232 p_sys->psz_device = p_demux->psz_path;
235 p_sys->psz_device = ALSA_DEFAULT;
236 ListAvailableDevices( p_demux );
239 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
241 DemuxClose( p_this );
248 /*****************************************************************************
249 * Close: close device, free resources
250 *****************************************************************************/
251 static void DemuxClose( vlc_object_t *p_this )
253 demux_t *p_demux = (demux_t *)p_this;
254 demux_sys_t *p_sys = p_demux->p_sys;
256 if( p_sys->p_alsa_pcm )
258 snd_pcm_close( p_sys->p_alsa_pcm );
261 if( p_sys->p_block ) block_Release( p_sys->p_block );
266 /*****************************************************************************
268 *****************************************************************************/
269 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
271 demux_sys_t *p_sys = p_demux->p_sys;
275 /* Special for access_demux */
276 case DEMUX_CAN_PAUSE:
278 case DEMUX_SET_PAUSE_STATE:
279 case DEMUX_CAN_CONTROL_PACE:
280 *va_arg( args, bool * ) = false;
283 case DEMUX_GET_PTS_DELAY:
284 *va_arg( args, int64_t * ) = (int64_t)p_sys->i_cache * 1000;
288 *va_arg( args, int64_t * ) = mdate();
291 case DEMUX_SET_NEXT_DEMUX_TIME:
292 p_sys->i_next_demux_date = va_arg( args, int64_t );
295 /* TODO implement others */
303 /*****************************************************************************
304 * Demux: Processes the audio frame
305 *****************************************************************************/
306 static int Demux( demux_t *p_demux )
308 demux_sys_t *p_sys = p_demux->p_sys;
310 block_t *p_block = NULL;
316 es_out_Send( p_demux->out, p_sys->p_es, p_block );
321 int i_wait = snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */
326 p_block = GrabAudio( p_demux );
328 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
331 /* FIXME: this is a copy paste from below. Shouldn't be needed
335 snd_pcm_prepare( p_sys->p_alsa_pcm );
340 int i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
341 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
346 } while( p_block && p_sys->i_next_demux_date > 0 &&
347 p_block->i_pts < p_sys->i_next_demux_date );
350 es_out_Send( p_demux->out, p_sys->p_es, p_block );
356 /*****************************************************************************
357 * GrabAudio: Grab an audio frame
358 *****************************************************************************/
359 static block_t* GrabAudio( demux_t *p_demux )
361 demux_sys_t *p_sys = p_demux->p_sys;
362 int i_read, i_correct;
365 if( p_sys->p_block ) p_block = p_sys->p_block;
366 else p_block = block_New( p_demux, p_sys->i_max_frame_size );
370 msg_Warn( p_demux, "cannot get block" );
374 p_sys->p_block = p_block;
377 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
387 snd_pcm_prepare( p_sys->p_alsa_pcm );
391 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
392 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
395 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
401 /* convert from frames to bytes */
402 i_read *= p_sys->i_alsa_frame_size;
405 if( i_read <= 0 ) return 0;
407 p_block->i_buffer = i_read;
410 /* Correct the date because of kernel buffering */
414 snd_pcm_sframes_t delay = 0;
415 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
417 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
418 /* Test for overrun */
419 if( i_correction_delta > p_sys->i_max_frame_size )
421 msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
422 i_correction_delta, p_sys->i_max_frame_size );
423 i_correction_delta = p_sys->i_max_frame_size;
424 snd_pcm_prepare( p_sys->p_alsa_pcm );
426 i_correct += i_correction_delta;
430 /* delay failed so reset */
431 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
432 snd_pcm_prepare( p_sys->p_alsa_pcm );
436 p_block->i_pts = p_block->i_dts =
437 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
438 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
443 /*****************************************************************************
444 * OpenAudioDev: open and set up the audio device and probe for capabilities
445 *****************************************************************************/
446 static int OpenAudioDevAlsa( demux_t *p_demux )
448 demux_sys_t *p_sys = p_demux->p_sys;
449 const char *psz_device = p_sys->psz_device;
450 p_sys->p_alsa_pcm = NULL;
451 snd_pcm_hw_params_t *p_hw_params = NULL;
452 snd_pcm_uframes_t buffer_size;
453 snd_pcm_uframes_t chunk_size;
458 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device,
459 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
461 msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)",
462 psz_device, snd_strerror( i_err ) );
466 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
468 msg_Err( p_demux, "Cannot set ALSA nonblock (%s)",
469 snd_strerror( i_err ) );
473 /* Begin setting hardware parameters */
475 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
478 "ALSA: cannot allocate hardware parameter structure (%s)",
479 snd_strerror( i_err ) );
483 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
486 "ALSA: cannot initialize hardware parameter structure (%s)",
487 snd_strerror( i_err ) );
491 /* Set Interleaved access */
492 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
494 msg_Err( p_demux, "ALSA: cannot set access type (%s)",
495 snd_strerror( i_err ) );
499 /* Set 16 bit little endian */
500 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
502 msg_Err( p_demux, "ALSA: cannot set sample format (%s)",
503 snd_strerror( i_err ) );
507 /* Set sample rate */
508 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
511 msg_Err( p_demux, "ALSA: cannot set sample rate (%s)",
512 snd_strerror( i_err ) );
517 unsigned int channels = p_sys->b_stereo ? 2 : 1;
518 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
520 channels = ( channels==1 ) ? 2 : 1;
521 msg_Warn( p_demux, "ALSA: cannot set channel count (%s). "
522 "Trying with channels=%d",
523 snd_strerror( i_err ),
525 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
527 msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
528 snd_strerror( i_err ) );
531 p_sys->b_stereo = ( channels == 2 );
534 /* Set metrics for buffer calculations later */
535 unsigned int buffer_time;
536 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
538 msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)",
539 snd_strerror( i_err ) );
542 if( buffer_time > 500000 ) buffer_time = 500000;
544 /* Set period time */
545 unsigned int period_time = buffer_time / 4;
546 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
549 msg_Err( p_demux, "ALSA: cannot set period time (%s)",
550 snd_strerror( i_err ) );
554 /* Set buffer time */
555 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
558 msg_Err( p_demux, "ALSA: cannot set buffer time (%s)",
559 snd_strerror( i_err ) );
563 /* Apply new hardware parameters */
564 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
566 msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)",
567 snd_strerror( i_err ) );
571 /* Get various buffer metrics */
572 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
573 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
574 if( chunk_size == buffer_size )
577 "ALSA: period cannot equal buffer size (%lu == %lu)",
578 chunk_size, buffer_size);
582 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
583 int bits_per_frame = bits_per_sample * channels;
585 p_sys->i_alsa_chunk_size = chunk_size;
586 p_sys->i_alsa_frame_size = bits_per_frame / 8;
587 p_sys->i_max_frame_size = chunk_size * bits_per_frame / 8;
589 snd_pcm_hw_params_free( p_hw_params );
593 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
596 "ALSA: cannot prepare audio interface for use (%s)",
597 snd_strerror( i_err ) );
601 snd_pcm_start( p_sys->p_alsa_pcm );
607 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
608 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
609 p_sys->p_alsa_pcm = NULL;
615 static int OpenAudioDev( demux_t *p_demux )
617 demux_sys_t *p_sys = p_demux->p_sys;
618 if( OpenAudioDevAlsa( p_demux ) != VLC_SUCCESS )
621 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
622 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
623 p_sys->i_sample_rate );
626 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
628 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
629 fmt.audio.i_rate = p_sys->i_sample_rate;
630 fmt.audio.i_bitspersample = 16;
631 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
632 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
634 msg_Dbg( p_demux, "new audio es %d channels %dHz",
635 fmt.audio.i_channels, fmt.audio.i_rate );
637 p_sys->p_es = es_out_Add( p_demux->out, &fmt );
642 /*****************************************************************************
643 * ProbeAudioDevAlsa: probe audio for capabilities
644 *****************************************************************************/
645 static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
648 snd_pcm_t *p_alsa_pcm;
650 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
652 msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
656 snd_pcm_close( p_alsa_pcm );