//go:build darwin
|
|
package audio
|
|
/*
|
#cgo CFLAGS: -x objective-c
|
#cgo LDFLAGS: -framework AVFoundation
|
#import <AVFoundation/AVFoundation.h>
|
|
static int pvMicrophoneStatus(void) {
|
return (int)[AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio];
|
}
|
|
static int pvRequestMicrophone(void) {
|
__block int granted = 0;
|
dispatch_semaphore_t sem = dispatch_semaphore_create(0);
|
[AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL ok) {
|
granted = ok ? 1 : 0;
|
dispatch_semaphore_signal(sem);
|
}];
|
dispatch_semaphore_wait(sem, dispatch_time(DISPATCH_TIME_NOW, 30 * NSEC_PER_SEC));
|
return granted;
|
}
|
*/
|
import "C"
|
|
type microphoneAuthorization struct {
|
granted bool
|
state string
|
}
|
|
var ensureMicrophoneAuthorization = defaultEnsureMicrophoneAuthorization
|
|
func defaultEnsureMicrophoneAuthorization() microphoneAuthorization {
|
status := int(C.pvMicrophoneStatus())
|
if status == 0 {
|
C.pvRequestMicrophone()
|
status = int(C.pvMicrophoneStatus())
|
}
|
return microphoneAuthorization{
|
granted: status == 3,
|
state: microphoneAuthorizationState(status),
|
}
|
}
|
|
func microphoneAuthorizationState(status int) string {
|
switch status {
|
case 0:
|
return "notDetermined"
|
case 1:
|
return "restricted"
|
case 2:
|
return "denied"
|
case 3:
|
return "granted"
|
default:
|
return "unknown"
|
}
|
}
|