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