VirtualBox

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

Last change on this file since 26046 was 26046, checked in by vboxsync, 15 years ago

Main: move Auto*StateDependency templates out of MachineImpl.h

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