Skip to content

Commit

Permalink
camera: Reenabled macOS/iOS support, with rewritten CoreMedia impleme…
Browse files Browse the repository at this point in the history
…ntation.
  • Loading branch information
icculus committed Feb 6, 2024
1 parent a3b3e7e commit ad30a0d
Show file tree
Hide file tree
Showing 8 changed files with 316 additions and 429 deletions.
4 changes: 2 additions & 2 deletions include/SDL3/SDL_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ typedef struct SDL_CameraSpec
Uint32 format; /**< Frame SDL_PixelFormatEnum format */
int width; /**< Frame width */
int height; /**< Frame height */
int interval_numerator; /**< Frame rate numerator ((dom / num) == fps) */
int interval_denominator; /**< Frame rate demoninator ((dom / num) == fps)*/
int interval_numerator; /**< Frame rate numerator ((dom / num) == fps, (num / dom) == duration) */
int interval_denominator; /**< Frame rate demoninator ((dom / num) == fps, (num / dom) == duration) */
} SDL_CameraSpec;

/**
Expand Down
2 changes: 0 additions & 2 deletions include/build_config/SDL_build_config.h.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@

#cmakedefine USE_POSIX_SPAWN @USE_POSIX_SPAWN@

#cmakedefine HAVE_COREMEDIA

/* SDL internal assertion support */
#if @SDL_DEFAULT_ASSERT_LEVEL_CONFIGURED@
#cmakedefine SDL_DEFAULT_ASSERT_LEVEL @SDL_DEFAULT_ASSERT_LEVEL@
Expand Down
6 changes: 4 additions & 2 deletions include/build_config/SDL_build_config_ios.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,8 +197,6 @@
#define SDL_VIDEO_METAL 1
#endif

#define HAVE_COREMEDIA 1

/* Enable system power support */
#define SDL_POWER_UIKIT 1

Expand All @@ -212,6 +210,10 @@
#define SDL_FILESYSTEM_COCOA 1

/* enable camera support */
#ifndef SDL_PLATFORM_TVOS
#define SDL_CAMERA_DRIVER_COREMEDIA 1
#endif

#define SDL_CAMERA_DRIVER_DUMMY 1

#endif /* SDL_build_config_ios_h_ */
2 changes: 0 additions & 2 deletions include/build_config/SDL_build_config_macos.h
Original file line number Diff line number Diff line change
Expand Up @@ -260,8 +260,6 @@
#endif
#endif

#define HAVE_COREMEDIA 1

/* Enable system power support */
#define SDL_POWER_MACOSX 1

Expand Down
10 changes: 8 additions & 2 deletions src/camera/SDL_camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ const char *SDL_GetCurrentCameraDriver(void)
return camera_driver.name;
}

char *SDL_GetCameraThreadName(SDL_CameraDevice *device, char *buf, size_t buflen)
{
(void)SDL_snprintf(buf, buflen, "SDLCamera%d", (int) device->instance_id);
return buf;
}

int SDL_AddCameraFormat(CameraFormatAddData *data, Uint32 fmt, int w, int h, int interval_numerator, int interval_denominator)
{
SDL_assert(data != NULL);
Expand Down Expand Up @@ -683,7 +689,7 @@ SDL_bool SDL_CameraThreadIterate(SDL_CameraDevice *device)
failed = SDL_TRUE;
}

// we can let go of the lock once we've tried to grab a frame of video and maybe moved the output frame from the empty to the filled list.
// we can let go of the lock once we've tried to grab a frame of video and maybe moved the output frame off the empty list.
// this lets us chew up the CPU for conversion and scaling without blocking other threads.
SDL_UnlockMutex(device->lock);

Expand Down Expand Up @@ -988,7 +994,7 @@ SDL_Camera *SDL_OpenCameraDevice(SDL_CameraDeviceID instance_id, const SDL_Camer
// Start the camera thread if necessary
if (!camera_driver.impl.ProvidesOwnCallbackThread) {
char threadname[64];
SDL_snprintf(threadname, sizeof (threadname), "SDLCamera%d", (int) instance_id);
SDL_GetCameraThreadName(device, threadname, sizeof (threadname));
device->thread = SDL_CreateThreadInternal(CameraThread, threadname, 0, device);
if (!device->thread) {
ClosePhysicalCameraDevice(device);
Expand Down
8 changes: 4 additions & 4 deletions src/camera/SDL_syscamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@
#define DEBUG_CAMERA 0


// !!! FIXME: update these drivers!
#ifdef SDL_CAMERA_DRIVER_COREMEDIA
#undef SDL_CAMERA_DRIVER_COREMEDIA
#endif
// !!! FIXME: update this driver!
#ifdef SDL_CAMERA_DRIVER_ANDROID
#undef SDL_CAMERA_DRIVER_ANDROID
#endif
Expand All @@ -53,6 +50,9 @@ extern SDL_CameraDevice *SDL_FindPhysicalCameraDeviceByCallback(SDL_bool (*callb
// Backends should call this when the user has approved/denied access to a camera.
extern void SDL_CameraDevicePermissionOutcome(SDL_CameraDevice *device, SDL_bool approved);

// Backends can call this to get a standardized name for a thread to power a specific camera device.
extern char *SDL_GetCameraThreadName(SDL_CameraDevice *device, char *buf, size_t buflen);

// These functions are the heart of the camera threads. Backends can call them directly if they aren't using the SDL-provided thread.
extern void SDL_CameraThreadSetup(SDL_CameraDevice *device);
extern SDL_bool SDL_CameraThreadIterate(SDL_CameraDevice *device);
Expand Down
Loading

0 comments on commit ad30a0d

Please sign in to comment.