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