]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* ./bootstrap : Fixed an issue with old shell versions
[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.20 2002/08/29 23:53:22 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     volatile vlc_bool_t   b_initialized;
66 };
67
68 #define FRAME_SIZE 1024
69 #define FRAME_COUNT 8
70 #define A52_FRAME_NB 1536
71
72 /*****************************************************************************
73  * Local prototypes
74  *****************************************************************************/
75 static int  Open         ( vlc_object_t * );
76 static void Close        ( vlc_object_t * );
77
78 static int  SetFormat    ( aout_instance_t * );
79 static void Play         ( aout_instance_t * );
80 static int  OSSThread    ( aout_instance_t * );
81
82 /*****************************************************************************
83  * Module descriptor
84  *****************************************************************************/
85 vlc_module_begin();
86     add_category_hint( N_("OSS"), NULL );
87     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
88     set_description( _("Linux OSS /dev/dsp module") );
89     set_capability( "audio output", 100 );
90     add_shortcut( "dsp" );
91     set_callbacks( Open, Close );
92 vlc_module_end();
93
94 /*****************************************************************************
95  * Open: open the audio device (the digital sound processor)
96  *****************************************************************************
97  * This function opens the dsp as a usual non-blocking write-only file, and
98  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
99  *****************************************************************************/
100 static int Open( vlc_object_t *p_this )
101 {
102     aout_instance_t * p_aout = (aout_instance_t *)p_this;
103     struct aout_sys_t * p_sys;
104     char * psz_device;
105
106     /* Allocate structure */
107     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
108     if( p_sys == NULL )
109     {
110         msg_Err( p_aout, "out of memory" );
111         return VLC_ENOMEM;
112     }
113
114     /* Initialize some variables */
115     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
116     {
117         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
118         free( p_sys );
119         return VLC_EGENERIC;
120     }
121
122     /* Open the sound device */
123     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
124     {
125         msg_Err( p_aout, "cannot open audio device (%s)", psz_device );
126         free( psz_device );
127         free( p_sys );
128         return VLC_EGENERIC;
129     }
130     free( psz_device );
131
132     /* Create OSS thread and wait for its readiness. */
133     p_sys->b_initialized = VLC_FALSE;
134     if( vlc_thread_create( p_aout, "aout", OSSThread,
135                            VLC_THREAD_PRIORITY_OUTPUT, VLC_FALSE ) )
136     {
137         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
138         close( p_sys->i_fd );
139         free( psz_device );
140         free( p_sys );
141         return VLC_ETHREAD;
142     }
143
144     p_aout->output.pf_setformat = SetFormat;
145     p_aout->output.pf_play = Play;
146
147     return VLC_SUCCESS;
148 }
149
150 /*****************************************************************************
151  * SetFormat: reset the dsp and set its format
152  *****************************************************************************
153  * This functions resets the DSP device, tries to initialize the output
154  * format with the value contained in the dsp structure, and if this value
155  * could not be set, the default value returned by ioctl is set. It then
156  * does the same for the stereo mode, and for the output rate.
157  *****************************************************************************/
158 static int SetFormat( aout_instance_t *p_aout )
159 {
160     struct aout_sys_t * p_sys = p_aout->output.p_sys;
161     int i_format;
162     int i_rate;
163     int i_fragments;
164     vlc_bool_t b_stereo;
165
166     p_sys->b_initialized = VLC_FALSE;
167
168     /* Reset the DSP device */
169     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
170     {
171         msg_Err( p_aout, "cannot reset OSS audio device" );
172         return VLC_EGENERIC;
173     }
174
175     /* Set the fragment size */
176     i_fragments = FRAME_COUNT << 16 | FRAME_SIZE;
177     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFRAGMENT, &i_fragments ) < 0 )
178     {
179         msg_Err( p_aout, "cannot set fragment size (%.8x)", i_fragments );
180         return VLC_EGENERIC;
181     }
182
183     /* Set the output format */
184     if ( p_aout->output.output.i_format == AOUT_FMT_SPDIF )
185     {
186         i_format = AOUT_FMT_SPDIF;
187         p_aout->output.i_nb_samples = A52_FRAME_NB;
188         p_aout->output.output.i_bytes_per_frame = AOUT_SPDIF_SIZE;
189         p_aout->output.output.i_frame_length = A52_FRAME_NB;
190     }
191     else
192     {
193         p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
194         p_aout->output.i_nb_samples = FRAME_SIZE;
195     }
196
197     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
198          || i_format != p_aout->output.output.i_format )
199     {
200         msg_Err( p_aout, "cannot set audio output format (%i)", i_format );
201         return VLC_EGENERIC;
202     }
203
204     if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
205     {
206         /* FIXME */
207         if ( p_aout->output.output.i_channels > 2 )
208         {
209             msg_Warn( p_aout, "only two channels are supported at the moment" );
210             /* Trigger downmixing */
211             p_aout->output.output.i_channels = 2;
212         }
213
214         /* Set the number of channels */
215         b_stereo = p_aout->output.output.i_channels - 1;
216
217         if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
218         {
219             msg_Err( p_aout, "cannot set number of audio channels (%i)",
220                               p_aout->output.output.i_channels );
221             return VLC_EGENERIC;
222         }
223
224         if ( b_stereo + 1 != p_aout->output.output.i_channels )
225         {
226             msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
227                               p_aout->output.output.i_channels,
228                               b_stereo + 1 );
229             p_aout->output.output.i_channels = b_stereo + 1;
230         }
231
232         /* Set the output rate */
233         i_rate = p_aout->output.output.i_rate;
234         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
235         {
236             msg_Err( p_aout, "cannot set audio output rate (%i)",
237                              p_aout->output.output.i_rate );
238             return VLC_EGENERIC;
239         }
240
241         if( i_rate != p_aout->output.output.i_rate )
242         {
243             msg_Warn( p_aout, "driver forced resampling %li->%li",
244                               p_aout->output.output.i_rate, i_rate );
245             p_aout->output.output.i_rate = i_rate;
246         }
247     }
248
249     p_sys->b_initialized = VLC_TRUE;
250
251     return VLC_SUCCESS;
252 }
253
254 /*****************************************************************************
255  * Play: queue a buffer for playing by OSSThread
256  *****************************************************************************/
257 static void Play( aout_instance_t *p_aout )
258 {
259 }
260
261 /*****************************************************************************
262  * Close: close the dsp audio device
263  *****************************************************************************/
264 static void Close( vlc_object_t * p_this )
265 {
266     aout_instance_t *p_aout = (aout_instance_t *)p_this;
267     struct aout_sys_t * p_sys = p_aout->output.p_sys;
268
269     p_aout->b_die = VLC_TRUE;
270     vlc_thread_join( p_aout );
271     p_aout->b_die = VLC_FALSE;
272
273     ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL );
274     close( p_sys->i_fd );
275
276     free( p_sys );
277 }
278
279
280 /*****************************************************************************
281  * BufferDuration: buffer status query
282  *****************************************************************************
283  * This function returns the duration in microseconfs of the current buffer.
284  *****************************************************************************/
285 static mtime_t BufferDuration( aout_instance_t * p_aout )
286 {
287     struct aout_sys_t * p_sys = p_aout->output.p_sys;
288     audio_buf_info audio_buf;
289     int i_bytes;
290
291     /* Fill the audio_buf_info structure:
292      * - fragstotal: total number of fragments allocated
293      * - fragsize: size of a fragment in bytes
294      * - bytes: available space in bytes (includes partially used fragments)
295      * Note! 'bytes' could be more than fragments*fragsize */
296     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
297
298     /* calculate number of available fragments (not partially used ones) */
299     i_bytes = (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes;
300
301     /* Return the fragment duration */
302     return (mtime_t)i_bytes * 1000000
303             / p_aout->output.output.i_bytes_per_frame
304             / p_aout->output.output.i_rate
305             * p_aout->output.output.i_frame_length;
306 }
307
308 /*****************************************************************************
309  * OSSThread: asynchronous thread used to DMA the data to the device
310  *****************************************************************************/
311 static int OSSThread( aout_instance_t * p_aout )
312 {
313     struct aout_sys_t * p_sys = p_aout->output.p_sys;
314     mtime_t next_date = 0;
315
316     while ( !p_aout->b_die )
317     {
318         aout_buffer_t * p_buffer;
319         int i_tmp, i_size;
320         byte_t * p_bytes;
321
322         if( !p_sys->b_initialized )
323         {
324             msleep( THREAD_SLEEP );
325             continue;
326         }
327
328         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
329         {
330             mtime_t buffered = BufferDuration( p_aout );
331
332             /* Wait a bit - we don't want our buffer to be full */
333             while( buffered > AOUT_PTS_TOLERANCE * 2 )
334             {
335                 msleep( buffered / 2 - 10000 );
336                 buffered = BufferDuration( p_aout );
337             }
338
339             if( !next_date )
340             {
341                 /* This is the _real_ presentation date */
342                 next_date = mdate() + buffered;
343             }
344             else
345             {
346                 /* Give a hint to the audio output about our drift, but
347                  * not too much because we want to make it happy with our
348                  * nicely calculated dates. */
349                 next_date = ( (next_date * 7) + (mdate() + buffered) ) / 8;
350             }
351
352             /* Next buffer will be played at mdate()+buffered */
353             p_buffer = aout_OutputNextBuffer( p_aout, next_date, VLC_FALSE );
354         }
355         else
356         {
357             p_buffer = aout_OutputNextBuffer( p_aout, 0, VLC_TRUE );
358         }
359
360         if ( p_buffer != NULL )
361         {
362             p_bytes = p_buffer->p_buffer;
363             i_size = p_buffer->i_nb_bytes;
364             /* This is theoretical ... we'll see next iteration whether
365              * we're drifting */
366             next_date += p_buffer->end_date - p_buffer->start_date;
367         }
368         else
369         {
370             i_size = FRAME_SIZE / p_aout->output.output.i_frame_length
371                       * p_aout->output.output.i_bytes_per_frame;
372             p_bytes = malloc( i_size );
373             memset( p_bytes, 0, i_size );
374             next_date = 0;
375         }
376
377         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
378
379         if( i_tmp < 0 )
380         {
381             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
382         }
383
384         if ( p_buffer != NULL )
385         {
386             aout_BufferFree( p_buffer );
387         }
388         else
389         {
390             free( p_bytes );
391         }
392     }
393
394     return VLC_SUCCESS;
395 }