]> git.sesse.net Git - vlc/blob - modules/audio_output/alsa.c
* ./modules/audio_output/alsa.c: Woody ALSA compilation fix.
[vlc] / modules / audio_output / alsa.c
1 /*****************************************************************************
2  * alsa.c : alsa plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: alsa.c,v 1.21 2003/02/03 00:39:42 sam Exp $
6  *
7  * Authors: Henri Fallon <henri@videolan.org> - Original Author
8  *          Jeffrey Baker <jwbaker@acm.org> - Port to ALSA 1.0 API
9  *          John Paul Lorenti <jpl31@columbia.edu> - Device selection
10  *          Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr> - S/PDIF and aout3
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
25  *****************************************************************************/
26
27 /*****************************************************************************
28  * Preamble
29  *****************************************************************************/
30 #include <errno.h>                                                 /* ENOMEM */
31 #include <string.h>                                            /* strerror() */
32 #include <stdlib.h>                            /* calloc(), malloc(), free() */
33
34 #include <vlc/vlc.h>
35
36 #include <vlc/aout.h>
37
38 #include "aout_internal.h"
39
40 /* ALSA part
41    Note: we use the new API which is available since 0.9.0beta10a. */
42 #define ALSA_PCM_NEW_HW_PARAMS_API
43 #define ALSA_PCM_NEW_SW_PARAMS_API
44 #include <alsa/asoundlib.h>
45
46 /*****************************************************************************
47  * aout_sys_t: ALSA audio output method descriptor
48  *****************************************************************************
49  * This structure is part of the audio output thread descriptor.
50  * It describes the ALSA specific properties of an audio device.
51  *****************************************************************************/
52 struct aout_sys_t
53 {
54     snd_pcm_t         * p_snd_pcm;
55     int                 i_period_time;
56
57 #ifdef DEBUG
58     snd_output_t      * p_snd_stderr;
59 #endif
60 };
61
62 #define A52_FRAME_NB 1536
63
64 /* These values are in frames.
65    To convert them to a number of bytes you have to multiply them by the
66    number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
67    2 for s16). */
68 #define ALSA_DEFAULT_PERIOD_SIZE        2048
69 #define ALSA_DEFAULT_BUFFER_SIZE        ( ALSA_DEFAULT_PERIOD_SIZE << 4 )
70 #define ALSA_SPDIF_PERIOD_SIZE          A52_FRAME_NB
71 #define ALSA_SPDIF_BUFFER_SIZE          ( ALSA_SPDIF_PERIOD_SIZE << 4 )
72 /* Why << 4 ? --Meuuh */
73 /* Why not ? --Bozo */
74 /* Right. --Meuuh */
75
76 #define DEFAULT_ALSA_DEVICE "default"
77
78 /*****************************************************************************
79  * Local prototypes
80  *****************************************************************************/
81 static int  Open         ( vlc_object_t * );
82 static void Close        ( vlc_object_t * );
83 static void Play         ( aout_instance_t * );
84 static int  ALSAThread   ( aout_instance_t * );
85 static void ALSAFill     ( aout_instance_t * );
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 #define SPDIF_TEXT N_("Try to use S/PDIF output")
91 #define SPDIF_LONGTEXT N_( \
92     "Sometimes we attempt to use the S/PDIF output, even if nothing is " \
93     "connected to it. Un-checking this option disables this behaviour, " \
94     "and permanently selects analog PCM output." )
95
96 vlc_module_begin();
97     add_category_hint( N_("ALSA"), NULL );
98     add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
99                 N_("ALSA device name"), NULL );
100     add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT );
101     set_description( _("ALSA audio module") );
102     set_capability( "audio output", 50 );
103     set_callbacks( Open, Close );
104 vlc_module_end();
105
106 /*****************************************************************************
107  * Probe: probe the audio device for available formats and channels
108  *****************************************************************************/
109 static void Probe( aout_instance_t * p_aout,
110                    const char * psz_device, const char * psz_iec_device,
111                    int i_snd_pcm_format )
112 {
113     struct aout_sys_t * p_sys = p_aout->output.p_sys;
114     vlc_value_t val;
115
116     var_Create ( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
117
118     /* Test for S/PDIF device if needed */
119     if ( psz_iec_device )
120     {
121         /* Opening the device should be enough */
122         if ( config_GetInt( p_aout, "spdif" )
123               && !snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
124                                 SND_PCM_STREAM_PLAYBACK, 0 ) )
125         {
126             val.psz_string = N_("A/52 over S/PDIF");
127             var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
128             snd_pcm_close( p_sys->p_snd_pcm );
129         }
130     }
131
132     /* Now test linear PCM capabilities */
133     if ( !snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
134                         SND_PCM_STREAM_PLAYBACK, 0 ) )
135     {
136         int i_channels;
137         snd_pcm_hw_params_t * p_hw;
138         snd_pcm_hw_params_alloca (&p_hw);
139
140         if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
141         {
142             msg_Warn( p_aout, "unable to retrieve initial hardware parameters"
143                               ", disabling linear PCM audio" );
144             snd_pcm_close( p_sys->p_snd_pcm );
145             return;
146         }
147
148         if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
149                                            i_snd_pcm_format ) < 0 )
150         {
151             /* Assume a FPU enabled computer can handle float32 format.
152                If somebody tells us it's not always true then we'll have
153                to change this */
154             msg_Warn( p_aout, "unable to set stream sample size and word order"
155                               ", disabling linear PCM audio" );
156             snd_pcm_close( p_sys->p_snd_pcm );
157             return;
158         }
159
160         i_channels = aout_FormatNbChannels( &p_aout->output.output );
161
162         while ( i_channels > 1 )
163         {
164             /* Here we have to probe multi-channel capabilities but I have
165                no idea (at the moment) of how its managed by the ALSA
166                library.
167                It seems that '6' channels aren't well handled on a stereo
168                sound card like my i810 but it requires some more
169                investigations. That's why '4' and '6' cases are disabled.
170                -- Bozo */
171             if ( !snd_pcm_hw_params_test_channels( p_sys->p_snd_pcm, p_hw,
172                                                    i_channels ) )
173             {
174                 switch ( i_channels )
175                 {
176                 case 1:
177                     val.psz_string = N_("Mono");
178                     var_Change( p_aout, "audio-device",
179                                 VLC_VAR_ADDCHOICE, &val );
180                     break;
181                 case 2:
182                     val.psz_string = N_("Stereo");
183                     var_Change( p_aout, "audio-device",
184                                 VLC_VAR_ADDCHOICE, &val );
185                     break;
186 /*
187                 case 4:
188                     val.psz_string = N_("2 Front 2 Rear");
189                     var_Change( p_aout, "audio-device",
190                                 VLC_VAR_ADDCHOICE, &val );
191                     break;
192                 case 6:
193                     val.psz_string = N_("5.1");
194                     var_Change( p_aout, "audio-device",
195                                 VLC_VAR_ADDCHOICE, &val );
196                     break;
197 */
198                 }
199             }
200
201             --i_channels;
202         }
203
204         /* Close the previously opened device */
205         snd_pcm_close( p_sys->p_snd_pcm );
206     }
207
208     /* Add final settings to the variable */
209     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
210     val.b_bool = VLC_TRUE;
211     var_Set( p_aout, "intf-change", val );
212 }
213
214 /*****************************************************************************
215  * Open: create a handle and open an alsa device
216  *****************************************************************************
217  * This function opens an alsa device, through the alsa API.
218  *
219  * Note: the only heap-allocated string is psz_device. All the other pointers
220  * are references to psz_device or to stack-allocated data.
221  *****************************************************************************/
222 static int Open( vlc_object_t *p_this )
223 {
224     aout_instance_t * p_aout = (aout_instance_t *)p_this;
225     struct aout_sys_t * p_sys;
226     vlc_value_t val;
227
228     char psz_default_iec_device[128]; /* Buffer used to store the default
229                                          S/PDIF device */
230     char * psz_device, * psz_iec_device; /* device names for PCM and S/PDIF
231                                             output */
232
233     int i_vlc_pcm_format; /* Audio format for VLC's data */
234     int i_snd_pcm_format; /* Audio format for ALSA's data */
235
236     snd_pcm_uframes_t i_buffer_size = 0;
237     snd_pcm_uframes_t i_period_size = 0;
238     int i_channels = 0;
239
240     snd_pcm_hw_params_t *p_hw;
241     snd_pcm_sw_params_t *p_sw;
242
243     int i_snd_rc = -1;
244
245     /* Allocate structures */
246     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
247     if( p_sys == NULL )
248     {
249         msg_Err( p_aout, "out of memory" );
250         return VLC_ENOMEM;
251     }
252
253     /* Get device name */
254     if( (psz_device = config_GetPsz( p_aout, "alsadev" )) == NULL )
255     {
256         msg_Err( p_aout, "no audio device given (maybe \"default\" ?)" );
257         free( p_sys );
258         return VLC_EGENERIC;
259     }
260
261     /* Choose the IEC device for S/PDIF output:
262        if the device is overriden by the user then it will be the one
263        otherwise we compute the default device based on the output format. */
264     if( AOUT_FMT_NON_LINEAR( &p_aout->output.output )
265         && !strcmp( psz_device, DEFAULT_ALSA_DEVICE ) )
266     {
267         snprintf( psz_default_iec_device, sizeof(psz_default_iec_device),
268                   "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
269                   IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
270                   IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
271                   0,
272                   ( p_aout->output.output.i_rate == 48000 ?
273                     IEC958_AES3_CON_FS_48000 :
274                     ( p_aout->output.output.i_rate == 44100 ?
275                       IEC958_AES3_CON_FS_44100 : IEC958_AES3_CON_FS_32000 ) ) );
276         psz_iec_device = psz_default_iec_device;
277     }
278     else if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
279     {
280         psz_iec_device = psz_device;
281     }
282     else
283     {
284         psz_iec_device = NULL;
285     }
286
287     /* Choose the linear PCM format (read the comment above about FPU
288        and float32) */
289     if( p_aout->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
290     {
291         i_vlc_pcm_format = VLC_FOURCC('f','l','3','2');
292         i_snd_pcm_format = SND_PCM_FORMAT_FLOAT;
293     }
294     else
295     {
296         i_vlc_pcm_format = AOUT_FMT_S16_NE;
297         i_snd_pcm_format = SND_PCM_FORMAT_S16;
298     }
299
300     /* If the variable doesn't exist then it's the first time we're called
301        and we have to probe the available audio formats and channels */
302     if ( var_Type( p_aout, "audio-device" ) == 0 )
303     {
304         Probe( p_aout, psz_device, psz_iec_device, i_snd_pcm_format );
305     }
306
307     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
308     {
309         free( p_sys );
310         free( psz_device );
311         return VLC_EGENERIC;
312     }
313
314     if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
315     {
316         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
317     }
318     else if ( !strcmp( val.psz_string, N_("5.1") ) )
319     {
320         p_aout->output.output.i_format = i_vlc_pcm_format;
321         p_aout->output.output.i_physical_channels
322             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
323                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
324                | AOUT_CHAN_LFE;
325     }
326     else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
327     {
328         p_aout->output.output.i_format = i_vlc_pcm_format;
329         p_aout->output.output.i_physical_channels
330             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
331                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
332     }
333     else if ( !strcmp( val.psz_string, N_("Stereo") ) )
334     {
335         p_aout->output.output.i_format = i_vlc_pcm_format;
336         p_aout->output.output.i_physical_channels
337             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
338     }
339     else if ( !strcmp( val.psz_string, N_("Mono") ) )
340     {
341         p_aout->output.output.i_format = i_vlc_pcm_format;
342         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
343     }
344
345     free( val.psz_string );
346
347 #ifdef DEBUG
348     snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
349 #endif
350
351     /* Open the device */
352     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
353     {
354         if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
355                             SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
356         {
357             msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
358                              psz_iec_device, snd_strerror( i_snd_rc ) );
359             free( p_sys );
360             free( psz_device );
361             return VLC_EGENERIC;
362         }
363         i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
364         i_snd_pcm_format = SND_PCM_FORMAT_S16;
365         i_channels = 2;
366
367         p_aout->output.i_nb_samples = i_period_size = ALSA_SPDIF_PERIOD_SIZE;
368         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
369         p_aout->output.output.i_frame_length = A52_FRAME_NB;
370
371         aout_VolumeNoneInit( p_aout );
372     }
373     else
374     {
375         if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
376                             SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
377         {
378             msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
379                              psz_device, snd_strerror( i_snd_rc ) );
380             free( p_sys );
381             free( psz_device );
382             return VLC_EGENERIC;
383         }
384         i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
385         i_channels = aout_FormatNbChannels( &p_aout->output.output );
386
387         p_aout->output.i_nb_samples = i_period_size = ALSA_DEFAULT_PERIOD_SIZE;
388
389         aout_VolumeSoftInit( p_aout );
390     }
391
392     /* Free psz_device so that all the remaining data is stack-allocated */
393     free( psz_device );
394
395     p_aout->output.pf_play = Play;
396
397     snd_pcm_hw_params_alloca(&p_hw);
398     snd_pcm_sw_params_alloca(&p_sw);
399
400     /* Get Initial hardware parameters */
401     if ( ( i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) ) < 0 )
402     {
403         msg_Err( p_aout, "unable to retrieve initial hardware parameters (%s)",
404                          snd_strerror( i_snd_rc ) );
405         goto error;
406     }
407
408     /* Set format. */
409     if ( ( i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
410                                                     i_snd_pcm_format ) ) < 0 )
411     {
412         msg_Err( p_aout, "unable to set stream sample size and word order (%s)",
413                          snd_strerror( i_snd_rc ) );
414         goto error;
415     }
416
417     if ( ( i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
418                                     SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
419     {
420         msg_Err( p_aout, "unable to set interleaved stream format (%s)",
421                          snd_strerror( i_snd_rc ) );
422         goto error;
423     }
424
425     /* Set channels. */
426     if ( ( i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
427                                                       i_channels ) ) < 0 )
428     {
429         msg_Err( p_aout, "unable to set number of output channels (%s)",
430                          snd_strerror( i_snd_rc ) );
431         goto error;
432     }
433
434     /* Set rate. */
435 #ifdef HAVE_ALSA_NEW_API
436     if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
437                                 &p_aout->output.output.i_rate, NULL ) ) < 0 )
438 #else
439     if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
440                                 p_aout->output.output.i_rate, NULL ) ) < 0 )
441 #endif
442     {
443         msg_Err( p_aout, "unable to set sample rate (%s)",
444                          snd_strerror( i_snd_rc ) );
445         goto error;
446     }
447
448     /* Set buffer size. */
449 #ifdef HAVE_ALSA_NEW_API
450     if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
451                                     p_hw, &i_buffer_size ) ) < 0 )
452 #else
453     if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
454                                     p_hw, i_buffer_size ) ) < 0 )
455 #endif
456     {
457         msg_Err( p_aout, "unable to set buffer size (%s)",
458                          snd_strerror( i_snd_rc ) );
459         goto error;
460     }
461
462     /* Set period size. */
463 #ifdef HAVE_ALSA_NEW_API
464     if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
465                                     p_hw, &i_period_size, NULL ) ) < 0 )
466 #else
467     if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
468                                     p_hw, i_period_size, NULL ) ) < 0 )
469 #endif
470     {
471         msg_Err( p_aout, "unable to set period size (%s)",
472                          snd_strerror( i_snd_rc ) );
473         goto error;
474     }
475     p_aout->output.i_nb_samples = i_period_size;
476
477     /* Commit hardware parameters. */
478     if ( ( i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) ) < 0 )
479     {
480         msg_Err( p_aout, "unable to commit hardware configuration (%s)",
481                          snd_strerror( i_snd_rc ) );
482         goto error;
483     }
484
485 #ifdef HAVE_ALSA_NEW_API
486     if( ( i_snd_rc = snd_pcm_hw_params_get_period_time( p_hw,
487                                     &p_sys->i_period_time, NULL ) ) < 0 )
488 #else
489     if( ( p_sys->i_period_time =
490                   snd_pcm_hw_params_get_period_time( p_hw, NULL ) ) < 0 )
491 #endif
492     {
493         msg_Err( p_aout, "unable to get period time (%s)",
494                          snd_strerror( i_snd_rc ) );
495         goto error;
496     }
497
498     /* Get Initial software parameters */
499     snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
500
501     i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
502
503     i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
504                                                 p_aout->output.i_nb_samples );
505
506     /* Commit software parameters. */
507     if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
508     {
509         msg_Err( p_aout, "unable to set software configuration" );
510         goto error;
511     }
512
513 #ifdef DEBUG
514     snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
515     snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
516     snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
517     snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
518     snd_output_printf( p_sys->p_snd_stderr, "\n" );
519 #endif
520
521     /* Create ALSA thread and wait for its readiness. */
522     if( vlc_thread_create( p_aout, "aout", ALSAThread,
523                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
524     {
525         msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
526         goto error;
527     }
528
529     return 0;
530
531 error:
532     snd_pcm_close( p_sys->p_snd_pcm );
533 #ifdef DEBUG
534     snd_output_close( p_sys->p_snd_stderr );
535 #endif
536     free( p_sys );
537     return VLC_EGENERIC;
538 }
539
540 /*****************************************************************************
541  * Play: nothing to do
542  *****************************************************************************/
543 static void Play( aout_instance_t *p_aout )
544 {
545 }
546
547 /*****************************************************************************
548  * Close: close the ALSA device
549  *****************************************************************************/
550 static void Close( vlc_object_t *p_this )
551 {
552     aout_instance_t *p_aout = (aout_instance_t *)p_this;
553     struct aout_sys_t * p_sys = p_aout->output.p_sys;
554     int i_snd_rc;
555
556     p_aout->b_die = VLC_TRUE;
557     vlc_thread_join( p_aout );
558     p_aout->b_die = VLC_FALSE;
559
560     i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
561
562     if( i_snd_rc > 0 )
563     {
564         msg_Err( p_aout, "failed closing ALSA device (%s)",
565                          snd_strerror( i_snd_rc ) );
566     }
567
568 #ifdef DEBUG
569     snd_output_close( p_sys->p_snd_stderr );
570 #endif
571
572     free( p_sys );
573 }
574
575 /*****************************************************************************
576  * ALSAThread: asynchronous thread used to DMA the data to the device
577  *****************************************************************************/
578 static int ALSAThread( aout_instance_t * p_aout )
579 {
580     struct aout_sys_t * p_sys = p_aout->output.p_sys;
581
582     while ( !p_aout->b_die )
583     {
584         ALSAFill( p_aout );
585
586         /* Sleep during less than one period to avoid a lot of buffer
587            underruns */
588
589         /* Why do we need to sleep ? --Meuuh */
590         /* Maybe because I don't want to eat all the cpu by looping
591            all the time. --Bozo */
592         /* Shouldn't snd_pcm_wait() make us wait ? --Meuuh */
593         msleep( p_sys->i_period_time >> 1 );
594     }
595
596     return 0;
597 }
598
599 /*****************************************************************************
600  * ALSAFill: function used to fill the ALSA buffer as much as possible
601  *****************************************************************************/
602 static void ALSAFill( aout_instance_t * p_aout )
603 {
604     struct aout_sys_t * p_sys = p_aout->output.p_sys;
605
606     aout_buffer_t * p_buffer;
607     snd_pcm_status_t * p_status;
608     snd_timestamp_t ts_next;
609     int i_snd_rc;
610     mtime_t next_date;
611
612     snd_pcm_status_alloca( &p_status );
613
614     /* Wait for the device's readiness (ie. there is enough space in the
615        buffer to write at least one complete chunk) */
616     i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
617     if( i_snd_rc < 0 )
618     {
619         msg_Err( p_aout, "ALSA device not ready !!! (%s)",
620                          snd_strerror( i_snd_rc ) );
621         return;
622     }
623
624     /* Fill in the buffer until space or audio output buffer shortage */
625     for ( ; ; )
626     {
627         /* Get the status */
628         i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
629         if( i_snd_rc < 0 )
630         {
631             msg_Err( p_aout, "unable to get the device's status (%s)",
632                              snd_strerror( i_snd_rc ) );
633             return;
634         }
635
636         /* Handle buffer underruns and reget the status */
637         if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
638         {
639             /* Prepare the device */
640             i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
641
642             if( i_snd_rc == 0 )
643             {
644                 msg_Warn( p_aout, "recovered from buffer underrun" );
645
646                 /* Reget the status */
647                 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
648                 if( i_snd_rc < 0 )
649                 {
650                     msg_Err( p_aout,
651                         "unable to get the device's status after recovery (%s)",
652                         snd_strerror( i_snd_rc ) );
653                     return;
654                 }
655             }
656             else
657             {
658                 msg_Err( p_aout, "unable to recover from buffer underrun" );
659                 return;
660             }
661         }
662
663         /* Here the device should be either in the RUNNING state either in
664            the PREPARE state. p_status is valid. */
665
666         snd_pcm_status_get_tstamp( p_status, &ts_next );
667         next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
668         next_date += (mtime_t)snd_pcm_status_get_delay(p_status)
669                      * 1000000 / p_aout->output.output.i_rate;
670
671         p_buffer = aout_OutputNextBuffer( p_aout, next_date,
672                         (p_aout->output.output.i_format ==
673                          VLC_FOURCC('s','p','d','i')) );
674
675         /* Audio output buffer shortage -> stop the fill process and
676            wait in ALSAThread */
677         if( p_buffer == NULL )
678             return;
679
680         i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
681                                    p_buffer->i_nb_samples );
682
683         if( i_snd_rc < 0 )
684         {
685             msg_Err( p_aout, "write failed (%s)",
686                              snd_strerror( i_snd_rc ) );
687         }
688
689         aout_BufferFree( p_buffer );
690
691         msleep( p_sys->i_period_time >> 2 );
692     }
693 }
694