1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.13 2002/10/05 03:44:50 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_can_sleek;
58 snd_output_t * p_snd_stderr;
62 #define A52_FRAME_NB 1536
64 /* These values are in frames.
65 To convert them to a number of bytes you have to multiply them by the
66 number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
68 #define ALSA_DEFAULT_PERIOD_SIZE 2048
69 #define ALSA_DEFAULT_BUFFER_SIZE ( ALSA_DEFAULT_PERIOD_SIZE << 4 )
70 #define ALSA_SPDIF_PERIOD_SIZE A52_FRAME_NB
71 #define ALSA_SPDIF_BUFFER_SIZE ( ALSA_SPDIF_PERIOD_SIZE << 4 )
73 /*****************************************************************************
75 *****************************************************************************/
76 static int Open ( vlc_object_t * );
77 static void Close ( vlc_object_t * );
78 static void Play ( aout_instance_t * );
79 static int ALSAThread ( aout_instance_t * );
80 static void ALSAFill ( aout_instance_t * );
82 /*****************************************************************************
84 *****************************************************************************/
86 add_category_hint( N_("ALSA"), NULL );
87 add_string( "alsa-device", NULL, NULL, N_("device name"), NULL );
88 set_description( _("ALSA audio module") );
89 set_capability( "audio output", 50 );
90 set_callbacks( Open, Close );
93 /*****************************************************************************
94 * Open: create a handle and open an alsa device
95 *****************************************************************************
96 * This function opens an alsa device, through the alsa API
97 *****************************************************************************/
98 static int Open( vlc_object_t *p_this )
100 aout_instance_t * p_aout = (aout_instance_t *)p_this;
101 struct aout_sys_t * p_sys;
105 char * psz_device = NULL;
106 char psz_alsadev[128];
109 int i_format_tries[2];
110 int i_tries_count, i;
115 snd_pcm_hw_params_t *p_hw;
116 snd_pcm_sw_params_t *p_sw;
118 /* Allocate structures */
119 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
122 msg_Err( p_aout, "out of memory" );
126 p_aout->output.pf_play = Play;
129 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
133 i_format_tries[i_tries_count++] = p_aout->output.output.i_format;
134 if( i_format_tries[0] == VLC_FOURCC('s','p','d','i') )
136 /* Try S/PDIF, and then FLOAT32 */
137 i_format_tries[i_tries_count++] = VLC_FOURCC('f','l','3','2');
140 /* Read in ALSA device preferences from configuration */
141 psz_userdev = config_GetPsz( p_aout, "alsa-device" );
143 for( i = 0 ; ( i_snd_rc < 0 ) && ( i < i_tries_count ) ; ++i )
145 if( i_format_tries[i] == VLC_FOURCC('s','p','d','i') )
149 psz_device = psz_userdev;
153 /* Will probably need some little modification in the case
154 we want to send some data at a different rate
155 (32000, 44100 and 48000 are the possibilities) -- bozo */
157 s[0] = IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO;
158 s[1] = IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER;
160 s[3] = IEC958_AES3_CON_FS_48000;
161 sprintf( psz_alsadev,
162 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
163 s[0], s[1], s[2], s[3] );
164 psz_device = psz_alsadev;
170 psz_device = psz_userdev;
172 psz_device = "default";
176 i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
177 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK);
182 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
183 psz_device, snd_strerror(i_snd_rc) );
186 p_sys->p_snd_pcm = NULL;
193 p_aout->output.output.i_format = i_format_tries[i - 1];
195 /* Default settings */
196 p_sys->b_can_sleek = VLC_FALSE;
197 i_channels = p_aout->output.output.i_channels;
198 if ( p_aout->output.output.i_format == VLC_FOURCC('s','p','d','i') )
200 p_sys->i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
201 p_aout->output.i_nb_samples = ALSA_SPDIF_PERIOD_SIZE;
202 aout_VolumeNoneInit( p_aout );
206 p_sys->i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
207 p_aout->output.i_nb_samples = ALSA_DEFAULT_PERIOD_SIZE;
208 aout_VolumeSoftInit( p_aout );
211 /* Compute the settings */
212 switch (p_aout->output.output.i_format)
214 case VLC_FOURCC('f','l','3','2'):
215 i_format = SND_PCM_FORMAT_FLOAT;
218 case VLC_FOURCC('s','p','d','i'):
219 /* Override some settings to make S/PDIF work */
220 p_sys->b_can_sleek = VLC_TRUE;
221 i_format = SND_PCM_FORMAT_S16_LE;
223 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
224 p_aout->output.output.i_frame_length = ALSA_SPDIF_PERIOD_SIZE;
227 msg_Err( p_aout, "audio output format '%.4s' not supported",
228 &p_aout->output.output.i_format );
233 snd_pcm_hw_params_alloca(&p_hw);
234 snd_pcm_sw_params_alloca(&p_sw);
236 i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw );
239 msg_Err( p_aout, "unable to retrieve initial hardware parameters" );
243 i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
244 SND_PCM_ACCESS_RW_INTERLEAVED );
247 msg_Err( p_aout, "unable to set interleaved stream format" );
251 i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw, i_format );
254 msg_Err( p_aout, "unable to set stream sample size and word order" );
258 i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
262 msg_Err( p_aout, "unable to set number of output channels" );
266 i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
267 p_aout->output.output.i_rate,
271 msg_Err( p_aout, "unable to set sample rate" );
274 p_aout->output.output.i_rate = i_snd_rc;
276 i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm, p_hw,
277 p_sys->i_buffer_size );
280 msg_Err( p_aout, "unable to set buffer time" );
283 p_sys->i_buffer_size = i_snd_rc;
285 i_snd_rc = snd_pcm_hw_params_set_period_size_near(
286 p_sys->p_snd_pcm, p_hw, p_aout->output.i_nb_samples, NULL );
289 msg_Err( p_aout, "unable to set period size" );
292 p_aout->output.i_nb_samples = i_snd_rc;
294 i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw );
297 msg_Err( p_aout, "unable to set hardware configuration" );
301 p_sys->i_period_time = snd_pcm_hw_params_get_period_time( p_hw, NULL );
303 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
304 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
306 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
307 p_aout->output.i_nb_samples );
309 i_snd_rc = snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw );
312 msg_Err( p_aout, "unable to set software configuration" );
317 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
318 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
319 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
320 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
321 snd_output_printf( p_sys->p_snd_stderr, "\n" );
324 /* Create ALSA thread and wait for its readiness. */
325 if( vlc_thread_create( p_aout, "aout", ALSAThread,
326 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
328 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
336 /*****************************************************************************
337 * Play: nothing to do
338 *****************************************************************************/
339 static void Play( aout_instance_t *p_aout )
343 /*****************************************************************************
344 * Close: close the Alsa device
345 *****************************************************************************/
346 static void Close( vlc_object_t *p_this )
348 aout_instance_t *p_aout = (aout_instance_t *)p_this;
349 struct aout_sys_t * p_sys = p_aout->output.p_sys;
353 vlc_thread_join( p_aout );
355 if( p_sys->p_snd_pcm )
357 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
361 msg_Err( p_aout, "failed closing ALSA device (%s)",
362 snd_strerror( i_snd_rc ) );
367 snd_output_close( p_sys->p_snd_stderr );
373 /*****************************************************************************
374 * ALSAThread: asynchronous thread used to DMA the data to the device
375 *****************************************************************************/
376 static int ALSAThread( aout_instance_t * p_aout )
378 struct aout_sys_t * p_sys = p_aout->output.p_sys;
380 while ( !p_aout->b_die )
384 /* Sleep during less than one period to avoid a lot of buffer
386 msleep( p_sys->i_period_time >> 2 );
392 /*****************************************************************************
393 * ALSAFill: function used to fill the ALSA buffer as much as possible
394 *****************************************************************************/
395 static void ALSAFill( aout_instance_t * p_aout )
397 struct aout_sys_t * p_sys = p_aout->output.p_sys;
399 aout_buffer_t * p_buffer;
400 snd_pcm_status_t * p_status;
401 snd_timestamp_t ts_next;
403 snd_pcm_uframes_t i_avail;
405 snd_pcm_status_alloca( &p_status );
407 /* Wait for the device's readiness (ie. there is enough space in the
408 buffer to write at least one complete chunk) */
409 i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
412 msg_Err( p_aout, "alsa device not ready !!! (%s)",
413 snd_strerror( i_snd_rc ) );
417 /* Fill in the buffer until space or audio output buffer shortage */
421 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
424 msg_Err( p_aout, "unable to get the device's status (%s)",
425 snd_strerror( i_snd_rc ) );
429 /* Handle buffer underruns and reget the status */
430 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
432 /* Prepare the device */
433 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
437 msg_Warn( p_aout, "recovered from buffer underrun" );
439 /* Reget the status */
440 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
444 "unable to get the device's status after recovery (%s)",
445 snd_strerror( i_snd_rc ) );
451 msg_Err( p_aout, "unable to recover from buffer underrun" );
456 /* Here the device should be either in the RUNNING state either in
457 the PREPARE state. p_status is valid. */
459 /* Try to write only if there is enough space */
460 i_avail = snd_pcm_status_get_avail( p_status );
462 if( i_avail >= p_aout->output.i_nb_samples )
465 snd_pcm_status_get_tstamp( p_status, &ts_next );
466 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
468 p_buffer = aout_OutputNextBuffer( p_aout, next_date,
469 p_sys->b_can_sleek );
471 /* Audio output buffer shortage -> stop the fill process and
472 wait in ALSAThread */
473 if( p_buffer == NULL )
476 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
477 p_buffer->i_nb_bytes );
481 msg_Err( p_aout, "write failed (%s)",
482 snd_strerror( i_snd_rc ) );
486 aout_BufferFree( p_buffer );