SDL: camera: Fix crash when setting frame rate and improve FPS precision (ef03c)

From ef03c66ef390265c94d7bc3dce3c615a7eec6219 Mon Sep 17 00:00:00 2001
From: Qiu Qiang <[EMAIL REDACTED]>
Date: Mon, 2 Mar 2026 14:11:41 -0500
Subject: [PATCH] camera: Fix crash when setting frame rate and improve FPS
 precision

- Use exact numerator/denominator from spec for CMTime to avoid rounding errors.
- Wrap frame rate setting in @try-catch to prevent crashes on strict drivers.

(cherry picked from commit e2a2e2c31e71d9a5a478918ce5fddfb14f814dbb)
---
 src/camera/coremedia/SDL_camera_coremedia.m | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/src/camera/coremedia/SDL_camera_coremedia.m b/src/camera/coremedia/SDL_camera_coremedia.m
index c171d604b6326..ef4d9a93d3d9f 100644
--- a/src/camera/coremedia/SDL_camera_coremedia.m
+++ b/src/camera/coremedia/SDL_camera_coremedia.m
@@ -368,8 +368,7 @@ static bool COREMEDIA_OpenDevice(SDL_Camera *device, const SDL_CameraSpec *spec)
     avdevice.activeFormat = spec_format;
 
     // Try to set the frame duration to enforce the requested frame rate
-    const float frameRate = (float)spec->framerate_numerator / spec->framerate_denominator;
-    const CMTime frameDuration = CMTimeMake(1, (int32_t)frameRate);
+    const CMTime frameDuration = CMTimeMake(spec->framerate_denominator, spec->framerate_numerator);
 
     // Check if the device supports setting frame duration
     if ([avdevice respondsToSelector:@selector(setActiveVideoMinFrameDuration:)] &&
@@ -439,8 +438,12 @@ static bool COREMEDIA_OpenDevice(SDL_Camera *device, const SDL_CameraSpec *spec)
 
     // Try to set the frame rate on the device (preferred modern approach)
     if ([avdevice lockForConfiguration:nil]) {
-        avdevice.activeVideoMinFrameDuration = frameDuration;
-        avdevice.activeVideoMaxFrameDuration = frameDuration;
+        @try {
+            avdevice.activeVideoMinFrameDuration = frameDuration;
+            avdevice.activeVideoMaxFrameDuration = frameDuration;
+        } @catch (NSException *exception) {
+            // Some devices don't support setting frame duration, that's okay
+        }
         [avdevice unlockForConfiguration];
     }