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