]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* src/playlist/playlist.c: fixed deadlock in playlist.
[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.34 2002/11/21 15:51:57 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     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_ISLIST );
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
210 /*****************************************************************************
211  * Open: open the audio device (the digital sound processor)
212  *****************************************************************************
213  * This function opens the dsp as a usual non-blocking write-only file, and
214  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
215  *****************************************************************************/
216 static int Open( vlc_object_t *p_this )
217 {
218     aout_instance_t * p_aout = (aout_instance_t *)p_this;
219     struct aout_sys_t * p_sys;
220     char * psz_device;
221     vlc_value_t val;
222
223     /* Allocate structure */
224     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
225     if( p_sys == NULL )
226     {
227         msg_Err( p_aout, "out of memory" );
228         return VLC_ENOMEM;
229     }
230
231     /* Get device name */
232     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
233     {
234         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
235         free( p_sys );
236         return VLC_EGENERIC;
237     }
238
239     /* Open the sound device */
240     p_sys->i_fd = open( psz_device, O_WRONLY );
241     free( psz_device );
242     if( p_sys->i_fd < 0 )
243     {
244         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
245         free( p_sys );
246         return VLC_EGENERIC;
247     }
248
249     p_aout->output.pf_play = Play;
250
251     if ( var_Type( p_aout, "audio-device" ) < 0 )
252     {
253         Probe( p_aout );
254     }
255
256     if ( var_Get( p_aout, "audio-device", &val ) < 0 )
257     {
258         /* Probe() has failed. */
259         free( p_sys );
260         return VLC_EGENERIC;
261     }
262
263     if ( !strcmp( val.psz_string, N_("S/PDIF") ) )
264     {
265         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
266     }
267     else if ( !strcmp( val.psz_string, N_("5.1") ) )
268     {
269         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
270         p_aout->output.output.i_physical_channels
271             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT | AOUT_CHAN_CENTER
272                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT
273                | AOUT_CHAN_LFE;
274     }
275     else if ( !strcmp( val.psz_string, N_("2 Front 2 Rear") ) )
276     {
277         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
278         p_aout->output.output.i_physical_channels
279             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT
280                | AOUT_CHAN_REARLEFT | AOUT_CHAN_REARLEFT;
281     }
282     else if ( !strcmp( val.psz_string, "Stereo" ) )
283     {
284         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
285         p_aout->output.output.i_physical_channels
286             = AOUT_CHAN_LEFT | AOUT_CHAN_RIGHT;
287     }
288     else if ( !strcmp( val.psz_string, "Mono" ) )
289     {
290         p_aout->output.output.i_format = AOUT_FMT_S16_NE;
291         p_aout->output.output.i_physical_channels = AOUT_CHAN_CENTER;
292     }
293     free( val.psz_string );
294
295     /* Reset the DSP device */
296     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
297     {
298         msg_Err( p_aout, "cannot reset OSS audio device" );
299         close( p_sys->i_fd );
300         free( p_sys );
301         return VLC_EGENERIC;
302     }
303
304     /* Set the output format */
305     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
306     {
307         int i_format = AFMT_AC3;
308
309         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
310              || i_format != AFMT_AC3 )
311         {
312             msg_Err( p_aout, "cannot reset OSS audio device" );
313             close( p_sys->i_fd );
314             free( p_sys );
315             return VLC_EGENERIC;
316         }
317
318         p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
319         p_aout->output.i_nb_samples = A52_FRAME_NB;
320         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
321         p_aout->output.output.i_frame_length = A52_FRAME_NB;
322
323         aout_VolumeNoneInit( p_aout );
324     }
325
326     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
327     {
328         int i_format = AFMT_S16_NE;
329         int i_frame_size, i_fragments;
330         int i_rate;
331         int i_nb_channels;
332         audio_buf_info audio_buf;
333
334         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
335         {
336             msg_Err( p_aout, "cannot set audio output format" );
337             close( p_sys->i_fd );
338             free( p_sys );
339             return VLC_EGENERIC;
340         }
341
342         switch ( i_format )
343         {
344         case AFMT_U8:
345             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
346             break;
347         case AFMT_S8:
348             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
349             break;
350         case AFMT_U16_LE:
351             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
352             break;
353         case AFMT_S16_LE:
354             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
355             break;
356         case AFMT_U16_BE:
357             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
358             break;
359         case AFMT_S16_BE:
360             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
361             break;
362         default:
363             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
364                      i_format );
365             close( p_sys->i_fd );
366             free( p_sys );
367             return VLC_EGENERIC;
368         }
369
370         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
371
372         /* Set the number of channels */
373         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 ||
374             i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
375         {
376             msg_Err( p_aout, "cannot set number of audio channels (%x)",
377                      aout_FormatPrintChannels( &p_aout->output.output) );
378             close( p_sys->i_fd );
379             free( p_sys );
380             return VLC_EGENERIC;
381         }
382
383         /* Set the output rate */
384         i_rate = p_aout->output.output.i_rate;
385         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
386         {
387             msg_Err( p_aout, "cannot set audio output rate (%i)",
388                              p_aout->output.output.i_rate );
389             close( p_sys->i_fd );
390             free( p_sys );
391             return VLC_EGENERIC;
392         }
393
394         if( i_rate != p_aout->output.output.i_rate )
395         {
396             p_aout->output.output.i_rate = i_rate;
397         }
398
399         /* Set the fragment size */
400         aout_FormatPrepare( &p_aout->output.output );
401
402         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
403          *                              1 << yyyy   is fragsize */
404         i_fragments = 0;
405         i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
406         while( i_frame_size >>= 1 )
407         {
408             ++i_fragments;
409         }
410         i_fragments |= FRAME_COUNT << 16;
411         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
412         {
413             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
414         }
415
416         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
417         {
418             msg_Err( p_aout, "cannot get fragment size" );
419             close( p_sys->i_fd );
420             free( p_sys );
421             return VLC_EGENERIC;
422         }
423         else
424         {
425             /* Number of fragments actually allocated */
426             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
427
428             /* Maximum duration the soundcard's buffer can hold */
429             p_aout->output.p_sys->max_buffer_duration =
430                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
431                 / p_aout->output.output.i_bytes_per_frame
432                 / p_aout->output.output.i_rate
433                 * p_aout->output.output.i_frame_length;
434
435             p_aout->output.i_nb_samples = audio_buf.fragsize /
436                 p_aout->output.output.i_bytes_per_frame;
437         }
438
439         aout_VolumeSoftInit( p_aout );
440     }
441
442     p_aout->output.p_sys->b_workaround_buggy_driver =
443         config_GetInt( p_aout, "oss-buggy" );
444
445     /* Create OSS thread and wait for its readiness. */
446     if( vlc_thread_create( p_aout, "aout", OSSThread,
447                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
448     {
449         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
450         close( p_sys->i_fd );
451         free( p_sys );
452         return VLC_ETHREAD;
453     }
454
455     return VLC_SUCCESS;
456 }
457
458 /*****************************************************************************
459  * Play: nothing to do
460  *****************************************************************************/
461 static void Play( aout_instance_t *p_aout )
462 {
463 }
464
465 /*****************************************************************************
466  * Close: close the dsp audio device
467  *****************************************************************************/
468 static void Close( vlc_object_t * p_this )
469 {
470     aout_instance_t *p_aout = (aout_instance_t *)p_this;
471     struct aout_sys_t * p_sys = p_aout->output.p_sys;
472
473     p_aout->b_die = VLC_TRUE;
474     vlc_thread_join( p_aout );
475     p_aout->b_die = VLC_FALSE;
476
477     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
478     close( p_sys->i_fd );
479
480     free( p_sys );
481 }
482
483 /*****************************************************************************
484  * BufferDuration: buffer status query
485  *****************************************************************************
486  * This function returns the duration in microseconds of the current buffer.
487  *****************************************************************************/
488 static mtime_t BufferDuration( aout_instance_t * p_aout )
489 {
490     struct aout_sys_t * p_sys = p_aout->output.p_sys;
491     audio_buf_info audio_buf;
492     int i_bytes;
493
494     /* Fill the audio_buf_info structure:
495      * - fragstotal: total number of fragments allocated
496      * - fragsize: size of a fragment in bytes
497      * - bytes: available space in bytes (includes partially used fragments)
498      * Note! 'bytes' could be more than fragments*fragsize */
499     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
500
501     /* calculate number of available fragments (not partially used ones) */
502     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
503
504     /* Return the fragment duration */
505     return (mtime_t)i_bytes * 1000000
506             / p_aout->output.output.i_bytes_per_frame
507             / p_aout->output.output.i_rate
508             * p_aout->output.output.i_frame_length;
509 }
510
511 /*****************************************************************************
512  * OSSThread: asynchronous thread used to DMA the data to the device
513  *****************************************************************************/
514 static int OSSThread( aout_instance_t * p_aout )
515 {
516     struct aout_sys_t * p_sys = p_aout->output.p_sys;
517     mtime_t next_date = 0;
518
519     while ( !p_aout->b_die )
520     {
521         aout_buffer_t * p_buffer = NULL;
522         int i_tmp, i_size;
523         byte_t * p_bytes;
524
525         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
526         {
527             mtime_t buffered = BufferDuration( p_aout );
528
529             if( p_aout->output.p_sys->b_workaround_buggy_driver )
530             {
531 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
532                 /* Wait a bit - we don't want our buffer to be full */
533                 if( buffered > (p_aout->output.p_sys->max_buffer_duration
534                                 / i_fragstotal * (i_fragstotal - 1)) )
535                 {
536                     msleep((p_aout->output.p_sys->max_buffer_duration
537                                 / i_fragstotal ));
538                     buffered = BufferDuration( p_aout );
539                 }
540 #undef i_fragstotal
541             }
542
543             /* Next buffer will be played at mdate() + buffered */
544             p_buffer = aout_OutputNextBuffer( p_aout, mdate() + buffered,
545                                               VLC_FALSE );
546
547             if( p_buffer == NULL &&
548                 buffered > ( p_aout->output.p_sys->max_buffer_duration
549                              / p_aout->output.p_sys->i_fragstotal ) )
550             {
551                 /* If we have at least a fragment full, then we can wait a
552                  * little and retry to get a new audio buffer instead of
553                  * playing a blank sample */
554                 msleep( ( p_aout->output.p_sys->max_buffer_duration
555                           / p_aout->output.p_sys->i_fragstotal / 2 ) );
556                 continue;
557             }
558         }
559         else
560         {
561             /* emu10k1 driver does not report Buffer Duration correctly in
562              * passthrough mode so we have to cheat */
563             if( !next_date )
564             {
565                 next_date = mdate();
566             }
567             else
568             {
569                 mtime_t delay = next_date - mdate();
570                 if( delay > AOUT_PTS_TOLERANCE )
571                 {
572                     msleep( delay / 2 );
573                 }
574             }
575             
576             while( !p_aout->b_die && ! ( p_buffer =
577                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
578             {
579                 msleep( 1000 );
580                 next_date = mdate();
581             }
582         }
583
584         if ( p_buffer != NULL )
585         {
586             p_bytes = p_buffer->p_buffer;
587             i_size = p_buffer->i_nb_bytes;
588             /* This is theoretical ... we'll see next iteration whether
589              * we're drifting */
590             next_date += p_buffer->end_date - p_buffer->start_date;
591         }
592         else
593         {
594             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
595                       * p_aout->output.output.i_bytes_per_frame;
596             p_bytes = malloc( i_size );
597             memset( p_bytes, 0, i_size );
598             next_date = 0;
599         }
600
601         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
602
603         if( i_tmp < 0 )
604         {
605             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
606         }
607
608         if ( p_buffer != NULL )
609         {
610             aout_BufferFree( p_buffer );
611         }
612         else
613         {
614             free( p_bytes );
615         }
616     }
617
618     return VLC_SUCCESS;
619 }