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

This commit is contained in:
Ryan C. Gordon 2024-02-18 00:47:03 -05:00
parent f3485a47b3
commit 47313bba32
2 changed files with 32 additions and 21 deletions

View File

@ -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)

View File

@ -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;
}