[DSOUND]
[reactos.git] / reactos / dll / directx / wine / dsound / buffer.c
1 /* DirectSound
2 *
3 * Copyright 1998 Marcus Meissner
4 * Copyright 1998 Rob Riggs
5 * Copyright 2000-2002 TransGaming Technologies, Inc.
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include <stdarg.h>
23
24 #define WIN32_NO_STATUS
25 #define _INC_WINDOWS
26 #define COM_NO_WINDOWS_H
27
28 #define NONAMELESSSTRUCT
29 #define NONAMELESSUNION
30 #include <windef.h>
31 #include <winbase.h>
32 //#include "winuser.h"
33 #include <mmsystem.h>
34 #include <winternl.h>
35 #include <vfwmsgs.h>
36 #include <wine/debug.h>
37 #include <dsound.h>
38 #include "dsound_private.h"
39 //#include <dsconf.h>
40
41 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
42
43 /*******************************************************************************
44 * IDirectSoundNotify
45 */
46
47 static inline struct IDirectSoundBufferImpl *impl_from_IDirectSoundNotify(IDirectSoundNotify *iface)
48 {
49 return CONTAINING_RECORD(iface, struct IDirectSoundBufferImpl, IDirectSoundNotify_iface);
50 }
51
52 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(IDirectSoundNotify *iface, REFIID riid,
53 void **ppobj)
54 {
55 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
56
57 TRACE("(%p,%s,%p)\n", This, debugstr_guid(riid), ppobj);
58
59 return IDirectSoundBuffer8_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
60 }
61
62 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(IDirectSoundNotify *iface)
63 {
64 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
65 ULONG ref = InterlockedIncrement(&This->refn);
66
67 TRACE("(%p) ref was %d\n", This, ref - 1);
68
69 if(ref == 1)
70 InterlockedIncrement(&This->numIfaces);
71
72 return ref;
73 }
74
75 static ULONG WINAPI IDirectSoundNotifyImpl_Release(IDirectSoundNotify *iface)
76 {
77 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
78 ULONG ref = InterlockedDecrement(&This->refn);
79
80 TRACE("(%p) ref was %d\n", This, ref + 1);
81
82 if (!ref && !InterlockedDecrement(&This->numIfaces))
83 secondarybuffer_destroy(This);
84
85 return ref;
86 }
87
88 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(IDirectSoundNotify *iface,
89 DWORD howmuch, const DSBPOSITIONNOTIFY *notify)
90 {
91 IDirectSoundBufferImpl *This = impl_from_IDirectSoundNotify(iface);
92
93 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
94
95 if (howmuch > 0 && notify == NULL) {
96 WARN("invalid parameter: notify == NULL\n");
97 return DSERR_INVALIDPARAM;
98 }
99
100 if (TRACE_ON(dsound)) {
101 unsigned int i;
102 for (i=0;i<howmuch;i++)
103 TRACE("notify at %d to %p\n",
104 notify[i].dwOffset,notify[i].hEventNotify);
105 }
106
107 if (howmuch > 0) {
108 /* Make an internal copy of the caller-supplied array.
109 * Replace the existing copy if one is already present. */
110 HeapFree(GetProcessHeap(), 0, This->notifies);
111 This->notifies = HeapAlloc(GetProcessHeap(), 0,
112 howmuch * sizeof(DSBPOSITIONNOTIFY));
113
114 if (This->notifies == NULL) {
115 WARN("out of memory\n");
116 return DSERR_OUTOFMEMORY;
117 }
118 CopyMemory(This->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
119 This->nrofnotifies = howmuch;
120 } else {
121 HeapFree(GetProcessHeap(), 0, This->notifies);
122 This->notifies = NULL;
123 This->nrofnotifies = 0;
124 }
125
126 return S_OK;
127 }
128
129 static const IDirectSoundNotifyVtbl dsnvt =
130 {
131 IDirectSoundNotifyImpl_QueryInterface,
132 IDirectSoundNotifyImpl_AddRef,
133 IDirectSoundNotifyImpl_Release,
134 IDirectSoundNotifyImpl_SetNotificationPositions,
135 };
136
137 /*******************************************************************************
138 * IDirectSoundBuffer
139 */
140
141 static inline IDirectSoundBufferImpl *impl_from_IDirectSoundBuffer8(IDirectSoundBuffer8 *iface)
142 {
143 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IDirectSoundBuffer8_iface);
144 }
145
146 static inline BOOL is_primary_buffer(IDirectSoundBufferImpl *This)
147 {
148 return (This->dsbd.dwFlags & DSBCAPS_PRIMARYBUFFER) != 0;
149 }
150
151 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(IDirectSoundBuffer8 *iface,
152 LPCWAVEFORMATEX wfex)
153 {
154 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
155
156 TRACE("(%p,%p)\n", iface, wfex);
157
158 if (is_primary_buffer(This))
159 return primarybuffer_SetFormat(This->device, wfex);
160 else {
161 WARN("not available for secondary buffers.\n");
162 return DSERR_INVALIDCALL;
163 }
164 }
165
166 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(IDirectSoundBuffer8 *iface, LONG vol)
167 {
168 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
169 LONG oldVol;
170
171 HRESULT hres = DS_OK;
172
173 TRACE("(%p,%d)\n",This,vol);
174
175 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
176 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
177 return DSERR_CONTROLUNAVAIL;
178 }
179
180 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
181 WARN("invalid parameter: vol = %d\n", vol);
182 return DSERR_INVALIDPARAM;
183 }
184
185 /* **** */
186 RtlAcquireResourceExclusive(&This->lock, TRUE);
187
188 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
189 oldVol = This->ds3db_lVolume;
190 This->ds3db_lVolume = vol;
191 if (vol != oldVol)
192 /* recalc 3d volume, which in turn recalcs the pans */
193 DSOUND_Calc3DBuffer(This);
194 } else {
195 oldVol = This->volpan.lVolume;
196 This->volpan.lVolume = vol;
197 if (vol != oldVol)
198 DSOUND_RecalcVolPan(&(This->volpan));
199 }
200
201 RtlReleaseResource(&This->lock);
202 /* **** */
203
204 return hres;
205 }
206
207 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(IDirectSoundBuffer8 *iface, LONG *vol)
208 {
209 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
210
211 TRACE("(%p,%p)\n",This,vol);
212
213 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
214 WARN("control unavailable\n");
215 return DSERR_CONTROLUNAVAIL;
216 }
217
218 if (vol == NULL) {
219 WARN("invalid parameter: vol == NULL\n");
220 return DSERR_INVALIDPARAM;
221 }
222
223 *vol = This->volpan.lVolume;
224
225 return DS_OK;
226 }
227
228 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(IDirectSoundBuffer8 *iface, DWORD freq)
229 {
230 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
231 DWORD oldFreq;
232
233 TRACE("(%p,%d)\n",This,freq);
234
235 if (is_primary_buffer(This)) {
236 WARN("not available for primary buffers.\n");
237 return DSERR_CONTROLUNAVAIL;
238 }
239
240 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
241 WARN("control unavailable\n");
242 return DSERR_CONTROLUNAVAIL;
243 }
244
245 if (freq == DSBFREQUENCY_ORIGINAL)
246 freq = This->pwfx->nSamplesPerSec;
247
248 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
249 WARN("invalid parameter: freq = %d\n", freq);
250 return DSERR_INVALIDPARAM;
251 }
252
253 /* **** */
254 RtlAcquireResourceExclusive(&This->lock, TRUE);
255
256 oldFreq = This->freq;
257 This->freq = freq;
258 if (freq != oldFreq) {
259 This->freqAdjust = This->freq / (float)This->device->pwfx->nSamplesPerSec;
260 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
261 DSOUND_RecalcFormat(This);
262 }
263
264 RtlReleaseResource(&This->lock);
265 /* **** */
266
267 return DS_OK;
268 }
269
270 static HRESULT WINAPI IDirectSoundBufferImpl_Play(IDirectSoundBuffer8 *iface, DWORD reserved1,
271 DWORD reserved2, DWORD flags)
272 {
273 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
274 HRESULT hres = DS_OK;
275
276 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
277
278 /* **** */
279 RtlAcquireResourceExclusive(&This->lock, TRUE);
280
281 This->playflags = flags;
282 if (This->state == STATE_STOPPED) {
283 This->leadin = TRUE;
284 This->state = STATE_STARTING;
285 } else if (This->state == STATE_STOPPING)
286 This->state = STATE_PLAYING;
287
288 RtlReleaseResource(&This->lock);
289 /* **** */
290
291 return hres;
292 }
293
294 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(IDirectSoundBuffer8 *iface)
295 {
296 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
297 HRESULT hres = DS_OK;
298
299 TRACE("(%p)\n",This);
300
301 /* **** */
302 RtlAcquireResourceExclusive(&This->lock, TRUE);
303
304 if (This->state == STATE_PLAYING)
305 This->state = STATE_STOPPING;
306 else if (This->state == STATE_STARTING)
307 {
308 This->state = STATE_STOPPED;
309 DSOUND_CheckEvent(This, 0, 0);
310 }
311
312 RtlReleaseResource(&This->lock);
313 /* **** */
314
315 return hres;
316 }
317
318 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(IDirectSoundBuffer8 *iface)
319 {
320 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
321 ULONG ref = InterlockedIncrement(&This->ref);
322
323 TRACE("(%p) ref was %d\n", This, ref - 1);
324
325 if(ref == 1)
326 InterlockedIncrement(&This->numIfaces);
327
328 return ref;
329 }
330
331 static ULONG WINAPI IDirectSoundBufferImpl_Release(IDirectSoundBuffer8 *iface)
332 {
333 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
334 ULONG ref;
335
336 if (is_primary_buffer(This)){
337 ref = capped_refcount_dec(&This->ref);
338 if(!ref)
339 capped_refcount_dec(&This->numIfaces);
340 TRACE("(%p) ref is now: %d\n", This, ref);
341 return ref;
342 }
343
344 ref = InterlockedDecrement(&This->ref);
345 if (!ref && !InterlockedDecrement(&This->numIfaces))
346 secondarybuffer_destroy(This);
347
348 TRACE("(%p) ref is now %d\n", This, ref);
349
350 return ref;
351 }
352
353 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(IDirectSoundBuffer8 *iface,
354 DWORD *playpos, DWORD *writepos)
355 {
356 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
357 DWORD pos;
358
359 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
360
361 RtlAcquireResourceShared(&This->lock, TRUE);
362
363 pos = This->sec_mixpos;
364
365 /* sanity */
366 if (pos >= This->buflen){
367 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
368 pos %= This->buflen;
369 }
370
371 if (playpos)
372 *playpos = pos;
373 if (writepos)
374 *writepos = pos;
375
376 if (writepos && This->state != STATE_STOPPED) {
377 /* apply the documented 10ms lead to writepos */
378 *writepos += This->writelead;
379 *writepos %= This->buflen;
380 }
381
382 RtlReleaseResource(&This->lock);
383
384 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
385 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
386
387 return DS_OK;
388 }
389
390 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(IDirectSoundBuffer8 *iface, DWORD *status)
391 {
392 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
393
394 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
395
396 if (status == NULL) {
397 WARN("invalid parameter: status = NULL\n");
398 return DSERR_INVALIDPARAM;
399 }
400
401 *status = 0;
402 RtlAcquireResourceShared(&This->lock, TRUE);
403 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
404 *status |= DSBSTATUS_PLAYING;
405 if (This->playflags & DSBPLAY_LOOPING)
406 *status |= DSBSTATUS_LOOPING;
407 }
408 RtlReleaseResource(&This->lock);
409
410 TRACE("status=%x\n", *status);
411 return DS_OK;
412 }
413
414
415 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(IDirectSoundBuffer8 *iface,
416 LPWAVEFORMATEX lpwf, DWORD wfsize, DWORD *wfwritten)
417 {
418 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
419 DWORD size;
420
421 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
422
423 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
424
425 if (lpwf) { /* NULL is valid */
426 if (wfsize >= size) {
427 CopyMemory(lpwf,This->pwfx,size);
428 if (wfwritten)
429 *wfwritten = size;
430 } else {
431 WARN("invalid parameter: wfsize too small\n");
432 CopyMemory(lpwf,This->pwfx,wfsize);
433 if (wfwritten)
434 *wfwritten = wfsize;
435 return DSERR_INVALIDPARAM;
436 }
437 } else {
438 if (wfwritten)
439 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
440 else {
441 WARN("invalid parameter: wfwritten == NULL\n");
442 return DSERR_INVALIDPARAM;
443 }
444 }
445
446 return DS_OK;
447 }
448
449 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(IDirectSoundBuffer8 *iface, DWORD writecursor,
450 DWORD writebytes, void **lplpaudioptr1, DWORD *audiobytes1, void **lplpaudioptr2,
451 DWORD *audiobytes2, DWORD flags)
452 {
453 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
454 HRESULT hres = DS_OK;
455
456 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n", This, writecursor, writebytes, lplpaudioptr1,
457 audiobytes1, lplpaudioptr2, audiobytes2, flags, GetTickCount());
458
459 if (!audiobytes1)
460 return DSERR_INVALIDPARAM;
461
462 /* when this flag is set, writecursor is meaningless and must be calculated */
463 if (flags & DSBLOCK_FROMWRITECURSOR) {
464 /* GetCurrentPosition does too much magic to duplicate here */
465 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
466 if (hres != DS_OK) {
467 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
468 return hres;
469 }
470 }
471
472 /* when this flag is set, writebytes is meaningless and must be set */
473 if (flags & DSBLOCK_ENTIREBUFFER)
474 writebytes = This->buflen;
475
476 if (writecursor >= This->buflen) {
477 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
478 writecursor, This->buflen);
479 return DSERR_INVALIDPARAM;
480 }
481
482 if (writebytes > This->buflen) {
483 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
484 writebytes, This->buflen);
485 return DSERR_INVALIDPARAM;
486 }
487
488 /* **** */
489 RtlAcquireResourceShared(&This->lock, TRUE);
490
491 if (writecursor+writebytes <= This->buflen) {
492 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
493 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
494 WARN("Overwriting mixing position, case 1\n");
495 *audiobytes1 = writebytes;
496 if (lplpaudioptr2)
497 *(LPBYTE*)lplpaudioptr2 = NULL;
498 if (audiobytes2)
499 *audiobytes2 = 0;
500 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
501 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
502 TRACE("->%d.0\n",writebytes);
503 } else {
504 DWORD remainder = writebytes + writecursor - This->buflen;
505 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
506 *audiobytes1 = This->buflen-writecursor;
507 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
508 WARN("Overwriting mixing position, case 2\n");
509 if (lplpaudioptr2)
510 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
511 if (audiobytes2)
512 *audiobytes2 = writebytes-(This->buflen-writecursor);
513 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
514 WARN("Overwriting mixing position, case 3\n");
515 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
516 }
517
518 RtlReleaseResource(&This->lock);
519 /* **** */
520
521 return DS_OK;
522 }
523
524 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(IDirectSoundBuffer8 *iface,
525 DWORD newpos)
526 {
527 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
528 HRESULT hres = DS_OK;
529
530 TRACE("(%p,%d)\n",This,newpos);
531
532 /* **** */
533 RtlAcquireResourceExclusive(&This->lock, TRUE);
534
535 /* start mixing from this new location instead */
536 newpos %= This->buflen;
537 newpos -= newpos%This->pwfx->nBlockAlign;
538 This->sec_mixpos = newpos;
539
540 /* at this point, do not attempt to reset buffers, mess with primary mix position,
541 or anything like that to reduce latency. The data already prebuffered cannot be changed */
542
543 RtlReleaseResource(&This->lock);
544 /* **** */
545
546 return hres;
547 }
548
549 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(IDirectSoundBuffer8 *iface, LONG pan)
550 {
551 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
552 HRESULT hres = DS_OK;
553
554 TRACE("(%p,%d)\n",This,pan);
555
556 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
557 WARN("invalid parameter: pan = %d\n", pan);
558 return DSERR_INVALIDPARAM;
559 }
560
561 /* You cannot use both pan and 3D controls */
562 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
563 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
564 WARN("control unavailable\n");
565 return DSERR_CONTROLUNAVAIL;
566 }
567
568 /* **** */
569 RtlAcquireResourceExclusive(&This->lock, TRUE);
570
571 if (This->volpan.lPan != pan) {
572 This->volpan.lPan = pan;
573 DSOUND_RecalcVolPan(&(This->volpan));
574 }
575
576 RtlReleaseResource(&This->lock);
577 /* **** */
578
579 return hres;
580 }
581
582 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(IDirectSoundBuffer8 *iface, LONG *pan)
583 {
584 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
585
586 TRACE("(%p,%p)\n",This,pan);
587
588 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
589 WARN("control unavailable\n");
590 return DSERR_CONTROLUNAVAIL;
591 }
592
593 if (pan == NULL) {
594 WARN("invalid parameter: pan = NULL\n");
595 return DSERR_INVALIDPARAM;
596 }
597
598 *pan = This->volpan.lPan;
599
600 return DS_OK;
601 }
602
603 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(IDirectSoundBuffer8 *iface, void *p1, DWORD x1,
604 void *p2, DWORD x2)
605 {
606 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface), *iter;
607 HRESULT hres = DS_OK;
608
609 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
610
611 if (!p2)
612 x2 = 0;
613
614 if((p1 && ((BYTE*)p1 < This->buffer->memory || (BYTE*)p1 >= This->buffer->memory + This->buflen)) ||
615 (p2 && ((BYTE*)p2 < This->buffer->memory || (BYTE*)p2 >= This->buffer->memory + This->buflen)))
616 return DSERR_INVALIDPARAM;
617
618 if (x1 || x2)
619 {
620 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
621 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
622 {
623 RtlAcquireResourceShared(&iter->lock, TRUE);
624 if (x1)
625 {
626 if(x1 + (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory > iter->buflen)
627 hres = DSERR_INVALIDPARAM;
628 }
629 RtlReleaseResource(&iter->lock);
630 }
631 RtlReleaseResource(&This->device->buffer_list_lock);
632 }
633
634 return hres;
635 }
636
637 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(IDirectSoundBuffer8 *iface)
638 {
639 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
640
641 FIXME("(%p):stub\n",This);
642 return DS_OK;
643 }
644
645 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(IDirectSoundBuffer8 *iface, DWORD *freq)
646 {
647 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
648
649 TRACE("(%p,%p)\n",This,freq);
650
651 if (freq == NULL) {
652 WARN("invalid parameter: freq = NULL\n");
653 return DSERR_INVALIDPARAM;
654 }
655
656 *freq = This->freq;
657 TRACE("-> %d\n", *freq);
658
659 return DS_OK;
660 }
661
662 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(IDirectSoundBuffer8 *iface, DWORD dwEffectsCount,
663 LPDSEFFECTDESC pDSFXDesc, DWORD *pdwResultCodes)
664 {
665 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
666 DWORD u;
667
668 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
669
670 if (pdwResultCodes)
671 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
672
673 WARN("control unavailable\n");
674 return DSERR_CONTROLUNAVAIL;
675 }
676
677 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(IDirectSoundBuffer8 *iface,
678 DWORD dwFlags, DWORD dwEffectsCount, DWORD *pdwResultCodes)
679 {
680 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
681 DWORD u;
682
683 FIXME("(%p,%08u,%u,%p): stub, faking success\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
684
685 if (pdwResultCodes)
686 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
687
688 WARN("control unavailable\n");
689 return DS_OK;
690 }
691
692 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(IDirectSoundBuffer8 *iface,
693 REFGUID rguidObject, DWORD dwIndex, REFGUID rguidInterface, void **ppObject)
694 {
695 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
696
697 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
698
699 WARN("control unavailable\n");
700 return DSERR_CONTROLUNAVAIL;
701 }
702
703 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(IDirectSoundBuffer8 *iface,
704 IDirectSound *dsound, LPCDSBUFFERDESC dbsd)
705 {
706 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
707
708 WARN("(%p) already initialized\n", This);
709 return DSERR_ALREADYINITIALIZED;
710 }
711
712 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(IDirectSoundBuffer8 *iface, LPDSBCAPS caps)
713 {
714 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
715
716 TRACE("(%p)->(%p)\n",This,caps);
717
718 if (caps == NULL) {
719 WARN("invalid parameter: caps == NULL\n");
720 return DSERR_INVALIDPARAM;
721 }
722
723 if (caps->dwSize < sizeof(*caps)) {
724 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
725 return DSERR_INVALIDPARAM;
726 }
727
728 caps->dwFlags = This->dsbd.dwFlags;
729 caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
730
731 caps->dwBufferBytes = This->buflen;
732
733 /* According to windows, this is zero*/
734 caps->dwUnlockTransferRate = 0;
735 caps->dwPlayCpuOverhead = 0;
736
737 return DS_OK;
738 }
739
740 static HRESULT WINAPI IDirectSoundBufferImpl_QueryInterface(IDirectSoundBuffer8 *iface, REFIID riid,
741 void **ppobj)
742 {
743 IDirectSoundBufferImpl *This = impl_from_IDirectSoundBuffer8(iface);
744
745 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
746
747 if (ppobj == NULL) {
748 WARN("invalid parameter\n");
749 return E_INVALIDARG;
750 }
751
752 *ppobj = NULL; /* assume failure */
753
754 if ( IsEqualGUID(riid, &IID_IUnknown) ||
755 IsEqualGUID(riid, &IID_IDirectSoundBuffer) ||
756 IsEqualGUID(riid, &IID_IDirectSoundBuffer8) ) {
757 IDirectSoundBuffer8_AddRef(iface);
758 *ppobj = iface;
759 return S_OK;
760 }
761
762 if ( IsEqualGUID( &IID_IDirectSoundNotify, riid ) ) {
763 IDirectSoundNotify_AddRef(&This->IDirectSoundNotify_iface);
764 *ppobj = &This->IDirectSoundNotify_iface;
765 return S_OK;
766 }
767
768 if ( IsEqualGUID( &IID_IDirectSound3DBuffer, riid ) ) {
769 if(This->dsbd.dwFlags & DSBCAPS_CTRL3D){
770 IDirectSound3DBuffer_AddRef(&This->IDirectSound3DBuffer_iface);
771 *ppobj = &This->IDirectSound3DBuffer_iface;
772 return S_OK;
773 }
774 TRACE("app requested IDirectSound3DBuffer on non-3D secondary buffer\n");
775 return E_NOINTERFACE;
776 }
777
778 if ( IsEqualGUID( &IID_IDirectSound3DListener, riid ) ) {
779 ERR("app requested IDirectSound3DListener on secondary buffer\n");
780 return E_NOINTERFACE;
781 }
782
783 if ( IsEqualGUID( &IID_IKsPropertySet, riid ) ) {
784 IKsPropertySet_AddRef(&This->IKsPropertySet_iface);
785 *ppobj = &This->IKsPropertySet_iface;
786 return S_OK;
787 }
788
789 FIXME( "Unknown IID %s\n", debugstr_guid( riid ) );
790
791 return E_NOINTERFACE;
792 }
793
794 static const IDirectSoundBuffer8Vtbl dsbvt =
795 {
796 IDirectSoundBufferImpl_QueryInterface,
797 IDirectSoundBufferImpl_AddRef,
798 IDirectSoundBufferImpl_Release,
799 IDirectSoundBufferImpl_GetCaps,
800 IDirectSoundBufferImpl_GetCurrentPosition,
801 IDirectSoundBufferImpl_GetFormat,
802 IDirectSoundBufferImpl_GetVolume,
803 IDirectSoundBufferImpl_GetPan,
804 IDirectSoundBufferImpl_GetFrequency,
805 IDirectSoundBufferImpl_GetStatus,
806 IDirectSoundBufferImpl_Initialize,
807 IDirectSoundBufferImpl_Lock,
808 IDirectSoundBufferImpl_Play,
809 IDirectSoundBufferImpl_SetCurrentPosition,
810 IDirectSoundBufferImpl_SetFormat,
811 IDirectSoundBufferImpl_SetVolume,
812 IDirectSoundBufferImpl_SetPan,
813 IDirectSoundBufferImpl_SetFrequency,
814 IDirectSoundBufferImpl_Stop,
815 IDirectSoundBufferImpl_Unlock,
816 IDirectSoundBufferImpl_Restore,
817 IDirectSoundBufferImpl_SetFX,
818 IDirectSoundBufferImpl_AcquireResources,
819 IDirectSoundBufferImpl_GetObjectInPath
820 };
821
822 HRESULT IDirectSoundBufferImpl_Create(
823 DirectSoundDevice * device,
824 IDirectSoundBufferImpl **pdsb,
825 LPCDSBUFFERDESC dsbd)
826 {
827 IDirectSoundBufferImpl *dsb;
828 LPWAVEFORMATEX wfex = dsbd->lpwfxFormat;
829 HRESULT err = DS_OK;
830 DWORD capf = 0;
831 TRACE("(%p,%p,%p)\n",device,pdsb,dsbd);
832
833 if (dsbd->dwBufferBytes < DSBSIZE_MIN || dsbd->dwBufferBytes > DSBSIZE_MAX) {
834 WARN("invalid parameter: dsbd->dwBufferBytes = %d\n", dsbd->dwBufferBytes);
835 *pdsb = NULL;
836 return DSERR_INVALIDPARAM; /* FIXME: which error? */
837 }
838
839 dsb = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(*dsb));
840
841 if (dsb == 0) {
842 WARN("out of memory\n");
843 *pdsb = NULL;
844 return DSERR_OUTOFMEMORY;
845 }
846
847 TRACE("Created buffer at %p\n", dsb);
848
849 dsb->ref = 0;
850 dsb->refn = 0;
851 dsb->ref3D = 0;
852 dsb->refiks = 0;
853 dsb->numIfaces = 0;
854 dsb->device = device;
855 dsb->IDirectSoundBuffer8_iface.lpVtbl = &dsbvt;
856 dsb->IDirectSoundNotify_iface.lpVtbl = &dsnvt;
857 dsb->IDirectSound3DBuffer_iface.lpVtbl = &ds3dbvt;
858 dsb->IKsPropertySet_iface.lpVtbl = &iksbvt;
859
860 /* size depends on version */
861 CopyMemory(&dsb->dsbd, dsbd, dsbd->dwSize);
862
863 dsb->pwfx = DSOUND_CopyFormat(wfex);
864 if (dsb->pwfx == NULL) {
865 HeapFree(GetProcessHeap(),0,dsb);
866 *pdsb = NULL;
867 return DSERR_OUTOFMEMORY;
868 }
869
870 if (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign)
871 dsb->buflen = dsbd->dwBufferBytes +
872 (dsbd->lpwfxFormat->nBlockAlign -
873 (dsbd->dwBufferBytes % dsbd->lpwfxFormat->nBlockAlign));
874 else
875 dsb->buflen = dsbd->dwBufferBytes;
876
877 dsb->freq = dsbd->lpwfxFormat->nSamplesPerSec;
878 dsb->notifies = NULL;
879 dsb->nrofnotifies = 0;
880
881 /* Check necessary hardware mixing capabilities */
882 if (wfex->nChannels==2) capf |= DSCAPS_SECONDARYSTEREO;
883 else capf |= DSCAPS_SECONDARYMONO;
884 if (wfex->wBitsPerSample==16) capf |= DSCAPS_SECONDARY16BIT;
885 else capf |= DSCAPS_SECONDARY8BIT;
886
887 TRACE("capf = 0x%08x, device->drvcaps.dwFlags = 0x%08x\n", capf, device->drvcaps.dwFlags);
888
889 /* Allocate an empty buffer */
890 dsb->buffer = HeapAlloc(GetProcessHeap(),0,sizeof(*(dsb->buffer)));
891 if (dsb->buffer == NULL) {
892 WARN("out of memory\n");
893 HeapFree(GetProcessHeap(),0,dsb->pwfx);
894 HeapFree(GetProcessHeap(),0,dsb);
895 *pdsb = NULL;
896 return DSERR_OUTOFMEMORY;
897 }
898
899 /* Allocate system memory for buffer */
900 dsb->buffer->memory = HeapAlloc(GetProcessHeap(),0,dsb->buflen);
901 if (dsb->buffer->memory == NULL) {
902 WARN("out of memory\n");
903 HeapFree(GetProcessHeap(),0,dsb->pwfx);
904 HeapFree(GetProcessHeap(),0,dsb->buffer);
905 HeapFree(GetProcessHeap(),0,dsb);
906 *pdsb = NULL;
907 return DSERR_OUTOFMEMORY;
908 }
909
910 dsb->buffer->ref = 1;
911 list_init(&dsb->buffer->buffers);
912 list_add_head(&dsb->buffer->buffers, &dsb->entry);
913 FillMemory(dsb->buffer->memory, dsb->buflen, dsbd->lpwfxFormat->wBitsPerSample == 8 ? 128 : 0);
914
915 /* It's not necessary to initialize values to zero since */
916 /* we allocated this structure with HEAP_ZERO_MEMORY... */
917 dsb->sec_mixpos = 0;
918 dsb->state = STATE_STOPPED;
919
920 dsb->freqAdjust = dsb->freq / (float)device->pwfx->nSamplesPerSec;
921 dsb->nAvgBytesPerSec = dsb->freq *
922 dsbd->lpwfxFormat->nBlockAlign;
923
924 /* calculate fragment size and write lead */
925 DSOUND_RecalcFormat(dsb);
926
927 if (dsb->dsbd.dwFlags & DSBCAPS_CTRL3D) {
928 dsb->ds3db_ds3db.dwSize = sizeof(DS3DBUFFER);
929 dsb->ds3db_ds3db.vPosition.x = 0.0;
930 dsb->ds3db_ds3db.vPosition.y = 0.0;
931 dsb->ds3db_ds3db.vPosition.z = 0.0;
932 dsb->ds3db_ds3db.vVelocity.x = 0.0;
933 dsb->ds3db_ds3db.vVelocity.y = 0.0;
934 dsb->ds3db_ds3db.vVelocity.z = 0.0;
935 dsb->ds3db_ds3db.dwInsideConeAngle = DS3D_DEFAULTCONEANGLE;
936 dsb->ds3db_ds3db.dwOutsideConeAngle = DS3D_DEFAULTCONEANGLE;
937 dsb->ds3db_ds3db.vConeOrientation.x = 0.0;
938 dsb->ds3db_ds3db.vConeOrientation.y = 0.0;
939 dsb->ds3db_ds3db.vConeOrientation.z = 0.0;
940 dsb->ds3db_ds3db.lConeOutsideVolume = DS3D_DEFAULTCONEOUTSIDEVOLUME;
941 dsb->ds3db_ds3db.flMinDistance = DS3D_DEFAULTMINDISTANCE;
942 dsb->ds3db_ds3db.flMaxDistance = DS3D_DEFAULTMAXDISTANCE;
943 dsb->ds3db_ds3db.dwMode = DS3DMODE_NORMAL;
944
945 dsb->ds3db_need_recalc = FALSE;
946 DSOUND_Calc3DBuffer(dsb);
947 } else
948 DSOUND_RecalcVolPan(&(dsb->volpan));
949
950 RtlInitializeResource(&dsb->lock);
951
952 /* register buffer if not primary */
953 if (!(dsbd->dwFlags & DSBCAPS_PRIMARYBUFFER)) {
954 err = DirectSoundDevice_AddBuffer(device, dsb);
955 if (err != DS_OK) {
956 HeapFree(GetProcessHeap(),0,dsb->buffer->memory);
957 HeapFree(GetProcessHeap(),0,dsb->buffer);
958 RtlDeleteResource(&dsb->lock);
959 HeapFree(GetProcessHeap(),0,dsb->pwfx);
960 HeapFree(GetProcessHeap(),0,dsb);
961 dsb = NULL;
962 }
963 }
964
965 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
966 *pdsb = dsb;
967 return err;
968 }
969
970 void secondarybuffer_destroy(IDirectSoundBufferImpl *This)
971 {
972 ULONG ref = InterlockedIncrement(&This->numIfaces);
973
974 if (ref > 1)
975 WARN("Destroying buffer with %u in use interfaces\n", ref - 1);
976
977 DirectSoundDevice_RemoveBuffer(This->device, This);
978 RtlDeleteResource(&This->lock);
979
980 This->buffer->ref--;
981 list_remove(&This->entry);
982 if (This->buffer->ref == 0) {
983 HeapFree(GetProcessHeap(), 0, This->buffer->memory);
984 HeapFree(GetProcessHeap(), 0, This->buffer);
985 }
986
987 HeapFree(GetProcessHeap(), 0, This->notifies);
988 HeapFree(GetProcessHeap(), 0, This->pwfx);
989 HeapFree(GetProcessHeap(), 0, This);
990
991 TRACE("(%p) released\n", This);
992 }
993
994 HRESULT IDirectSoundBufferImpl_Duplicate(
995 DirectSoundDevice *device,
996 IDirectSoundBufferImpl **ppdsb,
997 IDirectSoundBufferImpl *pdsb)
998 {
999 IDirectSoundBufferImpl *dsb;
1000 HRESULT hres = DS_OK;
1001 TRACE("(%p,%p,%p)\n", device, ppdsb, pdsb);
1002
1003 dsb = HeapAlloc(GetProcessHeap(),0,sizeof(*dsb));
1004 if (dsb == NULL) {
1005 WARN("out of memory\n");
1006 *ppdsb = NULL;
1007 return DSERR_OUTOFMEMORY;
1008 }
1009
1010 RtlAcquireResourceShared(&pdsb->lock, TRUE);
1011
1012 CopyMemory(dsb, pdsb, sizeof(*dsb));
1013
1014 dsb->pwfx = DSOUND_CopyFormat(pdsb->pwfx);
1015
1016 RtlReleaseResource(&pdsb->lock);
1017
1018 if (dsb->pwfx == NULL) {
1019 HeapFree(GetProcessHeap(),0,dsb);
1020 *ppdsb = NULL;
1021 return DSERR_OUTOFMEMORY;
1022 }
1023
1024 dsb->buffer->ref++;
1025 list_add_head(&dsb->buffer->buffers, &dsb->entry);
1026 dsb->ref = 0;
1027 dsb->refn = 0;
1028 dsb->ref3D = 0;
1029 dsb->refiks = 0;
1030 dsb->numIfaces = 0;
1031 dsb->state = STATE_STOPPED;
1032 dsb->sec_mixpos = 0;
1033 dsb->notifies = NULL;
1034 dsb->nrofnotifies = 0;
1035 dsb->device = device;
1036 DSOUND_RecalcFormat(dsb);
1037
1038 RtlInitializeResource(&dsb->lock);
1039
1040 /* register buffer */
1041 hres = DirectSoundDevice_AddBuffer(device, dsb);
1042 if (hres != DS_OK) {
1043 RtlDeleteResource(&dsb->lock);
1044 list_remove(&dsb->entry);
1045 dsb->buffer->ref--;
1046 HeapFree(GetProcessHeap(),0,dsb->pwfx);
1047 HeapFree(GetProcessHeap(),0,dsb);
1048 dsb = NULL;
1049 }
1050
1051 IDirectSoundBuffer8_AddRef(&dsb->IDirectSoundBuffer8_iface);
1052 *ppdsb = dsb;
1053 return hres;
1054 }
1055
1056 /*******************************************************************************
1057 * IKsPropertySet
1058 */
1059
1060 static inline IDirectSoundBufferImpl *impl_from_IKsPropertySet(IKsPropertySet *iface)
1061 {
1062 return CONTAINING_RECORD(iface, IDirectSoundBufferImpl, IKsPropertySet_iface);
1063 }
1064
1065 /* IUnknown methods */
1066 static HRESULT WINAPI IKsPropertySetImpl_QueryInterface(IKsPropertySet *iface, REFIID riid,
1067 void **ppobj)
1068 {
1069 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1070
1071 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
1072
1073 return IDirectSoundBuffer_QueryInterface(&This->IDirectSoundBuffer8_iface, riid, ppobj);
1074 }
1075
1076 static ULONG WINAPI IKsPropertySetImpl_AddRef(IKsPropertySet *iface)
1077 {
1078 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1079 ULONG ref = InterlockedIncrement(&This->refiks);
1080
1081 TRACE("(%p) ref was %d\n", This, ref - 1);
1082
1083 if(ref == 1)
1084 InterlockedIncrement(&This->numIfaces);
1085
1086 return ref;
1087 }
1088
1089 static ULONG WINAPI IKsPropertySetImpl_Release(IKsPropertySet *iface)
1090 {
1091 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1092 ULONG ref;
1093
1094 if (is_primary_buffer(This)){
1095 ref = capped_refcount_dec(&This->refiks);
1096 if(!ref)
1097 capped_refcount_dec(&This->numIfaces);
1098 TRACE("(%p) ref is now: %d\n", This, ref);
1099 return ref;
1100 }
1101
1102 ref = InterlockedDecrement(&This->refiks);
1103 if (!ref && !InterlockedDecrement(&This->numIfaces))
1104 secondarybuffer_destroy(This);
1105
1106 TRACE("(%p) ref is now %d\n", This, ref);
1107
1108 return ref;
1109 }
1110
1111 static HRESULT WINAPI IKsPropertySetImpl_Get(IKsPropertySet *iface, REFGUID guidPropSet,
1112 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1113 ULONG cbPropData, ULONG *pcbReturned)
1114 {
1115 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1116
1117 TRACE("(iface=%p,guidPropSet=%s,dwPropID=%d,pInstanceData=%p,cbInstanceData=%d,pPropData=%p,cbPropData=%d,pcbReturned=%p)\n",
1118 This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData,pcbReturned);
1119
1120 return E_PROP_ID_UNSUPPORTED;
1121 }
1122
1123 static HRESULT WINAPI IKsPropertySetImpl_Set(IKsPropertySet *iface, REFGUID guidPropSet,
1124 ULONG dwPropID, void *pInstanceData, ULONG cbInstanceData, void *pPropData,
1125 ULONG cbPropData)
1126 {
1127 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1128
1129 TRACE("(%p,%s,%d,%p,%d,%p,%d)\n",This,debugstr_guid(guidPropSet),dwPropID,pInstanceData,cbInstanceData,pPropData,cbPropData);
1130
1131 return E_PROP_ID_UNSUPPORTED;
1132 }
1133
1134 static HRESULT WINAPI IKsPropertySetImpl_QuerySupport(IKsPropertySet *iface, REFGUID guidPropSet,
1135 ULONG dwPropID, ULONG *pTypeSupport)
1136 {
1137 IDirectSoundBufferImpl *This = impl_from_IKsPropertySet(iface);
1138
1139 TRACE("(%p,%s,%d,%p)\n",This,debugstr_guid(guidPropSet),dwPropID,pTypeSupport);
1140
1141 return E_PROP_ID_UNSUPPORTED;
1142 }
1143
1144 const IKsPropertySetVtbl iksbvt = {
1145 IKsPropertySetImpl_QueryInterface,
1146 IKsPropertySetImpl_AddRef,
1147 IKsPropertySetImpl_Release,
1148 IKsPropertySetImpl_Get,
1149 IKsPropertySetImpl_Set,
1150 IKsPropertySetImpl_QuerySupport
1151 };