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