]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
* configure.ac.in: libvorbis depends on libogg.
[vlc] / modules / audio_output / waveout.c
1 /*****************************************************************************
2  * waveout.c : Windows waveOut plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: waveout.c,v 1.10 2002/10/28 22:31:49 gbazin Exp $
6  *
7  * Authors: Gildas Bazin <gbazin@netcourrier.com>
8  *      
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.
13  * 
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <errno.h>                                                 /* ENOMEM */
28 #include <fcntl.h>                                       /* open(), O_WRONLY */
29 #include <string.h>                                            /* strerror() */
30
31 #include <stdlib.h>                            /* calloc(), malloc(), free() */
32
33 #include <vlc/vlc.h>
34 #include <vlc/aout.h>
35 #include "aout_internal.h"
36
37 #include <windows.h>
38 #include <mmsystem.h>
39
40 #define FRAME_SIZE 1024              /* The size is in samples, not in bytes */
41 #define FRAMES_NUM 4
42
43 /*****************************************************************************
44  * Useful macros
45  *****************************************************************************/
46 #ifndef WAVE_FORMAT_IEEE_FLOAT
47 #   define WAVE_FORMAT_IEEE_FLOAT 0x0003
48 #endif
49
50 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static int  Open         ( vlc_object_t * );             
54 static void Close        ( vlc_object_t * );                   
55 static void Play         ( aout_instance_t * );
56
57 /* local functions */
58 static int OpenWaveOut   ( aout_instance_t *p_aout, int i_format,
59                            int i_channels, int i_rate );
60 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
61                            aout_buffer_t * );
62 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
63                                        DWORD _p_aout,
64                                        DWORD dwParam1, DWORD dwParam2 );
65
66 /*****************************************************************************
67  * Module descriptor
68  *****************************************************************************/
69 vlc_module_begin();
70     set_description( _("Win32 waveOut extension module") ); 
71     set_capability( "audio output", 50 );
72     set_callbacks( Open, Close );
73 vlc_module_end();
74
75 /*****************************************************************************
76  * aout_sys_t: waveOut audio output method descriptor
77  *****************************************************************************
78  * This structure is part of the audio output thread descriptor.
79  * It describes the waveOut specific properties of an audio device.
80  *****************************************************************************/
81 struct aout_sys_t
82 {
83     HWAVEOUT h_waveout;                        /* handle to waveout instance */
84
85     WAVEFORMATEX waveformat;                                 /* audio format */
86
87     WAVEHDR waveheader[FRAMES_NUM];
88
89     int i_buffer_size;
90
91     byte_t *p_silence_buffer;               /* buffer we use to play silence */
92 };
93
94 /*****************************************************************************
95  * Open: open the audio device
96  *****************************************************************************
97  * This function opens and setups Win32 waveOut
98  *****************************************************************************/
99 static int Open( vlc_object_t *p_this )
100 {   
101     aout_instance_t *p_aout = (aout_instance_t *)p_this;
102     int i_nb_channels, i;
103
104     /* Allocate structure */
105     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
106
107     if( p_aout->output.p_sys == NULL )
108     {
109         msg_Err( p_aout, "out of memory" );
110         return VLC_EGENERIC;
111     }
112
113     p_aout->output.pf_play = Play;
114     aout_VolumeSoftInit( p_aout );
115
116     i_nb_channels = aout_FormatNbChannels( &p_aout->output.output );
117     if ( i_nb_channels > 2 )
118     {
119         /* Waveout doesn't support more than two channels. */
120         i_nb_channels = 2;
121         p_aout->output.output.i_channels = AOUT_CHAN_STEREO;
122     }
123
124     /* We need to open the device with default values to be sure it is
125      * available */
126     p_aout->output.output.i_format = VLC_FOURCC('f','l','3','2');
127     if ( OpenWaveOut( p_aout, WAVE_FORMAT_IEEE_FLOAT, i_nb_channels,
128                       p_aout->output.output.i_rate ) )
129     {
130         msg_Err( p_aout, "Audio device doesn't allow WAVE_FORMAT_IEEE_FLOAT" );
131
132         p_aout->output.output.i_format = VLC_FOURCC('s','1','6','l');
133         if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, i_nb_channels,
134                           p_aout->output.output.i_rate ) )
135         {
136             msg_Err( p_aout, "cannot open waveout audio device" );
137             return VLC_EGENERIC;
138         }
139     }
140
141     waveOutReset( p_aout->output.p_sys->h_waveout );
142
143     /* Calculate the frame size in bytes */
144     p_aout->output.i_nb_samples = FRAME_SIZE;
145     aout_FormatPrepare( &p_aout->output.output );
146     p_aout->output.p_sys->i_buffer_size = FRAME_SIZE *
147                                       p_aout->output.output.i_bytes_per_frame;
148     /* Allocate silence buffer */
149     p_aout->output.p_sys->p_silence_buffer =
150         calloc( p_aout->output.p_sys->i_buffer_size, 1 );
151     if( p_aout->output.p_sys->p_silence_buffer == NULL )
152     {
153         msg_Err( p_aout, "out of memory" );
154         return 1;
155     }
156
157     /* We need to kick off the playback in order to have the callback properly
158      * working */
159     for( i = 0; i < FRAMES_NUM; i++ )
160     {
161         PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
162                      &p_aout->output.p_sys->waveheader[i], NULL );
163     }
164     return 0;
165 }
166
167 /*****************************************************************************
168  * Play: play a sound buffer
169  *****************************************************************************
170  * This doesn't actually play the buffer. This just stores the buffer so it
171  * can be played by the callback thread.
172  *****************************************************************************/
173 static void Play( aout_instance_t *p_aout )
174 {
175 }
176
177 /*****************************************************************************
178  * Close: close the audio device
179  *****************************************************************************/
180 static void Close( vlc_object_t *p_this )
181 {       
182     aout_instance_t *p_aout = (aout_instance_t *)p_this;
183
184     /* Before calling waveOutClose we must reset the device */
185     p_aout->b_die = VLC_TRUE;
186     //Hmmm, waveOutReset never seems to return... why ???
187     //waveOutReset( p_aout->output.p_sys->h_waveout );
188
189     /* Wait for the waveout buffers to be freed */
190     while( !(p_aout->output.p_sys->waveheader[0].dwFlags & WHDR_DONE) || 
191            !(p_aout->output.p_sys->waveheader[1].dwFlags & WHDR_DONE) )
192     {
193         msleep( 1000 );
194     }
195
196     /* Close the device */
197     if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
198     {
199         msg_Err( p_aout, "waveOutClose failed" );
200     }
201
202     /* Free silence buffer */
203     free( p_aout->output.p_sys->p_silence_buffer );
204
205     free( p_aout->output.p_sys );
206 }
207
208 /*****************************************************************************
209  * OpenWaveOut: open the waveout sound device
210  ****************************************************************************/
211 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
212                         int i_channels, int i_rate )
213 {
214     MMRESULT result;
215
216     /* Set sound format */
217     p_aout->output.p_sys->waveformat.wFormatTag = i_format;
218     p_aout->output.p_sys->waveformat.nChannels = i_channels;
219     p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
220
221     switch( i_format )
222     {
223     case WAVE_FORMAT_PCM:
224         p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
225         break;
226     case WAVE_FORMAT_IEEE_FLOAT:
227         p_aout->output.p_sys->waveformat.wBitsPerSample = sizeof(float) * 8;
228         break;
229     }
230
231     p_aout->output.p_sys->waveformat.nBlockAlign =
232         p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
233     p_aout->output.p_sys->waveformat.nAvgBytesPerSec  =
234         p_aout->output.p_sys->waveformat.nSamplesPerSec *
235             p_aout->output.p_sys->waveformat.nBlockAlign;
236
237     /* Open the device */
238     result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
239                           &p_aout->output.p_sys->waveformat,
240                           (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
241                           CALLBACK_FUNCTION );
242     if( result == WAVERR_BADFORMAT )
243     {
244         msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
245         return( 1 );
246     }
247     if( result != MMSYSERR_NOERROR )
248     {
249         msg_Err( p_aout, "waveOutOpen failed" );
250         return 1;
251     }
252
253     return 0;
254 }
255
256 /*****************************************************************************
257  * PlayWaveOut: play a buffer through the WaveOut device
258  *****************************************************************************/
259 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
260                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
261 {
262     MMRESULT result;
263
264     /* Prepare the buffer */
265     if( p_buffer != NULL )
266         p_waveheader->lpData = p_buffer->p_buffer;
267     else
268         /* Use silence buffer instead */
269         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
270
271     p_waveheader->dwUser = (DWORD_PTR)p_buffer;
272     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
273     p_waveheader->dwFlags = 0;
274
275     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
276     if( result != MMSYSERR_NOERROR )
277     {
278         msg_Err( p_aout, "waveOutPrepareHeader failed" );
279         return 1;
280     }
281
282     /* Send the buffer to the waveOut queue */
283     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
284     if( result != MMSYSERR_NOERROR )
285     {
286         msg_Err( p_aout, "waveOutWrite failed" );
287         return 1;
288     }
289
290     return 0;
291 }
292
293 /*****************************************************************************
294  * WaveOutCallback: what to do once WaveOut has played its sound samples
295  *****************************************************************************/
296 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
297                                       DWORD _p_aout,
298                                       DWORD dwParam1, DWORD dwParam2 )
299 {
300     aout_instance_t *p_aout = (aout_instance_t *)_p_aout;
301     WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
302     aout_buffer_t *p_buffer;
303
304     if( uMsg != WOM_DONE ) return;
305
306     /* Unprepare and free the buffer which has just been played */
307     waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
308     if( p_waveheader->dwUser )
309         aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
310
311     if( p_aout->b_die ) return;
312
313     /* Take into account WaveOut latency ( 1 FRAME ) */
314     p_buffer = aout_OutputNextBuffer( p_aout,
315         mdate() + 1000000 / p_aout->output.output.i_rate * FRAME_SIZE,
316         VLC_FALSE );
317
318     PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );
319 }