VirtualBox

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

Last change on this file since 17879 was 17020, checked in by vboxsync, 16 years ago

Main: reverse check

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.2 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#include "Logging.h"
25
26#include <iprt/cpputils.h>
27#include <iprt/ldr.h>
28#include <iprt/process.h>
29
30#include <VBox/settings.h>
31
32// constructor / destructor
33/////////////////////////////////////////////////////////////////////////////
34
35DEFINE_EMPTY_CTOR_DTOR (AudioAdapter)
36
37HRESULT AudioAdapter::FinalConstruct()
38{
39 return S_OK;
40}
41
42void AudioAdapter::FinalRelease()
43{
44 uninit ();
45}
46
47// public initializer/uninitializer for internal purposes only
48/////////////////////////////////////////////////////////////////////////////
49
50/**
51 * Initializes the audio adapter object.
52 *
53 * @param aParent Handle of the parent object.
54 */
55HRESULT AudioAdapter::init (Machine *aParent)
56{
57 LogFlowThisFunc (("aParent=%p\n", aParent));
58
59 ComAssertRet (aParent, E_INVALIDARG);
60
61 /* Enclose the state transition NotReady->InInit->Ready */
62 AutoInitSpan autoInitSpan (this);
63 AssertReturn (autoInitSpan.isOk(), E_FAIL);
64
65 unconst (mParent) = aParent;
66 /* mPeer is left null */
67
68 mData.allocate();
69
70 /* Confirm a successful initialization */
71 autoInitSpan.setSucceeded();
72
73 return S_OK;
74}
75
76/**
77 * Initializes the audio adapter object given another audio adapter object
78 * (a kind of copy constructor). This object shares data with
79 * the object passed as an argument.
80 *
81 * @note This object must be destroyed before the original object
82 * it shares data with is destroyed.
83 *
84 * @note Locks @a aThat object for reading.
85 */
86HRESULT AudioAdapter::init (Machine *aParent, AudioAdapter *aThat)
87{
88 LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
89
90 ComAssertRet (aParent && aThat, E_INVALIDARG);
91
92 /* Enclose the state transition NotReady->InInit->Ready */
93 AutoInitSpan autoInitSpan (this);
94 AssertReturn (autoInitSpan.isOk(), E_FAIL);
95
96 unconst (mParent) = aParent;
97 unconst (mPeer) = aThat;
98
99 AutoCaller thatCaller (aThat);
100 AssertComRCReturnRC (thatCaller.rc());
101
102 AutoReadLock thatLock (aThat);
103 mData.share (aThat->mData);
104
105 /* Confirm a successful initialization */
106 autoInitSpan.setSucceeded();
107
108 return S_OK;
109}
110
111/**
112 * Initializes the guest object given another guest object
113 * (a kind of copy constructor). This object makes a private copy of data
114 * of the original object passed as an argument.
115 *
116 * @note Locks @a aThat object for reading.
117 */
118HRESULT AudioAdapter::initCopy (Machine *aParent, AudioAdapter *aThat)
119{
120 LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
121
122 ComAssertRet (aParent && aThat, E_INVALIDARG);
123
124 /* Enclose the state transition NotReady->InInit->Ready */
125 AutoInitSpan autoInitSpan (this);
126 AssertReturn (autoInitSpan.isOk(), E_FAIL);
127
128 unconst (mParent) = aParent;
129 /* mPeer is left null */
130
131 AutoCaller thatCaller (aThat);
132 AssertComRCReturnRC (thatCaller.rc());
133
134 AutoReadLock thatLock (aThat);
135 mData.attachCopy (aThat->mData);
136
137 /* Confirm a successful initialization */
138 autoInitSpan.setSucceeded();
139
140 return S_OK;
141}
142
143/**
144 * Uninitializes the instance and sets the ready flag to FALSE.
145 * Called either from FinalRelease() or by the parent when it gets destroyed.
146 */
147void AudioAdapter::uninit()
148{
149 LogFlowThisFunc (("\n"));
150
151 /* Enclose the state transition Ready->InUninit->NotReady */
152 AutoUninitSpan autoUninitSpan (this);
153 if (autoUninitSpan.uninitDone())
154 return;
155
156 mData.free();
157
158 unconst (mPeer).setNull();
159 unconst (mParent).setNull();
160}
161
162// IAudioAdapter properties
163/////////////////////////////////////////////////////////////////////////////
164
165STDMETHODIMP AudioAdapter::COMGETTER(Enabled)(BOOL *aEnabled)
166{
167 CheckComArgOutPointerValid(aEnabled);
168
169 AutoCaller autoCaller (this);
170 CheckComRCReturnRC (autoCaller.rc());
171
172 AutoReadLock alock (this);
173
174 *aEnabled = mData->mEnabled;
175
176 return S_OK;
177}
178
179STDMETHODIMP AudioAdapter::COMSETTER(Enabled)(BOOL aEnabled)
180{
181 AutoCaller autoCaller (this);
182 CheckComRCReturnRC (autoCaller.rc());
183
184 /* the machine needs to be mutable */
185 Machine::AutoMutableStateDependency adep (mParent);
186 CheckComRCReturnRC (adep.rc());
187
188 AutoWriteLock alock (this);
189
190 if (mData->mEnabled != aEnabled)
191 {
192 mData.backup();
193 mData->mEnabled = aEnabled;
194 }
195
196 return S_OK;
197}
198
199STDMETHODIMP AudioAdapter::COMGETTER(AudioDriver)(AudioDriverType_T *aAudioDriver)
200{
201 CheckComArgOutPointerValid(aAudioDriver);
202
203 AutoCaller autoCaller (this);
204 CheckComRCReturnRC (autoCaller.rc());
205
206 AutoReadLock alock (this);
207
208 *aAudioDriver = mData->mAudioDriver;
209
210 return S_OK;
211}
212
213STDMETHODIMP AudioAdapter::COMSETTER(AudioDriver)(AudioDriverType_T aAudioDriver)
214{
215 AutoCaller autoCaller (this);
216 CheckComRCReturnRC (autoCaller.rc());
217
218 /* the machine needs to be mutable */
219 Machine::AutoMutableStateDependency adep (mParent);
220 CheckComRCReturnRC (adep.rc());
221
222 AutoWriteLock alock (this);
223
224 HRESULT rc = S_OK;
225
226 if (mData->mAudioDriver != aAudioDriver)
227 {
228 /*
229 * which audio driver type are we supposed to use?
230 */
231 switch (aAudioDriver)
232 {
233 case AudioDriverType_Null:
234#ifdef RT_OS_WINDOWS
235# ifdef VBOX_WITH_WINMM
236 case AudioDriverType_WinMM:
237# endif
238 case AudioDriverType_DirectSound:
239#endif /* RT_OS_WINDOWS */
240#ifdef RT_OS_SOLARIS
241 case AudioDriverType_SolAudio:
242#endif
243#ifdef RT_OS_LINUX
244 case AudioDriverType_OSS:
245# ifdef VBOX_WITH_ALSA
246 case AudioDriverType_ALSA:
247# endif
248# ifdef VBOX_WITH_PULSE
249 case AudioDriverType_Pulse:
250# endif
251#endif /* RT_OS_LINUX */
252#ifdef RT_OS_DARWIN
253 case AudioDriverType_CoreAudio:
254#endif
255#ifdef RT_OS_OS2
256 case AudioDriverType_MMPM:
257#endif
258 {
259 mData.backup();
260 mData->mAudioDriver = aAudioDriver;
261 break;
262 }
263
264 default:
265 {
266 AssertMsgFailed (("Wrong audio driver type %d\n",
267 aAudioDriver));
268 rc = E_FAIL;
269 }
270 }
271 }
272
273 return rc;
274}
275
276STDMETHODIMP AudioAdapter::COMGETTER(AudioController)(AudioControllerType_T *aAudioController)
277{
278 CheckComArgOutPointerValid(aAudioController);
279
280 AutoCaller autoCaller (this);
281 CheckComRCReturnRC (autoCaller.rc());
282
283 AutoReadLock alock (this);
284
285 *aAudioController = mData->mAudioController;
286
287 return S_OK;
288}
289
290STDMETHODIMP AudioAdapter::COMSETTER(AudioController)(AudioControllerType_T aAudioController)
291{
292 AutoCaller autoCaller (this);
293 CheckComRCReturnRC (autoCaller.rc());
294
295 /* the machine needs to be mutable */
296 Machine::AutoMutableStateDependency adep (mParent);
297 CheckComRCReturnRC (adep.rc());
298
299 AutoWriteLock alock (this);
300
301 HRESULT rc = S_OK;
302
303 if (mData->mAudioController != aAudioController)
304 {
305 /*
306 * which audio hardware type are we supposed to use?
307 */
308 switch (aAudioController)
309 {
310 case AudioControllerType_AC97:
311 case AudioControllerType_SB16:
312 mData.backup();
313 mData->mAudioController = aAudioController;
314 break;
315
316 default:
317 {
318 AssertMsgFailed (("Wrong audio controller type %d\n",
319 aAudioController));
320 rc = E_FAIL;
321 }
322 }
323 }
324
325 return rc;
326}
327
328// IAudioAdapter methods
329/////////////////////////////////////////////////////////////////////////////
330
331// public methods only for internal purposes
332/////////////////////////////////////////////////////////////////////////////
333
334AudioAdapter::Data::Data()
335{
336 /* Generic defaults */
337 mEnabled = false;
338 mAudioController = AudioControllerType_AC97;
339 /* Driver defaults which are OS specific */
340#if defined (RT_OS_WINDOWS)
341# ifdef VBOX_WITH_WINMM
342 mAudioDriver = AudioDriverType_WinMM;
343# else /* VBOX_WITH_WINMM */
344 mAudioDriver = AudioDriverType_DirectSound;
345# endif /* !VBOX_WITH_WINMM */
346#elif defined (RT_OS_SOLARIS)
347 mAudioDriver = AudioDriverType_SolAudio;
348#elif defined (RT_OS_LINUX)
349# if defined (VBOX_WITH_PULSE)
350 /* Check for the pulse library & that the pulse audio daemon is running. */
351 if (RTProcIsRunningByName ("pulseaudio") &&
352 RTLdrIsLoadable ("libpulse.so.0"))
353 mAudioDriver = AudioDriverType_Pulse;
354 else
355# endif /* VBOX_WITH_PULSE */
356# if defined (VBOX_WITH_ALSA)
357 /* Check if we can load the ALSA library */
358 if (RTLdrIsLoadable ("libasound.so.2"))
359 mAudioDriver = AudioDriverType_ALSA;
360 else
361# endif /* VBOX_WITH_ALSA */
362 mAudioDriver = AudioDriverType_OSS;
363#elif defined (RT_OS_DARWIN)
364 mAudioDriver = AudioDriverType_CoreAudio;
365#elif defined (RT_OS_OS2)
366 mAudioDriver = AudioDriverType_MMP;;
367#else
368 mAudioDriver = AudioDriverType_Null;
369#endif
370}
371
372/**
373 * Loads settings from the given machine node.
374 * May be called once right after this object creation.
375 *
376 * @param aMachineNode <Machine> node.
377 *
378 * @note Locks this object for writing.
379 */
380HRESULT AudioAdapter::loadSettings (const settings::Key &aMachineNode)
381{
382 using namespace settings;
383
384 AssertReturn (!aMachineNode.isNull(), E_FAIL);
385
386 AutoCaller autoCaller (this);
387 AssertComRCReturnRC (autoCaller.rc());
388
389 AutoWriteLock alock (this);
390
391 /* Note: we assume that the default values for attributes of optional
392 * nodes are assigned in the Data::Data() constructor and don't do it
393 * here. It implies that this method may only be called after constructing
394 * a new AudioAdapter object while all its data fields are in the default
395 * values. Exceptions are fields whose creation time defaults don't match
396 * values that should be applied when these fields are not explicitly set
397 * in the settings file (for backwards compatibility reasons). This takes
398 * place when a setting of a newly created object must default to A while
399 * the same setting of an object loaded from the old settings file must
400 * default to B. */
401
402 /* AudioAdapter node (required) */
403 Key audioAdapterNode = aMachineNode.key ("AudioAdapter");
404
405 /* is the adapter enabled? (required) */
406 mData->mEnabled = audioAdapterNode.value <bool> ("enabled");
407
408 /* now check the audio adapter */
409 const char *controller = audioAdapterNode.stringValue ("controller");
410 if (strcmp (controller, "SB16") == 0)
411 mData->mAudioController = AudioControllerType_SB16;
412 else if (strcmp (controller, "AC97") == 0)
413 mData->mAudioController = AudioControllerType_AC97;
414
415 /* now check the audio driver (required) */
416 const char *driver = audioAdapterNode.stringValue ("driver");
417 if (strcmp (driver, "Null") == 0)
418 mData->mAudioDriver = AudioDriverType_Null;
419#ifdef RT_OS_WINDOWS
420 else if (strcmp (driver, "WinMM") == 0)
421#ifdef VBOX_WITH_WINMM
422 mData->mAudioDriver = AudioDriverType_WinMM;
423#else
424 /* fall back to dsound */
425 mData->mAudioDriver = AudioDriverType_DirectSound;
426#endif
427 else if (strcmp (driver, "DirectSound") == 0)
428 mData->mAudioDriver = AudioDriverType_DirectSound;
429#endif // RT_OS_WINDOWS
430#ifdef RT_OS_SOLARIS
431 else if (strcmp (driver, "SolAudio") == 0)
432 mData->mAudioDriver = AudioDriverType_SolAudio;
433#endif // RT_OS_SOLARIS
434#ifdef RT_OS_LINUX
435 else if (strcmp (driver, "OSS") == 0)
436 mData->mAudioDriver = AudioDriverType_OSS;
437 else if (strcmp (driver, "ALSA") == 0)
438# ifdef VBOX_WITH_ALSA
439 mData->mAudioDriver = AudioDriverType_ALSA;
440# else
441 /* fall back to OSS */
442 mData->mAudioDriver = AudioDriverType_OSS;
443# endif
444 else if (strcmp (driver, "Pulse") == 0)
445# ifdef VBOX_WITH_PULSE
446 mData->mAudioDriver = AudioDriverType_Pulse;
447# else
448 /* fall back to OSS */
449 mData->mAudioDriver = AudioDriverType_OSS;
450# endif
451#endif // RT_OS_LINUX
452#ifdef RT_OS_DARWIN
453 else if (strcmp (driver, "CoreAudio") == 0)
454 mData->mAudioDriver = AudioDriverType_CoreAudio;
455#endif
456#ifdef RT_OS_OS2
457 else if (strcmp (driver, "MMPM") == 0)
458 mData->mAudioDriver = AudioDriverType_MMPM;
459#endif
460 else
461 AssertMsgFailed (("Invalid driver '%s'\n", driver));
462
463 return S_OK;
464}
465
466/**
467 * Saves settings to the given machine node.
468 *
469 * @param aMachineNode <Machine> node.
470 *
471 * @note Locks this object for reading.
472 */
473HRESULT AudioAdapter::saveSettings (settings::Key &aMachineNode)
474{
475 using namespace settings;
476
477 AssertReturn (!aMachineNode.isNull(), E_FAIL);
478
479 AutoCaller autoCaller (this);
480 AssertComRCReturnRC (autoCaller.rc());
481
482 AutoReadLock alock (this);
483
484 Key node = aMachineNode.createKey ("AudioAdapter");
485
486 const char *controllerStr = NULL;
487 switch (mData->mAudioController)
488 {
489 case AudioControllerType_SB16:
490 {
491 controllerStr = "SB16";
492 break;
493 }
494 default:
495 {
496 controllerStr = "AC97";
497 break;
498 }
499 }
500 node.setStringValue ("controller", controllerStr);
501
502 const char *driverStr = NULL;
503 switch (mData->mAudioDriver)
504 {
505 case AudioDriverType_Null:
506 {
507 driverStr = "Null";
508 break;
509 }
510#ifdef RT_OS_WINDOWS
511 case AudioDriverType_WinMM:
512# ifdef VBOX_WITH_WINMM
513 {
514 driverStr = "WinMM";
515 break;
516 }
517# endif
518 case AudioDriverType_DirectSound:
519 {
520 driverStr = "DirectSound";
521 break;
522 }
523#endif /* RT_OS_WINDOWS */
524#ifdef RT_OS_SOLARIS
525 case AudioDriverType_SolAudio:
526 {
527 driverStr = "SolAudio";
528 break;
529 }
530#endif
531#ifdef RT_OS_LINUX
532 case AudioDriverType_ALSA:
533# ifdef VBOX_WITH_ALSA
534 {
535 driverStr = "ALSA";
536 break;
537 }
538# endif
539 case AudioDriverType_Pulse:
540# ifdef VBOX_WITH_PULSE
541 {
542 driverStr = "Pulse";
543 break;
544 }
545# endif
546 case AudioDriverType_OSS:
547 {
548 driverStr = "OSS";
549 break;
550 }
551#endif /* RT_OS_LINUX */
552#ifdef RT_OS_DARWIN
553 case AudioDriverType_CoreAudio:
554 {
555 driverStr = "CoreAudio";
556 break;
557 }
558#endif
559#ifdef RT_OS_OS2
560 case AudioDriverType_MMPM:
561 {
562 driverStr = "MMPM";
563 break;
564 }
565#endif
566 default:
567 ComAssertMsgFailedRet (("Wrong audio driver type! driver = %d",
568 mData->mAudioDriver),
569 E_FAIL);
570 }
571 node.setStringValue ("driver", driverStr);
572
573 node.setValue <bool> ("enabled", !!mData->mEnabled);
574
575 return S_OK;
576}
577
578/**
579 * @note Locks this object for writing.
580 */
581bool AudioAdapter::rollback()
582{
583 /* sanity */
584 AutoCaller autoCaller (this);
585 AssertComRCReturn (autoCaller.rc(), false);
586
587 AutoWriteLock alock (this);
588
589 bool changed = false;
590
591 if (mData.isBackedUp())
592 {
593 /* we need to check all data to see whether anything will be changed
594 * after rollback */
595 changed = mData.hasActualChanges();
596 mData.rollback();
597 }
598
599 return changed;
600}
601
602/**
603 * @note Locks this object for writing, together with the peer object (also
604 * for writing) if there is one.
605 */
606void AudioAdapter::commit()
607{
608 /* sanity */
609 AutoCaller autoCaller (this);
610 AssertComRCReturnVoid (autoCaller.rc());
611
612 /* sanity too */
613 AutoCaller peerCaller (mPeer);
614 AssertComRCReturnVoid (peerCaller.rc());
615
616 /* lock both for writing since we modify both (mPeer is "master" so locked
617 * first) */
618 AutoMultiWriteLock2 alock (mPeer, this);
619
620 if (mData.isBackedUp())
621 {
622 mData.commit();
623 if (mPeer)
624 {
625 /* attach new data to the peer and reshare it */
626 mPeer->mData.attach (mData);
627 }
628 }
629}
630
631/**
632 * @note Locks this object for writing, together with the peer object
633 * represented by @a aThat (locked for reading).
634 */
635void AudioAdapter::copyFrom (AudioAdapter *aThat)
636{
637 AssertReturnVoid (aThat != NULL);
638
639 /* sanity */
640 AutoCaller autoCaller (this);
641 AssertComRCReturnVoid (autoCaller.rc());
642
643 /* sanity too */
644 AutoCaller thatCaller (aThat);
645 AssertComRCReturnVoid (thatCaller.rc());
646
647 /* peer is not modified, lock it for reading (aThat is "master" so locked
648 * first) */
649 AutoMultiLock2 alock (aThat->rlock(), this->wlock());
650
651 /* this will back up current data */
652 mData.assignCopy (aThat->mData);
653}
654/* 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