-
-
Notifications
You must be signed in to change notification settings - Fork 48
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
jimcase
wants to merge
2
commits into
capawesome-team:main
Choose a base branch
from
jimcase:fix/start-prematurely
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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() | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.") | ||
} | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.