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