]> git.sesse.net Git - vlc/blob - modules/audio_output/alsa.c
* include/configuration.h: added a new flag to the configuration stucture to
[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.23 2003/02/20 01:52:45 sigmunau 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, VLC_FALSE );
98     add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
99                 N_("ALSA device name"), NULL, VLC_FALSE );
100     add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT, VLC_FALSE );
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 > 0 )
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     else
346     {
347         /* This should not happen ! */
348         msg_Err( p_aout, "internal: can't find audio-device (%s)",
349                  val.psz_string );
350         free( p_sys );
351         free( val.psz_string );
352         return VLC_EGENERIC;
353     }
354     free( val.psz_string );
355
356 #ifdef DEBUG
357     snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
358 #endif
359
360     /* Open the device */
361     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
362     {
363         if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
364                             SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
365         {
366             msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
367                              psz_iec_device, snd_strerror( i_snd_rc ) );
368             free( p_sys );
369             free( psz_device );
370             return VLC_EGENERIC;
371         }
372         i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
373         i_snd_pcm_format = SND_PCM_FORMAT_S16;
374         i_channels = 2;
375
376         p_aout->output.i_nb_samples = i_period_size = ALSA_SPDIF_PERIOD_SIZE;
377         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
378         p_aout->output.output.i_frame_length = A52_FRAME_NB;
379
380         aout_VolumeNoneInit( p_aout );
381     }
382     else
383     {
384         if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
385                             SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
386         {
387             msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
388                              psz_device, snd_strerror( i_snd_rc ) );
389             free( p_sys );
390             free( psz_device );
391             return VLC_EGENERIC;
392         }
393         i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
394         i_channels = aout_FormatNbChannels( &p_aout->output.output );
395
396         p_aout->output.i_nb_samples = i_period_size = ALSA_DEFAULT_PERIOD_SIZE;
397
398         aout_VolumeSoftInit( p_aout );
399     }
400
401     /* Free psz_device so that all the remaining data is stack-allocated */
402     free( psz_device );
403
404     p_aout->output.pf_play = Play;
405
406     snd_pcm_hw_params_alloca(&p_hw);
407     snd_pcm_sw_params_alloca(&p_sw);
408
409     /* Get Initial hardware parameters */
410     if ( ( i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) ) < 0 )
411     {
412         msg_Err( p_aout, "unable to retrieve initial hardware parameters (%s)",
413                          snd_strerror( i_snd_rc ) );
414         goto error;
415     }
416
417     /* Set format. */
418     if ( ( i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
419                                                     i_snd_pcm_format ) ) < 0 )
420     {
421         msg_Err( p_aout, "unable to set stream sample size and word order (%s)",
422                          snd_strerror( i_snd_rc ) );
423         goto error;
424     }
425
426     if ( ( i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
427                                     SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
428     {
429         msg_Err( p_aout, "unable to set interleaved stream format (%s)",
430                          snd_strerror( i_snd_rc ) );
431         goto error;
432     }
433
434     /* Set channels. */
435     if ( ( i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
436                                                       i_channels ) ) < 0 )
437     {
438         msg_Err( p_aout, "unable to set number of output channels (%s)",
439                          snd_strerror( i_snd_rc ) );
440         goto error;
441     }
442
443     /* Set rate. */
444 #ifdef HAVE_ALSA_NEW_API
445     if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
446                                 &p_aout->output.output.i_rate, NULL ) ) < 0 )
447 #else
448     if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
449                                 p_aout->output.output.i_rate, NULL ) ) < 0 )
450 #endif
451     {
452         msg_Err( p_aout, "unable to set sample rate (%s)",
453                          snd_strerror( i_snd_rc ) );
454         goto error;
455     }
456
457     /* Set buffer size. */
458 #ifdef HAVE_ALSA_NEW_API
459     if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
460                                     p_hw, &i_buffer_size ) ) < 0 )
461 #else
462     if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
463                                     p_hw, i_buffer_size ) ) < 0 )
464 #endif
465     {
466         msg_Err( p_aout, "unable to set buffer size (%s)",
467                          snd_strerror( i_snd_rc ) );
468         goto error;
469     }
470
471     /* Set period size. */
472 #ifdef HAVE_ALSA_NEW_API
473     if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
474                                     p_hw, &i_period_size, NULL ) ) < 0 )
475 #else
476     if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
477                                     p_hw, i_period_size, NULL ) ) < 0 )
478 #endif
479     {
480         msg_Err( p_aout, "unable to set period size (%s)",
481                          snd_strerror( i_snd_rc ) );
482         goto error;
483     }
484     p_aout->output.i_nb_samples = i_period_size;
485
486     /* Commit hardware parameters. */
487     if ( ( i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) ) < 0 )
488     {
489         msg_Err( p_aout, "unable to commit hardware configuration (%s)",
490                          snd_strerror( i_snd_rc ) );
491         goto error;
492     }
493
494 #ifdef HAVE_ALSA_NEW_API
495     if( ( i_snd_rc = snd_pcm_hw_params_get_period_time( p_hw,
496                                     &p_sys->i_period_time, NULL ) ) < 0 )
497 #else
498     if( ( p_sys->i_period_time =
499                   snd_pcm_hw_params_get_period_time( p_hw, NULL ) ) < 0 )
500 #endif
501     {
502         msg_Err( p_aout, "unable to get period time (%s)",
503                          snd_strerror( i_snd_rc ) );
504         goto error;
505     }
506
507     /* Get Initial software parameters */
508     snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
509
510     i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
511
512     i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
513                                                 p_aout->output.i_nb_samples );
514
515     /* Commit software parameters. */
516     if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
517     {
518         msg_Err( p_aout, "unable to set software configuration" );
519         goto error;
520     }
521
522 #ifdef DEBUG
523     snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
524     snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
525     snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
526     snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
527     snd_output_printf( p_sys->p_snd_stderr, "\n" );
528 #endif
529
530     /* Create ALSA thread and wait for its readiness. */
531     if( vlc_thread_create( p_aout, "aout", ALSAThread,
532                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
533     {
534         msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
535         goto error;
536     }
537
538     return 0;
539
540 error:
541     snd_pcm_close( p_sys->p_snd_pcm );
542 #ifdef DEBUG
543     snd_output_close( p_sys->p_snd_stderr );
544 #endif
545     free( p_sys );
546     return VLC_EGENERIC;
547 }
548
549 /*****************************************************************************
550  * Play: nothing to do
551  *****************************************************************************/
552 static void Play( aout_instance_t *p_aout )
553 {
554 }
555
556 /*****************************************************************************
557  * Close: close the ALSA device
558  *****************************************************************************/
559 static void Close( vlc_object_t *p_this )
560 {
561     aout_instance_t *p_aout = (aout_instance_t *)p_this;
562     struct aout_sys_t * p_sys = p_aout->output.p_sys;
563     int i_snd_rc;
564
565     p_aout->b_die = VLC_TRUE;
566     vlc_thread_join( p_aout );
567     p_aout->b_die = VLC_FALSE;
568
569     i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
570
571     if( i_snd_rc > 0 )
572     {
573         msg_Err( p_aout, "failed closing ALSA device (%s)",
574                          snd_strerror( i_snd_rc ) );
575     }
576
577 #ifdef DEBUG
578     snd_output_close( p_sys->p_snd_stderr );
579 #endif
580
581     free( p_sys );
582 }
583
584 /*****************************************************************************
585  * ALSAThread: asynchronous thread used to DMA the data to the device
586  *****************************************************************************/
587 static int ALSAThread( aout_instance_t * p_aout )
588 {
589     struct aout_sys_t * p_sys = p_aout->output.p_sys;
590
591     while ( !p_aout->b_die )
592     {
593         ALSAFill( p_aout );
594
595         /* Sleep during less than one period to avoid a lot of buffer
596            underruns */
597
598         /* Why do we need to sleep ? --Meuuh */
599         /* Maybe because I don't want to eat all the cpu by looping
600            all the time. --Bozo */
601         /* Shouldn't snd_pcm_wait() make us wait ? --Meuuh */
602         msleep( p_sys->i_period_time >> 1 );
603     }
604
605     return 0;
606 }
607
608 /*****************************************************************************
609  * ALSAFill: function used to fill the ALSA buffer as much as possible
610  *****************************************************************************/
611 static void ALSAFill( aout_instance_t * p_aout )
612 {
613     struct aout_sys_t * p_sys = p_aout->output.p_sys;
614
615     aout_buffer_t * p_buffer;
616     snd_pcm_status_t * p_status;
617     snd_timestamp_t ts_next;
618     int i_snd_rc;
619     mtime_t next_date;
620
621     snd_pcm_status_alloca( &p_status );
622
623     /* Wait for the device's readiness (ie. there is enough space in the
624        buffer to write at least one complete chunk) */
625     i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
626     if( i_snd_rc < 0 )
627     {
628         msg_Err( p_aout, "ALSA device not ready !!! (%s)",
629                          snd_strerror( i_snd_rc ) );
630         return;
631     }
632
633     /* Fill in the buffer until space or audio output buffer shortage */
634     for ( ; ; )
635     {
636         /* Get the status */
637         i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
638         if( i_snd_rc < 0 )
639         {
640             msg_Err( p_aout, "unable to get the device's status (%s)",
641                              snd_strerror( i_snd_rc ) );
642             return;
643         }
644
645         /* Handle buffer underruns and reget the status */
646         if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
647         {
648             /* Prepare the device */
649             i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
650
651             if( i_snd_rc == 0 )
652             {
653                 msg_Warn( p_aout, "recovered from buffer underrun" );
654
655                 /* Reget the status */
656                 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
657                 if( i_snd_rc < 0 )
658                 {
659                     msg_Err( p_aout,
660                         "unable to get the device's status after recovery (%s)",
661                         snd_strerror( i_snd_rc ) );
662                     return;
663                 }
664             }
665             else
666             {
667                 msg_Err( p_aout, "unable to recover from buffer underrun" );
668                 return;
669             }
670         }
671
672         /* Here the device should be either in the RUNNING state either in
673            the PREPARE state. p_status is valid. */
674
675         snd_pcm_status_get_tstamp( p_status, &ts_next );
676         next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
677         next_date += (mtime_t)snd_pcm_status_get_delay(p_status)
678                      * 1000000 / p_aout->output.output.i_rate;
679
680         p_buffer = aout_OutputNextBuffer( p_aout, next_date,
681                         (p_aout->output.output.i_format ==
682                          VLC_FOURCC('s','p','d','i')) );
683
684         /* Audio output buffer shortage -> stop the fill process and
685            wait in ALSAThread */
686         if( p_buffer == NULL )
687             return;
688
689         i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
690                                    p_buffer->i_nb_samples );
691
692         if( i_snd_rc < 0 )
693         {
694             msg_Err( p_aout, "write failed (%s)",
695                              snd_strerror( i_snd_rc ) );
696         }
697
698         aout_BufferFree( p_buffer );
699
700         msleep( p_sys->i_period_time >> 2 );
701     }
702 }
703