1 | /* $Id: DrvHostDebugAudio.cpp 73350 2018-07-25 11:50:50Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Debug audio driver.
|
---|
4 | *
|
---|
5 | * Host backend for dumping and injecting audio data from/to the device emulation.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * Copyright (C) 2016-2018 Oracle Corporation
|
---|
10 | *
|
---|
11 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
12 | * available from http://www.virtualbox.org. This file is free software;
|
---|
13 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
14 | * General Public License (GPL) as published by the Free Software
|
---|
15 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
16 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
17 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
18 | */
|
---|
19 |
|
---|
20 | #include <iprt/alloc.h>
|
---|
21 | #include <iprt/uuid.h> /* For PDMIBASE_2_PDMDRV. */
|
---|
22 |
|
---|
23 | #define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
|
---|
24 | #include <VBox/log.h>
|
---|
25 | #include <VBox/vmm/pdmaudioifs.h>
|
---|
26 |
|
---|
27 | #include "DrvAudio.h"
|
---|
28 | #include "VBoxDD.h"
|
---|
29 |
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Structure for keeping a debug input/output stream.
|
---|
33 | */
|
---|
34 | typedef struct DEBUGAUDIOSTREAM
|
---|
35 | {
|
---|
36 | /** The stream's acquired configuration. */
|
---|
37 | PPDMAUDIOSTREAMCFG pCfg;
|
---|
38 | /** Audio file to dump output to or read input from. */
|
---|
39 | PPDMAUDIOFILE pFile;
|
---|
40 | union
|
---|
41 | {
|
---|
42 | struct
|
---|
43 | {
|
---|
44 | /** Timestamp of last captured samples. */
|
---|
45 | uint64_t tsLastCaptured;
|
---|
46 | } In;
|
---|
47 | };
|
---|
48 | } DEBUGAUDIOSTREAM, *PDEBUGAUDIOSTREAM;
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * Debug audio driver instance data.
|
---|
52 | * @implements PDMIAUDIOCONNECTOR
|
---|
53 | */
|
---|
54 | typedef struct DRVHOSTDEBUGAUDIO
|
---|
55 | {
|
---|
56 | /** Pointer to the driver instance structure. */
|
---|
57 | PPDMDRVINS pDrvIns;
|
---|
58 | /** Pointer to host audio interface. */
|
---|
59 | PDMIHOSTAUDIO IHostAudio;
|
---|
60 | } DRVHOSTDEBUGAUDIO, *PDRVHOSTDEBUGAUDIO;
|
---|
61 |
|
---|
62 | /*******************************************PDM_AUDIO_DRIVER******************************/
|
---|
63 |
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
|
---|
67 | */
|
---|
68 | static DECLCALLBACK(int) drvHostDebugAudioGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
|
---|
69 | {
|
---|
70 | RT_NOREF(pInterface);
|
---|
71 | AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
|
---|
72 |
|
---|
73 | pBackendCfg->cbStreamOut = sizeof(DEBUGAUDIOSTREAM);
|
---|
74 | pBackendCfg->cbStreamIn = sizeof(DEBUGAUDIOSTREAM);
|
---|
75 |
|
---|
76 | pBackendCfg->cMaxStreamsOut = 1; /* Output */
|
---|
77 | pBackendCfg->cMaxStreamsIn = 2; /* Line input + microphone input. */
|
---|
78 |
|
---|
79 | return VINF_SUCCESS;
|
---|
80 | }
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
|
---|
85 | */
|
---|
86 | static DECLCALLBACK(int) drvHostDebugAudioInit(PPDMIHOSTAUDIO pInterface)
|
---|
87 | {
|
---|
88 | RT_NOREF(pInterface);
|
---|
89 |
|
---|
90 | LogFlowFuncLeaveRC(VINF_SUCCESS);
|
---|
91 | return VINF_SUCCESS;
|
---|
92 | }
|
---|
93 |
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
|
---|
97 | */
|
---|
98 | static DECLCALLBACK(void) drvHostDebugAudioShutdown(PPDMIHOSTAUDIO pInterface)
|
---|
99 | {
|
---|
100 | RT_NOREF(pInterface);
|
---|
101 | }
|
---|
102 |
|
---|
103 |
|
---|
104 | /**
|
---|
105 | * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
|
---|
106 | */
|
---|
107 | static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostDebugAudioGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
|
---|
108 | {
|
---|
109 | RT_NOREF(enmDir);
|
---|
110 | AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
|
---|
111 |
|
---|
112 | return PDMAUDIOBACKENDSTS_RUNNING;
|
---|
113 | }
|
---|
114 |
|
---|
115 |
|
---|
116 | static int debugCreateStreamIn(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg,
|
---|
117 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
118 | {
|
---|
119 | RT_NOREF(pDrv, pStreamDbg, pCfgReq);
|
---|
120 |
|
---|
121 | if (pCfgAcq)
|
---|
122 | pCfgAcq->cFrameBufferHint = _1K;
|
---|
123 |
|
---|
124 | return VINF_SUCCESS;
|
---|
125 | }
|
---|
126 |
|
---|
127 |
|
---|
128 | static int debugCreateStreamOut(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg,
|
---|
129 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
130 | {
|
---|
131 | RT_NOREF(pDrv);
|
---|
132 |
|
---|
133 | char szTemp[RTPATH_MAX];
|
---|
134 | int rc = RTPathTemp(szTemp, sizeof(szTemp));
|
---|
135 | if (RT_SUCCESS(rc))
|
---|
136 | {
|
---|
137 | char szFile[RTPATH_MAX];
|
---|
138 | rc = DrvAudioHlpGetFileName(szFile, RT_ELEMENTS(szFile), szTemp, "DebugAudioOut",
|
---|
139 | pDrv->pDrvIns->iInstance, PDMAUDIOFILETYPE_WAV, PDMAUDIOFILENAME_FLAG_NONE);
|
---|
140 | if (RT_SUCCESS(rc))
|
---|
141 | {
|
---|
142 | rc = DrvAudioHlpFileCreate(PDMAUDIOFILETYPE_WAV, szFile, PDMAUDIOFILE_FLAG_NONE, &pStreamDbg->pFile);
|
---|
143 | if (RT_SUCCESS(rc))
|
---|
144 | {
|
---|
145 | rc = DrvAudioHlpFileOpen(pStreamDbg->pFile, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE,
|
---|
146 | &pCfgReq->Props);
|
---|
147 | }
|
---|
148 |
|
---|
149 | if (RT_FAILURE(rc))
|
---|
150 | LogRel(("DebugAudio: Creating output file '%s' failed with %Rrc\n", szFile, rc));
|
---|
151 | }
|
---|
152 | else
|
---|
153 | LogRel(("DebugAudio: Unable to build file name for temp dir '%s': %Rrc\n", szTemp, rc));
|
---|
154 | }
|
---|
155 | else
|
---|
156 | LogRel(("DebugAudio: Unable to retrieve temp dir: %Rrc\n", rc));
|
---|
157 |
|
---|
158 | if (RT_SUCCESS(rc))
|
---|
159 | {
|
---|
160 | if (pCfgAcq)
|
---|
161 | pCfgAcq->cFrameBufferHint = _1K;
|
---|
162 | }
|
---|
163 |
|
---|
164 | return rc;
|
---|
165 | }
|
---|
166 |
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
|
---|
170 | */
|
---|
171 | static DECLCALLBACK(int) drvHostDebugAudioStreamCreate(PPDMIHOSTAUDIO pInterface,
|
---|
172 | PPDMAUDIOBACKENDSTREAM pStream,
|
---|
173 | PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
|
---|
174 | {
|
---|
175 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
176 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
177 | AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
|
---|
178 | AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
|
---|
179 |
|
---|
180 | PDRVHOSTDEBUGAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTDEBUGAUDIO, IHostAudio);
|
---|
181 | PDEBUGAUDIOSTREAM pStreamDbg = (PDEBUGAUDIOSTREAM)pStream;
|
---|
182 |
|
---|
183 | int rc;
|
---|
184 | if (pCfgReq->enmDir == PDMAUDIODIR_IN)
|
---|
185 | rc = debugCreateStreamIn( pDrv, pStreamDbg, pCfgReq, pCfgAcq);
|
---|
186 | else
|
---|
187 | rc = debugCreateStreamOut(pDrv, pStreamDbg, pCfgReq, pCfgAcq);
|
---|
188 |
|
---|
189 | if (RT_SUCCESS(rc))
|
---|
190 | {
|
---|
191 | pStreamDbg->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
|
---|
192 | if (!pStreamDbg->pCfg)
|
---|
193 | rc = VERR_NO_MEMORY;
|
---|
194 | }
|
---|
195 |
|
---|
196 | return rc;
|
---|
197 | }
|
---|
198 |
|
---|
199 |
|
---|
200 | /**
|
---|
201 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
|
---|
202 | */
|
---|
203 | static DECLCALLBACK(int) drvHostDebugAudioStreamPlay(PPDMIHOSTAUDIO pInterface,
|
---|
204 | PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf, uint32_t cxBuf,
|
---|
205 | uint32_t *pcxWritten)
|
---|
206 | {
|
---|
207 | RT_NOREF(pInterface);
|
---|
208 | PDEBUGAUDIOSTREAM pStreamDbg = (PDEBUGAUDIOSTREAM)pStream;
|
---|
209 |
|
---|
210 | int rc = DrvAudioHlpFileWrite(pStreamDbg->pFile, pvBuf, cxBuf, 0 /* fFlags */);
|
---|
211 | if (RT_FAILURE(rc))
|
---|
212 | {
|
---|
213 | LogRel(("DebugAudio: Writing output failed with %Rrc\n", rc));
|
---|
214 | return rc;
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (pcxWritten)
|
---|
218 | *pcxWritten = cxBuf;
|
---|
219 |
|
---|
220 | return VINF_SUCCESS;
|
---|
221 | }
|
---|
222 |
|
---|
223 |
|
---|
224 | /**
|
---|
225 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
|
---|
226 | */
|
---|
227 | static DECLCALLBACK(int) drvHostDebugAudioStreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
|
---|
228 | void *pvBuf, uint32_t cxBuf, uint32_t *pcxRead)
|
---|
229 | {
|
---|
230 | RT_NOREF(pInterface, pStream, pvBuf, cxBuf);
|
---|
231 |
|
---|
232 | /* Never capture anything. */
|
---|
233 | if (pcxRead)
|
---|
234 | *pcxRead = 0;
|
---|
235 |
|
---|
236 | return VINF_SUCCESS;
|
---|
237 | }
|
---|
238 |
|
---|
239 |
|
---|
240 | static int debugDestroyStreamIn(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg)
|
---|
241 | {
|
---|
242 | RT_NOREF(pDrv, pStreamDbg);
|
---|
243 | return VINF_SUCCESS;
|
---|
244 | }
|
---|
245 |
|
---|
246 |
|
---|
247 | static int debugDestroyStreamOut(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg)
|
---|
248 | {
|
---|
249 | RT_NOREF(pDrv);
|
---|
250 |
|
---|
251 | DrvAudioHlpFileDestroy(pStreamDbg->pFile);
|
---|
252 |
|
---|
253 | return VINF_SUCCESS;
|
---|
254 | }
|
---|
255 |
|
---|
256 |
|
---|
257 | /**
|
---|
258 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
|
---|
259 | */
|
---|
260 | static DECLCALLBACK(int) drvHostDebugAudioStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
261 | {
|
---|
262 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
263 |
|
---|
264 | PDRVHOSTDEBUGAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTDEBUGAUDIO, IHostAudio);
|
---|
265 | PDEBUGAUDIOSTREAM pStreamDbg = (PDEBUGAUDIOSTREAM)pStream;
|
---|
266 |
|
---|
267 | if (!pStreamDbg->pCfg) /* Not (yet) configured? Skip. */
|
---|
268 | return VINF_SUCCESS;
|
---|
269 |
|
---|
270 | int rc;
|
---|
271 | if (pStreamDbg->pCfg->enmDir == PDMAUDIODIR_IN)
|
---|
272 | rc = debugDestroyStreamIn (pDrv, pStreamDbg);
|
---|
273 | else
|
---|
274 | rc = debugDestroyStreamOut(pDrv, pStreamDbg);
|
---|
275 |
|
---|
276 | if (RT_SUCCESS(rc))
|
---|
277 | {
|
---|
278 | DrvAudioHlpStreamCfgFree(pStreamDbg->pCfg);
|
---|
279 | pStreamDbg->pCfg = NULL;
|
---|
280 | }
|
---|
281 |
|
---|
282 | return rc;
|
---|
283 | }
|
---|
284 |
|
---|
285 |
|
---|
286 | /**
|
---|
287 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
|
---|
288 | */
|
---|
289 | static DECLCALLBACK(int) drvHostDebugAudioStreamControl(PPDMIHOSTAUDIO pInterface,
|
---|
290 | PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
|
---|
291 | {
|
---|
292 | RT_NOREF(enmStreamCmd);
|
---|
293 | AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
|
---|
294 | AssertPtrReturn(pStream, VERR_INVALID_POINTER);
|
---|
295 |
|
---|
296 | return VINF_SUCCESS;
|
---|
297 | }
|
---|
298 |
|
---|
299 |
|
---|
300 | /**
|
---|
301 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
|
---|
302 | */
|
---|
303 | static DECLCALLBACK(uint32_t) drvHostDebugAudioStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
304 | {
|
---|
305 | RT_NOREF(pInterface, pStream);
|
---|
306 |
|
---|
307 | return 0; /* Never capture anything. */
|
---|
308 | }
|
---|
309 |
|
---|
310 |
|
---|
311 | /**
|
---|
312 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
|
---|
313 | */
|
---|
314 | static DECLCALLBACK(uint32_t) drvHostDebugAudioStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
315 | {
|
---|
316 | RT_NOREF(pInterface, pStream);
|
---|
317 |
|
---|
318 | return UINT32_MAX;
|
---|
319 | }
|
---|
320 |
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
|
---|
324 | */
|
---|
325 | static DECLCALLBACK(PDMAUDIOSTREAMSTS) drvHostDebugAudioStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
326 | {
|
---|
327 | RT_NOREF(pInterface, pStream);
|
---|
328 |
|
---|
329 | return (PDMAUDIOSTREAMSTS_FLAG_INITIALIZED | PDMAUDIOSTREAMSTS_FLAG_ENABLED);
|
---|
330 | }
|
---|
331 |
|
---|
332 |
|
---|
333 | /**
|
---|
334 | * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
|
---|
335 | */
|
---|
336 | static DECLCALLBACK(int) drvHostDebugAudioStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
|
---|
337 | {
|
---|
338 | RT_NOREF(pInterface, pStream);
|
---|
339 | return VINF_SUCCESS;
|
---|
340 | }
|
---|
341 |
|
---|
342 |
|
---|
343 | /**
|
---|
344 | * @interface_method_impl{PDMIBASE,pfnQueryInterface}
|
---|
345 | */
|
---|
346 | static DECLCALLBACK(void *) drvHostDebugAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
|
---|
347 | {
|
---|
348 | PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
|
---|
349 | PDRVHOSTDEBUGAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDEBUGAUDIO);
|
---|
350 |
|
---|
351 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
|
---|
352 | PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
|
---|
353 | return NULL;
|
---|
354 | }
|
---|
355 |
|
---|
356 |
|
---|
357 | /**
|
---|
358 | * Constructs a Null audio driver instance.
|
---|
359 | *
|
---|
360 | * @copydoc FNPDMDRVCONSTRUCT
|
---|
361 | */
|
---|
362 | static DECLCALLBACK(int) drvHostDebugAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
|
---|
363 | {
|
---|
364 | RT_NOREF(pCfg, fFlags);
|
---|
365 | PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
|
---|
366 | PDRVHOSTDEBUGAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDEBUGAUDIO);
|
---|
367 | LogRel(("Audio: Initializing DEBUG driver\n"));
|
---|
368 |
|
---|
369 | /*
|
---|
370 | * Init the static parts.
|
---|
371 | */
|
---|
372 | pThis->pDrvIns = pDrvIns;
|
---|
373 | /* IBase */
|
---|
374 | pDrvIns->IBase.pfnQueryInterface = drvHostDebugAudioQueryInterface;
|
---|
375 | /* IHostAudio */
|
---|
376 | PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostDebugAudio);
|
---|
377 |
|
---|
378 | #ifdef VBOX_AUDIO_DEBUG_DUMP_PCM_DATA
|
---|
379 | RTFileDelete(VBOX_AUDIO_DEBUG_DUMP_PCM_DATA_PATH "AudioDebugOutput.pcm");
|
---|
380 | #endif
|
---|
381 |
|
---|
382 | return VINF_SUCCESS;
|
---|
383 | }
|
---|
384 |
|
---|
385 | /**
|
---|
386 | * Char driver registration record.
|
---|
387 | */
|
---|
388 | const PDMDRVREG g_DrvHostDebugAudio =
|
---|
389 | {
|
---|
390 | /* u32Version */
|
---|
391 | PDM_DRVREG_VERSION,
|
---|
392 | /* szName */
|
---|
393 | "DebugAudio",
|
---|
394 | /* szRCMod */
|
---|
395 | "",
|
---|
396 | /* szR0Mod */
|
---|
397 | "",
|
---|
398 | /* pszDescription */
|
---|
399 | "Debug audio host driver",
|
---|
400 | /* fFlags */
|
---|
401 | PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
|
---|
402 | /* fClass. */
|
---|
403 | PDM_DRVREG_CLASS_AUDIO,
|
---|
404 | /* cMaxInstances */
|
---|
405 | ~0U,
|
---|
406 | /* cbInstance */
|
---|
407 | sizeof(DRVHOSTDEBUGAUDIO),
|
---|
408 | /* pfnConstruct */
|
---|
409 | drvHostDebugAudioConstruct,
|
---|
410 | /* pfnDestruct */
|
---|
411 | NULL,
|
---|
412 | /* pfnRelocate */
|
---|
413 | NULL,
|
---|
414 | /* pfnIOCtl */
|
---|
415 | NULL,
|
---|
416 | /* pfnPowerOn */
|
---|
417 | NULL,
|
---|
418 | /* pfnReset */
|
---|
419 | NULL,
|
---|
420 | /* pfnSuspend */
|
---|
421 | NULL,
|
---|
422 | /* pfnResume */
|
---|
423 | NULL,
|
---|
424 | /* pfnAttach */
|
---|
425 | NULL,
|
---|
426 | /* pfnDetach */
|
---|
427 | NULL,
|
---|
428 | /* pfnPowerOff */
|
---|
429 | NULL,
|
---|
430 | /* pfnSoftReset */
|
---|
431 | NULL,
|
---|
432 | /* u32EndVersion */
|
---|
433 | PDM_DRVREG_VERSION
|
---|
434 | };
|
---|
435 |
|
---|