]> git.sesse.net Git - vlc/blob - plugins/dsp/dsp.c
* ALL: new module API. Makes a few things a lot simpler, and we gain
[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.19 2002/07/31 20:56:51 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_t
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 int  Open         ( vlc_object_t * );
67 static void Close        ( vlc_object_t * );
68
69 static int  SetFormat    ( aout_thread_t * );
70 static int  GetBufInfo   ( aout_thread_t *, int );
71 static void Play         ( aout_thread_t *, byte_t *, int );
72
73 /*****************************************************************************
74  * Module descriptor
75  *****************************************************************************/
76 vlc_module_begin();
77     add_category_hint( N_("Miscellaneous"), NULL );
78     add_file( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL );
79     set_description( _("Linux OSS /dev/dsp module") );
80     set_capability( "audio output", 100 );
81     set_callbacks( Open, Close );
82 vlc_module_end();
83
84 /*****************************************************************************
85  * Open: opens the audio device (the digital sound processor)
86  *****************************************************************************
87  * This function opens the dsp as a usual non-blocking write-only file, and
88  * modifies the p_aout->p_sys->i_fd with the file's descriptor.
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {
92     aout_thread_t *p_aout = (aout_thread_t *)p_this;
93
94     /* Allocate structure */
95     p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
96     if( p_aout->p_sys == NULL )
97     {
98         msg_Err( p_aout, "out of memory" );
99         return( 1 );
100     }
101
102     /* Initialize some variables */
103     if( !(p_aout->p_sys->psz_device = config_GetPsz( p_aout, "dspdev" )) )
104     {
105         msg_Err( p_aout, "don't know which audio device to open" );
106         free( p_aout->p_sys );
107         return( -1 );
108     }
109
110     p_aout->pf_setformat = SetFormat;
111     p_aout->pf_getbufinfo = GetBufInfo;
112     p_aout->pf_play = Play;
113
114     /* Open the sound device */
115     if( (p_aout->p_sys->i_fd = open( p_aout->p_sys->psz_device, O_WRONLY ))
116         < 0 )
117     {
118         msg_Err( p_aout, "cannot open audio device (%s)",
119                           p_aout->p_sys->psz_device );
120         free( p_aout->p_sys->psz_device );
121         free( p_aout->p_sys );
122         return( -1 );
123     }
124
125     return( 0 );
126 }
127
128 /*****************************************************************************
129  * SetFormat: resets the dsp and sets its format
130  *****************************************************************************
131  * This functions resets the DSP device, tries to initialize the output
132  * format with the value contained in the dsp structure, and if this value
133  * could not be set, the default value returned by ioctl is set. It then
134  * does the same for the stereo mode, and for the output rate.
135  *****************************************************************************/
136 static int SetFormat( aout_thread_t *p_aout )
137 {
138     int i_format;
139     int i_rate;
140     vlc_bool_t b_stereo;
141
142     /* Reset the DSP device */
143     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
144     {
145         msg_Err( p_aout, "cannot reset audio device (%s)",
146                           p_aout->p_sys->psz_device );
147         return( -1 );
148     }
149
150     /* Set the output format */
151     i_format = p_aout->i_format;
152     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0 )
153     {
154         msg_Err( p_aout, "cannot set audio output format (%i)",
155                           p_aout->i_format );
156         return( -1 );
157     }
158
159     if( i_format != p_aout->i_format )
160     {
161         msg_Warn( p_aout, "audio output format not supported (%i)",
162                               p_aout->i_format );
163         p_aout->i_format = i_format;
164     }
165
166     /* Set the number of channels */
167     b_stereo = ( p_aout->i_channels >= 2 );
168
169     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
170     {
171         msg_Err( p_aout, "cannot set number of audio channels (%i)",
172                           p_aout->i_channels );
173         return( -1 );
174     }
175
176     if( (1 + b_stereo) != p_aout->i_channels )
177     {
178         msg_Warn( p_aout, "%i audio channels not supported",
179                            p_aout->i_channels );
180         p_aout->i_channels = 1 + b_stereo;
181     }
182
183     /* Set the output rate */
184     i_rate = p_aout->i_rate;
185     if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_SPEED, &i_rate ) < 0 )
186     {
187         msg_Err( p_aout, "cannot set audio output rate (%i)", p_aout->i_rate );
188         return( -1 );
189     }
190
191     if( i_rate != p_aout->i_rate )
192     {
193         msg_Warn( p_aout, "audio output rate not supported (%li)",
194                           p_aout->i_rate );
195         p_aout->i_rate = i_rate;
196     }
197
198     return( 0 );
199 }
200
201 /*****************************************************************************
202  * GetBufInfo: buffer status query
203  *****************************************************************************
204  * This function fills in the audio_buf_info structure :
205  * - returns : number of available fragments (not partially used ones)
206  * - int fragstotal : total number of fragments allocated
207  * - int fragsize : size of a fragment in bytes
208  * - int bytes : available space in bytes (includes partially used fragments)
209  * Note! 'bytes' could be more than fragments*fragsize
210  *****************************************************************************/
211 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
212 {
213     ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_GETOSPACE,
214            &p_aout->p_sys->audio_buf );
215
216     /* returns the allocated space in bytes */
217     return ( (p_aout->p_sys->audio_buf.fragstotal
218                  * p_aout->p_sys->audio_buf.fragsize)
219             - p_aout->p_sys->audio_buf.bytes );
220 }
221
222 /*****************************************************************************
223  * Play: plays a sound samples buffer
224  *****************************************************************************
225  * This function writes a buffer of i_length bytes in the dsp
226  *****************************************************************************/
227 static void Play( aout_thread_t *p_aout, byte_t *buffer, int i_size )
228 {
229     int i_tmp;
230     i_tmp = write( p_aout->p_sys->i_fd, buffer, i_size );
231
232     if( i_tmp < 0 )
233     {
234         msg_Err( p_aout, "write failed (%s)", strerror(errno) );
235     }
236 }
237
238 /*****************************************************************************
239  * Close: closes the dsp audio device
240  *****************************************************************************/
241 static void Close( vlc_object_t *p_this )
242 {
243     aout_thread_t *p_aout = (aout_thread_t *)p_this;
244
245     close( p_aout->p_sys->i_fd );
246     free( p_aout->p_sys->psz_device );
247 }