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