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