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