-
Notifications
You must be signed in to change notification settings - Fork 1
/
IOAudioStream.cpp
2048 lines (1702 loc) · 85.2 KB
/
IOAudioStream.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-2012 Apple Computer, Inc. All rights reserved.
*
* @APPLE_LICENSE_HEADER_START@
*
* The contents of this file constitute Original Code as defined in and
* are subject to the Apple Public Source License Version 1.1 (the
* "License"). You may not use this file except in compliance with the
* License. Please obtain a copy of the License at
* http://www.apple.com/publicsource and read it before using this file.
*
* This 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 OR NON-INFRINGEMENT. Please see the
* License for the specific language governing rights and limitations
* under the License.
*
* @APPLE_LICENSE_HEADER_END@
*/
#include "IOAudioDebug.h"
#include "IOAudioStream.h"
#include "IOAudioEngine.h"
#include "IOAudioEngineUserClient.h"
#include "IOAudioControl.h"
#include "IOAudioTypes.h"
#include "IOAudioDefines.h"
#include <IOKit/IOLib.h>
#include <IOKit/IOWorkLoop.h>
#include <IOKit/IOCommandGate.h>
#include <libkern/c++/OSSymbol.h>
#include <libkern/c++/OSNumber.h>
#include <libkern/c++/OSArray.h>
#include <libkern/c++/OSDictionary.h>
typedef struct IOAudioStreamFormatExtensionDesc {
UInt32 version;
UInt32 flags;
UInt32 framesPerPacket;
UInt32 bytesPerPacket;
} IOAudioStreamFormatExtensionDesc;
typedef struct IOAudioStreamFormatDesc {
IOAudioStreamFormat format;
IOAudioSampleRate minimumSampleRate;
IOAudioSampleRate maximumSampleRate;
IOAudioStream::AudioIOFunction *ioFunctionList;
UInt32 numIOFunctions;
IOAudioStreamFormatExtensionDesc formatExtension;
} IOAudioStreamFormatDesc;
#define super IOService
OSDefineMetaClassAndStructors(IOAudioStream, IOService)
OSMetaClassDefineReservedUsed(IOAudioStream, 0);
OSMetaClassDefineReservedUsed(IOAudioStream, 1);
OSMetaClassDefineReservedUsed(IOAudioStream, 2);
OSMetaClassDefineReservedUsed(IOAudioStream, 3);
OSMetaClassDefineReservedUsed(IOAudioStream, 4);
OSMetaClassDefineReservedUsed(IOAudioStream, 5);
OSMetaClassDefineReservedUsed(IOAudioStream, 6);
OSMetaClassDefineReservedUsed(IOAudioStream, 7);
OSMetaClassDefineReservedUsed(IOAudioStream, 8);
OSMetaClassDefineReservedUsed(IOAudioStream, 9);
OSMetaClassDefineReservedUsed(IOAudioStream, 10);
OSMetaClassDefineReservedUsed(IOAudioStream, 11);
OSMetaClassDefineReservedUnused(IOAudioStream, 12);
OSMetaClassDefineReservedUnused(IOAudioStream, 13);
OSMetaClassDefineReservedUnused(IOAudioStream, 14);
OSMetaClassDefineReservedUnused(IOAudioStream, 15);
OSMetaClassDefineReservedUnused(IOAudioStream, 16);
OSMetaClassDefineReservedUnused(IOAudioStream, 17);
OSMetaClassDefineReservedUnused(IOAudioStream, 18);
OSMetaClassDefineReservedUnused(IOAudioStream, 19);
OSMetaClassDefineReservedUnused(IOAudioStream, 20);
OSMetaClassDefineReservedUnused(IOAudioStream, 21);
OSMetaClassDefineReservedUnused(IOAudioStream, 22);
OSMetaClassDefineReservedUnused(IOAudioStream, 23);
OSMetaClassDefineReservedUnused(IOAudioStream, 24);
OSMetaClassDefineReservedUnused(IOAudioStream, 25);
OSMetaClassDefineReservedUnused(IOAudioStream, 26);
OSMetaClassDefineReservedUnused(IOAudioStream, 27);
OSMetaClassDefineReservedUnused(IOAudioStream, 28);
OSMetaClassDefineReservedUnused(IOAudioStream, 29);
OSMetaClassDefineReservedUnused(IOAudioStream, 30);
OSMetaClassDefineReservedUnused(IOAudioStream, 31);
OSMetaClassDefineReservedUnused(IOAudioStream, 32);
OSMetaClassDefineReservedUnused(IOAudioStream, 33);
OSMetaClassDefineReservedUnused(IOAudioStream, 34);
OSMetaClassDefineReservedUnused(IOAudioStream, 35);
OSMetaClassDefineReservedUnused(IOAudioStream, 36);
OSMetaClassDefineReservedUnused(IOAudioStream, 37);
OSMetaClassDefineReservedUnused(IOAudioStream, 38);
OSMetaClassDefineReservedUnused(IOAudioStream, 39);
OSMetaClassDefineReservedUnused(IOAudioStream, 40);
OSMetaClassDefineReservedUnused(IOAudioStream, 41);
OSMetaClassDefineReservedUnused(IOAudioStream, 42);
OSMetaClassDefineReservedUnused(IOAudioStream, 43);
OSMetaClassDefineReservedUnused(IOAudioStream, 44);
OSMetaClassDefineReservedUnused(IOAudioStream, 45);
OSMetaClassDefineReservedUnused(IOAudioStream, 46);
OSMetaClassDefineReservedUnused(IOAudioStream, 47);
// New code added here:
#define CMPSAMPLERATE(left, right) ((left.whole < right->whole) ? -1 : (left.whole == right->whole) ? (left.fraction < right->fraction) ? -1 : 0 : 1)
// <rdar://problem/5994776> Amount of frames allowed to go over the pseudo mix buffer size. We use the source buffer as a mix buffer in encoded format mode.
// But we can't clip an arbitrary amount of data. We need to limit it to what the size of the source buffer is. The problem seems to
// be that the source buffer size is hidden by some VBR change. The true size of the source buffer is saved off an then readded after we can use it.
// So through imperical testing it looks like the source buffer size is 4 times the IOBufferSize. We set it to 3 times to be safe.
#define kMixBufferMaxSize ( 2043 ) // Limit to 2 pages but there is 16 bytes taken out of the sample buffer for VBR stuff
bool IOAudioStream::validateFormat(IOAudioStreamFormat *streamFormat, IOAudioStreamFormatExtension *formatExtension, IOAudioStreamFormatDesc *formatDesc, const IOAudioSampleRate *sampleRate)
{
bool foundFormat = false;
audioDebugIOLog(3, "+ IOAudioStream[%p]::validateFormat(%p, %p, %p)\n", this, streamFormat, formatExtension, formatDesc);
if (streamFormat && availableFormats && (numAvailableFormats > 0) && sampleRate) {
UInt32 formatIndex;
for (formatIndex = 0; formatIndex < numAvailableFormats; formatIndex++) {
audioDebugIOLog(3, " %ld: streamFormat->fNumChannels = %ld\n", (long int)availableFormats[formatIndex].format.fNumChannels, (long int)streamFormat->fNumChannels);
audioDebugIOLog(3, " 0x%lx: streamFormat->fSampleFormat = 0x%lx\n", (long unsigned int)availableFormats[formatIndex].format.fSampleFormat, (long unsigned int)streamFormat->fSampleFormat);
audioDebugIOLog(3, " 0x%lx: streamFormat->fNumericRepresentation = 0x%lx\n", (long unsigned int)availableFormats[formatIndex].format.fNumericRepresentation, (long unsigned int)streamFormat->fNumericRepresentation);
audioDebugIOLog(3, " %d: streamFormat->fBitDepth = %d\n", availableFormats[formatIndex].format.fBitDepth, streamFormat->fBitDepth);
audioDebugIOLog(3, " %d: streamFormat->fBitWidth = %d\n", availableFormats[formatIndex].format.fBitWidth, streamFormat->fBitWidth);
audioDebugIOLog(3, " %d: streamFormat->fAlignment = %d\n", availableFormats[formatIndex].format.fAlignment, streamFormat->fAlignment);
audioDebugIOLog(3, " %d: streamFormat->fByteOrder = %d\n", availableFormats[formatIndex].format.fByteOrder, streamFormat->fByteOrder);
if ((availableFormats[formatIndex].format.fNumChannels == streamFormat->fNumChannels)
&& (availableFormats[formatIndex].format.fSampleFormat == streamFormat->fSampleFormat)
&& (availableFormats[formatIndex].format.fNumericRepresentation == streamFormat->fNumericRepresentation)
&& (availableFormats[formatIndex].format.fBitDepth == streamFormat->fBitDepth)
&& (availableFormats[formatIndex].format.fBitWidth == streamFormat->fBitWidth)
&& (availableFormats[formatIndex].format.fAlignment == streamFormat->fAlignment)
&& (availableFormats[formatIndex].format.fByteOrder == streamFormat->fByteOrder)
&& (availableFormats[formatIndex].format.fIsMixable == streamFormat->fIsMixable)) {
bool passSRCheck = true;
if (0 != sampleRate->whole) {
if ((CMPSAMPLERATE (availableFormats[formatIndex].minimumSampleRate, sampleRate) > 0) || (CMPSAMPLERATE (availableFormats[formatIndex].maximumSampleRate, sampleRate) < 0)) {
passSRCheck = false;
}
}
if (passSRCheck) {
// <rdar://10957396> Only update the tag if required
if ( streamFormat->fDriverTag != availableFormats[formatIndex].format.fDriverTag ) {
streamFormat->fDriverTag = availableFormats[formatIndex].format.fDriverTag;
}
// streamFormat->fIsMixable = availableFormats[formatIndex].format.fIsMixable;
if (formatDesc) {
memcpy(formatDesc, &availableFormats[formatIndex], sizeof(IOAudioStreamFormatDesc));
}
foundFormat = true;
break;
}
}
}
}
audioDebugIOLog(3, "- IOAudioStream[%p]::validateFormat(%p, %p, %p) returns %d\n", this, streamFormat, formatExtension, formatDesc, foundFormat );
return foundFormat;
}
const IOAudioStreamFormatExtension *IOAudioStream::getFormatExtension()
{
assert(reserved);
return &reserved->streamFormatExtension;
}
IOReturn IOAudioStream::setFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, bool callDriver)
{
IOReturn result = kIOReturnSuccess;
OSDictionary *formatDict = NULL;
IOAudioStreamFormatExtension validFormatExtension;
if (streamFormat) {
if (!formatExtension) {
IOAudioStreamFormatDesc formatDesc;
validateFormat((IOAudioStreamFormat *)streamFormat, NULL, &formatDesc);
memcpy (&validFormatExtension, &formatDesc.formatExtension, sizeof (validFormatExtension));
} else {
validFormatExtension = *formatExtension;
}
if ( (formatDict = createDictionaryFromFormat(streamFormat, &validFormatExtension)) ) {
result = setFormat(streamFormat, &validFormatExtension, formatDict, callDriver);
formatDict->release();
} else {
result = kIOReturnError;
}
} else {
result = kIOReturnBadArgument;
}
return result;
}
// <rdar://8121989> Restructured for single point of entry and single point of exit so that
// the indentifier post processing tool can properly insert scope when post processing a log file
// obtained via fwkpfv.
IOReturn IOAudioStream::setFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, OSDictionary *formatDict, bool callDriver)
{
IOReturn result = kIOReturnSuccess;
IOAudioStreamFormat validFormat;
IOAudioStreamFormatDesc formatDesc;
IOAudioStreamFormatExtension validFormatExtension;
const IOAudioSampleRate *requestedSampleRate = NULL;
OSDictionary *sampleRateDict;
audioDebugIOLog(3, "+ IOAudioStream[%p]::setFormat(%p, %p)\n", this, streamFormat, formatDict);
if (!streamFormat || !formatDict)
{
result = kIOReturnBadArgument;
}
else
{
#ifdef DEBUG
setProperty("IOAudioStreamPendingFormat", formatDict);
#endif
validFormat = *streamFormat;
if (NULL != formatExtension) {
validFormatExtension = *formatExtension;
} else {
validFormatExtension.fVersion = kFormatExtensionCurrentVersion;
validFormatExtension.fFlags = 0;
validFormatExtension.fFramesPerPacket = 1;
validFormatExtension.fBytesPerPacket = streamFormat->fNumChannels * (streamFormat->fBitWidth / 8);
}
sampleRateDict = OSDynamicCast(OSDictionary, formatDict->getObject(kIOAudioSampleRateKey));
if (sampleRateDict) {
requestedSampleRate = IOAudioEngine::createSampleRateFromDictionary(sampleRateDict);
} else {
requestedSampleRate = audioEngine->getSampleRate();
}
if (validateFormat(&validFormat, &validFormatExtension, &formatDesc, requestedSampleRate)) {
// OSDictionary *sampleRateDict;
IOAudioSampleRate *newSampleRate = NULL;
OSSet *userClientsToLock;
sampleRateDict = OSDynamicCast(OSDictionary, formatDict->getObject(kIOAudioSampleRateKey));
if (sampleRateDict) {
const IOAudioSampleRate *currentSampleRate;
newSampleRate = IOAudioEngine::createSampleRateFromDictionary(sampleRateDict);
currentSampleRate = audioEngine->getSampleRate();
if (newSampleRate && (newSampleRate->whole == currentSampleRate->whole) && (newSampleRate->fraction == currentSampleRate->fraction)) {
newSampleRate = NULL;
}
}
// In order to avoid deadlocks, we need to ensure we hold all of the user client locks
// before making calls while holding our IO lock. Everything works fine as long
// as the order of the locks is workLoop -> user client -> stream.
// Any other order is sure to cause trouble.
// Because we pause the engine while doing the format change, the user clients will be removed
// from our list before we complete. Therefore, we must make a copy of the list to allow
// all of the clients to be unlocked when we are done.
userClientsToLock = OSSet::withCapacity(numClients);
if (userClientsToLock) {
OSCollectionIterator *clientIterator;
IOAudioClientBuffer *clientBuf;
IOAudioEngineUserClient *userClient;
clientBuf = userClientList;
while (clientBuf) {
assert(clientBuf->userClient);
userClientsToLock->setObject(clientBuf->userClient);
clientBuf = clientBuf->nextClient;
}
clientIterator = OSCollectionIterator::withCollection(userClientsToLock);
if (!clientIterator) {
userClientsToLock->release();
result = kIOReturnNoMemory;
goto Done;
}
while ( (userClient = (IOAudioEngineUserClient *)clientIterator->getNextObject()) ) {
userClient->lockBuffers();
}
clientIterator->release();
lockStreamForIO();
audioEngine->pauseAudioEngine();
if (callDriver) {
result = audioEngine->performFormatChange(this, &validFormat, &validFormatExtension, newSampleRate);
if ( result == kIOReturnUnsupported )
{
result = audioEngine->performFormatChange(this, &validFormat, newSampleRate);
}
}
if (result == kIOReturnSuccess) {
OSDictionary *newFormatDict;
if (formatDesc.ioFunctionList && (formatDesc.numIOFunctions > 0)) {
setIOFunctionList(formatDesc.ioFunctionList, formatDesc.numIOFunctions);
}
newFormatDict = createDictionaryFromFormat(&validFormat, &validFormatExtension);
if (newFormatDict) {
UInt32 oldNumChannels;
if (mixBuffer != NULL) {
// If we have a mix buffer and the new format is not mixable, free the mix buffer
if (!validFormat.fIsMixable) {
setMixBuffer(NULL, 0);
} else if (streamAllocatedMixBuffer && (format.fNumChannels != validFormat.fNumChannels)) { // We need to reallocate the mix buffer
UInt32 newMixBufSize;
assert(audioEngine);
newMixBufSize = validFormat.fNumChannels * kIOAudioEngineDefaultMixBufferSampleSize * audioEngine->numSampleFramesPerBuffer;
if (newMixBufSize > 0) {
void *newMixBuf = IOMallocAligned(newMixBufSize, 32);
if (newMixBuf) {
setMixBuffer(newMixBuf, newMixBufSize);
streamAllocatedMixBuffer = true;
}
}
}
}
oldNumChannels = format.fNumChannels;
format = validFormat;
setProperty(kIOAudioStreamFormatKey, newFormatDict);
newFormatDict->release();
if (format.fNumChannels != oldNumChannels) {
audioEngine->updateChannelNumbers();
}
if (newSampleRate) {
audioEngine->setSampleRate(newSampleRate);
}
} else {
result = kIOReturnError;
}
} else {
if ( kIOReturnNotReady != result ) { // <rdar://8094567>
IOLog("IOAudioStream<%p>::setFormat(0x%p, 0x%p) - audio engine unable to change format\n", this, streamFormat, formatDict);
}
}
if (result == kIOReturnSuccess) {
audioEngine->sendFormatChangeNotification(this);
}
audioEngine->resumeAudioEngine();
unlockStreamForIO();
// Unlock all of the user clients we originally locked
assert(userClientsToLock);
clientIterator = OSCollectionIterator::withCollection(userClientsToLock);
if (clientIterator) {
while ( (userClient = (IOAudioEngineUserClient *)clientIterator->getNextObject()) ) {
userClient->unlockBuffers();
}
clientIterator->release();
} else {
// Uh oh... we're in trouble now!
// We have to unlock the clients, but we can't get an iterator on the collection.
// All existing clients will now hang trying to play audio
result = kIOReturnNoMemory;
}
userClientsToLock->release();
} else {
result = kIOReturnNoMemory;
}
} else {
IOLog("IOAudioStream<0x%p>::setFormat(0x%p, 0x%p) - invalid format.\n", this, streamFormat, formatDict);
result = kIOReturnBadArgument;
}
}
Done:
audioDebugIOLog(3, "IOAudioStream[%p]::setFormat(%p, %p) returns 0x%lx", this, streamFormat, formatDict, (long unsigned int)result);
return result;
}
void IOAudioStream::addAvailableFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, const IOAudioSampleRate *minRate, const IOAudioSampleRate *maxRate, const AudioIOFunction *ioFunctionList, UInt32 numFunctions)
{
assert(availableFormatDictionaries);
if (streamFormat && minRate && maxRate) {
IOAudioStreamFormatDesc *newAvailableFormatList;
IOAudioStreamFormatExtension localFormatExtension;
newAvailableFormatList = (IOAudioStreamFormatDesc *)IOMallocAligned((numAvailableFormats+1) * sizeof(IOAudioStreamFormatDesc), sizeof (IOAudioStreamFormatDesc *));
if (newAvailableFormatList) {
if (availableFormats && (numAvailableFormats > 0)) {
memcpy(newAvailableFormatList, availableFormats, numAvailableFormats * sizeof(IOAudioStreamFormatDesc));
}
newAvailableFormatList[numAvailableFormats].format = *streamFormat;
newAvailableFormatList[numAvailableFormats].minimumSampleRate = *minRate;
newAvailableFormatList[numAvailableFormats].maximumSampleRate = *maxRate;
if (formatExtension) {
localFormatExtension = *formatExtension;
newAvailableFormatList[numAvailableFormats].formatExtension.flags = formatExtension->fFlags;
newAvailableFormatList[numAvailableFormats].formatExtension.framesPerPacket = formatExtension->fFramesPerPacket;
newAvailableFormatList[numAvailableFormats].formatExtension.bytesPerPacket = formatExtension->fBytesPerPacket;
} else {
newAvailableFormatList[numAvailableFormats].formatExtension.flags = localFormatExtension.fFlags = 0;
newAvailableFormatList[numAvailableFormats].formatExtension.framesPerPacket = localFormatExtension.fFramesPerPacket = 1;
newAvailableFormatList[numAvailableFormats].formatExtension.bytesPerPacket = localFormatExtension.fBytesPerPacket = streamFormat->fNumChannels * (streamFormat->fBitWidth / 8);
}
if (ioFunctionList && (numFunctions > 0)) {
newAvailableFormatList[numAvailableFormats].ioFunctionList = (AudioIOFunction *)IOMallocAligned(numFunctions * sizeof(AudioIOFunction), sizeof (AudioIOFunction *));
newAvailableFormatList[numAvailableFormats].numIOFunctions = numFunctions;
memcpy(newAvailableFormatList[numAvailableFormats].ioFunctionList, ioFunctionList, numFunctions * sizeof(AudioIOFunction));
} else {
newAvailableFormatList[numAvailableFormats].ioFunctionList = NULL;
newAvailableFormatList[numAvailableFormats].numIOFunctions = 0;
}
IOFreeAligned(availableFormats, numAvailableFormats * sizeof(IOAudioStreamFormatDesc));
availableFormats = newAvailableFormatList;
numAvailableFormats++;
}
OSDictionary *formatDict = createDictionaryFromFormat(streamFormat, &localFormatExtension);
if (formatDict) {
OSDictionary *sampleRateDict;
sampleRateDict = IOAudioEngine::createDictionaryFromSampleRate(minRate);
if (sampleRateDict) {
formatDict->setObject(gMinimumSampleRateKey, sampleRateDict);
sampleRateDict->release();
sampleRateDict = IOAudioEngine::createDictionaryFromSampleRate(maxRate);
if (sampleRateDict) {
OSArray *newAvailableFormats;
OSArray *oldAvailableFormats;
oldAvailableFormats = availableFormatDictionaries;
newAvailableFormats = OSDynamicCast(OSArray, availableFormatDictionaries->copyCollection()); // copyCollection() does a deep copy
if (newAvailableFormats) {
formatDict->setObject(gMaximumSampleRateKey, sampleRateDict);
newAvailableFormats->setObject(formatDict);
availableFormatDictionaries = newAvailableFormats;
setProperty(kIOAudioStreamAvailableFormatsKey, availableFormatDictionaries);
oldAvailableFormats->release();
if (streamFormat->fNumChannels > maxNumChannels) {
maxNumChannels = streamFormat->fNumChannels;
}
}
sampleRateDict->release();
}
}
formatDict->release();
}
}
}
void IOAudioStream::addAvailableFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, const IOAudioSampleRate *minRate, const IOAudioSampleRate *maxRate, AudioIOFunction ioFunction)
{
addAvailableFormat(streamFormat, formatExtension, minRate, maxRate, &ioFunction, 1);
}
bool IOAudioStream::validateFormat(IOAudioStreamFormat *streamFormat, IOAudioStreamFormatExtension *formatExtension, IOAudioStreamFormatDesc *formatDesc)
{
return validateFormat(streamFormat, formatExtension, formatDesc, audioEngine->getSampleRate());
}
void IOAudioStream::setTerminalType(const UInt32 terminalType)
{
if (terminalType) {
setProperty(kIOAudioStreamTerminalTypeKey, terminalType, 32);
}
}
IOReturn IOAudioStream::mixOutputSamples(const void *sourceBuf, void *mixBuf, UInt32 firstSampleFrame, UInt32 numSampleFrames, const IOAudioStreamFormat *streamFormat, IOAudioStream *audioStream)
{
bcopy (sourceBuf, (float *)mixBuf + (firstSampleFrame * streamFormat->fNumChannels), numSampleFrames * sizeof (float) * streamFormat->fNumChannels);
return kIOReturnSuccess;
}
void IOAudioStream::setSampleLatency(UInt32 numSamples)
{
audioDebugIOLog(3, "+-IOAudioStream[%p]::setSampleLatency(0x%lx)\n", this, (long unsigned int)numSamples);
setProperty(kIOAudioStreamSampleLatencyKey, numSamples, sizeof(UInt32)*8);
}
UInt32 IOAudioStream::getNumSampleFramesRead()
{
assert(reserved);
audioDebugIOLog(3, "+-IOAudioStream[%p]::getNumSampleFramesRead() returns %ld\n", this, (long unsigned int)reserved->mSampleFramesReadByEngine);
return reserved->mSampleFramesReadByEngine;
}
void IOAudioStream::setDefaultNumSampleFramesRead(UInt32 inDefaultNumFramesRead)
{
assert(reserved);
audioDebugIOLog(3, "+-IOAudioStream[%p]::setDefaultNumSampleFramesRead(%ld)\n", this, (long unsigned int)inDefaultNumFramesRead);
reserved->mSampleFramesReadByEngine = inDefaultNumFramesRead;
}
// Original code from here on:
const OSSymbol *IOAudioStream::gDirectionKey = NULL;
const OSSymbol *IOAudioStream::gNumChannelsKey = NULL;
const OSSymbol *IOAudioStream::gSampleFormatKey = NULL;
const OSSymbol *IOAudioStream::gNumericRepresentationKey = NULL;
const OSSymbol *IOAudioStream::gBitDepthKey = NULL;
const OSSymbol *IOAudioStream::gBitWidthKey = NULL;
const OSSymbol *IOAudioStream::gAlignmentKey = NULL;
const OSSymbol *IOAudioStream::gByteOrderKey = NULL;
const OSSymbol *IOAudioStream::gIsMixableKey = NULL;
const OSSymbol *IOAudioStream::gDriverTagKey = NULL;
const OSSymbol *IOAudioStream::gMinimumSampleRateKey = NULL;
const OSSymbol *IOAudioStream::gMaximumSampleRateKey = NULL;
void IOAudioStream::initKeys()
{
if (!gNumChannelsKey) {
gNumChannelsKey = OSSymbol::withCString(kIOAudioStreamNumChannelsKey);
gSampleFormatKey = OSSymbol::withCString(kIOAudioStreamSampleFormatKey);
gNumericRepresentationKey = OSSymbol::withCString(kIOAudioStreamNumericRepresentationKey);
gBitDepthKey = OSSymbol::withCString(kIOAudioStreamBitDepthKey);
gBitWidthKey = OSSymbol::withCString(kIOAudioStreamBitWidthKey);
gAlignmentKey = OSSymbol::withCString(kIOAudioStreamAlignmentKey);
gByteOrderKey = OSSymbol::withCString(kIOAudioStreamByteOrderKey);
gIsMixableKey = OSSymbol::withCString(kIOAudioStreamIsMixableKey);
gDriverTagKey = OSSymbol::withCString(kIOAudioStreamDriverTagKey);
gDirectionKey = OSSymbol::withCString(kIOAudioStreamDirectionKey);
gMinimumSampleRateKey = OSSymbol::withCString(kIOAudioStreamMinimumSampleRateKey);
gMaximumSampleRateKey = OSSymbol::withCString(kIOAudioStreamMaximumSampleRateKey);
}
}
OSDictionary *IOAudioStream::createDictionaryFromFormat(const IOAudioStreamFormat *streamFormat, const IOAudioStreamFormatExtension *formatExtension, OSDictionary *formatDict)
{
OSDictionary *newDict = NULL;
if (streamFormat) {
if (formatDict) {
newDict = formatDict;
} else {
newDict = OSDictionary::withCapacity(7);
}
if (newDict) {
OSNumber *num;
if (!gNumChannelsKey) {
initKeys();
}
num = OSNumber::withNumber(streamFormat->fNumChannels, 32);
newDict->setObject(gNumChannelsKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fSampleFormat, 32);
newDict->setObject(gSampleFormatKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fNumericRepresentation, 32);
newDict->setObject(gNumericRepresentationKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fBitDepth, 8);
newDict->setObject(gBitDepthKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fBitWidth, 8);
newDict->setObject(gBitWidthKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fAlignment, 8);
newDict->setObject(gAlignmentKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fByteOrder, 8);
newDict->setObject(gByteOrderKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fIsMixable, 8);
newDict->setObject(gIsMixableKey, num);
num->release();
num = OSNumber::withNumber(streamFormat->fDriverTag, 32);
newDict->setObject(gDriverTagKey, num);
num->release();
if (formatExtension && formatExtension->fVersion >= kFormatExtensionCurrentVersion) {
num = OSNumber::withNumber(formatExtension->fFlags, 32);
newDict->setObject(kIOAudioStreamFormatFlagsKey, num);
num->release();
num = OSNumber::withNumber(formatExtension->fFramesPerPacket, 32);
newDict->setObject(kIOAudioStreamFramesPerPacketKey, num);
num->release();
num = OSNumber::withNumber(formatExtension->fBytesPerPacket, 32);
newDict->setObject(kIOAudioStreamBytesPerPacketKey, num);
num->release();
}
}
}
return newDict;
}
IOAudioStreamFormat *IOAudioStream::createFormatFromDictionary(const OSDictionary *formatDict, IOAudioStreamFormat *streamFormat, IOAudioStreamFormatExtension *formatExtension)
{
IOAudioStreamFormat *format = NULL;
static IOAudioStreamFormat staticFormat;
if (formatDict) {
if (streamFormat) {
format = streamFormat;
} else {
format = &staticFormat;
}
if (format) {
OSNumber *num;
if (!gNumChannelsKey) {
initKeys();
}
bzero(format, sizeof(IOAudioStreamFormat));
num = OSDynamicCast(OSNumber, formatDict->getObject(gNumChannelsKey));
if (num) {
format->fNumChannels = num->unsigned32BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gSampleFormatKey));
if (num) {
format->fSampleFormat = num->unsigned32BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gNumericRepresentationKey));
if (num) {
format->fNumericRepresentation = num->unsigned32BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gBitDepthKey));
if (num) {
format->fBitDepth = num->unsigned8BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gBitWidthKey));
if (num) {
format->fBitWidth = num->unsigned8BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gAlignmentKey));
if (num) {
format->fAlignment = num->unsigned8BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gByteOrderKey));
if (num) {
format->fByteOrder = num->unsigned8BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gIsMixableKey));
if (num) {
format->fIsMixable = num->unsigned8BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(gDriverTagKey));
if (num) {
format->fDriverTag = num->unsigned32BitValue();
}
if (formatExtension) {
formatExtension->fVersion = kFormatExtensionCurrentVersion;
num = OSDynamicCast(OSNumber, formatDict->getObject(kIOAudioStreamFormatFlagsKey));
if (num) {
formatExtension->fFlags = num->unsigned32BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(kIOAudioStreamFramesPerPacketKey));
if (num) {
formatExtension->fFramesPerPacket = num->unsigned32BitValue();
}
num = OSDynamicCast(OSNumber, formatDict->getObject(kIOAudioStreamBytesPerPacketKey));
if (num) {
formatExtension->fBytesPerPacket = num->unsigned32BitValue();
}
}
}
}
return format;
}
bool IOAudioStream::initWithAudioEngine(IOAudioEngine *engine, IOAudioStreamDirection dir, UInt32 startChannelID, const char *streamDescription, OSDictionary *properties)
{
UInt32 streamID;
if (!gNumChannelsKey) {
initKeys();
}
if (!engine) {
return false;
}
if (!super::init(properties)) {
return false;
}
audioEngine = engine;
reserved = (ExpansionData *)IOMalloc (sizeof(struct ExpansionData));
if (!reserved) {
return false;
}
workLoop = audioEngine->getWorkLoop();
if (!workLoop) {
return false;
}
workLoop->retain();
commandGate = IOCommandGate::commandGate(this);
if (!commandGate) {
return false;
}
streamIOLock = IORecursiveLockAlloc();
if (!streamIOLock) {
return false;
}
setDirection(dir);
startingChannelID = startChannelID;
setProperty(kIOAudioStreamStartingChannelIDKey, startingChannelID, sizeof(UInt32)*8);
maxNumChannels = 0;
if (streamDescription) {
setProperty(kIOAudioStreamDescriptionKey, streamDescription);
}
availableFormatDictionaries = OSArray::withCapacity(1);
if (!availableFormatDictionaries) {
return false;
}
setProperty(kIOAudioStreamAvailableFormatsKey, availableFormatDictionaries);
// This needs to change to passing up a token rather than the "this" pointer.
streamID = engine->getNextStreamID (this);
setProperty(kIOAudioStreamIDKey, streamID, sizeof(UInt32)*8);
// setProperty(kIOAudioStreamIDKey, (UInt32)this, sizeof(UInt32)*8);
streamAvailable = true;
setProperty(kIOAudioStreamAvailableKey, (UInt8)1, sizeof(UInt8)*8);
numClients = 0;
updateNumClients();
resetClipInfo();
clientBufferListStart = NULL;
clientBufferListEnd = NULL;
userClientList = NULL;
audioIOFunctions = NULL;
numIOFunctions = false;
streamAllocatedMixBuffer = false;
workLoop->addEventSource(commandGate);
return true;
}
void IOAudioStream::free()
{
if (availableFormatDictionaries) {
availableFormatDictionaries->release();
availableFormatDictionaries = NULL;
}
if (mixBuffer && streamAllocatedMixBuffer) {
IOFreeAligned(mixBuffer, mixBufferSize);
mixBuffer = NULL;
mixBufferSize = 0;
}
if (defaultAudioControls) {
removeDefaultAudioControls();
defaultAudioControls->release();
defaultAudioControls = NULL;
}
if (commandGate) {
if (workLoop) {
workLoop->removeEventSource(commandGate);
}
commandGate->release();
commandGate = NULL;
}
if (workLoop) {
workLoop->release();
workLoop = NULL;
}
if (streamIOLock) {
IORecursiveLockFree(streamIOLock);
streamIOLock = NULL;
}
if (audioIOFunctions && (numIOFunctions > 0)) {
IOFreeAligned(audioIOFunctions, numIOFunctions * sizeof(AudioIOFunction));
audioIOFunctions = NULL;
numIOFunctions = 0;
}
if (availableFormats && (numAvailableFormats > 0)) {
UInt32 formatNum;
for (formatNum = 0; formatNum < numAvailableFormats; formatNum++) {
if (availableFormats[formatNum].ioFunctionList && (availableFormats[formatNum].numIOFunctions > 0)) {
IOFreeAligned(availableFormats[formatNum].ioFunctionList, availableFormats[formatNum].numIOFunctions * sizeof(AudioIOFunction));
}
}
IOFreeAligned(availableFormats, numAvailableFormats * sizeof(IOAudioStreamFormatDesc));
availableFormats = NULL;
numAvailableFormats = 0;
}
if (reserved) {
IOFree (reserved, sizeof(struct ExpansionData));
}
super::free();
}
void IOAudioStream::stop(IOService *provider)
{
if (commandGate) {
if (workLoop) {
workLoop->removeEventSource(commandGate);
}
commandGate->release();
commandGate = NULL;
}
super::stop(provider);
}
IOWorkLoop *IOAudioStream::getWorkLoop() const
{
return workLoop;
}
IOReturn IOAudioStream::setProperties(OSObject *properties)
{
OSDictionary *props;
IOReturn result = kIOReturnSuccess;
audioDebugIOLog(3, "+ IOAudioStream[%p]::setProperties(%p)\n", this, properties);
if (properties && (props = OSDynamicCast(OSDictionary, properties))) {
OSCollectionIterator *iterator;
OSObject *iteratorKey;
iterator = OSCollectionIterator::withCollection(props);
if (iterator) {
while ( (iteratorKey = iterator->getNextObject()) ) {
OSSymbol *key;
key = OSDynamicCast(OSSymbol, iteratorKey);
if (key && key->isEqualTo(kIOAudioStreamFormatKey)) {
OSDictionary *formatDict = OSDynamicCast(OSDictionary, props->getObject(key));
if (formatDict) {
assert(workLoop); // <rdar://8568040,8691669>
result = workLoop->runAction(_setFormatAction, this, formatDict); // <rdar://8568040,8691669>
}
}
}
iterator->release();
} else {
result = kIOReturnError;
}
} else {
result = kIOReturnBadArgument;
}
audioDebugIOLog(3, "- IOAudioStream[%p]::setProperties(%p) returns 0x%lX\n", this, properties, (long unsigned int)result );
return result;
}
void IOAudioStream::setDirection(IOAudioStreamDirection dir)
{
direction = dir;
setProperty(kIOAudioStreamDirectionKey, direction, 8);
}
IOAudioStreamDirection IOAudioStream::getDirection()
{
return direction;
}
void IOAudioStream::setSampleBuffer(void *buffer, UInt32 size)
{
lockStreamForIO();
sampleBuffer = buffer;
if (sampleBuffer) {
sampleBufferSize = size;
bzero(sampleBuffer, sampleBufferSize);
} else {
sampleBufferSize = 0;
}
unlockStreamForIO();
}
void *IOAudioStream::getSampleBuffer()
{
return sampleBuffer;
}
UInt32 IOAudioStream::getSampleBufferSize()
{
return sampleBufferSize;
}
void IOAudioStream::setMixBuffer(void *buffer, UInt32 size)
{
lockStreamForIO();
if (mixBuffer && streamAllocatedMixBuffer) {
IOFreeAligned(mixBuffer, mixBufferSize);
mixBuffer = NULL;
mixBufferSize = 0;
streamAllocatedMixBuffer = false;
}
mixBuffer = buffer;
if (mixBuffer) {
mixBufferSize = size;
bzero(mixBuffer, mixBufferSize);
} else {
mixBufferSize = 0;
}
unlockStreamForIO();
}
void *IOAudioStream::getMixBuffer()
{
return mixBuffer;
}
UInt32 IOAudioStream::getMixBufferSize()
{
return mixBufferSize;
}