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>
52 #include <sys/ioctl.h>
55 #include <sys/soundcard.h>
57 #define ALSA_PCM_NEW_HW_PARAMS_API
58 #define ALSA_PCM_NEW_SW_PARAMS_API
59 #include <alsa/asoundlib.h>
63 /*****************************************************************************
65 *****************************************************************************/
67 static int DemuxOpen ( vlc_object_t * );
68 static void DemuxClose( vlc_object_t * );
70 #define STEREO_TEXT N_( "Stereo" )
71 #define STEREO_LONGTEXT N_( \
72 "Capture the audio stream in stereo." )
74 #define SAMPLERATE_TEXT N_( "Samplerate" )
75 #define SAMPLERATE_LONGTEXT N_( \
76 "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
78 #define CACHING_TEXT N_("Caching value in ms")
79 #define CACHING_LONGTEXT N_( \
80 "Caching value for Alsa captures. This " \
81 "value should be set in milliseconds." )
83 #define ALSA_DEFAULT "hw"
84 #define CFG_PREFIX "alsa-"
87 set_shortname( N_("Alsa") );
88 set_description( N_("Alsa audio capture input") );
89 set_category( CAT_INPUT );
90 set_subcategory( SUBCAT_INPUT_ACCESS );
92 add_shortcut( "alsa" );
93 set_capability( "access_demux", 10 );
94 set_callbacks( DemuxOpen, DemuxClose );
96 add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
98 add_integer( CFG_PREFIX "samplerate", 48000, NULL, SAMPLERATE_TEXT,
99 SAMPLERATE_LONGTEXT, true );
100 add_integer( CFG_PREFIX "caching", DEFAULT_PTS_DELAY / 1000, NULL,
101 CACHING_TEXT, CACHING_LONGTEXT, true );
104 /*****************************************************************************
105 * Access: local prototypes
106 *****************************************************************************/
108 static int DemuxControl( demux_t *, int, va_list );
110 static int Demux( demux_t * );
112 static block_t* GrabAudio( demux_t *p_demux );
114 static int OpenAudioDev( demux_t * );
115 static bool ProbeAudioDevAlsa( demux_t *, const char *psz_device );
119 const char *psz_device; /* Alsa device from MRL */
123 unsigned int i_sample_rate;
125 size_t i_audio_max_frame_size;
126 block_t *p_block_audio;
127 es_out_id_t *p_es_audio;
130 snd_pcm_t *p_alsa_pcm;
131 size_t i_alsa_frame_size;
132 int i_alsa_chunk_size;
135 static int FindMainDevice( demux_t *p_demux )
137 msg_Dbg( p_demux, "opening device '%s'", p_demux->p_sys->psz_device );
138 if( ProbeAudioDevAlsa( p_demux, p_demux->p_sys->psz_device ) )
140 msg_Dbg( p_demux, "'%s' is an audio device",
141 p_demux->p_sys->psz_device );
142 OpenAudioDev( p_demux );
145 if( p_demux->p_sys->p_alsa_pcm == NULL )
150 /*****************************************************************************
151 * DemuxOpen: opens alsa device, access_demux callback
152 *****************************************************************************
154 * url: <alsa device>::::
156 *****************************************************************************/
157 static int DemuxOpen( vlc_object_t *p_this )
159 demux_t *p_demux = (demux_t*)p_this;
162 /* Only when selected */
163 if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
166 p_demux->pf_control = DemuxControl;
167 p_demux->pf_demux = Demux;
168 p_demux->info.i_update = 0;
169 p_demux->info.i_title = 0;
170 p_demux->info.i_seekpoint = 0;
172 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
173 if( p_sys == NULL ) return VLC_ENOMEM;
175 p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
176 p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
177 p_sys->i_pts = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
178 p_sys->psz_device = NULL;
179 p_sys->p_es_audio = NULL;
180 p_sys->p_block_audio = NULL;
182 if( p_demux->psz_path && *p_demux->psz_path )
183 p_sys->psz_device = p_demux->psz_path;
185 p_sys->psz_device = ALSA_DEFAULT;
187 if( FindMainDevice( p_demux ) != VLC_SUCCESS )
189 DemuxClose( p_this );
196 /*****************************************************************************
197 * Close: close device, free resources
198 *****************************************************************************/
199 static void DemuxClose( vlc_object_t *p_this )
201 demux_t *p_demux = (demux_t *)p_this;
202 demux_sys_t *p_sys = p_demux->p_sys;
204 if( p_sys->p_alsa_pcm )
206 snd_pcm_close( p_sys->p_alsa_pcm );
209 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
214 /*****************************************************************************
216 *****************************************************************************/
217 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
219 demux_sys_t *p_sys = p_demux->p_sys;
225 /* Special for access_demux */
226 case DEMUX_CAN_PAUSE:
228 case DEMUX_SET_PAUSE_STATE:
229 case DEMUX_CAN_CONTROL_PACE:
230 pb = (bool*)va_arg( args, bool * );
234 case DEMUX_GET_PTS_DELAY:
235 pi64 = (int64_t*)va_arg( args, int64_t * );
236 *pi64 = (int64_t)p_sys->i_pts * 1000;
240 pi64 = (int64_t*)va_arg( args, int64_t * );
244 /* TODO implement others */
252 /*****************************************************************************
253 * Demux: Processes the audio frame
254 *****************************************************************************/
255 static int Demux( demux_t *p_demux )
257 demux_sys_t *p_sys = p_demux->p_sys;
259 int i_wait = snd_pcm_wait( p_sys->p_alsa_pcm, 500 );
264 block_t *p_block = GrabAudio( p_demux );
267 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
268 es_out_Send( p_demux->out, p_sys->p_es_audio, p_block );
274 snd_pcm_prepare( p_sys->p_alsa_pcm );
279 int i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
280 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
289 /*****************************************************************************
290 * GrabAudio: Grab an audio frame
291 *****************************************************************************/
292 static block_t* GrabAudio( demux_t *p_demux )
294 demux_sys_t *p_sys = p_demux->p_sys;
295 int i_read, i_correct;
298 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
299 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
303 msg_Warn( p_demux, "cannot get block" );
307 p_sys->p_block_audio = p_block;
310 i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
320 snd_pcm_prepare( p_sys->p_alsa_pcm );
324 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
325 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
328 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
334 /* convert from frames to bytes */
335 i_read *= p_sys->i_alsa_frame_size;
338 if( i_read <= 0 ) return 0;
340 p_block->i_buffer = i_read;
341 p_sys->p_block_audio = 0;
343 /* Correct the date because of kernel buffering */
347 snd_pcm_sframes_t delay = 0;
348 if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
350 size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
351 /* Test for overrun */
352 if( i_correction_delta > p_sys->i_audio_max_frame_size )
354 msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
355 i_correction_delta, p_sys->i_audio_max_frame_size );
356 i_correction_delta = p_sys->i_audio_max_frame_size;
357 snd_pcm_prepare( p_sys->p_alsa_pcm );
359 i_correct += i_correction_delta;
363 /* delay failed so reset */
364 msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
365 snd_pcm_prepare( p_sys->p_alsa_pcm );
369 p_block->i_pts = p_block->i_dts =
370 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
371 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
376 /*****************************************************************************
377 * OpenAudioDev: open and set up the audio device and probe for capabilities
378 *****************************************************************************/
379 static int OpenAudioDevAlsa( demux_t *p_demux )
381 demux_sys_t *p_sys = p_demux->p_sys;
382 const char *psz_device = p_sys->psz_device;
383 p_sys->p_alsa_pcm = NULL;
384 snd_pcm_hw_params_t *p_hw_params = NULL;
385 snd_pcm_uframes_t buffer_size;
386 snd_pcm_uframes_t chunk_size;
391 if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device,
392 SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
394 msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)",
395 psz_device, snd_strerror( i_err ) );
399 if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
401 msg_Err( p_demux, "Cannot set ALSA nonblock (%s)",
402 snd_strerror( i_err ) );
406 /* Begin setting hardware parameters */
408 if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
411 "ALSA: cannot allocate hardware parameter structure (%s)",
412 snd_strerror( i_err ) );
416 if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
419 "ALSA: cannot initialize hardware parameter structure (%s)",
420 snd_strerror( i_err ) );
424 /* Set Interleaved access */
425 if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
427 msg_Err( p_demux, "ALSA: cannot set access type (%s)",
428 snd_strerror( i_err ) );
432 /* Set 16 bit little endian */
433 if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
435 msg_Err( p_demux, "ALSA: cannot set sample format (%s)",
436 snd_strerror( i_err ) );
440 /* Set sample rate */
441 #ifdef HAVE_ALSA_NEW_API
442 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
444 i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, p_sys->i_sample_rate, NULL );
448 msg_Err( p_demux, "ALSA: cannot set sample rate (%s)",
449 snd_strerror( i_err ) );
454 unsigned int channels = p_sys->b_stereo ? 2 : 1;
455 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
457 channels = ( channels==1 ) ? 2 : 1;
458 msg_Warn( p_demux, "ALSA: cannot set channel count (%s). "
459 "Trying with channels=%d",
460 snd_strerror( i_err ),
462 if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
464 msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
465 snd_strerror( i_err ) );
468 p_sys->b_stereo = ( channels == 2 );
471 /* Set metrics for buffer calculations later */
472 unsigned int buffer_time;
473 if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
475 msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)",
476 snd_strerror( i_err ) );
479 if( buffer_time > 500000 ) buffer_time = 500000;
481 /* Set period time */
482 unsigned int period_time = buffer_time / 4;
483 #ifdef HAVE_ALSA_NEW_API
484 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
486 i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, period_time, 0 );
490 msg_Err( p_demux, "ALSA: cannot set period time (%s)",
491 snd_strerror( i_err ) );
495 /* Set buffer time */
496 #ifdef HAVE_ALSA_NEW_API
497 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
499 i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, buffer_time, 0 );
503 msg_Err( p_demux, "ALSA: cannot set buffer time (%s)",
504 snd_strerror( i_err ) );
508 /* Apply new hardware parameters */
509 if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
511 msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)",
512 snd_strerror( i_err ) );
516 /* Get various buffer metrics */
517 snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
518 snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
519 if( chunk_size == buffer_size )
522 "ALSA: period cannot equal buffer size (%lu == %lu)",
523 chunk_size, buffer_size);
527 int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
528 int bits_per_frame = bits_per_sample * channels;
530 p_sys->i_alsa_chunk_size = chunk_size;
531 p_sys->i_alsa_frame_size = bits_per_frame / 8;
532 p_sys->i_audio_max_frame_size = chunk_size * bits_per_frame / 8;
534 snd_pcm_hw_params_free( p_hw_params );
538 if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
541 "ALSA: cannot prepare audio interface for use (%s)",
542 snd_strerror( i_err ) );
546 snd_pcm_start( p_sys->p_alsa_pcm );
552 if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
553 if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
554 p_sys->p_alsa_pcm = NULL;
560 static int OpenAudioDev( demux_t *p_demux )
562 demux_sys_t *p_sys = p_demux->p_sys;
563 if( OpenAudioDevAlsa( p_demux ) != VLC_SUCCESS )
566 msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
567 p_sys->psz_device, p_sys->b_stereo ? "stereo" : "mono",
568 p_sys->i_sample_rate );
571 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
573 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
574 fmt.audio.i_rate = p_sys->i_sample_rate;
575 fmt.audio.i_bitspersample = 16;
576 fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
577 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
579 msg_Dbg( p_demux, "new audio es %d channels %dHz",
580 fmt.audio.i_channels, fmt.audio.i_rate );
582 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
587 /*****************************************************************************
588 * ProbeAudioDevAlsa: probe audio for capabilities
589 *****************************************************************************/
590 static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
593 snd_pcm_t *p_alsa_pcm;
595 if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
597 msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
601 snd_pcm_close( p_alsa_pcm );