VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/AudioDriver.cpp@ 70644

最後變更 在這個檔案從70644是 70644,由 vboxsync 提交於 7 年 前

Audio/Main: More code needed for attaching / detaching host backends at runtime.

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 9.5 KB
 
1/* $Id: AudioDriver.cpp 70644 2018-01-19 12:20:33Z vboxsync $ */
2/** @file
3 * VirtualBox audio base class for Main audio drivers.
4 */
5
6/*
7 * Copyright (C) 2018 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.alldomusa.eu.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 "LoggingNew.h"
24
25#include <VBox/log.h>
26#include <VBox/vmm/cfgm.h>
27#include <VBox/vmm/pdmaudioifs.h>
28#include <VBox/vmm/pdmapi.h>
29#include <VBox/vmm/pdmdrv.h>
30
31#include "AudioDriver.h"
32#include "ConsoleImpl.h"
33
34AudioDriver::AudioDriver(Console *pConsole)
35 : mpConsole(pConsole)
36 , mfAttached(false)
37{
38}
39
40AudioDriver::~AudioDriver(void)
41{
42}
43
44
45/**
46 * Initializes the audio driver with a certain (device) configuration.
47 *
48 * @returns VBox status code.
49 * @param pCfg Audio driver configuration to use.
50 */
51int AudioDriver::InitializeConfig(AudioDriverCfg *pCfg)
52{
53 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
54
55 /* Sanity. */
56 AssertReturn(pCfg->strDev.isNotEmpty(), VERR_INVALID_PARAMETER);
57 AssertReturn(pCfg->uLUN != UINT8_MAX, VERR_INVALID_PARAMETER);
58 AssertReturn(pCfg->strName.isNotEmpty(), VERR_INVALID_PARAMETER);
59
60 /* Apply configuration. */
61 mCfg = *pCfg;
62
63 return VINF_SUCCESS;
64}
65
66
67/**
68 * Attaches the driver via EMT, if configured.
69 *
70 * @returns IPRT status code.
71 * @param pUVM The user mode VM handle for talking to EMT.
72 * @param pAutoLock The callers auto lock instance. Can be NULL if
73 * not locked.
74 */
75int AudioDriver::doAttachDriverViaEmt(PUVM pUVM, util::AutoWriteLock *pAutoLock)
76{
77 if (!isConfigured())
78 return VINF_SUCCESS;
79
80 PVMREQ pReq;
81 int vrc = VMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
82 (PFNRT)attachDriverOnEmt, 1, this);
83 if (vrc == VERR_TIMEOUT)
84 {
85 /* Release the lock before a blocking VMR3* call (EMT might wait for it, @bugref{7648})! */
86 if (pAutoLock)
87 pAutoLock->release();
88
89 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
90
91 if (pAutoLock)
92 pAutoLock->acquire();
93 }
94
95 AssertRC(vrc);
96 VMR3ReqFree(pReq);
97
98 return vrc;
99}
100
101
102/**
103 * Configures the audio driver (to CFGM) and attaches it to the audio chain.
104 * Does nothing if the audio driver already is attached.
105 *
106 * @returns VBox status code.
107 * @param pThis Audio driver to detach.
108 */
109/* static */
110DECLCALLBACK(int) AudioDriver::attachDriverOnEmt(AudioDriver *pThis)
111{
112 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
113
114 Console::SafeVMPtrQuiet ptrVM(pThis->mpConsole);
115 Assert(ptrVM.isOk());
116
117 if (pThis->mfAttached) /* Already attached? Bail out. */
118 {
119 LogFunc(("%s: Already attached\n", pThis->mCfg.strName.c_str()));
120 return VINF_SUCCESS;
121 }
122
123 AudioDriverCfg *pCfg = &pThis->mCfg;
124
125 LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
126 pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN));
127
128 int rc = pThis->configure(pCfg->uLUN, true /* Attach */);
129 if (RT_SUCCESS(rc))
130 rc = PDMR3DriverAttach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN, 0 /* fFlags */,
131 NULL /* ppBase */);
132 if (RT_SUCCESS(rc))
133 {
134 pThis->mfAttached = true;
135 LogRel2(("%s: Driver attached (LUN #%u)\n", pCfg->strName.c_str(), pCfg->uLUN));
136 }
137 else
138 LogRel(("%s: Failed to attach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), rc));
139
140 LogFunc(("Returning %Rrc\n", rc));
141 return rc;
142}
143
144
145/**
146 * Detatches the driver via EMT, if configured.
147 *
148 * @returns IPRT status code.
149 * @param pUVM The user mode VM handle for talking to EMT.
150 * @param pAutoLock The callers auto lock instance. Can be NULL if
151 * not locked.
152 */
153int AudioDriver::doDetachDriverViaEmt(PUVM pUVM, util::AutoWriteLock *pAutoLock)
154{
155 if (!isConfigured())
156 return VINF_SUCCESS;
157
158 PVMREQ pReq;
159 int vrc = VMR3ReqCallU(pUVM, VMCPUID_ANY, &pReq, 0 /* no wait! */, VMREQFLAGS_VBOX_STATUS,
160 (PFNRT)detachDriverOnEmt, 1, this);
161 if (vrc == VERR_TIMEOUT)
162 {
163 /* Release the lock before a blocking VMR3* call (EMT might wait for it, @bugref{7648})! */
164 if (pAutoLock)
165 pAutoLock->release();
166
167 vrc = VMR3ReqWait(pReq, RT_INDEFINITE_WAIT);
168
169 if (pAutoLock)
170 pAutoLock->acquire();
171 }
172
173 AssertRC(vrc);
174 VMR3ReqFree(pReq);
175
176 return vrc;
177}
178
179
180/**
181 * Detaches an already attached audio driver from the audio chain.
182 * Does nothing if the audio driver already is detached or not attached.
183 *
184 * @returns VBox status code.
185 * @param pThis Audio driver to detach.
186 */
187/* static */
188DECLCALLBACK(int) AudioDriver::detachDriverOnEmt(AudioDriver *pThis)
189{
190 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
191
192 if (!pThis->mfAttached) /* Not attached? Bail out. */
193 {
194 LogFunc(("%s: Not attached\n", pThis->mCfg.strName.c_str()));
195 return VINF_SUCCESS;
196 }
197
198 Console::SafeVMPtrQuiet ptrVM(pThis->mpConsole);
199 Assert(ptrVM.isOk());
200
201 AudioDriverCfg *pCfg = &pThis->mCfg;
202
203 Assert(pCfg->uLUN != UINT8_MAX);
204
205 LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
206 pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN));
207
208 int rc = PDMR3DeviceDetach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pCfg->uLUN, 0 /* fFlags */);
209 if (RT_SUCCESS(rc))
210 rc = pThis->configure(pCfg->uLUN, false /* Detach */);
211
212 if (RT_SUCCESS(rc))
213 {
214 pThis->mfAttached = false;
215 LogRel2(("%s: Driver detached\n", pCfg->strName.c_str()));
216 }
217 else
218 LogRel(("%s: Failed to detach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), rc));
219
220 LogFunc(("Returning %Rrc\n", rc));
221 return rc;
222}
223
224/**
225 * Configures the audio driver via CFGM.
226 *
227 * @returns VBox status code.
228 * @param uLUN LUN to attach driver to.
229 * @param fAttach Whether to attach or detach the driver configuration to CFGM.
230 *
231 * @thread EMT
232 */
233int AudioDriver::configure(unsigned uLUN, bool fAttach)
234{
235 Console::SafeVMPtrQuiet ptrVM(mpConsole);
236 Assert(ptrVM.isOk());
237
238 PUVM pUVM = ptrVM.rawUVM();
239 AssertPtr(pUVM);
240
241 PCFGMNODE pRoot = CFGMR3GetRootU(pUVM);
242 AssertPtr(pRoot);
243 PCFGMNODE pDev0 = CFGMR3GetChildF(pRoot, "Devices/%s/%u/", mCfg.strDev.c_str(), mCfg.uInst);
244
245 if (!pDev0) /* No audio device configured? Bail out. */
246 {
247 LogRel2(("%s: No audio device configured, skipping to attach driver\n", mCfg.strName.c_str()));
248 return VINF_SUCCESS;
249 }
250
251 int rc = VINF_SUCCESS;
252
253 PCFGMNODE pDevLun = CFGMR3GetChildF(pDev0, "LUN#%u/", uLUN);
254
255 if (fAttach)
256 {
257 do
258 {
259 AssertMsgBreakStmt(pDevLun, ("%s: Device LUN #%u not found\n", mCfg.strName.c_str(), uLUN), rc = VERR_NOT_FOUND);
260
261 LogRel2(("%s: Configuring audio driver (to LUN #%u)\n", mCfg.strName.c_str(), uLUN));
262
263 PCFGMNODE pLunCfg = CFGMR3GetChild(pDevLun, "Config");
264 AssertBreakStmt(pLunCfg, rc = VERR_NOT_FOUND);
265
266 rc = CFGMR3InsertStringF(pLunCfg, "DriverName", "%s", mCfg.strName.c_str()); AssertRCBreak(rc);
267
268 rc = CFGMR3InsertInteger(pLunCfg, "InputEnabled", 0); /* Play safe by default. */ AssertRCBreak(rc);
269 rc = CFGMR3InsertInteger(pLunCfg, "OutputEnabled", 1); AssertRCBreak(rc);
270
271 PCFGMNODE pAttachedDriver, pAttachedDriverCfg;
272 rc = CFGMR3InsertNode(pDevLun, "AttachedDriver", &pAttachedDriver); AssertRCBreak(rc);
273 rc = CFGMR3InsertStringF(pAttachedDriver, "Driver", "%s", mCfg.strName.c_str()); AssertRCBreak(rc);
274 rc = CFGMR3InsertNode(pAttachedDriver, "Config", &pAttachedDriverCfg); AssertRCBreak(rc);
275
276 /* Call the (virtual) method for driver-specific configuration. */
277 rc = configureDriver(pAttachedDriverCfg); AssertRCBreak(rc);
278
279 } while (0);
280 }
281 else /* Detach */
282 {
283 if (pDevLun)
284 {
285 LogRel2(("%s: Unconfiguring audio driver\n", mCfg.strName.c_str()));
286
287 PCFGMNODE pLunCfg = CFGMR3GetChild(pDevLun, "Config");
288 if (pLunCfg)
289 CFGMR3RemoveNode(pLunCfg);
290
291 rc = CFGMR3InsertNode(pDevLun, "Config", &pLunCfg);
292
293 PCFGMNODE pLunAttachedDriver = CFGMR3GetChild(pDevLun, "AttachedDriver");
294 if (pLunAttachedDriver)
295 CFGMR3RemoveNode(pLunAttachedDriver);
296 }
297 }
298
299 if (RT_FAILURE(rc))
300 LogRel(("%s: %s audio driver failed with rc=%Rrc\n",
301 mCfg.strName.c_str(), fAttach ? "Configuring" : "Unconfiguring", rc));
302
303 LogFunc(("Returning %Rrc\n", rc));
304 return rc;
305}
306
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

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