VirtualBox

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

Last change on this file since 70563 was 70563, checked in by vboxsync, 7 years ago

Audio/Main: Also made the video recording driver use the new AudioDriver base class, more bugfixing and clearer abstraction for the AudioDriver API.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.4 KB
Line 
1/* $Id: AudioDriver.cpp 70563 2018-01-12 17:52:10Z 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.215389.xyz. 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 , muLUN(UINT8_MAX)
38{
39}
40
41AudioDriver::~AudioDriver(void)
42{
43}
44
45/**
46 * Returns the next free LUN of the audio device driver
47 * chain.
48 *
49 * @return unsigned Next free LUN in audio device driver chain.
50 */
51unsigned AudioDriver::getFreeLUN(void)
52{
53 Console::SafeVMPtrQuiet ptrVM(mpConsole);
54 Assert(ptrVM.isOk());
55
56 PUVM pUVM = ptrVM.rawUVM();
57 AssertPtr(pUVM);
58
59 unsigned uLUN = 0;
60
61 PCFGMNODE pDevLUN;
62 for (;;)
63 {
64 pDevLUN = CFGMR3GetChildF(CFGMR3GetRootU(pUVM), "Devices/%s/%u/LUN#%u/", mCfg.strDev.c_str(), mCfg.uInst, uLUN);
65 if (!pDevLUN)
66 break;
67 uLUN++;
68 }
69
70 return uLUN;
71}
72
73int AudioDriver::Initialize(AudioDriverCfg *pCfg)
74{
75 AssertPtrReturn(pCfg, VERR_INVALID_POINTER);
76
77 /* Apply configuration. */
78 mCfg = *pCfg;
79
80 return VINF_SUCCESS;
81}
82
83/**
84 * Configures the audio driver (to CFGM) and attaches it to the audio chain.
85 * Does nothing if the audio driver already is attached.
86 *
87 * @returns IPRT status code.
88 * @param pThis Audio driver to detach.
89 */
90/* static */
91DECLCALLBACK(int) AudioDriver::Attach(AudioDriver *pThis)
92{
93 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
94
95 Console::SafeVMPtrQuiet ptrVM(pThis->mpConsole);
96 Assert(ptrVM.isOk());
97
98 int vrc = VINF_SUCCESS;
99
100 if (pThis->mfAttached) /* Already attached? Bail out. */
101 {
102 LogFunc(("%s: Already attached\n", pThis->mCfg.strName.c_str()));
103 return VINF_SUCCESS;
104 }
105
106 AudioDriverCfg *pCfg = &pThis->mCfg;
107
108 unsigned uLUN = pThis->muLUN;
109 if (uLUN == UINT8_MAX) /* No LUN assigned / configured yet? Retrieve it. */
110 uLUN = pThis->getFreeLUN();
111
112 LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
113 pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, uLUN));
114
115 vrc = pThis->configure(uLUN, true /* Attach */);
116 if (RT_SUCCESS(vrc))
117 vrc = PDMR3DeviceAttach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, uLUN, 0 /* fFlags */,
118 NULL /* ppBase */);
119 if (RT_SUCCESS(vrc))
120 {
121 pThis->muLUN = uLUN;
122 pThis->mfAttached = true;
123
124 LogRel2(("%s: Driver attached (LUN #%u)\n", pCfg->strName.c_str(), pThis->muLUN));
125 }
126 else
127 LogRel(("%s: Failed to attach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), vrc));
128
129 return vrc;
130}
131
132/**
133 * Detaches an already attached audio driver from the audio chain.
134 * Does nothing if the audio driver already is detached or not attached.
135 *
136 * @returns IPRT status code.
137 * @param pThis Audio driver to detach.
138 */
139/* static */
140DECLCALLBACK(int) AudioDriver::Detach(AudioDriver *pThis)
141{
142 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
143
144 if (!pThis->mfAttached) /* Not attached? Bail out. */
145 {
146 LogFunc(("%s: Not attached\n", pThis->mCfg.strName.c_str()));
147 return VINF_SUCCESS;
148 }
149
150 Console::SafeVMPtrQuiet ptrVM(pThis->mpConsole);
151 Assert(ptrVM.isOk());
152
153 Assert(pThis->muLUN != UINT8_MAX);
154
155 int vrc = VINF_SUCCESS;
156
157 AudioDriverCfg *pCfg = &pThis->mCfg;
158
159 LogFunc(("strName=%s, strDevice=%s, uInst=%u, uLUN=%u\n",
160 pCfg->strName.c_str(), pCfg->strDev.c_str(), pCfg->uInst, pThis->muLUN));
161
162 vrc = PDMR3DriverDetach(ptrVM.rawUVM(), pCfg->strDev.c_str(), pCfg->uInst, pThis->muLUN, "AUDIO",
163 0 /* iOccurrence */, 0 /* fFlags */);
164 if (RT_SUCCESS(vrc))
165 vrc = pThis->configure(pThis->muLUN, false /* Detach */);
166
167 if (RT_SUCCESS(vrc))
168 {
169 pThis->muLUN = UINT8_MAX;
170 pThis->mfAttached = false;
171
172 LogRel2(("%s: Driver detached\n", pCfg->strName.c_str()));
173 }
174 else
175 LogRel(("%s: Failed to detach audio driver, rc=%Rrc\n", pCfg->strName.c_str(), vrc));
176
177 return vrc;
178}
179
180/**
181 * Configures the audio driver via CFGM.
182 *
183 * @returns VBox status code.
184 * @param uLUN LUN to attach driver to.
185 * @param fAttach Whether to attach or detach the driver configuration to CFGM.
186 *
187 * @thread EMT
188 */
189int AudioDriver::configure(unsigned uLUN, bool fAttach)
190{
191 int rc = VINF_SUCCESS;
192
193 Console::SafeVMPtrQuiet ptrVM(mpConsole);
194 Assert(ptrVM.isOk());
195
196 PUVM pUVM = ptrVM.rawUVM();
197 AssertPtr(pUVM);
198
199 PCFGMNODE pRoot = CFGMR3GetRootU(pUVM);
200 AssertPtr(pRoot);
201 PCFGMNODE pDev0 = CFGMR3GetChildF(pRoot, "Devices/%s/%u/", mCfg.strDev.c_str(), mCfg.uInst);
202 AssertPtr(pDev0);
203
204 PCFGMNODE pDevLun = CFGMR3GetChildF(pDev0, "LUN#%u/", uLUN);
205
206 if (fAttach)
207 {
208 AssertMsg(uLUN != UINT8_MAX, ("%s: LUN is undefined when it must not\n", mCfg.strName.c_str()));
209
210 if (!pDevLun)
211 {
212 LogRel2(("%s: Configuring audio driver (to LUN #%RU8)\n", mCfg.strName.c_str(), uLUN));
213
214 PCFGMNODE pLunL0;
215 CFGMR3InsertNodeF(pDev0, &pLunL0, "LUN#%RU8", uLUN);
216 CFGMR3InsertString(pLunL0, "Driver", "AUDIO");
217
218 PCFGMNODE pLunCfg;
219 CFGMR3InsertNode(pLunL0, "Config", &pLunCfg);
220 CFGMR3InsertStringF(pLunCfg, "DriverName", "%s", mCfg.strName.c_str());
221 CFGMR3InsertInteger(pLunCfg, "InputEnabled", 0); /* Play safe by default. */
222 CFGMR3InsertInteger(pLunCfg, "OutputEnabled", 1);
223
224 PCFGMNODE pLunL1;
225 CFGMR3InsertNode(pLunL0, "AttachedDriver", &pLunL1);
226 CFGMR3InsertStringF(pLunL1, "Driver", "%s", mCfg.strName.c_str());
227
228 CFGMR3InsertNode(pLunL1, "Config", &pLunCfg);
229
230 /* Call the (virtual) method for driver-specific configuration. */
231 configureDriver(pLunCfg);
232 }
233 else
234 rc = VERR_ALREADY_EXISTS;
235 }
236 else /* Detach */
237 {
238 if (pDevLun)
239 {
240 LogRel2(("%s: Unconfiguring audio driver\n", mCfg.strName.c_str()));
241 CFGMR3RemoveNode(pDevLun);
242 }
243 else
244 rc = VERR_NOT_FOUND;
245 }
246
247#ifdef DEBUG_andy
248 CFGMR3Dump(pDev0);
249#endif
250
251 if (RT_FAILURE(rc))
252 LogRel(("%s: %s audio driver failed with rc=%Rrc\n",
253 mCfg.strName.c_str(), fAttach ? "Configuring" : "Unconfiguring", rc));
254
255 return rc;
256}
257
Note: See TracBrowser for help on using the repository browser.

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