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