/
AppleUSBAudioStream.cpp
3793 lines (3255 loc) · 152 KB
/
AppleUSBAudioStream.cpp
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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: AppleUSBAudioStream.cpp
//
// Contains: Support for the USB Audio Class Stream Interface.
// This includes support for setting sample rate (via
// a sample rate endpoint control and appropriate
// sized construction of USB isochronous frame lists),
// channel depth selection and bit depth selection.
//
// Technology: Mac OS X
//
//--------------------------------------------------------------------------------
#include "AppleUSBAudioStream.h"
#include "AppleUSBAudioEngine.h"
#include "AppleUSBAudioPlugin.h"
#define super IOAudioStream
OSDefineMetaClassAndStructors(AppleUSBAudioStream, IOAudioStream)
#pragma mark -IOKit Routines-
void AppleUSBAudioStream::free () {
UInt32 i;
debugIOLog ("+ AppleUSBAudioStream[%p]::free ()", this);
if (NULL != mCoalescenceMutex)
{
IORecursiveLockFree (mCoalescenceMutex); // <rdar://problem/7378275>
mCoalescenceMutex = NULL;
}
if (NULL != mFrameQueuedForList)
{
delete [] mFrameQueuedForList;
mFrameQueuedForList = NULL;
}
if (mAverageSampleRateBuffer)
{
// <rdar://7000283> Pointer was obtained from mAssociatedEndpointMemoryDescriptor, so no need to free it
// explicitly here.
mAverageSampleRateBuffer = NULL;
}
if (mAssociatedEndpointMemoryDescriptor)
{
mAssociatedEndpointMemoryDescriptor->release ();
mAssociatedEndpointMemoryDescriptor = NULL;
}
if (mUSBBufferDescriptor)
{
mUSBBufferDescriptor->release ();
mUSBBufferDescriptor = NULL;
}
if (mWrapRangeDescriptor)
{
mWrapRangeDescriptor->release ();
mWrapDescriptors[0]->release ();
mWrapDescriptors[1]->release ();
mWrapRangeDescriptor = NULL;
}
if (mSampleBufferMemoryDescriptor)
{
mSampleBufferMemoryDescriptor->release ();
mSampleBufferMemoryDescriptor = NULL;
}
if (NULL != mSampleBufferDescriptors)
{
for (i = 0; i < mNumUSBFrameLists; i++)
{
if (NULL != mSampleBufferDescriptors[i])
{
mSampleBufferDescriptors[i]->release ();
mSampleBufferDescriptors[i] = NULL;
}
}
IOFree (mSampleBufferDescriptors, mNumUSBFrameLists * sizeof (IOSubMemoryDescriptor *));
mSampleBufferDescriptors = NULL;
}
if (NULL != mUSBIsocFrames)
{
IOFree (mUSBIsocFrames, mNumUSBFrameLists * mNumTransactionsPerList * sizeof (IOUSBLowLatencyIsocFrame));
mUSBIsocFrames = NULL;
}
if (NULL != mUSBCompletion)
{
IOFree (mUSBCompletion, mNumUSBFrameLists * sizeof (IOUSBLowLatencyIsocCompletion));
mUSBCompletion = NULL;
}
if (mUSBAudioDevice)
{
mUSBAudioDevice->release ();
mUSBAudioDevice = NULL;
}
if (mUSBAudioEngine)
{
mUSBAudioEngine->release ();
mUSBAudioEngine = NULL;
}
if (mStreamInterface)
{
mStreamInterface->release ();
mStreamInterface = NULL;
}
super::free ();
debugIOLog ("- AppleUSBAudioStream[%p]::free()", this);
}
bool AppleUSBAudioStream::initWithAudioEngine (AppleUSBAudioDevice * device, AppleUSBAudioEngine * engine, IOUSBInterface * streamInterface, IOAudioSampleRate sampleRate, const char *streamDescription, OSDictionary *properties) {
AUAConfigurationDictionary * configDictionary;
Boolean result = false;
UInt32 startChannelID;
debugIOLog("+ AppleUSBAudioStream[%p]::initWithAudioEngine ()", this);
FailIf (NULL == device, Exit);
FailIf (NULL == engine, Exit);
FailIf (NULL == streamInterface, Exit);
result = FALSE;
mUSBAudioDevice = device;
mUSBAudioDevice->retain ();
mUSBAudioEngine = engine;
mUSBAudioEngine->retain ();
mStreamInterface = streamInterface;
mStreamInterface->retain ();
mInterfaceNumber = mStreamInterface->GetInterfaceNumber ();
debugIOLog ("? AppleUSBAudioStream[%p]::initWithAudioEngine () - mInterfaceNumber = %d", this, mInterfaceNumber);
mVendorID = mUSBAudioDevice->getVendorID ();
mProductID = mUSBAudioDevice->getProductID ();
FailIf (NULL == (configDictionary = mUSBAudioDevice->getConfigDictionary ()), Exit);
// Choose default alternate setting ID and sampling rate ( rdar://3866513 )
FailIf (kIOReturnSuccess != GetDefaultSettings (&mAlternateSettingID, &sampleRate), Exit);
FailIf (kIOReturnSuccess != configDictionary->getIsocEndpointDirection (&mDirection, mInterfaceNumber, mAlternateSettingID), Exit);
startChannelID = ( kIOAudioStreamDirectionOutput == mDirection ) ? mUSBAudioEngine->mStartOutputChannelID : mUSBAudioEngine->mStartInputChannelID;
FailIf (FALSE == super::initWithAudioEngine (engine, (IOAudioStreamDirection)mDirection, startChannelID, streamDescription, properties), Exit);
// Safeguard against USB-Audio 2.0 descriptors for rdar://4798933.
// Check both INTERFACE_PROTOCOL_UNDEFINED and IP_PROTOCOL_VERSION to protect against unsupported
// protocols.
FailIf ((INTERFACE_PROTOCOL_UNDEFINED != mStreamInterface->GetInterfaceProtocol()) && (IP_VERSION_02_00 != mStreamInterface->GetInterfaceProtocol()), Exit);
// Change this to use defines from the IOAudioFamily when they are available
setProperty ("IOAudioStreamSampleFormatByteOrder", "Little Endian");
// Attach to the stream interface so we get willTerminate message.
attach ( mStreamInterface );
mInitStampDifference = true; // <rdar://problem/7378275>
result = TRUE;
Exit:
debugIOLog("- AppleUSBAudioStream[%p]::initWithAudioEngine ()", this);
return result;
}
bool AppleUSBAudioStream::requestTerminate (IOService * provider, IOOptionBits options) {
bool result;
debugIOLog ("+ AppleUSBAudioStream[%p]::requestTerminate (%p, %x)", this, provider, options);
// if interface or audio device
if ( mUSBAudioEngine == provider || mStreamInterface == provider )
{
result = TRUE; // it is OK to terminate us
}
else
{
result = FALSE; // don't terminate us
}
debugIOLog ("- AppleUSBAudioStream[%p]::requestTerminate (%p, %x) = %d", this, provider, options, result);
return result;
}
void AppleUSBAudioStream::stop (IOService * provider) {
debugIOLog("+ AppleUSBAudioStream[%p]::stop (%p)", this, provider);
if (NULL != mPluginInitThread)
{
thread_call_cancel (mPluginInitThread);
thread_call_free (mPluginInitThread);
mPluginInitThread = NULL;
}
if (mPlugin)
{
mPlugin->close (this);
mPlugin = NULL;
}
if (mUSBAudioDevice)
{
mUSBAudioDevice->release ();
mUSBAudioDevice = NULL;
}
if (mUSBAudioEngine)
{
mUSBAudioEngine->release ();
mUSBAudioEngine = NULL;
}
if (mPipe)
{
mPipe->release ();
mPipe = NULL;
}
if (mAssociatedPipe)
{
mAssociatedPipe->release ();
mAssociatedPipe = NULL;
}
// [rdar://4287899] We don't expect the stream interface to need closing unless the following conditions are true.
if ( (mStreamInterface)
&& ( (provider == mUSBAudioEngine) || (provider == mStreamInterface) )
&& (mStreamInterface->isOpen()))
{
debugIOLog ("! AppleUSBAudioStream[%p]::stop () - mStreamInterface was still open when stop() was called. Closing ...", this);
mStreamInterface->close (this);
mStreamInterface->release ();
mStreamInterface = NULL;
}
//Exit:
super::stop (provider);
debugIOLog ("- AppleUSBAudioStream[%p]::stop (%p) - rc=%ld", this, provider, getRetainCount());
}
bool AppleUSBAudioStream::terminate (IOOptionBits options) {
bool shouldTerminate;
bool result;
result = TRUE;
shouldTerminate = TRUE;
debugIOLog ("+ AppleUSBAudioStream[%p]::terminate ()", this);
if (shouldTerminate)
{
result = super::terminate (options);
}
debugIOLog ("- AppleUSBAudioStream[%p]::terminate ()", this);
return result;
}
bool AppleUSBAudioStream::matchPropertyTable(OSDictionary * table, SInt32 *score)
{
bool returnValue = false;
//debugIOLog ("+ AppleUSBAudioStream[%p]::matchPropertyTable (%p, %p)", this, table, score);
if (super::matchPropertyTable (table, score))
{
if (compareProperty (table, kIDVendorString) &&
compareProperty (table, kIDProductString) &&
compareProperty (table, kIOAudioStreamDirectionKey))
{
returnValue = true;
}
}
//debugIOLog ("- AppleUSBAudioStream[%p]::matchPropertyTable (%p, %p) = %d", this, table, score, returnValue);
return returnValue;
}
// <rdar://7295322> Asynchronous to prevent deadlock if the device or interface is terminated while
// registerService() is performing matching.
void AppleUSBAudioStream::registerService(IOOptionBits options)
{
debugIOLog ("+ AppleUSBAudioStream[%p]::registerService ( 0x%lx )", this, options);
if ( 0 == ( kIOServiceSynchronous & options ) )
{
options |= kIOServiceAsynchronous;
}
super::registerService ( options );
debugIOLog ("- AppleUSBAudioStream[%p]::registerService ( 0x%lx )", this, options);
}
#pragma mark -USB Audio driver-
IOReturn AppleUSBAudioStream::addAvailableFormats (AUAConfigurationDictionary * configDictionary)
{
IOAudioStreamFormat streamFormat;
IOAudioStreamFormatExtension streamFormatExtension;
IOAudioSampleRate lowSampleRate;
IOAudioSampleRate highSampleRate;
OSArray * sampleRates = NULL;
OSObject * arrayObject = NULL;
OSNumber * arrayNumber = NULL;
IOReturn result = kIOReturnError;
UInt32 thisSampleRate;
UInt32 otherSampleRate;
UInt16 format;
UInt8 numAltInterfaces;
UInt8 numSampleRates;
UInt8 altSettingIndex;
UInt8 numChannels;
UInt8 rateIndex;
UInt8 candidateAC3AltSetting;
Boolean hasNativeAC3Format;
Boolean hasDigitalOutput = FALSE;
Boolean isClockSourceProgrammable = TRUE; // <rdar://5811247>
debugIOLog ("+ AppleUSBAudioStream[%p]::addAvailableFormats (%p)", this, configDictionary);
FailIf (NULL == mUSBAudioDevice, Exit); // <rdar://7085810>
FailIf (NULL == mUSBAudioEngine, Exit); // <rdar://7085810>
FailIf (NULL == mUSBAudioDevice->mControlInterface, Exit); // <rdar://7085810>
FailIf (NULL == configDictionary, Exit);
FailIf (kIOReturnSuccess != configDictionary->getNumAltSettings (&numAltInterfaces, mInterfaceNumber), Exit);
debugIOLog ("? AppleUSBAudioStream[%p]::addAvailableFormats () - There are %d alternate interfaces @ interface %d", this, numAltInterfaces, mInterfaceNumber);
hasNativeAC3Format = FALSE;
candidateAC3AltSetting = 0;
// <rdar://5811247>
if ( ( IP_VERSION_02_00 == mUSBAudioDevice->mControlInterface->GetInterfaceProtocol() ) && ( 0 != mUSBAudioEngine->mCurrentClockSourceID ) )
{
isClockSourceProgrammable = configDictionary->clockSourceHasFrequencyControl ( mUSBAudioDevice->mControlInterface->GetInterfaceNumber (), 0, mUSBAudioEngine->mCurrentClockSourceID, true );
}
// Find all of the available formats on the device.
altSettingIndex = configDictionary->alternateSettingZeroCanStream (mInterfaceNumber) ? 0 : 1;
for ( ; altSettingIndex < numAltInterfaces; altSettingIndex++)
{
// <rdar://5811247>If the clock source is present, use that to find out if the clock source is programmable.
// If it is programmable, then do what it is done now. If it is not programmable, then only 1 sample rate is supported, which
// is the sample rate of the clock source.
if ( isClockSourceProgrammable )
{
// [rdar://5067229]
if ( kIOReturnSuccess != configDictionary->getNumSampleRates (&numSampleRates, mInterfaceNumber, altSettingIndex) )
{
continue;
}
sampleRates = configDictionary->getSampleRates (mInterfaceNumber, altSettingIndex);
}
else
{
numSampleRates = 0;
sampleRates = NULL;
}
// [rdar://5284099] Check the format before deciding whether to retrieve the following values.
FailIf (kIOReturnSuccess != configDictionary->getFormat (&format, mInterfaceNumber, altSettingIndex), Exit);
if ( ( PCM == format )
|| ( IEC1937_AC3 == format ) )
{
FailIf (kIOReturnSuccess != configDictionary->getNumChannels (&numChannels, mInterfaceNumber, altSettingIndex), Exit);
FailIf (kIOReturnSuccess != configDictionary->getBitResolution (&(streamFormat.fBitDepth), mInterfaceNumber, altSettingIndex), Exit);
FailIf (kIOReturnSuccess != configDictionary->getSubframeSize (&(streamFormat.fBitWidth), mInterfaceNumber, altSettingIndex), Exit);
}
else
{
numChannels = 0;
}
streamFormat.fNumChannels = numChannels;
streamFormat.fBitWidth *= 8;
streamFormat.fAlignment = kIOAudioStreamAlignmentLowByte;
streamFormat.fByteOrder = kIOAudioStreamByteOrderLittleEndian;
streamFormat.fDriverTag = (mInterfaceNumber << 16) | altSettingIndex;
streamFormatExtension.fVersion = kFormatExtensionCurrentVersion;
streamFormatExtension.fFlags = 0;
streamFormatExtension.fFramesPerPacket = 1;
streamFormatExtension.fBytesPerPacket = numChannels * (streamFormat.fBitWidth / 8);
switch (format)
{
case PCM:
streamFormat.fSampleFormat = kIOAudioStreamSampleFormatLinearPCM;
streamFormat.fNumericRepresentation = kIOAudioStreamNumericRepresentationSignedInt;
streamFormat.fIsMixable = TRUE;
if (2 == streamFormat.fNumChannels && 16 == streamFormat.fBitDepth && 16 == streamFormat.fBitWidth)
{
candidateAC3AltSetting = altSettingIndex;
}
break;
case AC3: // just starting to stub something in for AC-3 support
debugIOLog ("? AppleUSBAudioStream[%p]::addAvailableFormats () - Variable bit rate AC-3 audio format type", this);
continue; // We're not supporting this at the moment, so just skip it.
streamFormat.fSampleFormat = kIOAudioStreamSampleFormatAC3;
streamFormat.fIsMixable = FALSE;
streamFormat.fNumChannels = 6;
streamFormat.fNumericRepresentation = kIOAudioStreamNumericRepresentationSignedInt;
streamFormat.fBitDepth = 16;
streamFormat.fBitWidth = 16;
streamFormat.fByteOrder = kIOAudioStreamByteOrderBigEndian;
FailIf (kIOReturnSuccess != configDictionary->getAC3BSID (&(streamFormatExtension.fFlags), mInterfaceNumber, altSettingIndex), Exit);
// streamFormatExtension.fFlags = USBToHostLong (configDictionary->GetAC3BSID (mInterfaceNumber, altSettingIndex));
// streamFormatExtension.fFramesPerPacket = configDictionary->GetSamplesPerFrame (mInterfaceNumber, altSettingIndex);
streamFormatExtension.fFramesPerPacket = 1536;
// streamFormatExtension.fBytesPerPacket = ((configDictionary->GetMaxBitRate (mInterfaceNumber, altSettingIndex) * 1024 / 8) + 500) / 1000;
streamFormatExtension.fBytesPerPacket = streamFormatExtension.fFramesPerPacket * streamFormat.fNumChannels * (streamFormat.fBitWidth / 8);
break;
case IEC1937_AC3:
debugIOLog ("? AppleUSBAudioStream[%p]::addAvailableFormats () - IEC1937 AC-3 audio format type", this);
hasNativeAC3Format = TRUE;
streamFormat.fSampleFormat = kIOAudioStreamSampleFormat1937AC3;
streamFormat.fNumericRepresentation = kIOAudioStreamNumericRepresentationSignedInt;
streamFormat.fIsMixable = FALSE;
streamFormatExtension.fFramesPerPacket = 1536;
streamFormatExtension.fBytesPerPacket = streamFormatExtension.fFramesPerPacket * streamFormat.fNumChannels * (streamFormat.fBitWidth / 8);
break;
default:
debugIOLog ("? AppleUSBAudioStream[%p]::addAvailableFormats () - Interface format = 0x%x not published.", this, format);
continue; // skip this alternate interface
}
debugIOLog ("? AppleUSBAudioStream[%p]::addAvailableFormats () - Interface %d, Alt %d has a ", this, mInterfaceNumber, altSettingIndex);
debugIOLog (" %d bit interface, ", streamFormat.fBitDepth);
debugIOLog (" %d channel(s), and ", streamFormat.fNumChannels);
debugIOLog (" %d sample rate(s), which is/are:", numSampleRates);
if (numSampleRates && sampleRates)
{
for (rateIndex = 0; rateIndex < numSampleRates; rateIndex++)
{
FailIf (NULL == (arrayObject = sampleRates->getObject (rateIndex)), Exit);
FailIf (NULL == (arrayNumber = OSDynamicCast (OSNumber, arrayObject)), Exit);
thisSampleRate = arrayNumber->unsigned32BitValue();
debugIOLog (" %d", thisSampleRate);
lowSampleRate.whole = thisSampleRate;
lowSampleRate.fraction = 0;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &lowSampleRate);
if (kIOAudioStreamSampleFormatLinearPCM == streamFormat.fSampleFormat)
{
streamFormat.fIsMixable = FALSE;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &lowSampleRate);
streamFormat.fIsMixable = TRUE; // set it back to TRUE for next time through the loop
}
}
debugIOLog ("");
}
else if (sampleRates)
{
FailIf (NULL == (arrayObject = sampleRates->getObject (0)), Exit);
FailIf (NULL == (arrayNumber = OSDynamicCast (OSNumber, arrayObject)), Exit);
thisSampleRate = arrayNumber->unsigned32BitValue();
FailIf (NULL == (arrayObject = sampleRates->getObject (1)), Exit);
FailIf (NULL == (arrayNumber = OSDynamicCast (OSNumber, arrayObject)), Exit);
otherSampleRate = arrayNumber->unsigned32BitValue();
debugIOLog (" %d to %d", thisSampleRate, otherSampleRate);
lowSampleRate.whole = thisSampleRate;
lowSampleRate.fraction = 0;
highSampleRate.whole = otherSampleRate;
highSampleRate.fraction = 0;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &highSampleRate);
if (kIOAudioStreamSampleFormatLinearPCM == streamFormat.fSampleFormat)
{
streamFormat.fIsMixable = FALSE;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &highSampleRate);
}
}
else
{
// <rdar://5811247>
if ( !isClockSourceProgrammable )
{
OSArray * clockPath = NULL;
OSArray * clockPathGroup = mUSBAudioDevice->getClockPathGroup ( mInterfaceNumber, altSettingIndex );
FailIf ( NULL == clockPathGroup, Exit );
FailIf ( NULL == ( clockPath = OSDynamicCast ( OSArray, clockPathGroup->getObject ( mUSBAudioEngine->mCurrentClockPathIndex - 1 ) ) ), Exit );
if ( kIOReturnSuccess == mUSBAudioDevice->getClockPathCurSampleRate ( &thisSampleRate, NULL, NULL, clockPath ) ) // <rdar://6945472>
{
debugIOLog (" %d", thisSampleRate);
lowSampleRate.whole = thisSampleRate;
lowSampleRate.fraction = 0;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &lowSampleRate);
}
}
}
} // for altSettingIndex
/*
configDictionary->getOutputTerminalType (&terminalType, mUSBAudioDevice->mControlInterface->GetInterfaceNumber (), 0, altSettingIndex);
switch (terminalType)
{
case EXTERNAL_DIGITAL_AUDIO_INTERFACE:
case EXTERNAL_SPDIF_INTERFACE:
case EMBEDDED_DVD_AUDIO:
hasDigitalOutput = TRUE;
break;
default:
hasDigitalOutput = FALSE;
}
*/
if (TRUE == hasDigitalOutput && FALSE == hasNativeAC3Format && 0 != candidateAC3AltSetting && kIOAudioStreamDirectionOutput == getDirection ())
{
FailIf (kIOReturnSuccess != configDictionary->getNumSampleRates (&numSampleRates, mInterfaceNumber, candidateAC3AltSetting), Exit);
sampleRates = configDictionary->getSampleRates (mInterfaceNumber, candidateAC3AltSetting);
FailIf (kIOReturnSuccess != configDictionary->getNumChannels (&numChannels, mInterfaceNumber, candidateAC3AltSetting), Exit);
streamFormat.fNumChannels = numChannels;
FailIf (kIOReturnSuccess != configDictionary->getBitResolution (&(streamFormat.fBitDepth), mInterfaceNumber, candidateAC3AltSetting), Exit);
FailIf (kIOReturnSuccess != configDictionary->getSubframeSize (&(streamFormat.fBitWidth), mInterfaceNumber, candidateAC3AltSetting), Exit);
streamFormat.fBitWidth *= 8;
streamFormat.fAlignment = kIOAudioStreamAlignmentLowByte;
streamFormat.fByteOrder = kIOAudioStreamByteOrderLittleEndian;
streamFormat.fDriverTag = (mInterfaceNumber << 16) | candidateAC3AltSetting;
streamFormat.fSampleFormat = kIOAudioStreamSampleFormat1937AC3;
streamFormat.fNumericRepresentation = kIOAudioStreamNumericRepresentationSignedInt;
streamFormat.fIsMixable = FALSE;
streamFormatExtension.fVersion = kFormatExtensionCurrentVersion;
streamFormatExtension.fFlags = 0;
streamFormatExtension.fFramesPerPacket = 1536;
streamFormatExtension.fBytesPerPacket = streamFormatExtension.fFramesPerPacket * streamFormat.fNumChannels * (streamFormat.fBitWidth / 8);
if (numSampleRates && sampleRates)
{
for (rateIndex = 0; rateIndex < numSampleRates; rateIndex++)
{
FailIf (NULL == (arrayObject = sampleRates->getObject (rateIndex)), Exit);
FailIf (NULL == (arrayNumber = OSDynamicCast (OSNumber, arrayObject)), Exit);
thisSampleRate = arrayNumber->unsigned32BitValue();
lowSampleRate.whole = thisSampleRate;
lowSampleRate.fraction = 0;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &lowSampleRate);
}
}
else if (sampleRates)
{
FailIf (NULL == (arrayObject = sampleRates->getObject (0)), Exit);
FailIf (NULL == (arrayNumber = OSDynamicCast (OSNumber, arrayObject)), Exit);
thisSampleRate = arrayNumber->unsigned32BitValue();
FailIf (NULL == (arrayObject = sampleRates->getObject (1)), Exit);
FailIf (NULL == (arrayNumber = OSDynamicCast (OSNumber, arrayObject)), Exit);
otherSampleRate = arrayNumber->unsigned32BitValue();
lowSampleRate.whole = thisSampleRate;
lowSampleRate.fraction = 0;
highSampleRate.whole = otherSampleRate;
highSampleRate.fraction = 0;
this->addAvailableFormat (&streamFormat, &streamFormatExtension, &lowSampleRate, &highSampleRate);
}
} // if hasDigitalOutput ...
result = kIOReturnSuccess;
Exit:
debugIOLog ("- AppleUSBAudioStream[%p]::addAvailableFormats (%p) = 0x%x", this, configDictionary, result);
return result;
}
// <rdar://7259238>
IOReturn AppleUSBAudioStream::setFormat(const IOAudioStreamFormat *streamFormat, bool callDriver)
{
IOReturn result;
debugIOLog ("- AppleUSBAudioStream[%p]::setFormat (%p, %d)", this, streamFormat, callDriver );
result = super::setFormat ( streamFormat, callDriver );
debugIOLog ("- AppleUSBAudioStream[%p]::setFormat (%p, %d) = 0x%x", this, streamFormat, callDriver, result);
return result;
}
// <rdar://7259238>
IOReturn AppleUSBAudioStream::setFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, OSDictionary *formatDict, bool callDriver)
{
IOReturn result = kIOReturnError;
bool streamIsRunning = false;
FailIf ( 0 == mUSBAudioEngine, Exit );
debugIOLog ("- AppleUSBAudioStream[%p]::setFormat (%p, %p, %p, %d)", this, streamFormat, formatExtension, formatDict, callDriver );
streamIsRunning = mUSBStreamRunning;
if (streamIsRunning)
{
mUSBAudioEngine->pauseAudioEngine ();
}
mUSBAudioEngine->beginConfigurationChange ();
result = super::setFormat ( streamFormat, formatExtension, formatDict, callDriver );
if ( kIOReturnSuccess == result )
{
debugIOLog ("? AppleUSBAudioStream[%p]::setFormat (%p, %p, %p, %d) - Delaying %u ms...", this, streamFormat, formatExtension, formatDict, callDriver, kFormatChangeDelayInMs );
// Wait a bit after format change so that the USB audio device has a chance to catch up.
IOSleep ( kFormatChangeDelayInMs );
}
// Send an engine change notification so that the HAL refreshes its settings.
mUSBAudioEngine->completeConfigurationChange ();
if (streamIsRunning)
{
mUSBAudioEngine->resumeAudioEngine ();
}
Exit:
debugIOLog ("- AppleUSBAudioStream[%p]::setFormat (%p, %p, %p, %d) = 0x%x", this, streamFormat, formatExtension, formatDict, callDriver, result);
return result;
}
// [rdar://4487489] - Use this method to allocate all USB buffers.
IOBufferMemoryDescriptor * AppleUSBAudioStream::allocateBufferDescriptor (IOOptionBits options, vm_size_t capacity, vm_offset_t alignment)
{
IOBufferMemoryDescriptor * bufferDescriptorPtr = NULL;
#ifdef IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND
mach_vm_address_t physicalMask;
IOOptionBits usbOptions;
IOUSBControllerV2 * usbController;
#endif
debugIOLog ("+ AppleUSBAudioStream[%p]::allocateBufferDescriptor ()", this);
FailIf (NULL == mStreamInterface, Exit);
#ifdef IOMEMORYDESCRIPTOR_SUPPORTS_DMACOMMAND
FailIf (NULL == (usbController = OSDynamicCast (IOUSBControllerV2, mStreamInterface->GetDevice()->GetBus ())), Exit);
// The following API call was introduced in IOUSBFamily 2.6.0b6 [rdar://4492080]
FailIf (kIOReturnSuccess != usbController->GetLowLatencyOptionsAndPhysicalMask (&usbOptions, &physicalMask), Exit);
options |= usbOptions;
debugIOLog ("? AppleUSBAudioStream[%p]::allocateBufferDescriptor () - allocating a buffer with mask 0x%x", this, physicalMask);
bufferDescriptorPtr = IOBufferMemoryDescriptor::inTaskWithPhysicalMask(kernel_task, options, capacity, physicalMask);
#else
if ( mUHCISupport )
{
options |= kIOMemoryPhysicallyContiguous;
}
bufferDescriptorPtr = IOBufferMemoryDescriptor::withOptions ( options, capacity, alignment );
#endif
Exit:
debugIOLog ("- AppleUSBAudioStream[%p]::allocateBufferDescriptor () = %p", this, bufferDescriptorPtr);
return bufferDescriptorPtr;
}
void AppleUSBAudioStream::calculateSamplesPerPacket (UInt32 sampleRate, UInt16 * averageFrameSamples, UInt16 * additionalSampleFrameFreq)
{
UInt32 divisor;
UInt32 modulus;
// [rdar://4801012] For USB 2.0 audio, packets correspond to microframes.
if ( mTransactionsPerUSBFrame )
{
modulus = 1000 * mTransactionsPerUSBFrame;
}
else
{
modulus = 1000;
}
* averageFrameSamples = sampleRate / modulus;
// [rdar://5600254] For UAC2.0, the following calculation is not useful.
// Aside from iSub, there should be no reason to use additionalSampleFrameFreq.
divisor = (sampleRate % modulus);
if ( divisor )
{
* additionalSampleFrameFreq = modulus / divisor;
}
else
{
* additionalSampleFrameFreq = 0;
}
// [rdar://5600254] We can log the data cadence here.
#if SHOWCADENCE
UInt32 transferSampleRate;
UInt32 currentSampleRate;
UInt32 remainder;
UInt32 accumulatedSamples = 0ul;
UInt16 transfer = 0;
UInt16 cycleLength = 0;
UInt8 powerOfTwo = 0;
UInt8 transactionsPerMS;
transactionsPerMS = mTransactionsPerUSBFrame;
while ( transactionsPerMS > 1 )
{
transactionsPerMS >>= 1;
powerOfTwo++;
}
transferSampleRate = sampleRate << ( 16 - powerOfTwo );
currentSampleRate = ( sampleRate / ( mTransactionsPerUSBFrame * 1000 ) );
currentSampleRate *= 1000;
currentSampleRate <<= 16;
remainder = transferSampleRate - currentSampleRate;
debugIOLog ( "? AppleUSBAudioStream[%p]::calculateSamplesPerPacket - transferSampleRate = 0x%8X, currentSampleRate = 0x%X", this, transferSampleRate, currentSampleRate );
transfer = 1;
cycleLength = 0;
while ( ( remainder )
&& ( ( 0 == accumulatedSamples )
|| ( accumulatedSamples % ( 1000 << 16 ) ) ) )
{
accumulatedSamples += remainder;
if ( accumulatedSamples >= ( 1000 << 16 ) )
{
debugIOLog ( "? AppleUSBAudioStream[%p]::calculateSamplesPerPacket - large packet on transfer %d (1-indexed)", this , transfer );
accumulatedSamples -= ( 1000 << 16 );
}
transfer++;
cycleLength++;
if ( ! accumulatedSamples )
{
break;
}
}
debugIOLog ( "? AppleUSBAudioStream[%p]::calculateSamplesPerPacket - cycleLength = %d", this, cycleLength );
#endif
}
IOReturn AppleUSBAudioStream::checkForFeedbackEndpoint (AUAConfigurationDictionary * configDictionary)
{
IOUSBFindEndpointRequest associatedEndpoint;
IOReturn result;
UInt16 maxPacketSize;
UInt8 assocEndpoint;
UInt8 address;
UInt8 syncType;
result = kIOReturnSuccess;
mAssociatedPipe = NULL;
FailIf (NULL == mUSBAudioDevice, Exit);
FailIf (NULL == mStreamInterface, Exit);
FailIf (configDictionary->getIsocEndpointAddress (&address, mInterfaceNumber, mAlternateSettingID, mDirection), Exit);
FailIf (configDictionary->getIsocEndpointSyncType (&syncType, mInterfaceNumber, mAlternateSettingID, address), Exit);
if (kAsynchSyncType == syncType)
{
// debugIOLog ("checking endpoint %d for an associated endpoint", address);
FailIf (kIOReturnSuccess != configDictionary->getIsocAssociatedEndpointAddress (&assocEndpoint, mInterfaceNumber, mAlternateSettingID, address), Exit);
if (assocEndpoint != 0)
{
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - assocEndpoint = 0x%x", this, assocEndpoint);
FailIf (kIOReturnSuccess != configDictionary->getIsocAssociatedEndpointRefreshInt (&mRefreshInterval, mInterfaceNumber, mAlternateSettingID, assocEndpoint), Exit);
FailIf (kIOReturnSuccess != configDictionary->getIsocAssociatedEndpointMaxPacketSize (&maxPacketSize, mInterfaceNumber, mAlternateSettingID, assocEndpoint), Exit);
if (kUSBDeviceSpeedHigh == mUSBAudioDevice->getDeviceSpeed())
{
// Request 4 bytes for the 16.16 value if the endpoint allows it
mFeedbackPacketSize = (maxPacketSize < kFixedPoint16_16ByteSize) ? maxPacketSize : kFixedPoint16_16ByteSize;
}
else
{
mFeedbackPacketSize = kFixedPoint10_14ByteSize;
}
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - Synch endpoint has refresh interval %d, feedback packet size %d", this, mRefreshInterval, mFeedbackPacketSize);
mRefreshInterval = mRefreshInterval ? mRefreshInterval : kMinimumSyncRefreshInterval;
mFramesUntilRefresh = 1 << mRefreshInterval; // the same as 2^mRefreshInterval
// If the hardware needs to be updated more often than PLAY_NUM_USB_FRAMES_PER_LIST ms, change list size to PLAY_NUM_USB_FRAMES_PER_LIST_SYNC frames.
if (mFramesUntilRefresh < mNumUSBFramesPerList)
{
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - Need to adjust mNumUSBFramesPerList: %ld < %ld", mFramesUntilRefresh, mNumUSBFramesPerList);
if (NULL != mUSBIsocFrames)
{
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - Disposing of current mUSBIsocFrames [%p]", this, mUSBIsocFrames);
IOFree (mUSBIsocFrames, mNumUSBFrameLists * mNumTransactionsPerList * sizeof (IOUSBLowLatencyIsocFrame));
mUSBIsocFrames = NULL;
}
mNumUSBFramesPerList = PLAY_NUM_USB_FRAMES_PER_LIST_SYNC;
mNumTransactionsPerList = mNumUSBFramesPerList * mTransactionsPerUSBFrame;
mNumUSBFrameLists = mNumUSBFrameListsToQueue * 2;
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - mNumUSBFramesPerList = %d, mNumUSBFrameListsToQueue = %d, mNumUSBFrameLists = %d", this, mNumUSBFramesPerList, mNumUSBFrameListsToQueue, mNumUSBFrameLists);
mUSBIsocFrames = (IOUSBLowLatencyIsocFrame *)IOMalloc (mNumUSBFrameLists * mNumTransactionsPerList * sizeof (IOUSBLowLatencyIsocFrame));
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - mUSBIsocFrames is now %p", this, mUSBIsocFrames);
FailIf (NULL == mUSBIsocFrames, Exit);
// <rdar://7568547> Initialize the USB Isoc frames so that CoalesceInputSamples() will not panic
// due to uninitialized values in frStatus & frActCount.
initializeUSBFrameList ( mUSBIsocFrames, mNumUSBFrameLists * mNumTransactionsPerList );
}
associatedEndpoint.type = kUSBIsoc;
associatedEndpoint.direction = kUSBIn; // The associated endpoint always goes "in"
// The sample rate should be either a 3-byte 10.14 or a 4-byte 16.16
associatedEndpoint.maxPacketSize = mFeedbackPacketSize;
associatedEndpoint.interval = 0xFF;
mAssociatedPipe = mStreamInterface->FindNextPipe (NULL, &associatedEndpoint);
FailWithAction (NULL == mAssociatedPipe, result = kIOReturnError, Exit);
if (NULL == mAssociatedEndpointMemoryDescriptor)
{
// <rdar://7000283> Remove the use of deprecated IOMallocContiguous(). Use IOBufferMemoryDescriptor to allocate memory instead.
mAssociatedEndpointMemoryDescriptor = IOBufferMemoryDescriptor::withOptions ( kIODirectionInOut, sizeof (UInt32), sizeof (UInt32) );
FailIf ( NULL == mAssociatedEndpointMemoryDescriptor, Exit );
mAverageSampleRateBuffer = (UInt32 *) mAssociatedEndpointMemoryDescriptor->getBytesNoCopy ();
FailIf (NULL == mAverageSampleRateBuffer, Exit);
bzero (mAverageSampleRateBuffer, sizeof (UInt32));
}
mSampleRateFrame.frStatus = -1;
mSampleRateFrame.frReqCount = mFeedbackPacketSize;
mSampleRateFrame.frActCount = 0;
mSampleRateCompletion.target = (void *)this;
mSampleRateCompletion.action = sampleRateHandler;
mSampleRateCompletion.parameter = 0;
mAssociatedPipe->retain ();
} // if (assocEndpoint != 0)
else
{
debugIOLog ("! AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - No associated synch endpoint found.", this);
}
} // if (kAsynchSyncType == syncType)
else
{
debugIOLog ("? AppleUSBAudioStream[%p]::checkForFeedbackEndpoint () - No associated synch endpoint.", this);
}
Exit:
return result;
}
// This function is called from both the IOProc's call to convertInputSamples and by the readHandler.
// To figure out where to start coalescing from, it looks at the mCurrentFrameList, which is updated by the readHandler.
// It will copy from currentFameList+1 the number of bytes requested or one USB frame list.
// When numBytesToCoalesce == 0 it will coalesce the current USB frame list (however big it is).
// If numBytesToCoalesce != 0, it will coalesce that many bytes starting from the current frame list and going to the next one if needed.
// When called from the readHandler it will just coalesce one USB frame starting from mCurrentFrameList.
// When called from convertInputSamples, it will convert the number of bytes that corresponds to the number of samples that are being asked to be converted,
// starting from mCurrentFrameList.
IOReturn AppleUSBAudioStream::CoalesceInputSamples (UInt32 numBytesToCoalesce, IOUSBLowLatencyIsocFrame * pFrames) {
IOReturn result = kIOReturnSuccess;
AbsoluteTime time;
UInt32 usbFrameIndex;
UInt32 firstUSBFrameIndex; // <rdar://6094454>
UInt32 totalNumUSBFrames; // <rdar://6094454>
UInt32 numFramesChecked;
UInt32 numBytesToCopy;
UInt32 numBytesToEnd;
UInt32 numBytesCopied;
UInt32 originalBufferOffset;
SInt32 numBytesLeft;
UInt32 preWrapBytes = 0;
UInt32 byteCount = 0;
UInt8 * source;
UInt8 * dest;
Boolean done;
bool onCoreAudioThread;
#if DEBUGINPUT
// <rdar://problem/7378275>
UInt32 numBytesOnLastCopy;
UInt32 numBytesMargin = -1;
UInt32 usbFrameIndexOnLastCopy;
UInt32 firstUSBFrameIndexOnLastCopy;
IOUSBLowLatencyIsocFrame * pFramesOnLastCopy;
#endif
if (mCoalescenceMutex)
{
IORecursiveLockLock (mCoalescenceMutex);
}
#if DEBUGINPUT
debugIOLog ("+ AppleUSBAudioStream[%p]::CoalesceInputSamples (%lu, %p)", this, numBytesToCoalesce, pFrames);
#endif
originalBufferOffset = 0;
if (0 != numBytesToCoalesce)
{
// This is being called from the CoreAudio thread
onCoreAudioThread = true;
originalBufferOffset = mBufferOffset; // So that when we later get called from the readHandler that we'll put everything in the right spot
#if DEBUGINPUT
debugIOLog ("! AppleUSBAudioStream[%p]::CoalesceInputSamples () - Coalesce from %ld %ld bytes (framelist %ld) on CoreAudio thread", this, originalBufferOffset, numBytesToCoalesce, mCurrentFrameList);
#endif
if ( mMasterMode && !mHaveTakenFirstTimeStamp )
{
debugIOLog ("! AppleUSBAudioStream[%p]::CoalesceInputSamples () - CoreAudio thread is asking for samples without having been sent a timestamp!", this );
}
}
else
{
onCoreAudioThread = false;
}
if (NULL == pFrames)
{
pFrames = &mUSBIsocFrames[mCurrentFrameList * mNumTransactionsPerList];
}
dest = (UInt8 *)getSampleBuffer () + mBufferOffset;
source = (UInt8 *)mReadBuffer + (mCurrentFrameList * mReadUSBFrameListSize);
// <rdar://6094454> Pre-compute these values here instead in the while loop. There is a race condition where
// mCurrentFrameList is updated in the readHandler(), and if it changes, then it could cause usbFrameIndex to get
// out of range when accessing pFrames. firstUSBFrameIndex should be tied to pFrames, so it should only change
// when pFrames changes in the wrap situation. totalNumUSBFrames shouldn't change at all.
firstUSBFrameIndex = (mCurrentFrameList * mNumTransactionsPerList);
totalNumUSBFrames = (mNumUSBFrameLists * mNumTransactionsPerList);
usbFrameIndex = 0;
numFramesChecked = 0;
numBytesCopied = 0;
numBytesLeft = numBytesToCoalesce;
done = FALSE;
while ( (FALSE == done)
&& ('llit' != pFrames[usbFrameIndex].frStatus) // IOUSBFamily is processing this now
&& (-1 != pFrames[usbFrameIndex].frStatus)) // IOUSBFamily hasn't gotten here yet
{
// Log unusual status here
if ( (!(mShouldStop))
&& ( (kIOReturnSuccess != pFrames[usbFrameIndex].frStatus)
&& ( (kIOReturnUnderrun != pFrames[usbFrameIndex].frStatus)
|| (pFrames[usbFrameIndex].frActCount < (mAverageFrameSize - 2 * mSampleSize))))) // [rdar://5889101]
{
debugIOLog ("! AppleUSBAudioStream[%p]::CoalesceInputSamples () - encountered unusual frame with status 0x%x in frame list %lu", this, pFrames[usbFrameIndex].frStatus, usbFrameIndex);
debugIOLog (" pFrames[%lu].frStatus = 0x%x", usbFrameIndex, pFrames[usbFrameIndex].frStatus);
debugIOLog (" pFrames[%lu].frReqCount = %lu", usbFrameIndex, pFrames[usbFrameIndex].frReqCount);
debugIOLog (" pFrames[%lu].frActCount = %lu", usbFrameIndex, pFrames[usbFrameIndex].frActCount);
debugIOLog (" pFrames[%lu].frTimeStamp = 0x%x", usbFrameIndex, (* (UInt64 *) &(pFrames[usbFrameIndex].frTimeStamp)));
// break;
// <rdar://6902105>, <rdar://6411577> Workaround for issue where the device sends more data than it should.
// This causes overruns and the USB host controller may indicate that the frActCount is zero (different
// host controller behaves differently).
if ( ( kIOReturnOverrun == pFrames[usbFrameIndex].frStatus ) && ( 0 == pFrames[usbFrameIndex].frActCount ) )
{
// Set the frActCount to frReqCount so that at least the timing is somewhat preserved and not dropping the
// whole packet.
pFrames[usbFrameIndex].frActCount = pFrames[usbFrameIndex].frReqCount;
mOverrunsCount++;
// If there is too many overruns, the audio stream is possibly corrupt constantly, so restart the
// audio engine if the engine has multiple streams and this input stream is the master stream. This
// is to prevent the continous corruptions.
if ( mMasterMode && ( mOverrunsCount >= mOverrunsThreshold ) )
{
if ( mUSBAudioDevice && mUSBAudioEngine && mUSBAudioEngine->mIOAudioStreamArray )
{
if ( 1 < mUSBAudioEngine->mIOAudioStreamArray->getCount() )
{
// Reset the engine to prevent constant corruption.