mf/tests: Clobber the alignment and bytes per second, to test if the DMO fixes it.
[wine.git] / dlls / dsound / buffer.c
blob8e261fde32cdd5dbbb4431f986fd4335d1ad07bc
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,%lu)\n",This,newpos);
579 if (newpos >= This->buflen) {
580 return E_INVALIDARG;
583 AcquireSRWLockExclusive(&This->lock);
585 /* start mixing from this new location instead */
586 newpos -= newpos%This->pwfx->nBlockAlign;
587 This->sec_mixpos = newpos;
589 This->use_committed = FALSE;
590 This->committed_mixpos = 0;
592 /* at this point, do not attempt to reset buffers, mess with primary mix position,
593 or anything like that to reduce latency. The data already prebuffered cannot be changed */
595 ReleaseSRWLockExclusive(&This->lock);
597 return hres;
600 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
602 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
603 HRESULT hres = DS_OK;
605 TRACE("(%p,%ld)\n",This,pan);
607 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
608 WARN("invalid parameter: pan = %ld\n", pan);
609 return DSERR_INVALIDPARAM;
612 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
613 WARN("control unavailable\n");
614 return DSERR_CONTROLUNAVAIL;
617 AcquireSRWLockExclusive(&This->lock);
619 if (This->volpan.lPan != pan) {
620 This->volpan.lPan = pan;
621 DSOUND_RecalcVolPan(&(This->volpan));
624 ReleaseSRWLockExclusive(&This->lock);
626 return hres;
629 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
631 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
633 TRACE("(%p,%p)\n",This,pan);
635 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
636 WARN("control unavailable\n");
637 return DSERR_CONTROLUNAVAIL;
640 if (pan == NULL) {
641 WARN("invalid parameter: pan = NULL\n");
642 return DSERR_INVALIDPARAM;
645 *pan = This->volpan.lPan;
647 return DS_OK;
650 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
651 void *p2, DWORD x2)
653 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
654 HRESULT hres = DS_OK;
656 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
658 if (!p2)
659 x2 = 0;
661 if((p1 && ((BYTE*)p1 < This->buffer->memory || (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
662 (p2 && ((BYTE*)p2 < This->buffer->memory || (BYTE*)p2 >= This->buffer->memory + This->buflen)))
663 return DSERR_INVALIDPARAM;
665 if (x1 || x2)
667 AcquireSRWLockShared(&This->device->buffer_list_lock);
668 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
670 AcquireSRWLockShared(&iter->lock);
671 if (x1)
673 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
674 hres = DSERR_INVALIDPARAM;
675 else
676 iter->buffer->lockedbytes -= x1;
679 if (x2)
681 if(x2 + (DWORD_PTR)p2 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
682 hres = DSERR_INVALIDPARAM;
683 else
684 iter->buffer->lockedbytes -= x2;
686 ReleaseSRWLockShared(&iter->lock);
688 ReleaseSRWLockShared(&This->device->buffer_list_lock);
691 return hres;
694 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
696 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
698 FIXME("(%p):stub\n",This);
699 return DS_OK;
702 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
704 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
706 TRACE("(%p,%p)\n",This,freq);
708 if (freq == NULL) {
709 WARN("invalid parameter: freq = NULL\n");
710 return DSERR_INVALIDPARAM;
713 *freq = (This->dsbd.dwFlags & DSBCAPS_CTRL3D) ? This->ds3db_freq : This->freq;
714 TRACE("-> %ld\n", *freq);
716 return DS_OK;
719 static const char* dump_DSFX_guid(const DSEFFECTDESC *desc)
721 #define FE(guid) if (IsEqualGUID(&guid, &desc->guidDSFXClass)) return #guid
722 FE(GUID_DSFX_STANDARD_GARGLE);
723 FE(GUID_DSFX_STANDARD_CHORUS);
724 FE(GUID_DSFX_STANDARD_FLANGER);
725 FE(GUID_DSFX_STANDARD_ECHO);
726 FE(GUID_DSFX_STANDARD_DISTORTION);
727 FE(GUID_DSFX_STANDARD_COMPRESSOR);
728 FE(GUID_DSFX_STANDARD_PARAMEQ);
729 FE(GUID_DSFX_STANDARD_I3DL2REVERB);
730 FE(GUID_DSFX_WAVES_REVERB);
731 #undef FE
733 return debugstr_guid(&desc->guidDSFXClass);
736 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
737 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
739 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
740 DWORD u;
741 DSFilter *filters;
742 HRESULT hr, hr2;
743 DMO_MEDIA_TYPE dmt;
744 WAVEFORMATEX wfx;
746 TRACE("(%p,%lu,%p,%p)\n", This, dwEffectsCount, pDSFXDesc, pdwResultCodes);
748 if (pdwResultCodes)
749 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
751 if ((dwEffectsCount > 0 && !pDSFXDesc) ||
752 (dwEffectsCount == 0 && (pDSFXDesc || pdwResultCodes))
754 return E_INVALIDARG;
756 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFX)) {
757 WARN("attempted to call SetFX on buffer without DSBCAPS_CTRLFX\n");
758 return DSERR_CONTROLUNAVAIL;
761 if (This->state != STATE_STOPPED)
762 return DSERR_INVALIDCALL;
764 if (This->buffer->lockedbytes > 0)
765 return DSERR_INVALIDCALL;
767 if (dwEffectsCount == 0) {
768 if (This->num_filters > 0) {
769 for (u = 0; u < This->num_filters; u++) {
770 IMediaObject_Release(This->filters[u].obj);
772 free(This->filters);
774 This->filters = NULL;
775 This->num_filters = 0;
778 return DS_OK;
781 filters = malloc(dwEffectsCount * sizeof(DSFilter));
782 if (!filters) {
783 WARN("out of memory\n");
784 return DSERR_OUTOFMEMORY;
787 hr = DS_OK;
789 wfx.wFormatTag = WAVE_FORMAT_IEEE_FLOAT;
790 wfx.nChannels = This->pwfx->nChannels;
791 wfx.nSamplesPerSec = This->pwfx->nSamplesPerSec;
792 wfx.wBitsPerSample = sizeof(float) * 8;
793 wfx.nBlockAlign = (wfx.nChannels * wfx.wBitsPerSample)/8;
794 wfx.nAvgBytesPerSec = wfx.nSamplesPerSec * wfx.nBlockAlign;
795 wfx.cbSize = sizeof(wfx);
797 dmt.majortype = KSDATAFORMAT_TYPE_AUDIO;
798 dmt.subtype = KSDATAFORMAT_SUBTYPE_IEEE_FLOAT;
799 dmt.bFixedSizeSamples = TRUE;
800 dmt.bTemporalCompression = FALSE;
801 dmt.lSampleSize = sizeof(float) * This->pwfx->nChannels / 8;
802 dmt.formattype = FORMAT_WaveFormatEx;
803 dmt.pUnk = NULL;
804 dmt.cbFormat = sizeof(WAVEFORMATEX);
805 dmt.pbFormat = (BYTE*)&wfx;
807 for (u = 0; u < dwEffectsCount; u++) {
808 TRACE("%ld: 0x%08lx, %s\n", u, pDSFXDesc[u].dwFlags, dump_DSFX_guid(&pDSFXDesc[u]));
810 hr2 = CoCreateInstance(&pDSFXDesc[u].guidDSFXClass, NULL, CLSCTX_INPROC_SERVER, &IID_IMediaObject, (LPVOID*)&filters[u].obj);
812 if (SUCCEEDED(hr2)) {
813 hr2 = IMediaObject_SetInputType(filters[u].obj, 0, &dmt, 0);
814 if (FAILED(hr2))
815 WARN("Could not set DMO input type\n");
818 if (SUCCEEDED(hr2)) {
819 hr2 = IMediaObject_SetOutputType(filters[u].obj, 0, &dmt, 0);
820 if (FAILED(hr2))
821 WARN("Could not set DMO output type\n");
824 if (FAILED(hr2)) {
825 if (hr == DS_OK)
826 hr = hr2;
828 if (pdwResultCodes)
829 pdwResultCodes[u] = (hr2 == REGDB_E_CLASSNOTREG) ? DSFXR_UNKNOWN : DSFXR_FAILED;
830 } else {
831 if (pdwResultCodes)
832 pdwResultCodes[u] = DSFXR_LOCSOFTWARE;
836 if (FAILED(hr)) {
837 for (u = 0; u < dwEffectsCount; u++) {
838 if (pdwResultCodes)
839 pdwResultCodes[u] = (pdwResultCodes[u] != DSFXR_UNKNOWN) ? DSFXR_PRESENT : DSFXR_UNKNOWN;
841 if (filters[u].obj)
842 IMediaObject_Release(filters[u].obj);
845 free(filters);
846 } else {
847 if (This->num_filters > 0) {
848 for (u = 0; u < This->num_filters; u++) {
849 IMediaObject_Release(This->filters[u].obj);
850 if (This->filters[u].inplace) IMediaObjectInPlace_Release(This->filters[u].inplace);
852 free(This->filters);
855 for (u = 0; u < dwEffectsCount; u++) {
856 memcpy(&filters[u].guid, &pDSFXDesc[u].guidDSFXClass, sizeof(GUID));
857 if (FAILED(IMediaObject_QueryInterface(filters[u].obj, &IID_IMediaObjectInPlace, (void*)&filters[u].inplace)))
858 filters[u].inplace = NULL;
861 This->filters = filters;
862 This->num_filters = dwEffectsCount;
865 return hr;
868 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
869 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
871 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
872 DWORD u;
874 FIXME("(%p,%08lu,%lu,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
876 if (pdwResultCodes)
877 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
879 WARN("control unavailable\n");
880 return DS_OK;
883 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
884 REFGUID clsid, DWORD index, REFGUID iid, void **out)
886 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
887 DWORD i, count = 0;
889 TRACE("(%p,%s,%lu,%s,%p)\n", This, debugstr_guid(clsid), index, debugstr_guid(iid), out);
891 if (!out)
892 return E_INVALIDARG;
894 for (i = 0; i < This->num_filters; i++)
896 if (IsEqualGUID(clsid, &This->filters[i].guid) || IsEqualGUID(clsid, &GUID_All_Objects))
898 if (count++ == index)
899 return IMediaObject_QueryInterface(This->filters[i].obj, iid, out);
902 return DSERR_OBJECTNOTFOUND;
905 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
906 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
908 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
910 WARN("(%p) already initialized\n", This);
911 return DSERR_ALREADYINITIALIZED;
914 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
916 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
918 TRACE("(%p)->(%p)\n",This,caps);
920 if (caps == NULL) {
921 WARN("invalid parameter: caps == NULL\n");
922 return DSERR_INVALIDPARAM;
925 if (caps->dwSize < sizeof(*caps)) {
926 WARN("invalid parameter: caps->dwSize = %ld\n",caps->dwSize);
927 return DSERR_INVALIDPARAM;
930 caps->dwFlags = This->dsbd.dwFlags;
931 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
933 caps->dwBufferBytes = This->buflen;
935 /* According to windows, this is zero*/
936 caps->dwUnlockTransferRate = 0;
937 caps->dwPlayCpuOverhead = 0;
939 return DS_OK;
942 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
943 void **ppobj)
945 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
947 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
949 if (ppobj == NULL) {
950 WARN("invalid parameter\n");
951 return E_INVALIDARG;
954 *ppobj = NULL; /* assume failure */
956 if ( IsEqualGUID(riid, &IID_IUnknown) ||
957 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
958 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
959 IDirectSoundBuffer8_AddRef(iface);
960 *ppobj = iface;
961 return S_OK;
964 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
965 if(This->dsbd.dwFlags & DSBCAPS_CTRLPOSITIONNOTIFY) {
966 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
967 *ppobj = &This->IDirectSoundNotify_iface;
968 return S_OK;
971 TRACE( "App requested IDirectSoundNotify without DSBCAPS_CTRLPOSITIONNOTIFY flag.\n");
972 return E_NOINTERFACE;
975 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
976 if(This->dsbd.dwFlags & DSBCAPS_CTRL3D){
977 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
978 *ppobj = &This->IDirectSound3DBuffer_iface;
979 return S_OK;
981 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
982 return E_NOINTERFACE;
985 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
986 ERR("app requested IDirectSound3DListener on secondary buffer\n");
987 return E_NOINTERFACE;
990 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
991 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
992 *ppobj = &This->IKsPropertySet_iface;
993 return S_OK;
996 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
998 return E_NOINTERFACE;
1001 static const IDirectSoundBuffer8Vtbl dsbvt =
1003 IDirectSoundBufferImpl_QueryInterface,
1004 IDirectSoundBufferImpl_AddRef,
1005 IDirectSoundBufferImpl_Release,
1006 IDirectSoundBufferImpl_GetCaps,
1007 IDirectSoundBufferImpl_GetCurrentPosition,
1008 IDirectSoundBufferImpl_GetFormat,
1009 IDirectSoundBufferImpl_GetVolume,
1010 IDirectSoundBufferImpl_GetPan,
1011 IDirectSoundBufferImpl_GetFrequency,
1012 IDirectSoundBufferImpl_GetStatus,
1013 IDirectSoundBufferImpl_Initialize,
1014 IDirectSoundBufferImpl_Lock,
1015 IDirectSoundBufferImpl_Play,
1016 IDirectSoundBufferImpl_SetCurrentPosition,
1017 IDirectSoundBufferImpl_SetFormat,
1018 IDirectSoundBufferImpl_SetVolume,
1019 IDirectSoundBufferImpl_SetPan,
1020 IDirectSoundBufferImpl_SetFrequency,
1021 IDirectSoundBufferImpl_Stop,
1022 IDirectSoundBufferImpl_Unlock,
1023 IDirectSoundBufferImpl_Restore,
1024 IDirectSoundBufferImpl_SetFX,
1025 IDirectSoundBufferImpl_AcquireResources,
1026 IDirectSoundBufferImpl_GetObjectInPath
1029 HRESULT secondarybuffer_create(DirectSoundDevice *device, const DSBUFFERDESC *dsbd,
1030 IDirectSoundBuffer **buffer)
1032 IDirectSoundBufferImpl *dsb;
1033 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1034 HRESULT err = DS_OK;
1035 DWORD capf = 0;
1036 size_t bufsize;
1038 TRACE("(%p,%p,%p)\n", device, dsbd, buffer);
1040 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1041 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1042 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1045 dsb = calloc(1, sizeof(*dsb));
1047 if (!dsb)
1048 return DSERR_OUTOFMEMORY;
1050 TRACE("Created buffer at %p\n", dsb);
1052 dsb->ref = 1;
1053 dsb->refn = 0;
1054 dsb->ref3D = 0;
1055 dsb->refiks = 0;
1056 dsb->numIfaces = 1;
1057 dsb->device = device;
1058 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
1059 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
1060 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
1061 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
1063 /* size depends on version */
1064 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
1066 dsb->pwfx = DSOUND_CopyFormat(wfex);
1067 if (!dsb->pwfx) {
1068 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1069 return DSERR_OUTOFMEMORY;
1072 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
1073 dsb->buflen = dsbd->dwBufferBytes +
1074 (dsbd->lpwfxFormat->nBlockAlign -
1075 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
1076 else
1077 dsb->buflen = dsbd->dwBufferBytes;
1079 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1080 dsb->notifies = NULL;
1081 dsb->nrofnotifies = 0;
1083 /* Check necessary hardware mixing capabilities */
1084 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1085 else capf |= DSCAPS_SECONDARYMONO;
1086 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1087 else capf |= DSCAPS_SECONDARY8BIT;
1089 TRACE("capf = 0x%08lx, device->drvcaps.dwFlags = 0x%08lx\n", capf, device->drvcaps.dwFlags);
1091 /* Allocate an empty buffer */
1092 bufsize = (sizeof(*(dsb->buffer)) + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
1093 dsb->buffer = malloc(bufsize + dsb->buflen);
1094 if (!dsb->buffer) {
1095 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1096 return DSERR_OUTOFMEMORY;
1099 /* Allocate system memory for buffer */
1100 dsb->buffer->memory = (BYTE *)dsb->buffer + bufsize;
1102 dsb->buffer->ref = 1;
1103 dsb->buffer->lockedbytes = 0;
1104 list_init(&dsb->buffer->buffers);
1105 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1106 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
1108 /* It's not necessary to initialize values to zero since */
1109 /* we allocated this structure with calloc... */
1110 dsb->sec_mixpos = 0;
1111 dsb->state = STATE_STOPPED;
1113 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1114 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1115 dsb->ds3db_ds3db.vPosition.x = 0.0;
1116 dsb->ds3db_ds3db.vPosition.y = 0.0;
1117 dsb->ds3db_ds3db.vPosition.z = 0.0;
1118 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1119 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1120 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1121 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1122 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1123 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1124 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1125 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1126 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1127 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1128 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1129 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1131 dsb->ds3db_freq = dsbd->lpwfxFormat->nSamplesPerSec;
1133 dsb->ds3db_need_recalc = FALSE;
1134 DSOUND_Calc3DBuffer(dsb);
1135 } else {
1136 DSOUND_RecalcVolPan(&(dsb->volpan));
1138 /* calculate fragment size and write lead */
1139 DSOUND_RecalcFormat(dsb);
1142 dsb->committedbuff = malloc(dsb->maxwritelead);
1143 if(!dsb->committedbuff) {
1144 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1145 return DSERR_OUTOFMEMORY;
1148 InitializeSRWLock(&dsb->lock);
1150 /* register buffer */
1151 err = DirectSoundDevice_AddBuffer(device, dsb);
1152 if (err == DS_OK)
1153 *buffer = (IDirectSoundBuffer*)&dsb->IDirectSoundBuffer8_iface;
1154 else
1155 IDirectSoundBuffer8_Release(&dsb->IDirectSoundBuffer8_iface);
1157 return err;
1160 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
1162 ULONG ref = InterlockedIncrement(&This->numIfaces);
1164 if (ref > 1)
1165 WARN("Destroying buffer with %lu in use interfaces\n", ref - 1);
1167 if (This->dsbd.dwFlags & DSBCAPS_LOCHARDWARE)
1168 This->device->drvcaps.dwFreeHwMixingAllBuffers++;
1170 DirectSoundDevice_RemoveBuffer(This->device, This);
1172 This->buffer->ref--;
1173 list_remove(&This->entry);
1174 if (This->buffer->ref == 0)
1175 free(This->buffer);
1177 free(This->notifies);
1178 free(This->pwfx);
1179 free(This->committedbuff);
1181 if (This->filters) {
1182 int i;
1183 for (i = 0; i < This->num_filters; i++) {
1184 IMediaObject_Release(This->filters[i].obj);
1185 if (This->filters[i].inplace) IMediaObjectInPlace_Release(This->filters[i].inplace);
1187 free(This->filters);
1190 TRACE("(%p) released\n", This);
1192 free(This);
1195 BOOL secondarybuffer_is_audible(IDirectSoundBufferImpl *This)
1197 UINT i;
1198 for (i = 0; i < This->device->pwfx->nChannels; i++) {
1199 if (This->volpan.dwTotalAmpFactor[i] != 0)
1200 return TRUE;
1203 return FALSE;
1206 HRESULT IDirectSoundBufferImpl_Duplicate(
1207 DirectSoundDevice *device,
1208 IDirectSoundBufferImpl **ppdsb,
1209 IDirectSoundBufferImpl *pdsb)
1211 IDirectSoundBufferImpl *dsb;
1212 HRESULT hres = DS_OK;
1213 VOID *committedbuff;
1214 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1216 dsb = malloc(sizeof(*dsb));
1217 if (dsb == NULL) {
1218 WARN("out of memory\n");
1219 *ppdsb = NULL;
1220 return DSERR_OUTOFMEMORY;
1223 committedbuff = malloc(pdsb->maxwritelead);
1224 if (committedbuff == NULL) {
1225 free(dsb);
1226 *ppdsb = NULL;
1227 return DSERR_OUTOFMEMORY;
1230 AcquireSRWLockShared(&pdsb->lock);
1232 CopyMemory(dsb, pdsb, sizeof(*dsb));
1234 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1236 ReleaseSRWLockShared(&pdsb->lock);
1238 if (dsb->pwfx == NULL) {
1239 free(committedbuff);
1240 free(dsb);
1241 *ppdsb = NULL;
1242 return DSERR_OUTOFMEMORY;
1245 dsb->buffer->ref++;
1246 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1247 dsb->ref = 0;
1248 dsb->refn = 0;
1249 dsb->ref3D = 0;
1250 dsb->refiks = 0;
1251 dsb->numIfaces = 0;
1252 dsb->state = STATE_STOPPED;
1253 dsb->sec_mixpos = 0;
1254 dsb->notifies = NULL;
1255 dsb->nrofnotifies = 0;
1256 dsb->device = device;
1257 dsb->committedbuff = committedbuff;
1258 dsb->use_committed = FALSE;
1259 dsb->committed_mixpos = 0;
1260 DSOUND_RecalcFormat(dsb);
1262 InitializeSRWLock(&dsb->lock);
1264 /* register buffer */
1265 hres = DirectSoundDevice_AddBuffer(device, dsb);
1266 if (hres != DS_OK) {
1267 list_remove(&dsb->entry);
1268 dsb->buffer->ref--;
1269 free(dsb->pwfx);
1270 free(dsb->committedbuff);
1271 free(dsb);
1272 dsb = NULL;
1273 }else
1274 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1276 *ppdsb = dsb;
1277 return hres;
1280 /*******************************************************************************
1281 * IKsPropertySet
1284 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1286 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1289 /* IUnknown methods */
1290 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1291 void **ppobj)
1293 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1295 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1297 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1300 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1302 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1303 ULONG ref = InterlockedIncrement(&This->refiks);
1305 TRACE("(%p) ref %ld\n", This, ref);
1307 if(ref == 1)
1308 InterlockedIncrement(&This->numIfaces);
1310 return ref;
1313 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1315 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1316 ULONG ref;
1318 if (is_primary_buffer(This)){
1319 ref = capped_refcount_dec(&This->refiks);
1320 if(!ref)
1321 capped_refcount_dec(&This->numIfaces);
1322 TRACE("(%p) ref %ld\n", This, ref);
1323 return ref;
1326 ref = InterlockedDecrement(&This->refiks);
1327 if (!ref && !InterlockedDecrement(&This->numIfaces))
1328 secondarybuffer_destroy(This);
1330 TRACE("(%p) ref %ld\n", This, ref);
1332 return ref;
1335 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1336 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1337 ULONG cbPropData, ULONG *pcbReturned)
1339 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1341 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%ld,pInstanceData=%p,cbInstanceData=%ld,pPropData=%p,cbPropData=%ld,pcbReturned=%p)\n",
1342 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1344 return E_PROP_ID_UNSUPPORTED;
1347 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1348 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1349 ULONG cbPropData)
1351 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1353 TRACE("(%p,%s,%ld,%p,%ld,%p,%ld)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1355 return E_PROP_ID_UNSUPPORTED;
1358 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1359 ULONG dwPropID, ULONG *pTypeSupport)
1361 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1363 TRACE("(%p,%s,%ld,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1365 return E_PROP_ID_UNSUPPORTED;
1368 const IKsPropertySetVtbl iksbvt = {
1369 IKsPropertySetImpl_QueryInterface,
1370 IKsPropertySetImpl_AddRef,
1371 IKsPropertySetImpl_Release,
1372 IKsPropertySetImpl_Get,
1373 IKsPropertySetImpl_Set,
1374 IKsPropertySetImpl_QuerySupport