msvcirt: Add implementation of streambuf::sputbackc.
[wine.git] / dlls / dsound / buffer.c
blobd735dc36059825b7383834a08fe2824389bedcfb
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 "winternl.h"
31 #include "vfwmsgs.h"
32 #include "wine/debug.h"
33 #include "dsound.h"
34 #include "dsound_private.h"
35 #include "dsconf.h"
37 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
39 /*******************************************************************************
40 * IDirectSoundNotify
43 static inline struct IDirectSoundBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
45 return CONTAINING_RECORD(iface, struct IDirectSoundBufferImpl, IDirectSoundNotify_iface);
48 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
49 void **ppobj)
51 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
53 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
55 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
58 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
60 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
61 ULONG ref = InterlockedIncrement(&This->refn);
63 TRACE("(%p) ref was %d\n", This, ref - 1);
65 if(ref == 1)
66 InterlockedIncrement(&This->numIfaces);
68 return ref;
71 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
73 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
74 ULONG ref = InterlockedDecrement(&This->refn);
76 TRACE("(%p) ref was %d\n", This, ref + 1);
78 if (!ref && !InterlockedDecrement(&This->numIfaces))
79 secondarybuffer_destroy(This);
81 return ref;
84 static int notify_compar(const void *l, const void *r)
86 const DSBPOSITIONNOTIFY *left = l;
87 const DSBPOSITIONNOTIFY *right = r;
89 /* place DSBPN_OFFSETSTOP at the start of the sorted array */
90 if(left->dwOffset == DSBPN_OFFSETSTOP){
91 if(right->dwOffset != DSBPN_OFFSETSTOP)
92 return -1;
93 }else if(right->dwOffset == DSBPN_OFFSETSTOP)
94 return 1;
96 if(left->dwOffset == right->dwOffset)
97 return 0;
99 if(left->dwOffset < right->dwOffset)
100 return -1;
102 return 1;
105 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
106 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
108 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
110 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
112 if (howmuch > 0 && notify == NULL) {
113 WARN("invalid parameter: notify == NULL\n");
114 return DSERR_INVALIDPARAM;
117 if (TRACE_ON(dsound)) {
118 unsigned int i;
119 for (i=0;i<howmuch;i++)
120 TRACE("notify at %d to %p\n",
121 notify[i].dwOffset,notify[i].hEventNotify);
124 if (howmuch > 0) {
125 /* Make an internal copy of the caller-supplied array.
126 * Replace the existing copy if one is already present. */
127 HeapFree(GetProcessHeap(), 0, This->notifies);
128 This->notifies = HeapAlloc(GetProcessHeap(), 0,
129 howmuch * sizeof(DSBPOSITIONNOTIFY));
131 if (This->notifies == NULL) {
132 WARN("out of memory\n");
133 return DSERR_OUTOFMEMORY;
135 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
136 This->nrofnotifies = howmuch;
137 qsort(This->notifies, howmuch, sizeof(DSBPOSITIONNOTIFY), notify_compar);
138 } else {
139 HeapFree(GetProcessHeap(), 0, This->notifies);
140 This->notifies = NULL;
141 This->nrofnotifies = 0;
144 return S_OK;
147 static const IDirectSoundNotifyVtbl dsnvt =
149 IDirectSoundNotifyImpl_QueryInterface,
150 IDirectSoundNotifyImpl_AddRef,
151 IDirectSoundNotifyImpl_Release,
152 IDirectSoundNotifyImpl_SetNotificationPositions,
155 /*******************************************************************************
156 * IDirectSoundBuffer
159 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
161 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
164 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
166 return (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) != 0;
169 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
170 LPCWAVEFORMATEX wfex)
172 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
174 TRACE("(%p,%p)\n", iface, wfex);
176 if (is_primary_buffer(This))
177 return primarybuffer_SetFormat(This->device, wfex);
178 else {
179 WARN("not available for secondary buffers.\n");
180 return DSERR_INVALIDCALL;
184 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
186 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
187 LONG oldVol;
189 HRESULT hres = DS_OK;
191 TRACE("(%p,%d)\n",This,vol);
193 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
194 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
195 return DSERR_CONTROLUNAVAIL;
198 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
199 WARN("invalid parameter: vol = %d\n", vol);
200 return DSERR_INVALIDPARAM;
203 /* **** */
204 RtlAcquireResourceExclusive(&This->lock, TRUE);
206 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
207 oldVol = This->ds3db_lVolume;
208 This->ds3db_lVolume = vol;
209 if (vol != oldVol)
210 /* recalc 3d volume, which in turn recalcs the pans */
211 DSOUND_Calc3DBuffer(This);
212 } else {
213 oldVol = This->volpan.lVolume;
214 This->volpan.lVolume = vol;
215 if (vol != oldVol)
216 DSOUND_RecalcVolPan(&(This->volpan));
219 RtlReleaseResource(&This->lock);
220 /* **** */
222 return hres;
225 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
227 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
229 TRACE("(%p,%p)\n",This,vol);
231 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
232 WARN("control unavailable\n");
233 return DSERR_CONTROLUNAVAIL;
236 if (vol == NULL) {
237 WARN("invalid parameter: vol == NULL\n");
238 return DSERR_INVALIDPARAM;
241 *vol = This->volpan.lVolume;
243 return DS_OK;
246 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
248 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
249 DWORD oldFreq;
251 TRACE("(%p,%d)\n",This,freq);
253 if (is_primary_buffer(This)) {
254 WARN("not available for primary buffers.\n");
255 return DSERR_CONTROLUNAVAIL;
258 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
259 WARN("control unavailable\n");
260 return DSERR_CONTROLUNAVAIL;
263 if (freq == DSBFREQUENCY_ORIGINAL)
264 freq = This->pwfx->nSamplesPerSec;
266 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
267 WARN("invalid parameter: freq = %d\n", freq);
268 return DSERR_INVALIDPARAM;
271 /* **** */
272 RtlAcquireResourceExclusive(&This->lock, TRUE);
274 oldFreq = This->freq;
275 This->freq = freq;
276 if (freq != oldFreq) {
277 This->freqAdjustNum = This->freq;
278 This->freqAdjustDen = This->device->pwfx->nSamplesPerSec;
279 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
280 DSOUND_RecalcFormat(This);
283 RtlReleaseResource(&This->lock);
284 /* **** */
286 return DS_OK;
289 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
290 DWORD reserved2, DWORD flags)
292 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
293 HRESULT hres = DS_OK;
294 int i;
296 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
298 /* **** */
299 RtlAcquireResourceExclusive(&This->lock, TRUE);
301 This->playflags = flags;
302 if (This->state == STATE_STOPPED) {
303 This->leadin = TRUE;
304 This->state = STATE_STARTING;
305 } else if (This->state == STATE_STOPPING)
306 This->state = STATE_PLAYING;
308 for (i = 0; i < This->num_filters; i++) {
309 IMediaObject_Discontinuity(This->filters[i].obj, 0);
312 RtlReleaseResource(&This->lock);
313 /* **** */
315 return hres;
318 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
320 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
321 HRESULT hres = DS_OK;
323 TRACE("(%p)\n",This);
325 /* **** */
326 RtlAcquireResourceExclusive(&This->lock, TRUE);
328 if (This->state == STATE_PLAYING)
329 This->state = STATE_STOPPING;
330 else if (This->state == STATE_STARTING)
332 This->state = STATE_STOPPED;
333 DSOUND_CheckEvent(This, 0, 0);
336 RtlReleaseResource(&This->lock);
337 /* **** */
339 return hres;
342 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
344 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
345 ULONG ref = InterlockedIncrement(&This->ref);
347 TRACE("(%p) ref was %d\n", This, ref - 1);
349 if(ref == 1)
350 InterlockedIncrement(&This->numIfaces);
352 return ref;
355 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
357 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
358 ULONG ref;
360 if (is_primary_buffer(This)){
361 ref = capped_refcount_dec(&This->ref);
362 if(!ref)
363 capped_refcount_dec(&This->numIfaces);
364 TRACE("(%p) ref is now: %d\n", This, ref);
365 return ref;
368 ref = InterlockedDecrement(&This->ref);
369 if (!ref && !InterlockedDecrement(&This->numIfaces))
370 secondarybuffer_destroy(This);
372 TRACE("(%p) ref is now %d\n", This, ref);
374 return ref;
377 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
378 DWORD *playpos, DWORD *writepos)
380 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
381 DWORD pos;
383 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
385 RtlAcquireResourceShared(&This->lock, TRUE);
387 pos = This->sec_mixpos;
389 /* sanity */
390 if (pos >= This->buflen){
391 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
392 pos %= This->buflen;
395 if (playpos)
396 *playpos = pos;
397 if (writepos)
398 *writepos = pos;
400 if (writepos && This->state != STATE_STOPPED) {
401 /* apply the documented 10ms lead to writepos */
402 *writepos += This->writelead;
403 *writepos %= This->buflen;
406 RtlReleaseResource(&This->lock);
408 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
409 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
411 return DS_OK;
414 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
416 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
418 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
420 if (status == NULL) {
421 WARN("invalid parameter: status = NULL\n");
422 return DSERR_INVALIDPARAM;
425 *status = 0;
426 RtlAcquireResourceShared(&This->lock, TRUE);
427 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
428 *status |= DSBSTATUS_PLAYING;
429 if (This->playflags & DSBPLAY_LOOPING)
430 *status |= DSBSTATUS_LOOPING;
432 RtlReleaseResource(&This->lock);
434 TRACE("status=%x\n", *status);
435 return DS_OK;
439 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
440 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
442 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
443 DWORD size;
445 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
447 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
449 if (lpwf) { /* NULL is valid */
450 if (wfsize >= size) {
451 CopyMemory(lpwf,This->pwfx,size);
452 if (wfwritten)
453 *wfwritten = size;
454 } else {
455 WARN("invalid parameter: wfsize too small\n");
456 CopyMemory(lpwf,This->pwfx,wfsize);
457 if (wfwritten)
458 *wfwritten = wfsize;
459 return DSERR_INVALIDPARAM;
461 } else {
462 if (wfwritten)
463 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
464 else {
465 WARN("invalid parameter: wfwritten == NULL\n");
466 return DSERR_INVALIDPARAM;
470 return DS_OK;
473 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
474 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
475 DWORD *audiobytes2, DWORD flags)
477 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
478 HRESULT hres = DS_OK;
480 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This, writecursor, writebytes, lplpaudioptr1,
481 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
483 if (!audiobytes1)
484 return DSERR_INVALIDPARAM;
486 /* when this flag is set, writecursor is meaningless and must be calculated */
487 if (flags & DSBLOCK_FROMWRITECURSOR) {
488 /* GetCurrentPosition does too much magic to duplicate here */
489 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
490 if (hres != DS_OK) {
491 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
492 return hres;
496 /* when this flag is set, writebytes is meaningless and must be set */
497 if (flags & DSBLOCK_ENTIREBUFFER)
498 writebytes = This->buflen;
500 if (writecursor >= This->buflen) {
501 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
502 writecursor, This->buflen);
503 return DSERR_INVALIDPARAM;
506 if (writebytes > This->buflen) {
507 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
508 writebytes, This->buflen);
509 return DSERR_INVALIDPARAM;
512 /* **** */
513 RtlAcquireResourceShared(&This->lock, TRUE);
515 if (writecursor+writebytes <= This->buflen) {
516 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
517 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
518 WARN("Overwriting mixing position, case 1\n");
519 *audiobytes1 = writebytes;
520 if (lplpaudioptr2)
521 *(LPBYTE*)lplpaudioptr2 = NULL;
522 if (audiobytes2)
523 *audiobytes2 = 0;
524 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
525 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
526 TRACE("->%d.0\n",writebytes);
527 This->buffer->lockedbytes += writebytes;
528 } else {
529 DWORD remainder = writebytes + writecursor - This->buflen;
530 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
531 *audiobytes1 = This->buflen-writecursor;
532 This->buffer->lockedbytes += *audiobytes1;
533 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
534 WARN("Overwriting mixing position, case 2\n");
535 if (lplpaudioptr2)
536 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
537 if (audiobytes2) {
538 *audiobytes2 = writebytes-(This->buflen-writecursor);
539 This->buffer->lockedbytes += *audiobytes2;
541 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
542 WARN("Overwriting mixing position, case 3\n");
543 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
546 RtlReleaseResource(&This->lock);
547 /* **** */
549 return DS_OK;
552 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
553 DWORD newpos)
555 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
556 HRESULT hres = DS_OK;
558 TRACE("(%p,%d)\n",This,newpos);
560 /* **** */
561 RtlAcquireResourceExclusive(&This->lock, TRUE);
563 /* start mixing from this new location instead */
564 newpos %= This->buflen;
565 newpos -= newpos%This->pwfx->nBlockAlign;
566 This->sec_mixpos = newpos;
568 /* at this point, do not attempt to reset buffers, mess with primary mix position,
569 or anything like that to reduce latency. The data already prebuffered cannot be changed */
571 RtlReleaseResource(&This->lock);
572 /* **** */
574 return hres;
577 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
579 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
580 HRESULT hres = DS_OK;
582 TRACE("(%p,%d)\n",This,pan);
584 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
585 WARN("invalid parameter: pan = %d\n", pan);
586 return DSERR_INVALIDPARAM;
589 /* You cannot use both pan and 3D controls */
590 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
591 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
592 WARN("control unavailable\n");
593 return DSERR_CONTROLUNAVAIL;
596 /* **** */
597 RtlAcquireResourceExclusive(&This->lock, TRUE);
599 if (This->volpan.lPan != pan) {
600 This->volpan.lPan = pan;
601 DSOUND_RecalcVolPan(&(This->volpan));
604 RtlReleaseResource(&This->lock);
605 /* **** */
607 return hres;
610 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
612 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
614 TRACE("(%p,%p)\n",This,pan);
616 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
617 WARN("control unavailable\n");
618 return DSERR_CONTROLUNAVAIL;
621 if (pan == NULL) {
622 WARN("invalid parameter: pan = NULL\n");
623 return DSERR_INVALIDPARAM;
626 *pan = This->volpan.lPan;
628 return DS_OK;
631 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
632 void *p2, DWORD x2)
634 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
635 HRESULT hres = DS_OK;
637 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
639 if (!p2)
640 x2 = 0;
642 if((p1 && ((BYTE*)p1 < This->buffer->memory || (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
643 (p2 && ((BYTE*)p2 < This->buffer->memory || (BYTE*)p2 >= This->buffer->memory + This->buflen)))
644 return DSERR_INVALIDPARAM;
646 if (x1 || x2)
648 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
649 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
651 RtlAcquireResourceShared(&iter->lock, TRUE);
652 if (x1)
654 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
655 hres = DSERR_INVALIDPARAM;
656 else
657 iter->buffer->lockedbytes -= x1;
660 if (x2)
662 if(x2 + (DWORD_PTR)p2 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
663 hres = DSERR_INVALIDPARAM;
664 else
665 iter->buffer->lockedbytes -= x2;
667 RtlReleaseResource(&iter->lock);
669 RtlReleaseResource(&This->device->buffer_list_lock);
672 return hres;
675 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
677 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
679 FIXME("(%p):stub\n",This);
680 return DS_OK;
683 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
685 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
687 TRACE("(%p,%p)\n",This,freq);
689 if (freq == NULL) {
690 WARN("invalid parameter: freq = NULL\n");
691 return DSERR_INVALIDPARAM;
694 *freq = This->freq;
695 TRACE("-> %d\n", *freq);
697 return DS_OK;
700 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
701 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
703 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
704 DWORD u;
705 DSFilter *filters;
706 HRESULT hr, hr2;
707 DMO_MEDIA_TYPE dmt;
708 WAVEFORMATEX wfx;
710 TRACE("(%p,%u,%p,%p)\n", This, dwEffectsCount, pDSFXDesc, pdwResultCodes);
712 if (pdwResultCodes)
713 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
715 if ((dwEffectsCount > 0 && !pDSFXDesc) ||
716 (dwEffectsCount == 0 && (pDSFXDesc || pdwResultCodes))
718 return E_INVALIDARG;
720 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFX)) {
721 WARN("attempted to call SetFX on buffer without DSBCAPS_CTRLFX\n");
722 return DSERR_CONTROLUNAVAIL;
725 if (This->state != STATE_STOPPED)
726 return DSERR_INVALIDCALL;
728 if (This->buffer->lockedbytes > 0)
729 return DSERR_INVALIDCALL;
731 if (dwEffectsCount == 0) {
732 if (This->num_filters > 0) {
733 for (u = 0; u < This->num_filters; u++) {
734 IMediaObject_Release(This->filters[u].obj);
736 HeapFree(GetProcessHeap(), 0, This->filters);
738 This->filters = NULL;
739 This->num_filters = 0;
742 return DS_OK;
745 filters = HeapAlloc(GetProcessHeap(), 0, dwEffectsCount * sizeof(DSFilter));
746 if (!filters) {
747 WARN("out of memory\n");
748 return DSERR_OUTOFMEMORY;
751 hr = DS_OK;
753 wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
754 wfx.nChannels = This->pwfx->nChannels;
755 wfx.nSamplesPerSec = This->pwfx->nSamplesPerSec;
756 wfx.wBitsPerSample = sizeof(float) * 8;
757 wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample)/8;
758 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
759 wfx.cbSize = sizeof(wfx);
761 dmt.majortype = KSDATAFORMAT_TYPE_AUDIO;
762 dmt.subtype = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
763 dmt.bFixedSizeSamples = TRUE;
764 dmt.bTemporalCompression = FALSE;
765 dmt.lSampleSize = sizeof(float) * This->pwfx->nChannels / 8;
766 dmt.formattype = FORMAT_WaveFormatEx;
767 dmt.pUnk = NULL;
768 dmt.cbFormat = sizeof(WAVEFORMATEX);
769 dmt.pbFormat = (BYTE*)&wfx;
771 for (u = 0; u < dwEffectsCount; u++) {
772 hr2 = CoCreateInstance(&pDSFXDesc[u].guidDSFXClass, NULL, CLSCTX_INPROC_SERVER, &IID_IMediaObject, (LPVOID*)&filters[u].obj);
774 if (SUCCEEDED(hr2)) {
775 hr2 = IMediaObject_SetInputType(filters[u].obj, 0, &dmt, 0);
776 if (FAILED(hr2))
777 WARN("Could not set DMO input type\n");
780 if (SUCCEEDED(hr2)) {
781 hr2 = IMediaObject_SetOutputType(filters[u].obj, 0, &dmt, 0);
782 if (FAILED(hr2))
783 WARN("Could not set DMO output type\n");
786 if (FAILED(hr2)) {
787 if (hr == DS_OK)
788 hr = hr2;
790 if (pdwResultCodes)
791 pdwResultCodes[u] = (hr2 == REGDB_E_CLASSNOTREG) ? DSFXR_UNKNOWN : DSFXR_FAILED;
792 } else {
793 if (pdwResultCodes)
794 pdwResultCodes[u] = DSFXR_LOCSOFTWARE;
798 if (FAILED(hr)) {
799 for (u = 0; u < dwEffectsCount; u++) {
800 if (pdwResultCodes)
801 pdwResultCodes[u] = (pdwResultCodes[u] != DSFXR_UNKNOWN) ? DSFXR_PRESENT : DSFXR_UNKNOWN;
803 if (filters[u].obj)
804 IMediaObject_Release(filters[u].obj);
807 HeapFree(GetProcessHeap(), 0, filters);
808 } else {
809 if (This->num_filters > 0) {
810 for (u = 0; u < This->num_filters; u++) {
811 IMediaObject_Release(This->filters[u].obj);
812 if (This->filters[u].inplace) IMediaObjectInPlace_Release(This->filters[u].inplace);
814 HeapFree(GetProcessHeap(), 0, This->filters);
817 for (u = 0; u < dwEffectsCount; u++) {
818 memcpy(&filters[u].guid, &pDSFXDesc[u].guidDSFXClass, sizeof(GUID));
819 if (FAILED(IMediaObject_QueryInterface(filters[u].obj, &IID_IMediaObjectInPlace, (void*)&filters[u].inplace)))
820 filters[u].inplace = NULL;
823 This->filters = filters;
824 This->num_filters = dwEffectsCount;
827 return hr;
830 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
831 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
833 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
834 DWORD u;
836 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
838 if (pdwResultCodes)
839 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
841 WARN("control unavailable\n");
842 return DS_OK;
845 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
846 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
848 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
850 TRACE("(%p,%s,%u,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
852 if (dwIndex >= This->num_filters)
853 return DSERR_CONTROLUNAVAIL;
855 if (!ppObject)
856 return E_INVALIDARG;
858 if (IsEqualGUID(rguidObject, &This->filters[dwIndex].guid) || IsEqualGUID(rguidObject, &GUID_All_Objects)) {
859 if (SUCCEEDED(IMediaObject_QueryInterface(This->filters[dwIndex].obj, rguidInterface, ppObject)))
860 return DS_OK;
861 else
862 return E_NOINTERFACE;
863 } else {
864 WARN("control unavailable\n");
865 return DSERR_OBJECTNOTFOUND;
869 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
870 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
872 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
874 WARN("(%p) already initialized\n", This);
875 return DSERR_ALREADYINITIALIZED;
878 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
880 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
882 TRACE("(%p)->(%p)\n",This,caps);
884 if (caps == NULL) {
885 WARN("invalid parameter: caps == NULL\n");
886 return DSERR_INVALIDPARAM;
889 if (caps->dwSize < sizeof(*caps)) {
890 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
891 return DSERR_INVALIDPARAM;
894 caps->dwFlags = This->dsbd.dwFlags;
895 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
897 caps->dwBufferBytes = This->buflen;
899 /* According to windows, this is zero*/
900 caps->dwUnlockTransferRate = 0;
901 caps->dwPlayCpuOverhead = 0;
903 return DS_OK;
906 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
907 void **ppobj)
909 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
911 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
913 if (ppobj == NULL) {
914 WARN("invalid parameter\n");
915 return E_INVALIDARG;
918 *ppobj = NULL; /* assume failure */
920 if ( IsEqualGUID(riid, &IID_IUnknown) ||
921 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
922 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
923 IDirectSoundBuffer8_AddRef(iface);
924 *ppobj = iface;
925 return S_OK;
928 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
929 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
930 *ppobj = &This->IDirectSoundNotify_iface;
931 return S_OK;
934 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
935 if(This->dsbd.dwFlags & DSBCAPS_CTRL3D){
936 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
937 *ppobj = &This->IDirectSound3DBuffer_iface;
938 return S_OK;
940 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
941 return E_NOINTERFACE;
944 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
945 ERR("app requested IDirectSound3DListener on secondary buffer\n");
946 return E_NOINTERFACE;
949 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
950 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
951 *ppobj = &This->IKsPropertySet_iface;
952 return S_OK;
955 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
957 return E_NOINTERFACE;
960 static const IDirectSoundBuffer8Vtbl dsbvt =
962 IDirectSoundBufferImpl_QueryInterface,
963 IDirectSoundBufferImpl_AddRef,
964 IDirectSoundBufferImpl_Release,
965 IDirectSoundBufferImpl_GetCaps,
966 IDirectSoundBufferImpl_GetCurrentPosition,
967 IDirectSoundBufferImpl_GetFormat,
968 IDirectSoundBufferImpl_GetVolume,
969 IDirectSoundBufferImpl_GetPan,
970 IDirectSoundBufferImpl_GetFrequency,
971 IDirectSoundBufferImpl_GetStatus,
972 IDirectSoundBufferImpl_Initialize,
973 IDirectSoundBufferImpl_Lock,
974 IDirectSoundBufferImpl_Play,
975 IDirectSoundBufferImpl_SetCurrentPosition,
976 IDirectSoundBufferImpl_SetFormat,
977 IDirectSoundBufferImpl_SetVolume,
978 IDirectSoundBufferImpl_SetPan,
979 IDirectSoundBufferImpl_SetFrequency,
980 IDirectSoundBufferImpl_Stop,
981 IDirectSoundBufferImpl_Unlock,
982 IDirectSoundBufferImpl_Restore,
983 IDirectSoundBufferImpl_SetFX,
984 IDirectSoundBufferImpl_AcquireResources,
985 IDirectSoundBufferImpl_GetObjectInPath
988 HRESULT IDirectSoundBufferImpl_Create(
989 DirectSoundDevice * device,
990 IDirectSoundBufferImpl **pdsb,
991 LPCDSBUFFERDESC dsbd)
993 IDirectSoundBufferImpl *dsb;
994 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
995 HRESULT err = DS_OK;
996 DWORD capf = 0;
997 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
999 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1000 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
1001 *pdsb = NULL;
1002 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1005 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1007 if (dsb == 0) {
1008 WARN("out of memory\n");
1009 *pdsb = NULL;
1010 return DSERR_OUTOFMEMORY;
1013 TRACE("Created buffer at %p\n", dsb);
1015 dsb->ref = 0;
1016 dsb->refn = 0;
1017 dsb->ref3D = 0;
1018 dsb->refiks = 0;
1019 dsb->numIfaces = 0;
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 == NULL) {
1031 HeapFree(GetProcessHeap(),0,dsb);
1032 *pdsb = NULL;
1033 return DSERR_OUTOFMEMORY;
1036 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1037 dsb->buflen = dsbd->dwBufferBytes +
1038 (dsbd->lpwfxFormat->nBlockAlign -
1039 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1040 else
1041 dsb->buflen = dsbd->dwBufferBytes;
1043 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1044 dsb->notifies = NULL;
1045 dsb->nrofnotifies = 0;
1047 /* Check necessary hardware mixing capabilities */
1048 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1049 else capf |= DSCAPS_SECONDARYMONO;
1050 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1051 else capf |= DSCAPS_SECONDARY8BIT;
1053 TRACE("capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", capf, device->drvcaps.dwFlags);
1055 /* Allocate an empty buffer */
1056 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1057 if (dsb->buffer == NULL) {
1058 WARN("out of memory\n");
1059 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1060 HeapFree(GetProcessHeap(),0,dsb);
1061 *pdsb = NULL;
1062 return DSERR_OUTOFMEMORY;
1065 /* Allocate system memory for buffer */
1066 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1067 if (dsb->buffer->memory == NULL) {
1068 WARN("out of memory\n");
1069 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1070 HeapFree(GetProcessHeap(),0,dsb->buffer);
1071 HeapFree(GetProcessHeap(),0,dsb);
1072 *pdsb = NULL;
1073 return DSERR_OUTOFMEMORY;
1076 dsb->buffer->ref = 1;
1077 dsb->buffer->lockedbytes = 0;
1078 list_init(&dsb->buffer->buffers);
1079 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1080 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1082 /* It's not necessary to initialize values to zero since */
1083 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1084 dsb->sec_mixpos = 0;
1085 dsb->state = STATE_STOPPED;
1087 dsb->freqAdjustNum = dsb->freq;
1088 dsb->freqAdjustDen = device->pwfx->nSamplesPerSec;
1089 dsb->nAvgBytesPerSec = dsb->freq *
1090 dsbd->lpwfxFormat->nBlockAlign;
1092 /* calculate fragment size and write lead */
1093 DSOUND_RecalcFormat(dsb);
1095 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1096 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1097 dsb->ds3db_ds3db.vPosition.x = 0.0;
1098 dsb->ds3db_ds3db.vPosition.y = 0.0;
1099 dsb->ds3db_ds3db.vPosition.z = 0.0;
1100 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1101 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1102 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1103 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1104 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1105 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1106 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1107 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1108 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1109 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1110 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1111 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1113 dsb->ds3db_need_recalc = FALSE;
1114 DSOUND_Calc3DBuffer(dsb);
1115 } else
1116 DSOUND_RecalcVolPan(&(dsb->volpan));
1118 RtlInitializeResource(&dsb->lock);
1120 /* register buffer if not primary */
1121 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1122 err = DirectSoundDevice_AddBuffer(device, dsb);
1123 if (err != DS_OK) {
1124 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1125 HeapFree(GetProcessHeap(),0,dsb->buffer);
1126 RtlDeleteResource(&dsb->lock);
1127 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1128 HeapFree(GetProcessHeap(),0,dsb);
1129 dsb = NULL;
1133 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1134 *pdsb = dsb;
1135 return err;
1138 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
1140 ULONG ref = InterlockedIncrement(&This->numIfaces);
1142 if (ref > 1)
1143 WARN("Destroying buffer with %u in use interfaces\n", ref - 1);
1145 if (This->dsbd.dwFlags & DSBCAPS_LOCHARDWARE)
1146 This->device->drvcaps.dwFreeHwMixingAllBuffers++;
1148 DirectSoundDevice_RemoveBuffer(This->device, This);
1149 RtlDeleteResource(&This->lock);
1151 This->buffer->ref--;
1152 list_remove(&This->entry);
1153 if (This->buffer->ref == 0) {
1154 HeapFree(GetProcessHeap(), 0, This->buffer->memory);
1155 HeapFree(GetProcessHeap(), 0, This->buffer);
1158 HeapFree(GetProcessHeap(), 0, This->notifies);
1159 HeapFree(GetProcessHeap(), 0, This->pwfx);
1161 if (This->filters) {
1162 int i;
1163 for (i = 0; i < This->num_filters; i++) {
1164 IMediaObject_Release(This->filters[i].obj);
1165 if (This->filters[i].inplace) IMediaObjectInPlace_Release(This->filters[i].inplace);
1167 HeapFree(GetProcessHeap(), 0, This->filters);
1170 HeapFree(GetProcessHeap(), 0, This);
1172 TRACE("(%p) released\n", This);
1175 HRESULT IDirectSoundBufferImpl_Duplicate(
1176 DirectSoundDevice *device,
1177 IDirectSoundBufferImpl **ppdsb,
1178 IDirectSoundBufferImpl *pdsb)
1180 IDirectSoundBufferImpl *dsb;
1181 HRESULT hres = DS_OK;
1182 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1184 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
1185 if (dsb == NULL) {
1186 WARN("out of memory\n");
1187 *ppdsb = NULL;
1188 return DSERR_OUTOFMEMORY;
1191 RtlAcquireResourceShared(&pdsb->lock, TRUE);
1193 CopyMemory(dsb, pdsb, sizeof(*dsb));
1195 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1197 RtlReleaseResource(&pdsb->lock);
1199 if (dsb->pwfx == NULL) {
1200 HeapFree(GetProcessHeap(),0,dsb);
1201 *ppdsb = NULL;
1202 return DSERR_OUTOFMEMORY;
1205 dsb->buffer->ref++;
1206 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1207 dsb->ref = 0;
1208 dsb->refn = 0;
1209 dsb->ref3D = 0;
1210 dsb->refiks = 0;
1211 dsb->numIfaces = 0;
1212 dsb->state = STATE_STOPPED;
1213 dsb->sec_mixpos = 0;
1214 dsb->notifies = NULL;
1215 dsb->nrofnotifies = 0;
1216 dsb->device = device;
1217 DSOUND_RecalcFormat(dsb);
1219 RtlInitializeResource(&dsb->lock);
1221 /* register buffer */
1222 hres = DirectSoundDevice_AddBuffer(device, dsb);
1223 if (hres != DS_OK) {
1224 RtlDeleteResource(&dsb->lock);
1225 list_remove(&dsb->entry);
1226 dsb->buffer->ref--;
1227 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1228 HeapFree(GetProcessHeap(),0,dsb);
1229 dsb = NULL;
1232 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1233 *ppdsb = dsb;
1234 return hres;
1237 /*******************************************************************************
1238 * IKsPropertySet
1241 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1243 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1246 /* IUnknown methods */
1247 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1248 void **ppobj)
1250 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1252 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1254 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1257 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1259 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1260 ULONG ref = InterlockedIncrement(&This->refiks);
1262 TRACE("(%p) ref was %d\n", This, ref - 1);
1264 if(ref == 1)
1265 InterlockedIncrement(&This->numIfaces);
1267 return ref;
1270 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1272 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1273 ULONG ref;
1275 if (is_primary_buffer(This)){
1276 ref = capped_refcount_dec(&This->refiks);
1277 if(!ref)
1278 capped_refcount_dec(&This->numIfaces);
1279 TRACE("(%p) ref is now: %d\n", This, ref);
1280 return ref;
1283 ref = InterlockedDecrement(&This->refiks);
1284 if (!ref && !InterlockedDecrement(&This->numIfaces))
1285 secondarybuffer_destroy(This);
1287 TRACE("(%p) ref is now %d\n", This, ref);
1289 return ref;
1292 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1293 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1294 ULONG cbPropData, ULONG *pcbReturned)
1296 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1298 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1299 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1301 return E_PROP_ID_UNSUPPORTED;
1304 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1305 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1306 ULONG cbPropData)
1308 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1310 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1312 return E_PROP_ID_UNSUPPORTED;
1315 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1316 ULONG dwPropID, ULONG *pTypeSupport)
1318 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1320 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1322 return E_PROP_ID_UNSUPPORTED;
1325 const IKsPropertySetVtbl iksbvt = {
1326 IKsPropertySetImpl_QueryInterface,
1327 IKsPropertySetImpl_AddRef,
1328 IKsPropertySetImpl_Release,
1329 IKsPropertySetImpl_Get,
1330 IKsPropertySetImpl_Set,
1331 IKsPropertySetImpl_QuerySupport