1 | /* $Id: DrvHostCoreAudio.cpp 85655 2020-08-10 08:02:03Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBox audio devices - Mac OS X CoreAudio audio driver.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
|
---|
23 | #include <VBox/log.h>
|
---|
24 |
|
---|
25 | #include "DrvAudio.h"
|
---|
26 | #include "VBoxDD.h"
|
---|
27 |
|
---|
28 | #include <iprt/asm.h>
|
---|
29 | #include <iprt/cdefs.h>
|
---|
30 | #include <iprt/circbuf.h>
|
---|
31 | #include <iprt/mem.h>
|
---|
32 |
|
---|
33 | #include <iprt/uuid.h>
|
---|
34 |
|
---|
35 | #include <CoreAudio/CoreAudio.h>
|
---|
36 | #include <CoreServices/CoreServices.h>
|
---|
37 | #include <AudioUnit/AudioUnit.h>
|
---|
38 | #include <AudioToolbox/AudioConverter.h>
|
---|
39 | #include <AudioToolbox/AudioToolbox.h>
|
---|
40 |
|
---|
41 |
|
---|
42 |
|
---|
43 | /* Enables utilizing the Core Audio converter unit for converting
|
---|
44 | * input / output from/to our requested formats. That might be more
|
---|
45 | * performant than using our own routines later down the road. */
|
---|
46 | /** @todo Needs more investigation and testing first before enabling. */
|
---|
47 | //# define VBOX_WITH_AUDIO_CA_CONVERTER
|
---|
48 |
|
---|
49 | /** @todo
|
---|
50 | * - Maybe make sure the threads are immediately stopped if playing/recording stops.
|
---|
51 | */
|
---|
52 |
|
---|
53 | /*
|
---|
54 | * Most of this is based on:
|
---|
55 | * http://developer.apple.com/mac/library/technotes/tn2004/tn2097.html
|
---|
56 | * http://developer.apple.com/mac/library/technotes/tn2002/tn2091.html
|
---|
57 | * http://developer.apple.com/mac/library/qa/qa2007/qa1533.html
|
---|
58 | * http://developer.apple.com/mac/library/qa/qa2001/qa1317.html
|
---|
59 | * http://developer.apple.com/mac/library/documentation/AudioUnit/Reference/AUComponentServicesReference/Reference/reference.html
|
---|
60 | */
|
---|
61 |
|
---|
62 | /* Prototypes needed for COREAUDIODEVICE. */
|
---|
63 | struct DRVHOSTCOREAUDIO;
|
---|
64 | typedef struct DRVHOSTCOREAUDIO *PDRVHOSTCOREAUDIO;
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * Structure for holding Core Audio-specific device data.
|
---|
68 | * This data then lives in the pvData part of the PDMAUDIODEVICE struct.
|
---|
69 | */
|
---|
70 | typedef struct COREAUDIODEVICEDATA
|
---|
71 | {
|
---|
72 | /** Pointer to driver instance this device is bound to. */
|
---|
73 | PDRVHOSTCOREAUDIO pDrv;
|
---|
74 | /** The audio device ID of the currently used device (UInt32 typedef). */
|
---|
75 | AudioDeviceID deviceID;
|
---|
76 | /** The device' UUID. */
|
---|
77 | CFStringRef UUID;
|
---|
78 | /** List of attached (native) Core Audio streams attached to this device. */
|
---|
79 | RTLISTANCHOR lstStreams;
|
---|
80 | } COREAUDIODEVICEDATA, *PCOREAUDIODEVICEDATA;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * Host Coreaudio driver instance data.
|
---|
84 | * @implements PDMIAUDIOCONNECTOR
|
---|
85 | */
|
---|
86 | typedef struct DRVHOSTCOREAUDIO
|
---|
87 | {
|
---|
88 | /** Pointer to the driver instance structure. */
|
---|
89 | PPDMDRVINS pDrvIns;
|
---|
90 | /** Pointer to host audio interface. */
|
---|
91 | PDMIHOSTAUDIO IHostAudio;
|
---|
92 | /** Critical section to serialize access. */
|
---|
93 | RTCRITSECT CritSect;
|
---|
94 | /** Current (last reported) device enumeration. */
|
---|
95 | PDMAUDIODEVICEENUM Devices;
|
---|
96 | /** Pointer to the currently used input device in the device enumeration.
|
---|
97 | * Can be NULL if none assigned. */
|
---|
98 | PPDMAUDIODEVICE pDefaultDevIn;
|
---|
99 | /** Pointer to the currently used output device in the device enumeration.
|
---|
100 | * Can be NULL if none assigned. */
|
---|
101 | PPDMAUDIODEVICE pDefaultDevOut;
|
---|
102 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
103 | /** Callback function to the upper driver.
|
---|
104 | * Can be NULL if not being used / registered. */
|
---|
105 | PFNPDMHOSTAUDIOCALLBACK pfnCallback;
|
---|
106 | #endif
|
---|
107 | } DRVHOSTCOREAUDIO, *PDRVHOSTCOREAUDIO;
|
---|
108 |
|
---|
109 | /** Converts a pointer to DRVHOSTCOREAUDIO::IHostAudio to a PDRVHOSTCOREAUDIO. */
|
---|
110 | #define PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface) RT_FROM_MEMBER(pInterface, DRVHOSTCOREAUDIO, IHostAudio)
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * Structure for holding a Core Audio unit
|
---|
114 | * and its data.
|
---|
115 | */
|
---|
116 | typedef struct COREAUDIOUNIT
|
---|
117 | {
|
---|
118 | /** Pointer to the device this audio unit is bound to.
|
---|
119 | * Can be NULL if not bound to a device (anymore). */
|
---|
120 | PPDMAUDIODEVICE pDevice;
|
---|
121 | /** The actual audio unit object. */
|
---|
122 | AudioUnit audioUnit;
|
---|
123 | /** Stream description for using with VBox:
|
---|
124 | * - When using this audio unit for input (capturing), this format states
|
---|
125 | * the unit's output format.
|
---|
126 | * - When using this audio unit for output (playback), this format states
|
---|
127 | * the unit's input format. */
|
---|
128 | AudioStreamBasicDescription streamFmt;
|
---|
129 | } COREAUDIOUNIT, *PCOREAUDIOUNIT;
|
---|
130 |
|
---|
131 |
|
---|
132 | DECLHIDDEN(int) coreAudioInputPermissionCheck(void);
|
---|
133 |
|
---|
134 | /*******************************************************************************
|
---|
135 | *
|
---|
136 | * Helper function section
|
---|
137 | *
|
---|
138 | ******************************************************************************/
|
---|
139 |
|
---|
140 | /* Move these down below the internal function prototypes... */
|
---|
141 |
|
---|
142 | static void coreAudioPrintASBD(const char *pszDesc, const AudioStreamBasicDescription *pASBD)
|
---|
143 | {
|
---|
144 | char pszSampleRate[32];
|
---|
145 | LogRel2(("CoreAudio: %s description:\n", pszDesc));
|
---|
146 | LogRel2(("CoreAudio:\tFormat ID: %RU32 (%c%c%c%c)\n", pASBD->mFormatID,
|
---|
147 | RT_BYTE4(pASBD->mFormatID), RT_BYTE3(pASBD->mFormatID),
|
---|
148 | RT_BYTE2(pASBD->mFormatID), RT_BYTE1(pASBD->mFormatID)));
|
---|
149 | LogRel2(("CoreAudio:\tFlags: %RU32", pASBD->mFormatFlags));
|
---|
150 | if (pASBD->mFormatFlags & kAudioFormatFlagIsFloat)
|
---|
151 | LogRel2((" Float"));
|
---|
152 | if (pASBD->mFormatFlags & kAudioFormatFlagIsBigEndian)
|
---|
153 | LogRel2((" BigEndian"));
|
---|
154 | if (pASBD->mFormatFlags & kAudioFormatFlagIsSignedInteger)
|
---|
155 | LogRel2((" SignedInteger"));
|
---|
156 | if (pASBD->mFormatFlags & kAudioFormatFlagIsPacked)
|
---|
157 | LogRel2((" Packed"));
|
---|
158 | if (pASBD->mFormatFlags & kAudioFormatFlagIsAlignedHigh)
|
---|
159 | LogRel2((" AlignedHigh"));
|
---|
160 | if (pASBD->mFormatFlags & kAudioFormatFlagIsNonInterleaved)
|
---|
161 | LogRel2((" NonInterleaved"));
|
---|
162 | if (pASBD->mFormatFlags & kAudioFormatFlagIsNonMixable)
|
---|
163 | LogRel2((" NonMixable"));
|
---|
164 | if (pASBD->mFormatFlags & kAudioFormatFlagsAreAllClear)
|
---|
165 | LogRel2((" AllClear"));
|
---|
166 | LogRel2(("\n"));
|
---|
167 | snprintf(pszSampleRate, 32, "%.2f", (float)pASBD->mSampleRate); /** @todo r=andy Use RTStrPrint*. */
|
---|
168 | LogRel2(("CoreAudio:\tSampleRate : %s\n", pszSampleRate));
|
---|
169 | LogRel2(("CoreAudio:\tChannelsPerFrame: %RU32\n", pASBD->mChannelsPerFrame));
|
---|
170 | LogRel2(("CoreAudio:\tFramesPerPacket : %RU32\n", pASBD->mFramesPerPacket));
|
---|
171 | LogRel2(("CoreAudio:\tBitsPerChannel : %RU32\n", pASBD->mBitsPerChannel));
|
---|
172 | LogRel2(("CoreAudio:\tBytesPerFrame : %RU32\n", pASBD->mBytesPerFrame));
|
---|
173 | LogRel2(("CoreAudio:\tBytesPerPacket : %RU32\n", pASBD->mBytesPerPacket));
|
---|
174 | }
|
---|
175 |
|
---|
176 | static void coreAudioPCMPropsToASBD(PDMAUDIOPCMPROPS *pPCMProps, AudioStreamBasicDescription *pASBD)
|
---|
177 | {
|
---|
178 | AssertPtrReturnVoid(pPCMProps);
|
---|
179 | AssertPtrReturnVoid(pASBD);
|
---|
180 |
|
---|
181 | RT_BZERO(pASBD, sizeof(AudioStreamBasicDescription));
|
---|
182 |
|
---|
183 | pASBD->mFormatID = kAudioFormatLinearPCM;
|
---|
184 | pASBD->mFormatFlags = kAudioFormatFlagIsPacked;
|
---|
185 | pASBD->mFramesPerPacket = 1; /* For uncompressed audio, set this to 1. */
|
---|
186 | pASBD->mSampleRate = (Float64)pPCMProps->uHz;
|
---|
187 | pASBD->mChannelsPerFrame = pPCMProps->cChannels;
|
---|
188 | pASBD->mBitsPerChannel = pPCMProps->cbSample * 8;
|
---|
189 | if (pPCMProps->fSigned)
|
---|
190 | pASBD->mFormatFlags |= kAudioFormatFlagIsSignedInteger;
|
---|
191 | pASBD->mBytesPerFrame = pASBD->mChannelsPerFrame * (pASBD->mBitsPerChannel / 8);
|
---|
192 | pASBD->mBytesPerPacket = pASBD->mFramesPerPacket * pASBD->mBytesPerFrame;
|
---|
193 | }
|
---|
194 |
|
---|
195 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
196 | static int coreAudioASBDToStreamCfg(AudioStreamBasicDescription *pASBD, PPDMAUDIOSTREAMCFG pCfg)
|
---|
197 | {
|
---|
198 | AssertPtrReturn(pASBD, VERR_INVALID_PARAMETER);
|
---|
199 | AssertPtrReturn(pCfg, VERR_INVALID_PARAMETER);
|
---|
200 |
|
---|
201 | pCfg->Props.cChannels = pASBD->mChannelsPerFrame;
|
---|
202 | pCfg->Props.uHz = (uint32_t)pASBD->mSampleRate;
|
---|
203 | AssertMsg(!(pASBD->mBitsPerChannel & 7), ("%u\n", pASBD->mBitsPerChannel));
|
---|
204 | pCfg->Props.cbSample = pASBD->mBitsPerChannel / 8;
|
---|
205 | pCfg->Props.fSigned = RT_BOOL(pASBD->mFormatFlags & kAudioFormatFlagIsSignedInteger);
|
---|
206 | pCfg->Props.cShift = PDMAUDIOPCMPROPS_MAKE_SHIFT_PARMS(pCfg->Props.cbSample, pCfg->Props.cChannels);
|
---|
207 | /** @todo r=bird: pCfg->Props.fSwapEndian is not initialized here! */
|
---|
208 |
|
---|
209 | return VINF_SUCCESS;
|
---|
210 | }
|
---|
211 | #endif /* !VBOX_WITH_AUDIO_CALLBACKS */
|
---|
212 |
|
---|
213 | #if 0 /* unused */
|
---|
214 | static int coreAudioCFStringToCString(const CFStringRef pCFString, char **ppszString)
|
---|
215 | {
|
---|
216 | CFIndex cLen = CFStringGetLength(pCFString) + 1;
|
---|
217 | char *pszResult = (char *)RTMemAllocZ(cLen * sizeof(char));
|
---|
218 | if (!CFStringGetCString(pCFString, pszResult, cLen, kCFStringEncodingUTF8))
|
---|
219 | {
|
---|
220 | RTMemFree(pszResult);
|
---|
221 | return VERR_NOT_FOUND;
|
---|
222 | }
|
---|
223 |
|
---|
224 | *ppszString = pszResult;
|
---|
225 | return VINF_SUCCESS;
|
---|
226 | }
|
---|
227 |
|
---|
228 | static AudioDeviceID coreAudioDeviceUIDtoID(const char* pszUID)
|
---|
229 | {
|
---|
230 | /* Create a CFString out of our CString. */
|
---|
231 | CFStringRef strUID = CFStringCreateWithCString(NULL, pszUID, kCFStringEncodingMacRoman);
|
---|
232 |
|
---|
233 | /* Fill the translation structure. */
|
---|
234 | AudioDeviceID deviceID;
|
---|
235 |
|
---|
236 | AudioValueTranslation translation;
|
---|
237 | translation.mInputData = &strUID;
|
---|
238 | translation.mInputDataSize = sizeof(CFStringRef);
|
---|
239 | translation.mOutputData = &deviceID;
|
---|
240 | translation.mOutputDataSize = sizeof(AudioDeviceID);
|
---|
241 |
|
---|
242 | /* Fetch the translation from the UID to the device ID. */
|
---|
243 | AudioObjectPropertyAddress propAdr = { kAudioHardwarePropertyDeviceForUID, kAudioObjectPropertyScopeGlobal,
|
---|
244 | kAudioObjectPropertyElementMaster };
|
---|
245 |
|
---|
246 | UInt32 uSize = sizeof(AudioValueTranslation);
|
---|
247 | OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propAdr, 0, NULL, &uSize, &translation);
|
---|
248 |
|
---|
249 | /* Release the temporary CFString */
|
---|
250 | CFRelease(strUID);
|
---|
251 |
|
---|
252 | if (RT_LIKELY(err == noErr))
|
---|
253 | return deviceID;
|
---|
254 |
|
---|
255 | /* Return the unknown device on error. */
|
---|
256 | return kAudioDeviceUnknown;
|
---|
257 | }
|
---|
258 | #endif /* unused */
|
---|
259 |
|
---|
260 |
|
---|
261 | /*********************************************************************************************************************************
|
---|
262 | * Defined Constants And Macros *
|
---|
263 | *********************************************************************************************************************************/
|
---|
264 |
|
---|
265 | /** @name Initialization status indicator used for the recreation of the AudioUnits.
|
---|
266 | *
|
---|
267 | * Global structures section
|
---|
268 | *
|
---|
269 | ******************************************************************************/
|
---|
270 |
|
---|
271 | /**
|
---|
272 | * Enumeration for a Core Audio stream status.
|
---|
273 | */
|
---|
274 | typedef enum COREAUDIOSTATUS
|
---|
275 | {
|
---|
276 | /** The device is uninitialized. */
|
---|
277 | COREAUDIOSTATUS_UNINIT = 0,
|
---|
278 | /** The device is currently initializing. */
|
---|
279 | COREAUDIOSTATUS_IN_INIT,
|
---|
280 | /** The device is initialized. */
|
---|
281 | COREAUDIOSTATUS_INIT,
|
---|
282 | /** The device is currently uninitializing. */
|
---|
283 | COREAUDIOSTATUS_IN_UNINIT,
|
---|
284 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
285 | /** The device has to be reinitialized.
|
---|
286 | * Note: Only needed if VBOX_WITH_AUDIO_CALLBACKS is not defined, as otherwise
|
---|
287 | * the Audio Connector will take care of this as soon as this backend
|
---|
288 | * tells it to do so via the provided audio callback. */
|
---|
289 | COREAUDIOSTATUS_REINIT,
|
---|
290 | #endif
|
---|
291 | /** The usual 32-bit hack. */
|
---|
292 | COREAUDIOSTATUS_32BIT_HACK = 0x7fffffff
|
---|
293 | } COREAUDIOSTATUS, *PCOREAUDIOSTATUS;
|
---|
294 |
|
---|
295 | #ifdef VBOX_WITH_AUDIO_CA_CONVERTER
|
---|
296 | /* Error code which indicates "End of data" */
|
---|
297 | static const OSStatus caConverterEOFDErr = 0x656F6664; /* 'eofd' */
|
---|
298 | #endif
|
---|
299 |
|
---|
300 | /* Prototypes needed for COREAUDIOSTREAMCBCTX. */
|
---|
301 | struct COREAUDIOSTREAM;
|
---|
302 | typedef struct COREAUDIOSTREAM *PCOREAUDIOSTREAM;
|
---|
303 |
|
---|
304 | /**
|
---|
305 | * Structure for keeping a conversion callback context.
|
---|
306 | * This is needed when using an audio converter during input/output processing.
|
---|
307 | */
|
---|
308 | typedef struct COREAUDIOCONVCBCTX
|
---|
309 | {
|
---|
310 | /** Pointer to the stream this context is bound to. */
|
---|
311 | PCOREAUDIOSTREAM pStream;
|
---|
312 | /** Source stream description. */
|
---|
313 | AudioStreamBasicDescription asbdSrc;
|
---|
314 | /** Destination stream description. */
|
---|
315 | AudioStreamBasicDescription asbdDst;
|
---|
316 | /** Pointer to native buffer list used for rendering the source audio data into. */
|
---|
317 | AudioBufferList *pBufLstSrc;
|
---|
318 | /** Total packet conversion count. */
|
---|
319 | UInt32 uPacketCnt;
|
---|
320 | /** Current packet conversion index. */
|
---|
321 | UInt32 uPacketIdx;
|
---|
322 | /** Error count, for limiting the logging. */
|
---|
323 | UInt32 cErrors;
|
---|
324 | } COREAUDIOCONVCBCTX, *PCOREAUDIOCONVCBCTX;
|
---|
325 |
|
---|
326 | /**
|
---|
327 | * Structure for keeping the input stream specifics.
|
---|
328 | */
|
---|
329 | typedef struct COREAUDIOSTREAMIN
|
---|
330 | {
|
---|
331 | #ifdef VBOX_WITH_AUDIO_CA_CONVERTER
|
---|
332 | /** The audio converter if necessary. NULL if no converter is being used. */
|
---|
333 | AudioConverterRef ConverterRef;
|
---|
334 | /** Callback context for the audio converter. */
|
---|
335 | COREAUDIOCONVCBCTX convCbCtx;
|
---|
336 | #endif
|
---|
337 | /** The ratio between the device & the stream sample rate. */
|
---|
338 | Float64 sampleRatio;
|
---|
339 | } COREAUDIOSTREAMIN, *PCOREAUDIOSTREAMIN;
|
---|
340 |
|
---|
341 | /**
|
---|
342 | * Structure for keeping the output stream specifics.
|
---|
343 | */
|
---|
344 | typedef struct COREAUDIOSTREAMOUT
|
---|
345 | {
|
---|
346 | /** Nothing here yet. */
|
---|
347 | } COREAUDIOSTREAMOUT, *PCOREAUDIOSTREAMOUT;
|
---|
348 |
|
---|
349 | /**
|
---|
350 | * Structure for maintaining a Core Audio stream.
|
---|
351 | */
|
---|
352 | typedef struct COREAUDIOSTREAM
|
---|
353 | {
|
---|
354 | /** The stream's acquired configuration. */
|
---|
355 | PPDMAUDIOSTREAMCFG pCfg;
|
---|
356 | /** Stream-specific data, depending on the stream type. */
|
---|
357 | union
|
---|
358 | {
|
---|
359 | COREAUDIOSTREAMIN In;
|
---|
360 | COREAUDIOSTREAMOUT Out;
|
---|
361 | };
|
---|
362 | /** List node for the device's stream list. */
|
---|
363 | RTLISTNODE Node;
|
---|
364 | /** Pointer to driver instance this stream is bound to. */
|
---|
365 | PDRVHOSTCOREAUDIO pDrv;
|
---|
366 | /** The stream's thread handle for maintaining the audio queue. */
|
---|
367 | RTTHREAD hThread;
|
---|
368 | /** Flag indicating to start a stream's data processing. */
|
---|
369 | bool fRun;
|
---|
370 | /** Whether the stream is in a running (active) state or not.
|
---|
371 | * For playback streams this means that audio data can be (or is being) played,
|
---|
372 | * for capturing streams this means that audio data is being captured (if available). */
|
---|
373 | bool fIsRunning;
|
---|
374 | /** Thread shutdown indicator. */
|
---|
375 | bool fShutdown;
|
---|
376 | /** Critical section for serializing access between thread + callbacks. */
|
---|
377 | RTCRITSECT CritSect;
|
---|
378 | /** The actual audio queue being used. */
|
---|
379 | AudioQueueRef audioQueue;
|
---|
380 | /** The audio buffers which are used with the above audio queue. */
|
---|
381 | AudioQueueBufferRef audioBuffer[2];
|
---|
382 | /** The acquired (final) audio format for this stream. */
|
---|
383 | AudioStreamBasicDescription asbdStream;
|
---|
384 | /** The audio unit for this stream. */
|
---|
385 | COREAUDIOUNIT Unit;
|
---|
386 | /** Initialization status tracker, actually COREAUDIOSTATUS.
|
---|
387 | * Used when some of the device parameters or the device itself is changed
|
---|
388 | * during the runtime. */
|
---|
389 | volatile uint32_t enmStatus;
|
---|
390 | /** An internal ring buffer for transferring data from/to the rendering callbacks. */
|
---|
391 | PRTCIRCBUF pCircBuf;
|
---|
392 | } COREAUDIOSTREAM, *PCOREAUDIOSTREAM;
|
---|
393 |
|
---|
394 | static int coreAudioStreamInit(PCOREAUDIOSTREAM pCAStream, PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICE pDev);
|
---|
395 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
396 | static int coreAudioStreamReinit(PDRVHOSTCOREAUDIO pThis, PCOREAUDIOSTREAM pCAStream, PPDMAUDIODEVICE pDev);
|
---|
397 | #endif
|
---|
398 | static int coreAudioStreamUninit(PCOREAUDIOSTREAM pCAStream);
|
---|
399 |
|
---|
400 | static int coreAudioStreamControl(PDRVHOSTCOREAUDIO pThis, PCOREAUDIOSTREAM pCAStream, PDMAUDIOSTREAMCMD enmStreamCmd);
|
---|
401 |
|
---|
402 | static int coreAudioDeviceRegisterCallbacks(PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICE pDev);
|
---|
403 | static int coreAudioDeviceUnregisterCallbacks(PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICE pDev);
|
---|
404 | static void coreAudioDeviceDataInit(PCOREAUDIODEVICEDATA pDevData, AudioDeviceID deviceID, bool fIsInput, PDRVHOSTCOREAUDIO pDrv);
|
---|
405 |
|
---|
406 | static DECLCALLBACK(OSStatus) coreAudioDevPropChgCb(AudioObjectID propertyID, UInt32 nAddresses,
|
---|
407 | const AudioObjectPropertyAddress properties[], void *pvUser);
|
---|
408 |
|
---|
409 | static int coreAudioStreamInitQueue(PCOREAUDIOSTREAM pCAStream, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq);
|
---|
410 | static DECLCALLBACK(void) coreAudioInputQueueCb(void *pvUser, AudioQueueRef audioQueue, AudioQueueBufferRef audioBuffer,
|
---|
411 | const AudioTimeStamp *pAudioTS, UInt32 cPacketDesc,
|
---|
412 | const AudioStreamPacketDescription *paPacketDesc);
|
---|
413 | static DECLCALLBACK(void) coreAudioOutputQueueCb(void *pvUser, AudioQueueRef audioQueue, AudioQueueBufferRef audioBuffer);
|
---|
414 |
|
---|
415 | #ifdef VBOX_WITH_AUDIO_CA_CONVERTER
|
---|
416 | /**
|
---|
417 | * Initializes a conversion callback context.
|
---|
418 | *
|
---|
419 | * @return IPRT status code.
|
---|
420 | * @param pConvCbCtx Conversion callback context to initialize.
|
---|
421 | * @param pStream Pointer to stream to use.
|
---|
422 | * @param pASBDSrc Input (source) stream description to use.
|
---|
423 | * @param pASBDDst Output (destination) stream description to use.
|
---|
424 | */
|
---|
425 | static int coreAudioInitConvCbCtx(PCOREAUDIOCONVCBCTX pConvCbCtx, PCOREAUDIOSTREAM pStream,
|
---|
426 | AudioStreamBasicDescription *pASBDSrc, AudioStreamBasicDescription *pASBDDst)
|
---|
427 | {
|
---|
428 | AssertPtrReturn(pConvCbCtx, VERR_INVALID_POINTER);
|
---|
429 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
430 | AssertPtrReturn(pASBDSrc, VERR_INVALID_POINTER);
|
---|
431 | AssertPtrReturn(pASBDDst, VERR_INVALID_POINTER);
|
---|
432 |
|
---|
433 | #ifdef DEBUG
|
---|
434 | coreAudioPrintASBD("CbCtx: Src", pASBDSrc);
|
---|
435 | coreAudioPrintASBD("CbCtx: Dst", pASBDDst);
|
---|
436 | #endif
|
---|
437 |
|
---|
438 | pConvCbCtx->pStream = pStream;
|
---|
439 |
|
---|
440 | memcpy(&pConvCbCtx->asbdSrc, pASBDSrc, sizeof(AudioStreamBasicDescription));
|
---|
441 | memcpy(&pConvCbCtx->asbdDst, pASBDDst, sizeof(AudioStreamBasicDescription));
|
---|
442 |
|
---|
443 | pConvCbCtx->pBufLstSrc = NULL;
|
---|
444 | pConvCbCtx->cErrors = 0;
|
---|
445 |
|
---|
446 | return VINF_SUCCESS;
|
---|
447 | }
|
---|
448 |
|
---|
449 |
|
---|
450 | /**
|
---|
451 | * Uninitializes a conversion callback context.
|
---|
452 | *
|
---|
453 | * @return IPRT status code.
|
---|
454 | * @param pConvCbCtx Conversion callback context to uninitialize.
|
---|
455 | */
|
---|
456 | static void coreAudioUninitConvCbCtx(PCOREAUDIOCONVCBCTX pConvCbCtx)
|
---|
457 | {
|
---|
458 | AssertPtrReturnVoid(pConvCbCtx);
|
---|
459 |
|
---|
460 | pConvCbCtx->pStream = NULL;
|
---|
461 |
|
---|
462 | RT_ZERO(pConvCbCtx->asbdSrc);
|
---|
463 | RT_ZERO(pConvCbCtx->asbdDst);
|
---|
464 |
|
---|
465 | pConvCbCtx->pBufLstSrc = NULL;
|
---|
466 | pConvCbCtx->cErrors = 0;
|
---|
467 | }
|
---|
468 | #endif /* VBOX_WITH_AUDIO_CA_CONVERTER */
|
---|
469 |
|
---|
470 |
|
---|
471 | /**
|
---|
472 | * Does a (re-)enumeration of the host's playback + recording devices.
|
---|
473 | *
|
---|
474 | * @return IPRT status code.
|
---|
475 | * @param pThis Host audio driver instance.
|
---|
476 | * @param enmUsage Which devices to enumerate.
|
---|
477 | * @param pDevEnm Where to store the enumerated devices.
|
---|
478 | */
|
---|
479 | static int coreAudioDevicesEnumerate(PDRVHOSTCOREAUDIO pThis, PDMAUDIODIR enmUsage, PPDMAUDIODEVICEENUM pDevEnm)
|
---|
480 | {
|
---|
481 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
482 | AssertPtrReturn(pDevEnm, VERR_INVALID_POINTER);
|
---|
483 |
|
---|
484 | int rc = VINF_SUCCESS;
|
---|
485 |
|
---|
486 | do
|
---|
487 | {
|
---|
488 | AudioDeviceID defaultDeviceID = kAudioDeviceUnknown;
|
---|
489 |
|
---|
490 | /* Fetch the default audio device currently in use. */
|
---|
491 | AudioObjectPropertyAddress propAdrDefaultDev = { enmUsage == PDMAUDIODIR_IN
|
---|
492 | ? kAudioHardwarePropertyDefaultInputDevice
|
---|
493 | : kAudioHardwarePropertyDefaultOutputDevice,
|
---|
494 | kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
|
---|
495 | UInt32 uSize = sizeof(defaultDeviceID);
|
---|
496 | OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propAdrDefaultDev, 0, NULL, &uSize, &defaultDeviceID);
|
---|
497 | if (err != noErr)
|
---|
498 | {
|
---|
499 | LogRel(("CoreAudio: Unable to determine default %s device (%RI32)\n",
|
---|
500 | enmUsage == PDMAUDIODIR_IN ? "capturing" : "playback", err));
|
---|
501 | return VERR_NOT_FOUND;
|
---|
502 | }
|
---|
503 |
|
---|
504 | if (defaultDeviceID == kAudioDeviceUnknown)
|
---|
505 | {
|
---|
506 | LogFunc(("No default %s device found\n", enmUsage == PDMAUDIODIR_IN ? "capturing" : "playback"));
|
---|
507 | /* Keep going. */
|
---|
508 | }
|
---|
509 |
|
---|
510 | AudioObjectPropertyAddress propAdrDevList = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal,
|
---|
511 | kAudioObjectPropertyElementMaster };
|
---|
512 |
|
---|
513 | err = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propAdrDevList, 0, NULL, &uSize);
|
---|
514 | if (err != kAudioHardwareNoError)
|
---|
515 | break;
|
---|
516 |
|
---|
517 | AudioDeviceID *pDevIDs = (AudioDeviceID *)alloca(uSize);
|
---|
518 | if (pDevIDs == NULL)
|
---|
519 | break;
|
---|
520 |
|
---|
521 | err = AudioObjectGetPropertyData(kAudioObjectSystemObject, &propAdrDevList, 0, NULL, &uSize, pDevIDs);
|
---|
522 | if (err != kAudioHardwareNoError)
|
---|
523 | break;
|
---|
524 |
|
---|
525 | rc = DrvAudioHlpDeviceEnumInit(pDevEnm);
|
---|
526 | if (RT_FAILURE(rc))
|
---|
527 | break;
|
---|
528 |
|
---|
529 | UInt16 cDevices = uSize / sizeof(AudioDeviceID);
|
---|
530 |
|
---|
531 | PPDMAUDIODEVICE pDev = NULL;
|
---|
532 | for (UInt16 i = 0; i < cDevices; i++)
|
---|
533 | {
|
---|
534 | if (pDev) /* Some (skipped) device to clean up first? */
|
---|
535 | DrvAudioHlpDeviceFree(pDev);
|
---|
536 |
|
---|
537 | pDev = DrvAudioHlpDeviceAlloc(sizeof(COREAUDIODEVICEDATA));
|
---|
538 | if (!pDev)
|
---|
539 | {
|
---|
540 | rc = VERR_NO_MEMORY;
|
---|
541 | break;
|
---|
542 | }
|
---|
543 |
|
---|
544 | /* Set usage. */
|
---|
545 | pDev->enmUsage = enmUsage;
|
---|
546 |
|
---|
547 | /* Init backend-specific device data. */
|
---|
548 | PCOREAUDIODEVICEDATA pDevData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
549 | AssertPtr(pDevData);
|
---|
550 | coreAudioDeviceDataInit(pDevData, pDevIDs[i], enmUsage == PDMAUDIODIR_IN, pThis);
|
---|
551 |
|
---|
552 | /* Check if the device is valid. */
|
---|
553 | AudioDeviceID curDevID = pDevData->deviceID;
|
---|
554 |
|
---|
555 | /* Is the device the default device? */
|
---|
556 | if (curDevID == defaultDeviceID)
|
---|
557 | pDev->fFlags |= PDMAUDIODEV_FLAGS_DEFAULT;
|
---|
558 |
|
---|
559 | AudioObjectPropertyAddress propAddrCfg = { kAudioDevicePropertyStreamConfiguration,
|
---|
560 | enmUsage == PDMAUDIODIR_IN
|
---|
561 | ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
|
---|
562 | kAudioObjectPropertyElementMaster };
|
---|
563 |
|
---|
564 | err = AudioObjectGetPropertyDataSize(curDevID, &propAddrCfg, 0, NULL, &uSize);
|
---|
565 | if (err != noErr)
|
---|
566 | continue;
|
---|
567 |
|
---|
568 | AudioBufferList *pBufList = (AudioBufferList *)RTMemAlloc(uSize);
|
---|
569 | if (!pBufList)
|
---|
570 | continue;
|
---|
571 |
|
---|
572 | err = AudioObjectGetPropertyData(curDevID, &propAddrCfg, 0, NULL, &uSize, pBufList);
|
---|
573 | if (err == noErr)
|
---|
574 | {
|
---|
575 | for (UInt32 a = 0; a < pBufList->mNumberBuffers; a++)
|
---|
576 | {
|
---|
577 | if (enmUsage == PDMAUDIODIR_IN)
|
---|
578 | pDev->cMaxInputChannels += pBufList->mBuffers[a].mNumberChannels;
|
---|
579 | else if (enmUsage == PDMAUDIODIR_OUT)
|
---|
580 | pDev->cMaxOutputChannels += pBufList->mBuffers[a].mNumberChannels;
|
---|
581 | }
|
---|
582 | }
|
---|
583 |
|
---|
584 | if (pBufList)
|
---|
585 | {
|
---|
586 | RTMemFree(pBufList);
|
---|
587 | pBufList = NULL;
|
---|
588 | }
|
---|
589 |
|
---|
590 | /* Check if the device is valid, e.g. has any input/output channels according to its usage. */
|
---|
591 | if ( enmUsage == PDMAUDIODIR_IN
|
---|
592 | && !pDev->cMaxInputChannels)
|
---|
593 | continue;
|
---|
594 | if ( enmUsage == PDMAUDIODIR_OUT
|
---|
595 | && !pDev->cMaxOutputChannels)
|
---|
596 | continue;
|
---|
597 |
|
---|
598 | /* Resolve the device's name. */
|
---|
599 | AudioObjectPropertyAddress propAddrName = { kAudioObjectPropertyName,
|
---|
600 | enmUsage == PDMAUDIODIR_IN
|
---|
601 | ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
|
---|
602 | kAudioObjectPropertyElementMaster };
|
---|
603 | uSize = sizeof(CFStringRef);
|
---|
604 | CFStringRef pcfstrName = NULL;
|
---|
605 |
|
---|
606 | err = AudioObjectGetPropertyData(curDevID, &propAddrName, 0, NULL, &uSize, &pcfstrName);
|
---|
607 | if (err != kAudioHardwareNoError)
|
---|
608 | continue;
|
---|
609 |
|
---|
610 | CFIndex cbName = CFStringGetMaximumSizeForEncoding(CFStringGetLength(pcfstrName), kCFStringEncodingUTF8) + 1;
|
---|
611 | if (cbName)
|
---|
612 | {
|
---|
613 | char *pszName = (char *)RTStrAlloc(cbName);
|
---|
614 | if ( pszName
|
---|
615 | && CFStringGetCString(pcfstrName, pszName, cbName, kCFStringEncodingUTF8))
|
---|
616 | RTStrCopy(pDev->szName, sizeof(pDev->szName), pszName);
|
---|
617 |
|
---|
618 | LogFunc(("Device '%s': %RU32\n", pszName, curDevID));
|
---|
619 |
|
---|
620 | if (pszName)
|
---|
621 | {
|
---|
622 | RTStrFree(pszName);
|
---|
623 | pszName = NULL;
|
---|
624 | }
|
---|
625 | }
|
---|
626 |
|
---|
627 | CFRelease(pcfstrName);
|
---|
628 |
|
---|
629 | /* Check if the device is alive for the intended usage. */
|
---|
630 | AudioObjectPropertyAddress propAddrAlive = { kAudioDevicePropertyDeviceIsAlive,
|
---|
631 | enmUsage == PDMAUDIODIR_IN
|
---|
632 | ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
|
---|
633 | kAudioObjectPropertyElementMaster };
|
---|
634 |
|
---|
635 | UInt32 uAlive = 0;
|
---|
636 | uSize = sizeof(uAlive);
|
---|
637 |
|
---|
638 | err = AudioObjectGetPropertyData(curDevID, &propAddrAlive, 0, NULL, &uSize, &uAlive);
|
---|
639 | if ( (err == noErr)
|
---|
640 | && !uAlive)
|
---|
641 | {
|
---|
642 | pDev->fFlags |= PDMAUDIODEV_FLAGS_DEAD;
|
---|
643 | }
|
---|
644 |
|
---|
645 | /* Check if the device is being hogged by someone else. */
|
---|
646 | AudioObjectPropertyAddress propAddrHogged = { kAudioDevicePropertyHogMode,
|
---|
647 | kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster };
|
---|
648 |
|
---|
649 | pid_t pid = 0;
|
---|
650 | uSize = sizeof(pid);
|
---|
651 |
|
---|
652 | err = AudioObjectGetPropertyData(curDevID, &propAddrHogged, 0, NULL, &uSize, &pid);
|
---|
653 | if ( (err == noErr)
|
---|
654 | && (pid != -1))
|
---|
655 | {
|
---|
656 | pDev->fFlags |= PDMAUDIODEV_FLAGS_LOCKED;
|
---|
657 | }
|
---|
658 |
|
---|
659 | /* Add the device to the enumeration. */
|
---|
660 | rc = DrvAudioHlpDeviceEnumAdd(pDevEnm, pDev);
|
---|
661 | if (RT_FAILURE(rc))
|
---|
662 | break;
|
---|
663 |
|
---|
664 | /* NULL device pointer because it's now part of the device enumeration. */
|
---|
665 | pDev = NULL;
|
---|
666 | }
|
---|
667 |
|
---|
668 | if (RT_FAILURE(rc))
|
---|
669 | {
|
---|
670 | DrvAudioHlpDeviceFree(pDev);
|
---|
671 | pDev = NULL;
|
---|
672 | }
|
---|
673 |
|
---|
674 | } while (0);
|
---|
675 |
|
---|
676 | if (RT_SUCCESS(rc))
|
---|
677 | {
|
---|
678 | #ifdef DEBUG
|
---|
679 | LogFunc(("Devices for pDevEnm=%p, enmUsage=%RU32:\n", pDevEnm, enmUsage));
|
---|
680 | DrvAudioHlpDeviceEnumPrint("Core Audio", pDevEnm);
|
---|
681 | #endif
|
---|
682 | }
|
---|
683 | else
|
---|
684 | DrvAudioHlpDeviceEnumFree(pDevEnm);
|
---|
685 |
|
---|
686 | LogFlowFuncLeaveRC(rc);
|
---|
687 | return rc;
|
---|
688 | }
|
---|
689 |
|
---|
690 |
|
---|
691 | /**
|
---|
692 | * Checks if an audio device with a specific device ID is in the given device
|
---|
693 | * enumeration or not.
|
---|
694 | *
|
---|
695 | * @retval true if the node is the last element in the list.
|
---|
696 | * @retval false otherwise.
|
---|
697 | *
|
---|
698 | * @param pEnmSrc Device enumeration to search device ID in.
|
---|
699 | * @param deviceID Device ID to search.
|
---|
700 | */
|
---|
701 | bool coreAudioDevicesHasDevice(PPDMAUDIODEVICEENUM pEnmSrc, AudioDeviceID deviceID)
|
---|
702 | {
|
---|
703 | PPDMAUDIODEVICE pDevSrc;
|
---|
704 | RTListForEach(&pEnmSrc->lstDevices, pDevSrc, PDMAUDIODEVICE, Node)
|
---|
705 | {
|
---|
706 | PCOREAUDIODEVICEDATA pDevSrcData = (PCOREAUDIODEVICEDATA)pDevSrc->pvData;
|
---|
707 | AssertPtr(pDevSrcData);
|
---|
708 |
|
---|
709 | if (pDevSrcData->deviceID == deviceID)
|
---|
710 | return true;
|
---|
711 | }
|
---|
712 |
|
---|
713 | return false;
|
---|
714 | }
|
---|
715 |
|
---|
716 |
|
---|
717 | /**
|
---|
718 | * Enumerates all host devices and builds a final device enumeration list, consisting
|
---|
719 | * of (duplex) input and output devices.
|
---|
720 | *
|
---|
721 | * @return IPRT status code.
|
---|
722 | * @param pThis Host audio driver instance.
|
---|
723 | * @param pEnmDst Where to store the device enumeration list.
|
---|
724 | */
|
---|
725 | int coreAudioDevicesEnumerateAll(PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICEENUM pEnmDst)
|
---|
726 | {
|
---|
727 | PDMAUDIODEVICEENUM devEnmIn;
|
---|
728 | int rc = coreAudioDevicesEnumerate(pThis, PDMAUDIODIR_IN, &devEnmIn);
|
---|
729 | if (RT_SUCCESS(rc))
|
---|
730 | {
|
---|
731 | PDMAUDIODEVICEENUM devEnmOut;
|
---|
732 | rc = coreAudioDevicesEnumerate(pThis, PDMAUDIODIR_OUT, &devEnmOut);
|
---|
733 | if (RT_SUCCESS(rc))
|
---|
734 | {
|
---|
735 | /*
|
---|
736 | * Build up the final device enumeration, based on the input and output device lists
|
---|
737 | * just enumerated.
|
---|
738 | *
|
---|
739 | * Also make sure to handle duplex devices, that is, devices which act as input and output
|
---|
740 | * at the same time.
|
---|
741 | */
|
---|
742 |
|
---|
743 | rc = DrvAudioHlpDeviceEnumInit(pEnmDst);
|
---|
744 | if (RT_SUCCESS(rc))
|
---|
745 | {
|
---|
746 | PPDMAUDIODEVICE pDevSrcIn;
|
---|
747 | RTListForEach(&devEnmIn.lstDevices, pDevSrcIn, PDMAUDIODEVICE, Node)
|
---|
748 | {
|
---|
749 | PCOREAUDIODEVICEDATA pDevSrcInData = (PCOREAUDIODEVICEDATA)pDevSrcIn->pvData;
|
---|
750 | AssertPtr(pDevSrcInData);
|
---|
751 |
|
---|
752 | PPDMAUDIODEVICE pDevDst = DrvAudioHlpDeviceAlloc(sizeof(COREAUDIODEVICEDATA));
|
---|
753 | if (!pDevDst)
|
---|
754 | {
|
---|
755 | rc = VERR_NO_MEMORY;
|
---|
756 | break;
|
---|
757 | }
|
---|
758 |
|
---|
759 | PCOREAUDIODEVICEDATA pDevDstData = (PCOREAUDIODEVICEDATA)pDevDst->pvData;
|
---|
760 | AssertPtr(pDevDstData);
|
---|
761 | coreAudioDeviceDataInit(pDevDstData, pDevSrcInData->deviceID, true /* fIsInput */, pThis);
|
---|
762 |
|
---|
763 | RTStrCopy(pDevDst->szName, sizeof(pDevDst->szName), pDevSrcIn->szName);
|
---|
764 |
|
---|
765 | pDevDst->enmUsage = PDMAUDIODIR_IN; /* Input device by default (simplex). */
|
---|
766 | pDevDst->cMaxInputChannels = pDevSrcIn->cMaxInputChannels;
|
---|
767 |
|
---|
768 | /* Handle flags. */
|
---|
769 | if (pDevSrcIn->fFlags & PDMAUDIODEV_FLAGS_DEFAULT)
|
---|
770 | pDevDst->fFlags |= PDMAUDIODEV_FLAGS_DEFAULT;
|
---|
771 | /** @todo Handle hot plugging? */
|
---|
772 |
|
---|
773 | /*
|
---|
774 | * Now search through the list of all found output devices and check if we found
|
---|
775 | * an output device with the same device ID as the currently handled input device.
|
---|
776 | *
|
---|
777 | * If found, this means we have to treat that device as a duplex device then.
|
---|
778 | */
|
---|
779 | PPDMAUDIODEVICE pDevSrcOut;
|
---|
780 | RTListForEach(&devEnmOut.lstDevices, pDevSrcOut, PDMAUDIODEVICE, Node)
|
---|
781 | {
|
---|
782 | PCOREAUDIODEVICEDATA pDevSrcOutData = (PCOREAUDIODEVICEDATA)pDevSrcOut->pvData;
|
---|
783 | AssertPtr(pDevSrcOutData);
|
---|
784 |
|
---|
785 | if (pDevSrcInData->deviceID == pDevSrcOutData->deviceID)
|
---|
786 | {
|
---|
787 | pDevDst->enmUsage = PDMAUDIODIR_ANY;
|
---|
788 | pDevDst->cMaxOutputChannels = pDevSrcOut->cMaxOutputChannels;
|
---|
789 | break;
|
---|
790 | }
|
---|
791 | }
|
---|
792 |
|
---|
793 | if (RT_SUCCESS(rc))
|
---|
794 | {
|
---|
795 | rc = DrvAudioHlpDeviceEnumAdd(pEnmDst, pDevDst);
|
---|
796 | }
|
---|
797 | else
|
---|
798 | {
|
---|
799 | DrvAudioHlpDeviceFree(pDevDst);
|
---|
800 | pDevDst = NULL;
|
---|
801 | }
|
---|
802 | }
|
---|
803 |
|
---|
804 | if (RT_SUCCESS(rc))
|
---|
805 | {
|
---|
806 | /*
|
---|
807 | * As a last step, add all remaining output devices which have not been handled in the loop above,
|
---|
808 | * that is, all output devices which operate in simplex mode.
|
---|
809 | */
|
---|
810 | PPDMAUDIODEVICE pDevSrcOut;
|
---|
811 | RTListForEach(&devEnmOut.lstDevices, pDevSrcOut, PDMAUDIODEVICE, Node)
|
---|
812 | {
|
---|
813 | PCOREAUDIODEVICEDATA pDevSrcOutData = (PCOREAUDIODEVICEDATA)pDevSrcOut->pvData;
|
---|
814 | AssertPtr(pDevSrcOutData);
|
---|
815 |
|
---|
816 | if (coreAudioDevicesHasDevice(pEnmDst, pDevSrcOutData->deviceID))
|
---|
817 | continue; /* Already in our list, skip. */
|
---|
818 |
|
---|
819 | PPDMAUDIODEVICE pDevDst = DrvAudioHlpDeviceAlloc(sizeof(COREAUDIODEVICEDATA));
|
---|
820 | if (!pDevDst)
|
---|
821 | {
|
---|
822 | rc = VERR_NO_MEMORY;
|
---|
823 | break;
|
---|
824 | }
|
---|
825 |
|
---|
826 | PCOREAUDIODEVICEDATA pDevDstData = (PCOREAUDIODEVICEDATA)pDevDst->pvData;
|
---|
827 | AssertPtr(pDevDstData);
|
---|
828 | coreAudioDeviceDataInit(pDevDstData, pDevSrcOutData->deviceID, false /* fIsInput */, pThis);
|
---|
829 |
|
---|
830 | RTStrCopy(pDevDst->szName, sizeof(pDevDst->szName), pDevSrcOut->szName);
|
---|
831 |
|
---|
832 | pDevDst->enmUsage = PDMAUDIODIR_OUT;
|
---|
833 | pDevDst->cMaxOutputChannels = pDevSrcOut->cMaxOutputChannels;
|
---|
834 |
|
---|
835 | pDevDstData->deviceID = pDevSrcOutData->deviceID;
|
---|
836 |
|
---|
837 | /* Handle flags. */
|
---|
838 | if (pDevSrcOut->fFlags & PDMAUDIODEV_FLAGS_DEFAULT)
|
---|
839 | pDevDst->fFlags |= PDMAUDIODEV_FLAGS_DEFAULT;
|
---|
840 | /** @todo Handle hot plugging? */
|
---|
841 |
|
---|
842 | rc = DrvAudioHlpDeviceEnumAdd(pEnmDst, pDevDst);
|
---|
843 | if (RT_FAILURE(rc))
|
---|
844 | {
|
---|
845 | DrvAudioHlpDeviceFree(pDevDst);
|
---|
846 | break;
|
---|
847 | }
|
---|
848 | }
|
---|
849 | }
|
---|
850 |
|
---|
851 | if (RT_FAILURE(rc))
|
---|
852 | DrvAudioHlpDeviceEnumFree(pEnmDst);
|
---|
853 | }
|
---|
854 |
|
---|
855 | DrvAudioHlpDeviceEnumFree(&devEnmOut);
|
---|
856 | }
|
---|
857 |
|
---|
858 | DrvAudioHlpDeviceEnumFree(&devEnmIn);
|
---|
859 | }
|
---|
860 |
|
---|
861 | #ifdef DEBUG
|
---|
862 | if (RT_SUCCESS(rc))
|
---|
863 | DrvAudioHlpDeviceEnumPrint("Core Audio (Final)", pEnmDst);
|
---|
864 | #endif
|
---|
865 |
|
---|
866 | LogFlowFuncLeaveRC(rc);
|
---|
867 | return rc;
|
---|
868 | }
|
---|
869 |
|
---|
870 |
|
---|
871 | /**
|
---|
872 | * Initializes a Core Audio-specific device data structure.
|
---|
873 | *
|
---|
874 | * @returns IPRT status code.
|
---|
875 | * @param pDevData Device data structure to initialize.
|
---|
876 | * @param deviceID Core Audio device ID to assign this structure to.
|
---|
877 | * @param fIsInput Whether this is an input device or not.
|
---|
878 | * @param pDrv Driver instance to use.
|
---|
879 | */
|
---|
880 | static void coreAudioDeviceDataInit(PCOREAUDIODEVICEDATA pDevData, AudioDeviceID deviceID, bool fIsInput, PDRVHOSTCOREAUDIO pDrv)
|
---|
881 | {
|
---|
882 | AssertPtrReturnVoid(pDevData);
|
---|
883 | AssertPtrReturnVoid(pDrv);
|
---|
884 |
|
---|
885 | pDevData->deviceID = deviceID;
|
---|
886 | pDevData->pDrv = pDrv;
|
---|
887 |
|
---|
888 | /* Get the device UUID. */
|
---|
889 | AudioObjectPropertyAddress propAdrDevUUID = { kAudioDevicePropertyDeviceUID,
|
---|
890 | fIsInput ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput,
|
---|
891 | kAudioObjectPropertyElementMaster };
|
---|
892 | UInt32 uSize = sizeof(pDevData->UUID);
|
---|
893 | OSStatus err = AudioObjectGetPropertyData(pDevData->deviceID, &propAdrDevUUID, 0, NULL, &uSize, &pDevData->UUID);
|
---|
894 | if (err != noErr)
|
---|
895 | LogRel(("CoreAudio: Failed to retrieve device UUID for device %RU32 (%RI32)\n", deviceID, err));
|
---|
896 |
|
---|
897 | RTListInit(&pDevData->lstStreams);
|
---|
898 | }
|
---|
899 |
|
---|
900 |
|
---|
901 | /**
|
---|
902 | * Propagates an audio device status to all its connected Core Audio streams.
|
---|
903 | *
|
---|
904 | * @return IPRT status code.
|
---|
905 | * @param pDev Audio device to propagate status for.
|
---|
906 | * @param enmSts Status to propagate.
|
---|
907 | */
|
---|
908 | static int coreAudioDevicePropagateStatus(PPDMAUDIODEVICE pDev, COREAUDIOSTATUS enmSts)
|
---|
909 | {
|
---|
910 | AssertPtrReturn(pDev, VERR_INVALID_POINTER);
|
---|
911 |
|
---|
912 | PCOREAUDIODEVICEDATA pDevData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
913 | AssertPtrReturn(pDevData, VERR_INVALID_POINTER);
|
---|
914 |
|
---|
915 | /* Sanity. */
|
---|
916 | AssertPtr(pDevData->pDrv);
|
---|
917 |
|
---|
918 | LogFlowFunc(("pDev=%p, pDevData=%p, enmSts=%RU32\n", pDev, pDevData, enmSts));
|
---|
919 |
|
---|
920 | PCOREAUDIOSTREAM pCAStream;
|
---|
921 | RTListForEach(&pDevData->lstStreams, pCAStream, COREAUDIOSTREAM, Node)
|
---|
922 | {
|
---|
923 | LogFlowFunc(("pCAStream=%p\n", pCAStream));
|
---|
924 |
|
---|
925 | /* We move the reinitialization to the next output event.
|
---|
926 | * This make sure this thread isn't blocked and the
|
---|
927 | * reinitialization is done when necessary only. */
|
---|
928 | ASMAtomicXchgU32(&pCAStream->enmStatus, enmSts);
|
---|
929 | }
|
---|
930 |
|
---|
931 | return VINF_SUCCESS;
|
---|
932 | }
|
---|
933 |
|
---|
934 |
|
---|
935 | static DECLCALLBACK(OSStatus) coreAudioDeviceStateChangedCb(AudioObjectID propertyID,
|
---|
936 | UInt32 nAddresses,
|
---|
937 | const AudioObjectPropertyAddress properties[],
|
---|
938 | void *pvUser)
|
---|
939 | {
|
---|
940 | RT_NOREF(propertyID, nAddresses, properties);
|
---|
941 |
|
---|
942 | LogFlowFunc(("propertyID=%u, nAddresses=%u, pvUser=%p\n", propertyID, nAddresses, pvUser));
|
---|
943 |
|
---|
944 | PPDMAUDIODEVICE pDev = (PPDMAUDIODEVICE)pvUser;
|
---|
945 | AssertPtr(pDev);
|
---|
946 |
|
---|
947 | PCOREAUDIODEVICEDATA pData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
948 | AssertPtrReturn(pData, VERR_INVALID_POINTER);
|
---|
949 |
|
---|
950 | PDRVHOSTCOREAUDIO pThis = pData->pDrv;
|
---|
951 | AssertPtr(pThis);
|
---|
952 |
|
---|
953 | int rc2 = RTCritSectEnter(&pThis->CritSect);
|
---|
954 | AssertRC(rc2);
|
---|
955 |
|
---|
956 | UInt32 uAlive = 1;
|
---|
957 | UInt32 uSize = sizeof(UInt32);
|
---|
958 |
|
---|
959 | AudioObjectPropertyAddress propAdr = { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeGlobal,
|
---|
960 | kAudioObjectPropertyElementMaster };
|
---|
961 |
|
---|
962 | AudioDeviceID deviceID = pData->deviceID;
|
---|
963 |
|
---|
964 | OSStatus err = AudioObjectGetPropertyData(deviceID, &propAdr, 0, NULL, &uSize, &uAlive);
|
---|
965 |
|
---|
966 | bool fIsDead = false;
|
---|
967 |
|
---|
968 | if (err == kAudioHardwareBadDeviceError)
|
---|
969 | fIsDead = true; /* Unplugged. */
|
---|
970 | else if ((err == kAudioHardwareNoError) && (!RT_BOOL(uAlive)))
|
---|
971 | fIsDead = true; /* Something else happened. */
|
---|
972 |
|
---|
973 | if (fIsDead)
|
---|
974 | {
|
---|
975 | LogRel2(("CoreAudio: Device '%s' stopped functioning\n", pDev->szName));
|
---|
976 |
|
---|
977 | /* Mark device as dead. */
|
---|
978 | rc2 = coreAudioDevicePropagateStatus(pDev, COREAUDIOSTATUS_UNINIT);
|
---|
979 | AssertRC(rc2);
|
---|
980 | }
|
---|
981 |
|
---|
982 | rc2 = RTCritSectLeave(&pThis->CritSect);
|
---|
983 | AssertRC(rc2);
|
---|
984 |
|
---|
985 | return noErr;
|
---|
986 | }
|
---|
987 |
|
---|
988 | /* Callback for getting notified when the default recording/playback device has been changed. */
|
---|
989 | static DECLCALLBACK(OSStatus) coreAudioDefaultDeviceChangedCb(AudioObjectID propertyID,
|
---|
990 | UInt32 nAddresses,
|
---|
991 | const AudioObjectPropertyAddress properties[],
|
---|
992 | void *pvUser)
|
---|
993 | {
|
---|
994 | RT_NOREF(propertyID, nAddresses);
|
---|
995 |
|
---|
996 | LogFlowFunc(("propertyID=%u, nAddresses=%u, pvUser=%p\n", propertyID, nAddresses, pvUser));
|
---|
997 |
|
---|
998 | PDRVHOSTCOREAUDIO pThis = (PDRVHOSTCOREAUDIO)pvUser;
|
---|
999 | AssertPtr(pThis);
|
---|
1000 |
|
---|
1001 | int rc2 = RTCritSectEnter(&pThis->CritSect);
|
---|
1002 | AssertRC(rc2);
|
---|
1003 |
|
---|
1004 | for (UInt32 idxAddress = 0; idxAddress < nAddresses; idxAddress++)
|
---|
1005 | {
|
---|
1006 | PPDMAUDIODEVICE pDev = NULL;
|
---|
1007 |
|
---|
1008 | /*
|
---|
1009 | * Check if the default input / output device has been changed.
|
---|
1010 | */
|
---|
1011 | const AudioObjectPropertyAddress *pProperty = &properties[idxAddress];
|
---|
1012 | switch (pProperty->mSelector)
|
---|
1013 | {
|
---|
1014 | case kAudioHardwarePropertyDefaultInputDevice:
|
---|
1015 | LogFlowFunc(("kAudioHardwarePropertyDefaultInputDevice\n"));
|
---|
1016 | pDev = pThis->pDefaultDevIn;
|
---|
1017 | break;
|
---|
1018 |
|
---|
1019 | case kAudioHardwarePropertyDefaultOutputDevice:
|
---|
1020 | LogFlowFunc(("kAudioHardwarePropertyDefaultOutputDevice\n"));
|
---|
1021 | pDev = pThis->pDefaultDevOut;
|
---|
1022 | break;
|
---|
1023 |
|
---|
1024 | default:
|
---|
1025 | /* Skip others. */
|
---|
1026 | break;
|
---|
1027 | }
|
---|
1028 |
|
---|
1029 | LogFlowFunc(("pDev=%p\n", pDev));
|
---|
1030 |
|
---|
1031 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
1032 | if (pDev)
|
---|
1033 | {
|
---|
1034 | PCOREAUDIODEVICEDATA pData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
1035 | AssertPtr(pData);
|
---|
1036 |
|
---|
1037 | /* This listener is called on every change of the hardware
|
---|
1038 | * device. So check if the default device has really changed. */
|
---|
1039 | UInt32 uSize = sizeof(AudioDeviceID);
|
---|
1040 | UInt32 uResp = 0;
|
---|
1041 |
|
---|
1042 | OSStatus err = AudioObjectGetPropertyData(kAudioObjectSystemObject, pProperty, 0, NULL, &uSize, &uResp);
|
---|
1043 | if (err == noErr)
|
---|
1044 | {
|
---|
1045 | if (pData->deviceID != uResp) /* Has the device ID changed? */
|
---|
1046 | {
|
---|
1047 | rc2 = coreAudioDevicePropagateStatus(pDev, COREAUDIOSTATUS_REINIT);
|
---|
1048 | AssertRC(rc2);
|
---|
1049 | }
|
---|
1050 | }
|
---|
1051 | }
|
---|
1052 | #endif /* VBOX_WITH_AUDIO_CALLBACKS */
|
---|
1053 | }
|
---|
1054 |
|
---|
1055 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
1056 | PFNPDMHOSTAUDIOCALLBACK pfnCallback = pThis->pfnCallback;
|
---|
1057 | #endif
|
---|
1058 |
|
---|
1059 | /* Make sure to leave the critical section before calling the callback. */
|
---|
1060 | rc2 = RTCritSectLeave(&pThis->CritSect);
|
---|
1061 | AssertRC(rc2);
|
---|
1062 |
|
---|
1063 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
1064 | if (pfnCallback)
|
---|
1065 | /* Ignore rc */ pfnCallback(pThis->pDrvIns, PDMAUDIOBACKENDCBTYPE_DEVICES_CHANGED, NULL, 0);
|
---|
1066 | #endif
|
---|
1067 |
|
---|
1068 | return noErr;
|
---|
1069 | }
|
---|
1070 |
|
---|
1071 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
1072 | /**
|
---|
1073 | * Re-initializes a Core Audio stream with a specific audio device and stream configuration.
|
---|
1074 | *
|
---|
1075 | * @return IPRT status code.
|
---|
1076 | * @param pThis Driver instance.
|
---|
1077 | * @param pCAStream Audio stream to re-initialize.
|
---|
1078 | * @param pDev Audio device to use for re-initialization.
|
---|
1079 | * @param pCfg Stream configuration to use for re-initialization.
|
---|
1080 | */
|
---|
1081 | static int coreAudioStreamReinitEx(PDRVHOSTCOREAUDIO pThis,
|
---|
1082 | PCOREAUDIOSTREAM pCAStream, PPDMAUDIODEVICE pDev, PPDMAUDIOSTREAMCFG pCfg)
|
---|
1083 | {
|
---|
1084 | LogFunc(("pCAStream=%p\n", pCAStream));
|
---|
1085 |
|
---|
1086 | int rc = coreAudioStreamUninit(pCAStream);
|
---|
1087 | if (RT_SUCCESS(rc))
|
---|
1088 | {
|
---|
1089 | rc = coreAudioStreamInit(pCAStream, pThis, pDev);
|
---|
1090 | if (RT_SUCCESS(rc))
|
---|
1091 | {
|
---|
1092 | rc = coreAudioStreamInitQueue(pCAStream, pCfg /* pCfgReq */, NULL /* pCfgAcq */);
|
---|
1093 | if (RT_SUCCESS(rc))
|
---|
1094 | rc = coreAudioStreamControl(pCAStream->pDrv, pCAStream, PDMAUDIOSTREAMCMD_ENABLE);
|
---|
1095 |
|
---|
1096 | if (RT_FAILURE(rc))
|
---|
1097 | {
|
---|
1098 | int rc2 = coreAudioStreamUninit(pCAStream);
|
---|
1099 | AssertRC(rc2);
|
---|
1100 | }
|
---|
1101 | }
|
---|
1102 | }
|
---|
1103 |
|
---|
1104 | if (RT_FAILURE(rc))
|
---|
1105 | LogRel(("CoreAudio: Unable to re-init stream: %Rrc\n", rc));
|
---|
1106 |
|
---|
1107 | return rc;
|
---|
1108 | }
|
---|
1109 |
|
---|
1110 | /**
|
---|
1111 | * Re-initializes a Core Audio stream with a specific audio device.
|
---|
1112 | *
|
---|
1113 | * @return IPRT status code.
|
---|
1114 | * @param pThis Driver instance.
|
---|
1115 | * @param pCAStream Audio stream to re-initialize.
|
---|
1116 | * @param pDev Audio device to use for re-initialization.
|
---|
1117 | */
|
---|
1118 | static int coreAudioStreamReinit(PDRVHOSTCOREAUDIO pThis, PCOREAUDIOSTREAM pCAStream, PPDMAUDIODEVICE pDev)
|
---|
1119 | {
|
---|
1120 | int rc = coreAudioStreamUninit(pCAStream);
|
---|
1121 | if (RT_SUCCESS(rc))
|
---|
1122 | {
|
---|
1123 | /* Use the acquired stream configuration from the former initialization to
|
---|
1124 | * re-initialize the stream. */
|
---|
1125 | PDMAUDIOSTREAMCFG CfgAcq;
|
---|
1126 | rc = coreAudioASBDToStreamCfg(&pCAStream->Unit.streamFmt, &CfgAcq);
|
---|
1127 | if (RT_SUCCESS(rc))
|
---|
1128 | rc = coreAudioStreamReinitEx(pThis, pCAStream, pDev, &CfgAcq);
|
---|
1129 | }
|
---|
1130 |
|
---|
1131 | return rc;
|
---|
1132 | }
|
---|
1133 | #endif /* !VBOX_WITH_AUDIO_CALLBACKS */
|
---|
1134 |
|
---|
1135 | #ifdef VBOX_WITH_AUDIO_CA_CONVERTER
|
---|
1136 | /* Callback to convert audio input data from one format to another. */
|
---|
1137 | static DECLCALLBACK(OSStatus) coreAudioConverterCb(AudioConverterRef inAudioConverter,
|
---|
1138 | UInt32 *ioNumberDataPackets,
|
---|
1139 | AudioBufferList *ioData,
|
---|
1140 | AudioStreamPacketDescription **ppASPD,
|
---|
1141 | void *pvUser)
|
---|
1142 | {
|
---|
1143 | RT_NOREF(inAudioConverter);
|
---|
1144 |
|
---|
1145 | AssertPtrReturn(ioNumberDataPackets, caConverterEOFDErr);
|
---|
1146 | AssertPtrReturn(ioData, caConverterEOFDErr);
|
---|
1147 |
|
---|
1148 | PCOREAUDIOCONVCBCTX pConvCbCtx = (PCOREAUDIOCONVCBCTX)pvUser;
|
---|
1149 | AssertPtr(pConvCbCtx);
|
---|
1150 |
|
---|
1151 | /* Initialize values. */
|
---|
1152 | ioData->mBuffers[0].mNumberChannels = 0;
|
---|
1153 | ioData->mBuffers[0].mDataByteSize = 0;
|
---|
1154 | ioData->mBuffers[0].mData = NULL;
|
---|
1155 |
|
---|
1156 | if (ppASPD)
|
---|
1157 | {
|
---|
1158 | Log3Func(("Handling packet description not implemented\n"));
|
---|
1159 | }
|
---|
1160 | else
|
---|
1161 | {
|
---|
1162 | /** @todo Check converter ID? */
|
---|
1163 |
|
---|
1164 | /** @todo Handled non-interleaved data by going through the full buffer list,
|
---|
1165 | * not only through the first buffer like we do now. */
|
---|
1166 | Log3Func(("ioNumberDataPackets=%RU32\n", *ioNumberDataPackets));
|
---|
1167 |
|
---|
1168 | UInt32 cNumberDataPackets = *ioNumberDataPackets;
|
---|
1169 | Assert(pConvCbCtx->uPacketIdx + cNumberDataPackets <= pConvCbCtx->uPacketCnt);
|
---|
1170 |
|
---|
1171 | if (cNumberDataPackets)
|
---|
1172 | {
|
---|
1173 | AssertPtr(pConvCbCtx->pBufLstSrc);
|
---|
1174 | Assert(pConvCbCtx->pBufLstSrc->mNumberBuffers == 1); /* Only one buffer for the source supported atm. */
|
---|
1175 |
|
---|
1176 | AudioStreamBasicDescription *pSrcASBD = &pConvCbCtx->asbdSrc;
|
---|
1177 | AudioBuffer *pSrcBuf = &pConvCbCtx->pBufLstSrc->mBuffers[0];
|
---|
1178 |
|
---|
1179 | size_t cbOff = pConvCbCtx->uPacketIdx * pSrcASBD->mBytesPerPacket;
|
---|
1180 |
|
---|
1181 | cNumberDataPackets = RT_MIN((pSrcBuf->mDataByteSize - cbOff) / pSrcASBD->mBytesPerPacket,
|
---|
1182 | cNumberDataPackets);
|
---|
1183 |
|
---|
1184 | void *pvAvail = (uint8_t *)pSrcBuf->mData + cbOff;
|
---|
1185 | size_t cbAvail = RT_MIN(pSrcBuf->mDataByteSize - cbOff, cNumberDataPackets * pSrcASBD->mBytesPerPacket);
|
---|
1186 |
|
---|
1187 | Log3Func(("cNumberDataPackets=%RU32, cbOff=%zu, cbAvail=%zu\n", cNumberDataPackets, cbOff, cbAvail));
|
---|
1188 |
|
---|
1189 | /* Set input data for the converter to use.
|
---|
1190 | * Note: For VBR (Variable Bit Rates) or interleaved data handling we need multiple buffers here. */
|
---|
1191 | ioData->mNumberBuffers = 1;
|
---|
1192 |
|
---|
1193 | ioData->mBuffers[0].mNumberChannels = pSrcBuf->mNumberChannels;
|
---|
1194 | ioData->mBuffers[0].mDataByteSize = cbAvail;
|
---|
1195 | ioData->mBuffers[0].mData = pvAvail;
|
---|
1196 |
|
---|
1197 | #ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
|
---|
1198 | RTFILE fh;
|
---|
1199 | int rc = RTFileOpen(&fh,VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "caConverterCbInput.pcm",
|
---|
1200 | RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
|
---|
1201 | if (RT_SUCCESS(rc))
|
---|
1202 | {
|
---|
1203 | RTFileWrite(fh, pvAvail, cbAvail, NULL);
|
---|
1204 | RTFileClose(fh);
|
---|
1205 | }
|
---|
1206 | else
|
---|
1207 | AssertFailed();
|
---|
1208 | #endif
|
---|
1209 | pConvCbCtx->uPacketIdx += cNumberDataPackets;
|
---|
1210 | Assert(pConvCbCtx->uPacketIdx <= pConvCbCtx->uPacketCnt);
|
---|
1211 |
|
---|
1212 | *ioNumberDataPackets = cNumberDataPackets;
|
---|
1213 | }
|
---|
1214 | }
|
---|
1215 |
|
---|
1216 | Log3Func(("%RU32 / %RU32 -> ioNumberDataPackets=%RU32\n",
|
---|
1217 | pConvCbCtx->uPacketIdx, pConvCbCtx->uPacketCnt, *ioNumberDataPackets));
|
---|
1218 |
|
---|
1219 | return noErr;
|
---|
1220 | }
|
---|
1221 | #endif /* VBOX_WITH_AUDIO_CA_CONVERTER */
|
---|
1222 |
|
---|
1223 |
|
---|
1224 | /**
|
---|
1225 | * Initializes a Core Audio stream.
|
---|
1226 | *
|
---|
1227 | * @return IPRT status code.
|
---|
1228 | * @param pThis Driver instance.
|
---|
1229 | * @param pCAStream Stream to initialize.
|
---|
1230 | * @param pDev Audio device to use for this stream.
|
---|
1231 | */
|
---|
1232 | static int coreAudioStreamInit(PCOREAUDIOSTREAM pCAStream, PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICE pDev)
|
---|
1233 | {
|
---|
1234 | AssertPtrReturn(pCAStream, VERR_INVALID_POINTER);
|
---|
1235 | AssertPtrReturn(pThis, VERR_INVALID_POINTER);
|
---|
1236 | AssertPtrReturn(pDev, VERR_INVALID_POINTER);
|
---|
1237 |
|
---|
1238 | Assert(pCAStream->Unit.pDevice == NULL); /* Make sure no device is assigned yet. */
|
---|
1239 | AssertPtr(pDev->pvData);
|
---|
1240 | Assert(pDev->cbData == sizeof(COREAUDIODEVICEDATA));
|
---|
1241 |
|
---|
1242 | #ifdef DEBUG
|
---|
1243 | PCOREAUDIODEVICEDATA pData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
1244 | LogFunc(("pCAStream=%p, pDev=%p ('%s', ID=%RU32)\n", pCAStream, pDev, pDev->szName, pData->deviceID));
|
---|
1245 | #endif
|
---|
1246 |
|
---|
1247 | pCAStream->Unit.pDevice = pDev;
|
---|
1248 | pCAStream->pDrv = pThis;
|
---|
1249 |
|
---|
1250 | return VINF_SUCCESS;
|
---|
1251 | }
|
---|
1252 |
|
---|
1253 | # define CA_BREAK_STMT(stmt) \
|
---|
1254 | stmt; \
|
---|
1255 | break;
|
---|
1256 |
|
---|
1257 | /**
|
---|
1258 | * Thread for a Core Audio stream's audio queue handling.
|
---|
1259 | *
|
---|
1260 | * This thread is required per audio queue to pump data to/from the Core Audio
|
---|
1261 | * stream and handling its callbacks.
|
---|
1262 | *
|
---|
1263 | * @returns IPRT status code.
|
---|
1264 | * @param hThreadSelf Thread handle.
|
---|
1265 | * @param pvUser User argument.
|
---|
1266 | */
|
---|
1267 | static DECLCALLBACK(int) coreAudioQueueThread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
1268 | {
|
---|
1269 | RT_NOREF(hThreadSelf);
|
---|
1270 |
|
---|
1271 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pvUser;
|
---|
1272 | AssertPtr(pCAStream);
|
---|
1273 | AssertPtr(pCAStream->pCfg);
|
---|
1274 |
|
---|
1275 | const bool fIn = pCAStream->pCfg->enmDir == PDMAUDIODIR_IN;
|
---|
1276 |
|
---|
1277 | LogFunc(("Thread started for pCAStream=%p, fIn=%RTbool\n", pCAStream, fIn));
|
---|
1278 |
|
---|
1279 | /*
|
---|
1280 | * Create audio queue.
|
---|
1281 | */
|
---|
1282 | OSStatus err;
|
---|
1283 | if (fIn)
|
---|
1284 | err = AudioQueueNewInput(&pCAStream->asbdStream, coreAudioInputQueueCb, pCAStream /* pvData */,
|
---|
1285 | CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &pCAStream->audioQueue);
|
---|
1286 | else
|
---|
1287 | err = AudioQueueNewOutput(&pCAStream->asbdStream, coreAudioOutputQueueCb, pCAStream /* pvData */,
|
---|
1288 | CFRunLoopGetCurrent(), kCFRunLoopDefaultMode, 0, &pCAStream->audioQueue);
|
---|
1289 |
|
---|
1290 | if (err != noErr)
|
---|
1291 | return VERR_GENERAL_FAILURE; /** @todo Fudge! */
|
---|
1292 |
|
---|
1293 | /*
|
---|
1294 | * Assign device to queue.
|
---|
1295 | */
|
---|
1296 | PCOREAUDIODEVICEDATA pData = (PCOREAUDIODEVICEDATA)pCAStream->Unit.pDevice->pvData;
|
---|
1297 | AssertPtr(pData);
|
---|
1298 |
|
---|
1299 | UInt32 uSize = sizeof(pData->UUID);
|
---|
1300 | err = AudioQueueSetProperty(pCAStream->audioQueue, kAudioQueueProperty_CurrentDevice, &pData->UUID, uSize);
|
---|
1301 | if (err != noErr)
|
---|
1302 | return VERR_GENERAL_FAILURE; /** @todo Fudge! */
|
---|
1303 |
|
---|
1304 | const size_t cbBufSize = DrvAudioHlpFramesToBytes(pCAStream->pCfg->Backend.cFramesPeriod, &pCAStream->pCfg->Props);
|
---|
1305 |
|
---|
1306 | /*
|
---|
1307 | * Allocate audio buffers.
|
---|
1308 | */
|
---|
1309 | for (size_t i = 0; i < RT_ELEMENTS(pCAStream->audioBuffer); i++)
|
---|
1310 | {
|
---|
1311 | err = AudioQueueAllocateBuffer(pCAStream->audioQueue, cbBufSize, &pCAStream->audioBuffer[i]);
|
---|
1312 | if (err != noErr)
|
---|
1313 | break;
|
---|
1314 | }
|
---|
1315 |
|
---|
1316 | if (err != noErr)
|
---|
1317 | return VERR_GENERAL_FAILURE; /** @todo Fudge! */
|
---|
1318 |
|
---|
1319 | /* Signal the main thread before entering the main loop. */
|
---|
1320 | RTThreadUserSignal(RTThreadSelf());
|
---|
1321 |
|
---|
1322 | /*
|
---|
1323 | * Enter the main loop.
|
---|
1324 | */
|
---|
1325 | while (!ASMAtomicReadBool(&pCAStream->fShutdown))
|
---|
1326 | {
|
---|
1327 | CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.10, 1);
|
---|
1328 | }
|
---|
1329 |
|
---|
1330 | /*
|
---|
1331 | * Cleanup.
|
---|
1332 | */
|
---|
1333 | if (fIn)
|
---|
1334 | {
|
---|
1335 | AudioQueueStop(pCAStream->audioQueue, 1);
|
---|
1336 | }
|
---|
1337 | else
|
---|
1338 | {
|
---|
1339 | AudioQueueStop(pCAStream->audioQueue, 0);
|
---|
1340 | }
|
---|
1341 |
|
---|
1342 | for (size_t i = 0; i < RT_ELEMENTS(pCAStream->audioBuffer); i++)
|
---|
1343 | {
|
---|
1344 | if (pCAStream->audioBuffer[i])
|
---|
1345 | AudioQueueFreeBuffer(pCAStream->audioQueue, pCAStream->audioBuffer[i]);
|
---|
1346 | }
|
---|
1347 |
|
---|
1348 | AudioQueueDispose(pCAStream->audioQueue, 1);
|
---|
1349 |
|
---|
1350 | LogFunc(("Thread ended for pCAStream=%p, fIn=%RTbool\n", pCAStream, fIn));
|
---|
1351 | return VINF_SUCCESS;
|
---|
1352 | }
|
---|
1353 |
|
---|
1354 | /**
|
---|
1355 | * Processes input data of an audio queue buffer and stores it into a Core Audio stream.
|
---|
1356 | *
|
---|
1357 | * @returns IPRT status code.
|
---|
1358 | * @param pCAStream Core Audio stream to store input data into.
|
---|
1359 | * @param audioBuffer Audio buffer to process input data from.
|
---|
1360 | */
|
---|
1361 | int coreAudioInputQueueProcBuffer(PCOREAUDIOSTREAM pCAStream, AudioQueueBufferRef audioBuffer)
|
---|
1362 | {
|
---|
1363 | PRTCIRCBUF pCircBuf = pCAStream->pCircBuf;
|
---|
1364 | AssertPtr(pCircBuf);
|
---|
1365 |
|
---|
1366 | UInt8 *pvSrc = (UInt8 *)audioBuffer->mAudioData;
|
---|
1367 | UInt8 *pvDst = NULL;
|
---|
1368 |
|
---|
1369 | size_t cbWritten = 0;
|
---|
1370 |
|
---|
1371 | size_t cbToWrite = audioBuffer->mAudioDataByteSize;
|
---|
1372 | size_t cbLeft = RT_MIN(cbToWrite, RTCircBufFree(pCircBuf));
|
---|
1373 |
|
---|
1374 | while (cbLeft)
|
---|
1375 | {
|
---|
1376 | /* Try to acquire the necessary block from the ring buffer. */
|
---|
1377 | RTCircBufAcquireWriteBlock(pCircBuf, cbLeft, (void **)&pvDst, &cbToWrite);
|
---|
1378 |
|
---|
1379 | if (!cbToWrite)
|
---|
1380 | break;
|
---|
1381 |
|
---|
1382 | /* Copy the data from our ring buffer to the core audio buffer. */
|
---|
1383 | memcpy((UInt8 *)pvDst, pvSrc + cbWritten, cbToWrite);
|
---|
1384 |
|
---|
1385 | /* Release the read buffer, so it could be used for new data. */
|
---|
1386 | RTCircBufReleaseWriteBlock(pCircBuf, cbToWrite);
|
---|
1387 |
|
---|
1388 | cbWritten += cbToWrite;
|
---|
1389 |
|
---|
1390 | Assert(cbLeft >= cbToWrite);
|
---|
1391 | cbLeft -= cbToWrite;
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | Log3Func(("pCAStream=%p, cbBuffer=%RU32/%zu, cbWritten=%zu\n",
|
---|
1395 | pCAStream, audioBuffer->mAudioDataByteSize, audioBuffer->mAudioDataBytesCapacity, cbWritten));
|
---|
1396 |
|
---|
1397 | return VINF_SUCCESS;
|
---|
1398 | }
|
---|
1399 |
|
---|
1400 | /**
|
---|
1401 | * Input audio queue callback. Called whenever input data from the audio queue becomes available.
|
---|
1402 | *
|
---|
1403 | * @param pvUser User argument.
|
---|
1404 | * @param audioQueue Audio queue to process input data from.
|
---|
1405 | * @param audioBuffer Audio buffer to process input data from. Must be part of audio queue.
|
---|
1406 | * @param pAudioTS Audio timestamp.
|
---|
1407 | * @param cPacketDesc Number of packet descriptors.
|
---|
1408 | * @param paPacketDesc Array of packet descriptors.
|
---|
1409 | */
|
---|
1410 | static DECLCALLBACK(void) coreAudioInputQueueCb(void *pvUser, AudioQueueRef audioQueue, AudioQueueBufferRef audioBuffer,
|
---|
1411 | const AudioTimeStamp *pAudioTS,
|
---|
1412 | UInt32 cPacketDesc, const AudioStreamPacketDescription *paPacketDesc)
|
---|
1413 | {
|
---|
1414 | RT_NOREF(audioQueue, pAudioTS, cPacketDesc, paPacketDesc);
|
---|
1415 |
|
---|
1416 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pvUser;
|
---|
1417 | AssertPtr(pCAStream);
|
---|
1418 |
|
---|
1419 | int rc = RTCritSectEnter(&pCAStream->CritSect);
|
---|
1420 | AssertRC(rc);
|
---|
1421 |
|
---|
1422 | rc = coreAudioInputQueueProcBuffer(pCAStream, audioBuffer);
|
---|
1423 | if (RT_SUCCESS(rc))
|
---|
1424 | AudioQueueEnqueueBuffer(audioQueue, audioBuffer, 0, NULL);
|
---|
1425 |
|
---|
1426 | rc = RTCritSectLeave(&pCAStream->CritSect);
|
---|
1427 | AssertRC(rc);
|
---|
1428 | }
|
---|
1429 |
|
---|
1430 | /**
|
---|
1431 | * Processes output data of a Core Audio stream into an audio queue buffer.
|
---|
1432 | *
|
---|
1433 | * @returns IPRT status code.
|
---|
1434 | * @param pCAStream Core Audio stream to process output data for.
|
---|
1435 | * @param audioBuffer Audio buffer to store data into.
|
---|
1436 | */
|
---|
1437 | int coreAudioOutputQueueProcBuffer(PCOREAUDIOSTREAM pCAStream, AudioQueueBufferRef audioBuffer)
|
---|
1438 | {
|
---|
1439 | AssertPtr(pCAStream);
|
---|
1440 |
|
---|
1441 | PRTCIRCBUF pCircBuf = pCAStream->pCircBuf;
|
---|
1442 | AssertPtr(pCircBuf);
|
---|
1443 |
|
---|
1444 | size_t cbRead = 0;
|
---|
1445 |
|
---|
1446 | UInt8 *pvSrc = NULL;
|
---|
1447 | UInt8 *pvDst = (UInt8 *)audioBuffer->mAudioData;
|
---|
1448 |
|
---|
1449 | size_t cbToRead = RT_MIN(RTCircBufUsed(pCircBuf), audioBuffer->mAudioDataBytesCapacity);
|
---|
1450 | size_t cbLeft = cbToRead;
|
---|
1451 |
|
---|
1452 | while (cbLeft)
|
---|
1453 | {
|
---|
1454 | /* Try to acquire the necessary block from the ring buffer. */
|
---|
1455 | RTCircBufAcquireReadBlock(pCircBuf, cbLeft, (void **)&pvSrc, &cbToRead);
|
---|
1456 |
|
---|
1457 | if (cbToRead)
|
---|
1458 | {
|
---|
1459 | /* Copy the data from our ring buffer to the core audio buffer. */
|
---|
1460 | memcpy((UInt8 *)pvDst + cbRead, pvSrc, cbToRead);
|
---|
1461 | }
|
---|
1462 |
|
---|
1463 | /* Release the read buffer, so it could be used for new data. */
|
---|
1464 | RTCircBufReleaseReadBlock(pCircBuf, cbToRead);
|
---|
1465 |
|
---|
1466 | if (!cbToRead)
|
---|
1467 | break;
|
---|
1468 |
|
---|
1469 | /* Move offset. */
|
---|
1470 | cbRead += cbToRead;
|
---|
1471 | Assert(cbRead <= audioBuffer->mAudioDataBytesCapacity);
|
---|
1472 |
|
---|
1473 | Assert(cbToRead <= cbLeft);
|
---|
1474 | cbLeft -= cbToRead;
|
---|
1475 | }
|
---|
1476 |
|
---|
1477 | audioBuffer->mAudioDataByteSize = cbRead;
|
---|
1478 |
|
---|
1479 | if (audioBuffer->mAudioDataByteSize < audioBuffer->mAudioDataBytesCapacity)
|
---|
1480 | {
|
---|
1481 | RT_BZERO((UInt8 *)audioBuffer->mAudioData + audioBuffer->mAudioDataByteSize,
|
---|
1482 | audioBuffer->mAudioDataBytesCapacity - audioBuffer->mAudioDataByteSize);
|
---|
1483 |
|
---|
1484 | audioBuffer->mAudioDataByteSize = audioBuffer->mAudioDataBytesCapacity;
|
---|
1485 | }
|
---|
1486 |
|
---|
1487 | Log3Func(("pCAStream=%p, cbCapacity=%RU32, cbRead=%zu\n",
|
---|
1488 | pCAStream, audioBuffer->mAudioDataBytesCapacity, cbRead));
|
---|
1489 |
|
---|
1490 | return VINF_SUCCESS;
|
---|
1491 | }
|
---|
1492 |
|
---|
1493 | /**
|
---|
1494 | * Output audio queue callback. Called whenever an audio queue is ready to process more output data.
|
---|
1495 | *
|
---|
1496 | * @param pvUser User argument.
|
---|
1497 | * @param audioQueue Audio queue to process output data for.
|
---|
1498 | * @param audioBuffer Audio buffer to store output data in. Must be part of audio queue.
|
---|
1499 | */
|
---|
1500 | static DECLCALLBACK(void) coreAudioOutputQueueCb(void *pvUser, AudioQueueRef audioQueue, AudioQueueBufferRef audioBuffer)
|
---|
1501 | {
|
---|
1502 | RT_NOREF(audioQueue);
|
---|
1503 |
|
---|
1504 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pvUser;
|
---|
1505 | AssertPtr(pCAStream);
|
---|
1506 |
|
---|
1507 | int rc = RTCritSectEnter(&pCAStream->CritSect);
|
---|
1508 | AssertRC(rc);
|
---|
1509 |
|
---|
1510 | rc = coreAudioOutputQueueProcBuffer(pCAStream, audioBuffer);
|
---|
1511 | if (RT_SUCCESS(rc))
|
---|
1512 | AudioQueueEnqueueBuffer(audioQueue, audioBuffer, 0, NULL);
|
---|
1513 |
|
---|
1514 | rc = RTCritSectLeave(&pCAStream->CritSect);
|
---|
1515 | AssertRC(rc);
|
---|
1516 | }
|
---|
1517 |
|
---|
1518 | /**
|
---|
1519 | * Invalidates a Core Audio stream's audio queue.
|
---|
1520 | *
|
---|
1521 | * @returns IPRT status code.
|
---|
1522 | * @param pCAStream Core Audio stream to invalidate its queue for.
|
---|
1523 | */
|
---|
1524 | static int coreAudioStreamInvalidateQueue(PCOREAUDIOSTREAM pCAStream)
|
---|
1525 | {
|
---|
1526 | int rc = VINF_SUCCESS;
|
---|
1527 |
|
---|
1528 | Log3Func(("pCAStream=%p\n", pCAStream));
|
---|
1529 |
|
---|
1530 | for (size_t i = 0; i < RT_ELEMENTS(pCAStream->audioBuffer); i++)
|
---|
1531 | {
|
---|
1532 | AudioQueueBufferRef pBuf = pCAStream->audioBuffer[i];
|
---|
1533 |
|
---|
1534 | if (pCAStream->pCfg->enmDir == PDMAUDIODIR_IN)
|
---|
1535 | {
|
---|
1536 | int rc2 = coreAudioInputQueueProcBuffer(pCAStream, pBuf);
|
---|
1537 | if (RT_SUCCESS(rc2))
|
---|
1538 | {
|
---|
1539 | AudioQueueEnqueueBuffer(pCAStream->audioQueue, pBuf, 0, NULL);
|
---|
1540 | }
|
---|
1541 | }
|
---|
1542 | else if (pCAStream->pCfg->enmDir == PDMAUDIODIR_OUT)
|
---|
1543 | {
|
---|
1544 | int rc2 = coreAudioOutputQueueProcBuffer(pCAStream, pBuf);
|
---|
1545 | if ( RT_SUCCESS(rc2)
|
---|
1546 | && pBuf->mAudioDataByteSize)
|
---|
1547 | {
|
---|
1548 | AudioQueueEnqueueBuffer(pCAStream->audioQueue, pBuf, 0, NULL);
|
---|
1549 | }
|
---|
1550 |
|
---|
1551 | if (RT_SUCCESS(rc))
|
---|
1552 | rc = rc2;
|
---|
1553 | }
|
---|
1554 | else
|
---|
1555 | AssertFailed();
|
---|
1556 | }
|
---|
1557 |
|
---|
1558 | return rc;
|
---|
1559 | }
|
---|
1560 |
|
---|
1561 | /**
|
---|
1562 | * Initializes a Core Audio stream's audio queue.
|
---|
1563 | *
|
---|
1564 | * @returns IPRT status code.
|
---|
1565 | * @param pCAStream Core Audio stream to initialize audio queue for.
|
---|
1566 | * @param pCfgReq Requested stream configuration.
|
---|
1567 | * @param pCfgAcq Acquired stream configuration on success.
|
---|
1568 | */
|
---|
1569 | static int coreAudioStreamInitQueue(PCOREAUDIOSTREAM pCAStream, PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
1570 | {
|
---|
1571 | RT_NOREF(pCfgAcq);
|
---|
1572 |
|
---|
1573 | LogFunc(("pCAStream=%p, pCfgReq=%p, pCfgAcq=%p\n", pCAStream, pCfgReq, pCfgAcq));
|
---|
1574 |
|
---|
1575 | /* No device assigned? Bail out early. */
|
---|
1576 | if (pCAStream->Unit.pDevice == NULL)
|
---|
1577 | return VERR_NOT_AVAILABLE;
|
---|
1578 |
|
---|
1579 | const bool fIn = pCfgReq->enmDir == PDMAUDIODIR_IN;
|
---|
1580 |
|
---|
1581 | int rc = VINF_SUCCESS;
|
---|
1582 |
|
---|
1583 | if (fIn)
|
---|
1584 | {
|
---|
1585 | rc = coreAudioInputPermissionCheck();
|
---|
1586 | if (RT_FAILURE(rc))
|
---|
1587 | return rc;
|
---|
1588 | }
|
---|
1589 |
|
---|
1590 | /* Create the recording device's out format based on our required audio settings. */
|
---|
1591 | Assert(pCAStream->pCfg == NULL);
|
---|
1592 | pCAStream->pCfg = DrvAudioHlpStreamCfgDup(pCfgReq);
|
---|
1593 | if (!pCAStream->pCfg)
|
---|
1594 | rc = VERR_NO_MEMORY;
|
---|
1595 |
|
---|
1596 | coreAudioPCMPropsToASBD(&pCfgReq->Props, &pCAStream->asbdStream);
|
---|
1597 | /** @todo Do some validation? */
|
---|
1598 |
|
---|
1599 | coreAudioPrintASBD( fIn
|
---|
1600 | ? "Capturing queue format"
|
---|
1601 | : "Playback queue format", &pCAStream->asbdStream);
|
---|
1602 |
|
---|
1603 | if (RT_FAILURE(rc))
|
---|
1604 | {
|
---|
1605 | LogRel(("CoreAudio: Failed to convert requested %s format to native format (%Rrc)\n", fIn ? "input" : "output", rc));
|
---|
1606 | return rc;
|
---|
1607 | }
|
---|
1608 |
|
---|
1609 | rc = RTCircBufCreate(&pCAStream->pCircBuf, PDMAUDIOSTREAMCFG_F2B(pCfgReq, pCfgReq->Backend.cFramesBufferSize));
|
---|
1610 | if (RT_FAILURE(rc))
|
---|
1611 | return rc;
|
---|
1612 |
|
---|
1613 | /*
|
---|
1614 | * Start the thread.
|
---|
1615 | */
|
---|
1616 | rc = RTThreadCreate(&pCAStream->hThread, coreAudioQueueThread,
|
---|
1617 | pCAStream /* pvUser */, 0 /* Default stack size */,
|
---|
1618 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "CAQUEUE");
|
---|
1619 | if (RT_SUCCESS(rc))
|
---|
1620 | rc = RTThreadUserWait(pCAStream->hThread, 10 * 1000 /* 10s timeout */);
|
---|
1621 |
|
---|
1622 | LogFunc(("Returning %Rrc\n", rc));
|
---|
1623 | return rc;
|
---|
1624 | }
|
---|
1625 |
|
---|
1626 | /**
|
---|
1627 | * Unitializes a Core Audio stream's audio queue.
|
---|
1628 | *
|
---|
1629 | * @returns IPRT status code.
|
---|
1630 | * @param pCAStream Core Audio stream to unitialize audio queue for.
|
---|
1631 | */
|
---|
1632 | static int coreAudioStreamUninitQueue(PCOREAUDIOSTREAM pCAStream)
|
---|
1633 | {
|
---|
1634 | LogFunc(("pCAStream=%p\n", pCAStream));
|
---|
1635 |
|
---|
1636 | if (pCAStream->hThread != NIL_RTTHREAD)
|
---|
1637 | {
|
---|
1638 | LogFunc(("Waiting for thread ...\n"));
|
---|
1639 |
|
---|
1640 | ASMAtomicXchgBool(&pCAStream->fShutdown, true);
|
---|
1641 |
|
---|
1642 | int rcThread;
|
---|
1643 | int rc = RTThreadWait(pCAStream->hThread, 30 * 1000, &rcThread);
|
---|
1644 | if (RT_FAILURE(rc))
|
---|
1645 | return rc;
|
---|
1646 |
|
---|
1647 | RT_NOREF(rcThread);
|
---|
1648 | LogFunc(("Thread stopped with %Rrc\n", rcThread));
|
---|
1649 |
|
---|
1650 | pCAStream->hThread = NIL_RTTHREAD;
|
---|
1651 | }
|
---|
1652 |
|
---|
1653 | if (pCAStream->pCfg)
|
---|
1654 | {
|
---|
1655 | DrvAudioHlpStreamCfgFree(pCAStream->pCfg);
|
---|
1656 | pCAStream->pCfg = NULL;
|
---|
1657 | }
|
---|
1658 |
|
---|
1659 | if (pCAStream->pCircBuf)
|
---|
1660 | {
|
---|
1661 | RTCircBufDestroy(pCAStream->pCircBuf);
|
---|
1662 | pCAStream->pCircBuf = NULL;
|
---|
1663 | }
|
---|
1664 |
|
---|
1665 | LogFunc(("Returning\n"));
|
---|
1666 | return VINF_SUCCESS;
|
---|
1667 | }
|
---|
1668 |
|
---|
1669 | /**
|
---|
1670 | * Unitializes a Core Audio stream.
|
---|
1671 | *
|
---|
1672 | * @returns IPRT status code.
|
---|
1673 | * @param pCAStream Core Audio stream to uninitialize.
|
---|
1674 | */
|
---|
1675 | static int coreAudioStreamUninit(PCOREAUDIOSTREAM pCAStream)
|
---|
1676 | {
|
---|
1677 | LogFunc(("pCAStream=%p\n", pCAStream));
|
---|
1678 |
|
---|
1679 | int rc = coreAudioStreamUninitQueue(pCAStream);
|
---|
1680 | if (RT_SUCCESS(rc))
|
---|
1681 | {
|
---|
1682 | pCAStream->Unit.pDevice = NULL;
|
---|
1683 | pCAStream->pDrv = NULL;
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | return rc;
|
---|
1687 | }
|
---|
1688 |
|
---|
1689 | /**
|
---|
1690 | * Registers callbacks for a specific Core Audio device.
|
---|
1691 | *
|
---|
1692 | * @return IPRT status code.
|
---|
1693 | * @param pThis Host audio driver instance.
|
---|
1694 | * @param pDev Audio device to use for the registered callbacks.
|
---|
1695 | */
|
---|
1696 | static int coreAudioDeviceRegisterCallbacks(PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICE pDev)
|
---|
1697 | {
|
---|
1698 | RT_NOREF(pThis);
|
---|
1699 |
|
---|
1700 | AudioDeviceID deviceID = kAudioDeviceUnknown;
|
---|
1701 |
|
---|
1702 | PCOREAUDIODEVICEDATA pData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
1703 | if (pData)
|
---|
1704 | deviceID = pData->deviceID;
|
---|
1705 |
|
---|
1706 | if (deviceID != kAudioDeviceUnknown)
|
---|
1707 | {
|
---|
1708 | LogFunc(("deviceID=%RU32\n", deviceID));
|
---|
1709 |
|
---|
1710 | /*
|
---|
1711 | * Register device callbacks.
|
---|
1712 | */
|
---|
1713 | AudioObjectPropertyAddress propAdr = { kAudioDevicePropertyDeviceIsAlive, kAudioObjectPropertyScopeGlobal,
|
---|
1714 | kAudioObjectPropertyElementMaster };
|
---|
1715 | OSStatus err = AudioObjectAddPropertyListener(deviceID, &propAdr,
|
---|
1716 | coreAudioDeviceStateChangedCb, pDev /* pvUser */);
|
---|
1717 | if ( err != noErr
|
---|
1718 | && err != kAudioHardwareIllegalOperationError)
|
---|
1719 | {
|
---|
1720 | LogRel(("CoreAudio: Failed to add the recording device state changed listener (%RI32)\n", err));
|
---|
1721 | }
|
---|
1722 |
|
---|
1723 | propAdr.mSelector = kAudioDeviceProcessorOverload;
|
---|
1724 | propAdr.mScope = kAudioUnitScope_Global;
|
---|
1725 | err = AudioObjectAddPropertyListener(deviceID, &propAdr,
|
---|
1726 | coreAudioDevPropChgCb, pDev /* pvUser */);
|
---|
1727 | if (err != noErr)
|
---|
1728 | LogRel(("CoreAudio: Failed to register processor overload listener (%RI32)\n", err));
|
---|
1729 |
|
---|
1730 | propAdr.mSelector = kAudioDevicePropertyNominalSampleRate;
|
---|
1731 | propAdr.mScope = kAudioUnitScope_Global;
|
---|
1732 | err = AudioObjectAddPropertyListener(deviceID, &propAdr,
|
---|
1733 | coreAudioDevPropChgCb, pDev /* pvUser */);
|
---|
1734 | if (err != noErr)
|
---|
1735 | LogRel(("CoreAudio: Failed to register sample rate changed listener (%RI32)\n", err));
|
---|
1736 | }
|
---|
1737 |
|
---|
1738 | return VINF_SUCCESS;
|
---|
1739 | }
|
---|
1740 |
|
---|
1741 | /**
|
---|
1742 | * Unregisters all formerly registered callbacks of a Core Audio device again.
|
---|
1743 | *
|
---|
1744 | * @return IPRT status code.
|
---|
1745 | * @param pThis Host audio driver instance.
|
---|
1746 | * @param pDev Audio device to use for the registered callbacks.
|
---|
1747 | */
|
---|
1748 | static int coreAudioDeviceUnregisterCallbacks(PDRVHOSTCOREAUDIO pThis, PPDMAUDIODEVICE pDev)
|
---|
1749 | {
|
---|
1750 | RT_NOREF(pThis);
|
---|
1751 |
|
---|
1752 | AudioDeviceID deviceID = kAudioDeviceUnknown;
|
---|
1753 |
|
---|
1754 | if (pDev)
|
---|
1755 | {
|
---|
1756 | PCOREAUDIODEVICEDATA pData = (PCOREAUDIODEVICEDATA)pDev->pvData;
|
---|
1757 | if (pData)
|
---|
1758 | deviceID = pData->deviceID;
|
---|
1759 | }
|
---|
1760 |
|
---|
1761 | if (deviceID != kAudioDeviceUnknown)
|
---|
1762 | {
|
---|
1763 | LogFunc(("deviceID=%RU32\n", deviceID));
|
---|
1764 |
|
---|
1765 | /*
|
---|
1766 | * Unregister per-device callbacks.
|
---|
1767 | */
|
---|
1768 | AudioObjectPropertyAddress propAdr = { kAudioDeviceProcessorOverload, kAudioObjectPropertyScopeGlobal,
|
---|
1769 | kAudioObjectPropertyElementMaster };
|
---|
1770 | OSStatus err = AudioObjectRemovePropertyListener(deviceID, &propAdr,
|
---|
1771 | coreAudioDevPropChgCb, pDev /* pvUser */);
|
---|
1772 | if ( err != noErr
|
---|
1773 | && err != kAudioHardwareBadObjectError)
|
---|
1774 | {
|
---|
1775 | LogRel(("CoreAudio: Failed to remove the recording processor overload listener (%RI32)\n", err));
|
---|
1776 | }
|
---|
1777 |
|
---|
1778 | propAdr.mSelector = kAudioDevicePropertyNominalSampleRate;
|
---|
1779 | err = AudioObjectRemovePropertyListener(deviceID, &propAdr,
|
---|
1780 | coreAudioDevPropChgCb, pDev /* pvUser */);
|
---|
1781 | if ( err != noErr
|
---|
1782 | && err != kAudioHardwareBadObjectError)
|
---|
1783 | {
|
---|
1784 | LogRel(("CoreAudio: Failed to remove the sample rate changed listener (%RI32)\n", err));
|
---|
1785 | }
|
---|
1786 |
|
---|
1787 | propAdr.mSelector = kAudioDevicePropertyDeviceIsAlive;
|
---|
1788 | err = AudioObjectRemovePropertyListener(deviceID, &propAdr,
|
---|
1789 | coreAudioDeviceStateChangedCb, pDev /* pvUser */);
|
---|
1790 | if ( err != noErr
|
---|
1791 | && err != kAudioHardwareBadObjectError)
|
---|
1792 | {
|
---|
1793 | LogRel(("CoreAudio: Failed to remove the device alive listener (%RI32)\n", err));
|
---|
1794 | }
|
---|
1795 | }
|
---|
1796 |
|
---|
1797 | return VINF_SUCCESS;
|
---|
1798 | }
|
---|
1799 |
|
---|
1800 | /* Callback for getting notified when some of the properties of an audio device have changed. */
|
---|
1801 | static DECLCALLBACK(OSStatus) coreAudioDevPropChgCb(AudioObjectID propertyID,
|
---|
1802 | UInt32 cAddresses,
|
---|
1803 | const AudioObjectPropertyAddress properties[],
|
---|
1804 | void *pvUser)
|
---|
1805 | {
|
---|
1806 | RT_NOREF(cAddresses, properties, pvUser);
|
---|
1807 |
|
---|
1808 | PPDMAUDIODEVICE pDev = (PPDMAUDIODEVICE)pvUser;
|
---|
1809 | AssertPtr(pDev);
|
---|
1810 |
|
---|
1811 | LogFlowFunc(("propertyID=%u, nAddresses=%u, pDev=%p\n", propertyID, cAddresses, pDev));
|
---|
1812 |
|
---|
1813 | switch (propertyID)
|
---|
1814 | {
|
---|
1815 | #ifdef DEBUG
|
---|
1816 | case kAudioDeviceProcessorOverload:
|
---|
1817 | {
|
---|
1818 | LogFunc(("Processor overload detected!\n"));
|
---|
1819 | break;
|
---|
1820 | }
|
---|
1821 | #endif /* DEBUG */
|
---|
1822 | case kAudioDevicePropertyNominalSampleRate:
|
---|
1823 | {
|
---|
1824 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
1825 | int rc2 = coreAudioDevicePropagateStatus(pDev, COREAUDIOSTATUS_REINIT);
|
---|
1826 | AssertRC(rc2);
|
---|
1827 | #else
|
---|
1828 | RT_NOREF(pDev);
|
---|
1829 | #endif
|
---|
1830 | break;
|
---|
1831 | }
|
---|
1832 |
|
---|
1833 | default:
|
---|
1834 | /* Just skip. */
|
---|
1835 | break;
|
---|
1836 | }
|
---|
1837 |
|
---|
1838 | return noErr;
|
---|
1839 | }
|
---|
1840 |
|
---|
1841 | /**
|
---|
1842 | * Enumerates all available host audio devices internally.
|
---|
1843 | *
|
---|
1844 | * @returns IPRT status code.
|
---|
1845 | * @param pThis Host audio driver instance.
|
---|
1846 | */
|
---|
1847 | static int coreAudioEnumerateDevices(PDRVHOSTCOREAUDIO pThis)
|
---|
1848 | {
|
---|
1849 | LogFlowFuncEnter();
|
---|
1850 |
|
---|
1851 | /*
|
---|
1852 | * Unregister old default devices, if any.
|
---|
1853 | */
|
---|
1854 | if (pThis->pDefaultDevIn)
|
---|
1855 | {
|
---|
1856 | coreAudioDeviceUnregisterCallbacks(pThis, pThis->pDefaultDevIn);
|
---|
1857 | pThis->pDefaultDevIn = NULL;
|
---|
1858 | }
|
---|
1859 |
|
---|
1860 | if (pThis->pDefaultDevOut)
|
---|
1861 | {
|
---|
1862 | coreAudioDeviceUnregisterCallbacks(pThis, pThis->pDefaultDevOut);
|
---|
1863 | pThis->pDefaultDevOut = NULL;
|
---|
1864 | }
|
---|
1865 |
|
---|
1866 | /* Remove old / stale device entries. */
|
---|
1867 | DrvAudioHlpDeviceEnumFree(&pThis->Devices);
|
---|
1868 |
|
---|
1869 | /* Enumerate all devices internally. */
|
---|
1870 | int rc = coreAudioDevicesEnumerateAll(pThis, &pThis->Devices);
|
---|
1871 | if (RT_SUCCESS(rc))
|
---|
1872 | {
|
---|
1873 | /*
|
---|
1874 | * Default input device.
|
---|
1875 | */
|
---|
1876 | pThis->pDefaultDevIn = DrvAudioHlpDeviceEnumGetDefaultDevice(&pThis->Devices, PDMAUDIODIR_IN);
|
---|
1877 | if (pThis->pDefaultDevIn)
|
---|
1878 | {
|
---|
1879 | LogRel2(("CoreAudio: Default capturing device is '%s'\n", pThis->pDefaultDevIn->szName));
|
---|
1880 |
|
---|
1881 | #ifdef DEBUG
|
---|
1882 | PCOREAUDIODEVICEDATA pDevData = (PCOREAUDIODEVICEDATA)pThis->pDefaultDevIn->pvData;
|
---|
1883 | AssertPtr(pDevData);
|
---|
1884 | LogFunc(("pDefaultDevIn=%p, ID=%RU32\n", pThis->pDefaultDevIn, pDevData->deviceID));
|
---|
1885 | #endif
|
---|
1886 | rc = coreAudioDeviceRegisterCallbacks(pThis, pThis->pDefaultDevIn);
|
---|
1887 | }
|
---|
1888 | else
|
---|
1889 | LogRel2(("CoreAudio: No default capturing device found\n"));
|
---|
1890 |
|
---|
1891 | /*
|
---|
1892 | * Default output device.
|
---|
1893 | */
|
---|
1894 | pThis->pDefaultDevOut = DrvAudioHlpDeviceEnumGetDefaultDevice(&pThis->Devices, PDMAUDIODIR_OUT);
|
---|
1895 | if (pThis->pDefaultDevOut)
|
---|
1896 | {
|
---|
1897 | LogRel2(("CoreAudio: Default playback device is '%s'\n", pThis->pDefaultDevOut->szName));
|
---|
1898 |
|
---|
1899 | #ifdef DEBUG
|
---|
1900 | PCOREAUDIODEVICEDATA pDevData = (PCOREAUDIODEVICEDATA)pThis->pDefaultDevOut->pvData;
|
---|
1901 | AssertPtr(pDevData);
|
---|
1902 | LogFunc(("pDefaultDevOut=%p, ID=%RU32\n", pThis->pDefaultDevOut, pDevData->deviceID));
|
---|
1903 | #endif
|
---|
1904 | rc = coreAudioDeviceRegisterCallbacks(pThis, pThis->pDefaultDevOut);
|
---|
1905 | }
|
---|
1906 | else
|
---|
1907 | LogRel2(("CoreAudio: No default playback device found\n"));
|
---|
1908 | }
|
---|
1909 |
|
---|
1910 | LogFunc(("Returning %Rrc\n", rc));
|
---|
1911 | return rc;
|
---|
1912 | }
|
---|
1913 |
|
---|
1914 | /**
|
---|
1915 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
|
---|
1916 | */
|
---|
1917 | static DECLCALLBACK(int) drvHostCoreAudioHA_StreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
1918 | void *pvBuf, uint32_t uBufSize, uint32_t *puRead)
|
---|
1919 | {
|
---|
1920 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
1921 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
1922 | /* puRead is optional. */
|
---|
1923 |
|
---|
1924 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
1925 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
1926 |
|
---|
1927 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
1928 | /* Check if the audio device should be reinitialized. If so do it. */
|
---|
1929 | if (ASMAtomicReadU32(&pCAStream->enmStatus) == COREAUDIOSTATUS_REINIT)
|
---|
1930 | {
|
---|
1931 | /* For now re just re-initialize with the current input device. */
|
---|
1932 | if (pThis->pDefaultDevIn)
|
---|
1933 | {
|
---|
1934 | int rc2 = coreAudioStreamReinit(pThis, pCAStream, pThis->pDefaultDevIn);
|
---|
1935 | if (RT_FAILURE(rc2))
|
---|
1936 | return VERR_NOT_AVAILABLE;
|
---|
1937 | }
|
---|
1938 | else
|
---|
1939 | return VERR_NOT_AVAILABLE;
|
---|
1940 | }
|
---|
1941 | #else
|
---|
1942 | RT_NOREF(pThis);
|
---|
1943 | #endif
|
---|
1944 |
|
---|
1945 | if (ASMAtomicReadU32(&pCAStream->enmStatus) != COREAUDIOSTATUS_INIT)
|
---|
1946 | {
|
---|
1947 | if (puRead)
|
---|
1948 | *puRead = 0;
|
---|
1949 | return VINF_SUCCESS;
|
---|
1950 | }
|
---|
1951 |
|
---|
1952 | int rc = VINF_SUCCESS;
|
---|
1953 |
|
---|
1954 | uint32_t cbReadTotal = 0;
|
---|
1955 |
|
---|
1956 | rc = RTCritSectEnter(&pCAStream->CritSect);
|
---|
1957 | AssertRC(rc);
|
---|
1958 |
|
---|
1959 | do
|
---|
1960 | {
|
---|
1961 | size_t cbToWrite = RT_MIN(uBufSize, RTCircBufUsed(pCAStream->pCircBuf));
|
---|
1962 |
|
---|
1963 | uint8_t *pvChunk;
|
---|
1964 | size_t cbChunk;
|
---|
1965 |
|
---|
1966 | Log3Func(("cbToWrite=%zu/%zu\n", cbToWrite, RTCircBufSize(pCAStream->pCircBuf)));
|
---|
1967 |
|
---|
1968 | while (cbToWrite)
|
---|
1969 | {
|
---|
1970 | /* Try to acquire the necessary block from the ring buffer. */
|
---|
1971 | RTCircBufAcquireReadBlock(pCAStream->pCircBuf, cbToWrite, (void **)&pvChunk, &cbChunk);
|
---|
1972 | if (cbChunk)
|
---|
1973 | memcpy((uint8_t *)pvBuf + cbReadTotal, pvChunk, cbChunk);
|
---|
1974 |
|
---|
1975 | /* Release the read buffer, so it could be used for new data. */
|
---|
1976 | RTCircBufReleaseReadBlock(pCAStream->pCircBuf, cbChunk);
|
---|
1977 |
|
---|
1978 | if (RT_FAILURE(rc))
|
---|
1979 | break;
|
---|
1980 |
|
---|
1981 | Assert(cbToWrite >= cbChunk);
|
---|
1982 | cbToWrite -= cbChunk;
|
---|
1983 |
|
---|
1984 | cbReadTotal += cbChunk;
|
---|
1985 | }
|
---|
1986 | }
|
---|
1987 | while (0);
|
---|
1988 |
|
---|
1989 | int rc2 = RTCritSectLeave(&pCAStream->CritSect);
|
---|
1990 | AssertRC(rc2);
|
---|
1991 |
|
---|
1992 | if (RT_SUCCESS(rc))
|
---|
1993 | {
|
---|
1994 | if (puRead)
|
---|
1995 | *puRead = cbReadTotal;
|
---|
1996 | }
|
---|
1997 |
|
---|
1998 | return rc;
|
---|
1999 | }
|
---|
2000 |
|
---|
2001 | /**
|
---|
2002 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
|
---|
2003 | */
|
---|
2004 | static DECLCALLBACK(int) drvHostCoreAudioHA_StreamPlay(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2005 | const void *pvBuf, uint32_t uBufSize, uint32_t *puWritten)
|
---|
2006 | {
|
---|
2007 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2008 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2009 |
|
---|
2010 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2011 | /* Check if the audio device should be reinitialized. If so do it. */
|
---|
2012 | if (ASMAtomicReadU32(&pCAStream->enmStatus) == COREAUDIOSTATUS_REINIT)
|
---|
2013 | {
|
---|
2014 | if (pThis->pDefaultDevOut)
|
---|
2015 | {
|
---|
2016 | /* For now re just re-initialize with the current output device. */
|
---|
2017 | int rc2 = coreAudioStreamReinit(pThis, pCAStream, pThis->pDefaultDevOut);
|
---|
2018 | if (RT_FAILURE(rc2))
|
---|
2019 | return VERR_NOT_AVAILABLE;
|
---|
2020 | }
|
---|
2021 | else
|
---|
2022 | return VERR_NOT_AVAILABLE;
|
---|
2023 | }
|
---|
2024 | #else
|
---|
2025 | RT_NOREF(pThis);
|
---|
2026 | #endif
|
---|
2027 |
|
---|
2028 | if (ASMAtomicReadU32(&pCAStream->enmStatus) != COREAUDIOSTATUS_INIT)
|
---|
2029 | {
|
---|
2030 | if (puWritten)
|
---|
2031 | *puWritten = 0;
|
---|
2032 | return VINF_SUCCESS;
|
---|
2033 | }
|
---|
2034 |
|
---|
2035 | uint32_t cbWrittenTotal = 0;
|
---|
2036 |
|
---|
2037 | int rc = VINF_SUCCESS;
|
---|
2038 |
|
---|
2039 | rc = RTCritSectEnter(&pCAStream->CritSect);
|
---|
2040 | AssertRC(rc);
|
---|
2041 |
|
---|
2042 | size_t cbToWrite = RT_MIN(uBufSize, RTCircBufFree(pCAStream->pCircBuf));
|
---|
2043 | Log3Func(("cbToWrite=%zu\n", cbToWrite));
|
---|
2044 |
|
---|
2045 | uint8_t *pvChunk;
|
---|
2046 | size_t cbChunk;
|
---|
2047 |
|
---|
2048 | while (cbToWrite)
|
---|
2049 | {
|
---|
2050 | /* Try to acquire the necessary space from the ring buffer. */
|
---|
2051 | RTCircBufAcquireWriteBlock(pCAStream->pCircBuf, cbToWrite, (void **)&pvChunk, &cbChunk);
|
---|
2052 | if (!cbChunk)
|
---|
2053 | {
|
---|
2054 | RTCircBufReleaseWriteBlock(pCAStream->pCircBuf, cbChunk);
|
---|
2055 | break;
|
---|
2056 | }
|
---|
2057 |
|
---|
2058 | Assert(cbChunk <= cbToWrite);
|
---|
2059 | Assert(cbWrittenTotal + cbChunk <= uBufSize);
|
---|
2060 |
|
---|
2061 | memcpy(pvChunk, (uint8_t *)pvBuf + cbWrittenTotal, cbChunk);
|
---|
2062 |
|
---|
2063 | #ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
|
---|
2064 | RTFILE fh;
|
---|
2065 | rc = RTFileOpen(&fh,VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "caPlayback.pcm",
|
---|
2066 | RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
|
---|
2067 | if (RT_SUCCESS(rc))
|
---|
2068 | {
|
---|
2069 | RTFileWrite(fh, pvChunk, cbChunk, NULL);
|
---|
2070 | RTFileClose(fh);
|
---|
2071 | }
|
---|
2072 | else
|
---|
2073 | AssertFailed();
|
---|
2074 | #endif
|
---|
2075 |
|
---|
2076 | /* Release the ring buffer, so the read thread could start reading this data. */
|
---|
2077 | RTCircBufReleaseWriteBlock(pCAStream->pCircBuf, cbChunk);
|
---|
2078 |
|
---|
2079 | if (RT_FAILURE(rc))
|
---|
2080 | break;
|
---|
2081 |
|
---|
2082 | Assert(cbToWrite >= cbChunk);
|
---|
2083 | cbToWrite -= cbChunk;
|
---|
2084 |
|
---|
2085 | cbWrittenTotal += cbChunk;
|
---|
2086 | }
|
---|
2087 |
|
---|
2088 | if ( RT_SUCCESS(rc)
|
---|
2089 | && pCAStream->fRun
|
---|
2090 | && !pCAStream->fIsRunning)
|
---|
2091 | {
|
---|
2092 | rc = coreAudioStreamInvalidateQueue(pCAStream);
|
---|
2093 | if (RT_SUCCESS(rc))
|
---|
2094 | {
|
---|
2095 | AudioQueueStart(pCAStream->audioQueue, NULL);
|
---|
2096 | pCAStream->fRun = false;
|
---|
2097 | pCAStream->fIsRunning = true;
|
---|
2098 | }
|
---|
2099 | }
|
---|
2100 |
|
---|
2101 | int rc2 = RTCritSectLeave(&pCAStream->CritSect);
|
---|
2102 | AssertRC(rc2);
|
---|
2103 |
|
---|
2104 | if (RT_SUCCESS(rc))
|
---|
2105 | {
|
---|
2106 | if (puWritten)
|
---|
2107 | *puWritten = cbWrittenTotal;
|
---|
2108 | }
|
---|
2109 |
|
---|
2110 | return rc;
|
---|
2111 | }
|
---|
2112 |
|
---|
2113 | static int coreAudioStreamControl(PDRVHOSTCOREAUDIO pThis, PCOREAUDIOSTREAM pCAStream, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2114 | {
|
---|
2115 | RT_NOREF(pThis);
|
---|
2116 |
|
---|
2117 | uint32_t enmStatus = ASMAtomicReadU32(&pCAStream->enmStatus);
|
---|
2118 |
|
---|
2119 | LogFlowFunc(("enmStreamCmd=%RU32, enmStatus=%RU32\n", enmStreamCmd, enmStatus));
|
---|
2120 |
|
---|
2121 | if (!( enmStatus == COREAUDIOSTATUS_INIT
|
---|
2122 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2123 | || enmStatus == COREAUDIOSTATUS_REINIT
|
---|
2124 | #endif
|
---|
2125 | ))
|
---|
2126 | {
|
---|
2127 | return VINF_SUCCESS;
|
---|
2128 | }
|
---|
2129 |
|
---|
2130 | if (!pCAStream->pCfg) /* Not (yet) configured? Skip. */
|
---|
2131 | return VINF_SUCCESS;
|
---|
2132 |
|
---|
2133 | int rc = VINF_SUCCESS;
|
---|
2134 |
|
---|
2135 | switch (enmStreamCmd)
|
---|
2136 | {
|
---|
2137 | case PDMAUDIOSTREAMCMD_ENABLE:
|
---|
2138 | case PDMAUDIOSTREAMCMD_RESUME:
|
---|
2139 | {
|
---|
2140 | LogFunc(("Queue enable\n"));
|
---|
2141 | if (pCAStream->pCfg->enmDir == PDMAUDIODIR_IN)
|
---|
2142 | {
|
---|
2143 | rc = coreAudioStreamInvalidateQueue(pCAStream);
|
---|
2144 | if (RT_SUCCESS(rc))
|
---|
2145 | {
|
---|
2146 | /* Start the audio queue immediately. */
|
---|
2147 | AudioQueueStart(pCAStream->audioQueue, NULL);
|
---|
2148 | }
|
---|
2149 | }
|
---|
2150 | else if (pCAStream->pCfg->enmDir == PDMAUDIODIR_OUT)
|
---|
2151 | {
|
---|
2152 | /* Touch the run flag to start the audio queue as soon as
|
---|
2153 | * we have anough data to actually play something. */
|
---|
2154 | ASMAtomicXchgBool(&pCAStream->fRun, true);
|
---|
2155 | }
|
---|
2156 | break;
|
---|
2157 | }
|
---|
2158 |
|
---|
2159 | case PDMAUDIOSTREAMCMD_DISABLE:
|
---|
2160 | {
|
---|
2161 | LogFunc(("Queue disable\n"));
|
---|
2162 | AudioQueueStop(pCAStream->audioQueue, 1 /* Immediately */);
|
---|
2163 | ASMAtomicXchgBool(&pCAStream->fRun, false);
|
---|
2164 | ASMAtomicXchgBool(&pCAStream->fIsRunning, false);
|
---|
2165 | break;
|
---|
2166 | }
|
---|
2167 | case PDMAUDIOSTREAMCMD_PAUSE:
|
---|
2168 | {
|
---|
2169 | LogFunc(("Queue pause\n"));
|
---|
2170 | AudioQueuePause(pCAStream->audioQueue);
|
---|
2171 | ASMAtomicXchgBool(&pCAStream->fIsRunning, false);
|
---|
2172 | break;
|
---|
2173 | }
|
---|
2174 |
|
---|
2175 | default:
|
---|
2176 | rc = VERR_NOT_SUPPORTED;
|
---|
2177 | break;
|
---|
2178 | }
|
---|
2179 |
|
---|
2180 | LogFlowFuncLeaveRC(rc);
|
---|
2181 | return rc;
|
---|
2182 | }
|
---|
2183 |
|
---|
2184 |
|
---|
2185 | /**
|
---|
2186 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
|
---|
2187 | */
|
---|
2188 | static DECLCALLBACK(int) drvHostCoreAudioHA_GetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
|
---|
2189 | {
|
---|
2190 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2191 | AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
|
---|
2192 |
|
---|
2193 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2194 |
|
---|
2195 | RT_BZERO(pBackendCfg, sizeof(PDMAUDIOBACKENDCFG));
|
---|
2196 |
|
---|
2197 | RTStrPrintf2(pBackendCfg->szName, sizeof(pBackendCfg->szName), "Core Audio");
|
---|
2198 |
|
---|
2199 | pBackendCfg->cbStreamIn = sizeof(COREAUDIOSTREAM);
|
---|
2200 | pBackendCfg->cbStreamOut = sizeof(COREAUDIOSTREAM);
|
---|
2201 |
|
---|
2202 | /* For Core Audio we provide one stream per device for now. */
|
---|
2203 | pBackendCfg->cMaxStreamsIn = DrvAudioHlpDeviceEnumGetDeviceCount(&pThis->Devices, PDMAUDIODIR_IN);
|
---|
2204 | pBackendCfg->cMaxStreamsOut = DrvAudioHlpDeviceEnumGetDeviceCount(&pThis->Devices, PDMAUDIODIR_OUT);
|
---|
2205 |
|
---|
2206 | LogFlowFunc(("Returning %Rrc\n", VINF_SUCCESS));
|
---|
2207 | return VINF_SUCCESS;
|
---|
2208 | }
|
---|
2209 |
|
---|
2210 |
|
---|
2211 | /**
|
---|
2212 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetDevices}
|
---|
2213 | */
|
---|
2214 | static DECLCALLBACK(int) drvHostCoreAudioHA_GetDevices(PPDMIHOSTAUDIO pInterface, PPDMAUDIODEVICEENUM pDeviceEnum)
|
---|
2215 | {
|
---|
2216 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2217 | AssertPtrReturn(pDeviceEnum, VERR_INVALID_POINTER);
|
---|
2218 |
|
---|
2219 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2220 |
|
---|
2221 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
2222 | if (RT_SUCCESS(rc))
|
---|
2223 | {
|
---|
2224 | rc = coreAudioEnumerateDevices(pThis);
|
---|
2225 | if (RT_SUCCESS(rc))
|
---|
2226 | {
|
---|
2227 | if (pDeviceEnum)
|
---|
2228 | {
|
---|
2229 | rc = DrvAudioHlpDeviceEnumInit(pDeviceEnum);
|
---|
2230 | if (RT_SUCCESS(rc))
|
---|
2231 | rc = DrvAudioHlpDeviceEnumCopy(pDeviceEnum, &pThis->Devices);
|
---|
2232 |
|
---|
2233 | if (RT_FAILURE(rc))
|
---|
2234 | DrvAudioHlpDeviceEnumFree(pDeviceEnum);
|
---|
2235 | }
|
---|
2236 | }
|
---|
2237 |
|
---|
2238 | int rc2 = RTCritSectLeave(&pThis->CritSect);
|
---|
2239 | AssertRC(rc2);
|
---|
2240 | }
|
---|
2241 |
|
---|
2242 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
2243 | return rc;
|
---|
2244 | }
|
---|
2245 |
|
---|
2246 |
|
---|
2247 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2248 | /**
|
---|
2249 | * @interface_method_impl{PDMIHOSTAUDIO,pfnSetCallback}
|
---|
2250 | */
|
---|
2251 | static DECLCALLBACK(int) drvHostCoreAudioHA_SetCallback(PPDMIHOSTAUDIO pInterface, PFNPDMHOSTAUDIOCALLBACK pfnCallback)
|
---|
2252 | {
|
---|
2253 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2254 | /* pfnCallback will be handled below. */
|
---|
2255 |
|
---|
2256 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2257 |
|
---|
2258 | int rc = RTCritSectEnter(&pThis->CritSect);
|
---|
2259 | if (RT_SUCCESS(rc))
|
---|
2260 | {
|
---|
2261 | LogFunc(("pfnCallback=%p\n", pfnCallback));
|
---|
2262 |
|
---|
2263 | if (pfnCallback) /* Register. */
|
---|
2264 | {
|
---|
2265 | Assert(pThis->pfnCallback == NULL);
|
---|
2266 | pThis->pfnCallback = pfnCallback;
|
---|
2267 | }
|
---|
2268 | else /* Unregister. */
|
---|
2269 | {
|
---|
2270 | if (pThis->pfnCallback)
|
---|
2271 | pThis->pfnCallback = NULL;
|
---|
2272 | }
|
---|
2273 |
|
---|
2274 | int rc2 = RTCritSectLeave(&pThis->CritSect);
|
---|
2275 | AssertRC(rc2);
|
---|
2276 | }
|
---|
2277 |
|
---|
2278 | return rc;
|
---|
2279 | }
|
---|
2280 | #endif
|
---|
2281 |
|
---|
2282 |
|
---|
2283 | /**
|
---|
2284 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
|
---|
2285 | */
|
---|
2286 | static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostCoreAudioHA_GetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
|
---|
2287 | {
|
---|
2288 | RT_NOREF(pInterface, enmDir);
|
---|
2289 | AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
|
---|
2290 |
|
---|
2291 | return PDMAUDIOBACKENDSTS_RUNNING;
|
---|
2292 | }
|
---|
2293 |
|
---|
2294 |
|
---|
2295 | /**
|
---|
2296 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
|
---|
2297 | */
|
---|
2298 | static DECLCALLBACK(int) drvHostCoreAudioHA_StreamCreate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
2299 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
2300 | {
|
---|
2301 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2302 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2303 | AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
|
---|
2304 | AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
|
---|
2305 |
|
---|
2306 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2307 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2308 |
|
---|
2309 | int rc = RTCritSectInit(&pCAStream->CritSect);
|
---|
2310 | if (RT_FAILURE(rc))
|
---|
2311 | return rc;
|
---|
2312 |
|
---|
2313 | pCAStream->hThread = NIL_RTTHREAD;
|
---|
2314 | pCAStream->fRun = false;
|
---|
2315 | pCAStream->fIsRunning = false;
|
---|
2316 | pCAStream->fShutdown = false;
|
---|
2317 |
|
---|
2318 | /* Input or output device? */
|
---|
2319 | bool fIn = pCfgReq->enmDir == PDMAUDIODIR_IN;
|
---|
2320 |
|
---|
2321 | /* For now, just use the default device available. */
|
---|
2322 | PPDMAUDIODEVICE pDev = fIn ? pThis->pDefaultDevIn : pThis->pDefaultDevOut;
|
---|
2323 |
|
---|
2324 | LogFunc(("pStream=%p, pCfgReq=%p, pCfgAcq=%p, fIn=%RTbool, pDev=%p\n", pStream, pCfgReq, pCfgAcq, fIn, pDev));
|
---|
2325 |
|
---|
2326 | if (pDev) /* (Default) device available? */
|
---|
2327 | {
|
---|
2328 | /* Sanity. */
|
---|
2329 | AssertPtr(pDev->pvData);
|
---|
2330 | Assert(pDev->cbData);
|
---|
2331 |
|
---|
2332 | /* Init the Core Audio stream. */
|
---|
2333 | rc = coreAudioStreamInit(pCAStream, pThis, pDev);
|
---|
2334 | if (RT_SUCCESS(rc))
|
---|
2335 | {
|
---|
2336 | ASMAtomicXchgU32(&pCAStream->enmStatus, COREAUDIOSTATUS_IN_INIT);
|
---|
2337 |
|
---|
2338 | rc = coreAudioStreamInitQueue(pCAStream, pCfgReq, pCfgAcq);
|
---|
2339 | if (RT_SUCCESS(rc))
|
---|
2340 | {
|
---|
2341 | ASMAtomicXchgU32(&pCAStream->enmStatus, COREAUDIOSTATUS_INIT);
|
---|
2342 | }
|
---|
2343 | else
|
---|
2344 | {
|
---|
2345 | ASMAtomicXchgU32(&pCAStream->enmStatus, COREAUDIOSTATUS_IN_UNINIT);
|
---|
2346 |
|
---|
2347 | int rc2 = coreAudioStreamUninit(pCAStream);
|
---|
2348 | AssertRC(rc2);
|
---|
2349 |
|
---|
2350 | ASMAtomicXchgU32(&pCAStream->enmStatus, COREAUDIOSTATUS_UNINIT);
|
---|
2351 | }
|
---|
2352 | }
|
---|
2353 | }
|
---|
2354 | else
|
---|
2355 | rc = VERR_AUDIO_STREAM_COULD_NOT_CREATE;
|
---|
2356 |
|
---|
2357 | LogFunc(("Returning %Rrc\n", rc));
|
---|
2358 | return rc;
|
---|
2359 | }
|
---|
2360 |
|
---|
2361 |
|
---|
2362 | /**
|
---|
2363 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
|
---|
2364 | */
|
---|
2365 | static DECLCALLBACK(int) drvHostCoreAudioHA_StreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2366 | {
|
---|
2367 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2368 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2369 |
|
---|
2370 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2371 |
|
---|
2372 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2373 |
|
---|
2374 | uint32_t status = ASMAtomicReadU32(&pCAStream->enmStatus);
|
---|
2375 | if (!( status == COREAUDIOSTATUS_INIT
|
---|
2376 | #ifndef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2377 | || status == COREAUDIOSTATUS_REINIT
|
---|
2378 | #endif
|
---|
2379 | ))
|
---|
2380 | {
|
---|
2381 | return VINF_SUCCESS;
|
---|
2382 | }
|
---|
2383 |
|
---|
2384 | if (!pCAStream->pCfg) /* Not (yet) configured? Skip. */
|
---|
2385 | return VINF_SUCCESS;
|
---|
2386 |
|
---|
2387 | int rc = coreAudioStreamControl(pThis, pCAStream, PDMAUDIOSTREAMCMD_DISABLE);
|
---|
2388 | if (RT_SUCCESS(rc))
|
---|
2389 | {
|
---|
2390 | ASMAtomicXchgU32(&pCAStream->enmStatus, COREAUDIOSTATUS_IN_UNINIT);
|
---|
2391 |
|
---|
2392 | rc = coreAudioStreamUninit(pCAStream);
|
---|
2393 |
|
---|
2394 | if (RT_SUCCESS(rc))
|
---|
2395 | ASMAtomicXchgU32(&pCAStream->enmStatus, COREAUDIOSTATUS_UNINIT);
|
---|
2396 | }
|
---|
2397 |
|
---|
2398 | if (RT_SUCCESS(rc))
|
---|
2399 | {
|
---|
2400 | if (RTCritSectIsInitialized(&pCAStream->CritSect))
|
---|
2401 | RTCritSectDelete(&pCAStream->CritSect);
|
---|
2402 | }
|
---|
2403 |
|
---|
2404 | LogFunc(("rc=%Rrc\n", rc));
|
---|
2405 | return rc;
|
---|
2406 | }
|
---|
2407 |
|
---|
2408 |
|
---|
2409 | /**
|
---|
2410 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
|
---|
2411 | */
|
---|
2412 | static DECLCALLBACK(int) drvHostCoreAudioHA_StreamControl(PPDMIHOSTAUDIO pInterface,
|
---|
2413 | PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
2414 | {
|
---|
2415 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2416 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2417 |
|
---|
2418 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2419 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2420 |
|
---|
2421 | return coreAudioStreamControl(pThis, pCAStream, enmStreamCmd);
|
---|
2422 | }
|
---|
2423 |
|
---|
2424 |
|
---|
2425 | /**
|
---|
2426 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
|
---|
2427 | */
|
---|
2428 | static DECLCALLBACK(uint32_t) drvHostCoreAudioHA_StreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2429 | {
|
---|
2430 | RT_NOREF(pInterface);
|
---|
2431 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2432 |
|
---|
2433 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2434 |
|
---|
2435 | if (ASMAtomicReadU32(&pCAStream->enmStatus) != COREAUDIOSTATUS_INIT)
|
---|
2436 | return 0;
|
---|
2437 |
|
---|
2438 | AssertPtr(pCAStream->pCfg);
|
---|
2439 | AssertPtr(pCAStream->pCircBuf);
|
---|
2440 |
|
---|
2441 | switch (pCAStream->pCfg->enmDir)
|
---|
2442 | {
|
---|
2443 | case PDMAUDIODIR_IN:
|
---|
2444 | return (uint32_t)RTCircBufUsed(pCAStream->pCircBuf);
|
---|
2445 |
|
---|
2446 | case PDMAUDIODIR_OUT:
|
---|
2447 | default:
|
---|
2448 | AssertFailed();
|
---|
2449 | break;
|
---|
2450 | }
|
---|
2451 |
|
---|
2452 | return 0;
|
---|
2453 | }
|
---|
2454 |
|
---|
2455 |
|
---|
2456 | /**
|
---|
2457 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
|
---|
2458 | */
|
---|
2459 | static DECLCALLBACK(uint32_t) drvHostCoreAudioHA_StreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2460 | {
|
---|
2461 | RT_NOREF(pInterface);
|
---|
2462 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2463 |
|
---|
2464 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2465 |
|
---|
2466 | uint32_t cbWritable = 0;
|
---|
2467 |
|
---|
2468 | if (ASMAtomicReadU32(&pCAStream->enmStatus) == COREAUDIOSTATUS_INIT)
|
---|
2469 | {
|
---|
2470 | AssertPtr(pCAStream->pCfg);
|
---|
2471 | AssertPtr(pCAStream->pCircBuf);
|
---|
2472 |
|
---|
2473 | switch (pCAStream->pCfg->enmDir)
|
---|
2474 | {
|
---|
2475 | case PDMAUDIODIR_OUT:
|
---|
2476 | cbWritable = (uint32_t)RTCircBufFree(pCAStream->pCircBuf);
|
---|
2477 | break;
|
---|
2478 |
|
---|
2479 | default:
|
---|
2480 | break;
|
---|
2481 | }
|
---|
2482 | }
|
---|
2483 |
|
---|
2484 | LogFlowFunc(("cbWritable=%RU32\n", cbWritable));
|
---|
2485 | return cbWritable;
|
---|
2486 | }
|
---|
2487 |
|
---|
2488 |
|
---|
2489 | /**
|
---|
2490 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetStatus}
|
---|
2491 | */
|
---|
2492 | static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvHostCoreAudioHA_StreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2493 | {
|
---|
2494 | RT_NOREF(pInterface);
|
---|
2495 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2496 |
|
---|
2497 | PCOREAUDIOSTREAM pCAStream = (PCOREAUDIOSTREAM)pStream;
|
---|
2498 |
|
---|
2499 | PDMAUDIOSTREAMSTS fStrmStatus = PDMAUDIOSTREAMSTS_FLAGS_NONE;
|
---|
2500 |
|
---|
2501 | if (pCAStream->pCfg) /* Configured? */
|
---|
2502 | {
|
---|
2503 | if (ASMAtomicReadU32(&pCAStream->enmStatus) == COREAUDIOSTATUS_INIT)
|
---|
2504 | fStrmStatus |= PDMAUDIOSTREAMSTS_FLAGS_INITIALIZED | PDMAUDIOSTREAMSTS_FLAGS_ENABLED;
|
---|
2505 | }
|
---|
2506 |
|
---|
2507 | return fStrmStatus;
|
---|
2508 | }
|
---|
2509 |
|
---|
2510 |
|
---|
2511 | /**
|
---|
2512 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
|
---|
2513 | */
|
---|
2514 | static DECLCALLBACK(int) drvHostCoreAudioHA_StreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
2515 | {
|
---|
2516 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
2517 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
2518 |
|
---|
2519 | RT_NOREF(pInterface, pStream);
|
---|
2520 |
|
---|
2521 | /* Nothing to do here for Core Audio. */
|
---|
2522 | return VINF_SUCCESS;
|
---|
2523 | }
|
---|
2524 |
|
---|
2525 |
|
---|
2526 | /**
|
---|
2527 | * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
|
---|
2528 | */
|
---|
2529 | static DECLCALLBACK(int) drvHostCoreAudioHA_Init(PPDMIHOSTAUDIO pInterface)
|
---|
2530 | {
|
---|
2531 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2532 |
|
---|
2533 | int rc = DrvAudioHlpDeviceEnumInit(&pThis->Devices);
|
---|
2534 | if (RT_SUCCESS(rc))
|
---|
2535 | {
|
---|
2536 | /* Do the first (initial) internal device enumeration. */
|
---|
2537 | rc = coreAudioEnumerateDevices(pThis);
|
---|
2538 | }
|
---|
2539 |
|
---|
2540 | if (RT_SUCCESS(rc))
|
---|
2541 | {
|
---|
2542 | /* Register system callbacks. */
|
---|
2543 | AudioObjectPropertyAddress propAdr = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal,
|
---|
2544 | kAudioObjectPropertyElementMaster };
|
---|
2545 |
|
---|
2546 | OSStatus err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &propAdr,
|
---|
2547 | coreAudioDefaultDeviceChangedCb, pThis /* pvUser */);
|
---|
2548 | if ( err != noErr
|
---|
2549 | && err != kAudioHardwareIllegalOperationError)
|
---|
2550 | {
|
---|
2551 | LogRel(("CoreAudio: Failed to add the input default device changed listener (%RI32)\n", err));
|
---|
2552 | }
|
---|
2553 |
|
---|
2554 | propAdr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
---|
2555 | err = AudioObjectAddPropertyListener(kAudioObjectSystemObject, &propAdr,
|
---|
2556 | coreAudioDefaultDeviceChangedCb, pThis /* pvUser */);
|
---|
2557 | if ( err != noErr
|
---|
2558 | && err != kAudioHardwareIllegalOperationError)
|
---|
2559 | {
|
---|
2560 | LogRel(("CoreAudio: Failed to add the output default device changed listener (%RI32)\n", err));
|
---|
2561 | }
|
---|
2562 | }
|
---|
2563 |
|
---|
2564 | LogFlowFunc(("Returning %Rrc\n", rc));
|
---|
2565 | return rc;
|
---|
2566 | }
|
---|
2567 |
|
---|
2568 |
|
---|
2569 | /**
|
---|
2570 | * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
|
---|
2571 | */
|
---|
2572 | static DECLCALLBACK(void) drvHostCoreAudioHA_Shutdown(PPDMIHOSTAUDIO pInterface)
|
---|
2573 | {
|
---|
2574 | PDRVHOSTCOREAUDIO pThis = PDMIHOSTAUDIO_2_DRVHOSTCOREAUDIO(pInterface);
|
---|
2575 |
|
---|
2576 | /*
|
---|
2577 | * Unregister system callbacks.
|
---|
2578 | */
|
---|
2579 | AudioObjectPropertyAddress propAdr = { kAudioHardwarePropertyDefaultInputDevice, kAudioObjectPropertyScopeGlobal,
|
---|
2580 | kAudioObjectPropertyElementMaster };
|
---|
2581 |
|
---|
2582 | OSStatus err = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &propAdr,
|
---|
2583 | coreAudioDefaultDeviceChangedCb, pThis /* pvUser */);
|
---|
2584 | if ( err != noErr
|
---|
2585 | && err != kAudioHardwareBadObjectError)
|
---|
2586 | {
|
---|
2587 | LogRel(("CoreAudio: Failed to remove the default input device changed listener (%RI32)\n", err));
|
---|
2588 | }
|
---|
2589 |
|
---|
2590 | propAdr.mSelector = kAudioHardwarePropertyDefaultOutputDevice;
|
---|
2591 | err = AudioObjectRemovePropertyListener(kAudioObjectSystemObject, &propAdr,
|
---|
2592 | coreAudioDefaultDeviceChangedCb, pThis /* pvUser */);
|
---|
2593 | if ( err != noErr
|
---|
2594 | && err != kAudioHardwareBadObjectError)
|
---|
2595 | {
|
---|
2596 | LogRel(("CoreAudio: Failed to remove the default output device changed listener (%RI32)\n", err));
|
---|
2597 | }
|
---|
2598 |
|
---|
2599 | LogFlowFuncEnter();
|
---|
2600 | }
|
---|
2601 |
|
---|
2602 |
|
---|
2603 | /**
|
---|
2604 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
2605 | */
|
---|
2606 | static DECLCALLBACK(void *) drvHostCoreAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
2607 | {
|
---|
2608 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
2609 | PDRVHOSTCOREAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTCOREAUDIO);
|
---|
2610 |
|
---|
2611 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
2612 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
|
---|
2613 |
|
---|
2614 | return NULL;
|
---|
2615 | }
|
---|
2616 |
|
---|
2617 |
|
---|
2618 | /**
|
---|
2619 | * @callback_method_impl{FNPDMDRVCONSTRUCT,
|
---|
2620 | * Construct a Core Audio driver instance.}
|
---|
2621 | */
|
---|
2622 | static DECLCALLBACK(int) drvHostCoreAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
2623 | {
|
---|
2624 | RT_NOREF(pCfg, fFlags);
|
---|
2625 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
2626 | PDRVHOSTCOREAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTCOREAUDIO);
|
---|
2627 | LogRel(("Audio: Initializing Core Audio driver\n"));
|
---|
2628 |
|
---|
2629 | /*
|
---|
2630 | * Init the static parts.
|
---|
2631 | */
|
---|
2632 | pThis->pDrvIns = pDrvIns;
|
---|
2633 | /* IBase */
|
---|
2634 | pDrvIns->IBase.pfnQueryInterface = drvHostCoreAudioQueryInterface;
|
---|
2635 | /* IHostAudio */
|
---|
2636 | pThis->IHostAudio.pfnInit = drvHostCoreAudioHA_Init;
|
---|
2637 | pThis->IHostAudio.pfnShutdown = drvHostCoreAudioHA_Shutdown;
|
---|
2638 | pThis->IHostAudio.pfnGetConfig = drvHostCoreAudioHA_GetConfig;
|
---|
2639 | pThis->IHostAudio.pfnGetStatus = drvHostCoreAudioHA_GetStatus;
|
---|
2640 | pThis->IHostAudio.pfnStreamCreate = drvHostCoreAudioHA_StreamCreate;
|
---|
2641 | pThis->IHostAudio.pfnStreamDestroy = drvHostCoreAudioHA_StreamDestroy;
|
---|
2642 | pThis->IHostAudio.pfnStreamControl = drvHostCoreAudioHA_StreamControl;
|
---|
2643 | pThis->IHostAudio.pfnStreamGetReadable = drvHostCoreAudioHA_StreamGetReadable;
|
---|
2644 | pThis->IHostAudio.pfnStreamGetWritable = drvHostCoreAudioHA_StreamGetWritable;
|
---|
2645 | pThis->IHostAudio.pfnStreamGetStatus = drvHostCoreAudioHA_StreamGetStatus;
|
---|
2646 | pThis->IHostAudio.pfnStreamIterate = drvHostCoreAudioHA_StreamIterate;
|
---|
2647 | pThis->IHostAudio.pfnStreamPlay = drvHostCoreAudioHA_StreamPlay;
|
---|
2648 | pThis->IHostAudio.pfnStreamCapture = drvHostCoreAudioHA_StreamCapture;
|
---|
2649 | #ifdef VBOX_WITH_AUDIO_CALLBACKS
|
---|
2650 | pThis->IHostAudio.pfnSetCallback = drvHostCoreAudioHA_SetCallback;
|
---|
2651 | pThis->pfnCallback = NULL;
|
---|
2652 | #else
|
---|
2653 | pThis->IHostAudio.pfnSetCallback = NULL;
|
---|
2654 | #endif
|
---|
2655 | pThis->IHostAudio.pfnGetDevices = drvHostCoreAudioHA_GetDevices;
|
---|
2656 | pThis->IHostAudio.pfnStreamGetPending = NULL;
|
---|
2657 | pThis->IHostAudio.pfnStreamPlayBegin = NULL;
|
---|
2658 | pThis->IHostAudio.pfnStreamPlayEnd = NULL;
|
---|
2659 | pThis->IHostAudio.pfnStreamCaptureBegin = NULL;
|
---|
2660 | pThis->IHostAudio.pfnStreamCaptureEnd = NULL;
|
---|
2661 |
|
---|
2662 | int rc = RTCritSectInit(&pThis->CritSect);
|
---|
2663 | AssertRCReturn(rc, rc);
|
---|
2664 |
|
---|
2665 | #ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
|
---|
2666 | RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "caConverterCbInput.pcm");
|
---|
2667 | RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "caPlayback.pcm");
|
---|
2668 | #endif
|
---|
2669 |
|
---|
2670 | LogFlowFuncLeaveRC(rc);
|
---|
2671 | return rc;
|
---|
2672 | }
|
---|
2673 |
|
---|
2674 |
|
---|
2675 | /**
|
---|
2676 | * @callback_method_impl{FNPDMDRVDESTRUCT}
|
---|
2677 | */
|
---|
2678 | static DECLCALLBACK(void) drvHostCoreAudioDestruct(PPDMDRVINS pDrvIns)
|
---|
2679 | {
|
---|
2680 | PDMDRV_CHECK_VERSIONS_RETURN_VOID(pDrvIns);
|
---|
2681 | PDRVHOSTCOREAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTCOREAUDIO);
|
---|
2682 |
|
---|
2683 | int rc2 = RTCritSectDelete(&pThis->CritSect);
|
---|
2684 | AssertRC(rc2);
|
---|
2685 |
|
---|
2686 | LogFlowFuncLeaveRC(rc2);
|
---|
2687 | }
|
---|
2688 |
|
---|
2689 |
|
---|
2690 | /**
|
---|
2691 | * Char driver registration record.
|
---|
2692 | */
|
---|
2693 | const PDMDRVREG g_DrvHostCoreAudio =
|
---|
2694 | {
|
---|
2695 | /* u32Version */
|
---|
2696 | PDM_DRVREG_VERSION,
|
---|
2697 | /* szName */
|
---|
2698 | "CoreAudio",
|
---|
2699 | /* szRCMod */
|
---|
2700 | "",
|
---|
2701 | /* szR0Mod */
|
---|
2702 | "",
|
---|
2703 | /* pszDescription */
|
---|
2704 | "Core Audio host driver",
|
---|
2705 | /* fFlags */
|
---|
2706 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
2707 | /* fClass. */
|
---|
2708 | PDM_DRVREG_CLASS_AUDIO,
|
---|
2709 | /* cMaxInstances */
|
---|
2710 | ~0U,
|
---|
2711 | /* cbInstance */
|
---|
2712 | sizeof(DRVHOSTCOREAUDIO),
|
---|
2713 | /* pfnConstruct */
|
---|
2714 | drvHostCoreAudioConstruct,
|
---|
2715 | /* pfnDestruct */
|
---|
2716 | drvHostCoreAudioDestruct,
|
---|
2717 | /* pfnRelocate */
|
---|
2718 | NULL,
|
---|
2719 | /* pfnIOCtl */
|
---|
2720 | NULL,
|
---|
2721 | /* pfnPowerOn */
|
---|
2722 | NULL,
|
---|
2723 | /* pfnReset */
|
---|
2724 | NULL,
|
---|
2725 | /* pfnSuspend */
|
---|
2726 | NULL,
|
---|
2727 | /* pfnResume */
|
---|
2728 | NULL,
|
---|
2729 | /* pfnAttach */
|
---|
2730 | NULL,
|
---|
2731 | /* pfnDetach */
|
---|
2732 | NULL,
|
---|
2733 | /* pfnPowerOff */
|
---|
2734 | NULL,
|
---|
2735 | /* pfnSoftReset */
|
---|
2736 | NULL,
|
---|
2737 | /* u32EndVersion */
|
---|
2738 | PDM_DRVREG_VERSION
|
---|
2739 | };
|
---|
2740 |
|
---|