1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.21 2003/02/03 00:39: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 Note: we use the new API which is available since 0.9.0beta10a. */
42 #define ALSA_PCM_NEW_HW_PARAMS_API
43 #define ALSA_PCM_NEW_SW_PARAMS_API
44 #include <alsa/asoundlib.h>
46 /*****************************************************************************
47 * aout_sys_t: ALSA audio output method descriptor
48 *****************************************************************************
49 * This structure is part of the audio output thread descriptor.
50 * It describes the ALSA specific properties of an audio device.
51 *****************************************************************************/
54 snd_pcm_t * p_snd_pcm;
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 )
72 /* Why << 4 ? --Meuuh */
73 /* Why not ? --Bozo */
76 #define DEFAULT_ALSA_DEVICE "default"
78 /*****************************************************************************
80 *****************************************************************************/
81 static int Open ( vlc_object_t * );
82 static void Close ( vlc_object_t * );
83 static void Play ( aout_instance_t * );
84 static int ALSAThread ( aout_instance_t * );
85 static void ALSAFill ( aout_instance_t * );
87 /*****************************************************************************
89 *****************************************************************************/
90 #define SPDIF_TEXT N_("Try to use S/PDIF output")
91 #define SPDIF_LONGTEXT N_( \
92 "Sometimes we attempt to use the S/PDIF output, even if nothing is " \
93 "connected to it. Un-checking this option disables this behaviour, " \
94 "and permanently selects analog PCM output." )
97 add_category_hint( N_("ALSA"), NULL );
98 add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
99 N_("ALSA device name"), NULL );
100 add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT );
101 set_description( _("ALSA audio module") );
102 set_capability( "audio output", 50 );
103 set_callbacks( Open, Close );
106 /*****************************************************************************
107 * Probe: probe the audio device for available formats and channels
108 *****************************************************************************/
109 static void Probe( aout_instance_t * p_aout,
110 const char * psz_device, const char * psz_iec_device,
111 int i_snd_pcm_format )
113 struct aout_sys_t * p_sys = p_aout->output.p_sys;
116 var_Create ( p_aout, "audio-device", VLC_VAR_STRING | VLC_VAR_HASCHOICE );
118 /* Test for S/PDIF device if needed */
119 if ( psz_iec_device )
121 /* Opening the device should be enough */
122 if ( config_GetInt( p_aout, "spdif" )
123 && !snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
124 SND_PCM_STREAM_PLAYBACK, 0 ) )
126 val.psz_string = N_("A/52 over S/PDIF");
127 var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val );
128 snd_pcm_close( p_sys->p_snd_pcm );
132 /* Now test linear PCM capabilities */
133 if ( !snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
134 SND_PCM_STREAM_PLAYBACK, 0 ) )
137 snd_pcm_hw_params_t * p_hw;
138 snd_pcm_hw_params_alloca (&p_hw);
140 if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
142 msg_Warn( p_aout, "unable to retrieve initial hardware parameters"
143 ", disabling linear PCM audio" );
144 snd_pcm_close( p_sys->p_snd_pcm );
148 if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
149 i_snd_pcm_format ) < 0 )
151 /* Assume a FPU enabled computer can handle float32 format.
152 If somebody tells us it's not always true then we'll have
154 msg_Warn( p_aout, "unable to set stream sample size and word order"
155 ", disabling linear PCM audio" );
156 snd_pcm_close( p_sys->p_snd_pcm );
160 i_channels = aout_FormatNbChannels( &p_aout->output.output );
162 while ( i_channels > 1 )
164 /* Here we have to probe multi-channel capabilities but I have
165 no idea (at the moment) of how its managed by the ALSA
167 It seems that '6' channels aren't well handled on a stereo
168 sound card like my i810 but it requires some more
169 investigations. That's why '4' and '6' cases are disabled.
171 if ( !snd_pcm_hw_params_test_channels( p_sys->p_snd_pcm, p_hw,
174 switch ( i_channels )
177 val.psz_string = N_("Mono");
178 var_Change( p_aout, "audio-device",
179 VLC_VAR_ADDCHOICE, &val );
182 val.psz_string = N_("Stereo");
183 var_Change( p_aout, "audio-device",
184 VLC_VAR_ADDCHOICE, &val );
188 val.psz_string = N_("2 Front 2 Rear");
189 var_Change( p_aout, "audio-device",
190 VLC_VAR_ADDCHOICE, &val );
193 val.psz_string = N_("5.1");
194 var_Change( p_aout, "audio-device",
195 VLC_VAR_ADDCHOICE, &val );
204 /* Close the previously opened device */
205 snd_pcm_close( p_sys->p_snd_pcm );
208 /* Add final settings to the variable */
209 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
210 val.b_bool = VLC_TRUE;
211 var_Set( p_aout, "intf-change", val );
214 /*****************************************************************************
215 * Open: create a handle and open an alsa device
216 *****************************************************************************
217 * This function opens an alsa device, through the alsa API.
219 * Note: the only heap-allocated string is psz_device. All the other pointers
220 * are references to psz_device or to stack-allocated data.
221 *****************************************************************************/
222 static int Open( vlc_object_t *p_this )
224 aout_instance_t * p_aout = (aout_instance_t *)p_this;
225 struct aout_sys_t * p_sys;
228 char psz_default_iec_device[128]; /* Buffer used to store the default
230 char * psz_device, * psz_iec_device; /* device names for PCM and S/PDIF
233 int i_vlc_pcm_format; /* Audio format for VLC's data */
234 int i_snd_pcm_format; /* Audio format for ALSA's data */
236 snd_pcm_uframes_t i_buffer_size = 0;
237 snd_pcm_uframes_t i_period_size = 0;
240 snd_pcm_hw_params_t *p_hw;
241 snd_pcm_sw_params_t *p_sw;
245 /* Allocate structures */
246 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
249 msg_Err( p_aout, "out of memory" );
253 /* Get device name */
254 if( (psz_device = config_GetPsz( p_aout, "alsadev" )) == NULL )
256 msg_Err( p_aout, "no audio device given (maybe \"default\" ?)" );
261 /* Choose the IEC device for S/PDIF output:
262 if the device is overriden by the user then it will be the one
263 otherwise we compute the default device based on the output format. */
264 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output )
265 && !strcmp( psz_device, DEFAULT_ALSA_DEVICE ) )
267 snprintf( psz_default_iec_device, sizeof(psz_default_iec_device),
268 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
269 IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
270 IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
272 ( p_aout->output.output.i_rate == 48000 ?
273 IEC958_AES3_CON_FS_48000 :
274 ( p_aout->output.output.i_rate == 44100 ?
275 IEC958_AES3_CON_FS_44100 : IEC958_AES3_CON_FS_32000 ) ) );
276 psz_iec_device = psz_default_iec_device;
278 else if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
280 psz_iec_device = psz_device;
284 psz_iec_device = NULL;
287 /* Choose the linear PCM format (read the comment above about FPU
289 if( p_aout->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
291 i_vlc_pcm_format = VLC_FOURCC('f','l','3','2');
292 i_snd_pcm_format = SND_PCM_FORMAT_FLOAT;
296 i_vlc_pcm_format = AOUT_FMT_S16_NE;
297 i_snd_pcm_format = SND_PCM_FORMAT_S16;
300 /* If the variable doesn't exist then it's the first time we're called
301 and we have to probe the available audio formats and channels */
302 if ( var_Type( p_aout, "audio-device" ) == 0 )
304 Probe( p_aout, psz_device, psz_iec_device, i_snd_pcm_format );
307 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
314 if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
316 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
318 else if ( !strcmp( val.psz_string, N_("5.1") ) )
320 p_aout->output.output.i_format = i_vlc_pcm_format;
321 p_aout->output.output.i_physical_channels
322 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
323 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
326 else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
328 p_aout->output.output.i_format = i_vlc_pcm_format;
329 p_aout->output.output.i_physical_channels
330 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
331 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
333 else if ( !strcmp( val.psz_string, N_("Stereo") ) )
335 p_aout->output.output.i_format = i_vlc_pcm_format;
336 p_aout->output.output.i_physical_channels
337 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
339 else if ( !strcmp( val.psz_string, N_("Mono") ) )
341 p_aout->output.output.i_format = i_vlc_pcm_format;
342 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
345 free( val.psz_string );
348 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
351 /* Open the device */
352 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
354 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
355 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
357 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
358 psz_iec_device, snd_strerror( i_snd_rc ) );
363 i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
364 i_snd_pcm_format = SND_PCM_FORMAT_S16;
367 p_aout->output.i_nb_samples = i_period_size = ALSA_SPDIF_PERIOD_SIZE;
368 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
369 p_aout->output.output.i_frame_length = A52_FRAME_NB;
371 aout_VolumeNoneInit( p_aout );
375 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
376 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
378 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
379 psz_device, snd_strerror( i_snd_rc ) );
384 i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
385 i_channels = aout_FormatNbChannels( &p_aout->output.output );
387 p_aout->output.i_nb_samples = i_period_size = ALSA_DEFAULT_PERIOD_SIZE;
389 aout_VolumeSoftInit( p_aout );
392 /* Free psz_device so that all the remaining data is stack-allocated */
395 p_aout->output.pf_play = Play;
397 snd_pcm_hw_params_alloca(&p_hw);
398 snd_pcm_sw_params_alloca(&p_sw);
400 /* Get Initial hardware parameters */
401 if ( ( i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) ) < 0 )
403 msg_Err( p_aout, "unable to retrieve initial hardware parameters (%s)",
404 snd_strerror( i_snd_rc ) );
409 if ( ( i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
410 i_snd_pcm_format ) ) < 0 )
412 msg_Err( p_aout, "unable to set stream sample size and word order (%s)",
413 snd_strerror( i_snd_rc ) );
417 if ( ( i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
418 SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
420 msg_Err( p_aout, "unable to set interleaved stream format (%s)",
421 snd_strerror( i_snd_rc ) );
426 if ( ( i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
429 msg_Err( p_aout, "unable to set number of output channels (%s)",
430 snd_strerror( i_snd_rc ) );
435 #ifdef HAVE_ALSA_NEW_API
436 if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
437 &p_aout->output.output.i_rate, NULL ) ) < 0 )
439 if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
440 p_aout->output.output.i_rate, NULL ) ) < 0 )
443 msg_Err( p_aout, "unable to set sample rate (%s)",
444 snd_strerror( i_snd_rc ) );
448 /* Set buffer size. */
449 #ifdef HAVE_ALSA_NEW_API
450 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
451 p_hw, &i_buffer_size ) ) < 0 )
453 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
454 p_hw, i_buffer_size ) ) < 0 )
457 msg_Err( p_aout, "unable to set buffer size (%s)",
458 snd_strerror( i_snd_rc ) );
462 /* Set period size. */
463 #ifdef HAVE_ALSA_NEW_API
464 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
465 p_hw, &i_period_size, NULL ) ) < 0 )
467 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
468 p_hw, i_period_size, NULL ) ) < 0 )
471 msg_Err( p_aout, "unable to set period size (%s)",
472 snd_strerror( i_snd_rc ) );
475 p_aout->output.i_nb_samples = i_period_size;
477 /* Commit hardware parameters. */
478 if ( ( i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) ) < 0 )
480 msg_Err( p_aout, "unable to commit hardware configuration (%s)",
481 snd_strerror( i_snd_rc ) );
485 #ifdef HAVE_ALSA_NEW_API
486 if( ( i_snd_rc = snd_pcm_hw_params_get_period_time( p_hw,
487 &p_sys->i_period_time, NULL ) ) < 0 )
489 if( ( p_sys->i_period_time =
490 snd_pcm_hw_params_get_period_time( p_hw, NULL ) ) < 0 )
493 msg_Err( p_aout, "unable to get period time (%s)",
494 snd_strerror( i_snd_rc ) );
498 /* Get Initial software parameters */
499 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
501 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
503 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
504 p_aout->output.i_nb_samples );
506 /* Commit software parameters. */
507 if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
509 msg_Err( p_aout, "unable to set software configuration" );
514 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
515 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
516 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
517 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
518 snd_output_printf( p_sys->p_snd_stderr, "\n" );
521 /* Create ALSA thread and wait for its readiness. */
522 if( vlc_thread_create( p_aout, "aout", ALSAThread,
523 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
525 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
532 snd_pcm_close( p_sys->p_snd_pcm );
534 snd_output_close( p_sys->p_snd_stderr );
540 /*****************************************************************************
541 * Play: nothing to do
542 *****************************************************************************/
543 static void Play( aout_instance_t *p_aout )
547 /*****************************************************************************
548 * Close: close the ALSA device
549 *****************************************************************************/
550 static void Close( vlc_object_t *p_this )
552 aout_instance_t *p_aout = (aout_instance_t *)p_this;
553 struct aout_sys_t * p_sys = p_aout->output.p_sys;
556 p_aout->b_die = VLC_TRUE;
557 vlc_thread_join( p_aout );
558 p_aout->b_die = VLC_FALSE;
560 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
564 msg_Err( p_aout, "failed closing ALSA device (%s)",
565 snd_strerror( i_snd_rc ) );
569 snd_output_close( p_sys->p_snd_stderr );
575 /*****************************************************************************
576 * ALSAThread: asynchronous thread used to DMA the data to the device
577 *****************************************************************************/
578 static int ALSAThread( aout_instance_t * p_aout )
580 struct aout_sys_t * p_sys = p_aout->output.p_sys;
582 while ( !p_aout->b_die )
586 /* Sleep during less than one period to avoid a lot of buffer
589 /* Why do we need to sleep ? --Meuuh */
590 /* Maybe because I don't want to eat all the cpu by looping
591 all the time. --Bozo */
592 /* Shouldn't snd_pcm_wait() make us wait ? --Meuuh */
593 msleep( p_sys->i_period_time >> 1 );
599 /*****************************************************************************
600 * ALSAFill: function used to fill the ALSA buffer as much as possible
601 *****************************************************************************/
602 static void ALSAFill( aout_instance_t * p_aout )
604 struct aout_sys_t * p_sys = p_aout->output.p_sys;
606 aout_buffer_t * p_buffer;
607 snd_pcm_status_t * p_status;
608 snd_timestamp_t ts_next;
612 snd_pcm_status_alloca( &p_status );
614 /* Wait for the device's readiness (ie. there is enough space in the
615 buffer to write at least one complete chunk) */
616 i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
619 msg_Err( p_aout, "ALSA device not ready !!! (%s)",
620 snd_strerror( i_snd_rc ) );
624 /* Fill in the buffer until space or audio output buffer shortage */
628 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
631 msg_Err( p_aout, "unable to get the device's status (%s)",
632 snd_strerror( i_snd_rc ) );
636 /* Handle buffer underruns and reget the status */
637 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
639 /* Prepare the device */
640 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
644 msg_Warn( p_aout, "recovered from buffer underrun" );
646 /* Reget the status */
647 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
651 "unable to get the device's status after recovery (%s)",
652 snd_strerror( i_snd_rc ) );
658 msg_Err( p_aout, "unable to recover from buffer underrun" );
663 /* Here the device should be either in the RUNNING state either in
664 the PREPARE state. p_status is valid. */
666 snd_pcm_status_get_tstamp( p_status, &ts_next );
667 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
668 next_date += (mtime_t)snd_pcm_status_get_delay(p_status)
669 * 1000000 / p_aout->output.output.i_rate;
671 p_buffer = aout_OutputNextBuffer( p_aout, next_date,
672 (p_aout->output.output.i_format ==
673 VLC_FOURCC('s','p','d','i')) );
675 /* Audio output buffer shortage -> stop the fill process and
676 wait in ALSAThread */
677 if( p_buffer == NULL )
680 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
681 p_buffer->i_nb_samples );
685 msg_Err( p_aout, "write failed (%s)",
686 snd_strerror( i_snd_rc ) );
689 aout_BufferFree( p_buffer );
691 msleep( p_sys->i_period_time >> 2 );