]> git.sesse.net Git - vlc/blob - modules/audio_output/alsa/alsa.c
* ./modules/*: moved plugins to the new tree. Yet untested builds include
[vlc] / modules / audio_output / alsa / alsa.c
1 /*****************************************************************************
2  * alsa.c : alsa plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: alsa.c,v 1.1 2002/08/04 17:23: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  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  * 
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <string.h>                                            /* strerror() */
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35
36 #include <alsa/asoundlib.h>
37
38 /*****************************************************************************
39  * Local prototypes
40  *****************************************************************************/
41 static int  Open         ( vlc_object_t * );
42 static void Close        ( vlc_object_t * );
43
44 static int  SetFormat    ( aout_thread_t * );
45 static int  GetBufInfo   ( aout_thread_t *, int );
46 static void Play         ( aout_thread_t *, byte_t *, int );
47
48 static void HandleXrun   ( aout_thread_t *);
49
50 /*****************************************************************************
51  * Module descriptor
52  *****************************************************************************/
53 vlc_module_begin();
54     add_category_hint( N_("Device"), NULL );
55     add_string( "alsa-device", NULL, NULL, N_("Name"), NULL );
56     set_description( _("ALSA audio module") );
57     set_capability( "audio output", 50 );
58     set_callbacks( Open, Close );
59 vlc_module_end();
60
61 /*****************************************************************************
62  * Preamble
63  *****************************************************************************/
64 typedef struct alsa_device_t
65 {
66     int i_num;
67 } alsa_device_t;
68
69 typedef struct alsa_card_t
70 {
71     int i_num;
72 } alsa_card_t;
73
74 /* here we store plugin dependant informations */
75
76 struct aout_sys_t
77 {
78     snd_pcm_t   * p_alsa_handle;
79     unsigned long buffer_time;
80     unsigned long period_time;
81     unsigned long chunk_size;
82     unsigned long buffer_size;
83     unsigned long rate;
84     unsigned int  bytes_per_sample;
85     unsigned int  samples_per_frame;
86     unsigned int  bytes_per_frame;
87 };
88
89 /*****************************************************************************
90  * Open: create a handle and open an alsa device
91  *****************************************************************************
92  * This function opens an alsa device, through the alsa API
93  *****************************************************************************/
94 static int Open( vlc_object_t *p_this )
95 {
96     aout_thread_t *p_aout = (aout_thread_t *)p_this;
97
98     /* Allows user to choose which ALSA device to use */
99     char  psz_alsadev[128];
100     char *psz_device, *psz_userdev;
101     int   i_ret;
102
103     /* Allocate structures */
104     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
105     if( p_aout->p_sys == NULL )
106     {
107         msg_Err( p_aout, "out of memory" );
108         return -1;
109     }
110
111     p_aout->pf_setformat = SetFormat;
112     p_aout->pf_getbufinfo = GetBufInfo;
113     p_aout->pf_play = Play;
114
115     /* Read in ALSA device preferences from configuration */
116     psz_userdev = config_GetPsz( p_aout, "alsa-device" );
117
118     if( psz_userdev )
119     {
120         psz_device = psz_userdev;
121     }
122     else
123     {
124         /* Use the internal logic to decide on the device name */
125         if( p_aout->i_format != AOUT_FMT_A52 )
126         {
127             psz_device = "default";
128         }
129         else
130         {
131             unsigned char s[4];
132             s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
133             s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
134             s[2] = 0;
135             s[3] = IEC958_AES3_CON_FS_48000;
136             sprintf( psz_alsadev,
137                      "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
138                      s[0], s[1], s[2], s[3] );
139             psz_device = psz_alsadev;
140         }
141     }
142
143     /* Open device */
144     i_ret = snd_pcm_open( &(p_aout->p_sys->p_alsa_handle),
145                           psz_device, SND_PCM_STREAM_PLAYBACK, 0);
146     if( i_ret != 0 )
147     {
148         msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
149                          psz_device, snd_strerror(i_ret) );
150         if( psz_userdev )
151         {
152             free( psz_userdev );
153         }
154
155         return -1;
156     }
157
158     if( psz_userdev )
159     {
160         free( psz_userdev );
161     }
162
163     return 0;
164 }
165
166 /*****************************************************************************
167  * SetFormat : sets the alsa output format
168  *****************************************************************************
169  * This function prepares the device, sets the rate, format, the mode
170  * ( "play as soon as you have data" ), and buffer information.
171  *****************************************************************************/
172 static int SetFormat( aout_thread_t *p_aout )
173 {
174     int i_rv;
175     int i_format;
176
177     snd_pcm_hw_params_t *p_hw;
178     snd_pcm_sw_params_t *p_sw;
179
180     snd_pcm_hw_params_alloca(&p_hw);
181     snd_pcm_sw_params_alloca(&p_sw);
182
183     /* default value for snd_pcm_hw_params_set_buffer_time_near() */
184     p_aout->p_sys->buffer_time = AOUT_BUFFER_DURATION;
185
186     switch (p_aout->i_format)
187     {
188         case AOUT_FMT_S16_LE:
189             i_format = SND_PCM_FORMAT_S16_LE;
190             p_aout->p_sys->bytes_per_sample = 2;
191             break;
192
193         case AOUT_FMT_A52:
194             i_format = SND_PCM_FORMAT_S16_LE;
195             p_aout->p_sys->bytes_per_sample = 2;
196             /* buffer_time must be 500000 to avoid a system crash */
197             p_aout->p_sys->buffer_time = 500000;
198             break;
199
200         default:
201             i_format = SND_PCM_FORMAT_S16_BE;
202             p_aout->p_sys->bytes_per_sample = 2;
203             break;
204     }
205
206     p_aout->p_sys->samples_per_frame = p_aout->i_channels;
207     p_aout->p_sys->bytes_per_frame = p_aout->p_sys->samples_per_frame *
208                                      p_aout->p_sys->bytes_per_sample;
209
210     i_rv = snd_pcm_hw_params_any( p_aout->p_sys->p_alsa_handle, p_hw );
211     if( i_rv < 0 )
212     {
213         msg_Err( p_aout, "unable to retrieve initial parameters" );
214         return -1;
215     }
216
217     i_rv = snd_pcm_hw_params_set_access( p_aout->p_sys->p_alsa_handle, p_hw,
218                                          SND_PCM_ACCESS_RW_INTERLEAVED );
219     if( i_rv < 0 )
220     {
221         msg_Err( p_aout, "unable to set interleaved stream format" );
222         return -1;
223     }
224
225     i_rv = snd_pcm_hw_params_set_format( p_aout->p_sys->p_alsa_handle,
226                                          p_hw, i_format );
227     if( i_rv < 0 )
228     {
229         msg_Err( p_aout, "unable to set stream sample size and word order" );
230         return -1;
231     }
232
233     i_rv = snd_pcm_hw_params_set_channels( p_aout->p_sys->p_alsa_handle, p_hw,
234                                            p_aout->i_channels );
235     if( i_rv < 0 )
236     {
237         msg_Err( p_aout, "unable to set number of output channels" );
238         return -1;
239     }
240
241     i_rv = snd_pcm_hw_params_set_rate_near( p_aout->p_sys->p_alsa_handle, p_hw,
242                                             p_aout->i_rate, 0 );
243     if( i_rv < 0 )
244     {
245         msg_Err( p_aout, "unable to set sample rate" );
246         return -1;
247     }
248     p_aout->p_sys->rate = i_rv;
249
250     i_rv = snd_pcm_hw_params_set_buffer_time_near( p_aout->p_sys->p_alsa_handle,
251                                                    p_hw,
252                                                    p_aout->p_sys->buffer_time,
253                                                    0 );
254     if( i_rv < 0 )
255     {
256         msg_Err( p_aout, "unable to set buffer time" );
257         return -1;
258     }
259     p_aout->p_sys->buffer_time = i_rv;
260
261     i_rv = snd_pcm_hw_params_set_period_time_near( p_aout->p_sys->p_alsa_handle,
262          p_hw, p_aout->p_sys->buffer_time / p_aout->p_sys->bytes_per_frame, 0 );
263     if( i_rv < 0 )
264     {
265         msg_Err( p_aout, "unable to set period time" );
266         return -1;
267     }
268     p_aout->p_sys->period_time = i_rv;
269
270     i_rv = snd_pcm_hw_params(p_aout->p_sys->p_alsa_handle, p_hw);
271     if (i_rv < 0)
272     {
273         msg_Err( p_aout, "unable to set hardware configuration" );
274         return -1;
275     }
276
277     p_aout->p_sys->chunk_size = snd_pcm_hw_params_get_period_size( p_hw, 0 );
278     p_aout->p_sys->buffer_size = snd_pcm_hw_params_get_buffer_size( p_hw );
279
280     snd_pcm_sw_params_current( p_aout->p_sys->p_alsa_handle, p_sw );
281     i_rv = snd_pcm_sw_params_set_sleep_min( p_aout->p_sys->p_alsa_handle, p_sw,
282                                             0 );
283
284     i_rv = snd_pcm_sw_params_set_avail_min( p_aout->p_sys->p_alsa_handle, p_sw,
285                                             p_aout->p_sys->chunk_size );
286
287     /* Worked with the CVS version but not with 0.9beta3
288     i_rv = snd_pcm_sw_params_set_start_threshold( p_aout->p_sys->p_alsa_handle,
289                                             p_sw, p_aout->p_sys->buffer_size );
290
291     i_rv = snd_pcm_sw_params_set_stop_threshold( p_aout->p_sys->p_alsa_handle,
292                                              p_sw, p_aout->p_sys->buffer_size);
293     */
294     i_rv = snd_pcm_sw_params( p_aout->p_sys->p_alsa_handle, p_sw );
295     if( i_rv < 0 )
296     {
297         msg_Err( p_aout, "unable to set software configuration" );
298         return -1;
299     }
300
301     return 0;
302 }
303
304 /*****************************************************************************
305  * HandleXrun : reprepare the output
306  *****************************************************************************
307  * When buffer gets empty, the driver goes in "Xrun" state, where it needs
308  * to be reprepared before playing again
309  *****************************************************************************/
310 static void HandleXrun(aout_thread_t *p_aout)
311 {
312     int i_rv;
313
314     msg_Err( p_aout, "resetting output after buffer underrun" );
315
316 //    i_rv = snd_pcm_reset( p_aout->p_sys->p_alsa_handle );
317     i_rv = snd_pcm_prepare( p_aout->p_sys->p_alsa_handle );
318     if( i_rv < 0 )
319     {
320         msg_Err( p_aout, "unable to recover from buffer underrun (%s)",
321                          snd_strerror( i_rv ) );
322     }
323 }
324
325
326 /*****************************************************************************
327  * BufInfo: buffer status query
328  *****************************************************************************
329  * This function returns the number of used byte in the queue.
330  * It also deals with errors : indeed if the device comes to run out
331  * of data to play, it switches to the "underrun" status. It has to
332  * be flushed and re-prepared
333  *****************************************************************************/
334 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
335 {
336     snd_pcm_status_t *p_status;
337     int i_alsa_get_status_returns;
338
339     snd_pcm_status_alloca( &p_status );
340
341     i_alsa_get_status_returns = snd_pcm_status( p_aout->p_sys->p_alsa_handle,
342                                                 p_status );
343
344     if( i_alsa_get_status_returns )
345     {
346         msg_Err( p_aout, "failed getting alsa buffer info (%s)",
347                          snd_strerror ( i_alsa_get_status_returns ) );
348         return ( -1 );
349     }
350
351     switch( snd_pcm_status_get_state( p_status ) )
352     {
353         case SND_PCM_STATE_XRUN :
354             HandleXrun( p_aout );
355             break;
356
357         case SND_PCM_STATE_OPEN:
358         case SND_PCM_STATE_PREPARED:
359         case SND_PCM_STATE_RUNNING:
360             break;
361
362         default:
363             msg_Err( p_aout, "unhandled condition %i",
364                              snd_pcm_status_get_state( p_status ) );
365             break;
366     }
367
368     return snd_pcm_status_get_avail(p_status) * p_aout->p_sys->bytes_per_frame;
369 }
370
371 /*****************************************************************************
372  * Play : plays a sample
373  *****************************************************************************
374  * Plays a sample using the snd_pcm_writei function from the alsa API
375  *****************************************************************************/
376 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
377 {
378     snd_pcm_uframes_t tot_frames;
379     snd_pcm_uframes_t frames_left;
380     snd_pcm_uframes_t rv;
381
382     tot_frames = i_size / p_aout->p_sys->bytes_per_frame;
383     frames_left = tot_frames;
384
385     while( frames_left > 0 )
386     {
387         rv = snd_pcm_writei( p_aout->p_sys->p_alsa_handle, buffer +
388                              (tot_frames - frames_left) *
389                              p_aout->p_sys->bytes_per_frame, frames_left );
390
391         if( (signed int) rv < 0 )
392         {
393             msg_Err( p_aout, "failed writing to output (%s)",
394                              snd_strerror( rv ) );
395             return;
396         }
397
398         frames_left -= rv;
399     }
400 }
401
402 /*****************************************************************************
403  * Close: close the Alsa device
404  *****************************************************************************/
405 static void Close( vlc_object_t *p_this )
406 {
407     aout_thread_t *p_aout = (aout_thread_t *)p_this;
408     int i_close_returns;
409
410     i_close_returns = snd_pcm_close( p_aout->p_sys->p_alsa_handle );
411
412     if( i_close_returns )
413     {
414         msg_Err( p_aout, "failed closing ALSA device (%s)",
415                          snd_strerror( i_close_returns ) );
416     }
417
418     free( p_aout->p_sys );
419 }
420