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