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