VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostNullAudio.cpp@ 55747

Last change on this file since 55747 was 55649, checked in by vboxsync, 10 years ago

DrvHostNullAudio: consume samples at configured rate

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 9.5 KB
Line 
1/* $Id: DrvHostNullAudio.cpp 55649 2015-05-04 14:46:57Z vboxsync $ */
2/** @file
3 * NULL audio driver -- also acts as a fallback if no
4 * other backend is available.
5 */
6
7/*
8 * Copyright (C) 2006-2015 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 * This code is based on: noaudio.c QEMU based code.
20 *
21 * QEMU Timer based audio emulation
22 *
23 * Copyright (c) 2004-2005 Vassili Karpov (malc)
24 *
25 * Permission is hereby granted, free of charge, to any person obtaining a copy
26 * of this software and associated documentation files (the "Software"), to deal
27 * in the Software without restriction, including without limitation the rights
28 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
29 * copies of the Software, and to permit persons to whom the Software is
30 * furnished to do so, subject to the following conditions:
31 *
32 * The above copyright notice and this permission notice shall be included in
33 * all copies or substantial portions of the Software.
34 *
35 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
36 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
37 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
38 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
39 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
40 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
41 * THE SOFTWARE.
42 */
43#include "DrvAudio.h"
44#include "AudioMixBuffer.h"
45
46#include "VBoxDD.h"
47#include "vl_vbox.h"
48
49#include <iprt/alloc.h>
50#include <iprt/uuid.h> /* For PDMIBASE_2_PDMDRV. */
51#include <VBox/vmm/pdmaudioifs.h>
52
53#ifdef LOG_GROUP
54# undef LOG_GROUP
55#endif
56#define LOG_GROUP LOG_GROUP_DEV_AUDIO
57#include <VBox/log.h>
58
59typedef struct NULLAUDIOSTREAMOUT
60{
61 /** Note: Always must come first! */
62 PDMAUDIOHSTSTRMOUT hw;
63 uint64_t u64TicksLast;
64} NULLAUDIOSTREAMOUT;
65
66typedef struct NULLAUDIOSTREAMIN
67{
68 /** Note: Always must come first! */
69 PDMAUDIOHSTSTRMIN hw;
70} NULLAUDIOSTREAMIN;
71
72/**
73 * NULL audio driver instance data.
74 * @implements PDMIAUDIOCONNECTOR
75 */
76typedef struct DRVHOSTNULLAUDIO
77{
78 /** Pointer to the driver instance structure. */
79 PPDMDRVINS pDrvIns;
80 /** Pointer to host audio interface. */
81 PDMIHOSTAUDIO IHostAudio;
82} DRVHOSTNULLAUDIO, *PDRVHOSTNULLAUDIO;
83
84/*******************************************PDM_AUDIO_DRIVER******************************/
85
86
87static DECLCALLBACK(int) drvHostNullAudioGetConf(PPDMIHOSTAUDIO pInterface, PPDMAUDIOBACKENDCFG pCfg)
88{
89 NOREF(pInterface);
90 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
91
92 pCfg->cbStreamOut = sizeof(NULLAUDIOSTREAMOUT);
93 pCfg->cbStreamIn = sizeof(NULLAUDIOSTREAMIN);
94
95 pCfg->cMaxHstStrmsOut = 1; /* Output */
96 pCfg->cMaxHstStrmsIn = 2; /* Line input + microphone input. */
97
98 return VINF_SUCCESS;
99}
100
101static DECLCALLBACK(int) drvHostNullAudioInit(PPDMIHOSTAUDIO pInterface)
102{
103 NOREF(pInterface);
104
105 return VINF_SUCCESS;
106}
107
108static DECLCALLBACK(int) drvHostNullAudioInitIn(PPDMIHOSTAUDIO pInterface,
109 PPDMAUDIOHSTSTRMIN pHstStrmIn, PPDMAUDIOSTREAMCFG pCfg,
110 PDMAUDIORECSOURCE enmRecSource,
111 uint32_t *pcSamples)
112{
113 NOREF(pInterface);
114 NOREF(enmRecSource);
115
116 /* Just adopt the wanted stream configuration. */
117 int rc = drvAudioStreamCfgToProps(pCfg, &pHstStrmIn->Props);
118 if (RT_SUCCESS(rc))
119 {
120 if (pcSamples)
121 *pcSamples = _1K;
122 }
123
124 return VINF_SUCCESS;
125}
126
127static DECLCALLBACK(int) drvHostNullAudioInitOut(PPDMIHOSTAUDIO pInterface,
128 PPDMAUDIOHSTSTRMOUT pHstStrmOut, PPDMAUDIOSTREAMCFG pCfg,
129 uint32_t *pcSamples)
130{
131 NOREF(pInterface);
132
133 /* Just adopt the wanted stream configuration. */
134 int rc = drvAudioStreamCfgToProps(pCfg, &pHstStrmOut->Props);
135 if (RT_SUCCESS(rc))
136 {
137 NULLAUDIOSTREAMOUT *pNullStrmOut = (NULLAUDIOSTREAMOUT *)pHstStrmOut;
138 pNullStrmOut->u64TicksLast = 0;
139 if (pcSamples)
140 *pcSamples = _1K;
141 }
142
143 return rc;
144}
145
146static DECLCALLBACK(bool) drvHostNullAudioIsEnabled(PPDMIHOSTAUDIO pInterface, PDMAUDIODIR enmDir)
147{
148 NOREF(pInterface);
149 NOREF(enmDir);
150 return true; /* Always all enabled. */
151}
152
153static DECLCALLBACK(int) drvHostNullAudioPlayOut(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMOUT pHstStrmOut,
154 uint32_t *pcSamplesPlayed)
155{
156 PDRVHOSTNULLAUDIO pDrv = RT_FROM_MEMBER(pInterface, DRVHOSTNULLAUDIO, IHostAudio);
157 NULLAUDIOSTREAMOUT *pNullStrmOut = (NULLAUDIOSTREAMOUT *)pHstStrmOut;
158
159 /* Consume as many samples as would be played at the current frequency since last call. */
160 uint32_t csLive = drvAudioHstOutSamplesLive(pHstStrmOut, NULL /* pcStreamsLive */);
161 uint64_t u64TicksNow = PDMDrvHlpTMGetVirtualTime(pDrv->pDrvIns);
162 uint64_t u64TicksElapsed = u64TicksNow - pNullStrmOut->u64TicksLast;
163 uint64_t u64TicksFreq = PDMDrvHlpTMGetVirtualFreq(pDrv->pDrvIns);
164
165 /* Remember when samples were consumed. */
166 pNullStrmOut->u64TicksLast = u64TicksNow;
167
168 /* Minimize the rounding error by adding 0.5: samples = int((u64TicksElapsed * samplesFreq) / u64TicksFreq + 0.5).
169 * If rounding is not taken into account then the playback rate will be consistently lower that expected.
170 */
171 uint64_t cSamplesPlayed = (2 * u64TicksElapsed * pHstStrmOut->Props.uHz + u64TicksFreq) / u64TicksFreq / 2;
172
173 /* Don't play more than available. */
174 if (cSamplesPlayed > csLive)
175 cSamplesPlayed = csLive;
176
177 audioMixBufFinish(&pHstStrmOut->MixBuf, cSamplesPlayed);
178
179 if (pcSamplesPlayed)
180 *pcSamplesPlayed = cSamplesPlayed;
181
182 return VINF_SUCCESS;
183}
184
185static DECLCALLBACK(int) drvHostNullAudioCaptureIn(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMIN pHstStrmIn,
186 uint32_t *pcSamplesCaptured)
187{
188 /* Never capture anything. */
189 if (pcSamplesCaptured)
190 *pcSamplesCaptured = 0;
191
192 return VINF_SUCCESS;
193}
194
195static DECLCALLBACK(int) drvHostNullAudioControlIn(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMIN pHstStrmIn,
196 PDMAUDIOSTREAMCMD enmStreamCmd)
197{
198 NOREF(pInterface);
199 NOREF(pHstStrmIn);
200 NOREF(enmStreamCmd);
201
202 return VINF_SUCCESS;
203}
204
205static DECLCALLBACK(int) drvHostNullAudioControlOut(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMOUT pHstStrmOut,
206 PDMAUDIOSTREAMCMD enmStreamCmd)
207{
208 NOREF(pInterface);
209 NOREF(pHstStrmOut);
210 NOREF(enmStreamCmd);
211
212 return VINF_SUCCESS;
213}
214
215static DECLCALLBACK(int) drvHostNullAudioFiniIn(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMIN pHstStrmIn)
216{
217 return VINF_SUCCESS;
218}
219
220static DECLCALLBACK(int) drvHostNullAudioFiniOut(PPDMIHOSTAUDIO pInterface, PPDMAUDIOHSTSTRMOUT pHstStrmOut)
221{
222 return VINF_SUCCESS;
223}
224
225/**
226 * @interface_method_impl{PDMIBASE,pfnQueryInterface}
227 */
228static DECLCALLBACK(void *) drvHostNullAudioQueryInterface(PPDMIBASE pInterface, const char *pszIID)
229{
230 PPDMDRVINS pDrvIns = PDMIBASE_2_PDMDRV(pInterface);
231 PDRVHOSTNULLAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTNULLAUDIO);
232
233 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIBASE, &pDrvIns->IBase);
234 PDMIBASE_RETURN_INTERFACE(pszIID, PDMIHOSTAUDIO, &pThis->IHostAudio);
235 return NULL;
236}
237
238static DECLCALLBACK(void) drvHostNullAudioShutdown(PPDMIHOSTAUDIO pInterface)
239{
240 NOREF(pInterface);
241}
242
243/**
244 * Constructs a Null audio driver instance.
245 *
246 * @copydoc FNPDMDRVCONSTRUCT
247 */
248static DECLCALLBACK(int) drvHostNullAudioConstruct(PPDMDRVINS pDrvIns, PCFGMNODE pCfg, uint32_t fFlags)
249{
250 AssertPtrReturn(pDrvIns, VERR_INVALID_POINTER);
251 /* pCfg is optional. */
252
253 PDRVHOSTNULLAUDIO pThis = PDMINS_2_DATA(pDrvIns, PDRVHOSTNULLAUDIO);
254 LogRel(("Audio: Initializing NULL driver\n"));
255
256 /*
257 * Init the static parts.
258 */
259 pThis->pDrvIns = pDrvIns;
260 /* IBase */
261 pDrvIns->IBase.pfnQueryInterface = drvHostNullAudioQueryInterface;
262 /* IHostAudio */
263 PDMAUDIO_IHOSTAUDIO_CALLBACKS(drvHostNullAudio);
264
265 return VINF_SUCCESS;
266}
267
268/**
269 * Char driver registration record.
270 */
271const PDMDRVREG g_DrvHostNullAudio =
272{
273 /* u32Version */
274 PDM_DRVREG_VERSION,
275 /* szName */
276 "NullAudio",
277 /* szRCMod */
278 "",
279 /* szR0Mod */
280 "",
281 /* pszDescription */
282 "NULL audio host driver",
283 /* fFlags */
284 PDM_DRVREG_FLAGS_HOST_BITS_DEFAULT,
285 /* fClass. */
286 PDM_DRVREG_CLASS_AUDIO,
287 /* cMaxInstances */
288 ~0U,
289 /* cbInstance */
290 sizeof(DRVHOSTNULLAUDIO),
291 /* pfnConstruct */
292 drvHostNullAudioConstruct,
293 /* pfnDestruct */
294 NULL,
295 /* pfnRelocate */
296 NULL,
297 /* pfnIOCtl */
298 NULL,
299 /* pfnPowerOn */
300 NULL,
301 /* pfnReset */
302 NULL,
303 /* pfnSuspend */
304 NULL,
305 /* pfnResume */
306 NULL,
307 /* pfnAttach */
308 NULL,
309 /* pfnDetach */
310 NULL,
311 /* pfnPowerOff */
312 NULL,
313 /* pfnSoftReset */
314 NULL,
315 /* u32EndVersion */
316 PDM_DRVREG_VERSION
317};
318
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