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 $
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 int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
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 );
73 /*****************************************************************************
75 *****************************************************************************/
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 );
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 )
92 aout_thread_t *p_aout = (aout_thread_t *)p_this;
94 /* Allocate structure */
95 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
96 if( p_aout->p_sys == NULL )
98 msg_Err( p_aout, "out of memory" );
102 /* Initialize some variables */
103 if( !(p_aout->p_sys->psz_device = config_GetPsz( p_aout, "dspdev" )) )
105 msg_Err( p_aout, "don't know which audio device to open" );
106 free( p_aout->p_sys );
110 p_aout->pf_setformat = SetFormat;
111 p_aout->pf_getbufinfo = GetBufInfo;
112 p_aout->pf_play = Play;
114 /* Open the sound device */
115 if( (p_aout->p_sys->i_fd = open( p_aout->p_sys->psz_device, O_WRONLY ))
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 );
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 )
142 /* Reset the DSP device */
143 if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_RESET, NULL ) < 0 )
145 msg_Err( p_aout, "cannot reset audio device (%s)",
146 p_aout->p_sys->psz_device );
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 )
154 msg_Err( p_aout, "cannot set audio output format (%i)",
159 if( i_format != p_aout->i_format )
161 msg_Warn( p_aout, "audio output format not supported (%i)",
163 p_aout->i_format = i_format;
166 /* Set the number of channels */
167 b_stereo = ( p_aout->i_channels >= 2 );
169 if( ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_STEREO, &b_stereo ) < 0 )
171 msg_Err( p_aout, "cannot set number of audio channels (%i)",
172 p_aout->i_channels );
176 if( (1 + b_stereo) != p_aout->i_channels )
178 msg_Warn( p_aout, "%i audio channels not supported",
179 p_aout->i_channels );
180 p_aout->i_channels = 1 + b_stereo;
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 )
187 msg_Err( p_aout, "cannot set audio output rate (%i)", p_aout->i_rate );
191 if( i_rate != p_aout->i_rate )
193 msg_Warn( p_aout, "audio output rate not supported (%li)",
195 p_aout->i_rate = i_rate;
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 )
213 ioctl( p_aout->p_sys->i_fd, SNDCTL_DSP_GETOSPACE,
214 &p_aout->p_sys->audio_buf );
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 );
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 )
230 i_tmp = write( p_aout->p_sys->i_fd, buffer, i_size );
234 msg_Err( p_aout, "write failed (%s)", strerror(errno) );
238 /*****************************************************************************
239 * Close: closes the dsp audio device
240 *****************************************************************************/
241 static void Close( vlc_object_t *p_this )
243 aout_thread_t *p_aout = (aout_thread_t *)p_this;
245 close( p_aout->p_sys->i_fd );
246 free( p_aout->p_sys->psz_device );