]> git.sesse.net Git - vlc/blob - plugins/dsp/dsp.c
* ALL: the first libvlc commit.
[vlc] / plugins / dsp / dsp.c
1 /*****************************************************************************
2  * dsp.c : OSS /dev/dsp module for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2001 VideoLAN
5  * $Id: dsp.c,v 1.17 2002/06/01 12:31:58 sam Exp $
6  *
7  * Authors: Michel Kaempf <maxx@via.ecp.fr>
8  *          Samuel Hocevar <sam@zoy.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  * 
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <errno.h>                                                 /* ENOMEM */
29 #include <fcntl.h>                                       /* open(), O_WRONLY */
30 #include <sys/ioctl.h>                                            /* ioctl() */
31 #include <string.h>                                            /* strerror() */
32 #include <unistd.h>                                      /* write(), close() */
33 #include <stdlib.h>                            /* calloc(), malloc(), free() */
34
35 #include <vlc/vlc.h>
36 #include <vlc/aout.h>
37
38 /* SNDCTL_DSP_RESET, SNDCTL_DSP_SETFMT, SNDCTL_DSP_STEREO, SNDCTL_DSP_SPEED,
39  * SNDCTL_DSP_GETOSPACE */
40 #ifdef HAVE_SOUNDCARD_H
41 #   include <soundcard.h>
42 #elif defined( HAVE_SYS_SOUNDCARD_H )
43 #   include <sys/soundcard.h>
44 #elif defined( HAVE_MACHINE_SOUNDCARD_H )
45 #   include <machine/soundcard.h>
46 #endif
47
48 /*****************************************************************************
49  * aout_sys_t: dsp audio output method descriptor
50  *****************************************************************************
51  * This structure is part of the audio output thread descriptor.
52  * It describes the dsp specific properties of an audio device.
53  *****************************************************************************/
54 struct aout_sys_s
55 {
56     audio_buf_info        audio_buf;
57
58     /* Path to the audio output device */
59     char *                psz_device;
60     int                   i_fd;
61 };
62
63 /*****************************************************************************
64  * Local prototypes.
65  *****************************************************************************/
66 static void aout_getfunctions ( function_list_t * );
67 static int  aout_Open         ( aout_thread_t * );
68 static int  aout_SetFormat    ( aout_thread_t * );
69 static int  aout_GetBufInfo   ( aout_thread_t *, int );
70 static void aout_Play         ( aout_thread_t *, byte_t *, int );
71 static void aout_Close        ( aout_thread_t * );
72
73 /*****************************************************************************
74  * Capabilities defined in the other files.
75  *****************************************************************************/
76
77 /*****************************************************************************
78  * Build configuration tree.
79  *****************************************************************************/
80 MODULE_CONFIG_START
81 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
82 ADD_FILE  ( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL )
83 MODULE_CONFIG_STOP
84
85 MODULE_INIT_START
86     SET_DESCRIPTION( _("Linux OSS /dev/dsp module") )
87     ADD_CAPABILITY( AOUT, 100 )
88 MODULE_INIT_STOP
89
90 MODULE_ACTIVATE_START
91     aout_getfunctions( &p_module->p_functions->aout );
92 MODULE_ACTIVATE_STOP
93
94 MODULE_DEACTIVATE_START
95 MODULE_DEACTIVATE_STOP
96
97 /*****************************************************************************
98  * Functions exported as capabilities. They are declared as static so that
99  * we don't pollute the namespace too much.
100  *****************************************************************************/
101 static void aout_getfunctions( function_list_t * p_function_list )
102 {
103     p_function_list->functions.aout.pf_open = aout_Open;
104     p_function_list->functions.aout.pf_setformat = aout_SetFormat;
105     p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
106     p_function_list->functions.aout.pf_play = aout_Play;
107     p_function_list->functions.aout.pf_close = aout_Close;
108 }
109
110 /*****************************************************************************
111  * aout_Open: opens the audio device (the digital sound processor)
112  *****************************************************************************
113  * This function opens the dsp as a usual non-blocking write-only file, and
114  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
115  *****************************************************************************/
116 static int aout_Open( aout_thread_t *p_aout )
117 {
118     /* Allocate structure */
119     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
120     if( p_aout->p_sys == NULL )
121     {
122         msg_Err( p_aout, "out of memory" );
123         return( 1 );
124     }
125
126     /* Initialize some variables */
127     if( !(p_aout->p_sys->psz_device = config_GetPsz( p_aout, "dspdev" )) )
128     {
129         msg_Err( p_aout, "don't know which audio device to open" );
130         free( p_aout->p_sys );
131         return( -1 );
132     }
133
134     /* Open the sound device */
135     if( (p_aout->p_sys->i_fd = open( p_aout->p_sys->psz_device, O_WRONLY ))
136         < 0 )
137     {
138         msg_Err( p_aout, "cannot open audio device (%s)",
139                           p_aout->p_sys->psz_device );
140         free( p_aout->p_sys->psz_device );
141         free( p_aout->p_sys );
142         return( -1 );
143     }
144
145     return( 0 );
146 }
147
148 /*****************************************************************************
149  * aout_SetFormat: resets the dsp and sets its format
150  *****************************************************************************
151  * This functions resets the DSP device, tries to initialize the output
152  * format with the value contained in the dsp structure, and if this value
153  * could not be set, the default value returned by ioctl is set. It then
154  * does the same for the stereo mode, and for the output rate.
155  *****************************************************************************/
156 static int aout_SetFormat( aout_thread_t *p_aout )
157 {
158     int i_format;
159     int i_rate;
160     vlc_bool_t b_stereo;
161
162     /* Reset the DSP device */
163     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
164     {
165         msg_Err( p_aout, "cannot reset audio device (%s)",
166                           p_aout->p_sys->psz_device );
167         return( -1 );
168     }
169
170     /* Set the output format */
171     i_format = p_aout->i_format;
172     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
173     {
174         msg_Err( p_aout, "cannot set audio output format (%i)",
175                           p_aout->i_format );
176         return( -1 );
177     }
178
179     if( i_format != p_aout->i_format )
180     {
181         msg_Warn( p_aout, "audio output format not supported (%i)",
182                               p_aout->i_format );
183         p_aout->i_format = i_format;
184     }
185
186     /* Set the number of channels */
187     b_stereo = ( p_aout->i_channels >= 2 );
188
189     if( ioctl( p_aout->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->i_channels );
193         return( -1 );
194     }
195
196     if( (1 + b_stereo) != p_aout->i_channels )
197     {
198         msg_Warn( p_aout, "%i audio channels not supported",
199                            p_aout->i_channels );
200         p_aout->i_channels = 1 + b_stereo;
201     }
202
203     /* Set the output rate */
204     i_rate = p_aout->i_rate;
205     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
206     {
207         msg_Err( p_aout, "cannot set audio output rate (%i)", p_aout->i_rate );
208         return( -1 );
209     }
210
211     if( i_rate != p_aout->i_rate )
212     {
213         msg_Warn( p_aout, "audio output rate not supported (%li)",
214                           p_aout->i_rate );
215         p_aout->i_rate = i_rate;
216     }
217
218     return( 0 );
219 }
220
221 /*****************************************************************************
222  * aout_GetBufInfo: buffer status query
223  *****************************************************************************
224  * This function fills in the audio_buf_info structure :
225  * - returns : number of available fragments (not partially used ones)
226  * - int fragstotal : total number of fragments allocated
227  * - int fragsize : size of a fragment in bytes
228  * - int bytes : available space in bytes (includes partially used fragments)
229  * Note! 'bytes' could be more than fragments*fragsize
230  *****************************************************************************/
231 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
232 {
233     ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_GETOSPACE,
234            &p_aout->p_sys->audio_buf );
235
236     /* returns the allocated space in bytes */
237     return ( (p_aout->p_sys->audio_buf.fragstotal
238                  * p_aout->p_sys->audio_buf.fragsize)
239             - p_aout->p_sys->audio_buf.bytes );
240 }
241
242 /*****************************************************************************
243  * aout_Play: plays a sound samples buffer
244  *****************************************************************************
245  * This function writes a buffer of i_length bytes in the dsp
246  *****************************************************************************/
247 static void aout_Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
248 {
249     int i_tmp;
250     i_tmp = write( p_aout->p_sys->i_fd, buffer, i_size );
251
252     if( i_tmp < 0 )
253     {
254         msg_Err( p_aout, "write failed (%s)", strerror(errno) );
255     }
256 }
257
258 /*****************************************************************************
259  * aout_Close: closes the dsp audio device
260  *****************************************************************************/
261 static void aout_Close( aout_thread_t *p_aout )
262 {
263     close( p_aout->p_sys->i_fd );
264     free( p_aout->p_sys->psz_device );
265 }