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 NONAMELESSSTRUCT
25 #define NONAMELESSUNION
26 #include "windef.h"
27 #include "winbase.h"
28 #include "winuser.h"
29 #include "mmsystem.h"
30 #include "winternl.h"
31 #include "wine/debug.h"
32 #include "dsound.h"
33 #include "dsdriver.h"
34 #include "dsound_private.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(dsound);
37
38 static HRESULT SecondaryBufferImpl_Destroy(SecondaryBufferImpl *pdsb);
39
40 /*******************************************************************************
41 * IDirectSoundNotify
42 */
43
44 struct IDirectSoundNotifyImpl
45 {
46 /* IUnknown fields */
47 const IDirectSoundNotifyVtbl *lpVtbl;
48 LONG ref;
49 IDirectSoundBufferImpl* dsb;
50 };
51
52 static HRESULT IDirectSoundNotifyImpl_Create(IDirectSoundBufferImpl *dsb,
53 IDirectSoundNotifyImpl **pdsn);
54 static HRESULT IDirectSoundNotifyImpl_Destroy(IDirectSoundNotifyImpl *pdsn);
55
56 static HRESULT WINAPI IDirectSoundNotifyImpl_QueryInterface(
57 LPDIRECTSOUNDNOTIFY iface,REFIID riid,LPVOID *ppobj
58 ) {
59 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
60 TRACE("(%p,%s,%p)\n",This,debugstr_guid(riid),ppobj);
61
62 if (This->dsb == NULL) {
63 WARN("invalid parameter\n");
64 return E_INVALIDARG;
65 }
66
67 return IDirectSoundBuffer_QueryInterface((LPDIRECTSOUNDBUFFER)This->dsb, riid, ppobj);
68 }
69
70 static ULONG WINAPI IDirectSoundNotifyImpl_AddRef(LPDIRECTSOUNDNOTIFY iface)
71 {
72 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
73 ULONG ref = InterlockedIncrement(&(This->ref));
74 TRACE("(%p) ref was %d\n", This, ref - 1);
75 return ref;
76 }
77
78 static ULONG WINAPI IDirectSoundNotifyImpl_Release(LPDIRECTSOUNDNOTIFY iface)
79 {
80 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
81 ULONG ref = InterlockedDecrement(&(This->ref));
82 TRACE("(%p) ref was %d\n", This, ref + 1);
83
84 if (!ref) {
85 IDirectSoundBuffer_Release((LPDIRECTSOUNDBUFFER)This->dsb);
86 This->dsb->notify = NULL;
87 HeapFree(GetProcessHeap(), 0, This);
88 TRACE("(%p) released\n", This);
89 }
90 return ref;
91 }
92
93 static HRESULT WINAPI IDirectSoundNotifyImpl_SetNotificationPositions(
94 LPDIRECTSOUNDNOTIFY iface,DWORD howmuch,LPCDSBPOSITIONNOTIFY notify
95 ) {
96 IDirectSoundNotifyImpl *This = (IDirectSoundNotifyImpl *)iface;
97 TRACE("(%p,0x%08x,%p)\n",This,howmuch,notify);
98
99 if (howmuch > 0 && notify == NULL) {
100 WARN("invalid parameter: notify == NULL\n");
101 return DSERR_INVALIDPARAM;
102 }
103
104 if (TRACE_ON(dsound)) {
105 unsigned int i;
106 for (i=0;i<howmuch;i++)
107 TRACE("notify at %d to %p\n",
108 notify[i].dwOffset,notify[i].hEventNotify);
109 }
110
111 if (This->dsb->hwnotify) {
112 HRESULT hres;
113 hres = IDsDriverNotify_SetNotificationPositions(This->dsb->hwnotify, howmuch, notify);
114 if (hres != DS_OK)
115 WARN("IDsDriverNotify_SetNotificationPositions failed\n");
116 return hres;
117 } else if (howmuch > 0) {
118 /* Make an internal copy of the caller-supplied array.
119 * Replace the existing copy if one is already present. */
120 if (This->dsb->notifies)
121 This->dsb->notifies = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
122 This->dsb->notifies, howmuch * sizeof(DSBPOSITIONNOTIFY));
123 else
124 This->dsb->notifies = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
125 howmuch * sizeof(DSBPOSITIONNOTIFY));
126
127 if (This->dsb->notifies == NULL) {
128 WARN("out of memory\n");
129 return DSERR_OUTOFMEMORY;
130 }
131 CopyMemory(This->dsb->notifies, notify, howmuch * sizeof(DSBPOSITIONNOTIFY));
132 This->dsb->nrofnotifies = howmuch;
133 } else {
134 HeapFree(GetProcessHeap(), 0, This->dsb->notifies);
135 This->dsb->notifies = NULL;
136 This->dsb->nrofnotifies = 0;
137 }
138
139 return S_OK;
140 }
141
142 static const IDirectSoundNotifyVtbl dsnvt =
143 {
144 IDirectSoundNotifyImpl_QueryInterface,
145 IDirectSoundNotifyImpl_AddRef,
146 IDirectSoundNotifyImpl_Release,
147 IDirectSoundNotifyImpl_SetNotificationPositions,
148 };
149
150 static HRESULT IDirectSoundNotifyImpl_Create(
151 IDirectSoundBufferImpl * dsb,
152 IDirectSoundNotifyImpl **pdsn)
153 {
154 IDirectSoundNotifyImpl * dsn;
155 TRACE("(%p,%p)\n",dsb,pdsn);
156
157 dsn = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(dsn));
158
159 if (dsn == NULL) {
160 WARN("out of memory\n");
161 return DSERR_OUTOFMEMORY;
162 }
163
164 dsn->ref = 0;
165 dsn->lpVtbl = &dsnvt;
166 dsn->dsb = dsb;
167 dsb->notify = dsn;
168 IDirectSoundBuffer_AddRef((LPDIRECTSOUNDBUFFER)dsb);
169
170 *pdsn = dsn;
171 return DS_OK;
172 }
173
174 static HRESULT IDirectSoundNotifyImpl_Destroy(
175 IDirectSoundNotifyImpl *pdsn)
176 {
177 TRACE("(%p)\n",pdsn);
178
179 while (IDirectSoundNotifyImpl_Release((LPDIRECTSOUNDNOTIFY)pdsn) > 0);
180
181 return DS_OK;
182 }
183
184 /*******************************************************************************
185 * IDirectSoundBuffer
186 */
187
188 static HRESULT WINAPI IDirectSoundBufferImpl_SetFormat(
189 LPDIRECTSOUNDBUFFER8 iface,LPCWAVEFORMATEX wfex
190 ) {
191 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
192
193 TRACE("(%p,%p)\n",This,wfex);
194 /* This method is not available on secondary buffers */
195 WARN("invalid call\n");
196 return DSERR_INVALIDCALL;
197 }
198
199 static HRESULT WINAPI IDirectSoundBufferImpl_SetVolume(
200 LPDIRECTSOUNDBUFFER8 iface,LONG vol
201 ) {
202 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
203 LONG oldVol;
204 HRESULT hres = DS_OK;
205
206 TRACE("(%p,%d)\n",This,vol);
207
208 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
209 WARN("control unavailable: This->dsbd.dwFlags = 0x%08x\n", This->dsbd.dwFlags);
210 return DSERR_CONTROLUNAVAIL;
211 }
212
213 if ((vol > DSBVOLUME_MAX) || (vol < DSBVOLUME_MIN)) {
214 WARN("invalid parameter: vol = %d\n", vol);
215 return DSERR_INVALIDPARAM;
216 }
217
218 /* **** */
219 RtlAcquireResourceExclusive(&This->lock, TRUE);
220
221 if (This->dsbd.dwFlags & DSBCAPS_CTRL3D) {
222 oldVol = This->ds3db_lVolume;
223 This->ds3db_lVolume = vol;
224 if (vol != oldVol)
225 /* recalc 3d volume, which in turn recalcs the pans */
226 DSOUND_Calc3DBuffer(This);
227 } else {
228 oldVol = This->volpan.lVolume;
229 This->volpan.lVolume = vol;
230 if (vol != oldVol)
231 DSOUND_RecalcVolPan(&(This->volpan));
232 }
233
234 if (vol != oldVol) {
235 if (This->hwbuf) {
236 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
237 if (hres != DS_OK)
238 WARN("IDsDriverBuffer_SetVolumePan failed\n");
239 }
240 }
241
242 RtlReleaseResource(&This->lock);
243 /* **** */
244
245 return hres;
246 }
247
248 static HRESULT WINAPI IDirectSoundBufferImpl_GetVolume(
249 LPDIRECTSOUNDBUFFER8 iface,LPLONG vol
250 ) {
251 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
252 TRACE("(%p,%p)\n",This,vol);
253
254 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLVOLUME)) {
255 WARN("control unavailable\n");
256 return DSERR_CONTROLUNAVAIL;
257 }
258
259 if (vol == NULL) {
260 WARN("invalid parameter: vol == NULL\n");
261 return DSERR_INVALIDPARAM;
262 }
263
264 *vol = This->volpan.lVolume;
265
266 return DS_OK;
267 }
268
269 static HRESULT WINAPI IDirectSoundBufferImpl_SetFrequency(
270 LPDIRECTSOUNDBUFFER8 iface,DWORD freq
271 ) {
272 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
273 DWORD oldFreq;
274
275 TRACE("(%p,%d)\n",This,freq);
276
277 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLFREQUENCY)) {
278 WARN("control unavailable\n");
279 return DSERR_CONTROLUNAVAIL;
280 }
281
282 if (freq == DSBFREQUENCY_ORIGINAL)
283 freq = This->pwfx->nSamplesPerSec;
284
285 if ((freq < DSBFREQUENCY_MIN) || (freq > DSBFREQUENCY_MAX)) {
286 WARN("invalid parameter: freq = %d\n", freq);
287 return DSERR_INVALIDPARAM;
288 }
289
290 /* **** */
291 RtlAcquireResourceExclusive(&This->lock, TRUE);
292
293 oldFreq = This->freq;
294 This->freq = freq;
295 if (freq != oldFreq) {
296 This->freqAdjust = ((DWORD64)This->freq << DSOUND_FREQSHIFT) / This->device->pwfx->nSamplesPerSec;
297 This->nAvgBytesPerSec = freq * This->pwfx->nBlockAlign;
298 DSOUND_RecalcFormat(This);
299 DSOUND_MixToTemporary(This, 0, This->buflen, FALSE);
300 }
301
302 RtlReleaseResource(&This->lock);
303 /* **** */
304
305 return DS_OK;
306 }
307
308 static HRESULT WINAPI IDirectSoundBufferImpl_Play(
309 LPDIRECTSOUNDBUFFER8 iface,DWORD reserved1,DWORD reserved2,DWORD flags
310 ) {
311 HRESULT hres = DS_OK;
312 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
313 TRACE("(%p,%08x,%08x,%08x)\n",This,reserved1,reserved2,flags);
314
315 /* **** */
316 RtlAcquireResourceExclusive(&This->lock, TRUE);
317
318 This->playflags = flags;
319 if (This->state == STATE_STOPPED && !This->hwbuf) {
320 This->leadin = TRUE;
321 This->state = STATE_STARTING;
322 } else if (This->state == STATE_STOPPING)
323 This->state = STATE_PLAYING;
324 if (This->hwbuf) {
325 hres = IDsDriverBuffer_Play(This->hwbuf, 0, 0, This->playflags);
326 if (hres != DS_OK)
327 WARN("IDsDriverBuffer_Play failed\n");
328 else
329 This->state = STATE_PLAYING;
330 }
331
332 RtlReleaseResource(&This->lock);
333 /* **** */
334
335 return hres;
336 }
337
338 static HRESULT WINAPI IDirectSoundBufferImpl_Stop(LPDIRECTSOUNDBUFFER8 iface)
339 {
340 HRESULT hres = DS_OK;
341 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
342 TRACE("(%p)\n",This);
343
344 /* **** */
345 RtlAcquireResourceExclusive(&This->lock, TRUE);
346
347 if (This->state == STATE_PLAYING)
348 This->state = STATE_STOPPING;
349 else if (This->state == STATE_STARTING)
350 {
351 This->state = STATE_STOPPED;
352 DSOUND_CheckEvent(This, 0, 0);
353 }
354 if (This->hwbuf) {
355 hres = IDsDriverBuffer_Stop(This->hwbuf);
356 if (hres != DS_OK)
357 WARN("IDsDriverBuffer_Stop failed\n");
358 else
359 This->state = STATE_STOPPED;
360 }
361
362 RtlReleaseResource(&This->lock);
363 /* **** */
364
365 return hres;
366 }
367
368 static ULONG WINAPI IDirectSoundBufferImpl_AddRef(LPDIRECTSOUNDBUFFER8 iface)
369 {
370 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
371 ULONG ref = InterlockedIncrement(&(This->ref));
372 TRACE("(%p) ref was %d\n", This, ref - 1);
373 return ref;
374 }
375
376 static ULONG WINAPI IDirectSoundBufferImpl_Release(LPDIRECTSOUNDBUFFER8 iface)
377 {
378 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
379 ULONG ref = InterlockedDecrement(&(This->ref));
380 TRACE("(%p) ref was %d\n", This, ref + 1);
381
382 if (!ref) {
383 DirectSoundDevice_RemoveBuffer(This->device, This);
384 RtlDeleteResource(&This->lock);
385
386 if (This->hwbuf) {
387 IDsDriverBuffer_Release(This->hwbuf);
388 if (This->device->drvdesc.dwFlags & DSDDESC_USESYSTEMMEMORY) {
389 This->buffer->ref--;
390 list_remove(&This->entry);
391 if (This->buffer->ref==0) {
392 HeapFree(GetProcessHeap(),0,This->buffer->memory);
393 HeapFree(GetProcessHeap(),0,This->buffer);
394 }
395 }
396 } else {
397 This->buffer->ref--;
398 list_remove(&This->entry);
399 if (This->buffer->ref==0) {
400 HeapFree(GetProcessHeap(),0,This->buffer->memory);
401 HeapFree(GetProcessHeap(),0,This->buffer);
402 }
403 }
404
405 HeapFree(GetProcessHeap(), 0, This->tmp_buffer);
406 HeapFree(GetProcessHeap(), 0, This->notifies);
407 HeapFree(GetProcessHeap(), 0, This->pwfx);
408 HeapFree(GetProcessHeap(), 0, This);
409
410 TRACE("(%p) released\n", This);
411 }
412 return ref;
413 }
414
415 static HRESULT WINAPI IDirectSoundBufferImpl_GetCurrentPosition(
416 LPDIRECTSOUNDBUFFER8 iface,LPDWORD playpos,LPDWORD writepos
417 ) {
418 HRESULT hres;
419 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
420 TRACE("(%p,%p,%p)\n",This,playpos,writepos);
421
422 RtlAcquireResourceShared(&This->lock, TRUE);
423 if (This->hwbuf) {
424 hres=IDsDriverBuffer_GetPosition(This->hwbuf,playpos,writepos);
425 if (hres != DS_OK) {
426 WARN("IDsDriverBuffer_GetPosition failed\n");
427 return hres;
428 }
429 } else {
430 DWORD pos = This->sec_mixpos;
431
432 /* sanity */
433 if (pos >= This->buflen){
434 FIXME("Bad play position. playpos: %d, buflen: %d\n", pos, This->buflen);
435 pos %= This->buflen;
436 }
437
438 if (playpos)
439 *playpos = pos;
440 if (writepos)
441 *writepos = pos;
442 }
443 if (writepos && This->state != STATE_STOPPED && (!This->hwbuf || !(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDWRITELEAD))) {
444 /* apply the documented 10ms lead to writepos */
445 *writepos += This->writelead;
446 *writepos %= This->buflen;
447 }
448 RtlReleaseResource(&This->lock);
449
450 TRACE("playpos = %d, writepos = %d, buflen=%d (%p, time=%d)\n",
451 playpos?*playpos:-1, writepos?*writepos:-1, This->buflen, This, GetTickCount());
452
453 return DS_OK;
454 }
455
456 static HRESULT WINAPI IDirectSoundBufferImpl_GetStatus(
457 LPDIRECTSOUNDBUFFER8 iface,LPDWORD status
458 ) {
459 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
460 TRACE("(%p,%p), thread is %04x\n",This,status,GetCurrentThreadId());
461
462 if (status == NULL) {
463 WARN("invalid parameter: status = NULL\n");
464 return DSERR_INVALIDPARAM;
465 }
466
467 *status = 0;
468 RtlAcquireResourceShared(&This->lock, TRUE);
469 if ((This->state == STATE_STARTING) || (This->state == STATE_PLAYING)) {
470 *status |= DSBSTATUS_PLAYING;
471 if (This->playflags & DSBPLAY_LOOPING)
472 *status |= DSBSTATUS_LOOPING;
473 }
474 RtlReleaseResource(&This->lock);
475
476 TRACE("status=%x\n", *status);
477 return DS_OK;
478 }
479
480
481 static HRESULT WINAPI IDirectSoundBufferImpl_GetFormat(
482 LPDIRECTSOUNDBUFFER8 iface,
483 LPWAVEFORMATEX lpwf,
484 DWORD wfsize,
485 LPDWORD wfwritten)
486 {
487 DWORD size;
488 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
489 TRACE("(%p,%p,%d,%p)\n",This,lpwf,wfsize,wfwritten);
490
491 size = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
492
493 if (lpwf) { /* NULL is valid */
494 if (wfsize >= size) {
495 CopyMemory(lpwf,This->pwfx,size);
496 if (wfwritten)
497 *wfwritten = size;
498 } else {
499 WARN("invalid parameter: wfsize too small\n");
500 CopyMemory(lpwf,This->pwfx,wfsize);
501 if (wfwritten)
502 *wfwritten = wfsize;
503 return DSERR_INVALIDPARAM;
504 }
505 } else {
506 if (wfwritten)
507 *wfwritten = sizeof(WAVEFORMATEX) + This->pwfx->cbSize;
508 else {
509 WARN("invalid parameter: wfwritten == NULL\n");
510 return DSERR_INVALIDPARAM;
511 }
512 }
513
514 return DS_OK;
515 }
516
517 static HRESULT WINAPI IDirectSoundBufferImpl_Lock(
518 LPDIRECTSOUNDBUFFER8 iface,DWORD writecursor,DWORD writebytes,LPVOID *lplpaudioptr1,LPDWORD audiobytes1,LPVOID *lplpaudioptr2,LPDWORD audiobytes2,DWORD flags
519 ) {
520 HRESULT hres = DS_OK;
521 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
522
523 TRACE("(%p,%d,%d,%p,%p,%p,%p,0x%08x) at %d\n",
524 This,
525 writecursor,
526 writebytes,
527 lplpaudioptr1,
528 audiobytes1,
529 lplpaudioptr2,
530 audiobytes2,
531 flags,
532 GetTickCount()
533 );
534
535 /* when this flag is set, writecursor is meaningless and must be calculated */
536 if (flags & DSBLOCK_FROMWRITECURSOR) {
537 /* GetCurrentPosition does too much magic to duplicate here */
538 hres = IDirectSoundBufferImpl_GetCurrentPosition(iface, NULL, &writecursor);
539 if (hres != DS_OK) {
540 WARN("IDirectSoundBufferImpl_GetCurrentPosition failed\n");
541 return hres;
542 }
543 }
544
545 /* when this flag is set, writebytes is meaningless and must be set */
546 if (flags & DSBLOCK_ENTIREBUFFER)
547 writebytes = This->buflen;
548
549 if (writecursor >= This->buflen) {
550 WARN("Invalid parameter, writecursor: %u >= buflen: %u\n",
551 writecursor, This->buflen);
552 return DSERR_INVALIDPARAM;
553 }
554
555 if (writebytes > This->buflen) {
556 WARN("Invalid parameter, writebytes: %u > buflen: %u\n",
557 writebytes, This->buflen);
558 return DSERR_INVALIDPARAM;
559 }
560
561 /* **** */
562 RtlAcquireResourceShared(&This->lock, TRUE);
563
564 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
565 hres = IDsDriverBuffer_Lock(This->hwbuf,
566 lplpaudioptr1, audiobytes1,
567 lplpaudioptr2, audiobytes2,
568 writecursor, writebytes,
569 0);
570 if (hres != DS_OK) {
571 WARN("IDsDriverBuffer_Lock failed\n");
572 RtlReleaseResource(&This->lock);
573 return hres;
574 }
575 } else {
576 if (writecursor+writebytes <= This->buflen) {
577 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
578 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
579 WARN("Overwriting mixing position, case 1\n");
580 *audiobytes1 = writebytes;
581 if (lplpaudioptr2)
582 *(LPBYTE*)lplpaudioptr2 = NULL;
583 if (audiobytes2)
584 *audiobytes2 = 0;
585 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n",
586 *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
587 TRACE("->%d.0\n",writebytes);
588 } else {
589 DWORD remainder = writebytes + writecursor - This->buflen;
590 *(LPBYTE*)lplpaudioptr1 = This->buffer->memory+writecursor;
591 *audiobytes1 = This->buflen-writecursor;
592 if (This->sec_mixpos >= writecursor && This->sec_mixpos < writecursor + writebytes && This->state == STATE_PLAYING)
593 WARN("Overwriting mixing position, case 2\n");
594 if (lplpaudioptr2)
595 *(LPBYTE*)lplpaudioptr2 = This->buffer->memory;
596 if (audiobytes2)
597 *audiobytes2 = writebytes-(This->buflen-writecursor);
598 if (audiobytes2 && This->sec_mixpos < remainder && This->state == STATE_PLAYING)
599 WARN("Overwriting mixing position, case 3\n");
600 TRACE("Locked %p(%i bytes) and %p(%i bytes) writecursor=%d\n", *(LPBYTE*)lplpaudioptr1, *audiobytes1, lplpaudioptr2 ? *(LPBYTE*)lplpaudioptr2 : NULL, audiobytes2 ? *audiobytes2: 0, writecursor);
601 }
602 }
603
604 RtlReleaseResource(&This->lock);
605 /* **** */
606
607 return DS_OK;
608 }
609
610 static HRESULT WINAPI IDirectSoundBufferImpl_SetCurrentPosition(
611 LPDIRECTSOUNDBUFFER8 iface,DWORD newpos
612 ) {
613 HRESULT hres = DS_OK;
614 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
615 DWORD oldpos;
616 TRACE("(%p,%d)\n",This,newpos);
617
618 /* **** */
619 RtlAcquireResourceExclusive(&This->lock, TRUE);
620
621 oldpos = This->sec_mixpos;
622
623 /* start mixing from this new location instead */
624 newpos %= This->buflen;
625 newpos -= newpos%This->pwfx->nBlockAlign;
626 This->sec_mixpos = newpos;
627
628 /* at this point, do not attempt to reset buffers, mess with primary mix position,
629 or anything like that to reduce latancy. The data already prebuffered cannot be changed */
630
631 /* position HW buffer if applicable, else just start mixing from new location instead */
632 if (This->hwbuf) {
633 hres = IDsDriverBuffer_SetPosition(This->hwbuf, This->buf_mixpos);
634 if (hres != DS_OK)
635 WARN("IDsDriverBuffer_SetPosition failed\n");
636 }
637 else if (oldpos != newpos)
638 /* FIXME: Perhaps add a call to DSOUND_MixToTemporary here? Not sure it's needed */
639 This->buf_mixpos = DSOUND_secpos_to_bufpos(This, newpos, 0, NULL);
640
641 RtlReleaseResource(&This->lock);
642 /* **** */
643
644 return hres;
645 }
646
647 static HRESULT WINAPI IDirectSoundBufferImpl_SetPan(
648 LPDIRECTSOUNDBUFFER8 iface,LONG pan
649 ) {
650 HRESULT hres = DS_OK;
651 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
652
653 TRACE("(%p,%d)\n",This,pan);
654
655 if ((pan > DSBPAN_RIGHT) || (pan < DSBPAN_LEFT)) {
656 WARN("invalid parameter: pan = %d\n", pan);
657 return DSERR_INVALIDPARAM;
658 }
659
660 /* You cannot use both pan and 3D controls */
661 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN) ||
662 (This->dsbd.dwFlags & DSBCAPS_CTRL3D)) {
663 WARN("control unavailable\n");
664 return DSERR_CONTROLUNAVAIL;
665 }
666
667 /* **** */
668 RtlAcquireResourceExclusive(&This->lock, TRUE);
669
670 if (This->volpan.lPan != pan) {
671 This->volpan.lPan = pan;
672 DSOUND_RecalcVolPan(&(This->volpan));
673
674 if (This->hwbuf) {
675 hres = IDsDriverBuffer_SetVolumePan(This->hwbuf, &(This->volpan));
676 if (hres != DS_OK)
677 WARN("IDsDriverBuffer_SetVolumePan failed\n");
678 }
679 }
680
681 RtlReleaseResource(&This->lock);
682 /* **** */
683
684 return hres;
685 }
686
687 static HRESULT WINAPI IDirectSoundBufferImpl_GetPan(
688 LPDIRECTSOUNDBUFFER8 iface,LPLONG pan
689 ) {
690 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
691 TRACE("(%p,%p)\n",This,pan);
692
693 if (!(This->dsbd.dwFlags & DSBCAPS_CTRLPAN)) {
694 WARN("control unavailable\n");
695 return DSERR_CONTROLUNAVAIL;
696 }
697
698 if (pan == NULL) {
699 WARN("invalid parameter: pan = NULL\n");
700 return DSERR_INVALIDPARAM;
701 }
702
703 *pan = This->volpan.lPan;
704
705 return DS_OK;
706 }
707
708 static HRESULT WINAPI IDirectSoundBufferImpl_Unlock(
709 LPDIRECTSOUNDBUFFER8 iface,LPVOID p1,DWORD x1,LPVOID p2,DWORD x2
710 ) {
711 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface, *iter;
712 HRESULT hres = DS_OK;
713
714 TRACE("(%p,%p,%d,%p,%d)\n", This,p1,x1,p2,x2);
715
716 /* **** */
717 RtlAcquireResourceShared(&This->lock, TRUE);
718
719 if (!(This->device->drvdesc.dwFlags & DSDDESC_DONTNEEDSECONDARYLOCK) && This->hwbuf) {
720 hres = IDsDriverBuffer_Unlock(This->hwbuf, p1, x1, p2, x2);
721 if (hres != DS_OK)
722 WARN("IDsDriverBuffer_Unlock failed\n");
723 }
724
725 RtlReleaseResource(&This->lock);
726 /* **** */
727
728 if (!p2)
729 x2 = 0;
730
731 if (!This->hwbuf && (x1 || x2))
732 {
733 RtlAcquireResourceShared(&This->device->buffer_list_lock, TRUE);
734 LIST_FOR_EACH_ENTRY(iter, &This->buffer->buffers, IDirectSoundBufferImpl, entry )
735 {
736 RtlAcquireResourceShared(&iter->lock, TRUE);
737 if (x1)
738 DSOUND_MixToTemporary(iter, (DWORD_PTR)p1 - (DWORD_PTR)iter->buffer->memory, x1, FALSE);
739 if (x2)
740 DSOUND_MixToTemporary(iter, 0, x2, FALSE);
741 RtlReleaseResource(&iter->lock);
742 }
743 RtlReleaseResource(&This->device->buffer_list_lock);
744 }
745
746 return hres;
747 }
748
749 static HRESULT WINAPI IDirectSoundBufferImpl_Restore(
750 LPDIRECTSOUNDBUFFER8 iface
751 ) {
752 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
753 FIXME("(%p):stub\n",This);
754 return DS_OK;
755 }
756
757 static HRESULT WINAPI IDirectSoundBufferImpl_GetFrequency(
758 LPDIRECTSOUNDBUFFER8 iface,LPDWORD freq
759 ) {
760 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
761 TRACE("(%p,%p)\n",This,freq);
762
763 if (freq == NULL) {
764 WARN("invalid parameter: freq = NULL\n");
765 return DSERR_INVALIDPARAM;
766 }
767
768 *freq = This->freq;
769 TRACE("-> %d\n", *freq);
770
771 return DS_OK;
772 }
773
774 static HRESULT WINAPI IDirectSoundBufferImpl_SetFX(
775 LPDIRECTSOUNDBUFFER8 iface,DWORD dwEffectsCount,LPDSEFFECTDESC pDSFXDesc,LPDWORD pdwResultCodes
776 ) {
777 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
778 DWORD u;
779
780 FIXME("(%p,%u,%p,%p): stub\n",This,dwEffectsCount,pDSFXDesc,pdwResultCodes);
781
782 if (pdwResultCodes)
783 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
784
785 WARN("control unavailable\n");
786 return DSERR_CONTROLUNAVAIL;
787 }
788
789 static HRESULT WINAPI IDirectSoundBufferImpl_AcquireResources(
790 LPDIRECTSOUNDBUFFER8 iface,DWORD dwFlags,DWORD dwEffectsCount,LPDWORD pdwResultCodes
791 ) {
792 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
793 DWORD u;
794
795 FIXME("(%p,%08u,%u,%p): stub\n",This,dwFlags,dwEffectsCount,pdwResultCodes);
796
797 if (pdwResultCodes)
798 for (u=0; u<dwEffectsCount; u++) pdwResultCodes[u] = DSFXR_UNKNOWN;
799
800 WARN("control unavailable\n");
801 return DSERR_CONTROLUNAVAIL;
802 }
803
804 static HRESULT WINAPI IDirectSoundBufferImpl_GetObjectInPath(
805 LPDIRECTSOUNDBUFFER8 iface,REFGUID rguidObject,DWORD dwIndex,REFGUID rguidInterface,LPVOID* ppObject
806 ) {
807 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
808
809 FIXME("(%p,%s,%u,%s,%p): stub\n",This,debugstr_guid(rguidObject),dwIndex,debugstr_guid(rguidInterface),ppObject);
810
811 WARN("control unavailable\n");
812 return DSERR_CONTROLUNAVAIL;
813 }
814
815 static HRESULT WINAPI IDirectSoundBufferImpl_Initialize(
816 LPDIRECTSOUNDBUFFER8 iface,LPDIRECTSOUND dsound,LPCDSBUFFERDESC dbsd
817 ) {
818 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
819 WARN("(%p) already initialized\n", This);
820 return DSERR_ALREADYINITIALIZED;
821 }
822
823 static HRESULT WINAPI IDirectSoundBufferImpl_GetCaps(
824 LPDIRECTSOUNDBUFFER8 iface,LPDSBCAPS caps
825 ) {
826 IDirectSoundBufferImpl *This = (IDirectSoundBufferImpl *)iface;
827 TRACE("(%p)->(%p)\n",This,caps);
828
829 if (caps == NULL) {
830 WARN("invalid parameter: caps == NULL\n");
831 return DSERR_INVALIDPARAM;
832 }
833
834 if (caps->dwSize < sizeof(*caps)) {
835 WARN("invalid parameter: caps->dwSize = %d\n",caps->dwSize);
836 return DSERR_INVALIDPARAM;
837 }
838
839 caps->dwFlags = This->dsbd.dwFlags;
840 if (This->hwbuf) caps->dwFlags |= DSBCAPS_LOCHARDWARE;
841 else caps->dwFlags |= DSBCAPS_LOCSOFTWARE;
842
843 caps->dwBufferBytes = This->buflen;
844
845 /* According to windows, this is zero*/
846 caps->dwUnlockTransferRate = 0;
847 caps->dwPlayCpuOverhead = 0;
848