1 /*****************************************************************************
2 * waveout.c : Windows waveOut plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2001 VideoLAN
5 * $Id: waveout.c,v 1.11 2002/07/31 20:56:52 sam 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 <stdlib.h> /* calloc(), malloc(), free() */
38 /*****************************************************************************
40 *****************************************************************************/
41 static int Open ( vlc_object_t * );
42 static void Close ( vlc_object_t * );
44 static int SetFormat ( aout_thread_t * );
45 static int GetBufInfo ( aout_thread_t *, int );
46 static void Play ( aout_thread_t *, byte_t *, int );
49 static int OpenWaveOutDevice( aout_thread_t *p_aout );
51 /*****************************************************************************
53 *****************************************************************************/
55 set_description( _("Win32 waveOut extension module") );
56 set_capability( "audio output", 250 );
57 set_callbacks( Open, Close );
60 /*****************************************************************************
61 * aout_sys_t: waveOut audio output method descriptor
62 *****************************************************************************
63 * This structure is part of the audio output thread descriptor.
64 * It describes the waveOut specific properties of an audio device.
65 *****************************************************************************/
67 #define NUMBUF 3 /* We use triple buffering to be on the safe side */
71 HWAVEOUT h_waveout; /* handle to waveout instance */
73 WAVEFORMATEX waveformat; /* Audio format */
75 WAVEHDR waveheader[NUMBUF];
79 DWORD dw_counter; /* Number of bytes played since beginning */
82 /*****************************************************************************
83 * Open: open the audio device
84 *****************************************************************************
85 * This function opens and setups Win32 waveOut
86 *****************************************************************************/
87 static int Open( vlc_object_t *p_this )
89 aout_thread_t *p_aout = (aout_thread_t *)p_this;
92 /* Allocate structure */
93 p_aout->p_sys = malloc( sizeof( aout_sys_t ) );
95 if( p_aout->p_sys == NULL )
97 msg_Err( p_aout, "out of memory" );
101 p_aout->pf_setformat = SetFormat;
102 p_aout->pf_getbufinfo = GetBufInfo;
103 p_aout->pf_play = Play;
105 /* Initialize some variables */
106 p_aout->p_sys->i_current_buffer = 0;
107 for( i=0; i<NUMBUF; i++)
108 p_aout->p_sys->waveheader[i].lpData = malloc( 1 );
110 return OpenWaveOutDevice( p_aout );
114 /*****************************************************************************
115 * SetFormat: reset the audio device and sets its format
116 *****************************************************************************
117 * This functions set a new audio format.
118 * For this we need to close the current device and create another
119 * one with the desired format.
120 *****************************************************************************/
121 static int SetFormat( aout_thread_t *p_aout )
123 msg_Dbg( p_aout, "SetFormat" );
125 /* Check if the format has changed */
127 if( (p_aout->p_sys->waveformat.nChannels != p_aout->i_channels) ||
128 (p_aout->p_sys->waveformat.nSamplesPerSec != p_aout->i_rate) )
130 /* Before calling waveOutClose we must reset the device */
131 waveOutReset( p_aout->p_sys->h_waveout );
133 if( waveOutClose( p_aout->p_sys->h_waveout ) != MMSYSERR_NOERROR )
135 msg_Err( p_aout, "waveOutClose failed" );
138 return OpenWaveOutDevice( p_aout );
144 /*****************************************************************************
145 * GetBufInfo: buffer status query
146 *****************************************************************************
147 * returns the number of bytes in the audio buffer that have not yet been
148 * sent to the sound device.
149 *****************************************************************************/
150 static int GetBufInfo( aout_thread_t *p_aout, int i_buffer_limit )
154 mmtime.wType = TIME_BYTES;
155 if( (waveOutGetPosition(p_aout->p_sys->h_waveout, &mmtime, sizeof(MMTIME)))
156 != MMSYSERR_NOERROR || (mmtime.wType != TIME_BYTES) )
158 msg_Warn( p_aout, "waveOutGetPosition failed" );
159 return i_buffer_limit;
164 msg_Dbg( p_aout, "GetBufInfo: %i",
165 p_aout->p_sys->dw_counter - mmtime.u.cb );
168 return (p_aout->p_sys->dw_counter - mmtime.u.cb);
171 /*****************************************************************************
172 * Play: play a sound buffer
173 *****************************************************************************
174 * This function writes a buffer of i_length bytes
175 *****************************************************************************/
176 static void Play( aout_thread_t *p_aout, byte_t *p_buffer, int i_size )
179 int current_buffer = p_aout->p_sys->i_current_buffer;
181 p_aout->p_sys->i_current_buffer = (current_buffer + 1) % NUMBUF;
183 /* Unprepare the old buffer */
184 waveOutUnprepareHeader( p_aout->p_sys->h_waveout,
185 &p_aout->p_sys->waveheader[current_buffer],
188 /* Prepare the buffer */
189 p_aout->p_sys->waveheader[current_buffer].lpData =
190 realloc( p_aout->p_sys->waveheader[current_buffer].lpData, i_size );
191 if( !p_aout->p_sys->waveheader[current_buffer].lpData )
193 msg_Err( p_aout, "could not allocate buffer" );
196 p_aout->p_sys->waveheader[current_buffer].dwBufferLength = i_size;
197 p_aout->p_sys->waveheader[current_buffer].dwFlags = 0;
199 result = waveOutPrepareHeader( p_aout->p_sys->h_waveout,
200 &p_aout->p_sys->waveheader[current_buffer],
202 if( result != MMSYSERR_NOERROR )
204 msg_Err( p_aout, "waveOutPrepareHeader failed" );
208 /* Send the buffer the waveOut queue */
209 p_aout->p_vlc->pf_memcpy( p_aout->p_sys->waveheader[current_buffer].lpData,
211 result = waveOutWrite( p_aout->p_sys->h_waveout,
212 &p_aout->p_sys->waveheader[current_buffer],
214 if( result != MMSYSERR_NOERROR )
216 msg_Err( p_aout, "waveOutWrite failed" );
220 /* keep track of number of bytes played */
221 p_aout->p_sys->dw_counter += i_size;
225 /*****************************************************************************
226 * Close: close the audio device
227 *****************************************************************************/
228 static void Close( vlc_object_t *p_this )
230 aout_thread_t *p_aout = (aout_thread_t *)p_this;
233 /* Before calling waveOutClose we must reset the device */
234 waveOutReset( p_aout->p_sys->h_waveout );
236 /* Close the device */
237 if( waveOutClose( p_aout->p_sys->h_waveout ) != MMSYSERR_NOERROR )
239 msg_Err( p_aout, "waveOutClose failed" );
242 /* Deallocate memory */
243 for( i=0; i<NUMBUF; i++ )
244 free( p_aout->p_sys->waveheader[i].lpData );
246 if( p_aout->p_sys != NULL )
248 free( p_aout->p_sys );
249 p_aout->p_sys = NULL;
253 /*****************************************************************************
254 * OpenWaveOutDevice: open the sound device
255 ****************************************************************************/
256 static int OpenWaveOutDevice( aout_thread_t *p_aout )
260 /* initialize played bytes counter */
261 p_aout->p_sys->dw_counter = 0;
263 /* Set sound format */
264 p_aout->p_sys->waveformat.wFormatTag = WAVE_FORMAT_PCM;
265 p_aout->p_sys->waveformat.nChannels = p_aout->i_channels;
266 p_aout->p_sys->waveformat.nSamplesPerSec = p_aout->i_rate;
267 p_aout->p_sys->waveformat.wBitsPerSample = 16;
268 p_aout->p_sys->waveformat.nBlockAlign =
269 p_aout->p_sys->waveformat.wBitsPerSample / 8 * p_aout->i_channels;
270 p_aout->p_sys->waveformat.nAvgBytesPerSec =
271 p_aout->p_sys->waveformat.nSamplesPerSec *
272 p_aout->p_sys->waveformat.nBlockAlign;
275 /* Open the device */
276 result = waveOutOpen( &p_aout->p_sys->h_waveout, WAVE_MAPPER,
277 &p_aout->p_sys->waveformat,
278 0 /*callback*/, 0 /*callback data*/, CALLBACK_NULL );
279 if( result != MMSYSERR_NOERROR )
281 msg_Err( p_aout, "waveOutOpen failed" );