1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
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;
61 int b_playing; /* playing status */
67 snd_pcm_status_t *p_status;
70 #define A52_FRAME_NB 1536
72 /* These values are in frames.
73 To convert them to a number of bytes you have to multiply them by the
74 number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
76 #define ALSA_DEFAULT_PERIOD_SIZE 1024
77 #define ALSA_DEFAULT_BUFFER_SIZE ( ALSA_DEFAULT_PERIOD_SIZE << 8 )
78 #define ALSA_SPDIF_PERIOD_SIZE A52_FRAME_NB
79 #define ALSA_SPDIF_BUFFER_SIZE ( ALSA_SPDIF_PERIOD_SIZE << 4 )
80 /* Why << 4 ? --Meuuh */
81 /* Why not ? --Bozo */
84 #define DEFAULT_ALSA_DEVICE N_("default")
86 /*****************************************************************************
88 *****************************************************************************/
89 static int Open ( vlc_object_t * );
90 static void Close ( vlc_object_t * );
91 static void Play ( aout_instance_t * );
92 static int ALSAThread ( aout_instance_t * );
93 static void ALSAFill ( aout_instance_t * );
95 /*****************************************************************************
97 *****************************************************************************/
99 set_description( _("ALSA audio output") );
100 add_string( "alsadev", DEFAULT_ALSA_DEVICE, aout_FindAndRestart,
101 N_("ALSA Device Name"), NULL, VLC_FALSE );
102 set_capability( "audio output", 150 );
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 *pi_snd_pcm_format )
113 struct aout_sys_t * p_sys = p_aout->output.p_sys;
114 vlc_value_t val, text;
117 var_Create ( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
118 text.psz_string = _("Audio Device");
119 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
121 /* We'll open the audio device in non blocking mode so we can just exit
122 * when it is already in use, but for the real stuff we'll still use
123 * the blocking mode */
125 /* Now test linear PCM capabilities */
126 if ( !(i_ret = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
127 SND_PCM_STREAM_PLAYBACK,
128 SND_PCM_NONBLOCK ) ) )
131 snd_pcm_hw_params_t * p_hw;
132 snd_pcm_hw_params_alloca (&p_hw);
134 if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
136 msg_Warn( p_aout, "unable to retrieve initial hardware parameters"
137 ", disabling linear PCM audio" );
138 snd_pcm_close( p_sys->p_snd_pcm );
139 var_Destroy( p_aout, "audio-device" );
143 if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
144 *pi_snd_pcm_format ) < 0 )
146 if( *pi_snd_pcm_format != SND_PCM_FORMAT_S16 )
148 *pi_snd_pcm_format = SND_PCM_FORMAT_S16;
149 if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
150 *pi_snd_pcm_format ) < 0 )
152 msg_Warn( p_aout, "unable to set stream sample size and "
153 "word order, disabling linear PCM audio" );
154 snd_pcm_close( p_sys->p_snd_pcm );
155 var_Destroy( p_aout, "audio-device" );
161 i_channels = aout_FormatNbChannels( &p_aout->output.output );
163 while ( i_channels > 0 )
165 if ( !snd_pcm_hw_params_test_channels( p_sys->p_snd_pcm, p_hw,
168 switch ( i_channels )
171 val.i_int = AOUT_VAR_MONO;
172 text.psz_string = N_("Mono");
173 var_Change( p_aout, "audio-device",
174 VLC_VAR_ADDCHOICE, &val, &text );
177 val.i_int = AOUT_VAR_STEREO;
178 text.psz_string = N_("Stereo");
179 var_Change( p_aout, "audio-device",
180 VLC_VAR_ADDCHOICE, &val, &text );
181 var_Set( p_aout, "audio-device", val );
184 val.i_int = AOUT_VAR_2F2R;
185 text.psz_string = N_("2 Front 2 Rear");
186 var_Change( p_aout, "audio-device",
187 VLC_VAR_ADDCHOICE, &val, &text );
190 val.i_int = AOUT_VAR_5_1;
191 text.psz_string = N_("5.1");
192 var_Change( p_aout, "audio-device",
193 VLC_VAR_ADDCHOICE, &val, &text );
201 /* Special case for mono on stereo only boards */
202 i_channels = aout_FormatNbChannels( &p_aout->output.output );
203 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
204 if( val.i_int <= 0 && i_channels == 1 )
206 if ( !snd_pcm_hw_params_test_channels( p_sys->p_snd_pcm, p_hw, 2 ))
208 val.i_int = AOUT_VAR_STEREO;
209 text.psz_string = N_("Stereo");
210 var_Change( p_aout, "audio-device",
211 VLC_VAR_ADDCHOICE, &val, &text );
212 var_Set( p_aout, "audio-device", val );
216 /* Close the previously opened device */
217 snd_pcm_close( p_sys->p_snd_pcm );
219 else if ( i_ret == -EBUSY )
221 msg_Warn( p_aout, "audio device: %s is already in use", psz_device );
224 /* Test for S/PDIF device if needed */
225 if ( psz_iec_device )
227 /* Opening the device should be enough */
228 if ( !(i_ret = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
229 SND_PCM_STREAM_PLAYBACK,
230 SND_PCM_NONBLOCK ) ) )
232 val.i_int = AOUT_VAR_SPDIF;
233 text.psz_string = N_("A/52 over S/PDIF");
234 var_Change( p_aout, "audio-device",
235 VLC_VAR_ADDCHOICE, &val, &text );
236 if( config_GetInt( p_aout, "spdif" ) )
237 var_Set( p_aout, "audio-device", val );
239 snd_pcm_close( p_sys->p_snd_pcm );
241 else if ( i_ret == -EBUSY )
243 msg_Warn( p_aout, "audio device: %s is already in use",
248 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
251 /* Probe() has failed. */
252 msg_Dbg( p_aout, "failed to find a useable alsa configuration" );
253 var_Destroy( p_aout, "audio-device" );
257 /* Add final settings to the variable */
258 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
259 val.b_bool = VLC_TRUE;
260 var_Set( p_aout, "intf-change", val );
263 /*****************************************************************************
264 * Open: create a handle and open an alsa device
265 *****************************************************************************
266 * This function opens an alsa device, through the alsa API.
268 * Note: the only heap-allocated string is psz_device. All the other pointers
269 * are references to psz_device or to stack-allocated data.
270 *****************************************************************************/
271 static int Open( vlc_object_t *p_this )
273 aout_instance_t * p_aout = (aout_instance_t *)p_this;
274 struct aout_sys_t * p_sys;
277 char psz_default_iec_device[128]; /* Buffer used to store the default
279 char * psz_device, * psz_iec_device; /* device names for PCM and S/PDIF
282 int i_vlc_pcm_format; /* Audio format for VLC's data */
283 int i_snd_pcm_format; /* Audio format for ALSA's data */
285 snd_pcm_uframes_t i_buffer_size = 0;
286 snd_pcm_uframes_t i_period_size = 0;
289 snd_pcm_hw_params_t *p_hw;
290 snd_pcm_sw_params_t *p_sw;
295 /* Allocate structures */
296 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
299 msg_Err( p_aout, "out of memory" );
302 p_sys->b_playing = VLC_FALSE;
303 p_sys->start_date = 0;
304 p_sys->p_status = (snd_pcm_status_t *)malloc(snd_pcm_status_sizeof());
305 vlc_cond_init( p_aout, &p_sys->wait );
306 vlc_mutex_init( p_aout, &p_sys->lock );
308 /* Get device name */
309 if( (psz_device = config_GetPsz( p_aout, "alsadev" )) == NULL )
311 msg_Err( p_aout, "no audio device given (maybe \"default\" ?)" );
316 /* Choose the IEC device for S/PDIF output:
317 if the device is overriden by the user then it will be the one
318 otherwise we compute the default device based on the output format. */
319 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output )
320 && !strcmp( psz_device, DEFAULT_ALSA_DEVICE ) )
322 snprintf( psz_default_iec_device, sizeof(psz_default_iec_device),
323 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
324 IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
325 IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
327 ( p_aout->output.output.i_rate == 48000 ?
328 IEC958_AES3_CON_FS_48000 :
329 ( p_aout->output.output.i_rate == 44100 ?
330 IEC958_AES3_CON_FS_44100 : IEC958_AES3_CON_FS_32000 ) ) );
331 psz_iec_device = psz_default_iec_device;
333 else if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
335 psz_iec_device = psz_device;
339 psz_iec_device = NULL;
342 /* Choose the linear PCM format (read the comment above about FPU
344 if( p_aout->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
346 i_vlc_pcm_format = VLC_FOURCC('f','l','3','2');
347 i_snd_pcm_format = SND_PCM_FORMAT_FLOAT;
351 i_vlc_pcm_format = AOUT_FMT_S16_NE;
352 i_snd_pcm_format = SND_PCM_FORMAT_S16;
355 /* If the variable doesn't exist then it's the first time we're called
356 and we have to probe the available audio formats and channels */
357 if ( var_Type( p_aout, "audio-device" ) == 0 )
359 Probe( p_aout, psz_device, psz_iec_device, &i_snd_pcm_format );
360 switch( i_snd_pcm_format )
362 case SND_PCM_FORMAT_FLOAT:
363 i_vlc_pcm_format = VLC_FOURCC('f','l','3','2');
365 case SND_PCM_FORMAT_S16:
366 i_vlc_pcm_format = AOUT_FMT_S16_NE;
371 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
378 if ( val.i_int == AOUT_VAR_SPDIF )
380 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
382 else if ( val.i_int == AOUT_VAR_5_1 )
384 p_aout->output.output.i_format = i_vlc_pcm_format;
385 p_aout->output.output.i_physical_channels
386 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
387 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
390 psz_device = strdup( "surround51" );
392 else if ( val.i_int == AOUT_VAR_2F2R )
394 p_aout->output.output.i_format = i_vlc_pcm_format;
395 p_aout->output.output.i_physical_channels
396 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
397 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
399 psz_device = strdup( "surround40" );
401 else if ( val.i_int == AOUT_VAR_STEREO )
403 p_aout->output.output.i_format = i_vlc_pcm_format;
404 p_aout->output.output.i_physical_channels
405 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
407 else if ( val.i_int == AOUT_VAR_MONO )
409 p_aout->output.output.i_format = i_vlc_pcm_format;
410 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
415 /* This should not happen ! */
416 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
422 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
425 /* Open the device */
426 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
428 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
429 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
431 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
432 psz_iec_device, snd_strerror( i_snd_rc ) );
437 i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
438 i_snd_pcm_format = SND_PCM_FORMAT_S16;
441 p_aout->output.i_nb_samples = i_period_size = ALSA_SPDIF_PERIOD_SIZE;
442 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
443 p_aout->output.output.i_frame_length = A52_FRAME_NB;
445 aout_VolumeNoneInit( p_aout );
449 msg_Dbg( p_aout, "opening ALSA device `%s'", psz_device );
451 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
452 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
454 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
455 psz_device, snd_strerror( i_snd_rc ) );
460 i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
461 i_channels = aout_FormatNbChannels( &p_aout->output.output );
463 p_aout->output.i_nb_samples = i_period_size = ALSA_DEFAULT_PERIOD_SIZE;
465 aout_VolumeSoftInit( p_aout );
468 /* Free psz_device so that all the remaining data is stack-allocated */
471 p_aout->output.pf_play = Play;
473 snd_pcm_hw_params_alloca(&p_hw);
474 snd_pcm_sw_params_alloca(&p_sw);
476 /* Get Initial hardware parameters */
477 if ( ( i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) ) < 0 )
479 msg_Err( p_aout, "unable to retrieve initial hardware parameters (%s)",
480 snd_strerror( i_snd_rc ) );
485 if ( ( i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
486 i_snd_pcm_format ) ) < 0 )
488 msg_Err( p_aout, "unable to set stream sample size and word order (%s)",
489 snd_strerror( i_snd_rc ) );
493 if ( ( i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
494 SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
496 msg_Err( p_aout, "unable to set interleaved stream format (%s)",
497 snd_strerror( i_snd_rc ) );
502 if ( ( i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
505 msg_Err( p_aout, "unable to set number of output channels (%s)",
506 snd_strerror( i_snd_rc ) );
511 i_old_rate = p_aout->output.output.i_rate;
512 #ifdef HAVE_ALSA_NEW_API
513 i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
514 &p_aout->output.output.i_rate,
517 i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
518 p_aout->output.output.i_rate,
521 if( i_snd_rc < 0 || p_aout->output.output.i_rate != i_old_rate )
523 msg_Warn( p_aout, "The rate %d Hz is not supported by your hardware. "
524 "Using %d Hz instead.\n", i_old_rate,
525 p_aout->output.output.i_rate );
528 /* Set buffer size. */
529 #ifdef HAVE_ALSA_NEW_API
530 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
531 p_hw, &i_buffer_size ) ) < 0 )
533 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
534 p_hw, i_buffer_size ) ) < 0 )
537 msg_Err( p_aout, "unable to set buffer size (%s)",
538 snd_strerror( i_snd_rc ) );
542 /* Set period size. */
543 #ifdef HAVE_ALSA_NEW_API
544 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
545 p_hw, &i_period_size, NULL ) ) < 0 )
547 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
548 p_hw, i_period_size, NULL ) ) < 0 )
551 msg_Err( p_aout, "unable to set period size (%s)",
552 snd_strerror( i_snd_rc ) );
555 p_aout->output.i_nb_samples = i_period_size;
557 /* Commit hardware parameters. */
558 if ( ( i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) ) < 0 )
560 msg_Err( p_aout, "unable to commit hardware configuration (%s)",
561 snd_strerror( i_snd_rc ) );
565 #ifdef HAVE_ALSA_NEW_API
566 if( ( i_snd_rc = snd_pcm_hw_params_get_period_time( p_hw,
567 &p_sys->i_period_time, NULL ) ) < 0 )
569 if( ( p_sys->i_period_time =
570 snd_pcm_hw_params_get_period_time( p_hw, NULL ) ) < 0 )
573 msg_Err( p_aout, "unable to get period time (%s)",
574 snd_strerror( i_snd_rc ) );
578 /* Get Initial software parameters */
579 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
581 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
583 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
584 p_aout->output.i_nb_samples );
586 /* Commit software parameters. */
587 if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
589 msg_Err( p_aout, "unable to set software configuration" );
594 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
595 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
596 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
597 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
598 snd_output_printf( p_sys->p_snd_stderr, "\n" );
601 /* Create ALSA thread and wait for its readiness. */
602 if( vlc_thread_create( p_aout, "aout", ALSAThread,
603 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
605 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
612 snd_pcm_close( p_sys->p_snd_pcm );
614 snd_output_close( p_sys->p_snd_stderr );
620 /*****************************************************************************
621 * Play: nothing to do
622 *****************************************************************************/
623 static void Play( aout_instance_t *p_aout )
625 if( !p_aout->output.p_sys->b_playing )
627 p_aout->output.p_sys->b_playing = 1;
629 /* get the playing date of the first aout buffer */
630 p_aout->output.p_sys->start_date =
631 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
633 /* wake up the audio output thread */
634 vlc_mutex_lock( &p_aout->output.p_sys->lock );
635 vlc_cond_signal( &p_aout->output.p_sys->wait );
636 vlc_mutex_unlock( &p_aout->output.p_sys->lock );
640 /*****************************************************************************
641 * Close: close the ALSA device
642 *****************************************************************************/
643 static void Close( vlc_object_t *p_this )
645 aout_instance_t *p_aout = (aout_instance_t *)p_this;
646 struct aout_sys_t * p_sys = p_aout->output.p_sys;
649 /* make sure the audio output thread is waken up */
650 vlc_mutex_lock( &p_aout->output.p_sys->lock );
651 vlc_cond_signal( &p_aout->output.p_sys->wait );
652 vlc_mutex_unlock( &p_aout->output.p_sys->lock );
654 p_aout->b_die = VLC_TRUE;
655 vlc_thread_join( p_aout );
656 p_aout->b_die = VLC_FALSE;
658 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
662 msg_Err( p_aout, "failed closing ALSA device (%s)",
663 snd_strerror( i_snd_rc ) );
667 snd_output_close( p_sys->p_snd_stderr );
670 free( p_sys->p_status );
674 /*****************************************************************************
675 * ALSAThread: asynchronous thread used to DMA the data to the device
676 *****************************************************************************/
677 static int ALSAThread( aout_instance_t * p_aout )
679 /* Wait for the exact time to start playing (avoids resampling) */
680 vlc_mutex_lock( &p_aout->output.p_sys->lock );
681 if( !p_aout->output.p_sys->start_date )
682 vlc_cond_wait( &p_aout->output.p_sys->wait,
683 &p_aout->output.p_sys->lock );
684 vlc_mutex_unlock( &p_aout->output.p_sys->lock );
686 mwait( p_aout->output.p_sys->start_date - AOUT_PTS_TOLERANCE / 4 );
688 while ( !p_aout->b_die )
696 /*****************************************************************************
697 * ALSAFill: function used to fill the ALSA buffer as much as possible
698 *****************************************************************************/
699 static void ALSAFill( aout_instance_t * p_aout )
701 struct aout_sys_t * p_sys = p_aout->output.p_sys;
703 aout_buffer_t * p_buffer;
704 snd_pcm_status_t * p_status = p_sys->p_status;
705 snd_timestamp_t ts_next;
709 /* Fill in the buffer until space or audio output buffer shortage */
712 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
715 msg_Err( p_aout, "unable to get the device's status (%s)",
716 snd_strerror( i_snd_rc ) );
718 msleep( p_sys->i_period_time >> 1 );
722 /* Handle buffer underruns and reget the status */
723 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
725 /* Prepare the device */
726 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
730 msg_Warn( p_aout, "recovered from buffer underrun" );
732 /* Reget the status */
733 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
736 msg_Err( p_aout, "unable to get the device's status after "
737 "recovery (%s)", snd_strerror( i_snd_rc ) );
739 msleep( p_sys->i_period_time >> 1 );
745 msg_Err( p_aout, "unable to recover from buffer underrun" );
747 msleep( p_sys->i_period_time >> 1 );
751 /* Underrun, try to recover as quickly as possible */
756 /* Here the device should be either in the RUNNING state.
757 * p_status is valid. */
759 snd_pcm_status_get_tstamp( p_status, &ts_next );
760 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
763 next_date += (mtime_t)snd_pcm_status_get_delay(p_status)
764 * 1000000 / p_aout->output.output.i_rate;
768 /* With screwed ALSA drivers the timestamp is always zero;
769 * use another method then */
770 snd_pcm_sframes_t delay;
773 if( !snd_pcm_delay( p_sys->p_snd_pcm, &delay ) )
775 i_bytes = snd_pcm_frames_to_bytes(p_sys->p_snd_pcm, delay);
777 next_date = mdate() + (mtime_t)i_bytes * 1000000
778 / p_aout->output.output.i_bytes_per_frame
779 / p_aout->output.output.i_rate
780 * p_aout->output.output.i_frame_length;
784 p_buffer = aout_OutputNextBuffer( p_aout, next_date,
785 (p_aout->output.output.i_format ==
786 VLC_FOURCC('s','p','d','i')) );
788 /* Audio output buffer shortage -> stop the fill process and wait */
789 if( p_buffer == NULL )
791 msleep( p_sys->i_period_time >> 1 );
795 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
796 p_buffer->i_nb_samples );
800 msg_Err( p_aout, "write failed (%s)",
801 snd_strerror( i_snd_rc ) );
804 aout_BufferFree( p_buffer );