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 $
7 * Authors: Michel Kaempf <maxx@via.ecp.fr>
8 * Samuel Hocevar <sam@zoy.org>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
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() */
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>
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 *****************************************************************************/
56 audio_buf_info audio_buf;
58 /* Path to the audio output device */
63 /*****************************************************************************
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 * );
73 /*****************************************************************************
74 * Capabilities defined in the other files.
75 *****************************************************************************/
77 /*****************************************************************************
78 * Build configuration tree.
79 *****************************************************************************/
81 ADD_CATEGORY_HINT( N_("Miscellaneous"), NULL )
82 ADD_FILE ( "dspdev", "/dev/dsp", NULL, N_("OSS dsp device"), NULL )
86 SET_DESCRIPTION( _("Linux OSS /dev/dsp module") )
87 ADD_CAPABILITY( AOUT, 100 )
91 aout_getfunctions( &p_module->p_functions->aout );
94 MODULE_DEACTIVATE_START
95 MODULE_DEACTIVATE_STOP
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 )
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;
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 )
118 /* Allocate structure */
119 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
120 if( p_aout->p_sys == NULL )
122 msg_Err( p_aout, "out of memory" );
126 /* Initialize some variables */
127 if( !(p_aout->p_sys->psz_device = config_GetPsz( p_aout, "dspdev" )) )
129 msg_Err( p_aout, "don't know which audio device to open" );
130 free( p_aout->p_sys );
134 /* Open the sound device */
135 if( (p_aout->p_sys->i_fd = open( p_aout->p_sys->psz_device, O_WRONLY ))
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 );
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 )
162 /* Reset the DSP device */
163 if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
165 msg_Err( p_aout, "cannot reset audio device (%s)",
166 p_aout->p_sys->psz_device );
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 )
174 msg_Err( p_aout, "cannot set audio output format (%i)",
179 if( i_format != p_aout->i_format )
181 msg_Warn( p_aout, "audio output format not supported (%i)",
183 p_aout->i_format = i_format;
186 /* Set the number of channels */
187 b_stereo = ( p_aout->i_channels >= 2 );
189 if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
191 msg_Err( p_aout, "cannot set number of audio channels (%i)",
192 p_aout->i_channels );
196 if( (1 + b_stereo) != p_aout->i_channels )
198 msg_Warn( p_aout, "%i audio channels not supported",
199 p_aout->i_channels );
200 p_aout->i_channels = 1 + b_stereo;
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 )
207 msg_Err( p_aout, "cannot set audio output rate (%i)", p_aout->i_rate );
211 if( i_rate != p_aout->i_rate )
213 msg_Warn( p_aout, "audio output rate not supported (%li)",
215 p_aout->i_rate = i_rate;
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 )
233 ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_GETOSPACE,
234 &p_aout->p_sys->audio_buf );
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 );
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 )
250 i_tmp = write( p_aout->p_sys->i_fd, buffer, i_size );
254 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
258 /*****************************************************************************
259 * aout_Close: closes the dsp audio device
260 *****************************************************************************/
261 static void aout_Close( aout_thread_t *p_aout )
263 close( p_aout->p_sys->i_fd );
264 free( p_aout->p_sys->psz_device );