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