VirtualBox

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

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

Main/AudioDriver: Documentation.

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