Fixed a race condition on RPC worker thread creation, and a typo.
[wine] / dlls / dsound / dsound_private.h
1 /*                      DirectSound
2  *
3  * Copyright 1998 Marcus Meissner
4  * Copyright 1998 Rob Riggs
5  * Copyright 2000-2001 TransGaming Technologies, Inc.
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20  */
21
22 /* Linux does not support better timing than 10ms */
23 #define DS_TIME_RES 10  /* Resolution of multimedia timer */
24 #define DS_TIME_DEL 10  /* Delay of multimedia timer callback, and duration of HEL fragment */
25
26 #define DS_HEL_FRAGS 48 /* HEL only: number of waveOut fragments in primary buffer
27                          * (changing this won't help you) */
28
29 /* direct sound hardware acceleration levels */
30 #define DS_HW_ACCEL_FULL        0       /* default on Windows 98 */
31 #define DS_HW_ACCEL_STANDARD    1       /* default on Windows 2000 */
32 #define DS_HW_ACCEL_BASIC       2
33 #define DS_HW_ACCEL_EMULATION   3
34
35 extern int ds_emuldriver;
36 extern int ds_hel_margin;
37 extern int ds_hel_queue;
38 extern int ds_snd_queue_max;
39 extern int ds_snd_queue_min;
40 extern int ds_hw_accel;
41 extern int ds_default_playback;
42 extern int ds_default_capture;
43
44 /*****************************************************************************
45  * Predeclare the interface implementation structures
46  */
47 typedef struct IDirectSoundImpl IDirectSoundImpl;
48 typedef struct IDirectSoundBufferImpl IDirectSoundBufferImpl;
49 typedef struct IDirectSoundCaptureImpl IDirectSoundCaptureImpl;
50 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
51 typedef struct IDirectSoundFullDuplexImpl IDirectSoundFullDuplexImpl;
52 typedef struct IDirectSoundNotifyImpl IDirectSoundNotifyImpl;
53 typedef struct IDirectSound3DListenerImpl IDirectSound3DListenerImpl;
54 typedef struct IDirectSound3DBufferImpl IDirectSound3DBufferImpl;
55 typedef struct IKsPropertySetImpl IKsPropertySetImpl;
56 typedef struct PrimaryBufferImpl PrimaryBufferImpl;
57
58 /*****************************************************************************
59  * IDirectSound implementation structure
60  */
61 struct IDirectSoundImpl
62 {
63     /* IUnknown fields */
64     ICOM_VFIELD(IDirectSound8);
65     DWORD                      ref;
66     /* IDirectSoundImpl fields */
67     GUID                        guid;
68     PIDSDRIVER                  driver;
69     DSDRIVERDESC                drvdesc;
70     DSDRIVERCAPS                drvcaps;
71     DWORD                       priolevel;
72     WAVEFORMATEX                wfx; /* current main waveformat */
73     HWAVEOUT                    hwo;
74     LPWAVEHDR                   pwave[DS_HEL_FRAGS];
75     UINT                        timerID, pwplay, pwwrite, pwqueue, prebuf, precount;
76     DWORD                       fraglen;
77     PIDSDRIVERBUFFER            hwbuf;
78     LPBYTE                      buffer;
79     DWORD                       writelead, buflen, state, playpos, mixpos;
80     BOOL                        need_remix;
81     int                         nrofbuffers;
82     IDirectSoundBufferImpl**    buffers;
83     IDirectSound3DListenerImpl* listener;
84     RTL_RWLOCK                  lock;
85     CRITICAL_SECTION            mixlock;
86     DSVOLUMEPAN                 volpan;
87 };
88
89 /*****************************************************************************
90  * IDirectSoundBuffer implementation structure
91  */
92 struct IDirectSoundBufferImpl
93 {
94     /* FIXME: document */
95     /* IUnknown fields */
96     ICOM_VFIELD(IDirectSoundBuffer8);
97     DWORD                     ref;
98     /* IDirectSoundBufferImpl fields */
99     IDirectSoundImpl*         dsound;
100     IDirectSoundBufferImpl*   parent;         /* for duplicates */
101     IDirectSound3DBufferImpl* ds3db;
102     IKsPropertySetImpl*       iks;
103     CRITICAL_SECTION          lock;
104     PIDSDRIVERBUFFER          hwbuf;
105     WAVEFORMATEX              wfx;
106     LPBYTE                    buffer;
107     DWORD                     playflags,state,leadin;
108     DWORD                     playpos,startpos,writelead,buflen;
109     DWORD                     nAvgBytesPerSec;
110     DWORD                     freq;
111     DSVOLUMEPAN               volpan, cvolpan;
112     DSBUFFERDESC              dsbd;
113     /* used for frequency conversion (PerfectPitch) */
114     ULONG                     freqAdjust, freqAcc;
115     /* used for intelligent (well, sort of) prebuffering */
116     DWORD                     probably_valid_to, last_playpos;
117     DWORD                     primary_mixpos, buf_mixpos;
118     BOOL                      need_remix;
119     /* IDirectSoundNotifyImpl fields */
120     LPDSBPOSITIONNOTIFY       notifies;
121     int                       nrofnotifies;
122 };
123
124 HRESULT WINAPI SecondaryBuffer_Create(
125         IDirectSoundImpl *This,
126         IDirectSoundBufferImpl **pdsb,
127         LPDSBUFFERDESC dsbd);
128
129 struct PrimaryBufferImpl {
130         ICOM_VFIELD(IDirectSoundBuffer8);
131         DWORD                   ref;
132         IDirectSoundImpl*       dsound;
133         DSBUFFERDESC            dsbd;
134 };
135
136 HRESULT WINAPI PrimaryBuffer_Create(
137         IDirectSoundImpl *This,
138         PrimaryBufferImpl **pdsb,
139         LPDSBUFFERDESC dsbd);
140
141 /*****************************************************************************
142  * IDirectSoundCapture implementation structure
143  */
144 struct IDirectSoundCaptureImpl
145 {
146     /* IUnknown fields */
147     ICOM_VFIELD(IDirectSoundCapture);
148     DWORD                              ref;
149
150     /* IDirectSoundCaptureImpl fields */
151     GUID                               guid;
152     BOOL                               initialized;
153
154     /* DirectSound driver stuff */
155     PIDSCDRIVER                        driver;
156     DSDRIVERDESC                       drvdesc;
157     DSCDRIVERCAPS                      drvcaps;
158     PIDSCDRIVERBUFFER                  hwbuf;
159
160     /* wave driver info */
161     HWAVEIN                            hwi;
162
163     /* more stuff */
164     LPBYTE                             buffer;
165     DWORD                              buflen;
166     DWORD                              read_position;
167
168     /* FIXME: this should be a pointer because it can be bigger */
169     WAVEFORMATEX                       wfx;
170
171     IDirectSoundCaptureBufferImpl*     capture_buffer;
172     DWORD                              state;
173     LPWAVEHDR                          pwave;
174     int                                nrofpwaves;
175     int                                index;
176     CRITICAL_SECTION                   lock;
177 };
178
179 /*****************************************************************************
180  * IDirectSoundCaptureBuffer implementation structure
181  */
182 struct IDirectSoundCaptureBufferImpl
183 {
184     /* IUnknown fields */
185     ICOM_VFIELD(IDirectSoundCaptureBuffer8);
186     DWORD                              ref;
187
188     /* IDirectSoundCaptureBufferImpl fields */
189     IDirectSoundCaptureImpl*           dsound;
190     /* FIXME: don't need this */
191     LPDSCBUFFERDESC                    pdscbd;
192     LPDSBPOSITIONNOTIFY                notifies;
193     int                                nrofnotifies;
194     DWORD                              flags;
195 };
196
197 /*****************************************************************************
198  * IDirectSoundFullDuplex implementation structure
199  */
200 struct IDirectSoundFullDuplexImpl
201 {
202     /* IUnknown fields */
203     ICOM_VFIELD(IDirectSoundFullDuplex);
204     DWORD                              ref;
205
206     /* IDirectSoundFullDuplexImpl fields */
207     CRITICAL_SECTION                   lock;
208 };
209
210 /*****************************************************************************
211  * IDirectSoundNotify implementation structure
212  */
213 struct IDirectSoundNotifyImpl
214 {
215     /* IUnknown fields */
216     ICOM_VFIELD(IDirectSoundNotify);
217     DWORD                            ref;
218     /* IDirectSoundNotifyImpl fields */
219     IDirectSoundBufferImpl*          dsb;
220     IDirectSoundCaptureBufferImpl*   dscb;
221 };
222
223 /*****************************************************************************
224  *  IDirectSound3DListener implementation structure
225  */
226 struct IDirectSound3DListenerImpl
227 {
228     /* IUnknown fields */
229     ICOM_VFIELD(IDirectSound3DListener);
230     DWORD                                ref;
231     /* IDirectSound3DListenerImpl fields */
232     PrimaryBufferImpl*      dsb;
233     DS3DLISTENER            ds3dl;
234     CRITICAL_SECTION        lock;
235     BOOL                    need_recalc;
236 };
237
238 HRESULT WINAPI IDirectSound3DListenerImpl_Create(
239         PrimaryBufferImpl *This,
240         IDirectSound3DListenerImpl **pdsl);
241
242 /*****************************************************************************
243  *  IKsPropertySet implementation structure
244  */
245 struct IKsPropertySetImpl
246 {
247     /* IUnknown fields */
248     ICOM_VFIELD(IKsPropertySet);
249     DWORD                       ref;
250     /* IKsPropertySetImpl fields */
251     IDirectSoundBufferImpl*     dsb;
252 };
253
254 HRESULT WINAPI IKsPropertySetImpl_Create(
255         IDirectSoundBufferImpl *This,
256         IKsPropertySetImpl **piks);
257
258 /*****************************************************************************
259  * IDirectSound3DBuffer implementation structure
260  */
261 struct IDirectSound3DBufferImpl
262 {
263     /* IUnknown fields */
264     ICOM_VFIELD(IDirectSound3DBuffer);
265     DWORD                              ref;
266     /* IDirectSound3DBufferImpl fields */
267     IDirectSoundBufferImpl* dsb;
268     DS3DBUFFER              ds3db;
269     LONG                    lVolume;
270     CRITICAL_SECTION        lock;
271     BOOL                    need_recalc;
272 };
273
274 HRESULT WINAPI IDirectSound3DBufferImpl_Create(
275         IDirectSoundBufferImpl *This,
276         IDirectSound3DBufferImpl **pds3db);
277
278 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
279 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
280
281 /* primary.c */
282
283 HRESULT DSOUND_PrimaryCreate(IDirectSoundImpl *This);
284 HRESULT DSOUND_PrimaryDestroy(IDirectSoundImpl *This);
285 HRESULT DSOUND_PrimaryPlay(IDirectSoundImpl *This);
286 HRESULT DSOUND_PrimaryStop(IDirectSoundImpl *This);
287 HRESULT DSOUND_PrimaryGetPosition(IDirectSoundImpl *This, LPDWORD playpos, LPDWORD writepos);
288
289 /* buffer.c */
290
291 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
292                               DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix);
293
294 /* mixer.c */
295
296 void DSOUND_CheckEvent(IDirectSoundBufferImpl *dsb, int len);
297 void DSOUND_ForceRemix(IDirectSoundBufferImpl *dsb);
298 void DSOUND_MixCancelAt(IDirectSoundBufferImpl *dsb, DWORD buf_writepos);
299 void DSOUND_WaveQueue(IDirectSoundImpl *dsound, DWORD mixq);
300 void DSOUND_PerformMix(void);
301 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
302 void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD dwUser, DWORD dw1, DWORD dw2);
303
304 #define STATE_STOPPED   0
305 #define STATE_STARTING  1
306 #define STATE_PLAYING   2
307 #define STATE_CAPTURING 2
308 #define STATE_STOPPING  3
309
310 #define DSOUND_FREQSHIFT (14)
311
312 extern IDirectSoundImpl* dsound;
313 extern IDirectSoundCaptureImpl* dsound_capture;
314
315 struct PrimaryBuffer {
316         DWORD ref;
317         PIDSDRIVERBUFFER hwbuf;
318         DWORD state;
319 };
320
321 extern ICOM_VTABLE(IDirectSoundNotify) dsnvt;
322 extern HRESULT mmErr(UINT err);
323 extern void setup_dsound_options(void);