2023-11-09 13:11:07 +03:00
|
|
|
/*
|
2024-01-02 00:15:26 +03:00
|
|
|
Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
|
2023-11-09 13:11:07 +03:00
|
|
|
|
|
|
|
This software is provided 'as-is', without any express or implied
|
|
|
|
warranty. In no event will the authors be held liable for any damages
|
|
|
|
arising from the use of this software.
|
|
|
|
|
|
|
|
Permission is granted to anyone to use this software for any purpose,
|
|
|
|
including commercial applications, and to alter it and redistribute it
|
|
|
|
freely.
|
|
|
|
*/
|
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
#define SDL_MAIN_USE_CALLBACKS 1
|
2024-06-04 20:42:44 +03:00
|
|
|
#include <SDL3/SDL_main.h>
|
2024-02-19 20:20:11 +03:00
|
|
|
#include <SDL3/SDL_test.h>
|
|
|
|
#include <SDL3/SDL_test_common.h>
|
|
|
|
|
|
|
|
static SDL_Window *window = NULL;
|
|
|
|
static SDL_Renderer *renderer = NULL;
|
|
|
|
static SDLTest_CommonState *state = NULL;
|
|
|
|
static SDL_Camera *camera = NULL;
|
|
|
|
static SDL_CameraSpec spec;
|
|
|
|
static SDL_Texture *texture = NULL;
|
|
|
|
static SDL_bool texture_updated = SDL_FALSE;
|
|
|
|
static SDL_Surface *frame_current = NULL;
|
2024-07-14 22:22:42 +03:00
|
|
|
static SDL_CameraID front_camera = 0;
|
|
|
|
static SDL_CameraID back_camera = 0;
|
2024-02-19 20:20:11 +03:00
|
|
|
|
2024-03-28 00:22:08 +03:00
|
|
|
int SDL_AppInit(void **appstate, int argc, char *argv[])
|
2023-11-09 13:11:07 +03:00
|
|
|
{
|
2024-06-14 00:05:06 +03:00
|
|
|
char window_title[128];
|
2024-02-19 20:20:11 +03:00
|
|
|
int devcount = 0;
|
|
|
|
int i;
|
2024-06-05 01:28:24 +03:00
|
|
|
const char *camera_name = NULL;
|
2023-11-09 13:11:07 +03:00
|
|
|
|
|
|
|
/* Initialize test framework */
|
2024-02-21 02:05:32 +03:00
|
|
|
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_CAMERA);
|
|
|
|
if (!state) {
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Enable standard application logging */
|
2024-05-16 20:00:50 +03:00
|
|
|
SDL_SetLogPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-06-05 01:28:24 +03:00
|
|
|
/* Parse commandline */
|
|
|
|
for (i = 1; i < argc;) {
|
|
|
|
int consumed;
|
|
|
|
|
|
|
|
consumed = SDLTest_CommonArg(state, i);
|
|
|
|
if (!consumed) {
|
|
|
|
if (SDL_strcmp(argv[i], "--camera") == 0 && argv[i+1]) {
|
|
|
|
camera_name = argv[i+1];
|
|
|
|
consumed = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (consumed <= 0) {
|
|
|
|
static const char *options[] = {
|
|
|
|
"[--camera name]",
|
|
|
|
NULL,
|
|
|
|
};
|
|
|
|
SDLTest_CommonLogUsage(state, argv[0], options);
|
|
|
|
SDLTest_CommonDestroyState(state);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
i += consumed;
|
2024-02-21 02:05:32 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
state->num_windows = 1;
|
|
|
|
|
2023-11-09 13:11:07 +03:00
|
|
|
/* Load the SDL library */
|
2024-02-21 02:05:32 +03:00
|
|
|
if (!SDLTest_CommonInit(state)) {
|
2023-11-09 13:11:07 +03:00
|
|
|
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-21 02:05:32 +03:00
|
|
|
window = state->windows[0];
|
|
|
|
if (!window) {
|
2023-11-09 13:11:07 +03:00
|
|
|
SDL_Log("Couldn't create window: %s", SDL_GetError());
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-05-16 20:00:50 +03:00
|
|
|
SDL_SetLogPriorities(SDL_LOG_PRIORITY_VERBOSE);
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-02-21 02:05:32 +03:00
|
|
|
renderer = state->renderers[0];
|
|
|
|
if (!renderer) {
|
2023-11-09 13:11:07 +03:00
|
|
|
/* SDL_Log("Couldn't create renderer: %s", SDL_GetError()); */
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-07-14 22:22:42 +03:00
|
|
|
SDL_CameraID *devices = SDL_GetCameras(&devcount);
|
2024-02-19 20:20:11 +03:00
|
|
|
if (!devices) {
|
2024-07-14 22:22:42 +03:00
|
|
|
SDL_Log("SDL_GetCameras failed: %s", SDL_GetError());
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-07-14 22:22:42 +03:00
|
|
|
SDL_CameraID camera_id = 0;
|
2024-06-05 01:28:24 +03:00
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_Log("Saw %d camera devices.", devcount);
|
|
|
|
for (i = 0; i < devcount; i++) {
|
2024-07-14 22:22:42 +03:00
|
|
|
const SDL_CameraID device = devices[i];
|
|
|
|
const char *name = SDL_GetCameraName(device);
|
|
|
|
const SDL_CameraPosition position = SDL_GetCameraPosition(device);
|
2024-02-20 07:52:43 +03:00
|
|
|
const char *posstr = "";
|
|
|
|
if (position == SDL_CAMERA_POSITION_FRONT_FACING) {
|
|
|
|
front_camera = device;
|
|
|
|
posstr = "[front-facing] ";
|
|
|
|
} else if (position == SDL_CAMERA_POSITION_BACK_FACING) {
|
|
|
|
back_camera = device;
|
|
|
|
posstr = "[back-facing] ";
|
|
|
|
}
|
2024-06-05 01:28:24 +03:00
|
|
|
if (camera_name && SDL_strcasecmp(name, camera_name) == 0) {
|
|
|
|
camera_id = device;
|
|
|
|
}
|
2024-02-20 07:52:43 +03:00
|
|
|
SDL_Log(" - Camera #%d: %s %s", i, posstr, name);
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-06-05 01:28:24 +03:00
|
|
|
if (!camera_id) {
|
|
|
|
if (camera_name) {
|
|
|
|
SDL_Log("Could not find camera \"%s\"", camera_name);
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
}
|
|
|
|
if (front_camera) {
|
|
|
|
camera_id = front_camera;
|
|
|
|
} else if (devcount > 0) {
|
|
|
|
camera_id = devices[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_free(devices);
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-06-05 01:28:24 +03:00
|
|
|
if (!camera_id) {
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_Log("No cameras available?");
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
2024-02-21 02:05:32 +03:00
|
|
|
|
2024-05-02 22:27:20 +03:00
|
|
|
SDL_CameraSpec *pspec = &spec;
|
2024-06-17 04:12:38 +03:00
|
|
|
spec.framerate_numerator = 1000;
|
|
|
|
spec.framerate_denominator = 1;
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-07-14 22:22:42 +03:00
|
|
|
camera = SDL_OpenCamera(camera_id, pspec);
|
2024-02-19 20:20:11 +03:00
|
|
|
if (!camera) {
|
|
|
|
SDL_Log("Failed to open camera device: %s", SDL_GetError());
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-07-14 22:22:42 +03:00
|
|
|
SDL_snprintf(window_title, sizeof (window_title), "testcamera: %s (%s)", SDL_GetCameraName(camera_id), SDL_GetCurrentCameraDriver());
|
2024-06-14 00:05:06 +03:00
|
|
|
SDL_SetWindowTitle(window, window_title);
|
|
|
|
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_CONTINUE;
|
2024-02-19 20:20:11 +03:00
|
|
|
}
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-02-20 07:52:43 +03:00
|
|
|
|
|
|
|
static int FlipCamera(void)
|
|
|
|
{
|
|
|
|
static Uint64 last_flip = 0;
|
|
|
|
if ((SDL_GetTicks() - last_flip) < 3000) { /* must wait at least 3 seconds between flips. */
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_CONTINUE;
|
2024-02-20 07:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (camera) {
|
2024-07-14 22:22:42 +03:00
|
|
|
const SDL_CameraID current = SDL_GetCameraID(camera);
|
|
|
|
SDL_CameraID nextcam = 0;
|
2024-02-20 07:52:43 +03:00
|
|
|
if (current == front_camera) {
|
|
|
|
nextcam = back_camera;
|
|
|
|
} else if (current == back_camera) {
|
|
|
|
nextcam = front_camera;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (nextcam) {
|
|
|
|
SDL_Log("Flip camera!");
|
|
|
|
|
|
|
|
if (frame_current) {
|
|
|
|
SDL_ReleaseCameraFrame(camera, frame_current);
|
|
|
|
frame_current = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_CloseCamera(camera);
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
SDL_DestroyTexture(texture);
|
|
|
|
texture = NULL; /* will rebuild when new camera is approved. */
|
|
|
|
}
|
|
|
|
|
2024-07-14 22:22:42 +03:00
|
|
|
camera = SDL_OpenCamera(nextcam, NULL);
|
2024-02-20 07:52:43 +03:00
|
|
|
if (!camera) {
|
|
|
|
SDL_Log("Failed to open camera device: %s", SDL_GetError());
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2024-02-20 07:52:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
last_flip = SDL_GetTicks();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_CONTINUE;
|
2024-02-20 07:52:43 +03:00
|
|
|
}
|
|
|
|
|
2024-03-28 00:22:08 +03:00
|
|
|
int SDL_AppEvent(void *appstate, const SDL_Event *event)
|
2024-02-19 20:20:11 +03:00
|
|
|
{
|
|
|
|
switch (event->type) {
|
|
|
|
case SDL_EVENT_KEY_DOWN: {
|
2024-06-22 05:50:10 +03:00
|
|
|
const SDL_Keycode sym = event->key.key;
|
2023-11-09 13:11:07 +03:00
|
|
|
if (sym == SDLK_ESCAPE || sym == SDLK_AC_BACK) {
|
|
|
|
SDL_Log("Key : Escape!");
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_SUCCESS;
|
2024-02-20 07:52:43 +03:00
|
|
|
} else if (sym == SDLK_SPACE) {
|
|
|
|
FlipCamera();
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_CONTINUE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
2024-02-19 20:20:11 +03:00
|
|
|
break;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 07:52:43 +03:00
|
|
|
case SDL_EVENT_MOUSE_BUTTON_DOWN:
|
|
|
|
/* !!! FIXME: only flip if clicked in the area of a "flip" icon. */
|
|
|
|
return FlipCamera();
|
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
case SDL_EVENT_QUIT:
|
2024-05-13 17:16:52 +03:00
|
|
|
SDL_Log("Quit!");
|
2024-02-19 20:20:11 +03:00
|
|
|
return 1;
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
case SDL_EVENT_CAMERA_DEVICE_APPROVED:
|
2024-02-20 07:52:43 +03:00
|
|
|
SDL_Log("Camera approved!");
|
2024-02-19 20:20:11 +03:00
|
|
|
break;
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
case SDL_EVENT_CAMERA_DEVICE_DENIED:
|
2024-02-20 07:52:43 +03:00
|
|
|
SDL_Log("Camera denied!");
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Camera permission denied!", "User denied access to the camera!", window);
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_FAILURE;
|
2024-03-06 22:37:39 +03:00
|
|
|
default:
|
|
|
|
break;
|
2024-02-19 20:20:11 +03:00
|
|
|
}
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
return SDLTest_CommonEventMainCallbacks(state, event);
|
|
|
|
}
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-03-28 00:22:08 +03:00
|
|
|
int SDL_AppIterate(void *appstate)
|
2024-02-19 20:20:11 +03:00
|
|
|
{
|
|
|
|
SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, 255);
|
|
|
|
SDL_RenderClear(renderer);
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-06-13 05:08:06 +03:00
|
|
|
int win_w, win_h;
|
|
|
|
float tw, th;
|
2024-06-04 21:34:39 +03:00
|
|
|
SDL_FRect d;
|
|
|
|
Uint64 timestampNS = 0;
|
|
|
|
SDL_Surface *frame_next = camera ? SDL_AcquireCameraFrame(camera, ×tampNS) : NULL;
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-06-04 21:34:39 +03:00
|
|
|
#if 0
|
|
|
|
if (frame_next) {
|
|
|
|
SDL_Log("frame: %p at %" SDL_PRIu64, (void*)frame_next->pixels, timestampNS);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
if (frame_next) {
|
|
|
|
if (frame_current) {
|
|
|
|
if (SDL_ReleaseCameraFrame(camera, frame_current) < 0) {
|
|
|
|
SDL_Log("err SDL_ReleaseCameraFrame: %s", SDL_GetError());
|
|
|
|
}
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-06-04 21:34:39 +03:00
|
|
|
/* It's not needed to keep the frame once updated the texture is updated.
|
|
|
|
* But in case of 0-copy, it's needed to have the frame while using the texture.
|
|
|
|
*/
|
|
|
|
frame_current = frame_next;
|
|
|
|
texture_updated = SDL_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (frame_current) {
|
|
|
|
if (!texture ||
|
2024-06-12 19:21:02 +03:00
|
|
|
SDL_GetTextureSize(texture, &tw, &th) < 0 ||
|
|
|
|
(int)tw != frame_current->w || (int)th != frame_current->h) {
|
2024-06-04 21:34:39 +03:00
|
|
|
/* Resize the window to match */
|
|
|
|
SDL_SetWindowSize(window, frame_current->w, frame_current->h);
|
|
|
|
|
|
|
|
if (texture) {
|
|
|
|
SDL_DestroyTexture(texture);
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-07-09 00:59:18 +03:00
|
|
|
SDL_Colorspace colorspace = SDL_GetSurfaceColorspace(frame_current);
|
2024-06-12 21:18:26 +03:00
|
|
|
|
2024-06-04 21:34:39 +03:00
|
|
|
/* Create texture with appropriate format */
|
2024-06-12 21:18:26 +03:00
|
|
|
SDL_PropertiesID props = SDL_CreateProperties();
|
2024-07-09 00:59:18 +03:00
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, frame_current->format);
|
2024-06-12 21:18:26 +03:00
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, colorspace);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, SDL_TEXTUREACCESS_STREAMING);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, frame_current->w);
|
|
|
|
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, frame_current->h);
|
|
|
|
texture = SDL_CreateTextureWithProperties(renderer, props);
|
|
|
|
SDL_DestroyProperties(props);
|
2024-06-04 21:34:39 +03:00
|
|
|
if (!texture) {
|
|
|
|
SDL_Log("Couldn't create texture: %s", SDL_GetError());
|
|
|
|
return SDL_APP_FAILURE;
|
|
|
|
}
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
/* Update SDL_Texture with last video frame (only once per new frame) */
|
|
|
|
if (frame_current && !texture_updated) {
|
|
|
|
SDL_UpdateTexture(texture, NULL, frame_current->pixels, frame_current->pitch);
|
|
|
|
texture_updated = SDL_TRUE;
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-06-12 19:21:02 +03:00
|
|
|
SDL_GetTextureSize(texture, &tw, &th);
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_GetRenderOutputSize(renderer, &win_w, &win_h);
|
2024-06-12 19:21:02 +03:00
|
|
|
d.x = ((win_w - tw) / 2);
|
|
|
|
d.y = ((win_h - th) / 2);
|
|
|
|
d.w = tw;
|
|
|
|
d.h = th;
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_RenderTexture(renderer, texture, NULL, &d);
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|
|
|
|
|
2024-02-20 07:52:43 +03:00
|
|
|
/* !!! FIXME: Render a "flip" icon if front_camera and back_camera are both != 0. */
|
|
|
|
|
2024-02-19 20:20:11 +03:00
|
|
|
SDL_RenderPresent(renderer);
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-05-27 23:03:45 +03:00
|
|
|
return SDL_APP_CONTINUE;
|
2024-02-19 20:20:11 +03:00
|
|
|
}
|
2023-11-09 13:11:07 +03:00
|
|
|
|
2024-03-28 00:22:08 +03:00
|
|
|
void SDL_AppQuit(void *appstate)
|
2024-02-19 20:20:11 +03:00
|
|
|
{
|
|
|
|
SDL_ReleaseCameraFrame(camera, frame_current);
|
|
|
|
SDL_CloseCamera(camera);
|
|
|
|
SDL_DestroyTexture(texture);
|
2024-02-21 02:05:32 +03:00
|
|
|
SDLTest_CommonQuit(state);
|
2023-11-09 13:11:07 +03:00
|
|
|
}
|