]> git.sesse.net Git - vlc/blob - modules/audio_output/oss.c
* ./modules/audio_output/oss.c: we spare a variable by using p_aout->b_die
[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.8 2002/08/13 11:59:36 sam 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 DEFAULT_FRAME_SIZE 2048
69
70 /*****************************************************************************
71  * Local prototypes
72  *****************************************************************************/
73 static int  Open         ( vlc_object_t * );
74 static void Close        ( vlc_object_t * );
75
76 static int  SetFormat    ( aout_instance_t * );
77 static void Play         ( aout_instance_t *, aout_buffer_t * );
78 static int  OSSThread    ( aout_instance_t * );
79
80 /*****************************************************************************
81  * Module descriptor
82  *****************************************************************************/
83 vlc_module_begin();
84     add_category_hint( N_("Audio"), NULL );
85     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
86     set_description( _("Linux OSS /dev/dsp module") );
87     set_capability( "audio output", 100 );
88     set_callbacks( Open, Close );
89 vlc_module_end();
90
91 /*****************************************************************************
92  * Open: open the audio device (the digital sound processor)
93  *****************************************************************************
94  * This function opens the dsp as a usual non-blocking write-only file, and
95  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     aout_instance_t * p_aout = (aout_instance_t *)p_this;
100     struct aout_sys_t * p_sys;
101     char * psz_device;
102
103     /* Allocate structure */
104     p_aout->output.p_sys = p_sys = malloc( sizeof( aout_sys_t ) );
105     if( p_sys == NULL )
106     {
107         msg_Err( p_aout, "out of memory" );
108         return 1;
109     }
110
111     /* Initialize some variables */
112     if( (psz_device = config_GetPsz( p_aout, "dspdev" )) == NULL )
113     {
114         msg_Err( p_aout, "no audio device given (maybe /dev/dsp ?)" );
115         free( p_sys );
116         return -1;
117     }
118
119     /* Open the sound device */
120     if( (p_sys->i_fd = open( psz_device, O_WRONLY )) < 0 )
121     {
122         msg_Err( p_aout, "cannot open audio device (%s)", 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_initialized = VLC_FALSE;
131     if( vlc_thread_create( p_aout, "aout", OSSThread, VLC_FALSE ) )
132     {
133         msg_Err( p_aout, "cannot create OSS thread (%s)", strerror(errno) );
134         free( psz_device );
135         free( p_sys );
136         return -1;
137     }
138
139     p_aout->output.pf_setformat = SetFormat;
140     p_aout->output.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     p_sys->b_initialized = VLC_FALSE;
161
162     /* Reset the DSP device */
163     if( ioctl( p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
164     {
165         msg_Err( p_aout, "cannot reset OSS audio device" );
166         return -1;
167     }
168
169     /* Set the output format */
170     if ( AOUT_FMT_IS_SPDIF( &p_aout->output.output ) )
171     {
172         p_aout->output.output.i_format = i_format = AOUT_FMT_SPDIF;
173         p_aout->output.i_nb_samples = 1;
174     }
175     else
176     {
177         p_aout->output.output.i_format = i_format = AOUT_FMT_S16_NE;
178         p_aout->output.i_nb_samples = DEFAULT_FRAME_SIZE;
179     }
180
181     if( ioctl( p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
182          || i_format != p_aout->output.output.i_format )
183     {
184         msg_Err( p_aout, "cannot set audio output format (%i)",
185                           i_format );
186         return -1;
187     }
188
189     if ( !AOUT_FMT_IS_SPDIF( &p_aout->output.output ) )
190     {
191         /* FIXME */
192         if ( p_aout->output.output.i_channels > 2 )
193         {
194             msg_Warn( p_aout, "only two channels are supported at the moment" );
195             /* Trigger downmixing */
196             p_aout->output.output.i_channels = 2;
197         }
198
199         /* Set the number of channels */
200         b_stereo = p_aout->output.output.i_channels - 1;
201
202         if( ioctl( p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
203         {
204             msg_Err( p_aout, "cannot set number of audio channels (%i)",
205                               p_aout->output.output.i_channels );
206             return -1;
207         }
208
209         if ( b_stereo + 1 != p_aout->output.output.i_channels )
210         {
211             msg_Warn( p_aout, "driver forced up/downmixing %li->%li",
212                               p_aout->output.output.i_channels,
213                               b_stereo + 1 );
214             p_aout->output.output.i_channels = b_stereo + 1;
215         }
216
217         /* Set the output rate */
218         i_rate = p_aout->output.output.i_rate;
219         if( ioctl( p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
220         {
221             msg_Err( p_aout, "cannot set audio output rate (%i)",
222                              p_aout->output.output.i_rate );
223             return -1;
224         }
225
226         if( i_rate != p_aout->output.output.i_rate )
227         {
228             msg_Warn( p_aout, "driver forced resampling %li->%li",
229                               p_aout->output.output.i_rate, i_rate );
230             p_aout->output.output.i_rate = i_rate;
231         }
232     }
233
234     p_sys->b_initialized = VLC_TRUE;
235
236     return 0;
237 }
238
239 /*****************************************************************************
240  * Play: queue a buffer for playing by OSSThread
241  *****************************************************************************/
242 static void Play( aout_instance_t *p_aout, aout_buffer_t * p_buffer )
243 {
244     aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
245 }
246
247 /*****************************************************************************
248  * Close: close the dsp audio device
249  *****************************************************************************/
250 static void Close( vlc_object_t * p_this )
251 {
252     aout_instance_t *p_aout = (aout_instance_t *)p_this;
253     struct aout_sys_t * p_sys = p_aout->output.p_sys;
254
255     p_aout->b_die = 1;
256     vlc_thread_join( p_aout );
257
258     close( p_sys->i_fd );
259     free( p_sys );
260 }
261
262
263 /*****************************************************************************
264  * GetBufInfo: buffer status query
265  *****************************************************************************
266  * This function fills in the audio_buf_info structure :
267  * - returns : number of available fragments (not partially used ones)
268  * - int fragstotal : total number of fragments allocated
269  * - int fragsize : size of a fragment in bytes
270  * - int bytes : available space in bytes (includes partially used fragments)
271  * Note! 'bytes' could be more than fragments*fragsize
272  *****************************************************************************/
273 static int GetBufInfo( aout_instance_t * p_aout )
274 {
275     struct aout_sys_t * p_sys = p_aout->output.p_sys;
276     audio_buf_info audio_buf;
277
278     ioctl( p_sys->i_fd, SNDCTL_DSP_GETOSPACE, &audio_buf );
279
280     /* returns the allocated space in bytes */
281     return ( (audio_buf.fragstotal * audio_buf.fragsize) - audio_buf.bytes );
282 }
283
284 /*****************************************************************************
285  * OSSThread: asynchronous thread used to DMA the data to the device
286  *****************************************************************************/
287 static int OSSThread( aout_instance_t * p_aout )
288 {
289     struct aout_sys_t * p_sys = p_aout->output.p_sys;
290
291     while ( !p_aout->b_die )
292     {
293         aout_buffer_t * p_buffer;
294         mtime_t next_date = 0;
295         int i_tmp, i_size;
296         byte_t * p_bytes;
297
298         if( !p_sys->b_initialized )
299         {
300             msleep( THREAD_SLEEP );
301             continue;
302         }
303
304         if ( p_aout->output.output.i_format != AOUT_FMT_SPDIF )
305         {
306             /* Get the presentation date of the next write() operation. It
307              * is equal to the current date + duration of buffered samples.
308              * Order is important here, since GetBufInfo is believed to take
309              * more time than mdate(). */
310             next_date = (mtime_t)GetBufInfo( p_aout ) * 1000000
311                       / aout_FormatToByterate( &p_aout->output.output,
312                                                p_aout->output.output.i_rate );
313             next_date += mdate();
314         }
315
316         p_buffer = aout_OutputNextBuffer( p_aout, next_date );
317
318         if ( p_buffer != NULL )
319         {
320             p_bytes = p_buffer->p_buffer;
321             i_size = aout_FormatToSize( &p_aout->output.output,
322                                         p_buffer->i_nb_samples );
323         }
324         else
325         {
326             i_size = aout_FormatToSize( &p_aout->output.output,
327                                         DEFAULT_FRAME_SIZE );
328             p_bytes = alloca( i_size );
329             memset( p_bytes, 0, i_size );
330         }
331
332         i_tmp = write( p_sys->i_fd, p_bytes, i_size );
333
334         if( i_tmp < 0 )
335         {
336             msg_Err( p_aout, "write failed (%s)", strerror(errno) );
337         }
338
339         if ( p_buffer != NULL )
340         {
341             aout_BufferFree( p_buffer );
342         }
343     }
344
345     return 0;
346 }