]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
Fixed a major bug with 5.1 output and the OSS plug-in.
[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.54 2003/03/10 10:41:22 massiot 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 /dev/dsp module") );
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 */
278     p_sys->i_fd = open( psz_device, O_WRONLY | O_NDELAY );
279     if( p_sys->i_fd < 0 )
280     {
281         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
282         free( p_sys );
283         return VLC_EGENERIC;
284     }
285
286     /* if the opening was ok, put the device back in blocking mode */
287     fcntl( p_sys->i_fd, F_SETFL,
288             fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
289
290     free( psz_device );
291
292     p_aout->output.pf_play = Play;
293
294     if ( var_Type( p_aout, "audio-device" ) == 0 )
295     {
296         Probe( p_aout );
297     }
298
299     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
300     {
301         /* Probe() has failed. */
302         free( p_sys );
303         return VLC_EGENERIC;
304     }
305
306     if ( !strcmp( val.psz_string, N_("A/52 over S/PDIF") ) )
307     {
308         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
309     }
310     else if ( !strcmp( val.psz_string, N_("5.1") ) )
311     {
312         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
313         p_aout->output.output.i_physical_channels
314             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
315                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
316                | AOUT_CHAN_LFE;
317     }
318     else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
319     {
320         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
321         p_aout->output.output.i_physical_channels
322             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
323                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
324     }
325     else if ( !strcmp( val.psz_string, N_("Stereo") ) )
326     {
327         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
328         p_aout->output.output.i_physical_channels
329             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
330     }
331     else if ( !strcmp( val.psz_string, N_("Mono") ) )
332     {
333         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
334         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
335     }
336     else
337     {
338         /* This should not happen ! */
339         msg_Err( p_aout, "internal: can't find audio-device (%s)",
340                  val.psz_string );
341         free( p_sys );
342         free( val.psz_string );
343         return VLC_EGENERIC;
344     }
345     free( val.psz_string );
346
347     val.b_bool = VLC_TRUE;
348     var_Set( p_aout, "intf-change", val );
349
350     /* Reset the DSP device */
351     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
352     {
353         msg_Err( p_aout, "cannot reset OSS audio device" );
354         close( p_sys->i_fd );
355         free( p_sys );
356         return VLC_EGENERIC;
357     }
358
359     /* Set the output format */
360     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
361     {
362         int i_format = AFMT_AC3;
363
364         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
365              || i_format != AFMT_AC3 )
366         {
367             msg_Err( p_aout, "cannot reset OSS audio device" );
368             close( p_sys->i_fd );
369             free( p_sys );
370             return VLC_EGENERIC;
371         }
372
373         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
374         p_aout->output.i_nb_samples = A52_FRAME_NB;
375         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
376         p_aout->output.output.i_frame_length = A52_FRAME_NB;
377
378         aout_VolumeNoneInit( p_aout );
379     }
380
381     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
382     {
383         unsigned int i_format = AFMT_S16_NE;
384         unsigned int i_frame_size, i_fragments;
385         unsigned int i_rate;
386         unsigned int i_nb_channels;
387         audio_buf_info audio_buf;
388
389         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
390         {
391             msg_Err( p_aout, "cannot set audio output format" );
392             close( p_sys->i_fd );
393             free( p_sys );
394             return VLC_EGENERIC;
395         }
396
397         switch ( i_format )
398         {
399         case AFMT_U8:
400             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
401             break;
402         case AFMT_S8:
403             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
404             break;
405         case AFMT_U16_LE:
406             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
407             break;
408         case AFMT_S16_LE:
409             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
410             break;
411         case AFMT_U16_BE:
412             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
413             break;
414         case AFMT_S16_BE:
415             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
416             break;
417         default:
418             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
419                      i_format );
420             close( p_sys->i_fd );
421             free( p_sys );
422             return VLC_EGENERIC;
423         }
424
425         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
426
427         /* Set the number of channels */
428         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
429             i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
430         {
431             msg_Err( p_aout, "cannot set number of audio channels (%s)",
432                      aout_FormatPrintChannels( &p_aout->output.output) );
433             close( p_sys->i_fd );
434             free( p_sys );
435             return VLC_EGENERIC;
436         }
437
438         /* Set the output rate */
439         i_rate = p_aout->output.output.i_rate;
440         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
441         {
442             msg_Err( p_aout, "cannot set audio output rate (%i)",
443                              p_aout->output.output.i_rate );
444             close( p_sys->i_fd );
445             free( p_sys );
446             return VLC_EGENERIC;
447         }
448
449         if( i_rate != p_aout->output.output.i_rate )
450         {
451             p_aout->output.output.i_rate = i_rate;
452         }
453
454         /* Set the fragment size */
455         aout_FormatPrepare( &p_aout->output.output );
456
457         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
458          *                              1 << yyyy   is fragsize */
459         i_fragments = 0;
460         i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
461         while( i_frame_size >>= 1 )
462         {
463             ++i_fragments;
464         }
465         i_fragments |= FRAME_COUNT << 16;
466         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
467         {
468             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
469         }
470
471         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
472         {
473             msg_Err( p_aout, "cannot get fragment size" );
474             close( p_sys->i_fd );
475             free( p_sys );
476             return VLC_EGENERIC;
477         }
478         else
479         {
480             /* Number of fragments actually allocated */
481             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
482
483             /* Maximum duration the soundcard's buffer can hold */
484             p_aout->output.p_sys->max_buffer_duration =
485                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
486                 / p_aout->output.output.i_bytes_per_frame
487                 / p_aout->output.output.i_rate
488                 * p_aout->output.output.i_frame_length;
489
490             p_aout->output.i_nb_samples = audio_buf.fragsize /
491                 p_aout->output.output.i_bytes_per_frame;
492         }
493
494         aout_VolumeSoftInit( p_aout );
495     }
496
497     p_aout->output.p_sys->b_workaround_buggy_driver =
498         config_GetInt( p_aout, "oss-buggy" );
499
500     /* Create OSS thread and wait for its readiness. */
501     if( vlc_thread_create( p_aout, "aout", OSSThread,
502                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
503     {
504         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
505         close( p_sys->i_fd );
506         free( p_sys );
507         return VLC_ETHREAD;
508     }
509
510     return VLC_SUCCESS;
511 }
512
513 /*****************************************************************************
514  * Play: nothing to do
515  *****************************************************************************/
516 static void Play( aout_instance_t *p_aout )
517 {
518 }
519
520 /*****************************************************************************
521  * Close: close the dsp audio device
522  *****************************************************************************/
523 static void Close( vlc_object_t * p_this )
524 {
525     aout_instance_t *p_aout = (aout_instance_t *)p_this;
526     struct aout_sys_t * p_sys = p_aout->output.p_sys;
527
528     p_aout->b_die = VLC_TRUE;
529     vlc_thread_join( p_aout );
530     p_aout->b_die = VLC_FALSE;
531
532     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
533     close( p_sys->i_fd );
534
535     free( p_sys );
536 }
537
538 /*****************************************************************************
539  * BufferDuration: buffer status query
540  *****************************************************************************
541  * This function returns the duration in microseconds of the current buffer.
542  *****************************************************************************/
543 static mtime_t BufferDuration( aout_instance_t * p_aout )
544 {
545     struct aout_sys_t * p_sys = p_aout->output.p_sys;
546     audio_buf_info audio_buf;
547     int i_bytes;
548
549     /* Fill the audio_buf_info structure:
550      * - fragstotal: total number of fragments allocated
551      * - fragsize: size of a fragment in bytes
552      * - bytes: available space in bytes (includes partially used fragments)
553      * Note! 'bytes' could be more than fragments*fragsize */
554     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
555
556     /* calculate number of available fragments (not partially used ones) */
557     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
558
559     /* Return the fragment duration */
560     return (mtime_t)i_bytes * 1000000
561             / p_aout->output.output.i_bytes_per_frame
562             / p_aout->output.output.i_rate
563             * p_aout->output.output.i_frame_length;
564 }
565
566 /*****************************************************************************
567  * OSSThread: asynchronous thread used to DMA the data to the device
568  *****************************************************************************/
569 static int OSSThread( aout_instance_t * p_aout )
570 {
571     struct aout_sys_t * p_sys = p_aout->output.p_sys;
572     mtime_t next_date = 0;
573
574     while ( !p_aout->b_die )
575     {
576         aout_buffer_t * p_buffer = NULL;
577         int i_tmp, i_size;
578         byte_t * p_bytes;
579
580         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
581         {
582             mtime_t buffered = BufferDuration( p_aout );
583
584             if( p_aout->output.p_sys->b_workaround_buggy_driver )
585             {
586 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
587                 /* Wait a bit - we don't want our buffer to be full */
588                 if( buffered > (p_aout->output.p_sys->max_buffer_duration
589                                 / i_fragstotal * (i_fragstotal - 1)) )
590                 {
591                     msleep((p_aout->output.p_sys->max_buffer_duration
592                                 / i_fragstotal ));
593                     buffered = BufferDuration( p_aout );
594                 }
595 #undef i_fragstotal
596             }
597
598             /* Next buffer will be played at mdate() + buffered */
599             p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
600                                               VLC_FALSE );
601
602             if( p_buffer == NULL &&
603                 buffered > ( p_aout->output.p_sys->max_buffer_duration
604                              / p_aout->output.p_sys->i_fragstotal ) )
605             {
606                 /* If we have at least a fragment full, then we can wait a
607                  * little and retry to get a new audio buffer instead of
608                  * playing a blank sample */
609                 msleep( ( p_aout->output.p_sys->max_buffer_duration
610                           / p_aout->output.p_sys->i_fragstotal / 2 ) );
611                 continue;
612             }
613         }
614         else
615         {
616             /* emu10k1 driver does not report Buffer Duration correctly in
617              * passthrough mode so we have to cheat */
618             if( !next_date )
619             {
620                 next_date = mdate();
621             }
622             else
623             {
624                 mtime_t delay = next_date - mdate();
625                 if( delay > AOUT_PTS_TOLERANCE )
626                 {
627                     msleep( delay / 2 );
628                 }
629             }
630
631             while( !p_aout->b_die && ! ( p_buffer =
632                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
633             {
634                 msleep( 1000 );
635                 next_date = mdate();
636             }
637         }
638
639         if ( p_buffer != NULL )
640         {
641             p_bytes = p_buffer->p_buffer;
642             i_size = p_buffer->i_nb_bytes;
643             /* This is theoretical ... we'll see next iteration whether
644              * we're drifting */
645             next_date += p_buffer->end_date - p_buffer->start_date;
646         }
647         else
648         {
649             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
650                       * p_aout->output.output.i_bytes_per_frame;
651             p_bytes = malloc( i_size );
652             memset( p_bytes, 0, i_size );
653             next_date = 0;
654         }
655
656         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
657
658         if( i_tmp < 0 )
659         {
660             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
661         }
662
663         if ( p_buffer != NULL )
664         {
665             aout_BufferFree( p_buffer );
666         }
667         else
668         {
669             free( p_bytes );
670         }
671     }
672
673     return VLC_SUCCESS;
674 }