]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* modules/audio_output/oss.c: when now set the fragment size of the OSS device
[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.32 2002/10/25 15:21: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     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 #define A52_FRAME_NB 1536
74
75 /*****************************************************************************
76  * Local prototypes
77  *****************************************************************************/
78 static int  Open         ( vlc_object_t * );
79 static void Close        ( vlc_object_t * );
80
81 static void Play         ( aout_instance_t * );
82 static int  OSSThread    ( aout_instance_t * );
83
84 static mtime_t BufferDuration( aout_instance_t * p_aout );
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     /* Open the sound device */
135     p_sys->i_fd = open( psz_device, O_WRONLY );
136     free( psz_device );
137     if( p_sys->i_fd < 0 )
138     {
139         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
140         free( p_sys );
141         return VLC_EGENERIC;
142     }
143
144     p_aout->output.pf_play = Play;
145
146     /* Reset the DSP device */
147     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
148     {
149         msg_Err( p_aout, "cannot reset OSS audio device" );
150         close( p_sys->i_fd );
151         free( p_sys );
152         return VLC_EGENERIC;
153     }
154     
155     /* Set the output format */
156     if ( AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
157     {
158         int i_format = AFMT_AC3;
159
160         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
161              || i_format != AFMT_AC3 )
162         {
163             p_aout->output.output.i_format = AOUT_FMT_S16_NE;
164         }
165         else
166         {
167             p_aout->output.output.i_format = VLC_FOURCC('s','p','d','i');
168             p_aout->output.i_nb_samples = A52_FRAME_NB;
169             p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
170             p_aout->output.output.i_frame_length = A52_FRAME_NB;
171
172             aout_VolumeNoneInit( p_aout );
173         }
174     }
175
176     if ( !AOUT_FMT_NON_LINEAR( &p_aout->output.output ) )
177     {
178         int i_format = AFMT_S16_NE;
179         int i_frame_size, i_fragments;
180         int i_rate;
181         int i_nb_channels;
182         audio_buf_info audio_buf;
183
184         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
185         {
186             msg_Err( p_aout, "cannot set audio output format" );
187             close( p_sys->i_fd );
188             free( p_sys );
189             return VLC_EGENERIC;
190         }
191
192         switch ( i_format )
193         {
194         case AFMT_U8:
195             p_aout->output.output.i_format = VLC_FOURCC('u','8',' ',' ');
196             break;
197         case AFMT_S8:
198             p_aout->output.output.i_format = VLC_FOURCC('s','8',' ',' ');
199             break;
200         case AFMT_U16_LE:
201             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','l');
202             break;
203         case AFMT_S16_LE:
204             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
205             break;
206         case AFMT_U16_BE:
207             p_aout->output.output.i_format = VLC_FOURCC('u','1','6','b');
208             break;
209         case AFMT_S16_BE:
210             p_aout->output.output.i_format = VLC_FOURCC('s','1','6','b');
211             break;
212         default:
213             msg_Err( p_aout, "OSS fell back to an unknown format (%d)",
214                      i_format );
215             close( p_sys->i_fd );
216             free( p_sys );
217             return VLC_EGENERIC;
218         }
219
220         /* These cases are desperate because of the OSS API and A/52 spec. */
221         switch ( p_aout->output.output.i_channels )
222         {
223             case AOUT_CHAN_3F:
224             case AOUT_CHAN_2F1R:
225             case AOUT_CHAN_3F1R:
226             case AOUT_CHAN_STEREO | AOUT_CHAN_LFE:
227             case AOUT_CHAN_2F1R | AOUT_CHAN_LFE:
228             case AOUT_CHAN_3F1R | AOUT_CHAN_LFE:
229             case AOUT_CHAN_DOLBY | AOUT_CHAN_LFE:
230                 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
231                 break;
232             case AOUT_CHAN_CHANNEL | AOUT_CHAN_LFE:
233             case AOUT_CHAN_CHANNEL1 | AOUT_CHAN_LFE:
234             case AOUT_CHAN_CHANNEL2 | AOUT_CHAN_LFE:
235             case AOUT_CHAN_MONO | AOUT_CHAN_LFE:
236             case AOUT_CHAN_2F2R | AOUT_CHAN_LFE:
237                 p_aout->output.output.i_channels &= ~AOUT_CHAN_LFE;
238                 break;
239             case AOUT_CHAN_3F2R:
240                 p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
241                 break;
242         }
243         /* In a nutshell, possible types : AOUT_CHAN_STEREO (and al.),
244            AOUT_CHAN_2F2R, AOUT_CHAN_3F1R | AOUT_CHAN_LFE. */
245
246         i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
247
248         if ( i_nb_channels > 2 )
249         {
250             /* Check that the device supports this. */
251
252 #ifdef SNDCTL_DSP_GETCHANNELMASK
253             int i_chanmask;
254             if ( ioctl( p_sys->i_fd, SNDCTL_DSP_GETCHANNELMASK,
255                         &i_chanmask ) == 0 )
256             {
257                 if ( !(i_chanmask & DSP_BIND_FRONT) )
258                 {
259                     msg_Err( p_aout, "No front channels ! (%x)",
260                              i_chanmask );
261                     close( p_sys->i_fd );
262                     free( p_sys );
263                     return VLC_EGENERIC;
264                 }
265
266                 if ( !(i_chanmask & DSP_BIND_SURR) )
267                 {
268                     p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
269                     i_nb_channels = 2;
270                 }
271  
272                 if ( p_aout->output.output.i_channels ==
273                          (AOUT_CHAN_3F2R | AOUT_CHAN_LFE)
274                       && !(i_chanmask & DSP_BIND_CENTER_LFE) )
275                 {
276                     p_aout->output.output.i_channels = AOUT_CHAN_2F2R;
277                     i_nb_channels = 4;
278                 }
279             }
280             else
281 #endif
282             {
283                 /* The driver doesn't support this call, assume it is stereo. */
284                 p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
285                 i_nb_channels = 2;
286             }
287         }
288
289         /* Set the number of channels */
290         if( ioctl( p_sys->i_fd, SNDCTL_DSP_CHANNELS, &i_nb_channels ) < 0 )
291         {
292             msg_Err( p_aout, "cannot set number of audio channels (%i)",
293                               p_aout->output.output.i_channels );
294             close( p_sys->i_fd );
295             free( p_sys );
296             return VLC_EGENERIC;
297         }
298
299         if ( i_nb_channels != aout_FormatNbChannels( &p_aout->output.output ) )
300         {
301             switch ( i_nb_channels )
302             {
303             case 1: p_aout->output.output.i_channels = AOUT_CHAN_MONO; break;
304             case 2: p_aout->output.output.i_channels = AOUT_CHAN_STEREO; break;
305             case 4: p_aout->output.output.i_channels = AOUT_CHAN_2F2R; break;
306             default:
307                 msg_Err( p_aout, "Unsupported downmixing (%d)", i_nb_channels );
308                 close( p_sys->i_fd );
309                 free( p_sys );
310                 return VLC_EGENERIC;
311             }
312         }
313
314         /* Set the output rate */
315         i_rate = p_aout->output.output.i_rate;
316         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
317         {
318             msg_Err( p_aout, "cannot set audio output rate (%i)",
319                              p_aout->output.output.i_rate );
320             close( p_sys->i_fd );
321             free( p_sys );
322             return VLC_EGENERIC;
323         }
324
325         if( i_rate != p_aout->output.output.i_rate )
326         {
327             p_aout->output.output.i_rate = i_rate;
328         }
329
330         /* Set the fragment size */
331         aout_FormatPrepare( &p_aout->output.output );
332
333         /* i_fragment = xxxxyyyy where: xxxx        is fragtotal
334          *                              1 << yyyy   is fragsize */
335         i_fragments = 0;
336         i_frame_size = FRAME_SIZE * p_aout->output.output.i_bytes_per_frame;
337         while( i_frame_size >>= 1 )
338         {
339             ++i_fragments;
340         }
341         i_fragments |= FRAME_COUNT << 16;
342         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
343         {
344             msg_Warn( p_aout, "cannot set fragment size (%.8x)", i_fragments );
345         }
346
347         if( ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf ) < 0 )
348         {
349             msg_Warn( p_aout, "cannot get fragment size" );
350             close( p_sys->i_fd );
351             free( p_sys );
352             return VLC_EGENERIC;
353         }
354         else
355         {
356             /* Number of fragments actually allocated */
357             p_aout->output.p_sys->i_fragstotal = audio_buf.fragstotal;
358
359             /* Maximum duration the soundcard's buffer can hold */
360             p_aout->output.p_sys->max_buffer_duration =
361                 (mtime_t)audio_buf.fragstotal * audio_buf.fragsize * 1000000
362                 / p_aout->output.output.i_bytes_per_frame
363                 / p_aout->output.output.i_rate
364                 * p_aout->output.output.i_frame_length;
365
366             p_aout->output.i_nb_samples = audio_buf.fragsize /
367                 p_aout->output.output.i_bytes_per_frame;
368         }
369
370         aout_VolumeSoftInit( p_aout );
371     }
372
373     /* Create OSS thread and wait for its readiness. */
374     if( vlc_thread_create( p_aout, "aout", OSSThread,
375                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
376     {
377         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
378         close( p_sys->i_fd );
379         free( p_sys );
380         return VLC_ETHREAD;
381     }
382
383     p_aout->output.p_sys->b_workaround_buggy_driver =
384         config_GetInt( p_aout, "oss-buggy" );
385
386     return VLC_SUCCESS;
387 }
388
389 /*****************************************************************************
390  * Play: nothing to do
391  *****************************************************************************/
392 static void Play( aout_instance_t *p_aout )
393 {
394 }
395
396 /*****************************************************************************
397  * Close: close the dsp audio device
398  *****************************************************************************/
399 static void Close( vlc_object_t * p_this )
400 {
401     aout_instance_t *p_aout = (aout_instance_t *)p_this;
402     struct aout_sys_t * p_sys = p_aout->output.p_sys;
403
404     p_aout->b_die = VLC_TRUE;
405     vlc_thread_join( p_aout );
406     p_aout->b_die = VLC_FALSE;
407
408     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
409     close( p_sys->i_fd );
410
411     free( p_sys );
412 }
413
414 /*****************************************************************************
415  * BufferDuration: buffer status query
416  *****************************************************************************
417  * This function returns the duration in microseconds of the current buffer.
418  *****************************************************************************/
419 static mtime_t BufferDuration( aout_instance_t * p_aout )
420 {
421     struct aout_sys_t * p_sys = p_aout->output.p_sys;
422     audio_buf_info audio_buf;
423     int i_bytes;
424
425     /* Fill the audio_buf_info structure:
426      * - fragstotal: total number of fragments allocated
427      * - fragsize: size of a fragment in bytes
428      * - bytes: available space in bytes (includes partially used fragments)
429      * Note! 'bytes' could be more than fragments*fragsize */
430     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
431
432     /* calculate number of available fragments (not partially used ones) */
433     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
434
435     /* Return the fragment duration */
436     return (mtime_t)i_bytes * 1000000
437             / p_aout->output.output.i_bytes_per_frame
438             / p_aout->output.output.i_rate
439             * p_aout->output.output.i_frame_length;
440 }
441
442 /*****************************************************************************
443  * OSSThread: asynchronous thread used to DMA the data to the device
444  *****************************************************************************/
445 static int OSSThread( aout_instance_t * p_aout )
446 {
447     struct aout_sys_t * p_sys = p_aout->output.p_sys;
448     mtime_t next_date = 0;
449
450     while ( !p_aout->b_die )
451     {
452         aout_buffer_t * p_buffer = NULL;
453         int i_tmp, i_size;
454         byte_t * p_bytes;
455
456         if ( p_aout->output.output.i_format != VLC_FOURCC('s','p','d','i') )
457         {
458             mtime_t buffered = BufferDuration( p_aout );
459
460             if( p_aout->output.p_sys->b_workaround_buggy_driver )
461             {
462 #define i_fragstotal p_aout->output.p_sys->i_fragstotal
463                 /* Wait a bit - we don't want our buffer to be full */
464                 if( buffered > (p_aout->output.p_sys->max_buffer_duration
465                                 / i_fragstotal * (i_fragstotal - 1)) )
466                 {
467                     msleep((p_aout->output.p_sys->max_buffer_duration
468                                 / i_fragstotal ));
469                     buffered = BufferDuration( p_aout );
470                 }
471 #undef i_fragstotal
472             }
473
474             if( !next_date )
475             {
476                 /* This is the _real_ presentation date */
477                 next_date = mdate() + buffered;
478             }
479             else
480             {
481                 /* Give a hint to the audio output about our drift, but
482                  * not too much because we want to make it happy with our
483                  * nicely calculated dates. */
484                 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
485             }
486
487             /* Next buffer will be played at mdate()+buffered */
488             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
489         }
490         else
491         {
492             /* emu10k1 driver does not report Buffer Duration correctly in
493              * passthrough mode so we have to cheat */
494             if( !next_date )
495             {
496                 next_date = mdate();
497             }
498             else
499             {
500                 mtime_t delay = next_date - mdate();
501                 if( delay > AOUT_PTS_TOLERANCE )
502                 {
503                     msleep( delay / 2 );
504                 }
505             }
506             
507             while( !p_aout->b_die && ! ( p_buffer =
508                 aout_OutputNextBuffer( p_aout, next_date, VLC_TRUE ) ) )
509             {
510                 msleep( 1000 );
511                 next_date = mdate();
512             }
513         }
514
515         if ( p_buffer != NULL )
516         {
517             p_bytes = p_buffer->p_buffer;
518             i_size = p_buffer->i_nb_bytes;
519             /* This is theoretical ... we'll see next iteration whether
520              * we're drifting */
521             next_date += p_buffer->end_date - p_buffer->start_date;
522         }
523         else
524         {
525             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
526                       * p_aout->output.output.i_bytes_per_frame;
527             p_bytes = malloc( i_size );
528             memset( p_bytes, 0, i_size );
529             next_date = 0;
530         }
531
532         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
533
534         if( i_tmp < 0 )
535         {
536             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
537         }
538
539         if ( p_buffer != NULL )
540         {
541             aout_BufferFree( p_buffer );
542         }
543         else
544         {
545             free( p_bytes );
546         }
547     }
548
549     return VLC_SUCCESS;
550 }