Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: DispatchQueue for synchronizing the session configuration and start operations #214

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/forty-avocados-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@capacitor-mlkit/barcode-scanning': minor
---

fix(ios): use queue for synchronizing the session configuration and start operations of the `scan(...)` method
100 changes: 60 additions & 40 deletions packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,59 +28,79 @@ public protocol BarcodeScannerViewDelegate {
private var torchButton: UIButton?
private var detectionAreaView: UIView?
private var detectionAreaViewFrame: CGRect?
private var error: Error?

init (implementation: BarcodeScanner, settings: ScanSettings) throws {
self.implementation = implementation
self.settings = settings

// creates a serial DispatchQueue, which ensures operations are executed in a First In, First Out
// (FIFO) order, meaning tasks are completed one at a time in the exact order they were added to
// the queue.
let captureSessionQueue = DispatchQueue(label: "com.google.mlkit.visiondetector.CaptureSessionQueue")

super.init(frame: UIScreen.main.bounds)

let captureSession = AVCaptureSession()
captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSession.Preset.hd1280x720
// Prepare capture session and preview layer
// It executes tasks one at a time in the order they are added (FIFO), ensuring that no other
// tasks on the same queue can run simultaneously or out of order with respect to the synchronous
// block
captureSessionQueue.sync {
do {
captureSession.beginConfiguration()
captureSession.sessionPreset = AVCaptureSession.Preset.hd1280x720

let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: AVMediaType.video, position: settings.lensFacing)
guard let captureDevice = captureDevice else {
throw RuntimeError(implementation.plugin.errorNoCaptureDeviceAvailable)
}
var deviceInput: AVCaptureDeviceInput
deviceInput = try AVCaptureDeviceInput(device: captureDevice)
if captureSession.canAddInput(deviceInput) {
captureSession.addInput(deviceInput)
} else {
throw RuntimeError(implementation.plugin.errorCannotAddCaptureInput)
guard let captureDevice = AVCaptureDevice.default(.builtInWideAngleCamera, for: .video, position: settings.lensFacing),
let deviceInput = try? AVCaptureDeviceInput(device: captureDevice) else {
throw RuntimeError(implementation.plugin.errorNoCaptureDeviceAvailable)
}

if captureSession.canAddInput(deviceInput) {
captureSession.addInput(deviceInput)
} else {
throw RuntimeError(implementation.plugin.errorCannotAddCaptureInput)
}

let deviceOutput = AVCaptureVideoDataOutput()
deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.alwaysDiscardsLateVideoFrames = true
let outputQueue = DispatchQueue(label: "com.google.mlkit.visiondetector.VideoDataOutputQueue")
deviceOutput.setSampleBufferDelegate(self, queue: outputQueue)

if captureSession.canAddOutput(deviceOutput) {
captureSession.addOutput(deviceOutput)
} else {
throw RuntimeError(implementation.plugin.errorCannotAddCaptureOutput)
}

captureSession.commitConfiguration()
} catch {
print("Failed to configure AVCaptureSession: \(error)")
self.error = error
}
}
let deviceOutput = AVCaptureVideoDataOutput()
deviceOutput.videoSettings = [kCVPixelBufferPixelFormatTypeKey as String: Int(kCVPixelFormatType_32BGRA)]
deviceOutput.alwaysDiscardsLateVideoFrames = true
let outputQueue = DispatchQueue(label: "com.google.mlkit.visiondetector.VideoDataOutputQueue")
deviceOutput.setSampleBufferDelegate(self, queue: outputQueue)
if captureSession.canAddOutput(deviceOutput) {
captureSession.addOutput(deviceOutput)
} else {
throw RuntimeError(implementation.plugin.errorCannotAddCaptureOutput)

if let error = self.error {
throw error
}
captureSession.commitConfiguration()
// `session.startRunning()` should be called after `session.commitConfiguration()` is complete.
// However, occacsionally `commitConfiguration()` runs asynchronously, so when `startRunning()`
// is called, `commitConfiguration()` is still in progress and the state is still `uncommited`.
// This can be reproduced by repeatedly switching or toggling the camera using the plugin demo.
// Adding a 100ms delay ensures that `session.commitConfiguration()` is complete before calling
// `session.startRunning()`.
Thread.sleep(forTimeInterval: 0.1)
DispatchQueue.global(qos: .background).async {

// Add Start task to the queue in the order, each task starts only after the previous task has
// finished, ensuring captureSession.startRunning() starts after the sync block
captureSessionQueue.async {
captureSession.startRunning()
}
self.captureSession = captureSession
let formats = settings.formats.count == 0 ? BarcodeFormat.all : BarcodeFormat(settings.formats)
self.barcodeScannerInstance = MLKitBarcodeScanner.barcodeScanner(options: BarcodeScannerOptions(formats: formats))

DispatchQueue.main.async {
self.captureSession = captureSession
let formats = settings.formats.count == 0 ? BarcodeFormat.all : BarcodeFormat(settings.formats)
self.barcodeScannerInstance = MLKitBarcodeScanner.barcodeScanner(options: BarcodeScannerOptions(formats: formats))
self.setVideoPreviewLayer(AVCaptureVideoPreviewLayer(session: captureSession))

self.setVideoPreviewLayer(AVCaptureVideoPreviewLayer(session: captureSession))

if settings.showUIElements {
self.addCancelButton()
if implementation.isTorchAvailable() {
self.addTorchButton()
if settings.showUIElements {
self.addCancelButton()
if implementation.isTorchAvailable() {
self.addTorchButton()
}
}
}
}
Expand Down