VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/AudioSettingsImpl.cpp@ 103395

Last change on this file since 103395 was 103395, checked in by vboxsync, 15 months ago

Main/Audio: Added mutable state dependency checks for audio adapter settings. bugref:10600

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.2 KB
Line 
1/* $Id: AudioSettingsImpl.cpp 103395 2024-02-16 09:18:42Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation - Audio settings for a VM.
4 */
5
6/*
7 * Copyright (C) 2022-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.215389.xyz.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#define LOG_GROUP LOG_GROUP_MAIN_AUDIOSETTINGS
29#include "AudioSettingsImpl.h"
30#include "MachineImpl.h"
31
32#include <iprt/cpp/utils.h>
33
34#include <VBox/settings.h>
35
36#include "AutoStateDep.h"
37#include "AutoCaller.h"
38#include "LoggingNew.h"
39
40
41////////////////////////////////////////////////////////////////////////////////
42//
43// AudioSettings private data definition
44//
45////////////////////////////////////////////////////////////////////////////////
46
47struct AudioSettings::Data
48{
49 Data()
50 : pMachine(NULL)
51 { }
52
53 Machine * const pMachine;
54 const ComObjPtr<AudioAdapter> pAdapter;
55 const ComObjPtr<AudioSettings> pPeer;
56};
57
58DEFINE_EMPTY_CTOR_DTOR(AudioSettings)
59
60HRESULT AudioSettings::FinalConstruct()
61{
62 return BaseFinalConstruct();
63}
64
65void AudioSettings::FinalRelease()
66{
67 uninit();
68 BaseFinalRelease();
69}
70
71
72// public initializer/uninitializer for internal purposes only
73////////////////////////////////////////////////////////////////////////////////
74
75/**
76 * Initializes the audio settings object.
77 *
78 * @returns HRESULT
79 * @param aParent Pointer of the parent object.
80 */
81HRESULT AudioSettings::init(Machine *aParent)
82{
83 ComAssertRet(aParent, E_INVALIDARG);
84
85 /* Enclose the state transition NotReady->InInit->Ready */
86 AutoInitSpan autoInitSpan(this);
87 AssertReturn(autoInitSpan.isOk(), E_FAIL);
88
89 m = new Data();
90
91 /* share the parent weakly */
92 unconst(m->pMachine) = aParent;
93
94 /* create the audio adapter object (always present, default is disabled) */
95 unconst(m->pAdapter).createObject();
96 m->pAdapter->init(this);
97
98 /* Confirm a successful initialization */
99 autoInitSpan.setSucceeded();
100
101 return S_OK;
102}
103
104/**
105 * Initializes the audio settings object given another audio settings object
106 * (a kind of copy constructor). This object shares data with
107 * the object passed as an argument.
108 *
109 * @note This object must be destroyed before the original object
110 * it shares data with is destroyed.
111 *
112 * @note Locks @a aThat object for reading.
113 *
114 * @returns HRESULT
115 * @param aParent Pointer of the parent object.
116 * @param aThat Pointer to audio adapter to use settings from.
117 */
118HRESULT AudioSettings::init(Machine *aParent, AudioSettings *aThat)
119{
120 ComAssertRet(aParent && aThat, E_INVALIDARG);
121
122 /* Enclose the state transition NotReady->InInit->Ready */
123 AutoInitSpan autoInitSpan(this);
124 AssertReturn(autoInitSpan.isOk(), E_FAIL);
125
126 m = new Data();
127
128 unconst(m->pMachine) = aParent;
129 unconst(m->pPeer) = aThat;
130
131 AutoCaller thatCaller(aThat);
132 AssertComRCReturnRC(thatCaller.hrc());
133
134 AutoReadLock thatlock(aThat COMMA_LOCKVAL_SRC_POS);
135
136 HRESULT hrc = unconst(m->pAdapter).createObject();
137 ComAssertComRCRet(hrc, hrc);
138 hrc = m->pAdapter->init(this, aThat->m->pAdapter);
139 ComAssertComRCRet(hrc, hrc);
140
141 autoInitSpan.setSucceeded();
142
143 return S_OK;
144}
145
146/**
147 * Initializes the guest object given another guest object
148 * (a kind of copy constructor). This object makes a private copy of data
149 * of the original object passed as an argument.
150 *
151 * @note Locks @a aThat object for reading.
152 *
153 * @returns HRESULT
154 * @param aParent Pointer of the parent object.
155 * @param aThat Pointer to audio adapter to use settings from.
156 */
157HRESULT AudioSettings::initCopy(Machine *aParent, AudioSettings *aThat)
158{
159 ComAssertRet(aParent && aThat, E_INVALIDARG);
160
161 /* Enclose the state transition NotReady->InInit->Ready */
162 AutoInitSpan autoInitSpan(this);
163 AssertReturn(autoInitSpan.isOk(), E_FAIL);
164
165 m = new Data();
166
167 unconst(m->pMachine) = aParent;
168 // pPeer is left null
169
170 AutoReadLock thatlock(aThat COMMA_LOCKVAL_SRC_POS);
171
172 HRESULT hrc = unconst(m->pAdapter).createObject();
173 ComAssertComRCRet(hrc, hrc);
174 hrc = m->pAdapter->init(this);
175 ComAssertComRCRet(hrc, hrc);
176 m->pAdapter->i_copyFrom(aThat->m->pAdapter);
177
178 autoInitSpan.setSucceeded();
179
180 return S_OK;
181}
182
183/**
184 * Uninitializes the instance and sets the ready flag to FALSE.
185 * Called either from FinalRelease() or by the parent when it gets destroyed.
186 */
187void AudioSettings::uninit(void)
188{
189 /* Enclose the state transition Ready->InUninit->NotReady */
190 AutoUninitSpan autoUninitSpan(this);
191 if (autoUninitSpan.uninitDone())
192 return;
193
194 unconst(m->pPeer) = NULL;
195 unconst(m->pMachine) = NULL;
196
197 delete m;
198 m = NULL;
199}
200
201
202// IAudioSettings properties
203////////////////////////////////////////////////////////////////////////////////
204
205HRESULT AudioSettings::getAdapter(ComPtr<IAudioAdapter> &aAdapter)
206{
207 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
208
209 aAdapter = m->pAdapter;
210
211 return S_OK;
212}
213
214
215// IAudioSettings methods
216////////////////////////////////////////////////////////////////////////////////
217
218HRESULT AudioSettings::getHostAudioDevice(AudioDirection_T aUsage, ComPtr<IHostAudioDevice> &aDevice)
219{
220 RT_NOREF(aUsage, aDevice);
221 ReturnComNotImplemented();
222}
223
224HRESULT AudioSettings::setHostAudioDevice(const ComPtr<IHostAudioDevice> &aDevice, AudioDirection_T aUsage)
225{
226 RT_NOREF(aDevice, aUsage);
227 ReturnComNotImplemented();
228}
229
230
231// public methods only for internal purposes
232////////////////////////////////////////////////////////////////////////////////
233
234/**
235 * Determines whether the audio settings currently can be changed or not.
236 *
237 * @returns \c true if the settings can be changed, \c false if not.
238 */
239bool AudioSettings::i_canChangeSettings(void)
240{
241 AutoAnyStateDependency adep(m->pMachine);
242 if (FAILED(adep.hrc()))
243 return false;
244
245 /** @todo Do some more checks here? */
246 return true;
247}
248
249Machine *AudioSettings::i_getMachine(void)
250{
251 return m->pMachine; // m->pMachine is const, needs no locking
252}
253
254/**
255 * Gets called when the machine object needs to know that audio adapter settings
256 * have been changed.
257 *
258 * @param pAdapter Pointer to audio adapter which has changed.
259 */
260void AudioSettings::i_onAdapterChanged(IAudioAdapter *pAdapter)
261{
262 AssertPtrReturnVoid(pAdapter);
263 m->pMachine->i_onAudioAdapterChange(pAdapter); // m->pMachine is const, needs no locking
264}
265
266/**
267 * Gets called when the machine object needs to know that a host audio device
268 * has been changed.
269 *
270 * @param pDevice Host audio device which has changed.
271 * @param fIsNew Set to \c true if this is a new device (i.e. has not been present before), \c false if not.
272 * @param enmState The current state of the device.
273 * @param pErrInfo Additional error information in case of error(s).
274 */
275void AudioSettings::i_onHostDeviceChanged(IHostAudioDevice *pDevice,
276 bool fIsNew, AudioDeviceState_T enmState, IVirtualBoxErrorInfo *pErrInfo)
277{
278 AssertPtrReturnVoid(pDevice);
279 m->pMachine->i_onHostAudioDeviceChange(pDevice, fIsNew, enmState, pErrInfo); // m->pMachine is const, needs no locking
280}
281
282/**
283 * Gets called when the machine object needs to know that the audio settings
284 * have been changed.
285 */
286void AudioSettings::i_onSettingsChanged(void)
287{
288 AutoWriteLock mlock(m->pMachine COMMA_LOCKVAL_SRC_POS);
289 m->pMachine->i_setModified(Machine::IsModified_AudioSettings);
290 mlock.release();
291}
292
293/**
294 * Loads settings from the given machine node.
295 * May be called once right after this object creation.
296 *
297 * @returns HRESULT
298 * @param data Audio adapter configuration settings to load from.
299 *
300 * @note Locks this object for writing.
301 */
302HRESULT AudioSettings::i_loadSettings(const settings::AudioAdapter &data)
303{
304 AutoCaller autoCaller(this);
305 AssertComRCReturnRC(autoCaller.hrc());
306
307 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
308
309 m->pAdapter->i_loadSettings(data);
310
311 /* Note: The host audio device selection is run-time only, e.g. won't be serialized in the settings! */
312 return S_OK;
313}
314
315/**
316 * Saves audio settings to the given machine node.
317 *
318 * @returns HRESULT
319 * @param data Audio configuration settings to save to.
320 *
321 * @note Locks this object for reading.
322 */
323HRESULT AudioSettings::i_saveSettings(settings::AudioAdapter &data)
324{
325 AutoCaller autoCaller(this);
326 AssertComRCReturnRC(autoCaller.hrc());
327
328 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
329
330 m->pAdapter->i_saveSettings(data);
331
332 /* Note: The host audio device selection is run-time only, e.g. won't be serialized in the settings! */
333 return S_OK;
334}
335
336/**
337 * Copies settings from a given audio settings object.
338 *
339 * This object makes a private copy of data of the original object passed as
340 * an argument.
341 *
342 * @note Locks this object for writing, together with the peer object
343 * represented by @a aThat (locked for reading).
344 *
345 * @param aThat Audio settings to load from.
346 */
347void AudioSettings::i_copyFrom(AudioSettings *aThat)
348{
349 AssertReturnVoid(aThat != NULL);
350
351 /* sanity */
352 AutoCaller autoCaller(this);
353 AssertComRCReturnVoid(autoCaller.hrc());
354
355 /* sanity too */
356 AutoCaller thatCaller(aThat);
357 AssertComRCReturnVoid(thatCaller.hrc());
358
359 /* peer is not modified, lock it for reading (aThat is "master" so locked
360 * first) */
361 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
362 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
363
364 m->pAdapter->i_copyFrom(aThat->m->pAdapter);
365}
366
367/**
368 * Applies default audio settings, based on the given guest OS type.
369 *
370 * @returns HRESULT
371 * @param aGuestOsType Guest OS type to use for basing the default settings on.
372 */
373HRESULT AudioSettings::i_applyDefaults(ComObjPtr<GuestOSType> &aGuestOsType)
374{
375 AutoCaller autoCaller(this);
376 AssertComRCReturnRC(autoCaller.hrc());
377
378 AudioControllerType_T audioController;
379 HRESULT hrc = aGuestOsType->COMGETTER(RecommendedAudioController)(&audioController);
380 if (FAILED(hrc)) return hrc;
381
382 hrc = m->pAdapter->COMSETTER(AudioController)(audioController);
383 if (FAILED(hrc)) return hrc;
384
385 AudioCodecType_T audioCodec;
386 hrc = aGuestOsType->COMGETTER(RecommendedAudioCodec)(&audioCodec);
387 if (FAILED(hrc)) return hrc;
388
389 hrc = m->pAdapter->COMSETTER(AudioCodec)(audioCodec);
390 if (FAILED(hrc)) return hrc;
391
392 hrc = m->pAdapter->COMSETTER(Enabled)(true);
393 if (FAILED(hrc)) return hrc;
394
395 hrc = m->pAdapter->COMSETTER(EnabledOut)(true);
396 if (FAILED(hrc)) return hrc;
397
398 /* Note: We do NOT enable audio input by default due to security reasons!
399 * This always has to be done by the user manually. */
400
401 /* Note: Does not touch the host audio device selection, as this is a run-time only setting. */
402 return S_OK;
403}
404
405/**
406 * Rolls back the current configuration to a former state.
407 *
408 * @note Locks this object for writing.
409 */
410void AudioSettings::i_rollback(void)
411{
412 /* sanity */
413 AutoCaller autoCaller(this);
414 AssertComRCReturnVoid(autoCaller.hrc());
415
416 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
417
418 m->pAdapter->i_rollback();
419
420 /* Note: Does not touch the host audio device selection, as this is a run-time only setting. */
421}
422
423/**
424 * Commits the current settings and propagates those to a peer (if assigned).
425 *
426 * @note Locks this object for writing, together with the peer object (also
427 * for writing) if there is one.
428 */
429void AudioSettings::i_commit(void)
430{
431 /* sanity */
432 AutoCaller autoCaller(this);
433 AssertComRCReturnVoid(autoCaller.hrc());
434
435 m->pAdapter->i_commit();
436
437 /* Note: Does not touch the host audio device selection, as this is a run-time only setting. */
438}
439
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