Jamoma API  0.6.0.a19
AUInputElement.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 "AUBase.h"
42 
43 inline bool HasGoodBufferPointers(const AudioBufferList &abl, UInt32 nBytes)
44 {
45  const AudioBuffer *buf = abl.mBuffers;
46  for (UInt32 i = abl.mNumberBuffers; i--;++buf) {
47  if (buf->mData == NULL || buf->mDataByteSize < nBytes)
48  return false;
49  }
50  return true;
51 }
52 
53 
54 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55 // AUInputElement::AUInputElement
56 //
57 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
58 AUInputElement::AUInputElement(AUBase *audioUnit) :
59  AUIOElement(audioUnit),
60  mInputType(kNoInput)
61 {
62 }
63 
64 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65 // AUInputElement::SetConnection
66 //
67 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
68 void AUInputElement::SetConnection(const AudioUnitConnection &conn)
69 {
70  if (conn.sourceAudioUnit == 0) {
71  Disconnect();
72  return;
73  }
74 
75  mInputType = kFromConnection;
76  mConnection = conn;
77  mConnRenderProc = NULL;
78  AllocateBuffer();
79 
80 #if !CA_AU_IS_ONLY_PLUGIN
81  UInt32 size = sizeof(AudioUnitRenderProc);
82  OSStatus result = AudioUnitGetProperty( conn.sourceAudioUnit,
83  kAudioUnitProperty_FastDispatch,
84  kAudioUnitScope_Global,
85  kAudioUnitRenderSelect,
86  &mConnRenderProc,
87  &size);
88  if (result == noErr)
89  mConnInstanceStorage = GetComponentInstanceStorage (conn.sourceAudioUnit);
90  else
91  mConnRenderProc = NULL;
92 #else // !TARGET_OS_IPHONE
93  mConnInstanceStorage = NULL;
94  mConnRenderProc = NULL;
95 #endif
96 }
97 
98 void AUInputElement::Disconnect()
99 {
100  mInputType = kNoInput;
101  mIOBuffer.Deallocate();
102 }
103 
104 
105 
106 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
107 // AUInputElement::SetInputCallback
108 //
109 //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
110 void AUInputElement::SetInputCallback(AURenderCallback proc, void *refCon)
111 {
112  if (proc == NULL)
113  Disconnect();
114  else {
115  mInputType = kFromCallback;
116  mInputProc = proc;
117  mInputProcRefCon = refCon;
118  AllocateBuffer();
119  }
120 }
121 
122 OSStatus AUInputElement::SetStreamFormat(const CAStreamBasicDescription &fmt)
123 {
124  OSStatus err = AUIOElement::SetStreamFormat(fmt);
125  if (err == AUBase::noErr)
126  AllocateBuffer();
127  return err;
128 }
129 
130 OSStatus AUInputElement::PullInput( AudioUnitRenderActionFlags & ioActionFlags,
131  const AudioTimeStamp & inTimeStamp,
132  AudioUnitElement inElement,
133  UInt32 nFrames)
134 {
135  if (!IsActive())
136  return kAudioUnitErr_NoConnection;
137 
138  AudioBufferList *pullBuffer;
139 
140  if (HasConnection() || !WillAllocateBuffer())
141  pullBuffer = &mIOBuffer.PrepareNullBuffer(mStreamFormat, nFrames);
142  else
143  pullBuffer = &mIOBuffer.PrepareBuffer(mStreamFormat, nFrames);
144 
145  return PullInputWithBufferList (ioActionFlags, inTimeStamp, inElement, nFrames, pullBuffer);
146 }