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