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