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