winewayland.drv: Update desktop window size on display changes.
[wine.git] / dlls / dsound / buffer.c
blob1b724ee775bc828aa880e1356add0daa5b12d551
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 %ld\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 %ld\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 void commit_next_chunk(IDirectSoundBufferImpl *dsb)
106 void *dstbuff = dsb->committedbuff, *srcbuff = dsb->buffer->memory;
107 DWORD srcoff = dsb->sec_mixpos, srcsize = dsb->buflen, cpysize = dsb->writelead;
109 if(dsb->state != STATE_PLAYING)
110 return;
112 if(cpysize > srcsize - srcoff) {
113 DWORD overflow = cpysize - (srcsize - srcoff);
114 memcpy(dstbuff, (BYTE*)srcbuff + srcoff, srcsize - srcoff);
115 memcpy((BYTE*)dstbuff + (srcsize - srcoff), srcbuff, overflow);
116 }else{
117 memcpy(dstbuff, (BYTE*)srcbuff + srcoff, cpysize);
120 dsb->use_committed = TRUE;
121 dsb->committed_mixpos = 0;
122 TRACE("committing %lu bytes from offset %lu\n", dsb->writelead, dsb->sec_mixpos);
125 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
126 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
128 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
130 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
132 if (howmuch > 0 && notify == NULL) {
133 WARN("invalid parameter: notify == NULL\n");
134 return DSERR_INVALIDPARAM;
137 if (TRACE_ON(dsound)) {
138 unsigned int i;
139 for (i=0;i<howmuch;i++)
140 TRACE("notify at %ld to %p\n",
141 notify[i].dwOffset,notify[i].hEventNotify);
144 if (howmuch > 0) {
145 /* Make an internal copy of the caller-supplied array.
146 * Replace the existing copy if one is already present. */
147 free(This->notifies);
148 This->notifies = malloc(howmuch * sizeof(DSBPOSITIONNOTIFY));
150 if (This->notifies == NULL) {
151 WARN("out of memory\n");
152 return DSERR_OUTOFMEMORY;
154 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
155 This->nrofnotifies = howmuch;
156 qsort(This->notifies, howmuch, sizeof(DSBPOSITIONNOTIFY), notify_compar);
157 } else {
158 free(This->notifies);
159 This->notifies = NULL;
160 This->nrofnotifies = 0;
163 return S_OK;
166 static const IDirectSoundNotifyVtbl dsnvt =
168 IDirectSoundNotifyImpl_QueryInterface,
169 IDirectSoundNotifyImpl_AddRef,
170 IDirectSoundNotifyImpl_Release,
171 IDirectSoundNotifyImpl_SetNotificationPositions,
174 /*******************************************************************************
175 * IDirectSoundBuffer
178 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
180 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
183 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
185 return (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) != 0;
188 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
189 LPCWAVEFORMATEX wfex)
191 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
193 TRACE("(%p,%p)\n", iface, wfex);
195 if (is_primary_buffer(This))
196 return primarybuffer_SetFormat(This->device, wfex);
197 else {
198 WARN("not available for secondary buffers.\n");
199 return DSERR_INVALIDCALL;
203 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
205 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
206 LONG oldVol;
208 HRESULT hres = DS_OK;
210 TRACE("(%p,%ld)\n",This,vol);
212 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
213 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
214 return DSERR_CONTROLUNAVAIL;
217 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
218 WARN("invalid parameter: vol = %ld\n", vol);
219 return DSERR_INVALIDPARAM;
222 AcquireSRWLockExclusive(&This->lock);
224 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
225 oldVol = This->ds3db_lVolume;
226 This->ds3db_lVolume = vol;
227 if (vol != oldVol)
228 /* recalc 3d volume, which in turn recalcs the pans */
229 DSOUND_Calc3DBuffer(This);
230 } else {
231 oldVol = This->volpan.lVolume;
232 This->volpan.lVolume = vol;
233 if (vol != oldVol)
234 DSOUND_RecalcVolPan(&(This->volpan));
237 ReleaseSRWLockExclusive(&This->lock);
239 return hres;
242 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
244 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
246 TRACE("(%p,%p)\n",This,vol);
248 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
249 WARN("control unavailable\n");
250 return DSERR_CONTROLUNAVAIL;
253 if (vol == NULL) {
254 WARN("invalid parameter: vol == NULL\n");
255 return DSERR_INVALIDPARAM;
258 *vol = This->volpan.lVolume;
260 return DS_OK;
263 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
265 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
266 DWORD oldFreq;
268 TRACE("(%p,%ld)\n",This,freq);
270 if (is_primary_buffer(This)) {
271 WARN("not available for primary buffers.\n");
272 return DSERR_CONTROLUNAVAIL;
275 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
276 WARN("control unavailable\n");
277 return DSERR_CONTROLUNAVAIL;
280 if (freq == DSBFREQUENCY_ORIGINAL)
281 freq = This->pwfx->nSamplesPerSec;
283 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
284 WARN("invalid parameter: freq = %ld\n", freq);
285 return DSERR_INVALIDPARAM;
288 AcquireSRWLockExclusive(&This->lock);
290 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
291 oldFreq = This->ds3db_freq;
292 This->ds3db_freq = freq;
293 if (freq != oldFreq)
294 DSOUND_Calc3DBuffer(This);
295 } else {
296 oldFreq = This->freq;
297 This->freq = freq;
298 if (freq != oldFreq)
299 DSOUND_RecalcFormat(This);
302 ReleaseSRWLockExclusive(&This->lock);
304 return DS_OK;
307 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
308 DWORD reserved2, DWORD flags)
310 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
311 HRESULT hres = DS_OK;
312 int i;
314 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
316 AcquireSRWLockExclusive(&This->lock);
318 This->playflags = flags;
319 if (This->state == STATE_STOPPED) {
320 This->leadin = TRUE;
321 This->state = STATE_STARTING;
324 for (i = 0; i < This->num_filters; i++) {
325 IMediaObject_Discontinuity(This->filters[i].obj, 0);
328 ReleaseSRWLockExclusive(&This->lock);
330 return hres;
333 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
335 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
336 HRESULT hres = DS_OK;
338 TRACE("(%p)\n",This);
340 AcquireSRWLockExclusive(&This->lock);
342 if (This->state == STATE_PLAYING || This->state == STATE_STARTING)
344 This->state = STATE_STOPPED;
345 This->use_committed = FALSE;
346 This->committed_mixpos = 0;
347 DSOUND_CheckEvent(This, 0, 0);
350 ReleaseSRWLockExclusive(&This->lock);
352 return hres;
355 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
357 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
358 ULONG ref = InterlockedIncrement(&This->ref);
360 TRACE("(%p) ref %ld\n", This, ref);
362 if(ref == 1)
363 InterlockedIncrement(&This->numIfaces);
365 return ref;
368 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
370 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
371 ULONG ref;
373 if (is_primary_buffer(This)){
374 ref = capped_refcount_dec(&This->ref);
375 if(!ref)
376 capped_refcount_dec(&This->numIfaces);
377 TRACE("(%p) ref %ld\n", This, ref);
378 return ref;
381 ref = InterlockedDecrement(&This->ref);
382 if (!ref && !InterlockedDecrement(&This->numIfaces))
383 secondarybuffer_destroy(This);
385 TRACE("(%p) ref %ld\n", This, ref);
387 return ref;
390 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
391 DWORD *playpos, DWORD *writepos)
393 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
394 DWORD pos;
396 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
398 AcquireSRWLockShared(&This->lock);
400 pos = This->sec_mixpos;
402 /* sanity */
403 if (pos >= This->buflen){
404 FIXME("Bad play position. playpos: %ld, buflen: %ld\n", pos, This->buflen);
405 pos %= This->buflen;
408 if (playpos)
409 *playpos = pos;
410 if (writepos)
411 *writepos = pos;
413 if (writepos && This->state != STATE_STOPPED) {
414 /* apply the documented 10ms lead to writepos */
415 *writepos += This->writelead;
416 *writepos %= This->buflen;
419 ReleaseSRWLockShared(&This->lock);
421 TRACE("playpos = %ld, writepos = %ld, buflen=%ld (%p, time=%ld)\n",
422 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
424 return DS_OK;
427 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
429 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
431 TRACE("(%p,%p)\n",This,status);
433 if (status == NULL) {
434 WARN("invalid parameter: status = NULL\n");
435 return DSERR_INVALIDPARAM;
438 *status = 0;
439 AcquireSRWLockShared(&This->lock);
440 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
441 *status |= DSBSTATUS_PLAYING;
442 if (This->playflags & DSBPLAY_LOOPING)
443 *status |= DSBSTATUS_LOOPING;
445 if (This->dsbd.dwFlags & DSBCAPS_LOCDEFER)
446 *status |= DSBSTATUS_LOCSOFTWARE;
447 ReleaseSRWLockShared(&This->lock);
449 TRACE("status=%lx\n", *status);
450 return DS_OK;
454 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
455 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
457 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
458 DWORD size;
460 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
462 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
464 if (lpwf) { /* NULL is valid */
465 if (wfsize >= size) {
466 CopyMemory(lpwf,This->pwfx,size);
467 if (wfwritten)
468 *wfwritten = size;
469 } else {
470 WARN("invalid parameter: wfsize too small\n");
471 CopyMemory(lpwf,This->pwfx,wfsize);
472 if (wfwritten)
473 *wfwritten = wfsize;
474 return DSERR_INVALIDPARAM;
476 } else {
477 if (wfwritten)
478 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
479 else {
480 WARN("invalid parameter: wfwritten == NULL\n");
481 return DSERR_INVALIDPARAM;
485 return DS_OK;
488 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
489 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
490 DWORD *audiobytes2, DWORD flags)
492 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
493 HRESULT hres = DS_OK;
495 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n", This, writecursor, writebytes, lplpaudioptr1,
496 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
498 if (!audiobytes1)
499 return DSERR_INVALIDPARAM;
501 /* when this flag is set, writecursor is meaningless and must be calculated */
502 if (flags & DSBLOCK_FROMWRITECURSOR) {
503 /* GetCurrentPosition does too much magic to duplicate here */
504 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
505 if (hres != DS_OK) {
506 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
507 return hres;
511 /* when this flag is set, writebytes is meaningless and must be set */
512 if (flags & DSBLOCK_ENTIREBUFFER)
513 writebytes = This->buflen;
515 if (writecursor >= This->buflen) {
516 WARN("Invalid parameter, writecursor: %lu >= buflen: %lu\n",
517 writecursor, This->buflen);
518 return DSERR_INVALIDPARAM;
521 if (writebytes > This->buflen) {
522 WARN("Invalid parameter, writebytes: %lu > buflen: %lu\n",
523 writebytes, This->buflen);
524 return DSERR_INVALIDPARAM;
527 AcquireSRWLockShared(&This->lock);
529 if (writecursor+writebytes <= This->buflen) {
530 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
531 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING) {
532 WARN("Overwriting mixing position, case 1\n");
533 commit_next_chunk(This);
535 *audiobytes1 = writebytes;
536 if (lplpaudioptr2)
537 *(LPBYTE*)lplpaudioptr2 = NULL;
538 if (audiobytes2)
539 *audiobytes2 = 0;
540 TRACE("Locked %p(%li bytes) and %p(%li bytes) writecursor=%ld\n",
541 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
542 TRACE("->%ld.0\n",writebytes);
543 This->buffer->lockedbytes += writebytes;
544 } else {
545 DWORD remainder = writebytes + writecursor - This->buflen;
546 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
547 *audiobytes1 = This->buflen-writecursor;
548 This->buffer->lockedbytes += *audiobytes1;
549 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING) {
550 WARN("Overwriting mixing position, case 2\n");
551 commit_next_chunk(This);
553 if (lplpaudioptr2)
554 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
555 if (audiobytes2) {
556 *audiobytes2 = writebytes-(This->buflen-writecursor);
557 This->buffer->lockedbytes += *audiobytes2;
559 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING) {
560 WARN("Overwriting mixing position, case 3\n");
561 commit_next_chunk(This);
563 TRACE("Locked %p(%li bytes) and %p(%li bytes) writecursor=%ld\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
566 ReleaseSRWLockShared(&This->lock);
568 return DS_OK;
571 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
572 DWORD newpos)
574 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
575 HRESULT hres = DS_OK;
577 TRACE("(%p,%ld)\n",This,newpos);
579 AcquireSRWLockExclusive(&This->lock);
581 /* start mixing from this new location instead */
582 newpos %= This->buflen;
583 newpos -= newpos%This->pwfx->nBlockAlign;
584 This->sec_mixpos = newpos;
586 This->use_committed = FALSE;
587 This->committed_mixpos = 0;
589 /* at this point, do not attempt to reset buffers, mess with primary mix position,
590 or anything like that to reduce latency. The data already prebuffered cannot be changed */
592 ReleaseSRWLockExclusive(&This->lock);
594 return hres;
597 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
599 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
600 HRESULT hres = DS_OK;
602 TRACE("(%p,%ld)\n",This,pan);
604 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
605 WARN("invalid parameter: pan = %ld\n", pan);
606 return DSERR_INVALIDPARAM;
609 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
610 WARN("control unavailable\n");
611 return DSERR_CONTROLUNAVAIL;
614 AcquireSRWLockExclusive(&This->lock);
616 if (This->volpan.lPan != pan) {
617 This->volpan.lPan = pan;
618 DSOUND_RecalcVolPan(&(This->volpan));
621 ReleaseSRWLockExclusive(&This->lock);
623 return hres;
626 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
628 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
630 TRACE("(%p,%p)\n",This,pan);
632 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
633 WARN("control unavailable\n");
634 return DSERR_CONTROLUNAVAIL;
637 if (pan == NULL) {
638 WARN("invalid parameter: pan = NULL\n");
639 return DSERR_INVALIDPARAM;
642 *pan = This->volpan.lPan;
644 return DS_OK;
647 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
648 void *p2, DWORD x2)
650 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
651 HRESULT hres = DS_OK;
653 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
655 if (!p2)
656 x2 = 0;
658 if((p1 && ((BYTE*)p1 < This->buffer->memory || (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
659 (p2 && ((BYTE*)p2 < This->buffer->memory || (BYTE*)p2 >= This->buffer->memory + This->buflen)))
660 return DSERR_INVALIDPARAM;
662 if (x1 || x2)
664 AcquireSRWLockShared(&This->device->buffer_list_lock);
665 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
667 AcquireSRWLockShared(&iter->lock);
668 if (x1)
670 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
671 hres = DSERR_INVALIDPARAM;
672 else
673 iter->buffer->lockedbytes -= x1;
676 if (x2)
678 if(x2 + (DWORD_PTR)p2 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
679 hres = DSERR_INVALIDPARAM;
680 else
681 iter->buffer->lockedbytes -= x2;
683 ReleaseSRWLockShared(&iter->lock);
685 ReleaseSRWLockShared(&This->device->buffer_list_lock);
688 return hres;
691 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
693 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
695 FIXME("(%p):stub\n",This);
696 return DS_OK;
699 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
701 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
703 TRACE("(%p,%p)\n",This,freq);
705 if (freq == NULL) {
706 WARN("invalid parameter: freq = NULL\n");
707 return DSERR_INVALIDPARAM;
710 *freq = (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ? This->ds3db_freq : This->freq;
711 TRACE("-> %ld\n", *freq);
713 return DS_OK;
716 static const char* dump_DSFX_guid(const DSEFFECTDESC *desc)
718 #define FE(guid) if (IsEqualGUID(&guid, &desc->guidDSFXClass)) return #guid
719 FE(GUID_DSFX_STANDARD_GARGLE);
720 FE(GUID_DSFX_STANDARD_CHORUS);
721 FE(GUID_DSFX_STANDARD_FLANGER);
722 FE(GUID_DSFX_STANDARD_ECHO);
723 FE(GUID_DSFX_STANDARD_DISTORTION);
724 FE(GUID_DSFX_STANDARD_COMPRESSOR);
725 FE(GUID_DSFX_STANDARD_PARAMEQ);
726 FE(GUID_DSFX_STANDARD_I3DL2REVERB);
727 FE(GUID_DSFX_WAVES_REVERB);
728 #undef FE
730 return debugstr_guid(&desc->guidDSFXClass);
733 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
734 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
736 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
737 DWORD u;
738 DSFilter *filters;
739 HRESULT hr, hr2;
740 DMO_MEDIA_TYPE dmt;
741 WAVEFORMATEX wfx;
743 TRACE("(%p,%lu,%p,%p)\n", This, dwEffectsCount, pDSFXDesc, pdwResultCodes);
745 if (pdwResultCodes)
746 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
748 if ((dwEffectsCount > 0 && !pDSFXDesc) ||
749 (dwEffectsCount == 0 && (pDSFXDesc || pdwResultCodes))
751 return E_INVALIDARG;
753 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFX)) {
754 WARN("attempted to call SetFX on buffer without DSBCAPS_CTRLFX\n");
755 return DSERR_CONTROLUNAVAIL;
758 if (This->state != STATE_STOPPED)
759 return DSERR_INVALIDCALL;
761 if (This->buffer->lockedbytes > 0)
762 return DSERR_INVALIDCALL;
764 if (dwEffectsCount == 0) {
765 if (This->num_filters > 0) {
766 for (u = 0; u < This->num_filters; u++) {
767 IMediaObject_Release(This->filters[u].obj);
769 free(This->filters);
771 This->filters = NULL;
772 This->num_filters = 0;
775 return DS_OK;
778 filters = malloc(dwEffectsCount * sizeof(DSFilter));
779 if (!filters) {
780 WARN("out of memory\n");
781 return DSERR_OUTOFMEMORY;
784 hr = DS_OK;
786 wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
787 wfx.nChannels = This->pwfx->nChannels;
788 wfx.nSamplesPerSec = This->pwfx->nSamplesPerSec;
789 wfx.wBitsPerSample = sizeof(float) * 8;
790 wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample)/8;
791 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
792 wfx.cbSize = sizeof(wfx);
794 dmt.majortype = KSDATAFORMAT_TYPE_AUDIO;
795 dmt.subtype = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
796 dmt.bFixedSizeSamples = TRUE;
797 dmt.bTemporalCompression = FALSE;
798 dmt.lSampleSize = sizeof(float) * This->pwfx->nChannels / 8;
799 dmt.formattype = FORMAT_WaveFormatEx;
800 dmt.pUnk = NULL;
801 dmt.cbFormat = sizeof(WAVEFORMATEX);
802 dmt.pbFormat = (BYTE*)&wfx;
804 for (u = 0; u < dwEffectsCount; u++) {
805 TRACE("%ld: 0x%08lx, %s\n", u, pDSFXDesc[u].dwFlags, dump_DSFX_guid(&pDSFXDesc[u]));
807 hr2 = CoCreateInstance(&pDSFXDesc[u].guidDSFXClass, NULL, CLSCTX_INPROC_SERVER, &IID_IMediaObject, (LPVOID*)&filters[u].obj);
809 if (SUCCEEDED(hr2)) {
810 hr2 = IMediaObject_SetInputType(filters[u].obj, 0, &dmt, 0);
811 if (FAILED(hr2))
812 WARN("Could not set DMO input type\n");
815 if (SUCCEEDED(hr2)) {
816 hr2 = IMediaObject_SetOutputType(filters[u].obj, 0, &dmt, 0);
817 if (FAILED(hr2))
818 WARN("Could not set DMO output type\n");
821 if (FAILED(hr2)) {
822 if (hr == DS_OK)
823 hr = hr2;
825 if (pdwResultCodes)
826 pdwResultCodes[u] = (hr2 == REGDB_E_CLASSNOTREG) ? DSFXR_UNKNOWN : DSFXR_FAILED;
827 } else {
828 if (pdwResultCodes)
829 pdwResultCodes[u] = DSFXR_LOCSOFTWARE;
833 if (FAILED(hr)) {
834 for (u = 0; u < dwEffectsCount; u++) {
835 if (pdwResultCodes)
836 pdwResultCodes[u] = (pdwResultCodes[u] != DSFXR_UNKNOWN) ? DSFXR_PRESENT : DSFXR_UNKNOWN;
838 if (filters[u].obj)
839 IMediaObject_Release(filters[u].obj);
842 free(filters);
843 } else {
844 if (This->num_filters > 0) {
845 for (u = 0; u < This->num_filters; u++) {
846 IMediaObject_Release(This->filters[u].obj);
847 if (This->filters[u].inplace) IMediaObjectInPlace_Release(This->filters[u].inplace);
849 free(This->filters);
852 for (u = 0; u < dwEffectsCount; u++) {
853 memcpy(&filters[u].guid, &pDSFXDesc[u].guidDSFXClass, sizeof(GUID));
854 if (FAILED(IMediaObject_QueryInterface(filters[u].obj, &IID_IMediaObjectInPlace, (void*)&filters[u].inplace)))
855 filters[u].inplace = NULL;
858 This->filters = filters;
859 This->num_filters = dwEffectsCount;
862 return hr;
865 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
866 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
868 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
869 DWORD u;
871 FIXME("(%p,%08lu,%lu,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
873 if (pdwResultCodes)
874 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
876 WARN("control unavailable\n");
877 return DS_OK;
880 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
881 REFGUID clsid, DWORD index, REFGUID iid, void **out)
883 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
884 DWORD i, count = 0;
886 TRACE("(%p,%s,%lu,%s,%p)\n", This, debugstr_guid(clsid), index, debugstr_guid(iid), out);
888 if (!out)
889 return E_INVALIDARG;
891 for (i = 0; i < This->num_filters; i++)
893 if (IsEqualGUID(clsid, &This->filters[i].guid) || IsEqualGUID(clsid, &GUID_All_Objects))
895 if (count++ == index)
896 return IMediaObject_QueryInterface(This->filters[i].obj, iid, out);
899 return DSERR_OBJECTNOTFOUND;
902 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
903 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
905 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
907 WARN("(%p) already initialized\n", This);
908 return DSERR_ALREADYINITIALIZED;
911 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
913 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
915 TRACE("(%p)->(%p)\n",This,caps);
917 if (caps == NULL) {
918 WARN("invalid parameter: caps == NULL\n");
919 return DSERR_INVALIDPARAM;
922 if (caps->dwSize < sizeof(*caps)) {
923 WARN("invalid parameter: caps->dwSize = %ld\n",caps->dwSize);
924 return DSERR_INVALIDPARAM;
927 caps->dwFlags = This->dsbd.dwFlags;
928 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
930 caps->dwBufferBytes = This->buflen;
932 /* According to windows, this is zero*/
933 caps->dwUnlockTransferRate = 0;
934 caps->dwPlayCpuOverhead = 0;
936 return DS_OK;
939 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
940 void **ppobj)
942 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
944 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
946 if (ppobj == NULL) {
947 WARN("invalid parameter\n");
948 return E_INVALIDARG;
951 *ppobj = NULL; /* assume failure */
953 if ( IsEqualGUID(riid, &IID_IUnknown) ||
954 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
955 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
956 IDirectSoundBuffer8_AddRef(iface);
957 *ppobj = iface;
958 return S_OK;
961 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
962 if(This->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY) {
963 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
964 *ppobj = &This->IDirectSoundNotify_iface;
965 return S_OK;
968 TRACE( "App requested IDirectSoundNotify without DSBCAPS_CTRLPOSITIONNOTIFY flag.\n");
969 return E_NOINTERFACE;
972 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
973 if(This->dsbd.dwFlags & DSBCAPS_CTRL3D){
974 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
975 *ppobj = &This->IDirectSound3DBuffer_iface;
976 return S_OK;
978 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
979 return E_NOINTERFACE;
982 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
983 ERR("app requested IDirectSound3DListener on secondary buffer\n");
984 return E_NOINTERFACE;
987 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
988 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
989 *ppobj = &This->IKsPropertySet_iface;
990 return S_OK;
993 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
995 return E_NOINTERFACE;
998 static const IDirectSoundBuffer8Vtbl dsbvt =
1000 IDirectSoundBufferImpl_QueryInterface,
1001 IDirectSoundBufferImpl_AddRef,
1002 IDirectSoundBufferImpl_Release,
1003 IDirectSoundBufferImpl_GetCaps,
1004 IDirectSoundBufferImpl_GetCurrentPosition,
1005 IDirectSoundBufferImpl_GetFormat,
1006 IDirectSoundBufferImpl_GetVolume,
1007 IDirectSoundBufferImpl_GetPan,
1008 IDirectSoundBufferImpl_GetFrequency,
1009 IDirectSoundBufferImpl_GetStatus,
1010 IDirectSoundBufferImpl_Initialize,
1011 IDirectSoundBufferImpl_Lock,
1012 IDirectSoundBufferImpl_Play,
1013 IDirectSoundBufferImpl_SetCurrentPosition,
1014 IDirectSoundBufferImpl_SetFormat,
1015 IDirectSoundBufferImpl_SetVolume,
1016 IDirectSoundBufferImpl_SetPan,
1017 IDirectSoundBufferImpl_SetFrequency,
1018 IDirectSoundBufferImpl_Stop,
1019 IDirectSoundBufferImpl_Unlock,
1020 IDirectSoundBufferImpl_Restore,
1021 IDirectSoundBufferImpl_SetFX,
1022 IDirectSoundBufferImpl_AcquireResources,
1023 IDirectSoundBufferImpl_GetObjectInPath
1026 HRESULT secondarybuffer_create(DirectSoundDevice *device, const DSBUFFERDESC *dsbd,
1027 IDirectSoundBuffer **buffer)
1029 IDirectSoundBufferImpl *dsb;
1030 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1031 HRESULT err = DS_OK;
1032 DWORD capf = 0;
1033 size_t bufsize;
1035 TRACE("(%p,%p,%p)\n", device, dsbd, buffer);
1037 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1038 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1039 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1042 dsb = calloc(1, sizeof(*dsb));
1044 if (!dsb)
1045 return DSERR_OUTOFMEMORY;
1047 TRACE("Created buffer at %p\n", dsb);
1049 dsb->ref = 1;
1050 dsb->refn = 0;
1051 dsb->ref3D = 0;
1052 dsb->refiks = 0;
1053 dsb->numIfaces = 1;
1054 dsb->device = device;
1055 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
1056 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
1057 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
1058 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1060 /* size depends on version */
1061 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1063 dsb->pwfx = DSOUND_CopyFormat(wfex);
1064 if (!dsb->pwfx) {
1065 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1066 return DSERR_OUTOFMEMORY;
1069 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1070 dsb->buflen = dsbd->dwBufferBytes +
1071 (dsbd->lpwfxFormat->nBlockAlign -
1072 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1073 else
1074 dsb->buflen = dsbd->dwBufferBytes;
1076 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1077 dsb->notifies = NULL;
1078 dsb->nrofnotifies = 0;
1080 /* Check necessary hardware mixing capabilities */
1081 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1082 else capf |= DSCAPS_SECONDARYMONO;
1083 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1084 else capf |= DSCAPS_SECONDARY8BIT;
1086 TRACE("capf = 0x%08lx, device->drvcaps.dwFlags = 0x%08lx\n", capf, device->drvcaps.dwFlags);
1088 /* Allocate an empty buffer */
1089 bufsize = (sizeof(*(dsb->buffer)) + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
1090 dsb->buffer = malloc(bufsize + dsb->buflen);
1091 if (!dsb->buffer) {
1092 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1093 return DSERR_OUTOFMEMORY;
1096 /* Allocate system memory for buffer */
1097 dsb->buffer->memory = (BYTE *)dsb->buffer + bufsize;
1099 dsb->buffer->ref = 1;
1100 dsb->buffer->lockedbytes = 0;
1101 list_init(&dsb->buffer->buffers);
1102 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1103 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1105 /* It's not necessary to initialize values to zero since */
1106 /* we allocated this structure with calloc... */
1107 dsb->sec_mixpos = 0;
1108 dsb->state = STATE_STOPPED;
1110 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1111 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1112 dsb->ds3db_ds3db.vPosition.x = 0.0;
1113 dsb->ds3db_ds3db.vPosition.y = 0.0;
1114 dsb->ds3db_ds3db.vPosition.z = 0.0;
1115 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1116 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1117 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1118 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1119 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1120 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1121 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1122 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1123 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1124 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1125 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1126 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1128 dsb->ds3db_freq = dsbd->lpwfxFormat->nSamplesPerSec;
1130 dsb->ds3db_need_recalc = FALSE;
1131 DSOUND_Calc3DBuffer(dsb);
1132 } else {
1133 DSOUND_RecalcVolPan(&(dsb->volpan));
1135 /* calculate fragment size and write lead */
1136 DSOUND_RecalcFormat(dsb);
1139 dsb->committedbuff = malloc(dsb->maxwritelead);
1140 if(!dsb->committedbuff) {
1141 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1142 return DSERR_OUTOFMEMORY;
1145 InitializeSRWLock(&dsb->lock);
1147 /* register buffer */
1148 err = DirectSoundDevice_AddBuffer(device, dsb);
1149 if (err == DS_OK)
1150 *buffer = (IDirectSoundBuffer*)&dsb->IDirectSoundBuffer8_iface;
1151 else
1152 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1154 return err;
1157 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
1159 ULONG ref = InterlockedIncrement(&This->numIfaces);
1161 if (ref > 1)
1162 WARN("Destroying buffer with %lu in use interfaces\n", ref - 1);
1164 if (This->dsbd.dwFlags & DSBCAPS_LOCHARDWARE)
1165 This->device->drvcaps.dwFreeHwMixingAllBuffers++;
1167 DirectSoundDevice_RemoveBuffer(This->device, This);
1169 This->buffer->ref--;
1170 list_remove(&This->entry);
1171 if (This->buffer->ref == 0)
1172 free(This->buffer);
1174 free(This->notifies);
1175 free(This->pwfx);
1176 free(This->committedbuff);
1178 if (This->filters) {
1179 int i;
1180 for (i = 0; i < This->num_filters; i++) {
1181 IMediaObject_Release(This->filters[i].obj);
1182 if (This->filters[i].inplace) IMediaObjectInPlace_Release(This->filters[i].inplace);
1184 free(This->filters);
1187 TRACE("(%p) released\n", This);
1189 free(This);
1192 BOOL secondarybuffer_is_audible(IDirectSoundBufferImpl *This)
1194 UINT i;
1195 for (i = 0; i < This->device->pwfx->nChannels; i++) {
1196 if (This->volpan.dwTotalAmpFactor[i] != 0)
1197 return TRUE;
1200 return FALSE;
1203 HRESULT IDirectSoundBufferImpl_Duplicate(
1204 DirectSoundDevice *device,
1205 IDirectSoundBufferImpl **ppdsb,
1206 IDirectSoundBufferImpl *pdsb)
1208 IDirectSoundBufferImpl *dsb;
1209 HRESULT hres = DS_OK;
1210 VOID *committedbuff;
1211 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1213 dsb = malloc(sizeof(*dsb));
1214 if (dsb == NULL) {
1215 WARN("out of memory\n");
1216 *ppdsb = NULL;
1217 return DSERR_OUTOFMEMORY;
1220 committedbuff = malloc(pdsb->maxwritelead);
1221 if (committedbuff == NULL) {
1222 free(dsb);
1223 *ppdsb = NULL;
1224 return DSERR_OUTOFMEMORY;
1227 AcquireSRWLockShared(&pdsb->lock);
1229 CopyMemory(dsb, pdsb, sizeof(*dsb));
1231 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1233 ReleaseSRWLockShared(&pdsb->lock);
1235 if (dsb->pwfx == NULL) {
1236 free(committedbuff);
1237 free(dsb);
1238 *ppdsb = NULL;
1239 return DSERR_OUTOFMEMORY;
1242 dsb->buffer->ref++;
1243 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1244 dsb->ref = 0;
1245 dsb->refn = 0;
1246 dsb->ref3D = 0;
1247 dsb->refiks = 0;
1248 dsb->numIfaces = 0;
1249 dsb->state = STATE_STOPPED;
1250 dsb->sec_mixpos = 0;
1251 dsb->notifies = NULL;
1252 dsb->nrofnotifies = 0;
1253 dsb->device = device;
1254 dsb->committedbuff = committedbuff;
1255 dsb->use_committed = FALSE;
1256 dsb->committed_mixpos = 0;
1257 DSOUND_RecalcFormat(dsb);
1259 InitializeSRWLock(&dsb->lock);
1261 /* register buffer */
1262 hres = DirectSoundDevice_AddBuffer(device, dsb);
1263 if (hres != DS_OK) {
1264 list_remove(&dsb->entry);
1265 dsb->buffer->ref--;
1266 free(dsb->pwfx);
1267 free(dsb->committedbuff);
1268 free(dsb);
1269 dsb = NULL;
1270 }else
1271 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1273 *ppdsb = dsb;
1274 return hres;
1277 /*******************************************************************************
1278 * IKsPropertySet
1281 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1283 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1286 /* IUnknown methods */
1287 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1288 void **ppobj)
1290 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1292 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1294 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1297 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1299 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1300 ULONG ref = InterlockedIncrement(&This->refiks);
1302 TRACE("(%p) ref %ld\n", This, ref);
1304 if(ref == 1)
1305 InterlockedIncrement(&This->numIfaces);
1307 return ref;
1310 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1312 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1313 ULONG ref;
1315 if (is_primary_buffer(This)){
1316 ref = capped_refcount_dec(&This->refiks);
1317 if(!ref)
1318 capped_refcount_dec(&This->numIfaces);
1319 TRACE("(%p) ref %ld\n", This, ref);
1320 return ref;
1323 ref = InterlockedDecrement(&This->refiks);
1324 if (!ref && !InterlockedDecrement(&This->numIfaces))
1325 secondarybuffer_destroy(This);
1327 TRACE("(%p) ref %ld\n", This, ref);
1329 return ref;
1332 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1333 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1334 ULONG cbPropData, ULONG *pcbReturned)
1336 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1338 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%ld,pInstanceData=%p,cbInstanceData=%ld,pPropData=%p,cbPropData=%ld,pcbReturned=%p)\n",
1339 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1341 return E_PROP_ID_UNSUPPORTED;
1344 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1345 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1346 ULONG cbPropData)
1348 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1350 TRACE("(%p,%s,%ld,%p,%ld,%p,%ld)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1352 return E_PROP_ID_UNSUPPORTED;
1355 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1356 ULONG dwPropID, ULONG *pTypeSupport)
1358 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1360 TRACE("(%p,%s,%ld,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1362 return E_PROP_ID_UNSUPPORTED;
1365 const IKsPropertySetVtbl iksbvt = {
1366 IKsPropertySetImpl_QueryInterface,
1367 IKsPropertySetImpl_AddRef,
1368 IKsPropertySetImpl_Release,
1369 IKsPropertySetImpl_Get,
1370 IKsPropertySetImpl_Set,
1371 IKsPropertySetImpl_QuerySupport