]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
Audio output 3. Expect major breakages.
[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.1 2002/08/07 21:36:55 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 #include "aout_internal.h"
44
45 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
46  * SNDCTL_DSP_GETOSPACE */
47 #ifdef HAVE_SOUNDCARD_H
48 #   include <soundcard.h>
49 #elif defined( HAVE_SYS_SOUNDCARD_H )
50 #   include <sys/soundcard.h>
51 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
52 #   include <machine/soundcard.h>
53 #endif
54
55 /*****************************************************************************
56  * aout_sys_t: OSS audio output method descriptor
57  *****************************************************************************
58  * This structure is part of the audio output thread descriptor.
59  * It describes the dsp specific properties of an audio device.
60  *****************************************************************************/
61 struct aout_sys_t
62 {
63     int                   i_fd;
64     volatile vlc_bool_t   b_die;
65 };
66
67 #define DEFAULT_FRAME_SIZE 2048
68
69 /*****************************************************************************
70  * Local prototypes
71  *****************************************************************************/
72 static int  Open         ( vlc_object_t * );
73 static void Close        ( vlc_object_t * );
74
75 static int  SetFormat    ( aout_instance_t * );
76 static void Play         ( aout_instance_t *, aout_buffer_t * );
77 static int  OSSThread    ( aout_instance_t * );
78
79 /*****************************************************************************
80  * Module descriptor
81  *****************************************************************************/
82 vlc_module_begin();
83     add_category_hint( N_("Audio"), NULL );
84     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
85     set_description( _("Linux OSS /dev/dsp module") );
86     set_capability( "audio output", 100 );
87     set_callbacks( Open, Close );
88 vlc_module_end();
89
90 /*****************************************************************************
91  * Open: open the audio device (the digital sound processor)
92  *****************************************************************************
93  * This function opens the dsp as a usual non-blocking write-only file, and
94  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
95  *****************************************************************************/
96 static int Open( vlc_object_t *p_this )
97 {
98     aout_instance_t * p_aout = (aout_instance_t *)p_this;
99     struct aout_sys_t * p_sys;
100     char * psz_device;
101
102     /* Allocate structure */
103     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
104     if( p_sys == NULL )
105     {
106         msg_Err( p_aout, "out of memory" );
107         return 1;
108     }
109
110     /* Initialize some variables */
111     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
112     {
113         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
114         free( p_aout->p_sys );
115         return -1;
116     }
117
118     /* Open the sound device */
119     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
120     {
121         msg_Err( p_aout, "cannot open audio device (%s)",
122                           psz_device );
123         free( psz_device );
124         free( p_sys );
125         return -1;
126     }
127     free( psz_device );
128
129     /* Create OSS thread and wait for its readiness. */
130     p_sys->b_die = 0;
131     if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_TRUE ) )
132     {
133         msg_Err( p_input, "cannot create OSS thread (%s)", strerror(errno) );
134         free( p_aout->p_sys->psz_device );
135         free( p_aout->p_sys );
136         return -1;
137     }
138
139     p_aout->pf_setformat = SetFormat;
140     p_aout->pf_play = Play;
141
142     return 0;
143 }
144
145 /*****************************************************************************
146  * SetFormat: reset the dsp and set its format
147  *****************************************************************************
148  * This functions resets the DSP device, tries to initialize the output
149  * format with the value contained in the dsp structure, and if this value
150  * could not be set, the default value returned by ioctl is set. It then
151  * does the same for the stereo mode, and for the output rate.
152  *****************************************************************************/
153 static int SetFormat( aout_instance_t *p_aout )
154 {
155     struct aout_sys_t * p_sys = p_aout->output.p_sys;
156     int i_format;
157     int i_rate;
158     vlc_bool_t b_stereo;
159
160     /* Reset the DSP device */
161     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
162     {
163         msg_Err( p_aout, "cannot reset OSS audio device" );
164         return -1;
165     }
166
167     /* Set the output format */
168     i_format = AOUT_FMT_S16_NE;
169     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
170          || i_format != AOUT_FMT_S16_NE )
171     {
172         msg_Err( p_aout, "cannot set audio output format (%i)",
173                           i_format );
174         return -1;
175     }
176     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
177
178     /* FIXME */
179     if ( p_aout->output.output.i_channels > 2 )
180     {
181         msg_Warn( p_aout, "only two channels are supported at the moment" );
182         /* Trigger downmixing */
183         p_aout->output.output.i_channels = 2;
184     }
185
186     /* Set the number of channels */
187     b_stereo = p_aout->output.output.i_channels - 1;
188
189     if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
190     {
191         msg_Err( p_aout, "cannot set number of audio channels (%i)",
192                           p_aout->output.output.i_channels );
193         return -1;
194     }
195
196     if ( b_stereo + 1 != p_aout->output.output.i_channels )
197     {
198         msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
199                           p_aout->output.output.i_channels,
200                           b_stereo + 1 );
201         p_aout->output.output.i_channels = b_stereo + 1;
202     }
203
204     /* Set the output rate */
205     i_rate = p_aout->output.output.i_rate;
206     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
207     {
208         msg_Err( p_aout, "cannot set audio output rate (%i)", p_aout->i_rate );
209         return -1;
210     }
211
212     if( i_rate != p_aout->output.output.i_rate )
213     {
214         msg_Warn( p_aout, "driver forced resampling %li->%li",
215                           p_aout->output.output.i_rate, i_rate );
216         p_aout->output.output.i_rate = i_rate;
217     }
218
219     p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
220
221     return 0;
222 }
223
224 /*****************************************************************************
225  * Play: queue a buffer for playing by OSSThread
226  *****************************************************************************/
227 static void Play( aout_instance_t *p_aout, aout_buffer_t * p_buffer )
228 {
229     aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
230 }
231
232 /*****************************************************************************
233  * Close: close the dsp audio device
234  *****************************************************************************/
235 static void Close( vlc_object_t * p_this )
236 {
237     aout_instance_t *p_aout = (aout_instance_t *)p_this;
238     struct aout_sys_t * p_sys = p_aout->output.p_sys;
239
240     p_sys->b_die = 1;
241     vlc_thread_join( p_aout );
242
243     close( p_aout->p_sys->i_fd );
244     free( p_aout->p_sys );
245 }
246
247
248 /*****************************************************************************
249  * GetBufInfo: buffer status query
250  *****************************************************************************
251  * This function fills in the audio_buf_info structure :
252  * - returns : number of available fragments (not partially used ones)
253  * - int fragstotal : total number of fragments allocated
254  * - int fragsize : size of a fragment in bytes
255  * - int bytes : available space in bytes (includes partially used fragments)
256  * Note! 'bytes' could be more than fragments*fragsize
257  *****************************************************************************/
258 static int GetBufInfo( aout_instance_t * p_aout )
259 {
260     struct aout_sys_t * p_sys = p_aout->output.p_sys;
261     audio_buf_info audio_buf;
262
263     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
264
265     /* returns the allocated space in bytes */
266     return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
267 }
268
269 /*****************************************************************************
270  * OSSThread: asynchronous thread used to DMA the data to the device
271  *****************************************************************************/
272 static int OSSThread( aout_instance_t * p_aout )
273 {
274     struct aout_sys_t * p_sys = p_aout->output.p_sys;
275
276     while ( !p_sys->b_die )
277     {
278         int i_bytes_per_sample = aout_FormatToBytes( &p_aout->output.output );
279         aout_buffer_t * p_buffer;
280         mtime_t next_date;
281         int i_tmp;
282         char * p_bytes;
283
284         /* Get the presentation date of the next write() operation. It
285          * is equal to the current date + duration of buffered samples.
286          * Order is important here, since GetBufInfo is believed to take
287          * more time than mdate(). */
288         next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
289                       / i_bytes_per_sample
290                       / p_aout->output.output.i_rate;
291         next_date += mdate();
292
293         p_buffer = aout_OutputNextBuffer( p_aout, next_date );
294
295         if ( p_buffer != NULL )
296         {
297             p_bytes = p_buffer->p_buffer;
298         }
299         else
300         {
301             p_bytes = alloca( DEFAULT_FRAME_SIZE * i_bytes_per_sample );
302             memset( p_bytes, 0, DEFAULT_FRAME_SIZE * i_bytes_per_sample );
303         }
304
305         i_tmp = write( p_sys->i_fd, p_bytes,
306                        DEFAULT_FRAME_SIZE * i_bytes_per_sample );
307
308         if( i_tmp < 0 )
309         {
310             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
311         }
312
313         if ( p_buffer != NULL )
314         {
315             aout_BufferFree( p_buffer );
316         }
317     }
318 }