dsound: Base fragment size off of the MMDevice's period.
[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 "wingdi.h"
27 #include "mmdeviceapi.h"
28 #include "audioclient.h"
29 #include "mmsystem.h"
30
31 #include "wine/list.h"
32
33 extern int ds_hel_buflen DECLSPEC_HIDDEN;
34 extern int ds_snd_queue_max DECLSPEC_HIDDEN;
35 extern int ds_snd_shadow_maxsize DECLSPEC_HIDDEN;
36 extern int ds_default_sample_rate DECLSPEC_HIDDEN;
37 extern int ds_default_bits_per_sample DECLSPEC_HIDDEN;
38
39 /*****************************************************************************
40  * Predeclare the interface implementation structures
41  */
42 typedef struct IDirectSoundImpl              IDirectSoundImpl;
43 typedef struct IDirectSound_IUnknown         IDirectSound_IUnknown;
44 typedef struct IDirectSound_IDirectSound     IDirectSound_IDirectSound;
45 typedef struct IDirectSound8_IUnknown        IDirectSound8_IUnknown;
46 typedef struct IDirectSound8_IDirectSound    IDirectSound8_IDirectSound;
47 typedef struct IDirectSound8_IDirectSound8   IDirectSound8_IDirectSound8;
48 typedef struct IDirectSoundBufferImpl        IDirectSoundBufferImpl;
49 typedef struct IDirectSoundCaptureImpl       IDirectSoundCaptureImpl;
50 typedef struct IDirectSoundCaptureBufferImpl IDirectSoundCaptureBufferImpl;
51 typedef struct DirectSoundDevice             DirectSoundDevice;
52 typedef struct DirectSoundCaptureDevice      DirectSoundCaptureDevice;
53
54 /* dsound_convert.h */
55 typedef float (*bitsgetfunc)(const IDirectSoundBufferImpl *, DWORD, DWORD);
56 typedef void (*bitsputfunc)(const IDirectSoundBufferImpl *, DWORD, DWORD, float);
57 extern const bitsgetfunc getbpp[5] DECLSPEC_HIDDEN;
58 extern const bitsputfunc putbpp[4] DECLSPEC_HIDDEN;
59 typedef void (*mixfunc)(const void *, void *, unsigned);
60 extern const mixfunc mixfunctions[4] DECLSPEC_HIDDEN;
61 typedef void (*normfunc)(const void *, void *, unsigned);
62 extern const normfunc normfunctions[4] DECLSPEC_HIDDEN;
63
64 typedef struct _DSVOLUMEPAN
65 {
66     DWORD       dwTotalLeftAmpFactor;
67     DWORD       dwTotalRightAmpFactor;
68     LONG        lVolume;
69     DWORD       dwVolAmpFactor;
70     LONG        lPan;
71     DWORD       dwPanLeftAmpFactor;
72     DWORD       dwPanRightAmpFactor;
73 } DSVOLUMEPAN,*PDSVOLUMEPAN;
74
75 /*****************************************************************************
76  * IDirectSoundDevice implementation structure
77  */
78 struct DirectSoundDevice
79 {
80     LONG                        ref;
81
82     GUID                        guid;
83     DSCAPS                      drvcaps;
84     DWORD                       priolevel;
85     PWAVEFORMATEX               pwfx;
86     UINT                        timerID, playing_offs_bytes, in_mmdev_bytes, prebuf, helfrags;
87     UINT64                      last_pos_bytes;
88     DWORD                       fraglen;
89     LPBYTE                      buffer;
90     DWORD                       writelead, buflen, state, playpos, mixpos;
91     int                         nrofbuffers;
92     IDirectSoundBufferImpl**    buffers;
93     RTL_RWLOCK                  buffer_list_lock;
94     CRITICAL_SECTION            mixlock;
95     IDirectSoundBufferImpl     *primary;
96     DWORD                       speaker_config;
97     LPBYTE                      tmp_buffer, mix_buffer;
98     DWORD                       tmp_buffer_len, mix_buffer_len;
99
100     DSVOLUMEPAN                 volpan;
101
102     mixfunc mixfunction;
103     normfunc normfunction;
104
105     /* DirectSound3DListener fields */
106     DS3DLISTENER                ds3dl;
107     BOOL                        ds3dl_need_recalc;
108
109     IMMDevice *mmdevice;
110     IAudioClient *client;
111     IAudioClock *clock;
112     IAudioStreamVolume *volume;
113     IAudioRenderClient *render;
114
115     struct list entry;
116 };
117
118 /* reference counted buffer memory for duplicated buffer memory */
119 typedef struct BufferMemory
120 {
121     LONG                        ref;
122     LPBYTE                      memory;
123     struct list buffers;
124 } BufferMemory;
125
126 ULONG DirectSoundDevice_Release(DirectSoundDevice * device) DECLSPEC_HIDDEN;
127 HRESULT DirectSoundDevice_Initialize(
128     DirectSoundDevice ** ppDevice,
129     LPCGUID lpcGUID) DECLSPEC_HIDDEN;
130 HRESULT DirectSoundDevice_AddBuffer(
131     DirectSoundDevice * device,
132     IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
133 HRESULT DirectSoundDevice_RemoveBuffer(
134     DirectSoundDevice * device,
135     IDirectSoundBufferImpl * pDSB) DECLSPEC_HIDDEN;
136 HRESULT DirectSoundDevice_GetCaps(DirectSoundDevice * device, LPDSCAPS lpDSCaps) DECLSPEC_HIDDEN;
137 HRESULT DirectSoundDevice_CreateSoundBuffer(
138     DirectSoundDevice * device,
139     LPCDSBUFFERDESC dsbd,
140     LPLPDIRECTSOUNDBUFFER ppdsb,
141     LPUNKNOWN lpunk,
142     BOOL from8) DECLSPEC_HIDDEN;
143 HRESULT DirectSoundDevice_DuplicateSoundBuffer(
144     DirectSoundDevice * device,
145     LPDIRECTSOUNDBUFFER psb,
146     LPLPDIRECTSOUNDBUFFER ppdsb) DECLSPEC_HIDDEN;
147 HRESULT DirectSoundDevice_SetCooperativeLevel(
148     DirectSoundDevice * devcie,
149     HWND hwnd,
150     DWORD level) DECLSPEC_HIDDEN;
151 HRESULT DirectSoundDevice_Compact(DirectSoundDevice * device) DECLSPEC_HIDDEN;
152 HRESULT DirectSoundDevice_GetSpeakerConfig(
153     DirectSoundDevice * device,
154     LPDWORD lpdwSpeakerConfig) DECLSPEC_HIDDEN;
155 HRESULT DirectSoundDevice_SetSpeakerConfig(
156     DirectSoundDevice * device,
157     DWORD config) DECLSPEC_HIDDEN;
158 HRESULT DirectSoundDevice_VerifyCertification(DirectSoundDevice * device,
159     LPDWORD pdwCertified) DECLSPEC_HIDDEN;
160
161 /*****************************************************************************
162  * IDirectSoundBuffer implementation structure
163  */
164 struct IDirectSoundBufferImpl
165 {
166     IDirectSoundBuffer8         IDirectSoundBuffer8_iface;
167     IDirectSoundNotify          IDirectSoundNotify_iface;
168     IDirectSound3DListener      IDirectSound3DListener_iface; /* only primary buffer */
169     IDirectSound3DBuffer        IDirectSound3DBuffer_iface; /* only secondary buffer */
170     IKsPropertySet              IKsPropertySet_iface;
171     LONG                        numIfaces; /* "in use interfaces" refcount */
172     LONG                        ref, refn, ref3D, refiks;
173     /* IDirectSoundBufferImpl fields */
174     DirectSoundDevice*          device;
175     RTL_RWLOCK                  lock;
176     PWAVEFORMATEX               pwfx;
177     BufferMemory*               buffer;
178     DWORD                       playflags,state,leadin;
179     DWORD                       writelead,buflen;
180     DWORD                       nAvgBytesPerSec;
181     DWORD                       freq;
182     DSVOLUMEPAN                 volpan;
183     DSBUFFERDESC                dsbd;
184     /* used for frequency conversion (PerfectPitch) */
185     ULONG                       freqneeded;
186     DWORD                       firstep;
187     float freqAcc, freqAdjust, firgain;
188     /* used for mixing */
189     DWORD                       primary_mixpos, sec_mixpos;
190
191     /* IDirectSoundNotify fields */
192     LPDSBPOSITIONNOTIFY         notifies;
193     int                         nrofnotifies;
194     /* DirectSound3DBuffer fields */
195     DS3DBUFFER                  ds3db_ds3db;
196     LONG                        ds3db_lVolume;
197     BOOL                        ds3db_need_recalc;
198     /* Used for bit depth conversion */
199     int                         mix_channels;
200     bitsgetfunc get, get_aux;
201     bitsputfunc put, put_aux;
202
203     struct list entry;
204 };
205
206 float get_mono(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel) DECLSPEC_HIDDEN;
207 void put_mono2stereo(const IDirectSoundBufferImpl *dsb, DWORD pos, DWORD channel, float value) DECLSPEC_HIDDEN;
208
209 HRESULT IDirectSoundBufferImpl_Create(
210     DirectSoundDevice *device,
211     IDirectSoundBufferImpl **ppdsb,
212     LPCDSBUFFERDESC dsbd) DECLSPEC_HIDDEN;
213 HRESULT IDirectSoundBufferImpl_Duplicate(
214     DirectSoundDevice *device,
215     IDirectSoundBufferImpl **ppdsb,
216     IDirectSoundBufferImpl *pdsb) DECLSPEC_HIDDEN;
217 void secondarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
218 const IDirectSound3DListenerVtbl ds3dlvt DECLSPEC_HIDDEN;
219 const IDirectSound3DBufferVtbl ds3dbvt DECLSPEC_HIDDEN;
220 const IKsPropertySetVtbl iksbvt DECLSPEC_HIDDEN;
221
222 /*****************************************************************************
223  * DirectSoundCaptureDevice implementation structure
224  */
225 struct DirectSoundCaptureDevice
226 {
227     GUID                               guid;
228     LONG                               ref;
229
230     DSCCAPS                            drvcaps;
231
232     LPBYTE                             buffer;
233     DWORD                              buflen, write_pos_bytes;
234
235     PWAVEFORMATEX                      pwfx;
236
237     IDirectSoundCaptureBufferImpl*     capture_buffer;
238     DWORD                              state;
239     UINT timerID;
240     CRITICAL_SECTION                   lock;
241
242     IMMDevice *mmdevice;
243     IAudioClient *client;
244     IAudioCaptureClient *capture;
245
246     struct list entry;
247 };
248
249 /*****************************************************************************
250  * IDirectSoundCaptureBuffer implementation structure
251  */
252 struct IDirectSoundCaptureBufferImpl
253 {
254     IDirectSoundCaptureBuffer8          IDirectSoundCaptureBuffer8_iface;
255     IDirectSoundNotify                  IDirectSoundNotify_iface;
256     LONG                                numIfaces; /* "in use interfaces" refcount */
257     LONG                                ref, refn;
258     /* IDirectSoundCaptureBuffer fields */
259     DirectSoundCaptureDevice*           device;
260     LPDSCBUFFERDESC                     pdscbd;
261     DWORD                               flags;
262     /* IDirectSoundNotify fields */
263     LPDSBPOSITIONNOTIFY                 notifies;
264     int                                 nrofnotifies;
265 };
266
267 HRESULT IKsPrivatePropertySetImpl_Create(REFIID riid, IKsPropertySet **piks) DECLSPEC_HIDDEN;
268
269 /*******************************************************************************
270  */
271
272 /* dsound.c */
273
274 HRESULT DSOUND_Create(REFIID riid, LPDIRECTSOUND *ppDS) DECLSPEC_HIDDEN;
275 HRESULT DSOUND_Create8(REFIID riid, LPDIRECTSOUND8 *ppDS) DECLSPEC_HIDDEN;
276
277 /* primary.c */
278
279 HRESULT DSOUND_PrimaryCreate(DirectSoundDevice *device) DECLSPEC_HIDDEN;
280 HRESULT DSOUND_PrimaryDestroy(DirectSoundDevice *device) DECLSPEC_HIDDEN;
281 HRESULT DSOUND_PrimaryPlay(DirectSoundDevice *device) DECLSPEC_HIDDEN;
282 HRESULT DSOUND_PrimaryStop(DirectSoundDevice *device) DECLSPEC_HIDDEN;
283 HRESULT DSOUND_PrimaryGetPosition(DirectSoundDevice *device, LPDWORD playpos, LPDWORD writepos) DECLSPEC_HIDDEN;
284 LPWAVEFORMATEX DSOUND_CopyFormat(LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
285 HRESULT DSOUND_ReopenDevice(DirectSoundDevice *device, BOOL forcewave) DECLSPEC_HIDDEN;
286 HRESULT primarybuffer_create(DirectSoundDevice *device, IDirectSoundBufferImpl **ppdsb,
287     const DSBUFFERDESC *dsbd) DECLSPEC_HIDDEN;
288 void primarybuffer_destroy(IDirectSoundBufferImpl *This) DECLSPEC_HIDDEN;
289 HRESULT primarybuffer_SetFormat(DirectSoundDevice *device, LPCWAVEFORMATEX wfex) DECLSPEC_HIDDEN;
290 LONG capped_refcount_dec(LONG *ref) DECLSPEC_HIDDEN;
291
292 /* duplex.c */
293  
294 HRESULT DSOUND_FullDuplexCreate(REFIID riid, LPDIRECTSOUNDFULLDUPLEX* ppDSFD) DECLSPEC_HIDDEN;
295
296 /* mixer.c */
297 DWORD DSOUND_bufpos_to_mixpos(const DirectSoundDevice* device, DWORD pos) DECLSPEC_HIDDEN;
298 void DSOUND_CheckEvent(const IDirectSoundBufferImpl *dsb, DWORD playpos, int len) DECLSPEC_HIDDEN;
299 void DSOUND_RecalcVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
300 void DSOUND_AmpFactorToVolPan(PDSVOLUMEPAN volpan) DECLSPEC_HIDDEN;
301 void DSOUND_RecalcFormat(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
302 DWORD DSOUND_secpos_to_bufpos(const IDirectSoundBufferImpl *dsb, DWORD secpos, DWORD secmixpos, float *overshot) DECLSPEC_HIDDEN;
303
304 void CALLBACK DSOUND_timer(UINT timerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR dw1, DWORD_PTR dw2) DECLSPEC_HIDDEN;
305
306 /* sound3d.c */
307
308 void DSOUND_Calc3DBuffer(IDirectSoundBufferImpl *dsb) DECLSPEC_HIDDEN;
309
310 /* capture.c */
311  
312 HRESULT DSOUND_CaptureCreate(REFIID riid, LPDIRECTSOUNDCAPTURE *ppDSC) DECLSPEC_HIDDEN;
313 HRESULT DSOUND_CaptureCreate8(REFIID riid, LPDIRECTSOUNDCAPTURE8 *ppDSC8) DECLSPEC_HIDDEN;
314
315 #define STATE_STOPPED   0
316 #define STATE_STARTING  1
317 #define STATE_PLAYING   2
318 #define STATE_CAPTURING 2
319 #define STATE_STOPPING  3
320
321 extern CRITICAL_SECTION DSOUND_renderers_lock DECLSPEC_HIDDEN;
322 extern CRITICAL_SECTION DSOUND_capturers_lock DECLSPEC_HIDDEN;
323 extern struct list DSOUND_capturers DECLSPEC_HIDDEN;
324 extern struct list DSOUND_renderers DECLSPEC_HIDDEN;
325
326 extern GUID DSOUND_renderer_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
327 extern GUID DSOUND_capture_guids[MAXWAVEDRIVERS] DECLSPEC_HIDDEN;
328
329 extern WCHAR wine_vxd_drv[] DECLSPEC_HIDDEN;
330
331 void setup_dsound_options(void) DECLSPEC_HIDDEN;
332 const char * dumpCooperativeLevel(DWORD level) DECLSPEC_HIDDEN;
333
334 HRESULT get_mmdevice(EDataFlow flow, const GUID *tgt, IMMDevice **device) DECLSPEC_HIDDEN;
335
336 BOOL DSOUND_check_supported(IAudioClient *client, DWORD rate,
337         DWORD depth, WORD channels) DECLSPEC_HIDDEN;
338 UINT DSOUND_create_timer(LPTIMECALLBACK cb, DWORD_PTR user) DECLSPEC_HIDDEN;
339 HRESULT enumerate_mmdevices(EDataFlow flow, GUID *guids,
340         LPDSENUMCALLBACKW cb, void *user) DECLSPEC_HIDDEN;