1 | /* $Id: DrvHostAudioWasApi.cpp 93115 2022-01-01 11:31:46Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Host audio driver - Windows Audio Session API.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2021-2022 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 | /*#define INITGUID - defined in VBoxhostAudioDSound.cpp already */
|
---|
24 | #include <VBox/log.h>
|
---|
25 | #include <iprt/win/windows.h>
|
---|
26 | #include <Mmdeviceapi.h>
|
---|
27 | #include <iprt/win/audioclient.h>
|
---|
28 | #include <functiondiscoverykeys_devpkey.h>
|
---|
29 | #include <AudioSessionTypes.h>
|
---|
30 | #ifndef AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY
|
---|
31 | # define AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY UINT32_C(0x08000000)
|
---|
32 | #endif
|
---|
33 | #ifndef AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM
|
---|
34 | # define AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM UINT32_C(0x80000000)
|
---|
35 | #endif
|
---|
36 |
|
---|
37 | #include <VBox/vmm/pdmaudioinline.h>
|
---|
38 | #include <VBox/vmm/pdmaudiohostenuminline.h>
|
---|
39 |
|
---|
40 | #include <iprt/rand.h>
|
---|
41 | #include <iprt/semaphore.h>
|
---|
42 | #include <iprt/utf16.h>
|
---|
43 | #include <iprt/uuid.h>
|
---|
44 |
|
---|
45 | #include <new> /* std::bad_alloc */
|
---|
46 |
|
---|
47 | #include "VBoxDD.h"
|
---|
48 |
|
---|
49 |
|
---|
50 | /*********************************************************************************************************************************
|
---|
51 | * Defined Constants And Macros *
|
---|
52 | *********************************************************************************************************************************/
|
---|
53 | /** Max GetCurrentPadding value we accept (to make sure it's safe to convert to bytes). */
|
---|
54 | #define VBOX_WASAPI_MAX_PADDING UINT32_C(0x007fffff)
|
---|
55 |
|
---|
56 | /** Maximum number of cached device configs in each direction.
|
---|
57 | * The number 4 was picked at random. */
|
---|
58 | #define VBOX_WASAPI_MAX_TOTAL_CONFIG_ENTRIES 4
|
---|
59 |
|
---|
60 | #if 0
|
---|
61 | /** @name WM_DRVHOSTAUDIOWAS_XXX - Worker thread messages.
|
---|
62 | * @{ */
|
---|
63 | #define WM_DRVHOSTAUDIOWAS_PURGE_CACHE (WM_APP + 3)
|
---|
64 | /** @} */
|
---|
65 | #endif
|
---|
66 |
|
---|
67 |
|
---|
68 | /** @name DRVHOSTAUDIOWAS_DO_XXX - Worker thread operations.
|
---|
69 | * @{ */
|
---|
70 | #define DRVHOSTAUDIOWAS_DO_PURGE_CACHE ((uintptr_t)0x49f37300 + 1)
|
---|
71 | #define DRVHOSTAUDIOWAS_DO_PRUNE_CACHE ((uintptr_t)0x49f37300 + 2)
|
---|
72 | #define DRVHOSTAUDIOWAS_DO_STREAM_DEV_SWITCH ((uintptr_t)0x49f37300 + 3)
|
---|
73 | /** @} */
|
---|
74 |
|
---|
75 |
|
---|
76 | /*********************************************************************************************************************************
|
---|
77 | * Structures and Typedefs *
|
---|
78 | *********************************************************************************************************************************/
|
---|
79 | class DrvHostAudioWasMmNotifyClient;
|
---|
80 |
|
---|
81 | /** Pointer to the cache entry for a host audio device (+dir). */
|
---|
82 | typedef struct DRVHOSTAUDIOWASCACHEDEV *PDRVHOSTAUDIOWASCACHEDEV;
|
---|
83 |
|
---|
84 | /**
|
---|
85 | * Cached pre-initialized audio client for a device.
|
---|
86 | *
|
---|
87 | * The activation and initialization of an IAudioClient has been observed to be
|
---|
88 | * very very slow (> 100 ms) and not suitable to be done on an EMT. So, we'll
|
---|
89 | * pre-initialize the device clients at construction time and when the default
|
---|
90 | * device changes to try avoid this problem.
|
---|
91 | *
|
---|
92 | * A client is returned to the cache after we're done with it, provided it still
|
---|
93 | * works fine.
|
---|
94 | */
|
---|
95 | typedef struct DRVHOSTAUDIOWASCACHEDEVCFG
|
---|
96 | {
|
---|
97 | /** Entry in DRVHOSTAUDIOWASCACHEDEV::ConfigList. */
|
---|
98 | RTLISTNODE ListEntry;
|
---|
99 | /** The device. */
|
---|
100 | PDRVHOSTAUDIOWASCACHEDEV pDevEntry;
|
---|
101 | /** The cached audio client. */
|
---|
102 | IAudioClient *pIAudioClient;
|
---|
103 | /** Output streams: The render client interface. */
|
---|
104 | IAudioRenderClient *pIAudioRenderClient;
|
---|
105 | /** Input streams: The capture client interface. */
|
---|
106 | IAudioCaptureClient *pIAudioCaptureClient;
|
---|
107 | /** The configuration. */
|
---|
108 | PDMAUDIOPCMPROPS Props;
|
---|
109 | /** The buffer size in frames. */
|
---|
110 | uint32_t cFramesBufferSize;
|
---|
111 | /** The device/whatever period in frames. */
|
---|
112 | uint32_t cFramesPeriod;
|
---|
113 | /** The setup status code.
|
---|
114 | * This is set to VERR_AUDIO_STREAM_INIT_IN_PROGRESS while the asynchronous
|
---|
115 | * initialization is still running. */
|
---|
116 | int volatile rcSetup;
|
---|
117 | /** Creation timestamp (just for reference). */
|
---|
118 | uint64_t nsCreated;
|
---|
119 | /** Init complete timestamp (just for reference). */
|
---|
120 | uint64_t nsInited;
|
---|
121 | /** When it was last used. */
|
---|
122 | uint64_t nsLastUsed;
|
---|
123 | /** The stringified properties. */
|
---|
124 | char szProps[32];
|
---|
125 | } DRVHOSTAUDIOWASCACHEDEVCFG;
|
---|
126 | /** Pointer to a pre-initialized audio client. */
|
---|
127 | typedef DRVHOSTAUDIOWASCACHEDEVCFG *PDRVHOSTAUDIOWASCACHEDEVCFG;
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * Per audio device (+ direction) cache entry.
|
---|
131 | */
|
---|
132 | typedef struct DRVHOSTAUDIOWASCACHEDEV
|
---|
133 | {
|
---|
134 | /** Entry in DRVHOSTAUDIOWAS::CacheHead. */
|
---|
135 | RTLISTNODE ListEntry;
|
---|
136 | /** The MM device associated with the stream. */
|
---|
137 | IMMDevice *pIDevice;
|
---|
138 | /** The direction of the device. */
|
---|
139 | PDMAUDIODIR enmDir;
|
---|
140 | #if 0 /* According to https://social.msdn.microsoft.com/Forums/en-US/1d974d90-6636-4121-bba3-a8861d9ab92a,
|
---|
141 | these were always support just missing from the SDK. */
|
---|
142 | /** Support for AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM: -1=unknown, 0=no, 1=yes. */
|
---|
143 | int8_t fSupportsAutoConvertPcm;
|
---|
144 | /** Support for AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY: -1=unknown, 0=no, 1=yes. */
|
---|
145 | int8_t fSupportsSrcDefaultQuality;
|
---|
146 | #endif
|
---|
147 | /** List of cached configurations (DRVHOSTAUDIOWASCACHEDEVCFG). */
|
---|
148 | RTLISTANCHOR ConfigList;
|
---|
149 | /** The device ID length in RTUTF16 units. */
|
---|
150 | size_t cwcDevId;
|
---|
151 | /** The device ID. */
|
---|
152 | RTUTF16 wszDevId[RT_FLEXIBLE_ARRAY];
|
---|
153 | } DRVHOSTAUDIOWASCACHEDEV;
|
---|
154 |
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Data for a WASABI stream.
|
---|
158 | */
|
---|
159 | typedef struct DRVHOSTAUDIOWASSTREAM
|
---|
160 | {
|
---|
161 | /** Common part. */
|
---|
162 | PDMAUDIOBACKENDSTREAM Core;
|
---|
163 |
|
---|
164 | /** Entry in DRVHOSTAUDIOWAS::StreamHead. */
|
---|
165 | RTLISTNODE ListEntry;
|
---|
166 | /** The stream's acquired configuration. */
|
---|
167 | PDMAUDIOSTREAMCFG Cfg;
|
---|
168 | /** Cache entry to be relased when destroying the stream. */
|
---|
169 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg;
|
---|
170 |
|
---|
171 | /** Set if the stream is enabled. */
|
---|
172 | bool fEnabled;
|
---|
173 | /** Set if the stream is started (playing/capturing). */
|
---|
174 | bool fStarted;
|
---|
175 | /** Set if the stream is draining (output only). */
|
---|
176 | bool fDraining;
|
---|
177 | /** Set if we should restart the stream on resume (saved pause state). */
|
---|
178 | bool fRestartOnResume;
|
---|
179 | /** Set if we're switching to a new output/input device. */
|
---|
180 | bool fSwitchingDevice;
|
---|
181 |
|
---|
182 | /** The RTTimeMilliTS() deadline for the draining of this stream (output). */
|
---|
183 | uint64_t msDrainDeadline;
|
---|
184 | /** Internal stream offset (bytes). */
|
---|
185 | uint64_t offInternal;
|
---|
186 | /** The RTTimeMilliTS() at the end of the last transfer. */
|
---|
187 | uint64_t msLastTransfer;
|
---|
188 |
|
---|
189 | /** Input: Current capture buffer (advanced as we read). */
|
---|
190 | uint8_t *pbCapture;
|
---|
191 | /** Input: The number of bytes left in the current capture buffer. */
|
---|
192 | uint32_t cbCapture;
|
---|
193 | /** Input: The full size of what pbCapture is part of (for ReleaseBuffer). */
|
---|
194 | uint32_t cFramesCaptureToRelease;
|
---|
195 |
|
---|
196 | /** Critical section protecting: . */
|
---|
197 | RTCRITSECT CritSect;
|
---|
198 | /** Buffer that drvHostWasStreamStatusString uses. */
|
---|
199 | char szStatus[128];
|
---|
200 | } DRVHOSTAUDIOWASSTREAM;
|
---|
201 | /** Pointer to a WASABI stream. */
|
---|
202 | typedef DRVHOSTAUDIOWASSTREAM *PDRVHOSTAUDIOWASSTREAM;
|
---|
203 |
|
---|
204 |
|
---|
205 | /**
|
---|
206 | * WASAPI-specific device entry.
|
---|
207 | */
|
---|
208 | typedef struct DRVHOSTAUDIOWASDEV
|
---|
209 | {
|
---|
210 | /** The core structure. */
|
---|
211 | PDMAUDIOHOSTDEV Core;
|
---|
212 | /** The device ID (flexible length). */
|
---|
213 | RTUTF16 wszDevId[RT_FLEXIBLE_ARRAY];
|
---|
214 | } DRVHOSTAUDIOWASDEV;
|
---|
215 | /** Pointer to a DirectSound device entry. */
|
---|
216 | typedef DRVHOSTAUDIOWASDEV *PDRVHOSTAUDIOWASDEV;
|
---|
217 |
|
---|
218 |
|
---|
219 | /**
|
---|
220 | * Data for a WASAPI host audio instance.
|
---|
221 | */
|
---|
222 | typedef struct DRVHOSTAUDIOWAS
|
---|
223 | {
|
---|
224 | /** The audio host audio interface we export. */
|
---|
225 | PDMIHOSTAUDIO IHostAudio;
|
---|
226 | /** Pointer to the PDM driver instance. */
|
---|
227 | PPDMDRVINS pDrvIns;
|
---|
228 | /** Audio device enumerator instance that we use for getting the default
|
---|
229 | * devices (or specific ones if overriden by config). Also used for
|
---|
230 | * implementing enumeration. */
|
---|
231 | IMMDeviceEnumerator *pIEnumerator;
|
---|
232 | /** The upwards interface. */
|
---|
233 | PPDMIHOSTAUDIOPORT pIHostAudioPort;
|
---|
234 | /** The output device ID, NULL for default.
|
---|
235 | * Protected by DrvHostAudioWasMmNotifyClient::m_CritSect. */
|
---|
236 | PRTUTF16 pwszOutputDevId;
|
---|
237 | /** The input device ID, NULL for default.
|
---|
238 | * Protected by DrvHostAudioWasMmNotifyClient::m_CritSect. */
|
---|
239 | PRTUTF16 pwszInputDevId;
|
---|
240 |
|
---|
241 | /** Pointer to the MM notification client instance. */
|
---|
242 | DrvHostAudioWasMmNotifyClient *pNotifyClient;
|
---|
243 | /** The input device to use. This can be NULL if there wasn't a suitable one
|
---|
244 | * around when we last looked or if it got removed/disabled/whatever.
|
---|
245 | * All access must be done inside the pNotifyClient critsect. */
|
---|
246 | IMMDevice *pIDeviceInput;
|
---|
247 | /** The output device to use. This can be NULL if there wasn't a suitable one
|
---|
248 | * around when we last looked or if it got removed/disabled/whatever.
|
---|
249 | * All access must be done inside the pNotifyClient critsect. */
|
---|
250 | IMMDevice *pIDeviceOutput;
|
---|
251 |
|
---|
252 | /** List of streams (DRVHOSTAUDIOWASSTREAM).
|
---|
253 | * Requires CritSect ownership. */
|
---|
254 | RTLISTANCHOR StreamHead;
|
---|
255 | /** Serializing access to StreamHead. */
|
---|
256 | RTCRITSECTRW CritSectStreamList;
|
---|
257 |
|
---|
258 | /** List of cached devices (DRVHOSTAUDIOWASCACHEDEV).
|
---|
259 | * Protected by CritSectCache */
|
---|
260 | RTLISTANCHOR CacheHead;
|
---|
261 | /** Serializing access to CacheHead. */
|
---|
262 | RTCRITSECT CritSectCache;
|
---|
263 | /** Semaphore for signalling that cache purge is done and that the destructor
|
---|
264 | * can do cleanups. */
|
---|
265 | RTSEMEVENTMULTI hEvtCachePurge;
|
---|
266 | /** Total number of device config entire for capturing.
|
---|
267 | * This includes in-use ones. */
|
---|
268 | uint32_t volatile cCacheEntriesIn;
|
---|
269 | /** Total number of device config entire for playback.
|
---|
270 | * This includes in-use ones. */
|
---|
271 | uint32_t volatile cCacheEntriesOut;
|
---|
272 |
|
---|
273 | #if 0
|
---|
274 | /** The worker thread. */
|
---|
275 | RTTHREAD hWorkerThread;
|
---|
276 | /** The TID of the worker thread (for posting messages to it). */
|
---|
277 | DWORD idWorkerThread;
|
---|
278 | /** The fixed wParam value for the worker thread. */
|
---|
279 | WPARAM uWorkerThreadFixedParam;
|
---|
280 | #endif
|
---|
281 | } DRVHOSTAUDIOWAS;
|
---|
282 | /** Pointer to the data for a WASAPI host audio driver instance. */
|
---|
283 | typedef DRVHOSTAUDIOWAS *PDRVHOSTAUDIOWAS;
|
---|
284 |
|
---|
285 |
|
---|
286 |
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Gets the stream status.
|
---|
290 | *
|
---|
291 | * @returns Pointer to stream status string.
|
---|
292 | * @param pStreamWas The stream to get the status for.
|
---|
293 | */
|
---|
294 | static const char *drvHostWasStreamStatusString(PDRVHOSTAUDIOWASSTREAM pStreamWas)
|
---|
295 | {
|
---|
296 | static RTSTRTUPLE const s_aEnable[2] =
|
---|
297 | {
|
---|
298 | { RT_STR_TUPLE("DISABLED") },
|
---|
299 | { RT_STR_TUPLE("ENABLED ") },
|
---|
300 | };
|
---|
301 | PCRTSTRTUPLE pTuple = &s_aEnable[pStreamWas->fEnabled];
|
---|
302 | memcpy(pStreamWas->szStatus, pTuple->psz, pTuple->cch);
|
---|
303 | size_t off = pTuple->cch;
|
---|
304 |
|
---|
305 | static RTSTRTUPLE const s_aStarted[2] =
|
---|
306 | {
|
---|
307 | { RT_STR_TUPLE(" STOPPED") },
|
---|
308 | { RT_STR_TUPLE(" STARTED") },
|
---|
309 | };
|
---|
310 | pTuple = &s_aStarted[pStreamWas->fStarted];
|
---|
311 | memcpy(&pStreamWas->szStatus[off], pTuple->psz, pTuple->cch);
|
---|
312 | off += pTuple->cch;
|
---|
313 |
|
---|
314 | static RTSTRTUPLE const s_aDraining[2] =
|
---|
315 | {
|
---|
316 | { RT_STR_TUPLE("") },
|
---|
317 | { RT_STR_TUPLE(" DRAINING") },
|
---|
318 | };
|
---|
319 | pTuple = &s_aDraining[pStreamWas->fDraining];
|
---|
320 | memcpy(&pStreamWas->szStatus[off], pTuple->psz, pTuple->cch);
|
---|
321 | off += pTuple->cch;
|
---|
322 |
|
---|
323 | Assert(off < sizeof(pStreamWas->szStatus));
|
---|
324 | pStreamWas->szStatus[off] = '\0';
|
---|
325 | return pStreamWas->szStatus;
|
---|
326 | }
|
---|
327 |
|
---|
328 |
|
---|
329 | /*********************************************************************************************************************************
|
---|
330 | * IMMNotificationClient implementation
|
---|
331 | *********************************************************************************************************************************/
|
---|
332 | /**
|
---|
333 | * Multimedia notification client.
|
---|
334 | *
|
---|
335 | * We want to know when the default device changes so we can switch running
|
---|
336 | * streams to use the new one and so we can pre-activate it in preparation
|
---|
337 | * for new streams.
|
---|
338 | */
|
---|
339 | class DrvHostAudioWasMmNotifyClient : public IMMNotificationClient
|
---|
340 | {
|
---|
341 | private:
|
---|
342 | /** Reference counter. */
|
---|
343 | uint32_t volatile m_cRefs;
|
---|
344 | /** The WASAPI host audio driver instance data.
|
---|
345 | * @note This can be NULL. Only access after entering critical section. */
|
---|
346 | PDRVHOSTAUDIOWAS m_pDrvWas;
|
---|
347 | /** Critical section serializing access to m_pDrvWas. */
|
---|
348 | RTCRITSECT m_CritSect;
|
---|
349 |
|
---|
350 | public:
|
---|
351 | /**
|
---|
352 | * @throws int on critical section init failure.
|
---|
353 | */
|
---|
354 | DrvHostAudioWasMmNotifyClient(PDRVHOSTAUDIOWAS a_pDrvWas)
|
---|
355 | : m_cRefs(1)
|
---|
356 | , m_pDrvWas(a_pDrvWas)
|
---|
357 | {
|
---|
358 | int rc = RTCritSectInit(&m_CritSect);
|
---|
359 | AssertRCStmt(rc, throw(rc));
|
---|
360 | }
|
---|
361 |
|
---|
362 | virtual ~DrvHostAudioWasMmNotifyClient() RT_NOEXCEPT
|
---|
363 | {
|
---|
364 | RTCritSectDelete(&m_CritSect);
|
---|
365 | }
|
---|
366 |
|
---|
367 | /**
|
---|
368 | * Called by drvHostAudioWasDestruct to set m_pDrvWas to NULL.
|
---|
369 | */
|
---|
370 | void notifyDriverDestroyed() RT_NOEXCEPT
|
---|
371 | {
|
---|
372 | RTCritSectEnter(&m_CritSect);
|
---|
373 | m_pDrvWas = NULL;
|
---|
374 | RTCritSectLeave(&m_CritSect);
|
---|
375 | }
|
---|
376 |
|
---|
377 | /**
|
---|
378 | * Enters the notification critsect for getting at the IMMDevice members in
|
---|
379 | * PDMHOSTAUDIOWAS.
|
---|
380 | */
|
---|
381 | void lockEnter() RT_NOEXCEPT
|
---|
382 | {
|
---|
383 | RTCritSectEnter(&m_CritSect);
|
---|
384 | }
|
---|
385 |
|
---|
386 | /**
|
---|
387 | * Leaves the notification critsect.
|
---|
388 | */
|
---|
389 | void lockLeave() RT_NOEXCEPT
|
---|
390 | {
|
---|
391 | RTCritSectLeave(&m_CritSect);
|
---|
392 | }
|
---|
393 |
|
---|
394 | /** @name IUnknown interface
|
---|
395 | * @{ */
|
---|
396 | IFACEMETHODIMP_(ULONG) AddRef()
|
---|
397 | {
|
---|
398 | uint32_t cRefs = ASMAtomicIncU32(&m_cRefs);
|
---|
399 | AssertMsg(cRefs < 64, ("%#x\n", cRefs));
|
---|
400 | Log6Func(("returns %u\n", cRefs));
|
---|
401 | return cRefs;
|
---|
402 | }
|
---|
403 |
|
---|
404 | IFACEMETHODIMP_(ULONG) Release()
|
---|
405 | {
|
---|
406 | uint32_t cRefs = ASMAtomicDecU32(&m_cRefs);
|
---|
407 | AssertMsg(cRefs < 64, ("%#x\n", cRefs));
|
---|
408 | if (cRefs == 0)
|
---|
409 | delete this;
|
---|
410 | Log6Func(("returns %u\n", cRefs));
|
---|
411 | return cRefs;
|
---|
412 | }
|
---|
413 |
|
---|
414 | IFACEMETHODIMP QueryInterface(const IID &rIID, void **ppvInterface)
|
---|
415 | {
|
---|
416 | if (IsEqualIID(rIID, IID_IUnknown))
|
---|
417 | *ppvInterface = static_cast<IUnknown *>(this);
|
---|
418 | else if (IsEqualIID(rIID, __uuidof(IMMNotificationClient)))
|
---|
419 | *ppvInterface = static_cast<IMMNotificationClient *>(this);
|
---|
420 | else
|
---|
421 | {
|
---|
422 | LogFunc(("Unknown rIID={%RTuuid}\n", &rIID));
|
---|
423 | *ppvInterface = NULL;
|
---|
424 | return E_NOINTERFACE;
|
---|
425 | }
|
---|
426 | Log6Func(("returns S_OK + %p\n", *ppvInterface));
|
---|
427 | return S_OK;
|
---|
428 | }
|
---|
429 | /** @} */
|
---|
430 |
|
---|
431 | /** @name IMMNotificationClient interface
|
---|
432 | * @{ */
|
---|
433 | IFACEMETHODIMP OnDeviceStateChanged(LPCWSTR pwszDeviceId, DWORD dwNewState)
|
---|
434 | {
|
---|
435 | RT_NOREF(pwszDeviceId, dwNewState);
|
---|
436 | Log7Func(("pwszDeviceId=%ls dwNewState=%u (%#x)\n", pwszDeviceId, dwNewState, dwNewState));
|
---|
437 |
|
---|
438 | /*
|
---|
439 | * Just trigger device re-enumeration.
|
---|
440 | */
|
---|
441 | notifyDeviceChanges();
|
---|
442 |
|
---|
443 | /** @todo do we need to check for our devices here too? Not when using a
|
---|
444 | * default device. But when using a specific device, we could perhaps
|
---|
445 | * re-init the stream when dwNewState indicates precense. We might
|
---|
446 | * also take action when a devices ceases to be operating, but again
|
---|
447 | * only for non-default devices, probably... */
|
---|
448 |
|
---|
449 | return S_OK;
|
---|
450 | }
|
---|
451 |
|
---|
452 | IFACEMETHODIMP OnDeviceAdded(LPCWSTR pwszDeviceId)
|
---|
453 | {
|
---|
454 | RT_NOREF(pwszDeviceId);
|
---|
455 | Log7Func(("pwszDeviceId=%ls\n", pwszDeviceId));
|
---|
456 |
|
---|
457 | /*
|
---|
458 | * Is this a device we're interested in? Grab the enumerator if it is.
|
---|
459 | */
|
---|
460 | bool fOutput = false;
|
---|
461 | IMMDeviceEnumerator *pIEnumerator = NULL;
|
---|
462 | RTCritSectEnter(&m_CritSect);
|
---|
463 | if ( m_pDrvWas != NULL
|
---|
464 | && ( (fOutput = RTUtf16ICmp(m_pDrvWas->pwszOutputDevId, pwszDeviceId) == 0)
|
---|
465 | || RTUtf16ICmp(m_pDrvWas->pwszInputDevId, pwszDeviceId) == 0))
|
---|
466 | {
|
---|
467 | pIEnumerator = m_pDrvWas->pIEnumerator;
|
---|
468 | if (pIEnumerator /* paranoia */)
|
---|
469 | pIEnumerator->AddRef();
|
---|
470 | }
|
---|
471 | RTCritSectLeave(&m_CritSect);
|
---|
472 | if (pIEnumerator)
|
---|
473 | {
|
---|
474 | /*
|
---|
475 | * Get the device and update it.
|
---|
476 | */
|
---|
477 | IMMDevice *pIDevice = NULL;
|
---|
478 | HRESULT hrc = pIEnumerator->GetDevice(pwszDeviceId, &pIDevice);
|
---|
479 | if (SUCCEEDED(hrc))
|
---|
480 | setDevice(fOutput, pIDevice, pwszDeviceId, __PRETTY_FUNCTION__);
|
---|
481 | else
|
---|
482 | LogRelMax(64, ("WasAPI: Failed to get %s device '%ls' (OnDeviceAdded): %Rhrc\n",
|
---|
483 | fOutput ? "output" : "input", pwszDeviceId, hrc));
|
---|
484 | pIEnumerator->Release();
|
---|
485 |
|
---|
486 | /*
|
---|
487 | * Trigger device re-enumeration.
|
---|
488 | */
|
---|
489 | notifyDeviceChanges();
|
---|
490 | }
|
---|
491 | return S_OK;
|
---|
492 | }
|
---|
493 |
|
---|
494 | IFACEMETHODIMP OnDeviceRemoved(LPCWSTR pwszDeviceId)
|
---|
495 | {
|
---|
496 | RT_NOREF(pwszDeviceId);
|
---|
497 | Log7Func(("pwszDeviceId=%ls\n", pwszDeviceId));
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * Is this a device we're interested in? Then set it to NULL.
|
---|
501 | */
|
---|
502 | bool fOutput = false;
|
---|
503 | RTCritSectEnter(&m_CritSect);
|
---|
504 | if ( m_pDrvWas != NULL
|
---|
505 | && ( (fOutput = RTUtf16ICmp(m_pDrvWas->pwszOutputDevId, pwszDeviceId) == 0)
|
---|
506 | || RTUtf16ICmp(m_pDrvWas->pwszInputDevId, pwszDeviceId) == 0))
|
---|
507 | {
|
---|
508 | RTCritSectLeave(&m_CritSect);
|
---|
509 | setDevice(fOutput, NULL, pwszDeviceId, __PRETTY_FUNCTION__);
|
---|
510 | }
|
---|
511 | else
|
---|
512 | RTCritSectLeave(&m_CritSect);
|
---|
513 |
|
---|
514 | /*
|
---|
515 | * Trigger device re-enumeration.
|
---|
516 | */
|
---|
517 | notifyDeviceChanges();
|
---|
518 | return S_OK;
|
---|
519 | }
|
---|
520 |
|
---|
521 | IFACEMETHODIMP OnDefaultDeviceChanged(EDataFlow enmFlow, ERole enmRole, LPCWSTR pwszDefaultDeviceId)
|
---|
522 | {
|
---|
523 | /*
|
---|
524 | * Are we interested in this device? If so grab the enumerator.
|
---|
525 | */
|
---|
526 | IMMDeviceEnumerator *pIEnumerator = NULL;
|
---|
527 | RTCritSectEnter(&m_CritSect);
|
---|
528 | if ( m_pDrvWas != NULL
|
---|
529 | && ( (enmFlow == eRender && enmRole == eMultimedia && !m_pDrvWas->pwszOutputDevId)
|
---|
530 | || (enmFlow == eCapture && enmRole == eMultimedia && !m_pDrvWas->pwszInputDevId)))
|
---|
531 | {
|
---|
532 | pIEnumerator = m_pDrvWas->pIEnumerator;
|
---|
533 | if (pIEnumerator /* paranoia */)
|
---|
534 | pIEnumerator->AddRef();
|
---|
535 | }
|
---|
536 | RTCritSectLeave(&m_CritSect);
|
---|
537 | if (pIEnumerator)
|
---|
538 | {
|
---|
539 | /*
|
---|
540 | * Get the device and update it.
|
---|
541 | */
|
---|
542 | IMMDevice *pIDevice = NULL;
|
---|
543 | HRESULT hrc = pIEnumerator->GetDefaultAudioEndpoint(enmFlow, enmRole, &pIDevice);
|
---|
544 | if (SUCCEEDED(hrc))
|
---|
545 | setDevice(enmFlow == eRender, pIDevice, pwszDefaultDeviceId, __PRETTY_FUNCTION__);
|
---|
546 | else
|
---|
547 | LogRelMax(64, ("WasAPI: Failed to get default %s device (OnDefaultDeviceChange): %Rhrc\n",
|
---|
548 | enmFlow == eRender ? "output" : "input", hrc));
|
---|
549 | pIEnumerator->Release();
|
---|
550 |
|
---|
551 | /*
|
---|
552 | * Trigger device re-enumeration.
|
---|
553 | */
|
---|
554 | notifyDeviceChanges();
|
---|
555 | }
|
---|
556 |
|
---|
557 | Log7Func(("enmFlow=%d enmRole=%d pwszDefaultDeviceId=%ls\n", enmFlow, enmRole, pwszDefaultDeviceId));
|
---|
558 | return S_OK;
|
---|
559 | }
|
---|
560 |
|
---|
561 | IFACEMETHODIMP OnPropertyValueChanged(LPCWSTR pwszDeviceId, const PROPERTYKEY Key)
|
---|
562 | {
|
---|
563 | RT_NOREF(pwszDeviceId, Key);
|
---|
564 | Log7Func(("pwszDeviceId=%ls Key={%RTuuid, %u (%#x)}\n", pwszDeviceId, &Key.fmtid, Key.pid, Key.pid));
|
---|
565 | return S_OK;
|
---|
566 | }
|
---|
567 | /** @} */
|
---|
568 |
|
---|
569 | private:
|
---|
570 | /**
|
---|
571 | * Sets DRVHOSTAUDIOWAS::pIDeviceOutput or DRVHOSTAUDIOWAS::pIDeviceInput to @a pIDevice.
|
---|
572 | */
|
---|
573 | void setDevice(bool fOutput, IMMDevice *pIDevice, LPCWSTR pwszDeviceId, const char *pszCaller)
|
---|
574 | {
|
---|
575 | RT_NOREF(pszCaller, pwszDeviceId);
|
---|
576 |
|
---|
577 | RTCritSectEnter(&m_CritSect);
|
---|
578 |
|
---|
579 | /*
|
---|
580 | * Update our internal device reference.
|
---|
581 | */
|
---|
582 | if (m_pDrvWas)
|
---|
583 | {
|
---|
584 | if (fOutput)
|
---|
585 | {
|
---|
586 | Log7((LOG_FN_FMT ": Changing output device from %p to %p (%ls)\n",
|
---|
587 | pszCaller, m_pDrvWas->pIDeviceOutput, pIDevice, pwszDeviceId));
|
---|
588 | if (m_pDrvWas->pIDeviceOutput)
|
---|
589 | m_pDrvWas->pIDeviceOutput->Release();
|
---|
590 | m_pDrvWas->pIDeviceOutput = pIDevice;
|
---|
591 | }
|
---|
592 | else
|
---|
593 | {
|
---|
594 | Log7((LOG_FN_FMT ": Changing input device from %p to %p (%ls)\n",
|
---|
595 | pszCaller, m_pDrvWas->pIDeviceInput, pIDevice, pwszDeviceId));
|
---|
596 | if (m_pDrvWas->pIDeviceInput)
|
---|
597 | m_pDrvWas->pIDeviceInput->Release();
|
---|
598 | m_pDrvWas->pIDeviceInput = pIDevice;
|
---|
599 | }
|
---|
600 | }
|
---|
601 | else if (pIDevice)
|
---|
602 | pIDevice->Release();
|
---|
603 |
|
---|
604 | /*
|
---|
605 | * Tell DrvAudio that the device has changed for one of the directions.
|
---|
606 | *
|
---|
607 | * We have to exit the critsect when doing so, or we'll create a locking
|
---|
608 | * order violation. So, try make sure the VM won't be destroyed while
|
---|
609 | * till DrvAudio have entered its critical section...
|
---|
610 | */
|
---|
611 | if (m_pDrvWas)
|
---|
612 | {
|
---|
613 | PPDMIHOSTAUDIOPORT const pIHostAudioPort = m_pDrvWas->pIHostAudioPort;
|
---|
614 | if (pIHostAudioPort)
|
---|
615 | {
|
---|
616 | VMSTATE const enmVmState = PDMDrvHlpVMState(m_pDrvWas->pDrvIns);
|
---|
617 | if (enmVmState < VMSTATE_POWERING_OFF)
|
---|
618 | {
|
---|
619 | RTCritSectLeave(&m_CritSect);
|
---|
620 | pIHostAudioPort->pfnNotifyDeviceChanged(pIHostAudioPort, fOutput ? PDMAUDIODIR_OUT : PDMAUDIODIR_IN, NULL);
|
---|
621 | return;
|
---|
622 | }
|
---|
623 | LogFlowFunc(("Ignoring change: enmVmState=%d\n", enmVmState));
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | RTCritSectLeave(&m_CritSect);
|
---|
628 | }
|
---|
629 |
|
---|
630 | /**
|
---|
631 | * Tell DrvAudio to re-enumerate devices when it get a chance.
|
---|
632 | *
|
---|
633 | * We exit the critsect here too before calling DrvAudio just to be on the safe
|
---|
634 | * side (see setDevice()), even though the current DrvAudio code doesn't take
|
---|
635 | * any critsects.
|
---|
636 | */
|
---|
637 | void notifyDeviceChanges(void)
|
---|
638 | {
|
---|
639 | RTCritSectEnter(&m_CritSect);
|
---|
640 | if (m_pDrvWas)
|
---|
641 | {
|
---|
642 | PPDMIHOSTAUDIOPORT const pIHostAudioPort = m_pDrvWas->pIHostAudioPort;
|
---|
643 | if (pIHostAudioPort)
|
---|
644 | {
|
---|
645 | VMSTATE const enmVmState = PDMDrvHlpVMState(m_pDrvWas->pDrvIns);
|
---|
646 | if (enmVmState < VMSTATE_POWERING_OFF)
|
---|
647 | {
|
---|
648 | RTCritSectLeave(&m_CritSect);
|
---|
649 | pIHostAudioPort->pfnNotifyDevicesChanged(pIHostAudioPort);
|
---|
650 | return;
|
---|
651 | }
|
---|
652 | LogFlowFunc(("Ignoring change: enmVmState=%d\n", enmVmState));
|
---|
653 | }
|
---|
654 | }
|
---|
655 | RTCritSectLeave(&m_CritSect);
|
---|
656 | }
|
---|
657 | };
|
---|
658 |
|
---|
659 |
|
---|
660 | /*********************************************************************************************************************************
|
---|
661 | * Pre-configured audio client cache. *
|
---|
662 | *********************************************************************************************************************************/
|
---|
663 | #define WAS_CACHE_MAX_ENTRIES_SAME_DEVICE 2
|
---|
664 |
|
---|
665 | /**
|
---|
666 | * Converts from PDM stream config to windows WAVEFORMATEXTENSIBLE struct.
|
---|
667 | *
|
---|
668 | * @param pProps The PDM audio PCM properties to convert from.
|
---|
669 | * @param pFmt The windows structure to initialize.
|
---|
670 | */
|
---|
671 | static void drvHostAudioWasWaveFmtExtFromProps(PCPDMAUDIOPCMPROPS pProps, PWAVEFORMATEXTENSIBLE pFmt)
|
---|
672 | {
|
---|
673 | RT_ZERO(*pFmt);
|
---|
674 | pFmt->Format.wFormatTag = WAVE_FORMAT_PCM;
|
---|
675 | pFmt->Format.nChannels = PDMAudioPropsChannels(pProps);
|
---|
676 | pFmt->Format.wBitsPerSample = PDMAudioPropsSampleBits(pProps);
|
---|
677 | pFmt->Format.nSamplesPerSec = PDMAudioPropsHz(pProps);
|
---|
678 | pFmt->Format.nBlockAlign = PDMAudioPropsFrameSize(pProps);
|
---|
679 | pFmt->Format.nAvgBytesPerSec = PDMAudioPropsFramesToBytes(pProps, PDMAudioPropsHz(pProps));
|
---|
680 | pFmt->Format.cbSize = 0; /* No extra data specified. */
|
---|
681 |
|
---|
682 | /*
|
---|
683 | * We need to use the extensible structure if there are more than two channels
|
---|
684 | * or if the channels have non-standard assignments.
|
---|
685 | */
|
---|
686 | if ( pFmt->Format.nChannels > 2
|
---|
687 | || ( pFmt->Format.nChannels == 1
|
---|
688 | ? pProps->aidChannels[0] != PDMAUDIOCHANNELID_MONO
|
---|
689 | : pProps->aidChannels[0] != PDMAUDIOCHANNELID_FRONT_LEFT
|
---|
690 | || pProps->aidChannels[1] != PDMAUDIOCHANNELID_FRONT_RIGHT))
|
---|
691 | {
|
---|
692 | pFmt->Format.wFormatTag = WAVE_FORMAT_EXTENSIBLE;
|
---|
693 | pFmt->Format.cbSize = sizeof(*pFmt) - sizeof(pFmt->Format);
|
---|
694 | pFmt->Samples.wValidBitsPerSample = PDMAudioPropsSampleBits(pProps);
|
---|
695 | pFmt->SubFormat = KSDATAFORMAT_SUBTYPE_PCM;
|
---|
696 | pFmt->dwChannelMask = 0;
|
---|
697 | unsigned const cSrcChannels = pFmt->Format.nChannels;
|
---|
698 | for (unsigned i = 0; i < cSrcChannels; i++)
|
---|
699 | if ( pProps->aidChannels[i] >= PDMAUDIOCHANNELID_FIRST_STANDARD
|
---|
700 | && pProps->aidChannels[i] < PDMAUDIOCHANNELID_END_STANDARD)
|
---|
701 | pFmt->dwChannelMask |= RT_BIT_32(pProps->aidChannels[i] - PDMAUDIOCHANNELID_FIRST_STANDARD);
|
---|
702 | else
|
---|
703 | pFmt->Format.nChannels -= 1;
|
---|
704 | }
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | #if 0 /* unused */
|
---|
709 | /**
|
---|
710 | * Converts from windows WAVEFORMATEX and stream props to PDM audio properties.
|
---|
711 | *
|
---|
712 | * @returns VINF_SUCCESS on success, VERR_AUDIO_STREAM_COULD_NOT_CREATE if not
|
---|
713 | * supported.
|
---|
714 | * @param pProps The output properties structure.
|
---|
715 | * @param pFmt The windows wave format structure.
|
---|
716 | * @param pszStream The stream name for error logging.
|
---|
717 | * @param pwszDevId The device ID for error logging.
|
---|
718 | */
|
---|
719 | static int drvHostAudioWasCacheWaveFmtExToProps(PPDMAUDIOPCMPROPS pProps, WAVEFORMATEX const *pFmt,
|
---|
720 | const char *pszStream, PCRTUTF16 pwszDevId)
|
---|
721 | {
|
---|
722 | if (pFmt->wFormatTag == WAVE_FORMAT_PCM)
|
---|
723 | {
|
---|
724 | if ( pFmt->wBitsPerSample == 8
|
---|
725 | || pFmt->wBitsPerSample == 16
|
---|
726 | || pFmt->wBitsPerSample == 32)
|
---|
727 | {
|
---|
728 | if (pFmt->nChannels > 0 && pFmt->nChannels < 16)
|
---|
729 | {
|
---|
730 | if (pFmt->nSamplesPerSec >= 4096 && pFmt->nSamplesPerSec <= 768000)
|
---|
731 | {
|
---|
732 | PDMAudioPropsInit(pProps, pFmt->wBitsPerSample / 8, true /*fSigned*/, pFmt->nChannels, pFmt->nSamplesPerSec);
|
---|
733 | if (PDMAudioPropsFrameSize(pProps) == pFmt->nBlockAlign)
|
---|
734 | return VINF_SUCCESS;
|
---|
735 | }
|
---|
736 | }
|
---|
737 | }
|
---|
738 | }
|
---|
739 | LogRelMax(64, ("WasAPI: Error! Unsupported stream format for '%s' suggested by '%ls':\n"
|
---|
740 | "WasAPI: wFormatTag = %RU16 (expected %d)\n"
|
---|
741 | "WasAPI: nChannels = %RU16 (expected 1..15)\n"
|
---|
742 | "WasAPI: nSamplesPerSec = %RU32 (expected 4096..768000)\n"
|
---|
743 | "WasAPI: nAvgBytesPerSec = %RU32\n"
|
---|
744 | "WasAPI: nBlockAlign = %RU16\n"
|
---|
745 | "WasAPI: wBitsPerSample = %RU16 (expected 8, 16, or 32)\n"
|
---|
746 | "WasAPI: cbSize = %RU16\n",
|
---|
747 | pszStream, pwszDevId, pFmt->wFormatTag, WAVE_FORMAT_PCM, pFmt->nChannels, pFmt->nSamplesPerSec, pFmt->nAvgBytesPerSec,
|
---|
748 | pFmt->nBlockAlign, pFmt->wBitsPerSample, pFmt->cbSize));
|
---|
749 | return VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
750 | }
|
---|
751 | #endif
|
---|
752 |
|
---|
753 |
|
---|
754 | /**
|
---|
755 | * Destroys a devie config cache entry.
|
---|
756 | *
|
---|
757 | * @param pThis The WASAPI host audio driver instance data.
|
---|
758 | * @param pDevCfg Device config entry. Must not be in the list.
|
---|
759 | */
|
---|
760 | static void drvHostAudioWasCacheDestroyDevConfig(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg)
|
---|
761 | {
|
---|
762 | if (pDevCfg->pDevEntry->enmDir == PDMAUDIODIR_IN)
|
---|
763 | ASMAtomicDecU32(&pThis->cCacheEntriesIn);
|
---|
764 | else
|
---|
765 | ASMAtomicDecU32(&pThis->cCacheEntriesOut);
|
---|
766 |
|
---|
767 | uint32_t cTypeClientRefs = 0;
|
---|
768 | if (pDevCfg->pIAudioCaptureClient)
|
---|
769 | {
|
---|
770 | cTypeClientRefs = pDevCfg->pIAudioCaptureClient->Release();
|
---|
771 | pDevCfg->pIAudioCaptureClient = NULL;
|
---|
772 | }
|
---|
773 |
|
---|
774 | if (pDevCfg->pIAudioRenderClient)
|
---|
775 | {
|
---|
776 | cTypeClientRefs = pDevCfg->pIAudioRenderClient->Release();
|
---|
777 | pDevCfg->pIAudioRenderClient = NULL;
|
---|
778 | }
|
---|
779 |
|
---|
780 | uint32_t cClientRefs = 0;
|
---|
781 | if (pDevCfg->pIAudioClient /* paranoia */)
|
---|
782 | {
|
---|
783 | cClientRefs = pDevCfg->pIAudioClient->Release();
|
---|
784 | pDevCfg->pIAudioClient = NULL;
|
---|
785 | }
|
---|
786 |
|
---|
787 | Log8Func(("Destroying cache config entry: '%ls: %s' - cClientRefs=%u cTypeClientRefs=%u\n",
|
---|
788 | pDevCfg->pDevEntry->wszDevId, pDevCfg->szProps, cClientRefs, cTypeClientRefs));
|
---|
789 | RT_NOREF(cClientRefs, cTypeClientRefs);
|
---|
790 |
|
---|
791 | pDevCfg->pDevEntry = NULL;
|
---|
792 | RTMemFree(pDevCfg);
|
---|
793 | }
|
---|
794 |
|
---|
795 |
|
---|
796 | /**
|
---|
797 | * Destroys a device cache entry.
|
---|
798 | *
|
---|
799 | * @param pThis The WASAPI host audio driver instance data.
|
---|
800 | * @param pDevEntry The device entry. Must not be in the cache!
|
---|
801 | */
|
---|
802 | static void drvHostAudioWasCacheDestroyDevEntry(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASCACHEDEV pDevEntry)
|
---|
803 | {
|
---|
804 | Log8Func(("Destroying cache entry: %p - '%ls'\n", pDevEntry, pDevEntry->wszDevId));
|
---|
805 |
|
---|
806 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg, pDevCfgNext;
|
---|
807 | RTListForEachSafe(&pDevEntry->ConfigList, pDevCfg, pDevCfgNext, DRVHOSTAUDIOWASCACHEDEVCFG, ListEntry)
|
---|
808 | {
|
---|
809 | drvHostAudioWasCacheDestroyDevConfig(pThis, pDevCfg);
|
---|
810 | }
|
---|
811 |
|
---|
812 | uint32_t cDevRefs = 0;
|
---|
813 | if (pDevEntry->pIDevice /* paranoia */)
|
---|
814 | {
|
---|
815 | cDevRefs = pDevEntry->pIDevice->Release();
|
---|
816 | pDevEntry->pIDevice = NULL;
|
---|
817 | }
|
---|
818 |
|
---|
819 | pDevEntry->cwcDevId = 0;
|
---|
820 | pDevEntry->wszDevId[0] = '\0';
|
---|
821 | RTMemFree(pDevEntry);
|
---|
822 | Log8Func(("Destroyed cache entry: %p cDevRefs=%u\n", pDevEntry, cDevRefs));
|
---|
823 | }
|
---|
824 |
|
---|
825 |
|
---|
826 | /**
|
---|
827 | * Prunes the cache.
|
---|
828 | */
|
---|
829 | static void drvHostAudioWasCachePrune(PDRVHOSTAUDIOWAS pThis)
|
---|
830 | {
|
---|
831 | /*
|
---|
832 | * Prune each direction separately.
|
---|
833 | */
|
---|
834 | struct
|
---|
835 | {
|
---|
836 | PDMAUDIODIR enmDir;
|
---|
837 | uint32_t volatile *pcEntries;
|
---|
838 | } aWork[] = { { PDMAUDIODIR_IN, &pThis->cCacheEntriesIn }, { PDMAUDIODIR_OUT, &pThis->cCacheEntriesOut }, };
|
---|
839 | for (uint32_t iWork = 0; iWork < RT_ELEMENTS(aWork); iWork++)
|
---|
840 | {
|
---|
841 | /*
|
---|
842 | * Remove the least recently used entry till we're below the threshold
|
---|
843 | * or there are no more inactive entries.
|
---|
844 | */
|
---|
845 | LogFlowFunc(("iWork=%u cEntries=%u\n", iWork, *aWork[iWork].pcEntries));
|
---|
846 | while (*aWork[iWork].pcEntries > VBOX_WASAPI_MAX_TOTAL_CONFIG_ENTRIES)
|
---|
847 | {
|
---|
848 | RTCritSectEnter(&pThis->CritSectCache);
|
---|
849 | PDRVHOSTAUDIOWASCACHEDEVCFG pLeastRecentlyUsed = NULL;
|
---|
850 | PDRVHOSTAUDIOWASCACHEDEV pDevEntry;
|
---|
851 | RTListForEach(&pThis->CacheHead, pDevEntry, DRVHOSTAUDIOWASCACHEDEV, ListEntry)
|
---|
852 | {
|
---|
853 | if (pDevEntry->enmDir == aWork[iWork].enmDir)
|
---|
854 | {
|
---|
855 | PDRVHOSTAUDIOWASCACHEDEVCFG pHeadCfg = RTListGetFirst(&pDevEntry->ConfigList,
|
---|
856 | DRVHOSTAUDIOWASCACHEDEVCFG, ListEntry);
|
---|
857 | if ( pHeadCfg
|
---|
858 | && (!pLeastRecentlyUsed || pHeadCfg->nsLastUsed < pLeastRecentlyUsed->nsLastUsed))
|
---|
859 | pLeastRecentlyUsed = pHeadCfg;
|
---|
860 | }
|
---|
861 | }
|
---|
862 | if (pLeastRecentlyUsed)
|
---|
863 | RTListNodeRemove(&pLeastRecentlyUsed->ListEntry);
|
---|
864 | RTCritSectLeave(&pThis->CritSectCache);
|
---|
865 |
|
---|
866 | if (!pLeastRecentlyUsed)
|
---|
867 | break;
|
---|
868 | drvHostAudioWasCacheDestroyDevConfig(pThis, pLeastRecentlyUsed);
|
---|
869 | }
|
---|
870 | }
|
---|
871 | }
|
---|
872 |
|
---|
873 |
|
---|
874 | /**
|
---|
875 | * Purges all the entries in the cache.
|
---|
876 | */
|
---|
877 | static void drvHostAudioWasCachePurge(PDRVHOSTAUDIOWAS pThis, bool fOnWorker)
|
---|
878 | {
|
---|
879 | for (;;)
|
---|
880 | {
|
---|
881 | RTCritSectEnter(&pThis->CritSectCache);
|
---|
882 | PDRVHOSTAUDIOWASCACHEDEV pDevEntry = RTListRemoveFirst(&pThis->CacheHead, DRVHOSTAUDIOWASCACHEDEV, ListEntry);
|
---|
883 | RTCritSectLeave(&pThis->CritSectCache);
|
---|
884 | if (!pDevEntry)
|
---|
885 | break;
|
---|
886 | drvHostAudioWasCacheDestroyDevEntry(pThis, pDevEntry);
|
---|
887 | }
|
---|
888 |
|
---|
889 | if (fOnWorker)
|
---|
890 | {
|
---|
891 | int rc = RTSemEventMultiSignal(pThis->hEvtCachePurge);
|
---|
892 | AssertRC(rc);
|
---|
893 | }
|
---|
894 | }
|
---|
895 |
|
---|
896 |
|
---|
897 | /**
|
---|
898 | * Looks up a specific configuration.
|
---|
899 | *
|
---|
900 | * @returns Pointer to the device config (removed from cache) on success. NULL
|
---|
901 | * if no matching config found.
|
---|
902 | * @param pDevEntry Where to perform the lookup.
|
---|
903 | * @param pProps The config properties to match.
|
---|
904 | */
|
---|
905 | static PDRVHOSTAUDIOWASCACHEDEVCFG
|
---|
906 | drvHostAudioWasCacheLookupLocked(PDRVHOSTAUDIOWASCACHEDEV pDevEntry, PCPDMAUDIOPCMPROPS pProps)
|
---|
907 | {
|
---|
908 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg;
|
---|
909 | RTListForEach(&pDevEntry->ConfigList, pDevCfg, DRVHOSTAUDIOWASCACHEDEVCFG, ListEntry)
|
---|
910 | {
|
---|
911 | if (PDMAudioPropsAreEqual(&pDevCfg->Props, pProps))
|
---|
912 | {
|
---|
913 | RTListNodeRemove(&pDevCfg->ListEntry);
|
---|
914 | pDevCfg->nsLastUsed = RTTimeNanoTS();
|
---|
915 | return pDevCfg;
|
---|
916 | }
|
---|
917 | }
|
---|
918 | return NULL;
|
---|
919 | }
|
---|
920 |
|
---|
921 |
|
---|
922 | /**
|
---|
923 | * Initializes a device config entry.
|
---|
924 | *
|
---|
925 | * This is usually done on the worker thread.
|
---|
926 | *
|
---|
927 | * @returns VBox status code.
|
---|
928 | * @param pDevCfg The device configuration entry to initialize.
|
---|
929 | */
|
---|
930 | static int drvHostAudioWasCacheInitConfig(PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg)
|
---|
931 | {
|
---|
932 | /*
|
---|
933 | * Assert some sanity given that we migth be called on the worker thread
|
---|
934 | * and pDevCfg being a message parameter.
|
---|
935 | */
|
---|
936 | AssertPtrReturn(pDevCfg, VERR_INTERNAL_ERROR_2);
|
---|
937 | AssertReturn(pDevCfg->rcSetup == VERR_AUDIO_STREAM_INIT_IN_PROGRESS, VERR_INTERNAL_ERROR_2);
|
---|
938 | AssertReturn(pDevCfg->pIAudioClient == NULL, VERR_INTERNAL_ERROR_2);
|
---|
939 | AssertReturn(pDevCfg->pIAudioCaptureClient == NULL, VERR_INTERNAL_ERROR_2);
|
---|
940 | AssertReturn(pDevCfg->pIAudioRenderClient == NULL, VERR_INTERNAL_ERROR_2);
|
---|
941 | AssertReturn(PDMAudioPropsAreValid(&pDevCfg->Props), VERR_INTERNAL_ERROR_2);
|
---|
942 |
|
---|
943 | PDRVHOSTAUDIOWASCACHEDEV pDevEntry = pDevCfg->pDevEntry;
|
---|
944 | AssertPtrReturn(pDevEntry, VERR_INTERNAL_ERROR_2);
|
---|
945 | AssertPtrReturn(pDevEntry->pIDevice, VERR_INTERNAL_ERROR_2);
|
---|
946 | AssertReturn(pDevEntry->enmDir == PDMAUDIODIR_IN || pDevEntry->enmDir == PDMAUDIODIR_OUT, VERR_INTERNAL_ERROR_2);
|
---|
947 |
|
---|
948 | /*
|
---|
949 | * First we need an IAudioClient interface for calling IsFormatSupported
|
---|
950 | * on so we can get guidance as to what to do next.
|
---|
951 | *
|
---|
952 | * Initially, I thought the AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM was not
|
---|
953 | * supported all the way back to Vista and that we'd had to try different
|
---|
954 | * things here to get the most optimal format. However, according to
|
---|
955 | * https://social.msdn.microsoft.com/Forums/en-US/1d974d90-6636-4121-bba3-a8861d9ab92a
|
---|
956 | * it is supported, just maybe missing from the SDK or something...
|
---|
957 | *
|
---|
958 | * I'll leave the IsFormatSupported call here as it gives us a clue as to
|
---|
959 | * what exactly the WAS needs to convert our audio stream into/from.
|
---|
960 | */
|
---|
961 | Log8Func(("Activating an IAudioClient for '%ls' ...\n", pDevEntry->wszDevId));
|
---|
962 | IAudioClient *pIAudioClient = NULL;
|
---|
963 | HRESULT hrc = pDevEntry->pIDevice->Activate(__uuidof(IAudioClient), CLSCTX_ALL,
|
---|
964 | NULL /*pActivationParams*/, (void **)&pIAudioClient);
|
---|
965 | Log8Func(("Activate('%ls', IAudioClient) -> %Rhrc\n", pDevEntry->wszDevId, hrc));
|
---|
966 | if (FAILED(hrc))
|
---|
967 | {
|
---|
968 | LogRelMax(64, ("WasAPI: Activate(%ls, IAudioClient) failed: %Rhrc\n", pDevEntry->wszDevId, hrc));
|
---|
969 | pDevCfg->nsInited = RTTimeNanoTS();
|
---|
970 | pDevCfg->nsLastUsed = pDevCfg->nsInited;
|
---|
971 | return pDevCfg->rcSetup = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
972 | }
|
---|
973 |
|
---|
974 | WAVEFORMATEXTENSIBLE WaveFmtExt;
|
---|
975 | drvHostAudioWasWaveFmtExtFromProps(&pDevCfg->Props, &WaveFmtExt);
|
---|
976 |
|
---|
977 | PWAVEFORMATEX pClosestMatch = NULL;
|
---|
978 | hrc = pIAudioClient->IsFormatSupported(AUDCLNT_SHAREMODE_SHARED, &WaveFmtExt.Format, &pClosestMatch);
|
---|
979 |
|
---|
980 | /*
|
---|
981 | * If the format is supported, go ahead and initialize the client instance.
|
---|
982 | *
|
---|
983 | * The docs talks about AUDCLNT_E_UNSUPPORTED_FORMAT being success too, but
|
---|
984 | * that doesn't seem to be the case (at least not for mixing up the
|
---|
985 | * WAVEFORMATEX::wFormatTag values). Seems that is the standard return code
|
---|
986 | * if there is anything it doesn't grok.
|
---|
987 | */
|
---|
988 | if (SUCCEEDED(hrc))
|
---|
989 | {
|
---|
990 | if (hrc == S_OK)
|
---|
991 | Log8Func(("IsFormatSupported(,%s,) -> S_OK + %p: requested format is supported\n", pDevCfg->szProps, pClosestMatch));
|
---|
992 | else
|
---|
993 | Log8Func(("IsFormatSupported(,%s,) -> %Rhrc + %p: %uch S%u %uHz\n", pDevCfg->szProps, hrc, pClosestMatch,
|
---|
994 | pClosestMatch ? pClosestMatch->nChannels : 0, pClosestMatch ? pClosestMatch->wBitsPerSample : 0,
|
---|
995 | pClosestMatch ? pClosestMatch->nSamplesPerSec : 0));
|
---|
996 |
|
---|
997 | REFERENCE_TIME const cBufferSizeInNtTicks = PDMAudioPropsFramesToNtTicks(&pDevCfg->Props, pDevCfg->cFramesBufferSize);
|
---|
998 | uint32_t fInitFlags = AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM
|
---|
999 | | AUDCLNT_STREAMFLAGS_SRC_DEFAULT_QUALITY;
|
---|
1000 | hrc = pIAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED, fInitFlags, cBufferSizeInNtTicks,
|
---|
1001 | 0 /*cPeriodicityInNtTicks*/, &WaveFmtExt.Format, NULL /*pAudioSessionGuid*/);
|
---|
1002 | Log8Func(("Initialize(,%x, %RI64, %s,) -> %Rhrc\n", fInitFlags, cBufferSizeInNtTicks, pDevCfg->szProps, hrc));
|
---|
1003 | if (SUCCEEDED(hrc))
|
---|
1004 | {
|
---|
1005 | /*
|
---|
1006 | * The direction specific client interface.
|
---|
1007 | */
|
---|
1008 | if (pDevEntry->enmDir == PDMAUDIODIR_IN)
|
---|
1009 | hrc = pIAudioClient->GetService(__uuidof(IAudioCaptureClient), (void **)&pDevCfg->pIAudioCaptureClient);
|
---|
1010 | else
|
---|
1011 | hrc = pIAudioClient->GetService(__uuidof(IAudioRenderClient), (void **)&pDevCfg->pIAudioRenderClient);
|
---|
1012 | Log8Func(("GetService -> %Rhrc + %p\n", hrc, pDevEntry->enmDir == PDMAUDIODIR_IN
|
---|
1013 | ? (void *)pDevCfg->pIAudioCaptureClient : (void *)pDevCfg->pIAudioRenderClient));
|
---|
1014 | if (SUCCEEDED(hrc))
|
---|
1015 | {
|
---|
1016 | /*
|
---|
1017 | * Obtain the actual stream format and buffer config.
|
---|
1018 | */
|
---|
1019 | UINT32 cFramesBufferSize = 0;
|
---|
1020 | REFERENCE_TIME cDefaultPeriodInNtTicks = 0;
|
---|
1021 | REFERENCE_TIME cMinimumPeriodInNtTicks = 0;
|
---|
1022 | REFERENCE_TIME cLatencyinNtTicks = 0;
|
---|
1023 | hrc = pIAudioClient->GetBufferSize(&cFramesBufferSize);
|
---|
1024 | if (SUCCEEDED(hrc))
|
---|
1025 | {
|
---|
1026 | hrc = pIAudioClient->GetDevicePeriod(&cDefaultPeriodInNtTicks, &cMinimumPeriodInNtTicks);
|
---|
1027 | if (SUCCEEDED(hrc))
|
---|
1028 | {
|
---|
1029 | hrc = pIAudioClient->GetStreamLatency(&cLatencyinNtTicks);
|
---|
1030 | if (SUCCEEDED(hrc))
|
---|
1031 | {
|
---|
1032 | LogRel2(("WasAPI: Aquired buffer parameters for %s:\n"
|
---|
1033 | "WasAPI: cFramesBufferSize = %RU32\n"
|
---|
1034 | "WasAPI: cDefaultPeriodInNtTicks = %RI64\n"
|
---|
1035 | "WasAPI: cMinimumPeriodInNtTicks = %RI64\n"
|
---|
1036 | "WasAPI: cLatencyinNtTicks = %RI64\n",
|
---|
1037 | pDevCfg->szProps, cFramesBufferSize, cDefaultPeriodInNtTicks,
|
---|
1038 | cMinimumPeriodInNtTicks, cLatencyinNtTicks));
|
---|
1039 |
|
---|
1040 | pDevCfg->pIAudioClient = pIAudioClient;
|
---|
1041 | pDevCfg->cFramesBufferSize = cFramesBufferSize;
|
---|
1042 | pDevCfg->cFramesPeriod = PDMAudioPropsNanoToFrames(&pDevCfg->Props,
|
---|
1043 | cDefaultPeriodInNtTicks * 100);
|
---|
1044 | pDevCfg->nsInited = RTTimeNanoTS();
|
---|
1045 | pDevCfg->nsLastUsed = pDevCfg->nsInited;
|
---|
1046 | pDevCfg->rcSetup = VINF_SUCCESS;
|
---|
1047 |
|
---|
1048 | if (pClosestMatch)
|
---|
1049 | CoTaskMemFree(pClosestMatch);
|
---|
1050 | Log8Func(("returns VINF_SUCCESS (%p (%s) inited in %'RU64 ns)\n",
|
---|
1051 | pDevCfg, pDevCfg->szProps, pDevCfg->nsInited - pDevCfg->nsCreated));
|
---|
1052 | return VINF_SUCCESS;
|
---|
1053 | }
|
---|
1054 | LogRelMax(64, ("WasAPI: GetStreamLatency failed: %Rhrc\n", hrc));
|
---|
1055 | }
|
---|
1056 | else
|
---|
1057 | LogRelMax(64, ("WasAPI: GetDevicePeriod failed: %Rhrc\n", hrc));
|
---|
1058 | }
|
---|
1059 | else
|
---|
1060 | LogRelMax(64, ("WasAPI: GetBufferSize failed: %Rhrc\n", hrc));
|
---|
1061 |
|
---|
1062 | if (pDevCfg->pIAudioCaptureClient)
|
---|
1063 | {
|
---|
1064 | pDevCfg->pIAudioCaptureClient->Release();
|
---|
1065 | pDevCfg->pIAudioCaptureClient = NULL;
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | if (pDevCfg->pIAudioRenderClient)
|
---|
1069 | {
|
---|
1070 | pDevCfg->pIAudioRenderClient->Release();
|
---|
1071 | pDevCfg->pIAudioRenderClient = NULL;
|
---|
1072 | }
|
---|
1073 | }
|
---|
1074 | else
|
---|
1075 | LogRelMax(64, ("WasAPI: IAudioClient::GetService(%s) failed: %Rhrc\n", pDevCfg->szProps, hrc));
|
---|
1076 | }
|
---|
1077 | else
|
---|
1078 | LogRelMax(64, ("WasAPI: IAudioClient::Initialize(%s) failed: %Rhrc\n", pDevCfg->szProps, hrc));
|
---|
1079 | }
|
---|
1080 | else
|
---|
1081 | LogRelMax(64,("WasAPI: IAudioClient::IsFormatSupported(,%s,) failed: %Rhrc\n", pDevCfg->szProps, hrc));
|
---|
1082 |
|
---|
1083 | pIAudioClient->Release();
|
---|
1084 | if (pClosestMatch)
|
---|
1085 | CoTaskMemFree(pClosestMatch);
|
---|
1086 | pDevCfg->nsInited = RTTimeNanoTS();
|
---|
1087 | pDevCfg->nsLastUsed = 0;
|
---|
1088 | Log8Func(("returns VERR_AUDIO_STREAM_COULD_NOT_CREATE (inited in %'RU64 ns)\n", pDevCfg->nsInited - pDevCfg->nsCreated));
|
---|
1089 | return pDevCfg->rcSetup = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
1090 | }
|
---|
1091 |
|
---|
1092 |
|
---|
1093 | /**
|
---|
1094 | * Worker for drvHostAudioWasCacheLookupOrCreate.
|
---|
1095 | *
|
---|
1096 | * If lookup fails, a new entry will be created.
|
---|
1097 | *
|
---|
1098 | * @note Called holding the lock, returning without holding it!
|
---|
1099 | */
|
---|
1100 | static int drvHostAudioWasCacheLookupOrCreateConfig(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASCACHEDEV pDevEntry,
|
---|
1101 | PCPDMAUDIOSTREAMCFG pCfgReq, bool fOnWorker,
|
---|
1102 | PDRVHOSTAUDIOWASCACHEDEVCFG *ppDevCfg)
|
---|
1103 | {
|
---|
1104 | /*
|
---|
1105 | * Check if we've got a matching config.
|
---|
1106 | */
|
---|
1107 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg = drvHostAudioWasCacheLookupLocked(pDevEntry, &pCfgReq->Props);
|
---|
1108 | if (pDevCfg)
|
---|
1109 | {
|
---|
1110 | *ppDevCfg = pDevCfg;
|
---|
1111 | RTCritSectLeave(&pThis->CritSectCache);
|
---|
1112 | Log8Func(("Config cache hit '%s' on '%ls': %p\n", pDevCfg->szProps, pDevEntry->wszDevId, pDevCfg));
|
---|
1113 | return VINF_SUCCESS;
|
---|
1114 | }
|
---|
1115 |
|
---|
1116 | RTCritSectLeave(&pThis->CritSectCache);
|
---|
1117 |
|
---|
1118 | /*
|
---|
1119 | * Allocate an device config entry and hand the creation task over to the
|
---|
1120 | * worker thread, unless we're already on it.
|
---|
1121 | */
|
---|
1122 | pDevCfg = (PDRVHOSTAUDIOWASCACHEDEVCFG)RTMemAllocZ(sizeof(*pDevCfg));
|
---|
1123 | AssertReturn(pDevCfg, VERR_NO_MEMORY);
|
---|
1124 | RTListInit(&pDevCfg->ListEntry);
|
---|
1125 | pDevCfg->pDevEntry = pDevEntry;
|
---|
1126 | pDevCfg->rcSetup = VERR_AUDIO_STREAM_INIT_IN_PROGRESS;
|
---|
1127 | pDevCfg->Props = pCfgReq->Props;
|
---|
1128 | pDevCfg->cFramesBufferSize = pCfgReq->Backend.cFramesBufferSize;
|
---|
1129 | PDMAudioPropsToString(&pDevCfg->Props, pDevCfg->szProps, sizeof(pDevCfg->szProps));
|
---|
1130 | pDevCfg->nsCreated = RTTimeNanoTS();
|
---|
1131 | pDevCfg->nsLastUsed = pDevCfg->nsCreated;
|
---|
1132 |
|
---|
1133 | uint32_t cCacheEntries;
|
---|
1134 | if (pDevCfg->pDevEntry->enmDir == PDMAUDIODIR_IN)
|
---|
1135 | cCacheEntries = ASMAtomicIncU32(&pThis->cCacheEntriesIn);
|
---|
1136 | else
|
---|
1137 | cCacheEntries = ASMAtomicIncU32(&pThis->cCacheEntriesOut);
|
---|
1138 | if (cCacheEntries > VBOX_WASAPI_MAX_TOTAL_CONFIG_ENTRIES)
|
---|
1139 | {
|
---|
1140 | LogFlowFunc(("Trigger cache pruning.\n"));
|
---|
1141 | int rc2 = pThis->pIHostAudioPort->pfnDoOnWorkerThread(pThis->pIHostAudioPort, NULL /*pStream*/,
|
---|
1142 | DRVHOSTAUDIOWAS_DO_PRUNE_CACHE, NULL /*pvUser*/);
|
---|
1143 | AssertRCStmt(rc2, drvHostAudioWasCachePrune(pThis));
|
---|
1144 | }
|
---|
1145 |
|
---|
1146 | if (!fOnWorker)
|
---|
1147 | {
|
---|
1148 | *ppDevCfg = pDevCfg;
|
---|
1149 | LogFlowFunc(("Doing the rest of the work on %p via pfnStreamInitAsync...\n", pDevCfg));
|
---|
1150 | return VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED;
|
---|
1151 | }
|
---|
1152 |
|
---|
1153 | /*
|
---|
1154 | * Initialize the entry on the calling thread.
|
---|
1155 | */
|
---|
1156 | int rc = drvHostAudioWasCacheInitConfig(pDevCfg);
|
---|
1157 | AssertRC(pDevCfg->rcSetup == rc);
|
---|
1158 | if (RT_SUCCESS(rc))
|
---|
1159 | rc = pDevCfg->rcSetup; /* paranoia */
|
---|
1160 | if (RT_SUCCESS(rc))
|
---|
1161 | {
|
---|
1162 | *ppDevCfg = pDevCfg;
|
---|
1163 | LogFlowFunc(("Returning %p\n", pDevCfg));
|
---|
1164 | return VINF_SUCCESS;
|
---|
1165 | }
|
---|
1166 | RTMemFree(pDevCfg);
|
---|
1167 | *ppDevCfg = NULL;
|
---|
1168 | return rc;
|
---|
1169 | }
|
---|
1170 |
|
---|
1171 |
|
---|
1172 | /**
|
---|
1173 | * Looks up the given device + config combo in the cache, creating a new entry
|
---|
1174 | * if missing.
|
---|
1175 | *
|
---|
1176 | * @returns VBox status code.
|
---|
1177 | * @retval VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED if @a fOnWorker is @c false and
|
---|
1178 | * we created a new entry that needs initalization by calling
|
---|
1179 | * drvHostAudioWasCacheInitConfig() on it.
|
---|
1180 | * @param pThis The WASAPI host audio driver instance data.
|
---|
1181 | * @param pIDevice The device to look up.
|
---|
1182 | * @param pCfgReq The configuration to look up.
|
---|
1183 | * @param fOnWorker Set if we're on a worker thread, otherwise false. When
|
---|
1184 | * set to @c true, VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED will
|
---|
1185 | * not be returned and a new entry will be fully
|
---|
1186 | * initialized before returning.
|
---|
1187 | * @param ppDevCfg Where to return the requested device config.
|
---|
1188 | */
|
---|
1189 | static int drvHostAudioWasCacheLookupOrCreate(PDRVHOSTAUDIOWAS pThis, IMMDevice *pIDevice, PCPDMAUDIOSTREAMCFG pCfgReq,
|
---|
1190 | bool fOnWorker, PDRVHOSTAUDIOWASCACHEDEVCFG *ppDevCfg)
|
---|
1191 | {
|
---|
1192 | *ppDevCfg = NULL;
|
---|
1193 |
|
---|
1194 | /*
|
---|
1195 | * Get the device ID so we can perform the lookup.
|
---|
1196 | */
|
---|
1197 | int rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
1198 | LPWSTR pwszDevId = NULL;
|
---|
1199 | HRESULT hrc = pIDevice->GetId(&pwszDevId);
|
---|
1200 | if (SUCCEEDED(hrc))
|
---|
1201 | {
|
---|
1202 | size_t cwcDevId = RTUtf16Len(pwszDevId);
|
---|
1203 |
|
---|
1204 | /*
|
---|
1205 | * The cache has two levels, so first the device entry.
|
---|
1206 | */
|
---|
1207 | PDRVHOSTAUDIOWASCACHEDEV pDevEntry;
|
---|
1208 | RTCritSectEnter(&pThis->CritSectCache);
|
---|
1209 | RTListForEach(&pThis->CacheHead, pDevEntry, DRVHOSTAUDIOWASCACHEDEV, ListEntry)
|
---|
1210 | {
|
---|
1211 | if ( pDevEntry->cwcDevId == cwcDevId
|
---|
1212 | && pDevEntry->enmDir == pCfgReq->enmDir
|
---|
1213 | && RTUtf16Cmp(pDevEntry->wszDevId, pwszDevId) == 0)
|
---|
1214 | {
|
---|
1215 | CoTaskMemFree(pwszDevId);
|
---|
1216 | Log8Func(("Cache hit for device '%ls': %p\n", pDevEntry->wszDevId, pDevEntry));
|
---|
1217 | return drvHostAudioWasCacheLookupOrCreateConfig(pThis, pDevEntry, pCfgReq, fOnWorker, ppDevCfg);
|
---|
1218 | }
|
---|
1219 | }
|
---|
1220 | RTCritSectLeave(&pThis->CritSectCache);
|
---|
1221 |
|
---|
1222 | /*
|
---|
1223 | * Device not in the cache, add it.
|
---|
1224 | */
|
---|
1225 | pDevEntry = (PDRVHOSTAUDIOWASCACHEDEV)RTMemAllocZVar(RT_UOFFSETOF_DYN(DRVHOSTAUDIOWASCACHEDEV, wszDevId[cwcDevId + 1]));
|
---|
1226 | if (pDevEntry)
|
---|
1227 | {
|
---|
1228 | pIDevice->AddRef();
|
---|
1229 | pDevEntry->pIDevice = pIDevice;
|
---|
1230 | pDevEntry->enmDir = pCfgReq->enmDir;
|
---|
1231 | pDevEntry->cwcDevId = cwcDevId;
|
---|
1232 | #if 0
|
---|
1233 | pDevEntry->fSupportsAutoConvertPcm = -1;
|
---|
1234 | pDevEntry->fSupportsSrcDefaultQuality = -1;
|
---|
1235 | #endif
|
---|
1236 | RTListInit(&pDevEntry->ConfigList);
|
---|
1237 | memcpy(pDevEntry->wszDevId, pwszDevId, cwcDevId * sizeof(RTUTF16));
|
---|
1238 | pDevEntry->wszDevId[cwcDevId] = '\0';
|
---|
1239 |
|
---|
1240 | CoTaskMemFree(pwszDevId);
|
---|
1241 | pwszDevId = NULL;
|
---|
1242 |
|
---|
1243 | /*
|
---|
1244 | * Before adding the device, check that someone didn't race us adding it.
|
---|
1245 | */
|
---|
1246 | RTCritSectEnter(&pThis->CritSectCache);
|
---|
1247 | PDRVHOSTAUDIOWASCACHEDEV pDevEntry2;
|
---|
1248 | RTListForEach(&pThis->CacheHead, pDevEntry2, DRVHOSTAUDIOWASCACHEDEV, ListEntry)
|
---|
1249 | {
|
---|
1250 | if ( pDevEntry2->cwcDevId == cwcDevId
|
---|
1251 | && pDevEntry2->enmDir == pCfgReq->enmDir
|
---|
1252 | && RTUtf16Cmp(pDevEntry2->wszDevId, pDevEntry->wszDevId) == 0)
|
---|
1253 | {
|
---|
1254 | pIDevice->Release();
|
---|
1255 | RTMemFree(pDevEntry);
|
---|
1256 | pDevEntry = NULL;
|
---|
1257 |
|
---|
1258 | Log8Func(("Lost race adding device '%ls': %p\n", pDevEntry2->wszDevId, pDevEntry2));
|
---|
1259 | return drvHostAudioWasCacheLookupOrCreateConfig(pThis, pDevEntry2, pCfgReq, fOnWorker, ppDevCfg);
|
---|
1260 | }
|
---|
1261 | }
|
---|
1262 | RTListPrepend(&pThis->CacheHead, &pDevEntry->ListEntry);
|
---|
1263 |
|
---|
1264 | Log8Func(("Added device '%ls' to cache: %p\n", pDevEntry->wszDevId, pDevEntry));
|
---|
1265 | return drvHostAudioWasCacheLookupOrCreateConfig(pThis, pDevEntry, pCfgReq, fOnWorker, ppDevCfg);
|
---|
1266 | }
|
---|
1267 | CoTaskMemFree(pwszDevId);
|
---|
1268 | }
|
---|
1269 | else
|
---|
1270 | LogRelMax(64, ("WasAPI: GetId failed (lookup): %Rhrc\n", hrc));
|
---|
1271 | return rc;
|
---|
1272 | }
|
---|
1273 |
|
---|
1274 |
|
---|
1275 | /**
|
---|
1276 | * Return the given config to the cache.
|
---|
1277 | *
|
---|
1278 | * @param pThis The WASAPI host audio driver instance data.
|
---|
1279 | * @param pDevCfg The device config to put back.
|
---|
1280 | */
|
---|
1281 | static void drvHostAudioWasCachePutBack(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg)
|
---|
1282 | {
|
---|
1283 | /*
|
---|
1284 | * Reset the audio client to see that it works and to make sure it's in a sensible state.
|
---|
1285 | */
|
---|
1286 | HRESULT hrc = pDevCfg->pIAudioClient ? pDevCfg->pIAudioClient->Reset()
|
---|
1287 | : pDevCfg->rcSetup == VERR_AUDIO_STREAM_INIT_IN_PROGRESS ? S_OK : E_FAIL;
|
---|
1288 | if (SUCCEEDED(hrc))
|
---|
1289 | {
|
---|
1290 | Log8Func(("Putting %p/'%s' back\n", pDevCfg, pDevCfg->szProps));
|
---|
1291 | RTCritSectEnter(&pThis->CritSectCache);
|
---|
1292 | RTListAppend(&pDevCfg->pDevEntry->ConfigList, &pDevCfg->ListEntry);
|
---|
1293 | uint32_t const cEntries = pDevCfg->pDevEntry->enmDir == PDMAUDIODIR_IN ? pThis->cCacheEntriesIn : pThis->cCacheEntriesOut;
|
---|
1294 | RTCritSectLeave(&pThis->CritSectCache);
|
---|
1295 |
|
---|
1296 | /* Trigger pruning if we're over the threshold. */
|
---|
1297 | if (cEntries > VBOX_WASAPI_MAX_TOTAL_CONFIG_ENTRIES)
|
---|
1298 | {
|
---|
1299 | LogFlowFunc(("Trigger cache pruning.\n"));
|
---|
1300 | int rc2 = pThis->pIHostAudioPort->pfnDoOnWorkerThread(pThis->pIHostAudioPort, NULL /*pStream*/,
|
---|
1301 | DRVHOSTAUDIOWAS_DO_PRUNE_CACHE, NULL /*pvUser*/);
|
---|
1302 | AssertRCStmt(rc2, drvHostAudioWasCachePrune(pThis));
|
---|
1303 | }
|
---|
1304 | }
|
---|
1305 | else
|
---|
1306 | {
|
---|
1307 | Log8Func(("IAudioClient::Reset failed (%Rhrc) on %p/'%s', destroying it.\n", hrc, pDevCfg, pDevCfg->szProps));
|
---|
1308 | drvHostAudioWasCacheDestroyDevConfig(pThis, pDevCfg);
|
---|
1309 | }
|
---|
1310 | }
|
---|
1311 |
|
---|
1312 |
|
---|
1313 | static void drvHostWasCacheConfigHinting(PDRVHOSTAUDIOWAS pThis, PPDMAUDIOSTREAMCFG pCfgReq, bool fOnWorker)
|
---|
1314 | {
|
---|
1315 | /*
|
---|
1316 | * Get the device.
|
---|
1317 | */
|
---|
1318 | pThis->pNotifyClient->lockEnter();
|
---|
1319 | IMMDevice *pIDevice = pCfgReq->enmDir == PDMAUDIODIR_IN ? pThis->pIDeviceInput : pThis->pIDeviceOutput;
|
---|
1320 | if (pIDevice)
|
---|
1321 | pIDevice->AddRef();
|
---|
1322 | pThis->pNotifyClient->lockLeave();
|
---|
1323 | if (pIDevice)
|
---|
1324 | {
|
---|
1325 | /*
|
---|
1326 | * Look up the config and put it back.
|
---|
1327 | */
|
---|
1328 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg = NULL;
|
---|
1329 | int rc = drvHostAudioWasCacheLookupOrCreate(pThis, pIDevice, pCfgReq, fOnWorker, &pDevCfg);
|
---|
1330 | LogFlowFunc(("pDevCfg=%p rc=%Rrc\n", pDevCfg, rc));
|
---|
1331 | if (pDevCfg && RT_SUCCESS(rc))
|
---|
1332 | drvHostAudioWasCachePutBack(pThis, pDevCfg);
|
---|
1333 | pIDevice->Release();
|
---|
1334 | }
|
---|
1335 | }
|
---|
1336 |
|
---|
1337 |
|
---|
1338 | /**
|
---|
1339 | * Prefills the cache.
|
---|
1340 | *
|
---|
1341 | * @param pThis The WASAPI host audio driver instance data.
|
---|
1342 | */
|
---|
1343 | static void drvHostAudioWasCacheFill(PDRVHOSTAUDIOWAS pThis)
|
---|
1344 | {
|
---|
1345 | #if 0 /* we don't have the buffer config nor do we really know which frequences to expect */
|
---|
1346 | Log8Func(("enter\n"));
|
---|
1347 | struct
|
---|
1348 | {
|
---|
1349 | PCRTUTF16 pwszDevId;
|
---|
1350 | PDMAUDIODIR enmDir;
|
---|
1351 | } aToCache[] =
|
---|
1352 | {
|
---|
1353 | { pThis->pwszInputDevId, PDMAUDIODIR_IN },
|
---|
1354 | { pThis->pwszOutputDevId, PDMAUDIODIR_OUT }
|
---|
1355 | };
|
---|
1356 | for (unsigned i = 0; i < RT_ELEMENTS(aToCache); i++)
|
---|
1357 | {
|
---|
1358 | PCRTUTF16 pwszDevId = aToCache[i].pwszDevId;
|
---|
1359 | IMMDevice *pIDevice = NULL;
|
---|
1360 | HRESULT hrc;
|
---|
1361 | if (pwszDevId)
|
---|
1362 | hrc = pThis->pIEnumerator->GetDevice(pwszDevId, &pIDevice);
|
---|
1363 | else
|
---|
1364 | {
|
---|
1365 | hrc = pThis->pIEnumerator->GetDefaultAudioEndpoint(aToCache[i].enmDir == PDMAUDIODIR_IN ? eCapture : eRender,
|
---|
1366 | eMultimedia, &pIDevice);
|
---|
1367 | pwszDevId = aToCache[i].enmDir == PDMAUDIODIR_IN ? L"{Default-In}" : L"{Default-Out}";
|
---|
1368 | }
|
---|
1369 | if (SUCCEEDED(hrc))
|
---|
1370 | {
|
---|
1371 | PDMAUDIOSTREAMCFG Cfg = { aToCache[i].enmDir, { PDMAUDIOPLAYBACKDST_INVALID },
|
---|
1372 | PDMAUDIOPCMPROPS_INITIALIZER(2, true, 2, 44100, false) };
|
---|
1373 | Cfg.Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(&Cfg.Props, 300);
|
---|
1374 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg = drvHostAudioWasCacheLookupOrCreate(pThis, pIDevice, &Cfg);
|
---|
1375 | if (pDevCfg)
|
---|
1376 | drvHostAudioWasCachePutBack(pThis, pDevCfg);
|
---|
1377 |
|
---|
1378 | pIDevice->Release();
|
---|
1379 | }
|
---|
1380 | else
|
---|
1381 | LogRelMax(64, ("WasAPI: Failed to open audio device '%ls' (pre-caching): %Rhrc\n", pwszDevId, hrc));
|
---|
1382 | }
|
---|
1383 | Log8Func(("leave\n"));
|
---|
1384 | #else
|
---|
1385 | RT_NOREF(pThis);
|
---|
1386 | #endif
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 |
|
---|
1390 | /*********************************************************************************************************************************
|
---|
1391 | * Worker thread *
|
---|
1392 | *********************************************************************************************************************************/
|
---|
1393 | #if 0
|
---|
1394 |
|
---|
1395 | /**
|
---|
1396 | * @callback_method_impl{FNRTTHREAD,
|
---|
1397 | * Asynchronous thread for setting up audio client configs.}
|
---|
1398 | */
|
---|
1399 | static DECLCALLBACK(int) drvHostWasWorkerThread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
1400 | {
|
---|
1401 | PDRVHOSTAUDIOWAS pThis = (PDRVHOSTAUDIOWAS)pvUser;
|
---|
1402 |
|
---|
1403 | /*
|
---|
1404 | * We need to set the thread ID so others can post us thread messages.
|
---|
1405 | * And before we signal that we're ready, make sure we've got a message queue.
|
---|
1406 | */
|
---|
1407 | pThis->idWorkerThread = GetCurrentThreadId();
|
---|
1408 | LogFunc(("idWorkerThread=%#x (%u)\n", pThis->idWorkerThread, pThis->idWorkerThread));
|
---|
1409 |
|
---|
1410 | MSG Msg;
|
---|
1411 | PeekMessageW(&Msg, NULL, WM_USER, WM_USER, PM_NOREMOVE);
|
---|
1412 |
|
---|
1413 | int rc = RTThreadUserSignal(hThreadSelf);
|
---|
1414 | AssertRC(rc);
|
---|
1415 |
|
---|
1416 | /*
|
---|
1417 | * Message loop.
|
---|
1418 | */
|
---|
1419 | BOOL fRet;
|
---|
1420 | while ((fRet = GetMessageW(&Msg, NULL, 0, 0)) != FALSE)
|
---|
1421 | {
|
---|
1422 | if (fRet != -1)
|
---|
1423 | {
|
---|
1424 | TranslateMessage(&Msg);
|
---|
1425 | Log9Func(("Msg: time=%u: msg=%#x l=%p w=%p for hwnd=%p\n", Msg.time, Msg.message, Msg.lParam, Msg.wParam, Msg.hwnd));
|
---|
1426 | switch (Msg.message)
|
---|
1427 | {
|
---|
1428 | case WM_DRVHOSTAUDIOWAS_PURGE_CACHE:
|
---|
1429 | {
|
---|
1430 | AssertMsgBreak(Msg.wParam == pThis->uWorkerThreadFixedParam, ("%p\n", Msg.wParam));
|
---|
1431 | AssertBreak(Msg.hwnd == NULL);
|
---|
1432 | AssertBreak(Msg.lParam == 0);
|
---|
1433 |
|
---|
1434 | drvHostAudioWasCachePurge(pThis, false /*fOnWorker*/);
|
---|
1435 | break;
|
---|
1436 | }
|
---|
1437 |
|
---|
1438 | default:
|
---|
1439 | break;
|
---|
1440 | }
|
---|
1441 | DispatchMessageW(&Msg);
|
---|
1442 | }
|
---|
1443 | else
|
---|
1444 | AssertMsgFailed(("GetLastError()=%u\n", GetLastError()));
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | LogFlowFunc(("Pre-quit cache purge...\n"));
|
---|
1448 | drvHostAudioWasCachePurge(pThis, false /*fOnWorker*/);
|
---|
1449 |
|
---|
1450 | LogFunc(("Quits\n"));
|
---|
1451 | return VINF_SUCCESS;
|
---|
1452 | }
|
---|
1453 | #endif
|
---|
1454 |
|
---|
1455 |
|
---|
1456 | /*********************************************************************************************************************************
|
---|
1457 | * PDMIHOSTAUDIO *
|
---|
1458 | *********************************************************************************************************************************/
|
---|
1459 |
|
---|
1460 | /**
|
---|
1461 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
|
---|
1462 | */
|
---|
1463 | static DECLCALLBACK(int) drvHostAudioWasHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
|
---|
1464 | {
|
---|
1465 | RT_NOREF(pInterface);
|
---|
1466 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
1467 | AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
|
---|
1468 |
|
---|
1469 |
|
---|
1470 | /*
|
---|
1471 | * Fill in the config structure.
|
---|
1472 | */
|
---|
1473 | RTStrCopy(pBackendCfg->szName, sizeof(pBackendCfg->szName), "WasAPI");
|
---|
1474 | pBackendCfg->cbStream = sizeof(DRVHOSTAUDIOWASSTREAM);
|
---|
1475 | pBackendCfg->fFlags = PDMAUDIOBACKEND_F_ASYNC_HINT;
|
---|
1476 | pBackendCfg->cMaxStreamsIn = UINT32_MAX;
|
---|
1477 | pBackendCfg->cMaxStreamsOut = UINT32_MAX;
|
---|
1478 |
|
---|
1479 | return VINF_SUCCESS;
|
---|
1480 | }
|
---|
1481 |
|
---|
1482 |
|
---|
1483 | /**
|
---|
1484 | * Queries information for @a pDevice and adds an entry to the enumeration.
|
---|
1485 | *
|
---|
1486 | * @returns VBox status code.
|
---|
1487 | * @param pDevEnm The enumeration to add the device to.
|
---|
1488 | * @param pIDevice The device.
|
---|
1489 | * @param enmType The type of device.
|
---|
1490 | * @param fDefault Whether it's the default device.
|
---|
1491 | */
|
---|
1492 | static int drvHostWasEnumAddDev(PPDMAUDIOHOSTENUM pDevEnm, IMMDevice *pIDevice, EDataFlow enmType, bool fDefault)
|
---|
1493 | {
|
---|
1494 | int rc = VINF_SUCCESS; /* ignore most errors */
|
---|
1495 | RT_NOREF(fDefault); /** @todo default device marking/skipping. */
|
---|
1496 |
|
---|
1497 | /*
|
---|
1498 | * Gather the necessary properties.
|
---|
1499 | */
|
---|
1500 | IPropertyStore *pProperties = NULL;
|
---|
1501 | HRESULT hrc = pIDevice->OpenPropertyStore(STGM_READ, &pProperties);
|
---|
1502 | if (SUCCEEDED(hrc))
|
---|
1503 | {
|
---|
1504 | /* Get the friendly name (string). */
|
---|
1505 | PROPVARIANT VarName;
|
---|
1506 | PropVariantInit(&VarName);
|
---|
1507 | hrc = pProperties->GetValue(PKEY_Device_FriendlyName, &VarName);
|
---|
1508 | if (SUCCEEDED(hrc))
|
---|
1509 | {
|
---|
1510 | /* Get the device ID (string). */
|
---|
1511 | LPWSTR pwszDevId = NULL;
|
---|
1512 | hrc = pIDevice->GetId(&pwszDevId);
|
---|
1513 | if (SUCCEEDED(hrc))
|
---|
1514 | {
|
---|
1515 | size_t const cwcDevId = RTUtf16Len(pwszDevId);
|
---|
1516 |
|
---|
1517 | /* Get the device format (blob). */
|
---|
1518 | PROPVARIANT VarFormat;
|
---|
1519 | PropVariantInit(&VarFormat);
|
---|
1520 | hrc = pProperties->GetValue(PKEY_AudioEngine_DeviceFormat, &VarFormat);
|
---|
1521 | if (SUCCEEDED(hrc))
|
---|
1522 | {
|
---|
1523 | WAVEFORMATEX const * const pFormat = (WAVEFORMATEX const *)VarFormat.blob.pBlobData;
|
---|
1524 | AssertPtr(pFormat); /* Observed sometimes being NULL on windows 7 sp1. */
|
---|
1525 |
|
---|
1526 | /*
|
---|
1527 | * Create a enumeration entry for it.
|
---|
1528 | */
|
---|
1529 | size_t const cbId = RTUtf16CalcUtf8Len(pwszDevId) + 1;
|
---|
1530 | size_t const cbName = RTUtf16CalcUtf8Len(VarName.pwszVal) + 1;
|
---|
1531 | size_t const cbDev = RT_ALIGN_Z( RT_OFFSETOF(DRVHOSTAUDIOWASDEV, wszDevId)
|
---|
1532 | + (cwcDevId + 1) * sizeof(RTUTF16),
|
---|
1533 | 64);
|
---|
1534 | PDRVHOSTAUDIOWASDEV pDev = (PDRVHOSTAUDIOWASDEV)PDMAudioHostDevAlloc(cbDev, cbName, cbId);
|
---|
1535 | if (pDev)
|
---|
1536 | {
|
---|
1537 | pDev->Core.enmType = PDMAUDIODEVICETYPE_BUILTIN;
|
---|
1538 | pDev->Core.enmUsage = enmType == eRender ? PDMAUDIODIR_OUT : PDMAUDIODIR_IN;
|
---|
1539 | if (fDefault)
|
---|
1540 | pDev->Core.fFlags = enmType == eRender ? PDMAUDIOHOSTDEV_F_DEFAULT_OUT : PDMAUDIOHOSTDEV_F_DEFAULT_IN;
|
---|
1541 | if (enmType == eRender)
|
---|
1542 | pDev->Core.cMaxOutputChannels = RT_VALID_PTR(pFormat) ? pFormat->nChannels : 2;
|
---|
1543 | else
|
---|
1544 | pDev->Core.cMaxInputChannels = RT_VALID_PTR(pFormat) ? pFormat->nChannels : 1;
|
---|
1545 |
|
---|
1546 | memcpy(pDev->wszDevId, pwszDevId, cwcDevId * sizeof(RTUTF16));
|
---|
1547 | pDev->wszDevId[cwcDevId] = '\0';
|
---|
1548 |
|
---|
1549 | Assert(pDev->Core.pszName);
|
---|
1550 | rc = RTUtf16ToUtf8Ex(VarName.pwszVal, RTSTR_MAX, &pDev->Core.pszName, cbName, NULL);
|
---|
1551 | if (RT_SUCCESS(rc))
|
---|
1552 | {
|
---|
1553 | Assert(pDev->Core.pszId);
|
---|
1554 | rc = RTUtf16ToUtf8Ex(pDev->wszDevId, RTSTR_MAX, &pDev->Core.pszId, cbId, NULL);
|
---|
1555 | if (RT_SUCCESS(rc))
|
---|
1556 | PDMAudioHostEnumAppend(pDevEnm, &pDev->Core);
|
---|
1557 | else
|
---|
1558 | PDMAudioHostDevFree(&pDev->Core);
|
---|
1559 | }
|
---|
1560 | else
|
---|
1561 | PDMAudioHostDevFree(&pDev->Core);
|
---|
1562 | }
|
---|
1563 | else
|
---|
1564 | rc = VERR_NO_MEMORY;
|
---|
1565 | PropVariantClear(&VarFormat);
|
---|
1566 | }
|
---|
1567 | else
|
---|
1568 | LogFunc(("Failed to get PKEY_AudioEngine_DeviceFormat: %Rhrc\n", hrc));
|
---|
1569 | CoTaskMemFree(pwszDevId);
|
---|
1570 | }
|
---|
1571 | else
|
---|
1572 | LogFunc(("Failed to get the device ID: %Rhrc\n", hrc));
|
---|
1573 | PropVariantClear(&VarName);
|
---|
1574 | }
|
---|
1575 | else
|
---|
1576 | LogFunc(("Failed to get PKEY_Device_FriendlyName: %Rhrc\n", hrc));
|
---|
1577 | pProperties->Release();
|
---|
1578 | }
|
---|
1579 | else
|
---|
1580 | LogFunc(("OpenPropertyStore failed: %Rhrc\n", hrc));
|
---|
1581 |
|
---|
1582 | if (hrc == E_OUTOFMEMORY && RT_SUCCESS_NP(rc))
|
---|
1583 | rc = VERR_NO_MEMORY;
|
---|
1584 | return rc;
|
---|
1585 | }
|
---|
1586 |
|
---|
1587 |
|
---|
1588 | /**
|
---|
1589 | * Does a (Re-)enumeration of the host's playback + capturing devices.
|
---|
1590 | *
|
---|
1591 | * @return VBox status code.
|
---|
1592 | * @param pThis The WASAPI host audio driver instance data.
|
---|
1593 | * @param pDevEnm Where to store the enumerated devices.
|
---|
1594 | */
|
---|
1595 | static int drvHostWasEnumerateDevices(PDRVHOSTAUDIOWAS pThis, PPDMAUDIOHOSTENUM pDevEnm)
|
---|
1596 | {
|
---|
1597 | LogRel2(("WasAPI: Enumerating devices ...\n"));
|
---|
1598 |
|
---|
1599 | int rc = VINF_SUCCESS;
|
---|
1600 | for (unsigned idxPass = 0; idxPass < 2 && RT_SUCCESS(rc); idxPass++)
|
---|
1601 | {
|
---|
1602 | EDataFlow const enmType = idxPass == 0 ? EDataFlow::eRender : EDataFlow::eCapture;
|
---|
1603 |
|
---|
1604 | /* Get the default device first. */
|
---|
1605 | IMMDevice *pIDefaultDevice = NULL;
|
---|
1606 | HRESULT hrc = pThis->pIEnumerator->GetDefaultAudioEndpoint(enmType, eMultimedia, &pIDefaultDevice);
|
---|
1607 | if (SUCCEEDED(hrc))
|
---|
1608 | rc = drvHostWasEnumAddDev(pDevEnm, pIDefaultDevice, enmType, true);
|
---|
1609 | else
|
---|
1610 | pIDefaultDevice = NULL;
|
---|
1611 |
|
---|
1612 | /* Enumerate the devices. */
|
---|
1613 | IMMDeviceCollection *pCollection = NULL;
|
---|
1614 | hrc = pThis->pIEnumerator->EnumAudioEndpoints(enmType, DEVICE_STATE_ACTIVE /*| DEVICE_STATE_UNPLUGGED?*/, &pCollection);
|
---|
1615 | if (SUCCEEDED(hrc) && pCollection != NULL)
|
---|
1616 | {
|
---|
1617 | UINT cDevices = 0;
|
---|
1618 | hrc = pCollection->GetCount(&cDevices);
|
---|
1619 | if (SUCCEEDED(hrc))
|
---|
1620 | {
|
---|
1621 | for (UINT idxDevice = 0; idxDevice < cDevices && RT_SUCCESS(rc); idxDevice++)
|
---|
1622 | {
|
---|
1623 | IMMDevice *pIDevice = NULL;
|
---|
1624 | hrc = pCollection->Item(idxDevice, &pIDevice);
|
---|
1625 | if (SUCCEEDED(hrc) && pIDevice)
|
---|
1626 | {
|
---|
1627 | if (pIDevice != pIDefaultDevice)
|
---|
1628 | rc = drvHostWasEnumAddDev(pDevEnm, pIDevice, enmType, false);
|
---|
1629 | pIDevice->Release();
|
---|
1630 | }
|
---|
1631 | }
|
---|
1632 | }
|
---|
1633 | pCollection->Release();
|
---|
1634 | }
|
---|
1635 | else
|
---|
1636 | LogRelMax(10, ("EnumAudioEndpoints(%s) failed: %Rhrc\n", idxPass == 0 ? "output" : "input", hrc));
|
---|
1637 |
|
---|
1638 | if (pIDefaultDevice)
|
---|
1639 | pIDefaultDevice->Release();
|
---|
1640 | }
|
---|
1641 |
|
---|
1642 | LogRel2(("WasAPI: Enumerating devices done - %u device (%Rrc)\n", pDevEnm->cDevices, rc));
|
---|
1643 | return rc;
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 |
|
---|
1647 | /**
|
---|
1648 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetDevices}
|
---|
1649 | */
|
---|
1650 | static DECLCALLBACK(int) drvHostAudioWasHA_GetDevices(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHOSTENUM pDeviceEnum)
|
---|
1651 | {
|
---|
1652 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
1653 | AssertPtrReturn(pDeviceEnum, VERR_INVALID_POINTER);
|
---|
1654 |
|
---|
1655 | PDMAudioHostEnumInit(pDeviceEnum);
|
---|
1656 | int rc = drvHostWasEnumerateDevices(pThis, pDeviceEnum);
|
---|
1657 | if (RT_FAILURE(rc))
|
---|
1658 | PDMAudioHostEnumDelete(pDeviceEnum);
|
---|
1659 |
|
---|
1660 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
1661 | return rc;
|
---|
1662 | }
|
---|
1663 |
|
---|
1664 |
|
---|
1665 | /**
|
---|
1666 | * Worker for drvHostAudioWasHA_SetDevice.
|
---|
1667 | */
|
---|
1668 | static int drvHostAudioWasSetDeviceWorker(PDRVHOSTAUDIOWAS pThis, const char *pszId, PRTUTF16 *ppwszDevId, IMMDevice **ppIDevice,
|
---|
1669 | EDataFlow enmFlow, PDMAUDIODIR enmDir, const char *pszWhat)
|
---|
1670 | {
|
---|
1671 | pThis->pNotifyClient->lockEnter();
|
---|
1672 |
|
---|
1673 | /*
|
---|
1674 | * Did anything actually change?
|
---|
1675 | */
|
---|
1676 | if ( (pszId == NULL) != (*ppwszDevId == NULL)
|
---|
1677 | || ( pszId
|
---|
1678 | && RTUtf16ICmpUtf8(*ppwszDevId, pszId) != 0))
|
---|
1679 | {
|
---|
1680 | /*
|
---|
1681 | * Duplicate the ID.
|
---|
1682 | */
|
---|
1683 | PRTUTF16 pwszDevId = NULL;
|
---|
1684 | if (pszId)
|
---|
1685 | {
|
---|
1686 | int rc = RTStrToUtf16(pszId, &pwszDevId);
|
---|
1687 | AssertRCReturnStmt(rc, pThis->pNotifyClient->lockLeave(), rc);
|
---|
1688 | }
|
---|
1689 |
|
---|
1690 | /*
|
---|
1691 | * Try get the device.
|
---|
1692 | */
|
---|
1693 | IMMDevice *pIDevice = NULL;
|
---|
1694 | HRESULT hrc;
|
---|
1695 | if (pwszDevId)
|
---|
1696 | hrc = pThis->pIEnumerator->GetDevice(pwszDevId, &pIDevice);
|
---|
1697 | else
|
---|
1698 | hrc = pThis->pIEnumerator->GetDefaultAudioEndpoint(enmFlow, eMultimedia, &pIDevice);
|
---|
1699 | LogFlowFunc(("Got device %p (%Rhrc)\n", pIDevice, hrc));
|
---|
1700 | if (FAILED(hrc))
|
---|
1701 | {
|
---|
1702 | LogRel(("WasAPI: Failed to get IMMDevice for %s audio device '%s' (SetDevice): %Rhrc\n",
|
---|
1703 | pszWhat, pszId ? pszId : "{default}", hrc));
|
---|
1704 | pIDevice = NULL;
|
---|
1705 | }
|
---|
1706 |
|
---|
1707 | /*
|
---|
1708 | * Make the switch.
|
---|
1709 | */
|
---|
1710 | LogRel(("PulseAudio: Changing %s device: '%ls' -> '%s'\n",
|
---|
1711 | pszWhat, *ppwszDevId ? *ppwszDevId : L"{Default}", pszId ? pszId : "{Default}"));
|
---|
1712 |
|
---|
1713 | if (*ppIDevice)
|
---|
1714 | (*ppIDevice)->Release();
|
---|
1715 | *ppIDevice = pIDevice;
|
---|
1716 |
|
---|
1717 | RTUtf16Free(*ppwszDevId);
|
---|
1718 | *ppwszDevId = pwszDevId;
|
---|
1719 |
|
---|
1720 | /*
|
---|
1721 | * Only notify the driver above us.
|
---|
1722 | */
|
---|
1723 | PPDMIHOSTAUDIOPORT const pIHostAudioPort = pThis->pIHostAudioPort;
|
---|
1724 | pThis->pNotifyClient->lockLeave();
|
---|
1725 |
|
---|
1726 | if (pIHostAudioPort)
|
---|
1727 | {
|
---|
1728 | LogFlowFunc(("Notifying parent driver about %s device change...\n", pszWhat));
|
---|
1729 | pIHostAudioPort->pfnNotifyDeviceChanged(pIHostAudioPort, enmDir, NULL);
|
---|
1730 | }
|
---|
1731 | }
|
---|
1732 | else
|
---|
1733 | {
|
---|
1734 | pThis->pNotifyClient->lockLeave();
|
---|
1735 | LogFunc(("No %s device change\n", pszWhat));
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 | return VINF_SUCCESS;
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 |
|
---|
1742 | /**
|
---|
1743 | * @interface_method_impl{PDMIHOSTAUDIO,pfnSetDevice}
|
---|
1744 | */
|
---|
1745 | static DECLCALLBACK(int) drvHostAudioWasHA_SetDevice(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir, const char *pszId)
|
---|
1746 | {
|
---|
1747 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
1748 |
|
---|
1749 | /*
|
---|
1750 | * Validate and normalize input.
|
---|
1751 | */
|
---|
1752 | AssertReturn(enmDir == PDMAUDIODIR_IN || enmDir == PDMAUDIODIR_OUT || enmDir == PDMAUDIODIR_DUPLEX, VERR_INVALID_PARAMETER);
|
---|
1753 | AssertPtrNullReturn(pszId, VERR_INVALID_POINTER);
|
---|
1754 | if (!pszId || !*pszId)
|
---|
1755 | pszId = NULL;
|
---|
1756 | else
|
---|
1757 | AssertReturn(strlen(pszId) < 1024, VERR_INVALID_NAME);
|
---|
1758 | LogFunc(("enmDir=%d pszId=%s\n", enmDir, pszId));
|
---|
1759 |
|
---|
1760 | /*
|
---|
1761 | * Do the updating.
|
---|
1762 | */
|
---|
1763 | if (enmDir == PDMAUDIODIR_IN || enmDir == PDMAUDIODIR_DUPLEX)
|
---|
1764 | {
|
---|
1765 | int rc = drvHostAudioWasSetDeviceWorker(pThis, pszId, &pThis->pwszInputDevId, &pThis->pIDeviceInput,
|
---|
1766 | eCapture, PDMAUDIODIR_IN, "input");
|
---|
1767 | AssertRCReturn(rc, rc);
|
---|
1768 | }
|
---|
1769 |
|
---|
1770 | if (enmDir == PDMAUDIODIR_OUT || enmDir == PDMAUDIODIR_DUPLEX)
|
---|
1771 | {
|
---|
1772 | int rc = drvHostAudioWasSetDeviceWorker(pThis, pszId, &pThis->pwszOutputDevId, &pThis->pIDeviceOutput,
|
---|
1773 | eRender, PDMAUDIODIR_OUT, "output");
|
---|
1774 | AssertRCReturn(rc, rc);
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | return VINF_SUCCESS;
|
---|
1778 | }
|
---|
1779 |
|
---|
1780 |
|
---|
1781 | /**
|
---|
1782 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
|
---|
1783 | */
|
---|
1784 | static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostAudioWasHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
|
---|
1785 | {
|
---|
1786 | RT_NOREF(pInterface, enmDir);
|
---|
1787 | return PDMAUDIOBACKENDSTS_RUNNING;
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 |
|
---|
1791 | /**
|
---|
1792 | * Performs the actual switching of device config.
|
---|
1793 | *
|
---|
1794 | * Worker for drvHostAudioWasDoStreamDevSwitch() and
|
---|
1795 | * drvHostAudioWasHA_StreamNotifyDeviceChanged().
|
---|
1796 | */
|
---|
1797 | static void drvHostAudioWasCompleteStreamDevSwitch(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASSTREAM pStreamWas,
|
---|
1798 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg)
|
---|
1799 | {
|
---|
1800 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
1801 |
|
---|
1802 | /* Do the switch. */
|
---|
1803 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfgOld = pStreamWas->pDevCfg;
|
---|
1804 | pStreamWas->pDevCfg = pDevCfg;
|
---|
1805 |
|
---|
1806 | /* The new stream is neither started nor draining. */
|
---|
1807 | pStreamWas->fStarted = false;
|
---|
1808 | pStreamWas->fDraining = false;
|
---|
1809 |
|
---|
1810 | /* Device switching is done now. */
|
---|
1811 | pStreamWas->fSwitchingDevice = false;
|
---|
1812 |
|
---|
1813 | /* Stop the old stream or Reset() will fail when putting it back into the cache. */
|
---|
1814 | if (pStreamWas->fEnabled && pDevCfgOld->pIAudioClient)
|
---|
1815 | pDevCfgOld->pIAudioClient->Stop();
|
---|
1816 |
|
---|
1817 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
1818 |
|
---|
1819 | /* Notify DrvAudio. */
|
---|
1820 | pThis->pIHostAudioPort->pfnStreamNotifyDeviceChanged(pThis->pIHostAudioPort, &pStreamWas->Core, false /*fReInit*/);
|
---|
1821 |
|
---|
1822 | /* Put the old config back into the cache. */
|
---|
1823 | drvHostAudioWasCachePutBack(pThis, pDevCfgOld);
|
---|
1824 |
|
---|
1825 | LogFlowFunc(("returns with '%s' state: %s\n", pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
1826 | }
|
---|
1827 |
|
---|
1828 |
|
---|
1829 | /**
|
---|
1830 | * Called on a worker thread to initialize a new device config and switch the
|
---|
1831 | * given stream to using it.
|
---|
1832 | *
|
---|
1833 | * @sa drvHostAudioWasHA_StreamNotifyDeviceChanged
|
---|
1834 | */
|
---|
1835 | static void drvHostAudioWasDoStreamDevSwitch(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASSTREAM pStreamWas,
|
---|
1836 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg)
|
---|
1837 | {
|
---|
1838 | /*
|
---|
1839 | * Do the initializing.
|
---|
1840 | */
|
---|
1841 | int rc = drvHostAudioWasCacheInitConfig(pDevCfg);
|
---|
1842 | if (RT_SUCCESS(rc))
|
---|
1843 | drvHostAudioWasCompleteStreamDevSwitch(pThis, pStreamWas, pDevCfg);
|
---|
1844 | else
|
---|
1845 | {
|
---|
1846 | LogRelMax(64, ("WasAPI: Failed to set up new device config '%ls:%s' for stream '%s': %Rrc\n",
|
---|
1847 | pDevCfg->pDevEntry->wszDevId, pDevCfg->szProps, pStreamWas->Cfg.szName, rc));
|
---|
1848 | drvHostAudioWasCacheDestroyDevConfig(pThis, pDevCfg);
|
---|
1849 | pThis->pIHostAudioPort->pfnStreamNotifyDeviceChanged(pThis->pIHostAudioPort, &pStreamWas->Core, true /*fReInit*/);
|
---|
1850 | }
|
---|
1851 | }
|
---|
1852 |
|
---|
1853 |
|
---|
1854 | /**
|
---|
1855 | * @interface_method_impl{PDMIHOSTAUDIO,pfnDoOnWorkerThread}
|
---|
1856 | */
|
---|
1857 | static DECLCALLBACK(void) drvHostAudioWasHA_DoOnWorkerThread(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
1858 | uintptr_t uUser, void *pvUser)
|
---|
1859 | {
|
---|
1860 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
1861 | LogFlowFunc(("uUser=%#zx pStream=%p pvUser=%p\n", uUser, pStream, pvUser));
|
---|
1862 |
|
---|
1863 | switch (uUser)
|
---|
1864 | {
|
---|
1865 | case DRVHOSTAUDIOWAS_DO_PURGE_CACHE:
|
---|
1866 | Assert(pStream == NULL);
|
---|
1867 | Assert(pvUser == NULL);
|
---|
1868 | drvHostAudioWasCachePurge(pThis, true /*fOnWorker*/);
|
---|
1869 | break;
|
---|
1870 |
|
---|
1871 | case DRVHOSTAUDIOWAS_DO_PRUNE_CACHE:
|
---|
1872 | Assert(pStream == NULL);
|
---|
1873 | Assert(pvUser == NULL);
|
---|
1874 | drvHostAudioWasCachePrune(pThis);
|
---|
1875 | break;
|
---|
1876 |
|
---|
1877 | case DRVHOSTAUDIOWAS_DO_STREAM_DEV_SWITCH:
|
---|
1878 | AssertPtr(pStream);
|
---|
1879 | AssertPtr(pvUser);
|
---|
1880 | drvHostAudioWasDoStreamDevSwitch(pThis, (PDRVHOSTAUDIOWASSTREAM)pStream, (PDRVHOSTAUDIOWASCACHEDEVCFG)pvUser);
|
---|
1881 | break;
|
---|
1882 |
|
---|
1883 | default:
|
---|
1884 | AssertMsgFailedBreak(("%#zx\n", uUser));
|
---|
1885 | }
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 |
|
---|
1889 | /**
|
---|
1890 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamConfigHint}
|
---|
1891 | *
|
---|
1892 | * @note This is called on a DrvAudio worker thread.
|
---|
1893 | */
|
---|
1894 | static DECLCALLBACK(void) drvHostAudioWasHA_StreamConfigHint(PPDMIHOSTAUDIO pInterface, PPDMAUDIOSTREAMCFG pCfg)
|
---|
1895 | {
|
---|
1896 | #if 0 /* disable to test async stream creation. */
|
---|
1897 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
1898 | LogFlowFunc(("pCfg=%p\n", pCfg));
|
---|
1899 |
|
---|
1900 | drvHostWasCacheConfigHinting(pThis, pCfg);
|
---|
1901 | #else
|
---|
1902 | RT_NOREF(pInterface, pCfg);
|
---|
1903 | #endif
|
---|
1904 | }
|
---|
1905 |
|
---|
1906 |
|
---|
1907 | /**
|
---|
1908 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
|
---|
1909 | */
|
---|
1910 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
1911 | PCPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
1912 | {
|
---|
1913 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
1914 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
1915 | AssertPtrReturn(pStreamWas, VERR_INVALID_POINTER);
|
---|
1916 | AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
|
---|
1917 | AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
|
---|
1918 | AssertReturn(pCfgReq->enmDir == PDMAUDIODIR_IN || pCfgReq->enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
|
---|
1919 | Assert(PDMAudioStrmCfgEquals(pCfgReq, pCfgAcq));
|
---|
1920 |
|
---|
1921 | const char * const pszStreamType = pCfgReq->enmDir == PDMAUDIODIR_IN ? "capture" : "playback"; RT_NOREF(pszStreamType);
|
---|
1922 | LogFlowFunc(("enmPath=%s '%s'\n", PDMAudioPathGetName(pCfgReq->enmPath), pCfgReq->szName));
|
---|
1923 | #if defined(RTLOG_REL_ENABLED) || defined(LOG_ENABLED)
|
---|
1924 | char szTmp[64];
|
---|
1925 | #endif
|
---|
1926 | LogRel2(("WasAPI: Opening %s stream '%s' (%s)\n", pCfgReq->szName, pszStreamType,
|
---|
1927 | PDMAudioPropsToString(&pCfgReq->Props, szTmp, sizeof(szTmp))));
|
---|
1928 |
|
---|
1929 | RTListInit(&pStreamWas->ListEntry);
|
---|
1930 |
|
---|
1931 | /*
|
---|
1932 | * Do configuration conversion.
|
---|
1933 | */
|
---|
1934 | WAVEFORMATEXTENSIBLE WaveFmtExt;
|
---|
1935 | drvHostAudioWasWaveFmtExtFromProps(&pCfgReq->Props, &WaveFmtExt);
|
---|
1936 | LogRel2(("WasAPI: Requested %s format for '%s':\n"
|
---|
1937 | "WasAPI: wFormatTag = %#RX16\n"
|
---|
1938 | "WasAPI: nChannels = %RU16\n"
|
---|
1939 | "WasAPI: nSamplesPerSec = %RU32\n"
|
---|
1940 | "WasAPI: nAvgBytesPerSec = %RU32\n"
|
---|
1941 | "WasAPI: nBlockAlign = %RU16\n"
|
---|
1942 | "WasAPI: wBitsPerSample = %RU16\n"
|
---|
1943 | "WasAPI: cbSize = %RU16\n"
|
---|
1944 | "WasAPI: cBufferSizeInNtTicks = %RU64\n",
|
---|
1945 | pszStreamType, pCfgReq->szName, WaveFmtExt.Format.wFormatTag, WaveFmtExt.Format.nChannels,
|
---|
1946 | WaveFmtExt.Format.nSamplesPerSec, WaveFmtExt.Format.nAvgBytesPerSec, WaveFmtExt.Format.nBlockAlign,
|
---|
1947 | WaveFmtExt.Format.wBitsPerSample, WaveFmtExt.Format.cbSize,
|
---|
1948 | PDMAudioPropsFramesToNtTicks(&pCfgReq->Props, pCfgReq->Backend.cFramesBufferSize) ));
|
---|
1949 | if (WaveFmtExt.Format.cbSize != 0)
|
---|
1950 | LogRel2(("WasAPI: dwChannelMask = %#RX32\n"
|
---|
1951 | "WasAPI: wValidBitsPerSample = %RU16\n",
|
---|
1952 | WaveFmtExt.dwChannelMask, WaveFmtExt.Samples.wValidBitsPerSample));
|
---|
1953 |
|
---|
1954 | /* Set up the acquired format here as channel count + layout may have
|
---|
1955 | changed and need to be communicated to caller and used in cache lookup. */
|
---|
1956 | *pCfgAcq = *pCfgReq;
|
---|
1957 | if (WaveFmtExt.Format.cbSize != 0)
|
---|
1958 | {
|
---|
1959 | PDMAudioPropsSetChannels(&pCfgAcq->Props, WaveFmtExt.Format.nChannels);
|
---|
1960 | uint8_t idCh = 0;
|
---|
1961 | for (unsigned iBit = 0; iBit < 32 && idCh < WaveFmtExt.Format.nChannels; iBit++)
|
---|
1962 | if (WaveFmtExt.dwChannelMask & RT_BIT_32(iBit))
|
---|
1963 | {
|
---|
1964 | pCfgAcq->Props.aidChannels[idCh] = (unsigned)PDMAUDIOCHANNELID_FIRST_STANDARD + iBit;
|
---|
1965 | idCh++;
|
---|
1966 | }
|
---|
1967 | Assert(idCh == WaveFmtExt.Format.nChannels);
|
---|
1968 | }
|
---|
1969 |
|
---|
1970 | /*
|
---|
1971 | * Get the device we're supposed to use.
|
---|
1972 | * (We cache this as it takes ~2ms to get the default device on a random W10 19042 system.)
|
---|
1973 | */
|
---|
1974 | pThis->pNotifyClient->lockEnter();
|
---|
1975 | IMMDevice *pIDevice = pCfgReq->enmDir == PDMAUDIODIR_IN ? pThis->pIDeviceInput : pThis->pIDeviceOutput;
|
---|
1976 | if (pIDevice)
|
---|
1977 | pIDevice->AddRef();
|
---|
1978 | pThis->pNotifyClient->lockLeave();
|
---|
1979 |
|
---|
1980 | PRTUTF16 pwszDevId = pCfgReq->enmDir == PDMAUDIODIR_IN ? pThis->pwszInputDevId : pThis->pwszOutputDevId;
|
---|
1981 | PRTUTF16 const pwszDevIdDesc = pwszDevId ? pwszDevId : pCfgReq->enmDir == PDMAUDIODIR_IN ? L"{Default-In}" : L"{Default-Out}";
|
---|
1982 | if (!pIDevice)
|
---|
1983 | {
|
---|
1984 | /* This might not strictly be necessary anymore, however it shouldn't
|
---|
1985 | hurt and may be useful when using specific devices. */
|
---|
1986 | HRESULT hrc;
|
---|
1987 | if (pwszDevId)
|
---|
1988 | hrc = pThis->pIEnumerator->GetDevice(pwszDevId, &pIDevice);
|
---|
1989 | else
|
---|
1990 | hrc = pThis->pIEnumerator->GetDefaultAudioEndpoint(pCfgReq->enmDir == PDMAUDIODIR_IN ? eCapture : eRender,
|
---|
1991 | eMultimedia, &pIDevice);
|
---|
1992 | LogFlowFunc(("Got device %p (%Rhrc)\n", pIDevice, hrc));
|
---|
1993 | if (FAILED(hrc))
|
---|
1994 | {
|
---|
1995 | LogRelMax(64, ("WasAPI: Failed to open audio %s device '%ls': %Rhrc\n", pszStreamType, pwszDevIdDesc, hrc));
|
---|
1996 | return VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
1997 | }
|
---|
1998 | }
|
---|
1999 |
|
---|
2000 | /*
|
---|
2001 | * Ask the cache to retrieve or instantiate the requested configuration.
|
---|
2002 | */
|
---|
2003 | /** @todo make it return a status code too and retry if the default device
|
---|
2004 | * was invalidated/changed while we where working on it here. */
|
---|
2005 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg = NULL;
|
---|
2006 | int rc = drvHostAudioWasCacheLookupOrCreate(pThis, pIDevice, pCfgAcq, false /*fOnWorker*/, &pDevCfg);
|
---|
2007 |
|
---|
2008 | pIDevice->Release();
|
---|
2009 | pIDevice = NULL;
|
---|
2010 |
|
---|
2011 | if (pDevCfg && RT_SUCCESS(rc))
|
---|
2012 | {
|
---|
2013 | pStreamWas->pDevCfg = pDevCfg;
|
---|
2014 |
|
---|
2015 | pCfgAcq->Props = pDevCfg->Props;
|
---|
2016 | pCfgAcq->Backend.cFramesBufferSize = pDevCfg->cFramesBufferSize;
|
---|
2017 | pCfgAcq->Backend.cFramesPeriod = pDevCfg->cFramesPeriod;
|
---|
2018 | pCfgAcq->Backend.cFramesPreBuffering = pCfgReq->Backend.cFramesPreBuffering * pDevCfg->cFramesBufferSize
|
---|
2019 | / RT_MAX(pCfgReq->Backend.cFramesBufferSize, 1);
|
---|
2020 |
|
---|
2021 | PDMAudioStrmCfgCopy(&pStreamWas->Cfg, pCfgAcq);
|
---|
2022 |
|
---|
2023 | /* Finally, the critical section. */
|
---|
2024 | int rc2 = RTCritSectInit(&pStreamWas->CritSect);
|
---|
2025 | if (RT_SUCCESS(rc2))
|
---|
2026 | {
|
---|
2027 | RTCritSectRwEnterExcl(&pThis->CritSectStreamList);
|
---|
2028 | RTListAppend(&pThis->StreamHead, &pStreamWas->ListEntry);
|
---|
2029 | RTCritSectRwLeaveExcl(&pThis->CritSectStreamList);
|
---|
2030 |
|
---|
2031 | if (pStreamWas->pDevCfg->pIAudioClient != NULL)
|
---|
2032 | {
|
---|
2033 | LogFlowFunc(("returns VINF_SUCCESS\n", rc));
|
---|
2034 | return VINF_SUCCESS;
|
---|
2035 | }
|
---|
2036 | LogFlowFunc(("returns VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED\n", rc));
|
---|
2037 | return VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED;
|
---|
2038 | }
|
---|
2039 |
|
---|
2040 | LogRelMax(64, ("WasAPI: Failed to create critical section for stream.\n"));
|
---|
2041 | drvHostAudioWasCachePutBack(pThis, pDevCfg);
|
---|
2042 | pStreamWas->pDevCfg = NULL;
|
---|
2043 | }
|
---|
2044 | else
|
---|
2045 | LogRelMax(64, ("WasAPI: Failed to setup %s on audio device '%ls' (%Rrc).\n", pszStreamType, pwszDevIdDesc, rc));
|
---|
2046 |
|
---|
2047 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2048 | return rc;
|
---|
2049 | }
|
---|
2050 |
|
---|
2051 |
|
---|
2052 | /**
|
---|
2053 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamInitAsync}
|
---|
2054 | */
|
---|
2055 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamInitAsync(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2056 | bool fDestroyed)
|
---|
2057 | {
|
---|
2058 | RT_NOREF(pInterface);
|
---|
2059 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2060 | AssertPtrReturn(pStreamWas, VERR_INVALID_POINTER);
|
---|
2061 | LogFlowFunc(("Stream '%s'%s\n", pStreamWas->Cfg.szName, fDestroyed ? " - destroyed!" : ""));
|
---|
2062 |
|
---|
2063 | /*
|
---|
2064 | * Assert sane preconditions for this call.
|
---|
2065 | */
|
---|
2066 | AssertPtrReturn(pStreamWas->Core.pStream, VERR_INTERNAL_ERROR);
|
---|
2067 | AssertPtrReturn(pStreamWas->pDevCfg, VERR_INTERNAL_ERROR_2);
|
---|
2068 | AssertPtrReturn(pStreamWas->pDevCfg->pDevEntry, VERR_INTERNAL_ERROR_3);
|
---|
2069 | AssertPtrReturn(pStreamWas->pDevCfg->pDevEntry->pIDevice, VERR_INTERNAL_ERROR_4);
|
---|
2070 | AssertReturn(pStreamWas->pDevCfg->pDevEntry->enmDir == pStreamWas->Core.pStream->Cfg.enmDir, VERR_INTERNAL_ERROR_4);
|
---|
2071 | AssertReturn(pStreamWas->pDevCfg->pIAudioClient == NULL, VERR_INTERNAL_ERROR_5);
|
---|
2072 | AssertReturn(pStreamWas->pDevCfg->pIAudioRenderClient == NULL, VERR_INTERNAL_ERROR_5);
|
---|
2073 | AssertReturn(pStreamWas->pDevCfg->pIAudioCaptureClient == NULL, VERR_INTERNAL_ERROR_5);
|
---|
2074 |
|
---|
2075 | /*
|
---|
2076 | * Do the job.
|
---|
2077 | */
|
---|
2078 | int rc;
|
---|
2079 | if (!fDestroyed)
|
---|
2080 | rc = drvHostAudioWasCacheInitConfig(pStreamWas->pDevCfg);
|
---|
2081 | else
|
---|
2082 | {
|
---|
2083 | AssertReturn(pStreamWas->pDevCfg->rcSetup == VERR_AUDIO_STREAM_INIT_IN_PROGRESS, VERR_INTERNAL_ERROR_2);
|
---|
2084 | pStreamWas->pDevCfg->rcSetup = VERR_WRONG_ORDER;
|
---|
2085 | rc = VINF_SUCCESS;
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | LogFlowFunc(("returns %Rrc (%s)\n", rc, pStreamWas->Cfg.szName));
|
---|
2089 | return rc;
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 |
|
---|
2093 | /**
|
---|
2094 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
|
---|
2095 | */
|
---|
2096 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2097 | bool fImmediate)
|
---|
2098 | {
|
---|
2099 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
2100 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2101 | AssertPtrReturn(pStreamWas, VERR_INVALID_POINTER);
|
---|
2102 | LogFlowFunc(("Stream '%s'\n", pStreamWas->Cfg.szName));
|
---|
2103 | RT_NOREF(fImmediate);
|
---|
2104 | HRESULT hrc;
|
---|
2105 |
|
---|
2106 | if (RTCritSectIsInitialized(&pStreamWas->CritSect))
|
---|
2107 | {
|
---|
2108 | RTCritSectRwEnterExcl(&pThis->CritSectStreamList);
|
---|
2109 | RTListNodeRemove(&pStreamWas->ListEntry);
|
---|
2110 | RTCritSectRwLeaveExcl(&pThis->CritSectStreamList);
|
---|
2111 |
|
---|
2112 | RTCritSectDelete(&pStreamWas->CritSect);
|
---|
2113 | }
|
---|
2114 |
|
---|
2115 | if (pStreamWas->fStarted && pStreamWas->pDevCfg && pStreamWas->pDevCfg->pIAudioClient)
|
---|
2116 | {
|
---|
2117 | hrc = pStreamWas->pDevCfg->pIAudioClient->Stop();
|
---|
2118 | LogFunc(("Stop('%s') -> %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2119 | pStreamWas->fStarted = false;
|
---|
2120 | }
|
---|
2121 |
|
---|
2122 | if (pStreamWas->cFramesCaptureToRelease)
|
---|
2123 | {
|
---|
2124 | hrc = pStreamWas->pDevCfg->pIAudioCaptureClient->ReleaseBuffer(0);
|
---|
2125 | Log4Func(("Releasing capture buffer (%#x frames): %Rhrc\n", pStreamWas->cFramesCaptureToRelease, hrc));
|
---|
2126 | pStreamWas->cFramesCaptureToRelease = 0;
|
---|
2127 | pStreamWas->pbCapture = NULL;
|
---|
2128 | pStreamWas->cbCapture = 0;
|
---|
2129 | }
|
---|
2130 |
|
---|
2131 | if (pStreamWas->pDevCfg)
|
---|
2132 | {
|
---|
2133 | drvHostAudioWasCachePutBack(pThis, pStreamWas->pDevCfg);
|
---|
2134 | pStreamWas->pDevCfg = NULL;
|
---|
2135 | }
|
---|
2136 |
|
---|
2137 | LogFlowFunc(("returns\n"));
|
---|
2138 | return VINF_SUCCESS;
|
---|
2139 | }
|
---|
2140 |
|
---|
2141 |
|
---|
2142 | /**
|
---|
2143 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamNotifyDeviceChanged}
|
---|
2144 | */
|
---|
2145 | static DECLCALLBACK(void) drvHostAudioWasHA_StreamNotifyDeviceChanged(PPDMIHOSTAUDIO pInterface,
|
---|
2146 | PPDMAUDIOBACKENDSTREAM pStream, void *pvUser)
|
---|
2147 | {
|
---|
2148 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
2149 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2150 | LogFlowFunc(("pStreamWas=%p (%s)\n", pStreamWas, pStreamWas->Cfg.szName));
|
---|
2151 | RT_NOREF(pvUser);
|
---|
2152 |
|
---|
2153 | /*
|
---|
2154 | * See if we've got a cached config for the new device around.
|
---|
2155 | * We ignore this entirely, for now at least, if the device was
|
---|
2156 | * disconnected and there is no replacement.
|
---|
2157 | */
|
---|
2158 | pThis->pNotifyClient->lockEnter();
|
---|
2159 | IMMDevice *pIDevice = pStreamWas->Cfg.enmDir == PDMAUDIODIR_IN ? pThis->pIDeviceInput : pThis->pIDeviceOutput;
|
---|
2160 | if (pIDevice)
|
---|
2161 | pIDevice->AddRef();
|
---|
2162 | pThis->pNotifyClient->lockLeave();
|
---|
2163 | if (pIDevice)
|
---|
2164 | {
|
---|
2165 | PDRVHOSTAUDIOWASCACHEDEVCFG pDevCfg = NULL;
|
---|
2166 | int rc = drvHostAudioWasCacheLookupOrCreate(pThis, pIDevice, &pStreamWas->Cfg, false /*fOnWorker*/, &pDevCfg);
|
---|
2167 |
|
---|
2168 | pIDevice->Release();
|
---|
2169 | pIDevice = NULL;
|
---|
2170 |
|
---|
2171 | /*
|
---|
2172 | * If we have a working audio client, just do the switch.
|
---|
2173 | */
|
---|
2174 | if (RT_SUCCESS(rc) && pDevCfg->pIAudioClient)
|
---|
2175 | {
|
---|
2176 | LogFlowFunc(("New device config is ready already!\n"));
|
---|
2177 | Assert(rc == VINF_SUCCESS);
|
---|
2178 | drvHostAudioWasCompleteStreamDevSwitch(pThis, pStreamWas, pDevCfg);
|
---|
2179 | }
|
---|
2180 | /*
|
---|
2181 | * Otherwise create one asynchronously on a worker thread.
|
---|
2182 | */
|
---|
2183 | else if (RT_SUCCESS(rc))
|
---|
2184 | {
|
---|
2185 | LogFlowFunc(("New device config needs async init ...\n"));
|
---|
2186 | Assert(rc == VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED);
|
---|
2187 |
|
---|
2188 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2189 | pStreamWas->fSwitchingDevice = true;
|
---|
2190 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2191 |
|
---|
2192 | pThis->pIHostAudioPort->pfnStreamNotifyPreparingDeviceSwitch(pThis->pIHostAudioPort, &pStreamWas->Core);
|
---|
2193 |
|
---|
2194 | rc = pThis->pIHostAudioPort->pfnDoOnWorkerThread(pThis->pIHostAudioPort, &pStreamWas->Core,
|
---|
2195 | DRVHOSTAUDIOWAS_DO_STREAM_DEV_SWITCH, pDevCfg);
|
---|
2196 | AssertRCStmt(rc, drvHostAudioWasDoStreamDevSwitch(pThis, pStreamWas, pDevCfg));
|
---|
2197 | }
|
---|
2198 | else
|
---|
2199 | {
|
---|
2200 | LogRelMax(64, ("WasAPI: Failed to create new device config '%ls:%s' for stream '%s': %Rrc\n",
|
---|
2201 | pDevCfg->pDevEntry->wszDevId, pDevCfg->szProps, pStreamWas->Cfg.szName, rc));
|
---|
2202 |
|
---|
2203 | pThis->pIHostAudioPort->pfnStreamNotifyDeviceChanged(pThis->pIHostAudioPort, &pStreamWas->Core, true /*fReInit*/);
|
---|
2204 | }
|
---|
2205 | }
|
---|
2206 | else
|
---|
2207 | LogFlowFunc(("no new device, leaving it as-is\n"));
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 |
|
---|
2211 | /**
|
---|
2212 | * Wrapper for starting a stream.
|
---|
2213 | *
|
---|
2214 | * @returns VBox status code.
|
---|
2215 | * @param pThis The WASAPI host audio driver instance data.
|
---|
2216 | * @param pStreamWas The stream.
|
---|
2217 | * @param pszOperation The operation we're doing.
|
---|
2218 | */
|
---|
2219 | static int drvHostAudioWasStreamStartWorker(PDRVHOSTAUDIOWAS pThis, PDRVHOSTAUDIOWASSTREAM pStreamWas, const char *pszOperation)
|
---|
2220 | {
|
---|
2221 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->Start();
|
---|
2222 | LogFlow(("%s: Start(%s) returns %Rhrc\n", pszOperation, pStreamWas->Cfg.szName, hrc));
|
---|
2223 | AssertStmt(hrc != AUDCLNT_E_NOT_STOPPED, hrc = S_OK);
|
---|
2224 | if (SUCCEEDED(hrc))
|
---|
2225 | {
|
---|
2226 | pStreamWas->fStarted = true;
|
---|
2227 | return VINF_SUCCESS;
|
---|
2228 | }
|
---|
2229 |
|
---|
2230 | /** @todo try re-setup the stuff on AUDCLNT_E_DEVICEINVALIDATED.
|
---|
2231 | * Need some way of telling the caller (e.g. playback, capture) so they can
|
---|
2232 | * retry what they're doing */
|
---|
2233 | RT_NOREF(pThis);
|
---|
2234 |
|
---|
2235 | pStreamWas->fStarted = false;
|
---|
2236 | LogRelMax(64, ("WasAPI: Starting '%s' failed (%s): %Rhrc\n", pStreamWas->Cfg.szName, pszOperation, hrc));
|
---|
2237 | return VERR_AUDIO_STREAM_NOT_READY;
|
---|
2238 | }
|
---|
2239 |
|
---|
2240 |
|
---|
2241 | /**
|
---|
2242 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamEnable}
|
---|
2243 | */
|
---|
2244 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamEnable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2245 | {
|
---|
2246 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
2247 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2248 | LogFlowFunc(("Stream '%s' {%s}\n", pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2249 | HRESULT hrc;
|
---|
2250 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2251 |
|
---|
2252 | Assert(!pStreamWas->fEnabled);
|
---|
2253 | Assert(!pStreamWas->fStarted);
|
---|
2254 |
|
---|
2255 | /*
|
---|
2256 | * We always reset the buffer before enabling the stream (normally never necessary).
|
---|
2257 | */
|
---|
2258 | if (pStreamWas->cFramesCaptureToRelease)
|
---|
2259 | {
|
---|
2260 | hrc = pStreamWas->pDevCfg->pIAudioCaptureClient->ReleaseBuffer(pStreamWas->cFramesCaptureToRelease);
|
---|
2261 | Log4Func(("Releasing capture buffer (%#x frames): %Rhrc\n", pStreamWas->cFramesCaptureToRelease, hrc));
|
---|
2262 | pStreamWas->cFramesCaptureToRelease = 0;
|
---|
2263 | pStreamWas->pbCapture = NULL;
|
---|
2264 | pStreamWas->cbCapture = 0;
|
---|
2265 | }
|
---|
2266 |
|
---|
2267 | hrc = pStreamWas->pDevCfg->pIAudioClient->Reset();
|
---|
2268 | if (FAILED(hrc))
|
---|
2269 | LogRelMax(64, ("WasAPI: Stream reset failed when enabling '%s': %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2270 | pStreamWas->offInternal = 0;
|
---|
2271 | pStreamWas->fDraining = false;
|
---|
2272 | pStreamWas->fEnabled = true;
|
---|
2273 | pStreamWas->fRestartOnResume = false;
|
---|
2274 |
|
---|
2275 | /*
|
---|
2276 | * Input streams will start capturing, while output streams will only start
|
---|
2277 | * playing once we get some audio data to play.
|
---|
2278 | */
|
---|
2279 | int rc = VINF_SUCCESS;
|
---|
2280 | if (pStreamWas->Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
2281 | rc = drvHostAudioWasStreamStartWorker(pThis, pStreamWas, "enable");
|
---|
2282 | else
|
---|
2283 | Assert(pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT);
|
---|
2284 |
|
---|
2285 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2286 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
2287 | return rc;
|
---|
2288 | }
|
---|
2289 |
|
---|
2290 |
|
---|
2291 | /**
|
---|
2292 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDisable}
|
---|
2293 | */
|
---|
2294 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamDisable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2295 | {
|
---|
2296 | RT_NOREF(pInterface);
|
---|
2297 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2298 | LogFlowFunc(("cMsLastTransfer=%RI64 ms, stream '%s' {%s} \n",
|
---|
2299 | pStreamWas->msLastTransfer ? RTTimeMilliTS() - pStreamWas->msLastTransfer : -1,
|
---|
2300 | pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2301 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2302 |
|
---|
2303 | /*
|
---|
2304 | * Always try stop it (draining or no).
|
---|
2305 | */
|
---|
2306 | pStreamWas->fEnabled = false;
|
---|
2307 | pStreamWas->fRestartOnResume = false;
|
---|
2308 | Assert(!pStreamWas->fDraining || pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT);
|
---|
2309 |
|
---|
2310 | int rc = VINF_SUCCESS;
|
---|
2311 | if (pStreamWas->fStarted)
|
---|
2312 | {
|
---|
2313 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->Stop();
|
---|
2314 | LogFlowFunc(("Stop(%s) returns %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2315 | if (FAILED(hrc))
|
---|
2316 | {
|
---|
2317 | LogRelMax(64, ("WasAPI: Stopping '%s' failed (disable): %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2318 | rc = VERR_GENERAL_FAILURE;
|
---|
2319 | }
|
---|
2320 | pStreamWas->fStarted = false;
|
---|
2321 | pStreamWas->fDraining = false;
|
---|
2322 | }
|
---|
2323 |
|
---|
2324 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2325 | LogFlowFunc(("returns %Rrc {%s}\n", rc, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2326 | return rc;
|
---|
2327 | }
|
---|
2328 |
|
---|
2329 |
|
---|
2330 | /**
|
---|
2331 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPause}
|
---|
2332 | *
|
---|
2333 | * @note Basically the same as drvHostAudioWasHA_StreamDisable, just w/o the
|
---|
2334 | * buffer resetting and fEnabled change.
|
---|
2335 | */
|
---|
2336 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamPause(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2337 | {
|
---|
2338 | RT_NOREF(pInterface);
|
---|
2339 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2340 | LogFlowFunc(("cMsLastTransfer=%RI64 ms, stream '%s' {%s} \n",
|
---|
2341 | pStreamWas->msLastTransfer ? RTTimeMilliTS() - pStreamWas->msLastTransfer : -1,
|
---|
2342 | pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2343 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2344 |
|
---|
2345 | /*
|
---|
2346 | * Unless we're draining the stream, stop it if it's started.
|
---|
2347 | */
|
---|
2348 | int rc = VINF_SUCCESS;
|
---|
2349 | if (pStreamWas->fStarted && !pStreamWas->fDraining)
|
---|
2350 | {
|
---|
2351 | pStreamWas->fRestartOnResume = true;
|
---|
2352 |
|
---|
2353 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->Stop();
|
---|
2354 | LogFlowFunc(("Stop(%s) returns %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2355 | if (FAILED(hrc))
|
---|
2356 | {
|
---|
2357 | LogRelMax(64, ("WasAPI: Stopping '%s' failed (pause): %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2358 | rc = VERR_GENERAL_FAILURE;
|
---|
2359 | }
|
---|
2360 | pStreamWas->fStarted = false;
|
---|
2361 | }
|
---|
2362 | else
|
---|
2363 | {
|
---|
2364 | pStreamWas->fRestartOnResume = false;
|
---|
2365 | if (pStreamWas->fDraining)
|
---|
2366 | {
|
---|
2367 | LogFunc(("Stream '%s' is draining\n", pStreamWas->Cfg.szName));
|
---|
2368 | Assert(pStreamWas->fStarted);
|
---|
2369 | }
|
---|
2370 | }
|
---|
2371 |
|
---|
2372 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2373 | LogFlowFunc(("returns %Rrc {%s}\n", rc, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2374 | return rc;
|
---|
2375 | }
|
---|
2376 |
|
---|
2377 |
|
---|
2378 | /**
|
---|
2379 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamResume}
|
---|
2380 | */
|
---|
2381 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamResume(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2382 | {
|
---|
2383 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
2384 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2385 | LogFlowFunc(("Stream '%s' {%s}\n", pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2386 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2387 |
|
---|
2388 | /*
|
---|
2389 | * Resume according to state saved by drvHostAudioWasHA_StreamPause.
|
---|
2390 | */
|
---|
2391 | int rc;
|
---|
2392 | if (pStreamWas->fRestartOnResume)
|
---|
2393 | rc = drvHostAudioWasStreamStartWorker(pThis, pStreamWas, "resume");
|
---|
2394 | else
|
---|
2395 | rc = VINF_SUCCESS;
|
---|
2396 | pStreamWas->fRestartOnResume = false;
|
---|
2397 |
|
---|
2398 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2399 | LogFlowFunc(("returns %Rrc {%s}\n", rc, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2400 | return rc;
|
---|
2401 | }
|
---|
2402 |
|
---|
2403 |
|
---|
2404 | /**
|
---|
2405 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDrain}
|
---|
2406 | */
|
---|
2407 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamDrain(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2408 | {
|
---|
2409 | RT_NOREF(pInterface);
|
---|
2410 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2411 | AssertReturn(pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT, VERR_INVALID_PARAMETER);
|
---|
2412 | LogFlowFunc(("cMsLastTransfer=%RI64 ms, stream '%s' {%s} \n",
|
---|
2413 | pStreamWas->msLastTransfer ? RTTimeMilliTS() - pStreamWas->msLastTransfer : -1,
|
---|
2414 | pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2415 |
|
---|
2416 | /*
|
---|
2417 | * If the stram was started, calculate when the buffered data has finished
|
---|
2418 | * playing and switch to drain mode. DrvAudio will keep on calling
|
---|
2419 | * pfnStreamPlay with an empty buffer while we're draining, so we'll use
|
---|
2420 | * that for checking the deadline and finally stopping the stream.
|
---|
2421 | */
|
---|
2422 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2423 | int rc = VINF_SUCCESS;
|
---|
2424 | if (pStreamWas->fStarted)
|
---|
2425 | {
|
---|
2426 | if (!pStreamWas->fDraining)
|
---|
2427 | {
|
---|
2428 | uint64_t const msNow = RTTimeMilliTS();
|
---|
2429 | uint64_t msDrainDeadline = 0;
|
---|
2430 | UINT32 cFramesPending = 0;
|
---|
2431 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->GetCurrentPadding(&cFramesPending);
|
---|
2432 | if (SUCCEEDED(hrc))
|
---|
2433 | msDrainDeadline = msNow
|
---|
2434 | + PDMAudioPropsFramesToMilli(&pStreamWas->Cfg.Props,
|
---|
2435 | RT_MIN(cFramesPending,
|
---|
2436 | pStreamWas->Cfg.Backend.cFramesBufferSize * 2))
|
---|
2437 | + 1 /*fudge*/;
|
---|
2438 | else
|
---|
2439 | {
|
---|
2440 | msDrainDeadline = msNow;
|
---|
2441 | LogRelMax(64, ("WasAPI: GetCurrentPadding fail on '%s' when starting draining: %Rhrc\n",
|
---|
2442 | pStreamWas->Cfg.szName, hrc));
|
---|
2443 | }
|
---|
2444 | pStreamWas->msDrainDeadline = msDrainDeadline;
|
---|
2445 | pStreamWas->fDraining = true;
|
---|
2446 | }
|
---|
2447 | else
|
---|
2448 | LogFlowFunc(("Already draining '%s' ...\n", pStreamWas->Cfg.szName));
|
---|
2449 | }
|
---|
2450 | else
|
---|
2451 | {
|
---|
2452 | LogFlowFunc(("Drain requested for '%s', but not started playback...\n", pStreamWas->Cfg.szName));
|
---|
2453 | AssertStmt(!pStreamWas->fDraining, pStreamWas->fDraining = false);
|
---|
2454 | }
|
---|
2455 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2456 |
|
---|
2457 | LogFlowFunc(("returns %Rrc {%s}\n", rc, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2458 | return rc;
|
---|
2459 | }
|
---|
2460 |
|
---|
2461 |
|
---|
2462 | /**
|
---|
2463 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetState}
|
---|
2464 | */
|
---|
2465 | static DECLCALLBACK(PDMHOSTAUDIOSTREAMSTATE) drvHostAudioWasHA_StreamGetState(PPDMIHOSTAUDIO pInterface,
|
---|
2466 | PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2467 | {
|
---|
2468 | RT_NOREF(pInterface);
|
---|
2469 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2470 | AssertPtrReturn(pStreamWas, PDMHOSTAUDIOSTREAMSTATE_INVALID);
|
---|
2471 |
|
---|
2472 | PDMHOSTAUDIOSTREAMSTATE enmState;
|
---|
2473 | AssertPtr(pStreamWas->pDevCfg);
|
---|
2474 | if (pStreamWas->pDevCfg /*paranoia*/)
|
---|
2475 | {
|
---|
2476 | if (RT_SUCCESS(pStreamWas->pDevCfg->rcSetup))
|
---|
2477 | {
|
---|
2478 | if (!pStreamWas->fDraining)
|
---|
2479 | enmState = PDMHOSTAUDIOSTREAMSTATE_OKAY;
|
---|
2480 | else
|
---|
2481 | {
|
---|
2482 | Assert(pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT);
|
---|
2483 | enmState = PDMHOSTAUDIOSTREAMSTATE_DRAINING;
|
---|
2484 | }
|
---|
2485 | }
|
---|
2486 | else if ( pStreamWas->pDevCfg->rcSetup == VERR_AUDIO_STREAM_INIT_IN_PROGRESS
|
---|
2487 | || pStreamWas->fSwitchingDevice )
|
---|
2488 | enmState = PDMHOSTAUDIOSTREAMSTATE_INITIALIZING;
|
---|
2489 | else
|
---|
2490 | enmState = PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING;
|
---|
2491 | }
|
---|
2492 | else if (pStreamWas->fSwitchingDevice)
|
---|
2493 | enmState = PDMHOSTAUDIOSTREAMSTATE_INITIALIZING;
|
---|
2494 | else
|
---|
2495 | enmState = PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING;
|
---|
2496 |
|
---|
2497 | LogFlowFunc(("returns %d for '%s' {%s}\n", enmState, pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2498 | return enmState;
|
---|
2499 | }
|
---|
2500 |
|
---|
2501 |
|
---|
2502 | /**
|
---|
2503 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetPending}
|
---|
2504 | */
|
---|
2505 | static DECLCALLBACK(uint32_t) drvHostAudioWasHA_StreamGetPending(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2506 | {
|
---|
2507 | RT_NOREF(pInterface);
|
---|
2508 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2509 | AssertPtrReturn(pStreamWas, 0);
|
---|
2510 | LogFlowFunc(("Stream '%s' {%s}\n", pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2511 | AssertReturn(pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT, 0);
|
---|
2512 |
|
---|
2513 | uint32_t cbPending = 0;
|
---|
2514 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2515 |
|
---|
2516 | if ( pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT
|
---|
2517 | && pStreamWas->pDevCfg->pIAudioClient /* paranoia */)
|
---|
2518 | {
|
---|
2519 | if (pStreamWas->fStarted)
|
---|
2520 | {
|
---|
2521 | UINT32 cFramesPending = 0;
|
---|
2522 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->GetCurrentPadding(&cFramesPending);
|
---|
2523 | if (SUCCEEDED(hrc))
|
---|
2524 | {
|
---|
2525 | AssertMsg(cFramesPending <= pStreamWas->Cfg.Backend.cFramesBufferSize,
|
---|
2526 | ("cFramesPending=%#x cFramesBufferSize=%#x\n",
|
---|
2527 | cFramesPending, pStreamWas->Cfg.Backend.cFramesBufferSize));
|
---|
2528 | cbPending = PDMAudioPropsFramesToBytes(&pStreamWas->Cfg.Props, RT_MIN(cFramesPending, VBOX_WASAPI_MAX_PADDING));
|
---|
2529 | }
|
---|
2530 | else
|
---|
2531 | LogRelMax(64, ("WasAPI: GetCurrentPadding failed on '%s': %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2532 | }
|
---|
2533 | }
|
---|
2534 |
|
---|
2535 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2536 |
|
---|
2537 | LogFlowFunc(("returns %#x (%u) {%s}\n", cbPending, cbPending, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2538 | return cbPending;
|
---|
2539 | }
|
---|
2540 |
|
---|
2541 |
|
---|
2542 | /**
|
---|
2543 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
|
---|
2544 | */
|
---|
2545 | static DECLCALLBACK(uint32_t) drvHostAudioWasHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2546 | {
|
---|
2547 | RT_NOREF(pInterface);
|
---|
2548 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2549 | AssertPtrReturn(pStreamWas, 0);
|
---|
2550 | LogFlowFunc(("Stream '%s' {%s}\n", pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2551 | Assert(pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT);
|
---|
2552 |
|
---|
2553 | uint32_t cbWritable = 0;
|
---|
2554 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2555 |
|
---|
2556 | if ( pStreamWas->Cfg.enmDir == PDMAUDIODIR_OUT
|
---|
2557 | && pStreamWas->pDevCfg->pIAudioClient /* paranoia */)
|
---|
2558 | {
|
---|
2559 | UINT32 cFramesPending = 0;
|
---|
2560 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->GetCurrentPadding(&cFramesPending);
|
---|
2561 | if (SUCCEEDED(hrc))
|
---|
2562 | {
|
---|
2563 | if (cFramesPending < pStreamWas->Cfg.Backend.cFramesBufferSize)
|
---|
2564 | cbWritable = PDMAudioPropsFramesToBytes(&pStreamWas->Cfg.Props,
|
---|
2565 | pStreamWas->Cfg.Backend.cFramesBufferSize - cFramesPending);
|
---|
2566 | else if (cFramesPending > pStreamWas->Cfg.Backend.cFramesBufferSize)
|
---|
2567 | {
|
---|
2568 | LogRelMax(64, ("WasAPI: Warning! GetCurrentPadding('%s') return too high: cFramesPending=%#x > cFramesBufferSize=%#x\n",
|
---|
2569 | pStreamWas->Cfg.szName, cFramesPending, pStreamWas->Cfg.Backend.cFramesBufferSize));
|
---|
2570 | AssertMsgFailed(("cFramesPending=%#x > cFramesBufferSize=%#x\n",
|
---|
2571 | cFramesPending, pStreamWas->Cfg.Backend.cFramesBufferSize));
|
---|
2572 | }
|
---|
2573 | }
|
---|
2574 | else
|
---|
2575 | LogRelMax(64, ("WasAPI: GetCurrentPadding failed on '%s': %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2576 | }
|
---|
2577 |
|
---|
2578 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2579 |
|
---|
2580 | LogFlowFunc(("returns %#x (%u) {%s}\n", cbWritable, cbWritable, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2581 | return cbWritable;
|
---|
2582 | }
|
---|
2583 |
|
---|
2584 |
|
---|
2585 | /**
|
---|
2586 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
|
---|
2587 | */
|
---|
2588 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2589 | const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
|
---|
2590 | {
|
---|
2591 | PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
2592 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2593 | AssertPtrReturn(pStreamWas, VERR_INVALID_POINTER);
|
---|
2594 | AssertPtrReturn(pcbWritten, VERR_INVALID_POINTER);
|
---|
2595 | if (cbBuf)
|
---|
2596 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
2597 | Assert(PDMAudioPropsIsSizeAligned(&pStreamWas->Cfg.Props, cbBuf));
|
---|
2598 |
|
---|
2599 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2600 | if (pStreamWas->fEnabled)
|
---|
2601 | { /* likely */ }
|
---|
2602 | else
|
---|
2603 | {
|
---|
2604 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2605 | *pcbWritten = 0;
|
---|
2606 | LogFunc(("Skipping %#x byte write to disabled stream {%s}\n", cbBuf, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2607 | return VINF_SUCCESS;
|
---|
2608 | }
|
---|
2609 | Log4Func(("cbBuf=%#x stream '%s' {%s}\n", cbBuf, pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2610 |
|
---|
2611 | /*
|
---|
2612 | * Transfer loop.
|
---|
2613 | */
|
---|
2614 | int rc = VINF_SUCCESS;
|
---|
2615 | uint32_t cReInits = 0;
|
---|
2616 | uint32_t cbWritten = 0;
|
---|
2617 | while (cbBuf > 0)
|
---|
2618 | {
|
---|
2619 | AssertBreakStmt(pStreamWas->pDevCfg && pStreamWas->pDevCfg->pIAudioRenderClient && pStreamWas->pDevCfg->pIAudioClient,
|
---|
2620 | rc = VERR_AUDIO_STREAM_NOT_READY);
|
---|
2621 |
|
---|
2622 | /*
|
---|
2623 | * Figure out how much we can possibly write.
|
---|
2624 | */
|
---|
2625 | UINT32 cFramesPending = 0;
|
---|
2626 | uint32_t cbWritable = 0;
|
---|
2627 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->GetCurrentPadding(&cFramesPending);
|
---|
2628 | if (SUCCEEDED(hrc))
|
---|
2629 | cbWritable = PDMAudioPropsFramesToBytes(&pStreamWas->Cfg.Props,
|
---|
2630 | pStreamWas->Cfg.Backend.cFramesBufferSize
|
---|
2631 | - RT_MIN(cFramesPending, pStreamWas->Cfg.Backend.cFramesBufferSize));
|
---|
2632 | else
|
---|
2633 | {
|
---|
2634 | LogRelMax(64, ("WasAPI: GetCurrentPadding(%s) failed during playback: %Rhrc (@%#RX64)\n",
|
---|
2635 | pStreamWas->Cfg.szName, hrc, pStreamWas->offInternal));
|
---|
2636 | /** @todo reinit on AUDCLNT_E_DEVICEINVALIDATED? */
|
---|
2637 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2638 | break;
|
---|
2639 | }
|
---|
2640 | if (cbWritable <= PDMAudioPropsFrameSize(&pStreamWas->Cfg.Props))
|
---|
2641 | break;
|
---|
2642 |
|
---|
2643 | uint32_t const cbToWrite = PDMAudioPropsFloorBytesToFrame(&pStreamWas->Cfg.Props, RT_MIN(cbWritable, cbBuf));
|
---|
2644 | uint32_t const cFramesToWrite = PDMAudioPropsBytesToFrames(&pStreamWas->Cfg.Props, cbToWrite);
|
---|
2645 | Assert(PDMAudioPropsFramesToBytes(&pStreamWas->Cfg.Props, cFramesToWrite) == cbToWrite);
|
---|
2646 | Log3Func(("@%#RX64: cFramesPending=%#x -> cbWritable=%#x cbToWrite=%#x cFramesToWrite=%#x {%s}\n",
|
---|
2647 | pStreamWas->offInternal, cFramesPending, cbWritable, cbToWrite, cFramesToWrite,
|
---|
2648 | drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2649 |
|
---|
2650 | /*
|
---|
2651 | * Get the buffer, copy the data into it, and relase it back to the WAS machinery.
|
---|
2652 | */
|
---|
2653 | BYTE *pbData = NULL;
|
---|
2654 | hrc = pStreamWas->pDevCfg->pIAudioRenderClient->GetBuffer(cFramesToWrite, &pbData);
|
---|
2655 | if (SUCCEEDED(hrc))
|
---|
2656 | {
|
---|
2657 | memcpy(pbData, pvBuf, cbToWrite);
|
---|
2658 | hrc = pStreamWas->pDevCfg->pIAudioRenderClient->ReleaseBuffer(cFramesToWrite, 0 /*fFlags*/);
|
---|
2659 | if (SUCCEEDED(hrc))
|
---|
2660 | {
|
---|
2661 | /*
|
---|
2662 | * Before we advance the buffer position (so we can resubmit it
|
---|
2663 | * after re-init), make sure we've successfully started stream.
|
---|
2664 | */
|
---|
2665 | if (pStreamWas->fStarted)
|
---|
2666 | { }
|
---|
2667 | else
|
---|
2668 | {
|
---|
2669 | rc = drvHostAudioWasStreamStartWorker(pThis, pStreamWas, "play");
|
---|
2670 | if (rc == VINF_SUCCESS)
|
---|
2671 | { /* likely */ }
|
---|
2672 | else if (RT_SUCCESS(rc) && ++cReInits < 5)
|
---|
2673 | continue; /* re-submit buffer after re-init */
|
---|
2674 | else
|
---|
2675 | break;
|
---|
2676 | }
|
---|
2677 |
|
---|
2678 | /* advance. */
|
---|
2679 | pvBuf = (uint8_t *)pvBuf + cbToWrite;
|
---|
2680 | cbBuf -= cbToWrite;
|
---|
2681 | cbWritten += cbToWrite;
|
---|
2682 | pStreamWas->offInternal += cbToWrite;
|
---|
2683 | }
|
---|
2684 | else
|
---|
2685 | {
|
---|
2686 | LogRelMax(64, ("WasAPI: ReleaseBuffer(%#x) failed on '%s' during playback: %Rhrc (@%#RX64)\n",
|
---|
2687 | cFramesToWrite, pStreamWas->Cfg.szName, hrc, pStreamWas->offInternal));
|
---|
2688 | /** @todo reinit on AUDCLNT_E_DEVICEINVALIDATED? */
|
---|
2689 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2690 | break;
|
---|
2691 | }
|
---|
2692 | }
|
---|
2693 | else
|
---|
2694 | {
|
---|
2695 | LogRelMax(64, ("WasAPI: GetBuffer(%#x) failed on '%s' during playback: %Rhrc (@%#RX64)\n",
|
---|
2696 | cFramesToWrite, pStreamWas->Cfg.szName, hrc, pStreamWas->offInternal));
|
---|
2697 | /** @todo reinit on AUDCLNT_E_DEVICEINVALIDATED? */
|
---|
2698 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2699 | break;
|
---|
2700 | }
|
---|
2701 | }
|
---|
2702 |
|
---|
2703 | /*
|
---|
2704 | * Do draining deadline processing.
|
---|
2705 | */
|
---|
2706 | uint64_t const msNow = RTTimeMilliTS();
|
---|
2707 | if ( !pStreamWas->fDraining
|
---|
2708 | || msNow < pStreamWas->msDrainDeadline)
|
---|
2709 | { /* likely */ }
|
---|
2710 | else
|
---|
2711 | {
|
---|
2712 | LogRel2(("WasAPI: Stopping draining of '%s' {%s} ...\n", pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2713 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->Stop();
|
---|
2714 | if (FAILED(hrc))
|
---|
2715 | LogRelMax(64, ("WasAPI: Failed to stop draining stream '%s': %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2716 | pStreamWas->fDraining = false;
|
---|
2717 | pStreamWas->fStarted = false;
|
---|
2718 | pStreamWas->fEnabled = false;
|
---|
2719 | }
|
---|
2720 |
|
---|
2721 | /*
|
---|
2722 | * Done.
|
---|
2723 | */
|
---|
2724 | uint64_t const msPrev = pStreamWas->msLastTransfer; RT_NOREF(msPrev);
|
---|
2725 | if (cbWritten)
|
---|
2726 | pStreamWas->msLastTransfer = msNow;
|
---|
2727 |
|
---|
2728 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2729 |
|
---|
2730 | *pcbWritten = cbWritten;
|
---|
2731 | if (RT_SUCCESS(rc) || !cbWritten)
|
---|
2732 | { }
|
---|
2733 | else
|
---|
2734 | {
|
---|
2735 | LogFlowFunc(("Suppressing %Rrc to report %#x bytes written\n", rc, cbWritten));
|
---|
2736 | rc = VINF_SUCCESS;
|
---|
2737 | }
|
---|
2738 | LogFlowFunc(("@%#RX64: rc=%Rrc cbWritten=%RU32 cMsDelta=%RU64 (%RU64 -> %RU64) {%s}\n", pStreamWas->offInternal, rc, cbWritten,
|
---|
2739 | msPrev ? msNow - msPrev : 0, msPrev, pStreamWas->msLastTransfer, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2740 | return rc;
|
---|
2741 | }
|
---|
2742 |
|
---|
2743 |
|
---|
2744 | /**
|
---|
2745 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
|
---|
2746 | */
|
---|
2747 | static DECLCALLBACK(uint32_t) drvHostAudioWasHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2748 | {
|
---|
2749 | RT_NOREF(pInterface);
|
---|
2750 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2751 | AssertPtrReturn(pStreamWas, 0);
|
---|
2752 | Assert(pStreamWas->Cfg.enmDir == PDMAUDIODIR_IN);
|
---|
2753 |
|
---|
2754 | uint32_t cbReadable = 0;
|
---|
2755 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2756 |
|
---|
2757 | if (pStreamWas->pDevCfg->pIAudioCaptureClient /* paranoia */)
|
---|
2758 | {
|
---|
2759 | UINT32 cFramesPending = 0;
|
---|
2760 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioClient->GetCurrentPadding(&cFramesPending);
|
---|
2761 | if (SUCCEEDED(hrc))
|
---|
2762 | {
|
---|
2763 | /* An unreleased buffer is included in the pending frame count, so subtract
|
---|
2764 | whatever we've got hanging around since the previous pfnStreamCapture call. */
|
---|
2765 | AssertMsgStmt(cFramesPending >= pStreamWas->cFramesCaptureToRelease,
|
---|
2766 | ("%#x vs %#x\n", cFramesPending, pStreamWas->cFramesCaptureToRelease),
|
---|
2767 | cFramesPending = pStreamWas->cFramesCaptureToRelease);
|
---|
2768 | cFramesPending -= pStreamWas->cFramesCaptureToRelease;
|
---|
2769 |
|
---|
2770 | /* Add what we've got left in said buffer. */
|
---|
2771 | uint32_t cFramesCurPacket = PDMAudioPropsBytesToFrames(&pStreamWas->Cfg.Props, pStreamWas->cbCapture);
|
---|
2772 | cFramesPending += cFramesCurPacket;
|
---|
2773 |
|
---|
2774 | /* Paranoia: Make sure we don't exceed the buffer size. */
|
---|
2775 | AssertMsgStmt(cFramesPending <= pStreamWas->Cfg.Backend.cFramesBufferSize,
|
---|
2776 | ("cFramesPending=%#x cFramesCaptureToRelease=%#x cFramesCurPacket=%#x cFramesBufferSize=%#x\n",
|
---|
2777 | cFramesPending, pStreamWas->cFramesCaptureToRelease, cFramesCurPacket,
|
---|
2778 | pStreamWas->Cfg.Backend.cFramesBufferSize),
|
---|
2779 | cFramesPending = pStreamWas->Cfg.Backend.cFramesBufferSize);
|
---|
2780 |
|
---|
2781 | cbReadable = PDMAudioPropsFramesToBytes(&pStreamWas->Cfg.Props, cFramesPending);
|
---|
2782 | }
|
---|
2783 | else
|
---|
2784 | LogRelMax(64, ("WasAPI: GetCurrentPadding failed on '%s': %Rhrc\n", pStreamWas->Cfg.szName, hrc));
|
---|
2785 | }
|
---|
2786 |
|
---|
2787 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2788 |
|
---|
2789 | LogFlowFunc(("returns %#x (%u) {%s}\n", cbReadable, cbReadable, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2790 | return cbReadable;
|
---|
2791 | }
|
---|
2792 |
|
---|
2793 |
|
---|
2794 | /**
|
---|
2795 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
|
---|
2796 | */
|
---|
2797 | static DECLCALLBACK(int) drvHostAudioWasHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2798 | void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
2799 | {
|
---|
2800 | RT_NOREF(pInterface); //PDRVHOSTAUDIOWAS pThis = RT_FROM_MEMBER(pInterface, DRVHOSTAUDIOWAS, IHostAudio);
|
---|
2801 | PDRVHOSTAUDIOWASSTREAM pStreamWas = (PDRVHOSTAUDIOWASSTREAM)pStream;
|
---|
2802 | AssertPtrReturn(pStreamWas, 0);
|
---|
2803 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
2804 | AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
2805 | AssertPtrReturn(pcbRead, VERR_INVALID_POINTER);
|
---|
2806 | Assert(PDMAudioPropsIsSizeAligned(&pStreamWas->Cfg.Props, cbBuf));
|
---|
2807 |
|
---|
2808 | RTCritSectEnter(&pStreamWas->CritSect);
|
---|
2809 | if (pStreamWas->fEnabled)
|
---|
2810 | { /* likely */ }
|
---|
2811 | else
|
---|
2812 | {
|
---|
2813 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2814 | *pcbRead = 0;
|
---|
2815 | LogFunc(("Skipping %#x byte read from disabled stream {%s}\n", cbBuf, drvHostWasStreamStatusString(pStreamWas)));
|
---|
2816 | return VINF_SUCCESS;
|
---|
2817 | }
|
---|
2818 | Log4Func(("cbBuf=%#x stream '%s' {%s}\n", cbBuf, pStreamWas->Cfg.szName, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2819 |
|
---|
2820 |
|
---|
2821 | /*
|
---|
2822 | * Transfer loop.
|
---|
2823 | */
|
---|
2824 | int rc = VINF_SUCCESS;
|
---|
2825 | uint32_t cbRead = 0;
|
---|
2826 | uint32_t const cbFrame = PDMAudioPropsFrameSize(&pStreamWas->Cfg.Props);
|
---|
2827 | while (cbBuf >= cbFrame)
|
---|
2828 | {
|
---|
2829 | AssertBreakStmt(pStreamWas->pDevCfg->pIAudioCaptureClient && pStreamWas->pDevCfg->pIAudioClient, rc = VERR_AUDIO_STREAM_NOT_READY);
|
---|
2830 |
|
---|
2831 | /*
|
---|
2832 | * Anything pending from last call?
|
---|
2833 | * (This is rather similar to the Pulse interface.)
|
---|
2834 | */
|
---|
2835 | if (pStreamWas->cFramesCaptureToRelease)
|
---|
2836 | {
|
---|
2837 | uint32_t const cbToCopy = RT_MIN(pStreamWas->cbCapture, cbBuf);
|
---|
2838 | memcpy(pvBuf, pStreamWas->pbCapture, cbToCopy);
|
---|
2839 | pvBuf = (uint8_t *)pvBuf + cbToCopy;
|
---|
2840 | cbBuf -= cbToCopy;
|
---|
2841 | cbRead += cbToCopy;
|
---|
2842 | pStreamWas->offInternal += cbToCopy;
|
---|
2843 | pStreamWas->pbCapture += cbToCopy;
|
---|
2844 | pStreamWas->cbCapture -= cbToCopy;
|
---|
2845 | if (!pStreamWas->cbCapture)
|
---|
2846 | {
|
---|
2847 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioCaptureClient->ReleaseBuffer(pStreamWas->cFramesCaptureToRelease);
|
---|
2848 | Log4Func(("@%#RX64: Releasing capture buffer (%#x frames): %Rhrc\n",
|
---|
2849 | pStreamWas->offInternal, pStreamWas->cFramesCaptureToRelease, hrc));
|
---|
2850 | if (SUCCEEDED(hrc))
|
---|
2851 | {
|
---|
2852 | pStreamWas->cFramesCaptureToRelease = 0;
|
---|
2853 | pStreamWas->pbCapture = NULL;
|
---|
2854 | }
|
---|
2855 | else
|
---|
2856 | {
|
---|
2857 | LogRelMax(64, ("WasAPI: ReleaseBuffer(%s) failed during capture: %Rhrc (@%#RX64)\n",
|
---|
2858 | pStreamWas->Cfg.szName, hrc, pStreamWas->offInternal));
|
---|
2859 | /** @todo reinit on AUDCLNT_E_DEVICEINVALIDATED? */
|
---|
2860 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2861 | break;
|
---|
2862 | }
|
---|
2863 | }
|
---|
2864 | if (cbBuf < cbFrame)
|
---|
2865 | break;
|
---|
2866 | }
|
---|
2867 |
|
---|
2868 | /*
|
---|
2869 | * Figure out if there is any data available to be read now. (Docs hint that we can not
|
---|
2870 | * skip this and go straight for GetBuffer or we risk getting unwritten buffer space back).
|
---|
2871 | */
|
---|
2872 | UINT32 cFramesCaptured = 0;
|
---|
2873 | HRESULT hrc = pStreamWas->pDevCfg->pIAudioCaptureClient->GetNextPacketSize(&cFramesCaptured);
|
---|
2874 | if (SUCCEEDED(hrc))
|
---|
2875 | {
|
---|
2876 | if (!cFramesCaptured)
|
---|
2877 | break;
|
---|
2878 | }
|
---|
2879 | else
|
---|
2880 | {
|
---|
2881 | LogRelMax(64, ("WasAPI: GetNextPacketSize(%s) failed during capture: %Rhrc (@%#RX64)\n",
|
---|
2882 | pStreamWas->Cfg.szName, hrc, pStreamWas->offInternal));
|
---|
2883 | /** @todo reinit on AUDCLNT_E_DEVICEINVALIDATED? */
|
---|
2884 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2885 | break;
|
---|
2886 | }
|
---|
2887 |
|
---|
2888 | /*
|
---|
2889 | * Get the buffer.
|
---|
2890 | */
|
---|
2891 | cFramesCaptured = 0;
|
---|
2892 | UINT64 uQpsNtTicks = 0;
|
---|
2893 | UINT64 offDevice = 0;
|
---|
2894 | DWORD fBufFlags = 0;
|
---|
2895 | BYTE *pbData = NULL;
|
---|
2896 | hrc = pStreamWas->pDevCfg->pIAudioCaptureClient->GetBuffer(&pbData, &cFramesCaptured, &fBufFlags, &offDevice, &uQpsNtTicks);
|
---|
2897 | Log4Func(("@%#RX64: GetBuffer -> %Rhrc pbData=%p cFramesCaptured=%#x fBufFlags=%#x offDevice=%#RX64 uQpcNtTicks=%#RX64\n",
|
---|
2898 | pStreamWas->offInternal, hrc, pbData, cFramesCaptured, fBufFlags, offDevice, uQpsNtTicks));
|
---|
2899 | if (SUCCEEDED(hrc))
|
---|
2900 | {
|
---|
2901 | Assert(cFramesCaptured < VBOX_WASAPI_MAX_PADDING);
|
---|
2902 | pStreamWas->pbCapture = pbData;
|
---|
2903 | pStreamWas->cFramesCaptureToRelease = cFramesCaptured;
|
---|
2904 | pStreamWas->cbCapture = PDMAudioPropsFramesToBytes(&pStreamWas->Cfg.Props, cFramesCaptured);
|
---|
2905 | /* Just loop and re-use the copying code above. Can optimize later. */
|
---|
2906 | }
|
---|
2907 | else
|
---|
2908 | {
|
---|
2909 | LogRelMax(64, ("WasAPI: GetBuffer() failed on '%s' during capture: %Rhrc (@%#RX64)\n",
|
---|
2910 | pStreamWas->Cfg.szName, hrc, pStreamWas->offInternal));
|
---|
2911 | /** @todo reinit on AUDCLNT_E_DEVICEINVALIDATED? */
|
---|
2912 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2913 | break;
|
---|
2914 | }
|
---|
2915 | }
|
---|
2916 |
|
---|
2917 | /*
|
---|
2918 | * Done.
|
---|
2919 | */
|
---|
2920 | uint64_t const msPrev = pStreamWas->msLastTransfer; RT_NOREF(msPrev);
|
---|
2921 | uint64_t const msNow = RTTimeMilliTS();
|
---|
2922 | if (cbRead)
|
---|
2923 | pStreamWas->msLastTransfer = msNow;
|
---|
2924 |
|
---|
2925 | RTCritSectLeave(&pStreamWas->CritSect);
|
---|
2926 |
|
---|
2927 | *pcbRead = cbRead;
|
---|
2928 | if (RT_SUCCESS(rc) || !cbRead)
|
---|
2929 | { }
|
---|
2930 | else
|
---|
2931 | {
|
---|
2932 | LogFlowFunc(("Suppressing %Rrc to report %#x bytes read\n", rc, cbRead));
|
---|
2933 | rc = VINF_SUCCESS;
|
---|
2934 | }
|
---|
2935 | LogFlowFunc(("@%#RX64: rc=%Rrc cbRead=%#RX32 cMsDelta=%RU64 (%RU64 -> %RU64) {%s}\n", pStreamWas->offInternal, rc, cbRead,
|
---|
2936 | msPrev ? msNow - msPrev : 0, msPrev, pStreamWas->msLastTransfer, drvHostWasStreamStatusString(pStreamWas) ));
|
---|
2937 | return rc;
|
---|
2938 | }
|
---|
2939 |
|
---|
2940 |
|
---|
2941 | /*********************************************************************************************************************************
|
---|
2942 | * PDMDRVINS::IBase Interface *
|
---|
2943 | *********************************************************************************************************************************/
|
---|
2944 |
|
---|
2945 | /**
|
---|
2946 | * @callback_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
2947 | */
|
---|
2948 | static DECLCALLBACK(void *) drvHostAudioWasQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
2949 | {
|
---|
2950 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
2951 | PDRVHOSTAUDIOWAS pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTAUDIOWAS);
|
---|
2952 |
|
---|
2953 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
2954 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
|
---|
2955 | return NULL;
|
---|
2956 | }
|
---|
2957 |
|
---|
2958 |
|
---|
2959 | /*********************************************************************************************************************************
|
---|
2960 | * PDMDRVREG Interface *
|
---|
2961 | *********************************************************************************************************************************/
|
---|
2962 |
|
---|
2963 | /**
|
---|
2964 | * @callback_method_impl{FNPDMDRVDESTRUCT, pfnDestruct}
|
---|
2965 | */
|
---|
2966 | static DECLCALLBACK(void) drvHostAudioWasPowerOff(PPDMDRVINS pDrvIns)
|
---|
2967 | {
|
---|
2968 | PDRVHOSTAUDIOWAS pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTAUDIOWAS);
|
---|
2969 |
|
---|
2970 | /*
|
---|
2971 | * Start purging the cache asynchronously before we get to destruct.
|
---|
2972 | * This might speed up VM shutdown a tiny fraction and also stress
|
---|
2973 | * the shutting down of the thread pool a little.
|
---|
2974 | */
|
---|
2975 | #if 0
|
---|
2976 | if (pThis->hWorkerThread != NIL_RTTHREAD)
|
---|
2977 | {
|
---|
2978 | BOOL fRc = PostThreadMessageW(pThis->idWorkerThread, WM_DRVHOSTAUDIOWAS_PURGE_CACHE, pThis->uWorkerThreadFixedParam, 0);
|
---|
2979 | LogFlowFunc(("Posted WM_DRVHOSTAUDIOWAS_PURGE_CACHE: %d\n", fRc));
|
---|
2980 | Assert(fRc); RT_NOREF(fRc);
|
---|
2981 | }
|
---|
2982 | #else
|
---|
2983 | if (!RTListIsEmpty(&pThis->CacheHead) && pThis->pIHostAudioPort)
|
---|
2984 | {
|
---|
2985 | int rc = RTSemEventMultiCreate(&pThis->hEvtCachePurge);
|
---|
2986 | if (RT_SUCCESS(rc))
|
---|
2987 | {
|
---|
2988 | rc = pThis->pIHostAudioPort->pfnDoOnWorkerThread(pThis->pIHostAudioPort, NULL/*pStream*/,
|
---|
2989 | DRVHOSTAUDIOWAS_DO_PURGE_CACHE, NULL /*pvUser*/);
|
---|
2990 | if (RT_FAILURE(rc))
|
---|
2991 | {
|
---|
2992 | LogFunc(("pfnDoOnWorkerThread/DRVHOSTAUDIOWAS_DO_PURGE_CACHE failed: %Rrc\n", rc));
|
---|
2993 | RTSemEventMultiDestroy(pThis->hEvtCachePurge);
|
---|
2994 | pThis->hEvtCachePurge = NIL_RTSEMEVENTMULTI;
|
---|
2995 | }
|
---|
2996 | }
|
---|
2997 | }
|
---|
2998 | #endif
|
---|
2999 |
|
---|
3000 | /*
|
---|
3001 | * Deregister the notification client to reduce the risk of notifications
|
---|
3002 | * comming in while we're being detatched or the VM is being destroyed.
|
---|
3003 | */
|
---|
3004 | if (pThis->pNotifyClient)
|
---|
3005 | {
|
---|
3006 | pThis->pNotifyClient->notifyDriverDestroyed();
|
---|
3007 | pThis->pIEnumerator->UnregisterEndpointNotificationCallback(pThis->pNotifyClient);
|
---|
3008 | pThis->pNotifyClient->Release();
|
---|
3009 | pThis->pNotifyClient = NULL;
|
---|
3010 | }
|
---|
3011 | }
|
---|
3012 |
|
---|
3013 |
|
---|
3014 | /**
|
---|
3015 | * @callback_method_impl{FNPDMDRVDESTRUCT, pfnDestruct}
|
---|
3016 | */
|
---|
3017 | static DECLCALLBACK(void) drvHostAudioWasDestruct(PPDMDRVINS pDrvIns)
|
---|
3018 | {
|
---|
3019 | PDRVHOSTAUDIOWAS pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTAUDIOWAS);
|
---|
3020 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
3021 | LogFlowFuncEnter();
|
---|
3022 |
|
---|
3023 | /*
|
---|
3024 | * Release the notification client first.
|
---|
3025 | */
|
---|
3026 | if (pThis->pNotifyClient)
|
---|
3027 | {
|
---|
3028 | pThis->pNotifyClient->notifyDriverDestroyed();
|
---|
3029 | pThis->pIEnumerator->UnregisterEndpointNotificationCallback(pThis->pNotifyClient);
|
---|
3030 | pThis->pNotifyClient->Release();
|
---|
3031 | pThis->pNotifyClient = NULL;
|
---|
3032 | }
|
---|
3033 |
|
---|
3034 | #if 0
|
---|
3035 | if (pThis->hWorkerThread != NIL_RTTHREAD)
|
---|
3036 | {
|
---|
3037 | BOOL fRc = PostThreadMessageW(pThis->idWorkerThread, WM_QUIT, 0, 0);
|
---|
3038 | Assert(fRc); RT_NOREF(fRc);
|
---|
3039 |
|
---|
3040 | int rc = RTThreadWait(pThis->hWorkerThread, RT_MS_15SEC, NULL);
|
---|
3041 | AssertRC(rc);
|
---|
3042 | }
|
---|
3043 | #endif
|
---|
3044 |
|
---|
3045 | if (RTCritSectIsInitialized(&pThis->CritSectCache))
|
---|
3046 | {
|
---|
3047 | drvHostAudioWasCachePurge(pThis, false /*fOnWorker*/);
|
---|
3048 | if (pThis->hEvtCachePurge != NIL_RTSEMEVENTMULTI)
|
---|
3049 | RTSemEventMultiWait(pThis->hEvtCachePurge, RT_MS_30SEC);
|
---|
3050 | RTCritSectDelete(&pThis->CritSectCache);
|
---|
3051 | }
|
---|
3052 |
|
---|
3053 | if (pThis->hEvtCachePurge != NIL_RTSEMEVENTMULTI)
|
---|
3054 | {
|
---|
3055 | RTSemEventMultiDestroy(pThis->hEvtCachePurge);
|
---|
3056 | pThis->hEvtCachePurge = NIL_RTSEMEVENTMULTI;
|
---|
3057 | }
|
---|
3058 |
|
---|
3059 | if (pThis->pIEnumerator)
|
---|
3060 | {
|
---|
3061 | uint32_t cRefs = pThis->pIEnumerator->Release(); RT_NOREF(cRefs);
|
---|
3062 | LogFlowFunc(("cRefs=%d\n", cRefs));
|
---|
3063 | }
|
---|
3064 |
|
---|
3065 | if (pThis->pIDeviceOutput)
|
---|
3066 | {
|
---|
3067 | pThis->pIDeviceOutput->Release();
|
---|
3068 | pThis->pIDeviceOutput = NULL;
|
---|
3069 | }
|
---|
3070 |
|
---|
3071 | if (pThis->pIDeviceInput)
|
---|
3072 | {
|
---|
3073 | pThis->pIDeviceInput->Release();
|
---|
3074 | pThis->pIDeviceInput = NULL;
|
---|
3075 | }
|
---|
3076 |
|
---|
3077 |
|
---|
3078 | if (RTCritSectRwIsInitialized(&pThis->CritSectStreamList))
|
---|
3079 | RTCritSectRwDelete(&pThis->CritSectStreamList);
|
---|
3080 |
|
---|
3081 | LogFlowFuncLeave();
|
---|
3082 | }
|
---|
3083 |
|
---|
3084 |
|
---|
3085 | /**
|
---|
3086 | * @callback_method_impl{FNPDMDRVCONSTRUCT, pfnConstruct}
|
---|
3087 | */
|
---|
3088 | static DECLCALLBACK(int) drvHostAudioWasConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
3089 | {
|
---|
3090 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
3091 | PDRVHOSTAUDIOWAS pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTAUDIOWAS);
|
---|
3092 | PCPDMDRVHLPR3 pHlp = pDrvIns->pHlpR3;
|
---|
3093 | RT_NOREF(fFlags, pCfg);
|
---|
3094 |
|
---|
3095 | /*
|
---|
3096 | * Init basic data members and interfaces.
|
---|
3097 | */
|
---|
3098 | pThis->pDrvIns = pDrvIns;
|
---|
3099 | pThis->hEvtCachePurge = NIL_RTSEMEVENTMULTI;
|
---|
3100 | #if 0
|
---|
3101 | pThis->hWorkerThread = NIL_RTTHREAD;
|
---|
3102 | pThis->idWorkerThread = 0;
|
---|
3103 | #endif
|
---|
3104 | RTListInit(&pThis->StreamHead);
|
---|
3105 | RTListInit(&pThis->CacheHead);
|
---|
3106 | /* IBase */
|
---|
3107 | pDrvIns->IBase.pfnQueryInterface = drvHostAudioWasQueryInterface;
|
---|
3108 | /* IHostAudio */
|
---|
3109 | pThis->IHostAudio.pfnGetConfig = drvHostAudioWasHA_GetConfig;
|
---|
3110 | pThis->IHostAudio.pfnGetDevices = drvHostAudioWasHA_GetDevices;
|
---|
3111 | pThis->IHostAudio.pfnSetDevice = drvHostAudioWasHA_SetDevice;
|
---|
3112 | pThis->IHostAudio.pfnGetStatus = drvHostAudioWasHA_GetStatus;
|
---|
3113 | pThis->IHostAudio.pfnDoOnWorkerThread = drvHostAudioWasHA_DoOnWorkerThread;
|
---|
3114 | pThis->IHostAudio.pfnStreamConfigHint = drvHostAudioWasHA_StreamConfigHint;
|
---|
3115 | pThis->IHostAudio.pfnStreamCreate = drvHostAudioWasHA_StreamCreate;
|
---|
3116 | pThis->IHostAudio.pfnStreamInitAsync = drvHostAudioWasHA_StreamInitAsync;
|
---|
3117 | pThis->IHostAudio.pfnStreamDestroy = drvHostAudioWasHA_StreamDestroy;
|
---|
3118 | pThis->IHostAudio.pfnStreamNotifyDeviceChanged = drvHostAudioWasHA_StreamNotifyDeviceChanged;
|
---|
3119 | pThis->IHostAudio.pfnStreamEnable = drvHostAudioWasHA_StreamEnable;
|
---|
3120 | pThis->IHostAudio.pfnStreamDisable = drvHostAudioWasHA_StreamDisable;
|
---|
3121 | pThis->IHostAudio.pfnStreamPause = drvHostAudioWasHA_StreamPause;
|
---|
3122 | pThis->IHostAudio.pfnStreamResume = drvHostAudioWasHA_StreamResume;
|
---|
3123 | pThis->IHostAudio.pfnStreamDrain = drvHostAudioWasHA_StreamDrain;
|
---|
3124 | pThis->IHostAudio.pfnStreamGetState = drvHostAudioWasHA_StreamGetState;
|
---|
3125 | pThis->IHostAudio.pfnStreamGetPending = drvHostAudioWasHA_StreamGetPending;
|
---|
3126 | pThis->IHostAudio.pfnStreamGetWritable = drvHostAudioWasHA_StreamGetWritable;
|
---|
3127 | pThis->IHostAudio.pfnStreamPlay = drvHostAudioWasHA_StreamPlay;
|
---|
3128 | pThis->IHostAudio.pfnStreamGetReadable = drvHostAudioWasHA_StreamGetReadable;
|
---|
3129 | pThis->IHostAudio.pfnStreamCapture = drvHostAudioWasHA_StreamCapture;
|
---|
3130 |
|
---|
3131 | /*
|
---|
3132 | * Validate and read the configuration.
|
---|
3133 | */
|
---|
3134 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns, "VmName|VmUuid|InputDeviceID|OutputDeviceID", "");
|
---|
3135 |
|
---|
3136 | char szTmp[1024];
|
---|
3137 | int rc = pHlp->pfnCFGMQueryStringDef(pCfg, "InputDeviceID", szTmp, sizeof(szTmp), "");
|
---|
3138 | AssertMsgRCReturn(rc, ("Confguration error: Failed to read \"InputDeviceID\" as string: rc=%Rrc\n", rc), rc);
|
---|
3139 | if (szTmp[0])
|
---|
3140 | {
|
---|
3141 | rc = RTStrToUtf16(szTmp, &pThis->pwszInputDevId);
|
---|
3142 | AssertRCReturn(rc, rc);
|
---|
3143 | }
|
---|
3144 |
|
---|
3145 | rc = pHlp->pfnCFGMQueryStringDef(pCfg, "OutputDeviceID", szTmp, sizeof(szTmp), "");
|
---|
3146 | AssertMsgRCReturn(rc, ("Confguration error: Failed to read \"OutputDeviceID\" as string: rc=%Rrc\n", rc), rc);
|
---|
3147 | if (szTmp[0])
|
---|
3148 | {
|
---|
3149 | rc = RTStrToUtf16(szTmp, &pThis->pwszOutputDevId);
|
---|
3150 | AssertRCReturn(rc, rc);
|
---|
3151 | }
|
---|
3152 |
|
---|
3153 | AssertMsgReturn(PDMDrvHlpNoAttach(pDrvIns) == VERR_PDM_NO_ATTACHED_DRIVER,
|
---|
3154 | ("Configuration error: Not possible to attach anything to this driver!\n"),
|
---|
3155 | VERR_PDM_DRVINS_NO_ATTACH);
|
---|
3156 |
|
---|
3157 | /*
|
---|
3158 | * Initialize the critical sections early.
|
---|
3159 | */
|
---|
3160 | rc = RTCritSectRwInit(&pThis->CritSectStreamList);
|
---|
3161 | AssertRCReturn(rc, rc);
|
---|
3162 |
|
---|
3163 | rc = RTCritSectInit(&pThis->CritSectCache);
|
---|
3164 | AssertRCReturn(rc, rc);
|
---|
3165 |
|
---|
3166 | /*
|
---|
3167 | * Create an enumerator instance that we can get the default devices from
|
---|
3168 | * as well as do enumeration thru.
|
---|
3169 | */
|
---|
3170 | HRESULT hrc = CoCreateInstance(__uuidof(MMDeviceEnumerator), 0, CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
|
---|
3171 | (void **)&pThis->pIEnumerator);
|
---|
3172 | if (FAILED(hrc))
|
---|
3173 | {
|
---|
3174 | pThis->pIEnumerator = NULL;
|
---|
3175 | LogRel(("WasAPI: Failed to create an MMDeviceEnumerator object: %Rhrc\n", hrc));
|
---|
3176 | return VERR_AUDIO_BACKEND_INIT_FAILED;
|
---|
3177 | }
|
---|
3178 | AssertPtr(pThis->pIEnumerator);
|
---|
3179 |
|
---|
3180 | /*
|
---|
3181 | * Resolve the interface to the driver above us.
|
---|
3182 | */
|
---|
3183 | pThis->pIHostAudioPort = PDMIBASE_QUERY_INTERFACE(pDrvIns->pUpBase, PDMIHOSTAUDIOPORT);
|
---|
3184 | AssertPtrReturn(pThis->pIHostAudioPort, VERR_PDM_MISSING_INTERFACE_ABOVE);
|
---|
3185 |
|
---|
3186 | /*
|
---|
3187 | * Instantiate and register the notification client with the enumerator.
|
---|
3188 | *
|
---|
3189 | * Failure here isn't considered fatal at this time as we'll just miss
|
---|
3190 | * default device changes.
|
---|
3191 | */
|
---|
3192 | try
|
---|
3193 | {
|
---|
3194 | pThis->pNotifyClient = new DrvHostAudioWasMmNotifyClient(pThis);
|
---|
3195 | }
|
---|
3196 | catch (std::bad_alloc &)
|
---|
3197 | {
|
---|
3198 | return VERR_NO_MEMORY;
|
---|
3199 | }
|
---|
3200 | catch (int rcXcpt)
|
---|
3201 | {
|
---|
3202 | return rcXcpt;
|
---|
3203 | }
|
---|
3204 | hrc = pThis->pIEnumerator->RegisterEndpointNotificationCallback(pThis->pNotifyClient);
|
---|
3205 | AssertMsg(SUCCEEDED(hrc), ("%Rhrc\n", hrc));
|
---|
3206 | if (FAILED(hrc))
|
---|
3207 | {
|
---|
3208 | LogRel(("WasAPI: RegisterEndpointNotificationCallback failed: %Rhrc (ignored)\n"
|
---|
3209 | "WasAPI: Warning! Will not be able to detect default device changes!\n"));
|
---|
3210 | pThis->pNotifyClient->notifyDriverDestroyed();
|
---|
3211 | pThis->pNotifyClient->Release();
|
---|
3212 | pThis->pNotifyClient = NULL;
|
---|
3213 | }
|
---|
3214 |
|
---|
3215 | /*
|
---|
3216 | * Retrieve the input and output device.
|
---|
3217 | */
|
---|
3218 | IMMDevice *pIDeviceInput = NULL;
|
---|
3219 | if (pThis->pwszInputDevId)
|
---|
3220 | hrc = pThis->pIEnumerator->GetDevice(pThis->pwszInputDevId, &pIDeviceInput);
|
---|
3221 | else
|
---|
3222 | hrc = pThis->pIEnumerator->GetDefaultAudioEndpoint(eCapture, eMultimedia, &pIDeviceInput);
|
---|
3223 | if (SUCCEEDED(hrc))
|
---|
3224 | LogFlowFunc(("pIDeviceInput=%p\n", pIDeviceInput));
|
---|
3225 | else
|
---|
3226 | {
|
---|
3227 | LogRel(("WasAPI: Failed to get audio input device '%ls': %Rhrc\n",
|
---|
3228 | pThis->pwszInputDevId ? pThis->pwszInputDevId : L"{Default}", hrc));
|
---|
3229 | pIDeviceInput = NULL;
|
---|
3230 | }
|
---|
3231 |
|
---|
3232 | IMMDevice *pIDeviceOutput = NULL;
|
---|
3233 | if (pThis->pwszOutputDevId)
|
---|
3234 | hrc = pThis->pIEnumerator->GetDevice(pThis->pwszOutputDevId, &pIDeviceOutput);
|
---|
3235 | else
|
---|
3236 | hrc = pThis->pIEnumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &pIDeviceOutput);
|
---|
3237 | if (SUCCEEDED(hrc))
|
---|
3238 | LogFlowFunc(("pIDeviceOutput=%p\n", pIDeviceOutput));
|
---|
3239 | else
|
---|
3240 | {
|
---|
3241 | LogRel(("WasAPI: Failed to get audio output device '%ls': %Rhrc\n",
|
---|
3242 | pThis->pwszOutputDevId ? pThis->pwszOutputDevId : L"{Default}", hrc));
|
---|
3243 | pIDeviceOutput = NULL;
|
---|
3244 | }
|
---|
3245 |
|
---|
3246 | /* Carefully place them in the instance data: */
|
---|
3247 | pThis->pNotifyClient->lockEnter();
|
---|
3248 |
|
---|
3249 | if (pThis->pIDeviceInput)
|
---|
3250 | pThis->pIDeviceInput->Release();
|
---|
3251 | pThis->pIDeviceInput = pIDeviceInput;
|
---|
3252 |
|
---|
3253 | if (pThis->pIDeviceOutput)
|
---|
3254 | pThis->pIDeviceOutput->Release();
|
---|
3255 | pThis->pIDeviceOutput = pIDeviceOutput;
|
---|
3256 |
|
---|
3257 | pThis->pNotifyClient->lockLeave();
|
---|
3258 |
|
---|
3259 | #if 0
|
---|
3260 | /*
|
---|
3261 | * Create the worker thread. This thread has a message loop and will be
|
---|
3262 | * signalled by DrvHostAudioWasMmNotifyClient while the VM is paused/whatever,
|
---|
3263 | * so better make it a regular thread rather than PDM thread.
|
---|
3264 | */
|
---|
3265 | pThis->uWorkerThreadFixedParam = (WPARAM)RTRandU64();
|
---|
3266 | rc = RTThreadCreateF(&pThis->hWorkerThread, drvHostWasWorkerThread, pThis, 0 /*cbStack*/, RTTHREADTYPE_DEFAULT,
|
---|
3267 | RTTHREADFLAGS_WAITABLE | RTTHREADFLAGS_COM_MTA, "WasWork%u", pDrvIns->iInstance);
|
---|
3268 | AssertRCReturn(rc, rc);
|
---|
3269 |
|
---|
3270 | rc = RTThreadUserWait(pThis->hWorkerThread, RT_MS_10SEC);
|
---|
3271 | AssertRC(rc);
|
---|
3272 | #endif
|
---|
3273 |
|
---|
3274 | /*
|
---|
3275 | * Prime the cache.
|
---|
3276 | */
|
---|
3277 | drvHostAudioWasCacheFill(pThis);
|
---|
3278 |
|
---|
3279 | return VINF_SUCCESS;
|
---|
3280 | }
|
---|
3281 |
|
---|
3282 |
|
---|
3283 | /**
|
---|
3284 | * PDM driver registration for WasAPI.
|
---|
3285 | */
|
---|
3286 | const PDMDRVREG g_DrvHostAudioWas =
|
---|
3287 | {
|
---|
3288 | /* u32Version */
|
---|
3289 | PDM_DRVREG_VERSION,
|
---|
3290 | /* szName */
|
---|
3291 | "HostAudioWas",
|
---|
3292 | /* szRCMod */
|
---|
3293 | "",
|
---|
3294 | /* szR0Mod */
|
---|
3295 | "",
|
---|
3296 | /* pszDescription */
|
---|
3297 | "Windows Audio Session API (WASAPI) host audio driver",
|
---|
3298 | /* fFlags */
|
---|
3299 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
3300 | /* fClass. */
|
---|
3301 | PDM_DRVREG_CLASS_AUDIO,
|
---|
3302 | /* cMaxInstances */
|
---|
3303 | ~0U,
|
---|
3304 | /* cbInstance */
|
---|
3305 | sizeof(DRVHOSTAUDIOWAS),
|
---|
3306 | /* pfnConstruct */
|
---|
3307 | drvHostAudioWasConstruct,
|
---|
3308 | /* pfnDestruct */
|
---|
3309 | drvHostAudioWasDestruct,
|
---|
3310 | /* pfnRelocate */
|
---|
3311 | NULL,
|
---|
3312 | /* pfnIOCtl */
|
---|
3313 | NULL,
|
---|
3314 | /* pfnPowerOn */
|
---|
3315 | NULL,
|
---|
3316 | /* pfnReset */
|
---|
3317 | NULL,
|
---|
3318 | /* pfnSuspend */
|
---|
3319 | NULL,
|
---|
3320 | /* pfnResume */
|
---|
3321 | NULL,
|
---|
3322 | /* pfnAttach */
|
---|
3323 | NULL,
|
---|
3324 | /* pfnDetach */
|
---|
3325 | NULL,
|
---|
3326 | /* pfnPowerOff */
|
---|
3327 | drvHostAudioWasPowerOff,
|
---|
3328 | /* pfnSoftReset */
|
---|
3329 | NULL,
|
---|
3330 | /* u32EndVersion */
|
---|
3331 | PDM_DRVREG_VERSION
|
---|
3332 | };
|
---|
3333 |
|
---|