Jamoma API  0.6.0.a19
ComponentBase.cpp
1 /* Copyright 2007 Apple Inc. All Rights Reserved.
2 
3  Disclaimer: IMPORTANT: This Apple software is supplied to you by
4  Apple Inc. ("Apple") in consideration of your agreement to the
5  following terms, and your use, installation, modification or
6  redistribution of this Apple software constitutes acceptance of these
7  terms. If you do not agree with these terms, please do not use,
8  install, modify or redistribute this Apple software.
9 
10  In consideration of your agreement to abide by the following terms, and
11  subject to these terms, Apple grants you a personal, non-exclusive
12  license, under Apple's copyrights in this original Apple software (the
13  "Apple Software"), to use, reproduce, modify and redistribute the Apple
14  Software, with or without modifications, in source and/or binary forms;
15  provided that if you redistribute the Apple Software in its entirety and
16  without modifications, you must retain this notice and the following
17  text and disclaimers in all such redistributions of the Apple Software.
18  Neither the name, trademarks, service marks or logos of Apple Inc.
19  may be used to endorse or promote products derived from the Apple
20  Software without specific prior written permission from Apple. Except
21  as expressly stated in this notice, no other rights or licenses, express
22  or implied, are granted by Apple herein, including but not limited to
23  any patent rights that may be infringed by your derivative works or by
24  other works in which the Apple Software may be incorporated.
25 
26  The Apple Software is provided by Apple on an "AS IS" basis. APPLE
27  MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
28  THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
29  FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
30  OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
31 
32  IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
33  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
34  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
35  INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
36  MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
37  AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
38  STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
39  POSSIBILITY OF SUCH DAMAGE.
40 */
41 #include "ComponentBase.h"
42 #include "CAXException.h"
43 
44 #if TARGET_OS_MAC
45 pthread_mutex_t ComponentInitLocker::sComponentOpenMutex = PTHREAD_MUTEX_INITIALIZER;
46 #elif TARGET_OS_WIN32
47 CAGuard ComponentInitLocker::sComponentOpenGuard("sComponentOpenGuard");
48 #endif
49 
50 #if CA_DO_NOT_USE_AUDIO_COMPONENT
51  #include <dlfcn.h>
52 #endif
53 
54 ComponentBase::~ComponentBase()
55 {
56 }
57 
58 void ComponentBase::PostConstructor()
59 {
60 }
61 
62 void ComponentBase::PreDestructor()
63 {
64 }
65 
66 #if !TARGET_OS_IPHONE
67 OSStatus ComponentBase::Version()
68 {
69  return 0x00000001;
70 }
71 
72 OSStatus ComponentBase::ComponentEntryDispatch(ComponentParameters *p, ComponentBase *This)
73 {
74  if (This == NULL) return paramErr;
75 
76  OSStatus result = noErr;
77 
78  switch (p->what) {
79  case kComponentCloseSelect:
80  This->PreDestructor();
81  delete This;
82  break;
83 
84  case kComponentVersionSelect:
85  result = This->Version();
86  break;
87 
88  case kComponentCanDoSelect:
89  switch (p->params[0]) {
90  case kComponentOpenSelect:
91  case kComponentCloseSelect:
92  case kComponentVersionSelect:
93  case kComponentCanDoSelect:
94  return 1;
95  default:
96  return 0;
97  }
98 
99  default:
100  result = badComponentSelector;
101  break;
102  }
103  return result;
104 }
105 #endif
106 
107 #if CA_DO_NOT_USE_AUDIO_COMPONENT
108 static OSStatus ComponentBase_GetComponentDescription (const AudioComponentInstance & inInstance, AudioComponentDescription &outDesc);
109 #endif
110 
111 AudioComponentDescription ComponentBase::GetComponentDescription() const
112 {
113  AudioComponentDescription desc;
114  OSStatus result;
115 
116 #if CA_DO_NOT_USE_AUDIO_COMPONENT
117  ca_require_noerr (result = ComponentBase_GetComponentDescription (mComponentInstance, desc), home);
118 #else
119  AudioComponent comp = AudioComponentInstanceGetComponent(mComponentInstance);
120  XAssert (comp);
121  if (comp) {
122  ca_require_noerr(result = AudioComponentGetDescription(comp, &desc), home);
123  } else
124  ca_require_noerr(result = -1, home);
125 #endif
126 
127 home:
128  if (result)
129  memset (&desc, 0, sizeof(AudioComponentDescription));
130  return desc;
131 }
132 
133 
134 #if CA_DO_NOT_USE_AUDIO_COMPONENT
135 OSStatus ComponentBase_GetComponentDescription (const AudioComponentInstance & inInstance, AudioComponentDescription & outDesc)
136 {
137  // we prefer to use the new API. If it is not available however, we have to go back to using the ComponentMgr one.
138  typedef AudioComponent (*AudioComponentInstanceGetComponentProc) (AudioComponentInstance);
139  static AudioComponentInstanceGetComponentProc aciGCProc = NULL;
140 
141  typedef OSStatus (*AudioComponentGetDescriptionProc)(AudioComponent, AudioComponentDescription *);
142  static AudioComponentGetDescriptionProc acGDProc = NULL;
143 
144  typedef OSErr (*GetComponentInfoProc)(Component, ComponentDescription *, void*, void*, void*);
145  static GetComponentInfoProc gciProc = NULL;
146 
147  static int doneInit = 0;
148  if (doneInit == 0) {
149  doneInit = 1;
150  bool loadCMgr = true;
151 
152  void* theImage = dlopen("/System/Library/Frameworks/AudioUnit.framework/AudioUnit", RTLD_LAZY);
153  if (theImage != NULL)
154  {
155  // we assume that all routine names passed here have a leading underscore which gets shaved
156  // off when passed to dlsym
157  aciGCProc = (AudioComponentInstanceGetComponentProc)dlsym (theImage, "AudioComponentInstanceGetComponent");
158  if (aciGCProc) {
159  acGDProc = (AudioComponentGetDescriptionProc)dlsym (theImage, "AudioComponentGetDescription");
160  if (acGDProc)
161  loadCMgr = false;
162  }
163  }
164  if (loadCMgr) {
165  theImage = dlopen("/System/Library/Frameworks/CoreServices.framework/CoreServices", RTLD_LAZY);
166  if (theImage != NULL)
167  {
168  gciProc = (GetComponentInfoProc)dlsym (theImage, "GetComponentInfo");
169  }
170  }
171  }
172 
173  OSStatus result = noErr;
174  if (acGDProc && aciGCProc) {
175  AudioComponent comp = (*aciGCProc)(inInstance);
176  XAssert (comp);
177  if (comp) {
178  ca_require_noerr(result = (*acGDProc)(comp, &outDesc), home);
179  } else
180  ca_require_noerr(result = -1, home);
181 
182  } else if (gciProc) {
183  // in this case we know that inInstance is directly castable to a Component
184  ca_require_noerr(result = (*gciProc)((Component)inInstance, (ComponentDescription*)&outDesc, NULL, NULL, NULL), home);
185  }
186 home:
187  return result;
188 }
189 #endif //CA_DO_NOT_USE_AUDIO_COMPONENT