[95423] | 1 | /* $Id: AudioSettingsImpl.cpp 103396 2024-02-16 09:26:21Z vboxsync $ */
|
---|
| 2 | /** @file
|
---|
| 3 | * VirtualBox COM class implementation - Audio settings for a VM.
|
---|
| 4 | */
|
---|
| 5 |
|
---|
| 6 | /*
|
---|
[98103] | 7 | * Copyright (C) 2022-2023 Oracle and/or its affiliates.
|
---|
[95423] | 8 | *
|
---|
[96407] | 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
|
---|
[95423] | 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 |
|
---|
| 47 | struct AudioSettings::Data
|
---|
| 48 | {
|
---|
| 49 | Data()
|
---|
[98028] | 50 | : pMachine(NULL)
|
---|
[95423] | 51 | { }
|
---|
| 52 |
|
---|
[98028] | 53 | Machine * const pMachine;
|
---|
[95423] | 54 | const ComObjPtr<AudioAdapter> pAdapter;
|
---|
| 55 | const ComObjPtr<AudioSettings> pPeer;
|
---|
| 56 | };
|
---|
| 57 |
|
---|
| 58 | DEFINE_EMPTY_CTOR_DTOR(AudioSettings)
|
---|
| 59 |
|
---|
| 60 | HRESULT AudioSettings::FinalConstruct()
|
---|
| 61 | {
|
---|
| 62 | return BaseFinalConstruct();
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | void 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 | *
|
---|
[98028] | 78 | * @returns HRESULT
|
---|
| 79 | * @param aParent Pointer of the parent object.
|
---|
[95423] | 80 | */
|
---|
| 81 | HRESULT 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 */
|
---|
[98028] | 92 | unconst(m->pMachine) = aParent;
|
---|
[95423] | 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
|
---|
[98028] | 110 | * it shares data with is destroyed.
|
---|
[95423] | 111 | *
|
---|
| 112 | * @note Locks @a aThat object for reading.
|
---|
[98028] | 113 | *
|
---|
| 114 | * @returns HRESULT
|
---|
| 115 | * @param aParent Pointer of the parent object.
|
---|
| 116 | * @param aThat Pointer to audio adapter to use settings from.
|
---|
[95423] | 117 | */
|
---|
| 118 | HRESULT 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 |
|
---|
[98028] | 128 | unconst(m->pMachine) = aParent;
|
---|
| 129 | unconst(m->pPeer) = aThat;
|
---|
[95423] | 130 |
|
---|
| 131 | AutoCaller thatCaller(aThat);
|
---|
[98262] | 132 | AssertComRCReturnRC(thatCaller.hrc());
|
---|
[95423] | 133 |
|
---|
| 134 | AutoReadLock thatlock(aThat COMMA_LOCKVAL_SRC_POS);
|
---|
| 135 |
|
---|
[98028] | 136 | HRESULT hrc = unconst(m->pAdapter).createObject();
|
---|
| 137 | ComAssertComRCRet(hrc, hrc);
|
---|
| 138 | hrc = m->pAdapter->init(this, aThat->m->pAdapter);
|
---|
| 139 | ComAssertComRCRet(hrc, hrc);
|
---|
[95423] | 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.
|
---|
[98028] | 152 | *
|
---|
| 153 | * @returns HRESULT
|
---|
| 154 | * @param aParent Pointer of the parent object.
|
---|
| 155 | * @param aThat Pointer to audio adapter to use settings from.
|
---|
[95423] | 156 | */
|
---|
| 157 | HRESULT 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 |
|
---|
[98028] | 167 | unconst(m->pMachine) = aParent;
|
---|
[95423] | 168 | // pPeer is left null
|
---|
| 169 |
|
---|
| 170 | AutoReadLock thatlock(aThat COMMA_LOCKVAL_SRC_POS);
|
---|
| 171 |
|
---|
[95433] | 172 | HRESULT hrc = unconst(m->pAdapter).createObject();
|
---|
| 173 | ComAssertComRCRet(hrc, hrc);
|
---|
| 174 | hrc = m->pAdapter->init(this);
|
---|
| 175 | ComAssertComRCRet(hrc, hrc);
|
---|
[95423] | 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 | */
|
---|
| 187 | void AudioSettings::uninit(void)
|
---|
| 188 | {
|
---|
| 189 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
| 190 | AutoUninitSpan autoUninitSpan(this);
|
---|
| 191 | if (autoUninitSpan.uninitDone())
|
---|
| 192 | return;
|
---|
| 193 |
|
---|
[98028] | 194 | unconst(m->pPeer) = NULL;
|
---|
| 195 | unconst(m->pMachine) = NULL;
|
---|
| 196 |
|
---|
[95423] | 197 | delete m;
|
---|
| 198 | m = NULL;
|
---|
| 199 | }
|
---|
| 200 |
|
---|
| 201 |
|
---|
| 202 | // IAudioSettings properties
|
---|
| 203 | ////////////////////////////////////////////////////////////////////////////////
|
---|
| 204 |
|
---|
| 205 | HRESULT 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 |
|
---|
| 218 | HRESULT AudioSettings::getHostAudioDevice(AudioDirection_T aUsage, ComPtr<IHostAudioDevice> &aDevice)
|
---|
| 219 | {
|
---|
| 220 | RT_NOREF(aUsage, aDevice);
|
---|
| 221 | ReturnComNotImplemented();
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | HRESULT 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 | */
|
---|
| 239 | bool AudioSettings::i_canChangeSettings(void)
|
---|
| 240 | {
|
---|
[98028] | 241 | AutoAnyStateDependency adep(m->pMachine);
|
---|
[98262] | 242 | if (FAILED(adep.hrc()))
|
---|
[95423] | 243 | return false;
|
---|
| 244 |
|
---|
| 245 | /** @todo Do some more checks here? */
|
---|
| 246 | return true;
|
---|
| 247 | }
|
---|
| 248 |
|
---|
[103396] | 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 | */
|
---|
[103395] | 254 | Machine *AudioSettings::i_getMachine(void)
|
---|
| 255 | {
|
---|
| 256 | return m->pMachine; // m->pMachine is const, needs no locking
|
---|
| 257 | }
|
---|
| 258 |
|
---|
[95423] | 259 | /**
|
---|
| 260 | * Gets called when the machine object needs to know that audio adapter settings
|
---|
| 261 | * have been changed.
|
---|
[98028] | 262 | *
|
---|
| 263 | * @param pAdapter Pointer to audio adapter which has changed.
|
---|
[95423] | 264 | */
|
---|
| 265 | void AudioSettings::i_onAdapterChanged(IAudioAdapter *pAdapter)
|
---|
| 266 | {
|
---|
| 267 | AssertPtrReturnVoid(pAdapter);
|
---|
[103395] | 268 | m->pMachine->i_onAudioAdapterChange(pAdapter); // m->pMachine is const, needs no locking
|
---|
[95423] | 269 | }
|
---|
| 270 |
|
---|
| 271 | /**
|
---|
| 272 | * Gets called when the machine object needs to know that a host audio device
|
---|
| 273 | * has been changed.
|
---|
[98028] | 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).
|
---|
[95423] | 279 | */
|
---|
| 280 | void AudioSettings::i_onHostDeviceChanged(IHostAudioDevice *pDevice,
|
---|
| 281 | bool fIsNew, AudioDeviceState_T enmState, IVirtualBoxErrorInfo *pErrInfo)
|
---|
| 282 | {
|
---|
| 283 | AssertPtrReturnVoid(pDevice);
|
---|
[103395] | 284 | m->pMachine->i_onHostAudioDeviceChange(pDevice, fIsNew, enmState, pErrInfo); // m->pMachine is const, needs no locking
|
---|
[95423] | 285 | }
|
---|
| 286 |
|
---|
| 287 | /**
|
---|
| 288 | * Gets called when the machine object needs to know that the audio settings
|
---|
| 289 | * have been changed.
|
---|
| 290 | */
|
---|
| 291 | void AudioSettings::i_onSettingsChanged(void)
|
---|
| 292 | {
|
---|
[98028] | 293 | AutoWriteLock mlock(m->pMachine COMMA_LOCKVAL_SRC_POS);
|
---|
| 294 | m->pMachine->i_setModified(Machine::IsModified_AudioSettings);
|
---|
[95423] | 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 | *
|
---|
[98028] | 302 | * @returns HRESULT
|
---|
| 303 | * @param data Audio adapter configuration settings to load from.
|
---|
[95423] | 304 | *
|
---|
| 305 | * @note Locks this object for writing.
|
---|
| 306 | */
|
---|
| 307 | HRESULT AudioSettings::i_loadSettings(const settings::AudioAdapter &data)
|
---|
| 308 | {
|
---|
| 309 | AutoCaller autoCaller(this);
|
---|
[98262] | 310 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
[95423] | 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 | /**
|
---|
[98028] | 321 | * Saves audio settings to the given machine node.
|
---|
[95423] | 322 | *
|
---|
[98028] | 323 | * @returns HRESULT
|
---|
| 324 | * @param data Audio configuration settings to save to.
|
---|
[95423] | 325 | *
|
---|
| 326 | * @note Locks this object for reading.
|
---|
| 327 | */
|
---|
| 328 | HRESULT AudioSettings::i_saveSettings(settings::AudioAdapter &data)
|
---|
| 329 | {
|
---|
| 330 | AutoCaller autoCaller(this);
|
---|
[98262] | 331 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
[95423] | 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 | /**
|
---|
[98028] | 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 | *
|
---|
[95423] | 347 | * @note Locks this object for writing, together with the peer object
|
---|
[98028] | 348 | * represented by @a aThat (locked for reading).
|
---|
| 349 | *
|
---|
| 350 | * @param aThat Audio settings to load from.
|
---|
[95423] | 351 | */
|
---|
| 352 | void AudioSettings::i_copyFrom(AudioSettings *aThat)
|
---|
| 353 | {
|
---|
| 354 | AssertReturnVoid(aThat != NULL);
|
---|
| 355 |
|
---|
| 356 | /* sanity */
|
---|
| 357 | AutoCaller autoCaller(this);
|
---|
[98262] | 358 | AssertComRCReturnVoid(autoCaller.hrc());
|
---|
[95423] | 359 |
|
---|
| 360 | /* sanity too */
|
---|
| 361 | AutoCaller thatCaller(aThat);
|
---|
[98262] | 362 | AssertComRCReturnVoid(thatCaller.hrc());
|
---|
[95423] | 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 | */
|
---|
| 378 | HRESULT AudioSettings::i_applyDefaults(ComObjPtr<GuestOSType> &aGuestOsType)
|
---|
| 379 | {
|
---|
| 380 | AutoCaller autoCaller(this);
|
---|
[98262] | 381 | AssertComRCReturnRC(autoCaller.hrc());
|
---|
[95423] | 382 |
|
---|
| 383 | AudioControllerType_T audioController;
|
---|
[98288] | 384 | HRESULT hrc = aGuestOsType->COMGETTER(RecommendedAudioController)(&audioController);
|
---|
| 385 | if (FAILED(hrc)) return hrc;
|
---|
[95423] | 386 |
|
---|
[98288] | 387 | hrc = m->pAdapter->COMSETTER(AudioController)(audioController);
|
---|
| 388 | if (FAILED(hrc)) return hrc;
|
---|
[95423] | 389 |
|
---|
| 390 | AudioCodecType_T audioCodec;
|
---|
[98288] | 391 | hrc = aGuestOsType->COMGETTER(RecommendedAudioCodec)(&audioCodec);
|
---|
| 392 | if (FAILED(hrc)) return hrc;
|
---|
[95423] | 393 |
|
---|
[98288] | 394 | hrc = m->pAdapter->COMSETTER(AudioCodec)(audioCodec);
|
---|
| 395 | if (FAILED(hrc)) return hrc;
|
---|
[95423] | 396 |
|
---|
[98288] | 397 | hrc = m->pAdapter->COMSETTER(Enabled)(true);
|
---|
| 398 | if (FAILED(hrc)) return hrc;
|
---|
[95423] | 399 |
|
---|
[98288] | 400 | hrc = m->pAdapter->COMSETTER(EnabledOut)(true);
|
---|
| 401 | if (FAILED(hrc)) return hrc;
|
---|
[95423] | 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 | /**
|
---|
[98028] | 411 | * Rolls back the current configuration to a former state.
|
---|
| 412 | *
|
---|
[95423] | 413 | * @note Locks this object for writing.
|
---|
| 414 | */
|
---|
| 415 | void AudioSettings::i_rollback(void)
|
---|
| 416 | {
|
---|
| 417 | /* sanity */
|
---|
| 418 | AutoCaller autoCaller(this);
|
---|
[98262] | 419 | AssertComRCReturnVoid(autoCaller.hrc());
|
---|
[95423] | 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 | /**
|
---|
[98028] | 429 | * Commits the current settings and propagates those to a peer (if assigned).
|
---|
| 430 | *
|
---|
[95423] | 431 | * @note Locks this object for writing, together with the peer object (also
|
---|
[98028] | 432 | * for writing) if there is one.
|
---|
[95423] | 433 | */
|
---|
| 434 | void AudioSettings::i_commit(void)
|
---|
| 435 | {
|
---|
| 436 | /* sanity */
|
---|
| 437 | AutoCaller autoCaller(this);
|
---|
[98262] | 438 | AssertComRCReturnVoid(autoCaller.hrc());
|
---|
[95423] | 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 |
|
---|