]> git.sesse.net Git - vlc/blob - modules/access/alsa.c
Move intf_EJect() out of core
[vlc] / modules / access / alsa.c
1 /*****************************************************************************
2  * alsa.c : Alsa input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2002-2011 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_fourcc.h>
48 #include <vlc_aout.h>
49
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 FORMAT_TEXT N_( "Capture format (default s16l)" )
74 #define FORMAT_LONGTEXT N_( \
75     "Capture format of audio stream." )
76
77 #define SAMPLERATE_TEXT N_( "Samplerate" )
78 #define SAMPLERATE_LONGTEXT N_( \
79     "Samplerate of the captured audio stream, in Hz (eg: 11025, 22050, 44100, 48000)" )
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 static const char *const ppsz_fourcc[] = {
90     "u8", "s8", "gsm", "u16l", "s16l", "u16b", "s16b",
91     "u24l", "s24l", "u24b", "s24b", "u32l", "s32l",
92     "u32b", "s32b", "f32l", "f32b", "f64l", "f64b"
93 };
94 static const char *const ppsz_fourcc_text[] = {
95     N_("PCM U8"), N_("PCM S8"), N_("GSM Audio"),
96     N_("PCM U16 LE"), N_("PCM S16 LE"),
97     N_("PCM U16 BE"), N_("PCM S16 BE"),
98     N_("PCM U24 LE"), N_("PCM S24 LE"),
99     N_("PCM U24 BE"), N_("PCM S24 BE"),
100     N_("PCM U32 LE"), N_("PCM S32 LE"),
101     N_("PCM U32 BE"), N_("PCM S32 BE"),
102     N_("PCM F32 LE"), N_("PCM F32 BE"),
103     N_("PCM F64 LE"), N_("PCM F64 BE")
104 };
105
106 vlc_module_begin()
107     set_shortname( N_("ALSA") )
108     set_description( N_("ALSA audio capture input") )
109     set_category( CAT_INPUT )
110     set_subcategory( SUBCAT_INPUT_ACCESS )
111     set_help( HELP_TEXT )
112
113     add_shortcut( "alsa" )
114     set_capability( "access_demux", 10 )
115     set_callbacks( DemuxOpen, DemuxClose )
116
117     add_bool( CFG_PREFIX "stereo", true, STEREO_TEXT, STEREO_LONGTEXT,
118                 true )
119     add_string( CFG_PREFIX "format", "s16l", FORMAT_TEXT,
120                 FORMAT_LONGTEXT, true )
121         change_string_list( ppsz_fourcc, ppsz_fourcc_text, 0 )
122     add_integer( CFG_PREFIX "samplerate", 48000, SAMPLERATE_TEXT,
123                 SAMPLERATE_LONGTEXT, true )
124 vlc_module_end()
125
126 /*****************************************************************************
127  * Access: local prototypes
128  *****************************************************************************/
129
130 static int DemuxControl( demux_t *, int, va_list );
131
132 static int Demux( demux_t * );
133
134 static block_t* GrabAudio( demux_t *p_demux );
135
136 static int OpenAudioDev( demux_t *, const char * );
137 static bool ProbeAudioDevAlsa( demux_t *, const char * );
138 static char *ListAvailableDevices( demux_t *, bool b_probe );
139
140 struct demux_sys_t
141 {
142     /* Audio */
143     unsigned int i_sample_rate;
144     bool b_stereo;
145     vlc_fourcc_t i_format;
146     size_t i_max_frame_size;
147     block_t *p_block;
148     es_out_id_t *p_es;
149
150     /* ALSA Audio */
151     snd_pcm_t *p_alsa_pcm;
152     size_t i_alsa_frame_size;
153     int i_alsa_chunk_size;
154
155     int64_t i_next_demux_date; /* Used to handle alsa:// as input-slave properly */
156 };
157
158 static int FindMainDevice( demux_t *p_demux, const char *psz_device )
159 {
160     if( psz_device )
161     {
162         msg_Dbg( p_demux, "opening device '%s'", psz_device );
163         if( ProbeAudioDevAlsa( p_demux, psz_device ) )
164         {
165             msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
166             OpenAudioDev( p_demux, psz_device );
167         }
168     }
169     else if( ProbeAudioDevAlsa( p_demux, ALSA_DEFAULT ) )
170     {
171         msg_Dbg( p_demux, "'%s' is an audio device", ALSA_DEFAULT );
172         OpenAudioDev( p_demux, ALSA_DEFAULT );
173     }
174     else if( ( psz_device = ListAvailableDevices( p_demux, true ) ) )
175     {
176         msg_Dbg( p_demux, "'%s' is an audio device", psz_device );
177         OpenAudioDev( p_demux, psz_device );
178         free( (char *)psz_device );
179     }
180
181     if( p_demux->p_sys->p_alsa_pcm == NULL )
182         return VLC_EGENERIC;
183     return VLC_SUCCESS;
184 }
185
186 static char *ListAvailableDevices( demux_t *p_demux, bool b_probe )
187 {
188     snd_ctl_card_info_t *p_info = NULL;
189     snd_ctl_card_info_alloca( &p_info );
190
191     snd_pcm_info_t *p_pcminfo = NULL;
192     snd_pcm_info_alloca( &p_pcminfo );
193
194     if( !b_probe )
195         msg_Dbg( p_demux, "Available alsa capture devices:" );
196     int i_card = -1;
197     while( !snd_card_next( &i_card ) && i_card >= 0 )
198     {
199         char psz_devname[10];
200         snprintf( psz_devname, 10, "hw:%d", i_card );
201
202         snd_ctl_t *p_ctl = NULL;
203         if( snd_ctl_open( &p_ctl, psz_devname, 0 ) < 0 ) continue;
204
205         snd_ctl_card_info( p_ctl, p_info );
206         if( !b_probe )
207             msg_Dbg( p_demux, "  %s (%s)",
208                      snd_ctl_card_info_get_id( p_info ),
209                      snd_ctl_card_info_get_name( p_info ) );
210
211         int i_dev = -1;
212         while( !snd_ctl_pcm_next_device( p_ctl, &i_dev ) && i_dev >= 0 )
213         {
214             snd_pcm_info_set_device( p_pcminfo, i_dev );
215             snd_pcm_info_set_subdevice( p_pcminfo, 0 );
216             snd_pcm_info_set_stream( p_pcminfo, SND_PCM_STREAM_CAPTURE );
217             if( snd_ctl_pcm_info( p_ctl, p_pcminfo ) < 0 ) continue;
218
219             if( !b_probe )
220                 msg_Dbg( p_demux, "    hw:%d,%d : %s (%s)", i_card, i_dev,
221                          snd_pcm_info_get_id( p_pcminfo ),
222                          snd_pcm_info_get_name( p_pcminfo ) );
223             else
224             {
225                 char *psz_device;
226                 if( asprintf( &psz_device, "hw:%d,%d", i_card, i_dev ) > 0 )
227                 {
228                     if( ProbeAudioDevAlsa( p_demux, psz_device ) )
229                     {
230                         snd_ctl_close( p_ctl );
231                         return psz_device;
232                     }
233                     else
234                         free( psz_device );
235                 }
236             }
237         }
238
239         snd_ctl_close( p_ctl );
240     }
241     return NULL;
242 }
243
244 /*****************************************************************************
245  * DemuxOpen: opens alsa device, access_demux callback
246  *****************************************************************************
247  *
248  * url: <alsa device>::::
249  *
250  *****************************************************************************/
251 static int DemuxOpen( vlc_object_t *p_this )
252 {
253     demux_t     *p_demux = (demux_t*)p_this;
254     demux_sys_t *p_sys;
255
256     /* Only when selected */
257     if( *p_demux->psz_access == '\0' ) return VLC_EGENERIC;
258
259     /* Set up p_demux */
260     p_demux->pf_control = DemuxControl;
261     p_demux->pf_demux = Demux;
262     p_demux->info.i_update = 0;
263     p_demux->info.i_title = 0;
264     p_demux->info.i_seekpoint = 0;
265
266     p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
267     if( p_sys == NULL ) return VLC_ENOMEM;
268
269     p_sys->i_sample_rate = var_InheritInteger( p_demux, CFG_PREFIX "samplerate" );
270     p_sys->b_stereo = var_InheritBool( p_demux, CFG_PREFIX "stereo" );
271     p_sys->p_es = NULL;
272     p_sys->p_block = NULL;
273     p_sys->i_next_demux_date = -1;
274
275     char *psz_format = var_InheritString( p_demux, CFG_PREFIX "format" );
276     p_sys->i_format = vlc_fourcc_GetCodecFromString( AUDIO_ES, psz_format );
277     free( psz_format );
278
279     const char *psz_device = NULL;
280     if( p_demux->psz_location && *p_demux->psz_location )
281         psz_device = p_demux->psz_location;
282     else
283         ListAvailableDevices( p_demux, false );
284
285     if( FindMainDevice( p_demux, psz_device ) != VLC_SUCCESS )
286     {
287         if( p_demux->psz_location && *p_demux->psz_location )
288             ListAvailableDevices( p_demux, false );
289         DemuxClose( p_this );
290         return VLC_EGENERIC;
291     }
292
293     return VLC_SUCCESS;
294 }
295
296 /*****************************************************************************
297  * Close: close device, free resources
298  *****************************************************************************/
299 static void DemuxClose( vlc_object_t *p_this )
300 {
301     demux_t     *p_demux = (demux_t *)p_this;
302     demux_sys_t *p_sys   = p_demux->p_sys;
303
304     if( p_sys->p_alsa_pcm )
305     {
306         snd_pcm_close( p_sys->p_alsa_pcm );
307     }
308
309     if( p_sys->p_block ) block_Release( p_sys->p_block );
310
311     free( p_sys );
312 }
313
314 /*****************************************************************************
315  * DemuxControl:
316  *****************************************************************************/
317 static int DemuxControl( demux_t *p_demux, int i_query, va_list args )
318 {
319     demux_sys_t *p_sys = p_demux->p_sys;
320
321     switch( i_query )
322     {
323         /* Special for access_demux */
324         case DEMUX_CAN_PAUSE:
325         case DEMUX_CAN_SEEK:
326         case DEMUX_SET_PAUSE_STATE:
327         case DEMUX_CAN_CONTROL_PACE:
328             *va_arg( args, bool * ) = false;
329             return VLC_SUCCESS;
330
331         case DEMUX_GET_PTS_DELAY:
332             *va_arg( args, int64_t * ) =
333                 INT64_C(1000) * var_InheritInteger( p_demux, "live-caching" );
334             return VLC_SUCCESS;
335
336         case DEMUX_GET_TIME:
337             *va_arg( args, int64_t * ) = mdate();
338             return VLC_SUCCESS;
339
340         case DEMUX_SET_NEXT_DEMUX_TIME:
341             p_sys->i_next_demux_date = va_arg( args, int64_t );
342             return VLC_SUCCESS;
343
344         /* TODO implement others */
345         default:
346             return VLC_EGENERIC;
347     }
348
349     return VLC_EGENERIC;
350 }
351
352 /*****************************************************************************
353  * Demux: Processes the audio frame
354  *****************************************************************************/
355 static int Demux( demux_t *p_demux )
356 {
357     demux_sys_t *p_sys = p_demux->p_sys;
358
359     block_t *p_block = NULL;
360
361     do
362     {
363         p_block = GrabAudio( p_demux );
364         if( p_block )
365         {
366             es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
367             es_out_Send( p_demux->out, p_sys->p_es, p_block );
368             p_block = NULL;
369         }
370
371     } while( p_block && p_sys->i_next_demux_date > 0 &&
372              p_block->i_pts < p_sys->i_next_demux_date );
373
374     if( p_block )
375         es_out_Send( p_demux->out, p_sys->p_es, p_block );
376
377     return 1;
378 }
379
380 /*****************************************************************************
381  * GrabAudio: Grab an audio frame
382  *****************************************************************************/
383 static block_t* GrabAudio( demux_t *p_demux )
384 {
385     demux_sys_t *p_sys = p_demux->p_sys;
386     int i_read, i_correct;
387     block_t *p_block;
388
389     if( p_sys->p_block ) p_block = p_sys->p_block;
390     else p_block = block_New( p_demux, p_sys->i_max_frame_size );
391
392     if( !p_block )
393     {
394         msg_Warn( p_demux, "cannot get block" );
395         return NULL;
396     }
397
398     p_sys->p_block = p_block;
399
400     /* ALSA */
401     i_read = snd_pcm_readi( p_sys->p_alsa_pcm, p_block->p_buffer,
402                             p_sys->i_alsa_chunk_size );
403     if( i_read == -EAGAIN )
404     {
405         snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */
406         return NULL;
407     }
408
409     if( i_read < 0 )
410         i_read = snd_pcm_recover( p_sys->p_alsa_pcm, i_read, 0 );
411
412     if( i_read <= 0 )
413     {
414         switch( i_read )
415         {
416             case 0: /* state recovered or no data */
417                 return NULL;
418             case -EAGAIN:
419                 snd_pcm_wait( p_sys->p_alsa_pcm, 10 ); /* See poll() comment in oss.c */
420                 return NULL;
421             default:
422                 msg_Err( p_demux, "Failed to read alsa frame (%s)",
423                          snd_strerror( i_read ) );
424                 return NULL;
425         }
426     }
427
428     /* convert from frames to bytes */
429     i_read *= p_sys->i_alsa_frame_size;
430
431     p_block->i_buffer = i_read;
432     p_sys->p_block = 0;
433
434     /* Correct the date because of kernel buffering */
435     i_correct = i_read;
436     /* ALSA */
437     int i_err;
438     snd_pcm_sframes_t delay = 0;
439     if( ( i_err = snd_pcm_delay( p_sys->p_alsa_pcm, &delay ) ) >= 0 )
440     {
441         size_t i_correction_delta = delay * p_sys->i_alsa_frame_size;
442         /* Test for overrun */
443         if( i_correction_delta > p_sys->i_max_frame_size )
444         {
445             msg_Warn( p_demux, "ALSA read overrun (%zu > %zu)",
446                       i_correction_delta, p_sys->i_max_frame_size );
447             i_correction_delta = p_sys->i_max_frame_size;
448             snd_pcm_prepare( p_sys->p_alsa_pcm );
449         }
450         i_correct += i_correction_delta;
451     }
452     else
453     {
454         /* delay failed so reset */
455         msg_Warn( p_demux, "ALSA snd_pcm_delay failed (%s)", snd_strerror( i_err ) );
456         snd_pcm_prepare( p_sys->p_alsa_pcm );
457     }
458
459     /* Timestamp */
460     p_block->i_pts = p_block->i_dts =
461         mdate() - INT64_C(1000000) * (mtime_t)i_correct /
462         2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
463
464     return p_block;
465 }
466
467 static snd_pcm_format_t GetAlsaPCMFormat( demux_t *p_demux, const vlc_fourcc_t i_format )
468 {
469     demux_sys_t *p_sys = p_demux->p_sys;
470
471     switch( i_format )
472     {
473         case VLC_CODEC_U8: return SND_PCM_FORMAT_U8;
474         case VLC_CODEC_S8: return SND_PCM_FORMAT_S8;
475
476         case VLC_CODEC_GSM: return SND_PCM_FORMAT_GSM;
477
478         case VLC_CODEC_U16L: return SND_PCM_FORMAT_U16_LE;
479         case VLC_CODEC_S16L: return SND_PCM_FORMAT_S16_LE;
480         case VLC_CODEC_U16B: return SND_PCM_FORMAT_U16_BE;
481         case VLC_CODEC_S16B: return SND_PCM_FORMAT_S16_BE;
482
483         case VLC_CODEC_U24L: return SND_PCM_FORMAT_U24_3LE;
484         case VLC_CODEC_S24L: return SND_PCM_FORMAT_S24_3LE;
485         case VLC_CODEC_U24B: return SND_PCM_FORMAT_U24_3BE;
486         case VLC_CODEC_S24B: return SND_PCM_FORMAT_S24_3BE;
487
488         case VLC_CODEC_U32L: return SND_PCM_FORMAT_U32_LE;
489         case VLC_CODEC_U32B: return SND_PCM_FORMAT_U32_BE;
490         case VLC_CODEC_S32L: return SND_PCM_FORMAT_S32_LE;
491         case VLC_CODEC_S32B: return SND_PCM_FORMAT_S32_BE;
492         case VLC_CODEC_F32L: return SND_PCM_FORMAT_FLOAT_LE;
493         case VLC_CODEC_F32B: return SND_PCM_FORMAT_FLOAT_BE;
494
495         case VLC_CODEC_F64L: return SND_PCM_FORMAT_FLOAT64_LE;
496         case VLC_CODEC_F64B: return SND_PCM_FORMAT_FLOAT64_BE;
497
498         default:
499             msg_Err( p_demux, "ALSA: unsupported sample format '%s' falling back to 's16l'",
500                               (const char *)&i_format );
501             p_sys->i_format = VLC_CODEC_S16L;
502     }
503
504     return SND_PCM_FORMAT_S16_LE;
505 }
506
507 /*****************************************************************************
508  * OpenAudioDev: open and set up the audio device and probe for capabilities
509  *****************************************************************************/
510 static int OpenAudioDevAlsa( demux_t *p_demux, const char *psz_device )
511 {
512     demux_sys_t *p_sys = p_demux->p_sys;
513     p_sys->p_alsa_pcm = NULL;
514     snd_pcm_hw_params_t *p_hw_params = NULL;
515     snd_pcm_format_t i_alsa_pcm_format;
516     snd_pcm_uframes_t buffer_size;
517     snd_pcm_uframes_t chunk_size;
518
519     /* ALSA */
520     int i_err;
521
522     if( ( i_err = snd_pcm_open( &p_sys->p_alsa_pcm, psz_device,
523         SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0)
524     {
525         msg_Err( p_demux, "Cannot open ALSA audio device %s (%s)",
526                  psz_device, snd_strerror( i_err ) );
527         goto adev_fail;
528     }
529
530     if( ( i_err = snd_pcm_nonblock( p_sys->p_alsa_pcm, 1 ) ) < 0)
531     {
532         msg_Err( p_demux, "Cannot set ALSA nonblock (%s)",
533                  snd_strerror( i_err ) );
534         goto adev_fail;
535     }
536
537     /* Begin setting hardware parameters */
538
539     if( ( i_err = snd_pcm_hw_params_malloc( &p_hw_params ) ) < 0 )
540     {
541         msg_Err( p_demux,
542                  "ALSA: cannot allocate hardware parameter structure (%s)",
543                  snd_strerror( i_err ) );
544         goto adev_fail;
545     }
546
547     if( ( i_err = snd_pcm_hw_params_any( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
548     {
549         msg_Err( p_demux,
550                 "ALSA: cannot initialize hardware parameter structure (%s)",
551                  snd_strerror( i_err ) );
552         goto adev_fail;
553     }
554
555     /* Set Interleaved access */
556     if( ( i_err = snd_pcm_hw_params_set_access( p_sys->p_alsa_pcm, p_hw_params,
557                                         SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
558     {
559         msg_Err( p_demux, "ALSA: cannot set access type (%s)",
560                  snd_strerror( i_err ) );
561         goto adev_fail;
562     }
563
564     /* Set capture format, default is signed 16 bit little endian */
565     i_alsa_pcm_format = GetAlsaPCMFormat( p_demux, p_sys->i_format );
566     if( ( i_err = snd_pcm_hw_params_set_format( p_sys->p_alsa_pcm, p_hw_params,
567                                                 i_alsa_pcm_format ) ) < 0 )
568     {
569         msg_Err( p_demux, "ALSA: cannot set sample format (%s)",
570                  snd_strerror( i_err ) );
571         goto adev_fail;
572     }
573
574     /* Set sample rate */
575     i_err = snd_pcm_hw_params_set_rate_near( p_sys->p_alsa_pcm, p_hw_params,
576                                              &p_sys->i_sample_rate, NULL );
577     if( i_err < 0 )
578     {
579         msg_Err( p_demux, "ALSA: cannot set sample rate (%s)",
580                  snd_strerror( i_err ) );
581         goto adev_fail;
582     }
583
584     /* Set channels */
585     unsigned int channels = p_sys->b_stereo ? 2 : 1;
586     if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params,
587                                                   channels ) ) < 0 )
588     {
589         channels = ( channels==1 ) ? 2 : 1;
590         msg_Warn( p_demux, "ALSA: cannot set channel count (%s). "
591                   "Trying with channels=%d",
592                   snd_strerror( i_err ),
593                   channels );
594         if( ( i_err = snd_pcm_hw_params_set_channels( p_sys->p_alsa_pcm, p_hw_params,
595                                                       channels ) ) < 0 )
596         {
597             msg_Err( p_demux, "ALSA: cannot set channel count (%s)",
598                      snd_strerror( i_err ) );
599             goto adev_fail;
600         }
601         p_sys->b_stereo = ( channels == 2 );
602     }
603
604     /* Set metrics for buffer calculations later */
605     unsigned int buffer_time;
606     if( ( i_err = snd_pcm_hw_params_get_buffer_time_max(p_hw_params, &buffer_time, 0) ) < 0 )
607     {
608         msg_Err( p_demux, "ALSA: cannot get buffer time max (%s)",
609                  snd_strerror( i_err ) );
610         goto adev_fail;
611     }
612     if( buffer_time > 500000 ) buffer_time = 500000;
613
614     /* Set period time */
615     unsigned int period_time = buffer_time / 4;
616     i_err = snd_pcm_hw_params_set_period_time_near( p_sys->p_alsa_pcm, p_hw_params,
617                                                     &period_time, 0 );
618     if( i_err < 0 )
619     {
620         msg_Err( p_demux, "ALSA: cannot set period time (%s)",
621                  snd_strerror( i_err ) );
622         goto adev_fail;
623     }
624
625     /* Set buffer time */
626     i_err = snd_pcm_hw_params_set_buffer_time_near( p_sys->p_alsa_pcm, p_hw_params,
627                                                     &buffer_time, 0 );
628     if( i_err < 0 )
629     {
630         msg_Err( p_demux, "ALSA: cannot set buffer time (%s)",
631                  snd_strerror( i_err ) );
632         goto adev_fail;
633     }
634
635     /* Apply new hardware parameters */
636     if( ( i_err = snd_pcm_hw_params( p_sys->p_alsa_pcm, p_hw_params ) ) < 0 )
637     {
638         msg_Err( p_demux, "ALSA: cannot set hw parameters (%s)",
639                  snd_strerror( i_err ) );
640         goto adev_fail;
641     }
642
643     /* Get various buffer metrics */
644     snd_pcm_hw_params_get_period_size( p_hw_params, &chunk_size, 0 );
645     snd_pcm_hw_params_get_buffer_size( p_hw_params, &buffer_size );
646     if( chunk_size == buffer_size )
647     {
648         msg_Err( p_demux,
649                  "ALSA: period cannot equal buffer size (%lu == %lu)",
650                  chunk_size, buffer_size);
651         goto adev_fail;
652     }
653
654     int bits_per_sample = snd_pcm_format_physical_width(i_alsa_pcm_format);
655     int bits_per_frame = bits_per_sample * channels;
656
657     p_sys->i_alsa_chunk_size = chunk_size;
658     p_sys->i_alsa_frame_size = bits_per_frame / 8;
659     p_sys->i_max_frame_size = chunk_size * bits_per_frame / 8;
660
661     snd_pcm_hw_params_free( p_hw_params );
662     p_hw_params = NULL;
663
664     /* Prep device */
665     if( ( i_err = snd_pcm_prepare( p_sys->p_alsa_pcm ) ) < 0 )
666     {
667         msg_Err( p_demux,
668                  "ALSA: cannot prepare audio interface for use (%s)",
669                  snd_strerror( i_err ) );
670         goto adev_fail;
671     }
672
673     snd_pcm_start( p_sys->p_alsa_pcm );
674
675     return VLC_SUCCESS;
676
677  adev_fail:
678
679     if( p_hw_params ) snd_pcm_hw_params_free( p_hw_params );
680     if( p_sys->p_alsa_pcm ) snd_pcm_close( p_sys->p_alsa_pcm );
681     p_sys->p_alsa_pcm = NULL;
682
683     return VLC_EGENERIC;
684 }
685
686 static int OpenAudioDev( demux_t *p_demux, const char *psz_device )
687 {
688     demux_sys_t *p_sys = p_demux->p_sys;
689     if( OpenAudioDevAlsa( p_demux, psz_device ) != VLC_SUCCESS )
690         return VLC_EGENERIC;
691
692     msg_Dbg( p_demux, "opened adev=`%s' %s %dHz codec '%s'",
693              psz_device, p_sys->b_stereo ? "stereo" : "mono",
694              p_sys->i_sample_rate,
695              vlc_fourcc_GetDescription( AUDIO_ES, p_sys->i_format ) );
696
697     es_format_t fmt;
698     es_format_Init( &fmt, AUDIO_ES, p_sys->i_format );
699
700     fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
701     fmt.audio.i_rate = p_sys->i_sample_rate;
702     fmt.audio.i_bitspersample = aout_BitsPerSample( p_sys->i_format );
703     fmt.audio.i_blockalign = fmt.audio.i_channels * fmt.audio.i_bitspersample / 8;
704     fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate * fmt.audio.i_bitspersample;
705
706     msg_Dbg( p_demux, "new audio es %d channels %dHz",
707              fmt.audio.i_channels, fmt.audio.i_rate );
708
709     p_sys->p_es = es_out_Add( p_demux->out, &fmt );
710
711     return VLC_SUCCESS;
712 }
713
714 /*****************************************************************************
715  * ProbeAudioDevAlsa: probe audio for capabilities
716  *****************************************************************************/
717 static bool ProbeAudioDevAlsa( demux_t *p_demux, const char *psz_device )
718 {
719     int i_err;
720     snd_pcm_t *p_alsa_pcm;
721
722     if( ( i_err = snd_pcm_open( &p_alsa_pcm, psz_device, SND_PCM_STREAM_CAPTURE, SND_PCM_NONBLOCK ) ) < 0 )
723     {
724         msg_Err( p_demux, "cannot open device %s for ALSA audio (%s)", psz_device, snd_strerror( i_err ) );
725         return false;
726     }
727
728     snd_pcm_close( p_alsa_pcm );
729
730     return true;
731 }