VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostDSound.cpp@ 88028

Last change on this file since 88028 was 88028, checked in by vboxsync, 4 years ago

Audio: Moving some of the DrvAudio.h stuff into PDM - VBox/vmm/pdmaudioinline.h. bugref:9890

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 91.5 KB
Line 
1/* $Id: DrvHostDSound.cpp 88028 2021-03-08 19:31:22Z vboxsync $ */
2/** @file
3 * Windows host backend driver using DirectSound.
4 */
5
6/*
7 * Copyright (C) 2006-2020 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18
19/*********************************************************************************************************************************
20* Header Files *
21*********************************************************************************************************************************/
22#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
23#include <VBox/log.h>
24#include <iprt/win/windows.h>
25#include <dsound.h>
26
27#include <iprt/alloc.h>
28#include <iprt/system.h>
29#include <iprt/uuid.h>
30#include <iprt/utf16.h>
31
32#include <VBox/vmm/pdmaudioinline.h>
33
34#include "DrvAudio.h"
35#include "VBoxDD.h"
36#ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
37# include <new> /* For bad_alloc. */
38# include "VBoxMMNotificationClient.h"
39#endif
40
41
42/*********************************************************************************************************************************
43* Defined Constants And Macros *
44*********************************************************************************************************************************/
45/*
46 * IDirectSound* interface uses HRESULT status codes and the driver callbacks use
47 * the IPRT status codes. To minimize HRESULT->IPRT conversion most internal functions
48 * in the driver return HRESULT and conversion is done in the driver callbacks.
49 *
50 * Naming convention:
51 * 'dsound*' functions return IPRT status code;
52 * 'directSound*' - return HRESULT.
53 */
54
55/*
56 * Optional release logging, which a user can turn on with the
57 * 'VBoxManage debugvm' command.
58 * Debug logging still uses the common Log* macros from IPRT.
59 * Messages which always should go to the release log use LogRel.
60 */
61/* General code behavior. */
62#define DSLOG(a) do { LogRel2(a); } while(0)
63/* Something which produce a lot of logging during playback/recording. */
64#define DSLOGF(a) do { LogRel3(a); } while(0)
65/* Important messages like errors. Limited in the default release log to avoid log flood. */
66#define DSLOGREL(a) \
67 do { \
68 static int8_t s_cLogged = 0; \
69 if (s_cLogged < 8) { \
70 ++s_cLogged; \
71 LogRel(a); \
72 } else DSLOG(a); \
73 } while (0)
74
75/** Maximum number of attempts to restore the sound buffer before giving up. */
76#define DRV_DSOUND_RESTORE_ATTEMPTS_MAX 3
77/** Default input latency (in ms). */
78#define DRV_DSOUND_DEFAULT_LATENCY_MS_IN 50
79/** Default output latency (in ms). */
80#define DRV_DSOUND_DEFAULT_LATENCY_MS_OUT 50
81
82/** Makes DRVHOSTDSOUND out of PDMIHOSTAUDIO. */
83#define PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface) \
84 ( (PDRVHOSTDSOUND)((uintptr_t)pInterface - RT_UOFFSETOF(DRVHOSTDSOUND, IHostAudio)) )
85
86
87/*********************************************************************************************************************************
88* Structures and Typedefs *
89*********************************************************************************************************************************/
90/* Dynamically load dsound.dll. */
91typedef HRESULT WINAPI FNDIRECTSOUNDENUMERATEW(LPDSENUMCALLBACKW pDSEnumCallback, PVOID pContext);
92typedef FNDIRECTSOUNDENUMERATEW *PFNDIRECTSOUNDENUMERATEW;
93typedef HRESULT WINAPI FNDIRECTSOUNDCAPTUREENUMERATEW(LPDSENUMCALLBACKW pDSEnumCallback, PVOID pContext);
94typedef FNDIRECTSOUNDCAPTUREENUMERATEW *PFNDIRECTSOUNDCAPTUREENUMERATEW;
95typedef HRESULT WINAPI FNDIRECTSOUNDCAPTURECREATE8(LPCGUID lpcGUID, LPDIRECTSOUNDCAPTURE8 *lplpDSC, LPUNKNOWN pUnkOuter);
96typedef FNDIRECTSOUNDCAPTURECREATE8 *PFNDIRECTSOUNDCAPTURECREATE8;
97
98#define VBOX_DSOUND_MAX_EVENTS 3
99
100typedef enum DSOUNDEVENT
101{
102 DSOUNDEVENT_NOTIFY = 0,
103 DSOUNDEVENT_INPUT,
104 DSOUNDEVENT_OUTPUT,
105} DSOUNDEVENT;
106
107typedef struct DSOUNDHOSTCFG
108{
109 RTUUID uuidPlay;
110 LPCGUID pGuidPlay;
111 RTUUID uuidCapture;
112 LPCGUID pGuidCapture;
113} DSOUNDHOSTCFG, *PDSOUNDHOSTCFG;
114
115typedef struct DSOUNDSTREAM
116{
117 /** The stream's acquired configuration. */
118 PDMAUDIOSTREAMCFG Cfg;
119 /** Buffer alignment. */
120 uint8_t uAlign;
121 /** Whether this stream is in an enable state on the DirectSound side. */
122 bool fEnabled;
123 bool afPadding[2];
124 /** Size (in bytes) of the DirectSound buffer.
125 * @note This in *not* the size of the circular buffer above! */
126 DWORD cbBufSize;
127 /** The stream's critical section for synchronizing access. */
128 RTCRITSECT CritSect;
129 /** The internal playback / capturing buffer. */
130 PRTCIRCBUF pCircBuf;
131 union
132 {
133 struct
134 {
135 /** The actual DirectSound Buffer (DSB) used for the capturing.
136 * This is a secondary buffer and is used as a streaming buffer. */
137 LPDIRECTSOUNDCAPTUREBUFFER8 pDSCB;
138 /** Current read offset (in bytes) within the DSB. */
139 DWORD offReadPos;
140 /** Number of buffer overruns happened. Used for logging. */
141 uint8_t cOverruns;
142 } In;
143 struct
144 {
145 /** The actual DirectSound Buffer (DSB) used for playback.
146 * This is a secondary buffer and is used as a streaming buffer. */
147 LPDIRECTSOUNDBUFFER8 pDSB;
148 /** Current write offset (in bytes) within the DSB. */
149 DWORD offWritePos;
150 /** Offset of last play cursor within the DSB when checked for pending. */
151 DWORD offPlayCursorLastPending;
152 /** Offset of last play cursor within the DSB when last played. */
153 DWORD offPlayCursorLastPlayed;
154 /** Total amount (in bytes) written to our internal ring buffer. */
155 uint64_t cbWritten;
156 /** Total amount (in bytes) played (to the DirectSound buffer). */
157 uint64_t cbTransferred;
158 /** Flag indicating whether playback was just (re)started. */
159 bool fFirstTransfer;
160 /** Flag indicating whether this stream is in draining mode, e.g. no new
161 * data is being written to it but DirectSound still needs to be able to
162 * play its remaining (buffered) data. */
163 bool fDrain;
164 /** How much (in bytes) the last transfer from the internal buffer
165 * to the DirectSound buffer was. */
166 uint32_t cbLastTransferred;
167 /** Timestamp (in ms) of the last transfer from the internal buffer
168 * to the DirectSound buffer. */
169 uint64_t tsLastTransferredMs;
170 /** Number of buffer underruns happened. Used for logging. */
171 uint8_t cUnderruns;
172 } Out;
173 };
174#ifdef LOG_ENABLED
175 struct
176 {
177 uint64_t tsLastTransferredMs;
178 } Dbg;
179#endif
180} DSOUNDSTREAM, *PDSOUNDSTREAM;
181
182/**
183 * Structure for keeping a DirectSound-specific device entry.
184 * This is then bound to the PDMAUDIODEVICE's pvData area.
185 */
186typedef struct DSOUNDDEV
187{
188 GUID Guid;
189} DSOUNDDEV, *PDSOUNDDEV;
190
191/**
192 * Structure for holding a device enumeration context.
193 */
194typedef struct DSOUNDENUMCBCTX
195{
196 /** Enumeration flags. */
197 uint32_t fFlags;
198 /** Pointer to device list to populate. */
199 PPDMAUDIODEVICEENUM pDevEnm;
200} DSOUNDENUMCBCTX, *PDSOUNDENUMCBCTX;
201
202typedef struct DRVHOSTDSOUND
203{
204 /** Pointer to the driver instance structure. */
205 PPDMDRVINS pDrvIns;
206 /** Our audio host audio interface. */
207 PDMIHOSTAUDIO IHostAudio;
208 /** Critical section to serialize access. */
209 RTCRITSECT CritSect;
210 /** DirectSound configuration options. */
211 DSOUNDHOSTCFG Cfg;
212 /** List of devices of last enumeration. */
213 PDMAUDIODEVICEENUM DeviceEnum;
214 /** Whether this backend supports any audio input. */
215 bool fEnabledIn;
216 /** Whether this backend supports any audio output. */
217 bool fEnabledOut;
218 /** The Direct Sound playback interface. */
219 LPDIRECTSOUND8 pDS;
220 /** The Direct Sound capturing interface. */
221 LPDIRECTSOUNDCAPTURE8 pDSC;
222#ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
223 VBoxMMNotificationClient *m_pNotificationClient;
224#endif
225#ifdef VBOX_WITH_AUDIO_CALLBACKS
226 /** Callback function to the upper driver.
227 * Can be NULL if not being used / registered. */
228 PFNPDMHOSTAUDIOCALLBACK pfnCallback;
229#endif
230 /** Pointer to the input stream. */
231 PDSOUNDSTREAM pDSStrmIn;
232 /** Pointer to the output stream. */
233 PDSOUNDSTREAM pDSStrmOut;
234} DRVHOSTDSOUND, *PDRVHOSTDSOUND;
235
236
237/*********************************************************************************************************************************
238* Internal Functions *
239*********************************************************************************************************************************/
240static HRESULT directSoundPlayRestore(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB);
241static HRESULT directSoundPlayStart(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS);
242static HRESULT directSoundPlayStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush);
243static HRESULT directSoundCaptureStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush);
244
245static int dsoundDevicesEnumerate(PDRVHOSTDSOUND pThis, PDMAUDIODEVICEENUM pDevEnm, uint32_t fEnum);
246
247static int dsoundStreamEnable(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fEnable);
248static void dsoundStreamReset(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS);
249static void dsoundUpdateStatusInternal(PDRVHOSTDSOUND pThis);
250
251
252static DWORD dsoundRingDistance(DWORD offEnd, DWORD offBegin, DWORD cSize)
253{
254 AssertReturn(offEnd <= cSize, 0);
255 AssertReturn(offBegin <= cSize, 0);
256
257 return offEnd >= offBegin ? offEnd - offBegin : cSize - offBegin + offEnd;
258}
259
260static int dsoundWaveFmtFromCfg(PPDMAUDIOSTREAMCFG pCfg, PWAVEFORMATEX pFmt)
261{
262 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
263 AssertPtrReturn(pFmt, VERR_INVALID_POINTER);
264
265 RT_BZERO(pFmt, sizeof(WAVEFORMATEX));
266
267 pFmt->wFormatTag = WAVE_FORMAT_PCM;
268 pFmt->nChannels = pCfg->Props.cChannels;
269 pFmt->wBitsPerSample = pCfg->Props.cbSample * 8;
270 pFmt->nSamplesPerSec = pCfg->Props.uHz;
271 pFmt->nBlockAlign = pFmt->nChannels * pFmt->wBitsPerSample / 8;
272 pFmt->nAvgBytesPerSec = pFmt->nSamplesPerSec * pFmt->nBlockAlign;
273 pFmt->cbSize = 0; /* No extra data specified. */
274
275 return VINF_SUCCESS;
276}
277
278/**
279 * Retrieves the number of free bytes available for writing to a DirectSound output stream.
280 *
281 * @return IPRT status code. VERR_NOT_AVAILABLE if unable to determine or the buffer was not recoverable.
282 * @param pThis Host audio driver instance.
283 * @param pStreamDS DirectSound output stream to retrieve number for.
284 * @param pdwFree Where to return the free amount on success.
285 */
286static int dsoundGetFreeOut(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, DWORD *pdwFree)
287{
288 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
289 AssertPtrReturn(pStreamDS, VERR_INVALID_POINTER);
290 AssertPtrReturn(pdwFree, VERR_INVALID_POINTER);
291
292 Assert(pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT); /* Paranoia. */
293
294 LPDIRECTSOUNDBUFFER8 pDSB = pStreamDS->Out.pDSB;
295 if (!pDSB)
296 {
297 AssertPtr(pDSB);
298 return VERR_INVALID_POINTER;
299 }
300
301 HRESULT hr = S_OK;
302
303 /* Get the current play position which is used for calculating the free space in the buffer. */
304 for (unsigned i = 0; i < DRV_DSOUND_RESTORE_ATTEMPTS_MAX; i++)
305 {
306 DWORD cbPlayCursor, cbWriteCursor;
307 hr = IDirectSoundBuffer8_GetCurrentPosition(pDSB, &cbPlayCursor, &cbWriteCursor);
308 if (SUCCEEDED(hr))
309 {
310 int32_t cbDiff = cbWriteCursor - cbPlayCursor;
311 if (cbDiff < 0)
312 cbDiff += pStreamDS->cbBufSize;
313
314 int32_t cbFree = cbPlayCursor - pStreamDS->Out.offWritePos;
315 if (cbFree < 0)
316 cbFree += pStreamDS->cbBufSize;
317
318 if (cbFree > (int32_t)pStreamDS->cbBufSize - cbDiff)
319 {
320 pStreamDS->Out.offWritePos = cbWriteCursor;
321 cbFree = pStreamDS->cbBufSize - cbDiff;
322 }
323
324 /* When starting to use a DirectSound buffer, cbPlayCursor and cbWriteCursor
325 * both point at position 0, so we won't be able to detect how many bytes
326 * are writable that way.
327 *
328 * So use our per-stream written indicator to see if we just started a stream. */
329 if (pStreamDS->Out.cbWritten == 0)
330 cbFree = pStreamDS->cbBufSize;
331
332 DSLOGREL(("DSound: cbPlayCursor=%RU32, cbWriteCursor=%RU32, offWritePos=%RU32 -> cbFree=%RI32\n",
333 cbPlayCursor, cbWriteCursor, pStreamDS->Out.offWritePos, cbFree));
334
335 *pdwFree = cbFree;
336
337 return VINF_SUCCESS;
338 }
339
340 if (hr != DSERR_BUFFERLOST) /** @todo MSDN doesn't state this error for GetCurrentPosition(). */
341 break;
342
343 LogFunc(("Getting playing position failed due to lost buffer, restoring ...\n"));
344
345 directSoundPlayRestore(pThis, pDSB);
346 }
347
348 if (hr != DSERR_BUFFERLOST) /* Avoid log flooding if the error is still there. */
349 DSLOGREL(("DSound: Getting current playback position failed with %Rhrc\n", hr));
350
351 LogFunc(("Failed with %Rhrc\n", hr));
352
353 return VERR_NOT_AVAILABLE;
354}
355
356static char *dsoundGUIDToUtf8StrA(LPCGUID pGUID)
357{
358 if (pGUID)
359 {
360 LPOLESTR lpOLEStr;
361 HRESULT hr = StringFromCLSID(*pGUID, &lpOLEStr);
362 if (SUCCEEDED(hr))
363 {
364 char *pszGUID;
365 int rc = RTUtf16ToUtf8(lpOLEStr, &pszGUID);
366 CoTaskMemFree(lpOLEStr);
367
368 return RT_SUCCESS(rc) ? pszGUID : NULL;
369 }
370 }
371
372 return RTStrDup("{Default device}");
373}
374
375static HRESULT directSoundPlayRestore(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB)
376{
377 RT_NOREF(pThis);
378 HRESULT hr = IDirectSoundBuffer8_Restore(pDSB);
379 if (FAILED(hr))
380 DSLOG(("DSound: Restoring playback buffer\n"));
381 else
382 DSLOGREL(("DSound: Restoring playback buffer failed with %Rhrc\n", hr));
383
384 return hr;
385}
386
387static HRESULT directSoundPlayUnlock(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB,
388 PVOID pv1, PVOID pv2,
389 DWORD cb1, DWORD cb2)
390{
391 RT_NOREF(pThis);
392 HRESULT hr = IDirectSoundBuffer8_Unlock(pDSB, pv1, cb1, pv2, cb2);
393 if (FAILED(hr))
394 DSLOGREL(("DSound: Unlocking playback buffer failed with %Rhrc\n", hr));
395 return hr;
396}
397
398static HRESULT directSoundCaptureUnlock(LPDIRECTSOUNDCAPTUREBUFFER8 pDSCB,
399 PVOID pv1, PVOID pv2,
400 DWORD cb1, DWORD cb2)
401{
402 HRESULT hr = IDirectSoundCaptureBuffer8_Unlock(pDSCB, pv1, cb1, pv2, cb2);
403 if (FAILED(hr))
404 DSLOGREL(("DSound: Unlocking capture buffer failed with %Rhrc\n", hr));
405 return hr;
406}
407
408static HRESULT directSoundPlayLock(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
409 DWORD dwOffset, DWORD dwBytes,
410 PVOID *ppv1, PVOID *ppv2,
411 DWORD *pcb1, DWORD *pcb2,
412 DWORD dwFlags)
413{
414 AssertReturn(dwBytes, VERR_INVALID_PARAMETER);
415
416 HRESULT hr = E_FAIL;
417 AssertCompile(DRV_DSOUND_RESTORE_ATTEMPTS_MAX > 0);
418 for (unsigned i = 0; i < DRV_DSOUND_RESTORE_ATTEMPTS_MAX; i++)
419 {
420 PVOID pv1, pv2;
421 DWORD cb1, cb2;
422 hr = IDirectSoundBuffer8_Lock(pStreamDS->Out.pDSB, dwOffset, dwBytes, &pv1, &cb1, &pv2, &cb2, dwFlags);
423 if (SUCCEEDED(hr))
424 {
425 if ( (!pv1 || !(cb1 & pStreamDS->uAlign))
426 && (!pv2 || !(cb2 & pStreamDS->uAlign)))
427 {
428 if (ppv1)
429 *ppv1 = pv1;
430 if (ppv2)
431 *ppv2 = pv2;
432 if (pcb1)
433 *pcb1 = cb1;
434 if (pcb2)
435 *pcb2 = cb2;
436 return S_OK;
437 }
438 DSLOGREL(("DSound: Locking playback buffer returned misaligned buffer: cb1=%#RX32, cb2=%#RX32 (alignment: %#RX32)\n",
439 *pcb1, *pcb2, pStreamDS->uAlign));
440 directSoundPlayUnlock(pThis, pStreamDS->Out.pDSB, pv1, pv2, cb1, cb2);
441 return E_FAIL;
442 }
443
444 if (hr != DSERR_BUFFERLOST)
445 break;
446
447 LogFlowFunc(("Locking failed due to lost buffer, restoring ...\n"));
448 directSoundPlayRestore(pThis, pStreamDS->Out.pDSB);
449 }
450
451 DSLOGREL(("DSound: Locking playback buffer failed with %Rhrc (dwOff=%ld, dwBytes=%ld)\n", hr, dwOffset, dwBytes));
452 return hr;
453}
454
455static HRESULT directSoundCaptureLock(PDSOUNDSTREAM pStreamDS,
456 DWORD dwOffset, DWORD dwBytes,
457 PVOID *ppv1, PVOID *ppv2,
458 DWORD *pcb1, DWORD *pcb2,
459 DWORD dwFlags)
460{
461 PVOID pv1 = NULL;
462 PVOID pv2 = NULL;
463 DWORD cb1 = 0;
464 DWORD cb2 = 0;
465
466 HRESULT hr = IDirectSoundCaptureBuffer8_Lock(pStreamDS->In.pDSCB, dwOffset, dwBytes,
467 &pv1, &cb1, &pv2, &cb2, dwFlags);
468 if (FAILED(hr))
469 {
470 DSLOGREL(("DSound: Locking capture buffer failed with %Rhrc\n", hr));
471 return hr;
472 }
473
474 if ( (pv1 && (cb1 & pStreamDS->uAlign))
475 || (pv2 && (cb2 & pStreamDS->uAlign)))
476 {
477 DSLOGREL(("DSound: Locking capture buffer returned misaligned buffer: cb1=%RI32, cb2=%RI32 (alignment: %RU32)\n",
478 cb1, cb2, pStreamDS->uAlign));
479 directSoundCaptureUnlock(pStreamDS->In.pDSCB, pv1, pv2, cb1, cb2);
480 return E_FAIL;
481 }
482
483 *ppv1 = pv1;
484 *ppv2 = pv2;
485 *pcb1 = cb1;
486 *pcb2 = cb2;
487
488 return S_OK;
489}
490
491/*
492 * DirectSound playback
493 */
494
495/**
496 * Destroys a DirectSound playback interface.
497 *
498 * @param pDS Playback interface to destroy.
499 */
500static void directSoundPlayInterfaceDestroy(LPDIRECTSOUND8 pDS)
501{
502 if (pDS)
503 {
504 LogFlowFuncEnter();
505
506 IDirectSound8_Release(pDS);
507 pDS = NULL;
508 }
509}
510
511/**
512 * Creates a DirectSound playback interface.
513 *
514 * @return HRESULT
515 * @param pGUID GUID of device to create the playback interface for.
516 * @param ppDS Where to store the created interface. Optional.
517 */
518static HRESULT directSoundPlayInterfaceCreate(LPCGUID pGUID, LPDIRECTSOUND8 *ppDS)
519{
520 /* pGUID can be NULL, if this is the default device. */
521 /* ppDS is optional. */
522
523 LogFlowFuncEnter();
524
525 LPDIRECTSOUND8 pDS;
526 HRESULT hr = CoCreateInstance(CLSID_DirectSound8, NULL, CLSCTX_ALL,
527 IID_IDirectSound8, (void **)&pDS);
528 if (FAILED(hr))
529 {
530 DSLOGREL(("DSound: Creating playback instance failed with %Rhrc\n", hr));
531 }
532 else
533 {
534 hr = IDirectSound8_Initialize(pDS, pGUID);
535 if (SUCCEEDED(hr))
536 {
537 HWND hWnd = GetDesktopWindow();
538 hr = IDirectSound8_SetCooperativeLevel(pDS, hWnd, DSSCL_PRIORITY);
539 if (FAILED(hr))
540 DSLOGREL(("DSound: Setting cooperative level for window %p failed with %Rhrc\n", hWnd, hr));
541 }
542
543 if (FAILED(hr))
544 {
545 if (hr == DSERR_NODRIVER) /* Usually means that no playback devices are attached. */
546 DSLOGREL(("DSound: DirectSound playback is currently unavailable\n"));
547 else
548 DSLOGREL(("DSound: DirectSound playback initialization failed with %Rhrc\n", hr));
549
550 directSoundPlayInterfaceDestroy(pDS);
551 }
552 else if (ppDS)
553 {
554 *ppDS = pDS;
555 }
556 }
557
558 LogFlowFunc(("Returning %Rhrc\n", hr));
559 return hr;
560}
561
562static HRESULT directSoundPlayClose(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
563{
564 AssertPtrReturn(pThis, E_POINTER);
565 AssertPtrReturn(pStreamDS, E_POINTER);
566
567 LogFlowFuncEnter();
568
569 HRESULT hr = directSoundPlayStop(pThis, pStreamDS, true /* fFlush */);
570 if (FAILED(hr))
571 return hr;
572
573 DSLOG(("DSound: Closing playback stream\n"));
574
575 if (pStreamDS->pCircBuf)
576 Assert(RTCircBufUsed(pStreamDS->pCircBuf) == 0);
577
578 if (SUCCEEDED(hr))
579 {
580 RTCritSectEnter(&pThis->CritSect);
581
582 if (pStreamDS->pCircBuf)
583 {
584 RTCircBufDestroy(pStreamDS->pCircBuf);
585 pStreamDS->pCircBuf = NULL;
586 }
587
588 if (pStreamDS->Out.pDSB)
589 {
590 IDirectSoundBuffer8_Release(pStreamDS->Out.pDSB);
591 pStreamDS->Out.pDSB = NULL;
592 }
593
594 pThis->pDSStrmOut = NULL;
595
596 RTCritSectLeave(&pThis->CritSect);
597 }
598
599 if (FAILED(hr))
600 DSLOGREL(("DSound: Stopping playback stream %p failed with %Rhrc\n", pStreamDS, hr));
601
602 return hr;
603}
604
605static HRESULT directSoundPlayOpen(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
606 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
607{
608 AssertPtrReturn(pThis, E_POINTER);
609 AssertPtrReturn(pStreamDS, E_POINTER);
610 AssertPtrReturn(pCfgReq, E_POINTER);
611 AssertPtrReturn(pCfgAcq, E_POINTER);
612
613 LogFlowFuncEnter();
614
615 Assert(pStreamDS->Out.pDSB == NULL);
616
617 DSLOG(("DSound: Opening playback stream (uHz=%RU32, cChannels=%RU8, cBits=%u, fSigned=%RTbool)\n",
618 pCfgReq->Props.uHz, pCfgReq->Props.cChannels, pCfgReq->Props.cbSample * 8, pCfgReq->Props.fSigned));
619
620 WAVEFORMATEX wfx;
621 int rc = dsoundWaveFmtFromCfg(pCfgReq, &wfx);
622 if (RT_FAILURE(rc))
623 return E_INVALIDARG;
624
625 DSLOG(("DSound: Requested playback format:\n"
626 " wFormatTag = %RI16\n"
627 " nChannels = %RI16\n"
628 " nSamplesPerSec = %RU32\n"
629 " nAvgBytesPerSec = %RU32\n"
630 " nBlockAlign = %RI16\n"
631 " wBitsPerSample = %RI16\n"
632 " cbSize = %RI16\n",
633 wfx.wFormatTag,
634 wfx.nChannels,
635 wfx.nSamplesPerSec,
636 wfx.nAvgBytesPerSec,
637 wfx.nBlockAlign,
638 wfx.wBitsPerSample,
639 wfx.cbSize));
640
641 dsoundUpdateStatusInternal(pThis);
642
643 HRESULT hr = directSoundPlayInterfaceCreate(pThis->Cfg.pGuidPlay, &pThis->pDS);
644 if (FAILED(hr))
645 return hr;
646
647 do /* To use breaks. */
648 {
649 LPDIRECTSOUNDBUFFER pDSB = NULL;
650
651 DSBUFFERDESC bd;
652 RT_ZERO(bd);
653 bd.dwSize = sizeof(bd);
654 bd.lpwfxFormat = &wfx;
655
656 /*
657 * As we reuse our (secondary) buffer for playing out data as it comes in,
658 * we're using this buffer as a so-called streaming buffer.
659 *
660 * See https://msdn.microsoft.com/en-us/library/windows/desktop/ee419014(v=vs.85).aspx
661 *
662 * However, as we do not want to use memory on the sound device directly
663 * (as most modern audio hardware on the host doesn't have this anyway),
664 * we're *not* going to use DSBCAPS_STATIC for that.
665 *
666 * Instead we're specifying DSBCAPS_LOCSOFTWARE, as this fits the bill
667 * of copying own buffer data to our secondary's Direct Sound buffer.
668 */
669 bd.dwFlags = DSBCAPS_GLOBALFOCUS | DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_LOCSOFTWARE;
670 bd.dwBufferBytes = PDMAudioPropsFramesToBytes(&pCfgReq->Props, pCfgReq->Backend.cFramesBufferSize);
671
672 DSLOG(("DSound: Requested playback buffer is %RU64ms (%ld bytes)\n",
673 PDMAudioPropsBytesToMilli(&pCfgReq->Props, bd.dwBufferBytes), bd.dwBufferBytes));
674
675 hr = IDirectSound8_CreateSoundBuffer(pThis->pDS, &bd, &pDSB, NULL);
676 if (FAILED(hr))
677 {
678 DSLOGREL(("DSound: Creating playback sound buffer failed with %Rhrc\n", hr));
679 break;
680 }
681
682 /* "Upgrade" to IDirectSoundBuffer8 interface. */
683 hr = IDirectSoundBuffer_QueryInterface(pDSB, IID_IDirectSoundBuffer8, (PVOID *)&pStreamDS->Out.pDSB);
684 IDirectSoundBuffer_Release(pDSB);
685 if (FAILED(hr))
686 {
687 DSLOGREL(("DSound: Querying playback sound buffer interface failed with %Rhrc\n", hr));
688 break;
689 }
690
691 /*
692 * Query the actual parameters set for this stream.
693 * Those might be different than the initially requested parameters.
694 */
695 RT_ZERO(wfx);
696 hr = IDirectSoundBuffer8_GetFormat(pStreamDS->Out.pDSB, &wfx, sizeof(wfx), NULL);
697 if (FAILED(hr))
698 {
699 DSLOGREL(("DSound: Getting playback format failed with %Rhrc\n", hr));
700 break;
701 }
702
703 DSBCAPS bc;
704 RT_ZERO(bc);
705 bc.dwSize = sizeof(bc);
706
707 hr = IDirectSoundBuffer8_GetCaps(pStreamDS->Out.pDSB, &bc);
708 if (FAILED(hr))
709 {
710 DSLOGREL(("DSound: Getting playback capabilities failed with %Rhrc\n", hr));
711 break;
712 }
713
714 DSLOG(("DSound: Acquired playback buffer is %RU64ms (%ld bytes)\n",
715 PDMAudioPropsBytesToMilli(&pCfgReq->Props, bc.dwBufferBytes), bc.dwBufferBytes));
716
717 DSLOG(("DSound: Acquired playback format:\n"
718 " dwBufferBytes = %RI32\n"
719 " dwFlags = 0x%x\n"
720 " wFormatTag = %RI16\n"
721 " nChannels = %RI16\n"
722 " nSamplesPerSec = %RU32\n"
723 " nAvgBytesPerSec = %RU32\n"
724 " nBlockAlign = %RI16\n"
725 " wBitsPerSample = %RI16\n"
726 " cbSize = %RI16\n",
727 bc.dwBufferBytes,
728 bc.dwFlags,
729 wfx.wFormatTag,
730 wfx.nChannels,
731 wfx.nSamplesPerSec,
732 wfx.nAvgBytesPerSec,
733 wfx.nBlockAlign,
734 wfx.wBitsPerSample,
735 wfx.cbSize));
736
737 if (bc.dwBufferBytes & pStreamDS->uAlign)
738 DSLOGREL(("DSound: Playback capabilities returned misaligned buffer: size %RU32, alignment %RU32\n",
739 bc.dwBufferBytes, pStreamDS->uAlign + 1));
740
741 /*
742 * Initial state.
743 * dsoundPlayStart initializes part of it to make sure that Stop/Start continues with a correct
744 * playback buffer position.
745 */
746 pStreamDS->cbBufSize = bc.dwBufferBytes;
747
748 rc = RTCircBufCreate(&pStreamDS->pCircBuf, pStreamDS->cbBufSize * 2 /* Use "double buffering" */);
749 AssertRC(rc);
750
751 pThis->pDSStrmOut = pStreamDS;
752
753 const uint32_t cfBufSize = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamDS->cbBufSize);
754
755 pCfgAcq->Backend.cFramesBufferSize = cfBufSize;
756 pCfgAcq->Backend.cFramesPeriod = cfBufSize / 4;
757 pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesPeriod * 2;
758
759 } while (0);
760
761 if (FAILED(hr))
762 directSoundPlayClose(pThis, pStreamDS);
763
764 return hr;
765}
766
767static void dsoundPlayClearBuffer(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
768{
769 AssertPtrReturnVoid(pStreamDS);
770
771 PPDMAUDIOPCMPROPS pProps = &pStreamDS->Cfg.Props;
772
773 HRESULT hr = IDirectSoundBuffer_SetCurrentPosition(pStreamDS->Out.pDSB, 0 /* Position */);
774 if (FAILED(hr))
775 DSLOGREL(("DSound: Setting current position to 0 when clearing buffer failed with %Rhrc\n", hr));
776
777 PVOID pv1;
778 hr = directSoundPlayLock(pThis, pStreamDS,
779 0 /* dwOffset */, pStreamDS->cbBufSize,
780 &pv1, NULL, 0, 0, DSBLOCK_ENTIREBUFFER);
781 if (SUCCEEDED(hr))
782 {
783 PDMAudioPropsClearBuffer(pProps, pv1, pStreamDS->cbBufSize, PDMAUDIOPCMPROPS_B2F(pProps, pStreamDS->cbBufSize));
784
785 directSoundPlayUnlock(pThis, pStreamDS->Out.pDSB, pv1, NULL, 0, 0);
786
787 /* Make sure to get the last playback position and current write position from DirectSound again.
788 * Those positions in theory could have changed, re-fetch them to be sure. */
789 hr = IDirectSoundBuffer_GetCurrentPosition(pStreamDS->Out.pDSB,
790 &pStreamDS->Out.offPlayCursorLastPlayed, &pStreamDS->Out.offWritePos);
791 if (FAILED(hr))
792 DSLOGREL(("DSound: Re-fetching current position when clearing buffer failed with %Rhrc\n", hr));
793 }
794}
795
796/**
797 * Transfers audio data from the internal buffer to the DirectSound playback instance.
798 * Due to internal accounting and querying DirectSound, this function knows how much it can transfer at once.
799 *
800 * @return IPRT status code.
801 * @param pThis Host audio driver instance.
802 * @param pStreamDS Stream to transfer playback data for.
803 */
804static int dsoundPlayTransfer(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
805{
806 if (!pStreamDS->fEnabled)
807 {
808 Log2Func(("Stream disabled, skipping\n"));
809 return VINF_SUCCESS;
810 }
811
812 uint32_t cbTransferred = 0;
813
814 PRTCIRCBUF pCircBuf = pStreamDS->pCircBuf;
815 AssertPtr(pCircBuf);
816
817 LPDIRECTSOUNDBUFFER8 pDSB = pStreamDS->Out.pDSB;
818 AssertPtr(pDSB);
819
820 int rc = VINF_SUCCESS;
821
822 DWORD offPlayCursor, offWriteCursor;
823 HRESULT hr = IDirectSoundBuffer8_GetCurrentPosition(pDSB, &offPlayCursor, &offWriteCursor);
824 if (FAILED(hr))
825 {
826 rc = VERR_ACCESS_DENIED; /** @todo Find a better rc. */
827 return rc;
828 }
829
830 DWORD cbFree, cbRemaining;
831 if (pStreamDS->Out.fFirstTransfer)
832 {
833 cbRemaining = 0;
834 cbFree = pStreamDS->cbBufSize;
835 }
836 else
837 {
838 cbFree = dsoundRingDistance(offPlayCursor, pStreamDS->Out.offWritePos, pStreamDS->cbBufSize);
839 cbRemaining = dsoundRingDistance(pStreamDS->Out.offWritePos, offPlayCursor, pStreamDS->cbBufSize);
840 }
841
842 uint32_t cbAvail = (uint32_t)RTCircBufUsed(pCircBuf);
843 uint32_t cbToTransfer = RT_MIN(cbFree, cbAvail);
844
845#ifdef LOG_ENABLED
846 if (!pStreamDS->Dbg.tsLastTransferredMs)
847 pStreamDS->Dbg.tsLastTransferredMs = RTTimeMilliTS();
848 Log3Func(("offPlay=%RU32, offWrite=%RU32, tsLastTransferredMs=%RU64ms, cbAvail=%RU32, cbFree=%RU32 -> cbToTransfer=%RU32 "
849 "(fFirst=%RTbool, fDrain=%RTbool)\n",
850 offPlayCursor, offWriteCursor, RTTimeMilliTS() - pStreamDS->Dbg.tsLastTransferredMs, cbAvail, cbFree, cbToTransfer,
851 pStreamDS->Out.fFirstTransfer, pStreamDS->Out.fDrain));
852 pStreamDS->Dbg.tsLastTransferredMs = RTTimeMilliTS();
853#endif
854
855 while (cbToTransfer)
856 {
857 DWORD cb1 = 0;
858 DWORD cb2 = 0;
859
860 void *pvBuf;
861 size_t cbBuf;
862 RTCircBufAcquireReadBlock(pCircBuf, cbToTransfer, &pvBuf, &cbBuf);
863
864 if (cbBuf)
865 {
866 PVOID pv1, pv2;
867 hr = directSoundPlayLock(pThis, pStreamDS, pStreamDS->Out.offWritePos, (DWORD)cbBuf,
868 &pv1, &pv2, &cb1, &cb2, 0 /* dwFlags */);
869 if (FAILED(hr))
870 {
871 rc = VERR_ACCESS_DENIED;
872 break;
873 }
874
875 AssertPtr(pv1);
876 Assert(cb1);
877
878 memcpy(pv1, pvBuf, cb1);
879
880 if (pv2 && cb2) /* Buffer wrap-around? Write second part. */
881 memcpy(pv2, (uint8_t *)pvBuf + cb1, cb2);
882
883 directSoundPlayUnlock(pThis, pDSB, pv1, pv2, cb1, cb2);
884
885 pStreamDS->Out.offWritePos = (pStreamDS->Out.offWritePos + cb1 + cb2) % pStreamDS->cbBufSize;
886
887 Assert(cbToTransfer >= cbBuf);
888 cbToTransfer -= (uint32_t)cbBuf;
889
890 cbTransferred += cb1 + cb2;
891 }
892
893 RTCircBufReleaseReadBlock(pCircBuf, cb1 + cb2);
894 }
895
896 pStreamDS->Out.cbTransferred += cbTransferred;
897
898 if ( pStreamDS->Out.fFirstTransfer
899 && pStreamDS->Out.cbTransferred >= PDMAudioPropsFramesToBytes(&pStreamDS->Cfg.Props, pStreamDS->Cfg.Backend.cFramesPreBuffering))
900 {
901 hr = directSoundPlayStart(pThis, pStreamDS);
902 if (SUCCEEDED(hr))
903 {
904 pStreamDS->Out.fFirstTransfer = false;
905 }
906 else
907 rc = VERR_ACCESS_DENIED; /** @todo Find a better rc. */
908 }
909
910 cbAvail = (uint32_t)RTCircBufUsed(pCircBuf);
911 if ( !cbAvail
912 && cbTransferred)
913 {
914 pStreamDS->Out.cbLastTransferred = cbTransferred;
915 pStreamDS->Out.tsLastTransferredMs = RTTimeMilliTS();
916
917 LogFlowFunc(("cbLastTransferred=%RU32, tsLastTransferredMs=%RU64\n",
918 pStreamDS->Out.cbLastTransferred, pStreamDS->Out.tsLastTransferredMs));
919 }
920
921 LogFlowFunc(("cbTransferred=%RU32, cbAvail=%RU32, rc=%Rrc\n", cbTransferred, cbAvail, rc));
922 return rc;
923}
924
925static HRESULT directSoundPlayGetStatus(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB, DWORD *pdwStatus)
926{
927 AssertPtrReturn(pThis, E_POINTER);
928 AssertPtrReturn(pDSB, E_POINTER);
929
930 AssertPtrNull(pdwStatus);
931
932 DWORD dwStatus = 0;
933 HRESULT hr = E_FAIL;
934 for (unsigned i = 0; i < DRV_DSOUND_RESTORE_ATTEMPTS_MAX; i++)
935 {
936 hr = IDirectSoundBuffer8_GetStatus(pDSB, &dwStatus);
937 if ( hr == DSERR_BUFFERLOST
938 || ( SUCCEEDED(hr)
939 && (dwStatus & DSBSTATUS_BUFFERLOST)))
940 {
941 LogFlowFunc(("Getting status failed due to lost buffer, restoring ...\n"));
942 directSoundPlayRestore(pThis, pDSB);
943 }
944 else
945 break;
946 }
947
948 if (SUCCEEDED(hr))
949 {
950 if (pdwStatus)
951 *pdwStatus = dwStatus;
952 }
953 else
954 DSLOGREL(("DSound: Retrieving playback status failed with %Rhrc\n", hr));
955
956 return hr;
957}
958
959static HRESULT directSoundPlayStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush)
960{
961 AssertPtrReturn(pThis, E_POINTER);
962 AssertPtrReturn(pStreamDS, E_POINTER);
963
964 HRESULT hr = S_OK;
965
966 if (pStreamDS->Out.pDSB)
967 {
968 DSLOG(("DSound: Stopping playback\n"));
969 hr = IDirectSoundBuffer8_Stop(pStreamDS->Out.pDSB);
970 if (FAILED(hr))
971 {
972 hr = directSoundPlayRestore(pThis, pStreamDS->Out.pDSB);
973 if (FAILED(hr))
974 hr = IDirectSoundBuffer8_Stop(pStreamDS->Out.pDSB);
975 }
976 }
977
978 if (SUCCEEDED(hr))
979 {
980 if (fFlush)
981 dsoundStreamReset(pThis, pStreamDS);
982 }
983
984 if (FAILED(hr))
985 DSLOGREL(("DSound: %s playback failed with %Rhrc\n", fFlush ? "Stopping" : "Pausing", hr));
986
987 return hr;
988}
989
990/**
991 * Enables or disables a stream.
992 *
993 * @return IPRT status code.
994 * @param pThis Host audio driver instance.
995 * @param pStreamDS Stream to enable / disable.
996 * @param fEnable Whether to enable or disable the stream.
997 */
998static int dsoundStreamEnable(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fEnable)
999{
1000 RT_NOREF(pThis);
1001
1002 LogFunc(("%s %s\n",
1003 fEnable ? "Enabling" : "Disabling",
1004 pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN ? "capture" : "playback"));
1005
1006 if (fEnable)
1007 dsoundStreamReset(pThis, pStreamDS);
1008
1009 pStreamDS->fEnabled = fEnable;
1010
1011 return VINF_SUCCESS;
1012}
1013
1014
1015/**
1016 * Resets the state of a DirectSound stream.
1017 *
1018 * @param pThis Host audio driver instance.
1019 * @param pStreamDS Stream to reset state for.
1020 */
1021static void dsoundStreamReset(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
1022{
1023 RT_NOREF(pThis);
1024
1025 LogFunc(("Resetting %s\n",
1026 pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN ? "capture" : "playback"));
1027
1028 if (pStreamDS->pCircBuf)
1029 RTCircBufReset(pStreamDS->pCircBuf);
1030
1031 if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
1032 {
1033 pStreamDS->In.offReadPos = 0;
1034 pStreamDS->In.cOverruns = 0;
1035
1036 /* Also reset the DirectSound Capture Buffer (DSCB) by clearing all data to make sure
1037 * not stale audio data is left. */
1038 if (pStreamDS->In.pDSCB)
1039 {
1040 PVOID pv1; PVOID pv2; DWORD cb1; DWORD cb2;
1041 HRESULT hr = directSoundCaptureLock(pStreamDS, 0 /* Offset */, pStreamDS->cbBufSize, &pv1, &pv2, &cb1, &cb2,
1042 0 /* Flags */);
1043 if (SUCCEEDED(hr))
1044 {
1045 PDMAudioPropsClearBuffer(&pStreamDS->Cfg.Props, pv1, cb1, PDMAUDIOPCMPROPS_B2F(&pStreamDS->Cfg.Props, cb1));
1046 if (pv2 && cb2)
1047 PDMAudioPropsClearBuffer(&pStreamDS->Cfg.Props, pv2, cb2, PDMAUDIOPCMPROPS_B2F(&pStreamDS->Cfg.Props, cb2));
1048 directSoundCaptureUnlock(pStreamDS->In.pDSCB, pv1, pv2, cb1, cb2);
1049 }
1050 }
1051 }
1052 else if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT)
1053 {
1054 pStreamDS->Out.fFirstTransfer = true;
1055 pStreamDS->Out.fDrain = false;
1056 pStreamDS->Out.cUnderruns = 0;
1057
1058 pStreamDS->Out.cbLastTransferred = 0;
1059 pStreamDS->Out.tsLastTransferredMs = 0;
1060
1061 pStreamDS->Out.cbTransferred = 0;
1062 pStreamDS->Out.cbWritten = 0;
1063
1064 pStreamDS->Out.offWritePos = 0;
1065 pStreamDS->Out.offPlayCursorLastPending = 0;
1066 pStreamDS->Out.offPlayCursorLastPlayed = 0;
1067
1068 /* Also reset the DirectSound Buffer (DSB) by setting the position to 0 and clear all data to make sure
1069 * not stale audio data is left. */
1070 if (pStreamDS->Out.pDSB)
1071 {
1072 HRESULT hr = IDirectSoundBuffer8_SetCurrentPosition(pStreamDS->Out.pDSB, 0);
1073 if (SUCCEEDED(hr))
1074 {
1075 PVOID pv1; PVOID pv2; DWORD cb1; DWORD cb2;
1076 hr = directSoundPlayLock(pThis, pStreamDS, 0 /* Offset */, pStreamDS->cbBufSize, &pv1, &pv2, &cb1, &cb2,
1077 0 /* Flags */);
1078 if (SUCCEEDED(hr))
1079 {
1080 PDMAudioPropsClearBuffer(&pStreamDS->Cfg.Props, pv1, cb1, PDMAUDIOPCMPROPS_B2F(&pStreamDS->Cfg.Props, cb1));
1081 if (pv2 && cb2)
1082 PDMAudioPropsClearBuffer(&pStreamDS->Cfg.Props, pv2, cb2, PDMAUDIOPCMPROPS_B2F(&pStreamDS->Cfg.Props, cb2));
1083 directSoundPlayUnlock(pThis, pStreamDS->Out.pDSB, pv1, pv2, cb1, cb2);
1084 }
1085 }
1086 }
1087 }
1088
1089#ifdef LOG_ENABLED
1090 pStreamDS->Dbg.tsLastTransferredMs = 0;
1091#endif
1092}
1093
1094
1095/**
1096 * Starts playing a DirectSound stream.
1097 *
1098 * @return HRESULT
1099 * @param pThis Host audio driver instance.
1100 * @param pStreamDS Stream to start playing.
1101 */
1102static HRESULT directSoundPlayStart(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
1103{
1104 HRESULT hr = S_OK;
1105
1106 DWORD fFlags = DSCBSTART_LOOPING;
1107
1108 for (unsigned i = 0; i < DRV_DSOUND_RESTORE_ATTEMPTS_MAX; i++)
1109 {
1110 DSLOG(("DSound: Starting playback\n"));
1111 hr = IDirectSoundBuffer8_Play(pStreamDS->Out.pDSB, 0, 0, fFlags);
1112 if ( SUCCEEDED(hr)
1113 || hr != DSERR_BUFFERLOST)
1114 break;
1115 else
1116 {
1117 LogFunc(("Restarting playback failed due to lost buffer, restoring ...\n"));
1118 directSoundPlayRestore(pThis, pStreamDS->Out.pDSB);
1119 }
1120 }
1121
1122 return hr;
1123}
1124
1125
1126/*
1127 * DirectSoundCapture
1128 */
1129
1130static LPCGUID dsoundCaptureSelectDevice(PDRVHOSTDSOUND pThis, PPDMAUDIOSTREAMCFG pCfg)
1131{
1132 AssertPtrReturn(pThis, NULL);
1133 AssertPtrReturn(pCfg, NULL);
1134
1135 int rc = VINF_SUCCESS;
1136
1137 LPCGUID pGUID = pThis->Cfg.pGuidCapture;
1138 if (!pGUID)
1139 {
1140 PPDMAUDIODEVICE pDev = NULL;
1141
1142 switch (pCfg->u.enmSrc)
1143 {
1144 case PDMAUDIORECSRC_LINE:
1145 /*
1146 * At the moment we're only supporting line-in in the HDA emulation,
1147 * and line-in + mic-in in the AC'97 emulation both are expected
1148 * to use the host's mic-in as well.
1149 *
1150 * So the fall through here is intentional for now.
1151 */
1152 case PDMAUDIORECSRC_MIC:
1153 {
1154 pDev = DrvAudioHlpDeviceEnumGetDefaultDevice(&pThis->DeviceEnum, PDMAUDIODIR_IN);
1155 break;
1156 }
1157
1158 default:
1159 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
1160 break;
1161 }
1162
1163 if ( RT_SUCCESS(rc)
1164 && pDev)
1165 {
1166 DSLOG(("DSound: Guest source '%s' is using host recording device '%s'\n",
1167 PDMAudioRecSrcGetName(pCfg->u.enmSrc), pDev->szName));
1168
1169 PDSOUNDDEV pDSoundDev = (PDSOUNDDEV)pDev->pvData;
1170 AssertPtr(pDSoundDev);
1171
1172 pGUID = &pDSoundDev->Guid;
1173 }
1174 }
1175
1176 if (RT_FAILURE(rc))
1177 {
1178 LogRel(("DSound: Selecting recording device failed with %Rrc\n", rc));
1179 return NULL;
1180 }
1181
1182 char *pszGUID = dsoundGUIDToUtf8StrA(pGUID);
1183
1184 /* This always has to be in the release log. */
1185 LogRel(("DSound: Guest source '%s' is using host recording device with GUID '%s'\n",
1186 PDMAudioRecSrcGetName(pCfg->u.enmSrc), pszGUID ? pszGUID: "{?}"));
1187
1188 if (pszGUID)
1189 {
1190 RTStrFree(pszGUID);
1191 pszGUID = NULL;
1192 }
1193
1194 return pGUID;
1195}
1196
1197/**
1198 * Transfers audio data from the DirectSound capture instance to the internal buffer.
1199 * Due to internal accounting and querying DirectSound, this function knows how much it can transfer at once.
1200 *
1201 * @return IPRT status code.
1202 * @param pThis Host audio driver instance.
1203 * @param pStreamDS Stream to capture audio data for.
1204 */
1205static int dsoundCaptureTransfer(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
1206{
1207 RT_NOREF(pThis);
1208
1209 LPDIRECTSOUNDCAPTUREBUFFER8 pDSCB = pStreamDS->In.pDSCB;
1210 AssertPtr(pDSCB);
1211
1212 DWORD offCaptureCursor;
1213 HRESULT hr = IDirectSoundCaptureBuffer_GetCurrentPosition(pDSCB, NULL, &offCaptureCursor);
1214 if (FAILED(hr))
1215 {
1216 AssertFailed();
1217 return VERR_ACCESS_DENIED; /** @todo Find a better rc. */
1218 }
1219
1220 DWORD cbUsed = dsoundRingDistance(offCaptureCursor, pStreamDS->In.offReadPos, pStreamDS->cbBufSize);
1221
1222 PRTCIRCBUF pCircBuf = pStreamDS->pCircBuf;
1223 AssertPtr(pCircBuf);
1224
1225 uint32_t cbFree = (uint32_t)RTCircBufFree(pCircBuf);
1226 if ( !cbFree
1227 && pStreamDS->In.cOverruns < 32) /** @todo Make this configurable. */
1228 {
1229 DSLOG(("DSound: Warning: Internal buffer full (size is %zu bytes), skipping to record data (overflow #%RU32)\n",
1230 RTCircBufSize(pCircBuf), pStreamDS->In.cOverruns));
1231 DSLOG(("DSound: Warning: DSound capture buffer currently uses %RU32/%RU32 bytes\n", cbUsed, pStreamDS->cbBufSize));
1232 pStreamDS->In.cOverruns++;
1233 }
1234
1235 DWORD cbToCapture = RT_MIN(cbUsed, cbFree);
1236
1237 Log3Func(("cbUsed=%ld, cbToCapture=%ld\n", cbUsed, cbToCapture));
1238
1239 while (cbToCapture)
1240 {
1241 void *pvBuf;
1242 size_t cbBuf;
1243 RTCircBufAcquireWriteBlock(pCircBuf, cbToCapture, &pvBuf, &cbBuf);
1244
1245 if (cbBuf)
1246 {
1247 PVOID pv1, pv2;
1248 DWORD cb1, cb2;
1249 hr = directSoundCaptureLock(pStreamDS, pStreamDS->In.offReadPos, (DWORD)cbBuf,
1250 &pv1, &pv2, &cb1, &cb2, 0 /* dwFlags */);
1251 if (FAILED(hr))
1252 break;
1253
1254 AssertPtr(pv1);
1255 Assert(cb1);
1256
1257 memcpy(pvBuf, pv1, cb1);
1258
1259 if (pv2 && cb2) /* Buffer wrap-around? Write second part. */
1260 memcpy((uint8_t *)pvBuf + cb1, pv2, cb2);
1261
1262 directSoundCaptureUnlock(pDSCB, pv1, pv2, cb1, cb2);
1263
1264 pStreamDS->In.offReadPos = (pStreamDS->In.offReadPos + cb1 + cb2) % pStreamDS->cbBufSize;
1265
1266 Assert(cbToCapture >= cbBuf);
1267 cbToCapture -= (uint32_t)cbBuf;
1268 }
1269
1270#ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
1271 if (cbBuf)
1272 {
1273 RTFILE fh;
1274 int rc2 = RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "dsoundCapture.pcm",
1275 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
1276 if (RT_SUCCESS(rc2))
1277 {
1278 RTFileWrite(fh, pvBuf, cbBuf, NULL);
1279 RTFileClose(fh);
1280 }
1281 }
1282#endif
1283 RTCircBufReleaseWriteBlock(pCircBuf, cbBuf);
1284 }
1285
1286 return VINF_SUCCESS;
1287}
1288
1289/**
1290 * Destroys a DirectSound capture interface.
1291 *
1292 * @param pDSC Capture interface to destroy.
1293 */
1294static void directSoundCaptureInterfaceDestroy(LPDIRECTSOUNDCAPTURE8 pDSC)
1295{
1296 if (pDSC)
1297 {
1298 LogFlowFuncEnter();
1299
1300 IDirectSoundCapture_Release(pDSC);
1301 pDSC = NULL;
1302 }
1303}
1304
1305/**
1306 * Creates a DirectSound capture interface.
1307 *
1308 * @return HRESULT
1309 * @param pGUID GUID of device to create the capture interface for.
1310 * @param ppDSC Where to store the created interface. Optional.
1311 */
1312static HRESULT directSoundCaptureInterfaceCreate(LPCGUID pGUID, LPDIRECTSOUNDCAPTURE8 *ppDSC)
1313{
1314 /* pGUID can be NULL, if this is the default device. */
1315 /* ppDSC is optional. */
1316
1317 LogFlowFuncEnter();
1318
1319 LPDIRECTSOUNDCAPTURE8 pDSC;
1320 HRESULT hr = CoCreateInstance(CLSID_DirectSoundCapture8, NULL, CLSCTX_ALL,
1321 IID_IDirectSoundCapture8, (void **)&pDSC);
1322 if (FAILED(hr))
1323 {
1324 DSLOGREL(("DSound: Creating capture instance failed with %Rhrc\n", hr));
1325 }
1326 else
1327 {
1328 hr = IDirectSoundCapture_Initialize(pDSC, pGUID);
1329 if (FAILED(hr))
1330 {
1331 if (hr == DSERR_NODRIVER) /* Usually means that no capture devices are attached. */
1332 DSLOGREL(("DSound: Capture device currently is unavailable\n"));
1333 else
1334 DSLOGREL(("DSound: Initializing capturing device failed with %Rhrc\n", hr));
1335
1336 directSoundCaptureInterfaceDestroy(pDSC);
1337 }
1338 else if (ppDSC)
1339 {
1340 *ppDSC = pDSC;
1341 }
1342 }
1343
1344 LogFlowFunc(("Returning %Rhrc\n", hr));
1345 return hr;
1346}
1347
1348static HRESULT directSoundCaptureClose(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
1349{
1350 AssertPtrReturn(pThis, E_POINTER);
1351 AssertPtrReturn(pStreamDS, E_POINTER);
1352
1353 LogFlowFuncEnter();
1354
1355 HRESULT hr = directSoundCaptureStop(pThis, pStreamDS, true /* fFlush */);
1356 if (FAILED(hr))
1357 return hr;
1358
1359 if ( pStreamDS
1360 && pStreamDS->In.pDSCB)
1361 {
1362 DSLOG(("DSound: Closing capturing stream\n"));
1363
1364 IDirectSoundCaptureBuffer8_Release(pStreamDS->In.pDSCB);
1365 pStreamDS->In.pDSCB = NULL;
1366 }
1367
1368 LogFlowFunc(("Returning %Rhrc\n", hr));
1369 return hr;
1370}
1371
1372static HRESULT directSoundCaptureOpen(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
1373 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
1374{
1375 AssertPtrReturn(pThis, E_POINTER);
1376 AssertPtrReturn(pStreamDS, E_POINTER);
1377 AssertPtrReturn(pCfgReq, E_POINTER);
1378 AssertPtrReturn(pCfgAcq, E_POINTER);
1379
1380 LogFlowFuncEnter();
1381
1382 Assert(pStreamDS->In.pDSCB == NULL);
1383
1384 DSLOG(("DSound: Opening capturing stream (uHz=%RU32, cChannels=%RU8, cBits=%u, fSigned=%RTbool)\n",
1385 pCfgReq->Props.uHz, pCfgReq->Props.cChannels, pCfgReq->Props.cbSample * 8, pCfgReq->Props.fSigned));
1386
1387 WAVEFORMATEX wfx;
1388 int rc = dsoundWaveFmtFromCfg(pCfgReq, &wfx);
1389 if (RT_FAILURE(rc))
1390 return E_INVALIDARG;
1391
1392 dsoundUpdateStatusInternal(pThis);
1393
1394 HRESULT hr = directSoundCaptureInterfaceCreate(pThis->Cfg.pGuidCapture, &pThis->pDSC);
1395 if (FAILED(hr))
1396 return hr;
1397
1398 do /* To use breaks. */
1399 {
1400 DSCBUFFERDESC bd;
1401 RT_ZERO(bd);
1402
1403 bd.dwSize = sizeof(bd);
1404 bd.lpwfxFormat = &wfx;
1405 bd.dwBufferBytes = PDMAudioPropsFramesToBytes(&pCfgReq->Props, pCfgReq->Backend.cFramesBufferSize);
1406
1407 DSLOG(("DSound: Requested capture buffer is %RU64ms (%ld bytes)\n",
1408 PDMAudioPropsBytesToMilli(&pCfgReq->Props, bd.dwBufferBytes), bd.dwBufferBytes));
1409
1410 LPDIRECTSOUNDCAPTUREBUFFER pDSCB;
1411 hr = IDirectSoundCapture_CreateCaptureBuffer(pThis->pDSC, &bd, &pDSCB, NULL);
1412 if (FAILED(hr))
1413 {
1414 if (hr == E_ACCESSDENIED)
1415 {
1416 DSLOGREL(("DSound: Capturing input from host not possible, access denied\n"));
1417 }
1418 else
1419 DSLOGREL(("DSound: Creating capture buffer failed with %Rhrc\n", hr));
1420 break;
1421 }
1422
1423 hr = IDirectSoundCaptureBuffer_QueryInterface(pDSCB, IID_IDirectSoundCaptureBuffer8, (void **)&pStreamDS->In.pDSCB);
1424 IDirectSoundCaptureBuffer_Release(pDSCB);
1425 if (FAILED(hr))
1426 {
1427 DSLOGREL(("DSound: Querying interface for capture buffer failed with %Rhrc\n", hr));
1428 break;
1429 }
1430
1431 /*
1432 * Query the actual parameters.
1433 */
1434 DWORD offByteReadPos = 0;
1435 hr = IDirectSoundCaptureBuffer8_GetCurrentPosition(pStreamDS->In.pDSCB, NULL, &offByteReadPos);
1436 if (FAILED(hr))
1437 {
1438 offByteReadPos = 0;
1439 DSLOGREL(("DSound: Getting capture position failed with %Rhrc\n", hr));
1440 }
1441
1442 RT_ZERO(wfx);
1443 hr = IDirectSoundCaptureBuffer8_GetFormat(pStreamDS->In.pDSCB, &wfx, sizeof(wfx), NULL);
1444 if (FAILED(hr))
1445 {
1446 DSLOGREL(("DSound: Getting capture format failed with %Rhrc\n", hr));
1447 break;
1448 }
1449
1450 DSCBCAPS bc;
1451 RT_ZERO(bc);
1452 bc.dwSize = sizeof(bc);
1453 hr = IDirectSoundCaptureBuffer8_GetCaps(pStreamDS->In.pDSCB, &bc);
1454 if (FAILED(hr))
1455 {
1456 DSLOGREL(("DSound: Getting capture capabilities failed with %Rhrc\n", hr));
1457 break;
1458 }
1459
1460 DSLOG(("DSound: Acquired capture buffer is %RU64ms (%ld bytes)\n",
1461 PDMAudioPropsBytesToMilli(&pCfgReq->Props, bc.dwBufferBytes), bc.dwBufferBytes));
1462
1463 DSLOG(("DSound: Capture format:\n"
1464 " dwBufferBytes = %RI32\n"
1465 " dwFlags = 0x%x\n"
1466 " wFormatTag = %RI16\n"
1467 " nChannels = %RI16\n"
1468 " nSamplesPerSec = %RU32\n"
1469 " nAvgBytesPerSec = %RU32\n"
1470 " nBlockAlign = %RI16\n"
1471 " wBitsPerSample = %RI16\n"
1472 " cbSize = %RI16\n",
1473 bc.dwBufferBytes,
1474 bc.dwFlags,
1475 wfx.wFormatTag,
1476 wfx.nChannels,
1477 wfx.nSamplesPerSec,
1478 wfx.nAvgBytesPerSec,
1479 wfx.nBlockAlign,
1480 wfx.wBitsPerSample,
1481 wfx.cbSize));
1482
1483 if (bc.dwBufferBytes & pStreamDS->uAlign)
1484 DSLOGREL(("DSound: Capture GetCaps returned misaligned buffer: size %RU32, alignment %RU32\n",
1485 bc.dwBufferBytes, pStreamDS->uAlign + 1));
1486
1487 /* Initial state: reading at the initial capture position, no error. */
1488 pStreamDS->In.offReadPos = 0;
1489 pStreamDS->cbBufSize = bc.dwBufferBytes;
1490
1491 rc = RTCircBufCreate(&pStreamDS->pCircBuf, pStreamDS->cbBufSize * 2 /* Use "double buffering" */);
1492 AssertRC(rc);
1493
1494 pThis->pDSStrmIn = pStreamDS;
1495
1496 pCfgAcq->Backend.cFramesBufferSize = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamDS->cbBufSize);
1497
1498 } while (0);
1499
1500 if (FAILED(hr))
1501 directSoundCaptureClose(pThis, pStreamDS);
1502
1503 LogFlowFunc(("Returning %Rhrc\n", hr));
1504 return hr;
1505}
1506
1507static HRESULT directSoundCaptureStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush)
1508{
1509 AssertPtrReturn(pThis, E_POINTER);
1510 AssertPtrReturn(pStreamDS, E_POINTER);
1511
1512 RT_NOREF(pThis);
1513
1514 HRESULT hr = S_OK;
1515
1516 if (pStreamDS->In.pDSCB)
1517 {
1518 DSLOG(("DSound: Stopping capture\n"));
1519 hr = IDirectSoundCaptureBuffer_Stop(pStreamDS->In.pDSCB);
1520 }
1521
1522 if (SUCCEEDED(hr))
1523 {
1524 if (fFlush)
1525 dsoundStreamReset(pThis, pStreamDS);
1526 }
1527
1528 if (FAILED(hr))
1529 DSLOGREL(("DSound: Stopping capture buffer failed with %Rhrc\n", hr));
1530
1531 return hr;
1532}
1533
1534static HRESULT directSoundCaptureStart(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
1535{
1536 AssertPtrReturn(pThis, E_POINTER);
1537 AssertPtrReturn(pStreamDS, E_POINTER);
1538
1539 HRESULT hr;
1540 if (pStreamDS->In.pDSCB)
1541 {
1542 DWORD dwStatus;
1543 hr = IDirectSoundCaptureBuffer8_GetStatus(pStreamDS->In.pDSCB, &dwStatus);
1544 if (FAILED(hr))
1545 {
1546 DSLOGREL(("DSound: Retrieving capture status failed with %Rhrc\n", hr));
1547 }
1548 else
1549 {
1550 if (dwStatus & DSCBSTATUS_CAPTURING)
1551 {
1552 DSLOG(("DSound: Already capturing\n"));
1553 }
1554 else
1555 {
1556 const DWORD fFlags = DSCBSTART_LOOPING;
1557
1558 DSLOG(("DSound: Starting to capture\n"));
1559 hr = IDirectSoundCaptureBuffer8_Start(pStreamDS->In.pDSCB, fFlags);
1560 if (FAILED(hr))
1561 DSLOGREL(("DSound: Starting to capture failed with %Rhrc\n", hr));
1562 }
1563 }
1564 }
1565 else
1566 hr = E_UNEXPECTED;
1567
1568 LogFlowFunc(("Returning %Rhrc\n", hr));
1569 return hr;
1570}
1571
1572/**
1573 * Callback for the playback device enumeration.
1574 *
1575 * @return TRUE if continuing enumeration, FALSE if not.
1576 * @param pGUID Pointer to GUID of enumerated device. Can be NULL.
1577 * @param pwszDescription Pointer to (friendly) description of enumerated device.
1578 * @param pwszModule Pointer to module name of enumerated device.
1579 * @param lpContext Pointer to PDSOUNDENUMCBCTX context for storing the enumerated information.
1580 */
1581static BOOL CALLBACK dsoundDevicesEnumCbPlayback(LPGUID pGUID, LPCWSTR pwszDescription, LPCWSTR pwszModule, PVOID lpContext)
1582{
1583 RT_NOREF(pwszModule);
1584
1585 PDSOUNDENUMCBCTX pEnumCtx = (PDSOUNDENUMCBCTX)lpContext;
1586 AssertPtrReturn(pEnumCtx , FALSE);
1587
1588 PPDMAUDIODEVICEENUM pDevEnm = pEnumCtx->pDevEnm;
1589 AssertPtrReturn(pDevEnm, FALSE);
1590
1591 /* pGUID can be NULL for default device(s). */
1592 AssertPtrReturn(pwszDescription, FALSE);
1593 /* Do not care about pwszModule. */
1594
1595 int rc;
1596
1597 PPDMAUDIODEVICE pDev = DrvAudioHlpDeviceAlloc(sizeof(DSOUNDDEV));
1598 if (pDev)
1599 {
1600 pDev->enmUsage = PDMAUDIODIR_OUT;
1601 pDev->enmType = PDMAUDIODEVICETYPE_BUILTIN;
1602
1603 if (pGUID == NULL)
1604 pDev->fFlags = PDMAUDIODEV_FLAGS_DEFAULT;
1605
1606 char *pszName;
1607 rc = RTUtf16ToUtf8(pwszDescription, &pszName);
1608 if (RT_SUCCESS(rc))
1609 {
1610 RTStrCopy(pDev->szName, sizeof(pDev->szName), pszName);
1611 RTStrFree(pszName);
1612
1613 PDSOUNDDEV pDSoundDev = (PDSOUNDDEV)pDev->pvData;
1614
1615 if (pGUID) /* pGUID == NULL means default device. */
1616 memcpy(&pDSoundDev->Guid, pGUID, sizeof(GUID));
1617
1618 rc = DrvAudioHlpDeviceEnumAdd(pDevEnm, pDev);
1619
1620 /* Note: Querying the actual device information will be done at some
1621 * later point in time outside this enumeration callback to prevent
1622 * DSound hangs. */
1623 }
1624 }
1625 else
1626 rc = VERR_NO_MEMORY;
1627
1628 if (RT_FAILURE(rc))
1629 {
1630 LogRel(("DSound: Error enumeration playback device '%ls', rc=%Rrc\n", pwszDescription, rc));
1631 return FALSE; /* Abort enumeration. */
1632 }
1633
1634 return TRUE;
1635}
1636
1637/**
1638 * Callback for the capture device enumeration.
1639 *
1640 * @return TRUE if continuing enumeration, FALSE if not.
1641 * @param pGUID Pointer to GUID of enumerated device. Can be NULL.
1642 * @param pwszDescription Pointer to (friendly) description of enumerated device.
1643 * @param pwszModule Pointer to module name of enumerated device.
1644 * @param lpContext Pointer to PDSOUNDENUMCBCTX context for storing the enumerated information.
1645 */
1646static BOOL CALLBACK dsoundDevicesEnumCbCapture(LPGUID pGUID, LPCWSTR pwszDescription, LPCWSTR pwszModule, PVOID lpContext)
1647{
1648 RT_NOREF(pwszModule);
1649
1650 PDSOUNDENUMCBCTX pEnumCtx = (PDSOUNDENUMCBCTX )lpContext;
1651 AssertPtrReturn(pEnumCtx , FALSE);
1652
1653 PPDMAUDIODEVICEENUM pDevEnm = pEnumCtx->pDevEnm;
1654 AssertPtrReturn(pDevEnm, FALSE);
1655
1656 /* pGUID can be NULL for default device(s). */
1657 AssertPtrReturn(pwszDescription, FALSE);
1658 /* Do not care about pwszModule. */
1659
1660 int rc;
1661
1662 PPDMAUDIODEVICE pDev = DrvAudioHlpDeviceAlloc(sizeof(DSOUNDDEV));
1663 if (pDev)
1664 {
1665 pDev->enmUsage = PDMAUDIODIR_IN;
1666 pDev->enmType = PDMAUDIODEVICETYPE_BUILTIN;
1667
1668 char *pszName;
1669 rc = RTUtf16ToUtf8(pwszDescription, &pszName);
1670 if (RT_SUCCESS(rc))
1671 {
1672 RTStrCopy(pDev->szName, sizeof(pDev->szName), pszName);
1673 RTStrFree(pszName);
1674
1675 PDSOUNDDEV pDSoundDev = (PDSOUNDDEV)pDev->pvData;
1676
1677 if (pGUID) /* pGUID == NULL means default capture device. */
1678 memcpy(&pDSoundDev->Guid, pGUID, sizeof(GUID));
1679
1680 rc = DrvAudioHlpDeviceEnumAdd(pDevEnm, pDev);
1681
1682 /* Note: Querying the actual device information will be done at some
1683 * later point in time outside this enumeration callback to prevent
1684 * DSound hangs. */
1685 }
1686 }
1687 else
1688 rc = VERR_NO_MEMORY;
1689
1690 if (RT_FAILURE(rc))
1691 {
1692 LogRel(("DSound: Error enumeration capture device '%ls', rc=%Rrc\n", pwszDescription, rc));
1693 return FALSE; /* Abort enumeration. */
1694 }
1695
1696 return TRUE;
1697}
1698
1699/**
1700 * Qqueries information for a given (DirectSound) device.
1701 *
1702 * @returns VBox status code.
1703 * @param pThis Host audio driver instance.
1704 * @param pDev Audio device to query information for.
1705 */
1706static int dsoundDeviceQueryInfo(PDRVHOSTDSOUND pThis, PPDMAUDIODEVICE pDev)
1707{
1708 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1709 AssertPtrReturn(pDev, VERR_INVALID_POINTER);
1710
1711 PDSOUNDDEV pDSoundDev = (PDSOUNDDEV)pDev->pvData;
1712 AssertPtr(pDSoundDev);
1713
1714 int rc;
1715
1716 if (pDev->enmUsage == PDMAUDIODIR_OUT)
1717 {
1718 LPDIRECTSOUND8 pDS;
1719 HRESULT hr = directSoundPlayInterfaceCreate(&pDSoundDev->Guid, &pDS);
1720 if (SUCCEEDED(hr))
1721 {
1722 DSCAPS DSCaps;
1723 RT_ZERO(DSCaps);
1724 DSCaps.dwSize = sizeof(DSCAPS);
1725 hr = IDirectSound_GetCaps(pDS, &DSCaps);
1726 if (SUCCEEDED(hr))
1727 {
1728 pDev->cMaxOutputChannels = DSCaps.dwFlags & DSCAPS_PRIMARYSTEREO ? 2 : 1;
1729
1730 DWORD dwSpeakerCfg;
1731 hr = IDirectSound_GetSpeakerConfig(pDS, &dwSpeakerCfg);
1732 if (SUCCEEDED(hr))
1733 {
1734 unsigned uSpeakerCount = 0;
1735 switch (DSSPEAKER_CONFIG(dwSpeakerCfg))
1736 {
1737 case DSSPEAKER_MONO: uSpeakerCount = 1; break;
1738 case DSSPEAKER_HEADPHONE: uSpeakerCount = 2; break;
1739 case DSSPEAKER_STEREO: uSpeakerCount = 2; break;
1740 case DSSPEAKER_QUAD: uSpeakerCount = 4; break;
1741 case DSSPEAKER_SURROUND: uSpeakerCount = 4; break;
1742 case DSSPEAKER_5POINT1: uSpeakerCount = 6; break;
1743 case DSSPEAKER_5POINT1_SURROUND: uSpeakerCount = 6; break;
1744 case DSSPEAKER_7POINT1: uSpeakerCount = 8; break;
1745 case DSSPEAKER_7POINT1_SURROUND: uSpeakerCount = 8; break;
1746 default: break;
1747 }
1748
1749 if (uSpeakerCount) /* Do we need to update the channel count? */
1750 pDev->cMaxOutputChannels = uSpeakerCount;
1751
1752 rc = VINF_SUCCESS;
1753 }
1754 else
1755 {
1756 LogRel(("DSound: Error retrieving playback device speaker config, hr=%Rhrc\n", hr));
1757 rc = VERR_ACCESS_DENIED; /** @todo Fudge! */
1758 }
1759 }
1760 else
1761 {
1762 LogRel(("DSound: Error retrieving playback device capabilities, hr=%Rhrc\n", hr));
1763 rc = VERR_ACCESS_DENIED; /** @todo Fudge! */
1764 }
1765
1766 directSoundPlayInterfaceDestroy(pDS);
1767 }
1768 else
1769 rc = VERR_GENERAL_FAILURE;
1770 }
1771 else if (pDev->enmUsage == PDMAUDIODIR_IN)
1772 {
1773 LPDIRECTSOUNDCAPTURE8 pDSC;
1774 HRESULT hr = directSoundCaptureInterfaceCreate(&pDSoundDev->Guid, &pDSC);
1775 if (SUCCEEDED(hr))
1776 {
1777 DSCCAPS DSCCaps;
1778 RT_ZERO(DSCCaps);
1779 DSCCaps.dwSize = sizeof(DSCCAPS);
1780 hr = IDirectSoundCapture_GetCaps(pDSC, &DSCCaps);
1781 if (SUCCEEDED(hr))
1782 {
1783 pDev->cMaxInputChannels = DSCCaps.dwChannels;
1784 rc = VINF_SUCCESS;
1785 }
1786 else
1787 {
1788 LogRel(("DSound: Error retrieving capture device capabilities, hr=%Rhrc\n", hr));
1789 rc = VERR_ACCESS_DENIED; /** @todo Fudge! */
1790 }
1791
1792 directSoundCaptureInterfaceDestroy(pDSC);
1793 }
1794 else
1795 rc = VERR_GENERAL_FAILURE;
1796 }
1797 else
1798 AssertFailedStmt(rc = VERR_NOT_SUPPORTED);
1799
1800 return rc;
1801}
1802
1803/**
1804 * Does a (Re-)enumeration of the host's playback + capturing devices.
1805 *
1806 * @return IPRT status code.
1807 * @param pThis Host audio driver instance.
1808 * @param pDevEnm Where to store the enumerated devices.
1809 */
1810static int dsoundDevicesEnumerate(PDRVHOSTDSOUND pThis, PPDMAUDIODEVICEENUM pDevEnm)
1811{
1812 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1813 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1814
1815 DSLOG(("DSound: Enumerating devices ...\n"));
1816
1817 RTLDRMOD hDSound = NULL;
1818 int rc = RTLdrLoadSystem("dsound.dll", true /*fNoUnload*/, &hDSound);
1819 if (RT_SUCCESS(rc))
1820 {
1821 DSOUNDENUMCBCTX EnumCtx;
1822 EnumCtx.fFlags = 0;
1823 EnumCtx.pDevEnm = pDevEnm;
1824
1825 /*
1826 * Enumerate playback devices.
1827 */
1828 PFNDIRECTSOUNDENUMERATEW pfnDirectSoundEnumerateW = NULL;
1829 rc = RTLdrGetSymbol(hDSound, "DirectSoundEnumerateW", (void**)&pfnDirectSoundEnumerateW);
1830 if (RT_SUCCESS(rc))
1831 {
1832 DSLOG(("DSound: Enumerating playback devices ...\n"));
1833
1834 HRESULT hr = pfnDirectSoundEnumerateW(&dsoundDevicesEnumCbPlayback, &EnumCtx);
1835 if (FAILED(hr))
1836 LogRel(("DSound: Error enumerating host playback devices: %Rhrc\n", hr));
1837 }
1838 else
1839 LogRel(("DSound: Error starting to enumerate host playback devices: %Rrc\n", rc));
1840
1841 /*
1842 * Enumerate capture devices.
1843 */
1844 PFNDIRECTSOUNDCAPTUREENUMERATEW pfnDirectSoundCaptureEnumerateW = NULL;
1845 rc = RTLdrGetSymbol(hDSound, "DirectSoundCaptureEnumerateW", (void**)&pfnDirectSoundCaptureEnumerateW);
1846 if (RT_SUCCESS(rc))
1847 {
1848 DSLOG(("DSound: Enumerating capture devices ...\n"));
1849
1850 HRESULT hr = pfnDirectSoundCaptureEnumerateW(&dsoundDevicesEnumCbCapture, &EnumCtx);
1851 if (FAILED(hr))
1852 LogRel(("DSound: Error enumerating host capture devices: %Rhrc\n", hr));
1853 }
1854 else
1855 LogRel(("DSound: Error starting to enumerate host capture devices: %Rrc\n", rc));
1856
1857 /*
1858 * Query Information from all enumerated devices.
1859 */
1860 PPDMAUDIODEVICE pDev;
1861 RTListForEach(&pDevEnm->lstDevices, pDev, PDMAUDIODEVICE, Node)
1862 /* ignore rc */ dsoundDeviceQueryInfo(pThis, pDev);
1863
1864 RTLdrClose(hDSound);
1865 }
1866 else
1867 {
1868 /* No dsound.dll on this system. */
1869 LogRel(("DSound: Could not load dsound.dll for enumerating devices: %Rrc\n", rc));
1870 }
1871
1872 DSLOG(("DSound: Enumerating devices done\n"));
1873
1874 return rc;
1875}
1876
1877/**
1878 * Updates this host driver's internal status, according to the global, overall input/output
1879 * state and all connected (native) audio streams.
1880 *
1881 * @param pThis Host audio driver instance.
1882 */
1883static void dsoundUpdateStatusInternal(PDRVHOSTDSOUND pThis)
1884{
1885 AssertPtrReturnVoid(pThis);
1886 /* pCfg is optional. */
1887
1888 LogFlowFuncEnter();
1889
1890 int rc = dsoundDevicesEnumerate(pThis, &pThis->DeviceEnum);
1891 if (RT_SUCCESS(rc))
1892 {
1893#if 0
1894 if ( pThis->fEnabledOut != RT_BOOL(cbCtx.cDevOut)
1895 || pThis->fEnabledIn != RT_BOOL(cbCtx.cDevIn))
1896 {
1897 /** @todo Use a registered callback to the audio connector (e.g "OnConfigurationChanged") to
1898 * let the connector know that something has changed within the host backend. */
1899 }
1900#endif
1901 pThis->fEnabledIn = RT_BOOL(DrvAudioHlpDeviceEnumGetDeviceCount(&pThis->DeviceEnum, PDMAUDIODIR_IN));
1902 pThis->fEnabledOut = RT_BOOL(DrvAudioHlpDeviceEnumGetDeviceCount(&pThis->DeviceEnum, PDMAUDIODIR_OUT));
1903 }
1904
1905 LogFlowFuncLeaveRC(rc);
1906}
1907
1908static int dsoundCreateStreamOut(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
1909 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
1910{
1911 LogFlowFunc(("pStreamDS=%p, pCfgReq=%p\n", pStreamDS, pCfgReq));
1912
1913 int rc = VINF_SUCCESS;
1914
1915 /* Try to open playback in case the device is already there. */
1916 HRESULT hr = directSoundPlayOpen(pThis, pStreamDS, pCfgReq, pCfgAcq);
1917 if (SUCCEEDED(hr))
1918 {
1919 rc = PDMAudioStrmCfgCopy(&pStreamDS->Cfg, pCfgAcq);
1920 if (RT_SUCCESS(rc))
1921 dsoundStreamReset(pThis, pStreamDS);
1922 }
1923 else
1924 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
1925
1926 LogFlowFuncLeaveRC(rc);
1927 return rc;
1928}
1929
1930static int dsoundControlStreamOut(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, PDMAUDIOSTREAMCMD enmStreamCmd)
1931{
1932 LogFlowFunc(("pStreamDS=%p, cmd=%d\n", pStreamDS, enmStreamCmd));
1933
1934 int rc = VINF_SUCCESS;
1935
1936 HRESULT hr;
1937 switch (enmStreamCmd)
1938 {
1939 case PDMAUDIOSTREAMCMD_ENABLE:
1940 {
1941 dsoundStreamEnable(pThis, pStreamDS, true /* fEnable */);
1942 break;
1943 }
1944
1945 case PDMAUDIOSTREAMCMD_RESUME:
1946 {
1947 hr = directSoundPlayStart(pThis, pStreamDS);
1948 if (FAILED(hr))
1949 rc = VERR_NOT_SUPPORTED; /** @todo Fix this. */
1950 break;
1951 }
1952
1953 case PDMAUDIOSTREAMCMD_DISABLE:
1954 {
1955 dsoundStreamEnable(pThis, pStreamDS, false /* fEnable */);
1956 hr = directSoundPlayStop(pThis, pStreamDS, true /* fFlush */);
1957 if (FAILED(hr))
1958 rc = VERR_NOT_SUPPORTED;
1959 break;
1960 }
1961
1962 case PDMAUDIOSTREAMCMD_PAUSE:
1963 {
1964 hr = directSoundPlayStop(pThis, pStreamDS, false /* fFlush */);
1965 if (FAILED(hr))
1966 rc = VERR_NOT_SUPPORTED;
1967 break;
1968 }
1969
1970 case PDMAUDIOSTREAMCMD_DRAIN:
1971 {
1972 /* Make sure we transferred everything. */
1973 pStreamDS->fEnabled = true;
1974 pStreamDS->Out.fDrain = true;
1975 rc = dsoundPlayTransfer(pThis, pStreamDS);
1976 if ( RT_SUCCESS(rc)
1977 && pStreamDS->Out.fFirstTransfer)
1978 {
1979 /* If this was the first transfer ever for this stream, make sure to also play the (short) audio data. */
1980 DSLOG(("DSound: Started playing output (short sound)\n"));
1981
1982 pStreamDS->Out.fFirstTransfer = false;
1983 pStreamDS->Out.cbLastTransferred = pStreamDS->Out.cbTransferred; /* All transferred audio data must be played. */
1984 pStreamDS->Out.tsLastTransferredMs = RTTimeMilliTS();
1985
1986 hr = directSoundPlayStart(pThis, pStreamDS);
1987 if (FAILED(hr))
1988 rc = VERR_ACCESS_DENIED; /** @todo Find a better rc. */
1989 }
1990 break;
1991 }
1992
1993 case PDMAUDIOSTREAMCMD_DROP:
1994 {
1995 pStreamDS->Out.cbLastTransferred = 0;
1996 pStreamDS->Out.tsLastTransferredMs = 0;
1997 RTCircBufReset(pStreamDS->pCircBuf);
1998 break;
1999 }
2000
2001 default:
2002 rc = VERR_NOT_SUPPORTED;
2003 break;
2004 }
2005
2006 LogFlowFuncLeaveRC(rc);
2007 return rc;
2008}
2009
2010/**
2011 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
2012 */
2013static DECLCALLBACK(int) drvHostDSoundHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf,
2014 uint32_t uBufSize, uint32_t *puWritten)
2015{
2016 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2017 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2018 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
2019 AssertReturn(uBufSize, VERR_INVALID_PARAMETER);
2020 /* puWritten is optional. */
2021
2022 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2023 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2024
2025 int rc = VINF_SUCCESS;
2026
2027 uint32_t cbWrittenTotal = 0;
2028
2029 uint8_t *pbBuf = (uint8_t *)pvBuf;
2030 PRTCIRCBUF pCircBuf = pStreamDS->pCircBuf;
2031
2032 uint32_t cbToPlay = RT_MIN(uBufSize, (uint32_t)RTCircBufFree(pCircBuf));
2033 while (cbToPlay)
2034 {
2035 void *pvChunk;
2036 size_t cbChunk;
2037 RTCircBufAcquireWriteBlock(pCircBuf, cbToPlay, &pvChunk, &cbChunk);
2038
2039 if (cbChunk)
2040 {
2041 memcpy(pvChunk, pbBuf, cbChunk);
2042
2043 pbBuf += cbChunk;
2044 Assert(cbToPlay >= cbChunk);
2045 cbToPlay -= (uint32_t)cbChunk;
2046
2047 cbWrittenTotal += (uint32_t)cbChunk;
2048 }
2049
2050 RTCircBufReleaseWriteBlock(pCircBuf, cbChunk);
2051 }
2052
2053 Assert(cbWrittenTotal <= uBufSize);
2054 Assert(cbWrittenTotal == uBufSize);
2055
2056 pStreamDS->Out.cbWritten += cbWrittenTotal;
2057
2058 if (RT_SUCCESS(rc))
2059 {
2060 if (puWritten)
2061 *puWritten = cbWrittenTotal;
2062 }
2063 else
2064 dsoundUpdateStatusInternal(pThis);
2065
2066 return rc;
2067}
2068
2069static int dsoundDestroyStreamOut(PDRVHOSTDSOUND pThis, PPDMAUDIOBACKENDSTREAM pStream)
2070{
2071 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2072
2073 LogFlowFuncEnter();
2074
2075 HRESULT hr = directSoundPlayStop(pThis, pStreamDS, true /* fFlush */);
2076 if (SUCCEEDED(hr))
2077 {
2078 hr = directSoundPlayClose(pThis, pStreamDS);
2079 if (FAILED(hr))
2080 return VERR_GENERAL_FAILURE; /** @todo Fix. */
2081 }
2082
2083 return VINF_SUCCESS;
2084}
2085
2086static int dsoundCreateStreamIn(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
2087 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
2088{
2089 LogFunc(("pStreamDS=%p, pCfgReq=%p, enmRecSource=%s\n",
2090 pStreamDS, pCfgReq, PDMAudioRecSrcGetName(pCfgReq->u.enmSrc)));
2091
2092 int rc = VINF_SUCCESS;
2093
2094 /* Try to open capture in case the device is already there. */
2095 HRESULT hr = directSoundCaptureOpen(pThis, pStreamDS, pCfgReq, pCfgAcq);
2096 if (SUCCEEDED(hr))
2097 {
2098 rc = PDMAudioStrmCfgCopy(&pStreamDS->Cfg, pCfgAcq);
2099 if (RT_SUCCESS(rc))
2100 dsoundStreamReset(pThis, pStreamDS);
2101 }
2102 else
2103 rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
2104
2105 return rc;
2106}
2107
2108static int dsoundControlStreamIn(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, PDMAUDIOSTREAMCMD enmStreamCmd)
2109{
2110 LogFlowFunc(("pStreamDS=%p, enmStreamCmd=%ld\n", pStreamDS, enmStreamCmd));
2111
2112 int rc = VINF_SUCCESS;
2113
2114 HRESULT hr;
2115 switch (enmStreamCmd)
2116 {
2117 case PDMAUDIOSTREAMCMD_ENABLE:
2118 dsoundStreamEnable(pThis, pStreamDS, true /* fEnable */);
2119 RT_FALL_THROUGH();
2120 case PDMAUDIOSTREAMCMD_RESUME:
2121 {
2122 /* Try to start capture. If it fails, then reopen and try again. */
2123 hr = directSoundCaptureStart(pThis, pStreamDS);
2124 if (FAILED(hr))
2125 {
2126 hr = directSoundCaptureClose(pThis, pStreamDS);
2127 if (SUCCEEDED(hr))
2128 {
2129 PDMAUDIOSTREAMCFG CfgAcq;
2130 hr = directSoundCaptureOpen(pThis, pStreamDS, &pStreamDS->Cfg /* pCfgReq */, &CfgAcq);
2131 if (SUCCEEDED(hr))
2132 {
2133 rc = PDMAudioStrmCfgCopy(&pStreamDS->Cfg, &CfgAcq);
2134 if (RT_FAILURE(rc))
2135 break;
2136
2137 /** @todo What to do if the format has changed? */
2138
2139 hr = directSoundCaptureStart(pThis, pStreamDS);
2140 }
2141 }
2142 }
2143
2144 if (FAILED(hr))
2145 rc = VERR_NOT_SUPPORTED;
2146 break;
2147 }
2148
2149 case PDMAUDIOSTREAMCMD_DISABLE:
2150 dsoundStreamEnable(pThis, pStreamDS, false /* fEnable */);
2151 RT_FALL_THROUGH();
2152 case PDMAUDIOSTREAMCMD_PAUSE:
2153 {
2154 directSoundCaptureStop(pThis, pStreamDS,
2155 enmStreamCmd == PDMAUDIOSTREAMCMD_DISABLE /* fFlush */);
2156
2157 /* Return success in any case, as stopping the capture can fail if
2158 * the capture buffer is not around anymore.
2159 *
2160 * This can happen if the host's capturing device has been changed suddenly. */
2161 rc = VINF_SUCCESS;
2162 break;
2163 }
2164
2165 default:
2166 rc = VERR_NOT_SUPPORTED;
2167 break;
2168 }
2169
2170 return rc;
2171}
2172
2173/**
2174 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
2175 */
2176static DECLCALLBACK(int) drvHostDSoundHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
2177 void *pvBuf, uint32_t uBufSize, uint32_t *puRead)
2178{
2179
2180 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2181 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2182 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
2183 AssertReturn(uBufSize, VERR_INVALID_PARAMETER);
2184
2185 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2186 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2187
2188 int rc = VINF_SUCCESS;
2189
2190 uint32_t cbReadTotal = 0;
2191
2192 uint32_t cbToRead = RT_MIN((uint32_t)RTCircBufUsed(pStreamDS->pCircBuf), uBufSize);
2193 while (cbToRead)
2194 {
2195 void *pvChunk;
2196 size_t cbChunk;
2197 RTCircBufAcquireReadBlock(pStreamDS->pCircBuf, cbToRead, &pvChunk, &cbChunk);
2198
2199 if (cbChunk)
2200 {
2201 memcpy((uint8_t *)pvBuf + cbReadTotal, pvChunk, cbChunk);
2202 cbReadTotal += (uint32_t)cbChunk;
2203 Assert(cbToRead >= cbChunk);
2204 cbToRead -= (uint32_t)cbChunk;
2205 }
2206
2207 RTCircBufReleaseReadBlock(pStreamDS->pCircBuf, cbChunk);
2208 }
2209
2210 if (RT_SUCCESS(rc))
2211 {
2212 if (puRead)
2213 *puRead = cbReadTotal;
2214 }
2215 else
2216 dsoundUpdateStatusInternal(pThis);
2217
2218 return rc;
2219}
2220
2221static int dsoundDestroyStreamIn(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
2222{
2223 LogFlowFuncEnter();
2224
2225 directSoundCaptureClose(pThis, pStreamDS);
2226
2227 return VINF_SUCCESS;
2228}
2229
2230/**
2231 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
2232 */
2233static DECLCALLBACK(int) drvHostDSoundHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
2234{
2235 RT_NOREF(pInterface);
2236 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2237 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
2238
2239 RT_BZERO(pBackendCfg, sizeof(PPDMAUDIOBACKENDCFG));
2240
2241 pBackendCfg->cbStreamOut = sizeof(DSOUNDSTREAM);
2242 pBackendCfg->cbStreamIn = sizeof(DSOUNDSTREAM);
2243
2244 RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "DirectSound");
2245
2246 pBackendCfg->cMaxStreamsIn = UINT32_MAX;
2247 pBackendCfg->cMaxStreamsOut = UINT32_MAX;
2248
2249 return VINF_SUCCESS;
2250}
2251
2252/**
2253 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetDevices}
2254 */
2255static DECLCALLBACK(int) drvHostDSoundHA_GetDevices(PPDMIHOSTAUDIO pInterface, PPDMAUDIODEVICEENUM pDeviceEnum)
2256{
2257 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2258 AssertPtrReturn(pDeviceEnum, VERR_INVALID_POINTER);
2259
2260 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2261
2262 int rc = RTCritSectEnter(&pThis->CritSect);
2263 if (RT_SUCCESS(rc))
2264 {
2265 rc = DrvAudioHlpDeviceEnumInit(pDeviceEnum);
2266 if (RT_SUCCESS(rc))
2267 {
2268 rc = dsoundDevicesEnumerate(pThis, pDeviceEnum);
2269 if (RT_FAILURE(rc))
2270 DrvAudioHlpDeviceEnumFree(pDeviceEnum);
2271 }
2272
2273 int rc2 = RTCritSectLeave(&pThis->CritSect);
2274 AssertRC(rc2);
2275 }
2276
2277 LogFlowFunc(("Returning %Rrc\n", rc));
2278 return rc;
2279}
2280
2281/**
2282 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
2283 */
2284static DECLCALLBACK(void) drvHostDSoundHA_Shutdown(PPDMIHOSTAUDIO pInterface)
2285{
2286 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2287
2288 LogFlowFuncEnter();
2289
2290 RT_NOREF(pThis);
2291
2292 LogFlowFuncLeave();
2293}
2294
2295/**
2296 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
2297 */
2298static DECLCALLBACK(int) drvHostDSoundHA_Init(PPDMIHOSTAUDIO pInterface)
2299{
2300 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2301 LogFlowFuncEnter();
2302
2303 int rc;
2304
2305 /* Verify that IDirectSound is available. */
2306 LPDIRECTSOUND pDirectSound = NULL;
2307 HRESULT hr = CoCreateInstance(CLSID_DirectSound, NULL, CLSCTX_ALL, IID_IDirectSound, (void **)&pDirectSound);
2308 if (SUCCEEDED(hr))
2309 {
2310 IDirectSound_Release(pDirectSound);
2311
2312 rc = VINF_SUCCESS;
2313
2314 dsoundUpdateStatusInternal(pThis);
2315 }
2316 else
2317 {
2318 DSLOGREL(("DSound: DirectSound not available: %Rhrc\n", hr));
2319 rc = VERR_NOT_SUPPORTED;
2320 }
2321
2322 LogFlowFuncLeaveRC(rc);
2323 return rc;
2324}
2325
2326static LPCGUID dsoundConfigQueryGUID(PCFGMNODE pCfg, const char *pszName, RTUUID *pUuid)
2327{
2328 LPCGUID pGuid = NULL;
2329
2330 char *pszGuid = NULL;
2331 int rc = CFGMR3QueryStringAlloc(pCfg, pszName, &pszGuid);
2332 if (RT_SUCCESS(rc))
2333 {
2334 rc = RTUuidFromStr(pUuid, pszGuid);
2335 if (RT_SUCCESS(rc))
2336 pGuid = (LPCGUID)&pUuid;
2337 else
2338 DSLOGREL(("DSound: Error parsing device GUID for device '%s': %Rrc\n", pszName, rc));
2339
2340 RTStrFree(pszGuid);
2341 }
2342
2343 return pGuid;
2344}
2345
2346static int dsoundConfigInit(PDRVHOSTDSOUND pThis, PCFGMNODE pCfg)
2347{
2348 pThis->Cfg.pGuidPlay = dsoundConfigQueryGUID(pCfg, "DeviceGuidOut", &pThis->Cfg.uuidPlay);
2349 pThis->Cfg.pGuidCapture = dsoundConfigQueryGUID(pCfg, "DeviceGuidIn", &pThis->Cfg.uuidCapture);
2350
2351 DSLOG(("DSound: Configuration: DeviceGuidOut {%RTuuid}, DeviceGuidIn {%RTuuid}\n",
2352 &pThis->Cfg.uuidPlay,
2353 &pThis->Cfg.uuidCapture));
2354
2355 return VINF_SUCCESS;
2356}
2357
2358/**
2359 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
2360 */
2361static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostDSoundHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
2362{
2363 RT_NOREF(enmDir);
2364 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
2365
2366 return PDMAUDIOBACKENDSTS_RUNNING;
2367}
2368
2369/**
2370 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
2371 */
2372static DECLCALLBACK(int) drvHostDSoundHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
2373 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
2374{
2375 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2376 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2377 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
2378 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
2379
2380 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2381 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2382
2383 int rc;
2384 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
2385 rc = dsoundCreateStreamIn(pThis, pStreamDS, pCfgReq, pCfgAcq);
2386 else
2387 rc = dsoundCreateStreamOut(pThis, pStreamDS, pCfgReq, pCfgAcq);
2388
2389 if (RT_SUCCESS(rc))
2390 {
2391 rc = PDMAudioStrmCfgCopy(&pStreamDS->Cfg, pCfgAcq);
2392 if (RT_SUCCESS(rc))
2393 rc = RTCritSectInit(&pStreamDS->CritSect);
2394 }
2395
2396 return rc;
2397}
2398
2399/**
2400 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
2401 */
2402static DECLCALLBACK(int) drvHostDSoundHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
2403{
2404 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2405 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2406
2407 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2408 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2409
2410 int rc;
2411 if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
2412 rc = dsoundDestroyStreamIn(pThis, pStreamDS);
2413 else
2414 rc = dsoundDestroyStreamOut(pThis, pStreamDS);
2415
2416 if (RT_SUCCESS(rc))
2417 {
2418 if (RTCritSectIsInitialized(&pStreamDS->CritSect))
2419 rc = RTCritSectDelete(&pStreamDS->CritSect);
2420 }
2421
2422 return rc;
2423}
2424
2425/**
2426 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
2427 */
2428static DECLCALLBACK(int) drvHostDSoundHA_StreamControl(PPDMIHOSTAUDIO pInterface,
2429 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
2430{
2431 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2432 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2433
2434 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2435 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2436
2437 int rc;
2438 if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
2439 rc = dsoundControlStreamIn(pThis, pStreamDS, enmStreamCmd);
2440 else
2441 rc = dsoundControlStreamOut(pThis, pStreamDS, enmStreamCmd);
2442
2443 return rc;
2444}
2445
2446/**
2447 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
2448 */
2449static DECLCALLBACK(uint32_t) drvHostDSoundHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
2450{
2451 RT_NOREF(pInterface);
2452 AssertPtrReturn(pStream, PDMAUDIOSTREAMSTS_FLAGS_NONE);
2453
2454 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2455
2456 if ( pStreamDS->fEnabled
2457 && pStreamDS->pCircBuf)
2458 {
2459 return (uint32_t)RTCircBufUsed(pStreamDS->pCircBuf);
2460 }
2461
2462 return 0;
2463}
2464
2465/**
2466 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
2467 */
2468static DECLCALLBACK(uint32_t) drvHostDSoundHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
2469{
2470 AssertPtrReturn(pInterface, PDMAUDIOSTREAMSTS_FLAGS_NONE);
2471 AssertPtrReturn(pStream, PDMAUDIOSTREAMSTS_FLAGS_NONE);
2472
2473 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2474
2475 if (pStreamDS->fEnabled)
2476 return (uint32_t)RTCircBufFree(pStreamDS->pCircBuf);
2477
2478 return 0;
2479}
2480
2481/**
2482 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetPending}
2483 */
2484static DECLCALLBACK(uint32_t) drvHostDSoundHA_StreamGetPending(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
2485{
2486 RT_NOREF(pInterface);
2487 AssertPtrReturn(pStream, 0);
2488
2489 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2490
2491 if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT)
2492 {
2493 uint32_t cbPending = 0;
2494
2495 /* Any uncommitted data left? */
2496 if (pStreamDS->pCircBuf)
2497 cbPending = (uint32_t)RTCircBufUsed(pStreamDS->pCircBuf);
2498
2499 /* Check if we have committed data which still needs to be played by
2500 * by DirectSound's streaming buffer. */
2501 if (!cbPending)
2502 {
2503 const uint64_t diffLastTransferredMs = RTTimeMilliTS() - pStreamDS->Out.tsLastTransferredMs;
2504 const uint64_t uLastTranserredChunkMs = PDMAudioPropsBytesToMilli(&pStreamDS->Cfg.Props, pStreamDS->Out.cbLastTransferred);
2505 if ( uLastTranserredChunkMs
2506 && diffLastTransferredMs < uLastTranserredChunkMs)
2507 cbPending = 1;
2508
2509 Log3Func(("diffLastTransferredMs=%RU64ms, uLastTranserredChunkMs=%RU64ms (%RU32 bytes) -> cbPending=%RU32\n",
2510 diffLastTransferredMs, uLastTranserredChunkMs, pStreamDS->Out.cbLastTransferred, cbPending));
2511 }
2512 else
2513 Log3Func(("cbPending=%RU32\n", cbPending));
2514
2515 return cbPending;
2516 }
2517 /* Note: For input streams we never have pending data left. */
2518
2519 return 0;
2520}
2521
2522/**
2523 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
2524 */
2525static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvHostDSoundHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
2526{
2527 RT_NOREF(pInterface);
2528 AssertPtrReturn(pStream, PDMAUDIOSTREAMSTS_FLAGS_NONE);
2529
2530 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2531
2532 PDMAUDIOSTREAMSTS fStrmStatus = PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED;
2533
2534 if (pStreamDS->fEnabled)
2535 fStrmStatus |= PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
2536
2537 return fStrmStatus;
2538}
2539
2540/**
2541 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
2542 */
2543static DECLCALLBACK(int) drvHostDSoundHA_StreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
2544{
2545 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2546 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
2547
2548 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2549 PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
2550
2551 if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
2552 {
2553 return dsoundCaptureTransfer(pThis, pStreamDS);
2554 }
2555 else if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT)
2556 {
2557 return dsoundPlayTransfer(pThis, pStreamDS);
2558 }
2559
2560 return VINF_SUCCESS;
2561}
2562
2563#ifdef VBOX_WITH_AUDIO_CALLBACKS
2564/**
2565 * @interface_method_impl{PDMIHOSTAUDIO,pfnSetCallback}
2566 */
2567static DECLCALLBACK(int) drvHostDSoundHA_SetCallback(PPDMIHOSTAUDIO pInterface, PFNPDMHOSTAUDIOCALLBACK pfnCallback)
2568{
2569 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
2570 /* pfnCallback will be handled below. */
2571
2572 PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
2573
2574 int rc = RTCritSectEnter(&pThis->CritSect);
2575 if (RT_SUCCESS(rc))
2576 {
2577 LogFunc(("pfnCallback=%p\n", pfnCallback));
2578
2579 if (pfnCallback) /* Register. */
2580 {
2581 Assert(pThis->pfnCallback == NULL);
2582 pThis->pfnCallback = pfnCallback;
2583
2584#ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
2585 if (pThis->m_pNotificationClient)
2586 pThis->m_pNotificationClient->RegisterCallback(pThis->pDrvIns, pfnCallback);
2587#endif
2588 }
2589 else /* Unregister. */
2590 {
2591 if (pThis->pfnCallback)
2592 pThis->pfnCallback = NULL;
2593
2594#ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
2595 if (pThis->m_pNotificationClient)
2596 pThis->m_pNotificationClient->UnregisterCallback();
2597#endif
2598 }
2599
2600 int rc2 = RTCritSectLeave(&pThis->CritSect);
2601 AssertRC(rc2);
2602 }
2603
2604 return rc;
2605}
2606#endif
2607
2608
2609/*********************************************************************************************************************************
2610* PDMDRVINS::IBase Interface *
2611*********************************************************************************************************************************/
2612
2613/**
2614 * @callback_method_impl{PDMIBASE,pfnQueryInterface}
2615 */
2616static DECLCALLBACK(void *) drvHostDSoundQueryInterface(PPDMIBASE pInterface, const char *pszIID)
2617{
2618 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
2619 PDRVHOSTDSOUND pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDSOUND);
2620
2621 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
2622 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
2623 return NULL;
2624}
2625
2626
2627/*********************************************************************************************************************************
2628* PDMDRVREG Interface *
2629*********************************************************************************************************************************/
2630
2631/**
2632 * @callback_method_impl{FNPDMDRVDESTRUCT, pfnDestruct}
2633 */
2634static DECLCALLBACK(void) drvHostDSoundDestruct(PPDMDRVINS pDrvIns)
2635{
2636 PDRVHOSTDSOUND pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDSOUND);
2637 PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
2638
2639 LogFlowFuncEnter();
2640
2641#ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
2642 if (pThis->m_pNotificationClient)
2643 {
2644 pThis->m_pNotificationClient->Unregister();
2645 pThis->m_pNotificationClient->Release();
2646
2647 pThis->m_pNotificationClient = NULL;
2648 }
2649#endif
2650
2651 DrvAudioHlpDeviceEnumFree(&pThis->DeviceEnum);
2652
2653 if (pThis->pDrvIns)
2654 CoUninitialize();
2655
2656 int rc2 = RTCritSectDelete(&pThis->CritSect);
2657 AssertRC(rc2);
2658
2659 LogFlowFuncLeave();
2660}
2661
2662/**
2663 * @callback_method_impl{FNPDMDRVCONSTRUCT,
2664 * Construct a DirectSound Audio driver instance.}
2665 */
2666static DECLCALLBACK(int) drvHostDSoundConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
2667{
2668 RT_NOREF(fFlags);
2669 PDRVHOSTDSOUND pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDSOUND);
2670
2671 LogRel(("Audio: Initializing DirectSound audio driver\n"));
2672
2673 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
2674
2675 HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
2676 if (FAILED(hr))
2677 {
2678 DSLOGREL(("DSound: CoInitializeEx failed with %Rhrc\n", hr));
2679 return VERR_NOT_SUPPORTED;
2680 }
2681
2682 /*
2683 * Init basic data members and interfaces.
2684 */
2685 pThis->pDrvIns = pDrvIns;
2686 /* IBase */
2687 pDrvIns->IBase.pfnQueryInterface = drvHostDSoundQueryInterface;
2688 /* IHostAudio */
2689 pThis->IHostAudio.pfnInit = drvHostDSoundHA_Init;
2690 pThis->IHostAudio.pfnShutdown = drvHostDSoundHA_Shutdown;
2691 pThis->IHostAudio.pfnGetConfig = drvHostDSoundHA_GetConfig;
2692 pThis->IHostAudio.pfnGetStatus = drvHostDSoundHA_GetStatus;
2693 pThis->IHostAudio.pfnStreamCreate = drvHostDSoundHA_StreamCreate;
2694 pThis->IHostAudio.pfnStreamDestroy = drvHostDSoundHA_StreamDestroy;
2695 pThis->IHostAudio.pfnStreamControl = drvHostDSoundHA_StreamControl;
2696 pThis->IHostAudio.pfnStreamGetReadable = drvHostDSoundHA_StreamGetReadable;
2697 pThis->IHostAudio.pfnStreamGetWritable = drvHostDSoundHA_StreamGetWritable;
2698 pThis->IHostAudio.pfnStreamGetStatus = drvHostDSoundHA_StreamGetStatus;
2699 pThis->IHostAudio.pfnStreamIterate = drvHostDSoundHA_StreamIterate;
2700 pThis->IHostAudio.pfnStreamPlay = drvHostDSoundHA_StreamPlay;
2701 pThis->IHostAudio.pfnStreamCapture = drvHostDSoundHA_StreamCapture;
2702#ifdef VBOX_WITH_AUDIO_CALLBACKS
2703 pThis->IHostAudio.pfnSetCallback = drvHostDSoundHA_SetCallback;
2704 pThis->pfnCallback = NULL;
2705#else
2706 pThis->IHostAudio.pfnSetCallback = NULL;
2707#endif
2708 pThis->IHostAudio.pfnGetDevices = drvHostDSoundHA_GetDevices;
2709 pThis->IHostAudio.pfnStreamGetPending = drvHostDSoundHA_StreamGetPending;
2710 pThis->IHostAudio.pfnStreamPlayBegin = NULL;
2711 pThis->IHostAudio.pfnStreamPlayEnd = NULL;
2712 pThis->IHostAudio.pfnStreamCaptureBegin = NULL;
2713 pThis->IHostAudio.pfnStreamCaptureEnd = NULL;
2714
2715 /*
2716 * Init the static parts.
2717 */
2718 DrvAudioHlpDeviceEnumInit(&pThis->DeviceEnum);
2719
2720 pThis->fEnabledIn = false;
2721 pThis->fEnabledOut = false;
2722
2723 int rc = VINF_SUCCESS;
2724
2725#ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
2726 bool fUseNotificationClient = false;
2727
2728 char szOSVersion[32];
2729 rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szOSVersion, sizeof(szOSVersion));
2730 if (RT_SUCCESS(rc))
2731 {
2732 /* IMMNotificationClient is available starting at Windows Vista. */
2733 if (RTStrVersionCompare(szOSVersion, "6.0") >= 0)
2734 fUseNotificationClient = true;
2735 }
2736
2737 if (fUseNotificationClient)
2738 {
2739 try
2740 {
2741 pThis->m_pNotificationClient = new VBoxMMNotificationClient();
2742
2743 hr = pThis->m_pNotificationClient->Initialize();
2744 if (SUCCEEDED(hr))
2745 hr = pThis->m_pNotificationClient->Register();
2746
2747 if (FAILED(hr))
2748 rc = VERR_AUDIO_BACKEND_INIT_FAILED;
2749 }
2750 catch (std::bad_alloc &)
2751 {
2752 rc = VERR_NO_MEMORY;
2753 }
2754 }
2755
2756 LogRel2(("DSound: Notification client is %s\n", fUseNotificationClient ? "enabled" : "disabled"));
2757#endif
2758
2759 if (RT_SUCCESS(rc))
2760 {
2761 /*
2762 * Initialize configuration values.
2763 */
2764 rc = dsoundConfigInit(pThis, pCfg);
2765 if (RT_SUCCESS(rc))
2766 rc = RTCritSectInit(&pThis->CritSect);
2767 }
2768
2769 return rc;
2770}
2771
2772
2773/**
2774 * PDM driver registration.
2775 */
2776const PDMDRVREG g_DrvHostDSound =
2777{
2778 /* u32Version */
2779 PDM_DRVREG_VERSION,
2780 /* szName */
2781 "DSoundAudio",
2782 /* szRCMod */
2783 "",
2784 /* szR0Mod */
2785 "",
2786 /* pszDescription */
2787 "DirectSound Audio host driver",
2788 /* fFlags */
2789 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
2790 /* fClass. */
2791 PDM_DRVREG_CLASS_AUDIO,
2792 /* cMaxInstances */
2793 ~0U,
2794 /* cbInstance */
2795 sizeof(DRVHOSTDSOUND),
2796 /* pfnConstruct */
2797 drvHostDSoundConstruct,
2798 /* pfnDestruct */
2799 drvHostDSoundDestruct,
2800 /* pfnRelocate */
2801 NULL,
2802 /* pfnIOCtl */
2803 NULL,
2804 /* pfnPowerOn */
2805 NULL,
2806 /* pfnReset */
2807 NULL,
2808 /* pfnSuspend */
2809 NULL,
2810 /* pfnResume */
2811 NULL,
2812 /* pfnAttach */
2813 NULL,
2814 /* pfnDetach */
2815 NULL,
2816 /* pfnPowerOff */
2817 NULL,
2818 /* pfnSoftReset */
2819 NULL,
2820 /* u32EndVersion */
2821 PDM_DRVREG_VERSION
2822};
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette