Path: blob/master/thirdparty/sdl/include/SDL3/SDL_camera.h
9912 views
/*1Simple DirectMedia Layer2Copyright (C) 1997-2025 Sam Lantinga <[email protected]>34This software is provided 'as-is', without any express or implied5warranty. In no event will the authors be held liable for any damages6arising from the use of this software.78Permission is granted to anyone to use this software for any purpose,9including commercial applications, and to alter it and redistribute it10freely, subject to the following restrictions:11121. The origin of this software must not be misrepresented; you must not13claim that you wrote the original software. If you use this software14in a product, an acknowledgment in the product documentation would be15appreciated but is not required.162. Altered source versions must be plainly marked as such, and must not be17misrepresented as being the original software.183. This notice may not be removed or altered from any source distribution.19*/2021/**22* # CategoryCamera23*24* Video capture for the SDL library.25*26* This API lets apps read input from video sources, like webcams. Camera27* devices can be enumerated, queried, and opened. Once opened, it will28* provide SDL_Surface objects as new frames of video come in. These surfaces29* can be uploaded to an SDL_Texture or processed as pixels in memory.30*31* Several platforms will alert the user if an app tries to access a camera,32* and some will present a UI asking the user if your application should be33* allowed to obtain images at all, which they can deny. A successfully opened34* camera will not provide images until permission is granted. Applications,35* after opening a camera device, can see if they were granted access by36* either polling with the SDL_GetCameraPermissionState() function, or waiting37* for an SDL_EVENT_CAMERA_DEVICE_APPROVED or SDL_EVENT_CAMERA_DEVICE_DENIED38* event. Platforms that don't have any user approval process will report39* approval immediately.40*41* Note that SDL cameras only provide video as individual frames; they will42* not provide full-motion video encoded in a movie file format, although an43* app is free to encode the acquired frames into any format it likes. It also44* does not provide audio from the camera hardware through this API; not only45* do many webcams not have microphones at all, many people--from streamers to46* people on Zoom calls--will want to use a separate microphone regardless of47* the camera. In any case, recorded audio will be available through SDL's48* audio API no matter what hardware provides the microphone.49*50* ## Camera gotchas51*52* Consumer-level camera hardware tends to take a little while to warm up,53* once the device has been opened. Generally most camera apps have some sort54* of UI to take a picture (a button to snap a pic while a preview is showing,55* some sort of multi-second countdown for the user to pose, like a photo56* booth), which puts control in the users' hands, or they are intended to57* stay on for long times (Pokemon Go, etc).58*59* It's not uncommon that a newly-opened camera will provide a couple of60* completely black frames, maybe followed by some under-exposed images. If61* taking a single frame automatically, or recording video from a camera's62* input without the user initiating it from a preview, it could be wise to63* drop the first several frames (if not the first several _seconds_ worth of64* frames!) before using images from a camera.65*/6667#ifndef SDL_camera_h_68#define SDL_camera_h_6970#include <SDL3/SDL_stdinc.h>71#include <SDL3/SDL_error.h>72#include <SDL3/SDL_pixels.h>73#include <SDL3/SDL_properties.h>74#include <SDL3/SDL_surface.h>7576#include <SDL3/SDL_begin_code.h>77/* Set up for C function definitions, even when using C++ */78#ifdef __cplusplus79extern "C" {80#endif8182/**83* This is a unique ID for a camera device for the time it is connected to the84* system, and is never reused for the lifetime of the application.85*86* If the device is disconnected and reconnected, it will get a new ID.87*88* The value 0 is an invalid ID.89*90* \since This datatype is available since SDL 3.2.0.91*92* \sa SDL_GetCameras93*/94typedef Uint32 SDL_CameraID;9596/**97* The opaque structure used to identify an opened SDL camera.98*99* \since This struct is available since SDL 3.2.0.100*/101typedef struct SDL_Camera SDL_Camera;102103/**104* The details of an output format for a camera device.105*106* Cameras often support multiple formats; each one will be encapsulated in107* this struct.108*109* \since This struct is available since SDL 3.2.0.110*111* \sa SDL_GetCameraSupportedFormats112* \sa SDL_GetCameraFormat113*/114typedef struct SDL_CameraSpec115{116SDL_PixelFormat format; /**< Frame format */117SDL_Colorspace colorspace; /**< Frame colorspace */118int width; /**< Frame width */119int height; /**< Frame height */120int framerate_numerator; /**< Frame rate numerator ((num / denom) == FPS, (denom / num) == duration in seconds) */121int framerate_denominator; /**< Frame rate demoninator ((num / denom) == FPS, (denom / num) == duration in seconds) */122} SDL_CameraSpec;123124/**125* The position of camera in relation to system device.126*127* \since This enum is available since SDL 3.2.0.128*129* \sa SDL_GetCameraPosition130*/131typedef enum SDL_CameraPosition132{133SDL_CAMERA_POSITION_UNKNOWN,134SDL_CAMERA_POSITION_FRONT_FACING,135SDL_CAMERA_POSITION_BACK_FACING136} SDL_CameraPosition;137138139/**140* Use this function to get the number of built-in camera drivers.141*142* This function returns a hardcoded number. This never returns a negative143* value; if there are no drivers compiled into this build of SDL, this144* function returns zero. The presence of a driver in this list does not mean145* it will function, it just means SDL is capable of interacting with that146* interface. For example, a build of SDL might have v4l2 support, but if147* there's no kernel support available, SDL's v4l2 driver would fail if used.148*149* By default, SDL tries all drivers, in its preferred order, until one is150* found to be usable.151*152* \returns the number of built-in camera drivers.153*154* \threadsafety It is safe to call this function from any thread.155*156* \since This function is available since SDL 3.2.0.157*158* \sa SDL_GetCameraDriver159*/160extern SDL_DECLSPEC int SDLCALL SDL_GetNumCameraDrivers(void);161162/**163* Use this function to get the name of a built in camera driver.164*165* The list of camera drivers is given in the order that they are normally166* initialized by default; the drivers that seem more reasonable to choose167* first (as far as the SDL developers believe) are earlier in the list.168*169* The names of drivers are all simple, low-ASCII identifiers, like "v4l2",170* "coremedia" or "android". These never have Unicode characters, and are not171* meant to be proper names.172*173* \param index the index of the camera driver; the value ranges from 0 to174* SDL_GetNumCameraDrivers() - 1.175* \returns the name of the camera driver at the requested index, or NULL if176* an invalid index was specified.177*178* \threadsafety It is safe to call this function from any thread.179*180* \since This function is available since SDL 3.2.0.181*182* \sa SDL_GetNumCameraDrivers183*/184extern SDL_DECLSPEC const char * SDLCALL SDL_GetCameraDriver(int index);185186/**187* Get the name of the current camera driver.188*189* The names of drivers are all simple, low-ASCII identifiers, like "v4l2",190* "coremedia" or "android". These never have Unicode characters, and are not191* meant to be proper names.192*193* \returns the name of the current camera driver or NULL if no driver has194* been initialized.195*196* \threadsafety It is safe to call this function from any thread.197*198* \since This function is available since SDL 3.2.0.199*/200extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentCameraDriver(void);201202/**203* Get a list of currently connected camera devices.204*205* \param count a pointer filled in with the number of cameras returned, may206* be NULL.207* \returns a 0 terminated array of camera instance IDs or NULL on failure;208* call SDL_GetError() for more information. This should be freed209* with SDL_free() when it is no longer needed.210*211* \threadsafety It is safe to call this function from any thread.212*213* \since This function is available since SDL 3.2.0.214*215* \sa SDL_OpenCamera216*/217extern SDL_DECLSPEC SDL_CameraID * SDLCALL SDL_GetCameras(int *count);218219/**220* Get the list of native formats/sizes a camera supports.221*222* This returns a list of all formats and frame sizes that a specific camera223* can offer. This is useful if your app can accept a variety of image formats224* and sizes and so want to find the optimal spec that doesn't require225* conversion.226*227* This function isn't strictly required; if you call SDL_OpenCamera with a228* NULL spec, SDL will choose a native format for you, and if you instead229* specify a desired format, it will transparently convert to the requested230* format on your behalf.231*232* If `count` is not NULL, it will be filled with the number of elements in233* the returned array.234*235* Note that it's legal for a camera to supply an empty list. This is what236* will happen on Emscripten builds, since that platform won't tell _anything_237* about available cameras until you've opened one, and won't even tell if238* there _is_ a camera until the user has given you permission to check239* through a scary warning popup.240*241* \param instance_id the camera device instance ID.242* \param count a pointer filled in with the number of elements in the list,243* may be NULL.244* \returns a NULL terminated array of pointers to SDL_CameraSpec or NULL on245* failure; call SDL_GetError() for more information. This is a246* single allocation that should be freed with SDL_free() when it is247* no longer needed.248*249* \threadsafety It is safe to call this function from any thread.250*251* \since This function is available since SDL 3.2.0.252*253* \sa SDL_GetCameras254* \sa SDL_OpenCamera255*/256extern SDL_DECLSPEC SDL_CameraSpec ** SDLCALL SDL_GetCameraSupportedFormats(SDL_CameraID instance_id, int *count);257258/**259* Get the human-readable device name for a camera.260*261* \param instance_id the camera device instance ID.262* \returns a human-readable device name or NULL on failure; call263* SDL_GetError() for more information.264*265* \threadsafety It is safe to call this function from any thread.266*267* \since This function is available since SDL 3.2.0.268*269* \sa SDL_GetCameras270*/271extern SDL_DECLSPEC const char * SDLCALL SDL_GetCameraName(SDL_CameraID instance_id);272273/**274* Get the position of the camera in relation to the system.275*276* Most platforms will report UNKNOWN, but mobile devices, like phones, can277* often make a distinction between cameras on the front of the device (that278* points towards the user, for taking "selfies") and cameras on the back (for279* filming in the direction the user is facing).280*281* \param instance_id the camera device instance ID.282* \returns the position of the camera on the system hardware.283*284* \threadsafety It is safe to call this function from any thread.285*286* \since This function is available since SDL 3.2.0.287*288* \sa SDL_GetCameras289*/290extern SDL_DECLSPEC SDL_CameraPosition SDLCALL SDL_GetCameraPosition(SDL_CameraID instance_id);291292/**293* Open a video recording device (a "camera").294*295* You can open the device with any reasonable spec, and if the hardware can't296* directly support it, it will convert data seamlessly to the requested297* format. This might incur overhead, including scaling of image data.298*299* If you would rather accept whatever format the device offers, you can pass300* a NULL spec here and it will choose one for you (and you can use301* SDL_Surface's conversion/scaling functions directly if necessary).302*303* You can call SDL_GetCameraFormat() to get the actual data format if passing304* a NULL spec here. You can see the exact specs a device can support without305* conversion with SDL_GetCameraSupportedFormats().306*307* SDL will not attempt to emulate framerate; it will try to set the hardware308* to the rate closest to the requested speed, but it won't attempt to limit309* or duplicate frames artificially; call SDL_GetCameraFormat() to see the310* actual framerate of the opened the device, and check your timestamps if311* this is crucial to your app!312*313* Note that the camera is not usable until the user approves its use! On some314* platforms, the operating system will prompt the user to permit access to315* the camera, and they can choose Yes or No at that point. Until they do, the316* camera will not be usable. The app should either wait for an317* SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event,318* or poll SDL_GetCameraPermissionState() occasionally until it returns319* non-zero. On platforms that don't require explicit user approval (and320* perhaps in places where the user previously permitted access), the approval321* event might come immediately, but it might come seconds, minutes, or hours322* later!323*324* \param instance_id the camera device instance ID.325* \param spec the desired format for data the device will provide. Can be326* NULL.327* \returns an SDL_Camera object or NULL on failure; call SDL_GetError() for328* more information.329*330* \threadsafety It is safe to call this function from any thread.331*332* \since This function is available since SDL 3.2.0.333*334* \sa SDL_GetCameras335* \sa SDL_GetCameraFormat336*/337extern SDL_DECLSPEC SDL_Camera * SDLCALL SDL_OpenCamera(SDL_CameraID instance_id, const SDL_CameraSpec *spec);338339/**340* Query if camera access has been approved by the user.341*342* Cameras will not function between when the device is opened by the app and343* when the user permits access to the hardware. On some platforms, this344* presents as a popup dialog where the user has to explicitly approve access;345* on others the approval might be implicit and not alert the user at all.346*347* This function can be used to check the status of that approval. It will348* return 0 if still waiting for user response, 1 if the camera is approved349* for use, and -1 if the user denied access.350*351* Instead of polling with this function, you can wait for a352* SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event353* in the standard SDL event loop, which is guaranteed to be sent once when354* permission to use the camera is decided.355*356* If a camera is declined, there's nothing to be done but call357* SDL_CloseCamera() to dispose of it.358*359* \param camera the opened camera device to query.360* \returns -1 if user denied access to the camera, 1 if user approved access,361* 0 if no decision has been made yet.362*363* \threadsafety It is safe to call this function from any thread.364*365* \since This function is available since SDL 3.2.0.366*367* \sa SDL_OpenCamera368* \sa SDL_CloseCamera369*/370extern SDL_DECLSPEC int SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);371372/**373* Get the instance ID of an opened camera.374*375* \param camera an SDL_Camera to query.376* \returns the instance ID of the specified camera on success or 0 on377* failure; call SDL_GetError() for more information.378*379* \threadsafety It is safe to call this function from any thread.380*381* \since This function is available since SDL 3.2.0.382*383* \sa SDL_OpenCamera384*/385extern SDL_DECLSPEC SDL_CameraID SDLCALL SDL_GetCameraID(SDL_Camera *camera);386387/**388* Get the properties associated with an opened camera.389*390* \param camera the SDL_Camera obtained from SDL_OpenCamera().391* \returns a valid property ID on success or 0 on failure; call392* SDL_GetError() for more information.393*394* \threadsafety It is safe to call this function from any thread.395*396* \since This function is available since SDL 3.2.0.397*/398extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetCameraProperties(SDL_Camera *camera);399400/**401* Get the spec that a camera is using when generating images.402*403* Note that this might not be the native format of the hardware, as SDL might404* be converting to this format behind the scenes.405*406* If the system is waiting for the user to approve access to the camera, as407* some platforms require, this will return false, but this isn't necessarily408* a fatal error; you should either wait for an409* SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event,410* or poll SDL_GetCameraPermissionState() occasionally until it returns411* non-zero.412*413* \param camera opened camera device.414* \param spec the SDL_CameraSpec to be initialized by this function.415* \returns true on success or false on failure; call SDL_GetError() for more416* information.417*418* \threadsafety It is safe to call this function from any thread.419*420* \since This function is available since SDL 3.2.0.421*422* \sa SDL_OpenCamera423*/424extern SDL_DECLSPEC bool SDLCALL SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec);425426/**427* Acquire a frame.428*429* The frame is a memory pointer to the image data, whose size and format are430* given by the spec requested when opening the device.431*432* This is a non blocking API. If there is a frame available, a non-NULL433* surface is returned, and timestampNS will be filled with a non-zero value.434*435* Note that an error case can also return NULL, but a NULL by itself is436* normal and just signifies that a new frame is not yet available. Note that437* even if a camera device fails outright (a USB camera is unplugged while in438* use, etc), SDL will send an event separately to notify the app, but439* continue to provide blank frames at ongoing intervals until440* SDL_CloseCamera() is called, so real failure here is almost always an out441* of memory condition.442*443* After use, the frame should be released with SDL_ReleaseCameraFrame(). If444* you don't do this, the system may stop providing more video!445*446* Do not call SDL_DestroySurface() on the returned surface! It must be given447* back to the camera subsystem with SDL_ReleaseCameraFrame!448*449* If the system is waiting for the user to approve access to the camera, as450* some platforms require, this will return NULL (no frames available); you451* should either wait for an SDL_EVENT_CAMERA_DEVICE_APPROVED (or452* SDL_EVENT_CAMERA_DEVICE_DENIED) event, or poll453* SDL_GetCameraPermissionState() occasionally until it returns non-zero.454*455* \param camera opened camera device.456* \param timestampNS a pointer filled in with the frame's timestamp, or 0 on457* error. Can be NULL.458* \returns a new frame of video on success, NULL if none is currently459* available.460*461* \threadsafety It is safe to call this function from any thread.462*463* \since This function is available since SDL 3.2.0.464*465* \sa SDL_ReleaseCameraFrame466*/467extern SDL_DECLSPEC SDL_Surface * SDLCALL SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS);468469/**470* Release a frame of video acquired from a camera.471*472* Let the back-end re-use the internal buffer for camera.473*474* This function _must_ be called only on surface objects returned by475* SDL_AcquireCameraFrame(). This function should be called as quickly as476* possible after acquisition, as SDL keeps a small FIFO queue of surfaces for477* video frames; if surfaces aren't released in a timely manner, SDL may drop478* upcoming video frames from the camera.479*480* If the app needs to keep the surface for a significant time, they should481* make a copy of it and release the original.482*483* The app should not use the surface again after calling this function;484* assume the surface is freed and the pointer is invalid.485*486* \param camera opened camera device.487* \param frame the video frame surface to release.488*489* \threadsafety It is safe to call this function from any thread.490*491* \since This function is available since SDL 3.2.0.492*493* \sa SDL_AcquireCameraFrame494*/495extern SDL_DECLSPEC void SDLCALL SDL_ReleaseCameraFrame(SDL_Camera *camera, SDL_Surface *frame);496497/**498* Use this function to shut down camera processing and close the camera499* device.500*501* \param camera opened camera device.502*503* \threadsafety It is safe to call this function from any thread, but no504* thread may reference `device` once this function is called.505*506* \since This function is available since SDL 3.2.0.507*508* \sa SDL_OpenCamera509*/510extern SDL_DECLSPEC void SDLCALL SDL_CloseCamera(SDL_Camera *camera);511512/* Ends C function definitions when using C++ */513#ifdef __cplusplus514}515#endif516#include <SDL3/SDL_close_code.h>517518#endif /* SDL_camera_h_ */519520521