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