dsound: Fix segv on use-after-free in IDirectSoundNotifyImpl_Release.
[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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21
22 /* Linux does not support better timing than 10ms */
23 #define DS_TIME_RES 2  /* Resolution of multimedia timer */
24 #define DS_TIME_DEL 10  /* Delay of multimedia timer callback, and duration of HEL fragment */
25
26 #include "wine/list.h"
27
28 /* direct sound hardware acceleration levels */
29 #define DS_HW_ACCEL_FULL        0       /* default on Windows 98 */
30 #define DS_HW_ACCEL_STANDARD    1       /* default on Windows 2000 */
31 #define DS_HW_ACCEL_BASIC       2
32 #define DS_HW_ACCEL_EMULATION   3
33
34 extern int ds_emuldriver;
35 extern int ds_hel_buflen;
36 extern int ds_snd_queue_max;
37 extern int ds_snd_queue_min;
38 extern int ds_snd_shadow_maxsize;
39 extern int ds_hw_accel;
40 extern int ds_default_sample_rate;
41 extern int ds_default_bits_per_sample;
42
43 /*****************************************************************************
44  * Predeclare the interface implementation structures
45  */
46 typedef struct IDirectSoundImpl              IDirectSoundImpl;
47 typedef struct IDirectSound_IUnknown         IDirectSound_IUnknown;
48 typedef struct IDirectSound_IDirectSound     IDirectSound_IDirectSound;
49 typedef struct IDirectSound8_IUnknown        IDirectSound8_IUnknown;
50 typedef struct IDirectSound8_IDirectSound    IDirectSound8_IDirectSound;
51 typedef struct IDirectSound8_IDirectSound8   IDirectSound8_IDirectSound8;
52 typedef struct IDirectSoundBufferImpl        IDirectSoundBufferImpl;
53 typedef struct IDirectSoundCaptureImpl       IDirectSoundCaptureImpl;
54 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
55 typedef struct IDirectSoundFullDuplexImpl    IDirectSoundFullDuplexImpl;
56 typedef struct IDirectSoundFullDuplex_IUnknown IDirectSoundFullDuplex_IUnknown;
57 typedef struct IDirectSoundFullDuplex_IDirectSound IDirectSoundFullDuplex_IDirectSound;
58 typedef struct IDirectSoundFullDuplex_IDirectSound8 IDirectSoundFullDuplex_IDirectSound8;
59 typedef struct IDirectSoundFullDuplex_IDirectSoundCapture IDirectSoundFullDuplex_IDirectSoundCapture;
60 typedef struct IDirectSoundNotifyImpl        IDirectSoundNotifyImpl;
61 typedef struct IDirectSoundCaptureNotifyImpl IDirectSoundCaptureNotifyImpl;
62 typedef struct IDirectSound3DListenerImpl    IDirectSound3DListenerImpl;
63 typedef struct IDirectSound3DBufferImpl      IDirectSound3DBufferImpl;
64 typedef struct IKsBufferPropertySetImpl      IKsBufferPropertySetImpl;
65 typedef struct IKsPrivatePropertySetImpl     IKsPrivatePropertySetImpl;
66 typedef struct PrimaryBufferImpl             PrimaryBufferImpl;
67 typedef struct SecondaryBufferImpl           SecondaryBufferImpl;
68 typedef struct DirectSoundDevice             DirectSoundDevice;
69 typedef struct DirectSoundCaptureDevice      DirectSoundCaptureDevice;
70
71 /* dsound_convert.h */
72 typedef void (*bitsconvertfunc)(const void *, void *, UINT, UINT, INT, UINT, UINT);
73 extern const bitsconvertfunc convertbpp[4][4];
74 typedef void (*mixfunc)(const void *, void *, unsigned);
75 extern const mixfunc mixfunctions[4];
76 typedef void (*normfunc)(const void *, void *, unsigned);
77 extern const normfunc normfunctions[4];
78
79 /*****************************************************************************
80  * IDirectSoundDevice implementation structure
81  */
82 struct DirectSoundDevice
83 {
84     LONG                        ref;
85
86     GUID                        guid;
87     PIDSDRIVER                  driver;
88     DSDRIVERDESC                drvdesc;
89     DSDRIVERCAPS                drvcaps;
90     DWORD                       priolevel;
91     PWAVEFORMATEX               pwfx;
92     HWAVEOUT                    hwo;
93     LPWAVEHDR                   pwave;
94     UINT                        timerID, pwplay, pwqueue, prebuf, helfrags;
95     DWORD                       fraglen;
96     PIDSDRIVERBUFFER            hwbuf;
97     LPBYTE                      buffer;
98     DWORD                       writelead, buflen, state, playpos, mixpos;
99     int                         nrofbuffers;
100     IDirectSoundBufferImpl**    buffers;
101     RTL_RWLOCK                  buffer_list_lock;
102     CRITICAL_SECTION            mixlock;
103     PrimaryBufferImpl*          primary;
104     DSBUFFERDESC                dsbd;
105     DWORD                       speaker_config;
106     LPBYTE                      tmp_buffer, mix_buffer;
107     DWORD                       tmp_buffer_len, mix_buffer_len;
108
109     DSVOLUMEPAN                 volpan;
110
111     mixfunc mixfunction;
112     normfunc normfunction;
113
114     /* DirectSound3DListener fields */
115     IDirectSound3DListenerImpl* listener;
116     DS3DLISTENER                ds3dl;
117     BOOL                        ds3dl_need_recalc;
118 };
119
120 /* reference counted buffer memory for duplicated buffer memory */
121 typedef struct BufferMemory
122 {
123     LONG                        ref;
124     LPBYTE                      memory;
125     struct list buffers;
126 } BufferMemory;
127
128 ULONG DirectSoundDevice_Release(DirectSoundDevice * device);
129 HRESULT DirectSoundDevice_Initialize(
130     DirectSoundDevice ** ppDevice,
131     LPCGUID lpcGUID);
132 HRESULT DirectSoundDevice_AddBuffer(
133     DirectSoundDevice * device,
134     IDirectSoundBufferImpl * pDSB);
135 HRESULT DirectSoundDevice_RemoveBuffer(
136     DirectSoundDevice * device,
137     IDirectSoundBufferImpl * pDSB);
138 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps);
139 HRESULT DirectSoundDevice_CreateSoundBuffer(
140     DirectSoundDevice * device,
141     LPCDSBUFFERDESC dsbd,
142     LPLPDIRECTSOUNDBUFFER ppdsb,
143     LPUNKNOWN lpunk,
144     BOOL from8);
145 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
146     DirectSoundDevice * device,
147     LPDIRECTSOUNDBUFFER psb,
148     LPLPDIRECTSOUNDBUFFER ppdsb);
149 HRESULT DirectSoundDevice_SetCooperativeLevel(
150     DirectSoundDevice * devcie,
151     HWND hwnd,
152     DWORD level);
153 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device);
154 HRESULT DirectSoundDevice_GetSpeakerConfig(
155     DirectSoundDevice * device,
156     LPDWORD lpdwSpeakerConfig);
157 HRESULT DirectSoundDevice_SetSpeakerConfig(
158     DirectSoundDevice * device,
159     DWORD config);
160
161 /*****************************************************************************
162  * IDirectSoundBuffer implementation structure
163  */
164 struct IDirectSoundBufferImpl
165 {
166     /* FIXME: document */
167     /* IUnknown fields */
168     const IDirectSoundBuffer8Vtbl *lpVtbl;
169     LONG                        ref;
170     /* IDirectSoundBufferImpl fields */
171     SecondaryBufferImpl*        secondary;
172     DirectSoundDevice*          device;
173     RTL_RWLOCK                  lock;
174     PIDSDRIVERBUFFER            hwbuf;
175     PWAVEFORMATEX               pwfx;
176     BufferMemory*               buffer;
177     LPBYTE                      tmp_buffer;
178     DWORD                       playflags,state,leadin;
179     DWORD                       writelead,buflen;
180     DWORD                       nAvgBytesPerSec;
181     DWORD                       freq, tmp_buffer_len, max_buffer_len;
182     DSVOLUMEPAN                 volpan;
183     DSBUFFERDESC                dsbd;
184     /* used for frequency conversion (PerfectPitch) */
185     ULONG                       freqneeded, freqAdjust, freqAcc, freqAccNext, resampleinmixer;
186     /* used for mixing */
187     DWORD                       primary_mixpos, buf_mixpos, sec_mixpos;
188
189     /* IDirectSoundNotifyImpl fields */
190     IDirectSoundNotifyImpl*     notify;
191     LPDSBPOSITIONNOTIFY         notifies;
192     int                         nrofnotifies;
193     PIDSDRIVERNOTIFY            hwnotify;
194
195     /* DirectSound3DBuffer fields */
196     IDirectSound3DBufferImpl*   ds3db;
197     DS3DBUFFER                  ds3db_ds3db;
198     LONG                        ds3db_lVolume;
199     BOOL                        ds3db_need_recalc;
200
201     /* IKsPropertySet fields */
202     IKsBufferPropertySetImpl*   iks;
203     bitsconvertfunc convert;
204     struct list entry;
205 };
206
207 HRESULT IDirectSoundBufferImpl_Create(
208     DirectSoundDevice *device,
209     IDirectSoundBufferImpl **ppdsb,
210     LPCDSBUFFERDESC dsbd);
211 HRESULT IDirectSoundBufferImpl_Destroy(
212     IDirectSoundBufferImpl *pdsb);
213 HRESULT IDirectSoundBufferImpl_Duplicate(
214     DirectSoundDevice *device,
215     IDirectSoundBufferImpl **ppdsb,
216     IDirectSoundBufferImpl *pdsb);
217
218 /*****************************************************************************
219  * SecondaryBuffer implementation structure
220  */
221 struct SecondaryBufferImpl
222 {
223     const IDirectSoundBuffer8Vtbl *lpVtbl;
224     LONG                        ref;
225     IDirectSoundBufferImpl*     dsb;
226 };
227
228 HRESULT SecondaryBufferImpl_Create(
229     IDirectSoundBufferImpl *dsb,
230     SecondaryBufferImpl **pdsb);
231
232 /*****************************************************************************
233  * PrimaryBuffer implementation structure
234  */
235 struct PrimaryBufferImpl
236 {
237     const IDirectSoundBufferVtbl *lpVtbl;
238     LONG                        ref;
239     DirectSoundDevice*          device;
240 };
241
242 HRESULT PrimaryBufferImpl_Create(
243     DirectSoundDevice * device,
244     PrimaryBufferImpl **ppdsb,
245     LPCDSBUFFERDESC dsbd);
246
247 /*****************************************************************************
248  * DirectSoundCaptureDevice implementation structure
249  */
250 struct DirectSoundCaptureDevice
251 {
252     /* IDirectSoundCaptureImpl fields */
253     GUID                               guid;
254     LONG                               ref;
255
256     /* DirectSound driver stuff */
257     PIDSCDRIVER                        driver;
258     DSDRIVERDESC                       drvdesc;
259     DSCDRIVERCAPS                      drvcaps;
260     PIDSCDRIVERBUFFER                  hwbuf;
261
262     /* wave driver info */
263     HWAVEIN                            hwi;
264
265     /* more stuff */
266     LPBYTE                             buffer;
267     DWORD                              buflen;
268
269     PWAVEFORMATEX                      pwfx;
270
271     IDirectSoundCaptureBufferImpl*     capture_buffer;
272     DWORD                              state;
273     LPWAVEHDR                          pwave;
274     int                                nrofpwaves;
275     int                                index;
276     CRITICAL_SECTION                   lock;
277 };
278
279 HRESULT DirectSoundCaptureDevice_Initialize(
280     DirectSoundCaptureDevice ** ppDevice,
281     LPCGUID lpcGUID);
282 ULONG DirectSoundCaptureDevice_Release(
283     DirectSoundCaptureDevice * device);
284
285 /*****************************************************************************
286  * IDirectSoundCaptureBuffer implementation structure
287  */
288 struct IDirectSoundCaptureBufferImpl
289 {
290     /* IUnknown fields */
291     const IDirectSoundCaptureBuffer8Vtbl *lpVtbl;
292     LONG                                ref;
293
294     /* IDirectSoundCaptureBufferImpl fields */
295     DirectSoundCaptureDevice*           device;
296     /* FIXME: don't need this */
297     LPDSCBUFFERDESC                     pdscbd;
298     DWORD                               flags;
299
300     /* IDirectSoundCaptureNotifyImpl fields */
301     IDirectSoundCaptureNotifyImpl*      notify;
302     LPDSBPOSITIONNOTIFY                 notifies;
303     int                                 nrofnotifies;
304     PIDSDRIVERNOTIFY                    hwnotify;
305 };
306
307 HRESULT IDirectSoundCaptureBufferImpl_Create(
308     DirectSoundCaptureDevice *device,
309     IDirectSoundCaptureBufferImpl ** ppobj,
310     LPCDSCBUFFERDESC lpcDSCBufferDesc);
311
312 /*****************************************************************************
313  * IDirectSoundFullDuplex implementation structure
314  */
315 struct IDirectSoundFullDuplexImpl
316 {
317     /* IUnknown fields */
318     const IDirectSoundFullDuplexVtbl *lpVtbl;
319     LONG                              ref;
320
321     /* IDirectSoundFullDuplexImpl fields */
322     DirectSoundDevice                *renderer_device;
323     DirectSoundCaptureDevice         *capture_device;
324
325     LPUNKNOWN                         pUnknown;
326     LPDIRECTSOUND                     pDS;
327     LPDIRECTSOUND8                    pDS8;
328     LPDIRECTSOUNDCAPTURE              pDSC;
329 };
330
331 /*****************************************************************************
332  * IDirectSoundFullDuplex COM components
333  */
334 struct IDirectSoundFullDuplex_IUnknown {
335     const IUnknownVtbl         *lpVtbl;
336     LONG                        ref;
337     IDirectSoundFullDuplexImpl *pdsfd;
338 };
339
340 struct IDirectSoundFullDuplex_IDirectSound {
341     const IDirectSoundVtbl     *lpVtbl;
342     LONG                        ref;
343     IDirectSoundFullDuplexImpl *pdsfd;
344 };
345
346 struct IDirectSoundFullDuplex_IDirectSound8 {
347     const IDirectSound8Vtbl    *lpVtbl;
348     LONG                        ref;
349     IDirectSoundFullDuplexImpl *pdsfd;
350 };
351
352 struct IDirectSoundFullDuplex_IDirectSoundCapture {
353     const IDirectSoundCaptureVtbl *lpVtbl;
354     LONG                           ref;
355     IDirectSoundFullDuplexImpl    *pdsfd;
356 };
357
358 /*****************************************************************************
359  *  IDirectSound3DListener implementation structure
360  */
361 struct IDirectSound3DListenerImpl
362 {
363     /* IUnknown fields */
364     const IDirectSound3DListenerVtbl *lpVtbl;
365     LONG                        ref;
366     /* IDirectSound3DListenerImpl fields */
367     DirectSoundDevice*          device;
368 };
369
370 HRESULT IDirectSound3DListenerImpl_Create(
371     DirectSoundDevice           *device,
372     IDirectSound3DListenerImpl **pdsl);
373
374 /*****************************************************************************
375  *  IKsBufferPropertySet implementation structure
376  */
377 struct IKsBufferPropertySetImpl
378 {
379     /* IUnknown fields */
380     const IKsPropertySetVtbl   *lpVtbl;
381     LONG                        ref;
382     /* IKsPropertySetImpl fields */
383     IDirectSoundBufferImpl*     dsb;
384 };
385
386 HRESULT IKsBufferPropertySetImpl_Create(
387     IDirectSoundBufferImpl *dsb,
388     IKsBufferPropertySetImpl **piks);
389 HRESULT IKsBufferPropertySetImpl_Destroy(
390     IKsBufferPropertySetImpl *piks);
391
392 /*****************************************************************************
393  *  IKsPrivatePropertySet implementation structure
394  */
395 struct IKsPrivatePropertySetImpl
396 {
397     /* IUnknown fields */
398     const IKsPropertySetVtbl   *lpVtbl;
399     LONG                        ref;
400 };
401
402 HRESULT IKsPrivatePropertySetImpl_Create(
403     REFIID riid,
404     IKsPrivatePropertySetImpl **piks);
405
406 /*****************************************************************************
407  * IDirectSound3DBuffer implementation structure
408  */
409 struct IDirectSound3DBufferImpl
410 {
411     /* IUnknown fields */
412     const IDirectSound3DBufferVtbl *lpVtbl;
413     LONG                        ref;
414     /* IDirectSound3DBufferImpl fields */
415     IDirectSoundBufferImpl*     dsb;
416 };
417
418 HRESULT IDirectSound3DBufferImpl_Create(
419     IDirectSoundBufferImpl *dsb,
420     IDirectSound3DBufferImpl **pds3db);
421 HRESULT IDirectSound3DBufferImpl_Destroy(
422     IDirectSound3DBufferImpl *pds3db);
423
424 /*******************************************************************************
425  */
426
427 /* dsound.c */
428
429 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS);
430 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS);
431
432 /* primary.c */
433
434 DWORD DSOUND_fraglen(DWORD nSamplesPerSec, DWORD nBlockAlign);
435 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device);
436 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device);
437 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device);
438 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device);
439 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos);
440 HRESULT DSOUND_PrimarySetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex, BOOL forced);
441 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave);
442
443 /* duplex.c */
444  
445 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD);
446
447 /* mixer.c */
448 DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos);
449 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len);
450 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan);
451 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan);
452 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb);
453 void DSOUND_MixToTemporary(const IDirectSoundBufferImpl *dsb, DWORD writepos, DWORD mixlen, BOOL inmixer);
454 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, DWORD* overshot);
455
456 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
457 void CALLBACK DSOUND_callback(HWAVEOUT hwo, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2);
458
459 /* sound3d.c */
460
461 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb);
462
463 /* capture.c */
464  
465 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC);
466 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8);
467 HRESULT WINAPI IDirectSoundCaptureImpl_CreateCaptureBuffer(
468     LPDIRECTSOUNDCAPTURE iface,
469     LPCDSCBUFFERDESC lpcDSCBufferDesc,
470     LPDIRECTSOUNDCAPTUREBUFFER* lplpDSCaptureBuffer,
471     LPUNKNOWN pUnk);
472 HRESULT WINAPI IDirectSoundCaptureImpl_GetCaps(
473     LPDIRECTSOUNDCAPTURE iface,
474     LPDSCCAPS lpDSCCaps);
475 HRESULT WINAPI IDirectSoundCaptureImpl_Initialize(
476     LPDIRECTSOUNDCAPTURE iface,
477     LPCGUID lpcGUID);
478
479 #define STATE_STOPPED   0
480 #define STATE_STARTING  1
481 #define STATE_PLAYING   2
482 #define STATE_CAPTURING 2
483 #define STATE_STOPPING  3
484
485 #define DSOUND_FREQSHIFT (20)
486
487 extern DirectSoundDevice* DSOUND_renderer[MAXWAVEDRIVERS];
488 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS];
489
490 extern DirectSoundCaptureDevice * DSOUND_capture[MAXWAVEDRIVERS];
491 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS];
492
493 HRESULT mmErr(UINT err);
494 void setup_dsound_options(void);
495 const char * dumpCooperativeLevel(DWORD level);