1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.23 2003/02/20 01:52:45 sigmunau 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, VLC_FALSE );
98 add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
99 N_("ALSA device name"), NULL, VLC_FALSE );
100 add_bool( "spdif", 1, NULL, SPDIF_TEXT, SPDIF_LONGTEXT, VLC_FALSE );
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 > 0 )
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;
347 /* This should not happen ! */
348 msg_Err( p_aout, "internal: can't find audio-device (%s)",
351 free( val.psz_string );
354 free( val.psz_string );
357 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
360 /* Open the device */
361 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
363 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
364 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
366 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
367 psz_iec_device, snd_strerror( i_snd_rc ) );
372 i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
373 i_snd_pcm_format = SND_PCM_FORMAT_S16;
376 p_aout->output.i_nb_samples = i_period_size = ALSA_SPDIF_PERIOD_SIZE;
377 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
378 p_aout->output.output.i_frame_length = A52_FRAME_NB;
380 aout_VolumeNoneInit( p_aout );
384 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
385 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
387 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
388 psz_device, snd_strerror( i_snd_rc ) );
393 i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
394 i_channels = aout_FormatNbChannels( &p_aout->output.output );
396 p_aout->output.i_nb_samples = i_period_size = ALSA_DEFAULT_PERIOD_SIZE;
398 aout_VolumeSoftInit( p_aout );
401 /* Free psz_device so that all the remaining data is stack-allocated */
404 p_aout->output.pf_play = Play;
406 snd_pcm_hw_params_alloca(&p_hw);
407 snd_pcm_sw_params_alloca(&p_sw);
409 /* Get Initial hardware parameters */
410 if ( ( i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) ) < 0 )
412 msg_Err( p_aout, "unable to retrieve initial hardware parameters (%s)",
413 snd_strerror( i_snd_rc ) );
418 if ( ( i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
419 i_snd_pcm_format ) ) < 0 )
421 msg_Err( p_aout, "unable to set stream sample size and word order (%s)",
422 snd_strerror( i_snd_rc ) );
426 if ( ( i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
427 SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
429 msg_Err( p_aout, "unable to set interleaved stream format (%s)",
430 snd_strerror( i_snd_rc ) );
435 if ( ( i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
438 msg_Err( p_aout, "unable to set number of output channels (%s)",
439 snd_strerror( i_snd_rc ) );
444 #ifdef HAVE_ALSA_NEW_API
445 if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
446 &p_aout->output.output.i_rate, NULL ) ) < 0 )
448 if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
449 p_aout->output.output.i_rate, NULL ) ) < 0 )
452 msg_Err( p_aout, "unable to set sample rate (%s)",
453 snd_strerror( i_snd_rc ) );
457 /* Set buffer size. */
458 #ifdef HAVE_ALSA_NEW_API
459 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
460 p_hw, &i_buffer_size ) ) < 0 )
462 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
463 p_hw, i_buffer_size ) ) < 0 )
466 msg_Err( p_aout, "unable to set buffer size (%s)",
467 snd_strerror( i_snd_rc ) );
471 /* Set period size. */
472 #ifdef HAVE_ALSA_NEW_API
473 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
474 p_hw, &i_period_size, NULL ) ) < 0 )
476 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
477 p_hw, i_period_size, NULL ) ) < 0 )
480 msg_Err( p_aout, "unable to set period size (%s)",
481 snd_strerror( i_snd_rc ) );
484 p_aout->output.i_nb_samples = i_period_size;
486 /* Commit hardware parameters. */
487 if ( ( i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) ) < 0 )
489 msg_Err( p_aout, "unable to commit hardware configuration (%s)",
490 snd_strerror( i_snd_rc ) );
494 #ifdef HAVE_ALSA_NEW_API
495 if( ( i_snd_rc = snd_pcm_hw_params_get_period_time( p_hw,
496 &p_sys->i_period_time, NULL ) ) < 0 )
498 if( ( p_sys->i_period_time =
499 snd_pcm_hw_params_get_period_time( p_hw, NULL ) ) < 0 )
502 msg_Err( p_aout, "unable to get period time (%s)",
503 snd_strerror( i_snd_rc ) );
507 /* Get Initial software parameters */
508 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
510 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
512 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
513 p_aout->output.i_nb_samples );
515 /* Commit software parameters. */
516 if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
518 msg_Err( p_aout, "unable to set software configuration" );
523 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
524 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
525 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
526 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
527 snd_output_printf( p_sys->p_snd_stderr, "\n" );
530 /* Create ALSA thread and wait for its readiness. */
531 if( vlc_thread_create( p_aout, "aout", ALSAThread,
532 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
534 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
541 snd_pcm_close( p_sys->p_snd_pcm );
543 snd_output_close( p_sys->p_snd_stderr );
549 /*****************************************************************************
550 * Play: nothing to do
551 *****************************************************************************/
552 static void Play( aout_instance_t *p_aout )
556 /*****************************************************************************
557 * Close: close the ALSA device
558 *****************************************************************************/
559 static void Close( vlc_object_t *p_this )
561 aout_instance_t *p_aout = (aout_instance_t *)p_this;
562 struct aout_sys_t * p_sys = p_aout->output.p_sys;
565 p_aout->b_die = VLC_TRUE;
566 vlc_thread_join( p_aout );
567 p_aout->b_die = VLC_FALSE;
569 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
573 msg_Err( p_aout, "failed closing ALSA device (%s)",
574 snd_strerror( i_snd_rc ) );
578 snd_output_close( p_sys->p_snd_stderr );
584 /*****************************************************************************
585 * ALSAThread: asynchronous thread used to DMA the data to the device
586 *****************************************************************************/
587 static int ALSAThread( aout_instance_t * p_aout )
589 struct aout_sys_t * p_sys = p_aout->output.p_sys;
591 while ( !p_aout->b_die )
595 /* Sleep during less than one period to avoid a lot of buffer
598 /* Why do we need to sleep ? --Meuuh */
599 /* Maybe because I don't want to eat all the cpu by looping
600 all the time. --Bozo */
601 /* Shouldn't snd_pcm_wait() make us wait ? --Meuuh */
602 msleep( p_sys->i_period_time >> 1 );
608 /*****************************************************************************
609 * ALSAFill: function used to fill the ALSA buffer as much as possible
610 *****************************************************************************/
611 static void ALSAFill( aout_instance_t * p_aout )
613 struct aout_sys_t * p_sys = p_aout->output.p_sys;
615 aout_buffer_t * p_buffer;
616 snd_pcm_status_t * p_status;
617 snd_timestamp_t ts_next;
621 snd_pcm_status_alloca( &p_status );
623 /* Wait for the device's readiness (ie. there is enough space in the
624 buffer to write at least one complete chunk) */
625 i_snd_rc = snd_pcm_wait( p_sys->p_snd_pcm, THREAD_SLEEP );
628 msg_Err( p_aout, "ALSA device not ready !!! (%s)",
629 snd_strerror( i_snd_rc ) );
633 /* Fill in the buffer until space or audio output buffer shortage */
637 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
640 msg_Err( p_aout, "unable to get the device's status (%s)",
641 snd_strerror( i_snd_rc ) );
645 /* Handle buffer underruns and reget the status */
646 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
648 /* Prepare the device */
649 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
653 msg_Warn( p_aout, "recovered from buffer underrun" );
655 /* Reget the status */
656 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
660 "unable to get the device's status after recovery (%s)",
661 snd_strerror( i_snd_rc ) );
667 msg_Err( p_aout, "unable to recover from buffer underrun" );
672 /* Here the device should be either in the RUNNING state either in
673 the PREPARE state. p_status is valid. */
675 snd_pcm_status_get_tstamp( p_status, &ts_next );
676 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
677 next_date += (mtime_t)snd_pcm_status_get_delay(p_status)
678 * 1000000 / p_aout->output.output.i_rate;
680 p_buffer = aout_OutputNextBuffer( p_aout, next_date,
681 (p_aout->output.output.i_format ==
682 VLC_FOURCC('s','p','d','i')) );
684 /* Audio output buffer shortage -> stop the fill process and
685 wait in ALSAThread */
686 if( p_buffer == NULL )
689 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
690 p_buffer->i_nb_samples );
694 msg_Err( p_aout, "write failed (%s)",
695 snd_strerror( i_snd_rc ) );
698 aout_BufferFree( p_buffer );
700 msleep( p_sys->i_period_time >> 2 );