]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
aout: move packet FIFO management to plugins that need it
[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         add_deprecated_alias( "dspdev" )   /* deprecated since 0.9.3 */
107
108     set_capability( "audio output", 100 )
109     add_shortcut( "oss" )
110     set_callbacks( Open, Close )
111 vlc_module_end ()
112
113 /*****************************************************************************
114  * Probe: probe the audio device for available formats and channels
115  *****************************************************************************/
116 static void Probe( audio_output_t * p_aout )
117 {
118     struct aout_sys_t * p_sys = p_aout->sys;
119     vlc_value_t val, text;
120     int i_format, i_nb_channels;
121
122     var_Create( p_aout, "audio-device", VLC_VAR_INTEGER | VLC_VAR_HASCHOICE );
123     text.psz_string = _("Audio Device");
124     var_Change( p_aout, "audio-device", VLC_VAR_SETTEXT, &text, NULL );
125
126     /* Test for multi-channel. */
127 #ifdef SNDCTL_DSP_GETCHANNELMASK
128     if ( aout_FormatNbChannels( &p_aout->format ) > 2 )
129     {
130         /* Check that the device supports this. */
131
132         int i_chanmask;
133
134         /* Reset all. */
135         i_format = AFMT_S16_NE;
136         if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
137             ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
138         {
139             msg_Err( p_aout, "cannot reset OSS audio device" );
140             var_Destroy( p_aout, "audio-device" );
141             return;
142         }
143
144         if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
145                     &i_chanmask ) == 0 )
146         {
147             if ( !(i_chanmask & DSP_BIND_FRONT) )
148             {
149                 msg_Err( p_aout, "no front channels! (%x)",
150                          i_chanmask );
151                 return;
152             }
153
154             if ( (i_chanmask & (DSP_BIND_SURR | DSP_BIND_CENTER_LFE))
155                   && (p_aout->format.i_physical_channels ==
156                        (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
157                          | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
158                          | AOUT_CHAN_LFE)) )
159             {
160                 val.i_int = AOUT_VAR_5_1;
161                 text.psz_string = (char*) "5.1";
162                 var_Change( p_aout, "audio-device",
163                             VLC_VAR_ADDCHOICE, &val, &text );
164             }
165
166             if ( (i_chanmask & DSP_BIND_SURR)
167                   && (p_aout->format.i_physical_channels &
168                        (AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
169                          | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT)) )
170             {
171                 val.i_int = AOUT_VAR_2F2R;
172                 text.psz_string = _("2 Front 2 Rear");
173                 var_Change( p_aout, "audio-device",
174                             VLC_VAR_ADDCHOICE, &val, &text );
175             }
176         }
177     }
178 #endif
179
180     /* Reset all. */
181     i_format = AFMT_S16_NE;
182     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
183         ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
184     {
185         msg_Err( p_aout, "cannot reset OSS audio device" );
186         var_Destroy( p_aout, "audio-device" );
187         return;
188     }
189
190     /* Test for stereo. */
191     i_nb_channels = 2;
192     if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
193          && i_nb_channels == 2 )
194     {
195         val.i_int = AOUT_VAR_STEREO;
196         text.psz_string = _("Stereo");
197         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
198     }
199
200     /* Reset all. */
201     i_format = AFMT_S16_NE;
202     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 ||
203         ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
204     {
205         msg_Err( p_aout, "cannot reset OSS audio device" );
206         var_Destroy( p_aout, "audio-device" );
207         return;
208     }
209
210     /* Test for mono. */
211     i_nb_channels = 1;
212     if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) >= 0
213          && i_nb_channels == 1 )
214     {
215         val.i_int = AOUT_VAR_MONO;
216         text.psz_string = _("Mono");
217         var_Change( p_aout, "audio-device", VLC_VAR_ADDCHOICE, &val, &text );
218         if ( p_aout->format.i_physical_channels == AOUT_CHAN_CENTER )
219         {
220             var_Set( p_aout, "audio-device", val );
221         }
222     }
223
224     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
225     {
226         msg_Err( p_aout, "cannot reset OSS audio device" );
227         var_Destroy( p_aout, "audio-device" );
228         return;
229     }
230
231     /* Test for spdif. */
232     if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
233     {
234         i_format = AFMT_AC3;
235
236         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) >= 0
237              && i_format == AFMT_AC3 )
238         {
239             val.i_int = AOUT_VAR_SPDIF;
240             text.psz_string = _("A/52 over S/PDIF");
241             var_Change( p_aout, "audio-device",
242                         VLC_VAR_ADDCHOICE, &val, &text );
243             if( var_InheritBool( p_aout, "spdif" ) )
244                 var_Set( p_aout, "audio-device", val );
245         }
246         else if( var_InheritBool( p_aout, "spdif" ) )
247         {
248             msg_Warn( p_aout, "S/PDIF not supported by card" );
249         }
250     }
251
252     var_AddCallback( p_aout, "audio-device", aout_ChannelsRestart,
253                      NULL );
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     {
308         Probe( p_aout );
309     }
310
311     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
312     {
313         /* Probe() has failed. */
314         close( p_sys->i_fd );
315         free( p_sys );
316         return VLC_EGENERIC;
317     }
318
319     if ( val.i_int == AOUT_VAR_SPDIF )
320     {
321         p_aout->format.i_format = VLC_CODEC_SPDIFL;
322     }
323     else if ( val.i_int == AOUT_VAR_5_1 )
324     {
325         p_aout->format.i_format = VLC_CODEC_S16N;
326         p_aout->format.i_physical_channels
327             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
328                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT
329                | AOUT_CHAN_LFE;
330     }
331     else if ( val.i_int == AOUT_VAR_2F2R )
332     {
333         p_aout->format.i_format = VLC_CODEC_S16N;
334         p_aout->format.i_physical_channels
335             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
336                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARRIGHT;
337     }
338     else if ( val.i_int == AOUT_VAR_STEREO )
339     {
340         p_aout->format.i_format = VLC_CODEC_S16N;
341         p_aout->format.i_physical_channels
342             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
343     }
344     else if ( val.i_int == AOUT_VAR_MONO )
345     {
346         p_aout->format.i_format = VLC_CODEC_S16N;
347         p_aout->format.i_physical_channels = AOUT_CHAN_CENTER;
348     }
349     else
350     {
351         /* This should not happen ! */
352         msg_Err( p_aout, "internal: can't find audio-device (%"PRId64")",
353                  val.i_int );
354         close( p_sys->i_fd );
355         free( p_sys );
356         return VLC_EGENERIC;
357     }
358
359     var_TriggerCallback( p_aout, "intf-change" );
360
361     /* Reset the DSP device */
362     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
363     {
364         msg_Err( p_aout, "cannot reset OSS audio device" );
365         close( p_sys->i_fd );
366         free( p_sys );
367         return VLC_EGENERIC;
368     }
369
370     /* Set the output format */
371     if ( AOUT_FMT_NON_LINEAR( &p_aout->format ) )
372     {
373         int i_format = AFMT_AC3;
374
375         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
376              || i_format != AFMT_AC3 )
377         {
378             msg_Err( p_aout, "cannot reset OSS audio device" );
379             close( p_sys->i_fd );
380             free( p_sys );
381             return VLC_EGENERIC;
382         }
383
384         p_aout->format.i_format = VLC_CODEC_SPDIFL;
385         p_aout->format.i_bytes_per_frame = AOUT_SPDIF_SIZE;
386         p_aout->format.i_frame_length = A52_FRAME_NB;
387
388         aout_PacketInit( p_aout, &p_sys->packet, A52_FRAME_NB );
389         aout_VolumeNoneInit( p_aout );
390     }
391
392     if ( !AOUT_FMT_NON_LINEAR( &p_aout->format ) )
393     {
394         unsigned int i_format = AFMT_S16_NE;
395         unsigned int i_frame_size, i_fragments;
396         unsigned int i_rate;
397         unsigned int i_nb_channels;
398         audio_buf_info audio_buf;
399
400         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
401         {
402             msg_Err( p_aout, "cannot set audio output format" );
403             close( p_sys->i_fd );
404             free( p_sys );
405             return VLC_EGENERIC;
406         }
407
408         switch ( i_format )
409         {
410         case AFMT_U8:
411             p_aout->format.i_format = VLC_CODEC_U8;
412             break;
413         case AFMT_S8:
414             p_aout->format.i_format = VLC_CODEC_S8;
415             break;
416         case AFMT_U16_LE:
417             p_aout->format.i_format = VLC_CODEC_U16L;
418             break;
419         case AFMT_S16_LE:
420             p_aout->format.i_format = VLC_CODEC_S16L;
421             break;
422         case AFMT_U16_BE:
423             p_aout->format.i_format = VLC_CODEC_U16B;
424             break;
425         case AFMT_S16_BE:
426             p_aout->format.i_format = VLC_CODEC_S16B;
427             break;
428         default:
429             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
430                      i_format );
431             close( p_sys->i_fd );
432             free( p_sys );
433             return VLC_EGENERIC;
434         }
435
436         i_nb_channels = aout_FormatNbChannels( &p_aout->format );
437
438         /* Set the number of channels */
439         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
440             i_nb_channels != aout_FormatNbChannels( &p_aout->format ) )
441         {
442             msg_Err( p_aout, "cannot set number of audio channels (%s)",
443                      aout_FormatPrintChannels( &p_aout->format) );
444             close( p_sys->i_fd );
445             free( p_sys );
446             return VLC_EGENERIC;
447         }
448
449         /* Set the output rate */
450         i_rate = p_aout->format.i_rate;
451         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
452         {
453             msg_Err( p_aout, "cannot set audio output rate (%i)",
454                              p_aout->format.i_rate );
455             close( p_sys->i_fd );
456             free( p_sys );
457             return VLC_EGENERIC;
458         }
459
460         if( i_rate != p_aout->format.i_rate )
461         {
462             p_aout->format.i_rate = i_rate;
463         }
464
465         /* Set the fragment size */
466         aout_FormatPrepare( &p_aout->format );
467
468         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
469          *                              1 << yyyy   is fragsize */
470         i_frame_size = ((uint64_t)p_aout->format.i_bytes_per_frame * p_aout->format.i_rate * 65536) / (48000 * 2 * 2) / FRAME_COUNT;
471         i_fragments = 4;
472         while( i_fragments < 12 && (1U << i_fragments) < i_frame_size )
473         {
474             ++i_fragments;
475         }
476         i_fragments |= FRAME_COUNT << 16;
477         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
478         {
479             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
480         }
481
482         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
483         {
484             msg_Err( p_aout, "cannot get fragment size" );
485             close( p_sys->i_fd );
486             free( p_sys );
487             return VLC_EGENERIC;
488         }
489
490         /* Number of fragments actually allocated */
491         p_aout->sys->i_fragstotal = audio_buf.fragstotal;
492
493         /* Maximum duration the soundcard's buffer can hold */
494         p_aout->sys->max_buffer_duration =
495                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
496                 / p_aout->format.i_bytes_per_frame
497                 / p_aout->format.i_rate
498                 * p_aout->format.i_frame_length;
499
500         aout_PacketInit( p_aout, &p_sys->packet,
501                          audio_buf.fragsize/p_aout->format.i_bytes_per_frame );
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  * Close: close the DSP audio device
520  *****************************************************************************/
521 static void Close( vlc_object_t * p_this )
522 {
523     audio_output_t *p_aout = (audio_output_t *)p_this;
524     struct aout_sys_t * p_sys = p_aout->sys;
525
526     vlc_cancel( p_sys->thread );
527     vlc_join( p_sys->thread, NULL );
528     p_aout->b_die = false;
529
530     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
531     close( p_sys->i_fd );
532
533     aout_PacketDestroy( p_aout );
534     free( p_sys );
535 }
536
537 /*****************************************************************************
538  * BufferDuration: buffer status query
539  *****************************************************************************
540  * This function returns the duration in microseconds of the current buffer.
541  *****************************************************************************/
542 static mtime_t BufferDuration( audio_output_t * p_aout )
543 {
544     struct aout_sys_t * p_sys = p_aout->sys;
545     audio_buf_info audio_buf;
546     int i_bytes;
547
548 #ifdef SNDCTL_DSP_GETODELAY
549     if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETODELAY, &i_bytes ) < 0 )
550 #endif
551     {
552         /* Fall back to GETOSPACE and approximate latency. */
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
565     /* Return the fragment duration */
566     return (mtime_t)i_bytes * 1000000
567             / p_aout->format.i_bytes_per_frame
568             / p_aout->format.i_rate
569             * p_aout->format.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     audio_output_t * p_aout = (audio_output_t*)obj;
593     struct aout_sys_t * p_sys = p_aout->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->format.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->sys->max_buffer_duration
611                              / p_aout->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->sys->max_buffer_duration
618                           / p_aout->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_MAX_PTS_ADVANCE )
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->format.i_frame_length
667                       * p_aout->format.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 }