]> git.sesse.net Git - vlc/blob - modules/audio_output/alsa.c
* ./configure: Fixed double detection of gethostbyname.
[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.5 2002/08/19 21:31:11 massiot 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_("Audio"), NULL );
92     add_string( "alsa-device", NULL, NULL, N_("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_sec =
238                     p_aout->output.output.i_rate * AOUT_SPDIF_SIZE /
239                     ALSA_SPDIF_PERIOD_SIZE;
240             break;
241         case AOUT_FMT_FIXED32:
242         default:
243             msg_Err( p_aout, "audio output format 0x%x not supported",
244                      p_aout->output.output.i_format );
245             return -1;
246             break;
247     }
248
249     snd_pcm_hw_params_alloca(&p_hw);
250     snd_pcm_sw_params_alloca(&p_sw);
251
252     i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw );
253     if( i_snd_rc < 0 )
254     {
255         msg_Err( p_aout, "unable to retrieve initial hardware parameters" );
256         return -1;
257     }
258
259     i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
260                                              SND_PCM_ACCESS_RW_INTERLEAVED );
261     if( i_snd_rc < 0 )
262     {
263         msg_Err( p_aout, "unable to set interleaved stream format" );
264         return -1;
265     }
266
267     i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw, i_format );
268     if( i_snd_rc < 0 )
269     {
270         msg_Err( p_aout, "unable to set stream sample size and word order" );
271         return -1;
272     }
273
274     i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
275                                                i_channels );
276     if( i_snd_rc < 0 )
277     {
278         msg_Err( p_aout, "unable to set number of output channels" );
279         return -1;
280     }
281
282     i_snd_rc = snd_pcm_hw_params_set_rate( p_sys->p_snd_pcm, p_hw,
283                                            p_aout->output.output.i_rate, 0 );
284     if( i_snd_rc < 0 )
285     {
286         msg_Err( p_aout, "unable to set sample rate" );
287         return -1;
288     }
289
290     i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm, p_hw,
291                                                        p_sys->i_buffer_size );
292     if( i_snd_rc < 0 )
293     {
294         msg_Err( p_aout, "unable to set buffer time" );
295         return -1;
296     }
297     p_sys->i_buffer_size = i_snd_rc;
298
299     i_snd_rc = snd_pcm_hw_params_set_period_size_near(
300                     p_sys->p_snd_pcm, p_hw, p_aout->output.i_nb_samples, 0 );
301     if( i_snd_rc < 0 )
302     {
303         msg_Err( p_aout, "unable to set period size" );
304         return -1;
305     }
306     p_aout->output.i_nb_samples = i_snd_rc;
307     p_sys->i_period_time = snd_pcm_hw_params_get_period_time( p_hw, 0 );
308
309     i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw );
310     if (i_snd_rc < 0)
311     {
312         msg_Err( p_aout, "unable to set hardware configuration" );
313         return -1;
314     }
315
316     snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
317     i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
318
319     i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
320                                                 p_aout->output.i_nb_samples );
321
322     i_snd_rc = snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw );
323     if( i_snd_rc < 0 )
324     {
325         msg_Err( p_aout, "unable to set software configuration" );
326         return -1;
327     }
328
329 #ifdef DEBUG
330     snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
331     snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
332     snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
333     snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
334     snd_output_printf( p_sys->p_snd_stderr, "\n" );
335 #endif
336
337     p_sys->b_initialized = VLC_TRUE;
338
339     return 0;
340 }
341
342 /*****************************************************************************
343  * Play: queue a buffer for playing by ALSAThread
344  *****************************************************************************/
345 static void Play( aout_instance_t *p_aout )
346 {
347 }
348
349 /*****************************************************************************
350  * Close: close the Alsa device
351  *****************************************************************************/
352 static void Close( vlc_object_t *p_this )
353 {
354     aout_instance_t *p_aout = (aout_instance_t *)p_this;
355     struct aout_sys_t * p_sys = p_aout->output.p_sys;
356     int i_snd_rc;
357
358     p_aout->b_die = 1;
359     vlc_thread_join( p_aout );
360
361     if( p_sys->p_snd_pcm )
362     {
363         i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
364
365         if( i_snd_rc > 0 )
366         {
367             msg_Err( p_aout, "failed closing ALSA device (%s)",
368                              snd_strerror( i_snd_rc ) );
369         }
370     }
371
372 #ifdef DEBUG
373     snd_output_close( p_sys->p_snd_stderr );
374 #endif
375
376     free( p_sys );
377 }
378
379 /*****************************************************************************
380  * ALSAThread: asynchronous thread used to DMA the data to the device
381  *****************************************************************************/
382 static int ALSAThread( aout_instance_t * p_aout )
383 {
384     struct aout_sys_t * p_sys = p_aout->output.p_sys;
385
386     while ( !p_aout->b_die && !p_sys->b_initialized )
387         msleep( THREAD_SLEEP );
388
389     while ( !p_aout->b_die )
390     {
391         ALSAFill( p_aout );
392
393         /* Sleep during less than one period to avoid a lot of buffer
394            underruns */
395         msleep( p_sys->i_period_time >> 2 );
396     }
397
398     return 0;
399 }
400
401 /*****************************************************************************
402  * ALSAFill: function used to fill the ALSA buffer as much as possible
403  *****************************************************************************/
404 static void ALSAFill( aout_instance_t * p_aout )
405 {
406     struct aout_sys_t * p_sys = p_aout->output.p_sys;
407
408     aout_buffer_t * p_buffer;
409     snd_pcm_status_t * p_status;
410     snd_timestamp_t ts_next;
411     int i_snd_rc;
412     snd_pcm_uframes_t i_avail;
413
414     snd_pcm_status_alloca( &p_status );
415
416     /* Wait for the device's readiness (ie. there is enough space in the
417        buffer to write at least one complete chunk) */
418     i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
419     if( i_snd_rc < 0 )
420     {
421         msg_Err( p_aout, "alsa device not ready !!! (%s)",
422                          snd_strerror( i_snd_rc ) );
423         return;
424     }
425
426     /* Fill in the buffer until space or audio output buffer shortage */
427     while( VLC_TRUE )
428     {
429         /* Get the status */
430         i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
431         if( i_snd_rc < 0 )
432         {
433             msg_Err( p_aout, "unable to get the device's status (%s)",
434                              snd_strerror( i_snd_rc ) );
435             return;
436         }
437
438         /* Handle buffer underruns and reget the status */
439         if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
440         {
441             /* Prepare the device */
442             i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
443
444             if( i_snd_rc == 0 )
445             {
446                 msg_Warn( p_aout, "recovered from buffer underrun" );
447
448                 /* Reget the status */
449                 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
450                 if( i_snd_rc < 0 )
451                 {
452                     msg_Err( p_aout,
453                         "unable to get the device's status after recovery (%s)",
454                         snd_strerror( i_snd_rc ) );
455                     return;
456                 }
457             }
458             else
459             {
460                 msg_Err( p_aout, "unable to recover from buffer underrun" );
461                 return;
462             }
463         }
464
465         /* Here the device should be either in the RUNNING state either in
466            the PREPARE state. p_status is valid. */
467
468         /* Try to write only if there is enough space */
469         i_avail = snd_pcm_status_get_avail( p_status );
470
471         if( i_avail >= p_aout->output.i_nb_samples )
472         {
473             mtime_t next_date;
474             snd_pcm_status_get_tstamp( p_status, &ts_next );
475             next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
476
477             p_buffer = aout_OutputNextBuffer( p_aout, next_date,
478                                               p_sys->b_can_sleek );
479
480             /* Audio output buffer shortage -> stop the fill process and
481                wait in ALSAThread */
482             if( p_buffer == NULL )
483                 return;
484
485             i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
486                                        p_buffer->i_nb_samples );
487
488             if( i_snd_rc < 0 )
489             {
490                 msg_Err( p_aout, "write failed (%s)",
491                                  snd_strerror( i_snd_rc ) );
492             }
493             else
494             {
495                 aout_BufferFree( p_buffer );
496             }
497         }
498     }
499 }
500