]> git.sesse.net Git - vlc/blob - modules/access/alsa.c
Change default alsa device lookup
[vlc] / modules / access / alsa.c
1 /*****************************************************************************
2  * alsa.c : Alsa input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2009 the VideoLAN team
5  * $Id$
6  *
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>
11  *
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.
16  *
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.
21  *
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  *****************************************************************************/
26
27 /*
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
32  */
33
34 /*****************************************************************************
35  * Preamble
36  *****************************************************************************/
37
38 #ifdef HAVE_CONFIG_H
39 # include "config.h"
40 #endif
41
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>
47
48 #include <ctype.h>
49 #include <fcntl.h>
50 #include <unistd.h>
51 #include <sys/ioctl.h>
52 #include <sys/mman.h>
53
54 #include <sys/soundcard.h>
55
56 #define ALSA_PCM_NEW_HW_PARAMS_API
57 #define ALSA_PCM_NEW_SW_PARAMS_API
58 #include <alsa/asoundlib.h>
59
60 #include <poll.h>
61
62 /*****************************************************************************
63  * Module descriptior
64  *****************************************************************************/
65
66 static int  DemuxOpen ( vlc_object_t * );
67 static void DemuxClose( vlc_object_t * );
68
69 #define STEREO_TEXT N_( "Stereo" )
70 #define STEREO_LONGTEXT N_( \
71     "Capture the audio stream in stereo." )
72
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)" )
76
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." )
81
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 ." )
86
87 #define ALSA_DEFAULT "hw"
88 #define CFG_PREFIX "alsa-"
89
90 vlc_module_begin()
91     set_shortname( N_("Alsa") )
92     set_description( N_("Alsa audio capture input") )
93     set_category( CAT_INPUT )
94     set_subcategory( SUBCAT_INPUT_ACCESS )
95     set_help( HELP_TEXT )
96
97     add_shortcut( "alsa" )
98     set_capability( "access_demux", 10 )
99     set_callbacks( DemuxOpen, DemuxClose )
100
101     add_bool( CFG_PREFIX "stereo", true, NULL, STEREO_TEXT, STEREO_LONGTEXT,
102                 true )
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 )
107 vlc_module_end()
108
109 /*****************************************************************************
110  * Access: local prototypes
111  *****************************************************************************/
112
113 static int DemuxControl( demux_t *, int, va_list );
114
115 static int Demux( demux_t * );
116
117 static block_t* GrabAudio( demux_t *p_demux );
118
119 static int OpenAudioDev( demux_t *, const char * );
120 static bool ProbeAudioDevAlsa( demux_t *, const char * );
121 static char *ListAvailableDevices( demux_t *, bool b_probe );
122
123 struct demux_sys_t
124 {
125     /* Audio */
126     int i_cache;
127     unsigned int i_sample_rate;
128     bool b_stereo;
129     size_t i_max_frame_size;
130     block_t *p_block;
131     es_out_id_t *p_es;
132
133     /* ALSA Audio */
134     snd_pcm_t *p_alsa_pcm;
135     size_t i_alsa_frame_size;
136     int i_alsa_chunk_size;
137
138     int64_t i_next_demux_date; /* Used to handle alsa:// as input-slave properly */
139 };
140
141 static int FindMainDevice( demux_t *p_demux, const char *psz_device )
142 {
143     if( psz_device )
144     {
145         msg_Dbg( p_demux, "opening device '%s'", psz_device );
146         if( ProbeAudioDevAlsa( p_demux, psz_device ) )
147         {
148             msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
149             OpenAudioDev( p_demux, psz_device );
150         }
151     }
152     else if( ProbeAudioDevAlsa( p_demux, ALSA_DEFAULT ) )
153     {
154         msg_Dbg( p_demux, "'%s' is an audio device", ALSA_DEFAULT );
155         OpenAudioDev( p_demux, ALSA_DEFAULT );
156     }
157     else if( ( psz_device = ListAvailableDevices( p_demux, true ) ) )
158     {
159         msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
160         OpenAudioDev( p_demux, psz_device );
161         free( (char *)psz_device );
162     }
163
164     if( p_demux->p_sys->p_alsa_pcm == NULL )
165         return VLC_EGENERIC;
166     return VLC_SUCCESS;
167 }
168
169 static char *ListAvailableDevices( demux_t *p_demux, bool b_probe )
170 {
171     snd_ctl_card_info_t *p_info = NULL;
172     snd_ctl_card_info_alloca( &p_info );
173
174     snd_pcm_info_t *p_pcminfo = NULL;
175     snd_pcm_info_alloca( &p_pcminfo );
176
177     if( !b_probe )
178         msg_Dbg( p_demux, "Available alsa capture devices:" );
179     int i_card = -1;
180     while( !snd_card_next( &i_card ) && i_card >= 0 )
181     {
182         char psz_devname[10];
183         snprintf( psz_devname, 10, "hw:%d", i_card );
184
185         snd_ctl_t *p_ctl = NULL;
186         if( snd_ctl_open( &p_ctl, psz_devname, 0 ) < 0 ) continue;
187
188         snd_ctl_card_info( p_ctl, p_info );
189         if( !b_probe )
190             msg_Dbg( p_demux, "  %s (%s)",
191                      snd_ctl_card_info_get_id( p_info ),
192                      snd_ctl_card_info_get_name( p_info ) );
193
194         int i_dev = -1;
195         while( !snd_ctl_pcm_next_device( p_ctl, &i_dev ) && i_dev >= 0 )
196         {
197             snd_pcm_info_set_device( p_pcminfo, i_dev );
198             snd_pcm_info_set_subdevice( p_pcminfo, 0 );
199             snd_pcm_info_set_stream( p_pcminfo, SND_PCM_STREAM_CAPTURE );
200             if( snd_ctl_pcm_info( p_ctl, p_pcminfo ) < 0 ) continue;
201
202             if( !b_probe )
203                 msg_Dbg( p_demux, "    hw:%d,%d : %s (%s)", i_card, i_dev,
204                          snd_pcm_info_get_id( p_pcminfo ),
205                          snd_pcm_info_get_name( p_pcminfo ) );
206             else
207             {
208                 char *psz_device;
209                 if( asprintf( &psz_device, "hw:%d,%d", i_card, i_dev ) > 0 )
210                 {
211                     if( ProbeAudioDevAlsa( p_demux, psz_device ) )
212                     {
213                         snd_ctl_close( p_ctl );
214                         return psz_device;
215                     }
216                     else
217                         free( psz_device );
218                 }
219             }
220         }
221
222         snd_ctl_close( p_ctl );
223     }
224     return NULL;
225 }
226
227 /*****************************************************************************
228  * DemuxOpen: opens alsa device, access_demux callback
229  *****************************************************************************
230  *
231  * url: <alsa device>::::
232  *
233  *****************************************************************************/
234 static int DemuxOpen( vlc_object_t *p_this )
235 {
236     demux_t     *p_demux = (demux_t*)p_this;
237     demux_sys_t *p_sys;
238
239     /* Only when selected */
240     if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
241
242     /* Set up p_demux */
243     p_demux->pf_control = DemuxControl;
244     p_demux->pf_demux = Demux;
245     p_demux->info.i_update = 0;
246     p_demux->info.i_title = 0;
247     p_demux->info.i_seekpoint = 0;
248
249     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
250     if( p_sys == NULL ) return VLC_ENOMEM;
251
252     p_sys->i_sample_rate = var_CreateGetInteger( p_demux, CFG_PREFIX "samplerate" );
253     p_sys->b_stereo = var_CreateGetBool( p_demux, CFG_PREFIX "stereo" );
254     p_sys->i_cache = var_CreateGetInteger( p_demux, CFG_PREFIX "caching" );
255     p_sys->p_es = NULL;
256     p_sys->p_block = NULL;
257     p_sys->i_next_demux_date = -1;
258
259     const char *psz_device = NULL;
260     if( p_demux->psz_path && *p_demux->psz_path )
261         psz_device = p_demux->psz_path;
262     else
263         ListAvailableDevices( p_demux, false );
264
265     if( FindMainDevice( p_demux, psz_device ) != VLC_SUCCESS )
266     {
267         if( p_demux->psz_path && *p_demux->psz_path )
268             ListAvailableDevices( p_demux, false );
269         DemuxClose( p_this );
270         return VLC_EGENERIC;
271     }
272
273     return VLC_SUCCESS;
274 }
275
276 /*****************************************************************************
277  * Close: close device, free resources
278  *****************************************************************************/
279 static void DemuxClose( vlc_object_t *p_this )
280 {
281     demux_t     *p_demux = (demux_t *)p_this;
282     demux_sys_t *p_sys   = p_demux->p_sys;
283
284     if( p_sys->p_alsa_pcm )
285     {
286         snd_pcm_close( p_sys->p_alsa_pcm );
287     }
288
289     if( p_sys->p_block ) block_Release( p_sys->p_block );
290
291     free( p_sys );
292 }
293
294 /*****************************************************************************
295  * DemuxControl:
296  *****************************************************************************/
297 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
298 {
299     demux_sys_t *p_sys = p_demux->p_sys;
300
301     switch( i_query )
302     {
303         /* Special for access_demux */
304         case DEMUX_CAN_PAUSE:
305         case DEMUX_CAN_SEEK:
306         case DEMUX_SET_PAUSE_STATE:
307         case DEMUX_CAN_CONTROL_PACE:
308             *va_arg( args, bool * ) = false;
309             return VLC_SUCCESS;
310
311         case DEMUX_GET_PTS_DELAY:
312             *va_arg( args, int64_t * ) = (int64_t)p_sys->i_cache * 1000;
313             return VLC_SUCCESS;
314
315         case DEMUX_GET_TIME:
316             *va_arg( args, int64_t * ) = mdate();
317             return VLC_SUCCESS;
318
319         case DEMUX_SET_NEXT_DEMUX_TIME:
320             p_sys->i_next_demux_date = va_arg( args, int64_t );
321             return VLC_SUCCESS;
322
323         /* TODO implement others */
324         default:
325             return VLC_EGENERIC;
326     }
327
328     return VLC_EGENERIC;
329 }
330
331 /*****************************************************************************
332  * Demux: Processes the audio frame
333  *****************************************************************************/
334 static int Demux( demux_t *p_demux )
335 {
336     demux_sys_t *p_sys = p_demux->p_sys;
337
338     block_t *p_block = NULL;
339
340     do
341     {
342         if( p_block )
343         {
344             es_out_Send( p_demux->out, p_sys->p_es, p_block );
345             p_block = NULL;
346         }
347
348         /* Wait for data */
349         int i_wait = snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */
350         switch( i_wait )
351         {
352             case 1:
353             {
354                 p_block = GrabAudio( p_demux );
355                 if( p_block )
356                     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
357             }
358
359             /* FIXME: this is a copy paste from below. Shouldn't be needed
360              * twice. */
361             case -EPIPE:
362                 /* xrun */
363                 snd_pcm_prepare( p_sys->p_alsa_pcm );
364                 break;
365             case -ESTRPIPE:
366             {
367                 /* suspend */
368                 int i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
369                 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
370                 break;
371             }
372             /* </FIXME> */
373         }
374     } while( p_block && p_sys->i_next_demux_date > 0 &&
375              p_block->i_pts < p_sys->i_next_demux_date );
376
377     if( p_block )
378         es_out_Send( p_demux->out, p_sys->p_es, p_block );
379
380     return 1;
381 }
382
383
384 /*****************************************************************************
385  * GrabAudio: Grab an audio frame
386  *****************************************************************************/
387 static block_t* GrabAudio( demux_t *p_demux )
388 {
389     demux_sys_t *p_sys = p_demux->p_sys;
390     int i_read, i_correct;
391     block_t *p_block;
392
393     if( p_sys->p_block ) p_block = p_sys->p_block;
394     else p_block = block_New( p_demux, p_sys->i_max_frame_size );
395
396     if( !p_block )
397     {
398         msg_Warn( p_demux, "cannot get block" );
399         return 0;
400     }
401
402     p_sys->p_block = p_block;
403
404     /* ALSA */
405     i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer, p_sys->i_alsa_chunk_size );
406     if( i_read <= 0 )
407     {
408         int i_resume;
409         switch( i_read )
410         {
411             case -EAGAIN:
412                 break;
413             case -EPIPE:
414                 /* xrun */
415                 snd_pcm_prepare( p_sys->p_alsa_pcm );
416                 break;
417             case -ESTRPIPE:
418                 /* suspend */
419                 i_resume = snd_pcm_resume( p_sys->p_alsa_pcm );
420                 if( i_resume < 0 && i_resume != -EAGAIN ) snd_pcm_prepare( p_sys->p_alsa_pcm );
421                 break;
422             default:
423                 msg_Err( p_demux, "Failed to read alsa frame (%s)", snd_strerror( i_read ) );
424                 return 0;
425         }
426     }
427     else
428     {
429         /* convert from frames to bytes */
430         i_read *= p_sys->i_alsa_frame_size;
431     }
432
433     if( i_read <= 0 ) return 0;
434
435     p_block->i_buffer = i_read;
436     p_sys->p_block = 0;
437
438     /* Correct the date because of kernel buffering */
439     i_correct = i_read;
440     /* ALSA */
441     int i_err;
442     snd_pcm_sframes_t delay = 0;
443     if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
444     {
445         size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
446         /* Test for overrun */
447         if( i_correction_delta > p_sys->i_max_frame_size )
448         {
449             msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
450                       i_correction_delta, p_sys->i_max_frame_size );
451             i_correction_delta = p_sys->i_max_frame_size;
452             snd_pcm_prepare( p_sys->p_alsa_pcm );
453         }
454         i_correct += i_correction_delta;
455     }
456     else
457     {
458         /* delay failed so reset */
459         msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
460         snd_pcm_prepare( p_sys->p_alsa_pcm );
461     }
462
463     /* Timestamp */
464     p_block->i_pts = p_block->i_dts =
465         mdate() - INT64_C(1000000) * (mtime_t)i_correct /
466         2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
467
468     return p_block;
469 }
470
471 /*****************************************************************************
472  * OpenAudioDev: open and set up the audio device and probe for capabilities
473  *****************************************************************************/
474 static int OpenAudioDevAlsa( demux_t *p_demux, const char *psz_device )
475 {
476     demux_sys_t *p_sys = p_demux->p_sys;
477     p_sys->p_alsa_pcm = NULL;
478     snd_pcm_hw_params_t *p_hw_params = NULL;
479     snd_pcm_uframes_t buffer_size;
480     snd_pcm_uframes_t chunk_size;
481
482     /* ALSA */
483     int i_err;
484
485     if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device,
486         SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
487     {
488         msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)",
489                  psz_device, snd_strerror( i_err ) );
490         goto adev_fail;
491     }
492
493     if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
494     {
495         msg_Err( p_demux, "Cannot set ALSA nonblock (%s)",
496                  snd_strerror( i_err ) );
497         goto adev_fail;
498     }
499
500     /* Begin setting hardware parameters */
501
502     if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
503     {
504         msg_Err( p_demux,
505                  "ALSA: cannot allocate hardware parameter structure (%s)",
506                  snd_strerror( i_err ) );
507         goto adev_fail;
508     }
509
510     if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
511     {
512         msg_Err( p_demux,
513                 "ALSA: cannot initialize hardware parameter structure (%s)",
514                  snd_strerror( i_err ) );
515         goto adev_fail;
516     }
517
518     /* Set Interleaved access */
519     if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
520     {
521         msg_Err( p_demux, "ALSA: cannot set access type (%s)",
522                  snd_strerror( i_err ) );
523         goto adev_fail;
524     }
525
526     /* Set 16 bit little endian */
527     if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params, SND_PCM_FORMAT_S16_LE ) ) < 0 )
528     {
529         msg_Err( p_demux, "ALSA: cannot set sample format (%s)",
530                  snd_strerror( i_err ) );
531         goto adev_fail;
532     }
533
534     /* Set sample rate */
535     i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params, &p_sys->i_sample_rate, NULL );
536     if( i_err < 0 )
537     {
538         msg_Err( p_demux, "ALSA: cannot set sample rate (%s)",
539                  snd_strerror( i_err ) );
540         goto adev_fail;
541     }
542
543     /* Set channels */
544     unsigned int channels = p_sys->b_stereo ? 2 : 1;
545     if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
546     {
547         channels = ( channels==1 ) ? 2 : 1;
548         msg_Warn( p_demux, "ALSA: cannot set channel count (%s). "
549                   "Trying with channels=%d",
550                   snd_strerror( i_err ),
551                   channels );
552         if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params, channels ) ) < 0 )
553         {
554             msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
555                      snd_strerror( i_err ) );
556             goto adev_fail;
557         }
558         p_sys->b_stereo = ( channels == 2 );
559     }
560
561     /* Set metrics for buffer calculations later */
562     unsigned int buffer_time;
563     if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
564     {
565         msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)",
566                  snd_strerror( i_err ) );
567         goto adev_fail;
568     }
569     if( buffer_time > 500000 ) buffer_time = 500000;
570
571     /* Set period time */
572     unsigned int period_time = buffer_time / 4;
573     i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params, &period_time, 0 );
574     if( i_err < 0 )
575     {
576         msg_Err( p_demux, "ALSA: cannot set period time (%s)",
577                  snd_strerror( i_err ) );
578         goto adev_fail;
579     }
580
581     /* Set buffer time */
582     i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params, &buffer_time, 0 );
583     if( i_err < 0 )
584     {
585         msg_Err( p_demux, "ALSA: cannot set buffer time (%s)",
586                  snd_strerror( i_err ) );
587         goto adev_fail;
588     }
589
590     /* Apply new hardware parameters */
591     if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
592     {
593         msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)",
594                  snd_strerror( i_err ) );
595         goto adev_fail;
596     }
597
598     /* Get various buffer metrics */
599     snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
600     snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
601     if( chunk_size == buffer_size )
602     {
603         msg_Err( p_demux,
604                  "ALSA: period cannot equal buffer size (%lu == %lu)",
605                  chunk_size, buffer_size);
606         goto adev_fail;
607     }
608
609     int bits_per_sample = snd_pcm_format_physical_width(SND_PCM_FORMAT_S16_LE);
610     int bits_per_frame = bits_per_sample * channels;
611
612     p_sys->i_alsa_chunk_size = chunk_size;
613     p_sys->i_alsa_frame_size = bits_per_frame / 8;
614     p_sys->i_max_frame_size = chunk_size * bits_per_frame / 8;
615
616     snd_pcm_hw_params_free( p_hw_params );
617     p_hw_params = NULL;
618
619     /* Prep device */
620     if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
621     {
622         msg_Err( p_demux,
623                  "ALSA: cannot prepare audio interface for use (%s)",
624                  snd_strerror( i_err ) );
625         goto adev_fail;
626     }
627
628     snd_pcm_start( p_sys->p_alsa_pcm );
629
630     return VLC_SUCCESS;
631
632  adev_fail:
633
634     if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
635     if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
636     p_sys->p_alsa_pcm = NULL;
637
638     return VLC_EGENERIC;
639
640 }
641
642 static int OpenAudioDev( demux_t *p_demux, const char *psz_device )
643 {
644     demux_sys_t *p_sys = p_demux->p_sys;
645     if( OpenAudioDevAlsa( p_demux, psz_device ) != VLC_SUCCESS )
646         return VLC_EGENERIC;
647
648     msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
649              psz_device, p_sys->b_stereo ? "stereo" : "mono",
650              p_sys->i_sample_rate );
651
652     es_format_t fmt;
653     es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
654
655     fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
656     fmt.audio.i_rate = p_sys->i_sample_rate;
657     fmt.audio.i_bitspersample = 16;
658     fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
659     fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
660
661     msg_Dbg( p_demux, "new audio es %d channels %dHz",
662              fmt.audio.i_channels, fmt.audio.i_rate );
663
664     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
665
666     return VLC_SUCCESS;
667 }
668
669 /*****************************************************************************
670  * ProbeAudioDevAlsa: probe audio for capabilities
671  *****************************************************************************/
672 static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
673 {
674     int i_err;
675     snd_pcm_t *p_alsa_pcm;
676
677     if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
678     {
679         msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
680         return false;
681     }
682
683     snd_pcm_close( p_alsa_pcm );
684
685     return true;
686 }