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