-
-
Notifications
You must be signed in to change notification settings - Fork 560
/
BlackHole.c
3856 lines (3260 loc) · 175 KB
/
BlackHole.c
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
/*
File: BlackHole.c
Copyright (C) 2019 Existential Audio Inc.
*/
/*==================================================================================================
BlackHole.c
==================================================================================================*/
//==================================================================================================
// Includes
//==================================================================================================
// System Includes
#include "BlackHole.h"
// Volume conversions
static Float32 volume_to_decibel(Float32 volume)
{
if (volume <= powf(10.0f, kVolume_MinDB / 20.0f))
return kVolume_MinDB;
else
return 20.0f * log10f(volume);
}
static Float32 volume_from_decibel(Float32 decibel)
{
if (decibel <= kVolume_MinDB)
return 0.0f;
else
return powf(10.0f, decibel / 20.0f);
}
static Float32 volume_to_scalar(Float32 volume)
{
Float32 decibel = volume_to_decibel(volume);
return (decibel - kVolume_MinDB) / (kVolume_MaxDB - kVolume_MinDB);
}
static Float32 volume_from_scalar(Float32 scalar)
{
Float32 decibel = scalar * (kVolume_MaxDB - kVolume_MinDB) + kVolume_MinDB;
return volume_from_decibel(decibel);
}
#pragma mark Factory
void* BlackHole_Create(CFAllocatorRef inAllocator, CFUUIDRef inRequestedTypeUUID)
{
// This is the CFPlugIn factory function. Its job is to create the implementation for the given
// type provided that the type is supported. Because this driver is simple and all its
// initialization is handled via static iniitalization when the bundle is loaded, all that
// needs to be done is to return the AudioServerPlugInDriverRef that points to the driver's
// interface. A more complicated driver would create any base line objects it needs to satisfy
// the IUnknown methods that are used to discover that actual interface to talk to the driver.
// The majority of the driver's initilization should be handled in the Initialize() method of
// the driver's AudioServerPlugInDriverInterface.
#pragma unused(inAllocator)
void* theAnswer = NULL;
if(CFEqual(inRequestedTypeUUID, kAudioServerPlugInTypeUUID))
{
theAnswer = gAudioServerPlugInDriverRef;
}
return theAnswer;
}
#pragma mark Inheritence
static HRESULT BlackHole_QueryInterface(void* inDriver, REFIID inUUID, LPVOID* outInterface)
{
// This function is called by the HAL to get the interface to talk to the plug-in through.
// AudioServerPlugIns are required to support the IUnknown interface and the
// AudioServerPlugInDriverInterface. As it happens, all interfaces must also provide the
// IUnknown interface, so we can always just return the single interface we made with
// gAudioServerPlugInDriverInterfacePtr regardless of which one is asked for.
// declare the local variables
HRESULT theAnswer = 0;
CFUUIDRef theRequestedUUID = NULL;
// validate the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_QueryInterface: bad driver reference");
FailWithAction(outInterface == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_QueryInterface: no place to store the returned interface");
// make a CFUUIDRef from inUUID
theRequestedUUID = CFUUIDCreateFromUUIDBytes(NULL, inUUID);
FailWithAction(theRequestedUUID == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_QueryInterface: failed to create the CFUUIDRef");
// AudioServerPlugIns only support two interfaces, IUnknown (which has to be supported by all
// CFPlugIns and AudioServerPlugInDriverInterface (which is the actual interface the HAL will
// use).
if(CFEqual(theRequestedUUID, IUnknownUUID) || CFEqual(theRequestedUUID, kAudioServerPlugInDriverInterfaceUUID))
{
pthread_mutex_lock(&gPlugIn_StateMutex);
++gPlugIn_RefCount;
pthread_mutex_unlock(&gPlugIn_StateMutex);
*outInterface = gAudioServerPlugInDriverRef;
}
else
{
theAnswer = E_NOINTERFACE;
}
// make sure to release the UUID we created
CFRelease(theRequestedUUID);
Done:
return theAnswer;
}
static ULONG BlackHole_AddRef(void* inDriver)
{
// This call returns the resulting reference count after the increment.
// declare the local variables
ULONG theAnswer = 0;
// check the arguments
FailIf(inDriver != gAudioServerPlugInDriverRef, Done, "BlackHole_AddRef: bad driver reference");
// increment the refcount
pthread_mutex_lock(&gPlugIn_StateMutex);
if(gPlugIn_RefCount < UINT32_MAX)
{
++gPlugIn_RefCount;
}
theAnswer = gPlugIn_RefCount;
pthread_mutex_unlock(&gPlugIn_StateMutex);
Done:
return theAnswer;
}
static ULONG BlackHole_Release(void* inDriver)
{
// This call returns the resulting reference count after the decrement.
// declare the local variables
ULONG theAnswer = 0;
// check the arguments
FailIf(inDriver != gAudioServerPlugInDriverRef, Done, "BlackHole_Release: bad driver reference");
// decrement the refcount
pthread_mutex_lock(&gPlugIn_StateMutex);
if(gPlugIn_RefCount > 0)
{
--gPlugIn_RefCount;
// Note that we don't do anything special if the refcount goes to zero as the HAL
// will never fully release a plug-in it opens. We keep managing the refcount so that
// the API semantics are correct though.
}
theAnswer = gPlugIn_RefCount;
pthread_mutex_unlock(&gPlugIn_StateMutex);
Done:
return theAnswer;
}
#pragma mark Basic Operations
static OSStatus BlackHole_Initialize(AudioServerPlugInDriverRef inDriver, AudioServerPlugInHostRef inHost)
{
// The job of this method is, as the name implies, to get the driver initialized. One specific
// thing that needs to be done is to store the AudioServerPlugInHostRef so that it can be used
// later. Note that when this call returns, the HAL will scan the various lists the driver
// maintains (such as the device list) to get the inital set of objects the driver is
// publishing. So, there is no need to notifiy the HAL about any objects created as part of the
// execution of this method.
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_Initialize: bad driver reference");
// store the AudioServerPlugInHostRef
gPlugIn_Host = inHost;
// initialize the box acquired property from the settings
CFPropertyListRef theSettingsData = NULL;
gPlugIn_Host->CopyFromStorage(gPlugIn_Host, CFSTR("box acquired"), &theSettingsData);
if(theSettingsData != NULL)
{
if(CFGetTypeID(theSettingsData) == CFBooleanGetTypeID())
{
gBox_Acquired = CFBooleanGetValue((CFBooleanRef)theSettingsData);
}
else if(CFGetTypeID(theSettingsData) == CFNumberGetTypeID())
{
SInt32 theValue = 0;
CFNumberGetValue((CFNumberRef)theSettingsData, kCFNumberSInt32Type, &theValue);
gBox_Acquired = theValue ? 1 : 0;
}
CFRelease(theSettingsData);
}
// initialize the box name from the settings
gPlugIn_Host->CopyFromStorage(gPlugIn_Host, CFSTR("box acquired"), &theSettingsData);
if(theSettingsData != NULL)
{
if(CFGetTypeID(theSettingsData) == CFStringGetTypeID())
{
gBox_Name = (CFStringRef)theSettingsData;
CFRetain(gBox_Name);
}
CFRelease(theSettingsData);
}
// set the box name directly as a last resort
if(gBox_Name == NULL)
{
gBox_Name = CFSTR("BlackHole Box");
}
// calculate the host ticks per frame
struct mach_timebase_info theTimeBaseInfo;
mach_timebase_info(&theTimeBaseInfo);
Float64 theHostClockFrequency = (Float64)theTimeBaseInfo.denom / (Float64)theTimeBaseInfo.numer;
theHostClockFrequency *= 1000000000.0;
gDevice_HostTicksPerFrame = theHostClockFrequency / gDevice_SampleRate;
// DebugMsg("BlackHole theTimeBaseInfo.numer: %u \t theTimeBaseInfo.denom: %u", theTimeBaseInfo.numer, theTimeBaseInfo.denom);
Done:
return theAnswer;
}
static OSStatus BlackHole_CreateDevice(AudioServerPlugInDriverRef inDriver, CFDictionaryRef inDescription, const AudioServerPlugInClientInfo* inClientInfo, AudioObjectID* outDeviceObjectID)
{
// This method is used to tell a driver that implements the Transport Manager semantics to
// create an AudioEndpointDevice from a set of AudioEndpoints. Since this driver is not a
// Transport Manager, we just check the arguments and return
// kAudioHardwareUnsupportedOperationError.
#pragma unused(inDescription, inClientInfo, outDeviceObjectID)
// declare the local variables
OSStatus theAnswer = kAudioHardwareUnsupportedOperationError;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_CreateDevice: bad driver reference");
Done:
return theAnswer;
}
static OSStatus BlackHole_DestroyDevice(AudioServerPlugInDriverRef inDriver, AudioObjectID inDeviceObjectID)
{
// This method is used to tell a driver that implements the Transport Manager semantics to
// destroy an AudioEndpointDevice. Since this driver is not a Transport Manager, we just check
// the arguments and return kAudioHardwareUnsupportedOperationError.
#pragma unused(inDeviceObjectID)
// declare the local variables
OSStatus theAnswer = kAudioHardwareUnsupportedOperationError;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_DestroyDevice: bad driver reference");
Done:
return theAnswer;
}
static OSStatus BlackHole_AddDeviceClient(AudioServerPlugInDriverRef inDriver, AudioObjectID inDeviceObjectID, const AudioServerPlugInClientInfo* inClientInfo)
{
// This method is used to inform the driver about a new client that is using the given device.
// This allows the device to act differently depending on who the client is. This driver does
// not need to track the clients using the device, so we just check the arguments and return
// successfully.
#pragma unused(inClientInfo)
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_AddDeviceClient: bad driver reference");
FailWithAction(inDeviceObjectID != kObjectID_Device, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_AddDeviceClient: bad device ID");
Done:
return theAnswer;
}
static OSStatus BlackHole_RemoveDeviceClient(AudioServerPlugInDriverRef inDriver, AudioObjectID inDeviceObjectID, const AudioServerPlugInClientInfo* inClientInfo)
{
// This method is used to inform the driver about a client that is no longer using the given
// device. This driver does not track clients, so we just check the arguments and return
// successfully.
#pragma unused(inClientInfo)
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_RemoveDeviceClient: bad driver reference");
FailWithAction(inDeviceObjectID != kObjectID_Device, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_RemoveDeviceClient: bad device ID");
Done:
return theAnswer;
}
static OSStatus BlackHole_PerformDeviceConfigurationChange(AudioServerPlugInDriverRef inDriver, AudioObjectID inDeviceObjectID, UInt64 inChangeAction, void* inChangeInfo)
{
// This method is called to tell the device that it can perform the configuation change that it
// had requested via a call to the host method, RequestDeviceConfigurationChange(). The
// arguments, inChangeAction and inChangeInfo are the same as what was passed to
// RequestDeviceConfigurationChange().
//
// The HAL guarantees that IO will be stopped while this method is in progress. The HAL will
// also handle figuring out exactly what changed for the non-control related properties. This
// means that the only notifications that would need to be sent here would be for either
// custom properties the HAL doesn't know about or for controls.
//
// For the device implemented by this driver, only sample rate changes go through this process
// as it is the only state that can be changed for the device that isn't a control. For this
// change, the new sample rate is passed in the inChangeAction argument.
#pragma unused(inChangeInfo)
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_PerformDeviceConfigurationChange: bad driver reference");
FailWithAction(inDeviceObjectID != kObjectID_Device, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_PerformDeviceConfigurationChange: bad device ID");
FailWithAction((inChangeAction != 44100)
&& (inChangeAction != 48000)
&& (inChangeAction != 88200)
&& (inChangeAction != 96000)
&& (inChangeAction != 176400)
&& (inChangeAction != 192000)
&& (inChangeAction != 352800)
&& (inChangeAction != 384000)
&& (inChangeAction != 705600)
&& (inChangeAction != 768000)
&& (inChangeAction != 8000)
&& (inChangeAction != 16000),
theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_PerformDeviceConfigurationChange: bad sample rate");
// lock the state mutex
pthread_mutex_lock(&gPlugIn_StateMutex);
// change the sample rate
gDevice_SampleRate = inChangeAction;
// recalculate the state that depends on the sample rate
struct mach_timebase_info theTimeBaseInfo;
mach_timebase_info(&theTimeBaseInfo);
Float64 theHostClockFrequency = (Float64)theTimeBaseInfo.denom / (Float64)theTimeBaseInfo.numer;
theHostClockFrequency *= 1000000000.0;
gDevice_HostTicksPerFrame = theHostClockFrequency / gDevice_SampleRate;
// unlock the state mutex
pthread_mutex_unlock(&gPlugIn_StateMutex);
// DebugMsg("BlackHole theTimeBaseInfo.numer: %u \t theTimeBaseInfo.denom: %u", theTimeBaseInfo.numer, theTimeBaseInfo.denom);
Done:
return theAnswer;
}
static OSStatus BlackHole_AbortDeviceConfigurationChange(AudioServerPlugInDriverRef inDriver, AudioObjectID inDeviceObjectID, UInt64 inChangeAction, void* inChangeInfo)
{
// This method is called to tell the driver that a request for a config change has been denied.
// This provides the driver an opportunity to clean up any state associated with the request.
// For this driver, an aborted config change requires no action. So we just check the arguments
// and return
#pragma unused(inChangeAction, inChangeInfo)
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_PerformDeviceConfigurationChange: bad driver reference");
FailWithAction(inDeviceObjectID != kObjectID_Device, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_PerformDeviceConfigurationChange: bad device ID");
Done:
return theAnswer;
}
#pragma mark Property Operations
static Boolean BlackHole_HasProperty(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress)
{
// This method returns whether or not the given object has the given property.
// declare the local variables
Boolean theAnswer = false;
// check the arguments
FailIf(inDriver != gAudioServerPlugInDriverRef, Done, "BlackHole_HasProperty: bad driver reference");
FailIf(inAddress == NULL, Done, "BlackHole_HasProperty: no address");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPropertyData() method.
switch(inObjectID)
{
case kObjectID_PlugIn:
theAnswer = BlackHole_HasPlugInProperty(inDriver, inObjectID, inClientProcessID, inAddress);
break;
case kObjectID_Box:
theAnswer = BlackHole_HasBoxProperty(inDriver, inObjectID, inClientProcessID, inAddress);
break;
case kObjectID_Device:
theAnswer = BlackHole_HasDeviceProperty(inDriver, inObjectID, inClientProcessID, inAddress);
break;
case kObjectID_Stream_Input:
case kObjectID_Stream_Output:
theAnswer = BlackHole_HasStreamProperty(inDriver, inObjectID, inClientProcessID, inAddress);
break;
case kObjectID_Volume_Output_Master:
case kObjectID_Mute_Output_Master:
case kObjectID_Volume_Input_Master:
case kObjectID_Mute_Input_Master:
theAnswer = BlackHole_HasControlProperty(inDriver, inObjectID, inClientProcessID, inAddress);
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_IsPropertySettable(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, Boolean* outIsSettable)
{
// This method returns whether or not the given property on the object can have its value
// changed.
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_IsPropertySettable: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_IsPropertySettable: no address");
FailWithAction(outIsSettable == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_IsPropertySettable: no place to put the return value");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPropertyData() method.
switch(inObjectID)
{
case kObjectID_PlugIn:
theAnswer = BlackHole_IsPlugInPropertySettable(inDriver, inObjectID, inClientProcessID, inAddress, outIsSettable);
break;
case kObjectID_Box:
theAnswer = BlackHole_IsBoxPropertySettable(inDriver, inObjectID, inClientProcessID, inAddress, outIsSettable);
break;
case kObjectID_Device:
theAnswer = BlackHole_IsDevicePropertySettable(inDriver, inObjectID, inClientProcessID, inAddress, outIsSettable);
break;
case kObjectID_Stream_Input:
case kObjectID_Stream_Output:
theAnswer = BlackHole_IsStreamPropertySettable(inDriver, inObjectID, inClientProcessID, inAddress, outIsSettable);
break;
case kObjectID_Volume_Output_Master:
case kObjectID_Mute_Output_Master:
case kObjectID_Volume_Input_Master:
case kObjectID_Mute_Input_Master:
theAnswer = BlackHole_IsControlPropertySettable(inDriver, inObjectID, inClientProcessID, inAddress, outIsSettable);
break;
default:
theAnswer = kAudioHardwareBadObjectError;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_GetPropertyDataSize(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, UInt32 inQualifierDataSize, const void* inQualifierData, UInt32* outDataSize)
{
// This method returns the byte size of the property's data.
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_GetPropertyDataSize: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPropertyDataSize: no address");
FailWithAction(outDataSize == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPropertyDataSize: no place to put the return value");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPropertyData() method.
switch(inObjectID)
{
case kObjectID_PlugIn:
theAnswer = BlackHole_GetPlugInPropertyDataSize(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, outDataSize);
break;
case kObjectID_Box:
theAnswer = BlackHole_GetBoxPropertyDataSize(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, outDataSize);
break;
case kObjectID_Device:
theAnswer = BlackHole_GetDevicePropertyDataSize(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, outDataSize);
break;
case kObjectID_Stream_Input:
case kObjectID_Stream_Output:
theAnswer = BlackHole_GetStreamPropertyDataSize(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, outDataSize);
break;
case kObjectID_Volume_Output_Master:
case kObjectID_Mute_Output_Master:
case kObjectID_Volume_Input_Master:
case kObjectID_Mute_Input_Master:
theAnswer = BlackHole_GetControlPropertyDataSize(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, outDataSize);
break;
default:
theAnswer = kAudioHardwareBadObjectError;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_GetPropertyData(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, UInt32 inQualifierDataSize, const void* inQualifierData, UInt32 inDataSize, UInt32* outDataSize, void* outData)
{
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_GetPropertyData: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPropertyData: no address");
FailWithAction(outDataSize == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPropertyData: no place to put the return value size");
FailWithAction(outData == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPropertyData: no place to put the return value");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required.
//
// Also, since most of the data that will get returned is static, there are few instances where
// it is necessary to lock the state mutex.
switch(inObjectID)
{
case kObjectID_PlugIn:
theAnswer = BlackHole_GetPlugInPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, outDataSize, outData);
break;
case kObjectID_Box:
theAnswer = BlackHole_GetBoxPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, outDataSize, outData);
break;
case kObjectID_Device:
theAnswer = BlackHole_GetDevicePropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, outDataSize, outData);
break;
case kObjectID_Stream_Input:
case kObjectID_Stream_Output:
theAnswer = BlackHole_GetStreamPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, outDataSize, outData);
break;
case kObjectID_Volume_Output_Master:
case kObjectID_Mute_Output_Master:
case kObjectID_Volume_Input_Master:
case kObjectID_Mute_Input_Master:
theAnswer = BlackHole_GetControlPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, outDataSize, outData);
break;
default:
theAnswer = kAudioHardwareBadObjectError;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_SetPropertyData(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, UInt32 inQualifierDataSize, const void* inQualifierData, UInt32 inDataSize, const void* inData)
{
// declare the local variables
OSStatus theAnswer = 0;
UInt32 theNumberPropertiesChanged = 0;
AudioObjectPropertyAddress theChangedAddresses[2];
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_SetPropertyData: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_SetPropertyData: no address");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPropertyData() method.
switch(inObjectID)
{
case kObjectID_PlugIn:
theAnswer = BlackHole_SetPlugInPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, inData, &theNumberPropertiesChanged, theChangedAddresses);
break;
case kObjectID_Box:
theAnswer = BlackHole_SetBoxPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, inData, &theNumberPropertiesChanged, theChangedAddresses);
break;
case kObjectID_Device:
theAnswer = BlackHole_SetDevicePropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, inData, &theNumberPropertiesChanged, theChangedAddresses);
break;
case kObjectID_Stream_Input:
case kObjectID_Stream_Output:
theAnswer = BlackHole_SetStreamPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, inData, &theNumberPropertiesChanged, theChangedAddresses);
break;
case kObjectID_Volume_Output_Master:
case kObjectID_Mute_Output_Master:
case kObjectID_Volume_Input_Master:
case kObjectID_Mute_Input_Master:
theAnswer = BlackHole_SetControlPropertyData(inDriver, inObjectID, inClientProcessID, inAddress, inQualifierDataSize, inQualifierData, inDataSize, inData, &theNumberPropertiesChanged, theChangedAddresses);
break;
default:
theAnswer = kAudioHardwareBadObjectError;
break;
};
// send any notifications
if(theNumberPropertiesChanged > 0)
{
gPlugIn_Host->PropertiesChanged(gPlugIn_Host, inObjectID, theNumberPropertiesChanged, theChangedAddresses);
}
Done:
return theAnswer;
}
#pragma mark PlugIn Property Operations
static Boolean BlackHole_HasPlugInProperty(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress)
{
// This method returns whether or not the plug-in object has the given property.
#pragma unused(inClientProcessID)
// declare the local variables
Boolean theAnswer = false;
// check the arguments
FailIf(inDriver != gAudioServerPlugInDriverRef, Done, "BlackHole_HasPlugInProperty: bad driver reference");
FailIf(inAddress == NULL, Done, "BlackHole_HasPlugInProperty: no address");
FailIf(inObjectID != kObjectID_PlugIn, Done, "BlackHole_HasPlugInProperty: not the plug-in object");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPlugInPropertyData() method.
switch(inAddress->mSelector)
{
case kAudioObjectPropertyBaseClass:
case kAudioObjectPropertyClass:
case kAudioObjectPropertyOwner:
case kAudioObjectPropertyManufacturer:
case kAudioObjectPropertyOwnedObjects:
case kAudioPlugInPropertyBoxList:
case kAudioPlugInPropertyTranslateUIDToBox:
case kAudioPlugInPropertyDeviceList:
case kAudioPlugInPropertyTranslateUIDToDevice:
case kAudioPlugInPropertyResourceBundle:
theAnswer = true;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_IsPlugInPropertySettable(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, Boolean* outIsSettable)
{
// This method returns whether or not the given property on the plug-in object can have its
// value changed.
#pragma unused(inClientProcessID)
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_IsPlugInPropertySettable: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_IsPlugInPropertySettable: no address");
FailWithAction(outIsSettable == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_IsPlugInPropertySettable: no place to put the return value");
FailWithAction(inObjectID != kObjectID_PlugIn, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_IsPlugInPropertySettable: not the plug-in object");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPlugInPropertyData() method.
switch(inAddress->mSelector)
{
case kAudioObjectPropertyBaseClass:
case kAudioObjectPropertyClass:
case kAudioObjectPropertyOwner:
case kAudioObjectPropertyManufacturer:
case kAudioObjectPropertyOwnedObjects:
case kAudioPlugInPropertyBoxList:
case kAudioPlugInPropertyTranslateUIDToBox:
case kAudioPlugInPropertyDeviceList:
case kAudioPlugInPropertyTranslateUIDToDevice:
case kAudioPlugInPropertyResourceBundle:
*outIsSettable = false;
break;
default:
theAnswer = kAudioHardwareUnknownPropertyError;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_GetPlugInPropertyDataSize(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, UInt32 inQualifierDataSize, const void* inQualifierData, UInt32* outDataSize)
{
// This method returns the byte size of the property's data.
#pragma unused(inClientProcessID, inQualifierDataSize, inQualifierData)
// declare the local variables
OSStatus theAnswer = 0;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_GetPlugInPropertyDataSize: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPlugInPropertyDataSize: no address");
FailWithAction(outDataSize == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPlugInPropertyDataSize: no place to put the return value");
FailWithAction(inObjectID != kObjectID_PlugIn, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_GetPlugInPropertyDataSize: not the plug-in object");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required. There is more detailed commentary about each
// property in the BlackHole_GetPlugInPropertyData() method.
switch(inAddress->mSelector)
{
case kAudioObjectPropertyBaseClass:
*outDataSize = sizeof(AudioClassID);
break;
case kAudioObjectPropertyClass:
*outDataSize = sizeof(AudioClassID);
break;
case kAudioObjectPropertyOwner:
*outDataSize = sizeof(AudioObjectID);
break;
case kAudioObjectPropertyManufacturer:
*outDataSize = sizeof(CFStringRef);
break;
case kAudioObjectPropertyOwnedObjects:
if(gBox_Acquired)
{
*outDataSize = 2 * sizeof(AudioClassID);
}
else
{
*outDataSize = sizeof(AudioClassID);
}
break;
case kAudioPlugInPropertyBoxList:
*outDataSize = sizeof(AudioClassID);
break;
case kAudioPlugInPropertyTranslateUIDToBox:
*outDataSize = sizeof(AudioObjectID);
break;
case kAudioPlugInPropertyDeviceList:
if(gBox_Acquired)
{
*outDataSize = sizeof(AudioClassID);
}
else
{
*outDataSize = 0;
}
break;
case kAudioPlugInPropertyTranslateUIDToDevice:
*outDataSize = sizeof(AudioObjectID);
break;
case kAudioPlugInPropertyResourceBundle:
*outDataSize = sizeof(CFStringRef);
break;
default:
theAnswer = kAudioHardwareUnknownPropertyError;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_GetPlugInPropertyData(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, UInt32 inQualifierDataSize, const void* inQualifierData, UInt32 inDataSize, UInt32* outDataSize, void* outData)
{
#pragma unused(inClientProcessID)
// declare the local variables
OSStatus theAnswer = 0;
UInt32 theNumberItemsToFetch;
// check the arguments
FailWithAction(inDriver != gAudioServerPlugInDriverRef, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_GetPlugInPropertyData: bad driver reference");
FailWithAction(inAddress == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPlugInPropertyData: no address");
FailWithAction(outDataSize == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPlugInPropertyData: no place to put the return value size");
FailWithAction(outData == NULL, theAnswer = kAudioHardwareIllegalOperationError, Done, "BlackHole_GetPlugInPropertyData: no place to put the return value");
FailWithAction(inObjectID != kObjectID_PlugIn, theAnswer = kAudioHardwareBadObjectError, Done, "BlackHole_GetPlugInPropertyData: not the plug-in object");
// Note that for each object, this driver implements all the required properties plus a few
// extras that are useful but not required.
//
// Also, since most of the data that will get returned is static, there are few instances where
// it is necessary to lock the state mutex.
switch(inAddress->mSelector)
{
case kAudioObjectPropertyBaseClass:
// The base class for kAudioPlugInClassID is kAudioObjectClassID
FailWithAction(inDataSize < sizeof(AudioClassID), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioObjectPropertyBaseClass for the plug-in");
*((AudioClassID*)outData) = kAudioObjectClassID;
*outDataSize = sizeof(AudioClassID);
break;
case kAudioObjectPropertyClass:
// The class is always kAudioPlugInClassID for regular drivers
FailWithAction(inDataSize < sizeof(AudioClassID), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioObjectPropertyClass for the plug-in");
*((AudioClassID*)outData) = kAudioPlugInClassID;
*outDataSize = sizeof(AudioClassID);
break;
case kAudioObjectPropertyOwner:
// The plug-in doesn't have an owning object
FailWithAction(inDataSize < sizeof(AudioObjectID), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioObjectPropertyOwner for the plug-in");
*((AudioObjectID*)outData) = kAudioObjectUnknown;
*outDataSize = sizeof(AudioObjectID);
break;
case kAudioObjectPropertyManufacturer:
// This is the human readable name of the maker of the plug-in.
FailWithAction(inDataSize < sizeof(CFStringRef), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioObjectPropertyManufacturer for the plug-in");
*((CFStringRef*)outData) = CFSTR("Apple Inc.");
*outDataSize = sizeof(CFStringRef);
break;
case kAudioObjectPropertyOwnedObjects:
// Calculate the number of items that have been requested. Note that this
// number is allowed to be smaller than the actual size of the list. In such
// case, only that number of items will be returned
theNumberItemsToFetch = inDataSize / sizeof(AudioObjectID);
// Clamp that to the number of boxes this driver implements (which is just 1)
if(theNumberItemsToFetch > (gBox_Acquired ? 2 : 1))
{
theNumberItemsToFetch = (gBox_Acquired ? 2 : 1);
}
// Write the devices' object IDs into the return value
if(theNumberItemsToFetch > 1)
{
((AudioObjectID*)outData)[0] = kObjectID_Box;
((AudioObjectID*)outData)[0] = kObjectID_Device;
}
else if(theNumberItemsToFetch > 0)
{
((AudioObjectID*)outData)[0] = kObjectID_Box;
}
// Return how many bytes we wrote to
*outDataSize = theNumberItemsToFetch * sizeof(AudioClassID);
break;
case kAudioPlugInPropertyBoxList:
// Calculate the number of items that have been requested. Note that this
// number is allowed to be smaller than the actual size of the list. In such
// case, only that number of items will be returned
theNumberItemsToFetch = inDataSize / sizeof(AudioObjectID);
// Clamp that to the number of boxes this driver implements (which is just 1)
if(theNumberItemsToFetch > 1)
{
theNumberItemsToFetch = 1;
}
// Write the devices' object IDs into the return value
if(theNumberItemsToFetch > 0)
{
((AudioObjectID*)outData)[0] = kObjectID_Box;
}
// Return how many bytes we wrote to
*outDataSize = theNumberItemsToFetch * sizeof(AudioClassID);
break;
case kAudioPlugInPropertyTranslateUIDToBox:
// This property takes the CFString passed in the qualifier and converts that
// to the object ID of the box it corresponds to. For this driver, there is
// just the one box. Note that it is not an error if the string in the
// qualifier doesn't match any devices. In such case, kAudioObjectUnknown is
// the object ID to return.
{
FailWithAction(inDataSize < sizeof(AudioObjectID), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioPlugInPropertyTranslateUIDToBox");
FailWithAction(inQualifierDataSize == sizeof(CFStringRef), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: the qualifier is the wrong size for kAudioPlugInPropertyTranslateUIDToBox");
FailWithAction(inQualifierData == NULL, theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: no qualifier for kAudioPlugInPropertyTranslateUIDToBox");
CFStringRef formattedString = CFStringCreateWithFormat(NULL, NULL, CFSTR(kBox_UID), kNumber_Of_Channels);
if(CFStringCompare(*((CFStringRef*)inQualifierData), formattedString, 0) == kCFCompareEqualTo)
{
*((AudioObjectID*)outData) = kObjectID_Box;
}
else
{
*((AudioObjectID*)outData) = kAudioObjectUnknown;
}
*outDataSize = sizeof(AudioObjectID);
CFRelease(formattedString);
}
break;
case kAudioPlugInPropertyDeviceList:
// Calculate the number of items that have been requested. Note that this
// number is allowed to be smaller than the actual size of the list. In such
// case, only that number of items will be returned
theNumberItemsToFetch = inDataSize / sizeof(AudioObjectID);
// Clamp that to the number of devices this driver implements (which is just 1 if the
// box has been acquired)
if(theNumberItemsToFetch > (gBox_Acquired ? 1 : 0))
{
theNumberItemsToFetch = (gBox_Acquired ? 1 : 0);
}
// Write the devices' object IDs into the return value
if(theNumberItemsToFetch > 0)
{
((AudioObjectID*)outData)[0] = kObjectID_Device;
}
// Return how many bytes we wrote to
*outDataSize = theNumberItemsToFetch * sizeof(AudioClassID);
break;
case kAudioPlugInPropertyTranslateUIDToDevice:
// This property takes the CFString passed in the qualifier and converts that
// to the object ID of the device it corresponds to. For this driver, there is
// just the one device. Note that it is not an error if the string in the
// qualifier doesn't match any devices. In such case, kAudioObjectUnknown is
// the object ID to return.
{
FailWithAction(inDataSize < sizeof(AudioObjectID), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioPlugInPropertyTranslateUIDToDevice");
FailWithAction(inQualifierDataSize == sizeof(CFStringRef), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: the qualifier is the wrong size for kAudioPlugInPropertyTranslateUIDToDevice");
FailWithAction(inQualifierData == NULL, theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: no qualifier for kAudioPlugInPropertyTranslateUIDToDevice");
CFStringRef formattedString = CFStringCreateWithFormat(NULL, NULL, CFSTR(kBox_UID), kNumber_Of_Channels);
if(CFStringCompare(*((CFStringRef*)inQualifierData), formattedString, 0) == kCFCompareEqualTo)
{
*((AudioObjectID*)outData) = kObjectID_Device;
}
else
{
*((AudioObjectID*)outData) = kAudioObjectUnknown;
}
*outDataSize = sizeof(AudioObjectID);
CFRelease(formattedString);
}
break;
case kAudioPlugInPropertyResourceBundle:
// The resource bundle is a path relative to the path of the plug-in's bundle.
// To specify that the plug-in bundle itself should be used, we just return the
// empty string.
FailWithAction(inDataSize < sizeof(AudioObjectID), theAnswer = kAudioHardwareBadPropertySizeError, Done, "BlackHole_GetPlugInPropertyData: not enough space for the return value of kAudioPlugInPropertyResourceBundle");
*((CFStringRef*)outData) = CFSTR("");
*outDataSize = sizeof(CFStringRef);
break;
default:
theAnswer = kAudioHardwareUnknownPropertyError;
break;
};
Done:
return theAnswer;
}
static OSStatus BlackHole_SetPlugInPropertyData(AudioServerPlugInDriverRef inDriver, AudioObjectID inObjectID, pid_t inClientProcessID, const AudioObjectPropertyAddress* inAddress, UInt32 inQualifierDataSize, const void* inQualifierData, UInt32 inDataSize, const void* inData, UInt32* outNumberPropertiesChanged, AudioObjectPropertyAddress outChangedAddresses[2])
{
#pragma unused(inClientProcessID, inQualifierDataSize, inQualifierData, inDataSize, inData)