SDL 3.0
SDL_camera.h
Go to the documentation of this file.
1/*
2 Simple DirectMedia Layer
3 Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
4
5 This software is provided 'as-is', without any express or implied
6 warranty. In no event will the authors be held liable for any damages
7 arising from the use of this software.
8
9 Permission is granted to anyone to use this software for any purpose,
10 including commercial applications, and to alter it and redistribute it
11 freely, subject to the following restrictions:
12
13 1. The origin of this software must not be misrepresented; you must not
14 claim that you wrote the original software. If you use this software
15 in a product, an acknowledgment in the product documentation would be
16 appreciated but is not required.
17 2. Altered source versions must be plainly marked as such, and must not be
18 misrepresented as being the original software.
19 3. This notice may not be removed or altered from any source distribution.
20*/
21
22/**
23 * # CategoryCamera
24 *
25 * Video capture for the SDL library.
26 *
27 * This API lets apps read input from video sources, like webcams. Camera
28 * devices can be enumerated, queried, and opened. Once opened, it will
29 * provide SDL_Surface objects as new frames of video come in. These surfaces
30 * can be uploaded to an SDL_Texture or processed as pixels in memory.
31 */
32
33#ifndef SDL_camera_h_
34#define SDL_camera_h_
35
36#include <SDL3/SDL_stdinc.h>
37#include <SDL3/SDL_error.h>
38#include <SDL3/SDL_pixels.h>
39#include <SDL3/SDL_properties.h>
40#include <SDL3/SDL_surface.h>
41
42#include <SDL3/SDL_begin_code.h>
43/* Set up for C function definitions, even when using C++ */
44#ifdef __cplusplus
45extern "C" {
46#endif
47
48/**
49 * This is a unique ID for a camera device for the time it is connected to the
50 * system, and is never reused for the lifetime of the application.
51 *
52 * If the device is disconnected and reconnected, it will get a new ID.
53 *
54 * The value 0 is an invalid ID.
55 *
56 * \since This datatype is available since SDL 3.1.3.
57 *
58 * \sa SDL_GetCameras
59 */
61
62/**
63 * The opaque structure used to identify an opened SDL camera.
64 *
65 * \since This struct is available since SDL 3.1.3.
66 */
67typedef struct SDL_Camera SDL_Camera;
68
69/**
70 * The details of an output format for a camera device.
71 *
72 * Cameras often support multiple formats; each one will be encapsulated in
73 * this struct.
74 *
75 * \since This struct is available since SDL 3.1.3.
76 *
77 * \sa SDL_GetCameraSupportedFormats
78 * \sa SDL_GetCameraFormat
79 */
80typedef struct SDL_CameraSpec
81{
82 SDL_PixelFormat format; /**< Frame format */
83 SDL_Colorspace colorspace; /**< Frame colorspace */
84 int width; /**< Frame width */
85 int height; /**< Frame height */
86 int framerate_numerator; /**< Frame rate numerator ((num / denom) == FPS, (denom / num) == duration in seconds) */
87 int framerate_denominator; /**< Frame rate demoninator ((num / denom) == FPS, (denom / num) == duration in seconds) */
89
90/**
91 * The position of camera in relation to system device.
92 *
93 * \since This enum is available since SDL 3.1.3.
94 *
95 * \sa SDL_GetCameraPosition
96 */
98{
103
104
105/**
106 * Use this function to get the number of built-in camera drivers.
107 *
108 * This function returns a hardcoded number. This never returns a negative
109 * value; if there are no drivers compiled into this build of SDL, this
110 * function returns zero. The presence of a driver in this list does not mean
111 * it will function, it just means SDL is capable of interacting with that
112 * interface. For example, a build of SDL might have v4l2 support, but if
113 * there's no kernel support available, SDL's v4l2 driver would fail if used.
114 *
115 * By default, SDL tries all drivers, in its preferred order, until one is
116 * found to be usable.
117 *
118 * \returns the number of built-in camera drivers.
119 *
120 * \threadsafety It is safe to call this function from any thread.
121 *
122 * \since This function is available since SDL 3.1.3.
123 *
124 * \sa SDL_GetCameraDriver
125 */
126extern SDL_DECLSPEC int SDLCALL SDL_GetNumCameraDrivers(void);
127
128/**
129 * Use this function to get the name of a built in camera driver.
130 *
131 * The list of camera drivers is given in the order that they are normally
132 * initialized by default; the drivers that seem more reasonable to choose
133 * first (as far as the SDL developers believe) are earlier in the list.
134 *
135 * The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
136 * "coremedia" or "android". These never have Unicode characters, and are not
137 * meant to be proper names.
138 *
139 * \param index the index of the camera driver; the value ranges from 0 to
140 * SDL_GetNumCameraDrivers() - 1.
141 * \returns the name of the camera driver at the requested index, or NULL if
142 * an invalid index was specified.
143 *
144 * \threadsafety It is safe to call this function from any thread.
145 *
146 * \since This function is available since SDL 3.1.3.
147 *
148 * \sa SDL_GetNumCameraDrivers
149 */
150extern SDL_DECLSPEC const char * SDLCALL SDL_GetCameraDriver(int index);
151
152/**
153 * Get the name of the current camera driver.
154 *
155 * The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
156 * "coremedia" or "android". These never have Unicode characters, and are not
157 * meant to be proper names.
158 *
159 * \returns the name of the current camera driver or NULL if no driver has
160 * been initialized.
161 *
162 * \threadsafety It is safe to call this function from any thread.
163 *
164 * \since This function is available since SDL 3.1.3.
165 */
166extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentCameraDriver(void);
167
168/**
169 * Get a list of currently connected camera devices.
170 *
171 * \param count a pointer filled in with the number of cameras returned, may
172 * be NULL.
173 * \returns a 0 terminated array of camera instance IDs or NULL on failure;
174 * call SDL_GetError() for more information. This should be freed
175 * with SDL_free() when it is no longer needed.
176 *
177 * \threadsafety It is safe to call this function from any thread.
178 *
179 * \since This function is available since SDL 3.1.3.
180 *
181 * \sa SDL_OpenCamera
182 */
183extern SDL_DECLSPEC SDL_CameraID * SDLCALL SDL_GetCameras(int *count);
184
185/**
186 * Get the list of native formats/sizes a camera supports.
187 *
188 * This returns a list of all formats and frame sizes that a specific camera
189 * can offer. This is useful if your app can accept a variety of image formats
190 * and sizes and so want to find the optimal spec that doesn't require
191 * conversion.
192 *
193 * This function isn't strictly required; if you call SDL_OpenCamera with a
194 * NULL spec, SDL will choose a native format for you, and if you instead
195 * specify a desired format, it will transparently convert to the requested
196 * format on your behalf.
197 *
198 * If `count` is not NULL, it will be filled with the number of elements in
199 * the returned array.
200 *
201 * Note that it's legal for a camera to supply an empty list. This is what
202 * will happen on Emscripten builds, since that platform won't tell _anything_
203 * about available cameras until you've opened one, and won't even tell if
204 * there _is_ a camera until the user has given you permission to check
205 * through a scary warning popup.
206 *
207 * \param devid the camera device instance ID to query.
208 * \param count a pointer filled in with the number of elements in the list,
209 * may be NULL.
210 * \returns a NULL terminated array of pointers to SDL_CameraSpec or NULL on
211 * failure; call SDL_GetError() for more information. This is a
212 * single allocation that should be freed with SDL_free() when it is
213 * no longer needed.
214 *
215 * \threadsafety It is safe to call this function from any thread.
216 *
217 * \since This function is available since SDL 3.1.3.
218 *
219 * \sa SDL_GetCameras
220 * \sa SDL_OpenCamera
221 */
222extern SDL_DECLSPEC SDL_CameraSpec ** SDLCALL SDL_GetCameraSupportedFormats(SDL_CameraID devid, int *count);
223
224/**
225 * Get the human-readable device name for a camera.
226 *
227 * \param instance_id the camera device instance ID.
228 * \returns a human-readable device name or NULL on failure; call
229 * SDL_GetError() for more information.
230 *
231 * \threadsafety It is safe to call this function from any thread.
232 *
233 * \since This function is available since SDL 3.1.3.
234 *
235 * \sa SDL_GetCameras
236 */
237extern SDL_DECLSPEC const char * SDLCALL SDL_GetCameraName(SDL_CameraID instance_id);
238
239/**
240 * Get the position of the camera in relation to the system.
241 *
242 * Most platforms will report UNKNOWN, but mobile devices, like phones, can
243 * often make a distinction between cameras on the front of the device (that
244 * points towards the user, for taking "selfies") and cameras on the back (for
245 * filming in the direction the user is facing).
246 *
247 * \param instance_id the camera device instance ID.
248 * \returns the position of the camera on the system hardware.
249 *
250 * \threadsafety It is safe to call this function from any thread.
251 *
252 * \since This function is available since SDL 3.1.3.
253 *
254 * \sa SDL_GetCameras
255 */
256extern SDL_DECLSPEC SDL_CameraPosition SDLCALL SDL_GetCameraPosition(SDL_CameraID instance_id);
257
258/**
259 * Open a video recording device (a "camera").
260 *
261 * You can open the device with any reasonable spec, and if the hardware can't
262 * directly support it, it will convert data seamlessly to the requested
263 * format. This might incur overhead, including scaling of image data.
264 *
265 * If you would rather accept whatever format the device offers, you can pass
266 * a NULL spec here and it will choose one for you (and you can use
267 * SDL_Surface's conversion/scaling functions directly if necessary).
268 *
269 * You can call SDL_GetCameraFormat() to get the actual data format if passing
270 * a NULL spec here. You can see the exact specs a device can support without
271 * conversion with SDL_GetCameraSupportedFormats().
272 *
273 * SDL will not attempt to emulate framerate; it will try to set the hardware
274 * to the rate closest to the requested speed, but it won't attempt to limit
275 * or duplicate frames artificially; call SDL_GetCameraFormat() to see the
276 * actual framerate of the opened the device, and check your timestamps if
277 * this is crucial to your app!
278 *
279 * Note that the camera is not usable until the user approves its use! On some
280 * platforms, the operating system will prompt the user to permit access to
281 * the camera, and they can choose Yes or No at that point. Until they do, the
282 * camera will not be usable. The app should either wait for an
283 * SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event,
284 * or poll SDL_GetCameraPermissionState() occasionally until it returns
285 * non-zero. On platforms that don't require explicit user approval (and
286 * perhaps in places where the user previously permitted access), the approval
287 * event might come immediately, but it might come seconds, minutes, or hours
288 * later!
289 *
290 * \param instance_id the camera device instance ID.
291 * \param spec the desired format for data the device will provide. Can be
292 * NULL.
293 * \returns an SDL_Camera object or NULL on failure; call SDL_GetError() for
294 * more information.
295 *
296 * \threadsafety It is safe to call this function from any thread.
297 *
298 * \since This function is available since SDL 3.1.3.
299 *
300 * \sa SDL_GetCameras
301 * \sa SDL_GetCameraFormat
302 */
303extern SDL_DECLSPEC SDL_Camera * SDLCALL SDL_OpenCamera(SDL_CameraID instance_id, const SDL_CameraSpec *spec);
304
305/**
306 * Query if camera access has been approved by the user.
307 *
308 * Cameras will not function between when the device is opened by the app and
309 * when the user permits access to the hardware. On some platforms, this
310 * presents as a popup dialog where the user has to explicitly approve access;
311 * on others the approval might be implicit and not alert the user at all.
312 *
313 * This function can be used to check the status of that approval. It will
314 * return 0 if still waiting for user response, 1 if the camera is approved
315 * for use, and -1 if the user denied access.
316 *
317 * Instead of polling with this function, you can wait for a
318 * SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event
319 * in the standard SDL event loop, which is guaranteed to be sent once when
320 * permission to use the camera is decided.
321 *
322 * If a camera is declined, there's nothing to be done but call
323 * SDL_CloseCamera() to dispose of it.
324 *
325 * \param camera the opened camera device to query.
326 * \returns -1 if user denied access to the camera, 1 if user approved access,
327 * 0 if no decision has been made yet.
328 *
329 * \threadsafety It is safe to call this function from any thread.
330 *
331 * \since This function is available since SDL 3.1.3.
332 *
333 * \sa SDL_OpenCamera
334 * \sa SDL_CloseCamera
335 */
336extern SDL_DECLSPEC int SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);
337
338/**
339 * Get the instance ID of an opened camera.
340 *
341 * \param camera an SDL_Camera to query.
342 * \returns the instance ID of the specified camera on success or 0 on
343 * failure; call SDL_GetError() for more information.
344 *
345 * \threadsafety It is safe to call this function from any thread.
346 *
347 * \since This function is available since SDL 3.1.3.
348 *
349 * \sa SDL_OpenCamera
350 */
351extern SDL_DECLSPEC SDL_CameraID SDLCALL SDL_GetCameraID(SDL_Camera *camera);
352
353/**
354 * Get the properties associated with an opened camera.
355 *
356 * \param camera the SDL_Camera obtained from SDL_OpenCamera().
357 * \returns a valid property ID on success or 0 on failure; call
358 * SDL_GetError() for more information.
359 *
360 * \threadsafety It is safe to call this function from any thread.
361 *
362 * \since This function is available since SDL 3.1.3.
363 */
364extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetCameraProperties(SDL_Camera *camera);
365
366/**
367 * Get the spec that a camera is using when generating images.
368 *
369 * Note that this might not be the native format of the hardware, as SDL might
370 * be converting to this format behind the scenes.
371 *
372 * If the system is waiting for the user to approve access to the camera, as
373 * some platforms require, this will return false, but this isn't necessarily
374 * a fatal error; you should either wait for an
375 * SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event,
376 * or poll SDL_GetCameraPermissionState() occasionally until it returns
377 * non-zero.
378 *
379 * \param camera opened camera device.
380 * \param spec the SDL_CameraSpec to be initialized by this function.
381 * \returns true on success or false on failure; call SDL_GetError() for more
382 * information.
383 *
384 * \threadsafety It is safe to call this function from any thread.
385 *
386 * \since This function is available since SDL 3.1.3.
387 *
388 * \sa SDL_OpenCamera
389 */
390extern SDL_DECLSPEC bool SDLCALL SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec);
391
392/**
393 * Acquire a frame.
394 *
395 * The frame is a memory pointer to the image data, whose size and format are
396 * given by the spec requested when opening the device.
397 *
398 * This is a non blocking API. If there is a frame available, a non-NULL
399 * surface is returned, and timestampNS will be filled with a non-zero value.
400 *
401 * Note that an error case can also return NULL, but a NULL by itself is
402 * normal and just signifies that a new frame is not yet available. Note that
403 * even if a camera device fails outright (a USB camera is unplugged while in
404 * use, etc), SDL will send an event separately to notify the app, but
405 * continue to provide blank frames at ongoing intervals until
406 * SDL_CloseCamera() is called, so real failure here is almost always an out
407 * of memory condition.
408 *
409 * After use, the frame should be released with SDL_ReleaseCameraFrame(). If
410 * you don't do this, the system may stop providing more video!
411 *
412 * Do not call SDL_FreeSurface() on the returned surface! It must be given
413 * back to the camera subsystem with SDL_ReleaseCameraFrame!
414 *
415 * If the system is waiting for the user to approve access to the camera, as
416 * some platforms require, this will return NULL (no frames available); you
417 * should either wait for an SDL_EVENT_CAMERA_DEVICE_APPROVED (or
418 * SDL_EVENT_CAMERA_DEVICE_DENIED) event, or poll
419 * SDL_GetCameraPermissionState() occasionally until it returns non-zero.
420 *
421 * \param camera opened camera device.
422 * \param timestampNS a pointer filled in with the frame's timestamp, or 0 on
423 * error. Can be NULL.
424 * \returns a new frame of video on success, NULL if none is currently
425 * available.
426 *
427 * \threadsafety It is safe to call this function from any thread.
428 *
429 * \since This function is available since SDL 3.1.3.
430 *
431 * \sa SDL_ReleaseCameraFrame
432 */
433extern SDL_DECLSPEC SDL_Surface * SDLCALL SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS);
434
435/**
436 * Release a frame of video acquired from a camera.
437 *
438 * Let the back-end re-use the internal buffer for camera.
439 *
440 * This function _must_ be called only on surface objects returned by
441 * SDL_AcquireCameraFrame(). This function should be called as quickly as
442 * possible after acquisition, as SDL keeps a small FIFO queue of surfaces for
443 * video frames; if surfaces aren't released in a timely manner, SDL may drop
444 * upcoming video frames from the camera.
445 *
446 * If the app needs to keep the surface for a significant time, they should
447 * make a copy of it and release the original.
448 *
449 * The app should not use the surface again after calling this function;
450 * assume the surface is freed and the pointer is invalid.
451 *
452 * \param camera opened camera device.
453 * \param frame the video frame surface to release.
454 *
455 * \threadsafety It is safe to call this function from any thread.
456 *
457 * \since This function is available since SDL 3.1.3.
458 *
459 * \sa SDL_AcquireCameraFrame
460 */
461extern SDL_DECLSPEC void SDLCALL SDL_ReleaseCameraFrame(SDL_Camera *camera, SDL_Surface *frame);
462
463/**
464 * Use this function to shut down camera processing and close the camera
465 * device.
466 *
467 * \param camera opened camera device.
468 *
469 * \threadsafety It is safe to call this function from any thread, but no
470 * thread may reference `device` once this function is called.
471 *
472 * \since This function is available since SDL 3.1.3.
473 *
474 * \sa SDL_OpenCameraWithSpec
475 * \sa SDL_OpenCamera
476 */
477extern SDL_DECLSPEC void SDLCALL SDL_CloseCamera(SDL_Camera *camera);
478
479/* Ends C function definitions when using C++ */
480#ifdef __cplusplus
481}
482#endif
483#include <SDL3/SDL_close_code.h>
484
485#endif /* SDL_camera_h_ */
SDL_CameraPosition
Definition: SDL_camera.h:98
@ SDL_CAMERA_POSITION_BACK_FACING
Definition: SDL_camera.h:101
@ SDL_CAMERA_POSITION_UNKNOWN
Definition: SDL_camera.h:99
@ SDL_CAMERA_POSITION_FRONT_FACING
Definition: SDL_camera.h:100
SDL_CameraSpec ** SDL_GetCameraSupportedFormats(SDL_CameraID devid, int *count)
SDL_Camera * SDL_OpenCamera(SDL_CameraID instance_id, const SDL_CameraSpec *spec)
const char * SDL_GetCameraName(SDL_CameraID instance_id)
SDL_CameraPosition SDL_GetCameraPosition(SDL_CameraID instance_id)
Uint32 SDL_CameraID
Definition: SDL_camera.h:60
int SDL_GetNumCameraDrivers(void)
void SDL_ReleaseCameraFrame(SDL_Camera *camera, SDL_Surface *frame)
void SDL_CloseCamera(SDL_Camera *camera)
const char * SDL_GetCurrentCameraDriver(void)
SDL_CameraID SDL_GetCameraID(SDL_Camera *camera)
SDL_PropertiesID SDL_GetCameraProperties(SDL_Camera *camera)
int SDL_GetCameraPermissionState(SDL_Camera *camera)
struct SDL_Camera SDL_Camera
Definition: SDL_camera.h:67
bool SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec)
SDL_Surface * SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS)
const char * SDL_GetCameraDriver(int index)
SDL_CameraID * SDL_GetCameras(int *count)
SDL_Colorspace
Definition: SDL_pixels.h:594
SDL_PixelFormat
Definition: SDL_pixels.h:265
Uint32 SDL_PropertiesID
uint64_t Uint64
Definition: SDL_stdinc.h:392
uint32_t Uint32
Definition: SDL_stdinc.h:370
int framerate_denominator
Definition: SDL_camera.h:87
int framerate_numerator
Definition: SDL_camera.h:86
SDL_PixelFormat format
Definition: SDL_camera.h:82
SDL_Colorspace colorspace
Definition: SDL_camera.h:83