1 | /* $Id: DrvAudio.cpp 89512 2021-06-04 15:27:51Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Intermediate audio driver - Connects the audio device emulation with the host backend.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_AUDIO
|
---|
23 | #include <VBox/log.h>
|
---|
24 | #include <VBox/vmm/pdm.h>
|
---|
25 | #include <VBox/err.h>
|
---|
26 | #include <VBox/vmm/mm.h>
|
---|
27 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
28 | #include <VBox/vmm/pdmaudioinline.h>
|
---|
29 | #include <VBox/vmm/pdmaudiohostenuminline.h>
|
---|
30 |
|
---|
31 | #include <iprt/alloc.h>
|
---|
32 | #include <iprt/asm-math.h>
|
---|
33 | #include <iprt/assert.h>
|
---|
34 | #include <iprt/circbuf.h>
|
---|
35 | #include <iprt/req.h>
|
---|
36 | #include <iprt/string.h>
|
---|
37 | #include <iprt/thread.h>
|
---|
38 | #include <iprt/uuid.h>
|
---|
39 |
|
---|
40 | #include "VBoxDD.h"
|
---|
41 |
|
---|
42 | #include <ctype.h>
|
---|
43 | #include <stdlib.h>
|
---|
44 |
|
---|
45 | #include "AudioHlp.h"
|
---|
46 | #include "AudioMixBuffer.h"
|
---|
47 |
|
---|
48 |
|
---|
49 | /*********************************************************************************************************************************
|
---|
50 | * Defined Constants And Macros *
|
---|
51 | *********************************************************************************************************************************/
|
---|
52 | /** @name PDMAUDIOSTREAM_STS_XXX - Used internally by DRVAUDIOSTREAM::fStatus.
|
---|
53 | * @{ */
|
---|
54 | /** No flags being set. */
|
---|
55 | #define PDMAUDIOSTREAM_STS_NONE UINT32_C(0)
|
---|
56 | /** Set if the stream is enabled, clear if disabled. */
|
---|
57 | #define PDMAUDIOSTREAM_STS_ENABLED RT_BIT_32(0)
|
---|
58 | /** Set if the stream is paused.
|
---|
59 | * Requires the ENABLED status to be set when used. */
|
---|
60 | #define PDMAUDIOSTREAM_STS_PAUSED RT_BIT_32(1)
|
---|
61 | /** Output only: Set when the stream is draining.
|
---|
62 | * Requires the ENABLED status to be set when used. */
|
---|
63 | #define PDMAUDIOSTREAM_STS_PENDING_DISABLE RT_BIT_32(2)
|
---|
64 |
|
---|
65 | /** Set if the backend for the stream has been created.
|
---|
66 | *
|
---|
67 | * This is generally always set after stream creation, but
|
---|
68 | * can be cleared if the re-initialization of the stream fails later on.
|
---|
69 | * Asynchronous init may still be incomplete, see
|
---|
70 | * PDMAUDIOSTREAM_STS_BACKEND_READY. */
|
---|
71 | #define PDMAUDIOSTREAM_STS_BACKEND_CREATED RT_BIT_32(3)
|
---|
72 | /** The backend is ready (PDMIHOSTAUDIO::pfnStreamInitAsync is done).
|
---|
73 | * Requires the BACKEND_CREATED status to be set. */
|
---|
74 | #define PDMAUDIOSTREAM_STS_BACKEND_READY RT_BIT_32(4)
|
---|
75 | /** Set if the stream needs to be re-initialized by the device (i.e. call
|
---|
76 | * PDMIAUDIOCONNECTOR::pfnStreamReInit). (The other status bits are preserved
|
---|
77 | * and are worked as normal while in this state, so that the stream can
|
---|
78 | * resume operation where it left off.) */
|
---|
79 | #define PDMAUDIOSTREAM_STS_NEED_REINIT RT_BIT_32(5)
|
---|
80 | /** Validation mask for PDMIAUDIOCONNECTOR. */
|
---|
81 | #define PDMAUDIOSTREAM_STS_VALID_MASK UINT32_C(0x0000003f)
|
---|
82 | /** Asserts the validity of the given stream status mask for PDMIAUDIOCONNECTOR. */
|
---|
83 | #define PDMAUDIOSTREAM_STS_ASSERT_VALID(a_fStreamStatus) do { \
|
---|
84 | AssertMsg(!((a_fStreamStatus) & ~PDMAUDIOSTREAM_STS_VALID_MASK), ("%#x\n", (a_fStreamStatus))); \
|
---|
85 | Assert(!((a_fStreamStatus) & PDMAUDIOSTREAM_STS_PAUSED) || ((a_fStreamStatus) & PDMAUDIOSTREAM_STS_ENABLED)); \
|
---|
86 | Assert(!((a_fStreamStatus) & PDMAUDIOSTREAM_STS_PENDING_DISABLE) || ((a_fStreamStatus) & PDMAUDIOSTREAM_STS_ENABLED)); \
|
---|
87 | Assert(!((a_fStreamStatus) & PDMAUDIOSTREAM_STS_BACKEND_READY) || ((a_fStreamStatus) & PDMAUDIOSTREAM_STS_BACKEND_CREATED)); \
|
---|
88 | } while (0)
|
---|
89 |
|
---|
90 | /** @} */
|
---|
91 |
|
---|
92 |
|
---|
93 | /*********************************************************************************************************************************
|
---|
94 | * Structures and Typedefs *
|
---|
95 | *********************************************************************************************************************************/
|
---|
96 | /**
|
---|
97 | * Audio stream context.
|
---|
98 | *
|
---|
99 | * Needed for separating data from the guest and host side (per stream).
|
---|
100 | */
|
---|
101 | typedef struct DRVAUDIOSTREAMCTX
|
---|
102 | {
|
---|
103 | /** The stream's audio configuration. */
|
---|
104 | PDMAUDIOSTREAMCFG Cfg;
|
---|
105 | } DRVAUDIOSTREAMCTX;
|
---|
106 |
|
---|
107 | /**
|
---|
108 | * Capture state of a stream wrt backend.
|
---|
109 | */
|
---|
110 | typedef enum DRVAUDIOCAPTURESTATE
|
---|
111 | {
|
---|
112 | /** Invalid zero value. */
|
---|
113 | DRVAUDIOCAPTURESTATE_INVALID = 0,
|
---|
114 | /** No capturing or pre-buffering. */
|
---|
115 | DRVAUDIOCAPTURESTATE_NO_CAPTURE,
|
---|
116 | /** Regular capturing. */
|
---|
117 | DRVAUDIOCAPTURESTATE_CAPTURING,
|
---|
118 | /** Returning silence till the backend buffer has reched the configured
|
---|
119 | * pre-buffering level. */
|
---|
120 | DRVAUDIOCAPTURESTATE_PREBUF,
|
---|
121 | /** End of valid values. */
|
---|
122 | DRVAUDIOCAPTURESTATE_END
|
---|
123 | } DRVAUDIOCAPTURESTATE;
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * Play state of a stream wrt backend.
|
---|
127 | */
|
---|
128 | typedef enum DRVAUDIOPLAYSTATE
|
---|
129 | {
|
---|
130 | /** Invalid zero value. */
|
---|
131 | DRVAUDIOPLAYSTATE_INVALID = 0,
|
---|
132 | /** No playback or pre-buffering. */
|
---|
133 | DRVAUDIOPLAYSTATE_NOPLAY,
|
---|
134 | /** Playing w/o any prebuffering. */
|
---|
135 | DRVAUDIOPLAYSTATE_PLAY,
|
---|
136 | /** Parallel pre-buffering prior to a device switch (i.e. we're outputting to
|
---|
137 | * the old device and pre-buffering the same data in parallel). */
|
---|
138 | DRVAUDIOPLAYSTATE_PLAY_PREBUF,
|
---|
139 | /** Initial pre-buffering or the pre-buffering for a device switch (if it
|
---|
140 | * the device setup took less time than filling up the pre-buffer). */
|
---|
141 | DRVAUDIOPLAYSTATE_PREBUF,
|
---|
142 | /** The device initialization is taking too long, pre-buffering wraps around
|
---|
143 | * and drops samples. */
|
---|
144 | DRVAUDIOPLAYSTATE_PREBUF_OVERDUE,
|
---|
145 | /** Same as play-prebuf, but we don't have a working output device any more. */
|
---|
146 | DRVAUDIOPLAYSTATE_PREBUF_SWITCHING,
|
---|
147 | /** Working on committing the pre-buffered data.
|
---|
148 | * We'll typically leave this state immediately and go to PLAY, however if
|
---|
149 | * the backend cannot handle all the pre-buffered data at once, we'll stay
|
---|
150 | * here till it does. */
|
---|
151 | DRVAUDIOPLAYSTATE_PREBUF_COMMITTING,
|
---|
152 | /** End of valid values. */
|
---|
153 | DRVAUDIOPLAYSTATE_END
|
---|
154 | } DRVAUDIOPLAYSTATE;
|
---|
155 |
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Extended stream structure.
|
---|
159 | */
|
---|
160 | typedef struct DRVAUDIOSTREAM
|
---|
161 | {
|
---|
162 | /** The publicly visible bit. */
|
---|
163 | PDMAUDIOSTREAM Core;
|
---|
164 |
|
---|
165 | /** Just an extra magic to verify that we allocated the stream rather than some
|
---|
166 | * faked up stuff from the device (DRVAUDIOSTREAM_MAGIC). */
|
---|
167 | uintptr_t uMagic;
|
---|
168 |
|
---|
169 | /** List entry in DRVAUDIO::LstStreams. */
|
---|
170 | RTLISTNODE ListEntry;
|
---|
171 |
|
---|
172 | /** Number of references to this stream.
|
---|
173 | * Only can be destroyed when the reference count reaches 0. */
|
---|
174 | uint32_t volatile cRefs;
|
---|
175 | /** Stream status - PDMAUDIOSTREAM_STS_XXX. */
|
---|
176 | uint32_t fStatus;
|
---|
177 |
|
---|
178 | /** Data to backend-specific stream data.
|
---|
179 | * This data block will be casted by the backend to access its backend-dependent data.
|
---|
180 | *
|
---|
181 | * That way the backends do not have access to the audio connector's data. */
|
---|
182 | PPDMAUDIOBACKENDSTREAM pBackend;
|
---|
183 |
|
---|
184 | /** Set if pfnStreamCreate returned VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED. */
|
---|
185 | bool fNeedAsyncInit;
|
---|
186 | /** The fImmediate parameter value for pfnStreamDestroy. */
|
---|
187 | bool fDestroyImmediate;
|
---|
188 | bool afPadding[2];
|
---|
189 |
|
---|
190 | /** Number of (re-)tries while re-initializing the stream. */
|
---|
191 | uint32_t cTriesReInit;
|
---|
192 |
|
---|
193 | /** The last backend state we saw.
|
---|
194 | * This is used to detect state changes (for what that is worth). */
|
---|
195 | PDMHOSTAUDIOSTREAMSTATE enmLastBackendState;
|
---|
196 |
|
---|
197 | /** The pre-buffering threshold expressed in bytes. */
|
---|
198 | uint32_t cbPreBufThreshold;
|
---|
199 |
|
---|
200 | /** The pfnStreamInitAsync request handle. */
|
---|
201 | PRTREQ hReqInitAsync;
|
---|
202 |
|
---|
203 | /** The nanosecond timestamp when the stream was started. */
|
---|
204 | uint64_t nsStarted;
|
---|
205 | /** Internal stream position (as per pfnStreamPlay/pfnStreamCapture). */
|
---|
206 | uint64_t offInternal;
|
---|
207 |
|
---|
208 | /** Timestamp (in ns) since last trying to re-initialize.
|
---|
209 | * Might be 0 if has not been tried yet. */
|
---|
210 | uint64_t nsLastReInit;
|
---|
211 | /** Timestamp (in ns) since last iteration. */
|
---|
212 | uint64_t nsLastIterated;
|
---|
213 | /** Timestamp (in ns) since last playback / capture. */
|
---|
214 | uint64_t nsLastPlayedCaptured;
|
---|
215 | /** Timestamp (in ns) since last read (input streams) or
|
---|
216 | * write (output streams). */
|
---|
217 | uint64_t nsLastReadWritten;
|
---|
218 |
|
---|
219 |
|
---|
220 | /** Union for input/output specifics depending on enmDir. */
|
---|
221 | union
|
---|
222 | {
|
---|
223 | /**
|
---|
224 | * The specifics for an audio input stream.
|
---|
225 | */
|
---|
226 | struct
|
---|
227 | {
|
---|
228 | /** The capture state. */
|
---|
229 | DRVAUDIOCAPTURESTATE enmCaptureState;
|
---|
230 |
|
---|
231 | struct
|
---|
232 | {
|
---|
233 | /** File for writing non-interleaved captures. */
|
---|
234 | PAUDIOHLPFILE pFileCapture;
|
---|
235 | } Dbg;
|
---|
236 | struct
|
---|
237 | {
|
---|
238 | uint32_t cbBackendReadableBefore;
|
---|
239 | uint32_t cbBackendReadableAfter;
|
---|
240 |
|
---|
241 | STAMCOUNTER TotalFramesCaptured;
|
---|
242 | STAMCOUNTER AvgFramesCaptured;
|
---|
243 | STAMCOUNTER TotalTimesCaptured;
|
---|
244 | STAMCOUNTER TotalFramesRead;
|
---|
245 | STAMCOUNTER AvgFramesRead;
|
---|
246 | STAMCOUNTER TotalTimesRead;
|
---|
247 | } Stats;
|
---|
248 | } In;
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * The specifics for an audio output stream.
|
---|
252 | */
|
---|
253 | struct
|
---|
254 | {
|
---|
255 | struct
|
---|
256 | {
|
---|
257 | /** File for writing stream playback. */
|
---|
258 | PAUDIOHLPFILE pFilePlay;
|
---|
259 | } Dbg;
|
---|
260 | struct
|
---|
261 | {
|
---|
262 | uint32_t cbBackendWritableBefore;
|
---|
263 | uint32_t cbBackendWritableAfter;
|
---|
264 | } Stats;
|
---|
265 |
|
---|
266 | /** Space for pre-buffering. */
|
---|
267 | uint8_t *pbPreBuf;
|
---|
268 | /** The size of the pre-buffer allocation (in bytes). */
|
---|
269 | uint32_t cbPreBufAlloc;
|
---|
270 | /** The current pre-buffering read offset. */
|
---|
271 | uint32_t offPreBuf;
|
---|
272 | /** Number of bytes we've pre-buffered. */
|
---|
273 | uint32_t cbPreBuffered;
|
---|
274 | /** The play state. */
|
---|
275 | DRVAUDIOPLAYSTATE enmPlayState;
|
---|
276 | } Out;
|
---|
277 | } RT_UNION_NM(u);
|
---|
278 | } DRVAUDIOSTREAM;
|
---|
279 | /** Pointer to an extended stream structure. */
|
---|
280 | typedef DRVAUDIOSTREAM *PDRVAUDIOSTREAM;
|
---|
281 |
|
---|
282 | /** Value for DRVAUDIOSTREAM::uMagic (Johann Sebastian Bach). */
|
---|
283 | #define DRVAUDIOSTREAM_MAGIC UINT32_C(0x16850331)
|
---|
284 | /** Value for DRVAUDIOSTREAM::uMagic after destruction */
|
---|
285 | #define DRVAUDIOSTREAM_MAGIC_DEAD UINT32_C(0x17500728)
|
---|
286 |
|
---|
287 |
|
---|
288 | /**
|
---|
289 | * Audio driver configuration data, tweakable via CFGM.
|
---|
290 | */
|
---|
291 | typedef struct DRVAUDIOCFG
|
---|
292 | {
|
---|
293 | /** PCM properties to use. */
|
---|
294 | PDMAUDIOPCMPROPS Props;
|
---|
295 | /** Whether using signed sample data or not.
|
---|
296 | * Needed in order to know whether there is a custom value set in CFGM or not.
|
---|
297 | * By default set to UINT8_MAX if not set to a custom value. */
|
---|
298 | uint8_t uSigned;
|
---|
299 | /** Whether swapping endianess of sample data or not.
|
---|
300 | * Needed in order to know whether there is a custom value set in CFGM or not.
|
---|
301 | * By default set to UINT8_MAX if not set to a custom value. */
|
---|
302 | uint8_t uSwapEndian;
|
---|
303 | /** Configures the period size (in ms).
|
---|
304 | * This value reflects the time in between each hardware interrupt on the
|
---|
305 | * backend (host) side. */
|
---|
306 | uint32_t uPeriodSizeMs;
|
---|
307 | /** Configures the (ring) buffer size (in ms). Often is a multiple of uPeriodMs. */
|
---|
308 | uint32_t uBufferSizeMs;
|
---|
309 | /** Configures the pre-buffering size (in ms).
|
---|
310 | * Time needed in buffer before the stream becomes active (pre buffering).
|
---|
311 | * The bigger this value is, the more latency for the stream will occur.
|
---|
312 | * Set to 0 to disable pre-buffering completely.
|
---|
313 | * By default set to UINT32_MAX if not set to a custom value. */
|
---|
314 | uint32_t uPreBufSizeMs;
|
---|
315 | /** The driver's debugging configuration. */
|
---|
316 | struct
|
---|
317 | {
|
---|
318 | /** Whether audio debugging is enabled or not. */
|
---|
319 | bool fEnabled;
|
---|
320 | /** Where to store the debugging files. */
|
---|
321 | char szPathOut[RTPATH_MAX];
|
---|
322 | } Dbg;
|
---|
323 | } DRVAUDIOCFG;
|
---|
324 | /** Pointer to tweakable audio configuration. */
|
---|
325 | typedef DRVAUDIOCFG *PDRVAUDIOCFG;
|
---|
326 | /** Pointer to const tweakable audio configuration. */
|
---|
327 | typedef DRVAUDIOCFG const *PCDRVAUDIOCFG;
|
---|
328 |
|
---|
329 |
|
---|
330 | /**
|
---|
331 | * Audio driver instance data.
|
---|
332 | *
|
---|
333 | * @implements PDMIAUDIOCONNECTOR
|
---|
334 | */
|
---|
335 | typedef struct DRVAUDIO
|
---|
336 | {
|
---|
337 | /** Read/Write critical section for guarding changes to pHostDrvAudio and
|
---|
338 | * BackendCfg during deteach/attach. Mostly taken in shared mode.
|
---|
339 | * @note Locking order: Must be entered after CritSectGlobals.
|
---|
340 | * @note Locking order: Must be entered after PDMAUDIOSTREAM::CritSect. */
|
---|
341 | RTCRITSECTRW CritSectHotPlug;
|
---|
342 | /** Critical section for protecting:
|
---|
343 | * - LstStreams
|
---|
344 | * - cStreams
|
---|
345 | * - In.fEnabled
|
---|
346 | * - In.cStreamsFree
|
---|
347 | * - Out.fEnabled
|
---|
348 | * - Out.cStreamsFree
|
---|
349 | * @note Locking order: Must be entered before PDMAUDIOSTREAM::CritSect.
|
---|
350 | * @note Locking order: Must be entered before CritSectHotPlug. */
|
---|
351 | RTCRITSECTRW CritSectGlobals;
|
---|
352 | /** List of audio streams (DRVAUDIOSTREAM). */
|
---|
353 | RTLISTANCHOR LstStreams;
|
---|
354 | /** Number of streams in the list. */
|
---|
355 | size_t cStreams;
|
---|
356 | struct
|
---|
357 | {
|
---|
358 | /** Whether this driver's input streams are enabled or not.
|
---|
359 | * This flag overrides all the attached stream statuses. */
|
---|
360 | bool fEnabled;
|
---|
361 | /** Max. number of free input streams.
|
---|
362 | * UINT32_MAX for unlimited streams. */
|
---|
363 | uint32_t cStreamsFree;
|
---|
364 | } In;
|
---|
365 | struct
|
---|
366 | {
|
---|
367 | /** Whether this driver's output streams are enabled or not.
|
---|
368 | * This flag overrides all the attached stream statuses. */
|
---|
369 | bool fEnabled;
|
---|
370 | /** Max. number of free output streams.
|
---|
371 | * UINT32_MAX for unlimited streams. */
|
---|
372 | uint32_t cStreamsFree;
|
---|
373 | } Out;
|
---|
374 |
|
---|
375 | /** Audio configuration settings retrieved from the backend.
|
---|
376 | * The szName field is used for the DriverName config value till we get the
|
---|
377 | * authoritative name from the backend (only for logging). */
|
---|
378 | PDMAUDIOBACKENDCFG BackendCfg;
|
---|
379 | /** Our audio connector interface. */
|
---|
380 | PDMIAUDIOCONNECTOR IAudioConnector;
|
---|
381 | /** Interface used by the host backend. */
|
---|
382 | PDMIHOSTAUDIOPORT IHostAudioPort;
|
---|
383 | /** Pointer to the driver instance. */
|
---|
384 | PPDMDRVINS pDrvIns;
|
---|
385 | /** Pointer to audio driver below us. */
|
---|
386 | PPDMIHOSTAUDIO pHostDrvAudio;
|
---|
387 |
|
---|
388 | /** Request pool if the backend needs it for async stream creation. */
|
---|
389 | RTREQPOOL hReqPool;
|
---|
390 |
|
---|
391 | #ifdef VBOX_WITH_STATISTICS
|
---|
392 | /** Statistics. */
|
---|
393 | struct
|
---|
394 | {
|
---|
395 | STAMCOUNTER TotalStreamsActive;
|
---|
396 | STAMCOUNTER TotalStreamsCreated;
|
---|
397 | STAMCOUNTER TotalFramesRead;
|
---|
398 | STAMCOUNTER TotalFramesIn;
|
---|
399 | STAMCOUNTER TotalBytesRead;
|
---|
400 | } Stats;
|
---|
401 | #endif
|
---|
402 |
|
---|
403 | #ifdef VBOX_WITH_AUDIO_ENUM
|
---|
404 | /** Handle to the timer for delayed re-enumeration of backend devices. */
|
---|
405 | TMTIMERHANDLE hEnumTimer;
|
---|
406 | /** Unique name for the the disable-iteration timer. */
|
---|
407 | char szEnumTimerName[24];
|
---|
408 | #endif
|
---|
409 |
|
---|
410 | /** Input audio configuration values (static). */
|
---|
411 | DRVAUDIOCFG CfgIn;
|
---|
412 | /** Output audio configuration values (static). */
|
---|
413 | DRVAUDIOCFG CfgOut;
|
---|
414 | } DRVAUDIO;
|
---|
415 | /** Pointer to the instance data of an audio driver. */
|
---|
416 | typedef DRVAUDIO *PDRVAUDIO;
|
---|
417 | /** Pointer to const instance data of an audio driver. */
|
---|
418 | typedef DRVAUDIO const *PCDRVAUDIO;
|
---|
419 |
|
---|
420 |
|
---|
421 | /*********************************************************************************************************************************
|
---|
422 | * Internal Functions *
|
---|
423 | *********************************************************************************************************************************/
|
---|
424 | static int drvAudioStreamControlInternalBackend(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, PDMAUDIOSTREAMCMD enmStreamCmd);
|
---|
425 | static int drvAudioStreamControlInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, PDMAUDIOSTREAMCMD enmStreamCmd);
|
---|
426 | static int drvAudioStreamUninitInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx);
|
---|
427 | static uint32_t drvAudioStreamRetainInternal(PDRVAUDIOSTREAM pStreamEx);
|
---|
428 | static uint32_t drvAudioStreamReleaseInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, bool fMayDestroy);
|
---|
429 | static void drvAudioStreamResetInternal(PDRVAUDIOSTREAM pStreamEx);
|
---|
430 |
|
---|
431 |
|
---|
432 | /** Buffer size for drvAudioStreamStatusToStr. */
|
---|
433 | # define DRVAUDIO_STATUS_STR_MAX sizeof("BACKEND_CREATED BACKEND_READY ENABLED PAUSED PENDING_DISABLED NEED_REINIT 0x12345678")
|
---|
434 |
|
---|
435 | /**
|
---|
436 | * Converts an audio stream status to a string.
|
---|
437 | *
|
---|
438 | * @returns pszDst
|
---|
439 | * @param pszDst Buffer to convert into, at least minimum size is
|
---|
440 | * DRVAUDIO_STATUS_STR_MAX.
|
---|
441 | * @param fStatus Stream status flags to convert.
|
---|
442 | */
|
---|
443 | static const char *drvAudioStreamStatusToStr(char pszDst[DRVAUDIO_STATUS_STR_MAX], uint32_t fStatus)
|
---|
444 | {
|
---|
445 | static const struct
|
---|
446 | {
|
---|
447 | const char *pszMnemonic;
|
---|
448 | uint32_t cchMnemnonic;
|
---|
449 | uint32_t fFlag;
|
---|
450 | } s_aFlags[] =
|
---|
451 | {
|
---|
452 | { RT_STR_TUPLE("BACKEND_CREATED "), PDMAUDIOSTREAM_STS_BACKEND_CREATED },
|
---|
453 | { RT_STR_TUPLE("BACKEND_READY "), PDMAUDIOSTREAM_STS_BACKEND_READY },
|
---|
454 | { RT_STR_TUPLE("ENABLED "), PDMAUDIOSTREAM_STS_ENABLED },
|
---|
455 | { RT_STR_TUPLE("PAUSED "), PDMAUDIOSTREAM_STS_PAUSED },
|
---|
456 | { RT_STR_TUPLE("PENDING_DISABLE "), PDMAUDIOSTREAM_STS_PENDING_DISABLE },
|
---|
457 | { RT_STR_TUPLE("NEED_REINIT "), PDMAUDIOSTREAM_STS_NEED_REINIT },
|
---|
458 | };
|
---|
459 | if (!fStatus)
|
---|
460 | strcpy(pszDst, "NONE");
|
---|
461 | else
|
---|
462 | {
|
---|
463 | char *psz = pszDst;
|
---|
464 | for (size_t i = 0; i < RT_ELEMENTS(s_aFlags); i++)
|
---|
465 | if (fStatus & s_aFlags[i].fFlag)
|
---|
466 | {
|
---|
467 | memcpy(psz, s_aFlags[i].pszMnemonic, s_aFlags[i].cchMnemnonic);
|
---|
468 | psz += s_aFlags[i].cchMnemnonic;
|
---|
469 | fStatus &= ~s_aFlags[i].fFlag;
|
---|
470 | if (!fStatus)
|
---|
471 | break;
|
---|
472 | }
|
---|
473 | if (fStatus == 0)
|
---|
474 | psz[-1] = '\0';
|
---|
475 | else
|
---|
476 | psz += RTStrPrintf(psz, DRVAUDIO_STATUS_STR_MAX - (psz - pszDst), "%#x", fStatus);
|
---|
477 | Assert((uintptr_t)(psz - pszDst) <= DRVAUDIO_STATUS_STR_MAX);
|
---|
478 | }
|
---|
479 | return pszDst;
|
---|
480 | }
|
---|
481 |
|
---|
482 |
|
---|
483 | /**
|
---|
484 | * Get play state name string.
|
---|
485 | */
|
---|
486 | static const char *drvAudioPlayStateName(DRVAUDIOPLAYSTATE enmState)
|
---|
487 | {
|
---|
488 | switch (enmState)
|
---|
489 | {
|
---|
490 | case DRVAUDIOPLAYSTATE_INVALID: return "INVALID";
|
---|
491 | case DRVAUDIOPLAYSTATE_NOPLAY: return "NOPLAY";
|
---|
492 | case DRVAUDIOPLAYSTATE_PLAY: return "PLAY";
|
---|
493 | case DRVAUDIOPLAYSTATE_PLAY_PREBUF: return "PLAY_PREBUF";
|
---|
494 | case DRVAUDIOPLAYSTATE_PREBUF: return "PREBUF";
|
---|
495 | case DRVAUDIOPLAYSTATE_PREBUF_OVERDUE: return "PREBUF_OVERDUE";
|
---|
496 | case DRVAUDIOPLAYSTATE_PREBUF_SWITCHING: return "PREBUF_SWITCHING";
|
---|
497 | case DRVAUDIOPLAYSTATE_PREBUF_COMMITTING: return "PREBUF_COMMITTING";
|
---|
498 | case DRVAUDIOPLAYSTATE_END:
|
---|
499 | break;
|
---|
500 | }
|
---|
501 | return "BAD";
|
---|
502 | }
|
---|
503 |
|
---|
504 | #ifdef LOG_ENABLED
|
---|
505 | /**
|
---|
506 | * Get capture state name string.
|
---|
507 | */
|
---|
508 | static const char *drvAudioCaptureStateName(DRVAUDIOCAPTURESTATE enmState)
|
---|
509 | {
|
---|
510 | switch (enmState)
|
---|
511 | {
|
---|
512 | case DRVAUDIOCAPTURESTATE_INVALID: return "INVALID";
|
---|
513 | case DRVAUDIOCAPTURESTATE_NO_CAPTURE: return "NO_CAPTURE";
|
---|
514 | case DRVAUDIOCAPTURESTATE_CAPTURING: return "CAPTURING";
|
---|
515 | case DRVAUDIOCAPTURESTATE_PREBUF: return "PREBUF";
|
---|
516 | case DRVAUDIOCAPTURESTATE_END:
|
---|
517 | break;
|
---|
518 | }
|
---|
519 | return "BAD";
|
---|
520 | }
|
---|
521 | #endif
|
---|
522 |
|
---|
523 | /**
|
---|
524 | * Checks if the stream status is one that can be read from.
|
---|
525 | *
|
---|
526 | * @returns @c true if ready to be read from, @c false if not.
|
---|
527 | * @param fStatus Stream status to evaluate, PDMAUDIOSTREAM_STS_XXX.
|
---|
528 | * @note Not for backend statuses (use PDMAudioStrmStatusBackendCanRead)!
|
---|
529 | */
|
---|
530 | DECLINLINE(bool) PDMAudioStrmStatusCanRead(uint32_t fStatus)
|
---|
531 | {
|
---|
532 | PDMAUDIOSTREAM_STS_ASSERT_VALID(fStatus);
|
---|
533 | AssertReturn(!(fStatus & ~PDMAUDIOSTREAM_STS_VALID_MASK), false);
|
---|
534 | return (fStatus & ( PDMAUDIOSTREAM_STS_BACKEND_CREATED
|
---|
535 | | PDMAUDIOSTREAM_STS_ENABLED
|
---|
536 | | PDMAUDIOSTREAM_STS_PAUSED
|
---|
537 | | PDMAUDIOSTREAM_STS_NEED_REINIT))
|
---|
538 | == ( PDMAUDIOSTREAM_STS_BACKEND_CREATED
|
---|
539 | | PDMAUDIOSTREAM_STS_ENABLED);
|
---|
540 | }
|
---|
541 |
|
---|
542 | /**
|
---|
543 | * Checks if the stream status is one that can be written to.
|
---|
544 | *
|
---|
545 | * @returns @c true if ready to be written to, @c false if not.
|
---|
546 | * @param fStatus Stream status to evaluate, PDMAUDIOSTREAM_STS_XXX.
|
---|
547 | * @note Not for backend statuses (use PDMAudioStrmStatusBackendCanWrite)!
|
---|
548 | */
|
---|
549 | DECLINLINE(bool) PDMAudioStrmStatusCanWrite(uint32_t fStatus)
|
---|
550 | {
|
---|
551 | PDMAUDIOSTREAM_STS_ASSERT_VALID(fStatus);
|
---|
552 | AssertReturn(!(fStatus & ~PDMAUDIOSTREAM_STS_VALID_MASK), false);
|
---|
553 | return (fStatus & ( PDMAUDIOSTREAM_STS_BACKEND_CREATED
|
---|
554 | | PDMAUDIOSTREAM_STS_ENABLED
|
---|
555 | | PDMAUDIOSTREAM_STS_PAUSED
|
---|
556 | | PDMAUDIOSTREAM_STS_PENDING_DISABLE
|
---|
557 | | PDMAUDIOSTREAM_STS_NEED_REINIT))
|
---|
558 | == ( PDMAUDIOSTREAM_STS_BACKEND_CREATED
|
---|
559 | | PDMAUDIOSTREAM_STS_ENABLED);
|
---|
560 | }
|
---|
561 |
|
---|
562 | /**
|
---|
563 | * Checks if the stream status is a ready-to-operate one.
|
---|
564 | *
|
---|
565 | * @returns @c true if ready to operate, @c false if not.
|
---|
566 | * @param fStatus Stream status to evaluate, PDMAUDIOSTREAM_STS_XXX.
|
---|
567 | * @note Not for backend statuses!
|
---|
568 | */
|
---|
569 | DECLINLINE(bool) PDMAudioStrmStatusIsReady(uint32_t fStatus)
|
---|
570 | {
|
---|
571 | PDMAUDIOSTREAM_STS_ASSERT_VALID(fStatus);
|
---|
572 | AssertReturn(!(fStatus & ~PDMAUDIOSTREAM_STS_VALID_MASK), false);
|
---|
573 | return (fStatus & ( PDMAUDIOSTREAM_STS_BACKEND_CREATED
|
---|
574 | | PDMAUDIOSTREAM_STS_ENABLED
|
---|
575 | | PDMAUDIOSTREAM_STS_NEED_REINIT))
|
---|
576 | == ( PDMAUDIOSTREAM_STS_BACKEND_CREATED
|
---|
577 | | PDMAUDIOSTREAM_STS_ENABLED);
|
---|
578 | }
|
---|
579 |
|
---|
580 |
|
---|
581 | /**
|
---|
582 | * Wrapper around PDMIHOSTAUDIO::pfnStreamGetStatus and checks the result.
|
---|
583 | *
|
---|
584 | * @returns A PDMHOSTAUDIOSTREAMSTATE value.
|
---|
585 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
586 | * @param pStreamEx The stream to get the backend status for.
|
---|
587 | */
|
---|
588 | DECLINLINE(PDMHOSTAUDIOSTREAMSTATE) drvAudioStreamGetBackendState(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
589 | {
|
---|
590 | if (pThis->pHostDrvAudio)
|
---|
591 | {
|
---|
592 | PDMHOSTAUDIOSTREAMSTATE enmState = pThis->pHostDrvAudio->pfnStreamGetState(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
593 | Log9Func(("%s: %s\n", pStreamEx->Core.Cfg.szName, PDMHostAudioStreamStateGetName(enmState) ));
|
---|
594 | Assert( enmState > PDMHOSTAUDIOSTREAMSTATE_INVALID
|
---|
595 | && enmState < PDMHOSTAUDIOSTREAMSTATE_END
|
---|
596 | && (enmState != PDMHOSTAUDIOSTREAMSTATE_DRAINING || pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT));
|
---|
597 | return enmState;
|
---|
598 | }
|
---|
599 | Log9Func(("%s: not-working\n", pStreamEx->Core.Cfg.szName));
|
---|
600 | return PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING;
|
---|
601 | }
|
---|
602 |
|
---|
603 |
|
---|
604 | /**
|
---|
605 | * Worker for drvAudioStreamProcessBackendStateChange that completes draining.
|
---|
606 | */
|
---|
607 | DECLINLINE(void) drvAudioStreamProcessBackendStateChangeWasDraining(PDRVAUDIOSTREAM pStreamEx)
|
---|
608 | {
|
---|
609 | Log(("drvAudioStreamProcessBackendStateChange: Stream '%s': Done draining - disabling stream.\n", pStreamEx->Core.Cfg.szName));
|
---|
610 | pStreamEx->fStatus &= ~(PDMAUDIOSTREAM_STS_ENABLED | PDMAUDIOSTREAM_STS_PENDING_DISABLE);
|
---|
611 | drvAudioStreamResetInternal(pStreamEx);
|
---|
612 | }
|
---|
613 |
|
---|
614 |
|
---|
615 | /**
|
---|
616 | * Processes backend state change.
|
---|
617 | *
|
---|
618 | * @returns the new state value.
|
---|
619 | */
|
---|
620 | static PDMHOSTAUDIOSTREAMSTATE drvAudioStreamProcessBackendStateChange(PDRVAUDIOSTREAM pStreamEx,
|
---|
621 | PDMHOSTAUDIOSTREAMSTATE enmNewState,
|
---|
622 | PDMHOSTAUDIOSTREAMSTATE enmOldState)
|
---|
623 | {
|
---|
624 | PDMAUDIODIR const enmDir = pStreamEx->Core.Cfg.enmDir;
|
---|
625 | #ifdef LOG_ENABLED
|
---|
626 | DRVAUDIOPLAYSTATE const enmPlayState = enmDir == PDMAUDIODIR_OUT
|
---|
627 | ? pStreamEx->Out.enmPlayState : DRVAUDIOPLAYSTATE_INVALID;
|
---|
628 | DRVAUDIOCAPTURESTATE const enmCaptureState = enmDir == PDMAUDIODIR_OUT
|
---|
629 | ? pStreamEx->In.enmCaptureState : DRVAUDIOCAPTURESTATE_INVALID;
|
---|
630 | #endif
|
---|
631 | Assert(enmNewState != enmOldState);
|
---|
632 | Assert(enmOldState > PDMHOSTAUDIOSTREAMSTATE_INVALID && enmOldState < PDMHOSTAUDIOSTREAMSTATE_END);
|
---|
633 | AssertReturn(enmNewState > PDMHOSTAUDIOSTREAMSTATE_INVALID && enmNewState < PDMHOSTAUDIOSTREAMSTATE_END, enmOldState);
|
---|
634 |
|
---|
635 | /*
|
---|
636 | * Figure out what happend and how that reflects on the playback state and stuff.
|
---|
637 | */
|
---|
638 | switch (enmNewState)
|
---|
639 | {
|
---|
640 | case PDMHOSTAUDIOSTREAMSTATE_INITIALIZING:
|
---|
641 | /* Guess we're switching device. Nothing to do because the backend will tell us, right? */
|
---|
642 | break;
|
---|
643 |
|
---|
644 | case PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING:
|
---|
645 | case PDMHOSTAUDIOSTREAMSTATE_INACTIVE:
|
---|
646 | /* The stream has stopped working or is inactive. Switch stop any draining & to noplay mode. */
|
---|
647 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE)
|
---|
648 | drvAudioStreamProcessBackendStateChangeWasDraining(pStreamEx);
|
---|
649 | if (enmDir == PDMAUDIODIR_OUT)
|
---|
650 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_NOPLAY;
|
---|
651 | else
|
---|
652 | pStreamEx->In.enmCaptureState = DRVAUDIOCAPTURESTATE_NO_CAPTURE;
|
---|
653 | break;
|
---|
654 |
|
---|
655 | case PDMHOSTAUDIOSTREAMSTATE_OKAY:
|
---|
656 | switch (enmOldState)
|
---|
657 | {
|
---|
658 | case PDMHOSTAUDIOSTREAMSTATE_INITIALIZING:
|
---|
659 | /* Should be taken care of elsewhere, so do nothing. */
|
---|
660 | break;
|
---|
661 |
|
---|
662 | case PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING:
|
---|
663 | case PDMHOSTAUDIOSTREAMSTATE_INACTIVE:
|
---|
664 | /* Go back to pre-buffering/playing depending on whether it is enabled
|
---|
665 | or not, resetting the stream state. */
|
---|
666 | drvAudioStreamResetInternal(pStreamEx);
|
---|
667 | break;
|
---|
668 |
|
---|
669 | case PDMHOSTAUDIOSTREAMSTATE_DRAINING:
|
---|
670 | /* Complete the draining. May race the iterate code. */
|
---|
671 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE)
|
---|
672 | drvAudioStreamProcessBackendStateChangeWasDraining(pStreamEx);
|
---|
673 | break;
|
---|
674 |
|
---|
675 | /* no default: */
|
---|
676 | case PDMHOSTAUDIOSTREAMSTATE_OKAY: /* impossible */
|
---|
677 | case PDMHOSTAUDIOSTREAMSTATE_INVALID:
|
---|
678 | case PDMHOSTAUDIOSTREAMSTATE_END:
|
---|
679 | case PDMHOSTAUDIOSTREAMSTATE_32BIT_HACK:
|
---|
680 | break;
|
---|
681 | }
|
---|
682 | break;
|
---|
683 |
|
---|
684 | case PDMHOSTAUDIOSTREAMSTATE_DRAINING:
|
---|
685 | /* We do all we need to do when issuing the DRAIN command. */
|
---|
686 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE);
|
---|
687 | break;
|
---|
688 |
|
---|
689 | /* no default: */
|
---|
690 | case PDMHOSTAUDIOSTREAMSTATE_INVALID:
|
---|
691 | case PDMHOSTAUDIOSTREAMSTATE_END:
|
---|
692 | case PDMHOSTAUDIOSTREAMSTATE_32BIT_HACK:
|
---|
693 | break;
|
---|
694 | }
|
---|
695 |
|
---|
696 | if (enmDir == PDMAUDIODIR_OUT)
|
---|
697 | LogFunc(("Output stream '%s': %s/%s -> %s/%s\n", pStreamEx->Core.Cfg.szName,
|
---|
698 | PDMHostAudioStreamStateGetName(enmOldState), drvAudioPlayStateName(enmPlayState),
|
---|
699 | PDMHostAudioStreamStateGetName(enmNewState), drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
700 | else
|
---|
701 | LogFunc(("Input stream '%s': %s/%s -> %s/%s\n", pStreamEx->Core.Cfg.szName,
|
---|
702 | PDMHostAudioStreamStateGetName(enmOldState), drvAudioCaptureStateName(enmCaptureState),
|
---|
703 | PDMHostAudioStreamStateGetName(enmNewState), drvAudioCaptureStateName(pStreamEx->In.enmCaptureState) ));
|
---|
704 |
|
---|
705 | pStreamEx->enmLastBackendState = enmNewState;
|
---|
706 | return enmNewState;
|
---|
707 | }
|
---|
708 |
|
---|
709 |
|
---|
710 | /**
|
---|
711 | * This gets the backend state and handles changes compared to
|
---|
712 | * DRVAUDIOSTREAM::enmLastBackendState (updated).
|
---|
713 | *
|
---|
714 | * @returns A PDMHOSTAUDIOSTREAMSTATE value.
|
---|
715 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
716 | * @param pStreamEx The stream to get the backend status for.
|
---|
717 | */
|
---|
718 | DECLINLINE(PDMHOSTAUDIOSTREAMSTATE) drvAudioStreamGetBackendStateAndProcessChanges(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
719 | {
|
---|
720 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
721 | if (pStreamEx->enmLastBackendState == enmBackendState)
|
---|
722 | return enmBackendState;
|
---|
723 | return drvAudioStreamProcessBackendStateChange(pStreamEx, enmBackendState, pStreamEx->enmLastBackendState);
|
---|
724 | }
|
---|
725 |
|
---|
726 |
|
---|
727 | #ifdef VBOX_WITH_AUDIO_ENUM
|
---|
728 | /**
|
---|
729 | * Enumerates all host audio devices.
|
---|
730 | *
|
---|
731 | * This functionality might not be implemented by all backends and will return
|
---|
732 | * VERR_NOT_SUPPORTED if not being supported.
|
---|
733 | *
|
---|
734 | * @note Must not hold the driver's critical section!
|
---|
735 | *
|
---|
736 | * @returns VBox status code.
|
---|
737 | * @param pThis Driver instance to be called.
|
---|
738 | * @param fLog Whether to print the enumerated device to the release log or not.
|
---|
739 | * @param pDevEnum Where to store the device enumeration.
|
---|
740 | *
|
---|
741 | * @remarks This is currently ONLY used for release logging.
|
---|
742 | */
|
---|
743 | static DECLCALLBACK(int) drvAudioDevicesEnumerateInternal(PDRVAUDIO pThis, bool fLog, PPDMAUDIOHOSTENUM pDevEnum)
|
---|
744 | {
|
---|
745 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
746 |
|
---|
747 | int rc;
|
---|
748 |
|
---|
749 | /*
|
---|
750 | * If the backend supports it, do a device enumeration.
|
---|
751 | */
|
---|
752 | if (pThis->pHostDrvAudio->pfnGetDevices)
|
---|
753 | {
|
---|
754 | PDMAUDIOHOSTENUM DevEnum;
|
---|
755 | rc = pThis->pHostDrvAudio->pfnGetDevices(pThis->pHostDrvAudio, &DevEnum);
|
---|
756 | if (RT_SUCCESS(rc))
|
---|
757 | {
|
---|
758 | if (fLog)
|
---|
759 | {
|
---|
760 | LogRel(("Audio: Found %RU16 devices for driver '%s'\n", DevEnum.cDevices, pThis->BackendCfg.szName));
|
---|
761 |
|
---|
762 | PPDMAUDIOHOSTDEV pDev;
|
---|
763 | RTListForEach(&DevEnum.LstDevices, pDev, PDMAUDIOHOSTDEV, ListEntry)
|
---|
764 | {
|
---|
765 | char szFlags[PDMAUDIOHOSTDEV_MAX_FLAGS_STRING_LEN];
|
---|
766 | LogRel(("Audio: Device '%s':\n"
|
---|
767 | "Audio: ID = %s\n"
|
---|
768 | "Audio: Usage = %s\n"
|
---|
769 | "Audio: Flags = %s\n"
|
---|
770 | "Audio: Input channels = %RU8\n"
|
---|
771 | "Audio: Output channels = %RU8\n",
|
---|
772 | pDev->pszName, pDev->pszId ? pDev->pszId : "",
|
---|
773 | PDMAudioDirGetName(pDev->enmUsage), PDMAudioHostDevFlagsToString(szFlags, pDev->fFlags),
|
---|
774 | pDev->cMaxInputChannels, pDev->cMaxOutputChannels));
|
---|
775 | }
|
---|
776 | }
|
---|
777 |
|
---|
778 | if (pDevEnum)
|
---|
779 | rc = PDMAudioHostEnumCopy(pDevEnum, &DevEnum, PDMAUDIODIR_INVALID /*all*/, true /*fOnlyCoreData*/);
|
---|
780 |
|
---|
781 | PDMAudioHostEnumDelete(&DevEnum);
|
---|
782 | }
|
---|
783 | else
|
---|
784 | {
|
---|
785 | if (fLog)
|
---|
786 | LogRel(("Audio: Device enumeration for driver '%s' failed with %Rrc\n", pThis->BackendCfg.szName, rc));
|
---|
787 | /* Not fatal. */
|
---|
788 | }
|
---|
789 | }
|
---|
790 | else
|
---|
791 | {
|
---|
792 | rc = VERR_NOT_SUPPORTED;
|
---|
793 |
|
---|
794 | if (fLog)
|
---|
795 | LogRel2(("Audio: Host driver '%s' does not support audio device enumeration, skipping\n", pThis->BackendCfg.szName));
|
---|
796 | }
|
---|
797 |
|
---|
798 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
799 | LogFunc(("Returning %Rrc\n", rc));
|
---|
800 | return rc;
|
---|
801 | }
|
---|
802 | #endif /* VBOX_WITH_AUDIO_ENUM */
|
---|
803 |
|
---|
804 |
|
---|
805 | /*********************************************************************************************************************************
|
---|
806 | * PDMIAUDIOCONNECTOR *
|
---|
807 | *********************************************************************************************************************************/
|
---|
808 |
|
---|
809 | /**
|
---|
810 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnEnable}
|
---|
811 | */
|
---|
812 | static DECLCALLBACK(int) drvAudioEnable(PPDMIAUDIOCONNECTOR pInterface, PDMAUDIODIR enmDir, bool fEnable)
|
---|
813 | {
|
---|
814 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
815 | AssertPtr(pThis);
|
---|
816 | LogFlowFunc(("enmDir=%s fEnable=%d\n", PDMAudioDirGetName(enmDir), fEnable));
|
---|
817 |
|
---|
818 | /*
|
---|
819 | * Figure which status flag variable is being updated.
|
---|
820 | */
|
---|
821 | bool *pfEnabled;
|
---|
822 | if (enmDir == PDMAUDIODIR_IN)
|
---|
823 | pfEnabled = &pThis->In.fEnabled;
|
---|
824 | else if (enmDir == PDMAUDIODIR_OUT)
|
---|
825 | pfEnabled = &pThis->Out.fEnabled;
|
---|
826 | else
|
---|
827 | AssertFailedReturn(VERR_INVALID_PARAMETER);
|
---|
828 |
|
---|
829 | /*
|
---|
830 | * Grab the driver wide lock and check it. Ignore call if no change.
|
---|
831 | */
|
---|
832 | int rc = RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
833 | AssertRCReturn(rc, rc);
|
---|
834 |
|
---|
835 | if (fEnable != *pfEnabled)
|
---|
836 | {
|
---|
837 | LogRel(("Audio: %s %s for driver '%s'\n",
|
---|
838 | fEnable ? "Enabling" : "Disabling", enmDir == PDMAUDIODIR_IN ? "input" : "output", pThis->BackendCfg.szName));
|
---|
839 |
|
---|
840 | /*
|
---|
841 | * When enabling, we must update flag before calling drvAudioStreamControlInternalBackend.
|
---|
842 | */
|
---|
843 | if (fEnable)
|
---|
844 | *pfEnabled = true;
|
---|
845 |
|
---|
846 | /*
|
---|
847 | * Update the backend status for the streams in the given direction.
|
---|
848 | *
|
---|
849 | * The pThis->Out.fEnable / pThis->In.fEnable status flags only reflect in the
|
---|
850 | * direction of the backend, drivers and devices above us in the chain does not
|
---|
851 | * know about this. When disabled playback goes to /dev/null and we capture
|
---|
852 | * only silence. This means pStreamEx->fStatus holds the nominal status
|
---|
853 | * and we'll use it to restore the operation. (See also @bugref{9882}.)
|
---|
854 | */
|
---|
855 | PDRVAUDIOSTREAM pStreamEx;
|
---|
856 | RTListForEach(&pThis->LstStreams, pStreamEx, DRVAUDIOSTREAM, ListEntry)
|
---|
857 | {
|
---|
858 | if (pStreamEx->Core.Cfg.enmDir == enmDir)
|
---|
859 | {
|
---|
860 | /*
|
---|
861 | * When (re-)enabling a stream, clear the disabled warning bit again.
|
---|
862 | */
|
---|
863 | if (fEnable)
|
---|
864 | pStreamEx->Core.fWarningsShown &= ~PDMAUDIOSTREAM_WARN_FLAGS_DISABLED;
|
---|
865 |
|
---|
866 | /*
|
---|
867 | * We don't need to do anything unless the stream is enabled.
|
---|
868 | * Paused includes enabled, as does draining, but we only want the former.
|
---|
869 | */
|
---|
870 | uint32_t const fStatus = pStreamEx->fStatus;
|
---|
871 | if (fStatus & PDMAUDIOSTREAM_STS_ENABLED)
|
---|
872 | {
|
---|
873 | const char *pszOperation;
|
---|
874 | int rc2;
|
---|
875 | if (fEnable)
|
---|
876 | {
|
---|
877 | if (!(fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE))
|
---|
878 | {
|
---|
879 | /** @todo r=bird: We need to redo pre-buffering OR switch to
|
---|
880 | * DRVAUDIOPLAYSTATE_PREBUF_SWITCHING playback mode when disabling
|
---|
881 | * output streams. The former is preferred if associated with
|
---|
882 | * reporting the stream as INACTIVE. */
|
---|
883 | rc2 = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_ENABLE);
|
---|
884 | pszOperation = "enable";
|
---|
885 | if (RT_SUCCESS(rc2) && (fStatus & PDMAUDIOSTREAM_STS_PAUSED))
|
---|
886 | {
|
---|
887 | rc2 = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_PAUSE);
|
---|
888 | pszOperation = "pause";
|
---|
889 | }
|
---|
890 | }
|
---|
891 | else
|
---|
892 | {
|
---|
893 | rc2 = VINF_SUCCESS;
|
---|
894 | pszOperation = NULL;
|
---|
895 | }
|
---|
896 | }
|
---|
897 | else
|
---|
898 | {
|
---|
899 | rc2 = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
900 | pszOperation = "disable";
|
---|
901 | }
|
---|
902 | if (RT_FAILURE(rc2))
|
---|
903 | {
|
---|
904 | LogRel(("Audio: Failed to %s %s stream '%s': %Rrc\n",
|
---|
905 | pszOperation, enmDir == PDMAUDIODIR_IN ? "input" : "output", pStreamEx->Core.Cfg.szName, rc2));
|
---|
906 | if (RT_SUCCESS(rc))
|
---|
907 | rc = rc2; /** @todo r=bird: This isn't entirely helpful to the caller since we'll update the status
|
---|
908 | * regardless of the status code we return. And anyway, there is nothing that can be done
|
---|
909 | * about individual stream by the caller... */
|
---|
910 | }
|
---|
911 | }
|
---|
912 | }
|
---|
913 | }
|
---|
914 |
|
---|
915 | /*
|
---|
916 | * When disabling, we must update the status flag after the
|
---|
917 | * drvAudioStreamControlInternalBackend(DISABLE) calls.
|
---|
918 | */
|
---|
919 | *pfEnabled = fEnable;
|
---|
920 | }
|
---|
921 |
|
---|
922 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
923 | LogFlowFuncLeaveRC(rc);
|
---|
924 | return rc;
|
---|
925 | }
|
---|
926 |
|
---|
927 |
|
---|
928 | /**
|
---|
929 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnIsEnabled}
|
---|
930 | */
|
---|
931 | static DECLCALLBACK(bool) drvAudioIsEnabled(PPDMIAUDIOCONNECTOR pInterface, PDMAUDIODIR enmDir)
|
---|
932 | {
|
---|
933 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
934 | AssertPtr(pThis);
|
---|
935 | int rc = RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
936 | AssertRCReturn(rc, false);
|
---|
937 |
|
---|
938 | bool fEnabled;
|
---|
939 | if (enmDir == PDMAUDIODIR_IN)
|
---|
940 | fEnabled = pThis->In.fEnabled;
|
---|
941 | else if (enmDir == PDMAUDIODIR_OUT)
|
---|
942 | fEnabled = pThis->Out.fEnabled;
|
---|
943 | else
|
---|
944 | AssertFailedStmt(fEnabled = false);
|
---|
945 |
|
---|
946 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
947 | return fEnabled;
|
---|
948 | }
|
---|
949 |
|
---|
950 |
|
---|
951 | /**
|
---|
952 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnGetConfig}
|
---|
953 | */
|
---|
954 | static DECLCALLBACK(int) drvAudioGetConfig(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOBACKENDCFG pCfg)
|
---|
955 | {
|
---|
956 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
957 | AssertPtr(pThis);
|
---|
958 | AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
|
---|
959 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
960 | AssertRCReturn(rc, rc);
|
---|
961 |
|
---|
962 | if (pThis->pHostDrvAudio)
|
---|
963 | rc = pThis->pHostDrvAudio->pfnGetConfig(pThis->pHostDrvAudio, pCfg);
|
---|
964 | else
|
---|
965 | rc = VERR_PDM_NO_ATTACHED_DRIVER;
|
---|
966 |
|
---|
967 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
968 | LogFlowFuncLeaveRC(rc);
|
---|
969 | return rc;
|
---|
970 | }
|
---|
971 |
|
---|
972 |
|
---|
973 | /**
|
---|
974 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnGetStatus}
|
---|
975 | */
|
---|
976 | static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvAudioGetStatus(PPDMIAUDIOCONNECTOR pInterface, PDMAUDIODIR enmDir)
|
---|
977 | {
|
---|
978 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
979 | AssertPtr(pThis);
|
---|
980 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
981 | AssertRCReturn(rc, PDMAUDIOBACKENDSTS_UNKNOWN);
|
---|
982 |
|
---|
983 | PDMAUDIOBACKENDSTS fBackendStatus;
|
---|
984 | if (pThis->pHostDrvAudio)
|
---|
985 | {
|
---|
986 | if (pThis->pHostDrvAudio->pfnGetStatus)
|
---|
987 | fBackendStatus = pThis->pHostDrvAudio->pfnGetStatus(pThis->pHostDrvAudio, enmDir);
|
---|
988 | else
|
---|
989 | fBackendStatus = PDMAUDIOBACKENDSTS_UNKNOWN;
|
---|
990 | }
|
---|
991 | else
|
---|
992 | fBackendStatus = PDMAUDIOBACKENDSTS_NOT_ATTACHED;
|
---|
993 |
|
---|
994 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
995 | LogFlowFunc(("LEAVE - %#x\n", fBackendStatus));
|
---|
996 | return fBackendStatus;
|
---|
997 | }
|
---|
998 |
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * Frees an audio stream and its allocated resources.
|
---|
1002 | *
|
---|
1003 | * @param pStreamEx Audio stream to free. After this call the pointer will
|
---|
1004 | * not be valid anymore.
|
---|
1005 | */
|
---|
1006 | static void drvAudioStreamFree(PDRVAUDIOSTREAM pStreamEx)
|
---|
1007 | {
|
---|
1008 | if (pStreamEx)
|
---|
1009 | {
|
---|
1010 | LogFunc(("[%s]\n", pStreamEx->Core.Cfg.szName));
|
---|
1011 | Assert(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC);
|
---|
1012 | Assert(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC);
|
---|
1013 |
|
---|
1014 | pStreamEx->Core.uMagic = ~PDMAUDIOSTREAM_MAGIC;
|
---|
1015 | pStreamEx->pBackend = NULL;
|
---|
1016 | pStreamEx->uMagic = DRVAUDIOSTREAM_MAGIC_DEAD;
|
---|
1017 |
|
---|
1018 | RTCritSectDelete(&pStreamEx->Core.CritSect);
|
---|
1019 |
|
---|
1020 | RTMemFree(pStreamEx);
|
---|
1021 | }
|
---|
1022 | }
|
---|
1023 |
|
---|
1024 |
|
---|
1025 | /**
|
---|
1026 | * Adjusts the request stream configuration, applying our settings.
|
---|
1027 | *
|
---|
1028 | * This also does some basic validations.
|
---|
1029 | *
|
---|
1030 | * Used by both the stream creation and stream configuration hinting code.
|
---|
1031 | *
|
---|
1032 | * @returns VBox status code.
|
---|
1033 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
1034 | * @param pCfg The configuration that should be adjusted.
|
---|
1035 | * @param pszName Stream name to use when logging warnings and errors.
|
---|
1036 | */
|
---|
1037 | static int drvAudioStreamAdjustConfig(PCDRVAUDIO pThis, PPDMAUDIOSTREAMCFG pCfg, const char *pszName)
|
---|
1038 | {
|
---|
1039 | /* Get the right configuration for the stream to be created. */
|
---|
1040 | PCDRVAUDIOCFG pDrvCfg = pCfg->enmDir == PDMAUDIODIR_IN ? &pThis->CfgIn: &pThis->CfgOut;
|
---|
1041 |
|
---|
1042 | /* Fill in the tweakable parameters into the requested host configuration.
|
---|
1043 | * All parameters in principle can be changed and returned by the backend via the acquired configuration. */
|
---|
1044 |
|
---|
1045 | /*
|
---|
1046 | * PCM
|
---|
1047 | */
|
---|
1048 | if (PDMAudioPropsSampleSize(&pDrvCfg->Props) != 0) /* Anything set via custom extra-data? */
|
---|
1049 | {
|
---|
1050 | PDMAudioPropsSetSampleSize(&pCfg->Props, PDMAudioPropsSampleSize(&pDrvCfg->Props));
|
---|
1051 | LogRel2(("Audio: Using custom sample size of %RU8 bytes for stream '%s'\n",
|
---|
1052 | PDMAudioPropsSampleSize(&pCfg->Props), pszName));
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | if (pDrvCfg->Props.uHz) /* Anything set via custom extra-data? */
|
---|
1056 | {
|
---|
1057 | pCfg->Props.uHz = pDrvCfg->Props.uHz;
|
---|
1058 | LogRel2(("Audio: Using custom Hz rate %RU32 for stream '%s'\n", pCfg->Props.uHz, pszName));
|
---|
1059 | }
|
---|
1060 |
|
---|
1061 | if (pDrvCfg->uSigned != UINT8_MAX) /* Anything set via custom extra-data? */
|
---|
1062 | {
|
---|
1063 | pCfg->Props.fSigned = RT_BOOL(pDrvCfg->uSigned);
|
---|
1064 | LogRel2(("Audio: Using custom %s sample format for stream '%s'\n",
|
---|
1065 | pCfg->Props.fSigned ? "signed" : "unsigned", pszName));
|
---|
1066 | }
|
---|
1067 |
|
---|
1068 | if (pDrvCfg->uSwapEndian != UINT8_MAX) /* Anything set via custom extra-data? */
|
---|
1069 | {
|
---|
1070 | pCfg->Props.fSwapEndian = RT_BOOL(pDrvCfg->uSwapEndian);
|
---|
1071 | LogRel2(("Audio: Using custom %s endianess for samples of stream '%s'\n",
|
---|
1072 | pCfg->Props.fSwapEndian ? "swapped" : "original", pszName));
|
---|
1073 | }
|
---|
1074 |
|
---|
1075 | if (PDMAudioPropsChannels(&pDrvCfg->Props) != 0) /* Anything set via custom extra-data? */
|
---|
1076 | {
|
---|
1077 | PDMAudioPropsSetChannels(&pCfg->Props, PDMAudioPropsChannels(&pDrvCfg->Props));
|
---|
1078 | LogRel2(("Audio: Using custom %RU8 channel(s) for stream '%s'\n", PDMAudioPropsChannels(&pDrvCfg->Props), pszName));
|
---|
1079 | }
|
---|
1080 |
|
---|
1081 | /* Validate PCM properties. */
|
---|
1082 | if (!AudioHlpPcmPropsAreValid(&pCfg->Props))
|
---|
1083 | {
|
---|
1084 | LogRel(("Audio: Invalid custom PCM properties set for stream '%s', cannot create stream\n", pszName));
|
---|
1085 | return VERR_INVALID_PARAMETER;
|
---|
1086 | }
|
---|
1087 |
|
---|
1088 | /*
|
---|
1089 | * Period size
|
---|
1090 | */
|
---|
1091 | const char *pszWhat = "device-specific";
|
---|
1092 | if (pDrvCfg->uPeriodSizeMs)
|
---|
1093 | {
|
---|
1094 | pCfg->Backend.cFramesPeriod = PDMAudioPropsMilliToFrames(&pCfg->Props, pDrvCfg->uPeriodSizeMs);
|
---|
1095 | pszWhat = "custom";
|
---|
1096 | }
|
---|
1097 |
|
---|
1098 | if (!pCfg->Backend.cFramesPeriod) /* Set default period size if nothing explicitly is set. */
|
---|
1099 | {
|
---|
1100 | pCfg->Backend.cFramesPeriod = PDMAudioPropsMilliToFrames(&pCfg->Props, 150 /*ms*/);
|
---|
1101 | pszWhat = "default";
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | LogRel2(("Audio: Using %s period size %RU64 ms / %RU32 frames for stream '%s'\n",
|
---|
1105 | pszWhat, PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesPeriod),
|
---|
1106 | pCfg->Backend.cFramesPeriod, pszName));
|
---|
1107 |
|
---|
1108 | /*
|
---|
1109 | * Buffer size
|
---|
1110 | */
|
---|
1111 | pszWhat = "device-specific";
|
---|
1112 | if (pDrvCfg->uBufferSizeMs)
|
---|
1113 | {
|
---|
1114 | pCfg->Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(&pCfg->Props, pDrvCfg->uBufferSizeMs);
|
---|
1115 | pszWhat = "custom";
|
---|
1116 | }
|
---|
1117 |
|
---|
1118 | if (!pCfg->Backend.cFramesBufferSize) /* Set default buffer size if nothing explicitly is set. */
|
---|
1119 | {
|
---|
1120 | pCfg->Backend.cFramesBufferSize = PDMAudioPropsMilliToFrames(&pCfg->Props, 300 /*ms*/);
|
---|
1121 | pszWhat = "default";
|
---|
1122 | }
|
---|
1123 |
|
---|
1124 | LogRel2(("Audio: Using %s buffer size %RU64 ms / %RU32 frames for stream '%s'\n",
|
---|
1125 | pszWhat, PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesBufferSize),
|
---|
1126 | pCfg->Backend.cFramesBufferSize, pszName));
|
---|
1127 |
|
---|
1128 | /*
|
---|
1129 | * Pre-buffering size
|
---|
1130 | */
|
---|
1131 | pszWhat = "device-specific";
|
---|
1132 | if (pDrvCfg->uPreBufSizeMs != UINT32_MAX) /* Anything set via global / per-VM extra-data? */
|
---|
1133 | {
|
---|
1134 | pCfg->Backend.cFramesPreBuffering = PDMAudioPropsMilliToFrames(&pCfg->Props, pDrvCfg->uPreBufSizeMs);
|
---|
1135 | pszWhat = "custom";
|
---|
1136 | }
|
---|
1137 | else /* No, then either use the default or device-specific settings (if any). */
|
---|
1138 | {
|
---|
1139 | if (pCfg->Backend.cFramesPreBuffering == UINT32_MAX) /* Set default pre-buffering size if nothing explicitly is set. */
|
---|
1140 | {
|
---|
1141 | /* Pre-buffer 50% for both output & input. Capping both at 200ms.
|
---|
1142 | The 50% reasoning being that we need to have sufficient slack space
|
---|
1143 | in both directions as the guest DMA timer might be delayed by host
|
---|
1144 | scheduling as well as sped up afterwards because of TM catch-up. */
|
---|
1145 | uint32_t const cFramesMax = PDMAudioPropsMilliToFrames(&pCfg->Props, 200);
|
---|
1146 | pCfg->Backend.cFramesPreBuffering = pCfg->Backend.cFramesBufferSize / 2;
|
---|
1147 | pCfg->Backend.cFramesPreBuffering = RT_MIN(pCfg->Backend.cFramesPreBuffering, cFramesMax);
|
---|
1148 | pszWhat = "default";
|
---|
1149 | }
|
---|
1150 | }
|
---|
1151 |
|
---|
1152 | LogRel2(("Audio: Using %s pre-buffering size %RU64 ms / %RU32 frames for stream '%s'\n",
|
---|
1153 | pszWhat, PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesPreBuffering),
|
---|
1154 | pCfg->Backend.cFramesPreBuffering, pszName));
|
---|
1155 |
|
---|
1156 | /*
|
---|
1157 | * Validate input.
|
---|
1158 | */
|
---|
1159 | if (pCfg->Backend.cFramesBufferSize < pCfg->Backend.cFramesPeriod)
|
---|
1160 | {
|
---|
1161 | LogRel(("Audio: Error for stream '%s': Buffering size (%RU64ms) must not be smaller than the period size (%RU64ms)\n",
|
---|
1162 | pszName, PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesBufferSize),
|
---|
1163 | PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesPeriod)));
|
---|
1164 | return VERR_INVALID_PARAMETER;
|
---|
1165 | }
|
---|
1166 |
|
---|
1167 | if ( pCfg->Backend.cFramesPreBuffering != UINT32_MAX /* Custom pre-buffering set? */
|
---|
1168 | && pCfg->Backend.cFramesPreBuffering)
|
---|
1169 | {
|
---|
1170 | if (pCfg->Backend.cFramesBufferSize < pCfg->Backend.cFramesPreBuffering)
|
---|
1171 | {
|
---|
1172 | LogRel(("Audio: Error for stream '%s': Buffering size (%RU64ms) must not be smaller than the pre-buffering size (%RU64ms)\n",
|
---|
1173 | pszName, PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesPreBuffering),
|
---|
1174 | PDMAudioPropsFramesToMilli(&pCfg->Props, pCfg->Backend.cFramesBufferSize)));
|
---|
1175 | return VERR_INVALID_PARAMETER;
|
---|
1176 | }
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | return VINF_SUCCESS;
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 |
|
---|
1183 | /**
|
---|
1184 | * Worker thread function for drvAudioStreamConfigHint that's used when
|
---|
1185 | * PDMAUDIOBACKEND_F_ASYNC_HINT is in effect.
|
---|
1186 | */
|
---|
1187 | static DECLCALLBACK(void) drvAudioStreamConfigHintWorker(PDRVAUDIO pThis, PPDMAUDIOSTREAMCFG pCfg)
|
---|
1188 | {
|
---|
1189 | LogFlowFunc(("pThis=%p pCfg=%p\n", pThis, pCfg));
|
---|
1190 | AssertPtrReturnVoid(pCfg);
|
---|
1191 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1192 | AssertRCReturnVoid(rc);
|
---|
1193 |
|
---|
1194 | PPDMIHOSTAUDIO const pHostDrvAudio = pThis->pHostDrvAudio;
|
---|
1195 | if (pHostDrvAudio)
|
---|
1196 | {
|
---|
1197 | AssertPtr(pHostDrvAudio->pfnStreamConfigHint);
|
---|
1198 | if (pHostDrvAudio->pfnStreamConfigHint)
|
---|
1199 | pHostDrvAudio->pfnStreamConfigHint(pHostDrvAudio, pCfg);
|
---|
1200 | }
|
---|
1201 | PDMAudioStrmCfgFree(pCfg);
|
---|
1202 |
|
---|
1203 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1204 | LogFlowFunc(("returns\n"));
|
---|
1205 | }
|
---|
1206 |
|
---|
1207 |
|
---|
1208 | /**
|
---|
1209 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamConfigHint}
|
---|
1210 | */
|
---|
1211 | static DECLCALLBACK(void) drvAudioStreamConfigHint(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAMCFG pCfg)
|
---|
1212 | {
|
---|
1213 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
1214 | AssertReturnVoid(pCfg->enmDir == PDMAUDIODIR_IN || pCfg->enmDir == PDMAUDIODIR_OUT);
|
---|
1215 |
|
---|
1216 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1217 | AssertRCReturnVoid(rc);
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * Don't do anything unless the backend has a pfnStreamConfigHint method
|
---|
1221 | * and the direction is currently enabled.
|
---|
1222 | */
|
---|
1223 | if ( pThis->pHostDrvAudio
|
---|
1224 | && pThis->pHostDrvAudio->pfnStreamConfigHint)
|
---|
1225 | {
|
---|
1226 | if (pCfg->enmDir == PDMAUDIODIR_OUT ? pThis->Out.fEnabled : pThis->In.fEnabled)
|
---|
1227 | {
|
---|
1228 | /*
|
---|
1229 | * Adjust the configuration (applying out settings) then call the backend driver.
|
---|
1230 | */
|
---|
1231 | rc = drvAudioStreamAdjustConfig(pThis, pCfg, pCfg->szName);
|
---|
1232 | AssertLogRelRC(rc);
|
---|
1233 | if (RT_SUCCESS(rc))
|
---|
1234 | {
|
---|
1235 | rc = VERR_CALLBACK_RETURN;
|
---|
1236 | if (pThis->BackendCfg.fFlags & PDMAUDIOBACKEND_F_ASYNC_HINT)
|
---|
1237 | {
|
---|
1238 | PPDMAUDIOSTREAMCFG pDupCfg = PDMAudioStrmCfgDup(pCfg);
|
---|
1239 | if (pDupCfg)
|
---|
1240 | {
|
---|
1241 | rc = RTReqPoolCallVoidNoWait(pThis->hReqPool, (PFNRT)drvAudioStreamConfigHintWorker, 2, pThis, pDupCfg);
|
---|
1242 | if (RT_SUCCESS(rc))
|
---|
1243 | LogFlowFunc(("Asynchronous call running on worker thread.\n"));
|
---|
1244 | else
|
---|
1245 | PDMAudioStrmCfgFree(pDupCfg);
|
---|
1246 | }
|
---|
1247 | }
|
---|
1248 | if (RT_FAILURE_NP(rc))
|
---|
1249 | {
|
---|
1250 | LogFlowFunc(("Doing synchronous call...\n"));
|
---|
1251 | pThis->pHostDrvAudio->pfnStreamConfigHint(pThis->pHostDrvAudio, pCfg);
|
---|
1252 | }
|
---|
1253 | }
|
---|
1254 | }
|
---|
1255 | else
|
---|
1256 | LogFunc(("Ignoring hint because direction is not currently enabled\n"));
|
---|
1257 | }
|
---|
1258 | else
|
---|
1259 | LogFlowFunc(("Ignoring hint because backend has no pfnStreamConfigHint method.\n"));
|
---|
1260 |
|
---|
1261 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1262 | }
|
---|
1263 |
|
---|
1264 |
|
---|
1265 | /**
|
---|
1266 | * Common worker for synchronizing the ENABLED and PAUSED status bits with the
|
---|
1267 | * backend after it becomes ready.
|
---|
1268 | *
|
---|
1269 | * Used by async init and re-init.
|
---|
1270 | *
|
---|
1271 | * @note Is sometimes called w/o having entered DRVAUDIO::CritSectHotPlug.
|
---|
1272 | * Caller must however own the stream critsect.
|
---|
1273 | */
|
---|
1274 | static int drvAudioStreamUpdateBackendOnStatus(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, const char *pszWhen)
|
---|
1275 | {
|
---|
1276 | int rc = VINF_SUCCESS;
|
---|
1277 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_ENABLED)
|
---|
1278 | {
|
---|
1279 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_ENABLE);
|
---|
1280 | if (RT_SUCCESS(rc))
|
---|
1281 | {
|
---|
1282 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PAUSED)
|
---|
1283 | {
|
---|
1284 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_PAUSE);
|
---|
1285 | if (RT_FAILURE(rc))
|
---|
1286 | LogRelMax(64, ("Audio: Failed to pause stream '%s' after %s: %Rrc\n", pStreamEx->Core.Cfg.szName, pszWhen, rc));
|
---|
1287 | }
|
---|
1288 | }
|
---|
1289 | else
|
---|
1290 | LogRelMax(64, ("Audio: Failed to enable stream '%s' after %s: %Rrc\n", pStreamEx->Core.Cfg.szName, pszWhen, rc));
|
---|
1291 | }
|
---|
1292 | return rc;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 |
|
---|
1296 | /**
|
---|
1297 | * For performing PDMIHOSTAUDIO::pfnStreamInitAsync on a worker thread.
|
---|
1298 | *
|
---|
1299 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
1300 | * @param pStreamEx The stream. One reference for us to release.
|
---|
1301 | */
|
---|
1302 | static DECLCALLBACK(void) drvAudioStreamInitAsync(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
1303 | {
|
---|
1304 | LogFlow(("pThis=%p pStreamEx=%p (%s)\n", pThis, pStreamEx, pStreamEx->Core.Cfg.szName));
|
---|
1305 |
|
---|
1306 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1307 | AssertRCReturnVoid(rc);
|
---|
1308 |
|
---|
1309 | /*
|
---|
1310 | * Do the init job.
|
---|
1311 | */
|
---|
1312 | bool fDestroyed;
|
---|
1313 | PPDMIHOSTAUDIO pIHostDrvAudio = pThis->pHostDrvAudio;
|
---|
1314 | AssertPtr(pIHostDrvAudio);
|
---|
1315 | if (pIHostDrvAudio && pIHostDrvAudio->pfnStreamInitAsync)
|
---|
1316 | {
|
---|
1317 | fDestroyed = pStreamEx->cRefs <= 1;
|
---|
1318 | rc = pIHostDrvAudio->pfnStreamInitAsync(pIHostDrvAudio, pStreamEx->pBackend, fDestroyed);
|
---|
1319 | LogFlow(("pfnStreamInitAsync returns %Rrc (on %p, fDestroyed=%d)\n", rc, pStreamEx, fDestroyed));
|
---|
1320 | }
|
---|
1321 | else
|
---|
1322 | {
|
---|
1323 | fDestroyed = true;
|
---|
1324 | rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1328 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
1329 |
|
---|
1330 | /*
|
---|
1331 | * On success, update the backend on the stream status and mark it ready for business.
|
---|
1332 | */
|
---|
1333 | if (RT_SUCCESS(rc) && !fDestroyed)
|
---|
1334 | {
|
---|
1335 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1336 |
|
---|
1337 | /*
|
---|
1338 | * Update the backend state.
|
---|
1339 | */
|
---|
1340 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_BACKEND_READY; /* before the backend control call! */
|
---|
1341 |
|
---|
1342 | rc = drvAudioStreamUpdateBackendOnStatus(pThis, pStreamEx, "asynchronous initialization completed");
|
---|
1343 |
|
---|
1344 | /*
|
---|
1345 | * Modify the play state if output stream.
|
---|
1346 | */
|
---|
1347 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
1348 | {
|
---|
1349 | DRVAUDIOPLAYSTATE const enmPlayState = pStreamEx->Out.enmPlayState;
|
---|
1350 | switch (enmPlayState)
|
---|
1351 | {
|
---|
1352 | case DRVAUDIOPLAYSTATE_PREBUF:
|
---|
1353 | case DRVAUDIOPLAYSTATE_PREBUF_SWITCHING:
|
---|
1354 | break;
|
---|
1355 | case DRVAUDIOPLAYSTATE_PREBUF_OVERDUE:
|
---|
1356 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF_COMMITTING;
|
---|
1357 | break;
|
---|
1358 | case DRVAUDIOPLAYSTATE_NOPLAY:
|
---|
1359 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF;
|
---|
1360 | break;
|
---|
1361 | case DRVAUDIOPLAYSTATE_PLAY:
|
---|
1362 | case DRVAUDIOPLAYSTATE_PLAY_PREBUF:
|
---|
1363 | case DRVAUDIOPLAYSTATE_PREBUF_COMMITTING:
|
---|
1364 | AssertFailedBreak();
|
---|
1365 | /* no default */
|
---|
1366 | case DRVAUDIOPLAYSTATE_END:
|
---|
1367 | case DRVAUDIOPLAYSTATE_INVALID:
|
---|
1368 | break;
|
---|
1369 | }
|
---|
1370 | LogFunc(("enmPlayState: %s -> %s\n", drvAudioPlayStateName(enmPlayState),
|
---|
1371 | drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
1372 | }
|
---|
1373 |
|
---|
1374 | /*
|
---|
1375 | * Update the last backend state.
|
---|
1376 | */
|
---|
1377 | pStreamEx->enmLastBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
1378 |
|
---|
1379 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1380 | }
|
---|
1381 | /*
|
---|
1382 | * Don't quite know what to do on failure...
|
---|
1383 | */
|
---|
1384 | else if (!fDestroyed)
|
---|
1385 | {
|
---|
1386 | LogRelMax(64, ("Audio: Failed to initialize stream '%s': %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
1387 | }
|
---|
1388 |
|
---|
1389 | /*
|
---|
1390 | * Release the request handle, must be done while inside the critical section.
|
---|
1391 | */
|
---|
1392 | if (pStreamEx->hReqInitAsync != NIL_RTREQ)
|
---|
1393 | {
|
---|
1394 | LogFlowFunc(("Releasing hReqInitAsync=%p\n", pStreamEx->hReqInitAsync));
|
---|
1395 | RTReqRelease(pStreamEx->hReqInitAsync);
|
---|
1396 | pStreamEx->hReqInitAsync = NIL_RTREQ;
|
---|
1397 | }
|
---|
1398 |
|
---|
1399 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
1400 |
|
---|
1401 | /*
|
---|
1402 | * Release our stream reference.
|
---|
1403 | */
|
---|
1404 | uint32_t cRefs = drvAudioStreamReleaseInternal(pThis, pStreamEx, true /*fMayDestroy*/);
|
---|
1405 | LogFlowFunc(("returns (fDestroyed=%d, cRefs=%u)\n", fDestroyed, cRefs)); RT_NOREF(cRefs);
|
---|
1406 | }
|
---|
1407 |
|
---|
1408 |
|
---|
1409 | /**
|
---|
1410 | * Worker for drvAudioStreamInitInternal and drvAudioStreamReInitInternal that
|
---|
1411 | * creates the backend (host driver) side of an audio stream.
|
---|
1412 | *
|
---|
1413 | * @returns VBox status code.
|
---|
1414 | * @param pThis Pointer to driver instance.
|
---|
1415 | * @param pStreamEx Stream to create backend for. The Core.Cfg field
|
---|
1416 | * contains the requested configuration when we're called
|
---|
1417 | * and the actual configuration when successfully
|
---|
1418 | * returning.
|
---|
1419 | *
|
---|
1420 | * @note Configuration precedence for requested audio stream configuration (first has highest priority, if set):
|
---|
1421 | * - per global extra-data
|
---|
1422 | * - per-VM extra-data
|
---|
1423 | * - requested configuration (by pCfgReq)
|
---|
1424 | * - default value
|
---|
1425 | */
|
---|
1426 | static int drvAudioStreamCreateInternalBackend(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
1427 | {
|
---|
1428 | AssertMsg((pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_CREATED) == 0,
|
---|
1429 | ("Stream '%s' already initialized in backend\n", pStreamEx->Core.Cfg.szName));
|
---|
1430 |
|
---|
1431 | /*
|
---|
1432 | * Adjust the stream config, applying defaults and any overriding settings.
|
---|
1433 | */
|
---|
1434 | int rc = drvAudioStreamAdjustConfig(pThis, &pStreamEx->Core.Cfg, pStreamEx->Core.Cfg.szName);
|
---|
1435 | if (RT_FAILURE(rc))
|
---|
1436 | return rc;
|
---|
1437 | PDMAUDIOSTREAMCFG const CfgReq = pStreamEx->Core.Cfg;
|
---|
1438 |
|
---|
1439 | /*
|
---|
1440 | * Call the host driver to create the stream.
|
---|
1441 | */
|
---|
1442 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1443 |
|
---|
1444 | AssertLogRelMsgStmt(RT_VALID_PTR(pThis->pHostDrvAudio),
|
---|
1445 | ("Audio: %p\n", pThis->pHostDrvAudio), rc = VERR_PDM_NO_ATTACHED_DRIVER);
|
---|
1446 | if (RT_SUCCESS(rc))
|
---|
1447 | AssertLogRelMsgStmt(pStreamEx->Core.cbBackend == pThis->BackendCfg.cbStream,
|
---|
1448 | ("Audio: Backend changed? cbBackend changed from %#x to %#x\n",
|
---|
1449 | pStreamEx->Core.cbBackend, pThis->BackendCfg.cbStream),
|
---|
1450 | rc = VERR_STATE_CHANGED);
|
---|
1451 | if (RT_SUCCESS(rc))
|
---|
1452 | rc = pThis->pHostDrvAudio->pfnStreamCreate(pThis->pHostDrvAudio, pStreamEx->pBackend, &CfgReq, &pStreamEx->Core.Cfg);
|
---|
1453 | if (RT_SUCCESS(rc))
|
---|
1454 | {
|
---|
1455 | pStreamEx->enmLastBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
1456 |
|
---|
1457 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1458 |
|
---|
1459 | AssertLogRelReturn(pStreamEx->pBackend->uMagic == PDMAUDIOBACKENDSTREAM_MAGIC, VERR_INTERNAL_ERROR_3);
|
---|
1460 | AssertLogRelReturn(pStreamEx->pBackend->pStream == &pStreamEx->Core, VERR_INTERNAL_ERROR_3);
|
---|
1461 |
|
---|
1462 | /* Must set the backend-initialized flag now or the backend won't be
|
---|
1463 | destroyed (this used to be done at the end of this function, with
|
---|
1464 | several possible early return paths before it). */
|
---|
1465 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_BACKEND_CREATED;
|
---|
1466 | }
|
---|
1467 | else
|
---|
1468 | {
|
---|
1469 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1470 | if (rc == VERR_NOT_SUPPORTED)
|
---|
1471 | LogRel2(("Audio: Creating stream '%s' in backend not supported\n", pStreamEx->Core.Cfg.szName));
|
---|
1472 | else if (rc == VERR_AUDIO_STREAM_COULD_NOT_CREATE)
|
---|
1473 | LogRel2(("Audio: Stream '%s' could not be created in backend because of missing hardware / drivers\n",
|
---|
1474 | pStreamEx->Core.Cfg.szName));
|
---|
1475 | else
|
---|
1476 | LogRel(("Audio: Creating stream '%s' in backend failed with %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
1477 | return rc;
|
---|
1478 | }
|
---|
1479 |
|
---|
1480 | /* Remember if we need to call pfnStreamInitAsync. */
|
---|
1481 | pStreamEx->fNeedAsyncInit = rc == VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED;
|
---|
1482 | AssertStmt(rc != VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED || pThis->pHostDrvAudio->pfnStreamInitAsync != NULL,
|
---|
1483 | pStreamEx->fNeedAsyncInit = false);
|
---|
1484 | AssertMsg( rc != VINF_AUDIO_STREAM_ASYNC_INIT_NEEDED
|
---|
1485 | || pStreamEx->enmLastBackendState == PDMHOSTAUDIOSTREAMSTATE_INITIALIZING,
|
---|
1486 | ("rc=%Rrc %s\n", rc, PDMHostAudioStreamStateGetName(pStreamEx->enmLastBackendState)));
|
---|
1487 |
|
---|
1488 | PPDMAUDIOSTREAMCFG const pCfgAcq = &pStreamEx->Core.Cfg;
|
---|
1489 |
|
---|
1490 | /*
|
---|
1491 | * Validate acquired configuration.
|
---|
1492 | */
|
---|
1493 | char szTmp[PDMAUDIOPROPSTOSTRING_MAX];
|
---|
1494 | LogFunc(("Backend returned: %s\n", PDMAudioStrmCfgToString(pCfgAcq, szTmp, sizeof(szTmp)) ));
|
---|
1495 | AssertLogRelMsgReturn(AudioHlpStreamCfgIsValid(pCfgAcq),
|
---|
1496 | ("Audio: Creating stream '%s' returned an invalid backend configuration (%s), skipping\n",
|
---|
1497 | pCfgAcq->szName, PDMAudioPropsToString(&pCfgAcq->Props, szTmp, sizeof(szTmp))),
|
---|
1498 | VERR_INVALID_PARAMETER);
|
---|
1499 |
|
---|
1500 | /* Let the user know that the backend changed one of the values requested above. */
|
---|
1501 | if (pCfgAcq->Backend.cFramesBufferSize != CfgReq.Backend.cFramesBufferSize)
|
---|
1502 | LogRel2(("Audio: Backend changed buffer size from %RU64ms (%RU32 frames) to %RU64ms (%RU32 frames)\n",
|
---|
1503 | PDMAudioPropsFramesToMilli(&CfgReq.Props, CfgReq.Backend.cFramesBufferSize), CfgReq.Backend.cFramesBufferSize,
|
---|
1504 | PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesBufferSize), pCfgAcq->Backend.cFramesBufferSize));
|
---|
1505 |
|
---|
1506 | if (pCfgAcq->Backend.cFramesPeriod != CfgReq.Backend.cFramesPeriod)
|
---|
1507 | LogRel2(("Audio: Backend changed period size from %RU64ms (%RU32 frames) to %RU64ms (%RU32 frames)\n",
|
---|
1508 | PDMAudioPropsFramesToMilli(&CfgReq.Props, CfgReq.Backend.cFramesPeriod), CfgReq.Backend.cFramesPeriod,
|
---|
1509 | PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesPeriod), pCfgAcq->Backend.cFramesPeriod));
|
---|
1510 |
|
---|
1511 | /* Was pre-buffering requested, but the acquired configuration from the backend told us something else? */
|
---|
1512 | if (CfgReq.Backend.cFramesPreBuffering)
|
---|
1513 | {
|
---|
1514 | if (pCfgAcq->Backend.cFramesPreBuffering != CfgReq.Backend.cFramesPreBuffering)
|
---|
1515 | LogRel2(("Audio: Backend changed pre-buffering size from %RU64ms (%RU32 frames) to %RU64ms (%RU32 frames)\n",
|
---|
1516 | PDMAudioPropsFramesToMilli(&CfgReq.Props, CfgReq.Backend.cFramesPreBuffering), CfgReq.Backend.cFramesPreBuffering,
|
---|
1517 | PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesPreBuffering), pCfgAcq->Backend.cFramesPreBuffering));
|
---|
1518 |
|
---|
1519 | if (pCfgAcq->Backend.cFramesPreBuffering > pCfgAcq->Backend.cFramesBufferSize)
|
---|
1520 | {
|
---|
1521 | pCfgAcq->Backend.cFramesPreBuffering = pCfgAcq->Backend.cFramesBufferSize;
|
---|
1522 | LogRel2(("Audio: Pre-buffering size bigger than buffer size for stream '%s', adjusting to %RU64ms (%RU32 frames)\n", pCfgAcq->szName,
|
---|
1523 | PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesPreBuffering), pCfgAcq->Backend.cFramesPreBuffering));
|
---|
1524 | }
|
---|
1525 | }
|
---|
1526 | else if (CfgReq.Backend.cFramesPreBuffering == 0) /* Was the pre-buffering requested as being disabeld? Tell the users. */
|
---|
1527 | {
|
---|
1528 | LogRel2(("Audio: Pre-buffering is disabled for stream '%s'\n", pCfgAcq->szName));
|
---|
1529 | pCfgAcq->Backend.cFramesPreBuffering = 0;
|
---|
1530 | }
|
---|
1531 |
|
---|
1532 | /* Sanity for detecting buggy backends. */
|
---|
1533 | AssertMsgReturn(pCfgAcq->Backend.cFramesPeriod < pCfgAcq->Backend.cFramesBufferSize,
|
---|
1534 | ("Acquired period size must be smaller than buffer size\n"),
|
---|
1535 | VERR_INVALID_PARAMETER);
|
---|
1536 | AssertMsgReturn(pCfgAcq->Backend.cFramesPreBuffering <= pCfgAcq->Backend.cFramesBufferSize,
|
---|
1537 | ("Acquired pre-buffering size must be smaller or as big as the buffer size\n"),
|
---|
1538 | VERR_INVALID_PARAMETER);
|
---|
1539 |
|
---|
1540 | /*
|
---|
1541 | * Check if the backend did return sane values and correct if necessary.
|
---|
1542 | * Should never happen with our own backends, but you never know ...
|
---|
1543 | */
|
---|
1544 | uint32_t const cFramesPreBufferingMax = pCfgAcq->Backend.cFramesBufferSize - RT_MIN(16, pCfgAcq->Backend.cFramesBufferSize);
|
---|
1545 | if (pCfgAcq->Backend.cFramesPreBuffering > cFramesPreBufferingMax)
|
---|
1546 | {
|
---|
1547 | LogRel2(("Audio: Warning: Pre-buffering size of %RU32 frames for stream '%s' is too close to or larger than the %RU32 frames buffer size, reducing it to %RU32 frames!\n",
|
---|
1548 | pCfgAcq->Backend.cFramesPreBuffering, pCfgAcq->szName, pCfgAcq->Backend.cFramesBufferSize, cFramesPreBufferingMax));
|
---|
1549 | AssertFailed();
|
---|
1550 | pCfgAcq->Backend.cFramesPreBuffering = cFramesPreBufferingMax;
|
---|
1551 | }
|
---|
1552 |
|
---|
1553 | if (pCfgAcq->Backend.cFramesPeriod > pCfgAcq->Backend.cFramesBufferSize)
|
---|
1554 | {
|
---|
1555 | LogRel2(("Audio: Warning: Period size of %RU32 frames for stream '%s' is larger than the %RU32 frames buffer size, reducing it to %RU32 frames!\n",
|
---|
1556 | pCfgAcq->Backend.cFramesPeriod, pCfgAcq->szName, pCfgAcq->Backend.cFramesBufferSize, pCfgAcq->Backend.cFramesBufferSize / 2));
|
---|
1557 | AssertFailed();
|
---|
1558 | pCfgAcq->Backend.cFramesPeriod = pCfgAcq->Backend.cFramesBufferSize / 2;
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | LogRel2(("Audio: Buffer size for stream '%s' is %RU64 ms / %RU32 frames\n", pCfgAcq->szName,
|
---|
1562 | PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesBufferSize), pCfgAcq->Backend.cFramesBufferSize));
|
---|
1563 | LogRel2(("Audio: Pre-buffering size for stream '%s' is %RU64 ms / %RU32 frames\n", pCfgAcq->szName,
|
---|
1564 | PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesPreBuffering), pCfgAcq->Backend.cFramesPreBuffering));
|
---|
1565 | LogRel2(("Audio: Scheduling hint for stream '%s' is %RU32ms / %RU32 frames\n", pCfgAcq->szName,
|
---|
1566 | pCfgAcq->Device.cMsSchedulingHint, PDMAudioPropsMilliToFrames(&pCfgAcq->Props, pCfgAcq->Device.cMsSchedulingHint)));
|
---|
1567 |
|
---|
1568 | /* Make sure the configured buffer size by the backend at least can hold the configured latency. */
|
---|
1569 | uint32_t const cMsPeriod = PDMAudioPropsFramesToMilli(&pCfgAcq->Props, pCfgAcq->Backend.cFramesPeriod);
|
---|
1570 | LogRel2(("Audio: Period size of stream '%s' is %RU64 ms / %RU32 frames\n",
|
---|
1571 | pCfgAcq->szName, cMsPeriod, pCfgAcq->Backend.cFramesPeriod));
|
---|
1572 | /** @todo r=bird: This is probably a misleading/harmless warning as we'd just
|
---|
1573 | * have to transfer more each time we move data. The period is generally
|
---|
1574 | * pure irrelevant fiction anyway. A more relevant comparison would
|
---|
1575 | * be to half the buffer size, i.e. making sure we get scheduled often
|
---|
1576 | * enough to keep the buffer at least half full (probably more
|
---|
1577 | * sensible if the buffer size was more than 2x scheduling periods). */
|
---|
1578 | if ( CfgReq.Device.cMsSchedulingHint /* Any scheduling hint set? */
|
---|
1579 | && CfgReq.Device.cMsSchedulingHint > cMsPeriod) /* This might lead to buffer underflows. */
|
---|
1580 | LogRel(("Audio: Warning: Scheduling hint of stream '%s' is bigger (%RU64ms) than used period size (%RU64ms)\n",
|
---|
1581 | pCfgAcq->szName, CfgReq.Device.cMsSchedulingHint, cMsPeriod));
|
---|
1582 |
|
---|
1583 | /*
|
---|
1584 | * Done, just log the result:
|
---|
1585 | */
|
---|
1586 | LogFunc(("[%s] Acquired format: %s\n", pCfgAcq->szName,
|
---|
1587 | PDMAudioStrmCfgToString(&pStreamEx->Core.Cfg, szTmp, sizeof(szTmp)) ));
|
---|
1588 | LogRel2(("Audio: Acquired format: %s\n", PDMAudioStrmCfgToString(&pStreamEx->Core.Cfg, szTmp, sizeof(szTmp)) ));
|
---|
1589 |
|
---|
1590 | return VINF_SUCCESS;
|
---|
1591 | }
|
---|
1592 |
|
---|
1593 |
|
---|
1594 | /**
|
---|
1595 | * Worker for drvAudioStreamCreate that initializes the audio stream.
|
---|
1596 | *
|
---|
1597 | * @returns VBox status code.
|
---|
1598 | * @param pThis Pointer to driver instance.
|
---|
1599 | * @param pStreamEx Stream to initialize. Caller already set a few fields.
|
---|
1600 | * The Core.Cfg field contains the requested configuration
|
---|
1601 | * when we're called and the actual configuration when
|
---|
1602 | * successfully returning.
|
---|
1603 | */
|
---|
1604 | static int drvAudioStreamInitInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
1605 | {
|
---|
1606 | /*
|
---|
1607 | * Init host stream.
|
---|
1608 | */
|
---|
1609 | pStreamEx->Core.uMagic = PDMAUDIOSTREAM_MAGIC;
|
---|
1610 |
|
---|
1611 | char szTmp[PDMAUDIOSTRMCFGTOSTRING_MAX];
|
---|
1612 | LogFunc(("[%s] Requested host format: %s\n", pStreamEx->Core.Cfg.szName,
|
---|
1613 | PDMAudioStrmCfgToString(&pStreamEx->Core.Cfg, szTmp, sizeof(szTmp)) ));
|
---|
1614 | LogRel2(("Audio: Creating stream: %s\n", PDMAudioStrmCfgToString(&pStreamEx->Core.Cfg, szTmp, sizeof(szTmp))));
|
---|
1615 |
|
---|
1616 | int rc = drvAudioStreamCreateInternalBackend(pThis, pStreamEx);
|
---|
1617 | if (RT_FAILURE(rc))
|
---|
1618 | return rc;
|
---|
1619 |
|
---|
1620 | /*
|
---|
1621 | * Configure host buffers.
|
---|
1622 | */
|
---|
1623 | Assert(pStreamEx->cbPreBufThreshold == 0);
|
---|
1624 | if (pStreamEx->Core.Cfg.Backend.cFramesPreBuffering != 0)
|
---|
1625 | pStreamEx->cbPreBufThreshold = PDMAudioPropsFramesToBytes(&pStreamEx->Core.Cfg.Props,
|
---|
1626 | pStreamEx->Core.Cfg.Backend.cFramesPreBuffering);
|
---|
1627 |
|
---|
1628 | /* Allocate space for pre-buffering of output stream w/o mixing buffers. */
|
---|
1629 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
1630 | {
|
---|
1631 | Assert(pStreamEx->Out.cbPreBufAlloc == 0);
|
---|
1632 | Assert(pStreamEx->Out.cbPreBuffered == 0);
|
---|
1633 | Assert(pStreamEx->Out.offPreBuf == 0);
|
---|
1634 | if (pStreamEx->Core.Cfg.Backend.cFramesPreBuffering != 0)
|
---|
1635 | {
|
---|
1636 | pStreamEx->Out.cbPreBufAlloc = PDMAudioPropsFramesToBytes(&pStreamEx->Core.Cfg.Props,
|
---|
1637 | pStreamEx->Core.Cfg.Backend.cFramesBufferSize - 2);
|
---|
1638 | pStreamEx->Out.cbPreBufAlloc = RT_MIN(RT_ALIGN_32(pStreamEx->cbPreBufThreshold + _8K, _4K),
|
---|
1639 | pStreamEx->Out.cbPreBufAlloc);
|
---|
1640 | pStreamEx->Out.pbPreBuf = (uint8_t *)RTMemAllocZ(pStreamEx->Out.cbPreBufAlloc);
|
---|
1641 | AssertReturn(pStreamEx->Out.pbPreBuf, VERR_NO_MEMORY);
|
---|
1642 | }
|
---|
1643 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_NOPLAY; /* Changed upon enable. */
|
---|
1644 | }
|
---|
1645 |
|
---|
1646 | /*
|
---|
1647 | * Register statistics.
|
---|
1648 | */
|
---|
1649 | PPDMDRVINS const pDrvIns = pThis->pDrvIns;
|
---|
1650 | /** @todo expose config and more. */
|
---|
1651 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pStreamEx->Core.Cfg.Backend.cFramesBufferSize, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_NONE,
|
---|
1652 | "Host side: The size of the backend buffer (in frames)", "%s/0-HostBackendBufSize", pStreamEx->Core.Cfg.szName);
|
---|
1653 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
1654 | {
|
---|
1655 | /** @todo later? */
|
---|
1656 | }
|
---|
1657 | else
|
---|
1658 | {
|
---|
1659 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pStreamEx->Out.Stats.cbBackendWritableBefore, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_NONE,
|
---|
1660 | "Host side: Free space in backend buffer before play", "%s/0-HostBackendBufFreeBefore", pStreamEx->Core.Cfg.szName);
|
---|
1661 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pStreamEx->Out.Stats.cbBackendWritableAfter, STAMTYPE_U32, STAMVISIBILITY_USED, STAMUNIT_NONE,
|
---|
1662 | "Host side: Free space in backend buffer after play", "%s/0-HostBackendBufFreeAfter", pStreamEx->Core.Cfg.szName);
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | #ifdef VBOX_WITH_STATISTICS
|
---|
1666 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
1667 | {
|
---|
1668 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pStreamEx->In.Stats.TotalFramesCaptured, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_NONE,
|
---|
1669 | "Total frames played.", "%s/TotalFramesCaptured", pStreamEx->Core.Cfg.szName);
|
---|
1670 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pStreamEx->In.Stats.TotalTimesCaptured, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_NONE,
|
---|
1671 | "Total number of playbacks.", "%s/TotalTimesCaptured", pStreamEx->Core.Cfg.szName);
|
---|
1672 | PDMDrvHlpSTAMRegisterF(pDrvIns, &pStreamEx->In.Stats.TotalTimesRead, STAMTYPE_COUNTER, STAMVISIBILITY_USED, STAMUNIT_NONE,
|
---|
1673 | "Total number of reads.", "%s/TotalTimesRead", pStreamEx->Core.Cfg.szName);
|
---|
1674 | }
|
---|
1675 | else
|
---|
1676 | {
|
---|
1677 | Assert(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT);
|
---|
1678 | }
|
---|
1679 | #endif /* VBOX_WITH_STATISTICS */
|
---|
1680 |
|
---|
1681 | LogFlowFunc(("[%s] Returning %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
1682 | return rc;
|
---|
1683 | }
|
---|
1684 |
|
---|
1685 |
|
---|
1686 | /**
|
---|
1687 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamCreate}
|
---|
1688 | */
|
---|
1689 | static DECLCALLBACK(int) drvAudioStreamCreate(PPDMIAUDIOCONNECTOR pInterface, uint32_t fFlags, PCPDMAUDIOSTREAMCFG pCfgReq,
|
---|
1690 | PPDMAUDIOSTREAM *ppStream)
|
---|
1691 | {
|
---|
1692 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
1693 | AssertPtr(pThis);
|
---|
1694 |
|
---|
1695 | /*
|
---|
1696 | * Assert sanity.
|
---|
1697 | */
|
---|
1698 | AssertReturn(!(fFlags & ~PDMAUDIOSTREAM_CREATE_F_NO_MIXBUF), VERR_INVALID_FLAGS);
|
---|
1699 | AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
|
---|
1700 | AssertPtrReturn(ppStream, VERR_INVALID_POINTER);
|
---|
1701 | *ppStream = NULL;
|
---|
1702 | LogFlowFunc(("pCfgReq=%s\n", pCfgReq->szName));
|
---|
1703 | #ifdef LOG_ENABLED
|
---|
1704 | PDMAudioStrmCfgLog(pCfgReq);
|
---|
1705 | #endif
|
---|
1706 | AssertReturn(AudioHlpStreamCfgIsValid(pCfgReq), VERR_INVALID_PARAMETER);
|
---|
1707 | AssertReturn(pCfgReq->enmDir == PDMAUDIODIR_IN || pCfgReq->enmDir == PDMAUDIODIR_OUT, VERR_NOT_SUPPORTED);
|
---|
1708 |
|
---|
1709 | /*
|
---|
1710 | * Grab a free stream count now.
|
---|
1711 | */
|
---|
1712 | int rc = RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
1713 | AssertRCReturn(rc, rc);
|
---|
1714 |
|
---|
1715 | uint32_t * const pcFreeStreams = pCfgReq->enmDir == PDMAUDIODIR_IN ? &pThis->In.cStreamsFree : &pThis->Out.cStreamsFree;
|
---|
1716 | if (*pcFreeStreams > 0)
|
---|
1717 | *pcFreeStreams -= 1;
|
---|
1718 | else
|
---|
1719 | {
|
---|
1720 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
1721 | LogFlowFunc(("Maximum number of host %s streams reached\n", PDMAudioDirGetName(pCfgReq->enmDir) ));
|
---|
1722 | return pCfgReq->enmDir == PDMAUDIODIR_IN ? VERR_AUDIO_NO_FREE_INPUT_STREAMS : VERR_AUDIO_NO_FREE_OUTPUT_STREAMS;
|
---|
1723 | }
|
---|
1724 |
|
---|
1725 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
1726 |
|
---|
1727 | /*
|
---|
1728 | * Get and check the backend size.
|
---|
1729 | *
|
---|
1730 | * Since we'll have to leave the hot-plug lock before we call the backend,
|
---|
1731 | * we'll have revalidate the size at that time.
|
---|
1732 | */
|
---|
1733 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1734 |
|
---|
1735 | size_t const cbHstStrm = pThis->BackendCfg.cbStream;
|
---|
1736 | AssertStmt(cbHstStrm >= sizeof(PDMAUDIOBACKENDSTREAM), rc = VERR_OUT_OF_RANGE);
|
---|
1737 | AssertStmt(cbHstStrm < _16M, rc = VERR_OUT_OF_RANGE);
|
---|
1738 |
|
---|
1739 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1740 | if (RT_SUCCESS(rc))
|
---|
1741 | {
|
---|
1742 | /*
|
---|
1743 | * Allocate and initialize common state.
|
---|
1744 | */
|
---|
1745 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)RTMemAllocZ(sizeof(DRVAUDIOSTREAM) + RT_ALIGN_Z(cbHstStrm, 64));
|
---|
1746 | if (pStreamEx)
|
---|
1747 | {
|
---|
1748 | rc = RTCritSectInit(&pStreamEx->Core.CritSect); /* (drvAudioStreamFree assumes it's initailized) */
|
---|
1749 | if (RT_SUCCESS(rc))
|
---|
1750 | {
|
---|
1751 | PPDMAUDIOBACKENDSTREAM pBackend = (PPDMAUDIOBACKENDSTREAM)(pStreamEx + 1);
|
---|
1752 | pBackend->uMagic = PDMAUDIOBACKENDSTREAM_MAGIC;
|
---|
1753 | pBackend->pStream = &pStreamEx->Core;
|
---|
1754 |
|
---|
1755 | pStreamEx->pBackend = pBackend;
|
---|
1756 | pStreamEx->Core.Cfg = *pCfgReq;
|
---|
1757 | pStreamEx->Core.cbBackend = (uint32_t)cbHstStrm;
|
---|
1758 | pStreamEx->fDestroyImmediate = true;
|
---|
1759 | pStreamEx->hReqInitAsync = NIL_RTREQ;
|
---|
1760 | pStreamEx->uMagic = DRVAUDIOSTREAM_MAGIC;
|
---|
1761 |
|
---|
1762 | /* Make a unqiue stream name including the host (backend) driver name. */
|
---|
1763 | AssertPtr(pThis->pHostDrvAudio);
|
---|
1764 | size_t cchName = RTStrPrintf(pStreamEx->Core.Cfg.szName, RT_ELEMENTS(pStreamEx->Core.Cfg.szName), "[%s] %s:0",
|
---|
1765 | pThis->BackendCfg.szName, pCfgReq->szName[0] != '\0' ? pCfgReq->szName : "<NoName>");
|
---|
1766 | if (cchName < sizeof(pStreamEx->Core.Cfg.szName))
|
---|
1767 | {
|
---|
1768 | RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
1769 | for (uint32_t i = 0; i < 256; i++)
|
---|
1770 | {
|
---|
1771 | bool fDone = true;
|
---|
1772 | PDRVAUDIOSTREAM pIt;
|
---|
1773 | RTListForEach(&pThis->LstStreams, pIt, DRVAUDIOSTREAM, ListEntry)
|
---|
1774 | {
|
---|
1775 | if (strcmp(pIt->Core.Cfg.szName, pStreamEx->Core.Cfg.szName) == 0)
|
---|
1776 | {
|
---|
1777 | RTStrPrintf(pStreamEx->Core.Cfg.szName, RT_ELEMENTS(pStreamEx->Core.Cfg.szName), "[%s] %s:%u",
|
---|
1778 | pThis->BackendCfg.szName, pCfgReq->szName[0] != '\0' ? pCfgReq->szName : "<NoName>",
|
---|
1779 | i);
|
---|
1780 | fDone = false;
|
---|
1781 | break;
|
---|
1782 | }
|
---|
1783 | }
|
---|
1784 | if (fDone)
|
---|
1785 | break;
|
---|
1786 | }
|
---|
1787 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
1788 | }
|
---|
1789 |
|
---|
1790 | /*
|
---|
1791 | * Try to init the rest.
|
---|
1792 | */
|
---|
1793 | rc = drvAudioStreamInitInternal(pThis, pStreamEx);
|
---|
1794 | if (RT_SUCCESS(rc))
|
---|
1795 | {
|
---|
1796 | /* Set initial reference counts. */
|
---|
1797 | pStreamEx->cRefs = pStreamEx->fNeedAsyncInit ? 2 : 1;
|
---|
1798 |
|
---|
1799 | /* Add it to the list. */
|
---|
1800 | RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
1801 |
|
---|
1802 | RTListAppend(&pThis->LstStreams, &pStreamEx->ListEntry);
|
---|
1803 | pThis->cStreams++;
|
---|
1804 | STAM_COUNTER_INC(&pThis->Stats.TotalStreamsCreated);
|
---|
1805 |
|
---|
1806 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
1807 |
|
---|
1808 | /*
|
---|
1809 | * Init debug stuff if enabled (ignore failures).
|
---|
1810 | */
|
---|
1811 | if (pCfgReq->enmDir == PDMAUDIODIR_IN)
|
---|
1812 | {
|
---|
1813 | if (pThis->CfgIn.Dbg.fEnabled)
|
---|
1814 | AudioHlpFileCreateAndOpen(&pStreamEx->In.Dbg.pFileCapture, pThis->CfgIn.Dbg.szPathOut,
|
---|
1815 | "DrvAudioCapture", pThis->pDrvIns->iInstance, &pStreamEx->Core.Cfg.Props);
|
---|
1816 | }
|
---|
1817 | else /* Out */
|
---|
1818 | {
|
---|
1819 | if (pThis->CfgOut.Dbg.fEnabled)
|
---|
1820 | AudioHlpFileCreateAndOpen(&pStreamEx->Out.Dbg.pFilePlay, pThis->CfgOut.Dbg.szPathOut,
|
---|
1821 | "DrvAudioPlay", pThis->pDrvIns->iInstance, &pStreamEx->Core.Cfg.Props);
|
---|
1822 | }
|
---|
1823 |
|
---|
1824 | /*
|
---|
1825 | * Kick off the asynchronous init.
|
---|
1826 | */
|
---|
1827 | if (!pStreamEx->fNeedAsyncInit)
|
---|
1828 | {
|
---|
1829 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_BACKEND_READY;
|
---|
1830 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
1831 | }
|
---|
1832 | else
|
---|
1833 | {
|
---|
1834 | int rc2 = RTReqPoolCallEx(pThis->hReqPool, 0 /*cMillies*/, &pStreamEx->hReqInitAsync,
|
---|
1835 | RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
1836 | (PFNRT)drvAudioStreamInitAsync, 2, pThis, pStreamEx);
|
---|
1837 | LogFlowFunc(("hReqInitAsync=%p rc2=%Rrc\n", pStreamEx->hReqInitAsync, rc2));
|
---|
1838 | AssertRCStmt(rc2, drvAudioStreamInitAsync(pThis, pStreamEx));
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | #ifdef VBOX_STRICT
|
---|
1842 | /*
|
---|
1843 | * Assert lock order to make sure the lock validator picks up on it.
|
---|
1844 | */
|
---|
1845 | RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
1846 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
1847 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
1848 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1849 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
1850 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
1851 | #endif
|
---|
1852 |
|
---|
1853 | *ppStream = &pStreamEx->Core;
|
---|
1854 | LogFlowFunc(("returns VINF_SUCCESS (pStreamEx=%p)\n", pStreamEx));
|
---|
1855 | return VINF_SUCCESS;
|
---|
1856 | }
|
---|
1857 |
|
---|
1858 | LogFunc(("drvAudioStreamInitInternal failed: %Rrc\n", rc));
|
---|
1859 | int rc2 = drvAudioStreamUninitInternal(pThis, pStreamEx);
|
---|
1860 | AssertRC(rc2);
|
---|
1861 | drvAudioStreamFree(pStreamEx);
|
---|
1862 | }
|
---|
1863 | else
|
---|
1864 | RTMemFree(pStreamEx);
|
---|
1865 | }
|
---|
1866 | else
|
---|
1867 | rc = VERR_NO_MEMORY;
|
---|
1868 | }
|
---|
1869 |
|
---|
1870 | /*
|
---|
1871 | * Give back the stream count, we couldn't use it after all.
|
---|
1872 | */
|
---|
1873 | RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
1874 | *pcFreeStreams += 1;
|
---|
1875 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
1876 |
|
---|
1877 | LogFlowFuncLeaveRC(rc);
|
---|
1878 | return rc;
|
---|
1879 | }
|
---|
1880 |
|
---|
1881 |
|
---|
1882 | /**
|
---|
1883 | * Calls the backend to give it the chance to destroy its part of the audio stream.
|
---|
1884 | *
|
---|
1885 | * Called from drvAudioPowerOff, drvAudioStreamUninitInternal and
|
---|
1886 | * drvAudioStreamReInitInternal.
|
---|
1887 | *
|
---|
1888 | * @returns VBox status code.
|
---|
1889 | * @param pThis Pointer to driver instance.
|
---|
1890 | * @param pStreamEx Audio stream destruct backend for.
|
---|
1891 | */
|
---|
1892 | static int drvAudioStreamDestroyInternalBackend(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
1893 | {
|
---|
1894 | AssertPtr(pThis);
|
---|
1895 | AssertPtr(pStreamEx);
|
---|
1896 |
|
---|
1897 | int rc = VINF_SUCCESS;
|
---|
1898 |
|
---|
1899 | #ifdef LOG_ENABLED
|
---|
1900 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
1901 | #endif
|
---|
1902 | LogFunc(("[%s] fStatus=%s\n", pStreamEx->Core.Cfg.szName, drvAudioStreamStatusToStr(szStreamSts, pStreamEx->fStatus)));
|
---|
1903 |
|
---|
1904 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_CREATED)
|
---|
1905 | {
|
---|
1906 | AssertPtr(pStreamEx->pBackend);
|
---|
1907 |
|
---|
1908 | /* Check if the pointer to the host audio driver is still valid.
|
---|
1909 | * It can be NULL if we were called in drvAudioDestruct, for example. */
|
---|
1910 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug); /** @todo needed? */
|
---|
1911 | if (pThis->pHostDrvAudio)
|
---|
1912 | rc = pThis->pHostDrvAudio->pfnStreamDestroy(pThis->pHostDrvAudio, pStreamEx->pBackend, pStreamEx->fDestroyImmediate);
|
---|
1913 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
1914 |
|
---|
1915 | pStreamEx->fStatus &= ~(PDMAUDIOSTREAM_STS_BACKEND_CREATED | PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
1916 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
1917 | }
|
---|
1918 |
|
---|
1919 | LogFlowFunc(("[%s] Returning %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
1920 | return rc;
|
---|
1921 | }
|
---|
1922 |
|
---|
1923 |
|
---|
1924 | /**
|
---|
1925 | * Uninitializes an audio stream - worker for drvAudioStreamDestroy,
|
---|
1926 | * drvAudioDestruct and drvAudioStreamCreate.
|
---|
1927 | *
|
---|
1928 | * @returns VBox status code.
|
---|
1929 | * @param pThis Pointer to driver instance.
|
---|
1930 | * @param pStreamEx Pointer to audio stream to uninitialize.
|
---|
1931 | */
|
---|
1932 | static int drvAudioStreamUninitInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
1933 | {
|
---|
1934 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
1935 | AssertMsgReturn(pStreamEx->cRefs <= 1,
|
---|
1936 | ("Stream '%s' still has %RU32 references held when uninitializing\n", pStreamEx->Core.Cfg.szName, pStreamEx->cRefs),
|
---|
1937 | VERR_WRONG_ORDER);
|
---|
1938 | LogFlowFunc(("[%s] cRefs=%RU32\n", pStreamEx->Core.Cfg.szName, pStreamEx->cRefs));
|
---|
1939 |
|
---|
1940 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
1941 |
|
---|
1942 | /*
|
---|
1943 | * ...
|
---|
1944 | */
|
---|
1945 | if (pStreamEx->fDestroyImmediate)
|
---|
1946 | drvAudioStreamControlInternal(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
1947 | int rc = drvAudioStreamDestroyInternalBackend(pThis, pStreamEx);
|
---|
1948 |
|
---|
1949 | /* Free pre-buffer space. */
|
---|
1950 | if ( pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT
|
---|
1951 | && pStreamEx->Out.pbPreBuf)
|
---|
1952 | {
|
---|
1953 | RTMemFree(pStreamEx->Out.pbPreBuf);
|
---|
1954 | pStreamEx->Out.pbPreBuf = NULL;
|
---|
1955 | pStreamEx->Out.cbPreBufAlloc = 0;
|
---|
1956 | pStreamEx->Out.cbPreBuffered = 0;
|
---|
1957 | pStreamEx->Out.offPreBuf = 0;
|
---|
1958 | }
|
---|
1959 |
|
---|
1960 | if (RT_SUCCESS(rc))
|
---|
1961 | {
|
---|
1962 | #ifdef LOG_ENABLED
|
---|
1963 | if (pStreamEx->fStatus != PDMAUDIOSTREAM_STS_NONE)
|
---|
1964 | {
|
---|
1965 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
1966 | LogFunc(("[%s] Warning: Still has %s set when uninitializing\n",
|
---|
1967 | pStreamEx->Core.Cfg.szName, drvAudioStreamStatusToStr(szStreamSts, pStreamEx->fStatus)));
|
---|
1968 | }
|
---|
1969 | #endif
|
---|
1970 | pStreamEx->fStatus = PDMAUDIOSTREAM_STS_NONE;
|
---|
1971 | }
|
---|
1972 |
|
---|
1973 | PPDMDRVINS const pDrvIns = pThis->pDrvIns;
|
---|
1974 | PDMDrvHlpSTAMDeregisterByPrefix(pDrvIns, pStreamEx->Core.Cfg.szName);
|
---|
1975 |
|
---|
1976 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
1977 | {
|
---|
1978 | if (pThis->CfgIn.Dbg.fEnabled)
|
---|
1979 | {
|
---|
1980 | AudioHlpFileDestroy(pStreamEx->In.Dbg.pFileCapture);
|
---|
1981 | pStreamEx->In.Dbg.pFileCapture = NULL;
|
---|
1982 | }
|
---|
1983 | }
|
---|
1984 | else
|
---|
1985 | {
|
---|
1986 | Assert(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT);
|
---|
1987 | if (pThis->CfgOut.Dbg.fEnabled)
|
---|
1988 | {
|
---|
1989 | AudioHlpFileDestroy(pStreamEx->Out.Dbg.pFilePlay);
|
---|
1990 | pStreamEx->Out.Dbg.pFilePlay = NULL;
|
---|
1991 | }
|
---|
1992 | }
|
---|
1993 |
|
---|
1994 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
1995 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
1996 | return rc;
|
---|
1997 | }
|
---|
1998 |
|
---|
1999 |
|
---|
2000 | /**
|
---|
2001 | * Internal release function.
|
---|
2002 | *
|
---|
2003 | * @returns New reference count, UINT32_MAX if bad stream.
|
---|
2004 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
2005 | * @param pStreamEx The stream to reference.
|
---|
2006 | * @param fMayDestroy Whether the caller is allowed to implicitly destroy
|
---|
2007 | * the stream or not.
|
---|
2008 | */
|
---|
2009 | static uint32_t drvAudioStreamReleaseInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, bool fMayDestroy)
|
---|
2010 | {
|
---|
2011 | AssertPtrReturn(pStreamEx, UINT32_MAX);
|
---|
2012 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, UINT32_MAX);
|
---|
2013 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, UINT32_MAX);
|
---|
2014 | Assert(!RTCritSectIsOwner(&pStreamEx->Core.CritSect));
|
---|
2015 |
|
---|
2016 | uint32_t cRefs = ASMAtomicDecU32(&pStreamEx->cRefs);
|
---|
2017 | if (cRefs != 0)
|
---|
2018 | Assert(cRefs < _1K);
|
---|
2019 | else if (fMayDestroy)
|
---|
2020 | {
|
---|
2021 | /** @todo r=bird: Caching one stream in each direction for some time,
|
---|
2022 | * depending on the time it took to create it. drvAudioStreamCreate can use it
|
---|
2023 | * if the configuration matches, otherwise it'll throw it away. This will
|
---|
2024 | * provide a general speedup independ of device (HDA used to do this, but
|
---|
2025 | * doesn't) and backend implementation. Ofc, the backend probably needs an
|
---|
2026 | * opt-out here. */
|
---|
2027 | int rc = drvAudioStreamUninitInternal(pThis, pStreamEx);
|
---|
2028 | if (RT_SUCCESS(rc))
|
---|
2029 | {
|
---|
2030 | RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
2031 |
|
---|
2032 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
2033 | pThis->In.cStreamsFree++;
|
---|
2034 | else /* Out */
|
---|
2035 | pThis->Out.cStreamsFree++;
|
---|
2036 | pThis->cStreams--;
|
---|
2037 |
|
---|
2038 | RTListNodeRemove(&pStreamEx->ListEntry);
|
---|
2039 |
|
---|
2040 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
2041 |
|
---|
2042 | drvAudioStreamFree(pStreamEx);
|
---|
2043 | }
|
---|
2044 | else
|
---|
2045 | {
|
---|
2046 | LogRel(("Audio: Uninitializing stream '%s' failed with %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
2047 | /** @todo r=bird: What's the plan now? */
|
---|
2048 | }
|
---|
2049 | }
|
---|
2050 | else
|
---|
2051 | {
|
---|
2052 | cRefs = ASMAtomicIncU32(&pStreamEx->cRefs);
|
---|
2053 | AssertFailed();
|
---|
2054 | }
|
---|
2055 |
|
---|
2056 | Log12Func(("returns %u (%s)\n", cRefs, cRefs > 0 ? pStreamEx->Core.Cfg.szName : "destroyed"));
|
---|
2057 | return cRefs;
|
---|
2058 | }
|
---|
2059 |
|
---|
2060 |
|
---|
2061 | /**
|
---|
2062 | * Asynchronous worker for drvAudioStreamDestroy.
|
---|
2063 | *
|
---|
2064 | * Does DISABLE and releases reference, possibly destroying the stream.
|
---|
2065 | *
|
---|
2066 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
2067 | * @param pStreamEx The stream. One reference for us to release.
|
---|
2068 | * @param fImmediate How to treat draining streams.
|
---|
2069 | */
|
---|
2070 | static DECLCALLBACK(void) drvAudioStreamDestroyAsync(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, bool fImmediate)
|
---|
2071 | {
|
---|
2072 | LogFlowFunc(("pThis=%p pStreamEx=%p (%s) fImmediate=%RTbool\n", pThis, pStreamEx, pStreamEx->Core.Cfg.szName, fImmediate));
|
---|
2073 | #ifdef LOG_ENABLED
|
---|
2074 | uint64_t const nsStart = RTTimeNanoTS();
|
---|
2075 | #endif
|
---|
2076 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
2077 |
|
---|
2078 | pStreamEx->fDestroyImmediate = fImmediate; /* Do NOT adjust for draining status, just pass it as-is. CoreAudio needs this. */
|
---|
2079 |
|
---|
2080 | if (!fImmediate && (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE))
|
---|
2081 | LogFlowFunc(("No DISABLE\n"));
|
---|
2082 | else
|
---|
2083 | {
|
---|
2084 | int rc2 = drvAudioStreamControlInternal(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2085 | LogFlowFunc(("DISABLE done: %Rrc\n", rc2));
|
---|
2086 | AssertRC(rc2);
|
---|
2087 | }
|
---|
2088 |
|
---|
2089 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
2090 |
|
---|
2091 | drvAudioStreamReleaseInternal(pThis, pStreamEx, true /*fMayDestroy*/);
|
---|
2092 |
|
---|
2093 | LogFlowFunc(("returning (after %'RU64 ns)\n", RTTimeNanoTS() - nsStart));
|
---|
2094 | }
|
---|
2095 |
|
---|
2096 |
|
---|
2097 | /**
|
---|
2098 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamDestroy}
|
---|
2099 | */
|
---|
2100 | static DECLCALLBACK(int) drvAudioStreamDestroy(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream, bool fImmediate)
|
---|
2101 | {
|
---|
2102 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
2103 | AssertPtr(pThis);
|
---|
2104 |
|
---|
2105 | /* Ignore NULL streams. */
|
---|
2106 | if (!pStream)
|
---|
2107 | return VINF_SUCCESS;
|
---|
2108 |
|
---|
2109 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream; /* Note! Do not touch pStream after this! */
|
---|
2110 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
2111 | LogFlowFunc(("ENTER - %p (%s) fImmediate=%RTbool\n", pStreamEx, pStreamEx->Core.Cfg.szName, fImmediate));
|
---|
2112 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2113 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2114 | AssertReturn(pStreamEx->pBackend && pStreamEx->pBackend->uMagic == PDMAUDIOBACKENDSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2115 |
|
---|
2116 | /*
|
---|
2117 | * The main difference from a regular release is that this will disable
|
---|
2118 | * (or drain if we could) the stream and we can cancel any pending
|
---|
2119 | * pfnStreamInitAsync call.
|
---|
2120 | */
|
---|
2121 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
2122 | AssertRCReturn(rc, rc);
|
---|
2123 |
|
---|
2124 | if (pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC)
|
---|
2125 | {
|
---|
2126 | if (pStreamEx->cRefs > 0 && pStreamEx->cRefs < UINT32_MAX / 4)
|
---|
2127 | {
|
---|
2128 | char szStatus[DRVAUDIO_STATUS_STR_MAX];
|
---|
2129 | LogRel2(("Audio: Destroying stream '%s': cRefs=%u; status: %s; backend: %s; hReqInitAsync=%p\n",
|
---|
2130 | pStreamEx->Core.Cfg.szName, pStreamEx->cRefs, drvAudioStreamStatusToStr(szStatus, pStreamEx->fStatus),
|
---|
2131 | PDMHostAudioStreamStateGetName(drvAudioStreamGetBackendState(pThis, pStreamEx)),
|
---|
2132 | pStreamEx->hReqInitAsync));
|
---|
2133 |
|
---|
2134 | /* Try cancel pending async init request and release the it. */
|
---|
2135 | if (pStreamEx->hReqInitAsync != NIL_RTREQ)
|
---|
2136 | {
|
---|
2137 | Assert(pStreamEx->cRefs >= 2);
|
---|
2138 | int rc2 = RTReqCancel(pStreamEx->hReqInitAsync);
|
---|
2139 |
|
---|
2140 | RTReqRelease(pStreamEx->hReqInitAsync);
|
---|
2141 | pStreamEx->hReqInitAsync = NIL_RTREQ;
|
---|
2142 |
|
---|
2143 | RTCritSectLeave(&pStreamEx->Core.CritSect); /* (exit before releasing the stream to avoid assertion) */
|
---|
2144 |
|
---|
2145 | if (RT_SUCCESS(rc2))
|
---|
2146 | {
|
---|
2147 | LogFlowFunc(("Successfully cancelled pending pfnStreamInitAsync call (hReqInitAsync=%p).\n",
|
---|
2148 | pStreamEx->hReqInitAsync));
|
---|
2149 | drvAudioStreamReleaseInternal(pThis, pStreamEx, true /*fMayDestroy*/);
|
---|
2150 | }
|
---|
2151 | else
|
---|
2152 | {
|
---|
2153 | LogFlowFunc(("Failed to cancel pending pfnStreamInitAsync call (hReqInitAsync=%p): %Rrc\n",
|
---|
2154 | pStreamEx->hReqInitAsync, rc2));
|
---|
2155 | Assert(rc2 == VERR_RT_REQUEST_STATE);
|
---|
2156 | }
|
---|
2157 | }
|
---|
2158 | else
|
---|
2159 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
2160 |
|
---|
2161 | /*
|
---|
2162 | * Now, if the backend requests asynchronous disabling and destruction
|
---|
2163 | * push the disabling and destroying over to a worker thread.
|
---|
2164 | *
|
---|
2165 | * This is a general offloading feature that all backends should make use of,
|
---|
2166 | * however it's rather precarious on macs where stopping an already draining
|
---|
2167 | * stream may take 8-10ms which naturally isn't something we should be doing
|
---|
2168 | * on an EMT.
|
---|
2169 | */
|
---|
2170 | if (!(pThis->BackendCfg.fFlags & PDMAUDIOBACKEND_F_ASYNC_STREAM_DESTROY))
|
---|
2171 | drvAudioStreamDestroyAsync(pThis, pStreamEx, fImmediate);
|
---|
2172 | else
|
---|
2173 | {
|
---|
2174 | int rc2 = RTReqPoolCallEx(pThis->hReqPool, 0 /*cMillies*/, NULL /*phReq*/,
|
---|
2175 | RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
2176 | (PFNRT)drvAudioStreamDestroyAsync, 3, pThis, pStreamEx, fImmediate);
|
---|
2177 | LogFlowFunc(("hReqInitAsync=%p rc2=%Rrc\n", pStreamEx->hReqInitAsync, rc2));
|
---|
2178 | AssertRCStmt(rc2, drvAudioStreamDestroyAsync(pThis, pStreamEx, fImmediate));
|
---|
2179 | }
|
---|
2180 | }
|
---|
2181 | else
|
---|
2182 | {
|
---|
2183 | AssertLogRelMsgFailedStmt(("%p cRefs=%#x\n", pStreamEx, pStreamEx->cRefs), rc = VERR_CALLER_NO_REFERENCE);
|
---|
2184 | RTCritSectLeave(&pStreamEx->Core.CritSect); /*??*/
|
---|
2185 | }
|
---|
2186 | }
|
---|
2187 | else
|
---|
2188 | {
|
---|
2189 | AssertLogRelMsgFailedStmt(("%p uMagic=%#x\n", pStreamEx, pStreamEx->uMagic), rc = VERR_INVALID_MAGIC);
|
---|
2190 | RTCritSectLeave(&pStreamEx->Core.CritSect); /*??*/
|
---|
2191 | }
|
---|
2192 |
|
---|
2193 | LogFlowFuncLeaveRC(rc);
|
---|
2194 | return rc;
|
---|
2195 | }
|
---|
2196 |
|
---|
2197 |
|
---|
2198 | /**
|
---|
2199 | * Drops all audio data (and associated state) of a stream.
|
---|
2200 | *
|
---|
2201 | * Used by drvAudioStreamIterateInternal(), drvAudioStreamResetOnDisable(), and
|
---|
2202 | * drvAudioStreamReInitInternal().
|
---|
2203 | *
|
---|
2204 | * @param pStreamEx Stream to drop data for.
|
---|
2205 | */
|
---|
2206 | static void drvAudioStreamResetInternal(PDRVAUDIOSTREAM pStreamEx)
|
---|
2207 | {
|
---|
2208 | LogFunc(("[%s]\n", pStreamEx->Core.Cfg.szName));
|
---|
2209 | Assert(RTCritSectIsOwner(&pStreamEx->Core.CritSect));
|
---|
2210 |
|
---|
2211 | pStreamEx->nsLastIterated = 0;
|
---|
2212 | pStreamEx->nsLastPlayedCaptured = 0;
|
---|
2213 | pStreamEx->nsLastReadWritten = 0;
|
---|
2214 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
2215 | {
|
---|
2216 | pStreamEx->Out.cbPreBuffered = 0;
|
---|
2217 | pStreamEx->Out.offPreBuf = 0;
|
---|
2218 | pStreamEx->Out.enmPlayState = pStreamEx->cbPreBufThreshold > 0
|
---|
2219 | ? DRVAUDIOPLAYSTATE_PREBUF : DRVAUDIOPLAYSTATE_PLAY;
|
---|
2220 | }
|
---|
2221 | else
|
---|
2222 | pStreamEx->In.enmCaptureState = pStreamEx->cbPreBufThreshold > 0
|
---|
2223 | ? DRVAUDIOCAPTURESTATE_PREBUF : DRVAUDIOCAPTURESTATE_CAPTURING;
|
---|
2224 | }
|
---|
2225 |
|
---|
2226 |
|
---|
2227 | /**
|
---|
2228 | * Re-initializes an audio stream with its existing host and guest stream
|
---|
2229 | * configuration.
|
---|
2230 | *
|
---|
2231 | * This might be the case if the backend told us we need to re-initialize
|
---|
2232 | * because something on the host side has changed.
|
---|
2233 | *
|
---|
2234 | * @note Does not touch the stream's status flags.
|
---|
2235 | *
|
---|
2236 | * @returns VBox status code.
|
---|
2237 | * @param pThis Pointer to driver instance.
|
---|
2238 | * @param pStreamEx Stream to re-initialize.
|
---|
2239 | */
|
---|
2240 | static int drvAudioStreamReInitInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
2241 | {
|
---|
2242 | char szTmp[RT_MAX(PDMAUDIOSTRMCFGTOSTRING_MAX, DRVAUDIO_STATUS_STR_MAX)];
|
---|
2243 | LogFlowFunc(("[%s] status: %s\n", pStreamEx->Core.Cfg.szName, drvAudioStreamStatusToStr(szTmp, pStreamEx->fStatus) ));
|
---|
2244 | Assert(RTCritSectIsOwner(&pStreamEx->Core.CritSect));
|
---|
2245 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
2246 |
|
---|
2247 | /*
|
---|
2248 | * Destroy and re-create stream on backend side.
|
---|
2249 | */
|
---|
2250 | if ( (pStreamEx->fStatus & (PDMAUDIOSTREAM_STS_ENABLED | PDMAUDIOSTREAM_STS_BACKEND_CREATED | PDMAUDIOSTREAM_STS_BACKEND_READY))
|
---|
2251 | == (PDMAUDIOSTREAM_STS_ENABLED | PDMAUDIOSTREAM_STS_BACKEND_CREATED | PDMAUDIOSTREAM_STS_BACKEND_READY))
|
---|
2252 | drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2253 |
|
---|
2254 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_CREATED)
|
---|
2255 | drvAudioStreamDestroyInternalBackend(pThis, pStreamEx);
|
---|
2256 |
|
---|
2257 | int rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
2258 | if (!(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_CREATED))
|
---|
2259 | {
|
---|
2260 | drvAudioStreamResetInternal(pStreamEx);
|
---|
2261 |
|
---|
2262 | RT_BZERO(pStreamEx->pBackend + 1, pStreamEx->Core.cbBackend - sizeof(*pStreamEx->pBackend));
|
---|
2263 |
|
---|
2264 | rc = drvAudioStreamCreateInternalBackend(pThis, pStreamEx);
|
---|
2265 | if (RT_SUCCESS(rc))
|
---|
2266 | {
|
---|
2267 | LogFunc(("[%s] Acquired host format: %s\n",
|
---|
2268 | pStreamEx->Core.Cfg.szName, PDMAudioStrmCfgToString(&pStreamEx->Core.Cfg, szTmp, sizeof(szTmp)) ));
|
---|
2269 | /** @todo Validate (re-)acquired configuration with pStreamEx->Core.Core.Cfg?
|
---|
2270 | * drvAudioStreamInitInternal() does some setup and a bunch of
|
---|
2271 | * validations + adjustments of the stream config, so this surely is quite
|
---|
2272 | * optimistic. */
|
---|
2273 | if (true)
|
---|
2274 | {
|
---|
2275 | /*
|
---|
2276 | * Kick off the asynchronous init.
|
---|
2277 | */
|
---|
2278 | if (!pStreamEx->fNeedAsyncInit)
|
---|
2279 | {
|
---|
2280 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_BACKEND_READY;
|
---|
2281 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2282 | }
|
---|
2283 | else
|
---|
2284 | {
|
---|
2285 | drvAudioStreamRetainInternal(pStreamEx);
|
---|
2286 | int rc2 = RTReqPoolCallEx(pThis->hReqPool, 0 /*cMillies*/, &pStreamEx->hReqInitAsync,
|
---|
2287 | RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
2288 | (PFNRT)drvAudioStreamInitAsync, 2, pThis, pStreamEx);
|
---|
2289 | LogFlowFunc(("hReqInitAsync=%p rc2=%Rrc\n", pStreamEx->hReqInitAsync, rc2));
|
---|
2290 | AssertRCStmt(rc2, drvAudioStreamInitAsync(pThis, pStreamEx));
|
---|
2291 | }
|
---|
2292 |
|
---|
2293 | /*
|
---|
2294 | * Update the backend on the stream state if it's ready, otherwise
|
---|
2295 | * let the worker thread do it after the async init has completed.
|
---|
2296 | */
|
---|
2297 | if ( (pStreamEx->fStatus & (PDMAUDIOSTREAM_STS_BACKEND_READY | PDMAUDIOSTREAM_STS_BACKEND_CREATED))
|
---|
2298 | == (PDMAUDIOSTREAM_STS_BACKEND_READY | PDMAUDIOSTREAM_STS_BACKEND_CREATED))
|
---|
2299 | {
|
---|
2300 | rc = drvAudioStreamUpdateBackendOnStatus(pThis, pStreamEx, "re-initializing");
|
---|
2301 | /** @todo not sure if we really need to care about this status code... */
|
---|
2302 | }
|
---|
2303 | else if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_CREATED)
|
---|
2304 | {
|
---|
2305 | Assert(pStreamEx->hReqInitAsync != NIL_RTREQ);
|
---|
2306 | LogFunc(("Asynchronous stream init (%p) ...\n", pStreamEx->hReqInitAsync));
|
---|
2307 | }
|
---|
2308 | else
|
---|
2309 | {
|
---|
2310 | LogRel(("Audio: Re-initializing stream '%s' somehow failed, status: %s\n", pStreamEx->Core.Cfg.szName,
|
---|
2311 | drvAudioStreamStatusToStr(szTmp, pStreamEx->fStatus) ));
|
---|
2312 | AssertFailed();
|
---|
2313 | rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
2314 | }
|
---|
2315 | }
|
---|
2316 | }
|
---|
2317 | else
|
---|
2318 | LogRel(("Audio: Re-initializing stream '%s' failed with %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
2319 | }
|
---|
2320 | else
|
---|
2321 | {
|
---|
2322 | LogRel(("Audio: Re-initializing stream '%s' failed to destroy previous backend.\n", pStreamEx->Core.Cfg.szName));
|
---|
2323 | AssertFailed();
|
---|
2324 | }
|
---|
2325 |
|
---|
2326 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
2327 | LogFunc(("[%s] Returning %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
2328 | return rc;
|
---|
2329 | }
|
---|
2330 |
|
---|
2331 |
|
---|
2332 | /**
|
---|
2333 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamReInit}
|
---|
2334 | */
|
---|
2335 | static DECLCALLBACK(int) drvAudioStreamReInit(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
2336 | {
|
---|
2337 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
2338 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
2339 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
2340 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2341 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2342 | AssertReturn(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_NEED_REINIT, VERR_INVALID_STATE);
|
---|
2343 | LogFlowFunc(("\n"));
|
---|
2344 |
|
---|
2345 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
2346 | AssertRCReturn(rc, rc);
|
---|
2347 |
|
---|
2348 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_NEED_REINIT)
|
---|
2349 | {
|
---|
2350 | const unsigned cMaxTries = 5;
|
---|
2351 | const uint64_t nsNow = RTTimeNanoTS();
|
---|
2352 |
|
---|
2353 | /* Throttle re-initializing streams on failure. */
|
---|
2354 | if ( pStreamEx->cTriesReInit < cMaxTries
|
---|
2355 | && pStreamEx->hReqInitAsync == NIL_RTREQ
|
---|
2356 | && ( pStreamEx->nsLastReInit == 0
|
---|
2357 | || nsNow - pStreamEx->nsLastReInit >= RT_NS_1SEC * pStreamEx->cTriesReInit))
|
---|
2358 | {
|
---|
2359 | rc = drvAudioStreamReInitInternal(pThis, pStreamEx);
|
---|
2360 | if (RT_SUCCESS(rc))
|
---|
2361 | {
|
---|
2362 | /* Remove the pending re-init flag on success. */
|
---|
2363 | pStreamEx->fStatus &= ~PDMAUDIOSTREAM_STS_NEED_REINIT;
|
---|
2364 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2365 | }
|
---|
2366 | else
|
---|
2367 | {
|
---|
2368 | pStreamEx->nsLastReInit = nsNow;
|
---|
2369 | pStreamEx->cTriesReInit++;
|
---|
2370 |
|
---|
2371 | /* Did we exceed our tries re-initializing the stream?
|
---|
2372 | * Then this one is dead-in-the-water, so disable it for further use. */
|
---|
2373 | if (pStreamEx->cTriesReInit >= cMaxTries)
|
---|
2374 | {
|
---|
2375 | LogRel(("Audio: Re-initializing stream '%s' exceeded maximum retries (%u), leaving as disabled\n",
|
---|
2376 | pStreamEx->Core.Cfg.szName, cMaxTries));
|
---|
2377 |
|
---|
2378 | /* Don't try to re-initialize anymore and mark as disabled. */
|
---|
2379 | /** @todo should mark it as not-initialized too, shouldn't we? */
|
---|
2380 | pStreamEx->fStatus &= ~(PDMAUDIOSTREAM_STS_NEED_REINIT | PDMAUDIOSTREAM_STS_ENABLED);
|
---|
2381 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2382 |
|
---|
2383 | /* Note: Further writes to this stream go to / will be read from the bit bucket (/dev/null) from now on. */
|
---|
2384 | }
|
---|
2385 | }
|
---|
2386 | }
|
---|
2387 | else
|
---|
2388 | Log8Func(("cTriesReInit=%d hReqInitAsync=%p nsLast=%RU64 nsNow=%RU64 nsDelta=%RU64\n", pStreamEx->cTriesReInit,
|
---|
2389 | pStreamEx->hReqInitAsync, pStreamEx->nsLastReInit, nsNow, nsNow - pStreamEx->nsLastReInit));
|
---|
2390 |
|
---|
2391 | #ifdef LOG_ENABLED
|
---|
2392 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
2393 | #endif
|
---|
2394 | Log3Func(("[%s] fStatus=%s\n", pStreamEx->Core.Cfg.szName, drvAudioStreamStatusToStr(szStreamSts, pStreamEx->fStatus)));
|
---|
2395 | }
|
---|
2396 | else
|
---|
2397 | {
|
---|
2398 | AssertFailed();
|
---|
2399 | rc = VERR_INVALID_STATE;
|
---|
2400 | }
|
---|
2401 |
|
---|
2402 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
2403 |
|
---|
2404 | LogFlowFuncLeaveRC(rc);
|
---|
2405 | return rc;
|
---|
2406 | }
|
---|
2407 |
|
---|
2408 |
|
---|
2409 | /**
|
---|
2410 | * Internal retain function.
|
---|
2411 | *
|
---|
2412 | * @returns New reference count, UINT32_MAX if bad stream.
|
---|
2413 | * @param pStreamEx The stream to reference.
|
---|
2414 | */
|
---|
2415 | static uint32_t drvAudioStreamRetainInternal(PDRVAUDIOSTREAM pStreamEx)
|
---|
2416 | {
|
---|
2417 | AssertPtrReturn(pStreamEx, UINT32_MAX);
|
---|
2418 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, UINT32_MAX);
|
---|
2419 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, UINT32_MAX);
|
---|
2420 |
|
---|
2421 | uint32_t const cRefs = ASMAtomicIncU32(&pStreamEx->cRefs);
|
---|
2422 | Assert(cRefs > 1);
|
---|
2423 | Assert(cRefs < _1K);
|
---|
2424 |
|
---|
2425 | Log12Func(("returns %u (%s)\n", cRefs, pStreamEx->Core.Cfg.szName));
|
---|
2426 | return cRefs;
|
---|
2427 | }
|
---|
2428 |
|
---|
2429 |
|
---|
2430 | /**
|
---|
2431 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamRetain}
|
---|
2432 | */
|
---|
2433 | static DECLCALLBACK(uint32_t) drvAudioStreamRetain(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
2434 | {
|
---|
2435 | RT_NOREF(pInterface);
|
---|
2436 | return drvAudioStreamRetainInternal((PDRVAUDIOSTREAM)pStream);
|
---|
2437 | }
|
---|
2438 |
|
---|
2439 |
|
---|
2440 | /**
|
---|
2441 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamRelease}
|
---|
2442 | */
|
---|
2443 | static DECLCALLBACK(uint32_t) drvAudioStreamRelease(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
2444 | {
|
---|
2445 | return drvAudioStreamReleaseInternal(RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector),
|
---|
2446 | (PDRVAUDIOSTREAM)pStream,
|
---|
2447 | false /*fMayDestroy*/);
|
---|
2448 | }
|
---|
2449 |
|
---|
2450 |
|
---|
2451 | /**
|
---|
2452 | * Controls a stream's backend.
|
---|
2453 | *
|
---|
2454 | * @returns VBox status code.
|
---|
2455 | * @param pThis Pointer to driver instance.
|
---|
2456 | * @param pStreamEx Stream to control.
|
---|
2457 | * @param enmStreamCmd Control command.
|
---|
2458 | *
|
---|
2459 | * @note Caller has entered the critical section of the stream.
|
---|
2460 | * @note Can be called w/o having entered DRVAUDIO::CritSectHotPlug.
|
---|
2461 | */
|
---|
2462 | static int drvAudioStreamControlInternalBackend(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2463 | {
|
---|
2464 | AssertPtr(pThis);
|
---|
2465 | AssertPtr(pStreamEx);
|
---|
2466 | Assert(RTCritSectIsOwner(&pStreamEx->Core.CritSect));
|
---|
2467 |
|
---|
2468 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
2469 | AssertRCReturn(rc, rc);
|
---|
2470 |
|
---|
2471 | /*
|
---|
2472 | * Whether to propagate commands down to the backend.
|
---|
2473 | *
|
---|
2474 | * 1. If the stream direction is disabled on the driver level, we should
|
---|
2475 | * obviously not call the backend. Our stream status will reflect the
|
---|
2476 | * actual state so drvAudioEnable() can tell the backend if the user
|
---|
2477 | * re-enables the stream direction.
|
---|
2478 | *
|
---|
2479 | * 2. If the backend hasn't finished initializing yet, don't try call
|
---|
2480 | * it to start/stop/pause/whatever the stream. (Better to do it here
|
---|
2481 | * than to replicate this in the relevant backends.) When the backend
|
---|
2482 | * finish initializing the stream, we'll update it about the stream state.
|
---|
2483 | */
|
---|
2484 | bool const fDirEnabled = pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN
|
---|
2485 | ? pThis->In.fEnabled : pThis->Out.fEnabled;
|
---|
2486 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
2487 | /* ^^^ (checks pThis->pHostDrvAudio != NULL too) */
|
---|
2488 |
|
---|
2489 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
2490 | LogRel2(("Audio: %s stream '%s' backend (%s is %s; status: %s; backend-status: %s)\n",
|
---|
2491 | PDMAudioStrmCmdGetName(enmStreamCmd), pStreamEx->Core.Cfg.szName, PDMAudioDirGetName(pStreamEx->Core.Cfg.enmDir),
|
---|
2492 | fDirEnabled ? "enabled" : "disabled", drvAudioStreamStatusToStr(szStreamSts, pStreamEx->fStatus),
|
---|
2493 | PDMHostAudioStreamStateGetName(enmBackendState) ));
|
---|
2494 |
|
---|
2495 | if (fDirEnabled)
|
---|
2496 | {
|
---|
2497 | if ( (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY /* don't really need this check, do we? */)
|
---|
2498 | && ( enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY
|
---|
2499 | || enmBackendState == PDMHOSTAUDIOSTREAMSTATE_DRAINING) )
|
---|
2500 | {
|
---|
2501 | switch (enmStreamCmd)
|
---|
2502 | {
|
---|
2503 | case PDMAUDIOSTREAMCMD_ENABLE:
|
---|
2504 | rc = pThis->pHostDrvAudio->pfnStreamEnable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2505 | break;
|
---|
2506 |
|
---|
2507 | case PDMAUDIOSTREAMCMD_DISABLE:
|
---|
2508 | rc = pThis->pHostDrvAudio->pfnStreamDisable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2509 | break;
|
---|
2510 |
|
---|
2511 | case PDMAUDIOSTREAMCMD_PAUSE:
|
---|
2512 | rc = pThis->pHostDrvAudio->pfnStreamPause(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2513 | break;
|
---|
2514 |
|
---|
2515 | case PDMAUDIOSTREAMCMD_RESUME:
|
---|
2516 | rc = pThis->pHostDrvAudio->pfnStreamResume(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2517 | break;
|
---|
2518 |
|
---|
2519 | case PDMAUDIOSTREAMCMD_DRAIN:
|
---|
2520 | if (pThis->pHostDrvAudio->pfnStreamDrain)
|
---|
2521 | rc = pThis->pHostDrvAudio->pfnStreamDrain(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2522 | else
|
---|
2523 | rc = VERR_NOT_SUPPORTED;
|
---|
2524 | break;
|
---|
2525 |
|
---|
2526 | default:
|
---|
2527 | AssertMsgFailedBreakStmt(("Command %RU32 not implemented\n", enmStreamCmd), rc = VERR_INTERNAL_ERROR_2);
|
---|
2528 | }
|
---|
2529 | if (RT_SUCCESS(rc))
|
---|
2530 | Log2Func(("[%s] %s succeeded (%Rrc)\n", pStreamEx->Core.Cfg.szName, PDMAudioStrmCmdGetName(enmStreamCmd), rc));
|
---|
2531 | else
|
---|
2532 | {
|
---|
2533 | LogFunc(("[%s] %s failed with %Rrc\n", pStreamEx->Core.Cfg.szName, PDMAudioStrmCmdGetName(enmStreamCmd), rc));
|
---|
2534 | if ( rc != VERR_NOT_IMPLEMENTED
|
---|
2535 | && rc != VERR_NOT_SUPPORTED
|
---|
2536 | && rc != VERR_AUDIO_STREAM_NOT_READY)
|
---|
2537 | LogRel(("Audio: %s stream '%s' failed with %Rrc\n", PDMAudioStrmCmdGetName(enmStreamCmd), pStreamEx->Core.Cfg.szName, rc));
|
---|
2538 | }
|
---|
2539 | }
|
---|
2540 | else
|
---|
2541 | LogFlowFunc(("enmBackendStat(=%s) != OKAY || !(fStatus(=%#x) & BACKEND_READY)\n",
|
---|
2542 | PDMHostAudioStreamStateGetName(enmBackendState), pStreamEx->fStatus));
|
---|
2543 | }
|
---|
2544 | else
|
---|
2545 | LogFlowFunc(("fDirEnabled=false\n"));
|
---|
2546 |
|
---|
2547 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
2548 | return rc;
|
---|
2549 | }
|
---|
2550 |
|
---|
2551 |
|
---|
2552 | /**
|
---|
2553 | * Resets the given audio stream.
|
---|
2554 | *
|
---|
2555 | * @param pStreamEx Stream to reset.
|
---|
2556 | */
|
---|
2557 | static void drvAudioStreamResetOnDisable(PDRVAUDIOSTREAM pStreamEx)
|
---|
2558 | {
|
---|
2559 | drvAudioStreamResetInternal(pStreamEx);
|
---|
2560 |
|
---|
2561 | LogFunc(("[%s]\n", pStreamEx->Core.Cfg.szName));
|
---|
2562 |
|
---|
2563 | pStreamEx->fStatus &= PDMAUDIOSTREAM_STS_BACKEND_CREATED | PDMAUDIOSTREAM_STS_BACKEND_READY;
|
---|
2564 | pStreamEx->Core.fWarningsShown = PDMAUDIOSTREAM_WARN_FLAGS_NONE;
|
---|
2565 |
|
---|
2566 | #ifdef VBOX_WITH_STATISTICS
|
---|
2567 | /*
|
---|
2568 | * Reset statistics.
|
---|
2569 | */
|
---|
2570 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN)
|
---|
2571 | {
|
---|
2572 | STAM_COUNTER_RESET(&pStreamEx->In.Stats.TotalFramesCaptured);
|
---|
2573 | STAM_COUNTER_RESET(&pStreamEx->In.Stats.TotalTimesCaptured);
|
---|
2574 | STAM_COUNTER_RESET(&pStreamEx->In.Stats.TotalTimesRead);
|
---|
2575 | }
|
---|
2576 | else if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
2577 | {
|
---|
2578 | }
|
---|
2579 | else
|
---|
2580 | AssertFailed();
|
---|
2581 | #endif
|
---|
2582 | }
|
---|
2583 |
|
---|
2584 |
|
---|
2585 | /**
|
---|
2586 | * Controls an audio stream.
|
---|
2587 | *
|
---|
2588 | * @returns VBox status code.
|
---|
2589 | * @param pThis Pointer to driver instance.
|
---|
2590 | * @param pStreamEx Stream to control.
|
---|
2591 | * @param enmStreamCmd Control command.
|
---|
2592 | */
|
---|
2593 | static int drvAudioStreamControlInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2594 | {
|
---|
2595 | AssertPtr(pThis);
|
---|
2596 | AssertPtr(pStreamEx);
|
---|
2597 | Assert(RTCritSectIsOwner(&pStreamEx->Core.CritSect));
|
---|
2598 |
|
---|
2599 | #ifdef LOG_ENABLED
|
---|
2600 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
2601 | #endif
|
---|
2602 | LogFunc(("[%s] enmStreamCmd=%s fStatus=%s\n", pStreamEx->Core.Cfg.szName, PDMAudioStrmCmdGetName(enmStreamCmd),
|
---|
2603 | drvAudioStreamStatusToStr(szStreamSts, pStreamEx->fStatus)));
|
---|
2604 |
|
---|
2605 | int rc = VINF_SUCCESS;
|
---|
2606 |
|
---|
2607 | switch (enmStreamCmd)
|
---|
2608 | {
|
---|
2609 | case PDMAUDIOSTREAMCMD_ENABLE:
|
---|
2610 | if (!(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_ENABLED))
|
---|
2611 | {
|
---|
2612 | /* Are we still draining this stream? Then we must disable it first. */
|
---|
2613 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE)
|
---|
2614 | {
|
---|
2615 | LogFunc(("Stream '%s' is still draining - disabling...\n", pStreamEx->Core.Cfg.szName));
|
---|
2616 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2617 | AssertRC(rc);
|
---|
2618 | if (drvAudioStreamGetBackendState(pThis, pStreamEx) != PDMHOSTAUDIOSTREAMSTATE_DRAINING)
|
---|
2619 | {
|
---|
2620 | pStreamEx->fStatus &= ~(PDMAUDIOSTREAM_STS_ENABLED | PDMAUDIOSTREAM_STS_PENDING_DISABLE);
|
---|
2621 | drvAudioStreamResetInternal(pStreamEx);
|
---|
2622 | rc = VINF_SUCCESS;
|
---|
2623 | }
|
---|
2624 | }
|
---|
2625 |
|
---|
2626 | if (RT_SUCCESS(rc))
|
---|
2627 | {
|
---|
2628 | /* Reset the state before we try to start. */
|
---|
2629 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
2630 | pStreamEx->enmLastBackendState = enmBackendState;
|
---|
2631 | pStreamEx->offInternal = 0;
|
---|
2632 |
|
---|
2633 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
2634 | {
|
---|
2635 | pStreamEx->Out.cbPreBuffered = 0;
|
---|
2636 | pStreamEx->Out.offPreBuf = 0;
|
---|
2637 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_NOPLAY;
|
---|
2638 | switch (enmBackendState)
|
---|
2639 | {
|
---|
2640 | case PDMHOSTAUDIOSTREAMSTATE_INITIALIZING:
|
---|
2641 | if (pStreamEx->cbPreBufThreshold > 0)
|
---|
2642 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF;
|
---|
2643 | break;
|
---|
2644 | case PDMHOSTAUDIOSTREAMSTATE_DRAINING:
|
---|
2645 | AssertFailed();
|
---|
2646 | RT_FALL_THROUGH();
|
---|
2647 | case PDMHOSTAUDIOSTREAMSTATE_OKAY:
|
---|
2648 | pStreamEx->Out.enmPlayState = pStreamEx->cbPreBufThreshold > 0
|
---|
2649 | ? DRVAUDIOPLAYSTATE_PREBUF : DRVAUDIOPLAYSTATE_PLAY;
|
---|
2650 | break;
|
---|
2651 | case PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING:
|
---|
2652 | case PDMHOSTAUDIOSTREAMSTATE_INACTIVE:
|
---|
2653 | break;
|
---|
2654 | /* no default */
|
---|
2655 | case PDMHOSTAUDIOSTREAMSTATE_INVALID:
|
---|
2656 | case PDMHOSTAUDIOSTREAMSTATE_END:
|
---|
2657 | case PDMHOSTAUDIOSTREAMSTATE_32BIT_HACK:
|
---|
2658 | break;
|
---|
2659 | }
|
---|
2660 | LogFunc(("ENABLE: enmBackendState=%s enmPlayState=%s\n", PDMHostAudioStreamStateGetName(enmBackendState),
|
---|
2661 | drvAudioPlayStateName(pStreamEx->Out.enmPlayState)));
|
---|
2662 | }
|
---|
2663 | else
|
---|
2664 | {
|
---|
2665 | pStreamEx->In.enmCaptureState = DRVAUDIOCAPTURESTATE_NO_CAPTURE;
|
---|
2666 | switch (enmBackendState)
|
---|
2667 | {
|
---|
2668 | case PDMHOSTAUDIOSTREAMSTATE_INITIALIZING:
|
---|
2669 | pStreamEx->In.enmCaptureState = DRVAUDIOCAPTURESTATE_PREBUF;
|
---|
2670 | break;
|
---|
2671 | case PDMHOSTAUDIOSTREAMSTATE_DRAINING:
|
---|
2672 | AssertFailed();
|
---|
2673 | RT_FALL_THROUGH();
|
---|
2674 | case PDMHOSTAUDIOSTREAMSTATE_OKAY:
|
---|
2675 | pStreamEx->In.enmCaptureState = pStreamEx->cbPreBufThreshold > 0
|
---|
2676 | ? DRVAUDIOCAPTURESTATE_PREBUF : DRVAUDIOCAPTURESTATE_CAPTURING;
|
---|
2677 | break;
|
---|
2678 | case PDMHOSTAUDIOSTREAMSTATE_NOT_WORKING:
|
---|
2679 | case PDMHOSTAUDIOSTREAMSTATE_INACTIVE:
|
---|
2680 | break;
|
---|
2681 | /* no default */
|
---|
2682 | case PDMHOSTAUDIOSTREAMSTATE_INVALID:
|
---|
2683 | case PDMHOSTAUDIOSTREAMSTATE_END:
|
---|
2684 | case PDMHOSTAUDIOSTREAMSTATE_32BIT_HACK:
|
---|
2685 | break;
|
---|
2686 | }
|
---|
2687 | LogFunc(("ENABLE: enmBackendState=%s enmCaptureState=%s\n", PDMHostAudioStreamStateGetName(enmBackendState),
|
---|
2688 | drvAudioCaptureStateName(pStreamEx->In.enmCaptureState)));
|
---|
2689 | }
|
---|
2690 |
|
---|
2691 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_ENABLE);
|
---|
2692 | if (RT_SUCCESS(rc))
|
---|
2693 | {
|
---|
2694 | pStreamEx->nsStarted = RTTimeNanoTS();
|
---|
2695 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_ENABLED;
|
---|
2696 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2697 | }
|
---|
2698 | }
|
---|
2699 | }
|
---|
2700 | break;
|
---|
2701 |
|
---|
2702 | case PDMAUDIOSTREAMCMD_DISABLE:
|
---|
2703 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_ENABLED)
|
---|
2704 | {
|
---|
2705 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2706 | LogFunc(("DISABLE '%s': Backend DISABLE -> %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
2707 | if (RT_SUCCESS(rc)) /** @todo ignore this and reset it anyway? */
|
---|
2708 | drvAudioStreamResetOnDisable(pStreamEx);
|
---|
2709 | }
|
---|
2710 | break;
|
---|
2711 |
|
---|
2712 | case PDMAUDIOSTREAMCMD_PAUSE:
|
---|
2713 | if ((pStreamEx->fStatus & (PDMAUDIOSTREAM_STS_ENABLED | PDMAUDIOSTREAM_STS_PAUSED)) == PDMAUDIOSTREAM_STS_ENABLED)
|
---|
2714 | {
|
---|
2715 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_PAUSE);
|
---|
2716 | if (RT_SUCCESS(rc))
|
---|
2717 | {
|
---|
2718 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_PAUSED;
|
---|
2719 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2720 | }
|
---|
2721 | }
|
---|
2722 | break;
|
---|
2723 |
|
---|
2724 | case PDMAUDIOSTREAMCMD_RESUME:
|
---|
2725 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PAUSED)
|
---|
2726 | {
|
---|
2727 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_ENABLED);
|
---|
2728 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_RESUME);
|
---|
2729 | if (RT_SUCCESS(rc))
|
---|
2730 | {
|
---|
2731 | pStreamEx->fStatus &= ~PDMAUDIOSTREAM_STS_PAUSED;
|
---|
2732 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2733 | }
|
---|
2734 | }
|
---|
2735 | break;
|
---|
2736 |
|
---|
2737 | case PDMAUDIOSTREAMCMD_DRAIN:
|
---|
2738 | /*
|
---|
2739 | * Only for output streams and we don't want this command more than once.
|
---|
2740 | */
|
---|
2741 | AssertReturn(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT, VERR_INVALID_FUNCTION);
|
---|
2742 | AssertBreak(!(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE));
|
---|
2743 | if (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_ENABLED)
|
---|
2744 | {
|
---|
2745 | rc = VERR_INTERNAL_ERROR_2;
|
---|
2746 | switch (pStreamEx->Out.enmPlayState)
|
---|
2747 | {
|
---|
2748 | case DRVAUDIOPLAYSTATE_PREBUF:
|
---|
2749 | if (pStreamEx->Out.cbPreBuffered > 0)
|
---|
2750 | {
|
---|
2751 | LogFunc(("DRAIN '%s': Initiating draining of pre-buffered data...\n", pStreamEx->Core.Cfg.szName));
|
---|
2752 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF_COMMITTING;
|
---|
2753 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_PENDING_DISABLE;
|
---|
2754 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2755 | rc = VINF_SUCCESS;
|
---|
2756 | break;
|
---|
2757 | }
|
---|
2758 | RT_FALL_THROUGH();
|
---|
2759 | case DRVAUDIOPLAYSTATE_NOPLAY:
|
---|
2760 | case DRVAUDIOPLAYSTATE_PREBUF_SWITCHING:
|
---|
2761 | case DRVAUDIOPLAYSTATE_PREBUF_OVERDUE:
|
---|
2762 | LogFunc(("DRAIN '%s': Nothing to drain (enmPlayState=%s)\n",
|
---|
2763 | pStreamEx->Core.Cfg.szName, drvAudioPlayStateName(pStreamEx->Out.enmPlayState)));
|
---|
2764 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2765 | AssertRC(rc);
|
---|
2766 | drvAudioStreamResetOnDisable(pStreamEx);
|
---|
2767 | break;
|
---|
2768 |
|
---|
2769 | case DRVAUDIOPLAYSTATE_PLAY:
|
---|
2770 | case DRVAUDIOPLAYSTATE_PLAY_PREBUF:
|
---|
2771 | LogFunc(("DRAIN '%s': Initiating backend draining (enmPlayState=%s -> NOPLAY) ...\n",
|
---|
2772 | pStreamEx->Core.Cfg.szName, drvAudioPlayStateName(pStreamEx->Out.enmPlayState)));
|
---|
2773 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_NOPLAY;
|
---|
2774 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DRAIN);
|
---|
2775 | if (RT_SUCCESS(rc))
|
---|
2776 | {
|
---|
2777 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_PENDING_DISABLE;
|
---|
2778 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2779 | }
|
---|
2780 | else
|
---|
2781 | {
|
---|
2782 | LogFunc(("DRAIN '%s': Backend DRAIN failed with %Rrc, disabling the stream instead...\n",
|
---|
2783 | pStreamEx->Core.Cfg.szName, rc));
|
---|
2784 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2785 | AssertRC(rc);
|
---|
2786 | drvAudioStreamResetOnDisable(pStreamEx);
|
---|
2787 | }
|
---|
2788 | break;
|
---|
2789 |
|
---|
2790 | case DRVAUDIOPLAYSTATE_PREBUF_COMMITTING:
|
---|
2791 | LogFunc(("DRAIN '%s': Initiating draining of pre-buffered data (already committing)...\n",
|
---|
2792 | pStreamEx->Core.Cfg.szName));
|
---|
2793 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_PENDING_DISABLE;
|
---|
2794 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
2795 | rc = VINF_SUCCESS;
|
---|
2796 | break;
|
---|
2797 |
|
---|
2798 | /* no default */
|
---|
2799 | case DRVAUDIOPLAYSTATE_INVALID:
|
---|
2800 | case DRVAUDIOPLAYSTATE_END:
|
---|
2801 | AssertFailedBreak();
|
---|
2802 | }
|
---|
2803 | }
|
---|
2804 | break;
|
---|
2805 |
|
---|
2806 | default:
|
---|
2807 | rc = VERR_NOT_IMPLEMENTED;
|
---|
2808 | break;
|
---|
2809 | }
|
---|
2810 |
|
---|
2811 | if (RT_FAILURE(rc))
|
---|
2812 | LogFunc(("[%s] Failed with %Rrc\n", pStreamEx->Core.Cfg.szName, rc));
|
---|
2813 |
|
---|
2814 | return rc;
|
---|
2815 | }
|
---|
2816 |
|
---|
2817 |
|
---|
2818 | /**
|
---|
2819 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamControl}
|
---|
2820 | */
|
---|
2821 | static DECLCALLBACK(int) drvAudioStreamControl(PPDMIAUDIOCONNECTOR pInterface,
|
---|
2822 | PPDMAUDIOSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2823 | {
|
---|
2824 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
2825 | AssertPtr(pThis);
|
---|
2826 |
|
---|
2827 | /** @todo r=bird: why? It's not documented to ignore NULL streams. */
|
---|
2828 | if (!pStream)
|
---|
2829 | return VINF_SUCCESS;
|
---|
2830 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
2831 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
2832 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2833 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
2834 |
|
---|
2835 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
2836 | AssertRCReturn(rc, rc);
|
---|
2837 |
|
---|
2838 | LogFlowFunc(("[%s] enmStreamCmd=%s\n", pStreamEx->Core.Cfg.szName, PDMAudioStrmCmdGetName(enmStreamCmd)));
|
---|
2839 |
|
---|
2840 | rc = drvAudioStreamControlInternal(pThis, pStreamEx, enmStreamCmd);
|
---|
2841 |
|
---|
2842 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
2843 | return rc;
|
---|
2844 | }
|
---|
2845 |
|
---|
2846 |
|
---|
2847 | /**
|
---|
2848 | * Copy data to the pre-buffer, ring-buffer style.
|
---|
2849 | *
|
---|
2850 | * The @a cbMax parameter is almost always set to the threshold size, the
|
---|
2851 | * exception is when commiting the buffer and we want to top it off to reduce
|
---|
2852 | * the number of transfers to the backend (the first transfer may start
|
---|
2853 | * playback, so more data is better).
|
---|
2854 | */
|
---|
2855 | static int drvAudioStreamPreBuffer(PDRVAUDIOSTREAM pStreamEx, const uint8_t *pbBuf, uint32_t cbBuf, uint32_t cbMax)
|
---|
2856 | {
|
---|
2857 | uint32_t const cbAlloc = pStreamEx->Out.cbPreBufAlloc;
|
---|
2858 | AssertReturn(cbAlloc >= cbMax, VERR_INTERNAL_ERROR_3);
|
---|
2859 | AssertReturn(cbAlloc >= 8, VERR_INTERNAL_ERROR_4);
|
---|
2860 | AssertReturn(cbMax >= 8, VERR_INTERNAL_ERROR_5);
|
---|
2861 |
|
---|
2862 | uint32_t offRead = pStreamEx->Out.offPreBuf;
|
---|
2863 | uint32_t cbCur = pStreamEx->Out.cbPreBuffered;
|
---|
2864 | AssertStmt(offRead < cbAlloc, offRead %= cbAlloc);
|
---|
2865 | AssertStmt(cbCur <= cbMax, offRead = (offRead + cbCur - cbMax) % cbAlloc; cbCur = cbMax);
|
---|
2866 |
|
---|
2867 | /*
|
---|
2868 | * First chunk.
|
---|
2869 | */
|
---|
2870 | uint32_t offWrite = (offRead + cbCur) % cbAlloc;
|
---|
2871 | uint32_t cbToCopy = RT_MIN(cbAlloc - offWrite, cbBuf);
|
---|
2872 | memcpy(&pStreamEx->Out.pbPreBuf[offWrite], pbBuf, cbToCopy);
|
---|
2873 |
|
---|
2874 | /* Advance. */
|
---|
2875 | offWrite = (offWrite + cbToCopy) % cbAlloc;
|
---|
2876 | for (;;)
|
---|
2877 | {
|
---|
2878 | pbBuf += cbToCopy;
|
---|
2879 | cbCur += cbToCopy;
|
---|
2880 | if (cbCur > cbMax)
|
---|
2881 | offRead = (offRead + cbCur - cbMax) % cbAlloc;
|
---|
2882 | cbBuf -= cbToCopy;
|
---|
2883 | if (!cbBuf)
|
---|
2884 | break;
|
---|
2885 |
|
---|
2886 | /*
|
---|
2887 | * Second+ chunk, from the start of the buffer.
|
---|
2888 | *
|
---|
2889 | * Note! It is assumed very unlikely that we will ever see a cbBuf larger than
|
---|
2890 | * cbMax, so we don't waste space on clipping cbBuf here (can happen with
|
---|
2891 | * custom pre-buffer sizes).
|
---|
2892 | */
|
---|
2893 | Assert(offWrite == 0);
|
---|
2894 | cbToCopy = RT_MIN(cbAlloc, cbBuf);
|
---|
2895 | memcpy(pStreamEx->Out.pbPreBuf, pbBuf, cbToCopy);
|
---|
2896 | }
|
---|
2897 |
|
---|
2898 | /*
|
---|
2899 | * Update the pre-buffering size and position.
|
---|
2900 | */
|
---|
2901 | pStreamEx->Out.cbPreBuffered = RT_MIN(cbCur, cbMax);
|
---|
2902 | pStreamEx->Out.offPreBuf = offRead;
|
---|
2903 | return VINF_SUCCESS;
|
---|
2904 | }
|
---|
2905 |
|
---|
2906 |
|
---|
2907 | /**
|
---|
2908 | * Worker for drvAudioStreamPlay() and drvAudioStreamPreBufComitting().
|
---|
2909 | *
|
---|
2910 | * Caller owns the lock.
|
---|
2911 | */
|
---|
2912 | static int drvAudioStreamPlayLocked(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx,
|
---|
2913 | const uint8_t *pbBuf, uint32_t cbBuf, uint32_t *pcbWritten)
|
---|
2914 | {
|
---|
2915 | Log3Func(("%s: @%#RX64: cbBuf=%#x\n", pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbBuf));
|
---|
2916 |
|
---|
2917 | uint32_t cbWritable = pThis->pHostDrvAudio->pfnStreamGetWritable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2918 | pStreamEx->Out.Stats.cbBackendWritableBefore = cbWritable;
|
---|
2919 |
|
---|
2920 | uint32_t cbWritten = 0;
|
---|
2921 | int rc = VINF_SUCCESS;
|
---|
2922 | uint8_t const cbFrame = PDMAudioPropsFrameSize(&pStreamEx->Core.Cfg.Props);
|
---|
2923 | while (cbBuf >= cbFrame && cbWritable >= cbFrame)
|
---|
2924 | {
|
---|
2925 | uint32_t const cbToWrite = PDMAudioPropsFloorBytesToFrame(&pStreamEx->Core.Cfg.Props, RT_MIN(cbBuf, cbWritable));
|
---|
2926 | uint32_t cbWrittenNow = 0;
|
---|
2927 | rc = pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStreamEx->pBackend, pbBuf, cbToWrite, &cbWrittenNow);
|
---|
2928 | if (RT_SUCCESS(rc))
|
---|
2929 | {
|
---|
2930 | if (cbWrittenNow != cbToWrite)
|
---|
2931 | Log3Func(("%s: @%#RX64: Wrote fewer bytes than requested: %#x, requested %#x\n",
|
---|
2932 | pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbWrittenNow, cbToWrite));
|
---|
2933 | #ifdef DEBUG_bird
|
---|
2934 | Assert(cbWrittenNow == cbToWrite);
|
---|
2935 | #endif
|
---|
2936 | AssertStmt(cbWrittenNow <= cbToWrite, cbWrittenNow = cbToWrite);
|
---|
2937 | cbWritten += cbWrittenNow;
|
---|
2938 | cbBuf -= cbWrittenNow;
|
---|
2939 | pbBuf += cbWrittenNow;
|
---|
2940 | pStreamEx->offInternal += cbWrittenNow;
|
---|
2941 | }
|
---|
2942 | else
|
---|
2943 | {
|
---|
2944 | *pcbWritten = cbWritten;
|
---|
2945 | LogFunc(("%s: @%#RX64: pfnStreamPlay failed writing %#x bytes (%#x previous written, %#x writable): %Rrc\n",
|
---|
2946 | pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbToWrite, cbWritten, cbWritable, rc));
|
---|
2947 | return cbWritten ? VINF_SUCCESS : rc;
|
---|
2948 | }
|
---|
2949 |
|
---|
2950 | cbWritable = pThis->pHostDrvAudio->pfnStreamGetWritable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
2951 | }
|
---|
2952 |
|
---|
2953 | *pcbWritten = cbWritten;
|
---|
2954 | pStreamEx->Out.Stats.cbBackendWritableAfter = cbWritable;
|
---|
2955 | if (cbWritten)
|
---|
2956 | pStreamEx->nsLastPlayedCaptured = RTTimeNanoTS();
|
---|
2957 |
|
---|
2958 | Log3Func(("%s: @%#RX64: Wrote %#x bytes (%#x bytes left)\n", pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbWritten, cbBuf));
|
---|
2959 | return rc;
|
---|
2960 | }
|
---|
2961 |
|
---|
2962 |
|
---|
2963 | /**
|
---|
2964 | * Worker for drvAudioStreamPlay() and drvAudioStreamPreBufComitting().
|
---|
2965 | */
|
---|
2966 | static int drvAudioStreamPlayToPreBuffer(PDRVAUDIOSTREAM pStreamEx, const void *pvBuf, uint32_t cbBuf, uint32_t cbMax,
|
---|
2967 | uint32_t *pcbWritten)
|
---|
2968 | {
|
---|
2969 | int rc = drvAudioStreamPreBuffer(pStreamEx, (uint8_t const *)pvBuf, cbBuf, cbMax);
|
---|
2970 | if (RT_SUCCESS(rc))
|
---|
2971 | {
|
---|
2972 | *pcbWritten = cbBuf;
|
---|
2973 | pStreamEx->offInternal += cbBuf;
|
---|
2974 | Log3Func(("[%s] Pre-buffering (%s): wrote %#x bytes => %#x bytes / %u%%\n",
|
---|
2975 | pStreamEx->Core.Cfg.szName, drvAudioPlayStateName(pStreamEx->Out.enmPlayState), cbBuf, pStreamEx->Out.cbPreBuffered,
|
---|
2976 | pStreamEx->Out.cbPreBuffered * 100 / RT_MAX(pStreamEx->cbPreBufThreshold, 1)));
|
---|
2977 |
|
---|
2978 | }
|
---|
2979 | else
|
---|
2980 | *pcbWritten = 0;
|
---|
2981 | return rc;
|
---|
2982 | }
|
---|
2983 |
|
---|
2984 |
|
---|
2985 | /**
|
---|
2986 | * Used when we're committing (transfering) the pre-buffered bytes to the
|
---|
2987 | * device.
|
---|
2988 | *
|
---|
2989 | * This is called both from drvAudioStreamPlay() and
|
---|
2990 | * drvAudioStreamIterateInternal().
|
---|
2991 | *
|
---|
2992 | * @returns VBox status code.
|
---|
2993 | * @param pThis Pointer to the DrvAudio instance data.
|
---|
2994 | * @param pStreamEx The stream to commit the pre-buffering for.
|
---|
2995 | * @param pbBuf Buffer with new bytes to write. Can be NULL when called
|
---|
2996 | * in the PENDING_DISABLE state from
|
---|
2997 | * drvAudioStreamIterateInternal().
|
---|
2998 | * @param cbBuf Number of new bytes. Can be zero.
|
---|
2999 | * @param pcbWritten Where to return the number of bytes written.
|
---|
3000 | *
|
---|
3001 | * @note Locking: Stream critsect and hot-plug in shared mode.
|
---|
3002 | */
|
---|
3003 | static int drvAudioStreamPreBufComitting(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx,
|
---|
3004 | const uint8_t *pbBuf, uint32_t cbBuf, uint32_t *pcbWritten)
|
---|
3005 | {
|
---|
3006 | /*
|
---|
3007 | * First, top up the buffer with new data from pbBuf.
|
---|
3008 | */
|
---|
3009 | *pcbWritten = 0;
|
---|
3010 | if (cbBuf > 0)
|
---|
3011 | {
|
---|
3012 | uint32_t const cbToCopy = RT_MIN(pStreamEx->Out.cbPreBufAlloc - pStreamEx->Out.cbPreBuffered, cbBuf);
|
---|
3013 | if (cbToCopy > 0)
|
---|
3014 | {
|
---|
3015 | int rc = drvAudioStreamPlayToPreBuffer(pStreamEx, pbBuf, cbBuf, pStreamEx->Out.cbPreBufAlloc, pcbWritten);
|
---|
3016 | AssertRCReturn(rc, rc);
|
---|
3017 | pbBuf += cbToCopy;
|
---|
3018 | cbBuf -= cbToCopy;
|
---|
3019 | }
|
---|
3020 | }
|
---|
3021 |
|
---|
3022 | AssertReturn(pThis->pHostDrvAudio, VERR_AUDIO_BACKEND_NOT_ATTACHED);
|
---|
3023 |
|
---|
3024 | /*
|
---|
3025 | * Write the pre-buffered chunk.
|
---|
3026 | */
|
---|
3027 | int rc = VINF_SUCCESS;
|
---|
3028 | uint32_t const cbAlloc = pStreamEx->Out.cbPreBufAlloc;
|
---|
3029 | AssertReturn(cbAlloc > 0, VERR_INTERNAL_ERROR_2);
|
---|
3030 | uint32_t off = pStreamEx->Out.offPreBuf;
|
---|
3031 | AssertStmt(off < pStreamEx->Out.cbPreBufAlloc, off %= cbAlloc);
|
---|
3032 | uint32_t cbLeft = pStreamEx->Out.cbPreBuffered;
|
---|
3033 | while (cbLeft > 0)
|
---|
3034 | {
|
---|
3035 | uint32_t const cbToWrite = RT_MIN(cbAlloc - off, cbLeft);
|
---|
3036 | Assert(cbToWrite > 0);
|
---|
3037 |
|
---|
3038 | uint32_t cbPreBufWritten = 0;
|
---|
3039 | rc = pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStreamEx->pBackend, &pStreamEx->Out.pbPreBuf[off],
|
---|
3040 | cbToWrite, &cbPreBufWritten);
|
---|
3041 | AssertRCBreak(rc);
|
---|
3042 | if (!cbPreBufWritten)
|
---|
3043 | break;
|
---|
3044 | AssertStmt(cbPreBufWritten <= cbToWrite, cbPreBufWritten = cbToWrite);
|
---|
3045 | off = (off + cbPreBufWritten) % cbAlloc;
|
---|
3046 | cbLeft -= cbPreBufWritten;
|
---|
3047 | }
|
---|
3048 |
|
---|
3049 | if (cbLeft == 0)
|
---|
3050 | {
|
---|
3051 | LogFunc(("@%#RX64: Wrote all %#x bytes of pre-buffered audio data. %s -> PLAY\n", pStreamEx->offInternal,
|
---|
3052 | pStreamEx->Out.cbPreBuffered, drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
3053 | pStreamEx->Out.cbPreBuffered = 0;
|
---|
3054 | pStreamEx->Out.offPreBuf = 0;
|
---|
3055 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PLAY;
|
---|
3056 |
|
---|
3057 | if (cbBuf > 0)
|
---|
3058 | {
|
---|
3059 | uint32_t cbWritten2 = 0;
|
---|
3060 | rc = drvAudioStreamPlayLocked(pThis, pStreamEx, pbBuf, cbBuf, &cbWritten2);
|
---|
3061 | if (RT_SUCCESS(rc))
|
---|
3062 | *pcbWritten += cbWritten2;
|
---|
3063 | }
|
---|
3064 | else
|
---|
3065 | pStreamEx->nsLastPlayedCaptured = RTTimeNanoTS();
|
---|
3066 | }
|
---|
3067 | else
|
---|
3068 | {
|
---|
3069 | if (cbLeft != pStreamEx->Out.cbPreBuffered)
|
---|
3070 | pStreamEx->nsLastPlayedCaptured = RTTimeNanoTS();
|
---|
3071 |
|
---|
3072 | LogRel2(("Audio: @%#RX64: Stream '%s' pre-buffering commit problem: wrote %#x out of %#x + %#x - rc=%Rrc *pcbWritten=%#x %s -> PREBUF_COMMITTING\n",
|
---|
3073 | pStreamEx->offInternal, pStreamEx->Core.Cfg.szName, pStreamEx->Out.cbPreBuffered - cbLeft,
|
---|
3074 | pStreamEx->Out.cbPreBuffered, cbBuf, rc, *pcbWritten, drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
3075 | AssertMsg( pStreamEx->Out.enmPlayState == DRVAUDIOPLAYSTATE_PREBUF_COMMITTING
|
---|
3076 | || pStreamEx->Out.enmPlayState == DRVAUDIOPLAYSTATE_PREBUF
|
---|
3077 | || RT_FAILURE(rc),
|
---|
3078 | ("Buggy host driver buffer reporting? cbLeft=%#x cbPreBuffered=%#x enmPlayState=%s\n",
|
---|
3079 | cbLeft, pStreamEx->Out.cbPreBuffered, drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
3080 |
|
---|
3081 | pStreamEx->Out.cbPreBuffered = cbLeft;
|
---|
3082 | pStreamEx->Out.offPreBuf = off;
|
---|
3083 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF_COMMITTING;
|
---|
3084 | }
|
---|
3085 |
|
---|
3086 | return *pcbWritten ? VINF_SUCCESS : rc;
|
---|
3087 | }
|
---|
3088 |
|
---|
3089 |
|
---|
3090 | /**
|
---|
3091 | * Does one iteration of an audio stream.
|
---|
3092 | *
|
---|
3093 | * This function gives the backend the chance of iterating / altering data and
|
---|
3094 | * does the actual mixing between the guest <-> host mixing buffers.
|
---|
3095 | *
|
---|
3096 | * @returns VBox status code.
|
---|
3097 | * @param pThis Pointer to driver instance.
|
---|
3098 | * @param pStreamEx Stream to iterate.
|
---|
3099 | */
|
---|
3100 | static int drvAudioStreamIterateInternal(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx)
|
---|
3101 | {
|
---|
3102 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
3103 |
|
---|
3104 | #ifdef LOG_ENABLED
|
---|
3105 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
3106 | #endif
|
---|
3107 | Log3Func(("[%s] fStatus=%s\n", pStreamEx->Core.Cfg.szName, drvAudioStreamStatusToStr(szStreamSts, pStreamEx->fStatus)));
|
---|
3108 |
|
---|
3109 | /* Not enabled or paused? Skip iteration. */
|
---|
3110 | if ((pStreamEx->fStatus & (PDMAUDIOSTREAM_STS_ENABLED | PDMAUDIOSTREAM_STS_PAUSED)) != PDMAUDIOSTREAM_STS_ENABLED)
|
---|
3111 | return VINF_SUCCESS;
|
---|
3112 |
|
---|
3113 | /*
|
---|
3114 | * Pending disable is really what we're here for.
|
---|
3115 | *
|
---|
3116 | * This only happens to output streams. We ASSUME the caller (MixerBuffer)
|
---|
3117 | * implements a timeout on the draining, so we skip that here.
|
---|
3118 | */
|
---|
3119 | if (!(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_PENDING_DISABLE))
|
---|
3120 | { /* likely until we get to the end of the stream at least. */ }
|
---|
3121 | else
|
---|
3122 | {
|
---|
3123 | AssertReturn(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT, VINF_SUCCESS);
|
---|
3124 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3125 |
|
---|
3126 | /*
|
---|
3127 | * Move pre-buffered samples to the backend.
|
---|
3128 | */
|
---|
3129 | if (pStreamEx->Out.enmPlayState == DRVAUDIOPLAYSTATE_PREBUF_COMMITTING)
|
---|
3130 | {
|
---|
3131 | if (pStreamEx->Out.cbPreBuffered > 0)
|
---|
3132 | {
|
---|
3133 | uint32_t cbIgnored = 0;
|
---|
3134 | drvAudioStreamPreBufComitting(pThis, pStreamEx, NULL, 0, &cbIgnored);
|
---|
3135 | Log3Func(("Stream '%s': Transferred %#x bytes\n", pStreamEx->Core.Cfg.szName, cbIgnored));
|
---|
3136 | }
|
---|
3137 | if (pStreamEx->Out.cbPreBuffered == 0)
|
---|
3138 | {
|
---|
3139 | Log3Func(("Stream '%s': No more pre-buffered data -> NOPLAY + backend DRAIN\n", pStreamEx->Core.Cfg.szName));
|
---|
3140 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_NOPLAY;
|
---|
3141 |
|
---|
3142 | int rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DRAIN);
|
---|
3143 | if (RT_FAILURE(rc))
|
---|
3144 | {
|
---|
3145 | LogFunc(("Stream '%s': Backend DRAIN failed with %Rrc, disabling the stream instead...\n",
|
---|
3146 | pStreamEx->Core.Cfg.szName, rc));
|
---|
3147 | rc = drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
3148 | AssertRC(rc);
|
---|
3149 | drvAudioStreamResetOnDisable(pStreamEx);
|
---|
3150 | }
|
---|
3151 | }
|
---|
3152 | }
|
---|
3153 | else
|
---|
3154 | Assert(pStreamEx->Out.enmPlayState == DRVAUDIOPLAYSTATE_NOPLAY);
|
---|
3155 |
|
---|
3156 | /*
|
---|
3157 | * Check the backend status to see if it's still draining and to
|
---|
3158 | * update our status when it stops doing so.
|
---|
3159 | */
|
---|
3160 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
3161 | if (enmBackendState == PDMHOSTAUDIOSTREAMSTATE_DRAINING)
|
---|
3162 | {
|
---|
3163 | uint32_t cbIgnored = 0;
|
---|
3164 | pThis->pHostDrvAudio->pfnStreamPlay(pThis->pHostDrvAudio, pStreamEx->pBackend, NULL, 0, &cbIgnored);
|
---|
3165 | }
|
---|
3166 | else
|
---|
3167 | {
|
---|
3168 | LogFunc(("Stream '%s': Backend finished draining.\n", pStreamEx->Core.Cfg.szName));
|
---|
3169 | drvAudioStreamResetOnDisable(pStreamEx);
|
---|
3170 | }
|
---|
3171 |
|
---|
3172 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3173 | }
|
---|
3174 |
|
---|
3175 | /* Update timestamps. */
|
---|
3176 | pStreamEx->nsLastIterated = RTTimeNanoTS();
|
---|
3177 |
|
---|
3178 | return VINF_SUCCESS; /** @todo r=bird: What can the caller do with an error status here? */
|
---|
3179 | }
|
---|
3180 |
|
---|
3181 |
|
---|
3182 | /**
|
---|
3183 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamIterate}
|
---|
3184 | */
|
---|
3185 | static DECLCALLBACK(int) drvAudioStreamIterate(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
3186 | {
|
---|
3187 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
3188 | AssertPtr(pThis);
|
---|
3189 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
3190 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
3191 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3192 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3193 |
|
---|
3194 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3195 | AssertRCReturn(rc, rc);
|
---|
3196 |
|
---|
3197 | rc = drvAudioStreamIterateInternal(pThis, pStreamEx);
|
---|
3198 |
|
---|
3199 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3200 |
|
---|
3201 | if (RT_FAILURE(rc))
|
---|
3202 | LogFlowFuncLeaveRC(rc);
|
---|
3203 | return rc;
|
---|
3204 | }
|
---|
3205 |
|
---|
3206 |
|
---|
3207 | /**
|
---|
3208 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamGetState}
|
---|
3209 | */
|
---|
3210 | static DECLCALLBACK(PDMAUDIOSTREAMSTATE) drvAudioStreamGetState(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
3211 | {
|
---|
3212 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
3213 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
3214 | AssertPtrReturn(pStreamEx, PDMAUDIOSTREAMSTATE_INVALID);
|
---|
3215 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, PDMAUDIOSTREAMSTATE_INVALID);
|
---|
3216 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, PDMAUDIOSTREAMSTATE_INVALID);
|
---|
3217 |
|
---|
3218 | /*
|
---|
3219 | * Get the status mask.
|
---|
3220 | */
|
---|
3221 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3222 | AssertRCReturn(rc, PDMAUDIOSTREAMSTATE_INVALID);
|
---|
3223 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3224 |
|
---|
3225 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendStateAndProcessChanges(pThis, pStreamEx);
|
---|
3226 | uint32_t const fStrmStatus = pStreamEx->fStatus;
|
---|
3227 | PDMAUDIODIR const enmDir = pStreamEx->Core.Cfg.enmDir;
|
---|
3228 | Assert(enmDir == PDMAUDIODIR_IN || enmDir == PDMAUDIODIR_OUT);
|
---|
3229 |
|
---|
3230 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3231 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3232 |
|
---|
3233 | /*
|
---|
3234 | * Translate it to state enum value.
|
---|
3235 | */
|
---|
3236 | PDMAUDIOSTREAMSTATE enmState;
|
---|
3237 | if (!(fStrmStatus & PDMAUDIOSTREAM_STS_NEED_REINIT))
|
---|
3238 | {
|
---|
3239 | if (fStrmStatus & PDMAUDIOSTREAM_STS_BACKEND_CREATED)
|
---|
3240 | {
|
---|
3241 | if ( (fStrmStatus & PDMAUDIOSTREAM_STS_ENABLED)
|
---|
3242 | && (enmDir == PDMAUDIODIR_IN ? pThis->In.fEnabled : pThis->Out.fEnabled)
|
---|
3243 | && ( enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY
|
---|
3244 | || enmBackendState == PDMHOSTAUDIOSTREAMSTATE_DRAINING
|
---|
3245 | || enmBackendState == PDMHOSTAUDIOSTREAMSTATE_INITIALIZING ))
|
---|
3246 | enmState = enmDir == PDMAUDIODIR_IN ? PDMAUDIOSTREAMSTATE_ENABLED_READABLE : PDMAUDIOSTREAMSTATE_ENABLED_WRITABLE;
|
---|
3247 | else
|
---|
3248 | enmState = PDMAUDIOSTREAMSTATE_INACTIVE;
|
---|
3249 | }
|
---|
3250 | else
|
---|
3251 | enmState = PDMAUDIOSTREAMSTATE_NOT_WORKING;
|
---|
3252 | }
|
---|
3253 | else
|
---|
3254 | enmState = PDMAUDIOSTREAMSTATE_NEED_REINIT;
|
---|
3255 |
|
---|
3256 | #ifdef LOG_ENABLED
|
---|
3257 | char szStreamSts[DRVAUDIO_STATUS_STR_MAX];
|
---|
3258 | #endif
|
---|
3259 | Log3Func(("[%s] returns %s (status: %s)\n", pStreamEx->Core.Cfg.szName, PDMAudioStreamStateGetName(enmState),
|
---|
3260 | drvAudioStreamStatusToStr(szStreamSts, fStrmStatus)));
|
---|
3261 | return enmState;
|
---|
3262 | }
|
---|
3263 |
|
---|
3264 |
|
---|
3265 | /**
|
---|
3266 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamGetWritable}
|
---|
3267 | */
|
---|
3268 | static DECLCALLBACK(uint32_t) drvAudioStreamGetWritable(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
3269 | {
|
---|
3270 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
3271 | AssertPtr(pThis);
|
---|
3272 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
3273 | AssertPtrReturn(pStreamEx, 0);
|
---|
3274 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, 0);
|
---|
3275 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, 0);
|
---|
3276 | AssertMsgReturn(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT, ("Can't write to a non-output stream\n"), 0);
|
---|
3277 |
|
---|
3278 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3279 | AssertRCReturn(rc, 0);
|
---|
3280 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3281 |
|
---|
3282 | /*
|
---|
3283 | * Use the playback and backend states to determin how much can be written, if anything.
|
---|
3284 | */
|
---|
3285 | uint32_t cbWritable = 0;
|
---|
3286 | DRVAUDIOPLAYSTATE const enmPlayMode = pStreamEx->Out.enmPlayState;
|
---|
3287 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx);
|
---|
3288 | if ( PDMAudioStrmStatusCanWrite(pStreamEx->fStatus)
|
---|
3289 | && pThis->pHostDrvAudio != NULL
|
---|
3290 | && enmBackendState != PDMHOSTAUDIOSTREAMSTATE_DRAINING)
|
---|
3291 | {
|
---|
3292 | switch (enmPlayMode)
|
---|
3293 | {
|
---|
3294 | /*
|
---|
3295 | * Whatever the backend can hold.
|
---|
3296 | */
|
---|
3297 | case DRVAUDIOPLAYSTATE_PLAY:
|
---|
3298 | case DRVAUDIOPLAYSTATE_PLAY_PREBUF:
|
---|
3299 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3300 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY /* potential unplug race */);
|
---|
3301 | cbWritable = pThis->pHostDrvAudio->pfnStreamGetWritable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
3302 | break;
|
---|
3303 |
|
---|
3304 | /*
|
---|
3305 | * Whatever we've got of available space in the pre-buffer.
|
---|
3306 | * Note! For the last round when we pass the pre-buffering threshold, we may
|
---|
3307 | * report fewer bytes than what a DMA timer period for the guest device
|
---|
3308 | * typically produces, however that should be transfered in the following
|
---|
3309 | * round that goes directly to the backend buffer.
|
---|
3310 | */
|
---|
3311 | case DRVAUDIOPLAYSTATE_PREBUF:
|
---|
3312 | cbWritable = pStreamEx->Out.cbPreBufAlloc - pStreamEx->Out.cbPreBuffered;
|
---|
3313 | if (!cbWritable)
|
---|
3314 | cbWritable = PDMAudioPropsFramesToBytes(&pStreamEx->Core.Cfg.Props, 2);
|
---|
3315 | break;
|
---|
3316 |
|
---|
3317 | /*
|
---|
3318 | * These are slightly more problematic and can go wrong if the pre-buffer is
|
---|
3319 | * manually configured to be smaller than the output of a typeical DMA timer
|
---|
3320 | * period for the guest device. So, to overcompensate, we just report back
|
---|
3321 | * the backend buffer size (the pre-buffer is circular, so no overflow issue).
|
---|
3322 | */
|
---|
3323 | case DRVAUDIOPLAYSTATE_PREBUF_OVERDUE:
|
---|
3324 | case DRVAUDIOPLAYSTATE_PREBUF_SWITCHING:
|
---|
3325 | cbWritable = PDMAudioPropsFramesToBytes(&pStreamEx->Core.Cfg.Props,
|
---|
3326 | RT_MAX(pStreamEx->Core.Cfg.Backend.cFramesBufferSize,
|
---|
3327 | pStreamEx->Core.Cfg.Backend.cFramesPreBuffering));
|
---|
3328 | break;
|
---|
3329 |
|
---|
3330 | case DRVAUDIOPLAYSTATE_PREBUF_COMMITTING:
|
---|
3331 | {
|
---|
3332 | /* Buggy backend: We weren't able to copy all the pre-buffered data to it
|
---|
3333 | when reaching the threshold. Try escape this situation, or at least
|
---|
3334 | keep the extra buffering to a minimum. We must try write something
|
---|
3335 | as long as there is space for it, as we need the pfnStreamWrite call
|
---|
3336 | to move the data. */
|
---|
3337 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3338 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY /* potential unplug race */);
|
---|
3339 | uint32_t const cbMin = PDMAudioPropsFramesToBytes(&pStreamEx->Core.Cfg.Props, 8);
|
---|
3340 | cbWritable = pThis->pHostDrvAudio->pfnStreamGetWritable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
3341 | if (cbWritable >= pStreamEx->Out.cbPreBuffered + cbMin)
|
---|
3342 | cbWritable -= pStreamEx->Out.cbPreBuffered + cbMin / 2;
|
---|
3343 | else
|
---|
3344 | cbWritable = RT_MIN(cbMin, pStreamEx->Out.cbPreBufAlloc - pStreamEx->Out.cbPreBuffered);
|
---|
3345 | AssertLogRel(cbWritable);
|
---|
3346 | break;
|
---|
3347 | }
|
---|
3348 |
|
---|
3349 | case DRVAUDIOPLAYSTATE_NOPLAY:
|
---|
3350 | break;
|
---|
3351 | case DRVAUDIOPLAYSTATE_INVALID:
|
---|
3352 | case DRVAUDIOPLAYSTATE_END:
|
---|
3353 | AssertFailed();
|
---|
3354 | break;
|
---|
3355 | }
|
---|
3356 |
|
---|
3357 | /* Make sure to align the writable size to the host's frame size. */
|
---|
3358 | cbWritable = PDMAudioPropsFloorBytesToFrame(&pStreamEx->Core.Cfg.Props, cbWritable);
|
---|
3359 | }
|
---|
3360 |
|
---|
3361 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3362 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3363 | Log3Func(("[%s] cbWritable=%#RX32 (%RU64ms) enmPlayMode=%s enmBackendState=%s\n",
|
---|
3364 | pStreamEx->Core.Cfg.szName, cbWritable, PDMAudioPropsBytesToMilli(&pStreamEx->Core.Cfg.Props, cbWritable),
|
---|
3365 | drvAudioPlayStateName(enmPlayMode), PDMHostAudioStreamStateGetName(enmBackendState) ));
|
---|
3366 | return cbWritable;
|
---|
3367 | }
|
---|
3368 |
|
---|
3369 |
|
---|
3370 | /**
|
---|
3371 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamPlay}
|
---|
3372 | */
|
---|
3373 | static DECLCALLBACK(int) drvAudioStreamPlay(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream,
|
---|
3374 | const void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten)
|
---|
3375 | {
|
---|
3376 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
3377 | AssertPtr(pThis);
|
---|
3378 |
|
---|
3379 | /*
|
---|
3380 | * Check input and sanity.
|
---|
3381 | */
|
---|
3382 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
3383 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
3384 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
3385 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
3386 | AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
3387 | uint32_t uTmp;
|
---|
3388 | if (pcbWritten)
|
---|
3389 | AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
|
---|
3390 | else
|
---|
3391 | pcbWritten = &uTmp;
|
---|
3392 |
|
---|
3393 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3394 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3395 | AssertMsgReturn(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT,
|
---|
3396 | ("Stream '%s' is not an output stream and therefore cannot be written to (direction is '%s')\n",
|
---|
3397 | pStreamEx->Core.Cfg.szName, PDMAudioDirGetName(pStreamEx->Core.Cfg.enmDir)), VERR_ACCESS_DENIED);
|
---|
3398 |
|
---|
3399 | AssertMsg(PDMAudioPropsIsSizeAligned(&pStreamEx->Core.Cfg.Props, cbBuf),
|
---|
3400 | ("Stream '%s' got a non-frame-aligned write (%#RX32 bytes)\n", pStreamEx->Core.Cfg.szName, cbBuf));
|
---|
3401 |
|
---|
3402 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3403 | AssertRCReturn(rc, rc);
|
---|
3404 |
|
---|
3405 | /*
|
---|
3406 | * First check that we can write to the stream, and if not,
|
---|
3407 | * whether to just drop the input into the bit bucket.
|
---|
3408 | */
|
---|
3409 | if (PDMAudioStrmStatusIsReady(pStreamEx->fStatus))
|
---|
3410 | {
|
---|
3411 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3412 | if ( pThis->Out.fEnabled /* (see @bugref{9882}) */
|
---|
3413 | && pThis->pHostDrvAudio != NULL)
|
---|
3414 | {
|
---|
3415 | /*
|
---|
3416 | * Get the backend state and process changes to it since last time we checked.
|
---|
3417 | */
|
---|
3418 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendStateAndProcessChanges(pThis, pStreamEx);
|
---|
3419 |
|
---|
3420 | /*
|
---|
3421 | * Do the transfering.
|
---|
3422 | */
|
---|
3423 | switch (pStreamEx->Out.enmPlayState)
|
---|
3424 | {
|
---|
3425 | case DRVAUDIOPLAYSTATE_PLAY:
|
---|
3426 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3427 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY);
|
---|
3428 | rc = drvAudioStreamPlayLocked(pThis, pStreamEx, (uint8_t const *)pvBuf, cbBuf, pcbWritten);
|
---|
3429 | break;
|
---|
3430 |
|
---|
3431 | case DRVAUDIOPLAYSTATE_PLAY_PREBUF:
|
---|
3432 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3433 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY);
|
---|
3434 | rc = drvAudioStreamPlayLocked(pThis, pStreamEx, (uint8_t const *)pvBuf, cbBuf, pcbWritten);
|
---|
3435 | drvAudioStreamPreBuffer(pStreamEx, (uint8_t const *)pvBuf, *pcbWritten, pStreamEx->cbPreBufThreshold);
|
---|
3436 | break;
|
---|
3437 |
|
---|
3438 | case DRVAUDIOPLAYSTATE_PREBUF:
|
---|
3439 | if (cbBuf + pStreamEx->Out.cbPreBuffered < pStreamEx->cbPreBufThreshold)
|
---|
3440 | rc = drvAudioStreamPlayToPreBuffer(pStreamEx, pvBuf, cbBuf, pStreamEx->cbPreBufThreshold, pcbWritten);
|
---|
3441 | else if ( enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY
|
---|
3442 | && (pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY))
|
---|
3443 | {
|
---|
3444 | Log3Func(("[%s] Pre-buffering completing: cbBuf=%#x cbPreBuffered=%#x => %#x vs cbPreBufThreshold=%#x\n",
|
---|
3445 | pStreamEx->Core.Cfg.szName, cbBuf, pStreamEx->Out.cbPreBuffered,
|
---|
3446 | cbBuf + pStreamEx->Out.cbPreBuffered, pStreamEx->cbPreBufThreshold));
|
---|
3447 | rc = drvAudioStreamPreBufComitting(pThis, pStreamEx, (uint8_t const *)pvBuf, cbBuf, pcbWritten);
|
---|
3448 | }
|
---|
3449 | else
|
---|
3450 | {
|
---|
3451 | Log3Func(("[%s] Pre-buffering completing but device not ready: cbBuf=%#x cbPreBuffered=%#x => %#x vs cbPreBufThreshold=%#x; PREBUF -> PREBUF_OVERDUE\n",
|
---|
3452 | pStreamEx->Core.Cfg.szName, cbBuf, pStreamEx->Out.cbPreBuffered,
|
---|
3453 | cbBuf + pStreamEx->Out.cbPreBuffered, pStreamEx->cbPreBufThreshold));
|
---|
3454 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF_OVERDUE;
|
---|
3455 | rc = drvAudioStreamPlayToPreBuffer(pStreamEx, pvBuf, cbBuf, pStreamEx->cbPreBufThreshold, pcbWritten);
|
---|
3456 | }
|
---|
3457 | break;
|
---|
3458 |
|
---|
3459 | case DRVAUDIOPLAYSTATE_PREBUF_OVERDUE:
|
---|
3460 | Assert( !(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY)
|
---|
3461 | || enmBackendState != PDMHOSTAUDIOSTREAMSTATE_OKAY);
|
---|
3462 | RT_FALL_THRU();
|
---|
3463 | case DRVAUDIOPLAYSTATE_PREBUF_SWITCHING:
|
---|
3464 | rc = drvAudioStreamPlayToPreBuffer(pStreamEx, pvBuf, cbBuf, pStreamEx->cbPreBufThreshold, pcbWritten);
|
---|
3465 | break;
|
---|
3466 |
|
---|
3467 | case DRVAUDIOPLAYSTATE_PREBUF_COMMITTING:
|
---|
3468 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3469 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY);
|
---|
3470 | rc = drvAudioStreamPreBufComitting(pThis, pStreamEx, (uint8_t const *)pvBuf, cbBuf, pcbWritten);
|
---|
3471 | break;
|
---|
3472 |
|
---|
3473 | case DRVAUDIOPLAYSTATE_NOPLAY:
|
---|
3474 | *pcbWritten = cbBuf;
|
---|
3475 | pStreamEx->offInternal += cbBuf;
|
---|
3476 | Log3Func(("[%s] Discarding the data, backend state: %s\n", pStreamEx->Core.Cfg.szName,
|
---|
3477 | PDMHostAudioStreamStateGetName(enmBackendState) ));
|
---|
3478 | break;
|
---|
3479 |
|
---|
3480 | default:
|
---|
3481 | *pcbWritten = cbBuf;
|
---|
3482 | AssertMsgFailedBreak(("%d; cbBuf=%#x\n", pStreamEx->Out.enmPlayState, cbBuf));
|
---|
3483 | }
|
---|
3484 |
|
---|
3485 | if (!pThis->CfgOut.Dbg.fEnabled || RT_FAILURE(rc))
|
---|
3486 | { /* likely */ }
|
---|
3487 | else
|
---|
3488 | AudioHlpFileWrite(pStreamEx->Out.Dbg.pFilePlay, pvBuf, *pcbWritten, 0 /* fFlags */);
|
---|
3489 | }
|
---|
3490 | else
|
---|
3491 | {
|
---|
3492 | *pcbWritten = cbBuf;
|
---|
3493 | pStreamEx->offInternal += cbBuf;
|
---|
3494 | Log3Func(("[%s] Backend stream %s, discarding the data\n", pStreamEx->Core.Cfg.szName,
|
---|
3495 | !pThis->Out.fEnabled ? "disabled" : !pThis->pHostDrvAudio ? "not attached" : "not ready yet"));
|
---|
3496 | }
|
---|
3497 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3498 | }
|
---|
3499 | else
|
---|
3500 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
3501 |
|
---|
3502 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3503 | return rc;
|
---|
3504 | }
|
---|
3505 |
|
---|
3506 |
|
---|
3507 | /**
|
---|
3508 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamGetReadable}
|
---|
3509 | */
|
---|
3510 | static DECLCALLBACK(uint32_t) drvAudioStreamGetReadable(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream)
|
---|
3511 | {
|
---|
3512 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
3513 | AssertPtr(pThis);
|
---|
3514 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
3515 | AssertPtrReturn(pStreamEx, 0);
|
---|
3516 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, 0);
|
---|
3517 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, 0);
|
---|
3518 | AssertMsg(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN, ("Can't read from a non-input stream\n"));
|
---|
3519 |
|
---|
3520 |
|
---|
3521 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3522 | AssertRCReturn(rc, 0);
|
---|
3523 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3524 |
|
---|
3525 | /*
|
---|
3526 | * Use the capture state to determin how much can be written, if anything.
|
---|
3527 | */
|
---|
3528 | uint32_t cbReadable = 0;
|
---|
3529 | DRVAUDIOCAPTURESTATE const enmCaptureState = pStreamEx->In.enmCaptureState;
|
---|
3530 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendState(pThis, pStreamEx); RT_NOREF(enmBackendState);
|
---|
3531 | if ( PDMAudioStrmStatusCanRead(pStreamEx->fStatus)
|
---|
3532 | && pThis->pHostDrvAudio != NULL)
|
---|
3533 | {
|
---|
3534 | switch (enmCaptureState)
|
---|
3535 | {
|
---|
3536 | /*
|
---|
3537 | * Whatever the backend has to offer when in capture mode.
|
---|
3538 | */
|
---|
3539 | case DRVAUDIOCAPTURESTATE_CAPTURING:
|
---|
3540 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3541 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY /* potential unplug race */);
|
---|
3542 | cbReadable = pThis->pHostDrvAudio->pfnStreamGetReadable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
3543 | break;
|
---|
3544 |
|
---|
3545 | /*
|
---|
3546 | * Same calculation as in drvAudioStreamCaptureSilence, only we cap it
|
---|
3547 | * at the pre-buffering threshold so we don't get into trouble when we
|
---|
3548 | * switch to capture mode between now and pfnStreamCapture.
|
---|
3549 | */
|
---|
3550 | case DRVAUDIOCAPTURESTATE_PREBUF:
|
---|
3551 | {
|
---|
3552 | uint64_t const cNsStream = RTTimeNanoTS() - pStreamEx->nsStarted;
|
---|
3553 | uint64_t const offCur = PDMAudioPropsNanoToBytes64(&pStreamEx->Core.Cfg.Props, cNsStream);
|
---|
3554 | if (offCur > pStreamEx->offInternal)
|
---|
3555 | {
|
---|
3556 | uint64_t const cbUnread = offCur - pStreamEx->offInternal;
|
---|
3557 | cbReadable = (uint32_t)RT_MIN(pStreamEx->cbPreBufThreshold, cbUnread);
|
---|
3558 | }
|
---|
3559 | break;
|
---|
3560 | }
|
---|
3561 |
|
---|
3562 | case DRVAUDIOCAPTURESTATE_NO_CAPTURE:
|
---|
3563 | break;
|
---|
3564 |
|
---|
3565 | case DRVAUDIOCAPTURESTATE_INVALID:
|
---|
3566 | case DRVAUDIOCAPTURESTATE_END:
|
---|
3567 | AssertFailed();
|
---|
3568 | break;
|
---|
3569 | }
|
---|
3570 |
|
---|
3571 | /* Make sure to align the readable size to the host's frame size. */
|
---|
3572 | cbReadable = PDMAudioPropsFloorBytesToFrame(&pStreamEx->Core.Cfg.Props, cbReadable);
|
---|
3573 | }
|
---|
3574 |
|
---|
3575 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3576 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3577 | Log3Func(("[%s] cbReadable=%#RX32 (%RU64ms) enmCaptureMode=%s enmBackendState=%s\n",
|
---|
3578 | pStreamEx->Core.Cfg.szName, cbReadable, PDMAudioPropsBytesToMilli(&pStreamEx->Core.Cfg.Props, cbReadable),
|
---|
3579 | drvAudioCaptureStateName(enmCaptureState), PDMHostAudioStreamStateGetName(enmBackendState) ));
|
---|
3580 | return cbReadable;
|
---|
3581 | }
|
---|
3582 |
|
---|
3583 |
|
---|
3584 | /**
|
---|
3585 | * Worker for drvAudioStreamCapture that returns silence.
|
---|
3586 | *
|
---|
3587 | * The amount of silence returned is a function of how long the stream has been
|
---|
3588 | * enabled.
|
---|
3589 | *
|
---|
3590 | * @returns VINF_SUCCESS
|
---|
3591 | * @param pStreamEx The stream to commit the pre-buffering for.
|
---|
3592 | * @param pbBuf The output buffer.
|
---|
3593 | * @param cbBuf The size of the output buffer.
|
---|
3594 | * @param pcbRead Where to return the number of bytes actually read.
|
---|
3595 | */
|
---|
3596 | static int drvAudioStreamCaptureSilence(PDRVAUDIOSTREAM pStreamEx, uint8_t *pbBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
3597 | {
|
---|
3598 | /** @todo Does not take paused time into account... */
|
---|
3599 | uint64_t const cNsStream = RTTimeNanoTS() - pStreamEx->nsStarted;
|
---|
3600 | uint64_t const offCur = PDMAudioPropsNanoToBytes64(&pStreamEx->Core.Cfg.Props, cNsStream);
|
---|
3601 | if (offCur > pStreamEx->offInternal)
|
---|
3602 | {
|
---|
3603 | uint64_t const cbUnread = offCur - pStreamEx->offInternal;
|
---|
3604 | uint32_t const cbToClear = (uint32_t)RT_MIN(cbBuf, cbUnread);
|
---|
3605 | *pcbRead = cbToClear;
|
---|
3606 | pStreamEx->offInternal += cbToClear;
|
---|
3607 | cbBuf -= cbToClear;
|
---|
3608 | PDMAudioPropsClearBuffer(&pStreamEx->Core.Cfg.Props, pbBuf, cbToClear,
|
---|
3609 | PDMAudioPropsBytesToFrames(&pStreamEx->Core.Cfg.Props, cbToClear));
|
---|
3610 | }
|
---|
3611 | else
|
---|
3612 | *pcbRead = 0;
|
---|
3613 | Log4Func(("%s: @%#RX64: Read %#x bytes of silence (%#x bytes left)\n",
|
---|
3614 | pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, *pcbRead, cbBuf));
|
---|
3615 | return VINF_SUCCESS;
|
---|
3616 | }
|
---|
3617 |
|
---|
3618 |
|
---|
3619 | /**
|
---|
3620 | * Worker for drvAudioStreamCapture.
|
---|
3621 | */
|
---|
3622 | static int drvAudioStreamCaptureLocked(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx,
|
---|
3623 | uint8_t *pbBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
3624 | {
|
---|
3625 | Log4Func(("%s: @%#RX64: cbBuf=%#x\n", pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbBuf));
|
---|
3626 |
|
---|
3627 | uint32_t cbReadable = pThis->pHostDrvAudio->pfnStreamGetReadable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
3628 | pStreamEx->In.Stats.cbBackendReadableBefore = cbReadable;
|
---|
3629 |
|
---|
3630 | uint32_t cbRead = 0;
|
---|
3631 | int rc = VINF_SUCCESS;
|
---|
3632 | uint8_t const cbFrame = PDMAudioPropsFrameSize(&pStreamEx->Core.Cfg.Props);
|
---|
3633 | while (cbBuf >= cbFrame && cbReadable >= cbFrame)
|
---|
3634 | {
|
---|
3635 | uint32_t const cbToRead = PDMAudioPropsFloorBytesToFrame(&pStreamEx->Core.Cfg.Props, RT_MIN(cbBuf, cbReadable));
|
---|
3636 | uint32_t cbReadNow = 0;
|
---|
3637 | rc = pThis->pHostDrvAudio->pfnStreamCapture(pThis->pHostDrvAudio, pStreamEx->pBackend, pbBuf, cbToRead, &cbReadNow);
|
---|
3638 | if (RT_SUCCESS(rc))
|
---|
3639 | {
|
---|
3640 | if (cbReadNow != cbToRead)
|
---|
3641 | Log4Func(("%s: @%#RX64: Read fewer bytes than requested: %#x, requested %#x\n",
|
---|
3642 | pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbReadNow, cbToRead));
|
---|
3643 | #ifdef DEBUG_bird
|
---|
3644 | Assert(cbReadNow == cbToRead);
|
---|
3645 | #endif
|
---|
3646 | AssertStmt(cbReadNow <= cbToRead, cbReadNow = cbToRead);
|
---|
3647 | cbRead += cbReadNow;
|
---|
3648 | cbBuf -= cbReadNow;
|
---|
3649 | pbBuf += cbReadNow;
|
---|
3650 | pStreamEx->offInternal += cbReadNow;
|
---|
3651 | }
|
---|
3652 | else
|
---|
3653 | {
|
---|
3654 | *pcbRead = cbRead;
|
---|
3655 | LogFunc(("%s: @%#RX64: pfnStreamCapture failed read %#x bytes (%#x previous read, %#x readable): %Rrc\n",
|
---|
3656 | pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbToRead, cbRead, cbReadable, rc));
|
---|
3657 | return cbRead ? VINF_SUCCESS : rc;
|
---|
3658 | }
|
---|
3659 |
|
---|
3660 | cbReadable = pThis->pHostDrvAudio->pfnStreamGetReadable(pThis->pHostDrvAudio, pStreamEx->pBackend);
|
---|
3661 | }
|
---|
3662 |
|
---|
3663 | *pcbRead = cbRead;
|
---|
3664 | pStreamEx->In.Stats.cbBackendReadableAfter = cbReadable;
|
---|
3665 | if (cbRead)
|
---|
3666 | pStreamEx->nsLastPlayedCaptured = RTTimeNanoTS();
|
---|
3667 |
|
---|
3668 | Log4Func(("%s: @%#RX64: Read %#x bytes (%#x bytes left)\n", pStreamEx->Core.Cfg.szName, pStreamEx->offInternal, cbRead, cbBuf));
|
---|
3669 | return rc;
|
---|
3670 | }
|
---|
3671 |
|
---|
3672 |
|
---|
3673 | /**
|
---|
3674 | * @interface_method_impl{PDMIAUDIOCONNECTOR,pfnStreamCapture}
|
---|
3675 | */
|
---|
3676 | static DECLCALLBACK(int) drvAudioStreamCapture(PPDMIAUDIOCONNECTOR pInterface, PPDMAUDIOSTREAM pStream,
|
---|
3677 | void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
3678 | {
|
---|
3679 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IAudioConnector);
|
---|
3680 | AssertPtr(pThis);
|
---|
3681 |
|
---|
3682 | /*
|
---|
3683 | * Check input and sanity.
|
---|
3684 | */
|
---|
3685 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
3686 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream;
|
---|
3687 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
3688 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
3689 | AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
3690 | uint32_t uTmp;
|
---|
3691 | if (pcbRead)
|
---|
3692 | AssertPtrReturn(pcbRead, VERR_INVALID_PARAMETER);
|
---|
3693 | else
|
---|
3694 | pcbRead = &uTmp;
|
---|
3695 |
|
---|
3696 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3697 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3698 | AssertMsgReturn(pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_IN,
|
---|
3699 | ("Stream '%s' is not an input stream and therefore cannot be read from (direction is '%s')\n",
|
---|
3700 | pStreamEx->Core.Cfg.szName, PDMAudioDirGetName(pStreamEx->Core.Cfg.enmDir)), VERR_ACCESS_DENIED);
|
---|
3701 |
|
---|
3702 | AssertMsg(PDMAudioPropsIsSizeAligned(&pStreamEx->Core.Cfg.Props, cbBuf),
|
---|
3703 | ("Stream '%s' got a non-frame-aligned write (%#RX32 bytes)\n", pStreamEx->Core.Cfg.szName, cbBuf));
|
---|
3704 |
|
---|
3705 | int rc = RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3706 | AssertRCReturn(rc, rc);
|
---|
3707 |
|
---|
3708 | /*
|
---|
3709 | * First check that we can read from the stream, and if not,
|
---|
3710 | * whether to just drop the input into the bit bucket.
|
---|
3711 | */
|
---|
3712 | if (PDMAudioStrmStatusIsReady(pStreamEx->fStatus))
|
---|
3713 | {
|
---|
3714 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3715 | if ( pThis->In.fEnabled /* (see @bugref{9882}) */
|
---|
3716 | && pThis->pHostDrvAudio != NULL)
|
---|
3717 | {
|
---|
3718 | /*
|
---|
3719 | * Get the backend state and process changes to it since last time we checked.
|
---|
3720 | */
|
---|
3721 | PDMHOSTAUDIOSTREAMSTATE const enmBackendState = drvAudioStreamGetBackendStateAndProcessChanges(pThis, pStreamEx);
|
---|
3722 |
|
---|
3723 | /*
|
---|
3724 | * Do the transfering.
|
---|
3725 | */
|
---|
3726 | switch (pStreamEx->In.enmCaptureState)
|
---|
3727 | {
|
---|
3728 | case DRVAUDIOCAPTURESTATE_CAPTURING:
|
---|
3729 | Assert(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_BACKEND_READY);
|
---|
3730 | Assert(enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY);
|
---|
3731 | rc = drvAudioStreamCaptureLocked(pThis, pStreamEx, (uint8_t *)pvBuf, cbBuf, pcbRead);
|
---|
3732 | break;
|
---|
3733 |
|
---|
3734 | case DRVAUDIOCAPTURESTATE_PREBUF:
|
---|
3735 | if (enmBackendState == PDMHOSTAUDIOSTREAMSTATE_OKAY)
|
---|
3736 | {
|
---|
3737 | uint32_t const cbReadable = pThis->pHostDrvAudio->pfnStreamGetReadable(pThis->pHostDrvAudio,
|
---|
3738 | pStreamEx->pBackend);
|
---|
3739 | if (cbReadable >= pStreamEx->cbPreBufThreshold)
|
---|
3740 | {
|
---|
3741 | Log4Func(("[%s] Pre-buffering completed: cbReadable=%#x vs cbPreBufThreshold=%#x (cbBuf=%#x)\n",
|
---|
3742 | pStreamEx->Core.Cfg.szName, cbReadable, pStreamEx->cbPreBufThreshold, cbBuf));
|
---|
3743 | pStreamEx->In.enmCaptureState = DRVAUDIOCAPTURESTATE_CAPTURING;
|
---|
3744 | rc = drvAudioStreamCaptureLocked(pThis, pStreamEx, (uint8_t *)pvBuf, cbBuf, pcbRead);
|
---|
3745 | break;
|
---|
3746 | }
|
---|
3747 | pStreamEx->In.Stats.cbBackendReadableBefore = cbReadable;
|
---|
3748 | pStreamEx->In.Stats.cbBackendReadableAfter = cbReadable;
|
---|
3749 | Log4Func(("[%s] Pre-buffering: Got %#x out of %#x\n",
|
---|
3750 | pStreamEx->Core.Cfg.szName, cbReadable, pStreamEx->cbPreBufThreshold));
|
---|
3751 | }
|
---|
3752 | else
|
---|
3753 | Log4Func(("[%s] Pre-buffering: Backend status %s\n",
|
---|
3754 | pStreamEx->Core.Cfg.szName, PDMHostAudioStreamStateGetName(enmBackendState) ));
|
---|
3755 | drvAudioStreamCaptureSilence(pStreamEx, (uint8_t *)pvBuf, cbBuf, pcbRead);
|
---|
3756 | break;
|
---|
3757 |
|
---|
3758 | case DRVAUDIOCAPTURESTATE_NO_CAPTURE:
|
---|
3759 | *pcbRead = 0;
|
---|
3760 | Log4Func(("[%s] Not capturing - backend state: %s\n",
|
---|
3761 | pStreamEx->Core.Cfg.szName, PDMHostAudioStreamStateGetName(enmBackendState) ));
|
---|
3762 | break;
|
---|
3763 |
|
---|
3764 | default:
|
---|
3765 | *pcbRead = 0;
|
---|
3766 | AssertMsgFailedBreak(("%d; cbBuf=%#x\n", pStreamEx->In.enmCaptureState, cbBuf));
|
---|
3767 | }
|
---|
3768 |
|
---|
3769 | if (!pThis->CfgIn.Dbg.fEnabled || RT_FAILURE(rc))
|
---|
3770 | { /* likely */ }
|
---|
3771 | else
|
---|
3772 | AudioHlpFileWrite(pStreamEx->In.Dbg.pFileCapture, pvBuf, *pcbRead, 0 /* fFlags */);
|
---|
3773 | }
|
---|
3774 | else
|
---|
3775 | {
|
---|
3776 | *pcbRead = 0;
|
---|
3777 | Log4Func(("[%s] Backend stream %s, returning no data\n", pStreamEx->Core.Cfg.szName,
|
---|
3778 | !pThis->Out.fEnabled ? "disabled" : !pThis->pHostDrvAudio ? "not attached" : "not ready yet"));
|
---|
3779 | }
|
---|
3780 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3781 | }
|
---|
3782 | else
|
---|
3783 | rc = VERR_AUDIO_STREAM_NOT_READY;
|
---|
3784 |
|
---|
3785 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3786 | return rc;
|
---|
3787 | }
|
---|
3788 |
|
---|
3789 |
|
---|
3790 | /*********************************************************************************************************************************
|
---|
3791 | * PDMIHOSTAUDIOPORT interface implementation. *
|
---|
3792 | *********************************************************************************************************************************/
|
---|
3793 |
|
---|
3794 | /**
|
---|
3795 | * Worker for drvAudioHostPort_DoOnWorkerThread with stream argument, called on
|
---|
3796 | * worker thread.
|
---|
3797 | */
|
---|
3798 | static DECLCALLBACK(void) drvAudioHostPort_DoOnWorkerThreadStreamWorker(PDRVAUDIO pThis, PDRVAUDIOSTREAM pStreamEx,
|
---|
3799 | uintptr_t uUser, void *pvUser)
|
---|
3800 | {
|
---|
3801 | LogFlowFunc(("pThis=%p uUser=%#zx pvUser=%p\n", pThis, uUser, pvUser));
|
---|
3802 | AssertPtrReturnVoid(pThis);
|
---|
3803 | AssertPtrReturnVoid(pStreamEx);
|
---|
3804 | AssertReturnVoid(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC);
|
---|
3805 |
|
---|
3806 | /*
|
---|
3807 | * The CritSectHotPlug lock should not be needed here as detach will destroy
|
---|
3808 | * the thread pool. So, we'll leave taking the stream lock to the worker we're
|
---|
3809 | * calling as there are no lock order concerns.
|
---|
3810 | */
|
---|
3811 | PPDMIHOSTAUDIO const pIHostDrvAudio = pThis->pHostDrvAudio;
|
---|
3812 | AssertPtrReturnVoid(pIHostDrvAudio);
|
---|
3813 | AssertPtrReturnVoid(pIHostDrvAudio->pfnDoOnWorkerThread);
|
---|
3814 | pIHostDrvAudio->pfnDoOnWorkerThread(pIHostDrvAudio, pStreamEx->pBackend, uUser, pvUser);
|
---|
3815 |
|
---|
3816 | drvAudioStreamReleaseInternal(pThis, pStreamEx, true /*fMayDestroy*/);
|
---|
3817 | LogFlowFunc(("returns\n"));
|
---|
3818 | }
|
---|
3819 |
|
---|
3820 |
|
---|
3821 | /**
|
---|
3822 | * Worker for drvAudioHostPort_DoOnWorkerThread without stream argument, called
|
---|
3823 | * on worker thread.
|
---|
3824 | *
|
---|
3825 | * This wrapper isn't technically required, but it helps with logging and a few
|
---|
3826 | * extra sanity checks.
|
---|
3827 | */
|
---|
3828 | static DECLCALLBACK(void) drvAudioHostPort_DoOnWorkerThreadWorker(PDRVAUDIO pThis, uintptr_t uUser, void *pvUser)
|
---|
3829 | {
|
---|
3830 | LogFlowFunc(("pThis=%p uUser=%#zx pvUser=%p\n", pThis, uUser, pvUser));
|
---|
3831 | AssertPtrReturnVoid(pThis);
|
---|
3832 |
|
---|
3833 | /*
|
---|
3834 | * The CritSectHotPlug lock should not be needed here as detach will destroy
|
---|
3835 | * the thread pool.
|
---|
3836 | */
|
---|
3837 | PPDMIHOSTAUDIO const pIHostDrvAudio = pThis->pHostDrvAudio;
|
---|
3838 | AssertPtrReturnVoid(pIHostDrvAudio);
|
---|
3839 | AssertPtrReturnVoid(pIHostDrvAudio->pfnDoOnWorkerThread);
|
---|
3840 |
|
---|
3841 | pIHostDrvAudio->pfnDoOnWorkerThread(pIHostDrvAudio, NULL, uUser, pvUser);
|
---|
3842 |
|
---|
3843 | LogFlowFunc(("returns\n"));
|
---|
3844 | }
|
---|
3845 |
|
---|
3846 |
|
---|
3847 | /**
|
---|
3848 | * @interface_method_impl{PDMIHOSTAUDIOPORT,pfnDoOnWorkerThread}
|
---|
3849 | */
|
---|
3850 | static DECLCALLBACK(int) drvAudioHostPort_DoOnWorkerThread(PPDMIHOSTAUDIOPORT pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
3851 | uintptr_t uUser, void *pvUser)
|
---|
3852 | {
|
---|
3853 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IHostAudioPort);
|
---|
3854 | LogFlowFunc(("pStream=%p uUser=%#zx pvUser=%p\n", pStream, uUser, pvUser));
|
---|
3855 |
|
---|
3856 | /*
|
---|
3857 | * Assert some sanity.
|
---|
3858 | */
|
---|
3859 | PDRVAUDIOSTREAM pStreamEx;
|
---|
3860 | if (!pStream)
|
---|
3861 | pStreamEx = NULL;
|
---|
3862 | else
|
---|
3863 | {
|
---|
3864 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
3865 | AssertReturn(pStream->uMagic == PDMAUDIOBACKENDSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3866 | pStreamEx = (PDRVAUDIOSTREAM)pStream->pStream;
|
---|
3867 | AssertPtrReturn(pStreamEx, VERR_INVALID_POINTER);
|
---|
3868 | AssertReturn(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3869 | AssertReturn(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC, VERR_INVALID_MAGIC);
|
---|
3870 | }
|
---|
3871 |
|
---|
3872 | int rc = RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3873 | AssertRCReturn(rc, rc);
|
---|
3874 |
|
---|
3875 | Assert(pThis->hReqPool != NIL_RTREQPOOL);
|
---|
3876 | AssertPtr(pThis->pHostDrvAudio);
|
---|
3877 | if ( pThis->hReqPool != NIL_RTREQPOOL
|
---|
3878 | && pThis->pHostDrvAudio != NULL)
|
---|
3879 | {
|
---|
3880 | AssertPtr(pThis->pHostDrvAudio->pfnDoOnWorkerThread);
|
---|
3881 | if (pThis->pHostDrvAudio->pfnDoOnWorkerThread)
|
---|
3882 | {
|
---|
3883 | /*
|
---|
3884 | * Try do the work.
|
---|
3885 | */
|
---|
3886 | if (!pStreamEx)
|
---|
3887 | {
|
---|
3888 | rc = RTReqPoolCallEx(pThis->hReqPool, 0 /*cMillies*/, NULL /*phReq*/, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
3889 | (PFNRT)drvAudioHostPort_DoOnWorkerThreadWorker, 3, pThis, uUser, pvUser);
|
---|
3890 | AssertRC(rc);
|
---|
3891 | }
|
---|
3892 | else
|
---|
3893 | {
|
---|
3894 | uint32_t cRefs = drvAudioStreamRetainInternal(pStreamEx);
|
---|
3895 | if (cRefs != UINT32_MAX)
|
---|
3896 | {
|
---|
3897 | rc = RTReqPoolCallEx(pThis->hReqPool, 0 /*cMillies*/, NULL, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
3898 | (PFNRT)drvAudioHostPort_DoOnWorkerThreadStreamWorker,
|
---|
3899 | 4, pThis, pStreamEx, uUser, pvUser);
|
---|
3900 | AssertRC(rc);
|
---|
3901 | if (RT_FAILURE(rc))
|
---|
3902 | {
|
---|
3903 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3904 | drvAudioStreamReleaseInternal(pThis, pStreamEx, true /*fMayDestroy*/);
|
---|
3905 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3906 | }
|
---|
3907 | }
|
---|
3908 | else
|
---|
3909 | rc = VERR_INVALID_PARAMETER;
|
---|
3910 | }
|
---|
3911 | }
|
---|
3912 | else
|
---|
3913 | rc = VERR_INVALID_FUNCTION;
|
---|
3914 | }
|
---|
3915 | else
|
---|
3916 | rc = VERR_INVALID_STATE;
|
---|
3917 |
|
---|
3918 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3919 | LogFlowFunc(("returns %Rrc\n", rc));
|
---|
3920 | return rc;
|
---|
3921 | }
|
---|
3922 |
|
---|
3923 |
|
---|
3924 | /**
|
---|
3925 | * Marks a stream for re-init.
|
---|
3926 | */
|
---|
3927 | static void drvAudioStreamMarkNeedReInit(PDRVAUDIOSTREAM pStreamEx, const char *pszCaller)
|
---|
3928 | {
|
---|
3929 | LogFlow((LOG_FN_FMT ": Flagging %s for re-init.\n", pszCaller, pStreamEx->Core.Cfg.szName)); RT_NOREF(pszCaller);
|
---|
3930 | Assert(RTCritSectIsOwner(&pStreamEx->Core.CritSect));
|
---|
3931 |
|
---|
3932 | pStreamEx->fStatus |= PDMAUDIOSTREAM_STS_NEED_REINIT;
|
---|
3933 | PDMAUDIOSTREAM_STS_ASSERT_VALID(pStreamEx->fStatus);
|
---|
3934 | pStreamEx->cTriesReInit = 0;
|
---|
3935 | pStreamEx->nsLastReInit = 0;
|
---|
3936 | }
|
---|
3937 |
|
---|
3938 |
|
---|
3939 | /**
|
---|
3940 | * @interface_method_impl{PDMIHOSTAUDIOPORT,pfnNotifyDeviceChanged}
|
---|
3941 | */
|
---|
3942 | static DECLCALLBACK(void) drvAudioHostPort_NotifyDeviceChanged(PPDMIHOSTAUDIOPORT pInterface, PDMAUDIODIR enmDir, void *pvUser)
|
---|
3943 | {
|
---|
3944 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IHostAudioPort);
|
---|
3945 | AssertReturnVoid(enmDir == PDMAUDIODIR_IN || enmDir == PDMAUDIODIR_OUT);
|
---|
3946 | LogRel(("Audio: The %s device for %s is changing.\n", enmDir == PDMAUDIODIR_IN ? "input" : "output", pThis->BackendCfg.szName));
|
---|
3947 |
|
---|
3948 | /*
|
---|
3949 | * Grab the list lock in shared mode and do the work.
|
---|
3950 | */
|
---|
3951 | int rc = RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
3952 | AssertRCReturnVoid(rc);
|
---|
3953 |
|
---|
3954 | PDRVAUDIOSTREAM pStreamEx;
|
---|
3955 | RTListForEach(&pThis->LstStreams, pStreamEx, DRVAUDIOSTREAM, ListEntry)
|
---|
3956 | {
|
---|
3957 | if (pStreamEx->Core.Cfg.enmDir == enmDir)
|
---|
3958 | {
|
---|
3959 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
3960 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
3961 |
|
---|
3962 | if (pThis->pHostDrvAudio->pfnStreamNotifyDeviceChanged)
|
---|
3963 | {
|
---|
3964 | LogFlowFunc(("Calling pfnStreamNotifyDeviceChanged on %s, old backend state: %s...\n", pStreamEx->Core.Cfg.szName,
|
---|
3965 | PDMHostAudioStreamStateGetName(drvAudioStreamGetBackendState(pThis, pStreamEx)) ));
|
---|
3966 | pThis->pHostDrvAudio->pfnStreamNotifyDeviceChanged(pThis->pHostDrvAudio, pStreamEx->pBackend, pvUser);
|
---|
3967 | LogFlowFunc(("New stream backend state: %s\n",
|
---|
3968 | PDMHostAudioStreamStateGetName(drvAudioStreamGetBackendState(pThis, pStreamEx)) ));
|
---|
3969 | }
|
---|
3970 | else
|
---|
3971 | drvAudioStreamMarkNeedReInit(pStreamEx, __PRETTY_FUNCTION__);
|
---|
3972 |
|
---|
3973 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
3974 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
3975 | }
|
---|
3976 | }
|
---|
3977 |
|
---|
3978 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
3979 | }
|
---|
3980 |
|
---|
3981 |
|
---|
3982 | /**
|
---|
3983 | * @interface_method_impl{PDMIHOSTAUDIOPORT,pfnStreamNotifyPreparingDeviceSwitch}
|
---|
3984 | */
|
---|
3985 | static DECLCALLBACK(void) drvAudioHostPort_StreamNotifyPreparingDeviceSwitch(PPDMIHOSTAUDIOPORT pInterface,
|
---|
3986 | PPDMAUDIOBACKENDSTREAM pStream)
|
---|
3987 | {
|
---|
3988 | RT_NOREF(pInterface);
|
---|
3989 |
|
---|
3990 | /*
|
---|
3991 | * Backend stream to validated DrvAudio stream:
|
---|
3992 | */
|
---|
3993 | AssertPtrReturnVoid(pStream);
|
---|
3994 | AssertReturnVoid(pStream->uMagic == PDMAUDIOBACKENDSTREAM_MAGIC);
|
---|
3995 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream->pStream;
|
---|
3996 | AssertPtrReturnVoid(pStreamEx);
|
---|
3997 | AssertReturnVoid(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC);
|
---|
3998 | AssertReturnVoid(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC);
|
---|
3999 | LogFlowFunc(("pStreamEx=%p '%s'\n", pStreamEx, pStreamEx->Core.Cfg.szName));
|
---|
4000 |
|
---|
4001 | /*
|
---|
4002 | * Grab the lock and do switch the state (only needed for output streams for now).
|
---|
4003 | */
|
---|
4004 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
4005 | AssertReturnVoidStmt(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, RTCritSectLeave(&pStreamEx->Core.CritSect)); /* paranoia */
|
---|
4006 |
|
---|
4007 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
4008 | {
|
---|
4009 | if (pStreamEx->cbPreBufThreshold > 0)
|
---|
4010 | {
|
---|
4011 | DRVAUDIOPLAYSTATE const enmPlayState = pStreamEx->Out.enmPlayState;
|
---|
4012 | switch (enmPlayState)
|
---|
4013 | {
|
---|
4014 | case DRVAUDIOPLAYSTATE_PREBUF:
|
---|
4015 | case DRVAUDIOPLAYSTATE_PREBUF_OVERDUE:
|
---|
4016 | case DRVAUDIOPLAYSTATE_NOPLAY:
|
---|
4017 | case DRVAUDIOPLAYSTATE_PREBUF_COMMITTING: /* simpler */
|
---|
4018 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF_SWITCHING;
|
---|
4019 | break;
|
---|
4020 | case DRVAUDIOPLAYSTATE_PLAY:
|
---|
4021 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PLAY_PREBUF;
|
---|
4022 | break;
|
---|
4023 | case DRVAUDIOPLAYSTATE_PREBUF_SWITCHING:
|
---|
4024 | case DRVAUDIOPLAYSTATE_PLAY_PREBUF:
|
---|
4025 | break;
|
---|
4026 | /* no default */
|
---|
4027 | case DRVAUDIOPLAYSTATE_END:
|
---|
4028 | case DRVAUDIOPLAYSTATE_INVALID:
|
---|
4029 | break;
|
---|
4030 | }
|
---|
4031 | LogFunc(("%s -> %s\n", drvAudioPlayStateName(enmPlayState), drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
4032 | }
|
---|
4033 | else
|
---|
4034 | LogFunc(("No pre-buffering configured.\n"));
|
---|
4035 | }
|
---|
4036 | else
|
---|
4037 | LogFunc(("input stream, nothing to do.\n"));
|
---|
4038 |
|
---|
4039 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
4040 | }
|
---|
4041 |
|
---|
4042 |
|
---|
4043 | /**
|
---|
4044 | * @interface_method_impl{PDMIHOSTAUDIOPORT,pfnStreamNotifyDeviceChanged}
|
---|
4045 | */
|
---|
4046 | static DECLCALLBACK(void) drvAudioHostPort_StreamNotifyDeviceChanged(PPDMIHOSTAUDIOPORT pInterface,
|
---|
4047 | PPDMAUDIOBACKENDSTREAM pStream, bool fReInit)
|
---|
4048 | {
|
---|
4049 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IHostAudioPort);
|
---|
4050 |
|
---|
4051 | /*
|
---|
4052 | * Backend stream to validated DrvAudio stream:
|
---|
4053 | */
|
---|
4054 | AssertPtrReturnVoid(pStream);
|
---|
4055 | AssertReturnVoid(pStream->uMagic == PDMAUDIOBACKENDSTREAM_MAGIC);
|
---|
4056 | PDRVAUDIOSTREAM pStreamEx = (PDRVAUDIOSTREAM)pStream->pStream;
|
---|
4057 | AssertPtrReturnVoid(pStreamEx);
|
---|
4058 | AssertReturnVoid(pStreamEx->Core.uMagic == PDMAUDIOSTREAM_MAGIC);
|
---|
4059 | AssertReturnVoid(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC);
|
---|
4060 |
|
---|
4061 | /*
|
---|
4062 | * Grab the lock and do the requested work.
|
---|
4063 | */
|
---|
4064 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
4065 | AssertReturnVoidStmt(pStreamEx->uMagic == DRVAUDIOSTREAM_MAGIC, RTCritSectLeave(&pStreamEx->Core.CritSect)); /* paranoia */
|
---|
4066 |
|
---|
4067 | if (fReInit)
|
---|
4068 | drvAudioStreamMarkNeedReInit(pStreamEx, __PRETTY_FUNCTION__);
|
---|
4069 | else
|
---|
4070 | {
|
---|
4071 | /*
|
---|
4072 | * Adjust the stream state now that the device has (perhaps finally) been switched.
|
---|
4073 | *
|
---|
4074 | * For enabled output streams, we must update the play state. We could try commit
|
---|
4075 | * pre-buffered data here, but it's really not worth the hazzle and risk (don't
|
---|
4076 | * know which thread we're on, do we now).
|
---|
4077 | */
|
---|
4078 | AssertStmt(!(pStreamEx->fStatus & PDMAUDIOSTREAM_STS_NEED_REINIT),
|
---|
4079 | pStreamEx->fStatus &= ~PDMAUDIOSTREAM_STS_NEED_REINIT);
|
---|
4080 |
|
---|
4081 |
|
---|
4082 | if (pStreamEx->Core.Cfg.enmDir == PDMAUDIODIR_OUT)
|
---|
4083 | {
|
---|
4084 | DRVAUDIOPLAYSTATE const enmPlayState = pStreamEx->Out.enmPlayState;
|
---|
4085 | pStreamEx->Out.enmPlayState = DRVAUDIOPLAYSTATE_PREBUF;
|
---|
4086 | LogFunc(("%s: %s -> %s\n", pStreamEx->Core.Cfg.szName, drvAudioPlayStateName(enmPlayState),
|
---|
4087 | drvAudioPlayStateName(pStreamEx->Out.enmPlayState) ));
|
---|
4088 | RT_NOREF(enmPlayState);
|
---|
4089 | }
|
---|
4090 |
|
---|
4091 | /* Disable and then fully resync. */
|
---|
4092 | /** @todo This doesn't work quite reliably if we're in draining mode
|
---|
4093 | * (PENDING_DISABLE, so the backend needs to take care of that prior to calling
|
---|
4094 | * us. Sigh. The idea was to avoid extra state mess in the backend... */
|
---|
4095 | drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
4096 | drvAudioStreamUpdateBackendOnStatus(pThis, pStreamEx, "device changed");
|
---|
4097 | }
|
---|
4098 |
|
---|
4099 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
4100 | }
|
---|
4101 |
|
---|
4102 |
|
---|
4103 | #ifdef VBOX_WITH_AUDIO_ENUM
|
---|
4104 | /**
|
---|
4105 | * @callback_method_impl{FNTMTIMERDRV, Re-enumerate backend devices.}
|
---|
4106 | *
|
---|
4107 | * Used to do/trigger re-enumeration of backend devices with a delay after we
|
---|
4108 | * got notification as there can be further notifications following shortly
|
---|
4109 | * after the first one. Also good to get it of random COM/whatever threads.
|
---|
4110 | */
|
---|
4111 | static DECLCALLBACK(void) drvAudioEnumerateTimer(PPDMDRVINS pDrvIns, TMTIMERHANDLE hTimer, void *pvUser)
|
---|
4112 | {
|
---|
4113 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4114 | RT_NOREF(hTimer, pvUser);
|
---|
4115 |
|
---|
4116 | /* Try push the work over to the thread-pool if we've got one. */
|
---|
4117 | RTCritSectRwEnterShared(&pThis->CritSectHotPlug);
|
---|
4118 | if (pThis->hReqPool != NIL_RTREQPOOL)
|
---|
4119 | {
|
---|
4120 | int rc = RTReqPoolCallEx(pThis->hReqPool, 0 /*cMillies*/, NULL, RTREQFLAGS_VOID | RTREQFLAGS_NO_WAIT,
|
---|
4121 | (PFNRT)drvAudioDevicesEnumerateInternal,
|
---|
4122 | 3, pThis, true /*fLog*/, (PPDMAUDIOHOSTENUM)NULL /*pDevEnum*/);
|
---|
4123 | LogFunc(("RTReqPoolCallEx: %Rrc\n", rc));
|
---|
4124 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
4125 | if (RT_SUCCESS(rc))
|
---|
4126 | return;
|
---|
4127 | }
|
---|
4128 | else
|
---|
4129 | RTCritSectRwLeaveShared(&pThis->CritSectHotPlug);
|
---|
4130 |
|
---|
4131 | LogFunc(("Calling drvAudioDevicesEnumerateInternal...\n"));
|
---|
4132 | drvAudioDevicesEnumerateInternal(pThis, true /* fLog */, NULL /* pDevEnum */);
|
---|
4133 | }
|
---|
4134 | #endif /* VBOX_WITH_AUDIO_ENUM */
|
---|
4135 |
|
---|
4136 |
|
---|
4137 | /**
|
---|
4138 | * @interface_method_impl{PDMIHOSTAUDIOPORT,pfnNotifyDevicesChanged}
|
---|
4139 | */
|
---|
4140 | static DECLCALLBACK(void) drvAudioHostPort_NotifyDevicesChanged(PPDMIHOSTAUDIOPORT pInterface)
|
---|
4141 | {
|
---|
4142 | PDRVAUDIO pThis = RT_FROM_MEMBER(pInterface, DRVAUDIO, IHostAudioPort);
|
---|
4143 | LogRel(("Audio: Device configuration of driver '%s' has changed\n", pThis->BackendCfg.szName));
|
---|
4144 |
|
---|
4145 | #ifdef RT_OS_DARWIN /** @todo Remove legacy behaviour: */
|
---|
4146 | /* Mark all host streams to re-initialize. */
|
---|
4147 | int rc2 = RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
4148 | AssertRCReturnVoid(rc2);
|
---|
4149 | PDRVAUDIOSTREAM pStreamEx;
|
---|
4150 | RTListForEach(&pThis->LstStreams, pStreamEx, DRVAUDIOSTREAM, ListEntry)
|
---|
4151 | {
|
---|
4152 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
4153 | drvAudioStreamMarkNeedReInit(pStreamEx, __PRETTY_FUNCTION__);
|
---|
4154 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
4155 | }
|
---|
4156 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
4157 | #endif
|
---|
4158 |
|
---|
4159 | #ifdef VBOX_WITH_AUDIO_ENUM
|
---|
4160 | /*
|
---|
4161 | * Re-enumerate all host devices with a tiny delay to avoid re-doing this
|
---|
4162 | * when a bunch of changes happens at once (they typically do on windows).
|
---|
4163 | * We'll keep postponing it till it quiesces for a fraction of a second.
|
---|
4164 | */
|
---|
4165 | int rc = PDMDrvHlpTimerSetMillies(pThis->pDrvIns, pThis->hEnumTimer, RT_MS_1SEC / 3);
|
---|
4166 | AssertRC(rc);
|
---|
4167 | #endif
|
---|
4168 | }
|
---|
4169 |
|
---|
4170 |
|
---|
4171 | /*********************************************************************************************************************************
|
---|
4172 | * PDMIBASE interface implementation. *
|
---|
4173 | *********************************************************************************************************************************/
|
---|
4174 |
|
---|
4175 | /**
|
---|
4176 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
4177 | */
|
---|
4178 | static DECLCALLBACK(void *) drvAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
4179 | {
|
---|
4180 | LogFlowFunc(("pInterface=%p, pszIID=%s\n", pInterface, pszIID));
|
---|
4181 |
|
---|
4182 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
4183 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4184 |
|
---|
4185 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
4186 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIAUDIOCONNECTOR, &pThis->IAudioConnector);
|
---|
4187 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIOPORT, &pThis->IHostAudioPort);
|
---|
4188 |
|
---|
4189 | return NULL;
|
---|
4190 | }
|
---|
4191 |
|
---|
4192 |
|
---|
4193 | /*********************************************************************************************************************************
|
---|
4194 | * PDMDRVREG interface implementation. *
|
---|
4195 | *********************************************************************************************************************************/
|
---|
4196 |
|
---|
4197 | /**
|
---|
4198 | * Power Off notification.
|
---|
4199 | *
|
---|
4200 | * @param pDrvIns The driver instance data.
|
---|
4201 | */
|
---|
4202 | static DECLCALLBACK(void) drvAudioPowerOff(PPDMDRVINS pDrvIns)
|
---|
4203 | {
|
---|
4204 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4205 |
|
---|
4206 | LogFlowFuncEnter();
|
---|
4207 |
|
---|
4208 | /** @todo locking? */
|
---|
4209 | if (pThis->pHostDrvAudio) /* If not lower driver is configured, bail out. */
|
---|
4210 | {
|
---|
4211 | /*
|
---|
4212 | * Just destroy the host stream on the backend side.
|
---|
4213 | * The rest will either be destructed by the device emulation or
|
---|
4214 | * in drvAudioDestruct().
|
---|
4215 | */
|
---|
4216 | int rc = RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
4217 | AssertRCReturnVoid(rc);
|
---|
4218 |
|
---|
4219 | PDRVAUDIOSTREAM pStreamEx;
|
---|
4220 | RTListForEach(&pThis->LstStreams, pStreamEx, DRVAUDIOSTREAM, ListEntry)
|
---|
4221 | {
|
---|
4222 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
4223 | drvAudioStreamControlInternalBackend(pThis, pStreamEx, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
4224 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
4225 | }
|
---|
4226 |
|
---|
4227 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
4228 | }
|
---|
4229 |
|
---|
4230 | LogFlowFuncLeave();
|
---|
4231 | }
|
---|
4232 |
|
---|
4233 |
|
---|
4234 | /**
|
---|
4235 | * Detach notification.
|
---|
4236 | *
|
---|
4237 | * @param pDrvIns The driver instance data.
|
---|
4238 | * @param fFlags Detach flags.
|
---|
4239 | */
|
---|
4240 | static DECLCALLBACK(void) drvAudioDetach(PPDMDRVINS pDrvIns, uint32_t fFlags)
|
---|
4241 | {
|
---|
4242 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
4243 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4244 | RT_NOREF(fFlags);
|
---|
4245 |
|
---|
4246 | int rc = RTCritSectRwEnterExcl(&pThis->CritSectHotPlug);
|
---|
4247 | AssertLogRelRCReturnVoid(rc);
|
---|
4248 |
|
---|
4249 | LogFunc(("%s (detached %p, hReqPool=%p)\n", pThis->BackendCfg.szName, pThis->pHostDrvAudio, pThis->hReqPool));
|
---|
4250 |
|
---|
4251 | /*
|
---|
4252 | * Must first destroy the thread pool first so we are certain no threads
|
---|
4253 | * are still using the instance being detached. Release lock while doing
|
---|
4254 | * this as the thread functions may need to take it to complete.
|
---|
4255 | */
|
---|
4256 | if (pThis->pHostDrvAudio && pThis->hReqPool != NIL_RTREQPOOL)
|
---|
4257 | {
|
---|
4258 | RTREQPOOL hReqPool = pThis->hReqPool;
|
---|
4259 | pThis->hReqPool = NIL_RTREQPOOL;
|
---|
4260 |
|
---|
4261 | RTCritSectRwLeaveExcl(&pThis->CritSectHotPlug);
|
---|
4262 |
|
---|
4263 | RTReqPoolRelease(hReqPool);
|
---|
4264 |
|
---|
4265 | RTCritSectRwEnterExcl(&pThis->CritSectHotPlug);
|
---|
4266 | }
|
---|
4267 |
|
---|
4268 | /*
|
---|
4269 | * Now we can safely set pHostDrvAudio to NULL.
|
---|
4270 | */
|
---|
4271 | pThis->pHostDrvAudio = NULL;
|
---|
4272 |
|
---|
4273 | RTCritSectRwLeaveExcl(&pThis->CritSectHotPlug);
|
---|
4274 | }
|
---|
4275 |
|
---|
4276 |
|
---|
4277 | /**
|
---|
4278 | * Initializes the host backend and queries its initial configuration.
|
---|
4279 | *
|
---|
4280 | * @returns VBox status code.
|
---|
4281 | * @param pThis Driver instance to be called.
|
---|
4282 | */
|
---|
4283 | static int drvAudioHostInit(PDRVAUDIO pThis)
|
---|
4284 | {
|
---|
4285 | LogFlowFuncEnter();
|
---|
4286 |
|
---|
4287 | /*
|
---|
4288 | * Check the function pointers, make sure the ones we define as
|
---|
4289 | * mandatory are present.
|
---|
4290 | */
|
---|
4291 | PPDMIHOSTAUDIO pIHostDrvAudio = pThis->pHostDrvAudio;
|
---|
4292 | AssertPtrReturn(pIHostDrvAudio, VERR_INVALID_POINTER);
|
---|
4293 | AssertPtrReturn(pIHostDrvAudio->pfnGetConfig, VERR_INVALID_POINTER);
|
---|
4294 | AssertPtrNullReturn(pIHostDrvAudio->pfnGetDevices, VERR_INVALID_POINTER);
|
---|
4295 | AssertPtrNullReturn(pIHostDrvAudio->pfnSetDevice, VERR_INVALID_POINTER);
|
---|
4296 | AssertPtrNullReturn(pIHostDrvAudio->pfnGetStatus, VERR_INVALID_POINTER);
|
---|
4297 | AssertPtrNullReturn(pIHostDrvAudio->pfnDoOnWorkerThread, VERR_INVALID_POINTER);
|
---|
4298 | AssertPtrNullReturn(pIHostDrvAudio->pfnStreamConfigHint, VERR_INVALID_POINTER);
|
---|
4299 | AssertPtrReturn(pIHostDrvAudio->pfnStreamCreate, VERR_INVALID_POINTER);
|
---|
4300 | AssertPtrNullReturn(pIHostDrvAudio->pfnStreamInitAsync, VERR_INVALID_POINTER);
|
---|
4301 | AssertPtrReturn(pIHostDrvAudio->pfnStreamDestroy, VERR_INVALID_POINTER);
|
---|
4302 | AssertPtrNullReturn(pIHostDrvAudio->pfnStreamNotifyDeviceChanged, VERR_INVALID_POINTER);
|
---|
4303 | AssertPtrReturn(pIHostDrvAudio->pfnStreamEnable, VERR_INVALID_POINTER);
|
---|
4304 | AssertPtrReturn(pIHostDrvAudio->pfnStreamDisable, VERR_INVALID_POINTER);
|
---|
4305 | AssertPtrReturn(pIHostDrvAudio->pfnStreamPause, VERR_INVALID_POINTER);
|
---|
4306 | AssertPtrReturn(pIHostDrvAudio->pfnStreamResume, VERR_INVALID_POINTER);
|
---|
4307 | AssertPtrNullReturn(pIHostDrvAudio->pfnStreamDrain, VERR_INVALID_POINTER);
|
---|
4308 | AssertPtrReturn(pIHostDrvAudio->pfnStreamGetReadable, VERR_INVALID_POINTER);
|
---|
4309 | AssertPtrReturn(pIHostDrvAudio->pfnStreamGetWritable, VERR_INVALID_POINTER);
|
---|
4310 | AssertPtrNullReturn(pIHostDrvAudio->pfnStreamGetPending, VERR_INVALID_POINTER);
|
---|
4311 | AssertPtrReturn(pIHostDrvAudio->pfnStreamGetState, VERR_INVALID_POINTER);
|
---|
4312 | AssertPtrReturn(pIHostDrvAudio->pfnStreamPlay, VERR_INVALID_POINTER);
|
---|
4313 | AssertPtrReturn(pIHostDrvAudio->pfnStreamCapture, VERR_INVALID_POINTER);
|
---|
4314 |
|
---|
4315 | /*
|
---|
4316 | * Get the backend configuration.
|
---|
4317 | *
|
---|
4318 | * Note! Limit the number of streams to max 128 in each direction to
|
---|
4319 | * prevent wasting resources.
|
---|
4320 | * Note! Take care not to wipe the DriverName config value on failure.
|
---|
4321 | */
|
---|
4322 | PDMAUDIOBACKENDCFG BackendCfg;
|
---|
4323 | RT_ZERO(BackendCfg);
|
---|
4324 | int rc = pIHostDrvAudio->pfnGetConfig(pIHostDrvAudio, &BackendCfg);
|
---|
4325 | if (RT_SUCCESS(rc))
|
---|
4326 | {
|
---|
4327 | if (LogIsEnabled() && strcmp(BackendCfg.szName, pThis->BackendCfg.szName) != 0)
|
---|
4328 | LogFunc(("BackendCfg.szName: '%s' -> '%s'\n", pThis->BackendCfg.szName, BackendCfg.szName));
|
---|
4329 | pThis->BackendCfg = BackendCfg;
|
---|
4330 | pThis->In.cStreamsFree = RT_MIN(BackendCfg.cMaxStreamsIn, 128);
|
---|
4331 | pThis->Out.cStreamsFree = RT_MIN(BackendCfg.cMaxStreamsOut, 128);
|
---|
4332 |
|
---|
4333 | LogFlowFunc(("cStreamsFreeIn=%RU8, cStreamsFreeOut=%RU8\n", pThis->In.cStreamsFree, pThis->Out.cStreamsFree));
|
---|
4334 | }
|
---|
4335 | else
|
---|
4336 | {
|
---|
4337 | LogRel(("Audio: Getting configuration for driver '%s' failed with %Rrc\n", pThis->BackendCfg.szName, rc));
|
---|
4338 | return VERR_AUDIO_BACKEND_INIT_FAILED;
|
---|
4339 | }
|
---|
4340 |
|
---|
4341 | LogRel2(("Audio: Host driver '%s' supports %RU32 input streams and %RU32 output streams at once.\n",
|
---|
4342 | pThis->BackendCfg.szName, pThis->In.cStreamsFree, pThis->Out.cStreamsFree));
|
---|
4343 |
|
---|
4344 | #ifdef VBOX_WITH_AUDIO_ENUM
|
---|
4345 | int rc2 = drvAudioDevicesEnumerateInternal(pThis, true /* fLog */, NULL /* pDevEnum */);
|
---|
4346 | if (rc2 != VERR_NOT_SUPPORTED) /* Some backends don't implement device enumeration. */
|
---|
4347 | AssertRC(rc2);
|
---|
4348 | /* Ignore rc2. */
|
---|
4349 | #endif
|
---|
4350 |
|
---|
4351 | /*
|
---|
4352 | * Create a thread pool if stream creation can be asynchronous.
|
---|
4353 | *
|
---|
4354 | * The pool employs no pushback as the caller is typically EMT and
|
---|
4355 | * shouldn't be delayed.
|
---|
4356 | *
|
---|
4357 | * The number of threads limits and the device implementations use
|
---|
4358 | * of pfnStreamDestroy limits the number of streams pending async
|
---|
4359 | * init. We use RTReqCancel in drvAudioStreamDestroy to allow us
|
---|
4360 | * to release extra reference held by the pfnStreamInitAsync call
|
---|
4361 | * if successful. Cancellation will only be possible if the call
|
---|
4362 | * hasn't been picked up by a worker thread yet, so the max number
|
---|
4363 | * of threads in the pool defines how many destroyed streams that
|
---|
4364 | * can be lingering. (We must keep this under control, otherwise
|
---|
4365 | * an evil guest could just rapidly trigger stream creation and
|
---|
4366 | * destruction to consume host heap and hog CPU resources for
|
---|
4367 | * configuring audio backends.)
|
---|
4368 | */
|
---|
4369 | if ( pThis->hReqPool == NIL_RTREQPOOL
|
---|
4370 | && ( pIHostDrvAudio->pfnStreamInitAsync
|
---|
4371 | || pIHostDrvAudio->pfnDoOnWorkerThread
|
---|
4372 | || (pThis->BackendCfg.fFlags & (PDMAUDIOBACKEND_F_ASYNC_HINT | PDMAUDIOBACKEND_F_ASYNC_STREAM_DESTROY)) ))
|
---|
4373 | {
|
---|
4374 | char szName[16];
|
---|
4375 | RTStrPrintf(szName, sizeof(szName), "Aud%uWr", pThis->pDrvIns->iInstance);
|
---|
4376 | RTREQPOOL hReqPool = NIL_RTREQPOOL;
|
---|
4377 | rc = RTReqPoolCreate(3 /*cMaxThreads*/, RT_MS_30SEC /*cMsMinIdle*/, UINT32_MAX /*cThreadsPushBackThreshold*/,
|
---|
4378 | 1 /*cMsMaxPushBack*/, szName, &hReqPool);
|
---|
4379 | LogFlowFunc(("Creating thread pool '%s': %Rrc, hReqPool=%p\n", szName, rc, hReqPool));
|
---|
4380 | AssertRCReturn(rc, rc);
|
---|
4381 |
|
---|
4382 | rc = RTReqPoolSetCfgVar(hReqPool, RTREQPOOLCFGVAR_THREAD_FLAGS, RTTHREADFLAGS_COM_MTA);
|
---|
4383 | AssertRCReturnStmt(rc, RTReqPoolRelease(hReqPool), rc);
|
---|
4384 |
|
---|
4385 | rc = RTReqPoolSetCfgVar(hReqPool, RTREQPOOLCFGVAR_MIN_THREADS, 1);
|
---|
4386 | AssertRC(rc); /* harmless */
|
---|
4387 |
|
---|
4388 | pThis->hReqPool = hReqPool;
|
---|
4389 | }
|
---|
4390 | else
|
---|
4391 | LogFlowFunc(("No thread pool.\n"));
|
---|
4392 |
|
---|
4393 | LogFlowFuncLeave();
|
---|
4394 | return VINF_SUCCESS;
|
---|
4395 | }
|
---|
4396 |
|
---|
4397 |
|
---|
4398 | /**
|
---|
4399 | * Does the actual backend driver attaching and queries the backend's interface.
|
---|
4400 | *
|
---|
4401 | * This is a worker for both drvAudioAttach and drvAudioConstruct.
|
---|
4402 | *
|
---|
4403 | * @returns VBox status code.
|
---|
4404 | * @param pDrvIns The driver instance.
|
---|
4405 | * @param pThis Pointer to driver instance.
|
---|
4406 | * @param fFlags Attach flags; see PDMDrvHlpAttach().
|
---|
4407 | */
|
---|
4408 | static int drvAudioDoAttachInternal(PPDMDRVINS pDrvIns, PDRVAUDIO pThis, uint32_t fFlags)
|
---|
4409 | {
|
---|
4410 | Assert(pThis->pHostDrvAudio == NULL); /* No nested attaching. */
|
---|
4411 |
|
---|
4412 | /*
|
---|
4413 | * Attach driver below and query its connector interface.
|
---|
4414 | */
|
---|
4415 | PPDMIBASE pDownBase;
|
---|
4416 | int rc = PDMDrvHlpAttach(pDrvIns, fFlags, &pDownBase);
|
---|
4417 | if (RT_SUCCESS(rc))
|
---|
4418 | {
|
---|
4419 | pThis->pHostDrvAudio = PDMIBASE_QUERY_INTERFACE(pDownBase, PDMIHOSTAUDIO);
|
---|
4420 | if (pThis->pHostDrvAudio)
|
---|
4421 | {
|
---|
4422 | /*
|
---|
4423 | * If everything went well, initialize the lower driver.
|
---|
4424 | */
|
---|
4425 | rc = drvAudioHostInit(pThis);
|
---|
4426 | if (RT_FAILURE(rc))
|
---|
4427 | pThis->pHostDrvAudio = NULL;
|
---|
4428 | }
|
---|
4429 | else
|
---|
4430 | {
|
---|
4431 | LogRel(("Audio: Failed to query interface for underlying host driver '%s'\n", pThis->BackendCfg.szName));
|
---|
4432 | rc = PDMDRV_SET_ERROR(pThis->pDrvIns, VERR_PDM_MISSING_INTERFACE_BELOW,
|
---|
4433 | N_("The host audio driver does not implement PDMIHOSTAUDIO!"));
|
---|
4434 | }
|
---|
4435 | }
|
---|
4436 | /*
|
---|
4437 | * If the host driver below us failed to construct for some beningn reason,
|
---|
4438 | * we'll report it as a runtime error and replace it with the Null driver.
|
---|
4439 | *
|
---|
4440 | * Note! We do NOT change anything in PDM (or CFGM), so pDrvIns->pDownBase
|
---|
4441 | * will remain NULL in this case.
|
---|
4442 | */
|
---|
4443 | else if ( rc == VERR_AUDIO_BACKEND_INIT_FAILED
|
---|
4444 | || rc == VERR_MODULE_NOT_FOUND
|
---|
4445 | || rc == VERR_SYMBOL_NOT_FOUND
|
---|
4446 | || rc == VERR_FILE_NOT_FOUND
|
---|
4447 | || rc == VERR_PATH_NOT_FOUND)
|
---|
4448 | {
|
---|
4449 | /* Complain: */
|
---|
4450 | LogRel(("DrvAudio: Host audio driver '%s' init failed with %Rrc. Switching to the NULL driver for now.\n",
|
---|
4451 | pThis->BackendCfg.szName, rc));
|
---|
4452 | PDMDrvHlpVMSetRuntimeError(pDrvIns, 0 /*fFlags*/, "HostAudioNotResponding",
|
---|
4453 | N_("Host audio backend (%s) initialization has failed. Selecting the NULL audio backend with the consequence that no sound is audible"),
|
---|
4454 | pThis->BackendCfg.szName);
|
---|
4455 |
|
---|
4456 | /* Replace with null audio: */
|
---|
4457 | pThis->pHostDrvAudio = (PPDMIHOSTAUDIO)&g_DrvHostAudioNull;
|
---|
4458 | RTStrCopy(pThis->BackendCfg.szName, sizeof(pThis->BackendCfg.szName), "NULL");
|
---|
4459 | rc = drvAudioHostInit(pThis);
|
---|
4460 | AssertRC(rc);
|
---|
4461 | }
|
---|
4462 |
|
---|
4463 | LogFunc(("[%s] rc=%Rrc\n", pThis->BackendCfg.szName, rc));
|
---|
4464 | return rc;
|
---|
4465 | }
|
---|
4466 |
|
---|
4467 |
|
---|
4468 | /**
|
---|
4469 | * Attach notification.
|
---|
4470 | *
|
---|
4471 | * @param pDrvIns The driver instance data.
|
---|
4472 | * @param fFlags Attach flags.
|
---|
4473 | */
|
---|
4474 | static DECLCALLBACK(int) drvAudioAttach(PPDMDRVINS pDrvIns, uint32_t fFlags)
|
---|
4475 | {
|
---|
4476 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
4477 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4478 | LogFunc(("%s\n", pThis->BackendCfg.szName));
|
---|
4479 |
|
---|
4480 | int rc = RTCritSectRwEnterExcl(&pThis->CritSectHotPlug);
|
---|
4481 | AssertRCReturn(rc, rc);
|
---|
4482 |
|
---|
4483 | rc = drvAudioDoAttachInternal(pDrvIns, pThis, fFlags);
|
---|
4484 |
|
---|
4485 | RTCritSectRwLeaveExcl(&pThis->CritSectHotPlug);
|
---|
4486 | return rc;
|
---|
4487 | }
|
---|
4488 |
|
---|
4489 |
|
---|
4490 | /**
|
---|
4491 | * Handles state changes for all audio streams.
|
---|
4492 | *
|
---|
4493 | * @param pDrvIns Pointer to driver instance.
|
---|
4494 | * @param enmCmd Stream command to set for all streams.
|
---|
4495 | */
|
---|
4496 | static void drvAudioStateHandler(PPDMDRVINS pDrvIns, PDMAUDIOSTREAMCMD enmCmd)
|
---|
4497 | {
|
---|
4498 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
4499 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4500 | LogFlowFunc(("enmCmd=%s\n", PDMAudioStrmCmdGetName(enmCmd)));
|
---|
4501 |
|
---|
4502 | int rc2 = RTCritSectRwEnterShared(&pThis->CritSectGlobals);
|
---|
4503 | AssertRCReturnVoid(rc2);
|
---|
4504 |
|
---|
4505 | PDRVAUDIOSTREAM pStreamEx;
|
---|
4506 | RTListForEach(&pThis->LstStreams, pStreamEx, DRVAUDIOSTREAM, ListEntry)
|
---|
4507 | {
|
---|
4508 | RTCritSectEnter(&pStreamEx->Core.CritSect);
|
---|
4509 | drvAudioStreamControlInternal(pThis, pStreamEx, enmCmd);
|
---|
4510 | RTCritSectLeave(&pStreamEx->Core.CritSect);
|
---|
4511 | }
|
---|
4512 |
|
---|
4513 | RTCritSectRwLeaveShared(&pThis->CritSectGlobals);
|
---|
4514 | }
|
---|
4515 |
|
---|
4516 |
|
---|
4517 | /**
|
---|
4518 | * Resume notification.
|
---|
4519 | *
|
---|
4520 | * @param pDrvIns The driver instance data.
|
---|
4521 | */
|
---|
4522 | static DECLCALLBACK(void) drvAudioResume(PPDMDRVINS pDrvIns)
|
---|
4523 | {
|
---|
4524 | drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_RESUME);
|
---|
4525 | }
|
---|
4526 |
|
---|
4527 |
|
---|
4528 | /**
|
---|
4529 | * Suspend notification.
|
---|
4530 | *
|
---|
4531 | * @param pDrvIns The driver instance data.
|
---|
4532 | */
|
---|
4533 | static DECLCALLBACK(void) drvAudioSuspend(PPDMDRVINS pDrvIns)
|
---|
4534 | {
|
---|
4535 | drvAudioStateHandler(pDrvIns, PDMAUDIOSTREAMCMD_PAUSE);
|
---|
4536 | }
|
---|
4537 |
|
---|
4538 |
|
---|
4539 | /**
|
---|
4540 | * Destructs an audio driver instance.
|
---|
4541 | *
|
---|
4542 | * @copydoc FNPDMDRVDESTRUCT
|
---|
4543 | */
|
---|
4544 | static DECLCALLBACK(void) drvAudioDestruct(PPDMDRVINS pDrvIns)
|
---|
4545 | {
|
---|
4546 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
4547 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4548 |
|
---|
4549 | LogFlowFuncEnter();
|
---|
4550 |
|
---|
4551 | /*
|
---|
4552 | * We must start by setting pHostDrvAudio to NULL here as the anything below
|
---|
4553 | * us has already been destroyed at this point.
|
---|
4554 | */
|
---|
4555 | if (RTCritSectRwIsInitialized(&pThis->CritSectHotPlug))
|
---|
4556 | {
|
---|
4557 | RTCritSectRwEnterExcl(&pThis->CritSectHotPlug);
|
---|
4558 | pThis->pHostDrvAudio = NULL;
|
---|
4559 | RTCritSectRwLeaveExcl(&pThis->CritSectHotPlug);
|
---|
4560 | }
|
---|
4561 | else
|
---|
4562 | {
|
---|
4563 | Assert(pThis->pHostDrvAudio == NULL);
|
---|
4564 | pThis->pHostDrvAudio = NULL;
|
---|
4565 | }
|
---|
4566 |
|
---|
4567 | /*
|
---|
4568 | * Make sure the thread pool is out of the picture before we terminate all the streams.
|
---|
4569 | */
|
---|
4570 | if (pThis->hReqPool != NIL_RTREQPOOL)
|
---|
4571 | {
|
---|
4572 | uint32_t cRefs = RTReqPoolRelease(pThis->hReqPool);
|
---|
4573 | Assert(cRefs == 0); RT_NOREF(cRefs);
|
---|
4574 | pThis->hReqPool = NIL_RTREQPOOL;
|
---|
4575 | }
|
---|
4576 |
|
---|
4577 | /*
|
---|
4578 | * Destroy all streams.
|
---|
4579 | */
|
---|
4580 | if (RTCritSectRwIsInitialized(&pThis->CritSectGlobals))
|
---|
4581 | {
|
---|
4582 | RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
4583 |
|
---|
4584 | PDRVAUDIOSTREAM pStreamEx, pStreamExNext;
|
---|
4585 | RTListForEachSafe(&pThis->LstStreams, pStreamEx, pStreamExNext, DRVAUDIOSTREAM, ListEntry)
|
---|
4586 | {
|
---|
4587 | int rc = drvAudioStreamUninitInternal(pThis, pStreamEx);
|
---|
4588 | if (RT_SUCCESS(rc))
|
---|
4589 | {
|
---|
4590 | RTListNodeRemove(&pStreamEx->ListEntry);
|
---|
4591 | drvAudioStreamFree(pStreamEx);
|
---|
4592 | }
|
---|
4593 | }
|
---|
4594 |
|
---|
4595 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
4596 | RTCritSectRwDelete(&pThis->CritSectGlobals);
|
---|
4597 | }
|
---|
4598 |
|
---|
4599 |
|
---|
4600 | /* Sanity. */
|
---|
4601 | Assert(RTListIsEmpty(&pThis->LstStreams));
|
---|
4602 |
|
---|
4603 | if (RTCritSectRwIsInitialized(&pThis->CritSectHotPlug))
|
---|
4604 | RTCritSectRwDelete(&pThis->CritSectHotPlug);
|
---|
4605 |
|
---|
4606 | #ifdef VBOX_WITH_STATISTICS
|
---|
4607 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->Stats.TotalStreamsActive);
|
---|
4608 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->Stats.TotalStreamsCreated);
|
---|
4609 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->Stats.TotalFramesRead);
|
---|
4610 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->Stats.TotalFramesIn);
|
---|
4611 | PDMDrvHlpSTAMDeregister(pDrvIns, &pThis->Stats.TotalBytesRead);
|
---|
4612 | #endif
|
---|
4613 |
|
---|
4614 | LogFlowFuncLeave();
|
---|
4615 | }
|
---|
4616 |
|
---|
4617 |
|
---|
4618 | /**
|
---|
4619 | * Constructs an audio driver instance.
|
---|
4620 | *
|
---|
4621 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
4622 | */
|
---|
4623 | static DECLCALLBACK(int) drvAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
4624 | {
|
---|
4625 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
4626 | PDRVAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVAUDIO);
|
---|
4627 | LogFlowFunc(("pDrvIns=%#p, pCfgHandle=%#p, fFlags=%x\n", pDrvIns, pCfg, fFlags));
|
---|
4628 |
|
---|
4629 | /*
|
---|
4630 | * Basic instance init.
|
---|
4631 | */
|
---|
4632 | RTListInit(&pThis->LstStreams);
|
---|
4633 | pThis->hReqPool = NIL_RTREQPOOL;
|
---|
4634 |
|
---|
4635 | /*
|
---|
4636 | * Read configuration.
|
---|
4637 | */
|
---|
4638 | PDMDRV_VALIDATE_CONFIG_RETURN(pDrvIns,
|
---|
4639 | "DriverName|"
|
---|
4640 | "InputEnabled|"
|
---|
4641 | "OutputEnabled|"
|
---|
4642 | "DebugEnabled|"
|
---|
4643 | "DebugPathOut|"
|
---|
4644 | /* Deprecated: */
|
---|
4645 | "PCMSampleBitIn|"
|
---|
4646 | "PCMSampleBitOut|"
|
---|
4647 | "PCMSampleHzIn|"
|
---|
4648 | "PCMSampleHzOut|"
|
---|
4649 | "PCMSampleSignedIn|"
|
---|
4650 | "PCMSampleSignedOut|"
|
---|
4651 | "PCMSampleSwapEndianIn|"
|
---|
4652 | "PCMSampleSwapEndianOut|"
|
---|
4653 | "PCMSampleChannelsIn|"
|
---|
4654 | "PCMSampleChannelsOut|"
|
---|
4655 | "PeriodSizeMsIn|"
|
---|
4656 | "PeriodSizeMsOut|"
|
---|
4657 | "BufferSizeMsIn|"
|
---|
4658 | "BufferSizeMsOut|"
|
---|
4659 | "PreBufferSizeMsIn|"
|
---|
4660 | "PreBufferSizeMsOut",
|
---|
4661 | "In|Out");
|
---|
4662 |
|
---|
4663 | int rc = CFGMR3QueryStringDef(pCfg, "DriverName", pThis->BackendCfg.szName, sizeof(pThis->BackendCfg.szName), "Untitled");
|
---|
4664 | AssertLogRelRCReturn(rc, rc);
|
---|
4665 |
|
---|
4666 | /* Neither input nor output by default for security reasons. */
|
---|
4667 | rc = CFGMR3QueryBoolDef(pCfg, "InputEnabled", &pThis->In.fEnabled, false);
|
---|
4668 | AssertLogRelRCReturn(rc, rc);
|
---|
4669 |
|
---|
4670 | rc = CFGMR3QueryBoolDef(pCfg, "OutputEnabled", &pThis->Out.fEnabled, false);
|
---|
4671 | AssertLogRelRCReturn(rc, rc);
|
---|
4672 |
|
---|
4673 | /* Debug stuff (same for both directions). */
|
---|
4674 | rc = CFGMR3QueryBoolDef(pCfg, "DebugEnabled", &pThis->CfgIn.Dbg.fEnabled, false);
|
---|
4675 | AssertLogRelRCReturn(rc, rc);
|
---|
4676 |
|
---|
4677 | rc = CFGMR3QueryStringDef(pCfg, "DebugPathOut", pThis->CfgIn.Dbg.szPathOut, sizeof(pThis->CfgIn.Dbg.szPathOut), "");
|
---|
4678 | AssertLogRelRCReturn(rc, rc);
|
---|
4679 | if (pThis->CfgIn.Dbg.szPathOut[0] == '\0')
|
---|
4680 | {
|
---|
4681 | rc = RTPathTemp(pThis->CfgIn.Dbg.szPathOut, sizeof(pThis->CfgIn.Dbg.szPathOut));
|
---|
4682 | if (RT_FAILURE(rc))
|
---|
4683 | {
|
---|
4684 | LogRel(("Audio: Warning! Failed to retrieve temporary directory: %Rrc - disabling debugging.\n", rc));
|
---|
4685 | pThis->CfgIn.Dbg.szPathOut[0] = '\0';
|
---|
4686 | pThis->CfgIn.Dbg.fEnabled = false;
|
---|
4687 | }
|
---|
4688 | }
|
---|
4689 | if (pThis->CfgIn.Dbg.fEnabled)
|
---|
4690 | LogRel(("Audio: Debugging for driver '%s' enabled (audio data written to '%s')\n",
|
---|
4691 | pThis->BackendCfg.szName, pThis->CfgIn.Dbg.szPathOut));
|
---|
4692 |
|
---|
4693 | /* Copy debug setup to the output direction. */
|
---|
4694 | pThis->CfgOut.Dbg = pThis->CfgIn.Dbg;
|
---|
4695 |
|
---|
4696 | LogRel2(("Audio: Verbose logging for driver '%s' is probably enabled too.\n", pThis->BackendCfg.szName));
|
---|
4697 | /* This ^^^^^^^ is the *WRONG* place for that kind of statement. Verbose logging might only be enabled for DrvAudio. */
|
---|
4698 | LogRel2(("Audio: Initial status for driver '%s' is: input is %s, output is %s\n",
|
---|
4699 | pThis->BackendCfg.szName, pThis->In.fEnabled ? "enabled" : "disabled", pThis->Out.fEnabled ? "enabled" : "disabled"));
|
---|
4700 |
|
---|
4701 | /*
|
---|
4702 | * Per direction configuration. A bit complicated as
|
---|
4703 | * these wasn't originally in sub-nodes.
|
---|
4704 | */
|
---|
4705 | for (unsigned iDir = 0; iDir < 2; iDir++)
|
---|
4706 | {
|
---|
4707 | char szNm[48];
|
---|
4708 | PDRVAUDIOCFG pAudioCfg = iDir == 0 ? &pThis->CfgIn : &pThis->CfgOut;
|
---|
4709 | const char *pszDir = iDir == 0 ? "In" : "Out";
|
---|
4710 |
|
---|
4711 | #define QUERY_VAL_RET(a_Width, a_szName, a_pValue, a_uDefault, a_ExprValid, a_szValidRange) \
|
---|
4712 | do { \
|
---|
4713 | rc = RT_CONCAT(CFGMR3QueryU,a_Width)(pDirNode, strcpy(szNm, a_szName), a_pValue); \
|
---|
4714 | if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT) \
|
---|
4715 | { \
|
---|
4716 | rc = RT_CONCAT(CFGMR3QueryU,a_Width)(pCfg, strcat(szNm, pszDir), a_pValue); \
|
---|
4717 | if (rc == VERR_CFGM_VALUE_NOT_FOUND || rc == VERR_CFGM_NO_PARENT) \
|
---|
4718 | { \
|
---|
4719 | *(a_pValue) = a_uDefault; \
|
---|
4720 | rc = VINF_SUCCESS; \
|
---|
4721 | } \
|
---|
4722 | else \
|
---|
4723 | LogRel(("DrvAudio: Warning! Please use '%s/" a_szName "' instead of '%s' for your VBoxInternal hacks\n", pszDir, szNm)); \
|
---|
4724 | } \
|
---|
4725 | AssertRCReturn(rc, PDMDrvHlpVMSetError(pDrvIns, rc, RT_SRC_POS, \
|
---|
4726 | N_("Configuration error: Failed to read %s config value '%s'"), pszDir, szNm)); \
|
---|
4727 | if (!(a_ExprValid)) \
|
---|
4728 | return PDMDrvHlpVMSetError(pDrvIns, VERR_OUT_OF_RANGE, RT_SRC_POS, \
|
---|
4729 | N_("Configuration error: Unsupported %s value %u. " a_szValidRange), szNm, *(a_pValue)); \
|
---|
4730 | } while (0)
|
---|
4731 |
|
---|
4732 | PCFGMNODE const pDirNode = CFGMR3GetChild(pCfg, pszDir);
|
---|
4733 | rc = CFGMR3ValidateConfig(pDirNode, iDir == 0 ? "In/" : "Out/",
|
---|
4734 | "PCMSampleBit|"
|
---|
4735 | "PCMSampleHz|"
|
---|
4736 | "PCMSampleSigned|"
|
---|
4737 | "PCMSampleSwapEndian|"
|
---|
4738 | "PCMSampleChannels|"
|
---|
4739 | "PeriodSizeMs|"
|
---|
4740 | "BufferSizeMs|"
|
---|
4741 | "PreBufferSizeMs",
|
---|
4742 | "", pDrvIns->pReg->szName, pDrvIns->iInstance);
|
---|
4743 | AssertRCReturn(rc, rc);
|
---|
4744 |
|
---|
4745 | uint8_t cSampleBits = 0;
|
---|
4746 | QUERY_VAL_RET(8, "PCMSampleBit", &cSampleBits, 0,
|
---|
4747 | cSampleBits == 0
|
---|
4748 | || cSampleBits == 8
|
---|
4749 | || cSampleBits == 16
|
---|
4750 | || cSampleBits == 32
|
---|
4751 | || cSampleBits == 64,
|
---|
4752 | "Must be either 0, 8, 16, 32 or 64");
|
---|
4753 | if (cSampleBits)
|
---|
4754 | PDMAudioPropsSetSampleSize(&pAudioCfg->Props, cSampleBits / 8);
|
---|
4755 |
|
---|
4756 | uint8_t cChannels;
|
---|
4757 | QUERY_VAL_RET(8, "PCMSampleChannels", &cChannels, 0, cChannels <= 16, "Max 16");
|
---|
4758 | if (cChannels)
|
---|
4759 | PDMAudioPropsSetChannels(&pAudioCfg->Props, cChannels);
|
---|
4760 |
|
---|
4761 | QUERY_VAL_RET(32, "PCMSampleHz", &pAudioCfg->Props.uHz, 0,
|
---|
4762 | pAudioCfg->Props.uHz == 0 || (pAudioCfg->Props.uHz >= 6000 && pAudioCfg->Props.uHz <= 768000),
|
---|
4763 | "In the range 6000 thru 768000, or 0");
|
---|
4764 |
|
---|
4765 | QUERY_VAL_RET(8, "PCMSampleSigned", &pAudioCfg->uSigned, UINT8_MAX,
|
---|
4766 | pAudioCfg->uSigned == 0 || pAudioCfg->uSigned == 1 || pAudioCfg->uSigned == UINT8_MAX,
|
---|
4767 | "Must be either 0, 1, or 255");
|
---|
4768 |
|
---|
4769 | QUERY_VAL_RET(8, "PCMSampleSwapEndian", &pAudioCfg->uSwapEndian, UINT8_MAX,
|
---|
4770 | pAudioCfg->uSwapEndian == 0 || pAudioCfg->uSwapEndian == 1 || pAudioCfg->uSwapEndian == UINT8_MAX,
|
---|
4771 | "Must be either 0, 1, or 255");
|
---|
4772 |
|
---|
4773 | QUERY_VAL_RET(32, "PeriodSizeMs", &pAudioCfg->uPeriodSizeMs, 0,
|
---|
4774 | pAudioCfg->uPeriodSizeMs <= RT_MS_1SEC, "Max 1000");
|
---|
4775 |
|
---|
4776 | QUERY_VAL_RET(32, "BufferSizeMs", &pAudioCfg->uBufferSizeMs, 0,
|
---|
4777 | pAudioCfg->uBufferSizeMs <= RT_MS_5SEC, "Max 5000");
|
---|
4778 |
|
---|
4779 | QUERY_VAL_RET(32, "PreBufferSizeMs", &pAudioCfg->uPreBufSizeMs, UINT32_MAX,
|
---|
4780 | pAudioCfg->uPreBufSizeMs <= RT_MS_1SEC || pAudioCfg->uPreBufSizeMs == UINT32_MAX,
|
---|
4781 | "Max 1000, or 0xffffffff");
|
---|
4782 | #undef QUERY_VAL_RET
|
---|
4783 | }
|
---|
4784 |
|
---|
4785 | /*
|
---|
4786 | * Init the rest of the driver instance data.
|
---|
4787 | */
|
---|
4788 | rc = RTCritSectRwInit(&pThis->CritSectHotPlug);
|
---|
4789 | AssertRCReturn(rc, rc);
|
---|
4790 | rc = RTCritSectRwInit(&pThis->CritSectGlobals);
|
---|
4791 | AssertRCReturn(rc, rc);
|
---|
4792 | #ifdef VBOX_STRICT
|
---|
4793 | /* Define locking order: */
|
---|
4794 | RTCritSectRwEnterExcl(&pThis->CritSectGlobals);
|
---|
4795 | RTCritSectRwEnterExcl(&pThis->CritSectHotPlug);
|
---|
4796 | RTCritSectRwLeaveExcl(&pThis->CritSectHotPlug);
|
---|
4797 | RTCritSectRwLeaveExcl(&pThis->CritSectGlobals);
|
---|
4798 | #endif
|
---|
4799 |
|
---|
4800 | pThis->pDrvIns = pDrvIns;
|
---|
4801 | /* IBase. */
|
---|
4802 | pDrvIns->IBase.pfnQueryInterface = drvAudioQueryInterface;
|
---|
4803 | /* IAudioConnector. */
|
---|
4804 | pThis->IAudioConnector.pfnEnable = drvAudioEnable;
|
---|
4805 | pThis->IAudioConnector.pfnIsEnabled = drvAudioIsEnabled;
|
---|
4806 | pThis->IAudioConnector.pfnGetConfig = drvAudioGetConfig;
|
---|
4807 | pThis->IAudioConnector.pfnGetStatus = drvAudioGetStatus;
|
---|
4808 | pThis->IAudioConnector.pfnStreamConfigHint = drvAudioStreamConfigHint;
|
---|
4809 | pThis->IAudioConnector.pfnStreamCreate = drvAudioStreamCreate;
|
---|
4810 | pThis->IAudioConnector.pfnStreamDestroy = drvAudioStreamDestroy;
|
---|
4811 | pThis->IAudioConnector.pfnStreamReInit = drvAudioStreamReInit;
|
---|
4812 | pThis->IAudioConnector.pfnStreamRetain = drvAudioStreamRetain;
|
---|
4813 | pThis->IAudioConnector.pfnStreamRelease = drvAudioStreamRelease;
|
---|
4814 | pThis->IAudioConnector.pfnStreamControl = drvAudioStreamControl;
|
---|
4815 | pThis->IAudioConnector.pfnStreamIterate = drvAudioStreamIterate;
|
---|
4816 | pThis->IAudioConnector.pfnStreamGetState = drvAudioStreamGetState;
|
---|
4817 | pThis->IAudioConnector.pfnStreamGetWritable = drvAudioStreamGetWritable;
|
---|
4818 | pThis->IAudioConnector.pfnStreamPlay = drvAudioStreamPlay;
|
---|
4819 | pThis->IAudioConnector.pfnStreamGetReadable = drvAudioStreamGetReadable;
|
---|
4820 | pThis->IAudioConnector.pfnStreamCapture = drvAudioStreamCapture;
|
---|
4821 | /* IHostAudioPort */
|
---|
4822 | pThis->IHostAudioPort.pfnDoOnWorkerThread = drvAudioHostPort_DoOnWorkerThread;
|
---|
4823 | pThis->IHostAudioPort.pfnNotifyDeviceChanged = drvAudioHostPort_NotifyDeviceChanged;
|
---|
4824 | pThis->IHostAudioPort.pfnStreamNotifyPreparingDeviceSwitch = drvAudioHostPort_StreamNotifyPreparingDeviceSwitch;
|
---|
4825 | pThis->IHostAudioPort.pfnStreamNotifyDeviceChanged = drvAudioHostPort_StreamNotifyDeviceChanged;
|
---|
4826 | pThis->IHostAudioPort.pfnNotifyDevicesChanged = drvAudioHostPort_NotifyDevicesChanged;
|
---|
4827 |
|
---|
4828 | /*
|
---|
4829 | * Statistics.
|
---|
4830 | */
|
---|
4831 | #ifdef VBOX_WITH_STATISTICS
|
---|
4832 | PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->Stats.TotalStreamsActive, "TotalStreamsActive",
|
---|
4833 | STAMUNIT_COUNT, "Total active audio streams.");
|
---|
4834 | PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->Stats.TotalStreamsCreated, "TotalStreamsCreated",
|
---|
4835 | STAMUNIT_COUNT, "Total created audio streams.");
|
---|
4836 | PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->Stats.TotalFramesRead, "TotalFramesRead",
|
---|
4837 | STAMUNIT_COUNT, "Total frames read by device emulation.");
|
---|
4838 | PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->Stats.TotalFramesIn, "TotalFramesIn",
|
---|
4839 | STAMUNIT_COUNT, "Total frames captured by backend.");
|
---|
4840 | PDMDrvHlpSTAMRegCounterEx(pDrvIns, &pThis->Stats.TotalBytesRead, "TotalBytesRead",
|
---|
4841 | STAMUNIT_BYTES, "Total bytes read.");
|
---|
4842 | #endif
|
---|
4843 |
|
---|
4844 | #ifdef VBOX_WITH_AUDIO_ENUM
|
---|
4845 | /*
|
---|
4846 | * Create a timer to trigger delayed device enumeration on device changes.
|
---|
4847 | */
|
---|
4848 | RTStrPrintf(pThis->szEnumTimerName, sizeof(pThis->szEnumTimerName), "AudioEnum-%u", pDrvIns->iInstance);
|
---|
4849 | rc = PDMDrvHlpTMTimerCreate(pDrvIns, TMCLOCK_REAL, drvAudioEnumerateTimer, NULL /*pvUser*/,
|
---|
4850 | 0 /*fFlags*/, pThis->szEnumTimerName, &pThis->hEnumTimer);
|
---|
4851 | AssertRCReturn(rc, rc);
|
---|
4852 | #endif
|
---|
4853 |
|
---|
4854 | /*
|
---|
4855 | * Attach the host driver, if present.
|
---|
4856 | */
|
---|
4857 | rc = drvAudioDoAttachInternal(pDrvIns, pThis, fFlags);
|
---|
4858 | if (rc == VERR_PDM_NO_ATTACHED_DRIVER)
|
---|
4859 | rc = VINF_SUCCESS;
|
---|
4860 |
|
---|
4861 | LogFlowFuncLeaveRC(rc);
|
---|
4862 | return rc;
|
---|
4863 | }
|
---|
4864 |
|
---|
4865 | /**
|
---|
4866 | * Audio driver registration record.
|
---|
4867 | */
|
---|
4868 | const PDMDRVREG g_DrvAUDIO =
|
---|
4869 | {
|
---|
4870 | /* u32Version */
|
---|
4871 | PDM_DRVREG_VERSION,
|
---|
4872 | /* szName */
|
---|
4873 | "AUDIO",
|
---|
4874 | /* szRCMod */
|
---|
4875 | "",
|
---|
4876 | /* szR0Mod */
|
---|
4877 | "",
|
---|
4878 | /* pszDescription */
|
---|
4879 | "Audio connector driver",
|
---|
4880 | /* fFlags */
|
---|
4881 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
4882 | /* fClass */
|
---|
4883 | PDM_DRVREG_CLASS_AUDIO,
|
---|
4884 | /* cMaxInstances */
|
---|
4885 | UINT32_MAX,
|
---|
4886 | /* cbInstance */
|
---|
4887 | sizeof(DRVAUDIO),
|
---|
4888 | /* pfnConstruct */
|
---|
4889 | drvAudioConstruct,
|
---|
4890 | /* pfnDestruct */
|
---|
4891 | drvAudioDestruct,
|
---|
4892 | /* pfnRelocate */
|
---|
4893 | NULL,
|
---|
4894 | /* pfnIOCtl */
|
---|
4895 | NULL,
|
---|
4896 | /* pfnPowerOn */
|
---|
4897 | NULL,
|
---|
4898 | /* pfnReset */
|
---|
4899 | NULL,
|
---|
4900 | /* pfnSuspend */
|
---|
4901 | drvAudioSuspend,
|
---|
4902 | /* pfnResume */
|
---|
4903 | drvAudioResume,
|
---|
4904 | /* pfnAttach */
|
---|
4905 | drvAudioAttach,
|
---|
4906 | /* pfnDetach */
|
---|
4907 | drvAudioDetach,
|
---|
4908 | /* pfnPowerOff */
|
---|
4909 | drvAudioPowerOff,
|
---|
4910 | /* pfnSoftReset */
|
---|
4911 | NULL,
|
---|
4912 | /* u32EndVersion */
|
---|
4913 | PDM_DRVREG_VERSION
|
---|
4914 | };
|
---|
4915 |
|
---|