SDL_AcquireCameraFrame(3)
π SDL_AcquireCameraFrame β the manual page, but make it cute
okay bestie, welcome to the SDL3 FUNCTIONS section of the manual π
this one's called SDL_AcquireCameraFrame(3) and it's genuinely one of the most useful little gremlins in the camera API. let's get into it π
β¨ NAME β¨
SDL_AcquireCameraFrame β grab yourself a frame π«Ά
πΈ SYNOPSIS πΈ
#include <SDL3/SDL_camera.h>
SDL_Surface * SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS);
π¦ DESCRIPTION π¦
so the frame you get back is a memory pointer to the image data, and its size and format? those are literally given by the spec you requested when you opened the device. no surprises, it's what you asked for π
this is a non blocking API. if a frame is available, you get a non-NULL surface back, and timestampNS gets filled in with a non-zero value. slay. β¨
now, plot twist: an error case can also return NULL. but a NULL on its own is totally normal β it just means a new frame isn't available yet. no drama, it's giving "not yet bestie" π
and here's the part that lowkey matters: even if a camera device fails outright (like a USB camera getting unplugged while in use, etc), SDL will send an event separately to notify the app, but it will keep providing blank frames at ongoing intervals until SDL_CloseCamera() is called. so a real failure here is almost always an out of memory condition π
after use, the frame should be released with SDL_ReleaseCameraFrame(). if you don't do this, the system may stop providing more video! fr fr, do not skip this step π
do not callSDL_DestroySurface()on the returned surface! it must be given back to the camera subsystem withSDL_ReleaseCameraFrame! not me warning you twice, it's just that serious π
and one more vibe check: if the system is waiting for the user to approve access to the camera β because some platforms are like that β this will return NULL (no frames available). what do you do then? either:
- wait for an
SDL_EVENT_CAMERA_DEVICE_APPROVEDevent (or anSDL_EVENT_CAMERA_DEVICE_DENIEDevent, awkward), or - poll
SDL_GetCameraPermissionState()occasionally until it returns non-zero.
periodt. π
π FUNCTION PARAMETERS π
cameraβ the opened camera device. the whole point bestie π«ΆtimestampNSβ a pointer that gets filled in with the frame's timestamp, or0on error. it can beNULL. so if you don't care about time, you can absolutely just not π
π RETURN VALUE π
returns a new frame of video on success, and NULL if none is currently available. that's it, that's the whole deal β¨
π THREAD SAFETY π
it is safe to call this function from any thread. go wild, main character energy, no locks needed π
β AVAILABILITY β
this function is available since SDL 3.2.0. if your SDL is older than that... bestie, upgrade π
π SEE ALSO π
SDL_ReleaseCameraFrame(3)β aka the one you must call after this one, iykyk
okay that's the page!! this was brought to you by Simple Directmedia Layer, SDL 3.4.18, SDL_AcquireCameraFrame(3) β go acquire some frames, release them properly, and remember: blank frames are not a crash, they're just the camera being shy πΈπ¦π
π¦ where this page comes from
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 43: SDL3-devel 3.4.18-1.fc43 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- fedora 44: SDL3-devel 3.4.18-1.fc44 Β· Zlib AND MIT AND Apache-2.0 AND (Apache-2.0 OR MIT)
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms