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 $
7 * Authors: Henri Fallon <henri@videolan.org> - Original Author
8 * Jeffrey Baker <jwbaker@acm.org> - Port to ALSA 1.0 API
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
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() */
33 #include <alsa/asoundlib.h>
35 #include <videolan/vlc.h>
37 #include "audio_output.h" /* aout_thread_t */
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 * );
50 /*****************************************************************************
51 * Build configuration tree.
52 *****************************************************************************/
58 SET_DESCRIPTION( _("ALSA audio module") )
59 ADD_CAPABILITY( AOUT, 50 )
60 ADD_SHORTCUT( "alsa" )
64 aout_getfunctions( &p_module->p_functions->aout );
67 MODULE_DEACTIVATE_START
68 MODULE_DEACTIVATE_STOP
70 /*****************************************************************************
72 *****************************************************************************/
73 typedef struct alsa_device_s
78 typedef struct alsa_card_s
83 /* here we store plugin dependant informations */
85 typedef struct aout_sys_s
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;
93 unsigned int bytes_per_sample;
94 unsigned int samples_per_frame;
95 unsigned int bytes_per_frame;
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 )
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;
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 )
119 char psz_alsadev[128];
121 /* Allocate structures */
122 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
123 if( p_aout->p_sys == NULL )
125 intf_ErrMsg( "aout error: failed allocating memory for ALSA (%s)",
130 if( p_aout->i_format != AOUT_FMT_AC3 )
132 strcpy( psz_alsadev, "default" );
137 s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
138 s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
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] );
146 i_ret = snd_pcm_open( &(p_aout->p_sys->p_alsa_handle),
147 psz_alsadev, SND_PCM_STREAM_PLAYBACK, 0);
150 intf_ErrMsg( "aout error: could not open ALSA device (%s)",
151 snd_strerror( i_ret ) );
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 )
170 snd_pcm_hw_params_t *p_hw;
171 snd_pcm_sw_params_t *p_sw;
173 snd_pcm_hw_params_alloca(&p_hw);
174 snd_pcm_sw_params_alloca(&p_sw);
176 /* default value for snd_pcm_hw_params_set_buffer_time_near() */
177 p_aout->p_sys->buffer_time = AOUT_BUFFER_DURATION;
179 switch (p_aout->i_format)
181 case AOUT_FMT_S16_LE:
182 i_format = SND_PCM_FORMAT_S16_LE;
183 p_aout->p_sys->bytes_per_sample = 2;
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;
194 i_format = SND_PCM_FORMAT_S16_BE;
195 p_aout->p_sys->bytes_per_sample = 2;
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;
203 i_rv = snd_pcm_hw_params_any( p_aout->p_sys->p_alsa_handle, p_hw );
206 intf_ErrMsg( "aout error: unable to retrieve initial parameters" );
210 i_rv = snd_pcm_hw_params_set_access( p_aout->p_sys->p_alsa_handle, p_hw,
211 SND_PCM_ACCESS_RW_INTERLEAVED );
214 intf_ErrMsg( "aout error: unable to set interleaved stream format" );
218 i_rv = snd_pcm_hw_params_set_format( p_aout->p_sys->p_alsa_handle,
222 intf_ErrMsg( "aout error: unable to set stream sample size and word"
227 i_rv = snd_pcm_hw_params_set_channels( p_aout->p_sys->p_alsa_handle, p_hw,
228 p_aout->i_channels );
231 intf_ErrMsg( "aout error: unable to set number of output channels" );
235 i_rv = snd_pcm_hw_params_set_rate_near( p_aout->p_sys->p_alsa_handle, p_hw,
239 intf_ErrMsg( "aout error: unable to set sample rate" );
242 p_aout->p_sys->rate = i_rv;
244 i_rv = snd_pcm_hw_params_set_buffer_time_near( p_aout->p_sys->p_alsa_handle,
246 p_aout->p_sys->buffer_time,
250 intf_ErrMsg( "aout error: unable to set buffer time" );
253 p_aout->p_sys->buffer_time = i_rv;
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 );
259 intf_ErrMsg( "aout error: unable to set period time" );
262 p_aout->p_sys->period_time = i_rv;
264 i_rv = snd_pcm_hw_params(p_aout->p_sys->p_alsa_handle, p_hw);
267 intf_ErrMsg( "aout error: unable to set hardware configuration" );
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 );
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,
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 );
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 );
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);
288 i_rv = snd_pcm_sw_params( p_aout->p_sys->p_alsa_handle, p_sw );
291 intf_ErrMsg( "aout error: unable to set software configuration" );
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)
308 intf_ErrMsg( "aout error: resetting output after buffer underrun" );
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 );
314 intf_ErrMsg( "aout error: unable to recover from buffer underrun (%s)",
315 snd_strerror( i_rv ) );
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 )
330 snd_pcm_status_t *p_status;
331 int i_alsa_get_status_returns;
333 snd_pcm_status_alloca( &p_status );
335 i_alsa_get_status_returns = snd_pcm_status( p_aout->p_sys->p_alsa_handle,
338 if( i_alsa_get_status_returns )
340 intf_ErrMsg ( "aout error: failed getting alsa buffer info (%s)",
341 snd_strerror ( i_alsa_get_status_returns ) );
345 switch( snd_pcm_status_get_state( p_status ) )
347 case SND_PCM_STATE_XRUN :
348 aout_HandleXrun( p_aout );
351 case SND_PCM_STATE_OPEN:
352 case SND_PCM_STATE_PREPARED:
353 case SND_PCM_STATE_RUNNING:
357 intf_ErrMsg( "aout error: unhandled condition %i",
358 snd_pcm_status_get_state( p_status ) );
362 return( snd_pcm_status_get_avail(p_status) *
363 p_aout->p_sys->bytes_per_frame );
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 )
373 snd_pcm_uframes_t tot_frames;
374 snd_pcm_uframes_t frames_left;
375 snd_pcm_uframes_t rv;
377 tot_frames = i_size / p_aout->p_sys->bytes_per_frame;
378 frames_left = tot_frames;
380 while( frames_left > 0 )
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 );
386 if( (signed int) rv < 0 )
388 intf_ErrMsg( "aout error: failed writing to output (%s)",
389 snd_strerror( rv ) );
397 /*****************************************************************************
398 * aout_Close : close the Alsa device
399 *****************************************************************************/
400 static void aout_Close( aout_thread_t *p_aout )
404 i_close_returns = snd_pcm_close( p_aout->p_sys->p_alsa_handle );
406 if( i_close_returns )
408 intf_ErrMsg( "aout error: failed closing ALSA device (%s)",
409 i_close_returns, snd_strerror( i_close_returns ) );
412 free( p_aout->p_sys );