VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostDebugAudio.cpp@ 65919

Last change on this file since 65919 was 65737, checked in by vboxsync, 8 years ago

DrvHostDebugAudio: Never capturing anything.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1/* $Id: DrvHostDebugAudio.cpp 65737 2017-02-10 16:10:45Z vboxsync $ */
2/** @file
3 * Debug audio driver -- host backend for dumping and injecting audio data
4 * from/to the device emulation.
5 */
6
7/*
8 * Copyright (C) 2016-2017 Oracle Corporation
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License (GPL) as published by the Free Software
14 * Foundation, in version 2 as it comes in the "COPYING" file of the
15 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
16 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
17 * --------------------------------------------------------------------
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 */
34typedef struct DEBUGAUDIOSTREAM
35{
36 /** The stream's acquired configuration. */
37 PPDMAUDIOSTREAMCFG pCfg;
38 /** Audio file to dump output to or read input from. */
39 PDMAUDIOFILE File;
40 union
41 {
42 struct
43 {
44 /** Timestamp of last captured samples. */
45 uint64_t tsLastCaptured;
46 } In;
47 struct
48 {
49 uint8_t *auPlayBuffer;
50 uint32_t cbPlayBuffer;
51 } Out;
52 };
53} DEBUGAUDIOSTREAM, *PDEBUGAUDIOSTREAM;
54
55/**
56 * Debug audio driver instance data.
57 * @implements PDMIAUDIOCONNECTOR
58 */
59typedef struct DRVHOSTDEBUGAUDIO
60{
61 /** Pointer to the driver instance structure. */
62 PPDMDRVINS pDrvIns;
63 /** Pointer to host audio interface. */
64 PDMIHOSTAUDIO IHostAudio;
65} DRVHOSTDEBUGAUDIO, *PDRVHOSTDEBUGAUDIO;
66
67/*******************************************PDM_AUDIO_DRIVER******************************/
68
69
70/**
71 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
72 */
73static DECLCALLBACK(int) drvHostDebugAudioGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
74{
75 RT_NOREF(pInterface);
76 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
77
78 pBackendCfg->cbStreamOut = sizeof(DEBUGAUDIOSTREAM);
79 pBackendCfg->cbStreamIn = sizeof(DEBUGAUDIOSTREAM);
80
81 pBackendCfg->cMaxStreamsOut = 1; /* Output */
82 pBackendCfg->cMaxStreamsIn = 2; /* Line input + microphone input. */
83
84 return VINF_SUCCESS;
85}
86
87
88/**
89 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
90 */
91static DECLCALLBACK(int) drvHostDebugAudioInit(PPDMIHOSTAUDIO pInterface)
92{
93 RT_NOREF(pInterface);
94
95 LogFlowFuncLeaveRC(VINF_SUCCESS);
96 return VINF_SUCCESS;
97}
98
99
100/**
101 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
102 */
103static DECLCALLBACK(void) drvHostDebugAudioShutdown(PPDMIHOSTAUDIO pInterface)
104{
105 RT_NOREF(pInterface);
106}
107
108
109/**
110 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
111 */
112static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostDebugAudioGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
113{
114 RT_NOREF(enmDir);
115 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
116
117 return PDMAUDIOBACKENDSTS_RUNNING;
118}
119
120
121static int debugCreateStreamIn(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg,
122 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
123{
124 RT_NOREF(pDrv, pStreamDbg, pCfgReq);
125
126 if (pCfgAcq)
127 pCfgAcq->cSampleBufferHint = _1K;
128
129 return VINF_SUCCESS;
130}
131
132
133static int debugCreateStreamOut(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg,
134 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
135{
136 RT_NOREF(pDrv);
137
138 int rc = VINF_SUCCESS;
139
140 pStreamDbg->Out.cbPlayBuffer = _1K * PDMAUDIOSTREAMCFG_S2B(pCfgReq, 1); /** @todo Make this configurable? */
141 pStreamDbg->Out.auPlayBuffer = (uint8_t *)RTMemAlloc(pStreamDbg->Out.cbPlayBuffer);
142 if (!pStreamDbg->Out.auPlayBuffer)
143 rc = VERR_NO_MEMORY;
144
145 if (RT_SUCCESS(rc))
146 {
147 char szTemp[RTPATH_MAX];
148 rc = RTPathTemp(szTemp, sizeof(szTemp));
149 if (RT_SUCCESS(rc))
150 {
151 char szFile[RTPATH_MAX];
152 rc = DrvAudioHlpGetFileName(szFile, RT_ELEMENTS(szFile), szTemp, NULL, PDMAUDIOFILETYPE_WAV);
153 if (RT_SUCCESS(rc))
154 {
155 LogFlowFunc(("%s\n", szFile));
156 rc = DrvAudioHlpWAVFileOpen(&pStreamDbg->File, szFile,
157 RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE,
158 &pCfgReq->Props, PDMAUDIOFILEFLAG_NONE);
159 if (RT_FAILURE(rc))
160 LogRel(("DebugAudio: Creating output file '%s' failed with %Rrc\n", szFile, rc));
161 }
162 else
163 LogRel(("DebugAudio: Unable to build file name for temp dir '%s': %Rrc\n", szTemp, rc));
164 }
165 else
166 LogRel(("DebugAudio: Unable to retrieve temp dir: %Rrc\n", rc));
167 }
168
169 if (RT_SUCCESS(rc))
170 {
171 if (pCfgAcq)
172 pCfgAcq->cSampleBufferHint = PDMAUDIOSTREAMCFG_B2S(pCfgAcq, pStreamDbg->Out.cbPlayBuffer);
173 }
174
175 return rc;
176}
177
178
179/**
180 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
181 */
182static DECLCALLBACK(int) drvHostDebugAudioStreamCreate(PPDMIHOSTAUDIO pInterface,
183 PPDMAUDIOBACKENDSTREAM pStream,
184 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
185{
186 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
187 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
188 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
189 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
190
191 PDRVHOSTDEBUGAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTDEBUGAUDIO, IHostAudio);
192 PDEBUGAUDIOSTREAM pStreamDbg = (PDEBUGAUDIOSTREAM)pStream;
193
194 int rc;
195 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
196 rc = debugCreateStreamIn( pDrv, pStreamDbg, pCfgReq, pCfgAcq);
197 else
198 rc = debugCreateStreamOut(pDrv, pStreamDbg, pCfgReq, pCfgAcq);
199
200 if (RT_SUCCESS(rc))
201 {
202 pStreamDbg->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
203 if (!pStreamDbg->pCfg)
204 rc = VERR_NO_MEMORY;
205 }
206
207 return rc;
208}
209
210
211/**
212 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
213 */
214static DECLCALLBACK(int) drvHostDebugAudioStreamPlay(PPDMIHOSTAUDIO pInterface,
215 PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf, uint32_t cbBuf,
216 uint32_t *pcbWritten)
217{
218 RT_NOREF(pInterface);
219 PDEBUGAUDIOSTREAM pStreamDbg = (PDEBUGAUDIOSTREAM)pStream;
220
221 uint32_t cbWrittenTotal = 0;
222
223 uint32_t cbAvail = cbBuf;
224 while (cbAvail)
225 {
226 uint32_t cbChunk = RT_MIN(cbAvail, pStreamDbg->Out.cbPlayBuffer);
227
228 memcpy(pStreamDbg->Out.auPlayBuffer, (uint8_t *)pvBuf + cbWrittenTotal, cbChunk);
229#if 0
230 RTFILE fh;
231 RTFileOpen(&fh, "/tmp/AudioDebug-Output.pcm",
232 RTFILE_O_OPEN_CREATE | RTFILE_O_APPEND | RTFILE_O_WRITE | RTFILE_O_DENY_NONE);
233 RTFileWrite(fh, pStreamDbg->Out.pu8PlayBuffer, cbChunk, NULL);
234 RTFileClose(fh);
235#endif
236 int rc2 = DrvAudioHlpWAVFileWrite(&pStreamDbg->File,
237 pStreamDbg->Out.auPlayBuffer, cbChunk, 0 /* fFlags */);
238 if (RT_FAILURE(rc2))
239 {
240 LogRel(("DebugAudio: Writing output failed with %Rrc\n", rc2));
241 break;
242 }
243
244 Assert(cbAvail >= cbAvail);
245 cbAvail -= cbChunk;
246
247 cbWrittenTotal += cbChunk;
248 }
249
250 if (pcbWritten)
251 *pcbWritten = cbWrittenTotal;
252
253 return VINF_SUCCESS;
254}
255
256
257/**
258 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
259 */
260static DECLCALLBACK(int) drvHostDebugAudioStreamCapture(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream,
261 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
262{
263 RT_NOREF(pInterface, pStream, pvBuf, cbBuf);
264
265 /* Never capture anything. */
266 if (pcbRead)
267 *pcbRead = 0;
268
269 return VINF_SUCCESS;
270}
271
272
273static int debugDestroyStreamIn(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg)
274{
275 RT_NOREF(pDrv, pStreamDbg);
276 return VINF_SUCCESS;
277}
278
279
280static int debugDestroyStreamOut(PDRVHOSTDEBUGAUDIO pDrv, PDEBUGAUDIOSTREAM pStreamDbg)
281{
282 RT_NOREF(pDrv);
283
284 if (pStreamDbg->Out.auPlayBuffer)
285 {
286 RTMemFree(pStreamDbg->Out.auPlayBuffer);
287 pStreamDbg->Out.auPlayBuffer = NULL;
288 }
289
290 size_t cbDataSize = DrvAudioHlpWAVFileGetDataSize(&pStreamDbg->File);
291
292 int rc = DrvAudioHlpWAVFileClose(&pStreamDbg->File);
293 if (RT_SUCCESS(rc))
294 {
295 /* Delete the file again if nothing but the header was written to it. */
296 bool fDeleteEmptyFiles = true; /** @todo Make deletion configurable? */
297
298 if ( !cbDataSize
299 && fDeleteEmptyFiles)
300 {
301 rc = RTFileDelete(pStreamDbg->File.szName);
302 }
303 else
304 LogRel(("DebugAudio: Created output file '%s' (%zu bytes)\n", pStreamDbg->File.szName, cbDataSize));
305 }
306
307 return rc;
308}
309
310
311/**
312 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamDestroy}
313 */
314static DECLCALLBACK(int) drvHostDebugAudioStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
315{
316 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
317
318 PDRVHOSTDEBUGAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTDEBUGAUDIO, IHostAudio);
319 PDEBUGAUDIOSTREAM pStreamDbg = (PDEBUGAUDIOSTREAM)pStream;
320
321 if (!pStreamDbg->pCfg) /* Not (yet) configured? Skip. */
322 return VINF_SUCCESS;
323
324 int rc;
325 if (pStreamDbg->pCfg->enmDir == PDMAUDIODIR_IN)
326 rc = debugDestroyStreamIn (pDrv, pStreamDbg);
327 else
328 rc = debugDestroyStreamOut(pDrv, pStreamDbg);
329
330 if (RT_SUCCESS(rc))
331 {
332 DrvAudioHlpStreamCfgFree(pStreamDbg->pCfg);
333 pStreamDbg->pCfg = NULL;
334 }
335
336 return rc;
337}
338
339
340/**
341 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamControl}
342 */
343static DECLCALLBACK(int) drvHostDebugAudioStreamControl(PPDMIHOSTAUDIO pInterface,
344 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
345{
346 RT_NOREF(enmStreamCmd);
347 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
348 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
349
350 return VINF_SUCCESS;
351}
352
353
354/**
355 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
356 */
357static DECLCALLBACK(uint32_t) drvHostDebugAudioStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
358{
359 RT_NOREF(pInterface, pStream);
360
361 return 0; /* Never capture anything. */
362}
363
364
365/**
366 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
367 */
368static DECLCALLBACK(uint32_t) drvHostDebugAudioStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
369{
370 RT_NOREF(pInterface, pStream);
371
372 return UINT32_MAX;
373}
374
375
376/**
377 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
378 */
379static DECLCALLBACK(PDMAUDIOSTRMSTS) drvHostDebugAudioStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
380{
381 RT_NOREF(pInterface, pStream);
382
383 return (PDMAUDIOSTRMSTS_FLAG_INITIALIZED | PDMAUDIOSTRMSTS_FLAG_ENABLED);
384}
385
386
387/**
388 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamIterate}
389 */
390static DECLCALLBACK(int) drvHostDebugAudioStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
391{
392 RT_NOREF(pInterface, pStream);
393 return VINF_SUCCESS;
394}
395
396
397/**
398 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
399 */
400static DECLCALLBACK(void *) drvHostDebugAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
401{
402 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
403 PDRVHOSTDEBUGAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDEBUGAUDIO);
404
405 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
406 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
407 return NULL;
408}
409
410
411/**
412 * Constructs a Null audio driver instance.
413 *
414 * @copydoc FNPDMDRVCONSTRUCT
415 */
416static DECLCALLBACK(int) drvHostDebugAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
417{
418 RT_NOREF(pCfg, fFlags);
419 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
420 PDRVHOSTDEBUGAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTDEBUGAUDIO);
421 LogRel(("Audio: Initializing DEBUG driver\n"));
422
423 /*
424 * Init the static parts.
425 */
426 pThis->pDrvIns = pDrvIns;
427 /* IBase */
428 pDrvIns->IBase.pfnQueryInterface = drvHostDebugAudioQueryInterface;
429 /* IHostAudio */
430 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostDebugAudio);
431
432 return VINF_SUCCESS;
433}
434
435/**
436 * Char driver registration record.
437 */
438const PDMDRVREG g_DrvHostDebugAudio =
439{
440 /* u32Version */
441 PDM_DRVREG_VERSION,
442 /* szName */
443 "DebugAudio",
444 /* szRCMod */
445 "",
446 /* szR0Mod */
447 "",
448 /* pszDescription */
449 "Debug audio host driver",
450 /* fFlags */
451 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
452 /* fClass. */
453 PDM_DRVREG_CLASS_AUDIO,
454 /* cMaxInstances */
455 ~0U,
456 /* cbInstance */
457 sizeof(DRVHOSTDEBUGAUDIO),
458 /* pfnConstruct */
459 drvHostDebugAudioConstruct,
460 /* pfnDestruct */
461 NULL,
462 /* pfnRelocate */
463 NULL,
464 /* pfnIOCtl */
465 NULL,
466 /* pfnPowerOn */
467 NULL,
468 /* pfnReset */
469 NULL,
470 /* pfnSuspend */
471 NULL,
472 /* pfnResume */
473 NULL,
474 /* pfnAttach */
475 NULL,
476 /* pfnDetach */
477 NULL,
478 /* pfnPowerOff */
479 NULL,
480 /* pfnSoftReset */
481 NULL,
482 /* u32EndVersion */
483 PDM_DRVREG_VERSION
484};
485
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette