VirtualBox

source: vbox/trunk/src/VBox/Main/AudioAdapterImpl.cpp@ 30935

Last change on this file since 30935 was 30934, checked in by vboxsync, 14 years ago

Main: automatically fix host-specific audio drivers when loading machine settings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.3 KB
Line 
1/** @file
2 *
3 * VirtualBox COM class implementation
4 */
5
6/*
7 * Copyright (C) 2006-2007 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#include "AudioAdapterImpl.h"
19#include "MachineImpl.h"
20
21#include <iprt/cpp/utils.h>
22
23#include <VBox/settings.h>
24
25#include "AutoStateDep.h"
26#include "AutoCaller.h"
27#include "Logging.h"
28
29// constructor / destructor
30/////////////////////////////////////////////////////////////////////////////
31
32AudioAdapter::AudioAdapter()
33 : mParent(NULL)
34{
35}
36
37AudioAdapter::~AudioAdapter()
38{
39}
40
41HRESULT AudioAdapter::FinalConstruct()
42{
43 return S_OK;
44}
45
46void AudioAdapter::FinalRelease()
47{
48 uninit();
49}
50
51// public initializer/uninitializer for internal purposes only
52/////////////////////////////////////////////////////////////////////////////
53
54/**
55 * Initializes the audio adapter object.
56 *
57 * @param aParent Handle of the parent object.
58 */
59HRESULT AudioAdapter::init (Machine *aParent)
60{
61 LogFlowThisFunc(("aParent=%p\n", aParent));
62
63 ComAssertRet(aParent, E_INVALIDARG);
64
65 /* Enclose the state transition NotReady->InInit->Ready */
66 AutoInitSpan autoInitSpan(this);
67 AssertReturn(autoInitSpan.isOk(), E_FAIL);
68
69 /* Get the default audio driver out of the system properties */
70 ComPtr<IVirtualBox> VBox;
71 HRESULT rc = aParent->COMGETTER(Parent)(VBox.asOutParam());
72 if (FAILED(rc)) return rc;
73 ComPtr<ISystemProperties> sysProps;
74 rc = VBox->COMGETTER(SystemProperties)(sysProps.asOutParam());
75 if (FAILED(rc)) return rc;
76 AudioDriverType_T defaultAudioDriver;
77 rc = sysProps->COMGETTER(DefaultAudioDriver)(&defaultAudioDriver);
78 if (FAILED(rc)) return rc;
79
80 unconst(mParent) = aParent;
81 /* mPeer is left null */
82
83 mData.allocate();
84 mData->mAudioDriver = defaultAudioDriver;
85
86 /* Confirm a successful initialization */
87 autoInitSpan.setSucceeded();
88
89 return S_OK;
90}
91
92/**
93 * Initializes the audio adapter object given another audio adapter object
94 * (a kind of copy constructor). This object shares data with
95 * the object passed as an argument.
96 *
97 * @note This object must be destroyed before the original object
98 * it shares data with is destroyed.
99 *
100 * @note Locks @a aThat object for reading.
101 */
102HRESULT AudioAdapter::init (Machine *aParent, AudioAdapter *aThat)
103{
104 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
105
106 ComAssertRet(aParent && aThat, E_INVALIDARG);
107
108 /* Enclose the state transition NotReady->InInit->Ready */
109 AutoInitSpan autoInitSpan(this);
110 AssertReturn(autoInitSpan.isOk(), E_FAIL);
111
112 unconst(mParent) = aParent;
113 unconst(mPeer) = aThat;
114
115 AutoCaller thatCaller (aThat);
116 AssertComRCReturnRC(thatCaller.rc());
117
118 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
119 mData.share (aThat->mData);
120
121 /* Confirm a successful initialization */
122 autoInitSpan.setSucceeded();
123
124 return S_OK;
125}
126
127/**
128 * Initializes the guest object given another guest object
129 * (a kind of copy constructor). This object makes a private copy of data
130 * of the original object passed as an argument.
131 *
132 * @note Locks @a aThat object for reading.
133 */
134HRESULT AudioAdapter::initCopy (Machine *aParent, AudioAdapter *aThat)
135{
136 LogFlowThisFunc(("aParent=%p, aThat=%p\n", aParent, aThat));
137
138 ComAssertRet(aParent && aThat, E_INVALIDARG);
139
140 /* Enclose the state transition NotReady->InInit->Ready */
141 AutoInitSpan autoInitSpan(this);
142 AssertReturn(autoInitSpan.isOk(), E_FAIL);
143
144 unconst(mParent) = aParent;
145 /* mPeer is left null */
146
147 AutoCaller thatCaller (aThat);
148 AssertComRCReturnRC(thatCaller.rc());
149
150 AutoReadLock thatLock(aThat COMMA_LOCKVAL_SRC_POS);
151 mData.attachCopy (aThat->mData);
152
153 /* Confirm a successful initialization */
154 autoInitSpan.setSucceeded();
155
156 return S_OK;
157}
158
159/**
160 * Uninitializes the instance and sets the ready flag to FALSE.
161 * Called either from FinalRelease() or by the parent when it gets destroyed.
162 */
163void AudioAdapter::uninit()
164{
165 LogFlowThisFunc(("\n"));
166
167 /* Enclose the state transition Ready->InUninit->NotReady */
168 AutoUninitSpan autoUninitSpan(this);
169 if (autoUninitSpan.uninitDone())
170 return;
171
172 mData.free();
173
174 unconst(mPeer) = NULL;
175 unconst(mParent) = NULL;
176}
177
178// IAudioAdapter properties
179/////////////////////////////////////////////////////////////////////////////
180
181STDMETHODIMP AudioAdapter::COMGETTER(Enabled)(BOOL *aEnabled)
182{
183 CheckComArgOutPointerValid(aEnabled);
184
185 AutoCaller autoCaller(this);
186 if (FAILED(autoCaller.rc())) return autoCaller.rc();
187
188 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
189
190 *aEnabled = mData->mEnabled;
191
192 return S_OK;
193}
194
195STDMETHODIMP AudioAdapter::COMSETTER(Enabled)(BOOL aEnabled)
196{
197 AutoCaller autoCaller(this);
198 if (FAILED(autoCaller.rc())) return autoCaller.rc();
199
200 /* the machine needs to be mutable */
201 AutoMutableStateDependency adep(mParent);
202 if (FAILED(adep.rc())) return adep.rc();
203
204 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
205
206 if (mData->mEnabled != aEnabled)
207 {
208 mData.backup();
209 mData->mEnabled = aEnabled;
210
211 alock.release();
212 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, needs no locking
213 mParent->setModified(Machine::IsModified_AudioAdapter);
214 }
215
216 return S_OK;
217}
218
219STDMETHODIMP AudioAdapter::COMGETTER(AudioDriver)(AudioDriverType_T *aAudioDriver)
220{
221 CheckComArgOutPointerValid(aAudioDriver);
222
223 AutoCaller autoCaller(this);
224 if (FAILED(autoCaller.rc())) return autoCaller.rc();
225
226 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
227
228 *aAudioDriver = mData->mAudioDriver;
229
230 return S_OK;
231}
232
233STDMETHODIMP AudioAdapter::COMSETTER(AudioDriver)(AudioDriverType_T aAudioDriver)
234{
235 AutoCaller autoCaller(this);
236 if (FAILED(autoCaller.rc())) return autoCaller.rc();
237
238 /* the machine needs to be mutable */
239 AutoMutableStateDependency adep(mParent);
240 if (FAILED(adep.rc())) return adep.rc();
241
242 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
243
244 HRESULT rc = S_OK;
245
246 if (mData->mAudioDriver != aAudioDriver)
247 {
248 if (settings::MachineConfigFile::isAudioDriverAllowedOnThisHost(aAudioDriver))
249 {
250 mData.backup();
251 mData->mAudioDriver = aAudioDriver;
252
253 alock.release();
254 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, needs no locking
255 mParent->setModified(Machine::IsModified_AudioAdapter);
256 }
257 else
258 {
259 AssertMsgFailed(("Wrong audio driver type %d\n", aAudioDriver));
260 rc = E_FAIL;
261 }
262 }
263
264 return rc;
265}
266
267STDMETHODIMP AudioAdapter::COMGETTER(AudioController)(AudioControllerType_T *aAudioController)
268{
269 CheckComArgOutPointerValid(aAudioController);
270
271 AutoCaller autoCaller(this);
272 if (FAILED(autoCaller.rc())) return autoCaller.rc();
273
274 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
275
276 *aAudioController = mData->mAudioController;
277
278 return S_OK;
279}
280
281STDMETHODIMP AudioAdapter::COMSETTER(AudioController)(AudioControllerType_T aAudioController)
282{
283 AutoCaller autoCaller(this);
284 if (FAILED(autoCaller.rc())) return autoCaller.rc();
285
286 /* the machine needs to be mutable */
287 AutoMutableStateDependency adep(mParent);
288 if (FAILED(adep.rc())) return adep.rc();
289
290 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
291
292 HRESULT rc = S_OK;
293
294 if (mData->mAudioController != aAudioController)
295 {
296 /*
297 * which audio hardware type are we supposed to use?
298 */
299 switch (aAudioController)
300 {
301 case AudioControllerType_AC97:
302 case AudioControllerType_SB16:
303 {
304 mData.backup();
305 mData->mAudioController = aAudioController;
306
307 alock.release();
308 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, needs no locking
309 mParent->setModified(Machine::IsModified_AudioAdapter);
310 break;
311 }
312
313 default:
314 AssertMsgFailed (("Wrong audio controller type %d\n",
315 aAudioController));
316 rc = E_FAIL;
317 }
318 }
319
320 return rc;
321}
322
323// IAudioAdapter methods
324/////////////////////////////////////////////////////////////////////////////
325
326// public methods only for internal purposes
327/////////////////////////////////////////////////////////////////////////////
328
329AudioAdapter::Data::Data()
330{
331 /* Generic defaults */
332 mEnabled = false;
333 mAudioController = AudioControllerType_AC97;
334 /* Driver defaults to the null audio driver */
335 mAudioDriver = AudioDriverType_Null;
336}
337
338/**
339 * Loads settings from the given machine node.
340 * May be called once right after this object creation.
341 *
342 * @param aMachineNode <Machine> node.
343 *
344 * @note Locks this object for writing.
345 */
346HRESULT AudioAdapter::loadSettings(const settings::AudioAdapter &data)
347{
348 AutoCaller autoCaller(this);
349 AssertComRCReturnRC(autoCaller.rc());
350
351 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
352
353 /* Note: we assume that the default values for attributes of optional
354 * nodes are assigned in the Data::Data() constructor and don't do it
355 * here. It implies that this method may only be called after constructing
356 * a new AudioAdapter object while all its data fields are in the default
357 * values. Exceptions are fields whose creation time defaults don't match
358 * values that should be applied when these fields are not explicitly set
359 * in the settings file (for backwards compatibility reasons). This takes
360 * place when a setting of a newly created object must default to A while
361 * the same setting of an object loaded from the old settings file must
362 * default to B. */
363
364 mData->mEnabled = data.fEnabled;
365 mData->mAudioController = data.controllerType;
366 mData->mAudioDriver = data.driverType;
367
368 return S_OK;
369}
370
371/**
372 * Saves settings to the given machine node.
373 *
374 * @param aMachineNode <Machine> node.
375 *
376 * @note Locks this object for reading.
377 */
378HRESULT AudioAdapter::saveSettings(settings::AudioAdapter &data)
379{
380 AutoCaller autoCaller(this);
381 AssertComRCReturnRC(autoCaller.rc());
382
383 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
384
385 data.fEnabled = !!mData->mEnabled;
386 data.controllerType = mData->mAudioController;
387 data.driverType = mData->mAudioDriver;
388 return S_OK;
389}
390
391/**
392 * @note Locks this object for writing.
393 */
394void AudioAdapter::rollback()
395{
396 /* sanity */
397 AutoCaller autoCaller(this);
398 AssertComRCReturnVoid(autoCaller.rc());
399
400 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
401
402 mData.rollback();
403}
404
405/**
406 * @note Locks this object for writing, together with the peer object (also
407 * for writing) if there is one.
408 */
409void AudioAdapter::commit()
410{
411 /* sanity */
412 AutoCaller autoCaller(this);
413 AssertComRCReturnVoid (autoCaller.rc());
414
415 /* sanity too */
416 AutoCaller peerCaller (mPeer);
417 AssertComRCReturnVoid (peerCaller.rc());
418
419 /* lock both for writing since we modify both (mPeer is "master" so locked
420 * first) */
421 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
422
423 if (mData.isBackedUp())
424 {
425 mData.commit();
426 if (mPeer)
427 {
428 /* attach new data to the peer and reshare it */
429 mPeer->mData.attach (mData);
430 }
431 }
432}
433
434/**
435 * @note Locks this object for writing, together with the peer object
436 * represented by @a aThat (locked for reading).
437 */
438void AudioAdapter::copyFrom(AudioAdapter *aThat)
439{
440 AssertReturnVoid (aThat != NULL);
441
442 /* sanity */
443 AutoCaller autoCaller(this);
444 AssertComRCReturnVoid (autoCaller.rc());
445
446 /* sanity too */
447 AutoCaller thatCaller (aThat);
448 AssertComRCReturnVoid (thatCaller.rc());
449
450 /* peer is not modified, lock it for reading (aThat is "master" so locked
451 * first) */
452 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
453 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
454
455 /* this will back up current data */
456 mData.assignCopy(aThat->mData);
457}
458/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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