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