Skip to content

Commit

Permalink
refactor: add permission checker
Browse files Browse the repository at this point in the history
  • Loading branch information
YangJonghun committed Mar 11, 2024
1 parent b35d550 commit 604fc6f
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions ios/Video/Features/RCTVideoCapture.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import AVFoundation
import Photos

// MARK: - CaptureError

enum CaptureError: Error {
case permissionDenied
case emptyPlayerItem
case emptyPlayerItemOutput
case emptyBuffer
Expand All @@ -22,12 +24,11 @@ enum RCTVideoCapture {
) {
DispatchQueue.global(qos: .userInitiated).async {
do {
try RCTVideoCapture.checkPhotoAddPermission()
let playerItem = try playerItem ?? { throw CaptureError.emptyPlayerItem }()
let playerOutput = try playerOutput ?? { throw CaptureError.emptyPlayerItemOutput }()

let currentTime = playerItem.currentTime()
let settings = [kCVPixelBufferPixelFormatTypeKey as String: kCVPixelFormatType_32BGRA]
let output = AVPlayerItemVideoOutput(pixelBufferAttributes: settings)
let buffer = try playerOutput.copyPixelBuffer(forItemTime: currentTime, itemTimeForDisplay: nil) ?? { throw CaptureError.emptyBuffer }()

let ciImage = CIImage(cvPixelBuffer: buffer)
Expand All @@ -50,4 +51,19 @@ enum RCTVideoCapture {
}
}
}

static private func checkPhotoAddPermission() throws {
var status: PHAuthorizationStatus?
if #available(iOS 14, *) {
status = PHPhotoLibrary.authorizationStatus(for: .addOnly)
} else {
status = PHPhotoLibrary.authorizationStatus()
}
switch status {
case .restricted, .denied:
throw CaptureError.permissionDenied
default:
return;
}
}
}

0 comments on commit 604fc6f

Please sign in to comment.