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