netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / dsound / buffer.c
blobdc3b54906cee53cbf1429c100695d8a13d9b99ce
1 /* DirectSound
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
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.
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.
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
22 #include <stdarg.h>
24 #define COBJMACROS
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "vfwmsgs.h"
31 #include "wine/debug.h"
32 #include "dsound.h"
33 #include "dsound_private.h"
34 #include "dsconf.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
38 /*******************************************************************************
39 * IDirectSoundNotify
42 static inline struct IDirectSoundBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
44 return CONTAINING_RECORD(iface, struct IDirectSoundBufferImpl, IDirectSoundNotify_iface);
47 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
48 void **ppobj)
50 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
52 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
54 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
57 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
59 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
60 ULONG ref = InterlockedIncrement(&This->refn);
62 TRACE("(%p) ref %d\n", This, ref);
64 if(ref == 1)
65 InterlockedIncrement(&This->numIfaces);
67 return ref;
70 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
72 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
73 ULONG ref = InterlockedDecrement(&This->refn);
75 TRACE("(%p) ref %d\n", This, ref);
77 if (!ref && !InterlockedDecrement(&This->numIfaces))
78 secondarybuffer_destroy(This);
80 return ref;
83 static int __cdecl notify_compar(const void *l, const void *r)
85 const DSBPOSITIONNOTIFY *left = l;
86 const DSBPOSITIONNOTIFY *right = r;
88 /* place DSBPN_OFFSETSTOP at the start of the sorted array */
89 if(left->dwOffset == DSBPN_OFFSETSTOP){
90 if(right->dwOffset != DSBPN_OFFSETSTOP)
91 return -1;
92 }else if(right->dwOffset == DSBPN_OFFSETSTOP)
93 return 1;
95 if(left->dwOffset == right->dwOffset)
96 return 0;
98 if(left->dwOffset < right->dwOffset)
99 return -1;
101 return 1;
104 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
105 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
107 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
109 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
111 if (howmuch > 0 && notify == NULL) {
112 WARN("invalid parameter: notify == NULL\n");
113 return DSERR_INVALIDPARAM;
116 if (TRACE_ON(dsound)) {
117 unsigned int i;
118 for (i=0;i<howmuch;i++)
119 TRACE("notify at %d to %p\n",
120 notify[i].dwOffset,notify[i].hEventNotify);
123 if (howmuch > 0) {
124 /* Make an internal copy of the caller-supplied array.
125 * Replace the existing copy if one is already present. */
126 HeapFree(GetProcessHeap(), 0, This->notifies);
127 This->notifies = HeapAlloc(GetProcessHeap(), 0,
128 howmuch * sizeof(DSBPOSITIONNOTIFY));
130 if (This->notifies == NULL) {
131 WARN("out of memory\n");
132 return DSERR_OUTOFMEMORY;
134 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
135 This->nrofnotifies = howmuch;
136 qsort(This->notifies, howmuch, sizeof(DSBPOSITIONNOTIFY), notify_compar);
137 } else {
138 HeapFree(GetProcessHeap(), 0, This->notifies);
139 This->notifies = NULL;
140 This->nrofnotifies = 0;
143 return S_OK;
146 static const IDirectSoundNotifyVtbl dsnvt =
148 IDirectSoundNotifyImpl_QueryInterface,
149 IDirectSoundNotifyImpl_AddRef,
150 IDirectSoundNotifyImpl_Release,
151 IDirectSoundNotifyImpl_SetNotificationPositions,
154 /*******************************************************************************
155 * IDirectSoundBuffer
158 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
160 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
163 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
165 return (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) != 0;
168 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
169 LPCWAVEFORMATEX wfex)
171 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
173 TRACE("(%p,%p)\n", iface, wfex);
175 if (is_primary_buffer(This))
176 return primarybuffer_SetFormat(This->device, wfex);
177 else {
178 WARN("not available for secondary buffers.\n");
179 return DSERR_INVALIDCALL;
183 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
185 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
186 LONG oldVol;
188 HRESULT hres = DS_OK;
190 TRACE("(%p,%d)\n",This,vol);
192 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
193 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
194 return DSERR_CONTROLUNAVAIL;
197 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
198 WARN("invalid parameter: vol = %d\n", vol);
199 return DSERR_INVALIDPARAM;
202 AcquireSRWLockExclusive(&This->lock);
204 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
205 oldVol = This->ds3db_lVolume;
206 This->ds3db_lVolume = vol;
207 if (vol != oldVol)
208 /* recalc 3d volume, which in turn recalcs the pans */
209 DSOUND_Calc3DBuffer(This);
210 } else {
211 oldVol = This->volpan.lVolume;
212 This->volpan.lVolume = vol;
213 if (vol != oldVol)
214 DSOUND_RecalcVolPan(&(This->volpan));
217 ReleaseSRWLockExclusive(&This->lock);
219 return hres;
222 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
224 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
226 TRACE("(%p,%p)\n",This,vol);
228 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
229 WARN("control unavailable\n");
230 return DSERR_CONTROLUNAVAIL;
233 if (vol == NULL) {
234 WARN("invalid parameter: vol == NULL\n");
235 return DSERR_INVALIDPARAM;
238 *vol = This->volpan.lVolume;
240 return DS_OK;
243 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
245 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
246 DWORD oldFreq;
248 TRACE("(%p,%d)\n",This,freq);
250 if (is_primary_buffer(This)) {
251 WARN("not available for primary buffers.\n");
252 return DSERR_CONTROLUNAVAIL;
255 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
256 WARN("control unavailable\n");
257 return DSERR_CONTROLUNAVAIL;
260 if (freq == DSBFREQUENCY_ORIGINAL)
261 freq = This->pwfx->nSamplesPerSec;
263 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
264 WARN("invalid parameter: freq = %d\n", freq);
265 return DSERR_INVALIDPARAM;
268 AcquireSRWLockExclusive(&This->lock);
270 oldFreq = This->freq;
271 This->freq = freq;
272 if (freq != oldFreq) {
273 This->freqAdjustNum = This->freq;
274 This->freqAdjustDen = This->device->pwfx->nSamplesPerSec;
275 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
276 DSOUND_RecalcFormat(This);
279 ReleaseSRWLockExclusive(&This->lock);
281 return DS_OK;
284 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
285 DWORD reserved2, DWORD flags)
287 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
288 HRESULT hres = DS_OK;
289 int i;
291 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
293 AcquireSRWLockExclusive(&This->lock);
295 This->playflags = flags;
296 if (This->state == STATE_STOPPED) {
297 This->leadin = TRUE;
298 This->state = STATE_STARTING;
301 for (i = 0; i < This->num_filters; i++) {
302 IMediaObject_Discontinuity(This->filters[i].obj, 0);
305 ReleaseSRWLockExclusive(&This->lock);
307 return hres;
310 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
312 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
313 HRESULT hres = DS_OK;
315 TRACE("(%p)\n",This);
317 AcquireSRWLockExclusive(&This->lock);
319 if (This->state == STATE_PLAYING || This->state == STATE_STARTING)
321 This->state = STATE_STOPPED;
322 DSOUND_CheckEvent(This, 0, 0);
325 ReleaseSRWLockExclusive(&This->lock);
327 return hres;
330 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
332 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
333 ULONG ref = InterlockedIncrement(&This->ref);
335 TRACE("(%p) ref %d\n", This, ref);
337 if(ref == 1)
338 InterlockedIncrement(&This->numIfaces);
340 return ref;
343 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
345 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
346 ULONG ref;
348 if (is_primary_buffer(This)){
349 ref = capped_refcount_dec(&This->ref);
350 if(!ref)
351 capped_refcount_dec(&This->numIfaces);
352 TRACE("(%p) ref %d\n", This, ref);
353 return ref;
356 ref = InterlockedDecrement(&This->ref);
357 if (!ref && !InterlockedDecrement(&This->numIfaces))
358 secondarybuffer_destroy(This);
360 TRACE("(%p) ref %d\n", This, ref);
362 return ref;
365 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
366 DWORD *playpos, DWORD *writepos)
368 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
369 DWORD pos;
371 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
373 AcquireSRWLockShared(&This->lock);
375 pos = This->sec_mixpos;
377 /* sanity */
378 if (pos >= This->buflen){
379 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
380 pos %= This->buflen;
383 if (playpos)
384 *playpos = pos;
385 if (writepos)
386 *writepos = pos;
388 if (writepos && This->state != STATE_STOPPED) {
389 /* apply the documented 10ms lead to writepos */
390 *writepos += This->writelead;
391 *writepos %= This->buflen;
394 ReleaseSRWLockShared(&This->lock);
396 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
397 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
399 return DS_OK;
402 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
404 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
406 TRACE("(%p,%p)\n",This,status);
408 if (status == NULL) {
409 WARN("invalid parameter: status = NULL\n");
410 return DSERR_INVALIDPARAM;
413 *status = 0;
414 AcquireSRWLockShared(&This->lock);
415 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
416 *status |= DSBSTATUS_PLAYING;
417 if (This->playflags & DSBPLAY_LOOPING)
418 *status |= DSBSTATUS_LOOPING;
420 if (This->dsbd.dwFlags & DSBCAPS_LOCDEFER)
421 *status |= DSBSTATUS_LOCSOFTWARE;
422 ReleaseSRWLockShared(&This->lock);
424 TRACE("status=%x\n", *status);
425 return DS_OK;
429 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
430 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
432 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
433 DWORD size;
435 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
437 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
439 if (lpwf) { /* NULL is valid */
440 if (wfsize >= size) {
441 CopyMemory(lpwf,This->pwfx,size);
442 if (wfwritten)
443 *wfwritten = size;
444 } else {
445 WARN("invalid parameter: wfsize too small\n");
446 CopyMemory(lpwf,This->pwfx,wfsize);
447 if (wfwritten)
448 *wfwritten = wfsize;
449 return DSERR_INVALIDPARAM;
451 } else {
452 if (wfwritten)
453 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
454 else {
455 WARN("invalid parameter: wfwritten == NULL\n");
456 return DSERR_INVALIDPARAM;
460 return DS_OK;
463 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
464 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
465 DWORD *audiobytes2, DWORD flags)
467 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
468 HRESULT hres = DS_OK;
470 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This, writecursor, writebytes, lplpaudioptr1,
471 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
473 if (!audiobytes1)
474 return DSERR_INVALIDPARAM;
476 /* when this flag is set, writecursor is meaningless and must be calculated */
477 if (flags & DSBLOCK_FROMWRITECURSOR) {
478 /* GetCurrentPosition does too much magic to duplicate here */
479 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
480 if (hres != DS_OK) {
481 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
482 return hres;
486 /* when this flag is set, writebytes is meaningless and must be set */
487 if (flags & DSBLOCK_ENTIREBUFFER)
488 writebytes = This->buflen;
490 if (writecursor >= This->buflen) {
491 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
492 writecursor, This->buflen);
493 return DSERR_INVALIDPARAM;
496 if (writebytes > This->buflen) {
497 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
498 writebytes, This->buflen);
499 return DSERR_INVALIDPARAM;
502 AcquireSRWLockShared(&This->lock);
504 if (writecursor+writebytes <= This->buflen) {
505 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
506 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
507 WARN("Overwriting mixing position, case 1\n");
508 *audiobytes1 = writebytes;
509 if (lplpaudioptr2)
510 *(LPBYTE*)lplpaudioptr2 = NULL;
511 if (audiobytes2)
512 *audiobytes2 = 0;
513 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
514 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
515 TRACE("->%d.0\n",writebytes);
516 This->buffer->lockedbytes += writebytes;
517 } else {
518 DWORD remainder = writebytes + writecursor - This->buflen;
519 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
520 *audiobytes1 = This->buflen-writecursor;
521 This->buffer->lockedbytes += *audiobytes1;
522 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
523 WARN("Overwriting mixing position, case 2\n");
524 if (lplpaudioptr2)
525 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
526 if (audiobytes2) {
527 *audiobytes2 = writebytes-(This->buflen-writecursor);
528 This->buffer->lockedbytes += *audiobytes2;
530 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
531 WARN("Overwriting mixing position, case 3\n");
532 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
535 ReleaseSRWLockShared(&This->lock);
537 return DS_OK;
540 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
541 DWORD newpos)
543 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
544 HRESULT hres = DS_OK;
546 TRACE("(%p,%d)\n",This,newpos);
548 AcquireSRWLockExclusive(&This->lock);
550 /* start mixing from this new location instead */
551 newpos %= This->buflen;
552 newpos -= newpos%This->pwfx->nBlockAlign;
553 This->sec_mixpos = newpos;
555 /* at this point, do not attempt to reset buffers, mess with primary mix position,
556 or anything like that to reduce latency. The data already prebuffered cannot be changed */
558 ReleaseSRWLockExclusive(&This->lock);
560 return hres;
563 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
565 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
566 HRESULT hres = DS_OK;
568 TRACE("(%p,%d)\n",This,pan);
570 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
571 WARN("invalid parameter: pan = %d\n", pan);
572 return DSERR_INVALIDPARAM;
575 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
576 WARN("control unavailable\n");
577 return DSERR_CONTROLUNAVAIL;
580 AcquireSRWLockExclusive(&This->lock);
582 if (This->volpan.lPan != pan) {
583 This->volpan.lPan = pan;
584 DSOUND_RecalcVolPan(&(This->volpan));
587 ReleaseSRWLockExclusive(&This->lock);
589 return hres;
592 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
594 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
596 TRACE("(%p,%p)\n",This,pan);
598 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
599 WARN("control unavailable\n");
600 return DSERR_CONTROLUNAVAIL;
603 if (pan == NULL) {
604 WARN("invalid parameter: pan = NULL\n");
605 return DSERR_INVALIDPARAM;
608 *pan = This->volpan.lPan;
610 return DS_OK;
613 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
614 void *p2, DWORD x2)
616 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
617 HRESULT hres = DS_OK;
619 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
621 if (!p2)
622 x2 = 0;
624 if((p1 && ((BYTE*)p1 < This->buffer->memory || (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
625 (p2 && ((BYTE*)p2 < This->buffer->memory || (BYTE*)p2 >= This->buffer->memory + This->buflen)))
626 return DSERR_INVALIDPARAM;
628 if (x1 || x2)
630 AcquireSRWLockShared(&This->device->buffer_list_lock);
631 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
633 AcquireSRWLockShared(&iter->lock);
634 if (x1)
636 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
637 hres = DSERR_INVALIDPARAM;
638 else
639 iter->buffer->lockedbytes -= x1;
642 if (x2)
644 if(x2 + (DWORD_PTR)p2 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
645 hres = DSERR_INVALIDPARAM;
646 else
647 iter->buffer->lockedbytes -= x2;
649 ReleaseSRWLockShared(&iter->lock);
651 ReleaseSRWLockShared(&This->device->buffer_list_lock);
654 return hres;
657 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
659 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
661 FIXME("(%p):stub\n",This);
662 return DS_OK;
665 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
667 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
669 TRACE("(%p,%p)\n",This,freq);
671 if (freq == NULL) {
672 WARN("invalid parameter: freq = NULL\n");
673 return DSERR_INVALIDPARAM;
676 *freq = This->freq;
677 TRACE("-> %d\n", *freq);
679 return DS_OK;
682 static const char* dump_DSFX_guid(const DSEFFECTDESC *desc)
684 #define FE(guid) if (IsEqualGUID(&guid, &desc->guidDSFXClass)) return #guid
685 FE(GUID_DSFX_STANDARD_GARGLE);
686 FE(GUID_DSFX_STANDARD_CHORUS);
687 FE(GUID_DSFX_STANDARD_FLANGER);
688 FE(GUID_DSFX_STANDARD_ECHO);
689 FE(GUID_DSFX_STANDARD_DISTORTION);
690 FE(GUID_DSFX_STANDARD_COMPRESSOR);
691 FE(GUID_DSFX_STANDARD_PARAMEQ);
692 FE(GUID_DSFX_STANDARD_I3DL2REVERB);
693 FE(GUID_DSFX_WAVES_REVERB);
694 #undef FE
696 return debugstr_guid(&desc->guidDSFXClass);
699 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
700 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
702 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
703 DWORD u;
704 DSFilter *filters;
705 HRESULT hr, hr2;
706 DMO_MEDIA_TYPE dmt;
707 WAVEFORMATEX wfx;
709 TRACE("(%p,%u,%p,%p)\n", This, dwEffectsCount, pDSFXDesc, pdwResultCodes);
711 if (pdwResultCodes)
712 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
714 if ((dwEffectsCount > 0 && !pDSFXDesc) ||
715 (dwEffectsCount == 0 && (pDSFXDesc || pdwResultCodes))
717 return E_INVALIDARG;
719 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFX)) {
720 WARN("attempted to call SetFX on buffer without DSBCAPS_CTRLFX\n");
721 return DSERR_CONTROLUNAVAIL;
724 if (This->state != STATE_STOPPED)
725 return DSERR_INVALIDCALL;
727 if (This->buffer->lockedbytes > 0)
728 return DSERR_INVALIDCALL;
730 if (dwEffectsCount == 0) {
731 if (This->num_filters > 0) {
732 for (u = 0; u < This->num_filters; u++) {
733 IMediaObject_Release(This->filters[u].obj);
735 HeapFree(GetProcessHeap(), 0, This->filters);
737 This->filters = NULL;
738 This->num_filters = 0;
741 return DS_OK;
744 filters = HeapAlloc(GetProcessHeap(), 0, dwEffectsCount * sizeof(DSFilter));
745 if (!filters) {
746 WARN("out of memory\n");
747 return DSERR_OUTOFMEMORY;
750 hr = DS_OK;
752 wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
753 wfx.nChannels = This->pwfx->nChannels;
754 wfx.nSamplesPerSec = This->pwfx->nSamplesPerSec;
755 wfx.wBitsPerSample = sizeof(float) * 8;
756 wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample)/8;
757 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
758 wfx.cbSize = sizeof(wfx);
760 dmt.majortype = KSDATAFORMAT_TYPE_AUDIO;
761 dmt.subtype = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
762 dmt.bFixedSizeSamples = TRUE;
763 dmt.bTemporalCompression = FALSE;
764 dmt.lSampleSize = sizeof(float) * This->pwfx->nChannels / 8;
765 dmt.formattype = FORMAT_WaveFormatEx;
766 dmt.pUnk = NULL;
767 dmt.cbFormat = sizeof(WAVEFORMATEX);
768 dmt.pbFormat = (BYTE*)&wfx;
770 for (u = 0; u < dwEffectsCount; u++) {
771 TRACE("%d: 0x%08x, %s\n", u, pDSFXDesc[u].dwFlags, dump_DSFX_guid(&pDSFXDesc[u]));
773 hr2 = CoCreateInstance(&pDSFXDesc[u].guidDSFXClass, NULL, CLSCTX_INPROC_SERVER, &IID_IMediaObject, (LPVOID*)&filters[u].obj);
775 if (SUCCEEDED(hr2)) {
776 hr2 = IMediaObject_SetInputType(filters[u].obj, 0, &dmt, 0);
777 if (FAILED(hr2))
778 WARN("Could not set DMO input type\n");
781 if (SUCCEEDED(hr2)) {
782 hr2 = IMediaObject_SetOutputType(filters[u].obj, 0, &dmt, 0);
783 if (FAILED(hr2))
784 WARN("Could not set DMO output type\n");
787 if (FAILED(hr2)) {
788 if (hr == DS_OK)
789 hr = hr2;
791 if (pdwResultCodes)
792 pdwResultCodes[u] = (hr2 == REGDB_E_CLASSNOTREG) ? DSFXR_UNKNOWN : DSFXR_FAILED;
793 } else {
794 if (pdwResultCodes)
795 pdwResultCodes[u] = DSFXR_LOCSOFTWARE;
799 if (FAILED(hr)) {
800 for (u = 0; u < dwEffectsCount; u++) {
801 if (pdwResultCodes)
802 pdwResultCodes[u] = (pdwResultCodes[u] != DSFXR_UNKNOWN) ? DSFXR_PRESENT : DSFXR_UNKNOWN;
804 if (filters[u].obj)
805 IMediaObject_Release(filters[u].obj);
808 HeapFree(GetProcessHeap(), 0, filters);
809 } else {
810 if (This->num_filters > 0) {
811 for (u = 0; u < This->num_filters; u++) {
812 IMediaObject_Release(This->filters[u].obj);
813 if (This->filters[u].inplace) IMediaObjectInPlace_Release(This->filters[u].inplace);
815 HeapFree(GetProcessHeap(), 0, This->filters);
818 for (u = 0; u < dwEffectsCount; u++) {
819 memcpy(&filters[u].guid, &pDSFXDesc[u].guidDSFXClass, sizeof(GUID));
820 if (FAILED(IMediaObject_QueryInterface(filters[u].obj, &IID_IMediaObjectInPlace, (void*)&filters[u].inplace)))
821 filters[u].inplace = NULL;
824 This->filters = filters;
825 This->num_filters = dwEffectsCount;
828 return hr;
831 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
832 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
834 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
835 DWORD u;
837 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
839 if (pdwResultCodes)
840 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
842 WARN("control unavailable\n");
843 return DS_OK;
846 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
847 REFGUID clsid, DWORD index, REFGUID iid, void **out)
849 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
850 DWORD i, count = 0;
852 TRACE("(%p,%s,%u,%s,%p)\n", This, debugstr_guid(clsid), index, debugstr_guid(iid), out);
854 if (!out)
855 return E_INVALIDARG;
857 for (i = 0; i < This->num_filters; i++)
859 if (IsEqualGUID(clsid, &This->filters[i].guid) || IsEqualGUID(clsid, &GUID_All_Objects))
861 if (count++ == index)
862 return IMediaObject_QueryInterface(This->filters[i].obj, iid, out);
865 return DSERR_OBJECTNOTFOUND;
868 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
869 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
871 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
873 WARN("(%p) already initialized\n", This);
874 return DSERR_ALREADYINITIALIZED;
877 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
879 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
881 TRACE("(%p)->(%p)\n",This,caps);
883 if (caps == NULL) {
884 WARN("invalid parameter: caps == NULL\n");
885 return DSERR_INVALIDPARAM;
888 if (caps->dwSize < sizeof(*caps)) {
889 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
890 return DSERR_INVALIDPARAM;
893 caps->dwFlags = This->dsbd.dwFlags;
894 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
896 caps->dwBufferBytes = This->buflen;
898 /* According to windows, this is zero*/
899 caps->dwUnlockTransferRate = 0;
900 caps->dwPlayCpuOverhead = 0;
902 return DS_OK;
905 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
906 void **ppobj)
908 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
910 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
912 if (ppobj == NULL) {
913 WARN("invalid parameter\n");
914 return E_INVALIDARG;
917 *ppobj = NULL; /* assume failure */
919 if ( IsEqualGUID(riid, &IID_IUnknown) ||
920 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
921 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
922 IDirectSoundBuffer8_AddRef(iface);
923 *ppobj = iface;
924 return S_OK;
927 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
928 if(This->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY) {
929 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
930 *ppobj = &This->IDirectSoundNotify_iface;
931 return S_OK;
934 TRACE( "App requested IDirectSoundNotify without DSBCAPS_CTRLPOSITIONNOTIFY flag.\n");
935 return E_NOINTERFACE;
938 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
939 if(This->dsbd.dwFlags & DSBCAPS_CTRL3D){
940 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
941 *ppobj = &This->IDirectSound3DBuffer_iface;
942 return S_OK;
944 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
945 return E_NOINTERFACE;
948 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
949 ERR("app requested IDirectSound3DListener on secondary buffer\n");
950 return E_NOINTERFACE;
953 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
954 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
955 *ppobj = &This->IKsPropertySet_iface;
956 return S_OK;
959 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
961 return E_NOINTERFACE;
964 static const IDirectSoundBuffer8Vtbl dsbvt =
966 IDirectSoundBufferImpl_QueryInterface,
967 IDirectSoundBufferImpl_AddRef,
968 IDirectSoundBufferImpl_Release,
969 IDirectSoundBufferImpl_GetCaps,
970 IDirectSoundBufferImpl_GetCurrentPosition,
971 IDirectSoundBufferImpl_GetFormat,
972 IDirectSoundBufferImpl_GetVolume,
973 IDirectSoundBufferImpl_GetPan,
974 IDirectSoundBufferImpl_GetFrequency,
975 IDirectSoundBufferImpl_GetStatus,
976 IDirectSoundBufferImpl_Initialize,
977 IDirectSoundBufferImpl_Lock,
978 IDirectSoundBufferImpl_Play,
979 IDirectSoundBufferImpl_SetCurrentPosition,
980 IDirectSoundBufferImpl_SetFormat,
981 IDirectSoundBufferImpl_SetVolume,
982 IDirectSoundBufferImpl_SetPan,
983 IDirectSoundBufferImpl_SetFrequency,
984 IDirectSoundBufferImpl_Stop,
985 IDirectSoundBufferImpl_Unlock,
986 IDirectSoundBufferImpl_Restore,
987 IDirectSoundBufferImpl_SetFX,
988 IDirectSoundBufferImpl_AcquireResources,
989 IDirectSoundBufferImpl_GetObjectInPath
992 HRESULT secondarybuffer_create(DirectSoundDevice *device, const DSBUFFERDESC *dsbd,
993 IDirectSoundBuffer **buffer)
995 IDirectSoundBufferImpl *dsb;
996 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
997 HRESULT err = DS_OK;
998 DWORD capf = 0;
999 size_t bufsize;
1001 TRACE("(%p,%p,%p)\n", device, dsbd, buffer);
1003 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1004 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
1005 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1008 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1010 if (!dsb)
1011 return DSERR_OUTOFMEMORY;
1013 TRACE("Created buffer at %p\n", dsb);
1015 dsb->ref = 1;
1016 dsb->refn = 0;
1017 dsb->ref3D = 0;
1018 dsb->refiks = 0;
1019 dsb->numIfaces = 1;
1020 dsb->device = device;
1021 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
1022 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
1023 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
1024 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1026 /* size depends on version */
1027 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1029 dsb->pwfx = DSOUND_CopyFormat(wfex);
1030 if (!dsb->pwfx) {
1031 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1032 return DSERR_OUTOFMEMORY;
1035 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1036 dsb->buflen = dsbd->dwBufferBytes +
1037 (dsbd->lpwfxFormat->nBlockAlign -
1038 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1039 else
1040 dsb->buflen = dsbd->dwBufferBytes;
1042 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1043 dsb->notifies = NULL;
1044 dsb->nrofnotifies = 0;
1046 /* Check necessary hardware mixing capabilities */
1047 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1048 else capf |= DSCAPS_SECONDARYMONO;
1049 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1050 else capf |= DSCAPS_SECONDARY8BIT;
1052 TRACE("capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", capf, device->drvcaps.dwFlags);
1054 /* Allocate an empty buffer */
1055 bufsize = (sizeof(*(dsb->buffer)) + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
1056 dsb->buffer = HeapAlloc(GetProcessHeap(),0,bufsize + dsb->buflen);
1057 if (!dsb->buffer) {
1058 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1059 return DSERR_OUTOFMEMORY;
1062 /* Allocate system memory for buffer */
1063 dsb->buffer->memory = (BYTE *)dsb->buffer + bufsize;
1065 dsb->buffer->ref = 1;
1066 dsb->buffer->lockedbytes = 0;
1067 list_init(&dsb->buffer->buffers);
1068 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1069 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1071 /* It's not necessary to initialize values to zero since */
1072 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1073 dsb->sec_mixpos = 0;
1074 dsb->state = STATE_STOPPED;
1076 dsb->freqAdjustNum = dsb->freq;
1077 dsb->freqAdjustDen = device->pwfx->nSamplesPerSec;
1078 dsb->nAvgBytesPerSec = dsb->freq *
1079 dsbd->lpwfxFormat->nBlockAlign;
1081 /* calculate fragment size and write lead */
1082 DSOUND_RecalcFormat(dsb);
1084 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1085 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1086 dsb->ds3db_ds3db.vPosition.x = 0.0;
1087 dsb->ds3db_ds3db.vPosition.y = 0.0;
1088 dsb->ds3db_ds3db.vPosition.z = 0.0;
1089 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1090 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1091 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1092 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1093 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1094 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1095 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1096 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1097 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1098 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1099 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1100 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1102 dsb->ds3db_need_recalc = FALSE;
1103 DSOUND_Calc3DBuffer(dsb);
1104 } else
1105 DSOUND_RecalcVolPan(&(dsb->volpan));
1107 InitializeSRWLock(&dsb->lock);
1109 /* register buffer */
1110 err = DirectSoundDevice_AddBuffer(device, dsb);
1111 if (err == DS_OK)
1112 *buffer = (IDirectSoundBuffer*)&dsb->IDirectSoundBuffer8_iface;
1113 else
1114 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1116 return err;
1119 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
1121 ULONG ref = InterlockedIncrement(&This->numIfaces);
1123 if (ref > 1)
1124 WARN("Destroying buffer with %u in use interfaces\n", ref - 1);
1126 if (This->dsbd.dwFlags & DSBCAPS_LOCHARDWARE)
1127 This->device->drvcaps.dwFreeHwMixingAllBuffers++;
1129 DirectSoundDevice_RemoveBuffer(This->device, This);
1131 This->buffer->ref--;
1132 list_remove(&This->entry);
1133 if (This->buffer->ref == 0)
1134 HeapFree(GetProcessHeap(), 0, This->buffer);
1136 HeapFree(GetProcessHeap(), 0, This->notifies);
1137 HeapFree(GetProcessHeap(), 0, This->pwfx);
1139 if (This->filters) {
1140 int i;
1141 for (i = 0; i < This->num_filters; i++) {
1142 IMediaObject_Release(This->filters[i].obj);
1143 if (This->filters[i].inplace) IMediaObjectInPlace_Release(This->filters[i].inplace);
1145 HeapFree(GetProcessHeap(), 0, This->filters);
1148 HeapFree(GetProcessHeap(), 0, This);
1150 TRACE("(%p) released\n", This);
1153 HRESULT IDirectSoundBufferImpl_Duplicate(
1154 DirectSoundDevice *device,
1155 IDirectSoundBufferImpl **ppdsb,
1156 IDirectSoundBufferImpl *pdsb)
1158 IDirectSoundBufferImpl *dsb;
1159 HRESULT hres = DS_OK;
1160 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1162 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
1163 if (dsb == NULL) {
1164 WARN("out of memory\n");
1165 *ppdsb = NULL;
1166 return DSERR_OUTOFMEMORY;
1169 AcquireSRWLockShared(&pdsb->lock);
1171 CopyMemory(dsb, pdsb, sizeof(*dsb));
1173 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1175 ReleaseSRWLockShared(&pdsb->lock);
1177 if (dsb->pwfx == NULL) {
1178 HeapFree(GetProcessHeap(),0,dsb);
1179 *ppdsb = NULL;
1180 return DSERR_OUTOFMEMORY;
1183 dsb->buffer->ref++;
1184 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1185 dsb->ref = 0;
1186 dsb->refn = 0;
1187 dsb->ref3D = 0;
1188 dsb->refiks = 0;
1189 dsb->numIfaces = 0;
1190 dsb->state = STATE_STOPPED;
1191 dsb->sec_mixpos = 0;
1192 dsb->notifies = NULL;
1193 dsb->nrofnotifies = 0;
1194 dsb->device = device;
1195 DSOUND_RecalcFormat(dsb);
1197 InitializeSRWLock(&dsb->lock);
1199 /* register buffer */
1200 hres = DirectSoundDevice_AddBuffer(device, dsb);
1201 if (hres != DS_OK) {
1202 list_remove(&dsb->entry);
1203 dsb->buffer->ref--;
1204 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1205 HeapFree(GetProcessHeap(),0,dsb);
1206 dsb = NULL;
1207 }else
1208 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1210 *ppdsb = dsb;
1211 return hres;
1214 /*******************************************************************************
1215 * IKsPropertySet
1218 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1220 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1223 /* IUnknown methods */
1224 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1225 void **ppobj)
1227 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1229 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1231 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1234 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1236 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1237 ULONG ref = InterlockedIncrement(&This->refiks);
1239 TRACE("(%p) ref %d\n", This, ref);
1241 if(ref == 1)
1242 InterlockedIncrement(&This->numIfaces);
1244 return ref;
1247 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1249 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1250 ULONG ref;
1252 if (is_primary_buffer(This)){
1253 ref = capped_refcount_dec(&This->refiks);
1254 if(!ref)
1255 capped_refcount_dec(&This->numIfaces);
1256 TRACE("(%p) ref %d\n", This, ref);
1257 return ref;
1260 ref = InterlockedDecrement(&This->refiks);
1261 if (!ref && !InterlockedDecrement(&This->numIfaces))
1262 secondarybuffer_destroy(This);
1264 TRACE("(%p) ref %d\n", This, ref);
1266 return ref;
1269 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1270 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1271 ULONG cbPropData, ULONG *pcbReturned)
1273 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1275 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1276 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1278 return E_PROP_ID_UNSUPPORTED;
1281 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1282 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1283 ULONG cbPropData)
1285 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1287 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1289 return E_PROP_ID_UNSUPPORTED;
1292 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1293 ULONG dwPropID, ULONG *pTypeSupport)
1295 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1297 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1299 return E_PROP_ID_UNSUPPORTED;
1302 const IKsPropertySetVtbl iksbvt = {
1303 IKsPropertySetImpl_QueryInterface,
1304 IKsPropertySetImpl_AddRef,
1305 IKsPropertySetImpl_Release,
1306 IKsPropertySetImpl_Get,
1307 IKsPropertySetImpl_Set,
1308 IKsPropertySetImpl_QuerySupport