1 | /** @file
|
---|
2 | *
|
---|
3 | * VirtualBox COM class implementation
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 | #include "AudioAdapterImpl.h"
|
---|
19 | #include "MachineImpl.h"
|
---|
20 | #include "Logging.h"
|
---|
21 |
|
---|
22 | #include <iprt/cpputils.h>
|
---|
23 |
|
---|
24 | // constructor / destructor
|
---|
25 | /////////////////////////////////////////////////////////////////////////////
|
---|
26 |
|
---|
27 | DEFINE_EMPTY_CTOR_DTOR (AudioAdapter)
|
---|
28 |
|
---|
29 | HRESULT AudioAdapter::FinalConstruct()
|
---|
30 | {
|
---|
31 | return S_OK;
|
---|
32 | }
|
---|
33 |
|
---|
34 | void AudioAdapter::FinalRelease()
|
---|
35 | {
|
---|
36 | uninit ();
|
---|
37 | }
|
---|
38 |
|
---|
39 | // public initializer/uninitializer for internal purposes only
|
---|
40 | /////////////////////////////////////////////////////////////////////////////
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Initializes the audio adapter object.
|
---|
44 | *
|
---|
45 | * @param aParent Handle of the parent object.
|
---|
46 | */
|
---|
47 | HRESULT AudioAdapter::init (Machine *aParent)
|
---|
48 | {
|
---|
49 | LogFlowThisFunc (("aParent=%p\n", aParent));
|
---|
50 |
|
---|
51 | ComAssertRet (aParent, E_INVALIDARG);
|
---|
52 |
|
---|
53 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
54 | AutoInitSpan autoInitSpan (this);
|
---|
55 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
56 |
|
---|
57 | unconst (mParent) = aParent;
|
---|
58 | /* mPeer is left null */
|
---|
59 |
|
---|
60 | mData.allocate();
|
---|
61 |
|
---|
62 | /* Confirm a successful initialization */
|
---|
63 | autoInitSpan.setSucceeded();
|
---|
64 |
|
---|
65 | return S_OK;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Initializes the audio adapter object given another audio adapter object
|
---|
70 | * (a kind of copy constructor). This object shares data with
|
---|
71 | * the object passed as an argument.
|
---|
72 | *
|
---|
73 | * @note This object must be destroyed before the original object
|
---|
74 | * it shares data with is destroyed.
|
---|
75 | *
|
---|
76 | * @note Locks @a aThat object for reading.
|
---|
77 | */
|
---|
78 | HRESULT AudioAdapter::init (Machine *aParent, AudioAdapter *aThat)
|
---|
79 | {
|
---|
80 | LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
|
---|
81 |
|
---|
82 | ComAssertRet (aParent && aThat, E_INVALIDARG);
|
---|
83 |
|
---|
84 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
85 | AutoInitSpan autoInitSpan (this);
|
---|
86 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
87 |
|
---|
88 | unconst (mParent) = aParent;
|
---|
89 | unconst (mPeer) = aThat;
|
---|
90 |
|
---|
91 | AutoCaller thatCaller (aThat);
|
---|
92 | AssertComRCReturnRC (thatCaller.rc());
|
---|
93 |
|
---|
94 | AutoReaderLock thatLock (aThat);
|
---|
95 | mData.share (aThat->mData);
|
---|
96 |
|
---|
97 | /* Confirm a successful initialization */
|
---|
98 | autoInitSpan.setSucceeded();
|
---|
99 |
|
---|
100 | return S_OK;
|
---|
101 | }
|
---|
102 |
|
---|
103 | /**
|
---|
104 | * Initializes the guest object given another guest object
|
---|
105 | * (a kind of copy constructor). This object makes a private copy of data
|
---|
106 | * of the original object passed as an argument.
|
---|
107 | *
|
---|
108 | * @note Locks @a aThat object for reading.
|
---|
109 | */
|
---|
110 | HRESULT AudioAdapter::initCopy (Machine *aParent, AudioAdapter *aThat)
|
---|
111 | {
|
---|
112 | LogFlowThisFunc (("aParent=%p, aThat=%p\n", aParent, aThat));
|
---|
113 |
|
---|
114 | ComAssertRet (aParent && aThat, E_INVALIDARG);
|
---|
115 |
|
---|
116 | /* Enclose the state transition NotReady->InInit->Ready */
|
---|
117 | AutoInitSpan autoInitSpan (this);
|
---|
118 | AssertReturn (autoInitSpan.isOk(), E_UNEXPECTED);
|
---|
119 |
|
---|
120 | unconst (mParent) = aParent;
|
---|
121 | /* mPeer is left null */
|
---|
122 |
|
---|
123 | AutoCaller thatCaller (aThat);
|
---|
124 | AssertComRCReturnRC (thatCaller.rc());
|
---|
125 |
|
---|
126 | AutoReaderLock thatLock (aThat);
|
---|
127 | mData.attachCopy (aThat->mData);
|
---|
128 |
|
---|
129 | /* Confirm a successful initialization */
|
---|
130 | autoInitSpan.setSucceeded();
|
---|
131 |
|
---|
132 | return S_OK;
|
---|
133 | }
|
---|
134 |
|
---|
135 | /**
|
---|
136 | * Uninitializes the instance and sets the ready flag to FALSE.
|
---|
137 | * Called either from FinalRelease() or by the parent when it gets destroyed.
|
---|
138 | */
|
---|
139 | void AudioAdapter::uninit()
|
---|
140 | {
|
---|
141 | LogFlowThisFunc (("\n"));
|
---|
142 |
|
---|
143 | /* Enclose the state transition Ready->InUninit->NotReady */
|
---|
144 | AutoUninitSpan autoUninitSpan (this);
|
---|
145 | if (autoUninitSpan.uninitDone())
|
---|
146 | return;
|
---|
147 |
|
---|
148 | mData.free();
|
---|
149 |
|
---|
150 | unconst (mPeer).setNull();
|
---|
151 | unconst (mParent).setNull();
|
---|
152 | }
|
---|
153 |
|
---|
154 | // IAudioAdapter properties
|
---|
155 | /////////////////////////////////////////////////////////////////////////////
|
---|
156 |
|
---|
157 | STDMETHODIMP AudioAdapter::COMGETTER(Enabled)(BOOL *aEnabled)
|
---|
158 | {
|
---|
159 | if (!aEnabled)
|
---|
160 | return E_POINTER;
|
---|
161 |
|
---|
162 | AutoCaller autoCaller (this);
|
---|
163 | CheckComRCReturnRC (autoCaller.rc());
|
---|
164 |
|
---|
165 | AutoReaderLock alock (this);
|
---|
166 |
|
---|
167 | *aEnabled = mData->mEnabled;
|
---|
168 |
|
---|
169 | return S_OK;
|
---|
170 | }
|
---|
171 |
|
---|
172 | STDMETHODIMP AudioAdapter::COMSETTER(Enabled)(BOOL aEnabled)
|
---|
173 | {
|
---|
174 | AutoCaller autoCaller (this);
|
---|
175 | CheckComRCReturnRC (autoCaller.rc());
|
---|
176 |
|
---|
177 | /* the machine needs to be mutable */
|
---|
178 | Machine::AutoMutableStateDependency adep (mParent);
|
---|
179 | CheckComRCReturnRC (adep.rc());
|
---|
180 |
|
---|
181 | AutoLock alock (this);
|
---|
182 |
|
---|
183 | if (mData->mEnabled != aEnabled)
|
---|
184 | {
|
---|
185 | mData.backup();
|
---|
186 | mData->mEnabled = aEnabled;
|
---|
187 | }
|
---|
188 |
|
---|
189 | return S_OK;
|
---|
190 | }
|
---|
191 |
|
---|
192 | STDMETHODIMP AudioAdapter::COMGETTER(AudioDriver)(AudioDriverType_T *aAudioDriver)
|
---|
193 | {
|
---|
194 | if (!aAudioDriver)
|
---|
195 | return E_POINTER;
|
---|
196 |
|
---|
197 | AutoCaller autoCaller (this);
|
---|
198 | CheckComRCReturnRC (autoCaller.rc());
|
---|
199 |
|
---|
200 | AutoReaderLock alock (this);
|
---|
201 |
|
---|
202 | *aAudioDriver = mData->mAudioDriver;
|
---|
203 |
|
---|
204 | return S_OK;
|
---|
205 | }
|
---|
206 |
|
---|
207 | STDMETHODIMP AudioAdapter::COMSETTER(AudioDriver)(AudioDriverType_T aAudioDriver)
|
---|
208 | {
|
---|
209 | AutoCaller autoCaller (this);
|
---|
210 | CheckComRCReturnRC (autoCaller.rc());
|
---|
211 |
|
---|
212 | /* the machine needs to be mutable */
|
---|
213 | Machine::AutoMutableStateDependency adep (mParent);
|
---|
214 | CheckComRCReturnRC (adep.rc());
|
---|
215 |
|
---|
216 | AutoLock alock (this);
|
---|
217 |
|
---|
218 | HRESULT rc = S_OK;
|
---|
219 |
|
---|
220 | if (mData->mAudioDriver != aAudioDriver)
|
---|
221 | {
|
---|
222 | /*
|
---|
223 | * which audio driver type are we supposed to use?
|
---|
224 | */
|
---|
225 | switch (aAudioDriver)
|
---|
226 | {
|
---|
227 | case AudioDriverType_NullAudioDriver:
|
---|
228 | #ifdef RT_OS_WINDOWS
|
---|
229 | #ifdef VBOX_WITH_WINMM
|
---|
230 | case AudioDriverType_WINMMAudioDriver:
|
---|
231 | #endif
|
---|
232 | case AudioDriverType_DSOUNDAudioDriver:
|
---|
233 | #endif /* RT_OS_WINDOWS */
|
---|
234 | #ifdef RT_OS_LINUX
|
---|
235 | case AudioDriverType_OSSAudioDriver:
|
---|
236 | #ifdef VBOX_WITH_ALSA
|
---|
237 | case AudioDriverType_ALSAAudioDriver:
|
---|
238 | #endif
|
---|
239 | #endif /* RT_OS_LINUX */
|
---|
240 | #ifdef RT_OS_DARWIN
|
---|
241 | case AudioDriverType_CoreAudioDriver:
|
---|
242 | #endif
|
---|
243 | #ifdef RT_OS_OS2
|
---|
244 | case AudioDriverType_MMPMAudioDriver:
|
---|
245 | #endif
|
---|
246 | {
|
---|
247 | mData.backup();
|
---|
248 | mData->mAudioDriver = aAudioDriver;
|
---|
249 | break;
|
---|
250 | }
|
---|
251 |
|
---|
252 | default:
|
---|
253 | {
|
---|
254 | AssertMsgFailed (("Wrong audio driver type %d\n",
|
---|
255 | aAudioDriver));
|
---|
256 | rc = E_FAIL;
|
---|
257 | }
|
---|
258 | }
|
---|
259 | }
|
---|
260 |
|
---|
261 | return rc;
|
---|
262 | }
|
---|
263 |
|
---|
264 | // IAudioAdapter methods
|
---|
265 | /////////////////////////////////////////////////////////////////////////////
|
---|
266 |
|
---|
267 | // public methods only for internal purposes
|
---|
268 | /////////////////////////////////////////////////////////////////////////////
|
---|
269 |
|
---|
270 | /**
|
---|
271 | * @note Locks this object for writing.
|
---|
272 | */
|
---|
273 | bool AudioAdapter::rollback()
|
---|
274 | {
|
---|
275 | /* sanity */
|
---|
276 | AutoCaller autoCaller (this);
|
---|
277 | AssertComRCReturn (autoCaller.rc(), false);
|
---|
278 |
|
---|
279 | AutoLock alock (this);
|
---|
280 |
|
---|
281 | bool changed = false;
|
---|
282 |
|
---|
283 | if (mData.isBackedUp())
|
---|
284 | {
|
---|
285 | /* we need to check all data to see whether anything will be changed
|
---|
286 | * after rollback */
|
---|
287 | changed = mData.hasActualChanges();
|
---|
288 | mData.rollback();
|
---|
289 | }
|
---|
290 |
|
---|
291 | return changed;
|
---|
292 | }
|
---|
293 |
|
---|
294 | /**
|
---|
295 | * @note Locks this object for writing, together with the peer object (also
|
---|
296 | * for writing) if there is one.
|
---|
297 | */
|
---|
298 | void AudioAdapter::commit()
|
---|
299 | {
|
---|
300 | /* sanity */
|
---|
301 | AutoCaller autoCaller (this);
|
---|
302 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
303 |
|
---|
304 | /* sanity too */
|
---|
305 | AutoCaller thatCaller (mPeer);
|
---|
306 | AssertComRCReturnVoid (thatCaller.rc());
|
---|
307 |
|
---|
308 | /* lock both for writing since we modify both */
|
---|
309 | AutoMultiLock <2> alock (this->wlock(), AutoLock::maybeWlock (mPeer));
|
---|
310 |
|
---|
311 | if (mData.isBackedUp())
|
---|
312 | {
|
---|
313 | mData.commit();
|
---|
314 | if (mPeer)
|
---|
315 | {
|
---|
316 | /* attach new data to the peer and reshare it */
|
---|
317 | mPeer->mData.attach (mData);
|
---|
318 | }
|
---|
319 | }
|
---|
320 | }
|
---|
321 |
|
---|
322 | /**
|
---|
323 | * @note Locks this object for writing, together with the peer object
|
---|
324 | * represented by @a aThat (locked for reading).
|
---|
325 | */
|
---|
326 | void AudioAdapter::copyFrom (AudioAdapter *aThat)
|
---|
327 | {
|
---|
328 | AssertReturnVoid (aThat != NULL);
|
---|
329 |
|
---|
330 | /* sanity */
|
---|
331 | AutoCaller autoCaller (this);
|
---|
332 | AssertComRCReturnVoid (autoCaller.rc());
|
---|
333 |
|
---|
334 | /* sanity too */
|
---|
335 | AutoCaller thatCaller (mPeer);
|
---|
336 | AssertComRCReturnVoid (thatCaller.rc());
|
---|
337 |
|
---|
338 | /* peer is not modified, lock it for reading */
|
---|
339 | AutoMultiLock <2> alock (this->wlock(), aThat->rlock());
|
---|
340 |
|
---|
341 | /* this will back up current data */
|
---|
342 | mData.assignCopy (aThat->mData);
|
---|
343 | }
|
---|