Device enumeration callbacks should return a NULL guid for default
[wine/gsoc_dplay.git] / dlls / dsound / buffer.c
blobf248228ffd420c23bd40ad2e91f19c02eb1c7f1f
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "config.h"
23 #include <assert.h>
24 #include <stdarg.h>
25 #include <stdio.h>
26 #include <sys/types.h>
27 #include <sys/fcntl.h>
28 #ifdef HAVE_UNISTD_H
29 # include <unistd.h>
30 #endif
31 #include <stdlib.h>
32 #include <string.h>
33 #include <math.h>
35 #define NONAMELESSSTRUCT
36 #define NONAMELESSUNION
37 #include "windef.h"
38 #include "winbase.h"
39 #include "wingdi.h"
40 #include "winuser.h"
41 #include "winerror.h"
42 #include "mmsystem.h"
43 #include "winreg.h"
44 #include "winternl.h"
45 #include "mmddk.h"
46 #include "wine/windef16.h"
47 #include "wine/debug.h"
48 #include "dsound.h"
49 #include "dsdriver.h"
50 #include "dsound_private.h"
52 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
54 /*******************************************************************************
55 * IDirectSoundNotify
57 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
58 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
59 ) {
60 ICOM_THIS(IDirectSoundNotifyImpl,iface);
61 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
63 if (This->dsb == NULL) {
64 WARN("invalid parameter\n");
65 return E_INVALIDARG;
68 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
71 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface) {
72 ICOM_THIS(IDirectSoundNotifyImpl,iface);
73 DWORD ref;
75 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
77 ref = InterlockedIncrement(&(This->ref));
78 return ref;
81 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface) {
82 ICOM_THIS(IDirectSoundNotifyImpl,iface);
83 DWORD ref;
85 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
87 ref = InterlockedDecrement(&(This->ref));
88 if (ref == 0) {
89 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
90 This->dsb->notify = NULL;
91 HeapFree(GetProcessHeap(),0,This);
92 TRACE("(%p) released\n",This);
94 return ref;
97 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
98 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
99 ) {
100 ICOM_THIS(IDirectSoundNotifyImpl,iface);
101 TRACE("(%p,0x%08lx,%p)\n",This,howmuch,notify);
103 if (notify == NULL) {
104 WARN("invalid parameter: notify == NULL\n");
105 return DSERR_INVALIDPARAM;
108 if (TRACE_ON(dsound)) {
109 int i;
110 for (i=0;i<howmuch;i++)
111 TRACE("notify at %ld to 0x%08lx\n",
112 notify[i].dwOffset,(DWORD)notify[i].hEventNotify);
115 if (This->dsb->hwnotify) {
116 HRESULT hres;
117 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
118 if (hres != DS_OK)
119 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
120 return hres;
121 } else {
122 /* Make an internal copy of the caller-supplied array.
123 * Replace the existing copy if one is already present. */
124 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
125 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
126 if (This->dsb->notifies == NULL) {
127 WARN("out of memory\n");
128 return DSERR_OUTOFMEMORY;
130 memcpy(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
131 This->dsb->nrofnotifies = howmuch;
134 return S_OK;
137 ICOM_VTABLE(IDirectSoundNotify) dsnvt =
139 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
140 IDirectSoundNotifyImpl_QueryInterface,
141 IDirectSoundNotifyImpl_AddRef,
142 IDirectSoundNotifyImpl_Release,
143 IDirectSoundNotifyImpl_SetNotificationPositions,
146 HRESULT WINAPI IDirectSoundNotifyImpl_Create(
147 IDirectSoundBufferImpl * dsb,
148 IDirectSoundNotifyImpl **pdsn)
150 IDirectSoundNotifyImpl * dsn;
151 TRACE("(%p,%p)\n",dsb,pdsn);
153 dsn = (IDirectSoundNotifyImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
155 if (dsn == NULL) {
156 WARN("out of memory\n");
157 return DSERR_OUTOFMEMORY;
160 dsn->ref = 0;
161 dsn->lpVtbl = &dsnvt;
162 dsn->dsb = dsb;
163 dsb->notify = dsn;
164 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
166 *pdsn = dsn;
167 return DS_OK;
170 /*******************************************************************************
171 * IDirectSoundBuffer
174 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
175 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX wfex
177 ICOM_THIS(IDirectSoundBufferImpl,iface);
179 TRACE("(%p,%p)\n",This,wfex);
180 /* This method is not available on secondary buffers */
181 WARN("invalid call\n");
182 return DSERR_INVALIDCALL;
185 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
186 LPDIRECTSOUNDBUFFER8 iface,LONG vol
188 ICOM_THIS(IDirectSoundBufferImpl,iface);
189 LONG oldVol;
191 TRACE("(%p,%ld)\n",This,vol);
193 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
194 WARN("control unavailable: This->dsbd.dwFlags = 0x%08lx\n", This->dsbd.dwFlags);
195 return DSERR_CONTROLUNAVAIL;
198 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
199 WARN("invalid parameter: vol = %ld\n", vol);
200 return DSERR_INVALIDPARAM;
203 /* **** */
204 EnterCriticalSection(&(This->lock));
206 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
207 oldVol = This->ds3db_lVolume;
208 This->ds3db_lVolume = vol;
209 } else {
210 oldVol = This->volpan.lVolume;
211 This->volpan.lVolume = vol;
212 if (vol != oldVol)
213 DSOUND_RecalcVolPan(&(This->volpan));
216 if (vol != oldVol) {
217 if (This->hwbuf) {
218 HRESULT hres;
219 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
220 if (hres != DS_OK)
221 WARN("IDsDriverBuffer_SetVolumePan failed\n");
222 } else
223 DSOUND_ForceRemix(This);
226 LeaveCriticalSection(&(This->lock));
227 /* **** */
229 return DS_OK;
232 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
233 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
235 ICOM_THIS(IDirectSoundBufferImpl,iface);
236 TRACE("(%p,%p)\n",This,vol);
238 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
239 WARN("control unavailable\n");
240 return DSERR_CONTROLUNAVAIL;
243 if (vol == NULL) {
244 WARN("invalid parameter: vol == NULL\n");
245 return DSERR_INVALIDPARAM;
248 *vol = This->volpan.lVolume;
250 return DS_OK;
253 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
254 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
256 ICOM_THIS(IDirectSoundBufferImpl,iface);
257 DWORD oldFreq;
259 TRACE("(%p,%ld)\n",This,freq);
261 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
262 WARN("control unavailable\n");
263 return DSERR_CONTROLUNAVAIL;
266 if (freq == DSBFREQUENCY_ORIGINAL)
267 freq = This->wfx.nSamplesPerSec;
269 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
270 WARN("invalid parameter: freq = %ld\n", freq);
271 return DSERR_INVALIDPARAM;
274 /* **** */
275 EnterCriticalSection(&(This->lock));
277 oldFreq = This->freq;
278 This->freq = freq;
279 if (freq != oldFreq) {
280 This->freqAdjust = (freq << DSOUND_FREQSHIFT) / This->dsound->wfx.nSamplesPerSec;
281 This->nAvgBytesPerSec = freq * This->wfx.nBlockAlign;
282 DSOUND_RecalcFormat(This);
283 if (!This->hwbuf)
284 DSOUND_ForceRemix(This);
287 LeaveCriticalSection(&(This->lock));
288 /* **** */
290 return DS_OK;
293 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
294 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
296 HRESULT hres = DS_OK;
297 ICOM_THIS(IDirectSoundBufferImpl,iface);
298 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
300 /* **** */
301 EnterCriticalSection(&(This->lock));
303 This->playflags = flags;
304 if (This->state == STATE_STOPPED) {
305 This->leadin = TRUE;
306 This->startpos = This->buf_mixpos;
307 This->state = STATE_STARTING;
308 } else if (This->state == STATE_STOPPING)
309 This->state = STATE_PLAYING;
310 if (This->hwbuf) {
311 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
312 if (hres != DS_OK)
313 WARN("IDsDriverBuffer_Play failed\n");
314 else
315 This->state = STATE_PLAYING;
318 LeaveCriticalSection(&(This->lock));
319 /* **** */
321 return hres;
324 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
326 HRESULT hres = DS_OK;
327 ICOM_THIS(IDirectSoundBufferImpl,iface);
328 TRACE("(%p)\n",This);
330 /* **** */
331 EnterCriticalSection(&(This->lock));
333 if (This->state == STATE_PLAYING)
334 This->state = STATE_STOPPING;
335 else if (This->state == STATE_STARTING)
336 This->state = STATE_STOPPED;
337 if (This->hwbuf) {
338 hres = IDsDriverBuffer_Stop(This->hwbuf);
339 if (hres != DS_OK)
340 WARN("IDsDriverBuffer_Stop failed\n");
341 else
342 This->state = STATE_STOPPED;
344 DSOUND_CheckEvent(This, 0);
346 LeaveCriticalSection(&(This->lock));
347 /* **** */
349 return hres;
352 static DWORD WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface) {
353 ICOM_THIS(IDirectSoundBufferImpl,iface);
354 DWORD ref;
356 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
358 ref = InterlockedIncrement(&(This->ref));
359 if (!ref) {
360 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
362 return ref;
365 static DWORD WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface) {
366 ICOM_THIS(IDirectSoundBufferImpl,iface);
367 int i;
368 DWORD ref;
370 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
372 ref = InterlockedDecrement(&(This->ref));
373 if (ref) return ref;
375 RtlAcquireResourceExclusive(&(This->dsound->lock), TRUE);
376 for (i=0;i<This->dsound->nrofbuffers;i++)
377 if (This->dsound->buffers[i] == This)
378 break;
379 if (i < This->dsound->nrofbuffers) {
380 /* Put the last buffer of the list in the (now empty) position */
381 This->dsound->buffers[i] = This->dsound->buffers[This->dsound->nrofbuffers - 1];
382 This->dsound->nrofbuffers--;
383 This->dsound->buffers = HeapReAlloc(GetProcessHeap(),0,This->dsound->buffers,sizeof(LPDIRECTSOUNDBUFFER8)*This->dsound->nrofbuffers);
384 TRACE("buffer count is now %d\n", This->dsound->nrofbuffers);
385 IDirectSound_Release((LPDIRECTSOUND)This->dsound);
387 RtlReleaseResource(&(This->dsound->lock));
389 DeleteCriticalSection(&(This->lock));
391 if (This->hwbuf) {
392 IDsDriverBuffer_Release(This->hwbuf);
393 if (This->dsound->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
394 This->buffer->ref--;
395 if (This->buffer->ref==0) {
396 HeapFree(GetProcessHeap(),0,This->buffer->memory);
397 HeapFree(GetProcessHeap(),0,This->buffer);
400 } else {
401 This->buffer->ref--;
402 if (This->buffer->ref==0) {
403 HeapFree(GetProcessHeap(),0,This->buffer->memory);
404 HeapFree(GetProcessHeap(),0,This->buffer);
408 if (This->notifies != NULL)
409 HeapFree(GetProcessHeap(), 0, This->notifies);
411 HeapFree(GetProcessHeap(),0,This);
413 TRACE("(%p) released\n",This);
414 return 0;
417 DWORD DSOUND_CalcPlayPosition(IDirectSoundBufferImpl *This,
418 DWORD state, DWORD pplay, DWORD pwrite, DWORD pmix, DWORD bmix)
420 DWORD bplay;
422 TRACE("primary playpos=%ld, mixpos=%ld\n", pplay, pmix);
423 TRACE("this mixpos=%ld, time=%ld\n", bmix, GetTickCount());
425 /* the actual primary play position (pplay) is always behind last mixed (pmix),
426 * unless the computer is too slow or something */
427 /* we need to know how far away we are from there */
428 #if 0 /* we'll never fill the primary entirely */
429 if (pmix == pplay) {
430 if ((state == STATE_PLAYING) || (state == STATE_STOPPING)) {
431 /* wow, the software mixer is really doing well,
432 * seems the entire primary buffer is filled! */
433 pmix += This->dsound->buflen;
435 /* else: the primary buffer is not playing, so probably empty */
437 #endif
438 if (pmix < pplay) pmix += This->dsound->buflen; /* wraparound */
439 pmix -= pplay;
440 /* detect buffer underrun */
441 if (pwrite < pplay) pwrite += This->dsound->buflen; /* wraparound */
442 pwrite -= pplay;
443 if (pmix > (ds_snd_queue_max * This->dsound->fraglen + pwrite + This->dsound->writelead)) {
444 WARN("detected an underrun: primary queue was %ld\n",pmix);
445 pmix = 0;
447 /* divide the offset by its sample size */
448 pmix /= This->dsound->wfx.nBlockAlign;
449 TRACE("primary back-samples=%ld\n",pmix);
450 /* adjust for our frequency */
451 pmix = (pmix * This->freqAdjust) >> DSOUND_FREQSHIFT;
452 /* multiply by our own sample size */
453 pmix *= This->wfx.nBlockAlign;
454 TRACE("this back-offset=%ld\n", pmix);
455 /* subtract from our last mixed position */
456 bplay = bmix;
457 while (bplay < pmix) bplay += This->buflen; /* wraparound */
458 bplay -= pmix;
459 if (This->leadin && ((bplay < This->startpos) || (bplay > bmix))) {
460 /* seems we haven't started playing yet */
461 TRACE("this still in lead-in phase\n");
462 bplay = This->startpos;
464 /* return the result */
465 return bplay;
468 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
469 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
471 HRESULT hres;
472 ICOM_THIS(IDirectSoundBufferImpl,iface);
473 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
474 if (This->hwbuf) {
475 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
476 if (hres != DS_OK) {
477 WARN("IDsDriverBuffer_GetPosition failed\n");
478 return hres;
481 else {
482 if (playpos && (This->state != STATE_PLAYING)) {
483 /* we haven't been merged into the primary buffer (yet) */
484 *playpos = This->buf_mixpos;
486 else if (playpos) {
487 DWORD pplay, pwrite, lplay, splay, pstate;
488 /* let's get this exact; first, recursively call GetPosition on the primary */
489 EnterCriticalSection(&(This->dsound->mixlock));
490 if (DSOUND_PrimaryGetPosition(This->dsound, &pplay, &pwrite) != DS_OK)
491 WARN("DSOUND_PrimaryGetPosition failed\n");
492 /* detect HEL mode underrun */
493 pstate = This->dsound->state;
494 if (!(This->dsound->hwbuf || This->dsound->pwqueue)) {
495 TRACE("detected an underrun\n");
496 /* pplay = ? */
497 if (pstate == STATE_PLAYING)
498 pstate = STATE_STARTING;
499 else if (pstate == STATE_STOPPING)
500 pstate = STATE_STOPPED;
502 /* get data for ourselves while we still have the lock */
503 pstate &= This->state;
504 lplay = This->primary_mixpos;
505 splay = This->buf_mixpos;
506 if ((This->dsbd.dwFlags & DSBCAPS_GETCURRENTPOSITION2) || This->dsound->hwbuf) {
507 /* calculate play position using this */
508 *playpos = DSOUND_CalcPlayPosition(This, pstate, pplay, pwrite, lplay, splay);
509 } else {
510 /* (unless the app isn't using GETCURRENTPOSITION2) */
511 /* don't know exactly how this should be handled...
512 * the docs says that play cursor is reported as directly
513 * behind write cursor, hmm... */
514 /* let's just do what might work for Half-Life */
515 DWORD wp;
516 wp = (This->dsound->pwplay + ds_hel_margin) * This->dsound->fraglen;
517 while (wp >= This->dsound->buflen)
518 wp -= This->dsound->buflen;
519 *playpos = DSOUND_CalcPlayPosition(This, pstate, wp, pwrite, lplay, splay);
521 LeaveCriticalSection(&(This->dsound->mixlock));
523 if (writepos) *writepos = This->buf_mixpos;
525 if (writepos) {
526 if (This->state != STATE_STOPPED)
527 /* apply the documented 10ms lead to writepos */
528 *writepos += This->writelead;
529 while (*writepos >= This->buflen) *writepos -= This->buflen;
531 if (playpos) This->last_playpos = *playpos;
532 TRACE("playpos = %ld, writepos = %ld (%p, time=%ld)\n", playpos?*playpos:0, writepos?*writepos:0, This, GetTickCount());
533 return DS_OK;
536 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
537 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
539 ICOM_THIS(IDirectSoundBufferImpl,iface);
540 TRACE("(%p,%p), thread is %04lx\n",This,status,GetCurrentThreadId());
542 if (status == NULL) {
543 WARN("invalid parameter: status = NULL\n");
544 return DSERR_INVALIDPARAM;
547 *status = 0;
548 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
549 *status |= DSBSTATUS_PLAYING;
550 if (This->playflags & DSBPLAY_LOOPING)
551 *status |= DSBSTATUS_LOOPING;
554 TRACE("status=%lx\n", *status);
555 return DS_OK;
559 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
560 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten
562 ICOM_THIS(IDirectSoundBufferImpl,iface);
563 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
565 if (wfsize>sizeof(This->wfx))
566 wfsize = sizeof(This->wfx);
567 if (lpwf) { /* NULL is valid */
568 memcpy(lpwf,&(This->wfx),wfsize);
569 if (wfwritten)
570 *wfwritten = wfsize;
571 } else {
572 if (wfwritten)
573 *wfwritten = sizeof(This->wfx);
574 else {
575 WARN("invalid parameter: wfwritten == NULL\n");
576 return DSERR_INVALIDPARAM;
580 return DS_OK;
583 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
584 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
586 HRESULT hres = DS_OK;
587 ICOM_THIS(IDirectSoundBufferImpl,iface);
589 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx) at %ld\n",
590 This,
591 writecursor,
592 writebytes,
593 lplpaudioptr1,
594 audiobytes1,
595 lplpaudioptr2,
596 audiobytes2,
597 flags,
598 GetTickCount()
601 if (flags & DSBLOCK_FROMWRITECURSOR) {
602 DWORD writepos;
603 /* GetCurrentPosition does too much magic to duplicate here */
604 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writepos);
605 if (hres != DS_OK) {
606 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
607 return hres;
609 writecursor += writepos;
611 while (writecursor >= This->buflen)
612 writecursor -= This->buflen;
613 if (flags & DSBLOCK_ENTIREBUFFER)
614 writebytes = This->buflen;
615 if (writebytes > This->buflen)
616 writebytes = This->buflen;
618 assert(audiobytes1!=audiobytes2);
619 assert(lplpaudioptr1!=lplpaudioptr2);
621 EnterCriticalSection(&(This->lock));
623 if ((writebytes == This->buflen) &&
624 ((This->state == STATE_STARTING) ||
625 (This->state == STATE_PLAYING)))
626 /* some games, like Half-Life, try to be clever (not) and
627 * keep one secondary buffer, and mix sounds into it itself,
628 * locking the entire buffer every time... so we can just forget
629 * about tracking the last-written-to-position... */
630 This->probably_valid_to = (DWORD)-1;
631 else
632 This->probably_valid_to = writecursor;
634 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
635 hres = IDsDriverBuffer_Lock(This->hwbuf,
636 lplpaudioptr1, audiobytes1,
637 lplpaudioptr2, audiobytes2,
638 writecursor, writebytes,
640 if (hres != DS_OK) {
641 WARN("IDsDriverBuffer_Lock failed\n");
642 LeaveCriticalSection(&(This->lock));
643 return hres;
645 } else {
646 BOOL remix = FALSE;
647 if (writecursor+writebytes <= This->buflen) {
648 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
649 *audiobytes1 = writebytes;
650 if (lplpaudioptr2)
651 *(LPBYTE*)lplpaudioptr2 = NULL;
652 if (audiobytes2)
653 *audiobytes2 = 0;
654 TRACE("->%ld.0\n",writebytes);
655 } else {
656 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
657 *audiobytes1 = This->buflen-writecursor;
658 if (lplpaudioptr2)
659 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
660 if (audiobytes2)
661 *audiobytes2 = writebytes-(This->buflen-writecursor);
662 TRACE("->%ld.%ld\n",*audiobytes1,audiobytes2?*audiobytes2:0);
664 if (This->state == STATE_PLAYING) {
665 /* if the segment between playpos and buf_mixpos is touched,
666 * we need to cancel some mixing */
667 /* we'll assume that the app always calls GetCurrentPosition before
668 * locking a playing buffer, so that last_playpos is up-to-date */
669 if (This->buf_mixpos >= This->last_playpos) {
670 if (This->buf_mixpos > writecursor &&
671 This->last_playpos < writecursor+writebytes)
672 remix = TRUE;
674 else {
675 if (This->buf_mixpos > writecursor ||
676 This->last_playpos < writecursor+writebytes)
677 remix = TRUE;
679 if (remix) {
680 TRACE("locking prebuffered region, ouch\n");
681 DSOUND_MixCancelAt(This, writecursor);
686 LeaveCriticalSection(&(This->lock));
687 return DS_OK;
690 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
691 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
693 HRESULT hres = DS_OK;
694 ICOM_THIS(IDirectSoundBufferImpl,iface);
695 TRACE("(%p,%ld)\n",This,newpos);
697 /* **** */
698 EnterCriticalSection(&(This->lock));
700 while (newpos >= This->buflen)
701 newpos -= This->buflen;
702 This->buf_mixpos = newpos;
703 if (This->hwbuf) {
704 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
705 if (hres != DS_OK)
706 WARN("IDsDriverBuffer_SetPosition failed\n");
709 LeaveCriticalSection(&(This->lock));
710 /* **** */
712 return hres;
715 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
716 LPDIRECTSOUNDBUFFER8 iface,LONG pan
718 HRESULT hres = DS_OK;
719 ICOM_THIS(IDirectSoundBufferImpl,iface);
720 LONG oldPan;
722 TRACE("(%p,%ld)\n",This,pan);
724 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
725 WARN("invalid parameter: pan = %ld\n", pan);
726 return DSERR_INVALIDPARAM;
729 /* You cannot use both pan and 3D controls */
730 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
731 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
732 WARN("control unavailable\n");
733 return DSERR_CONTROLUNAVAIL;
736 /* **** */
737 EnterCriticalSection(&(This->lock));
739 oldPan = This->volpan.lPan;
740 This->volpan.lPan = pan;
742 if (pan != oldPan) {
743 DSOUND_RecalcVolPan(&(This->volpan));
745 if (This->hwbuf) {
746 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
747 if (hres != DS_OK)
748 WARN("IDsDriverBuffer_SetVolumePan failed\n");
749 } else
750 DSOUND_ForceRemix(This);
753 LeaveCriticalSection(&(This->lock));
754 /* **** */
756 return hres;
759 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
760 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
762 ICOM_THIS(IDirectSoundBufferImpl,iface);
763 TRACE("(%p,%p)\n",This,pan);
765 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
766 WARN("control unavailable\n");
767 return DSERR_CONTROLUNAVAIL;
770 if (pan == NULL) {
771 WARN("invalid parameter: pan = NULL\n");
772 return DSERR_INVALIDPARAM;
775 *pan = This->volpan.lPan;
777 return DS_OK;
780 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
781 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
783 ICOM_THIS(IDirectSoundBufferImpl,iface);
784 DWORD probably_valid_to;
786 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
788 if (!(This->dsound->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
789 HRESULT hres;
790 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
791 if (hres != DS_OK) {
792 WARN("IDsDriverBuffer_Unlock failed\n");
793 return hres;
797 if (p2) probably_valid_to = (((LPBYTE)p2)-This->buffer->memory) + x2;
798 else probably_valid_to = (((LPBYTE)p1)-This->buffer->memory) + x1;
799 while (probably_valid_to >= This->buflen)
800 probably_valid_to -= This->buflen;
801 if ((probably_valid_to == 0) && ((x1+x2) == This->buflen) &&
802 ((This->state == STATE_STARTING) ||
803 (This->state == STATE_PLAYING)))
804 /* see IDirectSoundBufferImpl_Lock */
805 probably_valid_to = (DWORD)-1;
806 This->probably_valid_to = probably_valid_to;
808 return DS_OK;
811 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
812 LPDIRECTSOUNDBUFFER8 iface
814 ICOM_THIS(IDirectSoundBufferImpl,iface);
815 FIXME("(%p):stub\n",This);
816 return DS_OK;
819 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
820 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
822 ICOM_THIS(IDirectSoundBufferImpl,iface);
823 TRACE("(%p,%p)\n",This,freq);
825 if (freq == NULL) {
826 WARN("invalid parameter: freq = NULL\n");
827 return DSERR_INVALIDPARAM;
830 *freq = This->freq;
831 TRACE("-> %ld\n", *freq);
833 return DS_OK;
836 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
837 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
839 ICOM_THIS(IDirectSoundBufferImpl,iface);
840 DWORD u;
842 FIXME("(%p,%lu,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
844 if (pdwResultCodes)
845 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
847 WARN("control unavailable\n");
848 return DSERR_CONTROLUNAVAIL;
851 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
852 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
854 ICOM_THIS(IDirectSoundBufferImpl,iface);
855 DWORD u;
857 FIXME("(%p,%08lu,%lu,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
859 if (pdwResultCodes)
860 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
862 WARN("control unavailable\n");
863 return DSERR_CONTROLUNAVAIL;
866 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
867 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
869 ICOM_THIS(IDirectSoundBufferImpl,iface);
871 FIXME("(%p,%s,%lu,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
873 WARN("control unavailable\n");
874 return DSERR_CONTROLUNAVAIL;
877 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
878 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND8 dsound,LPDSBUFFERDESC dbsd
880 ICOM_THIS(IDirectSoundBufferImpl,iface);
881 FIXME("(%p,%p,%p):stub\n",This,dsound,dbsd);
882 DPRINTF("Re-Init!!!\n");
883 WARN("already initialized\n");
884 return DSERR_ALREADYINITIALIZED;
887 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
888 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
890 ICOM_THIS(IDirectSoundBufferImpl,iface);
891 TRACE("(%p)->(%p)\n",This,caps);
893 if (caps == NULL) {
894 WARN("invalid parameter: caps == NULL\n");
895 return DSERR_INVALIDPARAM;
898 if (caps->dwSize < sizeof(*caps)) {
899 WARN("invalid parameter: caps->dwSize = %ld < %d\n",caps->dwSize, sizeof(*caps));
900 return DSERR_INVALIDPARAM;
903 caps->dwFlags = This->dsbd.dwFlags;
904 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
905 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
907 caps->dwBufferBytes = This->buflen;
909 /* This value represents the speed of the "unlock" command.
910 As unlock is quite fast (it does not do anything), I put
911 4096 ko/s = 4 Mo / s */
912 /* FIXME: hwbuf speed */
913 caps->dwUnlockTransferRate = 4096;
914 caps->dwPlayCpuOverhead = 0;
916 return DS_OK;
919 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(
920 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj
922 ICOM_THIS(IDirectSoundBufferImpl,iface);
924 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
926 if (ppobj == NULL) {
927 WARN("invalid parameter\n");
928 return E_INVALIDARG;
931 *ppobj = NULL; /* assume failure */
933 if ( IsEqualGUID(riid, &IID_IUnknown) ||
934 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
935 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
936 if (!This->dsb)
937 SecondaryBufferImpl_Create(This, &(This->dsb));
938 if (This->dsb) {
939 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)This->dsb);
940 *ppobj = This->dsb;
941 return S_OK;
943 WARN("IID_IDirectSoundBuffer\n");
944 return E_NOINTERFACE;
947 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ||
948 IsEqualGUID( &IID_IDirectSoundNotify8, riid ) ) {
949 if (!This->notify)
950 IDirectSoundNotifyImpl_Create(This, &(This->notify));
951 if (This->notify) {
952 IDirectSoundNotify_AddRef((LPDIRECTSOUNDNOTIFY)This->notify);
953 *ppobj = This->notify;
954 return S_OK;
956 WARN("IID_IDirectSoundNotify\n");
957 return E_NOINTERFACE;
960 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
961 if (!This->ds3db)
962 IDirectSound3DBufferImpl_Create(This, &(This->ds3db));
963 if (This->ds3db) {
964 IDirectSound3DBuffer_AddRef((LPDIRECTSOUND3DBUFFER)This->ds3db);
965 *ppobj = This->ds3db;
966 return S_OK;
968 WARN("IID_IDirectSound3DBuffer\n");
969 return E_NOINTERFACE;
972 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
973 ERR("app requested IDirectSound3DListener on secondary buffer\n");
974 return E_NOINTERFACE;
977 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
978 /* only supported on hardware 3D secondary buffers */
979 if (!(This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) &&
980 (This->dsbd.dwFlags & DSBCAPS_CTRL3D) &&
981 (This->hwbuf != NULL) ) {
982 if (!This->iks)
983 IKsBufferPropertySetImpl_Create(This, &(This->iks));
984 if (This->iks) {
985 IKsPropertySet_AddRef((LPKSPROPERTYSET)*ppobj);
986 *ppobj = This->iks;
987 return S_OK;
990 WARN("IID_IKsPropertySet\n");
991 return E_NOINTERFACE;
994 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
996 return E_NOINTERFACE;
999 static ICOM_VTABLE(IDirectSoundBuffer8) dsbvt =
1001 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1002 IDirectSoundBufferImpl_QueryInterface,
1003 IDirectSoundBufferImpl_AddRef,
1004 IDirectSoundBufferImpl_Release,
1005 IDirectSoundBufferImpl_GetCaps,
1006 IDirectSoundBufferImpl_GetCurrentPosition,
1007 IDirectSoundBufferImpl_GetFormat,
1008 IDirectSoundBufferImpl_GetVolume,
1009 IDirectSoundBufferImpl_GetPan,
1010 IDirectSoundBufferImpl_GetFrequency,
1011 IDirectSoundBufferImpl_GetStatus,
1012 IDirectSoundBufferImpl_Initialize,
1013 IDirectSoundBufferImpl_Lock,
1014 IDirectSoundBufferImpl_Play,
1015 IDirectSoundBufferImpl_SetCurrentPosition,
1016 IDirectSoundBufferImpl_SetFormat,
1017 IDirectSoundBufferImpl_SetVolume,
1018 IDirectSoundBufferImpl_SetPan,
1019 IDirectSoundBufferImpl_SetFrequency,
1020 IDirectSoundBufferImpl_Stop,
1021 IDirectSoundBufferImpl_Unlock,
1022 IDirectSoundBufferImpl_Restore,
1023 IDirectSoundBufferImpl_SetFX,
1024 IDirectSoundBufferImpl_AcquireResources,
1025 IDirectSoundBufferImpl_GetObjectInPath
1028 HRESULT WINAPI IDirectSoundBufferImpl_Create(
1029 IDirectSoundImpl *ds,
1030 IDirectSoundBufferImpl **pdsb,
1031 LPDSBUFFERDESC dsbd)
1033 IDirectSoundBufferImpl *dsb;
1034 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
1035 HRESULT err = DS_OK;
1036 DWORD capf = 0;
1037 int use_hw;
1038 TRACE("(%p,%p,%p)\n",ds,pdsb,dsbd);
1040 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
1041 WARN("invalid parameter: dsbd->dwBufferBytes = %ld\n", dsbd->dwBufferBytes);
1042 *pdsb = NULL;
1043 return DSERR_INVALIDPARAM; /* FIXME: which error? */
1046 dsb = (IDirectSoundBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
1048 if (dsb == 0) {
1049 WARN("out of memory\n");
1050 *pdsb = NULL;
1051 return DSERR_OUTOFMEMORY;
1053 dsb->ref = 0;
1054 dsb->dsb = 0;
1055 dsb->dsound = ds;
1056 dsb->lpVtbl = &dsbvt;
1057 dsb->iks = NULL;
1059 memcpy(&dsb->dsbd, dsbd, sizeof(*dsbd));
1060 if (wfex)
1061 memcpy(&dsb->wfx, wfex, sizeof(dsb->wfx));
1063 TRACE("Created buffer at %p\n", dsb);
1065 dsb->buflen = dsbd->dwBufferBytes;
1066 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
1068 dsb->notify = NULL;
1069 dsb->notifies = NULL;
1070 dsb->nrofnotifies = 0;
1071 dsb->hwnotify = 0;
1073 /* Check necessary hardware mixing capabilities */
1074 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
1075 else capf |= DSCAPS_SECONDARYMONO;
1076 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
1077 else capf |= DSCAPS_SECONDARY8BIT;
1079 use_hw = (ds->drvcaps.dwFlags & capf) == capf;
1080 TRACE("use_hw = 0x%08x, capf = 0x%08lx, ds->drvcaps.dwFlags = 0x%08lx\n", use_hw, capf, ds->drvcaps.dwFlags);
1082 /* FIXME: check hardware sample rate mixing capabilities */
1083 /* FIXME: check app hints for software/hardware buffer (STATIC, LOCHARDWARE, etc) */
1084 /* FIXME: check whether any hardware buffers are left */
1085 /* FIXME: handle DSDHEAP_CREATEHEAP for hardware buffers */
1087 /* Allocate system memory if applicable */
1088 if ((ds->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) || !use_hw) {
1089 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
1090 if (dsb->buffer == NULL) {
1091 WARN("out of memory\n");
1092 HeapFree(GetProcessHeap(),0,dsb);
1093 *pdsb = NULL;
1094 return DSERR_OUTOFMEMORY;
1097 dsb->buffer->memory = (LPBYTE)HeapAlloc(GetProcessHeap(),0,dsb->buflen);
1098 if (dsb->buffer->memory == NULL) {
1099 WARN("out of memory\n");
1100 HeapFree(GetProcessHeap(),0,dsb->buffer);
1101 HeapFree(GetProcessHeap(),0,dsb);
1102 *pdsb = NULL;
1103 return DSERR_OUTOFMEMORY;
1107 /* Allocate the hardware buffer */
1108 if (use_hw) {
1109 err = IDsDriver_CreateSoundBuffer(ds->driver,wfex,dsbd->dwFlags,0,
1110 &(dsb->buflen),&(dsb->buffer->memory),
1111 (LPVOID*)&(dsb->hwbuf));
1112 if (err != DS_OK) {
1113 WARN("IDsDriver_CreateSoundBuffer failed\n");
1114 if (dsb->buffer->memory)
1115 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1116 if (dsb->buffer)
1117 HeapFree(GetProcessHeap(),0,dsb->buffer);
1118 HeapFree(GetProcessHeap(),0,dsb);
1119 *pdsb = NULL;
1120 return err;
1124 /* calculate fragment size and write lead */
1125 DSOUND_RecalcFormat(dsb);
1127 /* It's not necessary to initialize values to zero since */
1128 /* we allocated this structure with HEAP_ZERO_MEMORY... */
1129 dsb->playpos = 0;
1130 dsb->buf_mixpos = 0;
1131 dsb->state = STATE_STOPPED;
1133 dsb->freqAdjust = (dsb->freq << DSOUND_FREQSHIFT) /
1134 ds->wfx.nSamplesPerSec;
1135 dsb->nAvgBytesPerSec = dsb->freq *
1136 dsbd->lpwfxFormat->nBlockAlign;
1138 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
1139 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
1140 dsb->ds3db_ds3db.vPosition.x = 0.0;
1141 dsb->ds3db_ds3db.vPosition.y = 0.0;
1142 dsb->ds3db_ds3db.vPosition.z = 0.0;
1143 dsb->ds3db_ds3db.vVelocity.x = 0.0;
1144 dsb->ds3db_ds3db.vVelocity.y = 0.0;
1145 dsb->ds3db_ds3db.vVelocity.z = 0.0;
1146 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
1147 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
1148 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
1149 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
1150 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
1151 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
1152 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
1153 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
1154 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
1156 dsb->ds3db_need_recalc = FALSE;
1157 DSOUND_Calc3DBuffer(dsb);
1158 } else
1159 DSOUND_RecalcVolPan(&(dsb->volpan));
1161 InitializeCriticalSection(&(dsb->lock));
1163 /* register buffer */
1164 RtlAcquireResourceExclusive(&(ds->lock), TRUE);
1165 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
1166 IDirectSoundBufferImpl **newbuffers = (IDirectSoundBufferImpl**)HeapReAlloc(GetProcessHeap(),0,ds->buffers,sizeof(IDirectSoundBufferImpl*)*(ds->nrofbuffers+1));
1167 if (newbuffers) {
1168 ds->buffers = newbuffers;
1169 ds->buffers[ds->nrofbuffers] = dsb;
1170 ds->nrofbuffers++;
1171 TRACE("buffer count is now %d\n", ds->nrofbuffers);
1172 } else {
1173 ERR("out of memory for buffer list! Current buffer count is %d\n", ds->nrofbuffers);
1174 if (dsb->buffer->memory)
1175 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
1176 if (dsb->buffer)
1177 HeapFree(GetProcessHeap(),0,dsb->buffer);
1178 DeleteCriticalSection(&(dsb->lock));
1179 RtlReleaseResource(&(ds->lock));
1180 HeapFree(GetProcessHeap(),0,dsb);
1181 *pdsb = NULL;
1182 return DSERR_OUTOFMEMORY;
1185 RtlReleaseResource(&(ds->lock));
1186 IDirectSound8_AddRef((LPDIRECTSOUND8)ds);
1187 *pdsb = dsb;
1188 return S_OK;
1191 /*******************************************************************************
1192 * SecondaryBuffer
1195 static HRESULT WINAPI SecondaryBufferImpl_QueryInterface(
1196 LPDIRECTSOUNDBUFFER8 iface,REFIID riid,LPVOID *ppobj)
1198 ICOM_THIS(SecondaryBufferImpl,iface);
1199 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1201 return IDirectSoundBufferImpl_QueryInterface((LPDIRECTSOUNDBUFFER8)This->dsb,riid,ppobj);
1204 static DWORD WINAPI SecondaryBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
1206 ICOM_THIS(IDirectSoundBufferImpl,iface);
1207 DWORD ref;
1208 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1210 ref = InterlockedIncrement(&(This->ref));
1211 if (!ref) {
1212 FIXME("thread-safety alert! AddRef-ing with a zero refcount!\n");
1214 return ref;
1217 static DWORD WINAPI SecondaryBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
1219 ICOM_THIS(IDirectSoundBufferImpl,iface);
1220 DWORD ref;
1221 TRACE("(%p) ref was %ld, thread is %04lx\n",This, This->ref, GetCurrentThreadId());
1223 ref = InterlockedDecrement(&(This->ref));
1224 if (!ref) {
1225 This->dsb->dsb = NULL;
1226 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER8)This->dsb);
1227 HeapFree(GetProcessHeap(),0,This);
1228 TRACE("(%p) released\n",This);
1230 return ref;
1233 static HRESULT WINAPI SecondaryBufferImpl_GetCaps(
1234 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps)
1236 ICOM_THIS(SecondaryBufferImpl,iface);
1237 TRACE("(%p)->(%p)\n",This,caps);
1239 return IDirectSoundBufferImpl_GetCaps((LPDIRECTSOUNDBUFFER8)This->dsb,caps);
1242 static HRESULT WINAPI SecondaryBufferImpl_GetCurrentPosition(
1243 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos)
1245 ICOM_THIS(SecondaryBufferImpl,iface);
1246 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
1248 return IDirectSoundBufferImpl_GetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,playpos,writepos);
1251 static HRESULT WINAPI SecondaryBufferImpl_GetFormat(
1252 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX lpwf,DWORD wfsize,LPDWORD wfwritten)
1254 ICOM_THIS(SecondaryBufferImpl,iface);
1255 TRACE("(%p,%p,%ld,%p)\n",This,lpwf,wfsize,wfwritten);
1257 return IDirectSoundBufferImpl_GetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,lpwf,wfsize,wfwritten);
1260 static HRESULT WINAPI SecondaryBufferImpl_GetVolume(
1261 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol)
1263 ICOM_THIS(SecondaryBufferImpl,iface);
1264 TRACE("(%p,%p)\n",This,vol);
1266 return IDirectSoundBufferImpl_GetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1269 static HRESULT WINAPI SecondaryBufferImpl_GetPan(
1270 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan)
1272 ICOM_THIS(SecondaryBufferImpl,iface);
1273 TRACE("(%p,%p)\n",This,pan);
1275 return IDirectSoundBufferImpl_GetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1278 static HRESULT WINAPI SecondaryBufferImpl_GetFrequency(
1279 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq)
1281 ICOM_THIS(SecondaryBufferImpl,iface);
1282 TRACE("(%p,%p)\n",This,freq);
1284 return IDirectSoundBufferImpl_GetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1287 static HRESULT WINAPI SecondaryBufferImpl_GetStatus(
1288 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status)
1290 ICOM_THIS(SecondaryBufferImpl,iface);
1291 TRACE("(%p,%p)\n",This,status);
1293 return IDirectSoundBufferImpl_GetStatus((LPDIRECTSOUNDBUFFER8)This->dsb,status);
1296 static HRESULT WINAPI SecondaryBufferImpl_Initialize(
1297 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND8 dsound,LPDSBUFFERDESC dbsd)
1299 ICOM_THIS(SecondaryBufferImpl,iface);
1300 TRACE("(%p,%p,%p)\n",This,dsound,dbsd);
1302 return IDirectSoundBufferImpl_Initialize((LPDIRECTSOUNDBUFFER8)This->dsb,dsound,dbsd);
1305 static HRESULT WINAPI SecondaryBufferImpl_Lock(
1306 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID lplpaudioptr1,LPDWORD audiobytes1,LPVOID lplpaudioptr2,LPDWORD audiobytes2,DWORD flags)
1308 ICOM_THIS(SecondaryBufferImpl,iface);
1309 TRACE("(%p,%ld,%ld,%p,%p,%p,%p,0x%08lx)\n",
1310 This,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,flags);
1312 return IDirectSoundBufferImpl_Lock((LPDIRECTSOUNDBUFFER8)This->dsb,writecursor,writebytes,lplpaudioptr1,audiobytes1,lplpaudioptr2,audiobytes2,flags);
1315 static HRESULT WINAPI SecondaryBufferImpl_Play(
1316 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags)
1318 ICOM_THIS(SecondaryBufferImpl,iface);
1319 TRACE("(%p,%08lx,%08lx,%08lx)\n",This,reserved1,reserved2,flags);
1321 return IDirectSoundBufferImpl_Play((LPDIRECTSOUNDBUFFER8)This->dsb,reserved1,reserved2,flags);
1324 static HRESULT WINAPI SecondaryBufferImpl_SetCurrentPosition(
1325 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos)
1327 ICOM_THIS(SecondaryBufferImpl,iface);
1328 TRACE("(%p,%ld)\n",This,newpos);
1330 return IDirectSoundBufferImpl_SetCurrentPosition((LPDIRECTSOUNDBUFFER8)This->dsb,newpos);
1333 static HRESULT WINAPI SecondaryBufferImpl_SetFormat(
1334 LPDIRECTSOUNDBUFFER8 iface,LPWAVEFORMATEX wfex)
1336 ICOM_THIS(SecondaryBufferImpl,iface);
1337 TRACE("(%p,%p)\n",This,wfex);
1339 return IDirectSoundBufferImpl_SetFormat((LPDIRECTSOUNDBUFFER8)This->dsb,wfex);
1342 static HRESULT WINAPI SecondaryBufferImpl_SetVolume(
1343 LPDIRECTSOUNDBUFFER8 iface,LONG vol)
1345 ICOM_THIS(SecondaryBufferImpl,iface);
1346 TRACE("(%p,%ld)\n",This,vol);
1348 return IDirectSoundBufferImpl_SetVolume((LPDIRECTSOUNDBUFFER8)This->dsb,vol);
1351 static HRESULT WINAPI SecondaryBufferImpl_SetPan(
1352 LPDIRECTSOUNDBUFFER8 iface,LONG pan)
1354 ICOM_THIS(SecondaryBufferImpl,iface);
1355 TRACE("(%p,%ld)\n",This,pan);
1357 return IDirectSoundBufferImpl_SetPan((LPDIRECTSOUNDBUFFER8)This->dsb,pan);
1360 static HRESULT WINAPI SecondaryBufferImpl_SetFrequency(
1361 LPDIRECTSOUNDBUFFER8 iface,DWORD freq)
1363 ICOM_THIS(SecondaryBufferImpl,iface);
1364 TRACE("(%p,%ld)\n",This,freq);
1366 return IDirectSoundBufferImpl_SetFrequency((LPDIRECTSOUNDBUFFER8)This->dsb,freq);
1369 static HRESULT WINAPI SecondaryBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
1371 ICOM_THIS(SecondaryBufferImpl,iface);
1372 TRACE("(%p)\n",This);
1374 return IDirectSoundBufferImpl_Stop((LPDIRECTSOUNDBUFFER8)This->dsb);
1377 static HRESULT WINAPI SecondaryBufferImpl_Unlock(
1378 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2)
1380 ICOM_THIS(SecondaryBufferImpl,iface);
1381 TRACE("(%p,%p,%ld,%p,%ld)\n", This,p1,x1,p2,x2);
1383 return IDirectSoundBufferImpl_Unlock((LPDIRECTSOUNDBUFFER8)This->dsb,p1,x1,p2,x2);
1386 static HRESULT WINAPI SecondaryBufferImpl_Restore(
1387 LPDIRECTSOUNDBUFFER8 iface)
1389 ICOM_THIS(SecondaryBufferImpl,iface);
1390 TRACE("(%p)\n",This);
1392 return IDirectSoundBufferImpl_Restore((LPDIRECTSOUNDBUFFER8)This->dsb);
1395 static HRESULT WINAPI SecondaryBufferImpl_SetFX(
1396 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes)
1398 ICOM_THIS(SecondaryBufferImpl,iface);
1399 TRACE("(%p,%lu,%p,%p)\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1401 return IDirectSoundBufferImpl_SetFX((LPDIRECTSOUNDBUFFER8)This->dsb,dwEffectsCount,pDSFXDesc,pdwResultCodes);
1404 static HRESULT WINAPI SecondaryBufferImpl_AcquireResources(
1405 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes)
1407 ICOM_THIS(SecondaryBufferImpl,iface);
1408 TRACE("(%p,%08lu,%lu,%p)\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
1410 return IDirectSoundBufferImpl_AcquireResources((LPDIRECTSOUNDBUFFER8)This->dsb,dwFlags,dwEffectsCount,pdwResultCodes);
1413 static HRESULT WINAPI SecondaryBufferImpl_GetObjectInPath(
1414 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject)
1416 ICOM_THIS(SecondaryBufferImpl,iface);
1417 TRACE("(%p,%s,%lu,%s,%p)\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
1419 return IDirectSoundBufferImpl_GetObjectInPath((LPDIRECTSOUNDBUFFER8)This->dsb,rguidObject,dwIndex,rguidInterface,ppObject);
1422 static ICOM_VTABLE(IDirectSoundBuffer8) sbvt =
1424 ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
1425 SecondaryBufferImpl_QueryInterface,
1426 SecondaryBufferImpl_AddRef,
1427 SecondaryBufferImpl_Release,
1428 SecondaryBufferImpl_GetCaps,
1429 SecondaryBufferImpl_GetCurrentPosition,
1430 SecondaryBufferImpl_GetFormat,
1431 SecondaryBufferImpl_GetVolume,
1432 SecondaryBufferImpl_GetPan,
1433 SecondaryBufferImpl_GetFrequency,
1434 SecondaryBufferImpl_GetStatus,
1435 SecondaryBufferImpl_Initialize,
1436 SecondaryBufferImpl_Lock,
1437 SecondaryBufferImpl_Play,
1438 SecondaryBufferImpl_SetCurrentPosition,
1439 SecondaryBufferImpl_SetFormat,
1440 SecondaryBufferImpl_SetVolume,
1441 SecondaryBufferImpl_SetPan,
1442 SecondaryBufferImpl_SetFrequency,
1443 SecondaryBufferImpl_Stop,
1444 SecondaryBufferImpl_Unlock,
1445 SecondaryBufferImpl_Restore,
1446 SecondaryBufferImpl_SetFX,
1447 SecondaryBufferImpl_AcquireResources,
1448 SecondaryBufferImpl_GetObjectInPath
1451 HRESULT WINAPI SecondaryBufferImpl_Create(
1452 IDirectSoundBufferImpl *dsb,
1453 SecondaryBufferImpl **psb)
1455 SecondaryBufferImpl *sb;
1456 TRACE("(%p,%p)\n",dsb,psb);
1458 sb = (SecondaryBufferImpl*)HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*sb));
1460 if (sb == 0) {
1461 WARN("out of memory\n");
1462 *psb = NULL;
1463 return DSERR_OUTOFMEMORY;
1465 sb->ref = 0;
1466 sb->dsb = dsb;
1467 sb->lpVtbl = &sbvt;
1469 IDirectSoundBuffer8_AddRef((LPDIRECTSOUNDBUFFER8)dsb);
1470 *psb = sb;
1471 return S_OK;