Badminton-Scoreboard/Classes/Unity/AVCapture.mm
2023-10-08 10:24:48 +08:00

46 lines
1.3 KiB
Plaintext

#include "AVCapture.h"
#include <AVFoundation/AVFoundation.h>
static NSString* MediaTypeFromEnum(int captureType)
{
if (captureType == avAudioCapture)
return AVMediaTypeAudio;
else if (captureType == avVideoCapture)
return AVMediaTypeVideo;
return nil;
}
extern "C" int UnityGetAVCapturePermission(int captureType)
{
NSString* mediaType = MediaTypeFromEnum(captureType);
if (mediaType == nil)
return avCapturePermissionDenied;
#if !PLATFORM_TVOS && (UNITY_USES_WEBCAM || UNITY_USES_MICROPHONE)
NSInteger status = AVAuthorizationStatusAuthorized;
status = [AVCaptureDevice authorizationStatusForMediaType: mediaType];
if (status == AVAuthorizationStatusNotDetermined)
return avCapturePermissionUnknown;
else if (status == AVAuthorizationStatusAuthorized)
return avCapturePermissionGranted;
#endif
return avCapturePermissionDenied;
}
extern "C" void UnityRequestAVCapturePermission(int captureType, void* userData)
{
#if !PLATFORM_TVOS && (UNITY_USES_WEBCAM || UNITY_USES_MICROPHONE)
NSString* mediaType = MediaTypeFromEnum(captureType);
if (mediaType == nil)
return;
[AVCaptureDevice requestAccessForMediaType: mediaType completionHandler:^(BOOL granted) {
UnityReportAVCapturePermission(userData);
}];
#endif
}