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