-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppleUSBAudioStream.h
executable file
·342 lines (276 loc) · 13 KB
/
AppleUSBAudioStream.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
/*
* Copyright (c) 1998-2010 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* This file contains Original Code and/or Modifications of Original Code
* as defined in and that are subject to the Apple Public Source License
* Version 2.0 (the 'License'). You may not use this file except in
* compliance with the License. Please obtain a copy of the License at
* http://www.opensource.apple.com/apsl/ and read it before using this
* file.
*
* The Original Code and all software distributed under the License are
* distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
* EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
* INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
* Please see the License for the specific language governing rights and
* limitations under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
//--------------------------------------------------------------------------------
//
// File: DJM03AudioStream.h
//
// Contains: Support for the USB Audio Class Stream Interface.
//
// Technology: Mac OS X
//
//--------------------------------------------------------------------------------
#ifndef _DJM03AudioSTREAM_H
#define _DJM03AudioSTREAM_H
#include <libkern/OSByteOrder.h>
#include <libkern/c++/OSCollectionIterator.h>
#include <libkern/c++/OSMetaClass.h>
#include <IOKit/IOLib.h>
#include <IOKit/IOService.h>
#include <IOKit/IOMessage.h>
#include <IOKit/IOMemoryCursor.h>
#include <IOKit/IOMemoryDescriptor.h>
#include <IOKit/IOBufferMemoryDescriptor.h>
#ifdef __LP64__
#include <IOKit/IOSubMemoryDescriptor.h>
#endif /* !__LP64__ */
#include <IOKit/IOTimerEventSource.h>
#include <IOKit/IOMultiMemoryDescriptor.h>
#include <IOKit/audio/IOAudioDevice.h>
#include <IOKit/audio/IOAudioPort.h>
#include <IOKit/audio/IOAudioTypes.h>
#include <IOKit/audio/IOAudioDefines.h>
#include <IOKit/audio/IOAudioLevelControl.h>
#include <IOKit/audio/IOAudioEngine.h>
#include <IOKit/audio/IOAudioStream.h>
#include <IOKit/usb/IOUSBPipe.h>
#include <IOKit/usb/IOUSBDevice.h>
#include <IOKit/usb/IOUSBInterface.h>
#include "AppleUSBAudioCommon.h"
#include "AppleUSBAudioDevice.h"
#include "AppleUSBAudioDictionary.h"
#include "AppleUSBAudioClip.h"
class DJM03AudioDevice;
// -----------------------------------------------------------------
#define kSampleRate_96000 96000
#define kDefaultSamplingRate kSampleRate_96000
#define kBitDepth_24bits 24
#define kChannelDepth_MONO 1
#define kChannelDepth_STEREO 2
#define kFixedPoint10_14ByteSize 3
#define kFixedPoint16_16ByteSize 4
#define kMinimumFrameOffset 6
#define RECORD_NUM_USB_FRAME_LISTS 32 //8,128
#define RECORD_NUM_USB_FRAMES_PER_LIST 2
#define RECORD_NUM_USB_FRAME_LISTS_TO_QUEUE 16 //4,64
#define PLAY_NUM_USB_FRAME_LISTS 4
#define PLAY_NUM_USB_FRAMES_PER_LIST 8 //8,64
#define PLAY_NUM_USB_FRAME_LISTS_TO_QUEUE 2
#define PLAY_NUM_USB_FRAMES_PER_LIST_SYNC 4 //4,32
// [rdar://5623096] Make note of the slowest polling interval in ms for feedback endpoints
#define kMaxFeedbackPollingInterval 512
#define kSampleFractionAccumulatorRollover 65536 * 1000
#define kMaxFilterSize 33 // <rdar://problem/7378275>
#define kFilterScale 1024 // <rdar://problem/7378275>
// <rdar://problem/6954295>
typedef struct _IOAudioSamplesPerFrame {
UInt32 whole;
UInt32 fraction; // This fraction is stored x 1000 to preserve precision
} IOAudioSamplesPerFrame;
// <rdar://6411577> Overruns threshold in packets (about 2ms at 48kHz, close to the safety offset value)
#define kOverrunsThreshold 100
class DJM03AudioEngine;
class DJM03AudioPlugin;
class DJM03AudioStream : public IOAudioStream {
friend class DJM03AudioDevice;
friend class DJM03AudioEngine;
OSDeclareDefaultStructors (DJM03AudioStream);
public:
virtual bool initWithAudioEngine(DJM03AudioDevice * device, DJM03AudioEngine *engine, UInt16 interfaceNumber, IOAudioSampleRate sampleRate, const char *streamDescription = NULL, OSDictionary *properties = 0);
// virtual bool initWithAudioEngine(DJM03AudioDevice * device, DJM03AudioEngine *engine, IOUSBInterface * streamInterface, IOAudioSampleRate sampleRate, const char *streamDescription = NULL, OSDictionary *properties = 0);
virtual void free ();
// virtual bool initHardware (IOService *provider);
virtual void stop (IOService *provider);
virtual bool requestTerminate (IOService * provider, IOOptionBits options);
virtual bool terminate (IOOptionBits options = 0);
virtual bool matchPropertyTable(OSDictionary * table, SInt32 *score);
virtual void registerService(IOOptionBits options = 0); // <rdar://7295322>
virtual IOReturn setFormat(const IOAudioStreamFormat *streamFormat, bool callDriver = true); // <rdar://7259238>
virtual IOReturn setFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, OSDictionary *formatDict, bool callDriver = true); // <rdar://7259238>
virtual bool configureAudioStream (IOAudioSampleRate sampleRate);
virtual bool openStreamInterface ( void );
virtual void closeStreamInterface ( void );
virtual IOReturn pluginDeviceRequest (IOUSBDevRequest * request, IOUSBCompletion * completion);
virtual void pluginSetConfigurationApp (const char * bundleID);
virtual void registerPlugin (DJM03AudioPlugin * thePlugin);
static void pluginLoaded (DJM03AudioStream * usbAudioStreamObject);
virtual UInt32 getRateFromSamplesPerPacket ( IOAudioSamplesPerFrame samplesPerPacket ); // <rdar://problem/6954295>
static void sampleRateHandler (void * target, void * parameter, IOReturn result, IOUSBIsocFrame * pFrames);
#if PRIMEISOCINPUT
static void primeInputPipeHandler (void * object, void * parameter, IOReturn result, IOUSBLowLatencyIsocFrame * pFrames);
#endif
static void readHandler (void * object, void * frameListIndex, IOReturn result, IOUSBLowLatencyIsocFrame * pFrames);
static void writeHandler (void * object, void * parameter, IOReturn result, IOUSBLowLatencyIsocFrame * pFrames);
static void writeHandlerForUHCI (void * object, void * parameter, IOReturn result, IOUSBLowLatencyIsocFrame * pFrames);
protected:
volatile UInt32 mCurrentFrameList;
volatile UInt32 mShouldStop;
UInt64 mUSBFrameToQueue;
UInt64 mNextSyncReadFrame;
UInt64 * mFrameQueuedForList;
// <rdar://problem/7378275>
UInt64 mLastRawTimeStamp_nanos;
UInt64 mLastFilteredTimeStamp_nanos;
UInt64 mLastFilteredStampDifference;
UInt64 mLastWrapFrame;
Boolean mInitStampDifference;
UInt32 mNumTimestamp;
UInt64 mFilterData[kMaxFilterSize];
UInt32 mFilterWritePointer;
#if DEBUGTIMESTAMPS
SInt64 mStampDrift;
#endif
bool mSplitTransactions;
IORecursiveLock * mCoalescenceMutex; // <rdar://problem/7378275>
IOUSBLowLatencyIsocFrame * mUSBIsocFrames;
IOUSBIsocFrame mSampleRateFrame;
IOUSBLowLatencyIsocCompletion * mUSBCompletion;
IOUSBIsocCompletion mSampleRateCompletion;
IOUSBInterface * mStreamInterface;
IOUSBPipe * mPipe;
IOUSBPipe * mAssociatedPipe;
DJM03AudioDevice * mUSBAudioDevice;
DJM03AudioEngine * mUSBAudioEngine;
IOBufferMemoryDescriptor * mUSBBufferDescriptor;
IOBufferMemoryDescriptor * mSampleBufferMemoryDescriptor;
#if PRIMEISOCINPUT
IOUSBLowLatencyIsocFrame * mPrimeInputIsocFrames;
IOUSBLowLatencyIsocCompletion mPrimeInputCompletion;
#endif
IOMultiMemoryDescriptor * mWrapRangeDescriptor;
IOSubMemoryDescriptor * mWrapDescriptors[2];
IOSubMemoryDescriptor ** mSampleBufferDescriptors;
IOBufferMemoryDescriptor * mAssociatedEndpointMemoryDescriptor; // <rdar://7000283>
bool mMasterMode;
bool mSyncCompensation;
thread_call_t mPluginInitThread;
DJM03AudioPlugin * mPlugin;
// UHCI additions
UInt32 mSampleBufferSizeExtended;
UInt16 mNumFramesInFirstList;
IOUSBLowLatencyIsocCompletion mExtraUSBCompletion;
UInt16 mAverageFrameSize; // These sizes are in bytes.
UInt16 mAlternateFrameSize;
UInt16 mReadUSBFrameSize;
// end UHCI additions
void * mReadBuffer;
UInt32 * mAverageSampleRateBuffer; // needs to be 4 bytes for a 10.14 or 16.16 value
IOAudioSampleRate mCurSampleRate;
UInt32 mLastPreparedBufferOffset;
UInt32 mSafeErasePoint;
UInt32 mLastSafeErasePoint;
UInt32 mReadUSBFrameListSize;
UInt32 mBufferOffset;
IOAudioSamplesPerFrame mSamplesPerPacket; // store this as a 16.16 value <rdar://problem/6954295>
UInt32 mNumUSBFrameLists;
UInt32 mNumUSBFramesPerList;
UInt32 mNumTransactionsPerList;
UInt32 mNumUSBFrameListsToQueue;
UInt32 mSampleBufferSize;
UInt32 mBytesPerSampleFrame;
UInt32 mFractionalSamplesLeft;
#if DEBUGLATENCY
UInt32 mLastFrameListSize;
UInt32 mThisFrameListSize;
#endif
UInt16 mSampleSize;
UInt16 mSampleBitWidth;
UInt32 mNumChannels;
UInt16 mFramesUntilRefresh;
UInt8 mInterfaceNumber;
UInt8 mAlternateSettingID;
UInt8 mRefreshInterval;
UInt8 mFeedbackPacketSize;
UInt8 mDirection;
UInt8 mTransactionsPerUSBFrame;
Boolean mInCompletion;
Boolean mUSBStreamRunning;
Boolean mTerminatingDriver;
Boolean mUHCISupport;
OSArray * mActiveClockPath;
Boolean mNeedTimeStamps;
bool mHaveTakenFirstTimeStamp;
// [rdar://5417631] Keep track of devices that generate overruns on input
bool mGeneratesOverruns;
UInt32 mOverrunsCount; // <rdar://6902105>
UInt32 mOverrunsThreshold; // <rdar://6411577>
UInt64 mNumSampleRateFeedbackChangesCounter;
UInt64 mNumSampleRateFeedbackEqualCounter;
UInt16 mVendorID;
UInt16 mProductID;
// Default stream format and sample rate are stored.
IOAudioStreamFormat mDefaultAudioStreamFormat;
IOAudioSampleRate mDefaultAudioSampleRate;
bool mSw441;
static inline IOFixed IOUFixedDivide(UInt32 a, UInt32 b)
{
return (IOFixed)((((UInt64) a) << 16) / ((UInt64) b));
}
static inline UInt32 IOUFixedMultiply(UInt32 a, UInt32 b)
{
return (UInt32)((((UInt64) a) * ((UInt64) b)) >> 16);
}
IOReturn PrepareWriteFrameList (UInt32 usbFrameListIndex);
IOReturn PrepareAndReadFrameLists (UInt8 sampleSize, UInt8 numChannels, UInt32 usbFrameListIndex);
IOReturn setSampleRateControl (UInt8 address, UInt32 sampleRate);
IOReturn addAvailableFormats (DJM03ConfigurationDictionary * configDictionary);
IOReturn checkForFeedbackEndpoint (DJM03ConfigurationDictionary * configDictionary);
void queueInputFrames (void);
void queueOutputFrames (void);
UInt16 getAlternateFrameSize (void);
UInt8 getSyncType (void);
UInt32 getLockDelayFrames (void);
void initializeUSBFrameList ( IOUSBLowLatencyIsocFrame * usbIsocFrames, UInt32 numFrames ); // <rdar://7568547>
void setMasterStreamMode ( bool masterMode ) { mMasterMode = masterMode; }
void compensateForSynchronization ( bool syncCompensation ) { mSyncCompensation = syncCompensation; }
IOReturn GetDefaultSettings (UInt8 * altSettingID, IOAudioSampleRate * sampleRate); // added for rdar://3866513
virtual bool willTerminate (IOService * provider, IOOptionBits options);
virtual IOReturn readFrameList (UInt32 frameListNum);
virtual IOReturn writeFrameList (UInt32 frameListNum);
virtual IOReturn prepareUSBStream (void);
virtual IOReturn startUSBStream (UInt64 currentUSBFrame, UInt32 usbFramesToDelay);
virtual IOReturn stopUSBStream ();
virtual UInt32 getCurrentSampleFrame (void);
virtual IOReturn CoalesceInputSamples (UInt32 numBytesToCoalesce, IOUSBLowLatencyIsocFrame * pFrames);
virtual IOReturn controlledFormatChange (const IOAudioStreamFormat *newFormat, const IOAudioSampleRate *newSampleRate);
void calculateSamplesPerPacket (UInt32 sampleRate, UInt16 * averageFrameSize, UInt16 * additionalSampleFrameFreq);
void updateSampleOffsetAndLatency (void);
#if DEBUGLATENCY
virtual UInt64 getQueuedFrameForSample (UInt32 sampleFrame);
#endif
#if PRIMEISOCINPUT
virtual void primeInputPipe (IOUSBPipe * pipeToPrime, UInt32 bytesPerUSBFrame, UInt32 usbFramesToDelay);
#endif
virtual UInt64 generateTimeStamp (SInt32 usbFrameIndex, UInt32 preWrapBytes, UInt32 byteCount);
virtual IOReturn copyAnchor (UInt64 anchorFrame, UInt64 * anchorTime, UInt64 * usbCycleTime); // <rdar://problem/7378275>
virtual UInt64 jitterFilter (UInt64 curr, UInt32 nIter); // <rdar://problem/7378275>
virtual void takeTimeStamp (bool incrementLoopCount = true, UInt64 *timestamp = NULL);
// for IODMACommand
virtual IOBufferMemoryDescriptor * allocateBufferDescriptor (IOOptionBits options, vm_size_t capacity, vm_offset_t alignment = 1);
};
class DJM03AudioStreamNode : public IOService //<radr://6686515>
{
OSDeclareDefaultStructors ( DJM03AudioStreamNode );
public:
virtual bool start ( IOService * provider );
};
#endif /* _DJM03AudioSTREAM_H */