1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.2 2002/08/14 10:50:12 bozo 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
10 * Arnaud de Bossoreille de Ribou <bozo@via.ecp.fr> - S/PDIF and aout3
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.
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.
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 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
30 #include <errno.h> /* ENOMEM */
31 #include <string.h> /* strerror() */
32 #include <stdlib.h> /* calloc(), malloc(), free() */
38 #include "aout_internal.h"
41 #include <alsa/asoundlib.h>
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 *****************************************************************************/
51 snd_pcm_t * p_snd_pcm;
52 snd_pcm_sframes_t i_buffer_size;
55 volatile vlc_bool_t b_initialized;
58 snd_output_t * p_snd_stderr;
62 /* These values are in frames.
63 * To convert them to a numer of bytes you have to multiply them by the
64 * number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
66 #define ALSA_DEFAULT_PERIOD_SIZE 2048
67 #define ALSA_DEFAULT_BUFFER_SIZE ( ALSA_DEFAULT_PERIOD_SIZE << 4 )
68 #define ALSA_SPDIF_PERIOD_SIZE 1536
69 #define ALSA_SPDIF_BUFFER_SIZE ( ALSA_SPDIF_PERIOD_SIZE << 4 )
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close ( vlc_object_t * );
77 static int SetFormat ( aout_instance_t * );
78 static void Play ( aout_instance_t *,
81 static int ALSAThread ( aout_instance_t * );
82 static void ALSAFill ( aout_instance_t * );
84 /*****************************************************************************
86 *****************************************************************************/
88 add_category_hint( N_("Audio"), NULL );
89 add_string( "alsa-device", NULL, NULL, N_("Name"), NULL );
90 set_description( _("ALSA audio module") );
91 set_capability( "audio output", 50 );
92 set_callbacks( Open, Close );
95 /*****************************************************************************
96 * Open: create a handle and open an alsa device
97 *****************************************************************************
98 * This function opens an alsa device, through the alsa API
99 *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
102 aout_instance_t * p_aout = (aout_instance_t *)p_this;
103 struct aout_sys_t * p_sys;
106 /* Allows user to choose which ALSA device to use */
107 char psz_alsadev[128];
111 /* Allocate structures */
112 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
115 msg_Err( p_aout, "out of memory" );
120 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
123 /* Read in ALSA device preferences from configuration */
124 psz_userdev = config_GetPsz( p_aout, "alsa-device" );
128 psz_device = psz_userdev;
132 /* Use the internal logic to decide on the device name */
133 if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
136 s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
137 s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
139 s[3] = IEC958_AES3_CON_FS_48000;
140 sprintf( psz_alsadev,
141 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
142 s[0], s[1], s[2], s[3] );
143 psz_device = psz_alsadev;
144 p_sys->i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
145 p_aout->output.i_nb_samples = ALSA_SPDIF_PERIOD_SIZE;
149 psz_device = "default";
150 p_sys->i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
151 p_aout->output.i_nb_samples = ALSA_DEFAULT_PERIOD_SIZE;
156 i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
157 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
160 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
161 psz_device, snd_strerror(i_snd_rc) );
165 p_sys->p_snd_pcm = NULL;
174 /* Create ALSA thread and wait for its readiness. */
175 p_sys->b_initialized = VLC_FALSE;
176 if( vlc_thread_create( p_aout, "aout", ALSAThread, VLC_FALSE ) )
178 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
185 p_aout->output.pf_setformat = SetFormat;
186 p_aout->output.pf_play = Play;
191 /*****************************************************************************
192 * SetFormat : sets the alsa output format
193 *****************************************************************************
194 * This function prepares the device, sets the rate, format, the mode
195 * ( "play as soon as you have data" ), and buffer information.
196 *****************************************************************************/
197 static int SetFormat( aout_instance_t * p_aout )
199 struct aout_sys_t * p_sys = p_aout->output.p_sys;
204 snd_pcm_hw_params_t *p_hw;
205 snd_pcm_sw_params_t *p_sw;
207 snd_pcm_hw_params_alloca(&p_hw);
208 snd_pcm_sw_params_alloca(&p_sw);
210 switch (p_aout->output.output.i_format)
212 case AOUT_FMT_MU_LAW: i_format = SND_PCM_FORMAT_MU_LAW; break;
213 case AOUT_FMT_A_LAW: i_format = SND_PCM_FORMAT_A_LAW; break;
214 case AOUT_FMT_IMA_ADPCM: i_format = SND_PCM_FORMAT_IMA_ADPCM; break;
215 case AOUT_FMT_U8: i_format = SND_PCM_FORMAT_U8; break;
216 case AOUT_FMT_S16_LE: i_format = SND_PCM_FORMAT_S16_LE; break;
217 case AOUT_FMT_S16_BE: i_format = SND_PCM_FORMAT_S16_BE; break;
218 case AOUT_FMT_S8: i_format = SND_PCM_FORMAT_S8; break;
219 case AOUT_FMT_U16_LE: i_format = SND_PCM_FORMAT_U16_LE; break;
220 case AOUT_FMT_U16_BE: i_format = SND_PCM_FORMAT_U16_BE; break;
221 case AOUT_FMT_FLOAT32: i_format = SND_PCM_FORMAT_FLOAT; break;
222 case AOUT_FMT_FIXED32:
224 msg_Err( p_aout, "audio output format 0x%x not supported",
225 p_aout->output.output.i_format );
230 i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw );
233 msg_Err( p_aout, "unable to retrieve initial hardware parameters" );
237 i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
238 SND_PCM_ACCESS_RW_INTERLEAVED );
241 msg_Err( p_aout, "unable to set interleaved stream format" );
245 i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw, i_format );
248 msg_Err( p_aout, "unable to set stream sample size and word order" );
252 i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
253 p_aout->output.output.i_channels );
256 msg_Err( p_aout, "unable to set number of output channels" );
260 i_snd_rc = snd_pcm_hw_params_set_rate( p_sys->p_snd_pcm, p_hw,
261 p_aout->output.output.i_rate, 0 );
264 msg_Err( p_aout, "unable to set sample rate" );
268 i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm, p_hw,
269 p_sys->i_buffer_size );
272 msg_Err( p_aout, "unable to set buffer time" );
275 p_sys->i_buffer_size = i_snd_rc;
277 i_snd_rc = snd_pcm_hw_params_set_period_size_near(
278 p_sys->p_snd_pcm, p_hw, p_aout->output.i_nb_samples, 0 );
281 msg_Err( p_aout, "unable to set period size" );
284 p_aout->output.i_nb_samples = i_snd_rc;
285 p_sys->i_period_time = snd_pcm_hw_params_get_period_time( p_hw, 0 );
287 i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw );
290 msg_Err( p_aout, "unable to set hardware configuration" );
294 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
295 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
297 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
298 p_aout->output.i_nb_samples );
300 i_snd_rc = snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw );
303 msg_Err( p_aout, "unable to set software configuration" );
308 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
309 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
310 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
311 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
312 snd_output_printf( p_sys->p_snd_stderr, "\n" );
315 p_sys->b_initialized = VLC_TRUE;
320 /*****************************************************************************
321 * Play: queue a buffer for playing by ALSAThread
322 *****************************************************************************/
323 static void Play( aout_instance_t *p_aout, aout_buffer_t * p_buffer )
325 aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
328 /*****************************************************************************
329 * Close: close the Alsa device
330 *****************************************************************************/
331 static void Close( vlc_object_t *p_this )
333 aout_instance_t *p_aout = (aout_instance_t *)p_this;
334 struct aout_sys_t * p_sys = p_aout->output.p_sys;
338 vlc_thread_join( p_aout );
340 if( p_sys->p_snd_pcm )
342 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
346 msg_Err( p_aout, "failed closing ALSA device (%s)",
347 snd_strerror( i_snd_rc ) );
354 /*****************************************************************************
355 * ALSAThread: asynchronous thread used to DMA the data to the device
356 *****************************************************************************/
357 static int ALSAThread( aout_instance_t * p_aout )
359 struct aout_sys_t * p_sys = p_aout->output.p_sys;
361 while ( !p_aout->b_die )
363 if( !p_sys->b_initialized )
365 msleep( THREAD_SLEEP );
371 msleep( p_sys->i_period_time );
377 /*****************************************************************************
378 * ALSAFill: function used to fill the ALSA buffer as much as possible
379 *****************************************************************************/
380 static void ALSAFill( aout_instance_t * p_aout )
382 struct aout_sys_t * p_sys = p_aout->output.p_sys;
384 aout_buffer_t * p_buffer;
385 mtime_t next_date = 0;
386 snd_pcm_status_t * p_status;
387 snd_timestamp_t ts_next;
388 int i_snd_rc, i_size;
390 snd_pcm_uframes_t i_avail;
392 snd_pcm_status_alloca( &p_status );
394 i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
397 msg_Err( p_aout, "alsa device not ready !!! (%s)",
398 snd_strerror( i_snd_rc ) );
404 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
407 msg_Err( p_aout, "unable to get the device's status (%s)",
408 snd_strerror( i_snd_rc ) );
412 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
414 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
417 msg_Err( p_aout, "recovered from buffer underrun" );
419 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
423 "unable to get the device's status after recovery (%s)",
424 snd_strerror( i_snd_rc ) );
430 msg_Err( p_aout, "unable to recover from buffer underrun" );
435 i_avail = snd_pcm_status_get_avail( p_status );
436 if( i_avail >= p_aout->output.i_nb_samples )
438 snd_pcm_status_get_tstamp( p_status, &ts_next );
439 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
441 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0 );
443 if( p_buffer == NULL )
446 p_bytes = p_buffer->p_buffer;
448 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_bytes,
449 p_buffer->i_nb_samples );
453 msg_Err( p_aout, "write failed (%s)",
454 snd_strerror( i_snd_rc ) );
458 aout_BufferFree( p_buffer );