VirtualBox

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

Last change on this file since 106378 was 106061, checked in by vboxsync, 2 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/* $Id: AudioSettingsImpl.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VirtualBox COM class implementation - Audio settings for a VM.
4 */
5
6/*
7 * Copyright (C) 2022-2024 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
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
249/**
250 * Returns a (weak) pointer of the audio setting's machine object.
251 *
252 * @returns Wweak pointer of the audio setting's machine object.
253 */
254Machine *AudioSettings::i_getMachine(void)
255{
256 return m->pMachine; // m->pMachine is const, needs no locking
257}
258
259/**
260 * Gets called when the machine object needs to know that audio adapter settings
261 * have been changed.
262 *
263 * @param pAdapter Pointer to audio adapter which has changed.
264 */
265void AudioSettings::i_onAdapterChanged(IAudioAdapter *pAdapter)
266{
267 AssertPtrReturnVoid(pAdapter);
268 m->pMachine->i_onAudioAdapterChange(pAdapter); // m->pMachine is const, needs no locking
269}
270
271/**
272 * Gets called when the machine object needs to know that a host audio device
273 * has been changed.
274 *
275 * @param pDevice Host audio device which has changed.
276 * @param fIsNew Set to \c true if this is a new device (i.e. has not been present before), \c false if not.
277 * @param enmState The current state of the device.
278 * @param pErrInfo Additional error information in case of error(s).
279 */
280void AudioSettings::i_onHostDeviceChanged(IHostAudioDevice *pDevice,
281 bool fIsNew, AudioDeviceState_T enmState, IVirtualBoxErrorInfo *pErrInfo)
282{
283 AssertPtrReturnVoid(pDevice);
284 m->pMachine->i_onHostAudioDeviceChange(pDevice, fIsNew, enmState, pErrInfo); // m->pMachine is const, needs no locking
285}
286
287/**
288 * Gets called when the machine object needs to know that the audio settings
289 * have been changed.
290 */
291void AudioSettings::i_onSettingsChanged(void)
292{
293 AutoWriteLock mlock(m->pMachine COMMA_LOCKVAL_SRC_POS);
294 m->pMachine->i_setModified(Machine::IsModified_AudioSettings);
295 mlock.release();
296}
297
298/**
299 * Loads settings from the given machine node.
300 * May be called once right after this object creation.
301 *
302 * @returns HRESULT
303 * @param data Audio adapter configuration settings to load from.
304 *
305 * @note Locks this object for writing.
306 */
307HRESULT AudioSettings::i_loadSettings(const settings::AudioAdapter &data)
308{
309 AutoCaller autoCaller(this);
310 AssertComRCReturnRC(autoCaller.hrc());
311
312 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
313
314 m->pAdapter->i_loadSettings(data);
315
316 /* Note: The host audio device selection is run-time only, e.g. won't be serialized in the settings! */
317 return S_OK;
318}
319
320/**
321 * Saves audio settings to the given machine node.
322 *
323 * @returns HRESULT
324 * @param data Audio configuration settings to save to.
325 *
326 * @note Locks this object for reading.
327 */
328HRESULT AudioSettings::i_saveSettings(settings::AudioAdapter &data)
329{
330 AutoCaller autoCaller(this);
331 AssertComRCReturnRC(autoCaller.hrc());
332
333 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
334
335 m->pAdapter->i_saveSettings(data);
336
337 /* Note: The host audio device selection is run-time only, e.g. won't be serialized in the settings! */
338 return S_OK;
339}
340
341/**
342 * Copies settings from a given audio settings object.
343 *
344 * This object makes a private copy of data of the original object passed as
345 * an argument.
346 *
347 * @note Locks this object for writing, together with the peer object
348 * represented by @a aThat (locked for reading).
349 *
350 * @param aThat Audio settings to load from.
351 */
352void AudioSettings::i_copyFrom(AudioSettings *aThat)
353{
354 AssertReturnVoid(aThat != NULL);
355
356 /* sanity */
357 AutoCaller autoCaller(this);
358 AssertComRCReturnVoid(autoCaller.hrc());
359
360 /* sanity too */
361 AutoCaller thatCaller(aThat);
362 AssertComRCReturnVoid(thatCaller.hrc());
363
364 /* peer is not modified, lock it for reading (aThat is "master" so locked
365 * first) */
366 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
367 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
368
369 m->pAdapter->i_copyFrom(aThat->m->pAdapter);
370}
371
372/**
373 * Applies default audio settings, based on the given guest OS type.
374 *
375 * @returns HRESULT
376 * @param aGuestOsType Guest OS type to use for basing the default settings on.
377 */
378HRESULT AudioSettings::i_applyDefaults(ComObjPtr<GuestOSType> &aGuestOsType)
379{
380 AutoCaller autoCaller(this);
381 AssertComRCReturnRC(autoCaller.hrc());
382
383 AudioControllerType_T audioController;
384 HRESULT hrc = aGuestOsType->COMGETTER(RecommendedAudioController)(&audioController);
385 if (FAILED(hrc)) return hrc;
386
387 hrc = m->pAdapter->COMSETTER(AudioController)(audioController);
388 if (FAILED(hrc)) return hrc;
389
390 AudioCodecType_T audioCodec;
391 hrc = aGuestOsType->COMGETTER(RecommendedAudioCodec)(&audioCodec);
392 if (FAILED(hrc)) return hrc;
393
394 hrc = m->pAdapter->COMSETTER(AudioCodec)(audioCodec);
395 if (FAILED(hrc)) return hrc;
396
397 hrc = m->pAdapter->COMSETTER(Enabled)(true);
398 if (FAILED(hrc)) return hrc;
399
400 hrc = m->pAdapter->COMSETTER(EnabledOut)(true);
401 if (FAILED(hrc)) return hrc;
402
403 /* Note: We do NOT enable audio input by default due to security reasons!
404 * This always has to be done by the user manually. */
405
406 /* Note: Does not touch the host audio device selection, as this is a run-time only setting. */
407 return S_OK;
408}
409
410/**
411 * Rolls back the current configuration to a former state.
412 *
413 * @note Locks this object for writing.
414 */
415void AudioSettings::i_rollback(void)
416{
417 /* sanity */
418 AutoCaller autoCaller(this);
419 AssertComRCReturnVoid(autoCaller.hrc());
420
421 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
422
423 m->pAdapter->i_rollback();
424
425 /* Note: Does not touch the host audio device selection, as this is a run-time only setting. */
426}
427
428/**
429 * Commits the current settings and propagates those to a peer (if assigned).
430 *
431 * @note Locks this object for writing, together with the peer object (also
432 * for writing) if there is one.
433 */
434void AudioSettings::i_commit(void)
435{
436 /* sanity */
437 AutoCaller autoCaller(this);
438 AssertComRCReturnVoid(autoCaller.hrc());
439
440 m->pAdapter->i_commit();
441
442 /* Note: Does not touch the host audio device selection, as this is a run-time only setting. */
443}
444
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