1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.1 2002/08/07 21:36:55 massiot Exp $
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
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.
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.
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 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
29 #include <errno.h> /* ENOMEM */
30 #include <string.h> /* strerror() */
31 #include <stdlib.h> /* calloc(), malloc(), free() */
36 #include <alsa/asoundlib.h>
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
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 );
48 static void HandleXrun ( aout_thread_t *);
50 /*****************************************************************************
52 *****************************************************************************/
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 );
61 /*****************************************************************************
63 *****************************************************************************/
64 typedef struct alsa_device_t
69 typedef struct alsa_card_t
74 /* here we store plugin dependant informations */
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;
84 unsigned int bytes_per_sample;
85 unsigned int samples_per_frame;
86 unsigned int bytes_per_frame;
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 )
96 aout_thread_t *p_aout = (aout_thread_t *)p_this;
98 /* Allows user to choose which ALSA device to use */
99 char psz_alsadev[128];
100 char *psz_device, *psz_userdev;
103 /* Allocate structures */
104 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
105 if( p_aout->p_sys == NULL )
107 msg_Err( p_aout, "out of memory" );
111 p_aout->pf_setformat = SetFormat;
112 p_aout->pf_getbufinfo = GetBufInfo;
113 p_aout->pf_play = Play;
115 /* Read in ALSA device preferences from configuration */
116 psz_userdev = config_GetPsz( p_aout, "alsa-device" );
120 psz_device = psz_userdev;
124 /* Use the internal logic to decide on the device name */
125 if( p_aout->i_format != AOUT_FMT_A52 )
127 psz_device = "default";
132 s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
133 s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
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;
144 i_ret = snd_pcm_open( &(p_aout->p_sys->p_alsa_handle),
145 psz_device, SND_PCM_STREAM_PLAYBACK, 0);
148 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
149 psz_device, snd_strerror(i_ret) );
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 )
177 snd_pcm_hw_params_t *p_hw;
178 snd_pcm_sw_params_t *p_sw;
180 snd_pcm_hw_params_alloca(&p_hw);
181 snd_pcm_sw_params_alloca(&p_sw);
183 /* default value for snd_pcm_hw_params_set_buffer_time_near() */
184 p_aout->p_sys->buffer_time = AOUT_BUFFER_DURATION;
186 switch (p_aout->i_format)
188 case AOUT_FMT_S16_LE:
189 i_format = SND_PCM_FORMAT_S16_LE;
190 p_aout->p_sys->bytes_per_sample = 2;
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;
201 i_format = SND_PCM_FORMAT_S16_BE;
202 p_aout->p_sys->bytes_per_sample = 2;
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;
210 i_rv = snd_pcm_hw_params_any( p_aout->p_sys->p_alsa_handle, p_hw );
213 msg_Err( p_aout, "unable to retrieve initial parameters" );
217 i_rv = snd_pcm_hw_params_set_access( p_aout->p_sys->p_alsa_handle, p_hw,
218 SND_PCM_ACCESS_RW_INTERLEAVED );
221 msg_Err( p_aout, "unable to set interleaved stream format" );
225 i_rv = snd_pcm_hw_params_set_format( p_aout->p_sys->p_alsa_handle,
229 msg_Err( p_aout, "unable to set stream sample size and word order" );
233 i_rv = snd_pcm_hw_params_set_channels( p_aout->p_sys->p_alsa_handle, p_hw,
234 p_aout->i_channels );
237 msg_Err( p_aout, "unable to set number of output channels" );
241 i_rv = snd_pcm_hw_params_set_rate_near( p_aout->p_sys->p_alsa_handle, p_hw,
245 msg_Err( p_aout, "unable to set sample rate" );
248 p_aout->p_sys->rate = i_rv;
250 i_rv = snd_pcm_hw_params_set_buffer_time_near( p_aout->p_sys->p_alsa_handle,
252 p_aout->p_sys->buffer_time,
256 msg_Err( p_aout, "unable to set buffer time" );
259 p_aout->p_sys->buffer_time = i_rv;
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 );
265 msg_Err( p_aout, "unable to set period time" );
268 p_aout->p_sys->period_time = i_rv;
270 i_rv = snd_pcm_hw_params(p_aout->p_sys->p_alsa_handle, p_hw);
273 msg_Err( p_aout, "unable to set hardware configuration" );
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 );
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,
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 );
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 );
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);
294 i_rv = snd_pcm_sw_params( p_aout->p_sys->p_alsa_handle, p_sw );
297 msg_Err( p_aout, "unable to set software configuration" );
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)
314 msg_Err( p_aout, "resetting output after buffer underrun" );
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 );
320 msg_Err( p_aout, "unable to recover from buffer underrun (%s)",
321 snd_strerror( i_rv ) );
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 )
336 snd_pcm_status_t *p_status;
337 int i_alsa_get_status_returns;
339 snd_pcm_status_alloca( &p_status );
341 i_alsa_get_status_returns = snd_pcm_status( p_aout->p_sys->p_alsa_handle,
344 if( i_alsa_get_status_returns )
346 msg_Err( p_aout, "failed getting alsa buffer info (%s)",
347 snd_strerror ( i_alsa_get_status_returns ) );
351 switch( snd_pcm_status_get_state( p_status ) )
353 case SND_PCM_STATE_XRUN :
354 HandleXrun( p_aout );
357 case SND_PCM_STATE_OPEN:
358 case SND_PCM_STATE_PREPARED:
359 case SND_PCM_STATE_RUNNING:
363 msg_Err( p_aout, "unhandled condition %i",
364 snd_pcm_status_get_state( p_status ) );
368 return snd_pcm_status_get_avail(p_status) * p_aout->p_sys->bytes_per_frame;
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 )
378 snd_pcm_uframes_t tot_frames;
379 snd_pcm_uframes_t frames_left;
380 snd_pcm_uframes_t rv;
382 tot_frames = i_size / p_aout->p_sys->bytes_per_frame;
383 frames_left = tot_frames;
385 while( frames_left > 0 )
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 );
391 if( (signed int) rv < 0 )
393 msg_Err( p_aout, "failed writing to output (%s)",
394 snd_strerror( rv ) );
402 /*****************************************************************************
403 * Close: close the Alsa device
404 *****************************************************************************/
405 static void Close( vlc_object_t *p_this )
407 aout_thread_t *p_aout = (aout_thread_t *)p_this;
410 i_close_returns = snd_pcm_close( p_aout->p_sys->p_alsa_handle );
412 if( i_close_returns )
414 msg_Err( p_aout, "failed closing ALSA device (%s)",
415 snd_strerror( i_close_returns ) );
418 free( p_aout->p_sys );