]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
a92e7d71a42248e5039f71b616d8302e32b5ddf6
[vlc] / modules / audio_output / oss.c
1 /*****************************************************************************
2  * oss.c : OSS /dev/dsp module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 VideoLAN (Centrale Réseaux) and its contributors
5  * $Id$
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Sam Hocevar <sam@zoy.org>
9  *          Christophe Massiot <massiot@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29 #include <errno.h>                                                 /* ENOMEM */
30 #include <fcntl.h>                                       /* open(), O_WRONLY */
31 #include <sys/ioctl.h>                                            /* ioctl() */
32 #include <string.h>                                            /* strerror() */
33 #include <unistd.h>                                      /* write(), close() */
34 #include <stdlib.h>                            /* calloc(), malloc(), free() */
35
36 #include <vlc/vlc.h>
37
38 #ifdef HAVE_ALLOCA_H
39 #   include <alloca.h>
40 #endif
41
42 #include <vlc/aout.h>
43
44 #include "aout_internal.h"
45
46 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
47  * SNDCTL_DSP_GETOSPACE */
48 #ifdef HAVE_SOUNDCARD_H
49 #   include <soundcard.h>
50 #elif defined( HAVE_SYS_SOUNDCARD_H )
51 #   include <sys/soundcard.h>
52 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
53 #   include <machine/soundcard.h>
54 #endif
55
56 /* Patches for ignorant OSS versions */
57 #ifndef AFMT_AC3
58 #   define AFMT_AC3     0x00000400        /* Dolby Digital AC3 */
59 #endif
60
61 #ifndef AFMT_S16_NE
62 #   ifdef WORDS_BIGENDIAN
63 #       define AFMT_S16_NE AFMT_S16_BE
64 #   else
65 #       define AFMT_S16_NE AFMT_S16_LE
66 #   endif
67 #endif
68
69 /*****************************************************************************
70  * aout_sys_t: OSS audio output method descriptor
71  *****************************************************************************
72  * This structure is part of the audio output thread descriptor.
73  * It describes the DSP specific properties of an audio device.
74  *****************************************************************************/
75 struct aout_sys_t
76 {
77     int i_fd;
78     int b_workaround_buggy_driver;
79     int i_fragstotal;
80     mtime_t max_buffer_duration;
81 };
82
83 /* This must be a power of 2. */
84 #define FRAME_SIZE 1024
85 #define FRAME_COUNT 4
86
87 /*****************************************************************************
88  * Local prototypes
89  *****************************************************************************/
90 static int  Open         ( vlc_object_t * );
91 static void Close        ( vlc_object_t * );
92
93 static void Play         ( aout_instance_t * );
94 static int  OSSThread    ( aout_instance_t * );
95
96 static mtime_t BufferDuration( aout_instance_t * p_aout );
97
98 /*****************************************************************************
99  * Module descriptor
100  *****************************************************************************/
101 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
102 #define BUGGY_LONGTEXT N_( \
103     "Some buggy OSS drivers just don't like when their internal buffers " \
104     "are completely filled (the sound gets heavily hashed). If you have one " \
105     "of these drivers, then you need to enable this option." )
106
107 vlc_module_begin();
108     set_shortname( "OSS" );
109     set_description( _("Linux OSS audio output") );
110
111     set_category( CAT_AUDIO );
112     set_subcategory( SUBCAT_AUDIO_AOUT );
113     add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
114               N_("OSS DSP device"), NULL, VLC_FALSE );
115     add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, VLC_TRUE );
116
117     set_capability( "audio output", 100 );
118     add_shortcut( "oss" );
119     set_callbacks( Open, Close );
120 vlc_module_end();
121
122 /*****************************************************************************
123  * Probe: probe the audio device for available formats and channels
124  *****************************************************************************/
125 static void Probe( aout_instance_t * p_aout )
126 {
127     struct aout_sys_t * p_sys = p_aout->output.p_sys;
128     vlc_value_t val, text;
129     int i_format, i_nb_channels;
130
131     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
132     text.psz_string = _("Audio Device");
133     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
134
135     /* Test for multi-channel. */
136 #ifdef SNDCTL_DSP_GETCHANNELMASK
137     if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
138     {
139         /* Check that the device supports this. */
140
141         int i_chanmask;
142
143         /* Reset all. */
144         i_format = AFMT_S16_NE;
145         if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
146             ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
147         {
148             msg_Err( p_aout, "cannot reset OSS audio device" );
149             var_Destroy( p_aout, "audio-device" );
150             return;
151         }
152
153         if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
154                     &i_chanmask ) == 0 )
155         {
156             if ( !(i_chanmask & DSP_BIND_FRONT) )
157             {
158                 msg_Err( p_aout, "No front channels ! (%x)",
159                          i_chanmask );
160                 return;
161             }
162
163             if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
164                   && (p_aout->output.output.i_physical_channels ==
165                        (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
166                          | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
167                          | AOUT_CHAN_LFE)) )
168             {
169                 val.i_int = AOUT_VAR_5_1;
170                 text.psz_string = N_("5.1");
171                 var_Change( p_aout, "audio-device",
172                             VLC_VAR_ADDCHOICE, &val, &text );
173             }
174
175             if ( (i_chanmask & DSP_BIND_SURR)
176                   && (p_aout->output.output.i_physical_channels &
177                        (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
178                          | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
179             {
180                 val.i_int = AOUT_VAR_2F2R;
181                 text.psz_string = N_("2 Front 2 Rear");
182                 var_Change( p_aout, "audio-device",
183                             VLC_VAR_ADDCHOICE, &val, &text );
184             }
185         }
186     }
187 #endif
188
189     /* Reset all. */
190     i_format = AFMT_S16_NE;
191     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
192         ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
193     {
194         msg_Err( p_aout, "cannot reset OSS audio device" );
195         var_Destroy( p_aout, "audio-device" );
196         return;
197     }
198
199     /* Test for stereo. */
200     i_nb_channels = 2;
201     if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
202          && i_nb_channels == 2 )
203     {
204         val.i_int = AOUT_VAR_STEREO;
205         text.psz_string = N_("Stereo");
206         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
207     }
208
209     /* Reset all. */
210     i_format = AFMT_S16_NE;
211     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
212         ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
213     {
214         msg_Err( p_aout, "cannot reset OSS audio device" );
215         var_Destroy( p_aout, "audio-device" );
216         return;
217     }
218
219     /* Test for mono. */
220     i_nb_channels = 1;
221     if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
222          && i_nb_channels == 1 )
223     {
224         val.i_int = AOUT_VAR_MONO;
225         text.psz_string = N_("Mono");
226         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
227         if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
228         {
229             var_Set( p_aout, "audio-device", val );
230         }
231     }
232
233     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
234     {
235         msg_Err( p_aout, "cannot reset OSS audio device" );
236         var_Destroy( p_aout, "audio-device" );
237         return;
238     }
239
240     /* Test for spdif. */
241     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
242     {
243         i_format = AFMT_AC3;
244
245         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
246              && i_format == AFMT_AC3 )
247         {
248             val.i_int = AOUT_VAR_SPDIF;
249             text.psz_string = N_("A/52 over S/PDIF");
250             var_Change( p_aout, "audio-device",
251                         VLC_VAR_ADDCHOICE, &val, &text );
252             if( config_GetInt( p_aout, "spdif" ) )
253                 var_Set( p_aout, "audio-device", val );
254         }
255         else if( config_GetInt( p_aout, "spdif" ) )
256         {
257             msg_Warn( p_aout, "s/pdif not supported by card" );
258         }
259     }
260
261     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
262                      NULL );
263 }
264
265 /*****************************************************************************
266  * Open: open the audio device (the digital sound processor)
267  *****************************************************************************
268  * This function opens the DSP as a usual non-blocking write-only file, and
269  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
270  *****************************************************************************/
271 static int Open( vlc_object_t *p_this )
272 {
273     aout_instance_t * p_aout = (aout_instance_t *)p_this;
274     struct aout_sys_t * p_sys;
275     char * psz_device;
276     vlc_value_t val;
277
278     /* Allocate structure */
279     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
280     if( p_sys == NULL )
281     {
282         msg_Err( p_aout, "out of memory" );
283         return VLC_ENOMEM;
284     }
285
286     /* Get device name */
287     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
288     {
289         msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
290         free( p_sys );
291         return VLC_EGENERIC;
292     }
293
294     /* Open the sound device in non-blocking mode, because ALSA's OSS
295      * emulation and some broken OSS drivers would make a blocking call
296      * wait forever until the device is available. Since this breaks the
297      * OSS spec, we immediately put it back to blocking mode if the
298      * operation was successful. */
299     p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
300     if( p_sys->i_fd < 0 )
301     {
302         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
303         free( p_sys );
304         return VLC_EGENERIC;
305     }
306
307     /* if the opening was ok, put the device back in blocking mode */
308     fcntl( p_sys->i_fd, F_SETFL,
309             fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
310
311     free( psz_device );
312
313     p_aout->output.pf_play = Play;
314
315     if ( var_Type( p_aout, "audio-device" ) == 0 )
316     {
317         Probe( p_aout );
318     }
319
320     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
321     {
322         /* Probe() has failed. */
323         free( p_sys );
324         return VLC_EGENERIC;
325     }
326
327     if ( val.i_int == AOUT_VAR_SPDIF )
328     {
329         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
330     }
331     else if ( val.i_int == AOUT_VAR_5_1 )
332     {
333         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
334         p_aout->output.output.i_physical_channels
335             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
336                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
337                | AOUT_CHAN_LFE;
338     }
339     else if ( val.i_int == AOUT_VAR_2F2R )
340     {
341         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
342         p_aout->output.output.i_physical_channels
343             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
344                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
345     }
346     else if ( val.i_int == AOUT_VAR_STEREO )
347     {
348         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
349         p_aout->output.output.i_physical_channels
350             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
351     }
352     else if ( val.i_int == AOUT_VAR_MONO )
353     {
354         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
355         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
356     }
357     else
358     {
359         /* This should not happen ! */
360         msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
361         free( p_sys );
362         return VLC_EGENERIC;
363     }
364
365     val.b_bool = VLC_TRUE;
366     var_Set( p_aout, "intf-change", val );
367
368     /* Reset the DSP device */
369     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
370     {
371         msg_Err( p_aout, "cannot reset OSS audio device" );
372         close( p_sys->i_fd );
373         free( p_sys );
374         return VLC_EGENERIC;
375     }
376
377     /* Set the output format */
378     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
379     {
380         int i_format = AFMT_AC3;
381
382         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
383              || i_format != AFMT_AC3 )
384         {
385             msg_Err( p_aout, "cannot reset OSS audio device" );
386             close( p_sys->i_fd );
387             free( p_sys );
388             return VLC_EGENERIC;
389         }
390
391         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
392         p_aout->output.i_nb_samples = A52_FRAME_NB;
393         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
394         p_aout->output.output.i_frame_length = A52_FRAME_NB;
395
396         aout_VolumeNoneInit( p_aout );
397     }
398
399     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
400     {
401         unsigned int i_format = AFMT_S16_NE;
402         unsigned int i_frame_size, i_fragments;
403         unsigned int i_rate;
404         unsigned int i_nb_channels;
405         audio_buf_info audio_buf;
406
407         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
408         {
409             msg_Err( p_aout, "cannot set audio output format" );
410             close( p_sys->i_fd );
411             free( p_sys );
412             return VLC_EGENERIC;
413         }
414
415         switch ( i_format )
416         {
417         case AFMT_U8:
418             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
419             break;
420         case AFMT_S8:
421             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
422             break;
423         case AFMT_U16_LE:
424             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
425             break;
426         case AFMT_S16_LE:
427             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
428             break;
429         case AFMT_U16_BE:
430             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
431             break;
432         case AFMT_S16_BE:
433             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
434             break;
435         default:
436             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
437                      i_format );
438             close( p_sys->i_fd );
439             free( p_sys );
440             return VLC_EGENERIC;
441         }
442
443         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
444
445         /* Set the number of channels */
446         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
447             i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
448         {
449             msg_Err( p_aout, "cannot set number of audio channels (%s)",
450                      aout_FormatPrintChannels( &p_aout->output.output) );
451             close( p_sys->i_fd );
452             free( p_sys );
453             return VLC_EGENERIC;
454         }
455
456         /* Set the output rate */
457         i_rate = p_aout->output.output.i_rate;
458         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
459         {
460             msg_Err( p_aout, "cannot set audio output rate (%i)",
461                              p_aout->output.output.i_rate );
462             close( p_sys->i_fd );
463             free( p_sys );
464             return VLC_EGENERIC;
465         }
466
467         if( i_rate != p_aout->output.output.i_rate )
468         {
469             p_aout->output.output.i_rate = i_rate;
470         }
471
472         /* Set the fragment size */
473         aout_FormatPrepare( &p_aout->output.output );
474
475         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
476          *                              1 << yyyy   is fragsize */
477         i_fragments = 0;
478         i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
479         while( i_frame_size >>= 1 )
480         {
481             ++i_fragments;
482         }
483         i_fragments |= FRAME_COUNT << 16;
484         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
485         {
486             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
487         }
488
489         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
490         {
491             msg_Err( p_aout, "cannot get fragment size" );
492             close( p_sys->i_fd );
493             free( p_sys );
494             return VLC_EGENERIC;
495         }
496         else
497         {
498             /* Number of fragments actually allocated */
499             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
500
501             /* Maximum duration the soundcard's buffer can hold */
502             p_aout->output.p_sys->max_buffer_duration =
503                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
504                 / p_aout->output.output.i_bytes_per_frame
505                 / p_aout->output.output.i_rate
506                 * p_aout->output.output.i_frame_length;
507
508             p_aout->output.i_nb_samples = audio_buf.fragsize /
509                 p_aout->output.output.i_bytes_per_frame;
510         }
511
512         aout_VolumeSoftInit( p_aout );
513     }
514
515     p_aout->output.p_sys->b_workaround_buggy_driver =
516         config_GetInt( p_aout, "oss-buggy" );
517
518     /* Create OSS thread and wait for its readiness. */
519     if( vlc_thread_create( p_aout, "aout", OSSThread,
520                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
521     {
522         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
523         close( p_sys->i_fd );
524         free( p_sys );
525         return VLC_ETHREAD;
526     }
527
528     return VLC_SUCCESS;
529 }
530
531 /*****************************************************************************
532  * Play: nothing to do
533  *****************************************************************************/
534 static void Play( aout_instance_t *p_aout )
535 {
536 }
537
538 /*****************************************************************************
539  * Close: close the DSP audio device
540  *****************************************************************************/
541 static void Close( vlc_object_t * p_this )
542 {
543     aout_instance_t *p_aout = (aout_instance_t *)p_this;
544     struct aout_sys_t * p_sys = p_aout->output.p_sys;
545
546     p_aout->b_die = VLC_TRUE;
547     vlc_thread_join( p_aout );
548     p_aout->b_die = VLC_FALSE;
549
550     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
551     close( p_sys->i_fd );
552
553     free( p_sys );
554 }
555
556 /*****************************************************************************
557  * BufferDuration: buffer status query
558  *****************************************************************************
559  * This function returns the duration in microseconds of the current buffer.
560  *****************************************************************************/
561 static mtime_t BufferDuration( aout_instance_t * p_aout )
562 {
563     struct aout_sys_t * p_sys = p_aout->output.p_sys;
564     audio_buf_info audio_buf;
565     int i_bytes;
566
567     /* Fill the audio_buf_info structure:
568      * - fragstotal: total number of fragments allocated
569      * - fragsize: size of a fragment in bytes
570      * - bytes: available space in bytes (includes partially used fragments)
571      * Note! 'bytes' could be more than fragments*fragsize */
572     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
573
574     /* calculate number of available fragments (not partially used ones) */
575     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
576
577     /* Return the fragment duration */
578     return (mtime_t)i_bytes * 1000000
579             / p_aout->output.output.i_bytes_per_frame
580             / p_aout->output.output.i_rate
581             * p_aout->output.output.i_frame_length;
582 }
583
584 /*****************************************************************************
585  * OSSThread: asynchronous thread used to DMA the data to the device
586  *****************************************************************************/
587 static int OSSThread( aout_instance_t * p_aout )
588 {
589     struct aout_sys_t * p_sys = p_aout->output.p_sys;
590     mtime_t next_date = 0;
591
592     while ( !p_aout->b_die )
593     {
594         aout_buffer_t * p_buffer = NULL;
595         int i_tmp, i_size;
596         byte_t * p_bytes;
597
598         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
599         {
600             mtime_t buffered = BufferDuration( p_aout );
601
602             if( p_aout->output.p_sys->b_workaround_buggy_driver )
603             {
604 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
605                 /* Wait a bit - we don't want our buffer to be full */
606                 if( buffered > (p_aout->output.p_sys->max_buffer_duration
607                                 / i_fragstotal * (i_fragstotal - 1)) )
608                 {
609                     msleep((p_aout->output.p_sys->max_buffer_duration
610                                 / i_fragstotal ));
611                     buffered = BufferDuration( p_aout );
612                 }
613 #undef i_fragstotal
614             }
615
616             /* Next buffer will be played at mdate() + buffered */
617             p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
618                                               VLC_FALSE );
619
620             if( p_buffer == NULL &&
621                 buffered > ( p_aout->output.p_sys->max_buffer_duration
622                              / p_aout->output.p_sys->i_fragstotal ) )
623             {
624                 /* If we have at least a fragment full, then we can wait a
625                  * little and retry to get a new audio buffer instead of
626                  * playing a blank sample */
627                 msleep( ( p_aout->output.p_sys->max_buffer_duration
628                           / p_aout->output.p_sys->i_fragstotal / 2 ) );
629                 continue;
630             }
631         }
632         else
633         {
634             /* emu10k1 driver does not report Buffer Duration correctly in
635              * passthrough mode so we have to cheat */
636             if( !next_date )
637             {
638                 next_date = mdate();
639             }
640             else
641             {
642                 mtime_t delay = next_date - mdate();
643                 if( delay > AOUT_PTS_TOLERANCE )
644                 {
645                     msleep( delay / 2 );
646                 }
647             }
648
649             while( !p_aout->b_die && ! ( p_buffer =
650                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
651             {
652                 msleep( 1000 );
653                 next_date = mdate();
654             }
655         }
656
657         if ( p_buffer != NULL )
658         {
659             p_bytes = p_buffer->p_buffer;
660             i_size = p_buffer->i_nb_bytes;
661             /* This is theoretical ... we'll see next iteration whether
662              * we're drifting */
663             next_date += p_buffer->end_date - p_buffer->start_date;
664         }
665         else
666         {
667             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
668                       * p_aout->output.output.i_bytes_per_frame;
669             p_bytes = malloc( i_size );
670             memset( p_bytes, 0, i_size );
671             next_date = 0;
672         }
673
674         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
675
676         if( i_tmp < 0 )
677         {
678             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
679         }
680
681         if ( p_buffer != NULL )
682         {
683             aout_BufferFree( p_buffer );
684         }
685         else
686         {
687             free( p_bytes );
688         }
689     }
690
691     return VLC_SUCCESS;
692 }