1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.7 2002/08/24 10:19:42 sam 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;
57 volatile vlc_bool_t b_can_sleek;
60 snd_output_t * p_snd_stderr;
64 #define A52_FRAME_NB 1536
66 /* These values are in frames.
67 To convert them to a number of bytes you have to multiply them by the
68 number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
70 #define ALSA_DEFAULT_PERIOD_SIZE 2048
71 #define ALSA_DEFAULT_BUFFER_SIZE ( ALSA_DEFAULT_PERIOD_SIZE << 4 )
72 #define ALSA_SPDIF_PERIOD_SIZE A52_FRAME_NB
73 #define ALSA_SPDIF_BUFFER_SIZE ( ALSA_SPDIF_PERIOD_SIZE << 4 )
75 /*****************************************************************************
77 *****************************************************************************/
78 static int Open ( vlc_object_t * );
79 static void Close ( vlc_object_t * );
81 static int SetFormat ( aout_instance_t * );
82 static void Play ( aout_instance_t * );
84 static int ALSAThread ( aout_instance_t * );
85 static void ALSAFill ( aout_instance_t * );
87 /*****************************************************************************
89 *****************************************************************************/
91 add_category_hint( N_("ALSA"), NULL );
92 add_string( "alsa-device", NULL, NULL, N_("device name"), NULL );
93 set_description( _("ALSA audio module") );
94 set_capability( "audio output", 50 );
95 set_callbacks( Open, Close );
98 /*****************************************************************************
99 * Open: create a handle and open an alsa device
100 *****************************************************************************
101 * This function opens an alsa device, through the alsa API
102 *****************************************************************************/
103 static int Open( vlc_object_t *p_this )
105 aout_instance_t * p_aout = (aout_instance_t *)p_this;
106 struct aout_sys_t * p_sys;
108 /* Allocate structures */
109 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
112 msg_Err( p_aout, "out of memory" );
116 /* Create ALSA thread and wait for its readiness. */
117 p_sys->b_initialized = VLC_FALSE;
118 if( vlc_thread_create( p_aout, "aout", ALSAThread, VLC_FALSE ) )
120 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
125 p_aout->output.pf_setformat = SetFormat;
126 p_aout->output.pf_play = Play;
129 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
135 /*****************************************************************************
136 * SetFormat : sets the alsa output format
137 *****************************************************************************
138 * This function prepares the device, sets the rate, format, the mode
139 * ( "play as soon as you have data" ), and buffer information.
140 *****************************************************************************/
141 static int SetFormat( aout_instance_t * p_aout )
143 struct aout_sys_t * p_sys = p_aout->output.p_sys;
148 char psz_alsadev[128];
154 snd_pcm_hw_params_t *p_hw;
155 snd_pcm_sw_params_t *p_sw;
157 /* Read in ALSA device preferences from configuration */
158 psz_userdev = config_GetPsz( p_aout, "alsa-device" );
162 psz_device = psz_userdev;
166 /* Use the internal logic to decide on the device name */
167 if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
169 /* Will probably need some little modification in the case
170 we want to send some data at a different rate
171 (32000, 44100 and 48000 are the possibilities) -- bozo */
173 s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
174 s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
176 s[3] = IEC958_AES3_CON_FS_48000;
177 sprintf( psz_alsadev,
178 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
179 s[0], s[1], s[2], s[3] );
180 psz_device = psz_alsadev;
184 psz_device = "default";
189 i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
190 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
193 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
194 psz_device, snd_strerror(i_snd_rc) );
197 p_sys->p_snd_pcm = NULL;
204 /* Default settings */
205 p_sys->b_can_sleek = VLC_FALSE;
206 i_channels = p_aout->output.output.i_channels;
207 if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
209 p_sys->i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
210 p_aout->output.i_nb_samples = ALSA_SPDIF_PERIOD_SIZE;
214 p_sys->i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
215 p_aout->output.i_nb_samples = ALSA_DEFAULT_PERIOD_SIZE;
219 /* Compute the settings */
220 switch (p_aout->output.output.i_format)
222 case AOUT_FMT_MU_LAW: i_format = SND_PCM_FORMAT_MU_LAW; break;
223 case AOUT_FMT_A_LAW: i_format = SND_PCM_FORMAT_A_LAW; break;
224 case AOUT_FMT_IMA_ADPCM: i_format = SND_PCM_FORMAT_IMA_ADPCM; break;
225 case AOUT_FMT_U8: i_format = SND_PCM_FORMAT_U8; break;
226 case AOUT_FMT_S16_LE: i_format = SND_PCM_FORMAT_S16_LE; break;
227 case AOUT_FMT_S16_BE: i_format = SND_PCM_FORMAT_S16_BE; break;
228 case AOUT_FMT_S8: i_format = SND_PCM_FORMAT_S8; break;
229 case AOUT_FMT_U16_LE: i_format = SND_PCM_FORMAT_U16_LE; break;
230 case AOUT_FMT_U16_BE: i_format = SND_PCM_FORMAT_U16_BE; break;
231 case AOUT_FMT_FLOAT32: i_format = SND_PCM_FORMAT_FLOAT; break;
233 /* Override some settings to make S/PDIF work */
234 p_sys->b_can_sleek = VLC_TRUE;
235 i_format = SND_PCM_FORMAT_S16_LE;
237 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
238 p_aout->output.output.i_frame_length = ALSA_SPDIF_PERIOD_SIZE;
240 case AOUT_FMT_FIXED32:
242 msg_Err( p_aout, "audio output format 0x%x not supported",
243 p_aout->output.output.i_format );
248 snd_pcm_hw_params_alloca(&p_hw);
249 snd_pcm_sw_params_alloca(&p_sw);
251 i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw );
254 msg_Err( p_aout, "unable to retrieve initial hardware parameters" );
258 i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
259 SND_PCM_ACCESS_RW_INTERLEAVED );
262 msg_Err( p_aout, "unable to set interleaved stream format" );
266 i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw, i_format );
269 msg_Err( p_aout, "unable to set stream sample size and word order" );
273 i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
277 msg_Err( p_aout, "unable to set number of output channels" );
281 i_snd_rc = snd_pcm_hw_params_set_rate( p_sys->p_snd_pcm, p_hw,
282 p_aout->output.output.i_rate, 0 );
285 msg_Err( p_aout, "unable to set sample rate" );
289 i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm, p_hw,
290 p_sys->i_buffer_size );
293 msg_Err( p_aout, "unable to set buffer time" );
296 p_sys->i_buffer_size = i_snd_rc;
298 i_snd_rc = snd_pcm_hw_params_set_period_size_near(
299 p_sys->p_snd_pcm, p_hw, p_aout->output.i_nb_samples, 0 );
302 msg_Err( p_aout, "unable to set period size" );
305 p_aout->output.i_nb_samples = i_snd_rc;
306 p_sys->i_period_time = snd_pcm_hw_params_get_period_time( p_hw, 0 );
308 i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw );
311 msg_Err( p_aout, "unable to set hardware configuration" );
315 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
316 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
318 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
319 p_aout->output.i_nb_samples );
321 i_snd_rc = snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw );
324 msg_Err( p_aout, "unable to set software configuration" );
329 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
330 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
331 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
332 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
333 snd_output_printf( p_sys->p_snd_stderr, "\n" );
336 p_sys->b_initialized = VLC_TRUE;
341 /*****************************************************************************
342 * Play: queue a buffer for playing by ALSAThread
343 *****************************************************************************/
344 static void Play( aout_instance_t *p_aout )
348 /*****************************************************************************
349 * Close: close the Alsa device
350 *****************************************************************************/
351 static void Close( vlc_object_t *p_this )
353 aout_instance_t *p_aout = (aout_instance_t *)p_this;
354 struct aout_sys_t * p_sys = p_aout->output.p_sys;
358 vlc_thread_join( p_aout );
360 if( p_sys->p_snd_pcm )
362 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
366 msg_Err( p_aout, "failed closing ALSA device (%s)",
367 snd_strerror( i_snd_rc ) );
372 snd_output_close( p_sys->p_snd_stderr );
378 /*****************************************************************************
379 * ALSAThread: asynchronous thread used to DMA the data to the device
380 *****************************************************************************/
381 static int ALSAThread( aout_instance_t * p_aout )
383 struct aout_sys_t * p_sys = p_aout->output.p_sys;
385 while ( !p_aout->b_die && !p_sys->b_initialized )
386 msleep( THREAD_SLEEP );
388 while ( !p_aout->b_die )
392 /* Sleep during less than one period to avoid a lot of buffer
394 msleep( p_sys->i_period_time >> 2 );
400 /*****************************************************************************
401 * ALSAFill: function used to fill the ALSA buffer as much as possible
402 *****************************************************************************/
403 static void ALSAFill( aout_instance_t * p_aout )
405 struct aout_sys_t * p_sys = p_aout->output.p_sys;
407 aout_buffer_t * p_buffer;
408 snd_pcm_status_t * p_status;
409 snd_timestamp_t ts_next;
411 snd_pcm_uframes_t i_avail;
413 snd_pcm_status_alloca( &p_status );
415 /* Wait for the device's readiness (ie. there is enough space in the
416 buffer to write at least one complete chunk) */
417 i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
420 msg_Err( p_aout, "alsa device not ready !!! (%s)",
421 snd_strerror( i_snd_rc ) );
425 /* Fill in the buffer until space or audio output buffer shortage */
429 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
432 msg_Err( p_aout, "unable to get the device's status (%s)",
433 snd_strerror( i_snd_rc ) );
437 /* Handle buffer underruns and reget the status */
438 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
440 /* Prepare the device */
441 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
445 msg_Warn( p_aout, "recovered from buffer underrun" );
447 /* Reget the status */
448 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
452 "unable to get the device's status after recovery (%s)",
453 snd_strerror( i_snd_rc ) );
459 msg_Err( p_aout, "unable to recover from buffer underrun" );
464 /* Here the device should be either in the RUNNING state either in
465 the PREPARE state. p_status is valid. */
467 /* Try to write only if there is enough space */
468 i_avail = snd_pcm_status_get_avail( p_status );
470 if( i_avail >= p_aout->output.i_nb_samples )
473 snd_pcm_status_get_tstamp( p_status, &ts_next );
474 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
476 p_buffer = aout_OutputNextBuffer( p_aout, next_date, 0,
477 p_sys->b_can_sleek );
479 /* Audio output buffer shortage -> stop the fill process and
480 wait in ALSAThread */
481 if( p_buffer == NULL )
484 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
485 p_buffer->i_nb_samples );
489 msg_Err( p_aout, "write failed (%s)",
490 snd_strerror( i_snd_rc ) );
494 aout_BufferFree( p_buffer );