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 2 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
9 changes: 9 additions & 0 deletions .changeset/forty-avocados-allow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'@capacitor-mlkit/barcode-scanning': minor
---

Introduce a serial DispatchQueue for managing AVCaptureSession operations in a FIFO (First In, First Out) sequence. Configuration tasks are encapsulated within a synchronous block to ensure complete setup before proceeding. Following this, startRunning() is scheduled asynchronously, guaranteeing it executes only after the configuration is fully committed.

This approach not only prevents the NSGenericException by ensuring proper sequence of operations but also maintains high performance and responsiveness of the application.

Related PR [fix(barcode-scanning): add delay before starting camera session #188](https://github.com/capawesome-team/capacitor-mlkit/pull/188)
Comment on lines +5 to +9
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Introduce a serial DispatchQueue for managing AVCaptureSession operations in a FIFO (First In, First Out) sequence. Configuration tasks are encapsulated within a synchronous block to ensure complete setup before proceeding. Following this, startRunning() is scheduled asynchronously, guaranteeing it executes only after the configuration is fully committed.
This approach not only prevents the NSGenericException by ensuring proper sequence of operations but also maintains high performance and responsiveness of the application.
Related PR [fix(barcode-scanning): add delay before starting camera session #188](https://github.com/capawesome-team/capacitor-mlkit/pull/188)
fix(ios): use queue for synchronizing the session configuration and start operations of the `scan(...)` method

101 changes: 61 additions & 40 deletions packages/barcode-scanning/ios/Plugin/BarcodeScannerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,63 +28,84 @@ 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()
}
}
}
}


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove this empty line.

required init?(coder: NSCoder) {
fatalError("coder initialization not supported.")
}
Expand Down