1 /*
2      File: ComponentBase.cpp
3  Abstract: ComponentBase.h
4   Version: 1.1
5 
6  Disclaimer: IMPORTANT:  This Apple software is supplied to you by Apple
7  Inc. ("Apple") in consideration of your agreement to the following
8  terms, and your use, installation, modification or redistribution of
9  this Apple software constitutes acceptance of these terms.  If you do
10  not agree with these terms, please do not use, install, modify or
11  redistribute this Apple software.
12 
13  In consideration of your agreement to abide by the following terms, and
14  subject to these terms, Apple grants you a personal, non-exclusive
15  license, under Apple's copyrights in this original Apple software (the
16  "Apple Software"), to use, reproduce, modify and redistribute the Apple
17  Software, with or without modifications, in source and/or binary forms;
18  provided that if you redistribute the Apple Software in its entirety and
19  without modifications, you must retain this notice and the following
20  text and disclaimers in all such redistributions of the Apple Software.
21  Neither the name, trademarks, service marks or logos of Apple Inc. may
22  be used to endorse or promote products derived from the Apple Software
23  without specific prior written permission from Apple.  Except as
24  expressly stated in this notice, no other rights or licenses, express or
25  implied, are granted by Apple herein, including but not limited to any
26  patent rights that may be infringed by your derivative works or by other
27  works in which the Apple Software may be incorporated.
28 
29  The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
30  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
31  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
32  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
33  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
34 
35  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
36  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
37  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
38  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
39  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
40  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
41  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
42  POSSIBILITY OF SUCH DAMAGE.
43 
44  Copyright (C) 2014 Apple Inc. All Rights Reserved.
45 
46 */
47 #include "ComponentBase.h"
48 #include "CAXException.h"
49 
50 #if TARGET_OS_MAC
51 pthread_mutex_t ComponentInitLocker::sComponentOpenMutex = PTHREAD_MUTEX_INITIALIZER;
52 pthread_once_t ComponentInitLocker::sOnce = PTHREAD_ONCE_INIT;
53 
InitComponentInitLocker()54 void ComponentInitLocker::InitComponentInitLocker()
55 {
56 	// have to do this because OS X lacks PTHREAD_MUTEX_RECURSIVE_INITIALIZER_NP
57 	pthread_mutexattr_t attr;
58 	pthread_mutexattr_init(&attr);
59 	pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE);
60 	pthread_mutex_init(&sComponentOpenMutex, &attr);
61 	pthread_mutexattr_destroy(&attr);
62 }
63 
64 #elif TARGET_OS_WIN32
65 CAGuard	ComponentInitLocker::sComponentOpenGuard("sComponentOpenGuard");
66 #endif
67 
68 ComponentBase::EInstanceType ComponentBase::sNewInstanceType;
69 
70 static OSStatus CB_GetComponentDescription (const AudioComponentInstance inInstance, AudioComponentDescription * outDesc);
71 #if !CA_USE_AUDIO_PLUGIN_ONLY && !TARGET_OS_WIN32
72 	static OSStatus CMgr_GetComponentDescription (const AudioComponentInstance inInstance, AudioComponentDescription * outDesc);
73 #endif
74 
ComponentBase(AudioComponentInstance inInstance)75 ComponentBase::ComponentBase(AudioComponentInstance inInstance)
76 	: mComponentInstance(inInstance),
77 	  mInstanceType(sNewInstanceType)
78 {
79 	GetComponentDescription();
80 }
81 
~ComponentBase()82 ComponentBase::~ComponentBase()
83 {
84 }
85 
PostConstructor()86 void			ComponentBase::PostConstructor()
87 {
88 }
89 
PreDestructor()90 void			ComponentBase::PreDestructor()
91 {
92 }
93 
94 #define ACPI	((AudioComponentPlugInInstance *)self)
95 #define ACImp	((ComponentBase *)&ACPI->mInstanceStorage)
96 
AP_Open(void * self,AudioUnit compInstance)97 OSStatus ComponentBase::AP_Open(void *self, AudioUnit compInstance)
98 {
99 	OSStatus result = noErr;
100 	try {
101 		ComponentInitLocker lock;
102 
103 		ComponentBase::sNewInstanceType = ComponentBase::kAudioComponentInstance;
104 		ComponentBase *cb = (ComponentBase *)(*ACPI->mConstruct)(&ACPI->mInstanceStorage, compInstance);
105 		cb->PostConstructor();	// allows base class to do additional initialization
106 		// once the derived class is fully constructed
107 		result = noErr;
108 	}
109 	COMPONENT_CATCH
110 	if (result)
111 		delete ACPI;
112 	return result;
113 }
114 
AP_Close(void * self)115 OSStatus ComponentBase::AP_Close(void *self)
116 {
117 	OSStatus result = noErr;
118 	try {
119 		if (ACImp) {
120 			ACImp->PreDestructor();
121 			(*ACPI->mDestruct)(&ACPI->mInstanceStorage);
122 			free(self);
123 		}
124 	}
125 	COMPONENT_CATCH
126 	return result;
127 }
128 
129 #if !CA_USE_AUDIO_PLUGIN_ONLY
Version()130 OSStatus		ComponentBase::Version()
131 {
132 	return 0x00000001;
133 }
134 
ComponentEntryDispatch(ComponentParameters * p,ComponentBase * This)135 OSStatus		ComponentBase::ComponentEntryDispatch(ComponentParameters *p, ComponentBase *This)
136 {
137 	if (This == NULL) return kAudio_ParamError;
138 
139 	OSStatus result = noErr;
140 
141 	switch (p->what) {
142 	case kComponentCloseSelect:
143 		This->PreDestructor();
144 		delete This;
145 		break;
146 
147 	case kComponentVersionSelect:
148 		result = This->Version();
149 		break;
150 
151 	case kComponentCanDoSelect:
152 		switch (GetSelectorForCanDo(p)) {
153 		case kComponentOpenSelect:
154 		case kComponentCloseSelect:
155 		case kComponentVersionSelect:
156 		case kComponentCanDoSelect:
157 			return 1;
158 		default:
159 			return 0;
160 		}
161 
162 	default:
163 		result = badComponentSelector;
164 		break;
165 	}
166 	return result;
167 }
168 
GetSelectorForCanDo(ComponentParameters * params)169 SInt16		ComponentBase::GetSelectorForCanDo(ComponentParameters *params)
170 {
171 	if (params->what != kComponentCanDoSelect) return 0;
172 
173 	#if TARGET_CPU_X86
174 		SInt16 sel = params->params[0];
175 	#elif TARGET_CPU_X86_64
176 		SInt16 sel = params->params[1];
177 	#elif TARGET_CPU_PPC
178 		SInt16 sel = (params->params[0] >> 16);
179 	#else
180 		SInt16 sel = params->params[0];
181 	#endif
182 
183 	return sel;
184 /*
185 		printf ("flags:%d, paramSize: %d, what: %d\n\t", params->flags, params->paramSize, params->what);
186 		for (int i = 0; i < params->paramSize; ++i) {
187 			printf ("[%d]:%d(0x%x), ", i, params->params[i], params->params[i]);
188 		}
189 		printf("\n\tsel:%d\n", sel);
190 */
191 }
192 
193 #endif
194 
195 #if CA_DO_NOT_USE_AUDIO_COMPONENT
196 static OSStatus ComponentBase_GetComponentDescription (const AudioComponentInstance & inInstance, AudioComponentDescription &outDesc);
197 #endif
198 
GetComponentDescription() const199 AudioComponentDescription ComponentBase::GetComponentDescription() const
200 {
201 	AudioComponentDescription desc;
202 	OSStatus result = 1;
203 
204 	if (IsPluginObject()) {
205 		ca_require_noerr(result = CB_GetComponentDescription (mComponentInstance, &desc), home);
206 	}
207 #if !CA_USE_AUDIO_PLUGIN_ONLY
208 	else {
209 		ca_require_noerr(result = CMgr_GetComponentDescription (mComponentInstance, &desc), home);
210 	}
211 #endif
212 
213 home:
214 	if (result)
215 		memset (&desc, 0, sizeof(AudioComponentDescription));
216 
217 	return desc;
218 }
219 
220 #if CA_USE_AUDIO_PLUGIN_ONLY
221 // everything we need is there and we should be linking against it
CB_GetComponentDescription(const AudioComponentInstance inInstance,AudioComponentDescription * outDesc)222 static OSStatus CB_GetComponentDescription (const AudioComponentInstance inInstance, AudioComponentDescription * outDesc)
223 {
224 	AudioComponent comp = AudioComponentInstanceGetComponent(inInstance);
225 	if (comp)
226 		return AudioComponentGetDescription(comp, outDesc);
227 
228 	return kAudio_ParamError;
229 }
230 
231 #elif !TARGET_OS_WIN32
232 // these are the direct dependencies on ComponentMgr calls that an AU
233 // that is a component mgr is dependent on
234 
235 // these are dynamically loaded so that these calls will work on Leopard
236 #include <dlfcn.h>
237 
CB_GetComponentDescription(const AudioComponentInstance inInstance,AudioComponentDescription * outDesc)238 static OSStatus CB_GetComponentDescription (const AudioComponentInstance inInstance, AudioComponentDescription * outDesc)
239 {
240 	typedef AudioComponent (*AudioComponentInstanceGetComponentProc) (AudioComponentInstance);
241 	static AudioComponentInstanceGetComponentProc aciGCProc = NULL;
242 
243 	typedef OSStatus (*AudioComponentGetDescriptionProc)(AudioComponent, AudioComponentDescription *);
244 	static AudioComponentGetDescriptionProc acGDProc = NULL;
245 
246 	static int doneInit = 0;
247 	if (doneInit == 0) {
248 		doneInit = 1;
249 		void* theImage = dlopen("/System/Library/Frameworks/AudioUnit.framework/AudioUnit", RTLD_LAZY);
250 		if (theImage != NULL)
251 		{
252 			aciGCProc = (AudioComponentInstanceGetComponentProc)dlsym (theImage, "AudioComponentInstanceGetComponent");
253 			if (aciGCProc) {
254 				acGDProc = (AudioComponentGetDescriptionProc)dlsym (theImage, "AudioComponentGetDescription");
255 			}
256 		}
257 	}
258 
259 	OSStatus result = kAudio_UnimplementedError;
260 	if (acGDProc && aciGCProc) {
261 		AudioComponent comp = (*aciGCProc)(inInstance);
262 		if (comp)
263 			result = (*acGDProc)(comp, outDesc);
264 	}
265 #if !CA_USE_AUDIO_PLUGIN_ONLY
266 	else {
267 		result = CMgr_GetComponentDescription (inInstance, outDesc);
268 	}
269 #endif
270 
271 	return result;
272 }
273 
274 #if !CA_USE_AUDIO_PLUGIN_ONLY
275 // these are the direct dependencies on ComponentMgr calls that an AU
276 // that is a component mgr is dependent on
277 
278 // these are dynamically loaded
279 
280 #include <CoreServices/CoreServices.h>
281 #include <AudioUnit/AudioUnit.h>
282 #include "CAXException.h"
283 #include "ComponentBase.h"
284 
285 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
286 // Component Manager
287 // Used for fast dispatch with audio units
288 typedef Handle (*GetComponentInstanceStorageProc)(ComponentInstance aComponentInstance);
289 static GetComponentInstanceStorageProc sGetComponentInstanceStorageProc = NULL;
290 
291 typedef OSErr (*GetComponentInfoProc)(Component, ComponentDescription *, void*, void*, void*);
292 static GetComponentInfoProc sGetComponentInfoProc = NULL;
293 
294 typedef void (*SetComponentInstanceStorageProc)(ComponentInstance, Handle);
295 static SetComponentInstanceStorageProc sSetComponentInstanceStorageProc = NULL;
296 
297 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
298 
CSInitOnce(void *)299 static void CSInitOnce(void* /*unused*/)
300 {
301 	void *theImage = dlopen("/System/Library/Frameworks/CoreServices.framework/CoreServices", RTLD_LAZY);
302 	if (!theImage) return;
303 
304 	sGetComponentInstanceStorageProc = (GetComponentInstanceStorageProc) dlsym(theImage, "GetComponentInstanceStorage");
305 	sGetComponentInfoProc = (GetComponentInfoProc)dlsym (theImage, "GetComponentInfo");
306 	sSetComponentInstanceStorageProc = (SetComponentInstanceStorageProc) dlsym(theImage, "SetComponentInstanceStorage");
307 }
308 
309 #if TARGET_OS_MAC
310 
311 #include <dispatch/dispatch.h>
312 
313 static dispatch_once_t sCSInitOnce = 0;
314 
CSInit()315 static void CSInit ()
316 {
317 	dispatch_once_f(&sCSInitOnce, NULL, CSInitOnce);
318 }
319 
320 #else
321 
CSInit()322 static void CSInit ()
323 {
324 	static int sDoCSLoad = 1;
325 	if (sDoCSLoad) {
326 		sDoCSLoad = 0;
327 		CSInitOnce(NULL);
328 	}
329 }
330 
331 #endif
332 
CMgr_GetComponentDescription(const AudioComponentInstance inInstance,AudioComponentDescription * outDesc)333 OSStatus CMgr_GetComponentDescription (const AudioComponentInstance inInstance, AudioComponentDescription * outDesc)
334 {
335 	CSInit();
336 	if (sGetComponentInfoProc)
337 		return (*sGetComponentInfoProc)((Component)inInstance, (ComponentDescription*)outDesc, NULL, NULL, NULL);
338 	return kAudio_UnimplementedError;
339 }
340 
CMgr_GetComponentInstanceStorage(ComponentInstance aComponentInstance)341 Handle CMgr_GetComponentInstanceStorage(ComponentInstance aComponentInstance)
342 {
343 	CSInit();
344 	if (sGetComponentInstanceStorageProc)
345 		return (*sGetComponentInstanceStorageProc)(aComponentInstance);
346 	return NULL;
347 }
348 
CMgr_SetComponentInstanceStorage(ComponentInstance aComponentInstance,Handle theStorage)349 void CMgr_SetComponentInstanceStorage(ComponentInstance aComponentInstance, Handle theStorage)
350 {
351 	CSInit();
352 	if (sSetComponentInstanceStorageProc)
353 		(*sSetComponentInstanceStorageProc)(aComponentInstance, theStorage);
354 }
355 #endif // !CA_USE_AUDIO_PLUGIN_ONLY
356 
357 #else
358 //#include "ComponentManagerDependenciesWin.h"
359 // everything we need is there and we should be linking against it
CB_GetComponentDescription(const AudioComponentInstance inInstance,AudioComponentDescription * outDesc)360 static OSStatus CB_GetComponentDescription (const AudioComponentInstance inInstance, AudioComponentDescription * outDesc)
361 {
362 	AudioComponent comp = AudioComponentInstanceGetComponent(inInstance);
363 	if (comp)
364 		return AudioComponentGetDescription(comp, outDesc);
365 
366 	return kAudio_ParamError;
367 }
368 
369 #endif
370 
371