]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
OpenSL ES audio output for Android
[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     vlc_thread_t thread;
77 };
78
79 /* This must be a power of 2. */
80 #define FRAME_SIZE 1024
81 #define FRAME_COUNT 32
82
83 /*****************************************************************************
84  * Local prototypes
85  *****************************************************************************/
86 static int  Open         ( vlc_object_t * );
87 static void Close        ( vlc_object_t * );
88
89 static void Play         ( aout_instance_t * );
90 static void* OSSThread   ( void * );
91
92 static mtime_t BufferDuration( aout_instance_t * p_aout );
93
94 /*****************************************************************************
95  * Module descriptor
96  *****************************************************************************/
97 vlc_module_begin ()
98     set_shortname( "OSS" )
99     set_description( N_("Open Sound System") )
100
101     set_category( CAT_AUDIO )
102     set_subcategory( SUBCAT_AUDIO_AOUT )
103     add_loadfile( "oss-audio-device", "/dev/dsp",
104                   N_("OSS DSP device"), NULL, false )
105         add_deprecated_alias( "dspdev" )   /* deprecated since 0.9.3 */
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( aout_instance_t * p_aout )
116 {
117     struct aout_sys_t * p_sys = p_aout->output.p_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->output.output ) > 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->output.output.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->output.output.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->output.output.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_NON_LINEAR( &p_aout->output.output ) )
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     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
252                      NULL );
253 }
254
255 /*****************************************************************************
256  * Open: open the audio device (the digital sound processor)
257  *****************************************************************************
258  * This function opens the DSP as a usual non-blocking write-only file, and
259  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
260  *****************************************************************************/
261 static int Open( vlc_object_t *p_this )
262 {
263     aout_instance_t * p_aout = (aout_instance_t *)p_this;
264     struct aout_sys_t * p_sys;
265     char * psz_device;
266     vlc_value_t val;
267
268     /* Allocate structure */
269     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
270     if( p_sys == NULL )
271         return VLC_ENOMEM;
272
273     /* Get device name */
274     if( (psz_device = var_InheritString( p_aout, "oss-audio-device" )) == NULL )
275     {
276         msg_Err( p_aout, "no audio device specified (maybe /dev/dsp?)" );
277         free( p_sys );
278         return VLC_EGENERIC;
279     }
280
281     /* Open the sound device in non-blocking mode, because ALSA's OSS
282      * emulation and some broken OSS drivers would make a blocking call
283      * wait forever until the device is available. Since this breaks the
284      * OSS spec, we immediately put it back to blocking mode if the
285      * operation was successful. */
286     p_sys->i_fd = vlc_open( psz_device, O_WRONLY | O_NDELAY );
287     if( p_sys->i_fd < 0 )
288     {
289         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
290         free( psz_device );
291         free( p_sys );
292         return VLC_EGENERIC;
293     }
294
295     /* if the opening was ok, put the device back in blocking mode */
296     fcntl( p_sys->i_fd, F_SETFL,
297             fcntl( p_sys->i_fd, F_GETFL ) &~ FNDELAY );
298
299     free( psz_device );
300
301     p_aout->output.pf_play = Play;
302
303     if ( var_Type( p_aout, "audio-device" ) == 0 )
304     {
305         Probe( p_aout );
306     }
307
308     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
309     {
310         /* Probe() has failed. */
311         close( p_sys->i_fd );
312         free( p_sys );
313         return VLC_EGENERIC;
314     }
315
316     if ( val.i_int == AOUT_VAR_SPDIF )
317     {
318         p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
319     }
320     else if ( val.i_int == AOUT_VAR_5_1 )
321     {
322         p_aout->output.output.i_format = VLC_CODEC_S16N;
323         p_aout->output.output.i_physical_channels
324             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
325                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
326                | AOUT_CHAN_LFE;
327     }
328     else if ( val.i_int == AOUT_VAR_2F2R )
329     {
330         p_aout->output.output.i_format = VLC_CODEC_S16N;
331         p_aout->output.output.i_physical_channels
332             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
333                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
334     }
335     else if ( val.i_int == AOUT_VAR_STEREO )
336     {
337         p_aout->output.output.i_format = VLC_CODEC_S16N;
338         p_aout->output.output.i_physical_channels
339             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
340     }
341     else if ( val.i_int == AOUT_VAR_MONO )
342     {
343         p_aout->output.output.i_format = VLC_CODEC_S16N;
344         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
345     }
346     else
347     {
348         /* This should not happen ! */
349         msg_Err( p_aout, "internal: can't find audio-device (%"PRId64")",
350                  val.i_int );
351         close( p_sys->i_fd );
352         free( p_sys );
353         return VLC_EGENERIC;
354     }
355
356     var_TriggerCallback( p_aout, "intf-change" );
357
358     /* Reset the DSP device */
359     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
360     {
361         msg_Err( p_aout, "cannot reset OSS audio device" );
362         close( p_sys->i_fd );
363         free( p_sys );
364         return VLC_EGENERIC;
365     }
366
367     /* Set the output format */
368     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
369     {
370         int i_format = AFMT_AC3;
371
372         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
373              || i_format != AFMT_AC3 )
374         {
375             msg_Err( p_aout, "cannot reset OSS audio device" );
376             close( p_sys->i_fd );
377             free( p_sys );
378             return VLC_EGENERIC;
379         }
380
381         p_aout->output.output.i_format = VLC_CODEC_SPDIFL;
382         p_aout->output.i_nb_samples = A52_FRAME_NB;
383         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
384         p_aout->output.output.i_frame_length = A52_FRAME_NB;
385
386         aout_VolumeNoneInit( p_aout );
387     }
388
389     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
390     {
391         unsigned int i_format = AFMT_S16_NE;
392         unsigned int i_frame_size, i_fragments;
393         unsigned int i_rate;
394         unsigned int i_nb_channels;
395         audio_buf_info audio_buf;
396
397         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
398         {
399             msg_Err( p_aout, "cannot set audio output format" );
400             close( p_sys->i_fd );
401             free( p_sys );
402             return VLC_EGENERIC;
403         }
404
405         switch ( i_format )
406         {
407         case AFMT_U8:
408             p_aout->output.output.i_format = VLC_CODEC_U8;
409             break;
410         case AFMT_S8:
411             p_aout->output.output.i_format = VLC_CODEC_S8;
412             break;
413         case AFMT_U16_LE:
414             p_aout->output.output.i_format = VLC_CODEC_U16L;
415             break;
416         case AFMT_S16_LE:
417             p_aout->output.output.i_format = VLC_CODEC_S16L;
418             break;
419         case AFMT_U16_BE:
420             p_aout->output.output.i_format = VLC_CODEC_U16B;
421             break;
422         case AFMT_S16_BE:
423             p_aout->output.output.i_format = VLC_CODEC_S16B;
424             break;
425         default:
426             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
427                      i_format );
428             close( p_sys->i_fd );
429             free( p_sys );
430             return VLC_EGENERIC;
431         }
432
433         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
434
435         /* Set the number of channels */
436         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
437             i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
438         {
439             msg_Err( p_aout, "cannot set number of audio channels (%s)",
440                      aout_FormatPrintChannels( &p_aout->output.output) );
441             close( p_sys->i_fd );
442             free( p_sys );
443             return VLC_EGENERIC;
444         }
445
446         /* Set the output rate */
447         i_rate = p_aout->output.output.i_rate;
448         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
449         {
450             msg_Err( p_aout, "cannot set audio output rate (%i)",
451                              p_aout->output.output.i_rate );
452             close( p_sys->i_fd );
453             free( p_sys );
454             return VLC_EGENERIC;
455         }
456
457         if( i_rate != p_aout->output.output.i_rate )
458         {
459             p_aout->output.output.i_rate = i_rate;
460         }
461
462         /* Set the fragment size */
463         aout_FormatPrepare( &p_aout->output.output );
464
465         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
466          *                              1 << yyyy   is fragsize */
467         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;
468         i_fragments = 4;
469         while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
470         {
471             ++i_fragments;
472         }
473         i_fragments |= FRAME_COUNT << 16;
474         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
475         {
476             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
477         }
478
479         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
480         {
481             msg_Err( p_aout, "cannot get fragment size" );
482             close( p_sys->i_fd );
483             free( p_sys );
484             return VLC_EGENERIC;
485         }
486         else
487         {
488             /* Number of fragments actually allocated */
489             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
490
491             /* Maximum duration the soundcard's buffer can hold */
492             p_aout->output.p_sys->max_buffer_duration =
493                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
494                 / p_aout->output.output.i_bytes_per_frame
495                 / p_aout->output.output.i_rate
496                 * p_aout->output.output.i_frame_length;
497
498             p_aout->output.i_nb_samples = audio_buf.fragsize /
499                 p_aout->output.output.i_bytes_per_frame;
500         }
501
502         aout_VolumeSoftInit( p_aout );
503     }
504
505     /* Create OSS thread and wait for its readiness. */
506     if( vlc_clone( &p_sys->thread, OSSThread, p_aout,
507                    VLC_THREAD_PRIORITY_OUTPUT ) )
508     {
509         msg_Err( p_aout, "cannot create OSS thread (%m)" );
510         close( p_sys->i_fd );
511         free( p_sys );
512         return VLC_ENOMEM;
513     }
514
515     return VLC_SUCCESS;
516 }
517
518 /*****************************************************************************
519  * Play: nothing to do
520  *****************************************************************************/
521 static void Play( aout_instance_t *p_aout )
522 {
523     VLC_UNUSED(p_aout);
524 }
525
526 /*****************************************************************************
527  * Close: close the DSP audio device
528  *****************************************************************************/
529 static void Close( vlc_object_t * p_this )
530 {
531     aout_instance_t *p_aout = (aout_instance_t *)p_this;
532     struct aout_sys_t * p_sys = p_aout->output.p_sys;
533
534     vlc_cancel( p_sys->thread );
535     vlc_join( p_sys->thread, NULL );
536     p_aout->b_die = false;
537
538     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
539     close( p_sys->i_fd );
540
541     free( p_sys );
542 }
543
544 /*****************************************************************************
545  * BufferDuration: buffer status query
546  *****************************************************************************
547  * This function returns the duration in microseconds of the current buffer.
548  *****************************************************************************/
549 static mtime_t BufferDuration( aout_instance_t * p_aout )
550 {
551     struct aout_sys_t * p_sys = p_aout->output.p_sys;
552     audio_buf_info audio_buf;
553     int i_bytes;
554
555     /* Fill the audio_buf_info structure:
556      * - fragstotal: total number of fragments allocated
557      * - fragsize: size of a fragment in bytes
558      * - bytes: available space in bytes (includes partially used fragments)
559      * Note! 'bytes' could be more than fragments*fragsize */
560     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
561
562     /* calculate number of available fragments (not partially used ones) */
563     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
564
565     /* Return the fragment duration */
566     return (mtime_t)i_bytes * 1000000
567             / p_aout->output.output.i_bytes_per_frame
568             / p_aout->output.output.i_rate
569             * p_aout->output.output.i_frame_length;
570 }
571
572 typedef struct
573 {
574     aout_buffer_t *p_buffer;
575     void          *p_bytes;
576 } oss_thread_ctx_t;
577
578 static void OSSThreadCleanup( void *data )
579 {
580     oss_thread_ctx_t *p_ctx = data;
581     if( p_ctx->p_buffer )
582         aout_BufferFree( p_ctx->p_buffer );
583     else
584         free( p_ctx->p_bytes );
585 }
586
587 /*****************************************************************************
588  * OSSThread: asynchronous thread used to DMA the data to the device
589  *****************************************************************************/
590 static void* OSSThread( void *obj )
591 {
592     aout_instance_t * p_aout = (aout_instance_t*)obj;
593     struct aout_sys_t * p_sys = p_aout->output.p_sys;
594     mtime_t next_date = 0;
595
596     for( ;; )
597     {
598         aout_buffer_t * p_buffer = NULL;
599
600         int canc = vlc_savecancel ();
601         if ( p_aout->output.output.i_format != VLC_CODEC_SPDIFL )
602         {
603             mtime_t buffered = BufferDuration( p_aout );
604
605             /* Next buffer will be played at mdate() + buffered */
606             p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
607                                               false );
608
609             if( p_buffer == NULL &&
610                 buffered > ( p_aout->output.p_sys->max_buffer_duration
611                              / p_aout->output.p_sys->i_fragstotal ) )
612             {
613                 vlc_restorecancel (canc);
614                 /* If we have at least a fragment full, then we can wait a
615                  * little and retry to get a new audio buffer instead of
616                  * playing a blank sample */
617                 msleep( ( p_aout->output.p_sys->max_buffer_duration
618                           / p_aout->output.p_sys->i_fragstotal / 2 ) );
619                 continue;
620             }
621         }
622         else
623         {
624             vlc_restorecancel (canc);
625
626             /* emu10k1 driver does not report Buffer Duration correctly in
627              * passthrough mode so we have to cheat */
628             if( !next_date )
629             {
630                 next_date = mdate();
631             }
632             else
633             {
634                 mtime_t delay = next_date - mdate();
635                 if( delay > AOUT_PTS_TOLERANCE )
636                 {
637                     msleep( delay / 2 );
638                 }
639             }
640
641             for( ;; )
642             {
643                 canc = vlc_savecancel ();
644                 p_buffer = aout_OutputNextBuffer( p_aout, next_date, true );
645                 if ( p_buffer )
646                     break;
647                 vlc_restorecancel (canc);
648
649                 msleep( VLC_HARD_MIN_SLEEP );
650                 next_date = mdate();
651             }
652         }
653
654         uint8_t * p_bytes;
655         int i_size;
656         if ( p_buffer != NULL )
657         {
658             p_bytes = p_buffer->p_buffer;
659             i_size = p_buffer->i_buffer;
660             /* This is theoretical ... we'll see next iteration whether
661              * we're drifting */
662             next_date += p_buffer->i_length;
663         }
664         else
665         {
666             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
667                       * p_aout->output.output.i_bytes_per_frame;
668             p_bytes = malloc( i_size );
669             memset( p_bytes, 0, i_size );
670             next_date = 0;
671         }
672
673         oss_thread_ctx_t ctx = {
674             .p_buffer = p_buffer,
675             .p_bytes  = p_bytes,
676         };
677
678         vlc_cleanup_push( OSSThreadCleanup, &ctx );
679         vlc_restorecancel( canc );
680
681         int i_tmp = write( p_sys->i_fd, p_bytes, i_size );
682
683         if( i_tmp < 0 )
684         {
685             msg_Err( p_aout, "write failed (%m)" );
686         }
687         vlc_cleanup_run();
688     }
689
690     return NULL;
691 }