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