VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostValidationKit.cpp@ 65890

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

Audio/ValKit: Corrected deletion of data files for an empty streams, bugref:7673

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 15.0 KB
Line 
1/** @file
2 * VaKit audio driver -- host backend for dumping and injecting audio data
3 * from/to the device emulation.
4 */
5
6/*
7 * Copyright (C) 2016-2017 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#include <iprt/alloc.h>
20#include <iprt/uuid.h> /* For PDMIBASE_2_PDMDRV. */
21
22#define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
23#include <VBox/log.h>
24#include <VBox/vmm/pdmaudioifs.h>
25
26#include "DrvAudio.h"
27#include "VBoxDD.h"
28
29
30/**
31 * Structure for keeping a VAKIT input/output stream.
32 */
33typedef struct VAKITAUDIOSTREAM
34{
35 /** The stream's acquired configuration. */
36 PPDMAUDIOSTREAMCFG pCfg;
37 /** Audio file to dump output to or read input from. */
38 PDMAUDIOFILE File;
39 /** Text file to store timing of audio buffers submittions**/
40 RTFILE hFileTiming;
41 /** Timestamp of the first play or record request**/
42 uint64_t tsStarted;
43 /** Total number of samples played or recorded so far**/
44 uint32_t uSamplesSinceStarted;
45 union
46 {
47 struct
48 {
49 /** Timestamp of last captured samples. */
50 uint64_t tsLastCaptured;
51 } In;
52 struct
53 {
54 /** Timestamp of last played samples. */
55 uint64_t tsLastPlayed;
56 uint8_t *pu8PlayBuffer;
57 uint32_t cbPlayBuffer;
58 } Out;
59 };
60} VAKITAUDIOSTREAM, *PVAKITAUDIOSTREAM;
61
62/**
63 * VAKIT audio driver instance data.
64 * @implements PDMIAUDIOCONNECTOR
65 */
66typedef struct DRVHOSTVAKITAUDIO
67{
68 /** Pointer to the driver instance structure. */
69 PPDMDRVINS pDrvIns;
70 /** Pointer to host audio interface. */
71 PDMIHOSTAUDIO IHostAudio;
72} DRVHOSTVAKITAUDIO, *PDRVHOSTVAKITAUDIO;
73
74/*******************************************PDM_AUDIO_DRIVER******************************/
75
76
77/**
78 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetConfig}
79 */
80static DECLCALLBACK(int) drvHostVaKitAudioGetConfig(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pBackendCfg)
81{
82 RT_NOREF(pInterface);
83 AssertPtrReturn(pBackendCfg, VERR_INVALID_POINTER);
84
85 pBackendCfg->cbStreamOut = sizeof(VAKITAUDIOSTREAM);
86 pBackendCfg->cbStreamIn = sizeof(VAKITAUDIOSTREAM);
87
88 pBackendCfg->cMaxStreamsOut = 1; /* Output */
89 pBackendCfg->cMaxStreamsIn = 2; /* Line input + microphone input. */
90
91 return VINF_SUCCESS;
92}
93
94
95/**
96 * @interface_method_impl{PDMIHOSTAUDIO,pfnInit}
97 */
98static DECLCALLBACK(int) drvHostVaKitAudioInit(PPDMIHOSTAUDIO pInterface)
99{
100 RT_NOREF(pInterface);
101
102 LogFlowFuncLeaveRC(VINF_SUCCESS);
103 return VINF_SUCCESS;
104}
105
106
107/**
108 * @interface_method_impl{PDMIHOSTAUDIO,pfnShutdown}
109 */
110static DECLCALLBACK(void) drvHostVaKitAudioShutdown(PPDMIHOSTAUDIO pInterface)
111{
112 RT_NOREF(pInterface);
113}
114
115
116/**
117 * @interface_method_impl{PDMIHOSTAUDIO,pfnGetStatus}
118 */
119static DECLCALLBACK(PDMAUDIOBACKENDSTS) drvHostVaKitAudioGetStatus(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
120{
121 RT_NOREF(enmDir);
122 AssertPtrReturn(pInterface, PDMAUDIOBACKENDSTS_UNKNOWN);
123
124 return PDMAUDIOBACKENDSTS_RUNNING;
125}
126
127
128static int debugCreateStreamIn(PDRVHOSTVAKITAUDIO pDrv, PVAKITAUDIOSTREAM pStreamDbg,
129 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
130{
131 RT_NOREF(pDrv, pStreamDbg, pCfgReq);
132
133 if (pCfgAcq)
134 pCfgAcq->cSampleBufferHint = _1K;
135
136 return VINF_SUCCESS;
137}
138
139
140static int debugCreateStreamOut(PDRVHOSTVAKITAUDIO pDrv, PVAKITAUDIOSTREAM pStreamDbg,
141 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
142{
143 RT_NOREF(pDrv);
144
145 int rc = VINF_SUCCESS;
146
147 pStreamDbg->tsStarted = 0;
148 pStreamDbg->uSamplesSinceStarted = 0;
149 pStreamDbg->Out.tsLastPlayed = 0;
150 pStreamDbg->Out.cbPlayBuffer = 16 * _1K * PDMAUDIOSTREAMCFG_S2B(pCfgReq, 1); /** @todo Make this configurable? */
151 pStreamDbg->Out.pu8PlayBuffer = (uint8_t *)RTMemAlloc(pStreamDbg->Out.cbPlayBuffer);
152 if (!pStreamDbg->Out.pu8PlayBuffer)
153 rc = VERR_NO_MEMORY;
154
155 if (RT_SUCCESS(rc))
156 {
157 char szTemp[RTPATH_MAX];
158 rc = RTPathTemp(szTemp, sizeof(szTemp));
159
160 RTPathAppend(szTemp, sizeof(szTemp), "VBoxAudioValKit");
161
162 if (RT_SUCCESS(rc))
163 {
164 char szFile[RTPATH_MAX];
165 rc = DrvAudioHlpGetFileName(szFile, RT_ELEMENTS(szFile), szTemp, NULL, PDMAUDIOFILETYPE_WAV);
166 if (RT_SUCCESS(rc))
167 {
168 LogFlowFunc(("%s\n", szFile));
169 rc = DrvAudioHlpWAVFileOpen(&pStreamDbg->File, szFile,
170 RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE,
171 &pCfgReq->Props, PDMAUDIOFILEFLAG_NONE);
172 if (RT_FAILURE(rc))
173 LogRel(("VaKitAudio: Creating output file '%s' failed with %Rrc\n", szFile, rc));
174
175 RTStrCat(szFile, sizeof(szFile), ".timing");
176 rc = RTFileOpen(&pStreamDbg->hFileTiming, szFile, RTFILE_O_WRITE | RTFILE_O_DENY_WRITE | RTFILE_O_CREATE_REPLACE);
177
178 if (RT_FAILURE(rc))
179 {
180 LogRel(("VaKitAudio: Creating output file '%s' failed with %Rrc\n", szFile, rc));
181 }
182 else
183 {
184 size_t cch;
185 char szTimingInfo[128];
186 cch = RTStrPrintf(szTimingInfo, sizeof(szTimingInfo), "# %dHz %dch %dbps\n",
187 pCfgReq->Props.uHz, pCfgReq->Props.cChannels, pCfgReq->Props.cBits);
188
189 RTFileWrite(pStreamDbg->hFileTiming, szTimingInfo, cch, NULL);
190 }
191 }
192 else
193 LogRel(("VaKitAudio: Unable to build file name for temp dir '%s': %Rrc\n", szTemp, rc));
194 }
195 else
196 LogRel(("VaKitAudio: Unable to retrieve temp dir: %Rrc\n", rc));
197 }
198
199 if (RT_SUCCESS(rc))
200 {
201 if (pCfgAcq)
202 pCfgAcq->cSampleBufferHint = PDMAUDIOSTREAMCFG_B2S(pCfgAcq, pStreamDbg->Out.cbPlayBuffer);
203 }
204
205 return rc;
206}
207
208
209/**
210 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCreate}
211 */
212static DECLCALLBACK(int) drvHostVaKitAudioStreamCreate(PPDMIHOSTAUDIO pInterface,
213 PPDMAUDIOBACKENDSTREAM pStream,
214 PPDMAUDIOSTREAMCFG pCfgReq, PPDMAUDIOSTREAMCFG pCfgAcq)
215{
216 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
217 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
218 AssertPtrReturn(pCfgReq, VERR_INVALID_POINTER);
219 AssertPtrReturn(pCfgAcq, VERR_INVALID_POINTER);
220
221 PDRVHOSTVAKITAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTVAKITAUDIO, IHostAudio);
222 PVAKITAUDIOSTREAM pStreamDbg = (PVAKITAUDIOSTREAM)pStream;
223
224 int rc;
225 if (pCfgReq->enmDir == PDMAUDIODIR_IN)
226 rc = debugCreateStreamIn( pDrv, pStreamDbg, pCfgReq, pCfgAcq);
227 else
228 rc = debugCreateStreamOut(pDrv, pStreamDbg, pCfgReq, pCfgAcq);
229
230 if (RT_SUCCESS(rc))
231 {
232 pStreamDbg->pCfg = DrvAudioHlpStreamCfgDup(pCfgAcq);
233 if (!pStreamDbg->pCfg)
234 rc = VERR_NO_MEMORY;
235 }
236
237 return rc;
238}
239
240
241/**
242 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamPlay}
243 */
244static DECLCALLBACK(int) drvHostVaKitAudioStreamPlay(PPDMIHOSTAUDIO pInterface,
245 PPDMAUDIOBACKENDSTREAM pStream, const void *pvBuf, uint32_t cbBuf,
246 uint32_t *pcbWritten)
247{
248 PDRVHOSTVAKITAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTVAKITAUDIO, IHostAudio);
249 PVAKITAUDIOSTREAM pStreamDbg = (PVAKITAUDIOSTREAM)pStream;
250 RT_NOREF(pDrv);
251
252 uint64_t tsSinceStart;
253 size_t cch;
254 char szTimingInfo[128];
255
256 if (pStreamDbg->tsStarted == 0)
257 {
258 pStreamDbg->tsStarted = RTTimeNanoTS();
259 tsSinceStart = 0;
260 }
261 else
262 {
263 tsSinceStart = RTTimeNanoTS() - pStreamDbg->tsStarted;
264 }
265
266 // Microseconds are used everythere below
267 uint32_t sBuf = cbBuf >> pStreamDbg->pCfg->Props.cShift;
268 cch = RTStrPrintf(szTimingInfo, sizeof(szTimingInfo), "%d %d %d %d\n",
269 (uint32_t)(tsSinceStart / 1000), // Host time elapsed since Guest submitted the first buffer for playback
270 (uint32_t)(pStreamDbg->uSamplesSinceStarted * 1.0E6 / pStreamDbg->pCfg->Props.uHz), // how long all the samples submitted previously were played
271 (uint32_t)(sBuf * 1.0E6 / pStreamDbg->pCfg->Props.uHz), // how long a new uSamplesReady samples should\will be played
272 sBuf);
273 RTFileWrite(pStreamDbg->hFileTiming, szTimingInfo, cch, NULL);
274 pStreamDbg->uSamplesSinceStarted += sBuf;
275
276 /* Remember when samples were consumed. */
277 // pStreamDbg->Out.tsLastPlayed = PDMDrvHlpTMGetVirtualTime(pDrv->pDrvIns);;
278
279 int rc2 = DrvAudioHlpWAVFileWrite(&pStreamDbg->File, pvBuf, cbBuf, 0 /* fFlags */);
280 if (RT_FAILURE(rc2))
281 LogRel(("DebugAudio: Writing output failed with %Rrc\n", rc2));
282
283 *pcbWritten = cbBuf;
284
285 return VINF_SUCCESS;
286}
287
288
289/**
290 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamCapture}
291 */
292static DECLCALLBACK(int) drvHostVaKitAudioStreamCapture(PPDMIHOSTAUDIO pInterface,
293 PPDMAUDIOBACKENDSTREAM pStream, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
294{
295 RT_NOREF(pInterface, pStream, pvBuf, cbBuf);
296
297 /* Never capture anything. */
298 if (pcbRead)
299 *pcbRead = 0;
300
301 return VINF_SUCCESS;
302}
303
304
305static int debugDestroyStreamIn(PDRVHOSTVAKITAUDIO pDrv, PVAKITAUDIOSTREAM pStreamDbg)
306{
307 RT_NOREF(pDrv, pStreamDbg);
308 return VINF_SUCCESS;
309}
310
311
312static int debugDestroyStreamOut(PDRVHOSTVAKITAUDIO pDrv, PVAKITAUDIOSTREAM pStreamDbg)
313{
314 RT_NOREF(pDrv);
315
316 if (pStreamDbg->Out.pu8PlayBuffer)
317 {
318 RTMemFree(pStreamDbg->Out.pu8PlayBuffer);
319 pStreamDbg->Out.pu8PlayBuffer = NULL;
320 }
321
322 size_t cbDataSize = DrvAudioHlpWAVFileGetDataSize(&pStreamDbg->File);
323
324 int rc = DrvAudioHlpWAVFileClose(&pStreamDbg->File);
325 RTFileClose(pStreamDbg->hFileTiming);
326
327 if (RT_SUCCESS(rc))
328 {
329 /* Delete the file again if nothing but the header was written to it. */
330 bool fDeleteEmptyFiles = true; /** @todo Make deletion configurable? */
331
332 if ( !cbDataSize
333 && fDeleteEmptyFiles)
334 {
335 char szFile[RTPATH_MAX];
336
337 RTStrCopy(szFile, sizeof(szFile), pStreamDbg->File.szName);
338 RTFileDelete(szFile);
339
340 RTStrCat(szFile, sizeof(szFile), ".timing");
341 RTFileDelete(szFile);
342 }
343 else
344 LogRel(("VaKitAudio: Created output file '%s' (%zu bytes)\n", pStreamDbg->File.szName, cbDataSize));
345 }
346
347 return rc;
348}
349
350
351static DECLCALLBACK(int) drvHostVaKitAudioStreamDestroy(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
352{
353 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
354
355 PDRVHOSTVAKITAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTVAKITAUDIO, IHostAudio);
356 PVAKITAUDIOSTREAM pStreamDbg = (PVAKITAUDIOSTREAM)pStream;
357
358 if (!pStreamDbg->pCfg) /* Not (yet) configured? Skip. */
359 return VINF_SUCCESS;
360
361 int rc;
362 if (pStreamDbg->pCfg->enmDir == PDMAUDIODIR_IN)
363 rc = debugDestroyStreamIn (pDrv, pStreamDbg);
364 else
365 rc = debugDestroyStreamOut(pDrv, pStreamDbg);
366
367 if (RT_SUCCESS(rc))
368 {
369 DrvAudioHlpStreamCfgFree(pStreamDbg->pCfg);
370 pStreamDbg->pCfg = NULL;
371 }
372
373 return rc;
374}
375
376static DECLCALLBACK(int) drvHostVaKitAudioStreamControl(PPDMIHOSTAUDIO pInterface,
377 PPDMAUDIOBACKENDSTREAM pStream, PDMAUDIOSTREAMCMD enmStreamCmd)
378{
379 RT_NOREF(enmStreamCmd);
380 AssertPtrReturn(pInterface, VERR_INVALID_POINTER);
381 AssertPtrReturn(pStream, VERR_INVALID_POINTER);
382
383 return VINF_SUCCESS;
384}
385
386/**
387 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetReadable}
388 */
389static DECLCALLBACK(uint32_t) drvHostVaKitAudioStreamGetReadable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
390{
391 RT_NOREF(pInterface, pStream);
392
393 return UINT32_MAX;
394}
395
396
397/**
398 * @interface_method_impl{PDMIHOSTAUDIO,pfnStreamGetWritable}
399 */
400static DECLCALLBACK(uint32_t) drvHostVaKitAudioStreamGetWritable(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
401{
402 RT_NOREF(pInterface, pStream);
403
404 return UINT32_MAX;
405}
406
407static DECLCALLBACK(PDMAUDIOSTRMSTS) drvHostVaKitAudioStreamGetStatus(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
408{
409 RT_NOREF(pInterface, pStream);
410
411 return (PDMAUDIOSTRMSTS_FLAG_INITIALIZED | PDMAUDIOSTRMSTS_FLAG_ENABLED);
412}
413
414static DECLCALLBACK(int) drvHostVaKitAudioStreamIterate(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDSTREAM pStream)
415{
416 RT_NOREF(pInterface, pStream);
417 return VINF_SUCCESS;
418}
419
420
421/**
422 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
423 */
424static DECLCALLBACK(void *) drvHostVaKitAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
425{
426 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
427 PDRVHOSTVAKITAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTVAKITAUDIO);
428
429 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
430 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
431 return NULL;
432}
433
434
435/**
436 * Constructs a VaKit audio driver instance.
437 *
438 * @copydoc FNPDMDRVCONSTRUCT
439 */
440static DECLCALLBACK(int) drvHostVaKitAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
441{
442 RT_NOREF(pCfg, fFlags);
443 PDMDRV_CHECK_VERSIONS_RETURN(pDrvIns);
444 PDRVHOSTVAKITAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTVAKITAUDIO);
445 LogRel(("Audio: Initializing VAKIT driver\n"));
446
447 /*
448 * Init the static parts.
449 */
450 pThis->pDrvIns = pDrvIns;
451 /* IBase */
452 pDrvIns->IBase.pfnQueryInterface = drvHostVaKitAudioQueryInterface;
453 /* IHostAudio */
454 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostVaKitAudio);
455
456 return VINF_SUCCESS;
457}
458
459/**
460 * Char driver registration record.
461 */
462const PDMDRVREG g_DrvHostValidationKitAudio =
463{
464 /* u32Version */
465 PDM_DRVREG_VERSION,
466 /* szName */
467 "ValidationKitAudio",
468 /* szRCMod */
469 "",
470 /* szR0Mod */
471 "",
472 /* pszDescription */
473 "ValidationKitAudio audio host driver",
474 /* fFlags */
475 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
476 /* fClass. */
477 PDM_DRVREG_CLASS_AUDIO,
478 /* cMaxInstances */
479 ~0U,
480 /* cbInstance */
481 sizeof(DRVHOSTVAKITAUDIO),
482 /* pfnConstruct */
483 drvHostVaKitAudioConstruct,
484 /* pfnDestruct */
485 NULL,
486 /* pfnRelocate */
487 NULL,
488 /* pfnIOCtl */
489 NULL,
490 /* pfnPowerOn */
491 NULL,
492 /* pfnReset */
493 NULL,
494 /* pfnSuspend */
495 NULL,
496 /* pfnResume */
497 NULL,
498 /* pfnAttach */
499 NULL,
500 /* pfnDetach */
501 NULL,
502 /* pfnPowerOff */
503 NULL,
504 /* pfnSoftReset */
505 NULL,
506 /* u32EndVersion */
507 PDM_DRVREG_VERSION
508};
509
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