SDL: camera: SDL_GetCameraDevices should not report "no devices" like an error.

From 47313bba32157edddbac4bb206878e4772513fd7 Mon Sep 17 00:00:00 2001
From: "Ryan C. Gordon" <[EMAIL REDACTED]>
Date: Sun, 18 Feb 2024 00:47:03 -0500
Subject: [PATCH] camera: SDL_GetCameraDevices should not report "no devices"
 like an error.

---
 src/camera/SDL_camera.c  | 39 ++++++++++++++++++++-------------------
 test/testcameraminimal.c | 14 ++++++++++++--
 2 files changed, 32 insertions(+), 21 deletions(-)

diff --git a/src/camera/SDL_camera.c b/src/camera/SDL_camera.c
index 6088f3d49d4ac..eb957d73d358e 100644
--- a/src/camera/SDL_camera.c
+++ b/src/camera/SDL_camera.c
@@ -539,7 +539,13 @@ char *SDL_GetCameraDeviceName(SDL_CameraDeviceID instance_id)
 
 SDL_CameraDeviceID *SDL_GetCameraDevices(int *count)
 {
+    int dummy_count;
+    if (!count) {
+        count = &dummy_count;
+    }
+
     if (!SDL_GetCurrentCameraDriver()) {
+        *count = 0;
         SDL_SetError("Camera subsystem is not initialized");
         return NULL;
     }
@@ -548,31 +554,26 @@ SDL_CameraDeviceID *SDL_GetCameraDevices(int *count)
 
     SDL_LockRWLockForReading(camera_driver.device_hash_lock);
     int num_devices = SDL_AtomicGet(&camera_driver.device_count);
-    if (num_devices > 0) {
-        retval = (SDL_CameraDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_CameraDeviceID));
-        if (!retval) {
-            num_devices = 0;
-        } else {
-            int devs_seen = 0;
-            const void *key;
-            const void *value;
-            void *iter = NULL;
-            while (SDL_IterateHashTable(camera_driver.device_hash, &key, &value, &iter)) {
-                retval[devs_seen++] = (SDL_CameraDeviceID) (uintptr_t) key;
-            }
-
-            SDL_assert(devs_seen == num_devices);
-            retval[devs_seen] = 0;  // null-terminated.
+    retval = (SDL_CameraDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_CameraDeviceID));
+    if (!retval) {
+        num_devices = 0;
+    } else {
+        int devs_seen = 0;
+        const void *key;
+        const void *value;
+        void *iter = NULL;
+        while (SDL_IterateHashTable(camera_driver.device_hash, &key, &value, &iter)) {
+            retval[devs_seen++] = (SDL_CameraDeviceID) (uintptr_t) key;
         }
+
+        SDL_assert(devs_seen == num_devices);
+        retval[devs_seen] = 0;  // null-terminated.
     }
     SDL_UnlockRWLock(camera_driver.device_hash_lock);
 
-    if (count) {
-        *count = num_devices;
-    }
+    *count = num_devices;
 
     return retval;
-
 }
 
 SDL_CameraSpec *SDL_GetCameraDeviceSupportedFormats(SDL_CameraDeviceID instance_id, int *count)
diff --git a/test/testcameraminimal.c b/test/testcameraminimal.c
index 6eb4f7968289a..9fc04c534b9db 100644
--- a/test/testcameraminimal.c
+++ b/test/testcameraminimal.c
@@ -26,6 +26,9 @@ static SDL_Surface *frame_current = NULL;
 
 int SDL_AppInit(int argc, char *argv[])
 {
+    int devcount = 0;
+    int i;
+
     /* Initialize test framework */
     state = SDLTest_CommonCreateState(argv, 0);
     if (state == NULL) {
@@ -55,17 +58,24 @@ int SDL_AppInit(int argc, char *argv[])
         return -1;
     }
 
-    SDL_CameraDeviceID *devices = SDL_GetCameraDevices(NULL);
+    SDL_CameraDeviceID *devices = SDL_GetCameraDevices(&devcount);
     if (!devices) {
         SDL_Log("SDL_GetCameraDevices failed: %s", SDL_GetError());
         return -1;
     }
 
+    SDL_Log("Saw %d camera devices.", devcount);
+    for (i = 0; i < devcount; i++) {
+        char *name = SDL_GetCameraDeviceName(devices[i]);
+        SDL_Log("  - Camera #%d: %s", i, name);
+        SDL_free(name);
+    }
+
     const SDL_CameraDeviceID devid = devices[0];  /* just take the first one. */
     SDL_free(devices);
 
     if (!devid) {
-        SDL_Log("No cameras available? %s", SDL_GetError());
+        SDL_Log("No cameras available?");
         return -1;
     }