1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.6 2002/03/20 23:00:16 gbazin Exp $
7 * Authors: Gildas Bazin <gbazin@netcourrier.com>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <errno.h> /* ENOMEM */
28 #include <fcntl.h> /* open(), O_WRONLY */
29 #include <string.h> /* strerror() */
31 #include <stdio.h> /* "intf_msg.h" */
32 #include <stdlib.h> /* calloc(), malloc(), free() */
34 #include <videolan/vlc.h>
37 #include "audio_output.h" /* aout_thread_t */
39 /*****************************************************************************
40 * Capabilities defined in the other files.
41 *****************************************************************************/
42 void _M( aout_getfunctions )( function_list_t * p_function_list );
44 /*****************************************************************************
45 * Building configuration tree
46 *****************************************************************************/
51 SET_DESCRIPTION( "Win32 waveOut extension module" )
52 ADD_CAPABILITY( AOUT, 250 )
53 ADD_SHORTCUT( "waveout" )
57 _M( aout_getfunctions )( &p_module->p_functions->aout );
60 MODULE_DEACTIVATE_START
61 MODULE_DEACTIVATE_STOP
63 /*****************************************************************************
64 * aout_sys_t: waveOut audio output method descriptor
65 *****************************************************************************
66 * This structure is part of the audio output thread descriptor.
67 * It describes the waveOut specific properties of an audio device.
68 *****************************************************************************/
70 #define NUMBUF 3 /* We use triple buffering to be on the safe side */
72 typedef struct aout_sys_s
74 HWAVEOUT h_waveout; /* handle to waveout instance */
76 WAVEFORMATEX waveformat; /* Audio format */
78 WAVEHDR waveheader[NUMBUF];
82 DWORD dw_counter; /* Number of bytes played since beginning */
86 /*****************************************************************************
88 *****************************************************************************/
89 static int aout_Open ( aout_thread_t *p_aout );
90 static int aout_SetFormat ( aout_thread_t *p_aout );
91 static int aout_GetBufInfo ( aout_thread_t *p_aout, int i_buffer_info );
92 static void aout_Play ( aout_thread_t *p_aout,
93 byte_t *buffer, int i_size );
94 static void aout_Close ( aout_thread_t *p_aout );
97 static int OpenWaveOutDevice( aout_thread_t *p_aout );
99 /*****************************************************************************
100 * Functions exported as capabilities. They are declared as static so that
101 * we don't pollute the namespace too much.
102 *****************************************************************************/
103 void _M( aout_getfunctions )( function_list_t * p_function_list )
105 p_function_list->functions.aout.pf_open = aout_Open;
106 p_function_list->functions.aout.pf_setformat = aout_SetFormat;
107 p_function_list->functions.aout.pf_getbufinfo = aout_GetBufInfo;
108 p_function_list->functions.aout.pf_play = aout_Play;
109 p_function_list->functions.aout.pf_close = aout_Close;
112 /*****************************************************************************
113 * aout_Open: open the audio device
114 *****************************************************************************
115 * This function opens and setups Win32 waveOut
116 *****************************************************************************/
117 static int aout_Open( aout_thread_t *p_aout )
121 intf_WarnMsg( 3, "aout: waveOut aout_Open ");
123 /* Allocate structure */
124 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
126 if( p_aout->p_sys == NULL )
128 intf_ErrMsg( "aout error: %s", strerror(ENOMEM) );
133 /* Initialize some variables */
134 p_aout->p_sys->i_current_buffer = 0;
135 for( i=0; i<NUMBUF; i++)
136 p_aout->p_sys->waveheader[i].lpData = malloc( 1 );
138 return OpenWaveOutDevice( p_aout );
142 /*****************************************************************************
143 * aout_SetFormat: reset the audio device and sets its format
144 *****************************************************************************
145 * This functions set a new audio format.
146 * For this we need to close the current device and create another
147 * one with the desired format.
148 *****************************************************************************/
149 static int aout_SetFormat( aout_thread_t *p_aout )
151 intf_WarnMsg( 3, "aout: WaveOut aout_SetFormat ");
153 /* Check if the format has changed */
155 if( (p_aout->p_sys->waveformat.nChannels != p_aout->i_channels) ||
156 (p_aout->p_sys->waveformat.nSamplesPerSec != p_aout->i_rate) )
158 /* Before calling waveOutClose we must reset the device */
159 waveOutReset( p_aout->p_sys->h_waveout );
161 if( waveOutClose( p_aout->p_sys->h_waveout ) != MMSYSERR_NOERROR )
163 intf_ErrMsg( "aout error: waveOutClose failed" );
166 return OpenWaveOutDevice( p_aout );
172 /*****************************************************************************
173 * aout_GetBufInfo: buffer status query
174 *****************************************************************************
175 * returns the number of bytes in the audio buffer that have not yet been
176 * sent to the sound device.
177 *****************************************************************************/
178 static int aout_GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
182 mmtime.wType = TIME_BYTES;
183 if( (waveOutGetPosition(p_aout->p_sys->h_waveout, &mmtime, sizeof(MMTIME)))
184 != MMSYSERR_NOERROR || (mmtime.wType != TIME_BYTES) )
186 intf_WarnMsg( 3, "aout: aout_GetBufInfo waveOutGetPosition failed");
187 return i_buffer_limit;
192 intf_WarnMsg( 3, "aout: waveOut aout_GetBufInfo: %i",
193 p_aout->p_sys->dw_counter - mmtime.u.cb );
196 return (p_aout->p_sys->dw_counter - mmtime.u.cb);
199 /*****************************************************************************
200 * aout_Play: play a sound buffer
201 *****************************************************************************
202 * This function writes a buffer of i_length bytes
203 *****************************************************************************/
204 static void aout_Play( aout_thread_t *p_aout, byte_t *p_buffer, int i_size )
207 int current_buffer = p_aout->p_sys->i_current_buffer;
209 p_aout->p_sys->i_current_buffer = (current_buffer + 1) % NUMBUF;
211 /* Prepare the buffer */
212 p_aout->p_sys->waveheader[current_buffer].lpData =
213 realloc( p_aout->p_sys->waveheader[current_buffer].lpData, i_size );
214 if( !p_aout->p_sys->waveheader[current_buffer].lpData )
216 intf_ErrMsg( "aout error: aou_Play couldn't alloc buffer" );
219 p_aout->p_sys->waveheader[current_buffer].dwBufferLength = i_size;
220 p_aout->p_sys->waveheader[current_buffer].dwFlags = 0;
222 result = waveOutPrepareHeader( p_aout->p_sys->h_waveout,
223 &p_aout->p_sys->waveheader[current_buffer],
225 if( result != MMSYSERR_NOERROR )
227 intf_ErrMsg( "aout error: waveOutPrepareHeader failed" );
231 /* Send the buffer the waveOut queue */
232 FAST_MEMCPY( p_aout->p_sys->waveheader[current_buffer].lpData,
234 result = waveOutWrite( p_aout->p_sys->h_waveout,
235 &p_aout->p_sys->waveheader[current_buffer],
237 if( result != MMSYSERR_NOERROR )
239 intf_ErrMsg( "aout error: waveOutWrite failed" );
243 /* keep track of number of bytes played */
244 p_aout->p_sys->dw_counter += i_size;
248 /*****************************************************************************
249 * aout_Close: close the audio device
250 *****************************************************************************/
251 static void aout_Close( aout_thread_t *p_aout )
255 intf_WarnMsg( 3, "aout: waveOut aout_Close ");
257 /* Before calling waveOutClose we must reset the device */
258 waveOutReset( p_aout->p_sys->h_waveout );
260 /* Close the device */
261 if( waveOutClose( p_aout->p_sys->h_waveout ) != MMSYSERR_NOERROR )
263 intf_ErrMsg( "aout error: waveOutClose failed" );
266 /* Deallocate memory */
267 for( i=0; i<NUMBUF; i++ )
268 free( p_aout->p_sys->waveheader[i].lpData );
270 if( p_aout->p_sys != NULL )
272 free( p_aout->p_sys );
273 p_aout->p_sys = NULL;
277 /*****************************************************************************
278 * OpenWaveOutDevice: open the sound device
279 ****************************************************************************/
280 static int OpenWaveOutDevice( aout_thread_t *p_aout )
284 /* initialize played bytes counter */
285 p_aout->p_sys->dw_counter = 0;
287 /* Set sound format */
288 p_aout->p_sys->waveformat.wFormatTag = WAVE_FORMAT_PCM;
289 p_aout->p_sys->waveformat.nChannels = p_aout->i_channels;
290 p_aout->p_sys->waveformat.nSamplesPerSec = p_aout->i_rate;
291 p_aout->p_sys->waveformat.wBitsPerSample = 16;
292 p_aout->p_sys->waveformat.nBlockAlign =
293 p_aout->p_sys->waveformat.wBitsPerSample / 8 * p_aout->i_channels;
294 p_aout->p_sys->waveformat.nAvgBytesPerSec =
295 p_aout->p_sys->waveformat.nSamplesPerSec *
296 p_aout->p_sys->waveformat.nBlockAlign;
299 /* Open the device */
300 result = waveOutOpen( &p_aout->p_sys->h_waveout, WAVE_MAPPER,
301 &p_aout->p_sys->waveformat,
302 0 /*callback*/, 0 /*callback data*/, CALLBACK_NULL );
303 if( result != MMSYSERR_NOERROR )
305 intf_ErrMsg( "aout error: waveOutOpen failed" );