mrousavy / react-native-vision-camera

📸 A powerful, high-performance React Native Camera library.
https://react-native-vision-camera.com
MIT License
7.57k stars 1.1k forks source link

🐛 [AVCaptureDevice setActiveColorSpace:] Not supported - use activeFormat.supportedColorSpaces'. #2892

Open short-dsb opened 5 months ago

short-dsb commented 5 months ago

What's happening?

This iOS crash appears to be a new instance of #198 on v4.0.4.

Reproduceable Code

const camera = useRef<CameraRef>(null);
// ...
const device = useCameraDevice("back", {
  physicalDevices: [
    "ultra-wide-angle-camera",
    "wide-angle-camera",
    "telephoto-camera",
  ],
});
// ...
const format = useCameraFormat(device, [
  {
    photoAspectRatio: 1,
  },
  {
    videoAspectRatio: 1,
  },
  {
    photoResolution: {
      height: MAX_IMAGE_DIMENSION,
      width: MAX_IMAGE_DIMENSION,
    },
  },
  {
    videoResolution: {
      height: MAX_IMAGE_DIMENSION,
      width: MAX_IMAGE_DIMENSION,
    },
  },
]);
// ...
<Camera
  ref={camera}
  device={device}
  enableZoomGesture={true}
  isActive={isActive}
  photo={true}
  resizeMode="contain"
  style={styles.camera}
/>

Relevant log output

SIGABRT (#0): Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[AVCaptureDevice setActiveColorSpace:] Not supported - use activeFormat.supportedColorSpaces'.
0   CoreFoundation                      0x000000019cb9eb28 0x19cab2000 + 969512
1   libobjc.A.dylib                     0x00000001949eaf78 0x1949c0000 + 175992
2   AVFCapture                          0x00000001b9a3daac 0x1b99a3000 + 633516
3   libdispatch.dylib                   0x00000001a49aedd4 0x1a49ab000 + 15828
4   libdispatch.dylib                   0x00000001a49be2c4 0x1a49ab000 + 78532
5   AVFCapture                          0x00000001b9a3d948 0x1b99a3000 + 633160
6   Foundation                          0x000000019b9d4b0c 0x19b949000 + 572172
7   Foundation                          0x000000019b9d4884 0x19b949000 + 571524
8   Foundation                          0x000000019ba6e264 0x19b949000 + 1200740
9   AVFCapture                          0x00000001b99a971c 0x1b99a3000 + 26396
10  AVFCapture                          0x00000001b99a7e10 0x1b99a3000 + 19984
11  AVFCapture                          0x00000001b99a7b94 0x1b99a3000 + 19348
12  AVFCapture                          0x00000001b99aaf3c 0x1b99a3000 + 32572
13  shortboxed                          0x0000000104c16300 0x104030000 + 12477184
14  shortboxed                          0x0000000104c12830 0x104030000 + 12462128
15  shortboxed                          0x00000001040b4d1c 0x104030000 + 544028
16  libdispatch.dylib                   0x00000001a49ad13c 0x1a49ab000 + 8508
17  libdispatch.dylib                   0x00000001a49aedd4 0x1a49ab000 + 15828
18  libdispatch.dylib                   0x00000001a49b6400 0x1a49ab000 + 46080
19  libdispatch.dylib                   0x00000001a49b6f30 0x1a49ab000 + 48944
20  libdispatch.dylib                   0x00000001a49c1cb4 0x1a49ab000 + 93364
21  libdispatch.dylib                   0x00000001a49c1528 0x1a49ab000 + 91432
22  libsystem_pthread.dylib             0x00000001f8a94f20 0x1f8a93000 + 7968
23  libsystem_pthread.dylib             0x00000001f8a94fc0 0x1f8a93000 + 8128

Camera Device

// This is a production issue discovered via analytics; we do not have a local repro.

Device

iOS 17.4.1

VisionCamera Version

4.0.4

Can you reproduce this issue in the VisionCamera Example app?

No, I cannot reproduce the issue in the Example app

Additional information

mrousavy commented 5 months ago

I don't even have activeColorSpace in the codebase. This was in V2, but not in V3 or V4. Are you sure you are on 4.0.4?

short-dsb commented 5 months ago

Hm. Maybe this user was on an older version. I don’t see any repeats of this particular issue today. Thanks for taking a look. 🙂

mars-lan commented 4 months ago

Can confirm that this is still happening with 4.3.2. Here's the symbolicated stack traces:

          Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x83f20 __exceptionPreprocess
1  libobjc.A.dylib                0x16018 objc_exception_throw
2  AVFCapture                     0x59d90 __61-[AVCaptureDeferredPhotoProcessor _establishServerConnection]_block_invoke
3  libdispatch.dylib              0x3dd4 _dispatch_client_callout
4  libdispatch.dylib              0x132c4 _dispatch_lane_barrier_sync_invoke_and_complete
5  AVFCapture                     0x5d7ec -[AVCaptureFigVideoDevice setActiveColorSpace:]
6  Foundation                     0x147c0c -[NSObject(NSKeyValueObservingPrivate) _changeValueForKeys:count:maybeOldValuesDict:maybeNewValuesDict:usingBlock:]
7  Foundation                     0x1478ac -[NSObject(NSKeyValueObservingPrivate) _changeValueForKey:key:key:usingBlock:]
8  Foundation                     0x1467e8 _NSSetLongLongValueAndNotify
9  AVFCapture                     0xb698 -[AVCaptureSession _updateDeviceActiveFormatsAndActiveConnections]
10 AVFCapture                     0x28498 -[AVCaptureSession _buildAndRunGraph:]
11 AVFCapture                     0x2821c -[AVCaptureSession _commitConfiguration]
12 AVFCapture                     0x20cf4 -[AVCaptureSession addOutput:]
13 <app_name>                     0x6bdac8 CameraSession.configureOutputs(configuration:) + 16 (AVCapturePhotoOutput.QualityPrioritization+descriptor.swift:16)
14 <app_name>                     0x6b9fe4 closure #1 in CameraSession.configure(_:) + 135 (CameraSession.swift:135)
15 <app_name>                     0x8abcc thunk for @escaping @callee_guaranteed @Sendable () -> () (<compiler-generated>)

@mrousavy please consider reopening this issue.

mrousavy commented 4 months ago

This is a completely different issue @mars-lan. Also, what is the NSInvalidArgumentException's message? There's only a stacktrace, but no error.

mars-lan commented 4 months ago

This is a completely different issue @mars-lan. Also, what is the NSInvalidArgumentException's message? There's only a stacktrace, but no error.

My bad. Here's the missing error message for NSInvalidArgumentException, which is why I believe it's the same issue.

Fatal Exception: NSInvalidArgumentException
*** -[AVCaptureDevice setActiveColorSpace:] Not supported - use activeFormat.supportedColorSpaces
mrousavy commented 4 months ago

@mars-lan you're either on VisionCamera V2 (which is by far no longer supported), or this is an Apple bug. As I said above, activeColorSpace is not used by VisionCamera anymore.

mars-lan commented 4 months ago

@mars-lan you're either on VisionCamera V2 (which is by far no longer supported), or this is an Apple bug. As I said above, activeColorSpace is not used by VisionCamera anymore.

The app is using VisionCamera 4.3.2. You're correct that it doesn't set activeColorSpace directly, but based on the stack traces, the function is eventually invoked by AVFCapture:

5  AVFCapture                     0x5d7ec -[AVCaptureFigVideoDevice setActiveColorSpace:]

FWIW, we've seen this issue on iOS 16.5.1, 17.3.1 & 17.5.1 for various iPhone models (12, 13, 15).

mrousavy commented 4 months ago

Well then it might be an iOS bug? I don't see anything in the stacktrace that I'm doing wrong.. Also I can't reproduce this 😅

mars-lan commented 4 months ago

Well then it might be an iOS bug? I don't see anything in the stacktrace that I'm doing wrong.. Also I can't reproduce this 😅

It's possible. We also can't reproduce this ourselves either. Seen this on 7 different devices so far in Crashlytics so it's not exactly rare.

short-dsb commented 2 months ago

I don’t see any repeats of this particular issue today.

Just an update that we’re still seeing this issue periodically. As @mars-lan said, it seems to affect a variety of models and iOS versions.

Per this Stack Overflow comment, a workaround may be to set automaticallyConfiguresCaptureDeviceForWideColor to false on the AVCaptureSession instance. Worth a shot?

mrousavy commented 2 months ago

Interesting - yea can you just set that on your end and let me know if it fixed anything? You can put that inside the configure block.

foratbahrani commented 2 months ago

Same here, found this on firebase crashlytics + users reporting it, but can't reproduce it myself.

short-dsb commented 2 months ago

@mrousavy We haven’t seen this issue since we applied the workaround, although it’s only been a couple weeks. At the very least, there seem to be no undesirable side effects.

If anyone wants to try this as well and maybe get more signal, you can apply the following diff via patch-package.

diff --git a/node_modules/react-native-vision-camera/ios/Core/CameraSession.swift b/node_modules/react-native-vision-camera/ios/Core/CameraSession.swift
index 10b0f33..587b92e 100644
--- a/node_modules/react-native-vision-camera/ios/Core/CameraSession.swift
+++ b/node_modules/react-native-vision-camera/ios/Core/CameraSession.swift
@@ -132,6 +132,9 @@ final class CameraSession: NSObject, AVCaptureVideoDataOutputSampleBufferDelegat
       VisionLogger.log(level: .info, message: "configure { ... }: Updating CameraSession Configuration... \(difference)")

       do {
+        // https://github.com/mrousavy/react-native-vision-camera/issues/2892
+        self.captureSession.automaticallyConfiguresCaptureDeviceForWideColor = false;
+
         // If needed, configure the AVCaptureSession (inputs, outputs)
         if difference.isSessionConfigurationDirty {
           self.captureSession.beginConfiguration()
mars-lan commented 2 months ago

Can confirm that the fix works. @mrousavy please consider adding it.