]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
utf8_* -> vlc_* (sed roxxors)
[vlc] / modules / audio_output / oss.c
1 /*****************************************************************************
2  * oss.c : OSS /dev/dsp module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2002 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /*****************************************************************************
27  * Preamble
28  *****************************************************************************/
29
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
33
34 #include <fcntl.h>                                       /* open(), O_WRONLY */
35 #include <sys/ioctl.h>                                            /* ioctl() */
36 #include <unistd.h>                                      /* write(), close() */
37
38 #include <vlc_common.h>
39 #include <vlc_plugin.h>
40 #include <vlc_fs.h>
41
42 #include <vlc_aout.h>
43
44 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
45  * SNDCTL_DSP_GETOSPACE */
46 #ifdef HAVE_SOUNDCARD_H
47 #   include <soundcard.h>
48 #elif defined( HAVE_SYS_SOUNDCARD_H )
49 #   include <sys/soundcard.h>
50 #endif
51
52 /* Patches for ignorant OSS versions */
53 #ifndef AFMT_AC3
54 #   define AFMT_AC3     0x00000400        /* Dolby Digital AC3 */
55 #endif
56
57 #ifndef AFMT_S16_NE
58 #   ifdef WORDS_BIGENDIAN
59 #       define AFMT_S16_NE AFMT_S16_BE
60 #   else
61 #       define AFMT_S16_NE AFMT_S16_LE
62 #   endif
63 #endif
64
65 /*****************************************************************************
66  * aout_sys_t: OSS audio output method descriptor
67  *****************************************************************************
68  * This structure is part of the audio output thread descriptor.
69  * It describes the DSP specific properties of an audio device.
70  *****************************************************************************/
71 struct aout_sys_t
72 {
73     int i_fd;
74     int b_workaround_buggy_driver;
75     int i_fragstotal;
76     mtime_t max_buffer_duration;
77 };
78
79 /* This must be a power of 2. */
80 #define FRAME_SIZE 1024
81 #define FRAME_COUNT 32
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int  Open         ( vlc_object_t * );
87 static void Close        ( vlc_object_t * );
88
89 static void Play         ( aout_instance_t * );
90 static void* OSSThread   ( vlc_object_t * );
91
92 static mtime_t BufferDuration( aout_instance_t * p_aout );
93
94 /*****************************************************************************
95  * Module descriptor
96  *****************************************************************************/
97 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
98 #define BUGGY_LONGTEXT N_( \
99     "Some buggy OSS drivers just don't like when their internal buffers " \
100     "are completely filled (the sound gets heavily hashed). If you have one " \
101     "of these drivers, then you need to enable this option." )
102
103 vlc_module_begin ()
104     set_shortname( "OSS" )
105     set_description( N_("UNIX OSS audio output") )
106
107     set_category( CAT_AUDIO )
108     set_subcategory( SUBCAT_AUDIO_AOUT )
109     add_file( "oss-audio-device", "/dev/dsp", aout_FindAndRestart,
110               N_("OSS DSP device"), NULL, false )
111         add_deprecated_alias( "dspdev" )   /* deprecated since 0.9.3 */
112     add_bool( "oss-buggy", false, NULL, BUGGY_TEXT, BUGGY_LONGTEXT, true )
113
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, 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 = (char*) "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 = _("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 = _("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 = _("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 = _("A/52 over S/PDIF");
247             var_Change( p_aout, "audio-device",
248                         VLC_VAR_ADDCHOICE, &val, &text );
249             if( var_InheritInteger( p_aout, "spdif" ) )
250                 var_Set( p_aout, "audio-device", val );
251         }
252         else if( var_InheritInteger( p_aout, "spdif" ) )
253         {
254             msg_Warn( p_aout, "S/PDIF not supported by card" );
255         }
256     }
257
258     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
259                      NULL );
260 }
261
262 /*****************************************************************************
263  * Open: open the audio device (the digital sound processor)
264  *****************************************************************************
265  * This function opens the DSP as a usual non-blocking write-only file, and
266  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
267  *****************************************************************************/
268 static int Open( vlc_object_t *p_this )
269 {
270     aout_instance_t * p_aout = (aout_instance_t *)p_this;
271     struct aout_sys_t * p_sys;
272     char * psz_device;
273     vlc_value_t val;
274
275     /* Allocate structure */
276     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
277     if( p_sys == NULL )
278         return VLC_ENOMEM;
279
280     /* Get device name */
281     if( (psz_device = var_InheritString( p_aout, "oss-audio-device" )) == NULL )
282     {
283         msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
284         free( p_sys );
285         return VLC_EGENERIC;
286     }
287
288     /* Open the sound device in non-blocking mode, because ALSA's OSS
289      * emulation and some broken OSS drivers would make a blocking call
290      * wait forever until the device is available. Since this breaks the
291      * OSS spec, we immediately put it back to blocking mode if the
292      * operation was successful. */
293     p_sys->i_fd = vlc_open( psz_device, O_WRONLY | O_NDELAY );
294     if( p_sys->i_fd < 0 )
295     {
296         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
297         free( psz_device );
298         free( p_sys );
299         return VLC_EGENERIC;
300     }
301
302     /* if the opening was ok, put the device back in blocking mode */
303     fcntl( p_sys->i_fd, F_SETFL,
304             fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
305
306     free( psz_device );
307
308     p_aout->output.pf_play = Play;
309
310     if ( var_Type( p_aout, "audio-device" ) == 0 )
311     {
312         Probe( p_aout );
313     }
314
315     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
316     {
317         /* Probe() has failed. */
318         close( p_sys->i_fd );
319         free( p_sys );
320         return VLC_EGENERIC;
321     }
322
323     if ( val.i_int == AOUT_VAR_SPDIF )
324     {
325         p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
326     }
327     else if ( val.i_int == AOUT_VAR_5_1 )
328     {
329         p_aout->output.output.i_format = VLC_CODEC_S16N;
330         p_aout->output.output.i_physical_channels
331             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
332                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
333                | AOUT_CHAN_LFE;
334     }
335     else if ( val.i_int == AOUT_VAR_2F2R )
336     {
337         p_aout->output.output.i_format = VLC_CODEC_S16N;
338         p_aout->output.output.i_physical_channels
339             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
340                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
341     }
342     else if ( val.i_int == AOUT_VAR_STEREO )
343     {
344         p_aout->output.output.i_format = VLC_CODEC_S16N;
345         p_aout->output.output.i_physical_channels
346             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
347     }
348     else if ( val.i_int == AOUT_VAR_MONO )
349     {
350         p_aout->output.output.i_format = VLC_CODEC_S16N;
351         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
352     }
353     else
354     {
355         /* This should not happen ! */
356         msg_Err( p_aout, "internal: can't find audio-device (%i)", val.i_int );
357         close( p_sys->i_fd );
358         free( p_sys );
359         return VLC_EGENERIC;
360     }
361
362     var_SetBool( p_aout, "intf-change", true );
363
364     /* Reset the DSP device */
365     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
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     /* Set the output format */
374     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
375     {
376         int i_format = AFMT_AC3;
377
378         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
379              || i_format != AFMT_AC3 )
380         {
381             msg_Err( p_aout, "cannot reset OSS audio device" );
382             close( p_sys->i_fd );
383             free( p_sys );
384             return VLC_EGENERIC;
385         }
386
387         p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
388         p_aout->output.i_nb_samples = A52_FRAME_NB;
389         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
390         p_aout->output.output.i_frame_length = A52_FRAME_NB;
391
392         aout_VolumeNoneInit( p_aout );
393     }
394
395     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
396     {
397         unsigned int i_format = AFMT_S16_NE;
398         unsigned int i_frame_size, i_fragments;
399         unsigned int i_rate;
400         unsigned int i_nb_channels;
401         audio_buf_info audio_buf;
402
403         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
404         {
405             msg_Err( p_aout, "cannot set audio output format" );
406             close( p_sys->i_fd );
407             free( p_sys );
408             return VLC_EGENERIC;
409         }
410
411         switch ( i_format )
412         {
413         case AFMT_U8:
414             p_aout->output.output.i_format = VLC_CODEC_U8;
415             break;
416         case AFMT_S8:
417             p_aout->output.output.i_format = VLC_CODEC_S8;
418             break;
419         case AFMT_U16_LE:
420             p_aout->output.output.i_format = VLC_CODEC_U16L;
421             break;
422         case AFMT_S16_LE:
423             p_aout->output.output.i_format = VLC_CODEC_S16L;
424             break;
425         case AFMT_U16_BE:
426             p_aout->output.output.i_format = VLC_CODEC_U16B;
427             break;
428         case AFMT_S16_BE:
429             p_aout->output.output.i_format = VLC_CODEC_S16B;
430             break;
431         default:
432             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
433                      i_format );
434             close( p_sys->i_fd );
435             free( p_sys );
436             return VLC_EGENERIC;
437         }
438
439         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
440
441         /* Set the number of channels */
442         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
443             i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
444         {
445             msg_Err( p_aout, "cannot set number of audio channels (%s)",
446                      aout_FormatPrintChannels( &p_aout->output.output) );
447             close( p_sys->i_fd );
448             free( p_sys );
449             return VLC_EGENERIC;
450         }
451
452         /* Set the output rate */
453         i_rate = p_aout->output.output.i_rate;
454         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
455         {
456             msg_Err( p_aout, "cannot set audio output rate (%i)",
457                              p_aout->output.output.i_rate );
458             close( p_sys->i_fd );
459             free( p_sys );
460             return VLC_EGENERIC;
461         }
462
463         if( i_rate != p_aout->output.output.i_rate )
464         {
465             p_aout->output.output.i_rate = i_rate;
466         }
467
468         /* Set the fragment size */
469         aout_FormatPrepare( &p_aout->output.output );
470
471         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
472          *                              1 << yyyy   is fragsize */
473         i_frame_size = ((uint64_t)p_aout->output.output.i_bytes_per_frame * p_aout->output.output.i_rate * 65536) / (48000 * 2 * 2) / FRAME_COUNT;
474         i_fragments = 4;
475         while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
476         {
477             ++i_fragments;
478         }
479         i_fragments |= FRAME_COUNT << 16;
480         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
481         {
482             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
483         }
484
485         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
486         {
487             msg_Err( p_aout, "cannot get fragment size" );
488             close( p_sys->i_fd );
489             free( p_sys );
490             return VLC_EGENERIC;
491         }
492         else
493         {
494             /* Number of fragments actually allocated */
495             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
496
497             /* Maximum duration the soundcard's buffer can hold */
498             p_aout->output.p_sys->max_buffer_duration =
499                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
500                 / p_aout->output.output.i_bytes_per_frame
501                 / p_aout->output.output.i_rate
502                 * p_aout->output.output.i_frame_length;
503
504             p_aout->output.i_nb_samples = audio_buf.fragsize /
505                 p_aout->output.output.i_bytes_per_frame;
506         }
507
508         aout_VolumeSoftInit( p_aout );
509     }
510
511     p_aout->output.p_sys->b_workaround_buggy_driver =
512         var_InheritInteger( p_aout, "oss-buggy" );
513
514     /* Create OSS thread and wait for its readiness. */
515     if( vlc_thread_create( p_aout, "aout", OSSThread,
516                            VLC_THREAD_PRIORITY_OUTPUT ) )
517     {
518         msg_Err( p_aout, "cannot create OSS thread (%m)" );
519         close( p_sys->i_fd );
520         free( p_sys );
521         return VLC_ENOMEM;
522     }
523
524     return VLC_SUCCESS;
525 }
526
527 /*****************************************************************************
528  * Play: nothing to do
529  *****************************************************************************/
530 static void Play( aout_instance_t *p_aout )
531 {
532     VLC_UNUSED(p_aout);
533 }
534
535 /*****************************************************************************
536  * Close: close the DSP audio device
537  *****************************************************************************/
538 static void Close( vlc_object_t * p_this )
539 {
540     aout_instance_t *p_aout = (aout_instance_t *)p_this;
541     struct aout_sys_t * p_sys = p_aout->output.p_sys;
542
543     vlc_object_kill( p_aout );
544     vlc_thread_join( p_aout );
545     p_aout->b_die = false;
546
547     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
548     close( p_sys->i_fd );
549
550     free( p_sys );
551 }
552
553 /*****************************************************************************
554  * BufferDuration: buffer status query
555  *****************************************************************************
556  * This function returns the duration in microseconds of the current buffer.
557  *****************************************************************************/
558 static mtime_t BufferDuration( aout_instance_t * p_aout )
559 {
560     struct aout_sys_t * p_sys = p_aout->output.p_sys;
561     audio_buf_info audio_buf;
562     int i_bytes;
563
564     /* Fill the audio_buf_info structure:
565      * - fragstotal: total number of fragments allocated
566      * - fragsize: size of a fragment in bytes
567      * - bytes: available space in bytes (includes partially used fragments)
568      * Note! 'bytes' could be more than fragments*fragsize */
569     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
570
571     /* calculate number of available fragments (not partially used ones) */
572     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
573
574     /* Return the fragment duration */
575     return (mtime_t)i_bytes * 1000000
576             / p_aout->output.output.i_bytes_per_frame
577             / p_aout->output.output.i_rate
578             * p_aout->output.output.i_frame_length;
579 }
580
581 /*****************************************************************************
582  * OSSThread: asynchronous thread used to DMA the data to the device
583  *****************************************************************************/
584 static void* OSSThread( vlc_object_t *p_this )
585 {
586     aout_instance_t * p_aout = (aout_instance_t*)p_this;
587     struct aout_sys_t * p_sys = p_aout->output.p_sys;
588     mtime_t next_date = 0;
589     int canc = vlc_savecancel ();
590
591     while ( vlc_object_alive (p_aout) )
592     {
593         aout_buffer_t * p_buffer = NULL;
594         int i_tmp, i_size;
595         uint8_t * p_bytes;
596
597         if ( p_aout->output.output.i_format != VLC_CODEC_SPDIFL )
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                                               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( vlc_object_alive (p_aout) && ! ( p_buffer =
649                 aout_OutputNextBuffer( p_aout, next_date, true ) ) )
650             {
651                 msleep( VLC_HARD_MIN_SLEEP );
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_buffer;
660             /* This is theoretical ... we'll see next iteration whether
661              * we're drifting */
662             next_date += p_buffer->i_length;
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 (%m)" );
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     vlc_restorecancel (canc);
691     return NULL;
692 }