VirtualBox

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

Last change on this file since 32751 was 30976, checked in by vboxsync, 14 years ago

add support for Intel HD Audio, now built by default

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.4 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 case AudioControllerType_HDA:
304 {
305 mData.backup();
306 mData->mAudioController = aAudioController;
307
308 alock.release();
309 AutoWriteLock mlock(mParent COMMA_LOCKVAL_SRC_POS); // mParent is const, needs no locking
310 mParent->setModified(Machine::IsModified_AudioAdapter);
311 break;
312 }
313
314 default:
315 AssertMsgFailed (("Wrong audio controller type %d\n",
316 aAudioController));
317 rc = E_FAIL;
318 }
319 }
320
321 return rc;
322}
323
324// IAudioAdapter methods
325/////////////////////////////////////////////////////////////////////////////
326
327// public methods only for internal purposes
328/////////////////////////////////////////////////////////////////////////////
329
330AudioAdapter::Data::Data()
331{
332 /* Generic defaults */
333 mEnabled = false;
334 mAudioController = AudioControllerType_AC97;
335 /* Driver defaults to the null audio driver */
336 mAudioDriver = AudioDriverType_Null;
337}
338
339/**
340 * Loads settings from the given machine node.
341 * May be called once right after this object creation.
342 *
343 * @param aMachineNode <Machine> node.
344 *
345 * @note Locks this object for writing.
346 */
347HRESULT AudioAdapter::loadSettings(const settings::AudioAdapter &data)
348{
349 AutoCaller autoCaller(this);
350 AssertComRCReturnRC(autoCaller.rc());
351
352 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
353
354 /* Note: we assume that the default values for attributes of optional
355 * nodes are assigned in the Data::Data() constructor and don't do it
356 * here. It implies that this method may only be called after constructing
357 * a new AudioAdapter object while all its data fields are in the default
358 * values. Exceptions are fields whose creation time defaults don't match
359 * values that should be applied when these fields are not explicitly set
360 * in the settings file (for backwards compatibility reasons). This takes
361 * place when a setting of a newly created object must default to A while
362 * the same setting of an object loaded from the old settings file must
363 * default to B. */
364
365 mData->mEnabled = data.fEnabled;
366 mData->mAudioController = data.controllerType;
367 mData->mAudioDriver = data.driverType;
368
369 return S_OK;
370}
371
372/**
373 * Saves settings to the given machine node.
374 *
375 * @param aMachineNode <Machine> node.
376 *
377 * @note Locks this object for reading.
378 */
379HRESULT AudioAdapter::saveSettings(settings::AudioAdapter &data)
380{
381 AutoCaller autoCaller(this);
382 AssertComRCReturnRC(autoCaller.rc());
383
384 AutoReadLock alock(this COMMA_LOCKVAL_SRC_POS);
385
386 data.fEnabled = !!mData->mEnabled;
387 data.controllerType = mData->mAudioController;
388 data.driverType = mData->mAudioDriver;
389 return S_OK;
390}
391
392/**
393 * @note Locks this object for writing.
394 */
395void AudioAdapter::rollback()
396{
397 /* sanity */
398 AutoCaller autoCaller(this);
399 AssertComRCReturnVoid(autoCaller.rc());
400
401 AutoWriteLock alock(this COMMA_LOCKVAL_SRC_POS);
402
403 mData.rollback();
404}
405
406/**
407 * @note Locks this object for writing, together with the peer object (also
408 * for writing) if there is one.
409 */
410void AudioAdapter::commit()
411{
412 /* sanity */
413 AutoCaller autoCaller(this);
414 AssertComRCReturnVoid (autoCaller.rc());
415
416 /* sanity too */
417 AutoCaller peerCaller (mPeer);
418 AssertComRCReturnVoid (peerCaller.rc());
419
420 /* lock both for writing since we modify both (mPeer is "master" so locked
421 * first) */
422 AutoMultiWriteLock2 alock(mPeer, this COMMA_LOCKVAL_SRC_POS);
423
424 if (mData.isBackedUp())
425 {
426 mData.commit();
427 if (mPeer)
428 {
429 /* attach new data to the peer and reshare it */
430 mPeer->mData.attach (mData);
431 }
432 }
433}
434
435/**
436 * @note Locks this object for writing, together with the peer object
437 * represented by @a aThat (locked for reading).
438 */
439void AudioAdapter::copyFrom(AudioAdapter *aThat)
440{
441 AssertReturnVoid (aThat != NULL);
442
443 /* sanity */
444 AutoCaller autoCaller(this);
445 AssertComRCReturnVoid (autoCaller.rc());
446
447 /* sanity too */
448 AutoCaller thatCaller (aThat);
449 AssertComRCReturnVoid (thatCaller.rc());
450
451 /* peer is not modified, lock it for reading (aThat is "master" so locked
452 * first) */
453 AutoReadLock rl(aThat COMMA_LOCKVAL_SRC_POS);
454 AutoWriteLock wl(this COMMA_LOCKVAL_SRC_POS);
455
456 /* this will back up current data */
457 mData.assignCopy(aThat->mData);
458}
459/* 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