@@ -17,6 +17,46 @@ extension CameraSession {
1717 func takePhoto( options: TakePhotoOptions , promise: Promise ) {
1818 // Run on Camera Queue
1919 CameraQueues . cameraQueue. async {
20+ // Ensure AVCaptureSession is running
21+ if !self . captureSession. isRunning {
22+ VisionLogger . log ( level: . error, message: " Capture session is not running. Attempting to start... " )
23+ self . captureSession. startRunning ( )
24+ usleep ( 200_000 ) // 200ms
25+ if !self . captureSession. isRunning {
26+ promise. reject ( error: . session( . cameraNotReady) )
27+ return
28+ }
29+ }
30+
31+ // Check video connection readiness
32+ if let photoOutput = self . photoOutput,
33+ let videoConnection = photoOutput. connection ( with: . video) ,
34+ !( videoConnection. isEnabled && videoConnection. isActive) {
35+ VisionLogger . log ( level: . error, message: " No active/enabled video connection. Attempting auto-repair... " )
36+ self . captureSession. beginConfiguration ( )
37+ self . captureSession. removeOutput ( photoOutput)
38+ let newPhotoOutput = AVCapturePhotoOutput ( )
39+ self . captureSession. addOutput ( newPhotoOutput)
40+ self . photoOutput = newPhotoOutput
41+ if self . captureSession. canSetSessionPreset ( . photo) {
42+ self . captureSession. sessionPreset = . photo
43+ }
44+ self . captureSession. commitConfiguration ( )
45+ self . captureSession. startRunning ( )
46+ // Poll for video connection readiness (max 0.5s)
47+ var ready = false
48+ for _ in 0 ..< 10 {
49+ if let conn = newPhotoOutput. connection ( with: . video) , conn. isEnabled, conn. isActive {
50+ ready = true
51+ break
52+ }
53+ usleep ( 50_000 ) // 50ms
54+ }
55+ if !ready {
56+ promise. reject ( error: . session( . cameraNotReady) )
57+ return
58+ }
59+ }
2060 // Get Photo Output configuration
2161 guard let configuration = self . configuration else {
2262 promise. reject ( error: . session( . cameraNotReady) )
0 commit comments