1 | /* $Id: DrvHostDSound.cpp 77031 2019-01-29 13:11:37Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Windows host backend driver using DirectSound.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 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 "DrvAudio.h"
|
---|
33 | #include "VBoxDD.h"
|
---|
34 | #ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
|
---|
35 | # include <new> /* For bad_alloc. */
|
---|
36 | # include "VBoxMMNotificationClient.h"
|
---|
37 | #endif
|
---|
38 |
|
---|
39 |
|
---|
40 | /*********************************************************************************************************************************
|
---|
41 | * Defined Constants And Macros *
|
---|
42 | *********************************************************************************************************************************/
|
---|
43 | /*
|
---|
44 | * IDirectSound* interface uses HRESULT status codes and the driver callbacks use
|
---|
45 | * the IPRT status codes. To minimize HRESULT->IPRT conversion most internal functions
|
---|
46 | * in the driver return HRESULT and conversion is done in the driver callbacks.
|
---|
47 | *
|
---|
48 | * Naming convention:
|
---|
49 | * 'dsound*' functions return IPRT status code;
|
---|
50 | * 'directSound*' - return HRESULT.
|
---|
51 | */
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Optional release logging, which a user can turn on with the
|
---|
55 | * 'VBoxManage debugvm' command.
|
---|
56 | * Debug logging still uses the common Log* macros from IPRT.
|
---|
57 | * Messages which always should go to the release log use LogRel.
|
---|
58 | */
|
---|
59 | /* General code behavior. */
|
---|
60 | #define DSLOG(a) do { LogRel2(a); } while(0)
|
---|
61 | /* Something which produce a lot of logging during playback/recording. */
|
---|
62 | #define DSLOGF(a) do { LogRel3(a); } while(0)
|
---|
63 | /* Important messages like errors. Limited in the default release log to avoid log flood. */
|
---|
64 | #define DSLOGREL(a) \
|
---|
65 | do { \
|
---|
66 | static int8_t s_cLogged = 0; \
|
---|
67 | if (s_cLogged < 8) { \
|
---|
68 | ++s_cLogged; \
|
---|
69 | LogRel(a); \
|
---|
70 | } else DSLOG(a); \
|
---|
71 | } while (0)
|
---|
72 |
|
---|
73 | /** Maximum number of attempts to restore the sound buffer before giving up. */
|
---|
74 | #define DRV_DSOUND_RESTORE_ATTEMPTS_MAX 3
|
---|
75 | /** Default input latency (in ms). */
|
---|
76 | #define DRV_DSOUND_DEFAULT_LATENCY_MS_IN 50
|
---|
77 | /** Default output latency (in ms). */
|
---|
78 | #define DRV_DSOUND_DEFAULT_LATENCY_MS_OUT 50
|
---|
79 |
|
---|
80 | /** Makes DRVHOSTDSOUND out of PDMIHOSTAUDIO. */
|
---|
81 | #define PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface) \
|
---|
82 | ( (PDRVHOSTDSOUND)((uintptr_t)pInterface - RT_UOFFSETOF(DRVHOSTDSOUND, IHostAudio)) )
|
---|
83 |
|
---|
84 |
|
---|
85 | /*********************************************************************************************************************************
|
---|
86 | * Structures and Typedefs *
|
---|
87 | *********************************************************************************************************************************/
|
---|
88 | /* Dynamically load dsound.dll. */
|
---|
89 | typedef HRESULT WINAPI FNDIRECTSOUNDENUMERATEW(LPDSENUMCALLBACKW pDSEnumCallback, PVOID pContext);
|
---|
90 | typedef FNDIRECTSOUNDENUMERATEW *PFNDIRECTSOUNDENUMERATEW;
|
---|
91 | typedef HRESULT WINAPI FNDIRECTSOUNDCAPTUREENUMERATEW(LPDSENUMCALLBACKW pDSEnumCallback, PVOID pContext);
|
---|
92 | typedef FNDIRECTSOUNDCAPTUREENUMERATEW *PFNDIRECTSOUNDCAPTUREENUMERATEW;
|
---|
93 | typedef HRESULT WINAPI FNDIRECTSOUNDCAPTURECREATE8(LPCGUID lpcGUID, LPDIRECTSOUNDCAPTURE8 *lplpDSC, LPUNKNOWN pUnkOuter);
|
---|
94 | typedef FNDIRECTSOUNDCAPTURECREATE8 *PFNDIRECTSOUNDCAPTURECREATE8;
|
---|
95 |
|
---|
96 | #define VBOX_DSOUND_MAX_EVENTS 3
|
---|
97 |
|
---|
98 | typedef enum DSOUNDEVENT
|
---|
99 | {
|
---|
100 | DSOUNDEVENT_NOTIFY = 0,
|
---|
101 | DSOUNDEVENT_INPUT,
|
---|
102 | DSOUNDEVENT_OUTPUT,
|
---|
103 | } DSOUNDEVENT;
|
---|
104 |
|
---|
105 | typedef struct DSOUNDHOSTCFG
|
---|
106 | {
|
---|
107 | RTUUID uuidPlay;
|
---|
108 | LPCGUID pGuidPlay;
|
---|
109 | RTUUID uuidCapture;
|
---|
110 | LPCGUID pGuidCapture;
|
---|
111 | } DSOUNDHOSTCFG, *PDSOUNDHOSTCFG;
|
---|
112 |
|
---|
113 | typedef struct DSOUNDSTREAM
|
---|
114 | {
|
---|
115 | /** The stream's acquired configuration. */
|
---|
116 | PDMAUDIOSTREAMCFG Cfg;
|
---|
117 | /** Buffer alignment. */
|
---|
118 | uint8_t uAlign;
|
---|
119 | /** Whether this stream is in an enable state on the DirectSound side. */
|
---|
120 | bool fEnabled;
|
---|
121 | /** The stream's critical section for synchronizing access. */
|
---|
122 | RTCRITSECT CritSect;
|
---|
123 | /** The internal playback / capturing buffer. */
|
---|
124 | PRTCIRCBUF pCircBuf;
|
---|
125 | /** Size (in bytes) of the DirectSound buffer.
|
---|
126 | * Note: This in *not* the size of the circular buffer above! */
|
---|
127 | DWORD cbBufSize;
|
---|
128 | union
|
---|
129 | {
|
---|
130 | struct
|
---|
131 | {
|
---|
132 | /** The actual DirectSound Buffer (DSB) used for the capturing.
|
---|
133 | * This is a secondary buffer and is used as a streaming buffer. */
|
---|
134 | LPDIRECTSOUNDCAPTUREBUFFER8 pDSCB;
|
---|
135 | /** Current read offset (in bytes) within the DSB. */
|
---|
136 | DWORD offReadPos;
|
---|
137 | /** Number of buffer overruns happened. Used for logging. */
|
---|
138 | uint8_t cOverruns;
|
---|
139 | } In;
|
---|
140 | struct
|
---|
141 | {
|
---|
142 | /** The actual DirectSound Buffer (DSB) used for playback.
|
---|
143 | * This is a secondary buffer and is used as a streaming buffer. */
|
---|
144 | LPDIRECTSOUNDBUFFER8 pDSB;
|
---|
145 | /** Current write offset (in bytes) within the DSB. */
|
---|
146 | DWORD offWritePos;
|
---|
147 | /** Offset of last play cursor within the DSB when checked for pending. */
|
---|
148 | DWORD offPlayCursorLastPending;
|
---|
149 | /** Offset of last play cursor within the DSB when last played. */
|
---|
150 | DWORD offPlayCursorLastPlayed;
|
---|
151 | /** Total amount (in bytes) written to our internal ring buffer. */
|
---|
152 | uint64_t cbWritten;
|
---|
153 | /** Total amount (in bytes) played (to the DirectSound buffer). */
|
---|
154 | uint64_t cbTransferred;
|
---|
155 | /** Flag indicating whether playback was just (re)started. */
|
---|
156 | bool fFirstTransfer;
|
---|
157 | /** Flag indicating whether this stream is in draining mode, e.g. no new
|
---|
158 | * data is being written to it but DirectSound still needs to be able to
|
---|
159 | * play its remaining (buffered) data. */
|
---|
160 | bool fDrain;
|
---|
161 | /** How much (in bytes) the last transfer from the internal buffer
|
---|
162 | * to the DirectSound buffer was. */
|
---|
163 | uint32_t cbLastTransferred;
|
---|
164 | /** Timestamp (in ms) of the last transfer from the internal buffer
|
---|
165 | * to the DirectSound buffer. */
|
---|
166 | uint64_t tsLastTransferredMs;
|
---|
167 | /** Number of buffer underruns happened. Used for logging. */
|
---|
168 | uint8_t cUnderruns;
|
---|
169 | } Out;
|
---|
170 | };
|
---|
171 | #ifdef LOG_ENABLED
|
---|
172 | struct
|
---|
173 | {
|
---|
174 | uint64_t tsLastTransferredMs;
|
---|
175 | } Dbg;
|
---|
176 | #endif
|
---|
177 | } DSOUNDSTREAM, *PDSOUNDSTREAM;
|
---|
178 |
|
---|
179 | /**
|
---|
180 | * Structure for keeping a DirectSound-specific device entry.
|
---|
181 | * This is then bound to the PDMAUDIODEVICE's pvData area.
|
---|
182 | */
|
---|
183 | typedef struct DSOUNDDEV
|
---|
184 | {
|
---|
185 | GUID Guid;
|
---|
186 | } DSOUNDDEV, *PDSOUNDDEV;
|
---|
187 |
|
---|
188 | /**
|
---|
189 | * Structure for holding a device enumeration context.
|
---|
190 | */
|
---|
191 | typedef struct DSOUNDENUMCBCTX
|
---|
192 | {
|
---|
193 | /** Enumeration flags. */
|
---|
194 | uint32_t fFlags;
|
---|
195 | /** Pointer to device list to populate. */
|
---|
196 | PPDMAUDIODEVICEENUM pDevEnm;
|
---|
197 | } DSOUNDENUMCBCTX, *PDSOUNDENUMCBCTX;
|
---|
198 |
|
---|
199 | typedef struct DRVHOSTDSOUND
|
---|
200 | {
|
---|
201 | /** Pointer to the driver instance structure. */
|
---|
202 | PPDMDRVINS pDrvIns;
|
---|
203 | /** Our audio host audio interface. */
|
---|
204 | PDMIHOSTAUDIO IHostAudio;
|
---|
205 | /** Critical section to serialize access. */
|
---|
206 | RTCRITSECT CritSect;
|
---|
207 | /** DirectSound configuration options. */
|
---|
208 | DSOUNDHOSTCFG Cfg;
|
---|
209 | /** List of devices of last enumeration. */
|
---|
210 | PDMAUDIODEVICEENUM DeviceEnum;
|
---|
211 | /** Whether this backend supports any audio input. */
|
---|
212 | bool fEnabledIn;
|
---|
213 | /** Whether this backend supports any audio output. */
|
---|
214 | bool fEnabledOut;
|
---|
215 | /** The Direct Sound playback interface. */
|
---|
216 | LPDIRECTSOUND8 pDS;
|
---|
217 | /** The Direct Sound capturing interface. */
|
---|
218 | LPDIRECTSOUNDCAPTURE8 pDSC;
|
---|
219 | #ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
|
---|
220 | VBoxMMNotificationClient *m_pNotificationClient;
|
---|
221 | #endif
|
---|
222 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
223 | /** Callback function to the upper driver.
|
---|
224 | * Can be NULL if not being used / registered. */
|
---|
225 | PFNPDMHOSTAUDIOCALLBACK pfnCallback;
|
---|
226 | #endif
|
---|
227 | /** Pointer to the input stream. */
|
---|
228 | PDSOUNDSTREAM pDSStrmIn;
|
---|
229 | /** Pointer to the output stream. */
|
---|
230 | PDSOUNDSTREAM pDSStrmOut;
|
---|
231 | } DRVHOSTDSOUND, *PDRVHOSTDSOUND;
|
---|
232 |
|
---|
233 |
|
---|
234 | /*********************************************************************************************************************************
|
---|
235 | * Internal Functions *
|
---|
236 | *********************************************************************************************************************************/
|
---|
237 | static HRESULT directSoundPlayRestore(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB);
|
---|
238 | static HRESULT directSoundPlayStart(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS);
|
---|
239 | static HRESULT directSoundPlayStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush);
|
---|
240 | static HRESULT directSoundCaptureStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush);
|
---|
241 |
|
---|
242 | static int dsoundDevicesEnumerate(PDRVHOSTDSOUND pThis, PDMAUDIODEVICEENUM pDevEnm, uint32_t fEnum);
|
---|
243 |
|
---|
244 | static int dsoundStreamEnable(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fEnable);
|
---|
245 | static void dsoundStreamReset(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS);
|
---|
246 | static void dsoundUpdateStatusInternal(PDRVHOSTDSOUND pThis);
|
---|
247 |
|
---|
248 |
|
---|
249 | static DWORD dsoundRingDistance(DWORD offEnd, DWORD offBegin, DWORD cSize)
|
---|
250 | {
|
---|
251 | AssertReturn(offEnd <= cSize, 0);
|
---|
252 | AssertReturn(offBegin <= cSize, 0);
|
---|
253 |
|
---|
254 | return offEnd >= offBegin ? offEnd - offBegin : cSize - offBegin + offEnd;
|
---|
255 | }
|
---|
256 |
|
---|
257 | static int dsoundWaveFmtFromCfg(PPDMAUDIOSTREAMCFG pCfg, PWAVEFORMATEX pFmt)
|
---|
258 | {
|
---|
259 | AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
|
---|
260 | AssertPtrReturn(pFmt, VERR_INVALID_POINTER);
|
---|
261 |
|
---|
262 | RT_BZERO(pFmt, sizeof(WAVEFORMATEX));
|
---|
263 |
|
---|
264 | pFmt->wFormatTag = WAVE_FORMAT_PCM;
|
---|
265 | pFmt->nChannels = pCfg->Props.cChannels;
|
---|
266 | pFmt->wBitsPerSample = pCfg->Props.cBytes * 8;
|
---|
267 | pFmt->nSamplesPerSec = pCfg->Props.uHz;
|
---|
268 | pFmt->nBlockAlign = pFmt->nChannels * pFmt->wBitsPerSample / 8;
|
---|
269 | pFmt->nAvgBytesPerSec = pFmt->nSamplesPerSec * pFmt->nBlockAlign;
|
---|
270 | pFmt->cbSize = 0; /* No extra data specified. */
|
---|
271 |
|
---|
272 | return VINF_SUCCESS;
|
---|
273 | }
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * Retrieves the number of free bytes available for writing to a DirectSound output stream.
|
---|
277 | *
|
---|
278 | * @return IPRT status code. VERR_NOT_AVAILABLE if unable to determine or the buffer was not recoverable.
|
---|
279 | * @param pThis Host audio driver instance.
|
---|
280 | * @param pStreamDS DirectSound output stream to retrieve number for.
|
---|
281 | * @param pdwFree Where to return the free amount on success.
|
---|
282 | */
|
---|
283 | static int dsoundGetFreeOut(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, DWORD *pdwFree)
|
---|
284 | {
|
---|
285 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
286 | AssertPtrReturn(pStreamDS, VERR_INVALID_POINTER);
|
---|
287 | AssertPtrReturn(pdwFree, VERR_INVALID_POINTER);
|
---|
288 |
|
---|
289 | Assert(pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT); /* Paranoia. */
|
---|
290 |
|
---|
291 | LPDIRECTSOUNDBUFFER8 pDSB = pStreamDS->Out.pDSB;
|
---|
292 | if (!pDSB)
|
---|
293 | {
|
---|
294 | AssertPtr(pDSB);
|
---|
295 | return VERR_INVALID_POINTER;
|
---|
296 | }
|
---|
297 |
|
---|
298 | HRESULT hr = S_OK;
|
---|
299 |
|
---|
300 | /* Get the current play position which is used for calculating the free space in the buffer. */
|
---|
301 | for (unsigned i = 0; i < DRV_DSOUND_RESTORE_ATTEMPTS_MAX; i++)
|
---|
302 | {
|
---|
303 | DWORD cbPlayCursor, cbWriteCursor;
|
---|
304 | hr = IDirectSoundBuffer8_GetCurrentPosition(pDSB, &cbPlayCursor, &cbWriteCursor);
|
---|
305 | if (SUCCEEDED(hr))
|
---|
306 | {
|
---|
307 | int32_t cbDiff = cbWriteCursor - cbPlayCursor;
|
---|
308 | if (cbDiff < 0)
|
---|
309 | cbDiff += pStreamDS->cbBufSize;
|
---|
310 |
|
---|
311 | int32_t cbFree = cbPlayCursor - pStreamDS->Out.offWritePos;
|
---|
312 | if (cbFree < 0)
|
---|
313 | cbFree += pStreamDS->cbBufSize;
|
---|
314 |
|
---|
315 | if (cbFree > (int32_t)pStreamDS->cbBufSize - cbDiff)
|
---|
316 | {
|
---|
317 | pStreamDS->Out.offWritePos = cbWriteCursor;
|
---|
318 | cbFree = pStreamDS->cbBufSize - cbDiff;
|
---|
319 | }
|
---|
320 |
|
---|
321 | /* When starting to use a DirectSound buffer, cbPlayCursor and cbWriteCursor
|
---|
322 | * both point at position 0, so we won't be able to detect how many bytes
|
---|
323 | * are writable that way.
|
---|
324 | *
|
---|
325 | * So use our per-stream written indicator to see if we just started a stream. */
|
---|
326 | if (pStreamDS->Out.cbWritten == 0)
|
---|
327 | cbFree = pStreamDS->cbBufSize;
|
---|
328 |
|
---|
329 | DSLOGREL(("DSound: cbPlayCursor=%RU32, cbWriteCursor=%RU32, offWritePos=%RU32 -> cbFree=%RI32\n",
|
---|
330 | cbPlayCursor, cbWriteCursor, pStreamDS->Out.offWritePos, cbFree));
|
---|
331 |
|
---|
332 | *pdwFree = cbFree;
|
---|
333 |
|
---|
334 | return VINF_SUCCESS;
|
---|
335 | }
|
---|
336 |
|
---|
337 | if (hr != DSERR_BUFFERLOST) /** @todo MSDN doesn't state this error for GetCurrentPosition(). */
|
---|
338 | break;
|
---|
339 |
|
---|
340 | LogFunc(("Getting playing position failed due to lost buffer, restoring ...\n"));
|
---|
341 |
|
---|
342 | directSoundPlayRestore(pThis, pDSB);
|
---|
343 | }
|
---|
344 |
|
---|
345 | if (hr != DSERR_BUFFERLOST) /* Avoid log flooding if the error is still there. */
|
---|
346 | DSLOGREL(("DSound: Getting current playback position failed with %Rhrc\n", hr));
|
---|
347 |
|
---|
348 | LogFunc(("Failed with %Rhrc\n", hr));
|
---|
349 |
|
---|
350 | return VERR_NOT_AVAILABLE;
|
---|
351 | }
|
---|
352 |
|
---|
353 | static char *dsoundGUIDToUtf8StrA(LPCGUID pGUID)
|
---|
354 | {
|
---|
355 | if (pGUID)
|
---|
356 | {
|
---|
357 | LPOLESTR lpOLEStr;
|
---|
358 | HRESULT hr = StringFromCLSID(*pGUID, &lpOLEStr);
|
---|
359 | if (SUCCEEDED(hr))
|
---|
360 | {
|
---|
361 | char *pszGUID;
|
---|
362 | int rc = RTUtf16ToUtf8(lpOLEStr, &pszGUID);
|
---|
363 | CoTaskMemFree(lpOLEStr);
|
---|
364 |
|
---|
365 | return RT_SUCCESS(rc) ? pszGUID : NULL;
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | return RTStrDup("{Default device}");
|
---|
370 | }
|
---|
371 |
|
---|
372 | static HRESULT directSoundPlayRestore(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB)
|
---|
373 | {
|
---|
374 | RT_NOREF(pThis);
|
---|
375 | HRESULT hr = IDirectSoundBuffer8_Restore(pDSB);
|
---|
376 | if (FAILED(hr))
|
---|
377 | DSLOG(("DSound: Restoring playback buffer\n"));
|
---|
378 | else
|
---|
379 | DSLOGREL(("DSound: Restoring playback buffer failed with %Rhrc\n", hr));
|
---|
380 |
|
---|
381 | return hr;
|
---|
382 | }
|
---|
383 |
|
---|
384 | static HRESULT directSoundPlayUnlock(PDRVHOSTDSOUND pThis, LPDIRECTSOUNDBUFFER8 pDSB,
|
---|
385 | PVOID pv1, PVOID pv2,
|
---|
386 | DWORD cb1, DWORD cb2)
|
---|
387 | {
|
---|
388 | RT_NOREF(pThis);
|
---|
389 | HRESULT hr = IDirectSoundBuffer8_Unlock(pDSB, pv1, cb1, pv2, cb2);
|
---|
390 | if (FAILED(hr))
|
---|
391 | DSLOGREL(("DSound: Unlocking playback buffer failed with %Rhrc\n", hr));
|
---|
392 | return hr;
|
---|
393 | }
|
---|
394 |
|
---|
395 | static HRESULT directSoundCaptureUnlock(LPDIRECTSOUNDCAPTUREBUFFER8 pDSCB,
|
---|
396 | PVOID pv1, PVOID pv2,
|
---|
397 | DWORD cb1, DWORD cb2)
|
---|
398 | {
|
---|
399 | HRESULT hr = IDirectSoundCaptureBuffer8_Unlock(pDSCB, pv1, cb1, pv2, cb2);
|
---|
400 | if (FAILED(hr))
|
---|
401 | DSLOGREL(("DSound: Unlocking capture buffer failed with %Rhrc\n", hr));
|
---|
402 | return hr;
|
---|
403 | }
|
---|
404 |
|
---|
405 | static HRESULT directSoundPlayLock(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
|
---|
406 | DWORD dwOffset, DWORD dwBytes,
|
---|
407 | PVOID *ppv1, PVOID *ppv2,
|
---|
408 | DWORD *pcb1, DWORD *pcb2,
|
---|
409 | DWORD dwFlags)
|
---|
410 | {
|
---|
411 | AssertReturn(dwBytes, VERR_INVALID_PARAMETER);
|
---|
412 |
|
---|
413 | HRESULT hr = E_FAIL;
|
---|
414 | AssertCompile(DRV_DSOUND_RESTORE_ATTEMPTS_MAX > 0);
|
---|
415 | for (unsigned i = 0; i < DRV_DSOUND_RESTORE_ATTEMPTS_MAX; i++)
|
---|
416 | {
|
---|
417 | PVOID pv1, pv2;
|
---|
418 | DWORD cb1, cb2;
|
---|
419 | hr = IDirectSoundBuffer8_Lock(pStreamDS->Out.pDSB, dwOffset, dwBytes, &pv1, &cb1, &pv2, &cb2, dwFlags);
|
---|
420 | if (SUCCEEDED(hr))
|
---|
421 | {
|
---|
422 | if ( (!pv1 || !(cb1 & pStreamDS->uAlign))
|
---|
423 | && (!pv2 || !(cb2 & pStreamDS->uAlign)))
|
---|
424 | {
|
---|
425 | if (ppv1)
|
---|
426 | *ppv1 = pv1;
|
---|
427 | if (ppv2)
|
---|
428 | *ppv2 = pv2;
|
---|
429 | if (pcb1)
|
---|
430 | *pcb1 = cb1;
|
---|
431 | if (pcb2)
|
---|
432 | *pcb2 = cb2;
|
---|
433 | return S_OK;
|
---|
434 | }
|
---|
435 | DSLOGREL(("DSound: Locking playback buffer returned misaligned buffer: cb1=%#RX32, cb2=%#RX32 (alignment: %#RX32)\n",
|
---|
436 | *pcb1, *pcb2, pStreamDS->uAlign));
|
---|
437 | directSoundPlayUnlock(pThis, pStreamDS->Out.pDSB, pv1, pv2, cb1, cb2);
|
---|
438 | return E_FAIL;
|
---|
439 | }
|
---|
440 |
|
---|
441 | if (hr != DSERR_BUFFERLOST)
|
---|
442 | break;
|
---|
443 |
|
---|
444 | LogFlowFunc(("Locking failed due to lost buffer, restoring ...\n"));
|
---|
445 | directSoundPlayRestore(pThis, pStreamDS->Out.pDSB);
|
---|
446 | }
|
---|
447 |
|
---|
448 | DSLOGREL(("DSound: Locking playback buffer failed with %Rhrc (dwOff=%ld, dwBytes=%ld)\n", hr, dwOffset, dwBytes));
|
---|
449 | return hr;
|
---|
450 | }
|
---|
451 |
|
---|
452 | static HRESULT directSoundCaptureLock(PDSOUNDSTREAM pStreamDS,
|
---|
453 | DWORD dwOffset, DWORD dwBytes,
|
---|
454 | PVOID *ppv1, PVOID *ppv2,
|
---|
455 | DWORD *pcb1, DWORD *pcb2,
|
---|
456 | DWORD dwFlags)
|
---|
457 | {
|
---|
458 | PVOID pv1 = NULL;
|
---|
459 | PVOID pv2 = NULL;
|
---|
460 | DWORD cb1 = 0;
|
---|
461 | DWORD cb2 = 0;
|
---|
462 |
|
---|
463 | HRESULT hr = IDirectSoundCaptureBuffer8_Lock(pStreamDS->In.pDSCB, dwOffset, dwBytes,
|
---|
464 | &pv1, &cb1, &pv2, &cb2, dwFlags);
|
---|
465 | if (FAILED(hr))
|
---|
466 | {
|
---|
467 | DSLOGREL(("DSound: Locking capture buffer failed with %Rhrc\n", hr));
|
---|
468 | return hr;
|
---|
469 | }
|
---|
470 |
|
---|
471 | if ( (pv1 && (cb1 & pStreamDS->uAlign))
|
---|
472 | || (pv2 && (cb2 & pStreamDS->uAlign)))
|
---|
473 | {
|
---|
474 | DSLOGREL(("DSound: Locking capture buffer returned misaligned buffer: cb1=%RI32, cb2=%RI32 (alignment: %RU32)\n",
|
---|
475 | cb1, cb2, pStreamDS->uAlign));
|
---|
476 | directSoundCaptureUnlock(pStreamDS->In.pDSCB, pv1, pv2, cb1, cb2);
|
---|
477 | return E_FAIL;
|
---|
478 | }
|
---|
479 |
|
---|
480 | *ppv1 = pv1;
|
---|
481 | *ppv2 = pv2;
|
---|
482 | *pcb1 = cb1;
|
---|
483 | *pcb2 = cb2;
|
---|
484 |
|
---|
485 | return S_OK;
|
---|
486 | }
|
---|
487 |
|
---|
488 | /*
|
---|
489 | * DirectSound playback
|
---|
490 | */
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Destroys a DirectSound playback interface.
|
---|
494 | *
|
---|
495 | * @param pDS Playback interface to destroy.
|
---|
496 | */
|
---|
497 | static void directSoundPlayInterfaceDestroy(LPDIRECTSOUND8 pDS)
|
---|
498 | {
|
---|
499 | if (pDS)
|
---|
500 | {
|
---|
501 | LogFlowFuncEnter();
|
---|
502 |
|
---|
503 | IDirectSound8_Release(pDS);
|
---|
504 | pDS = NULL;
|
---|
505 | }
|
---|
506 | }
|
---|
507 |
|
---|
508 | /**
|
---|
509 | * Creates a DirectSound playback interface.
|
---|
510 | *
|
---|
511 | * @return HRESULT
|
---|
512 | * @param pGUID GUID of device to create the playback interface for.
|
---|
513 | * @param ppDS Where to store the created interface. Optional.
|
---|
514 | */
|
---|
515 | static HRESULT directSoundPlayInterfaceCreate(LPCGUID pGUID, LPDIRECTSOUND8 *ppDS)
|
---|
516 | {
|
---|
517 | /* pGUID can be NULL, if this is the default device. */
|
---|
518 | /* ppDS is optional. */
|
---|
519 |
|
---|
520 | LogFlowFuncEnter();
|
---|
521 |
|
---|
522 | LPDIRECTSOUND8 pDS;
|
---|
523 | HRESULT hr = CoCreateInstance(CLSID_DirectSound8, NULL, CLSCTX_ALL,
|
---|
524 | IID_IDirectSound8, (void **)&pDS);
|
---|
525 | if (FAILED(hr))
|
---|
526 | {
|
---|
527 | DSLOGREL(("DSound: Creating playback instance failed with %Rhrc\n", hr));
|
---|
528 | }
|
---|
529 | else
|
---|
530 | {
|
---|
531 | hr = IDirectSound8_Initialize(pDS, pGUID);
|
---|
532 | if (SUCCEEDED(hr))
|
---|
533 | {
|
---|
534 | HWND hWnd = GetDesktopWindow();
|
---|
535 | hr = IDirectSound8_SetCooperativeLevel(pDS, hWnd, DSSCL_PRIORITY);
|
---|
536 | if (FAILED(hr))
|
---|
537 | DSLOGREL(("DSound: Setting cooperative level for window %p failed with %Rhrc\n", hWnd, hr));
|
---|
538 | }
|
---|
539 |
|
---|
540 | if (FAILED(hr))
|
---|
541 | {
|
---|
542 | if (hr == DSERR_NODRIVER) /* Usually means that no playback devices are attached. */
|
---|
543 | DSLOGREL(("DSound: DirectSound playback is currently unavailable\n"));
|
---|
544 | else
|
---|
545 | DSLOGREL(("DSound: DirectSound playback initialization failed with %Rhrc\n", hr));
|
---|
546 |
|
---|
547 | directSoundPlayInterfaceDestroy(pDS);
|
---|
548 | }
|
---|
549 | else if (ppDS)
|
---|
550 | {
|
---|
551 | *ppDS = pDS;
|
---|
552 | }
|
---|
553 | }
|
---|
554 |
|
---|
555 | LogFlowFunc(("Returning %Rhrc\n", hr));
|
---|
556 | return hr;
|
---|
557 | }
|
---|
558 |
|
---|
559 | static HRESULT directSoundPlayClose(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
|
---|
560 | {
|
---|
561 | AssertPtrReturn(pThis, E_POINTER);
|
---|
562 | AssertPtrReturn(pStreamDS, E_POINTER);
|
---|
563 |
|
---|
564 | LogFlowFuncEnter();
|
---|
565 |
|
---|
566 | HRESULT hr = directSoundPlayStop(pThis, pStreamDS, true /* fFlush */);
|
---|
567 | if (FAILED(hr))
|
---|
568 | return hr;
|
---|
569 |
|
---|
570 | DSLOG(("DSound: Closing playback stream\n"));
|
---|
571 |
|
---|
572 | if (pStreamDS->pCircBuf)
|
---|
573 | Assert(RTCircBufUsed(pStreamDS->pCircBuf) == 0);
|
---|
574 |
|
---|
575 | if (SUCCEEDED(hr))
|
---|
576 | {
|
---|
577 | RTCritSectEnter(&pThis->CritSect);
|
---|
578 |
|
---|
579 | if (pStreamDS->pCircBuf)
|
---|
580 | {
|
---|
581 | RTCircBufDestroy(pStreamDS->pCircBuf);
|
---|
582 | pStreamDS->pCircBuf = NULL;
|
---|
583 | }
|
---|
584 |
|
---|
585 | if (pStreamDS->Out.pDSB)
|
---|
586 | {
|
---|
587 | IDirectSoundBuffer8_Release(pStreamDS->Out.pDSB);
|
---|
588 | pStreamDS->Out.pDSB = NULL;
|
---|
589 | }
|
---|
590 |
|
---|
591 | pThis->pDSStrmOut = NULL;
|
---|
592 |
|
---|
593 | RTCritSectLeave(&pThis->CritSect);
|
---|
594 | }
|
---|
595 |
|
---|
596 | if (FAILED(hr))
|
---|
597 | DSLOGREL(("DSound: Stopping playback stream %p failed with %Rhrc\n", pStreamDS, hr));
|
---|
598 |
|
---|
599 | return hr;
|
---|
600 | }
|
---|
601 |
|
---|
602 | static HRESULT directSoundPlayOpen(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
|
---|
603 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
604 | {
|
---|
605 | AssertPtrReturn(pThis, E_POINTER);
|
---|
606 | AssertPtrReturn(pStreamDS, E_POINTER);
|
---|
607 | AssertPtrReturn(pCfgReq, E_POINTER);
|
---|
608 | AssertPtrReturn(pCfgAcq, E_POINTER);
|
---|
609 |
|
---|
610 | LogFlowFuncEnter();
|
---|
611 |
|
---|
612 | Assert(pStreamDS->Out.pDSB == NULL);
|
---|
613 |
|
---|
614 | DSLOG(("DSound: Opening playback stream (uHz=%RU32, cChannels=%RU8, cBits=%RU8, fSigned=%RTbool)\n",
|
---|
615 | pCfgReq->Props.uHz,
|
---|
616 | pCfgReq->Props.cChannels,
|
---|
617 | pCfgReq->Props.cBytes * 8,
|
---|
618 | 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 = DrvAudioHlpFramesToBytes(pCfgReq->Backend.cfBufferSize, &pCfgReq->Props);
|
---|
671 |
|
---|
672 | DSLOG(("DSound: Requested playback buffer is %RU64ms (%ld bytes)\n",
|
---|
673 | DrvAudioHlpBytesToMilli(bd.dwBufferBytes, &pCfgReq->Props), 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 | DrvAudioHlpBytesToMilli(bc.dwBufferBytes, &pCfgReq->Props), 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.cfBufferSize = cfBufSize;
|
---|
756 | pCfgAcq->Backend.cfPeriod = cfBufSize / 4;
|
---|
757 | pCfgAcq->Backend.cfPreBuf = pCfgAcq->Backend.cfPeriod * 2;
|
---|
758 |
|
---|
759 | } while (0);
|
---|
760 |
|
---|
761 | if (FAILED(hr))
|
---|
762 | directSoundPlayClose(pThis, pStreamDS);
|
---|
763 |
|
---|
764 | return hr;
|
---|
765 | }
|
---|
766 |
|
---|
767 | static 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 | DrvAudioHlpClearBuf(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 | */
|
---|
804 | static 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 >= DrvAudioHlpFramesToBytes(pStreamDS->Cfg.Backend.cfPreBuf, &pStreamDS->Cfg.Props))
|
---|
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 |
|
---|
925 | static 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 |
|
---|
959 | static 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 | */
|
---|
998 | static 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 | */
|
---|
1021 | static 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 | DrvAudioHlpClearBuf(&pStreamDS->Cfg.Props, pv1, cb1, PDMAUDIOPCMPROPS_B2F(&pStreamDS->Cfg.Props, cb1));
|
---|
1046 | if (pv2 && cb2)
|
---|
1047 | DrvAudioHlpClearBuf(&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 | DrvAudioHlpClearBuf(&pStreamDS->Cfg.Props, pv1, cb1, PDMAUDIOPCMPROPS_B2F(&pStreamDS->Cfg.Props, cb1));
|
---|
1081 | if (pv2 && cb2)
|
---|
1082 | DrvAudioHlpClearBuf(&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 | */
|
---|
1102 | static 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 |
|
---|
1130 | static 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->DestSource.Source)
|
---|
1143 | {
|
---|
1144 | case PDMAUDIORECSOURCE_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 PDMAUDIORECSOURCE_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 | DrvAudioHlpRecSrcToStr(pCfg->DestSource.Source), 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 | DrvAudioHlpRecSrcToStr(pCfg->DestSource.Source), 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 | */
|
---|
1205 | static 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: Capture buffer full, skipping to record data (%RU32 bytes)\n", cbUsed));
|
---|
1230 | pStreamDS->In.cOverruns++;
|
---|
1231 | }
|
---|
1232 |
|
---|
1233 | DWORD cbToCapture = RT_MIN(cbUsed, cbFree);
|
---|
1234 |
|
---|
1235 | Log3Func(("cbUsed=%ld, cbToCapture=%ld\n", cbUsed, cbToCapture));
|
---|
1236 |
|
---|
1237 | while (cbToCapture)
|
---|
1238 | {
|
---|
1239 | void *pvBuf;
|
---|
1240 | size_t cbBuf;
|
---|
1241 | RTCircBufAcquireWriteBlock(pCircBuf, cbToCapture, &pvBuf, &cbBuf);
|
---|
1242 |
|
---|
1243 | if (cbBuf)
|
---|
1244 | {
|
---|
1245 | PVOID pv1, pv2;
|
---|
1246 | DWORD cb1, cb2;
|
---|
1247 | hr = directSoundCaptureLock(pStreamDS, pStreamDS->In.offReadPos, (DWORD)cbBuf,
|
---|
1248 | &pv1, &pv2, &cb1, &cb2, 0 /* dwFlags */);
|
---|
1249 | if (FAILED(hr))
|
---|
1250 | break;
|
---|
1251 |
|
---|
1252 | AssertPtr(pv1);
|
---|
1253 | Assert(cb1);
|
---|
1254 |
|
---|
1255 | memcpy(pvBuf, pv1, cb1);
|
---|
1256 |
|
---|
1257 | if (pv2 && cb2) /* Buffer wrap-around? Write second part. */
|
---|
1258 | memcpy((uint8_t *)pvBuf + cb1, pv2, cb2);
|
---|
1259 |
|
---|
1260 | directSoundCaptureUnlock(pDSCB, pv1, pv2, cb1, cb2);
|
---|
1261 |
|
---|
1262 | pStreamDS->In.offReadPos = (pStreamDS->In.offReadPos + cb1 + cb2) % pStreamDS->cbBufSize;
|
---|
1263 |
|
---|
1264 | Assert(cbToCapture >= cbBuf);
|
---|
1265 | cbToCapture -= (uint32_t)cbBuf;
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | #ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
|
---|
1269 | if (cbBuf)
|
---|
1270 | {
|
---|
1271 | RTFILE fh;
|
---|
1272 | int rc2 = RTFileOpen(&fh, VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "dsoundCapture.pcm",
|
---|
1273 | RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
|
---|
1274 | if (RT_SUCCESS(rc2))
|
---|
1275 | {
|
---|
1276 | RTFileWrite(fh, pvBuf, cbBuf, NULL);
|
---|
1277 | RTFileClose(fh);
|
---|
1278 | }
|
---|
1279 | }
|
---|
1280 | #endif
|
---|
1281 | RTCircBufReleaseWriteBlock(pCircBuf, cbBuf);
|
---|
1282 | }
|
---|
1283 |
|
---|
1284 | return VINF_SUCCESS;
|
---|
1285 | }
|
---|
1286 |
|
---|
1287 | /**
|
---|
1288 | * Destroys a DirectSound capture interface.
|
---|
1289 | *
|
---|
1290 | * @param pDSC Capture interface to destroy.
|
---|
1291 | */
|
---|
1292 | static void directSoundCaptureInterfaceDestroy(LPDIRECTSOUNDCAPTURE8 pDSC)
|
---|
1293 | {
|
---|
1294 | if (pDSC)
|
---|
1295 | {
|
---|
1296 | LogFlowFuncEnter();
|
---|
1297 |
|
---|
1298 | IDirectSoundCapture_Release(pDSC);
|
---|
1299 | pDSC = NULL;
|
---|
1300 | }
|
---|
1301 | }
|
---|
1302 |
|
---|
1303 | /**
|
---|
1304 | * Creates a DirectSound capture interface.
|
---|
1305 | *
|
---|
1306 | * @return HRESULT
|
---|
1307 | * @param pGUID GUID of device to create the capture interface for.
|
---|
1308 | * @param ppDSC Where to store the created interface. Optional.
|
---|
1309 | */
|
---|
1310 | static HRESULT directSoundCaptureInterfaceCreate(LPCGUID pGUID, LPDIRECTSOUNDCAPTURE8 *ppDSC)
|
---|
1311 | {
|
---|
1312 | /* pGUID can be NULL, if this is the default device. */
|
---|
1313 | /* ppDSC is optional. */
|
---|
1314 |
|
---|
1315 | LogFlowFuncEnter();
|
---|
1316 |
|
---|
1317 | LPDIRECTSOUNDCAPTURE8 pDSC;
|
---|
1318 | HRESULT hr = CoCreateInstance(CLSID_DirectSoundCapture8, NULL, CLSCTX_ALL,
|
---|
1319 | IID_IDirectSoundCapture8, (void **)&pDSC);
|
---|
1320 | if (FAILED(hr))
|
---|
1321 | {
|
---|
1322 | DSLOGREL(("DSound: Creating capture instance failed with %Rhrc\n", hr));
|
---|
1323 | }
|
---|
1324 | else
|
---|
1325 | {
|
---|
1326 | hr = IDirectSoundCapture_Initialize(pDSC, pGUID);
|
---|
1327 | if (FAILED(hr))
|
---|
1328 | {
|
---|
1329 | if (hr == DSERR_NODRIVER) /* Usually means that no capture devices are attached. */
|
---|
1330 | DSLOGREL(("DSound: Capture device currently is unavailable\n"));
|
---|
1331 | else
|
---|
1332 | DSLOGREL(("DSound: Initializing capturing device failed with %Rhrc\n", hr));
|
---|
1333 |
|
---|
1334 | directSoundCaptureInterfaceDestroy(pDSC);
|
---|
1335 | }
|
---|
1336 | else if (ppDSC)
|
---|
1337 | {
|
---|
1338 | *ppDSC = pDSC;
|
---|
1339 | }
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | LogFlowFunc(("Returning %Rhrc\n", hr));
|
---|
1343 | return hr;
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | static HRESULT directSoundCaptureClose(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
|
---|
1347 | {
|
---|
1348 | AssertPtrReturn(pThis, E_POINTER);
|
---|
1349 | AssertPtrReturn(pStreamDS, E_POINTER);
|
---|
1350 |
|
---|
1351 | LogFlowFuncEnter();
|
---|
1352 |
|
---|
1353 | HRESULT hr = directSoundCaptureStop(pThis, pStreamDS, true /* fFlush */);
|
---|
1354 | if (FAILED(hr))
|
---|
1355 | return hr;
|
---|
1356 |
|
---|
1357 | if ( pStreamDS
|
---|
1358 | && pStreamDS->In.pDSCB)
|
---|
1359 | {
|
---|
1360 | DSLOG(("DSound: Closing capturing stream\n"));
|
---|
1361 |
|
---|
1362 | IDirectSoundCaptureBuffer8_Release(pStreamDS->In.pDSCB);
|
---|
1363 | pStreamDS->In.pDSCB = NULL;
|
---|
1364 | }
|
---|
1365 |
|
---|
1366 | LogFlowFunc(("Returning %Rhrc\n", hr));
|
---|
1367 | return hr;
|
---|
1368 | }
|
---|
1369 |
|
---|
1370 | static HRESULT directSoundCaptureOpen(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
|
---|
1371 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
1372 | {
|
---|
1373 | AssertPtrReturn(pThis, E_POINTER);
|
---|
1374 | AssertPtrReturn(pStreamDS, E_POINTER);
|
---|
1375 | AssertPtrReturn(pCfgReq, E_POINTER);
|
---|
1376 | AssertPtrReturn(pCfgAcq, E_POINTER);
|
---|
1377 |
|
---|
1378 | LogFlowFuncEnter();
|
---|
1379 |
|
---|
1380 | Assert(pStreamDS->In.pDSCB == NULL);
|
---|
1381 |
|
---|
1382 | DSLOG(("DSound: Opening capturing stream (uHz=%RU32, cChannels=%RU8, cBits=%RU8, fSigned=%RTbool)\n",
|
---|
1383 | pCfgReq->Props.uHz,
|
---|
1384 | pCfgReq->Props.cChannels,
|
---|
1385 | pCfgReq->Props.cBytes * 8,
|
---|
1386 | pCfgReq->Props.fSigned));
|
---|
1387 |
|
---|
1388 | WAVEFORMATEX wfx;
|
---|
1389 | int rc = dsoundWaveFmtFromCfg(pCfgReq, &wfx);
|
---|
1390 | if (RT_FAILURE(rc))
|
---|
1391 | return E_INVALIDARG;
|
---|
1392 |
|
---|
1393 | dsoundUpdateStatusInternal(pThis);
|
---|
1394 |
|
---|
1395 | HRESULT hr = directSoundCaptureInterfaceCreate(pThis->Cfg.pGuidCapture, &pThis->pDSC);
|
---|
1396 | if (FAILED(hr))
|
---|
1397 | return hr;
|
---|
1398 |
|
---|
1399 | do /* To use breaks. */
|
---|
1400 | {
|
---|
1401 | DSCBUFFERDESC bd;
|
---|
1402 | RT_ZERO(bd);
|
---|
1403 |
|
---|
1404 | bd.dwSize = sizeof(bd);
|
---|
1405 | bd.lpwfxFormat = &wfx;
|
---|
1406 | bd.dwBufferBytes = DrvAudioHlpFramesToBytes(pCfgReq->Backend.cfBufferSize, &pCfgReq->Props);
|
---|
1407 |
|
---|
1408 | DSLOG(("DSound: Requested capture buffer is %RU64ms (%ld bytes)\n",
|
---|
1409 | DrvAudioHlpBytesToMilli(bd.dwBufferBytes, &pCfgReq->Props), bd.dwBufferBytes));
|
---|
1410 |
|
---|
1411 | LPDIRECTSOUNDCAPTUREBUFFER pDSCB;
|
---|
1412 | hr = IDirectSoundCapture_CreateCaptureBuffer(pThis->pDSC, &bd, &pDSCB, NULL);
|
---|
1413 | if (FAILED(hr))
|
---|
1414 | {
|
---|
1415 | if (hr == E_ACCESSDENIED)
|
---|
1416 | {
|
---|
1417 | DSLOGREL(("DSound: Capturing input from host not possible, access denied\n"));
|
---|
1418 | }
|
---|
1419 | else
|
---|
1420 | DSLOGREL(("DSound: Creating capture buffer failed with %Rhrc\n", hr));
|
---|
1421 | break;
|
---|
1422 | }
|
---|
1423 |
|
---|
1424 | hr = IDirectSoundCaptureBuffer_QueryInterface(pDSCB, IID_IDirectSoundCaptureBuffer8, (void **)&pStreamDS->In.pDSCB);
|
---|
1425 | IDirectSoundCaptureBuffer_Release(pDSCB);
|
---|
1426 | if (FAILED(hr))
|
---|
1427 | {
|
---|
1428 | DSLOGREL(("DSound: Querying interface for capture buffer failed with %Rhrc\n", hr));
|
---|
1429 | break;
|
---|
1430 | }
|
---|
1431 |
|
---|
1432 | /*
|
---|
1433 | * Query the actual parameters.
|
---|
1434 | */
|
---|
1435 | DWORD offByteReadPos = 0;
|
---|
1436 | hr = IDirectSoundCaptureBuffer8_GetCurrentPosition(pStreamDS->In.pDSCB, NULL, &offByteReadPos);
|
---|
1437 | if (FAILED(hr))
|
---|
1438 | {
|
---|
1439 | offByteReadPos = 0;
|
---|
1440 | DSLOGREL(("DSound: Getting capture position failed with %Rhrc\n", hr));
|
---|
1441 | }
|
---|
1442 |
|
---|
1443 | RT_ZERO(wfx);
|
---|
1444 | hr = IDirectSoundCaptureBuffer8_GetFormat(pStreamDS->In.pDSCB, &wfx, sizeof(wfx), NULL);
|
---|
1445 | if (FAILED(hr))
|
---|
1446 | {
|
---|
1447 | DSLOGREL(("DSound: Getting capture format failed with %Rhrc\n", hr));
|
---|
1448 | break;
|
---|
1449 | }
|
---|
1450 |
|
---|
1451 | DSCBCAPS bc;
|
---|
1452 | RT_ZERO(bc);
|
---|
1453 | bc.dwSize = sizeof(bc);
|
---|
1454 | hr = IDirectSoundCaptureBuffer8_GetCaps(pStreamDS->In.pDSCB, &bc);
|
---|
1455 | if (FAILED(hr))
|
---|
1456 | {
|
---|
1457 | DSLOGREL(("DSound: Getting capture capabilities failed with %Rhrc\n", hr));
|
---|
1458 | break;
|
---|
1459 | }
|
---|
1460 |
|
---|
1461 | DSLOG(("DSound: Acquired capture buffer is %RU64ms (%ld bytes)\n",
|
---|
1462 | DrvAudioHlpBytesToMilli(bc.dwBufferBytes, &pCfgReq->Props), bc.dwBufferBytes));
|
---|
1463 |
|
---|
1464 | DSLOG(("DSound: Capture format:\n"
|
---|
1465 | " dwBufferBytes = %RI32\n"
|
---|
1466 | " dwFlags = 0x%x\n"
|
---|
1467 | " wFormatTag = %RI16\n"
|
---|
1468 | " nChannels = %RI16\n"
|
---|
1469 | " nSamplesPerSec = %RU32\n"
|
---|
1470 | " nAvgBytesPerSec = %RU32\n"
|
---|
1471 | " nBlockAlign = %RI16\n"
|
---|
1472 | " wBitsPerSample = %RI16\n"
|
---|
1473 | " cbSize = %RI16\n",
|
---|
1474 | bc.dwBufferBytes,
|
---|
1475 | bc.dwFlags,
|
---|
1476 | wfx.wFormatTag,
|
---|
1477 | wfx.nChannels,
|
---|
1478 | wfx.nSamplesPerSec,
|
---|
1479 | wfx.nAvgBytesPerSec,
|
---|
1480 | wfx.nBlockAlign,
|
---|
1481 | wfx.wBitsPerSample,
|
---|
1482 | wfx.cbSize));
|
---|
1483 |
|
---|
1484 | if (bc.dwBufferBytes & pStreamDS->uAlign)
|
---|
1485 | DSLOGREL(("DSound: Capture GetCaps returned misaligned buffer: size %RU32, alignment %RU32\n",
|
---|
1486 | bc.dwBufferBytes, pStreamDS->uAlign + 1));
|
---|
1487 |
|
---|
1488 | /* Initial state: reading at the initial capture position, no error. */
|
---|
1489 | pStreamDS->In.offReadPos = 0;
|
---|
1490 | pStreamDS->cbBufSize = bc.dwBufferBytes;
|
---|
1491 |
|
---|
1492 | rc = RTCircBufCreate(&pStreamDS->pCircBuf, pStreamDS->cbBufSize) * 2; /* Use "double buffering". */
|
---|
1493 | AssertRC(rc);
|
---|
1494 |
|
---|
1495 | pThis->pDSStrmIn = pStreamDS;
|
---|
1496 |
|
---|
1497 | pCfgAcq->Backend.cfBufferSize = PDMAUDIOSTREAMCFG_B2F(pCfgAcq, pStreamDS->cbBufSize);
|
---|
1498 |
|
---|
1499 | } while (0);
|
---|
1500 |
|
---|
1501 | if (FAILED(hr))
|
---|
1502 | directSoundCaptureClose(pThis, pStreamDS);
|
---|
1503 |
|
---|
1504 | LogFlowFunc(("Returning %Rhrc\n", hr));
|
---|
1505 | return hr;
|
---|
1506 | }
|
---|
1507 |
|
---|
1508 | static HRESULT directSoundCaptureStop(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, bool fFlush)
|
---|
1509 | {
|
---|
1510 | AssertPtrReturn(pThis, E_POINTER);
|
---|
1511 | AssertPtrReturn(pStreamDS, E_POINTER);
|
---|
1512 |
|
---|
1513 | RT_NOREF(pThis);
|
---|
1514 |
|
---|
1515 | HRESULT hr = S_OK;
|
---|
1516 |
|
---|
1517 | if (pStreamDS->In.pDSCB)
|
---|
1518 | {
|
---|
1519 | DSLOG(("DSound: Stopping capture\n"));
|
---|
1520 | hr = IDirectSoundCaptureBuffer_Stop(pStreamDS->In.pDSCB);
|
---|
1521 | }
|
---|
1522 |
|
---|
1523 | if (SUCCEEDED(hr))
|
---|
1524 | {
|
---|
1525 | if (fFlush)
|
---|
1526 | dsoundStreamReset(pThis, pStreamDS);
|
---|
1527 | }
|
---|
1528 |
|
---|
1529 | if (FAILED(hr))
|
---|
1530 | DSLOGREL(("DSound: Stopping capture buffer failed with %Rhrc\n", hr));
|
---|
1531 |
|
---|
1532 | return hr;
|
---|
1533 | }
|
---|
1534 |
|
---|
1535 | static HRESULT directSoundCaptureStart(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
|
---|
1536 | {
|
---|
1537 | AssertPtrReturn(pThis, E_POINTER);
|
---|
1538 | AssertPtrReturn(pStreamDS, E_POINTER);
|
---|
1539 |
|
---|
1540 | HRESULT hr;
|
---|
1541 | if (pStreamDS->In.pDSCB)
|
---|
1542 | {
|
---|
1543 | DWORD dwStatus;
|
---|
1544 | hr = IDirectSoundCaptureBuffer8_GetStatus(pStreamDS->In.pDSCB, &dwStatus);
|
---|
1545 | if (FAILED(hr))
|
---|
1546 | {
|
---|
1547 | DSLOGREL(("DSound: Retrieving capture status failed with %Rhrc\n", hr));
|
---|
1548 | }
|
---|
1549 | else
|
---|
1550 | {
|
---|
1551 | if (dwStatus & DSCBSTATUS_CAPTURING)
|
---|
1552 | {
|
---|
1553 | DSLOG(("DSound: Already capturing\n"));
|
---|
1554 | }
|
---|
1555 | else
|
---|
1556 | {
|
---|
1557 | const DWORD fFlags = DSCBSTART_LOOPING;
|
---|
1558 |
|
---|
1559 | DSLOG(("DSound: Starting to capture\n"));
|
---|
1560 | hr = IDirectSoundCaptureBuffer8_Start(pStreamDS->In.pDSCB, fFlags);
|
---|
1561 | if (FAILED(hr))
|
---|
1562 | DSLOGREL(("DSound: Starting to capture failed with %Rhrc\n", hr));
|
---|
1563 | }
|
---|
1564 | }
|
---|
1565 | }
|
---|
1566 | else
|
---|
1567 | hr = E_UNEXPECTED;
|
---|
1568 |
|
---|
1569 | LogFlowFunc(("Returning %Rhrc\n", hr));
|
---|
1570 | return hr;
|
---|
1571 | }
|
---|
1572 |
|
---|
1573 | /**
|
---|
1574 | * Callback for the playback device enumeration.
|
---|
1575 | *
|
---|
1576 | * @return TRUE if continuing enumeration, FALSE if not.
|
---|
1577 | * @param pGUID Pointer to GUID of enumerated device. Can be NULL.
|
---|
1578 | * @param pwszDescription Pointer to (friendly) description of enumerated device.
|
---|
1579 | * @param pwszModule Pointer to module name of enumerated device.
|
---|
1580 | * @param lpContext Pointer to PDSOUNDENUMCBCTX context for storing the enumerated information.
|
---|
1581 | */
|
---|
1582 | static BOOL CALLBACK dsoundDevicesEnumCbPlayback(LPGUID pGUID, LPCWSTR pwszDescription, LPCWSTR pwszModule, PVOID lpContext)
|
---|
1583 | {
|
---|
1584 | RT_NOREF(pwszModule);
|
---|
1585 |
|
---|
1586 | PDSOUNDENUMCBCTX pEnumCtx = (PDSOUNDENUMCBCTX)lpContext;
|
---|
1587 | AssertPtrReturn(pEnumCtx , FALSE);
|
---|
1588 |
|
---|
1589 | PPDMAUDIODEVICEENUM pDevEnm = pEnumCtx->pDevEnm;
|
---|
1590 | AssertPtrReturn(pDevEnm, FALSE);
|
---|
1591 |
|
---|
1592 | /* pGUID can be NULL for default device(s). */
|
---|
1593 | AssertPtrReturn(pwszDescription, FALSE);
|
---|
1594 | /* Do not care about pwszModule. */
|
---|
1595 |
|
---|
1596 | int rc;
|
---|
1597 |
|
---|
1598 | PPDMAUDIODEVICE pDev = DrvAudioHlpDeviceAlloc(sizeof(DSOUNDDEV));
|
---|
1599 | if (pDev)
|
---|
1600 | {
|
---|
1601 | pDev->enmUsage = PDMAUDIODIR_OUT;
|
---|
1602 | pDev->enmType = PDMAUDIODEVICETYPE_BUILTIN;
|
---|
1603 |
|
---|
1604 | if (pGUID == NULL)
|
---|
1605 | pDev->fFlags = PDMAUDIODEV_FLAGS_DEFAULT;
|
---|
1606 |
|
---|
1607 | char *pszName;
|
---|
1608 | rc = RTUtf16ToUtf8(pwszDescription, &pszName);
|
---|
1609 | if (RT_SUCCESS(rc))
|
---|
1610 | {
|
---|
1611 | RTStrCopy(pDev->szName, sizeof(pDev->szName), pszName);
|
---|
1612 | RTStrFree(pszName);
|
---|
1613 |
|
---|
1614 | PDSOUNDDEV pDSoundDev = (PDSOUNDDEV)pDev->pvData;
|
---|
1615 |
|
---|
1616 | if (pGUID)
|
---|
1617 | memcpy(&pDSoundDev->Guid, pGUID, sizeof(GUID));
|
---|
1618 |
|
---|
1619 | LPDIRECTSOUND8 pDS;
|
---|
1620 | HRESULT hr = directSoundPlayInterfaceCreate(pGUID, &pDS);
|
---|
1621 | if (SUCCEEDED(hr))
|
---|
1622 | {
|
---|
1623 | do
|
---|
1624 | {
|
---|
1625 | DSCAPS DSCaps;
|
---|
1626 | RT_ZERO(DSCaps);
|
---|
1627 | DSCaps.dwSize = sizeof(DSCAPS);
|
---|
1628 | hr = IDirectSound_GetCaps(pDS, &DSCaps);
|
---|
1629 | if (FAILED(hr))
|
---|
1630 | break;
|
---|
1631 |
|
---|
1632 | pDev->cMaxOutputChannels = DSCaps.dwFlags & DSCAPS_PRIMARYSTEREO ? 2 : 1;
|
---|
1633 |
|
---|
1634 | DWORD dwSpeakerCfg;
|
---|
1635 | hr = IDirectSound_GetSpeakerConfig(pDS, &dwSpeakerCfg);
|
---|
1636 | if (FAILED(hr))
|
---|
1637 | break;
|
---|
1638 |
|
---|
1639 | unsigned uSpeakerCount = 0;
|
---|
1640 | switch (DSSPEAKER_CONFIG(dwSpeakerCfg))
|
---|
1641 | {
|
---|
1642 | case DSSPEAKER_MONO: uSpeakerCount = 1; break;
|
---|
1643 | case DSSPEAKER_HEADPHONE: uSpeakerCount = 2; break;
|
---|
1644 | case DSSPEAKER_STEREO: uSpeakerCount = 2; break;
|
---|
1645 | case DSSPEAKER_QUAD: uSpeakerCount = 4; break;
|
---|
1646 | case DSSPEAKER_SURROUND: uSpeakerCount = 4; break;
|
---|
1647 | case DSSPEAKER_5POINT1: uSpeakerCount = 6; break;
|
---|
1648 | case DSSPEAKER_5POINT1_SURROUND: uSpeakerCount = 6; break;
|
---|
1649 | case DSSPEAKER_7POINT1: uSpeakerCount = 8; break;
|
---|
1650 | case DSSPEAKER_7POINT1_SURROUND: uSpeakerCount = 8; break;
|
---|
1651 | default: break;
|
---|
1652 | }
|
---|
1653 |
|
---|
1654 | if (uSpeakerCount) /* Do we need to update the channel count? */
|
---|
1655 | pDev->cMaxOutputChannels = uSpeakerCount;
|
---|
1656 |
|
---|
1657 | } while (0);
|
---|
1658 |
|
---|
1659 | directSoundPlayInterfaceDestroy(pDS);
|
---|
1660 |
|
---|
1661 | rc = VINF_SUCCESS;
|
---|
1662 | }
|
---|
1663 | else
|
---|
1664 | rc = VERR_GENERAL_FAILURE;
|
---|
1665 |
|
---|
1666 | if (RT_SUCCESS(rc))
|
---|
1667 | rc = DrvAudioHlpDeviceEnumAdd(pDevEnm, pDev);
|
---|
1668 | }
|
---|
1669 | }
|
---|
1670 | else
|
---|
1671 | rc = VERR_NO_MEMORY;
|
---|
1672 |
|
---|
1673 | if (RT_FAILURE(rc))
|
---|
1674 | {
|
---|
1675 | LogRel(("DSound: Error enumeration playback device '%ls', rc=%Rrc\n", pwszDescription, rc));
|
---|
1676 | return FALSE; /* Abort enumeration. */
|
---|
1677 | }
|
---|
1678 |
|
---|
1679 | return TRUE;
|
---|
1680 | }
|
---|
1681 |
|
---|
1682 | /**
|
---|
1683 | * Callback for the capture device enumeration.
|
---|
1684 | *
|
---|
1685 | * @return TRUE if continuing enumeration, FALSE if not.
|
---|
1686 | * @param pGUID Pointer to GUID of enumerated device. Can be NULL.
|
---|
1687 | * @param pwszDescription Pointer to (friendly) description of enumerated device.
|
---|
1688 | * @param pwszModule Pointer to module name of enumerated device.
|
---|
1689 | * @param lpContext Pointer to PDSOUNDENUMCBCTX context for storing the enumerated information.
|
---|
1690 | */
|
---|
1691 | static BOOL CALLBACK dsoundDevicesEnumCbCapture(LPGUID pGUID, LPCWSTR pwszDescription, LPCWSTR pwszModule, PVOID lpContext)
|
---|
1692 | {
|
---|
1693 | RT_NOREF(pwszModule);
|
---|
1694 |
|
---|
1695 | PDSOUNDENUMCBCTX pEnumCtx = (PDSOUNDENUMCBCTX )lpContext;
|
---|
1696 | AssertPtrReturn(pEnumCtx , FALSE);
|
---|
1697 |
|
---|
1698 | PPDMAUDIODEVICEENUM pDevEnm = pEnumCtx->pDevEnm;
|
---|
1699 | AssertPtrReturn(pDevEnm, FALSE);
|
---|
1700 |
|
---|
1701 | /* pGUID can be NULL for default device(s). */
|
---|
1702 | AssertPtrReturn(pwszDescription, FALSE);
|
---|
1703 | /* Do not care about pwszModule. */
|
---|
1704 |
|
---|
1705 | int rc;
|
---|
1706 |
|
---|
1707 | PPDMAUDIODEVICE pDev = DrvAudioHlpDeviceAlloc(sizeof(DSOUNDDEV));
|
---|
1708 | if (pDev)
|
---|
1709 | {
|
---|
1710 | pDev->enmUsage = PDMAUDIODIR_IN;
|
---|
1711 | pDev->enmType = PDMAUDIODEVICETYPE_BUILTIN;
|
---|
1712 |
|
---|
1713 | char *pszName;
|
---|
1714 | rc = RTUtf16ToUtf8(pwszDescription, &pszName);
|
---|
1715 | if (RT_SUCCESS(rc))
|
---|
1716 | {
|
---|
1717 | RTStrCopy(pDev->szName, sizeof(pDev->szName), pszName);
|
---|
1718 | RTStrFree(pszName);
|
---|
1719 |
|
---|
1720 | PDSOUNDDEV pDSoundDev = (PDSOUNDDEV)pDev->pvData;
|
---|
1721 |
|
---|
1722 | if (pGUID)
|
---|
1723 | memcpy(&pDSoundDev->Guid, pGUID, sizeof(GUID));
|
---|
1724 |
|
---|
1725 | LPDIRECTSOUNDCAPTURE8 pDSC;
|
---|
1726 | HRESULT hr = directSoundCaptureInterfaceCreate(pGUID, &pDSC);
|
---|
1727 | if (SUCCEEDED(hr))
|
---|
1728 | {
|
---|
1729 | do
|
---|
1730 | {
|
---|
1731 | DSCCAPS DSCCaps;
|
---|
1732 | RT_ZERO(DSCCaps);
|
---|
1733 | DSCCaps.dwSize = sizeof(DSCCAPS);
|
---|
1734 | hr = IDirectSoundCapture_GetCaps(pDSC, &DSCCaps);
|
---|
1735 | if (FAILED(hr))
|
---|
1736 | break;
|
---|
1737 |
|
---|
1738 | pDev->cMaxInputChannels = DSCCaps.dwChannels;
|
---|
1739 |
|
---|
1740 | } while (0);
|
---|
1741 |
|
---|
1742 | directSoundCaptureInterfaceDestroy(pDSC);
|
---|
1743 |
|
---|
1744 | rc = VINF_SUCCESS;
|
---|
1745 | }
|
---|
1746 | else
|
---|
1747 | rc = VERR_GENERAL_FAILURE;
|
---|
1748 |
|
---|
1749 | if (RT_SUCCESS(rc))
|
---|
1750 | rc = DrvAudioHlpDeviceEnumAdd(pDevEnm, pDev);
|
---|
1751 | }
|
---|
1752 | }
|
---|
1753 | else
|
---|
1754 | rc = VERR_NO_MEMORY;
|
---|
1755 |
|
---|
1756 | if (RT_FAILURE(rc))
|
---|
1757 | {
|
---|
1758 | LogRel(("DSound: Error enumeration capture device '%ls', rc=%Rrc\n", pwszDescription, rc));
|
---|
1759 | return FALSE; /* Abort enumeration. */
|
---|
1760 | }
|
---|
1761 |
|
---|
1762 | return TRUE;
|
---|
1763 | }
|
---|
1764 |
|
---|
1765 | /**
|
---|
1766 | * Does a (Re-)enumeration of the host's playback + capturing devices.
|
---|
1767 | *
|
---|
1768 | * @return IPRT status code.
|
---|
1769 | * @param pThis Host audio driver instance.
|
---|
1770 | * @param pDevEnm Where to store the enumerated devices.
|
---|
1771 | */
|
---|
1772 | static int dsoundDevicesEnumerate(PDRVHOSTDSOUND pThis, PPDMAUDIODEVICEENUM pDevEnm)
|
---|
1773 | {
|
---|
1774 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
1775 |
|
---|
1776 | DSLOG(("DSound: Enumerating devices ...\n"));
|
---|
1777 |
|
---|
1778 | RTLDRMOD hDSound = NULL;
|
---|
1779 | int rc = RTLdrLoadSystem("dsound.dll", true /*fNoUnload*/, &hDSound);
|
---|
1780 | if (RT_SUCCESS(rc))
|
---|
1781 | {
|
---|
1782 | DSOUNDENUMCBCTX EnumCtx;
|
---|
1783 | EnumCtx.fFlags = 0;
|
---|
1784 | EnumCtx.pDevEnm = pDevEnm;
|
---|
1785 |
|
---|
1786 | PFNDIRECTSOUNDENUMERATEW pfnDirectSoundEnumerateW = NULL;
|
---|
1787 | rc = RTLdrGetSymbol(hDSound, "DirectSoundEnumerateW", (void**)&pfnDirectSoundEnumerateW);
|
---|
1788 | if (RT_SUCCESS(rc))
|
---|
1789 | {
|
---|
1790 | HRESULT hr = pfnDirectSoundEnumerateW(&dsoundDevicesEnumCbPlayback, &EnumCtx);
|
---|
1791 | if (FAILED(hr))
|
---|
1792 | LogRel(("DSound: Error enumerating host playback devices: %Rhrc\n", hr));
|
---|
1793 | }
|
---|
1794 | else
|
---|
1795 | LogRel(("DSound: Error starting to enumerate host playback devices: %Rrc\n", rc));
|
---|
1796 |
|
---|
1797 | PFNDIRECTSOUNDCAPTUREENUMERATEW pfnDirectSoundCaptureEnumerateW = NULL;
|
---|
1798 | rc = RTLdrGetSymbol(hDSound, "DirectSoundCaptureEnumerateW", (void**)&pfnDirectSoundCaptureEnumerateW);
|
---|
1799 | if (RT_SUCCESS(rc))
|
---|
1800 | {
|
---|
1801 | HRESULT hr = pfnDirectSoundCaptureEnumerateW(&dsoundDevicesEnumCbCapture, &EnumCtx);
|
---|
1802 | if (FAILED(hr))
|
---|
1803 | LogRel(("DSound: Error enumerating host capture devices: %Rhrc\n", hr));
|
---|
1804 | }
|
---|
1805 | else
|
---|
1806 | LogRel(("DSound: Error starting to enumerate host capture devices: %Rrc\n", rc));
|
---|
1807 |
|
---|
1808 | RTLdrClose(hDSound);
|
---|
1809 | }
|
---|
1810 | else
|
---|
1811 | {
|
---|
1812 | /* No dsound.dll on this system. */
|
---|
1813 | LogRel(("DSound: Could not load dsound.dll: %Rrc\n", rc));
|
---|
1814 | }
|
---|
1815 |
|
---|
1816 | DSLOG(("DSound: Enumerating devices done\n"));
|
---|
1817 |
|
---|
1818 | return rc;
|
---|
1819 | }
|
---|
1820 |
|
---|
1821 | /**
|
---|
1822 | * Updates this host driver's internal status, according to the global, overall input/output
|
---|
1823 | * state and all connected (native) audio streams.
|
---|
1824 | *
|
---|
1825 | * @param pThis Host audio driver instance.
|
---|
1826 | */
|
---|
1827 | static void dsoundUpdateStatusInternal(PDRVHOSTDSOUND pThis)
|
---|
1828 | {
|
---|
1829 | AssertPtrReturnVoid(pThis);
|
---|
1830 | /* pCfg is optional. */
|
---|
1831 |
|
---|
1832 | LogFlowFuncEnter();
|
---|
1833 |
|
---|
1834 | int rc = dsoundDevicesEnumerate(pThis, &pThis->DeviceEnum);
|
---|
1835 | if (RT_SUCCESS(rc))
|
---|
1836 | {
|
---|
1837 | #if 0
|
---|
1838 | if ( pThis->fEnabledOut != RT_BOOL(cbCtx.cDevOut)
|
---|
1839 | || pThis->fEnabledIn != RT_BOOL(cbCtx.cDevIn))
|
---|
1840 | {
|
---|
1841 | /** @todo Use a registered callback to the audio connector (e.g "OnConfigurationChanged") to
|
---|
1842 | * let the connector know that something has changed within the host backend. */
|
---|
1843 | }
|
---|
1844 | #endif
|
---|
1845 | pThis->fEnabledIn = RT_BOOL(DrvAudioHlpDeviceEnumGetDeviceCount(&pThis->DeviceEnum, PDMAUDIODIR_IN));
|
---|
1846 | pThis->fEnabledOut = RT_BOOL(DrvAudioHlpDeviceEnumGetDeviceCount(&pThis->DeviceEnum, PDMAUDIODIR_OUT));
|
---|
1847 | }
|
---|
1848 |
|
---|
1849 | LogFlowFuncLeaveRC(rc);
|
---|
1850 | }
|
---|
1851 |
|
---|
1852 | static int dsoundCreateStreamOut(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
|
---|
1853 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
1854 | {
|
---|
1855 | LogFlowFunc(("pStreamDS=%p, pCfgReq=%p\n", pStreamDS, pCfgReq));
|
---|
1856 |
|
---|
1857 | int rc = VINF_SUCCESS;
|
---|
1858 |
|
---|
1859 | /* Try to open playback in case the device is already there. */
|
---|
1860 | HRESULT hr = directSoundPlayOpen(pThis, pStreamDS, pCfgReq, pCfgAcq);
|
---|
1861 | if (SUCCEEDED(hr))
|
---|
1862 | {
|
---|
1863 | rc = DrvAudioHlpStreamCfgCopy(&pStreamDS->Cfg, pCfgAcq);
|
---|
1864 | if (RT_SUCCESS(rc))
|
---|
1865 | dsoundStreamReset(pThis, pStreamDS);
|
---|
1866 | }
|
---|
1867 | else
|
---|
1868 | rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
1869 |
|
---|
1870 | LogFlowFuncLeaveRC(rc);
|
---|
1871 | return rc;
|
---|
1872 | }
|
---|
1873 |
|
---|
1874 | static int dsoundControlStreamOut(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
1875 | {
|
---|
1876 | LogFlowFunc(("pStreamDS=%p, cmd=%d\n", pStreamDS, enmStreamCmd));
|
---|
1877 |
|
---|
1878 | int rc = VINF_SUCCESS;
|
---|
1879 |
|
---|
1880 | HRESULT hr;
|
---|
1881 | switch (enmStreamCmd)
|
---|
1882 | {
|
---|
1883 | case PDMAUDIOSTREAMCMD_ENABLE:
|
---|
1884 | {
|
---|
1885 | dsoundStreamEnable(pThis, pStreamDS, true /* fEnable */);
|
---|
1886 | break;
|
---|
1887 | }
|
---|
1888 |
|
---|
1889 | case PDMAUDIOSTREAMCMD_RESUME:
|
---|
1890 | {
|
---|
1891 | hr = directSoundPlayStart(pThis, pStreamDS);
|
---|
1892 | if (FAILED(hr))
|
---|
1893 | rc = VERR_NOT_SUPPORTED; /** @todo Fix this. */
|
---|
1894 | break;
|
---|
1895 | }
|
---|
1896 |
|
---|
1897 | case PDMAUDIOSTREAMCMD_DISABLE:
|
---|
1898 | {
|
---|
1899 | dsoundStreamEnable(pThis, pStreamDS, false /* fEnable */);
|
---|
1900 | hr = directSoundPlayStop(pThis, pStreamDS, true /* fFlush */);
|
---|
1901 | if (FAILED(hr))
|
---|
1902 | rc = VERR_NOT_SUPPORTED;
|
---|
1903 | break;
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 | case PDMAUDIOSTREAMCMD_PAUSE:
|
---|
1907 | {
|
---|
1908 | hr = directSoundPlayStop(pThis, pStreamDS, false /* fFlush */);
|
---|
1909 | if (FAILED(hr))
|
---|
1910 | rc = VERR_NOT_SUPPORTED;
|
---|
1911 | break;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | case PDMAUDIOSTREAMCMD_DRAIN:
|
---|
1915 | {
|
---|
1916 | /* Make sure we transferred everything. */
|
---|
1917 | pStreamDS->fEnabled = true;
|
---|
1918 | pStreamDS->Out.fDrain = true;
|
---|
1919 | rc = dsoundPlayTransfer(pThis, pStreamDS);
|
---|
1920 | if ( RT_SUCCESS(rc)
|
---|
1921 | && pStreamDS->Out.fFirstTransfer)
|
---|
1922 | {
|
---|
1923 | /* If this was the first transfer ever for this stream, make sure to also play the (short) audio data. */
|
---|
1924 | DSLOG(("DSound: Started playing output (short sound)\n"));
|
---|
1925 |
|
---|
1926 | pStreamDS->Out.fFirstTransfer = false;
|
---|
1927 | pStreamDS->Out.cbLastTransferred = pStreamDS->Out.cbTransferred; /* All transferred audio data must be played. */
|
---|
1928 | pStreamDS->Out.tsLastTransferredMs = RTTimeMilliTS();
|
---|
1929 |
|
---|
1930 | hr = directSoundPlayStart(pThis, pStreamDS);
|
---|
1931 | if (FAILED(hr))
|
---|
1932 | rc = VERR_ACCESS_DENIED; /** @todo Find a better rc. */
|
---|
1933 | }
|
---|
1934 | break;
|
---|
1935 | }
|
---|
1936 |
|
---|
1937 | case PDMAUDIOSTREAMCMD_DROP:
|
---|
1938 | {
|
---|
1939 | pStreamDS->Out.cbLastTransferred = 0;
|
---|
1940 | pStreamDS->Out.tsLastTransferredMs = 0;
|
---|
1941 | RTCircBufReset(pStreamDS->pCircBuf);
|
---|
1942 | break;
|
---|
1943 | }
|
---|
1944 |
|
---|
1945 | default:
|
---|
1946 | rc = VERR_NOT_SUPPORTED;
|
---|
1947 | break;
|
---|
1948 | }
|
---|
1949 |
|
---|
1950 | LogFlowFuncLeaveRC(rc);
|
---|
1951 | return rc;
|
---|
1952 | }
|
---|
1953 |
|
---|
1954 | /**
|
---|
1955 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
|
---|
1956 | */
|
---|
1957 | int drvHostDSoundStreamPlay(PPDMIHOSTAUDIO pInterface,
|
---|
1958 | PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf, uint32_t cxBuf, uint32_t *pcxWritten)
|
---|
1959 | {
|
---|
1960 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
1961 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
1962 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
1963 | AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
|
---|
1964 | /* pcxWritten is optional. */
|
---|
1965 |
|
---|
1966 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
1967 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
1968 |
|
---|
1969 | int rc = VINF_SUCCESS;
|
---|
1970 |
|
---|
1971 | uint32_t cbWrittenTotal = 0;
|
---|
1972 |
|
---|
1973 | uint8_t *pbBuf = (uint8_t *)pvBuf;
|
---|
1974 | PRTCIRCBUF pCircBuf = pStreamDS->pCircBuf;
|
---|
1975 |
|
---|
1976 | uint32_t cbToPlay = RT_MIN(cxBuf, (uint32_t)RTCircBufFree(pCircBuf));
|
---|
1977 | while (cbToPlay)
|
---|
1978 | {
|
---|
1979 | void *pvChunk;
|
---|
1980 | size_t cbChunk;
|
---|
1981 | RTCircBufAcquireWriteBlock(pCircBuf, cbToPlay, &pvChunk, &cbChunk);
|
---|
1982 |
|
---|
1983 | if (cbChunk)
|
---|
1984 | {
|
---|
1985 | memcpy(pvChunk, pbBuf, cbChunk);
|
---|
1986 |
|
---|
1987 | pbBuf += cbChunk;
|
---|
1988 | Assert(cbToPlay >= cbChunk);
|
---|
1989 | cbToPlay -= (uint32_t)cbChunk;
|
---|
1990 |
|
---|
1991 | cbWrittenTotal += (uint32_t)cbChunk;
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | RTCircBufReleaseWriteBlock(pCircBuf, cbChunk);
|
---|
1995 | }
|
---|
1996 |
|
---|
1997 | Assert(cbWrittenTotal <= cxBuf);
|
---|
1998 | Assert(cbWrittenTotal == cxBuf);
|
---|
1999 |
|
---|
2000 | pStreamDS->Out.cbWritten += cbWrittenTotal;
|
---|
2001 |
|
---|
2002 | if (RT_SUCCESS(rc))
|
---|
2003 | {
|
---|
2004 | if (pcxWritten)
|
---|
2005 | *pcxWritten = cbWrittenTotal;
|
---|
2006 | }
|
---|
2007 | else
|
---|
2008 | dsoundUpdateStatusInternal(pThis);
|
---|
2009 |
|
---|
2010 | return rc;
|
---|
2011 | }
|
---|
2012 |
|
---|
2013 | static int dsoundDestroyStreamOut(PDRVHOSTDSOUND pThis, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2014 | {
|
---|
2015 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2016 |
|
---|
2017 | LogFlowFuncEnter();
|
---|
2018 |
|
---|
2019 | HRESULT hr = directSoundPlayStop(pThis, pStreamDS, true /* fFlush */);
|
---|
2020 | if (SUCCEEDED(hr))
|
---|
2021 | {
|
---|
2022 | hr = directSoundPlayClose(pThis, pStreamDS);
|
---|
2023 | if (FAILED(hr))
|
---|
2024 | return VERR_GENERAL_FAILURE; /** @todo Fix. */
|
---|
2025 | }
|
---|
2026 |
|
---|
2027 | return VINF_SUCCESS;
|
---|
2028 | }
|
---|
2029 |
|
---|
2030 | static int dsoundCreateStreamIn(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS,
|
---|
2031 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
2032 | {
|
---|
2033 | LogFunc(("pStreamDS=%p, pCfgReq=%p, enmRecSource=%s\n",
|
---|
2034 | pStreamDS, pCfgReq, DrvAudioHlpRecSrcToStr(pCfgReq->DestSource.Source)));
|
---|
2035 |
|
---|
2036 | int rc = VINF_SUCCESS;
|
---|
2037 |
|
---|
2038 | /* Try to open capture in case the device is already there. */
|
---|
2039 | HRESULT hr = directSoundCaptureOpen(pThis, pStreamDS, pCfgReq, pCfgAcq);
|
---|
2040 | if (SUCCEEDED(hr))
|
---|
2041 | {
|
---|
2042 | rc = DrvAudioHlpStreamCfgCopy(&pStreamDS->Cfg, pCfgAcq);
|
---|
2043 | if (RT_SUCCESS(rc))
|
---|
2044 | dsoundStreamReset(pThis, pStreamDS);
|
---|
2045 | }
|
---|
2046 | else
|
---|
2047 | rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
2048 |
|
---|
2049 | return rc;
|
---|
2050 | }
|
---|
2051 |
|
---|
2052 | static int dsoundControlStreamIn(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2053 | {
|
---|
2054 | LogFlowFunc(("pStreamDS=%p, enmStreamCmd=%ld\n", pStreamDS, enmStreamCmd));
|
---|
2055 |
|
---|
2056 | int rc = VINF_SUCCESS;
|
---|
2057 |
|
---|
2058 | HRESULT hr;
|
---|
2059 | switch (enmStreamCmd)
|
---|
2060 | {
|
---|
2061 | case PDMAUDIOSTREAMCMD_ENABLE:
|
---|
2062 | dsoundStreamEnable(pThis, pStreamDS, true /* fEnable */);
|
---|
2063 | RT_FALL_THROUGH();
|
---|
2064 | case PDMAUDIOSTREAMCMD_RESUME:
|
---|
2065 | {
|
---|
2066 | /* Try to start capture. If it fails, then reopen and try again. */
|
---|
2067 | hr = directSoundCaptureStart(pThis, pStreamDS);
|
---|
2068 | if (FAILED(hr))
|
---|
2069 | {
|
---|
2070 | hr = directSoundCaptureClose(pThis, pStreamDS);
|
---|
2071 | if (SUCCEEDED(hr))
|
---|
2072 | {
|
---|
2073 | PDMAUDIOSTREAMCFG CfgAcq;
|
---|
2074 | hr = directSoundCaptureOpen(pThis, pStreamDS, &pStreamDS->Cfg /* pCfgReq */, &CfgAcq);
|
---|
2075 | if (SUCCEEDED(hr))
|
---|
2076 | {
|
---|
2077 | rc = DrvAudioHlpStreamCfgCopy(&pStreamDS->Cfg, &CfgAcq);
|
---|
2078 | if (RT_FAILURE(rc))
|
---|
2079 | break;
|
---|
2080 |
|
---|
2081 | /** @todo What to do if the format has changed? */
|
---|
2082 |
|
---|
2083 | hr = directSoundCaptureStart(pThis, pStreamDS);
|
---|
2084 | }
|
---|
2085 | }
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | if (FAILED(hr))
|
---|
2089 | rc = VERR_NOT_SUPPORTED;
|
---|
2090 | break;
|
---|
2091 | }
|
---|
2092 |
|
---|
2093 | case PDMAUDIOSTREAMCMD_DISABLE:
|
---|
2094 | dsoundStreamEnable(pThis, pStreamDS, false /* fEnable */);
|
---|
2095 | RT_FALL_THROUGH();
|
---|
2096 | case PDMAUDIOSTREAMCMD_PAUSE:
|
---|
2097 | {
|
---|
2098 | directSoundCaptureStop(pThis, pStreamDS,
|
---|
2099 | enmStreamCmd == PDMAUDIOSTREAMCMD_DISABLE /* fFlush */);
|
---|
2100 |
|
---|
2101 | /* Return success in any case, as stopping the capture can fail if
|
---|
2102 | * the capture buffer is not around anymore.
|
---|
2103 | *
|
---|
2104 | * This can happen if the host's capturing device has been changed suddenly. */
|
---|
2105 | rc = VINF_SUCCESS;
|
---|
2106 | break;
|
---|
2107 | }
|
---|
2108 |
|
---|
2109 | default:
|
---|
2110 | rc = VERR_NOT_SUPPORTED;
|
---|
2111 | break;
|
---|
2112 | }
|
---|
2113 |
|
---|
2114 | return rc;
|
---|
2115 | }
|
---|
2116 |
|
---|
2117 | /**
|
---|
2118 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
|
---|
2119 | */
|
---|
2120 | int drvHostDSoundStreamCapture(PPDMIHOSTAUDIO pInterface,
|
---|
2121 | PPDMAUDIOBACKENDSTREAM pStream, void *pvBuf, uint32_t cxBuf, uint32_t *pcxRead)
|
---|
2122 | {
|
---|
2123 |
|
---|
2124 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2125 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2126 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
2127 | AssertReturn(cxBuf, VERR_INVALID_PARAMETER);
|
---|
2128 |
|
---|
2129 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2130 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2131 |
|
---|
2132 | int rc = VINF_SUCCESS;
|
---|
2133 |
|
---|
2134 | uint32_t cbReadTotal = 0;
|
---|
2135 |
|
---|
2136 | uint32_t cbToRead = RT_MIN((uint32_t)RTCircBufUsed(pStreamDS->pCircBuf), cxBuf);
|
---|
2137 | while (cbToRead)
|
---|
2138 | {
|
---|
2139 | void *pvChunk;
|
---|
2140 | size_t cbChunk;
|
---|
2141 | RTCircBufAcquireReadBlock(pStreamDS->pCircBuf, cbToRead, &pvChunk, &cbChunk);
|
---|
2142 |
|
---|
2143 | if (cbChunk)
|
---|
2144 | {
|
---|
2145 | memcpy((uint8_t *)pvBuf + cbReadTotal, pvChunk, cbChunk);
|
---|
2146 | cbReadTotal += (uint32_t)cbChunk;
|
---|
2147 | Assert(cbToRead >= cbChunk);
|
---|
2148 | cbToRead -= (uint32_t)cbChunk;
|
---|
2149 | }
|
---|
2150 |
|
---|
2151 | RTCircBufReleaseReadBlock(pStreamDS->pCircBuf, cbChunk);
|
---|
2152 | }
|
---|
2153 |
|
---|
2154 | if (RT_SUCCESS(rc))
|
---|
2155 | {
|
---|
2156 | if (pcxRead)
|
---|
2157 | *pcxRead = cbReadTotal;
|
---|
2158 | }
|
---|
2159 | else
|
---|
2160 | dsoundUpdateStatusInternal(pThis);
|
---|
2161 |
|
---|
2162 | return rc;
|
---|
2163 | }
|
---|
2164 |
|
---|
2165 | static int dsoundDestroyStreamIn(PDRVHOSTDSOUND pThis, PDSOUNDSTREAM pStreamDS)
|
---|
2166 | {
|
---|
2167 | LogFlowFuncEnter();
|
---|
2168 |
|
---|
2169 | directSoundCaptureClose(pThis, pStreamDS);
|
---|
2170 |
|
---|
2171 | return VINF_SUCCESS;
|
---|
2172 | }
|
---|
2173 |
|
---|
2174 | /**
|
---|
2175 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
|
---|
2176 | */
|
---|
2177 | int drvHostDSoundGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
|
---|
2178 | {
|
---|
2179 | RT_NOREF(pInterface);
|
---|
2180 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2181 | AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
|
---|
2182 |
|
---|
2183 | RT_BZERO(pBackendCfg, sizeof(PPDMAUDIOBACKENDCFG));
|
---|
2184 |
|
---|
2185 | pBackendCfg->cbStreamOut = sizeof(DSOUNDSTREAM);
|
---|
2186 | pBackendCfg->cbStreamIn = sizeof(DSOUNDSTREAM);
|
---|
2187 |
|
---|
2188 | RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "DirectSound audio driver");
|
---|
2189 |
|
---|
2190 | pBackendCfg->cMaxStreamsIn = UINT32_MAX;
|
---|
2191 | pBackendCfg->cMaxStreamsOut = UINT32_MAX;
|
---|
2192 |
|
---|
2193 | return VINF_SUCCESS;
|
---|
2194 | }
|
---|
2195 |
|
---|
2196 | /**
|
---|
2197 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetDevices}
|
---|
2198 | */
|
---|
2199 | static DECLCALLBACK(int) drvHostDSoundGetDevices(PPDMIHOSTAUDIO pInterface, PPDMAUDIODEVICEENUM pDeviceEnum)
|
---|
2200 | {
|
---|
2201 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2202 | AssertPtrReturn(pDeviceEnum, VERR_INVALID_POINTER);
|
---|
2203 |
|
---|
2204 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2205 |
|
---|
2206 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
2207 | if (RT_SUCCESS(rc))
|
---|
2208 | {
|
---|
2209 | rc = DrvAudioHlpDeviceEnumInit(pDeviceEnum);
|
---|
2210 | if (RT_SUCCESS(rc))
|
---|
2211 | {
|
---|
2212 | rc = dsoundDevicesEnumerate(pThis, pDeviceEnum);
|
---|
2213 | if (RT_FAILURE(rc))
|
---|
2214 | DrvAudioHlpDeviceEnumFree(pDeviceEnum);
|
---|
2215 | }
|
---|
2216 |
|
---|
2217 | int rc2 = RTCritSectLeave(&pThis->CritSect);
|
---|
2218 | AssertRC(rc2);
|
---|
2219 | }
|
---|
2220 |
|
---|
2221 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
2222 | return rc;
|
---|
2223 | }
|
---|
2224 |
|
---|
2225 | /**
|
---|
2226 | * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
|
---|
2227 | */
|
---|
2228 | void drvHostDSoundShutdown(PPDMIHOSTAUDIO pInterface)
|
---|
2229 | {
|
---|
2230 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2231 |
|
---|
2232 | LogFlowFuncEnter();
|
---|
2233 |
|
---|
2234 | RT_NOREF(pThis);
|
---|
2235 |
|
---|
2236 | LogFlowFuncLeave();
|
---|
2237 | }
|
---|
2238 |
|
---|
2239 | /**
|
---|
2240 | * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
|
---|
2241 | */
|
---|
2242 | static DECLCALLBACK(int) drvHostDSoundInit(PPDMIHOSTAUDIO pInterface)
|
---|
2243 | {
|
---|
2244 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2245 | LogFlowFuncEnter();
|
---|
2246 |
|
---|
2247 | int rc;
|
---|
2248 |
|
---|
2249 | /* Verify that IDirectSound is available. */
|
---|
2250 | LPDIRECTSOUND pDirectSound = NULL;
|
---|
2251 | HRESULT hr = CoCreateInstance(CLSID_DirectSound, NULL, CLSCTX_ALL, IID_IDirectSound, (void **)&pDirectSound);
|
---|
2252 | if (SUCCEEDED(hr))
|
---|
2253 | {
|
---|
2254 | IDirectSound_Release(pDirectSound);
|
---|
2255 |
|
---|
2256 | rc = VINF_SUCCESS;
|
---|
2257 |
|
---|
2258 | dsoundUpdateStatusInternal(pThis);
|
---|
2259 | }
|
---|
2260 | else
|
---|
2261 | {
|
---|
2262 | DSLOGREL(("DSound: DirectSound not available: %Rhrc\n", hr));
|
---|
2263 | rc = VERR_NOT_SUPPORTED;
|
---|
2264 | }
|
---|
2265 |
|
---|
2266 | LogFlowFuncLeaveRC(rc);
|
---|
2267 | return rc;
|
---|
2268 | }
|
---|
2269 |
|
---|
2270 | static LPCGUID dsoundConfigQueryGUID(PCFGMNODE pCfg, const char *pszName, RTUUID *pUuid)
|
---|
2271 | {
|
---|
2272 | LPCGUID pGuid = NULL;
|
---|
2273 |
|
---|
2274 | char *pszGuid = NULL;
|
---|
2275 | int rc = CFGMR3QueryStringAlloc(pCfg, pszName, &pszGuid);
|
---|
2276 | if (RT_SUCCESS(rc))
|
---|
2277 | {
|
---|
2278 | rc = RTUuidFromStr(pUuid, pszGuid);
|
---|
2279 | if (RT_SUCCESS(rc))
|
---|
2280 | pGuid = (LPCGUID)&pUuid;
|
---|
2281 | else
|
---|
2282 | DSLOGREL(("DSound: Error parsing device GUID for device '%s': %Rrc\n", pszName, rc));
|
---|
2283 |
|
---|
2284 | RTStrFree(pszGuid);
|
---|
2285 | }
|
---|
2286 |
|
---|
2287 | return pGuid;
|
---|
2288 | }
|
---|
2289 |
|
---|
2290 | static int dsoundConfigInit(PDRVHOSTDSOUND pThis, PCFGMNODE pCfg)
|
---|
2291 | {
|
---|
2292 | pThis->Cfg.pGuidPlay = dsoundConfigQueryGUID(pCfg, "DeviceGuidOut", &pThis->Cfg.uuidPlay);
|
---|
2293 | pThis->Cfg.pGuidCapture = dsoundConfigQueryGUID(pCfg, "DeviceGuidIn", &pThis->Cfg.uuidCapture);
|
---|
2294 |
|
---|
2295 | DSLOG(("DSound: Configuration: DeviceGuidOut {%RTuuid}, DeviceGuidIn {%RTuuid}\n",
|
---|
2296 | &pThis->Cfg.uuidPlay,
|
---|
2297 | &pThis->Cfg.uuidCapture));
|
---|
2298 |
|
---|
2299 | return VINF_SUCCESS;
|
---|
2300 | }
|
---|
2301 |
|
---|
2302 | /**
|
---|
2303 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
|
---|
2304 | */
|
---|
2305 | static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostDSoundGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
|
---|
2306 | {
|
---|
2307 | RT_NOREF(enmDir);
|
---|
2308 | AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
|
---|
2309 |
|
---|
2310 | return PDMAUDIOBACKENDSTS_RUNNING;
|
---|
2311 | }
|
---|
2312 |
|
---|
2313 | /**
|
---|
2314 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
|
---|
2315 | */
|
---|
2316 | static DECLCALLBACK(int) drvHostDSoundStreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2317 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
2318 | {
|
---|
2319 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2320 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2321 | AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
|
---|
2322 | AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
|
---|
2323 |
|
---|
2324 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2325 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2326 |
|
---|
2327 | int rc;
|
---|
2328 | if (pCfgReq->enmDir == PDMAUDIODIR_IN)
|
---|
2329 | rc = dsoundCreateStreamIn(pThis, pStreamDS, pCfgReq, pCfgAcq);
|
---|
2330 | else
|
---|
2331 | rc = dsoundCreateStreamOut(pThis, pStreamDS, pCfgReq, pCfgAcq);
|
---|
2332 |
|
---|
2333 | if (RT_SUCCESS(rc))
|
---|
2334 | {
|
---|
2335 | rc = DrvAudioHlpStreamCfgCopy(&pStreamDS->Cfg, pCfgAcq);
|
---|
2336 | if (RT_SUCCESS(rc))
|
---|
2337 | rc = RTCritSectInit(&pStreamDS->CritSect);
|
---|
2338 | }
|
---|
2339 |
|
---|
2340 | return rc;
|
---|
2341 | }
|
---|
2342 |
|
---|
2343 | /**
|
---|
2344 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
|
---|
2345 | */
|
---|
2346 | static DECLCALLBACK(int) drvHostDSoundStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2347 | {
|
---|
2348 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2349 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2350 |
|
---|
2351 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2352 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2353 |
|
---|
2354 | int rc;
|
---|
2355 | if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
2356 | rc = dsoundDestroyStreamIn(pThis, pStreamDS);
|
---|
2357 | else
|
---|
2358 | rc = dsoundDestroyStreamOut(pThis, pStreamDS);
|
---|
2359 |
|
---|
2360 | if (RT_SUCCESS(rc))
|
---|
2361 | {
|
---|
2362 | if (RTCritSectIsInitialized(&pStreamDS->CritSect))
|
---|
2363 | rc = RTCritSectDelete(&pStreamDS->CritSect);
|
---|
2364 | }
|
---|
2365 |
|
---|
2366 | return rc;
|
---|
2367 | }
|
---|
2368 |
|
---|
2369 | /**
|
---|
2370 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
|
---|
2371 | */
|
---|
2372 | static DECLCALLBACK(int) drvHostDSoundStreamControl(PPDMIHOSTAUDIO pInterface,
|
---|
2373 | PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2374 | {
|
---|
2375 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2376 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2377 |
|
---|
2378 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2379 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2380 |
|
---|
2381 | int rc;
|
---|
2382 | if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
2383 | rc = dsoundControlStreamIn(pThis, pStreamDS, enmStreamCmd);
|
---|
2384 | else
|
---|
2385 | rc = dsoundControlStreamOut(pThis, pStreamDS, enmStreamCmd);
|
---|
2386 |
|
---|
2387 | return rc;
|
---|
2388 | }
|
---|
2389 |
|
---|
2390 | /**
|
---|
2391 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
|
---|
2392 | */
|
---|
2393 | static DECLCALLBACK(uint32_t) drvHostDSoundStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2394 | {
|
---|
2395 | RT_NOREF(pInterface);
|
---|
2396 | AssertPtrReturn(pStream, PDMAUDIOSTREAMSTS_FLAG_NONE);
|
---|
2397 |
|
---|
2398 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2399 |
|
---|
2400 | if ( pStreamDS->fEnabled
|
---|
2401 | && pStreamDS->pCircBuf)
|
---|
2402 | {
|
---|
2403 | return (uint32_t)RTCircBufUsed(pStreamDS->pCircBuf);
|
---|
2404 | }
|
---|
2405 |
|
---|
2406 | return 0;
|
---|
2407 | }
|
---|
2408 |
|
---|
2409 | /**
|
---|
2410 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
|
---|
2411 | */
|
---|
2412 | static DECLCALLBACK(uint32_t) drvHostDSoundStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2413 | {
|
---|
2414 | AssertPtrReturn(pInterface, PDMAUDIOSTREAMSTS_FLAG_NONE);
|
---|
2415 | AssertPtrReturn(pStream, PDMAUDIOSTREAMSTS_FLAG_NONE);
|
---|
2416 |
|
---|
2417 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2418 |
|
---|
2419 | if (pStreamDS->fEnabled)
|
---|
2420 | return (uint32_t)RTCircBufFree(pStreamDS->pCircBuf);
|
---|
2421 |
|
---|
2422 | return 0;
|
---|
2423 | }
|
---|
2424 |
|
---|
2425 | /**
|
---|
2426 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetPending}
|
---|
2427 | */
|
---|
2428 | static DECLCALLBACK(uint32_t) drvHostDSoundStreamGetPending(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2429 | {
|
---|
2430 | RT_NOREF(pInterface);
|
---|
2431 | AssertPtrReturn(pStream, 0);
|
---|
2432 |
|
---|
2433 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2434 |
|
---|
2435 | if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
2436 | {
|
---|
2437 | uint32_t cbPending = 0;
|
---|
2438 |
|
---|
2439 | /* Any uncommitted data left? */
|
---|
2440 | if (pStreamDS->pCircBuf)
|
---|
2441 | cbPending = (uint32_t)RTCircBufUsed(pStreamDS->pCircBuf);
|
---|
2442 |
|
---|
2443 | /* Check if we have committed data which still needs to be played by
|
---|
2444 | * by DirectSound's streaming buffer. */
|
---|
2445 | if (!cbPending)
|
---|
2446 | {
|
---|
2447 | const uint64_t diffLastTransferredMs = RTTimeMilliTS() - pStreamDS->Out.tsLastTransferredMs;
|
---|
2448 | const uint64_t uLastTranserredChunkMs = DrvAudioHlpBytesToMilli(pStreamDS->Out.cbLastTransferred, &pStreamDS->Cfg.Props);
|
---|
2449 | if ( uLastTranserredChunkMs
|
---|
2450 | && diffLastTransferredMs < uLastTranserredChunkMs)
|
---|
2451 | cbPending = 1;
|
---|
2452 |
|
---|
2453 | Log3Func(("diffLastTransferredMs=%RU64ms, uLastTranserredChunkMs=%RU64ms (%RU32 bytes) -> cbPending=%RU32\n",
|
---|
2454 | diffLastTransferredMs, uLastTranserredChunkMs, pStreamDS->Out.cbLastTransferred, cbPending));
|
---|
2455 | }
|
---|
2456 | else
|
---|
2457 | Log3Func(("cbPending=%RU32\n", cbPending));
|
---|
2458 |
|
---|
2459 | return cbPending;
|
---|
2460 | }
|
---|
2461 | /* Note: For input streams we never have pending data left. */
|
---|
2462 |
|
---|
2463 | return 0;
|
---|
2464 | }
|
---|
2465 |
|
---|
2466 | /**
|
---|
2467 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
|
---|
2468 | */
|
---|
2469 | static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvHostDSoundStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2470 | {
|
---|
2471 | RT_NOREF(pInterface);
|
---|
2472 | AssertPtrReturn(pStream, PDMAUDIOSTREAMSTS_FLAG_NONE);
|
---|
2473 |
|
---|
2474 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2475 |
|
---|
2476 | PDMAUDIOSTREAMSTS strmSts = PDMAUDIOSTREAMSTS_FLAG_INITIALIZED;
|
---|
2477 |
|
---|
2478 | if (pStreamDS->fEnabled)
|
---|
2479 | strmSts |= PDMAUDIOSTREAMSTS_FLAG_ENABLED;
|
---|
2480 |
|
---|
2481 | return strmSts;
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | /**
|
---|
2485 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
|
---|
2486 | */
|
---|
2487 | static DECLCALLBACK(int) drvHostDSoundStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2488 | {
|
---|
2489 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2490 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2491 |
|
---|
2492 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2493 | PDSOUNDSTREAM pStreamDS = (PDSOUNDSTREAM)pStream;
|
---|
2494 |
|
---|
2495 | if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
2496 | {
|
---|
2497 | return dsoundCaptureTransfer(pThis, pStreamDS);
|
---|
2498 | }
|
---|
2499 | else if (pStreamDS->Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
2500 | {
|
---|
2501 | return dsoundPlayTransfer(pThis, pStreamDS);
|
---|
2502 | }
|
---|
2503 |
|
---|
2504 | return VINF_SUCCESS;
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2508 | /**
|
---|
2509 | * @interface_method_impl{PDMIHOSTAUDIO,pfnSetCallback}
|
---|
2510 | */
|
---|
2511 | static DECLCALLBACK(int) drvHostDSoundSetCallback(PPDMIHOSTAUDIO pInterface, PFNPDMHOSTAUDIOCALLBACK pfnCallback)
|
---|
2512 | {
|
---|
2513 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2514 | /* pfnCallback will be handled below. */
|
---|
2515 |
|
---|
2516 | PDRVHOSTDSOUND pThis = PDMIHOSTAUDIO_2_DRVHOSTDSOUND(pInterface);
|
---|
2517 |
|
---|
2518 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
2519 | if (RT_SUCCESS(rc))
|
---|
2520 | {
|
---|
2521 | LogFunc(("pfnCallback=%p\n", pfnCallback));
|
---|
2522 |
|
---|
2523 | if (pfnCallback) /* Register. */
|
---|
2524 | {
|
---|
2525 | Assert(pThis->pfnCallback == NULL);
|
---|
2526 | pThis->pfnCallback = pfnCallback;
|
---|
2527 |
|
---|
2528 | #ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
|
---|
2529 | if (pThis->m_pNotificationClient)
|
---|
2530 | pThis->m_pNotificationClient->RegisterCallback(pThis->pDrvIns, pfnCallback);
|
---|
2531 | #endif
|
---|
2532 | }
|
---|
2533 | else /* Unregister. */
|
---|
2534 | {
|
---|
2535 | if (pThis->pfnCallback)
|
---|
2536 | pThis->pfnCallback = NULL;
|
---|
2537 |
|
---|
2538 | #ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
|
---|
2539 | if (pThis->m_pNotificationClient)
|
---|
2540 | pThis->m_pNotificationClient->UnregisterCallback();
|
---|
2541 | #endif
|
---|
2542 | }
|
---|
2543 |
|
---|
2544 | int rc2 = RTCritSectLeave(&pThis->CritSect);
|
---|
2545 | AssertRC(rc2);
|
---|
2546 | }
|
---|
2547 |
|
---|
2548 | return rc;
|
---|
2549 | }
|
---|
2550 | #endif
|
---|
2551 |
|
---|
2552 |
|
---|
2553 | /*********************************************************************************************************************************
|
---|
2554 | * PDMDRVINS::IBase Interface *
|
---|
2555 | *********************************************************************************************************************************/
|
---|
2556 |
|
---|
2557 | /**
|
---|
2558 | * @callback_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
2559 | */
|
---|
2560 | static DECLCALLBACK(void *) drvHostDSoundQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
2561 | {
|
---|
2562 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
2563 | PDRVHOSTDSOUND pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDSOUND);
|
---|
2564 |
|
---|
2565 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
2566 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
|
---|
2567 | return NULL;
|
---|
2568 | }
|
---|
2569 |
|
---|
2570 |
|
---|
2571 | /*********************************************************************************************************************************
|
---|
2572 | * PDMDRVREG Interface *
|
---|
2573 | *********************************************************************************************************************************/
|
---|
2574 |
|
---|
2575 | /**
|
---|
2576 | * @callback_method_impl{FNPDMDRVDESTRUCT, pfnDestruct}
|
---|
2577 | */
|
---|
2578 | static DECLCALLBACK(void) drvHostDSoundDestruct(PPDMDRVINS pDrvIns)
|
---|
2579 | {
|
---|
2580 | PDRVHOSTDSOUND pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDSOUND);
|
---|
2581 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
2582 |
|
---|
2583 | LogFlowFuncEnter();
|
---|
2584 |
|
---|
2585 | #ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
|
---|
2586 | if (pThis->m_pNotificationClient)
|
---|
2587 | {
|
---|
2588 | pThis->m_pNotificationClient->Dispose();
|
---|
2589 | pThis->m_pNotificationClient->Release();
|
---|
2590 |
|
---|
2591 | pThis->m_pNotificationClient = NULL;
|
---|
2592 | }
|
---|
2593 | #endif
|
---|
2594 |
|
---|
2595 | DrvAudioHlpDeviceEnumFree(&pThis->DeviceEnum);
|
---|
2596 |
|
---|
2597 | if (pThis->pDrvIns)
|
---|
2598 | CoUninitialize();
|
---|
2599 |
|
---|
2600 | int rc2 = RTCritSectDelete(&pThis->CritSect);
|
---|
2601 | AssertRC(rc2);
|
---|
2602 |
|
---|
2603 | LogFlowFuncLeave();
|
---|
2604 | }
|
---|
2605 |
|
---|
2606 | /**
|
---|
2607 | * @callback_method_impl{FNPDMDRVCONSTRUCT,
|
---|
2608 | * Construct a DirectSound Audio driver instance.}
|
---|
2609 | */
|
---|
2610 | static DECLCALLBACK(int) drvHostDSoundConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
2611 | {
|
---|
2612 | RT_NOREF(fFlags);
|
---|
2613 | PDRVHOSTDSOUND pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDSOUND);
|
---|
2614 |
|
---|
2615 | LogRel(("Audio: Initializing DirectSound audio driver\n"));
|
---|
2616 |
|
---|
2617 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
2618 |
|
---|
2619 | HRESULT hr = CoInitializeEx(NULL, COINIT_MULTITHREADED);
|
---|
2620 | if (FAILED(hr))
|
---|
2621 | {
|
---|
2622 | DSLOGREL(("DSound: CoInitializeEx failed with %Rhrc\n", hr));
|
---|
2623 | return VERR_NOT_SUPPORTED;
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 | /*
|
---|
2627 | * Init basic data members and interfaces.
|
---|
2628 | */
|
---|
2629 | pThis->pDrvIns = pDrvIns;
|
---|
2630 | /* IBase */
|
---|
2631 | pDrvIns->IBase.pfnQueryInterface = drvHostDSoundQueryInterface;
|
---|
2632 | /* IHostAudio */
|
---|
2633 | PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostDSound);
|
---|
2634 | pThis->IHostAudio.pfnStreamGetPending = drvHostDSoundStreamGetPending;
|
---|
2635 |
|
---|
2636 | /* This backend supports device enumeration. */
|
---|
2637 | pThis->IHostAudio.pfnGetDevices = drvHostDSoundGetDevices;
|
---|
2638 |
|
---|
2639 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2640 | /* This backend supports host audio callbacks. */
|
---|
2641 | pThis->IHostAudio.pfnSetCallback = drvHostDSoundSetCallback;
|
---|
2642 | pThis->pfnCallback = NULL;
|
---|
2643 | #endif
|
---|
2644 |
|
---|
2645 | /*
|
---|
2646 | * Init the static parts.
|
---|
2647 | */
|
---|
2648 | DrvAudioHlpDeviceEnumInit(&pThis->DeviceEnum);
|
---|
2649 |
|
---|
2650 | pThis->fEnabledIn = false;
|
---|
2651 | pThis->fEnabledOut = false;
|
---|
2652 |
|
---|
2653 | int rc = VINF_SUCCESS;
|
---|
2654 |
|
---|
2655 | #ifdef VBOX_WITH_AUDIO_MMNOTIFICATION_CLIENT
|
---|
2656 | bool fUseNotificationClient = false;
|
---|
2657 |
|
---|
2658 | char szOSVersion[32];
|
---|
2659 | rc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szOSVersion, sizeof(szOSVersion));
|
---|
2660 | if (RT_SUCCESS(rc))
|
---|
2661 | {
|
---|
2662 | /* IMMNotificationClient is available starting at Windows Vista. */
|
---|
2663 | if (RTStrVersionCompare(szOSVersion, "6.0") >= 0)
|
---|
2664 | fUseNotificationClient = true;
|
---|
2665 | }
|
---|
2666 |
|
---|
2667 | if (fUseNotificationClient)
|
---|
2668 | {
|
---|
2669 | try
|
---|
2670 | {
|
---|
2671 | pThis->m_pNotificationClient = new VBoxMMNotificationClient();
|
---|
2672 |
|
---|
2673 | HRESULT hr = pThis->m_pNotificationClient->Initialize();
|
---|
2674 | if (FAILED(hr))
|
---|
2675 | rc = VERR_AUDIO_BACKEND_INIT_FAILED;
|
---|
2676 | }
|
---|
2677 | catch (std::bad_alloc &ex)
|
---|
2678 | {
|
---|
2679 | NOREF(ex);
|
---|
2680 | rc = VERR_NO_MEMORY;
|
---|
2681 | }
|
---|
2682 | }
|
---|
2683 |
|
---|
2684 | LogRel2(("DSound: Notification client is %s\n", fUseNotificationClient ? "enabled" : "disabled"));
|
---|
2685 | #endif
|
---|
2686 |
|
---|
2687 | if (RT_SUCCESS(rc))
|
---|
2688 | {
|
---|
2689 | /*
|
---|
2690 | * Initialize configuration values.
|
---|
2691 | */
|
---|
2692 | rc = dsoundConfigInit(pThis, pCfg);
|
---|
2693 | if (RT_SUCCESS(rc))
|
---|
2694 | rc = RTCritSectInit(&pThis->CritSect);
|
---|
2695 | }
|
---|
2696 |
|
---|
2697 | return rc;
|
---|
2698 | }
|
---|
2699 |
|
---|
2700 |
|
---|
2701 | /**
|
---|
2702 | * PDM driver registration.
|
---|
2703 | */
|
---|
2704 | const PDMDRVREG g_DrvHostDSound =
|
---|
2705 | {
|
---|
2706 | /* u32Version */
|
---|
2707 | PDM_DRVREG_VERSION,
|
---|
2708 | /* szName */
|
---|
2709 | "DSoundAudio",
|
---|
2710 | /* szRCMod */
|
---|
2711 | "",
|
---|
2712 | /* szR0Mod */
|
---|
2713 | "",
|
---|
2714 | /* pszDescription */
|
---|
2715 | "DirectSound Audio host driver",
|
---|
2716 | /* fFlags */
|
---|
2717 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
2718 | /* fClass. */
|
---|
2719 | PDM_DRVREG_CLASS_AUDIO,
|
---|
2720 | /* cMaxInstances */
|
---|
2721 | ~0U,
|
---|
2722 | /* cbInstance */
|
---|
2723 | sizeof(DRVHOSTDSOUND),
|
---|
2724 | /* pfnConstruct */
|
---|
2725 | drvHostDSoundConstruct,
|
---|
2726 | /* pfnDestruct */
|
---|
2727 | drvHostDSoundDestruct,
|
---|
2728 | /* pfnRelocate */
|
---|
2729 | NULL,
|
---|
2730 | /* pfnIOCtl */
|
---|
2731 | NULL,
|
---|
2732 | /* pfnPowerOn */
|
---|
2733 | NULL,
|
---|
2734 | /* pfnReset */
|
---|
2735 | NULL,
|
---|
2736 | /* pfnSuspend */
|
---|
2737 | NULL,
|
---|
2738 | /* pfnResume */
|
---|
2739 | NULL,
|
---|
2740 | /* pfnAttach */
|
---|
2741 | NULL,
|
---|
2742 | /* pfnDetach */
|
---|
2743 | NULL,
|
---|
2744 | /* pfnPowerOff */
|
---|
2745 | NULL,
|
---|
2746 | /* pfnSoftReset */
|
---|
2747 | NULL,
|
---|
2748 | /* u32EndVersion */
|
---|
2749 | PDM_DRVREG_VERSION
|
---|
2750 | };
|
---|
2751 |
|
---|