1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 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 #include "dsound_private.h"
23
24 /*******************************************************************************
25 * IDirectSoundNotify
26 */
27
28 struct IDirectSoundNotifyImpl
29 {
30 /* IUnknown fields */
31 const IDirectSoundNotifyVtbl *lpVtbl;
32 LONG ref;
33 IDirectSoundBufferImpl* dsb;
34 };
35
36 static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
37 IDirectSoundNotifyImpl **pdsn);
38 static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);
39
IDirectSoundNotifyImpl_QueryInterface(LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID * ppobj)40 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
41 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
42 ) {
43 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
44 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
45
46 if (This->dsb == NULL) {
47 WARN("invalid parameter\n");
48 return E_INVALIDARG;
49 }
50
51 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
52 }
53
IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)54 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
55 {
56 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
57 ULONG ref = InterlockedIncrement(&(This->ref));
58 TRACE("(%p) ref was %d\n", This, ref - 1);
59 return ref;
60 }
61
IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)62 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
63 {
64 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
65 ULONG ref = InterlockedDecrement(&(This->ref));
66 TRACE("(%p) ref was %d\n", This, ref + 1);
67
68 if (!ref) {
69 This->dsb->notify = NULL;
70 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
71 HeapFree(GetProcessHeap(), 0, This);
72 TRACE("(%p) released\n", This);
73 }
74 return ref;
75 }
76
IDirectSoundNotifyImpl_SetNotificationPositions(LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify)77 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
78 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
79 ) {
80 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
81 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
82
83 if (howmuch > 0 && notify == NULL) {
84 WARN("invalid parameter: notify == NULL\n");
85 return DSERR_INVALIDPARAM;
86 }
87
88 if (TRACE_ON(dsound)) {
89 unsigned int i;
90 for (i=0;i<howmuch;i++)
91 TRACE("notify at %d to %p\n",
92 notify[i].dwOffset,notify[i].hEventNotify);
93 }
94
95 if (This->dsb->hwnotify) {
96 HRESULT hres;
97 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
98 if (hres != DS_OK)
99 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
100 return hres;
101 } else if (howmuch > 0) {
102 /* Make an internal copy of the caller-supplied array.
103 * Replace the existing copy if one is already present. */
104 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
105 This->dsb->notifies = HeapAlloc(GetProcessHeap(), 0,
106 howmuch * sizeof(DSBPOSITIONNOTIFY));
107
108 if (This->dsb->notifies == NULL) {
109 WARN("out of memory\n");
110 return DSERR_OUTOFMEMORY;
111 }
112 CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
113 This->dsb->nrofnotifies = howmuch;
114 } else {
115 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
116 This->dsb->notifies = NULL;
117 This->dsb->nrofnotifies = 0;
118 }
119
120 return S_OK;
121 }
122
123 static const IDirectSoundNotifyVtbl dsnvt =
124 {
125 IDirectSoundNotifyImpl_QueryInterface,
126 IDirectSoundNotifyImpl_AddRef,
127 IDirectSoundNotifyImpl_Release,
128 IDirectSoundNotifyImpl_SetNotificationPositions,
129 };
130
IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl * dsb,IDirectSoundNotifyImpl ** pdsn)131 static HRESULT IDirectSoundNotifyImpl_Create(
132 IDirectSoundBufferImpl * dsb,
133 IDirectSoundNotifyImpl **pdsn)
134 {
135 IDirectSoundNotifyImpl * dsn;
136 TRACE("(%p,%p)\n",dsb,pdsn);
137
138 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*dsn));
139
140 if (dsn == NULL) {
141 WARN("out of memory\n");
142 return DSERR_OUTOFMEMORY;
143 }
144
145 dsn->ref = 0;
146 dsn->lpVtbl = &dsnvt;
147 dsn->dsb = dsb;
148 dsb->notify = dsn;
149 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
150
151 *pdsn = dsn;
152 return DS_OK;
153 }
154
IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl * pdsn)155 static HRESULT IDirectSoundNotifyImpl_Destroy(
156 IDirectSoundNotifyImpl *pdsn)
157 {
158 TRACE("(%p)\n",pdsn);
159
160 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
161
162 return DS_OK;
163 }
164
165 /*******************************************************************************
166 * IDirectSoundBuffer
167 */
168
impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 * iface)169 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
170 {
171 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
172 }
173
is_primary_buffer(IDirectSoundBufferImpl * This)174 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
175 {
176 return This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER ? TRUE : FALSE;
177 }
178
IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 * iface,LPCWAVEFORMATEX wfex)179 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
180 LPCWAVEFORMATEX wfex)
181 {
182 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
183
184 TRACE("(%p,%p)\n", iface, wfex);
185
186 if (is_primary_buffer(This))
187 return primarybuffer_SetFormat(This->device, wfex);
188 else {
189 WARN("not available for secondary buffers.\n");
190 return DSERR_INVALIDCALL;
191 }
192 }
193
IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 * iface,LONG vol)194 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
195 {
196 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
197 LONG oldVol;
198
199 HRESULT hres = DS_OK;
200
201 TRACE("(%p,%d)\n",This,vol);
202
203 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
204 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
205 return DSERR_CONTROLUNAVAIL;
206 }
207
208 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
209 WARN("invalid parameter: vol = %d\n", vol);
210 return DSERR_INVALIDPARAM;
211 }
212
213 /* **** */
214 RtlAcquireResourceExclusive(&This->lock, TRUE);
215
216 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
217 oldVol = This->ds3db_lVolume;
218 This->ds3db_lVolume = vol;
219 if (vol != oldVol)
220 /* recalc 3d volume, which in turn recalcs the pans */
221 DSOUND_Calc3DBuffer(This);
222 } else {
223 oldVol = This->volpan.lVolume;
224 This->volpan.lVolume = vol;
225 if (vol != oldVol)
226 DSOUND_RecalcVolPan(&(This->volpan));
227 }
228
229 if (vol != oldVol) {
230 if (This->hwbuf) {
231 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
232 if (hres != DS_OK)
233 WARN("IDsDriverBuffer_SetVolumePan failed\n");
234 }
235 }
236
237 RtlReleaseResource(&This->lock);
238 /* **** */
239
240 return hres;
241 }
242
IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 * iface,LONG * vol)243 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
244 {
245 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
246
247 TRACE("(%p,%p)\n",This,vol);
248
249 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
250 WARN("control unavailable\n");
251 return DSERR_CONTROLUNAVAIL;
252 }
253
254 if (vol == NULL) {
255 WARN("invalid parameter: vol == NULL\n");
256 return DSERR_INVALIDPARAM;
257 }
258
259 *vol = This->volpan.lVolume;
260
261 return DS_OK;
262 }
263
IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 * iface,DWORD freq)264 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
265 {
266 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
267 DWORD oldFreq;
268
269 TRACE("(%p,%d)\n",This,freq);
270
271 if (is_primary_buffer(This)) {
272 WARN("not available for primary buffers.\n");
273 return DSERR_CONTROLUNAVAIL;
274 }
275
276 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
277 WARN("control unavailable\n");
278 return DSERR_CONTROLUNAVAIL;
279 }
280
281 if (freq == DSBFREQUENCY_ORIGINAL)
282 freq = This->pwfx->nSamplesPerSec;
283
284 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
285 WARN("invalid parameter: freq = %d\n", freq);
286 return DSERR_INVALIDPARAM;
287 }
288
289 /* **** */
290 RtlAcquireResourceExclusive(&This->lock, TRUE);
291
292 oldFreq = This->freq;
293 This->freq = freq;
294 if (freq != oldFreq) {
295 This->freqAdjust = ((DWORD64)This->freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
296 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
297 DSOUND_RecalcFormat(This);
298 DSOUND_MixToTemporary(This, 0, This->buflen, FALSE);
299 }
300
301 RtlReleaseResource(&This->lock);
302 /* **** */
303
304 return DS_OK;
305 }
306
IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 * iface,DWORD reserved1,DWORD reserved2,DWORD flags)307 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
308 DWORD reserved2, DWORD flags)
309 {
310 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
311 HRESULT hres = DS_OK;
312
313 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
314
315 /* **** */
316 RtlAcquireResourceExclusive(&This->lock, TRUE);
317
318 This->playflags = flags;
319 if (This->state == STATE_STOPPED && !This->hwbuf) {
320 This->leadin = TRUE;
321 This->state = STATE_STARTING;
322 } else if (This->state == STATE_STOPPING)
323 This->state = STATE_PLAYING;
324 if (This->hwbuf) {
325 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
326 if (hres != DS_OK)
327 WARN("IDsDriverBuffer_Play failed\n");
328 else
329 This->state = STATE_PLAYING;
330 }
331
332 RtlReleaseResource(&This->lock);
333 /* **** */
334
335 return hres;
336 }
337
IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 * iface)338 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
339 {
340 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
341 HRESULT hres = DS_OK;
342
343 TRACE("(%p)\n",This);
344
345 /* **** */
346 RtlAcquireResourceExclusive(&This->lock, TRUE);
347
348 if (This->state == STATE_PLAYING)
349 This->state = STATE_STOPPING;
350 else if (This->state == STATE_STARTING)
351 {
352 This->state = STATE_STOPPED;
353 DSOUND_CheckEvent(This, 0, 0);
354 }
355 if (This->hwbuf) {
356 hres = IDsDriverBuffer_Stop(This->hwbuf);
357 if (hres != DS_OK)
358 WARN("IDsDriverBuffer_Stop failed\n");
359 else
360 This->state = STATE_STOPPED;
361 }
362
363 RtlReleaseResource(&This->lock);
364 /* **** */
365
366 return hres;
367 }
368
IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 * iface)369 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
370 {
371 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
372 ULONG ref = InterlockedIncrement(&This->ref);
373
374 TRACE("(%p) ref was %d\n", This, ref - 1);
375
376 if(ref == 1)
377 InterlockedIncrement(&This->numIfaces);
378
379 return ref;
380 }
381
IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 * iface)382 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
383 {
384 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
385 ULONG ref = InterlockedDecrement(&This->ref);
386
387 TRACE("(%p) ref was %d\n", This, ref + 1);
388
389 if (!ref && !InterlockedDecrement(&This->numIfaces)) {
390 if (is_primary_buffer(This))
391 primarybuffer_destroy(This);
392 else
393 secondarybuffer_destroy(This);
394 }
395 return ref;
396 }
397
IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 * iface,DWORD * playpos,DWORD * writepos)398 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
399 DWORD *playpos, DWORD *writepos)
400 {
401 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
402 HRESULT hres;
403
404 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
405
406 RtlAcquireResourceShared(&This->lock, TRUE);
407 if (This->hwbuf) {
408 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
409 if (hres != DS_OK) {
410 WARN("IDsDriverBuffer_GetPosition failed\n");
411 return hres;
412 }
413 } else {
414 DWORD pos = This->sec_mixpos;
415
416 /* sanity */
417 if (pos >= This->buflen){
418 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
419 pos %= This->buflen;
420 }
421
422 if (playpos)
423 *playpos = pos;
424 if (writepos)
425 *writepos = pos;
426 }
427 if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
428 /* apply the documented 10ms lead to writepos */
429 *writepos += This->writelead;
430 *writepos %= This->buflen;
431 }
432 RtlReleaseResource(&This->lock);
433
434 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
435 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
436
437 return DS_OK;
438 }
439
IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 * iface,DWORD * status)440 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
441 {
442 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
443
444 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
445
446 if (status == NULL) {
447 WARN("invalid parameter: status = NULL\n");
448 return DSERR_INVALIDPARAM;
449 }
450
451 *status = 0;
452 RtlAcquireResourceShared(&This->lock, TRUE);
453 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
454 *status |= DSBSTATUS_PLAYING;
455 if (This->playflags & DSBPLAY_LOOPING)
456 *status |= DSBSTATUS_LOOPING;
457 }
458 RtlReleaseResource(&This->lock);
459
460 TRACE("status=%x\n", *status);
461 return DS_OK;
462 }
463
464
IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 * iface,LPWAVEFORMATEX lpwf,DWORD wfsize,DWORD * wfwritten)465 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
466 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
467 {
468 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
469 DWORD size;
470
471 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
472
473 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
474
475 if (lpwf) { /* NULL is valid */
476 if (wfsize >= size) {
477 CopyMemory(lpwf,This->pwfx,size);
478 if (wfwritten)
479 *wfwritten = size;
480 } else {
481 WARN("invalid parameter: wfsize too small\n");
482 CopyMemory(lpwf,This->pwfx,wfsize);
483 if (wfwritten)
484 *wfwritten = wfsize;
485 return DSERR_INVALIDPARAM;
486 }
487 } else {
488 if (wfwritten)
489 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
490 else {
491 WARN("invalid parameter: wfwritten == NULL\n");
492 return DSERR_INVALIDPARAM;
493 }
494 }
495
496 return DS_OK;
497 }
498
IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 * iface,DWORD writecursor,DWORD writebytes,void ** lplpaudioptr1,DWORD * audiobytes1,void ** lplpaudioptr2,DWORD * audiobytes2,DWORD flags)499 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
500 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
501 DWORD *audiobytes2, DWORD flags)
502 {
503 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
504 HRESULT hres = DS_OK;
505
506 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This, writecursor, writebytes, lplpaudioptr1,
507 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
508
509 if (!audiobytes1)
510 return DSERR_INVALIDPARAM;
511
512 /* when this flag is set, writecursor is meaningless and must be calculated */
513 if (flags & DSBLOCK_FROMWRITECURSOR) {
514 /* GetCurrentPosition does too much magic to duplicate here */
515 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
516 if (hres != DS_OK) {
517 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
518 return hres;
519 }
520 }
521
522 /* when this flag is set, writebytes is meaningless and must be set */
523 if (flags & DSBLOCK_ENTIREBUFFER)
524 writebytes = This->buflen;
525
526 if (writecursor >= This->buflen) {
527 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
528 writecursor, This->buflen);
529 return DSERR_INVALIDPARAM;
530 }
531
532 if (writebytes > This->buflen) {
533 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
534 writebytes, This->buflen);
535 return DSERR_INVALIDPARAM;
536 }
537
538 /* **** */
539 RtlAcquireResourceShared(&This->lock, TRUE);
540
541 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
542 hres = IDsDriverBuffer_Lock(This->hwbuf,
543 lplpaudioptr1, audiobytes1,
544 lplpaudioptr2, audiobytes2,
545 writecursor, writebytes,
546 0);
547 if (hres != DS_OK) {
548 WARN("IDsDriverBuffer_Lock failed\n");
549 RtlReleaseResource(&This->lock);
550 return hres;
551 }
552 } else {
553 if (writecursor+writebytes <= This->buflen) {
554 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
555 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
556 WARN("Overwriting mixing position, case 1\n");
557 *audiobytes1 = writebytes;
558 if (lplpaudioptr2)
559 *(LPBYTE*)lplpaudioptr2 = NULL;
560 if (audiobytes2)
561 *audiobytes2 = 0;
562 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
563 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
564 TRACE("->%d.0\n",writebytes);
565 } else {
566 DWORD remainder = writebytes + writecursor - This->buflen;
567 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
568 *audiobytes1 = This->buflen-writecursor;
569 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
570 WARN("Overwriting mixing position, case 2\n");
571 if (lplpaudioptr2)
572 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
573 if (audiobytes2)
574 *audiobytes2 = writebytes-(This->buflen-writecursor);
575 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
576 WARN("Overwriting mixing position, case 3\n");
577 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
578 }
579 }
580
581 RtlReleaseResource(&This->lock);
582 /* **** */
583
584 return DS_OK;
585 }
586
IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 * iface,DWORD newpos)587 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
588 DWORD newpos)
589 {
590 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
591 HRESULT hres = DS_OK;
592 DWORD oldpos;
593
594 TRACE("(%p,%d)\n",This,newpos);
595
596 /* **** */
597 RtlAcquireResourceExclusive(&This->lock, TRUE);
598
599 oldpos = This->sec_mixpos;
600
601 /* start mixing from this new location instead */
602 newpos %= This->buflen;
603 newpos -= newpos%This->pwfx->nBlockAlign;
604 This->sec_mixpos = newpos;
605
606 /* at this point, do not attempt to reset buffers, mess with primary mix position,
607 or anything like that to reduce latency. The data already prebuffered cannot be changed */
608
609 /* position HW buffer if applicable, else just start mixing from new location instead */
610 if (This->hwbuf) {
611 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
612 if (hres != DS_OK)
613 WARN("IDsDriverBuffer_SetPosition failed\n");
614 }
615 else if (oldpos != newpos)
616 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
617 This->buf_mixpos = DSOUND_secpos_to_bufpos(This, newpos, 0, NULL);
618
619 RtlReleaseResource(&This->lock);
620 /* **** */
621
622 return hres;
623 }
624
IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 * iface,LONG pan)625 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
626 {
627 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
628 HRESULT hres = DS_OK;
629
630 TRACE("(%p,%d)\n",This,pan);
631
632 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
633 WARN("invalid parameter: pan = %d\n", pan);
634 return DSERR_INVALIDPARAM;
635 }
636
637 /* You cannot use both pan and 3D controls */
638 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
639 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
640 WARN("control unavailable\n");
641 return DSERR_CONTROLUNAVAIL;
642 }
643
644 /* **** */
645 RtlAcquireResourceExclusive(&This->lock, TRUE);
646
647 if (This->volpan.lPan != pan) {
648 This->volpan.lPan = pan;
649 DSOUND_RecalcVolPan(&(This->volpan));
650
651 if (This->hwbuf) {
652 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
653 if (hres != DS_OK)
654 WARN("IDsDriverBuffer_SetVolumePan failed\n");
655 }
656 }
657
658 RtlReleaseResource(&This->lock);
659 /* **** */
660
661 return hres;
662 }
663
IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 * iface,LONG * pan)664 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
665 {
666 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
667
668 TRACE("(%p,%p)\n",This,pan);
669
670 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
671 WARN("control unavailable\n");
672 return DSERR_CONTROLUNAVAIL;
673 }
674
675 if (pan == NULL) {
676 WARN("invalid parameter: pan = NULL\n");
677 return DSERR_INVALIDPARAM;
678 }
679
680 *pan = This->volpan.lPan;
681
682 return DS_OK;
683 }
684
IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 * iface,void * p1,DWORD x1,void * p2,DWORD x2)685 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
686 void *p2, DWORD x2)
687 {
688 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
689 HRESULT hres = DS_OK;
690
691 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
692
693 /* **** */
694 RtlAcquireResourceShared(&This->lock, TRUE);
695
696 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
697 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
698 if (hres != DS_OK)
699 WARN("IDsDriverBuffer_Unlock failed\n");
700 }
701
702 RtlReleaseResource(&This->lock);
703 /* **** */
704
705 if (!p2)
706 x2 = 0;
707
708 if (!This->hwbuf && (x1 || x2))
709 {
710 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
711 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
712 {
713 RtlAcquireResourceShared(&iter->lock, TRUE);
714 if (x1)
715 {
716 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
717 hres = DSERR_INVALIDPARAM;
718 else
719 DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1, FALSE);
720 }
721 if (x2)
722 DSOUND_MixToTemporary(iter, 0, x2, FALSE);
723 RtlReleaseResource(&iter->lock);
724 }
725 RtlReleaseResource(&This->device->buffer_list_lock);
726 }
727
728 return hres;
729 }
730
IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 * iface)731 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
732 {
733 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
734
735 FIXME("(%p):stub\n",This);
736 return DS_OK;
737 }
738
IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 * iface,DWORD * freq)739 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
740 {
741 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
742
743 TRACE("(%p,%p)\n",This,freq);
744
745 if (freq == NULL) {
746 WARN("invalid parameter: freq = NULL\n");
747 return DSERR_INVALIDPARAM;
748 }
749
750 *freq = This->freq;
751 TRACE("-> %d\n", *freq);
752
753 return DS_OK;
754 }
755
IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 * iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,DWORD * pdwResultCodes)756 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
757 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
758 {
759 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
760 DWORD u;
761
762 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
763
764 if (pdwResultCodes)
765 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
766
767 WARN("control unavailable\n");
768 return DSERR_CONTROLUNAVAIL;
769 }
770
IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 * iface,DWORD dwFlags,DWORD dwEffectsCount,DWORD * pdwResultCodes)771 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
772 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
773 {
774 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
775 DWORD u;
776
777 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
778
779 if (pdwResultCodes)
780 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
781
782 WARN("control unavailable\n");
783 return DS_OK;
784 }
785
IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 * iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,void ** ppObject)786 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
787 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
788 {
789 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
790
791 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
792
793 WARN("control unavailable\n");
794 return DSERR_CONTROLUNAVAIL;
795 }
796
IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 * iface,IDirectSound * dsound,LPCDSBUFFERDESC dbsd)797 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
798 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
799 {
800 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
801
802 WARN("(%p) already initialized\n", This);
803 return DSERR_ALREADYINITIALIZED;
804 }
805
IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 * iface,LPDSBCAPS caps)806 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
807 {
808 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
809
810 TRACE("(%p)->(%p)\n",This,caps);
811
812 if (caps == NULL) {
813 WARN("invalid parameter: caps == NULL\n");
814 return DSERR_INVALIDPARAM;
815 }
816
817 if (caps->dwSize < sizeof(*caps)) {
818 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
819 return DSERR_INVALIDPARAM;
820 }
821
822 caps->dwFlags = This->dsbd.dwFlags;
823 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
824 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
825
826 caps->dwBufferBytes = This->buflen;
827
828 /* According to windows, this is zero*/
829 caps->dwUnlockTransferRate = 0;
830 caps->dwPlayCpuOverhead = 0;
831
832 return DS_OK;
833 }
834
IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 * iface,REFIID riid,void ** ppobj)835 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
836 void **ppobj)
837 {
838 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
839
840 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
841
842 if (ppobj == NULL) {
843 WARN("invalid parameter\n");
844 return E_INVALIDARG;
845 }
846
847 *ppobj = NULL; /* assume failure */
848
849 if ( IsEqualGUID(riid, &IID_IUnknown) ||
850 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
851 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
852 IDirectSoundBuffer8_AddRef(iface);
853 *ppobj = iface;
854 return S_OK;
855 }
856
857 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
858 if (!This->notify)
859 IDirectSoundNotifyImpl_Create(This, &(This->notify));
860 if (This->notify) {
861 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
862 *ppobj = This->notify;
863 return S_OK;
864 }
865 WARN("IID_IDirectSoundNotify\n");
866 return E_NOINTERFACE;
867 }
868
869 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
870 if (!This->ds3db)
871 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
872 if (This->ds3db) {
873 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
874 *ppobj = This->ds3db;
875 return S_OK;
876 }
877 WARN("IID_IDirectSound3DBuffer\n");
878 return E_NOINTERFACE;
879 }
880
881 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
882 ERR("app requested IDirectSound3DListener on secondary buffer\n");
883 return E_NOINTERFACE;
884 }
885
886 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
887 if (!This->iks)
888 IKsBufferPropertySetImpl_Create(This, &(This->iks));
889 if (This->iks) {
890 IKsPropertySet_AddRef((LPKSPROPERTYSET)This->iks);
891 *ppobj = This->iks;
892 return S_OK;
893 }
894 WARN("IID_IKsPropertySet\n");
895 return E_NOINTERFACE;
896 }
897
898 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
899
900 return E_NOINTERFACE;
901 }
902
903 static const IDirectSoundBuffer8Vtbl dsbvt =
904 {
905 IDirectSoundBufferImpl_QueryInterface,
906 IDirectSoundBufferImpl_AddRef,
907 IDirectSoundBufferImpl_Release,
908 IDirectSoundBufferImpl_GetCaps,
909 IDirectSoundBufferImpl_GetCurrentPosition,
910 IDirectSoundBufferImpl_GetFormat,
911 IDirectSoundBufferImpl_GetVolume,
912 IDirectSoundBufferImpl_GetPan,
913 IDirectSoundBufferImpl_GetFrequency,
914 IDirectSoundBufferImpl_GetStatus,
915 IDirectSoundBufferImpl_Initialize,
916 IDirectSoundBufferImpl_Lock,
917 IDirectSoundBufferImpl_Play,
918 IDirectSoundBufferImpl_SetCurrentPosition,
919 IDirectSoundBufferImpl_SetFormat,
920 IDirectSoundBufferImpl_SetVolume,
921 IDirectSoundBufferImpl_SetPan,
922 IDirectSoundBufferImpl_SetFrequency,
923 IDirectSoundBufferImpl_Stop,
924 IDirectSoundBufferImpl_Unlock,
925 IDirectSoundBufferImpl_Restore,
926 IDirectSoundBufferImpl_SetFX,
927 IDirectSoundBufferImpl_AcquireResources,
928 IDirectSoundBufferImpl_GetObjectInPath
929 };
930
IDirectSoundBufferImpl_Create(DirectSoundDevice * device,IDirectSoundBufferImpl ** pdsb,LPCDSBUFFERDESC dsbd)931 HRESULT IDirectSoundBufferImpl_Create(
932 DirectSoundDevice * device,
933 IDirectSoundBufferImpl **pdsb,
934 LPCDSBUFFERDESC dsbd)
935 {
936 IDirectSoundBufferImpl *dsb;
937 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
938 HRESULT err = DS_OK;
939 DWORD capf = 0;
940 int use_hw;
941 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
942
943 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
944 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
945 *pdsb = NULL;
946 return DSERR_INVALIDPARAM; /* FIXME: which error? */
947 }
948
949 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
950
951 if (dsb == 0) {
952 WARN("out of memory\n");
953 *pdsb = NULL;
954 return DSERR_OUTOFMEMORY;
955 }
956
957 TRACE("Created buffer at %p\n", dsb);
958
959 dsb->ref = 1;
960 dsb->numIfaces = 1;
961 dsb->device = device;
962 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
963 dsb->iks = NULL;
964
965 /* size depends on version */
966 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
967
968 dsb->pwfx = DSOUND_CopyFormat(wfex);
969 if (dsb->pwfx == NULL) {
970 HeapFree(GetProcessHeap(),0,dsb);
971 *pdsb = NULL;
972 return DSERR_OUTOFMEMORY;
973 }
974
975 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
976 dsb->buflen = dsbd->dwBufferBytes +
977 (dsbd->lpwfxFormat->nBlockAlign -
978 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
979 else
980 dsb->buflen = dsbd->dwBufferBytes;
981
982 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
983 dsb->notify = NULL;
984 dsb->notifies = NULL;
985 dsb->nrofnotifies = 0;
986 dsb->hwnotify = 0;
987
988 /* Check necessary hardware mixing capabilities */
989 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
990 else capf |= DSCAPS_SECONDARYMONO;
991 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
992 else capf |= DSCAPS_SECONDARY8BIT;
993
994 use_hw = !!(dsbd->dwFlags & DSBCAPS_LOCHARDWARE);
995 TRACE("use_hw = %d, capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", use_hw, capf, device->drvcaps.dwFlags);
996 if (use_hw && ((device->drvcaps.dwFlags & capf) != capf || !device->driver))
997 {
998 if (device->driver)
999 WARN("Format not supported for hardware buffer\n");
1000 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1001 HeapFree(GetProcessHeap(),0,dsb);
1002 *pdsb = NULL;
1003 if ((device->drvcaps.dwFlags & capf) != capf)
1004 return DSERR_BADFORMAT;
1005 return DSERR_GENERIC;
1006 }
1007
1008 /* FIXME: check hardware sample rate mixing capabilities */
1009 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1010 /* FIXME: check whether any hardware buffers are left */
1011 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1012
1013 /* Allocate an empty buffer */
1014 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1015 if (dsb->buffer == NULL) {
1016 WARN("out of memory\n");
1017 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1018 HeapFree(GetProcessHeap(),0,dsb);
1019 *pdsb = NULL;
1020 return DSERR_OUTOFMEMORY;
1021 }
1022
1023 /* Allocate system memory for buffer if applicable */
1024 if ((device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1025 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1026 if (dsb->buffer->memory == NULL) {
1027 WARN("out of memory\n");
1028 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1029 HeapFree(GetProcessHeap(),0,dsb->buffer);
1030 HeapFree(GetProcessHeap(),0,dsb);
1031 *pdsb = NULL;
1032 return DSERR_OUTOFMEMORY;
1033 }
1034 }
1035
1036 /* Allocate the hardware buffer */
1037 if (use_hw) {
1038 err = IDsDriver_CreateSoundBuffer(device->driver,wfex,dsbd->dwFlags,0,
1039 &(dsb->buflen),&(dsb->buffer->memory),
1040 (LPVOID*)&(dsb->hwbuf));
1041 if (FAILED(err))
1042 {
1043 WARN("Failed to create hardware secondary buffer: %08x\n", err);
1044 if (device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)
1045 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1046 HeapFree(GetProcessHeap(),0,dsb->buffer);
1047 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1048 HeapFree(GetProcessHeap(),0,dsb);
1049 *pdsb = NULL;
1050 return DSERR_GENERIC;
1051 }
1052 }
1053
1054 dsb->buffer->ref = 1;
1055 list_init(&dsb->buffer->buffers);
1056 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1057 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1058
1059 /* It's not necessary to initialize values to zero since */
1060 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1061 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1062 dsb->state = STATE_STOPPED;
1063
1064 dsb->freqAdjust = ((DWORD64)dsb->freq << DSOUND_FREQSHIFT) / device->pwfx->nSamplesPerSec;
1065 dsb->nAvgBytesPerSec = dsb->freq *
1066 dsbd->lpwfxFormat->nBlockAlign;
1067
1068 /* calculate fragment size and write lead */
1069 DSOUND_RecalcFormat(dsb);
1070
1071 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1072 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1073 dsb->ds3db_ds3db.vPosition.x = 0.0;
1074 dsb->ds3db_ds3db.vPosition.y = 0.0;
1075 dsb->ds3db_ds3db.vPosition.z = 0.0;
1076 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1077 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1078 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1079 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1080 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1081 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1082 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1083 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1084 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1085 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1086 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1087 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1088
1089 dsb->ds3db_need_recalc = FALSE;
1090 DSOUND_Calc3DBuffer(dsb);
1091 } else
1092 DSOUND_RecalcVolPan(&(dsb->volpan));
1093
1094 RtlInitializeResource(&dsb->lock);
1095
1096 /* register buffer if not primary */
1097 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1098 err = DirectSoundDevice_AddBuffer(device, dsb);
1099 if (err != DS_OK) {
1100 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1101 HeapFree(GetProcessHeap(),0,dsb->buffer);
1102 RtlDeleteResource(&dsb->lock);
1103 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1104 HeapFree(GetProcessHeap(),0,dsb);
1105 dsb = NULL;
1106 }
1107 }
1108
1109 *pdsb = dsb;
1110 return err;
1111 }
1112
secondarybuffer_destroy(IDirectSoundBufferImpl * This)1113 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
1114 {
1115 DirectSoundDevice_RemoveBuffer(This->device, This);
1116 RtlDeleteResource(&This->lock);
1117
1118 if (This->hwbuf)
1119 IDsDriverBuffer_Release(This->hwbuf);
1120 if (!This->hwbuf || (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY)) {
1121 This->buffer->ref--;
1122 list_remove(&This->entry);
1123 if (This->buffer->ref == 0) {
1124 HeapFree(GetProcessHeap(), 0, This->buffer->memory);
1125 HeapFree(GetProcessHeap(), 0, This->buffer);
1126 }
1127 }
1128
1129 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
1130 HeapFree(GetProcessHeap(), 0, This->notifies);
1131 HeapFree(GetProcessHeap(), 0, This->pwfx);
1132 HeapFree(GetProcessHeap(), 0, This);
1133
1134 TRACE("(%p) released\n", This);
1135 }
1136
IDirectSoundBufferImpl_Destroy(IDirectSoundBufferImpl * pdsb)1137 HRESULT IDirectSoundBufferImpl_Destroy(
1138 IDirectSoundBufferImpl *pdsb)
1139 {
1140 TRACE("(%p)\n",pdsb);
1141
1142 /* This keeps the *_Destroy functions from possibly deleting
1143 * this object until it is ready to be deleted */
1144 InterlockedIncrement(&pdsb->numIfaces);
1145
1146 if (pdsb->iks) {
1147 WARN("iks not NULL\n");
1148 IKsBufferPropertySetImpl_Destroy(pdsb->iks);
1149 pdsb->iks = NULL;
1150 }
1151
1152 if (pdsb->ds3db) {
1153 WARN("ds3db not NULL\n");
1154 IDirectSound3DBufferImpl_Destroy(pdsb->ds3db);
1155 pdsb->ds3db = NULL;
1156 }
1157
1158 if (pdsb->notify) {
1159 WARN("notify not NULL\n");
1160 IDirectSoundNotifyImpl_Destroy(pdsb->notify);
1161 pdsb->notify = NULL;
1162 }
1163
1164 secondarybuffer_destroy(pdsb);
1165
1166 return S_OK;
1167 }
1168
IDirectSoundBufferImpl_Duplicate(DirectSoundDevice * device,IDirectSoundBufferImpl ** ppdsb,IDirectSoundBufferImpl * pdsb)1169 HRESULT IDirectSoundBufferImpl_Duplicate(
1170 DirectSoundDevice *device,
1171 IDirectSoundBufferImpl **ppdsb,
1172 IDirectSoundBufferImpl *pdsb)
1173 {
1174 IDirectSoundBufferImpl *dsb;
1175 HRESULT hres = DS_OK;
1176 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1177
1178 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
1179 if (dsb == NULL) {
1180 WARN("out of memory\n");
1181 *ppdsb = NULL;
1182 return DSERR_OUTOFMEMORY;
1183 }
1184 CopyMemory(dsb, pdsb, sizeof(*dsb));
1185
1186 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1187 if (dsb->pwfx == NULL) {
1188 HeapFree(GetProcessHeap(),0,dsb);
1189 *ppdsb = NULL;
1190 return DSERR_OUTOFMEMORY;
1191 }
1192
1193 if (pdsb->hwbuf) {
1194 TRACE("duplicating hardware buffer\n");
1195
1196 hres = IDsDriver_DuplicateSoundBuffer(device->driver, pdsb->hwbuf,
1197 (LPVOID *)&dsb->hwbuf);
1198 if (FAILED(hres)) {
1199 WARN("IDsDriver_DuplicateSoundBuffer failed (%08x)\n", hres);
1200 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1201 HeapFree(GetProcessHeap(),0,dsb);
1202 *ppdsb = NULL;
1203 return hres;
1204 }
1205 }
1206
1207 dsb->buffer->ref++;
1208 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1209 dsb->ref = 1;
1210 dsb->numIfaces = 1;
1211 dsb->state = STATE_STOPPED;
1212 dsb->buf_mixpos = dsb->sec_mixpos = 0;
1213 dsb->notify = NULL;
1214 dsb->notifies = NULL;
1215 dsb->nrofnotifies = 0;
1216 dsb->device = device;
1217 dsb->ds3db = NULL;
1218 dsb->iks = NULL; /* FIXME? */
1219 dsb->tmp_buffer = NULL;
1220 DSOUND_RecalcFormat(dsb);
1221 DSOUND_MixToTemporary(dsb, 0, dsb->buflen, FALSE);
1222
1223 RtlInitializeResource(&dsb->lock);
1224
1225 /* register buffer */
1226 hres = DirectSoundDevice_AddBuffer(device, dsb);
1227 if (hres != DS_OK) {
1228 RtlDeleteResource(&dsb->lock);
1229 HeapFree(GetProcessHeap(),0,dsb->tmp_buffer);
1230 list_remove(&dsb->entry);
1231 dsb->buffer->ref--;
1232 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1233 HeapFree(GetProcessHeap(),0,dsb);
1234 dsb = NULL;
1235 }
1236
1237 *ppdsb = dsb;
1238 return hres;
1239 }
1240
1241 /*******************************************************************************
1242 * IKsBufferPropertySet
1243 */
1244
1245 /* IUnknown methods */
IKsBufferPropertySetImpl_QueryInterface(LPKSPROPERTYSET iface,REFIID riid,LPVOID * ppobj)1246 static HRESULT WINAPI IKsBufferPropertySetImpl_QueryInterface(
1247 LPKSPROPERTYSET iface,
1248 REFIID riid,
1249 LPVOID *ppobj )
1250 {
1251 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1252 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1253
1254 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb, riid, ppobj);
1255 }
1256
IKsBufferPropertySetImpl_AddRef(LPKSPROPERTYSET iface)1257 static ULONG WINAPI IKsBufferPropertySetImpl_AddRef(LPKSPROPERTYSET iface)
1258 {
1259 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1260 ULONG ref = InterlockedIncrement(&(This->ref));
1261 TRACE("(%p) ref was %d\n", This, ref - 1);
1262 return ref;
1263 }
1264
IKsBufferPropertySetImpl_Release(LPKSPROPERTYSET iface)1265 static ULONG WINAPI IKsBufferPropertySetImpl_Release(LPKSPROPERTYSET iface)
1266 {
1267 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1268 ULONG ref = InterlockedDecrement(&(This->ref));
1269 TRACE("(%p) ref was %d\n", This, ref + 1);
1270
1271 if (!ref) {
1272 This->dsb->iks = 0;
1273 IDirectSoundBuffer_Release((LPDIRECTSOUND3DBUFFER)This->dsb);
1274 HeapFree(GetProcessHeap(), 0, This);
1275 TRACE("(%p) released\n", This);
1276 }
1277 return ref;
1278 }
1279
IKsBufferPropertySetImpl_Get(LPKSPROPERTYSET iface,REFGUID guidPropSet,ULONG dwPropID,LPVOID pInstanceData,ULONG cbInstanceData,LPVOID pPropData,ULONG cbPropData,PULONG pcbReturned)1280 static HRESULT WINAPI IKsBufferPropertySetImpl_Get(
1281 LPKSPROPERTYSET iface,
1282 REFGUID guidPropSet,
1283 ULONG dwPropID,
1284 LPVOID pInstanceData,
1285 ULONG cbInstanceData,
1286 LPVOID pPropData,
1287 ULONG cbPropData,
1288 PULONG pcbReturned )
1289 {
1290 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1291 PIDSDRIVERPROPERTYSET ps;
1292 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1293 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1294
1295 if (This->dsb->hwbuf) {
1296 IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1297
1298 if (ps) {
1299 DSPROPERTY prop;
1300 HRESULT hres;
1301
1302 prop.s.Set = *guidPropSet;
1303 prop.s.Id = dwPropID;
1304 prop.s.Flags = 0; /* unused */
1305 prop.s.InstanceId = (ULONG_PTR)This->dsb->device;
1306
1307
1308 hres = IDsDriverPropertySet_Get(ps, &prop, pInstanceData, cbInstanceData, pPropData, cbPropData, pcbReturned);
1309
1310 IDsDriverPropertySet_Release(ps);
1311
1312 return hres;
1313 }
1314 }
1315
1316 return E_PROP_ID_UNSUPPORTED;
1317 }
1318
IKsBufferPropertySetImpl_Set(LPKSPROPERTYSET iface,REFGUID guidPropSet,ULONG dwPropID,LPVOID pInstanceData,ULONG cbInstanceData,LPVOID pPropData,ULONG cbPropData)1319 static HRESULT WINAPI IKsBufferPropertySetImpl_Set(
1320 LPKSPROPERTYSET iface,
1321 REFGUID guidPropSet,
1322 ULONG dwPropID,
1323 LPVOID pInstanceData,
1324 ULONG cbInstanceData,
1325 LPVOID pPropData,
1326 ULONG cbPropData )
1327 {
1328 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1329 PIDSDRIVERPROPERTYSET ps;
1330 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1331
1332 if (This->dsb->hwbuf) {
1333 IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1334
1335 if (ps) {
1336 DSPROPERTY prop;
1337 HRESULT hres;
1338
1339 prop.s.Set = *guidPropSet;
1340 prop.s.Id = dwPropID;
1341 prop.s.Flags = 0; /* unused */
1342 prop.s.InstanceId = (ULONG_PTR)This->dsb->device;
1343 hres = IDsDriverPropertySet_Set(ps,&prop,pInstanceData,cbInstanceData,pPropData,cbPropData);
1344
1345 IDsDriverPropertySet_Release(ps);
1346
1347 return hres;
1348 }
1349 }
1350
1351 return E_PROP_ID_UNSUPPORTED;
1352 }
1353
IKsBufferPropertySetImpl_QuerySupport(LPKSPROPERTYSET iface,REFGUID guidPropSet,ULONG dwPropID,PULONG pTypeSupport)1354 static HRESULT WINAPI IKsBufferPropertySetImpl_QuerySupport(
1355 LPKSPROPERTYSET iface,
1356 REFGUID guidPropSet,
1357 ULONG dwPropID,
1358 PULONG pTypeSupport )
1359 {
1360 IKsBufferPropertySetImpl *This = (IKsBufferPropertySetImpl *)iface;
1361 PIDSDRIVERPROPERTYSET ps;
1362 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1363
1364 if (This->dsb->hwbuf) {
1365 IDsDriver_QueryInterface(This->dsb->hwbuf, &IID_IDsDriverPropertySet, (void **)&ps);
1366
1367 if (ps) {
1368 HRESULT hres;
1369
1370 hres = IDsDriverPropertySet_QuerySupport(ps,guidPropSet, dwPropID,pTypeSupport);
1371
1372 IDsDriverPropertySet_Release(ps);
1373
1374 return hres;
1375 }
1376 }
1377
1378 return E_PROP_ID_UNSUPPORTED;
1379 }
1380
1381 static const IKsPropertySetVtbl iksbvt = {
1382 IKsBufferPropertySetImpl_QueryInterface,
1383 IKsBufferPropertySetImpl_AddRef,
1384 IKsBufferPropertySetImpl_Release,
1385 IKsBufferPropertySetImpl_Get,
1386 IKsBufferPropertySetImpl_Set,
1387 IKsBufferPropertySetImpl_QuerySupport
1388 };
1389
IKsBufferPropertySetImpl_Create(IDirectSoundBufferImpl * dsb,IKsBufferPropertySetImpl ** piks)1390 HRESULT IKsBufferPropertySetImpl_Create(
1391 IDirectSoundBufferImpl *dsb,
1392 IKsBufferPropertySetImpl **piks)
1393 {
1394 IKsBufferPropertySetImpl *iks;
1395 TRACE("(%p,%p)\n",dsb,piks);
1396 *piks = NULL;
1397
1398 iks = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*iks));
1399 if (iks == 0) {
1400 WARN("out of memory\n");
1401 *piks = NULL;
1402 return DSERR_OUTOFMEMORY;
1403 }
1404
1405 iks->ref = 0;
1406 iks->dsb = dsb;
1407 dsb->iks = iks;
1408 iks->lpVtbl = &iksbvt;
1409
1410 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
1411
1412 *piks = iks;
1413 return S_OK;
1414 }
1415
IKsBufferPropertySetImpl_Destroy(IKsBufferPropertySetImpl * piks)1416 HRESULT IKsBufferPropertySetImpl_Destroy(
1417 IKsBufferPropertySetImpl *piks)
1418 {
1419 TRACE("(%p)\n",piks);
1420
1421 while (IKsBufferPropertySetImpl_Release((LPKSPROPERTYSET)piks) > 0);
1422
1423 return S_OK;
1424 }
1425