]> git.sesse.net Git - vlc/blob - modules/audio_output/alsa.c
* ./src/audio_output/output.c: added an argument to aout_OutputNextBuffer
[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.7 2002/08/24 10:19: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 #include <alsa/asoundlib.h>
42
43 /*****************************************************************************
44  * aout_sys_t: ALSA audio output method descriptor
45  *****************************************************************************
46  * This structure is part of the audio output thread descriptor.
47  * It describes the ALSA specific properties of an audio device.
48  *****************************************************************************/
49 struct aout_sys_t
50 {
51     snd_pcm_t         * p_snd_pcm;
52     snd_pcm_sframes_t   i_buffer_size;
53     int                 i_period_time;
54
55     volatile vlc_bool_t b_initialized;
56
57     volatile vlc_bool_t b_can_sleek;
58
59 #ifdef DEBUG
60     snd_output_t      * p_snd_stderr;
61 #endif
62 };
63
64 #define A52_FRAME_NB 1536
65
66 /* These values are in frames.
67    To convert them to a number of bytes you have to multiply them by the
68    number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
69    2 for s16). */
70 #define ALSA_DEFAULT_PERIOD_SIZE        2048
71 #define ALSA_DEFAULT_BUFFER_SIZE        ( ALSA_DEFAULT_PERIOD_SIZE << 4 )
72 #define ALSA_SPDIF_PERIOD_SIZE          A52_FRAME_NB
73 #define ALSA_SPDIF_BUFFER_SIZE          ( ALSA_SPDIF_PERIOD_SIZE << 4 )
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static int  Open         ( vlc_object_t * );
79 static void Close        ( vlc_object_t * );
80
81 static int  SetFormat    ( aout_instance_t * );
82 static void Play         ( aout_instance_t * );
83
84 static int  ALSAThread   ( aout_instance_t * );
85 static void ALSAFill     ( aout_instance_t * );
86
87 /*****************************************************************************
88  * Module descriptor
89  *****************************************************************************/
90 vlc_module_begin();
91     add_category_hint( N_("ALSA"), NULL );
92     add_string( "alsa-device", NULL, NULL, N_("device name"), NULL );
93     set_description( _("ALSA audio module") );
94     set_capability( "audio output", 50 );
95     set_callbacks( Open, Close );
96 vlc_module_end();
97
98 /*****************************************************************************
99  * Open: create a handle and open an alsa device
100  *****************************************************************************
101  * This function opens an alsa device, through the alsa API
102  *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
104 {
105     aout_instance_t * p_aout = (aout_instance_t *)p_this;
106     struct aout_sys_t * p_sys;
107
108     /* Allocate structures */
109     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
110     if( p_sys == NULL )
111     {
112         msg_Err( p_aout, "out of memory" );
113         return -1;
114     }
115
116     /* Create ALSA thread and wait for its readiness. */
117     p_sys->b_initialized = VLC_FALSE;
118     if( vlc_thread_create( p_aout, "aout", ALSAThread, VLC_FALSE ) )
119     {
120         msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
121         free( p_sys );
122         return -1;
123     }
124
125     p_aout->output.pf_setformat = SetFormat;
126     p_aout->output.pf_play = Play;
127
128 #ifdef DEBUG
129     snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
130 #endif
131
132     return 0;
133 }
134
135 /*****************************************************************************
136  * SetFormat : sets the alsa output format
137  *****************************************************************************
138  * This function prepares the device, sets the rate, format, the mode
139  * ( "play as soon as you have data" ), and buffer information.
140  *****************************************************************************/
141 static int SetFormat( aout_instance_t * p_aout )
142 {
143     struct aout_sys_t * p_sys = p_aout->output.p_sys;
144
145     int i_snd_rc;
146
147     char * psz_device;
148     char psz_alsadev[128];
149     char * psz_userdev;
150
151     int i_format;
152     int i_channels;
153
154     snd_pcm_hw_params_t *p_hw;
155     snd_pcm_sw_params_t *p_sw;
156
157     /* Read in ALSA device preferences from configuration */
158     psz_userdev = config_GetPsz( p_aout, "alsa-device" );
159
160     if( psz_userdev )
161     {
162         psz_device = psz_userdev;
163     }
164     else
165     {
166         /* Use the internal logic to decide on the device name */
167         if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
168         {
169             /* Will probably need some little modification in the case
170                we want to send some data at a different rate
171                (32000, 44100 and 48000 are the possibilities) -- bozo */
172             unsigned char s[4];
173             s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
174             s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
175             s[2] = 0;
176             s[3] = IEC958_AES3_CON_FS_48000;
177             sprintf( psz_alsadev,
178                      "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
179                      s[0], s[1], s[2], s[3] );
180             psz_device = psz_alsadev;
181         }
182         else
183         {
184             psz_device = "default";
185         }
186     }
187
188     /* Open device */
189     i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
190                              SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
191     if( i_snd_rc < 0 )
192     {
193         msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
194                          psz_device, snd_strerror(i_snd_rc) );
195         if( psz_userdev )
196             free( psz_userdev );
197         p_sys->p_snd_pcm = NULL;
198         return -1;
199     }
200
201     if( psz_userdev )
202         free( psz_userdev );
203
204     /* Default settings */
205     p_sys->b_can_sleek = VLC_FALSE;
206     i_channels = p_aout->output.output.i_channels;
207     if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
208     {
209         p_sys->i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
210         p_aout->output.i_nb_samples = ALSA_SPDIF_PERIOD_SIZE;
211     }
212     else
213     {
214         p_sys->i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
215         p_aout->output.i_nb_samples = ALSA_DEFAULT_PERIOD_SIZE;
216     }
217
218
219     /* Compute the settings */
220     switch (p_aout->output.output.i_format)
221     {
222         case AOUT_FMT_MU_LAW:    i_format = SND_PCM_FORMAT_MU_LAW; break;
223         case AOUT_FMT_A_LAW:     i_format = SND_PCM_FORMAT_A_LAW; break;
224         case AOUT_FMT_IMA_ADPCM: i_format = SND_PCM_FORMAT_IMA_ADPCM; break;
225         case AOUT_FMT_U8:        i_format = SND_PCM_FORMAT_U8; break;
226         case AOUT_FMT_S16_LE:    i_format = SND_PCM_FORMAT_S16_LE; break;
227         case AOUT_FMT_S16_BE:    i_format = SND_PCM_FORMAT_S16_BE; break;
228         case AOUT_FMT_S8:        i_format = SND_PCM_FORMAT_S8; break;
229         case AOUT_FMT_U16_LE:    i_format = SND_PCM_FORMAT_U16_LE; break;
230         case AOUT_FMT_U16_BE:    i_format = SND_PCM_FORMAT_U16_BE; break;
231         case AOUT_FMT_FLOAT32:   i_format = SND_PCM_FORMAT_FLOAT; break;
232         case AOUT_FMT_SPDIF:
233             /* Override some settings to make S/PDIF work */
234             p_sys->b_can_sleek = VLC_TRUE;
235             i_format = SND_PCM_FORMAT_S16_LE;
236             i_channels = 2;
237             p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
238             p_aout->output.output.i_frame_length = ALSA_SPDIF_PERIOD_SIZE;
239             break;
240         case AOUT_FMT_FIXED32:
241         default:
242             msg_Err( p_aout, "audio output format 0x%x not supported",
243                      p_aout->output.output.i_format );
244             return -1;
245             break;
246     }
247
248     snd_pcm_hw_params_alloca(&p_hw);
249     snd_pcm_sw_params_alloca(&p_sw);
250
251     i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw );
252     if( i_snd_rc < 0 )
253     {
254         msg_Err( p_aout, "unable to retrieve initial hardware parameters" );
255         return -1;
256     }
257
258     i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
259                                              SND_PCM_ACCESS_RW_INTERLEAVED );
260     if( i_snd_rc < 0 )
261     {
262         msg_Err( p_aout, "unable to set interleaved stream format" );
263         return -1;
264     }
265
266     i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw, i_format );
267     if( i_snd_rc < 0 )
268     {
269         msg_Err( p_aout, "unable to set stream sample size and word order" );
270         return -1;
271     }
272
273     i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
274                                                i_channels );
275     if( i_snd_rc < 0 )
276     {
277         msg_Err( p_aout, "unable to set number of output channels" );
278         return -1;
279     }
280
281     i_snd_rc = snd_pcm_hw_params_set_rate( p_sys->p_snd_pcm, p_hw,
282                                            p_aout->output.output.i_rate, 0 );
283     if( i_snd_rc < 0 )
284     {
285         msg_Err( p_aout, "unable to set sample rate" );
286         return -1;
287     }
288
289     i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm, p_hw,
290                                                        p_sys->i_buffer_size );
291     if( i_snd_rc < 0 )
292     {
293         msg_Err( p_aout, "unable to set buffer time" );
294         return -1;
295     }
296     p_sys->i_buffer_size = i_snd_rc;
297
298     i_snd_rc = snd_pcm_hw_params_set_period_size_near(
299                     p_sys->p_snd_pcm, p_hw, p_aout->output.i_nb_samples, 0 );
300     if( i_snd_rc < 0 )
301     {
302         msg_Err( p_aout, "unable to set period size" );
303         return -1;
304     }
305     p_aout->output.i_nb_samples = i_snd_rc;
306     p_sys->i_period_time = snd_pcm_hw_params_get_period_time( p_hw, 0 );
307
308     i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw );
309     if (i_snd_rc < 0)
310     {
311         msg_Err( p_aout, "unable to set hardware configuration" );
312         return -1;
313     }
314
315     snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
316     i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
317
318     i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
319                                                 p_aout->output.i_nb_samples );
320
321     i_snd_rc = snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw );
322     if( i_snd_rc < 0 )
323     {
324         msg_Err( p_aout, "unable to set software configuration" );
325         return -1;
326     }
327
328 #ifdef DEBUG
329     snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
330     snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
331     snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
332     snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
333     snd_output_printf( p_sys->p_snd_stderr, "\n" );
334 #endif
335
336     p_sys->b_initialized = VLC_TRUE;
337
338     return 0;
339 }
340
341 /*****************************************************************************
342  * Play: queue a buffer for playing by ALSAThread
343  *****************************************************************************/
344 static void Play( aout_instance_t *p_aout )
345 {
346 }
347
348 /*****************************************************************************
349  * Close: close the Alsa device
350  *****************************************************************************/
351 static void Close( vlc_object_t *p_this )
352 {
353     aout_instance_t *p_aout = (aout_instance_t *)p_this;
354     struct aout_sys_t * p_sys = p_aout->output.p_sys;
355     int i_snd_rc;
356
357     p_aout->b_die = 1;
358     vlc_thread_join( p_aout );
359
360     if( p_sys->p_snd_pcm )
361     {
362         i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
363
364         if( i_snd_rc > 0 )
365         {
366             msg_Err( p_aout, "failed closing ALSA device (%s)",
367                              snd_strerror( i_snd_rc ) );
368         }
369     }
370
371 #ifdef DEBUG
372     snd_output_close( p_sys->p_snd_stderr );
373 #endif
374
375     free( p_sys );
376 }
377
378 /*****************************************************************************
379  * ALSAThread: asynchronous thread used to DMA the data to the device
380  *****************************************************************************/
381 static int ALSAThread( aout_instance_t * p_aout )
382 {
383     struct aout_sys_t * p_sys = p_aout->output.p_sys;
384
385     while ( !p_aout->b_die && !p_sys->b_initialized )
386         msleep( THREAD_SLEEP );
387
388     while ( !p_aout->b_die )
389     {
390         ALSAFill( p_aout );
391
392         /* Sleep during less than one period to avoid a lot of buffer
393            underruns */
394         msleep( p_sys->i_period_time >> 2 );
395     }
396
397     return 0;
398 }
399
400 /*****************************************************************************
401  * ALSAFill: function used to fill the ALSA buffer as much as possible
402  *****************************************************************************/
403 static void ALSAFill( aout_instance_t * p_aout )
404 {
405     struct aout_sys_t * p_sys = p_aout->output.p_sys;
406
407     aout_buffer_t * p_buffer;
408     snd_pcm_status_t * p_status;
409     snd_timestamp_t ts_next;
410     int i_snd_rc;
411     snd_pcm_uframes_t i_avail;
412
413     snd_pcm_status_alloca( &p_status );
414
415     /* Wait for the device's readiness (ie. there is enough space in the
416        buffer to write at least one complete chunk) */
417     i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
418     if( i_snd_rc < 0 )
419     {
420         msg_Err( p_aout, "alsa device not ready !!! (%s)",
421                          snd_strerror( i_snd_rc ) );
422         return;
423     }
424
425     /* Fill in the buffer until space or audio output buffer shortage */
426     while( VLC_TRUE )
427     {
428         /* Get the status */
429         i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
430         if( i_snd_rc < 0 )
431         {
432             msg_Err( p_aout, "unable to get the device's status (%s)",
433                              snd_strerror( i_snd_rc ) );
434             return;
435         }
436
437         /* Handle buffer underruns and reget the status */
438         if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
439         {
440             /* Prepare the device */
441             i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
442
443             if( i_snd_rc == 0 )
444             {
445                 msg_Warn( p_aout, "recovered from buffer underrun" );
446
447                 /* Reget the status */
448                 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
449                 if( i_snd_rc < 0 )
450                 {
451                     msg_Err( p_aout,
452                         "unable to get the device's status after recovery (%s)",
453                         snd_strerror( i_snd_rc ) );
454                     return;
455                 }
456             }
457             else
458             {
459                 msg_Err( p_aout, "unable to recover from buffer underrun" );
460                 return;
461             }
462         }
463
464         /* Here the device should be either in the RUNNING state either in
465            the PREPARE state. p_status is valid. */
466
467         /* Try to write only if there is enough space */
468         i_avail = snd_pcm_status_get_avail( p_status );
469
470         if( i_avail >= p_aout->output.i_nb_samples )
471         {
472             mtime_t next_date;
473             snd_pcm_status_get_tstamp( p_status, &ts_next );
474             next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
475
476             p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0,
477                                               p_sys->b_can_sleek );
478
479             /* Audio output buffer shortage -> stop the fill process and
480                wait in ALSAThread */
481             if( p_buffer == NULL )
482                 return;
483
484             i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
485                                        p_buffer->i_nb_samples );
486
487             if( i_snd_rc < 0 )
488             {
489                 msg_Err( p_aout, "write failed (%s)",
490                                  snd_strerror( i_snd_rc ) );
491             }
492             else
493             {
494                 aout_BufferFree( p_buffer );
495             }
496         }
497     }
498 }
499