1 /*****************************************************************************
2 * alsa.c : alsa plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2000-2001 VideoLAN
5 * $Id: alsa.c,v 1.35 2003/10/23 21:55:50 gbazin 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;
61 int b_playing; /* playing status */
68 #define A52_FRAME_NB 1536
70 /* These values are in frames.
71 To convert them to a number of bytes you have to multiply them by the
72 number of channel(s) (eg. 2 for stereo) and the size of a sample (eg.
74 #define ALSA_DEFAULT_PERIOD_SIZE 1024
75 #define ALSA_DEFAULT_BUFFER_SIZE ( ALSA_DEFAULT_PERIOD_SIZE << 8 )
76 #define ALSA_SPDIF_PERIOD_SIZE A52_FRAME_NB
77 #define ALSA_SPDIF_BUFFER_SIZE ( ALSA_SPDIF_PERIOD_SIZE << 4 )
78 /* Why << 4 ? --Meuuh */
79 /* Why not ? --Bozo */
82 #define DEFAULT_ALSA_DEVICE "default"
84 /*****************************************************************************
86 *****************************************************************************/
87 static int Open ( vlc_object_t * );
88 static void Close ( vlc_object_t * );
89 static void Play ( aout_instance_t * );
90 static int ALSAThread ( aout_instance_t * );
91 static void ALSAFill ( aout_instance_t * );
93 /*****************************************************************************
95 *****************************************************************************/
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 set_description( _("ALSA audio output") );
101 set_capability( "audio output", 150 );
102 set_callbacks( Open, Close );
105 /*****************************************************************************
106 * Probe: probe the audio device for available formats and channels
107 *****************************************************************************/
108 static void Probe( aout_instance_t * p_aout,
109 const char * psz_device, const char * psz_iec_device,
110 int *pi_snd_pcm_format )
112 struct aout_sys_t * p_sys = p_aout->output.p_sys;
113 vlc_value_t val, text;
116 var_Create ( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
117 text.psz_string = _("Audio device");
118 var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
120 /* We'll open the audio device in non blocking mode so we can just exit
121 * when it is already in use, but for the real stuff we'll still use
122 * the blocking mode */
124 /* Now test linear PCM capabilities */
125 if ( !(i_ret = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
126 SND_PCM_STREAM_PLAYBACK,
127 SND_PCM_NONBLOCK ) ) )
130 snd_pcm_hw_params_t * p_hw;
131 snd_pcm_hw_params_alloca (&p_hw);
133 if ( snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) < 0 )
135 msg_Warn( p_aout, "unable to retrieve initial hardware parameters"
136 ", disabling linear PCM audio" );
137 snd_pcm_close( p_sys->p_snd_pcm );
138 var_Destroy( p_aout, "audio-device" );
142 if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
143 *pi_snd_pcm_format ) < 0 )
145 if( *pi_snd_pcm_format != SND_PCM_FORMAT_S16 )
147 *pi_snd_pcm_format = SND_PCM_FORMAT_S16;
148 if ( snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
149 *pi_snd_pcm_format ) < 0 )
151 msg_Warn( p_aout, "unable to set stream sample size and "
152 "word order, disabling linear PCM audio" );
153 snd_pcm_close( p_sys->p_snd_pcm );
154 var_Destroy( p_aout, "audio-device" );
160 i_channels = aout_FormatNbChannels( &p_aout->output.output );
162 while ( i_channels > 0 )
164 if ( !snd_pcm_hw_params_test_channels( p_sys->p_snd_pcm, p_hw,
167 switch ( i_channels )
170 val.i_int = AOUT_VAR_MONO;
171 text.psz_string = N_("Mono");
172 var_Change( p_aout, "audio-device",
173 VLC_VAR_ADDCHOICE, &val, &text );
176 val.i_int = AOUT_VAR_STEREO;
177 text.psz_string = N_("Stereo");
178 var_Change( p_aout, "audio-device",
179 VLC_VAR_ADDCHOICE, &val, &text );
180 var_Set( p_aout, "audio-device", val );
183 val.i_int = AOUT_VAR_2F2R;
184 text.psz_string = N_("2 Front 2 Rear");
185 var_Change( p_aout, "audio-device",
186 VLC_VAR_ADDCHOICE, &val, &text );
189 val.i_int = AOUT_VAR_5_1;
190 text.psz_string = N_("5.1");
191 var_Change( p_aout, "audio-device",
192 VLC_VAR_ADDCHOICE, &val, &text );
200 /* Close the previously opened device */
201 snd_pcm_close( p_sys->p_snd_pcm );
203 else if ( i_ret == -EBUSY )
205 msg_Warn( p_aout, "audio device: %s is already in use", psz_device );
208 /* Test for S/PDIF device if needed */
209 if ( psz_iec_device )
211 /* Opening the device should be enough */
212 if ( !(i_ret = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
213 SND_PCM_STREAM_PLAYBACK,
214 SND_PCM_NONBLOCK ) ) )
216 val.i_int = AOUT_VAR_SPDIF;
217 text.psz_string = N_("A/52 over S/PDIF");
218 var_Change( p_aout, "audio-device",
219 VLC_VAR_ADDCHOICE, &val, &text );
220 if( config_GetInt( p_aout, "spdif" ) )
221 var_Set( p_aout, "audio-device", val );
223 snd_pcm_close( p_sys->p_snd_pcm );
225 else if ( i_ret == -EBUSY )
227 msg_Warn( p_aout, "audio device: %s is already in use",
232 var_Change( p_aout, "audio-device", VLC_VAR_CHOICESCOUNT, &val, NULL );
235 /* Probe() has failed. */
236 var_Destroy( p_aout, "audio-device" );
240 /* Add final settings to the variable */
241 var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart, NULL );
242 val.b_bool = VLC_TRUE;
243 var_Set( p_aout, "intf-change", val );
246 /*****************************************************************************
247 * Open: create a handle and open an alsa device
248 *****************************************************************************
249 * This function opens an alsa device, through the alsa API.
251 * Note: the only heap-allocated string is psz_device. All the other pointers
252 * are references to psz_device or to stack-allocated data.
253 *****************************************************************************/
254 static int Open( vlc_object_t *p_this )
256 aout_instance_t * p_aout = (aout_instance_t *)p_this;
257 struct aout_sys_t * p_sys;
260 char psz_default_iec_device[128]; /* Buffer used to store the default
262 char * psz_device, * psz_iec_device; /* device names for PCM and S/PDIF
265 int i_vlc_pcm_format; /* Audio format for VLC's data */
266 int i_snd_pcm_format; /* Audio format for ALSA's data */
268 snd_pcm_uframes_t i_buffer_size = 0;
269 snd_pcm_uframes_t i_period_size = 0;
272 snd_pcm_hw_params_t *p_hw;
273 snd_pcm_sw_params_t *p_sw;
277 /* Allocate structures */
278 p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
281 msg_Err( p_aout, "out of memory" );
284 p_sys->b_playing = VLC_FALSE;
285 p_sys->start_date = 0;
286 vlc_cond_init( p_aout, &p_sys->wait );
287 vlc_mutex_init( p_aout, &p_sys->lock );
289 /* Get device name */
290 if( (psz_device = config_GetPsz( p_aout, "alsadev" )) == NULL )
292 msg_Err( p_aout, "no audio device given (maybe \"default\" ?)" );
297 /* Choose the IEC device for S/PDIF output:
298 if the device is overriden by the user then it will be the one
299 otherwise we compute the default device based on the output format. */
300 if( AOUT_FMT_NON_LINEAR( &p_aout->output.output )
301 && !strcmp( psz_device, DEFAULT_ALSA_DEVICE ) )
303 snprintf( psz_default_iec_device, sizeof(psz_default_iec_device),
304 "iec958:AES0=0x%x,AES1=0x%x,AES2=0x%x,AES3=0x%x",
305 IEC958_AES0_CON_EMPHASIS_NONE | IEC958_AES0_NONAUDIO,
306 IEC958_AES1_CON_ORIGINAL | IEC958_AES1_CON_PCM_CODER,
308 ( p_aout->output.output.i_rate == 48000 ?
309 IEC958_AES3_CON_FS_48000 :
310 ( p_aout->output.output.i_rate == 44100 ?
311 IEC958_AES3_CON_FS_44100 : IEC958_AES3_CON_FS_32000 ) ) );
312 psz_iec_device = psz_default_iec_device;
314 else if( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
316 psz_iec_device = psz_device;
320 psz_iec_device = NULL;
323 /* Choose the linear PCM format (read the comment above about FPU
325 if( p_aout->p_libvlc->i_cpu & CPU_CAPABILITY_FPU )
327 i_vlc_pcm_format = VLC_FOURCC('f','l','3','2');
328 i_snd_pcm_format = SND_PCM_FORMAT_FLOAT;
332 i_vlc_pcm_format = AOUT_FMT_S16_NE;
333 i_snd_pcm_format = SND_PCM_FORMAT_S16;
336 /* If the variable doesn't exist then it's the first time we're called
337 and we have to probe the available audio formats and channels */
338 if ( var_Type( p_aout, "audio-device" ) == 0 )
340 Probe( p_aout, psz_device, psz_iec_device, &i_snd_pcm_format );
341 switch( i_snd_pcm_format )
343 case SND_PCM_FORMAT_FLOAT:
344 i_vlc_pcm_format = VLC_FOURCC('f','l','3','2');
346 case SND_PCM_FORMAT_S16:
347 i_vlc_pcm_format = AOUT_FMT_S16_NE;
352 if ( var_Get( p_aout, "audio-device", &val ) < 0 )
359 if ( val.i_int == AOUT_VAR_SPDIF )
361 p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
363 else if ( val.i_int == AOUT_VAR_5_1 )
365 p_aout->output.output.i_format = i_vlc_pcm_format;
366 p_aout->output.output.i_physical_channels
367 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
368 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
371 psz_device = strdup( "surround51" );
373 else if ( val.i_int == AOUT_VAR_2F2R )
375 p_aout->output.output.i_format = i_vlc_pcm_format;
376 p_aout->output.output.i_physical_channels
377 = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
378 | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
380 psz_device = strdup( "surround40" );
382 else if ( val.i_int == AOUT_VAR_STEREO )
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;
388 else if ( val.i_int == AOUT_VAR_MONO )
390 p_aout->output.output.i_format = i_vlc_pcm_format;
391 p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
396 /* This should not happen ! */
397 msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
403 snd_output_stdio_attach( &p_sys->p_snd_stderr, stderr, 0 );
406 /* Open the device */
407 if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
409 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_iec_device,
410 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
412 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
413 psz_iec_device, snd_strerror( i_snd_rc ) );
418 i_buffer_size = ALSA_SPDIF_BUFFER_SIZE;
419 i_snd_pcm_format = SND_PCM_FORMAT_S16;
422 p_aout->output.i_nb_samples = i_period_size = ALSA_SPDIF_PERIOD_SIZE;
423 p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
424 p_aout->output.output.i_frame_length = A52_FRAME_NB;
426 aout_VolumeNoneInit( p_aout );
430 msg_Dbg( p_aout, "opening ALSA device `%s'", psz_device );
432 if ( ( i_snd_rc = snd_pcm_open( &p_sys->p_snd_pcm, psz_device,
433 SND_PCM_STREAM_PLAYBACK, 0 ) ) < 0 )
435 msg_Err( p_aout, "cannot open ALSA device `%s' (%s)",
436 psz_device, snd_strerror( i_snd_rc ) );
441 i_buffer_size = ALSA_DEFAULT_BUFFER_SIZE;
442 i_channels = aout_FormatNbChannels( &p_aout->output.output );
444 p_aout->output.i_nb_samples = i_period_size = ALSA_DEFAULT_PERIOD_SIZE;
446 aout_VolumeSoftInit( p_aout );
449 /* Free psz_device so that all the remaining data is stack-allocated */
452 p_aout->output.pf_play = Play;
454 snd_pcm_hw_params_alloca(&p_hw);
455 snd_pcm_sw_params_alloca(&p_sw);
457 /* Get Initial hardware parameters */
458 if ( ( i_snd_rc = snd_pcm_hw_params_any( p_sys->p_snd_pcm, p_hw ) ) < 0 )
460 msg_Err( p_aout, "unable to retrieve initial hardware parameters (%s)",
461 snd_strerror( i_snd_rc ) );
466 if ( ( i_snd_rc = snd_pcm_hw_params_set_format( p_sys->p_snd_pcm, p_hw,
467 i_snd_pcm_format ) ) < 0 )
469 msg_Err( p_aout, "unable to set stream sample size and word order (%s)",
470 snd_strerror( i_snd_rc ) );
474 if ( ( i_snd_rc = snd_pcm_hw_params_set_access( p_sys->p_snd_pcm, p_hw,
475 SND_PCM_ACCESS_RW_INTERLEAVED ) ) < 0 )
477 msg_Err( p_aout, "unable to set interleaved stream format (%s)",
478 snd_strerror( i_snd_rc ) );
483 if ( ( i_snd_rc = snd_pcm_hw_params_set_channels( p_sys->p_snd_pcm, p_hw,
486 msg_Err( p_aout, "unable to set number of output channels (%s)",
487 snd_strerror( i_snd_rc ) );
492 #ifdef HAVE_ALSA_NEW_API
493 if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
494 &p_aout->output.output.i_rate, NULL ) ) < 0 )
496 if ( ( i_snd_rc = snd_pcm_hw_params_set_rate_near( p_sys->p_snd_pcm, p_hw,
497 p_aout->output.output.i_rate, NULL ) ) < 0 )
500 msg_Warn( p_aout, "The rate %d Hz is not supported by your hardware. "
501 "Using %d Hz instead.\n", p_aout->output.output.i_rate,
503 p_aout->output.output.i_rate = i_snd_rc;
506 /* Set buffer size. */
507 #ifdef HAVE_ALSA_NEW_API
508 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
509 p_hw, &i_buffer_size ) ) < 0 )
511 if ( ( i_snd_rc = snd_pcm_hw_params_set_buffer_size_near( p_sys->p_snd_pcm,
512 p_hw, i_buffer_size ) ) < 0 )
515 msg_Err( p_aout, "unable to set buffer size (%s)",
516 snd_strerror( i_snd_rc ) );
520 /* Set period size. */
521 #ifdef HAVE_ALSA_NEW_API
522 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
523 p_hw, &i_period_size, NULL ) ) < 0 )
525 if ( ( i_snd_rc = snd_pcm_hw_params_set_period_size_near( p_sys->p_snd_pcm,
526 p_hw, i_period_size, NULL ) ) < 0 )
529 msg_Err( p_aout, "unable to set period size (%s)",
530 snd_strerror( i_snd_rc ) );
533 p_aout->output.i_nb_samples = i_period_size;
535 /* Commit hardware parameters. */
536 if ( ( i_snd_rc = snd_pcm_hw_params( p_sys->p_snd_pcm, p_hw ) ) < 0 )
538 msg_Err( p_aout, "unable to commit hardware configuration (%s)",
539 snd_strerror( i_snd_rc ) );
543 #ifdef HAVE_ALSA_NEW_API
544 if( ( i_snd_rc = snd_pcm_hw_params_get_period_time( p_hw,
545 &p_sys->i_period_time, NULL ) ) < 0 )
547 if( ( p_sys->i_period_time =
548 snd_pcm_hw_params_get_period_time( p_hw, NULL ) ) < 0 )
551 msg_Err( p_aout, "unable to get period time (%s)",
552 snd_strerror( i_snd_rc ) );
556 /* Get Initial software parameters */
557 snd_pcm_sw_params_current( p_sys->p_snd_pcm, p_sw );
559 i_snd_rc = snd_pcm_sw_params_set_sleep_min( p_sys->p_snd_pcm, p_sw, 0 );
561 i_snd_rc = snd_pcm_sw_params_set_avail_min( p_sys->p_snd_pcm, p_sw,
562 p_aout->output.i_nb_samples );
564 /* Commit software parameters. */
565 if ( snd_pcm_sw_params( p_sys->p_snd_pcm, p_sw ) < 0 )
567 msg_Err( p_aout, "unable to set software configuration" );
572 snd_output_printf( p_sys->p_snd_stderr, "\nALSA hardware setup:\n\n" );
573 snd_pcm_dump_hw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
574 snd_output_printf( p_sys->p_snd_stderr, "\nALSA software setup:\n\n" );
575 snd_pcm_dump_sw_setup( p_sys->p_snd_pcm, p_sys->p_snd_stderr );
576 snd_output_printf( p_sys->p_snd_stderr, "\n" );
579 /* Create ALSA thread and wait for its readiness. */
580 if( vlc_thread_create( p_aout, "aout", ALSAThread,
581 VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
583 msg_Err( p_aout, "cannot create ALSA thread (%s)", strerror(errno) );
590 snd_pcm_close( p_sys->p_snd_pcm );
592 snd_output_close( p_sys->p_snd_stderr );
598 /*****************************************************************************
599 * Play: nothing to do
600 *****************************************************************************/
601 static void Play( aout_instance_t *p_aout )
603 if( !p_aout->output.p_sys->b_playing )
605 p_aout->output.p_sys->b_playing = 1;
607 /* get the playing date of the first aout buffer */
608 p_aout->output.p_sys->start_date =
609 aout_FifoFirstDate( p_aout, &p_aout->output.fifo );
611 /* wake up the audio output thread */
612 vlc_mutex_lock( &p_aout->output.p_sys->lock );
613 vlc_cond_signal( &p_aout->output.p_sys->wait );
614 vlc_mutex_unlock( &p_aout->output.p_sys->lock );
618 /*****************************************************************************
619 * Close: close the ALSA device
620 *****************************************************************************/
621 static void Close( vlc_object_t *p_this )
623 aout_instance_t *p_aout = (aout_instance_t *)p_this;
624 struct aout_sys_t * p_sys = p_aout->output.p_sys;
627 /* make sure the audio output thread is waken up */
628 vlc_mutex_lock( &p_aout->output.p_sys->lock );
629 vlc_cond_signal( &p_aout->output.p_sys->wait );
630 vlc_mutex_unlock( &p_aout->output.p_sys->lock );
632 p_aout->b_die = VLC_TRUE;
633 vlc_thread_join( p_aout );
634 p_aout->b_die = VLC_FALSE;
636 i_snd_rc = snd_pcm_close( p_sys->p_snd_pcm );
640 msg_Err( p_aout, "failed closing ALSA device (%s)",
641 snd_strerror( i_snd_rc ) );
645 snd_output_close( p_sys->p_snd_stderr );
651 /*****************************************************************************
652 * ALSAThread: asynchronous thread used to DMA the data to the device
653 *****************************************************************************/
654 static int ALSAThread( aout_instance_t * p_aout )
656 /* Wait for the exact time to start playing (avoids resampling) */
657 vlc_mutex_lock( &p_aout->output.p_sys->lock );
658 if( !p_aout->output.p_sys->start_date )
659 vlc_cond_wait( &p_aout->output.p_sys->wait,
660 &p_aout->output.p_sys->lock );
661 vlc_mutex_unlock( &p_aout->output.p_sys->lock );
663 mwait( p_aout->output.p_sys->start_date - AOUT_PTS_TOLERANCE / 4 );
665 while ( !p_aout->b_die )
673 /*****************************************************************************
674 * ALSAFill: function used to fill the ALSA buffer as much as possible
675 *****************************************************************************/
676 static void ALSAFill( aout_instance_t * p_aout )
678 struct aout_sys_t * p_sys = p_aout->output.p_sys;
680 aout_buffer_t * p_buffer;
681 snd_pcm_status_t * p_status;
682 snd_timestamp_t ts_next;
686 snd_pcm_status_alloca( &p_status );
688 /* Fill in the buffer until space or audio output buffer shortage */
691 i_snd_rc = snd_pcm_status( p_sys->p_snd_pcm, p_status );
694 msg_Err( p_aout, "unable to get the device's status (%s)",
695 snd_strerror( i_snd_rc ) );
697 msleep( p_sys->i_period_time >> 1 );
701 /* Handle buffer underruns and reget the status */
702 if( snd_pcm_status_get_state( p_status ) == SND_PCM_STATE_XRUN )
704 /* Prepare the device */
705 i_snd_rc = snd_pcm_prepare( p_sys->p_snd_pcm );
709 msg_Warn( p_aout, "recovered from buffer underrun" );
711 /* Reget the status */
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 after "
716 "recovery (%s)", snd_strerror( i_snd_rc ) );
718 msleep( p_sys->i_period_time >> 1 );
724 msg_Err( p_aout, "unable to recover from buffer underrun" );
726 msleep( p_sys->i_period_time >> 1 );
730 /* Underrun, try to recover as quickly as possible */
735 /* Here the device should be either in the RUNNING state.
736 * p_status is valid. */
738 snd_pcm_status_get_tstamp( p_status, &ts_next );
739 next_date = (mtime_t)ts_next.tv_sec * 1000000 + ts_next.tv_usec;
740 next_date += (mtime_t)snd_pcm_status_get_delay(p_status)
741 * 1000000 / p_aout->output.output.i_rate;
744 p_buffer = aout_OutputNextBuffer( p_aout, next_date,
745 (p_aout->output.output.i_format ==
746 VLC_FOURCC('s','p','d','i')) );
748 /* Audio output buffer shortage -> stop the fill process and wait */
749 if( p_buffer == NULL )
751 msleep( p_sys->i_period_time >> 1 );
755 i_snd_rc = snd_pcm_writei( p_sys->p_snd_pcm, p_buffer->p_buffer,
756 p_buffer->i_nb_samples );
760 msg_Err( p_aout, "write failed (%s)",
761 snd_strerror( i_snd_rc ) );
764 aout_BufferFree( p_buffer );