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