AVCapture.mm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. #include "AVCapture.h"
  2. #include <AVFoundation/AVFoundation.h>
  3. static NSString* MediaTypeFromEnum(int captureType)
  4. {
  5. if (captureType == avAudioCapture)
  6. return AVMediaTypeAudio;
  7. else if (captureType == avVideoCapture)
  8. return AVMediaTypeVideo;
  9. return nil;
  10. }
  11. extern "C" int UnityGetAVCapturePermission(int captureType)
  12. {
  13. NSString* mediaType = MediaTypeFromEnum(captureType);
  14. if (mediaType == nil)
  15. return avCapturePermissionDenied;
  16. #if !PLATFORM_TVOS && (UNITY_USES_WEBCAM || UNITY_USES_MICROPHONE)
  17. NSInteger status = AVAuthorizationStatusAuthorized;
  18. status = [AVCaptureDevice authorizationStatusForMediaType: mediaType];
  19. if (status == AVAuthorizationStatusNotDetermined)
  20. return avCapturePermissionUnknown;
  21. else if (status == AVAuthorizationStatusAuthorized)
  22. return avCapturePermissionGranted;
  23. #endif
  24. return avCapturePermissionDenied;
  25. }
  26. extern "C" void UnityRequestAVCapturePermission(int captureType)
  27. {
  28. #if !PLATFORM_TVOS && (UNITY_USES_WEBCAM || UNITY_USES_MICROPHONE)
  29. NSString* mediaType = MediaTypeFromEnum(captureType);
  30. if (mediaType == nil)
  31. return;
  32. [AVCaptureDevice requestAccessForMediaType: mediaType completionHandler:^(BOOL granted) {
  33. UnityReportAVCapturePermission();
  34. }];
  35. #endif
  36. }