]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
* Added a third argument to aout_OutputNextBuffer. In case the buffer
[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.3 2002/08/14 00:43:52 massiot 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 <mmsystem.h>
38
39 #define FRAME_SIZE 2048              /* The size is in samples, not in bytes */
40
41 /*****************************************************************************
42  * Local prototypes
43  *****************************************************************************/
44 static int  Open         ( vlc_object_t * );             
45 static void Close        ( vlc_object_t * );                   
46
47 static int  SetFormat    ( aout_instance_t * );  
48 static void Play         ( aout_instance_t *, aout_buffer_t * );
49
50 /* local functions */
51 static int OpenWaveOut   ( aout_instance_t *p_aout, int i_format,
52                            int i_channels, int i_rate );
53 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
54                            aout_buffer_t * );
55 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
56                                        DWORD _p_aout,
57                                        DWORD dwParam1, DWORD dwParam2 );
58
59 /*****************************************************************************
60  * Module descriptor
61  *****************************************************************************/
62 vlc_module_begin();
63     set_description( _("Win32 waveOut extension module") ); 
64     set_capability( "audio output", 50 );
65     set_callbacks( Open, Close );
66 vlc_module_end();
67
68 /*****************************************************************************
69  * aout_sys_t: waveOut audio output method descriptor
70  *****************************************************************************
71  * This structure is part of the audio output thread descriptor.
72  * It describes the waveOut specific properties of an audio device.
73  *****************************************************************************/
74 struct aout_sys_t
75 {
76     HWAVEOUT h_waveout;                        /* handle to waveout instance */
77
78     WAVEFORMATEX waveformat;                                 /* audio format */
79
80     WAVEHDR waveheader[2];
81
82     int i_buffer_size;
83
84     byte_t *p_silence_buffer;               /* buffer we use to play silence */
85 };
86
87 /*****************************************************************************
88  * Open: open the audio device
89  *****************************************************************************
90  * This function opens and setups Win32 waveOut
91  *****************************************************************************/
92 static int Open( vlc_object_t *p_this )
93 {   
94     aout_instance_t *p_aout = (aout_instance_t *)p_this;
95
96     /* Allocate structure */
97     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
98
99     if( p_aout->output.p_sys == NULL )
100     {
101         msg_Err( p_aout, "out of memory" );
102         return 1;
103     }
104
105     p_aout->output.pf_setformat = SetFormat;
106     p_aout->output.pf_play = Play;
107
108     /* calculate the frame size in bytes */
109     p_aout->output.p_sys->i_buffer_size = FRAME_SIZE * sizeof(s16)
110                                   * p_aout->output.p_sys->waveformat.nChannels;
111     /* Allocate silence buffer */
112     p_aout->output.p_sys->p_silence_buffer =
113         calloc( p_aout->output.p_sys->i_buffer_size, 1 );
114     if( p_aout->output.p_sys->p_silence_buffer == NULL )
115     {
116         msg_Err( p_aout, "out of memory" );
117         return 1;
118     }
119
120     /* We need to open the device with default values to be sure it is
121      * available */
122     return OpenWaveOut( p_aout, WAVE_FORMAT_PCM, 2, 44100 );
123 }
124
125 /*****************************************************************************
126  * SetFormat: reset the audio device and sets its format
127  *****************************************************************************
128  * This functions set a new audio format.
129  * For this we need to close the current device and create another
130  * one with the desired format.
131  *****************************************************************************/
132 static int SetFormat( aout_instance_t *p_aout )
133 {
134     msg_Dbg( p_aout, "SetFormat" );
135
136     waveOutReset( p_aout->output.p_sys->h_waveout );
137
138     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
139     p_aout->output.i_nb_samples = FRAME_SIZE;
140
141     /* Check if the format has changed */
142     if( (p_aout->output.p_sys->waveformat.nChannels !=
143              p_aout->output.output.i_channels) ||
144         (p_aout->output.p_sys->waveformat.nSamplesPerSec !=
145              p_aout->output.output.i_rate) )
146     {
147         if( waveOutClose( p_aout->output.p_sys->h_waveout ) !=
148                 MMSYSERR_NOERROR )
149         {
150             msg_Err( p_aout, "waveOutClose failed" );
151         }
152
153         /* calculate the frame size in bytes */
154         p_aout->output.p_sys->i_buffer_size = FRAME_SIZE * sizeof(s16)
155                                             * p_aout->output.output.i_channels;
156
157         /* take care of silence buffer */
158         free( p_aout->output.p_sys->p_silence_buffer );
159         p_aout->output.p_sys->p_silence_buffer =
160             calloc( p_aout->output.p_sys->i_buffer_size, 1 );
161         if( p_aout->output.p_sys->p_silence_buffer == NULL )
162         {
163             msg_Err( p_aout, "out of memory" );
164             return 1;
165         }
166
167         if( OpenWaveOut( p_aout, WAVE_FORMAT_PCM,
168                          p_aout->output.output.i_channels,
169                          p_aout->output.output.i_rate ) )
170             return 1;
171     }
172
173     /* We need to kick off the playback in order to have the callback properly
174      * working */
175     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
176                  &p_aout->output.p_sys->waveheader[0], NULL );
177     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
178                  &p_aout->output.p_sys->waveheader[1], NULL );
179
180     return 0;
181 }
182
183 /*****************************************************************************
184  * Play: play a sound buffer
185  *****************************************************************************
186  * This doesn't actually play the buffer. This just stores the buffer so it
187  * can be played by the callback thread.
188  *****************************************************************************/
189 static void Play( aout_instance_t *p_aout, aout_buffer_t *p_buffer )
190 {
191     aout_FifoPush( p_aout, &p_aout->output.fifo, p_buffer );
192 }
193
194 /*****************************************************************************
195  * Close: close the audio device
196  *****************************************************************************/
197 static void Close( vlc_object_t *p_this )
198 {       
199     aout_instance_t *p_aout = (aout_instance_t *)p_this;
200
201     /* Before calling waveOutClose we must reset the device */
202     waveOutReset( p_aout->output.p_sys->h_waveout );
203
204     /* Close the device */
205     if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
206     {
207         msg_Err( p_aout, "waveOutClose failed" );
208     }
209
210     /* Free silence buffer */
211     free( p_aout->output.p_sys->p_silence_buffer );
212
213     if( p_aout->output.p_sys != NULL )
214     { 
215         free( p_aout->output.p_sys );
216         p_aout->output.p_sys = NULL;
217     }
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     p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
233     p_aout->output.p_sys->waveformat.nBlockAlign =
234         p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
235     p_aout->output.p_sys->waveformat.nAvgBytesPerSec  =
236         p_aout->output.p_sys->waveformat.nSamplesPerSec *
237             p_aout->output.p_sys->waveformat.nBlockAlign;
238
239     /* Open the device */
240     result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
241                           &p_aout->output.p_sys->waveformat,
242                           (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
243                           CALLBACK_FUNCTION );
244     if( result == WAVERR_BADFORMAT )
245     {
246         msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
247         return( 1 );
248     }
249     if( result != MMSYSERR_NOERROR )
250     {
251         msg_Err( p_aout, "waveOutOpen failed" );
252         return 1;
253     }
254
255     return 0;
256 }
257
258 /*****************************************************************************
259  * PlayWaveOut: play a buffer through the WaveOut device
260  *****************************************************************************/
261 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
262                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
263 {
264     MMRESULT result;
265
266     /* Prepare the buffer */
267     if( p_buffer != NULL )
268         p_waveheader->lpData = p_buffer->p_buffer;
269     else
270         /* Use silence buffer instead */
271         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
272
273     p_waveheader->dwUser = (DWORD_PTR)p_buffer;
274     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
275     p_waveheader->dwFlags = 0;
276
277     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
278     if( result != MMSYSERR_NOERROR )
279     {
280         msg_Err( p_aout, "waveOutPrepareHeader failed" );
281         return 1;
282     }
283
284     /* Send the buffer to the waveOut queue */
285     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
286     if( result != MMSYSERR_NOERROR )
287     {
288         msg_Err( p_aout, "waveOutWrite failed" );
289         return 1;
290     }
291
292     return 0;
293 }
294
295 /*****************************************************************************
296  * WaveOutCallback: what to do once WaveOut has played its sound samples
297  *****************************************************************************/
298 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
299                                       DWORD _p_aout,
300                                       DWORD dwParam1, DWORD dwParam2 )
301 {
302     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
303     WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
304     aout_buffer_t * p_buffer;
305
306     if( uMsg != WOM_DONE ) return;
307
308     /* Unprepare and free the buffer which has just been played */
309     waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
310     if( p_waveheader->dwUser )
311         aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
312
313     /* FIXME : take into account WaveOut latency instead of mdate() */
314     p_buffer = aout_OutputNextBuffer( p_aout, mdate(), 0 );
315
316     PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );
317 }