1 | /* $Id: DrvHostAudioCoreAudioAuth.mm 89110 2021-05-17 16:04:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * Host audio driver - Mac OS X CoreAudio, authorization helpers for Mojave+.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #define LOG_GROUP LOG_GROUP_DRV_HOST_AUDIO
|
---|
23 | #include <VBox/log.h>
|
---|
24 |
|
---|
25 | #include <iprt/errcore.h>
|
---|
26 | #include <iprt/semaphore.h>
|
---|
27 |
|
---|
28 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070
|
---|
29 | # import <AVFoundation/AVFoundation.h>
|
---|
30 | # import <AVFoundation/AVMediaFormat.h>
|
---|
31 | #endif
|
---|
32 | #import <Foundation/NSException.h>
|
---|
33 |
|
---|
34 |
|
---|
35 | #if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
|
---|
36 |
|
---|
37 | /* HACK ALERT! It's there in the 10.13 SDK, but only for iOS 7.0+. Deploying CPP trickery to shut up warnings/errors. */
|
---|
38 | # if MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
|
---|
39 | # define AVAuthorizationStatus OurAVAuthorizationStatus
|
---|
40 | # define AVAuthorizationStatusNotDetermined OurAVAuthorizationStatusNotDetermined
|
---|
41 | # define AVAuthorizationStatusRestricted OurAVAuthorizationStatusRestricted
|
---|
42 | # define AVAuthorizationStatusDenied OurAVAuthorizationStatusDenied
|
---|
43 | # define AVAuthorizationStatusAuthorized OurAVAuthorizationStatusAuthorized
|
---|
44 | # endif
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * The authorization status enum.
|
---|
48 | *
|
---|
49 | * Starting macOS 10.14 we need to request permissions in order to use any audio input device
|
---|
50 | * but as we build against an older SDK where this is not available we have to duplicate
|
---|
51 | * AVAuthorizationStatus and do everything dynmically during runtime, sigh...
|
---|
52 | */
|
---|
53 | typedef enum AVAuthorizationStatus
|
---|
54 | # if RT_CPLUSPLUS_PREREQ(201100)
|
---|
55 | : NSInteger
|
---|
56 | # endif
|
---|
57 | {
|
---|
58 | AVAuthorizationStatusNotDetermined = 0,
|
---|
59 | AVAuthorizationStatusRestricted = 1,
|
---|
60 | AVAuthorizationStatusDenied = 2,
|
---|
61 | AVAuthorizationStatusAuthorized = 3
|
---|
62 | } AVAuthorizationStatus;
|
---|
63 |
|
---|
64 | #endif
|
---|
65 |
|
---|
66 |
|
---|
67 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 /** @todo need some better fix/whatever for AudioTest */
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Requests camera permissions for Mojave and onwards.
|
---|
71 | *
|
---|
72 | * @returns VBox status code.
|
---|
73 | */
|
---|
74 | static int coreAudioInputPermissionRequest(void)
|
---|
75 | {
|
---|
76 | __block RTSEMEVENT hEvt = NIL_RTSEMEVENT;
|
---|
77 | __block int rc = RTSemEventCreate(&hEvt);
|
---|
78 | if (RT_SUCCESS(rc))
|
---|
79 | {
|
---|
80 | /* Perform auth request. */
|
---|
81 | [AVCaptureDevice performSelector: @selector(requestAccessForMediaType: completionHandler:)
|
---|
82 | withObject: (id)AVMediaTypeAudio
|
---|
83 | withObject: (id)^(BOOL granted)
|
---|
84 | {
|
---|
85 | if (!granted)
|
---|
86 | {
|
---|
87 | LogRel(("CoreAudio: Access denied!\n"));
|
---|
88 | rc = VERR_ACCESS_DENIED;
|
---|
89 | }
|
---|
90 | RTSemEventSignal(hEvt);
|
---|
91 | }];
|
---|
92 |
|
---|
93 | rc = RTSemEventWait(hEvt, RT_MS_10SEC);
|
---|
94 | RTSemEventDestroy(hEvt);
|
---|
95 | }
|
---|
96 |
|
---|
97 | return rc;
|
---|
98 | }
|
---|
99 |
|
---|
100 | #endif
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * Checks permission for capturing devices on Mojave and onwards.
|
---|
104 | *
|
---|
105 | * @returns VBox status code.
|
---|
106 | */
|
---|
107 | DECLHIDDEN(int) coreAudioInputPermissionCheck(void)
|
---|
108 | {
|
---|
109 | int rc = VINF_SUCCESS;
|
---|
110 |
|
---|
111 | if (NSFoundationVersionNumber >= 10.14)
|
---|
112 | {
|
---|
113 | /*
|
---|
114 | * Because we build with an older SDK where the authorization APIs are not available
|
---|
115 | * (introduced with Mojave 10.14) we have to resort to resolving the APIs dynamically.
|
---|
116 | */
|
---|
117 | #if MAC_OS_X_VERSION_MIN_REQUIRED >= 1070 /** @todo need some better fix/whatever for AudioTest */
|
---|
118 | LogRel(("CoreAudio: macOS 10.14+ detected, checking audio input permissions\n"));
|
---|
119 |
|
---|
120 | if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
|
---|
121 | {
|
---|
122 | AVAuthorizationStatus enmAuthSts
|
---|
123 | = (AVAuthorizationStatus)(NSInteger)[AVCaptureDevice performSelector: @selector(authorizationStatusForMediaType:)
|
---|
124 | withObject: (id)AVMediaTypeAudio];
|
---|
125 | if (enmAuthSts == AVAuthorizationStatusNotDetermined)
|
---|
126 | rc = coreAudioInputPermissionRequest();
|
---|
127 | else if ( enmAuthSts == AVAuthorizationStatusRestricted
|
---|
128 | || enmAuthSts == AVAuthorizationStatusDenied)
|
---|
129 | {
|
---|
130 | LogRel(("CoreAudio: Access denied!\n"));
|
---|
131 | rc = VERR_ACCESS_DENIED;
|
---|
132 | }
|
---|
133 | }
|
---|
134 | #else
|
---|
135 | LogRel(("CoreAudio: WARNING! macOS 10.14+ detected. Audio input probably wont work as this app was compiled using a too old SDK.\n"));
|
---|
136 | #endif
|
---|
137 | }
|
---|
138 |
|
---|
139 | return rc;
|
---|
140 | }
|
---|