Jamoma API  0.6.0.a19
CAComponent.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 "CAComponent.h"
42 #include "CAComponentDescription.h"
43 #include "CACFDictionary.h"
44 #include <stdlib.h>
45 #include "CAAutoDisposer.h"
46 
47 CAComponent::CAComponent (const AudioComponentDescription& inDesc, CAComponent* next)
48  : mManuName(0), mAUName(0), mCompName(0)
49 {
50  mComp = AudioComponentFindNext ((next ? next->Comp() : NULL), &inDesc);
51  if (mComp)
52  AudioComponentGetDescription(Comp(), &mDesc);
53  else
54  memcpy (&mDesc, &inDesc, sizeof(AudioComponentDescription));
55 }
56 
57 CAComponent::CAComponent (const AudioComponent& comp)
58  : mComp (comp),
59  mManuName(0),
60  mAUName(0),
61  mCompName(0)
62 {
63  AudioComponentGetDescription (Comp(), &mDesc);
64 }
65 
66 CAComponent::CAComponent (const AudioComponentInstance& compInst)
67  : mComp (NULL),
68  mManuName(0),
69  mAUName(0),
70  mCompName(0)
71 {
72  mComp = AudioComponentInstanceGetComponent (compInst);
73  AudioComponentGetDescription (Comp(), &mDesc);
74 }
75 
76 CAComponent::CAComponent (OSType inType, OSType inSubtype, OSType inManu)
77  : mDesc (inType, inSubtype, inManu),
78  mManuName(0), mAUName(0), mCompName(0)
79 {
80  mComp = AudioComponentFindNext (NULL, &mDesc);
81  AudioComponentGetDescription (Comp(), &mDesc);
82 }
83 
84 CAComponent::~CAComponent ()
85 {
86  Clear();
87 }
88 
89 OSStatus CAComponent::GetVersion (UInt32 &outVersion) const
90 {
91  return AudioComponentGetVersion (mComp, &outVersion);
92 }
93 
94 void CAComponent::Clear ()
95 {
96  if (mManuName) { CFRelease (mManuName); mManuName = 0; }
97  if (mAUName) { CFRelease (mAUName); mAUName = 0; }
98  if (mCompName) { CFRelease (mCompName); mCompName = 0; }
99 }
100 
101 CAComponent& CAComponent::operator= (const CAComponent& y)
102 {
103  Clear();
104 
105  mComp = y.mComp;
106  mDesc = y.mDesc;
107 
108  if (y.mManuName) { mManuName = y.mManuName; CFRetain (mManuName); }
109  if (y.mAUName) { mAUName = y.mAUName; CFRetain (mAUName); }
110  if (y.mCompName) { mCompName = y.mCompName; CFRetain (mCompName); }
111 
112  return *this;
113 }
114 
115 void CAComponent::SetCompNames () const
116 {
117  if (!mCompName) {
118 
119  CFStringRef compName;
120  OSStatus result = AudioComponentCopyName (Comp(), &compName);
121  if (result) return;
122 
123  const_cast<CAComponent*>(this)->mCompName = compName;
124  if (compName)
125  {
126  CFArrayRef splitStrArray = CFStringCreateArrayBySeparatingStrings(NULL, compName, CFSTR(":"));
127 
128  // we need to retain these values so the strings are not lost when the array is released
129  CFRetain(CFArrayGetValueAtIndex(splitStrArray, 0));
130  const_cast<CAComponent*>(this)->mManuName = (CFStringRef)CFArrayGetValueAtIndex(splitStrArray, 0);
131  if (CFArrayGetCount(splitStrArray) > 1)
132  {
133  CFStringRef str = (CFStringRef)CFArrayGetValueAtIndex(splitStrArray, 1);
134 
135  CFMutableStringRef mstr = CFStringCreateMutableCopy (NULL, CFStringGetLength(str), str);
136 
137  // this needs to trim out white space:
138 
139  CFStringTrimWhitespace (mstr);
140 
141  const_cast<CAComponent*>(this)->mAUName = mstr;
142  } else
143  const_cast<CAComponent*>(this)->mAUName = NULL;
144 
145  CFRelease(splitStrArray);
146  }
147  }
148 }
149 
150 static void _ShowCF (FILE* file, CFStringRef str)
151 {
152  if (CFGetTypeID(str) != CFStringGetTypeID()) {
153  CFShow(str);
154  return;
155  }
156 
157  UInt32 len = CFStringGetLength(str);
158  char* chars = (char*)CA_malloc (len * 2); // give us plenty of room for unichar chars
159  if (CFStringGetCString (str, chars, len * 2, kCFStringEncodingUTF8))
160  fprintf (file, "%s", chars);
161  else
162  CFShow (str);
163 
164  free (chars);
165 }
166 
167 void CAComponent::Print(FILE* file) const
168 {
169  fprintf (file, "CAComponent: %p", Comp());
170  if (mManuName) {
171  fprintf (file, ", Manu:"); _ShowCF (file, mManuName);
172  if (mAUName) fprintf (file, ", Name:"); _ShowCF (file, mAUName);
173  }
174  fprintf (file, ", ");
175  Desc ().Print(file);
176 }