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