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