]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* modules/audio_output/oss.c: Finally fixed! There also is a new config option
[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.31 2002/10/24 17:36:42 gbazin 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 /*****************************************************************************
57  * aout_sys_t: OSS audio output method descriptor
58  *****************************************************************************
59  * This structure is part of the audio output thread descriptor.
60  * It describes the dsp specific properties of an audio device.
61  *****************************************************************************/
62 struct aout_sys_t
63 {
64     int i_fd;
65     int b_workaround_buggy_driver;
66 };
67
68 /* This must be a power of 2. */
69 #define FRAME_SIZE 1024
70 #define FRAME_COUNT 8
71 #define A52_FRAME_NB 1536
72
73 /* estimation of the size of the internal soundcard driver
74  * (used for buggy drivers) */
75 #define OSS_BUFFER_SIZE 20000
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int  Open         ( vlc_object_t * );
81 static void Close        ( vlc_object_t * );
82
83 static void Play         ( aout_instance_t * );
84 static int  OSSThread    ( aout_instance_t * );
85
86 /*****************************************************************************
87  * Module descriptor
88  *****************************************************************************/
89 #define BUGGY_TEXT N_("Try to work around buggy OSS drivers")
90 #define BUGGY_LONGTEXT N_( \
91     "Some buggy OSS drivers just don't like when their internal buffers " \
92     "are completely filled (the sound gets heavily hashed). If you have one " \
93     "of these drivers, then you need to enable this option." )
94
95 vlc_module_begin();
96     add_category_hint( N_("OSS"), NULL );
97     add_file( "dspdev", "/dev/dsp", aout_FindAndRestart,
98               N_("OSS dsp device"), NULL );
99     add_bool( "oss-buggy", 0, NULL, BUGGY_TEXT, BUGGY_LONGTEXT );
100     set_description( _("Linux OSS /dev/dsp module") );
101     set_capability( "audio output", 100 );
102     add_shortcut( "oss" );
103     set_callbacks( Open, Close );
104 vlc_module_end();
105
106 /*****************************************************************************
107  * Open: open the audio device (the digital sound processor)
108  *****************************************************************************
109  * This function opens the dsp as a usual non-blocking write-only file, and
110  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
111  *****************************************************************************/
112 static int Open( vlc_object_t *p_this )
113 {
114     aout_instance_t * p_aout = (aout_instance_t *)p_this;
115     struct aout_sys_t * p_sys;
116     char * psz_device;
117
118     /* Allocate structure */
119     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
120     if( p_sys == NULL )
121     {
122         msg_Err( p_aout, "out of memory" );
123         return VLC_ENOMEM;
124     }
125
126     /* Get device name */
127     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
128     {
129         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
130         free( p_sys );
131         return VLC_EGENERIC;
132     }
133
134     p_aout->output.p_sys->b_workaround_buggy_driver =
135         config_GetInt( p_aout, "oss-buggy" );
136
137     if( p_aout->output.p_sys->b_workaround_buggy_driver )
138         p_sys->i_fd = open( psz_device, O_WRONLY|O_NONBLOCK );
139     else
140         p_sys->i_fd = open( psz_device, O_WRONLY );
141
142     /* Open the sound device */
143     if( p_sys->i_fd < 0 )
144     {
145         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
146         free( psz_device );
147         free( p_sys );
148         return VLC_EGENERIC;
149     }
150     free( psz_device );
151
152     p_aout->output.pf_play = Play;
153
154     /* Reset the DSP device */
155     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
156     {
157         msg_Err( p_aout, "cannot reset OSS audio device" );
158         close( p_sys->i_fd );
159         free( p_sys );
160         return VLC_EGENERIC;
161     }
162     
163     /* Set the output format */
164     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
165     {
166         int i_format = AFMT_AC3;
167
168         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
169              || i_format != AFMT_AC3 )
170         {
171             p_aout->output.output.i_format = AOUT_FMT_S16_NE;
172         }
173         else
174         {
175             p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
176             p_aout->output.i_nb_samples = A52_FRAME_NB;
177             p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
178             p_aout->output.output.i_frame_length = A52_FRAME_NB;
179
180             aout_VolumeNoneInit( p_aout );
181         }
182     }
183
184     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
185     {
186         int i_format = AFMT_S16_NE;
187         int i_frame_size, i_fragments;
188         int i_rate;
189         int i_nb_channels;
190
191         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
192         {
193             msg_Err( p_aout, "cannot set audio output format" );
194             close( p_sys->i_fd );
195             free( p_sys );
196             return VLC_EGENERIC;
197         }
198
199         switch ( i_format )
200         {
201         case AFMT_U8:
202             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
203             break;
204         case AFMT_S8:
205             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
206             break;
207         case AFMT_U16_LE:
208             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
209             break;
210         case AFMT_S16_LE:
211             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
212             break;
213         case AFMT_U16_BE:
214             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
215             break;
216         case AFMT_S16_BE:
217             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
218             break;
219         default:
220             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
221                      i_format );
222             close( p_sys->i_fd );
223             free( p_sys );
224             return VLC_EGENERIC;
225         }
226
227         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
228         p_aout->output.i_nb_samples = FRAME_SIZE;
229
230         aout_VolumeSoftInit( p_aout );
231
232         /* Set the fragment size
233          * i_fragment = xxxxyyyy where: xxxx        is fragtotal
234          *                              1 << yyyy   is fragsize */
235         i_fragments = 0;
236         i_frame_size = FRAME_SIZE;
237         while( i_frame_size >>= 1 )
238         {
239             ++i_fragments;
240         }
241         i_fragments |= FRAME_COUNT << 16;
242         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
243         {
244             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
245         }
246
247         /* These cases are desperate because of the OSS API and A/52 spec. */
248         switch ( p_aout->output.output.i_channels )
249         {
250             case AOUT_CHAN_3F:
251             case AOUT_CHAN_2F1R:
252             case AOUT_CHAN_3F1R:
253             case AOUT_CHAN_STEREO | AOUT_CHAN_LFE:
254             case AOUT_CHAN_2F1R | AOUT_CHAN_LFE:
255             case AOUT_CHAN_3F1R | AOUT_CHAN_LFE:
256             case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE:
257                 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
258                 break;
259             case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE:
260             case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE:
261             case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE:
262             case AOUT_CHAN_MONO | AOUT_CHAN_LFE:
263             case AOUT_CHAN_2F2R | AOUT_CHAN_LFE:
264                 p_aout->output.output.i_channels &= ~AOUT_CHAN_LFE;
265                 break;
266             case AOUT_CHAN_3F2R:
267                 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
268                 break;
269         }
270         /* In a nutshell, possible types : AOUT_CHAN_STEREO (and al.),
271            AOUT_CHAN_2F2R, AOUT_CHAN_3F1R | AOUT_CHAN_LFE. */
272
273         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
274
275         if ( i_nb_channels > 2 )
276         {
277             /* Check that the device supports this. */
278
279 #ifdef SNDCTL_DSP_GETCHANNELMASK
280             int i_chanmask;
281             if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
282                         &i_chanmask ) == 0 )
283             {
284                 if ( !(i_chanmask & DSP_BIND_FRONT) )
285                 {
286                     msg_Err( p_aout, "No front channels ! (%x)",
287                              i_chanmask );
288                     close( p_sys->i_fd );
289                     free( p_sys );
290                     return VLC_EGENERIC;
291                 }
292
293                 if ( !(i_chanmask & DSP_BIND_SURR) )
294                 {
295                     p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
296                     i_nb_channels = 2;
297                 }
298  
299                 if ( p_aout->output.output.i_channels ==
300                          (AOUT_CHAN_3F2R | AOUT_CHAN_LFE)
301                       && !(i_chanmask & DSP_BIND_CENTER_LFE) )
302                 {
303                     p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
304                     i_nb_channels = 4;
305                 }
306             }
307             else
308 #endif
309             {
310                 /* The driver doesn't support this call, assume it is stereo. */
311                 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
312                 i_nb_channels = 2;
313             }
314         }
315
316         /* Set the number of channels */
317         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 )
318         {
319             msg_Err( p_aout, "cannot set number of audio channels (%i)",
320                               p_aout->output.output.i_channels );
321             close( p_sys->i_fd );
322             free( p_sys );
323             return VLC_EGENERIC;
324         }
325
326         if ( i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
327         {
328             switch ( i_nb_channels )
329             {
330             case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
331             case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
332             case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
333             default:
334                 msg_Err( p_aout, "Unsupported downmixing (%d)", i_nb_channels );
335                 close( p_sys->i_fd );
336                 free( p_sys );
337                 return VLC_EGENERIC;
338             }
339         }
340
341         /* Set the output rate */
342         i_rate = p_aout->output.output.i_rate;
343         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
344         {
345             msg_Err( p_aout, "cannot set audio output rate (%i)",
346                              p_aout->output.output.i_rate );
347             close( p_sys->i_fd );
348             free( p_sys );
349             return VLC_EGENERIC;
350         }
351
352         if( i_rate != p_aout->output.output.i_rate )
353         {
354             p_aout->output.output.i_rate = i_rate;
355         }
356     }
357
358     /* Create OSS thread and wait for its readiness. */
359     if( vlc_thread_create( p_aout, "aout", OSSThread,
360                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
361     {
362         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
363         close( p_sys->i_fd );
364         free( psz_device );
365         free( p_sys );
366         return VLC_ETHREAD;
367     }
368
369     return VLC_SUCCESS;
370 }
371
372 /*****************************************************************************
373  * Play: nothing to do
374  *****************************************************************************/
375 static void Play( aout_instance_t *p_aout )
376 {
377 }
378
379 /*****************************************************************************
380  * Close: close the dsp audio device
381  *****************************************************************************/
382 static void Close( vlc_object_t * p_this )
383 {
384     aout_instance_t *p_aout = (aout_instance_t *)p_this;
385     struct aout_sys_t * p_sys = p_aout->output.p_sys;
386
387     p_aout->b_die = VLC_TRUE;
388     vlc_thread_join( p_aout );
389     p_aout->b_die = VLC_FALSE;
390
391     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
392     close( p_sys->i_fd );
393
394     free( p_sys );
395 }
396
397
398 /*****************************************************************************
399  * BufferDuration: buffer status query
400  *****************************************************************************
401  * This function returns the duration in microseconds of the current buffer.
402  *****************************************************************************/
403 static mtime_t BufferDuration( aout_instance_t * p_aout )
404 {
405     struct aout_sys_t * p_sys = p_aout->output.p_sys;
406     audio_buf_info audio_buf;
407     int i_bytes;
408
409     /* Fill the audio_buf_info structure:
410      * - fragstotal: total number of fragments allocated
411      * - fragsize: size of a fragment in bytes
412      * - bytes: available space in bytes (includes partially used fragments)
413      * Note! 'bytes' could be more than fragments*fragsize */
414     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
415
416     /* calculate number of available fragments (not partially used ones) */
417     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
418
419     /* Return the fragment duration */
420     return (mtime_t)i_bytes * 1000000
421             / p_aout->output.output.i_bytes_per_frame
422             / p_aout->output.output.i_rate
423             * p_aout->output.output.i_frame_length;
424 }
425
426 /*****************************************************************************
427  * OSSThread: asynchronous thread used to DMA the data to the device
428  *****************************************************************************/
429 static int OSSThread( aout_instance_t * p_aout )
430 {
431     struct aout_sys_t * p_sys = p_aout->output.p_sys;
432     mtime_t next_date = 0;
433
434     while ( !p_aout->b_die )
435     {
436         aout_buffer_t * p_buffer = NULL;
437         int i_tmp, i_size;
438         byte_t * p_bytes;
439
440         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
441         {
442             mtime_t buffered = BufferDuration( p_aout );
443
444             if( p_aout->output.p_sys->b_workaround_buggy_driver )
445             {
446                 /* Wait a bit - we don't want our buffer to be full */
447                 while( buffered > OSS_BUFFER_SIZE )
448                 {
449                     msleep( buffered - OSS_BUFFER_SIZE * 2 );
450                     buffered = BufferDuration( p_aout );
451                 }
452             }
453
454             if( !next_date )
455             {
456                 /* This is the _real_ presentation date */
457                 next_date = mdate() + buffered;
458             }
459             else
460             {
461                 /* Give a hint to the audio output about our drift, but
462                  * not too much because we want to make it happy with our
463                  * nicely calculated dates. */
464                 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
465             }
466
467             /* Next buffer will be played at mdate()+buffered */
468             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
469         }
470         else
471         {
472             /* emu10k1 driver does not report Buffer Duration correctly in
473              * passthrough mode so we have to cheat */
474             if( !next_date )
475             {
476                 next_date = mdate();
477             }
478             else
479             {
480                 mtime_t delay = next_date - mdate();
481                 if( delay > AOUT_PTS_TOLERANCE )
482                 {
483                     msleep( delay / 2 );
484                 }
485             }
486             
487             while( !p_aout->b_die && ! ( p_buffer =
488                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
489             {
490                 msleep( 1000 );
491                 next_date = mdate();
492             }
493         }
494
495         if ( p_buffer != NULL )
496         {
497             p_bytes = p_buffer->p_buffer;
498             i_size = p_buffer->i_nb_bytes;
499             /* This is theoretical ... we'll see next iteration whether
500              * we're drifting */
501             next_date += p_buffer->end_date - p_buffer->start_date;
502         }
503         else
504         {
505             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
506                       * p_aout->output.output.i_bytes_per_frame;
507             p_bytes = malloc( i_size );
508             memset( p_bytes, 0, i_size );
509             next_date = 0;
510         }
511
512         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
513
514         if( i_tmp < 0 )
515         {
516             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
517         }
518
519         if ( p_buffer != NULL )
520         {
521             aout_BufferFree( p_buffer );
522         }
523         else
524         {
525             free( p_bytes );
526         }
527     }
528
529     return VLC_SUCCESS;
530 }