VirtualBox

source: vbox/trunk/src/VBox/Devices/Audio/DrvHostCoreAudio-auth.mm@ 87854

Last change on this file since 87854 was 86221, checked in by vboxsync, 4 years ago

DrvHostCoreAudio-auth.mm: Darwin build fix. bugref:9790

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1/* $Id: DrvHostCoreAudio-auth.mm 86221 2020-09-22 14:10:23Z vboxsync $ */
2/** @file
3 * VBox audio devices - Mac OS X CoreAudio audio driver, 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#import <AVFoundation/AVFoundation.h>
29#import <AVFoundation/AVMediaFormat.h>
30#import <Foundation/NSException.h>
31
32
33#if MAC_OS_X_VERSION_MIN_REQUIRED < 101400
34
35/* HACK ALERT! It's there in the 10.13 SDK, but only for iOS 7.0+. Deploying CPP trickery to shut up warnings/errors. */
36# if MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
37# define AVAuthorizationStatus OurAVAuthorizationStatus
38# define AVAuthorizationStatusNotDetermined OurAVAuthorizationStatusNotDetermined
39# define AVAuthorizationStatusRestricted OurAVAuthorizationStatusRestricted
40# define AVAuthorizationStatusDenied OurAVAuthorizationStatusDenied
41# define AVAuthorizationStatusAuthorized OurAVAuthorizationStatusAuthorized
42# endif
43
44/**
45 * The authorization status enum.
46 *
47 * Starting macOS 10.14 we need to request permissions in order to use any audio input device
48 * but as we build against an older SDK where this is not available we have to duplicate
49 * AVAuthorizationStatus and do everything dynmically during runtime, sigh...
50 */
51typedef enum AVAuthorizationStatus: NSInteger
52{
53 AVAuthorizationStatusNotDetermined = 0,
54 AVAuthorizationStatusRestricted = 1,
55 AVAuthorizationStatusDenied = 2,
56 AVAuthorizationStatusAuthorized = 3,
57} AVAuthorizationStatus;
58
59#endif
60
61
62/**
63 * Requests camera permissions for Mojave and onwards.
64 *
65 * @returns VBox status code.
66 */
67static int coreAudioInputPermissionRequest(void)
68{
69 __block RTSEMEVENT hEvt = NIL_RTSEMEVENT;
70 __block int rc = RTSemEventCreate(&hEvt);
71 if (RT_SUCCESS(rc))
72 {
73 /* Perform auth request. */
74 [AVCaptureDevice performSelector: @selector(requestAccessForMediaType: completionHandler:) withObject: (id)AVMediaTypeAudio withObject: (id)^(BOOL granted) {
75 if (!granted) {
76 LogRel(("CoreAudio: Access denied!\n"));
77 rc = VERR_ACCESS_DENIED;
78 }
79 RTSemEventSignal(hEvt);
80 }];
81
82 rc = RTSemEventWait(hEvt, 10 * RT_MS_1SEC);
83 RTSemEventDestroy(hEvt);
84 }
85
86 return rc;
87}
88
89/**
90 * Checks permission for capturing devices on Mojave and onwards.
91 *
92 * @returns VBox status code.
93 */
94DECLHIDDEN(int) coreAudioInputPermissionCheck(void)
95{
96 int rc = VINF_SUCCESS;
97
98 if (NSFoundationVersionNumber >= 10.14)
99 {
100 /*
101 * Because we build with an older SDK where the authorization APIs are not available
102 * (introduced with Mojave 10.14) we have to resort to resolving the APIs dynamically.
103 */
104 LogRel(("CoreAudio: macOS 10.14+ detected, checking audio input permissions\n"));
105
106 if ([AVCaptureDevice respondsToSelector:@selector(authorizationStatusForMediaType:)])
107 {
108 AVAuthorizationStatus enmAuthSts = (AVAuthorizationStatus)(NSInteger)[AVCaptureDevice performSelector: @selector(authorizationStatusForMediaType:) withObject: (id)AVMediaTypeAudio];
109 if (enmAuthSts == AVAuthorizationStatusNotDetermined)
110 rc = coreAudioInputPermissionRequest();
111 else if ( enmAuthSts == AVAuthorizationStatusRestricted
112 || enmAuthSts == AVAuthorizationStatusDenied)
113 {
114 LogRel(("CoreAudio: Access denied!\n"));
115 rc = VERR_ACCESS_DENIED;
116 }
117 }
118 }
119
120 return rc;
121}
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette