]> git.sesse.net Git - vlc/blob - modules/audio_output/waveout.c
aout3 API change :
[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.6 2002/08/30 23:27:06 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 static void Play         ( aout_instance_t * );
47
48 /* local functions */
49 static int OpenWaveOut   ( aout_instance_t *p_aout, int i_format,
50                            int i_channels, int i_rate );
51 static int PlayWaveOut   ( aout_instance_t *, HWAVEOUT, WAVEHDR *,
52                            aout_buffer_t * );
53 static void CALLBACK WaveOutCallback ( HWAVEOUT h_waveout, UINT uMsg,
54                                        DWORD _p_aout,
55                                        DWORD dwParam1, DWORD dwParam2 );
56
57 /*****************************************************************************
58  * Module descriptor
59  *****************************************************************************/
60 vlc_module_begin();
61     set_description( _("Win32 waveOut extension module") ); 
62     set_capability( "audio output", 50 );
63     set_callbacks( Open, Close );
64 vlc_module_end();
65
66 /*****************************************************************************
67  * aout_sys_t: waveOut audio output method descriptor
68  *****************************************************************************
69  * This structure is part of the audio output thread descriptor.
70  * It describes the waveOut specific properties of an audio device.
71  *****************************************************************************/
72 struct aout_sys_t
73 {
74     HWAVEOUT h_waveout;                        /* handle to waveout instance */
75
76     WAVEFORMATEX waveformat;                                 /* audio format */
77
78     WAVEHDR waveheader[2];
79
80     int i_buffer_size;
81
82     byte_t *p_silence_buffer;               /* buffer we use to play silence */
83 };
84
85 /*****************************************************************************
86  * Open: open the audio device
87  *****************************************************************************
88  * This function opens and setups Win32 waveOut
89  *****************************************************************************/
90 static int Open( vlc_object_t *p_this )
91 {   
92     aout_instance_t *p_aout = (aout_instance_t *)p_this;
93
94     /* Allocate structure */
95     p_aout->output.p_sys = malloc( sizeof( aout_sys_t ) );
96
97     if( p_aout->output.p_sys == NULL )
98     {
99         msg_Err( p_aout, "out of memory" );
100         return 1;
101     }
102
103     p_aout->output.pf_play = Play;
104
105     /* calculate the frame size in bytes */
106     p_aout->output.p_sys->i_buffer_size = FRAME_SIZE * sizeof(s16)
107                                   * p_aout->output.p_sys->waveformat.nChannels;
108     /* Allocate silence buffer */
109     p_aout->output.p_sys->p_silence_buffer =
110         calloc( p_aout->output.p_sys->i_buffer_size, 1 );
111     if( p_aout->output.p_sys->p_silence_buffer == NULL )
112     {
113         msg_Err( p_aout, "out of memory" );
114         return 1;
115     }
116
117     /* We need to open the device with default values to be sure it is
118      * available */
119     if ( OpenWaveOut( p_aout, WAVE_FORMAT_PCM, 2, 44100 ) )
120     {
121         msg_Err( p_aout, "cannot open waveout" );
122         return 1;
123     }
124
125     waveOutReset( p_aout->output.p_sys->h_waveout );
126
127     p_aout->output.output.i_format = AOUT_FMT_S16_NE;
128     p_aout->output.i_nb_samples = FRAME_SIZE;
129
130     /* We need to kick off the playback in order to have the callback properly
131      * working */
132     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
133                  &p_aout->output.p_sys->waveheader[0], NULL );
134     PlayWaveOut( p_aout, p_aout->output.p_sys->h_waveout,
135                  &p_aout->output.p_sys->waveheader[1], NULL );
136
137     return 0;
138 }
139
140 /*****************************************************************************
141  * Play: play a sound buffer
142  *****************************************************************************
143  * This doesn't actually play the buffer. This just stores the buffer so it
144  * can be played by the callback thread.
145  *****************************************************************************/
146 static void Play( aout_instance_t *p_aout )
147 {
148 }
149
150 /*****************************************************************************
151  * Close: close the audio device
152  *****************************************************************************/
153 static void Close( vlc_object_t *p_this )
154 {       
155     aout_instance_t *p_aout = (aout_instance_t *)p_this;
156
157     /* Before calling waveOutClose we must reset the device */
158     waveOutReset( p_aout->output.p_sys->h_waveout );
159
160     /* Close the device */
161     if( waveOutClose( p_aout->output.p_sys->h_waveout ) != MMSYSERR_NOERROR )
162     {
163         msg_Err( p_aout, "waveOutClose failed" );
164     }
165
166     /* Free silence buffer */
167     free( p_aout->output.p_sys->p_silence_buffer );
168
169     if( p_aout->output.p_sys != NULL )
170     { 
171         free( p_aout->output.p_sys );
172         p_aout->output.p_sys = NULL;
173     }
174 }
175
176 /*****************************************************************************
177  * OpenWaveOut: open the waveout sound device
178  ****************************************************************************/
179 static int OpenWaveOut( aout_instance_t *p_aout, int i_format,
180                         int i_channels, int i_rate )
181 {
182     MMRESULT result;
183
184     /* Set sound format */
185     p_aout->output.p_sys->waveformat.wFormatTag = i_format;
186     p_aout->output.p_sys->waveformat.nChannels = i_channels;
187     p_aout->output.p_sys->waveformat.nSamplesPerSec = i_rate;
188     p_aout->output.p_sys->waveformat.wBitsPerSample = 16;
189     p_aout->output.p_sys->waveformat.nBlockAlign =
190         p_aout->output.p_sys->waveformat.wBitsPerSample / 8 * i_channels;
191     p_aout->output.p_sys->waveformat.nAvgBytesPerSec  =
192         p_aout->output.p_sys->waveformat.nSamplesPerSec *
193             p_aout->output.p_sys->waveformat.nBlockAlign;
194
195     /* Open the device */
196     result = waveOutOpen( &p_aout->output.p_sys->h_waveout, WAVE_MAPPER,
197                           &p_aout->output.p_sys->waveformat,
198                           (DWORD_PTR)WaveOutCallback, (DWORD_PTR)p_aout,
199                           CALLBACK_FUNCTION );
200     if( result == WAVERR_BADFORMAT )
201     {
202         msg_Err( p_aout, "waveOutOpen failed WAVERR_BADFORMAT" );
203         return( 1 );
204     }
205     if( result != MMSYSERR_NOERROR )
206     {
207         msg_Err( p_aout, "waveOutOpen failed" );
208         return 1;
209     }
210
211     return 0;
212 }
213
214 /*****************************************************************************
215  * PlayWaveOut: play a buffer through the WaveOut device
216  *****************************************************************************/
217 static int PlayWaveOut( aout_instance_t *p_aout, HWAVEOUT h_waveout,
218                         WAVEHDR *p_waveheader, aout_buffer_t *p_buffer )
219 {
220     MMRESULT result;
221
222     /* Prepare the buffer */
223     if( p_buffer != NULL )
224         p_waveheader->lpData = p_buffer->p_buffer;
225     else
226         /* Use silence buffer instead */
227         p_waveheader->lpData = p_aout->output.p_sys->p_silence_buffer;
228
229     p_waveheader->dwUser = (DWORD_PTR)p_buffer;
230     p_waveheader->dwBufferLength = p_aout->output.p_sys->i_buffer_size;
231     p_waveheader->dwFlags = 0;
232
233     result = waveOutPrepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
234     if( result != MMSYSERR_NOERROR )
235     {
236         msg_Err( p_aout, "waveOutPrepareHeader failed" );
237         return 1;
238     }
239
240     /* Send the buffer to the waveOut queue */
241     result = waveOutWrite( h_waveout, p_waveheader, sizeof(WAVEHDR) );
242     if( result != MMSYSERR_NOERROR )
243     {
244         msg_Err( p_aout, "waveOutWrite failed" );
245         return 1;
246     }
247
248     return 0;
249 }
250
251 /*****************************************************************************
252  * WaveOutCallback: what to do once WaveOut has played its sound samples
253  *****************************************************************************/
254 static void CALLBACK WaveOutCallback( HWAVEOUT h_waveout, UINT uMsg,
255                                       DWORD _p_aout,
256                                       DWORD dwParam1, DWORD dwParam2 )
257 {
258     aout_instance_t * p_aout = (aout_instance_t *)_p_aout;
259     WAVEHDR *p_waveheader = (WAVEHDR *)dwParam1;
260     aout_buffer_t * p_buffer;
261
262     if( uMsg != WOM_DONE ) return;
263
264     /* Unprepare and free the buffer which has just been played */
265     waveOutUnprepareHeader( h_waveout, p_waveheader, sizeof(WAVEHDR) );
266     if( p_waveheader->dwUser )
267         aout_BufferFree( (aout_buffer_t *)p_waveheader->dwUser );
268
269     /* FIXME : take into account WaveOut latency instead of mdate() */
270     p_buffer = aout_OutputNextBuffer( p_aout, mdate(), VLC_FALSE );
271
272     PlayWaveOut( p_aout, h_waveout, p_waveheader, p_buffer );
273 }