1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.15 2002/10/22 20:55:27 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;
55 snd_output_t * p_snd_stderr;
59 #define A52_FRAME_NB 1536
61 /* These values are in frames.
62 To convert them to a number of bytes you have to multiply them by the
63 number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
65 #define ALSA_DEFAULT_PERIOD_SIZE 2048
66 #define ALSA_DEFAULT_BUFFER_SIZE ( ALSA_DEFAULT_PERIOD_SIZE << 4 )
67 #define ALSA_SPDIF_PERIOD_SIZE A52_FRAME_NB
68 #define ALSA_SPDIF_BUFFER_SIZE ( ALSA_SPDIF_PERIOD_SIZE << 4 )
69 /* Why << 4 ? --Meuuh */
71 /*****************************************************************************
73 *****************************************************************************/
74 static int Open ( vlc_object_t * );
75 static void Close ( vlc_object_t * );
76 static void Play ( aout_instance_t * );
77 static int ALSAThread ( aout_instance_t * );
78 static void ALSAFill ( aout_instance_t * );
80 /*****************************************************************************
82 *****************************************************************************/
84 add_category_hint( N_("ALSA"), NULL );
85 add_string( "alsa-device", "default", aout_FindAndRestart,
86 N_("device name"), NULL );
87 set_description( _("ALSA audio module") );
88 set_capability( "audio output", 50 );
89 set_callbacks( Open, Close );
92 /*****************************************************************************
93 * Open: create a handle and open an alsa device
94 *****************************************************************************
95 * This function opens an alsa device, through the alsa API
96 *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
99 aout_instance_t * p_aout = (aout_instance_t *)p_this;
100 struct aout_sys_t * p_sys;
102 int i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
103 int i_format = SND_PCM_FORMAT_S16;
105 snd_pcm_hw_params_t *p_hw;
106 snd_pcm_sw_params_t *p_sw;
110 /* Allocate structures */
111 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
114 msg_Err( p_aout, "out of memory" );
118 /* Get device name */
119 if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
121 msg_Err( p_aout, "no audio device given (maybe \"default\" ?)" );
127 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
130 /* Open the device */
131 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output )
132 && !strcmp( "default", psz_device ) )
134 /* ALSA doesn't understand "default" for S/PDIF. Cheat a little. */
135 char psz_iecdev[128];
137 if ( !strcmp( "default", psz_device ) )
139 snprintf( psz_iecdev, sizeof(psz_iecdev),
140 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
141 IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
142 IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
144 (p_aout->output.output.i_rate == 48000 ?
145 IEC958_AES3_CON_FS_48000 :
146 (p_aout->output.output.i_rate == 44100 ?
147 IEC958_AES3_CON_FS_44100 : IEC958_AES3_CON_FS_32000)) );
151 strncat( psz_iecdev, psz_device, sizeof(psz_iecdev) );
154 if ( (i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iecdev,
155 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0 )
158 msg_Warn( p_aout, "cannot open S/PDIF ALSA device `%s' (%s)",
159 psz_device, snd_strerror(i_snd_rc) );
160 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
164 i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
165 i_format = SND_PCM_FORMAT_S16;
167 p_aout->output.i_nb_samples = ALSA_SPDIF_PERIOD_SIZE;
168 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
169 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
170 p_aout->output.output.i_frame_length = A52_FRAME_NB;
172 aout_VolumeNoneInit( p_aout );
176 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
178 if ( (i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
179 SND_PCM_STREAM_PLAYBACK, SND_PCM_NONBLOCK)) < 0 )
181 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
182 psz_device, snd_strerror(i_snd_rc) );
188 if ( p_aout->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
190 p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
191 i_format = SND_PCM_FORMAT_FLOAT;
195 p_aout->output.output.i_format = AOUT_FMT_S16_NE;
196 i_format = SND_PCM_FORMAT_S16;
199 i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
200 p_aout->output.i_nb_samples = ALSA_DEFAULT_PERIOD_SIZE;
202 aout_VolumeSoftInit( p_aout );
206 p_aout->output.pf_play = Play;
208 snd_pcm_hw_params_alloca(&p_hw);
209 snd_pcm_sw_params_alloca(&p_sw);
211 if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
213 msg_Err( p_aout, "unable to retrieve initial hardware parameters" );
218 if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw, i_format ) < 0 )
220 msg_Err( p_aout, "unable to set stream sample size and word order" );
224 if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
228 if ( snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
229 SND_PCM_ACCESS_RW_INTERLEAVED ) < 0 )
231 msg_Err( p_aout, "unable to set interleaved stream format" );
236 i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
238 if ( (i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm,
239 p_hw, i_nb_channels )) < 0 )
241 msg_Err( p_aout, "unable to set number of output channels" );
244 if ( i_snd_rc != i_nb_channels )
248 case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
249 case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
250 case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
252 msg_Err( p_aout, "Unsupported downmixing (%d)", i_snd_rc );
258 if ( (i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm,
259 p_hw, p_aout->output.output.i_rate,
262 msg_Err( p_aout, "unable to set sample rate" );
265 p_aout->output.output.i_rate = i_snd_rc;
268 /* Set buffer size. */
269 i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm, p_hw,
273 msg_Err( p_aout, "unable to set buffer size" );
277 /* Set period size. */
278 i_snd_rc = snd_pcm_hw_params_set_period_size_near(
279 p_sys->p_snd_pcm, p_hw, p_aout->output.i_nb_samples, NULL );
282 msg_Err( p_aout, "unable to set period size" );
285 p_aout->output.i_nb_samples = i_snd_rc;
287 /* Write hardware configuration. */
288 if ( snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) < 0 )
290 msg_Err( p_aout, "unable to set hardware configuration" );
294 p_sys->i_period_time = snd_pcm_hw_params_get_period_time( p_hw, NULL );
296 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
297 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
299 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
300 p_aout->output.i_nb_samples );
302 /* Write software configuration. */
303 if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
305 msg_Err( p_aout, "unable to set software configuration" );
310 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
311 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
312 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
313 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
314 snd_output_printf( p_sys->p_snd_stderr, "\n" );
317 /* Create ALSA thread and wait for its readiness. */
318 if( vlc_thread_create( p_aout, "aout", ALSAThread,
319 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
321 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
328 snd_pcm_close( p_sys->p_snd_pcm );
330 snd_output_close( p_sys->p_snd_stderr );
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 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
359 msg_Err( p_aout, "failed closing ALSA device (%s)",
360 snd_strerror( i_snd_rc ) );
364 snd_output_close( p_sys->p_snd_stderr );
370 /*****************************************************************************
371 * ALSAThread: asynchronous thread used to DMA the data to the device
372 *****************************************************************************/
373 static int ALSAThread( aout_instance_t * p_aout )
375 struct aout_sys_t * p_sys = p_aout->output.p_sys;
377 while ( !p_aout->b_die )
381 /* Sleep during less than one period to avoid a lot of buffer
384 /* Why do we need to sleep ? --Meuuh */
385 msleep( p_sys->i_period_time >> 2 );
391 /*****************************************************************************
392 * ALSAFill: function used to fill the ALSA buffer as much as possible
393 *****************************************************************************/
394 static void ALSAFill( aout_instance_t * p_aout )
396 struct aout_sys_t * p_sys = p_aout->output.p_sys;
398 aout_buffer_t * p_buffer;
399 snd_pcm_status_t * p_status;
400 snd_timestamp_t ts_next;
402 snd_pcm_uframes_t i_avail;
404 snd_pcm_status_alloca( &p_status );
406 /* Wait for the device's readiness (ie. there is enough space in the
407 buffer to write at least one complete chunk) */
408 i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
411 msg_Err( p_aout, "ALSA device not ready !!! (%s)",
412 snd_strerror( i_snd_rc ) );
416 /* Fill in the buffer until space or audio output buffer shortage */
420 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
423 msg_Err( p_aout, "unable to get the device's status (%s)",
424 snd_strerror( i_snd_rc ) );
428 /* Handle buffer underruns and reget the status */
429 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
431 /* Prepare the device */
432 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
436 msg_Warn( p_aout, "recovered from buffer underrun" );
438 /* Reget the status */
439 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
443 "unable to get the device's status after recovery (%s)",
444 snd_strerror( i_snd_rc ) );
450 msg_Err( p_aout, "unable to recover from buffer underrun" );
455 /* Here the device should be either in the RUNNING state either in
456 the PREPARE state. p_status is valid. */
458 /* Try to write only if there is enough space */
459 i_avail = snd_pcm_status_get_avail( p_status );
461 if( i_avail >= p_aout->output.i_nb_samples )
464 snd_pcm_status_get_tstamp( p_status, &ts_next );
465 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
467 p_buffer = aout_OutputNextBuffer( p_aout, next_date,
468 (p_aout->output.output.i_format !=
469 VLC_FOURCC('s','p','d','i')) );
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 ) );
485 aout_BufferFree( p_buffer );