]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* src/misc/variables.c, ALL: improvements to the object variables api.
[vlc] / modules / audio_output / oss.c
1 /*****************************************************************************
2  * oss.c : OSS /dev/dsp module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 VideoLAN
5  * $Id: oss.c,v 1.58 2003/05/04 22:42:15 gbazin Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Samuel 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     add_category_hint( N_("OSS"), NULL, VLC_FALSE );
109     add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
110               N_("OSS dsp device"), NULL, VLC_FALSE );
111     add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, VLC_TRUE );
112     set_description( _("Linux OSS audio output") );
113     set_capability( "audio output", 100 );
114
115     add_shortcut( "oss" );
116     set_callbacks( Open, Close );
117 vlc_module_end();
118
119 /*****************************************************************************
120  * Probe: probe the audio device for available formats and channels
121  *****************************************************************************/
122 static void Probe( aout_instance_t * p_aout )
123 {
124     struct aout_sys_t * p_sys = p_aout->output.p_sys;
125     vlc_value_t val, text;
126     int i_format, i_nb_channels;
127
128     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
129     text.psz_string = _("Audio device");
130     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
131
132     /* Test for multi-channel. */
133 #ifdef SNDCTL_DSP_GETCHANNELMASK
134     if ( aout_FormatNbChannels( &p_aout->output.output ) > 2 )
135     {
136         /* Check that the device supports this. */
137
138         int i_chanmask;
139
140         /* Reset all. */
141         i_format = AFMT_S16_NE;
142         if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
143             ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
144         {
145             msg_Err( p_aout, "cannot reset OSS audio device" );
146             var_Destroy( p_aout, "audio-device" );
147             return;
148         }
149
150         if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
151                     &i_chanmask ) == 0 )
152         {
153             if ( !(i_chanmask & DSP_BIND_FRONT) )
154             {
155                 msg_Err( p_aout, "No front channels ! (%x)",
156                          i_chanmask );
157                 return;
158             }
159
160             if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
161                   && (p_aout->output.output.i_physical_channels ==
162                        (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
163                          | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
164                          | AOUT_CHAN_LFE)) )
165             {
166                 val.i_int = AOUT_VAR_5_1;
167                 text.psz_string = N_("5.1");
168                 var_Change( p_aout, "audio-device",
169                             VLC_VAR_ADDCHOICE, &val, &text );
170             }
171
172             if ( (i_chanmask & DSP_BIND_SURR)
173                   && (p_aout->output.output.i_physical_channels &
174                        (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
175                          | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
176             {
177                 val.i_int = AOUT_VAR_2F2R;
178                 text.psz_string = N_("2 Front 2 Rear");
179                 var_Change( p_aout, "audio-device",
180                             VLC_VAR_ADDCHOICE, &val, &text );
181             }
182         }
183     }
184 #endif
185
186     /* Reset all. */
187     i_format = AFMT_S16_NE;
188     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
189         ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
190     {
191         msg_Err( p_aout, "cannot reset OSS audio device" );
192         var_Destroy( p_aout, "audio-device" );
193         return;
194     }
195
196     /* Test for stereo. */
197     i_nb_channels = 2;
198     if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
199          && i_nb_channels == 2 )
200     {
201         val.i_int = AOUT_VAR_STEREO;
202         text.psz_string = N_("Stereo");
203         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
204     }
205
206     /* Reset all. */
207     i_format = AFMT_S16_NE;
208     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
209         ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
210     {
211         msg_Err( p_aout, "cannot reset OSS audio device" );
212         var_Destroy( p_aout, "audio-device" );
213         return;
214     }
215
216     /* Test for mono. */
217     i_nb_channels = 1;
218     if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
219          && i_nb_channels == 1 )
220     {
221         val.i_int = AOUT_VAR_MONO;
222         text.psz_string = N_("Mono");
223         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
224         if ( p_aout->output.output.i_physical_channels == AOUT_CHAN_CENTER )
225         {
226             var_Set( p_aout, "audio-device", val );
227         }
228     }
229
230     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
231     {
232         msg_Err( p_aout, "cannot reset OSS audio device" );
233         var_Destroy( p_aout, "audio-device" );
234         return;
235     }
236
237     /* Test for spdif. */
238     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
239     {
240         i_format = AFMT_AC3;
241
242         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
243              && i_format == AFMT_AC3 )
244         {
245             val.i_int = AOUT_VAR_SPDIF;
246             text.psz_string = N_("A/52 over S/PDIF");
247             var_Change( p_aout, "audio-device",
248                         VLC_VAR_ADDCHOICE, &val, &text );
249             if( config_GetInt( p_aout, "spdif" ) )
250                 var_Set( p_aout, "audio-device", val );
251         }
252     }
253
254     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
255                      NULL );
256 }
257
258 /*****************************************************************************
259  * Open: open the audio device (the digital sound processor)
260  *****************************************************************************
261  * This function opens the dsp as a usual non-blocking write-only file, and
262  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
263  *****************************************************************************/
264 static int Open( vlc_object_t *p_this )
265 {
266     aout_instance_t * p_aout = (aout_instance_t *)p_this;
267     struct aout_sys_t * p_sys;
268     char * psz_device;
269     vlc_value_t val;
270
271     /* Allocate structure */
272     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
273     if( p_sys == NULL )
274     {
275         msg_Err( p_aout, "out of memory" );
276         return VLC_ENOMEM;
277     }
278
279     /* Get device name */
280     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
281     {
282         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
283         free( p_sys );
284         return VLC_EGENERIC;
285     }
286
287     /* Open the sound device in non-blocking mode, because ALSA's OSS
288      * emulation and some broken OSS drivers would make a blocking call
289      * wait forever until the device is available. Since this breaks the
290      * OSS spec, we immediately put it back to blocking mode if the
291      * operation was successful. */
292     p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
293     if( p_sys->i_fd < 0 )
294     {
295         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
296         free( p_sys );
297         return VLC_EGENERIC;
298     }
299
300     /* if the opening was ok, put the device back in blocking mode */
301     fcntl( p_sys->i_fd, F_SETFL,
302             fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
303
304     free( psz_device );
305
306     p_aout->output.pf_play = Play;
307
308     if ( var_Type( p_aout, "audio-device" ) == 0 )
309     {
310         Probe( p_aout );
311     }
312
313     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
314     {
315         /* Probe() has failed. */
316         free( p_sys );
317         return VLC_EGENERIC;
318     }
319
320     if ( val.i_int == AOUT_VAR_SPDIF )
321     {
322         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
323     }
324     else if ( val.i_int == AOUT_VAR_5_1 )
325     {
326         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
327         p_aout->output.output.i_physical_channels
328             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
329                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
330                | AOUT_CHAN_LFE;
331     }
332     else if ( val.i_int == AOUT_VAR_2F2R )
333     {
334         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
335         p_aout->output.output.i_physical_channels
336             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
337                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
338     }
339     else if ( val.i_int == AOUT_VAR_STEREO )
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     }
345     else if ( val.i_int == AOUT_VAR_MONO )
346     {
347         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
348         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
349     }
350     else
351     {
352         /* This should not happen ! */
353         msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
354         free( p_sys );
355         return VLC_EGENERIC;
356     }
357
358     val.b_bool = VLC_TRUE;
359     var_Set( p_aout, "intf-change", val );
360
361     /* Reset the DSP device */
362     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
363     {
364         msg_Err( p_aout, "cannot reset OSS audio device" );
365         close( p_sys->i_fd );
366         free( p_sys );
367         return VLC_EGENERIC;
368     }
369
370     /* Set the output format */
371     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
372     {
373         int i_format = AFMT_AC3;
374
375         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
376              || i_format != AFMT_AC3 )
377         {
378             msg_Err( p_aout, "cannot reset OSS audio device" );
379             close( p_sys->i_fd );
380             free( p_sys );
381             return VLC_EGENERIC;
382         }
383
384         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
385         p_aout->output.i_nb_samples = A52_FRAME_NB;
386         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
387         p_aout->output.output.i_frame_length = A52_FRAME_NB;
388
389         aout_VolumeNoneInit( p_aout );
390     }
391
392     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
393     {
394         unsigned int i_format = AFMT_S16_NE;
395         unsigned int i_frame_size, i_fragments;
396         unsigned int i_rate;
397         unsigned int i_nb_channels;
398         audio_buf_info audio_buf;
399
400         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
401         {
402             msg_Err( p_aout, "cannot set audio output format" );
403             close( p_sys->i_fd );
404             free( p_sys );
405             return VLC_EGENERIC;
406         }
407
408         switch ( i_format )
409         {
410         case AFMT_U8:
411             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
412             break;
413         case AFMT_S8:
414             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
415             break;
416         case AFMT_U16_LE:
417             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
418             break;
419         case AFMT_S16_LE:
420             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
421             break;
422         case AFMT_U16_BE:
423             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
424             break;
425         case AFMT_S16_BE:
426             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
427             break;
428         default:
429             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
430                      i_format );
431             close( p_sys->i_fd );
432             free( p_sys );
433             return VLC_EGENERIC;
434         }
435
436         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
437
438         /* Set the number of channels */
439         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
440             i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
441         {
442             msg_Err( p_aout, "cannot set number of audio channels (%s)",
443                      aout_FormatPrintChannels( &p_aout->output.output) );
444             close( p_sys->i_fd );
445             free( p_sys );
446             return VLC_EGENERIC;
447         }
448
449         /* Set the output rate */
450         i_rate = p_aout->output.output.i_rate;
451         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
452         {
453             msg_Err( p_aout, "cannot set audio output rate (%i)",
454                              p_aout->output.output.i_rate );
455             close( p_sys->i_fd );
456             free( p_sys );
457             return VLC_EGENERIC;
458         }
459
460         if( i_rate != p_aout->output.output.i_rate )
461         {
462             p_aout->output.output.i_rate = i_rate;
463         }
464
465         /* Set the fragment size */
466         aout_FormatPrepare( &p_aout->output.output );
467
468         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
469          *                              1 << yyyy   is fragsize */
470         i_fragments = 0;
471         i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
472         while( i_frame_size >>= 1 )
473         {
474             ++i_fragments;
475         }
476         i_fragments |= FRAME_COUNT << 16;
477         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
478         {
479             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
480         }
481
482         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
483         {
484             msg_Err( p_aout, "cannot get fragment size" );
485             close( p_sys->i_fd );
486             free( p_sys );
487             return VLC_EGENERIC;
488         }
489         else
490         {
491             /* Number of fragments actually allocated */
492             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
493
494             /* Maximum duration the soundcard's buffer can hold */
495             p_aout->output.p_sys->max_buffer_duration =
496                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
497                 / p_aout->output.output.i_bytes_per_frame
498                 / p_aout->output.output.i_rate
499                 * p_aout->output.output.i_frame_length;
500
501             p_aout->output.i_nb_samples = audio_buf.fragsize /
502                 p_aout->output.output.i_bytes_per_frame;
503         }
504
505         aout_VolumeSoftInit( p_aout );
506     }
507
508     p_aout->output.p_sys->b_workaround_buggy_driver =
509         config_GetInt( p_aout, "oss-buggy" );
510
511     /* Create OSS thread and wait for its readiness. */
512     if( vlc_thread_create( p_aout, "aout", OSSThread,
513                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
514     {
515         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
516         close( p_sys->i_fd );
517         free( p_sys );
518         return VLC_ETHREAD;
519     }
520
521     return VLC_SUCCESS;
522 }
523
524 /*****************************************************************************
525  * Play: nothing to do
526  *****************************************************************************/
527 static void Play( aout_instance_t *p_aout )
528 {
529 }
530
531 /*****************************************************************************
532  * Close: close the dsp audio device
533  *****************************************************************************/
534 static void Close( vlc_object_t * p_this )
535 {
536     aout_instance_t *p_aout = (aout_instance_t *)p_this;
537     struct aout_sys_t * p_sys = p_aout->output.p_sys;
538
539     p_aout->b_die = VLC_TRUE;
540     vlc_thread_join( p_aout );
541     p_aout->b_die = VLC_FALSE;
542
543     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
544     close( p_sys->i_fd );
545
546     free( p_sys );
547 }
548
549 /*****************************************************************************
550  * BufferDuration: buffer status query
551  *****************************************************************************
552  * This function returns the duration in microseconds of the current buffer.
553  *****************************************************************************/
554 static mtime_t BufferDuration( aout_instance_t * p_aout )
555 {
556     struct aout_sys_t * p_sys = p_aout->output.p_sys;
557     audio_buf_info audio_buf;
558     int i_bytes;
559
560     /* Fill the audio_buf_info structure:
561      * - fragstotal: total number of fragments allocated
562      * - fragsize: size of a fragment in bytes
563      * - bytes: available space in bytes (includes partially used fragments)
564      * Note! 'bytes' could be more than fragments*fragsize */
565     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
566
567     /* calculate number of available fragments (not partially used ones) */
568     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
569
570     /* Return the fragment duration */
571     return (mtime_t)i_bytes * 1000000
572             / p_aout->output.output.i_bytes_per_frame
573             / p_aout->output.output.i_rate
574             * p_aout->output.output.i_frame_length;
575 }
576
577 /*****************************************************************************
578  * OSSThread: asynchronous thread used to DMA the data to the device
579  *****************************************************************************/
580 static int OSSThread( aout_instance_t * p_aout )
581 {
582     struct aout_sys_t * p_sys = p_aout->output.p_sys;
583     mtime_t next_date = 0;
584
585     while ( !p_aout->b_die )
586     {
587         aout_buffer_t * p_buffer = NULL;
588         int i_tmp, i_size;
589         byte_t * p_bytes;
590
591         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
592         {
593             mtime_t buffered = BufferDuration( p_aout );
594
595             if( p_aout->output.p_sys->b_workaround_buggy_driver )
596             {
597 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
598                 /* Wait a bit - we don't want our buffer to be full */
599                 if( buffered > (p_aout->output.p_sys->max_buffer_duration
600                                 / i_fragstotal * (i_fragstotal - 1)) )
601                 {
602                     msleep((p_aout->output.p_sys->max_buffer_duration
603                                 / i_fragstotal ));
604                     buffered = BufferDuration( p_aout );
605                 }
606 #undef i_fragstotal
607             }
608
609             /* Next buffer will be played at mdate() + buffered */
610             p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
611                                               VLC_FALSE );
612
613             if( p_buffer == NULL &&
614                 buffered > ( p_aout->output.p_sys->max_buffer_duration
615                              / p_aout->output.p_sys->i_fragstotal ) )
616             {
617                 /* If we have at least a fragment full, then we can wait a
618                  * little and retry to get a new audio buffer instead of
619                  * playing a blank sample */
620                 msleep( ( p_aout->output.p_sys->max_buffer_duration
621                           / p_aout->output.p_sys->i_fragstotal / 2 ) );
622                 continue;
623             }
624         }
625         else
626         {
627             /* emu10k1 driver does not report Buffer Duration correctly in
628              * passthrough mode so we have to cheat */
629             if( !next_date )
630             {
631                 next_date = mdate();
632             }
633             else
634             {
635                 mtime_t delay = next_date - mdate();
636                 if( delay > AOUT_PTS_TOLERANCE )
637                 {
638                     msleep( delay / 2 );
639                 }
640             }
641
642             while( !p_aout->b_die && ! ( p_buffer =
643                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
644             {
645                 msleep( 1000 );
646                 next_date = mdate();
647             }
648         }
649
650         if ( p_buffer != NULL )
651         {
652             p_bytes = p_buffer->p_buffer;
653             i_size = p_buffer->i_nb_bytes;
654             /* This is theoretical ... we'll see next iteration whether
655              * we're drifting */
656             next_date += p_buffer->end_date - p_buffer->start_date;
657         }
658         else
659         {
660             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
661                       * p_aout->output.output.i_bytes_per_frame;
662             p_bytes = malloc( i_size );
663             memset( p_bytes, 0, i_size );
664             next_date = 0;
665         }
666
667         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
668
669         if( i_tmp < 0 )
670         {
671             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
672         }
673
674         if ( p_buffer != NULL )
675         {
676             aout_BufferFree( p_buffer );
677         }
678         else
679         {
680             free( p_bytes );
681         }
682     }
683
684     return VLC_SUCCESS;
685 }