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