Ariver
2026-07-12 24f551a39eae849d891da26cc91f90d354e3a2db
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
//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"
    }
}