sdl3_sys/generated/camera.rs
1//! Video capture for the SDL library.
2//!
3//! This API lets apps read input from video sources, like webcams. Camera
4//! devices can be enumerated, queried, and opened. Once opened, it will
5//! provide [`SDL_Surface`] objects as new frames of video come in. These surfaces
6//! can be uploaded to an [`SDL_Texture`] or processed as pixels in memory.
7//!
8//! Several platforms will alert the user if an app tries to access a camera,
9//! and some will present a UI asking the user if your application should be
10//! allowed to obtain images at all, which they can deny. A successfully opened
11//! camera will not provide images until permission is granted. Applications,
12//! after opening a camera device, can see if they were granted access by
13//! either polling with the [`SDL_GetCameraPermissionState()`] function, or waiting
14//! for an [`SDL_EVENT_CAMERA_DEVICE_APPROVED`] or [`SDL_EVENT_CAMERA_DEVICE_DENIED`]
15//! event. Platforms that don't have any user approval process will report
16//! approval immediately.
17//!
18//! Note that SDL cameras only provide video as individual frames; they will
19//! not provide full-motion video encoded in a movie file format, although an
20//! app is free to encode the acquired frames into any format it likes. It also
21//! does not provide audio from the camera hardware through this API; not only
22//! do many webcams not have microphones at all, many people--from streamers to
23//! people on Zoom calls--will want to use a separate microphone regardless of
24//! the camera. In any case, recorded audio will be available through SDL's
25//! audio API no matter what hardware provides the microphone.
26//!
27//! ## Camera gotchas
28//!
29//! Consumer-level camera hardware tends to take a little while to warm up,
30//! once the device has been opened. Generally most camera apps have some sort
31//! of UI to take a picture (a button to snap a pic while a preview is showing,
32//! some sort of multi-second countdown for the user to pose, like a photo
33//! booth), which puts control in the users' hands, or they are intended to
34//! stay on for long times (Pokemon Go, etc).
35//!
36//! It's not uncommon that a newly-opened camera will provide a couple of
37//! completely black frames, maybe followed by some under-exposed images. If
38//! taking a single frame automatically, or recording video from a camera's
39//! input without the user initiating it from a preview, it could be wise to
40//! drop the first several frames (if not the first several _seconds_ worth of
41//! frames!) before using images from a camera.
42
43use super::stdinc::*;
44
45use super::error::*;
46
47use super::pixels::*;
48
49use super::properties::*;
50
51use super::surface::*;
52
53/// This is a unique ID for a camera device for the time it is connected to the
54/// system, and is never reused for the lifetime of the application.
55///
56/// If the device is disconnected and reconnected, it will get a new ID.
57///
58/// The value 0 is an invalid ID.
59///
60/// ## Availability
61/// This datatype is available since SDL 3.2.0.
62///
63/// ## See also
64/// - [`SDL_GetCameras`]
65#[repr(transparent)]
66#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
67#[cfg_attr(feature = "debug-impls", derive(Debug))]
68pub struct SDL_CameraID(pub Uint32);
69
70impl ::core::cmp::PartialEq<Uint32> for SDL_CameraID {
71 #[inline(always)]
72 fn eq(&self, other: &Uint32) -> bool {
73 &self.0 == other
74 }
75}
76
77impl ::core::cmp::PartialEq<SDL_CameraID> for Uint32 {
78 #[inline(always)]
79 fn eq(&self, other: &SDL_CameraID) -> bool {
80 self == &other.0
81 }
82}
83
84impl From<SDL_CameraID> for Uint32 {
85 #[inline(always)]
86 fn from(value: SDL_CameraID) -> Self {
87 value.0
88 }
89}
90
91#[cfg(feature = "display-impls")]
92impl ::core::fmt::Display for SDL_CameraID {
93 #[inline(always)]
94 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
95 <Uint32 as ::core::fmt::Display>::fmt(&self.0, f)
96 }
97}
98
99impl SDL_CameraID {
100 /// Initialize a `SDL_CameraID` from a raw value.
101 #[inline(always)]
102 pub const fn new(value: Uint32) -> Self {
103 Self(value)
104 }
105}
106
107impl SDL_CameraID {
108 /// Get a copy of the inner raw value.
109 #[inline(always)]
110 pub const fn value(&self) -> Uint32 {
111 self.0
112 }
113}
114
115#[cfg(feature = "metadata")]
116impl sdl3_sys::metadata::GroupMetadata for SDL_CameraID {
117 const GROUP_METADATA: &'static sdl3_sys::metadata::Group =
118 &crate::metadata::camera::METADATA_SDL_CameraID;
119}
120
121/// The details of an output format for a camera device.
122///
123/// Cameras often support multiple formats; each one will be encapsulated in
124/// this struct.
125///
126/// ## Availability
127/// This struct is available since SDL 3.2.0.
128///
129/// ## See also
130/// - [`SDL_GetCameraSupportedFormats`]
131/// - [`SDL_GetCameraFormat`]
132#[repr(C)]
133#[derive(Clone, Copy, Default, PartialEq, Eq, Hash)]
134#[cfg_attr(feature = "debug-impls", derive(Debug))]
135pub struct SDL_CameraSpec {
136 /// Frame format
137 pub format: SDL_PixelFormat,
138 /// Frame colorspace
139 pub colorspace: SDL_Colorspace,
140 /// Frame width
141 pub width: ::core::ffi::c_int,
142 /// Frame height
143 pub height: ::core::ffi::c_int,
144 /// Frame rate numerator ((num / denom) == FPS, (denom / num) == duration in seconds)
145 pub framerate_numerator: ::core::ffi::c_int,
146 /// Frame rate denominator ((num / denom) == FPS, (denom / num) == duration in seconds)
147 pub framerate_denominator: ::core::ffi::c_int,
148}
149
150/// The position of camera in relation to system device.
151///
152/// ## Availability
153/// This enum is available since SDL 3.2.0.
154///
155/// ## See also
156/// - [`SDL_GetCameraPosition`]
157///
158/// ## Known values (`sdl3-sys`)
159/// | Associated constant | Global constant | Description |
160/// | ------------------- | --------------- | ----------- |
161/// | [`UNKNOWN`](SDL_CameraPosition::UNKNOWN) | [`SDL_CAMERA_POSITION_UNKNOWN`] | |
162/// | [`FRONT_FACING`](SDL_CameraPosition::FRONT_FACING) | [`SDL_CAMERA_POSITION_FRONT_FACING`] | |
163/// | [`BACK_FACING`](SDL_CameraPosition::BACK_FACING) | [`SDL_CAMERA_POSITION_BACK_FACING`] | |
164#[repr(transparent)]
165#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
166pub struct SDL_CameraPosition(pub ::core::ffi::c_int);
167
168impl ::core::cmp::PartialEq<::core::ffi::c_int> for SDL_CameraPosition {
169 #[inline(always)]
170 fn eq(&self, other: &::core::ffi::c_int) -> bool {
171 &self.0 == other
172 }
173}
174
175impl ::core::cmp::PartialEq<SDL_CameraPosition> for ::core::ffi::c_int {
176 #[inline(always)]
177 fn eq(&self, other: &SDL_CameraPosition) -> bool {
178 self == &other.0
179 }
180}
181
182impl From<SDL_CameraPosition> for ::core::ffi::c_int {
183 #[inline(always)]
184 fn from(value: SDL_CameraPosition) -> Self {
185 value.0
186 }
187}
188
189#[cfg(feature = "debug-impls")]
190impl ::core::fmt::Debug for SDL_CameraPosition {
191 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
192 #[allow(unreachable_patterns)]
193 f.write_str(match *self {
194 Self::UNKNOWN => "SDL_CAMERA_POSITION_UNKNOWN",
195 Self::FRONT_FACING => "SDL_CAMERA_POSITION_FRONT_FACING",
196 Self::BACK_FACING => "SDL_CAMERA_POSITION_BACK_FACING",
197
198 _ => return write!(f, "SDL_CameraPosition({})", self.0),
199 })
200 }
201}
202
203impl SDL_CameraPosition {
204 pub const UNKNOWN: Self = Self((0 as ::core::ffi::c_int));
205 pub const FRONT_FACING: Self = Self((1 as ::core::ffi::c_int));
206 pub const BACK_FACING: Self = Self((2 as ::core::ffi::c_int));
207}
208
209pub const SDL_CAMERA_POSITION_UNKNOWN: SDL_CameraPosition = SDL_CameraPosition::UNKNOWN;
210pub const SDL_CAMERA_POSITION_FRONT_FACING: SDL_CameraPosition = SDL_CameraPosition::FRONT_FACING;
211pub const SDL_CAMERA_POSITION_BACK_FACING: SDL_CameraPosition = SDL_CameraPosition::BACK_FACING;
212
213impl SDL_CameraPosition {
214 /// Initialize a `SDL_CameraPosition` from a raw value.
215 #[inline(always)]
216 pub const fn new(value: ::core::ffi::c_int) -> Self {
217 Self(value)
218 }
219}
220
221impl SDL_CameraPosition {
222 /// Get a copy of the inner raw value.
223 #[inline(always)]
224 pub const fn value(&self) -> ::core::ffi::c_int {
225 self.0
226 }
227}
228
229#[cfg(feature = "metadata")]
230impl sdl3_sys::metadata::GroupMetadata for SDL_CameraPosition {
231 const GROUP_METADATA: &'static sdl3_sys::metadata::Group =
232 &crate::metadata::camera::METADATA_SDL_CameraPosition;
233}
234
235/// The current state of a request for camera access.
236///
237/// ## Availability
238/// This enum is available since SDL 3.4.0.
239///
240/// ## See also
241/// - [`SDL_GetCameraPermissionState`]
242///
243/// ## Known values (`sdl3-sys`)
244/// | Associated constant | Global constant | Description |
245/// | ------------------- | --------------- | ----------- |
246/// | [`DENIED`](SDL_CameraPermissionState::DENIED) | [`SDL_CAMERA_PERMISSION_STATE_DENIED`] | |
247/// | [`PENDING`](SDL_CameraPermissionState::PENDING) | [`SDL_CAMERA_PERMISSION_STATE_PENDING`] | |
248/// | [`APPROVED`](SDL_CameraPermissionState::APPROVED) | [`SDL_CAMERA_PERMISSION_STATE_APPROVED`] | |
249#[repr(transparent)]
250#[derive(Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
251pub struct SDL_CameraPermissionState(pub ::core::ffi::c_int);
252
253impl ::core::cmp::PartialEq<::core::ffi::c_int> for SDL_CameraPermissionState {
254 #[inline(always)]
255 fn eq(&self, other: &::core::ffi::c_int) -> bool {
256 &self.0 == other
257 }
258}
259
260impl ::core::cmp::PartialEq<SDL_CameraPermissionState> for ::core::ffi::c_int {
261 #[inline(always)]
262 fn eq(&self, other: &SDL_CameraPermissionState) -> bool {
263 self == &other.0
264 }
265}
266
267impl From<SDL_CameraPermissionState> for ::core::ffi::c_int {
268 #[inline(always)]
269 fn from(value: SDL_CameraPermissionState) -> Self {
270 value.0
271 }
272}
273
274#[cfg(feature = "debug-impls")]
275impl ::core::fmt::Debug for SDL_CameraPermissionState {
276 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
277 #[allow(unreachable_patterns)]
278 f.write_str(match *self {
279 Self::DENIED => "SDL_CAMERA_PERMISSION_STATE_DENIED",
280 Self::PENDING => "SDL_CAMERA_PERMISSION_STATE_PENDING",
281 Self::APPROVED => "SDL_CAMERA_PERMISSION_STATE_APPROVED",
282
283 _ => return write!(f, "SDL_CameraPermissionState({})", self.0),
284 })
285 }
286}
287
288impl SDL_CameraPermissionState {
289 pub const DENIED: Self = Self((-1_i32 as ::core::ffi::c_int));
290 pub const PENDING: Self = Self((0_i32 as ::core::ffi::c_int));
291 pub const APPROVED: Self = Self((1_i32 as ::core::ffi::c_int));
292}
293
294pub const SDL_CAMERA_PERMISSION_STATE_DENIED: SDL_CameraPermissionState =
295 SDL_CameraPermissionState::DENIED;
296pub const SDL_CAMERA_PERMISSION_STATE_PENDING: SDL_CameraPermissionState =
297 SDL_CameraPermissionState::PENDING;
298pub const SDL_CAMERA_PERMISSION_STATE_APPROVED: SDL_CameraPermissionState =
299 SDL_CameraPermissionState::APPROVED;
300
301impl SDL_CameraPermissionState {
302 /// Initialize a `SDL_CameraPermissionState` from a raw value.
303 #[inline(always)]
304 pub const fn new(value: ::core::ffi::c_int) -> Self {
305 Self(value)
306 }
307}
308
309impl SDL_CameraPermissionState {
310 /// Get a copy of the inner raw value.
311 #[inline(always)]
312 pub const fn value(&self) -> ::core::ffi::c_int {
313 self.0
314 }
315}
316
317#[cfg(feature = "metadata")]
318impl sdl3_sys::metadata::GroupMetadata for SDL_CameraPermissionState {
319 const GROUP_METADATA: &'static sdl3_sys::metadata::Group =
320 &crate::metadata::camera::METADATA_SDL_CameraPermissionState;
321}
322
323unsafe extern "C" {
324 /// Use this function to get the number of built-in camera drivers.
325 ///
326 /// This function returns a hardcoded number. This never returns a negative
327 /// value; if there are no drivers compiled into this build of SDL, this
328 /// function returns zero. The presence of a driver in this list does not mean
329 /// it will function, it just means SDL is capable of interacting with that
330 /// interface. For example, a build of SDL might have v4l2 support, but if
331 /// there's no kernel support available, SDL's v4l2 driver would fail if used.
332 ///
333 /// By default, SDL tries all drivers, in its preferred order, until one is
334 /// found to be usable.
335 ///
336 /// ## Return value
337 /// Returns the number of built-in camera drivers.
338 ///
339 /// ## Thread safety
340 /// It is safe to call this function from any thread.
341 ///
342 /// ## Availability
343 /// This function is available since SDL 3.2.0.
344 ///
345 /// ## See also
346 /// - [`SDL_GetCameraDriver`]
347 pub fn SDL_GetNumCameraDrivers() -> ::core::ffi::c_int;
348}
349
350unsafe extern "C" {
351 /// Use this function to get the name of a built in camera driver.
352 ///
353 /// The list of camera drivers is given in the order that they are normally
354 /// initialized by default; the drivers that seem more reasonable to choose
355 /// first (as far as the SDL developers believe) are earlier in the list.
356 ///
357 /// The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
358 /// "coremedia" or "android". These never have Unicode characters, and are not
359 /// meant to be proper names.
360 ///
361 /// ## Parameters
362 /// - `index`: the index of the camera driver; the value ranges from 0 to
363 /// [`SDL_GetNumCameraDrivers()`] - 1.
364 ///
365 /// ## Return value
366 /// Returns the name of the camera driver at the requested index, or NULL if
367 /// an invalid index was specified.
368 ///
369 /// ## Thread safety
370 /// It is safe to call this function from any thread.
371 ///
372 /// ## Availability
373 /// This function is available since SDL 3.2.0.
374 ///
375 /// ## See also
376 /// - [`SDL_GetNumCameraDrivers`]
377 pub fn SDL_GetCameraDriver(index: ::core::ffi::c_int) -> *const ::core::ffi::c_char;
378}
379
380unsafe extern "C" {
381 /// Get the name of the current camera driver.
382 ///
383 /// The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
384 /// "coremedia" or "android". These never have Unicode characters, and are not
385 /// meant to be proper names.
386 ///
387 /// ## Return value
388 /// Returns the name of the current camera driver or NULL if no driver has
389 /// been initialized.
390 ///
391 /// ## Thread safety
392 /// It is safe to call this function from any thread.
393 ///
394 /// ## Availability
395 /// This function is available since SDL 3.2.0.
396 pub fn SDL_GetCurrentCameraDriver() -> *const ::core::ffi::c_char;
397}
398
399unsafe extern "C" {
400 /// Get a list of currently connected camera devices.
401 ///
402 /// ## Parameters
403 /// - `count`: a pointer filled in with the number of cameras returned, may
404 /// be NULL.
405 ///
406 /// ## Return value
407 /// Returns a 0 terminated array of camera instance IDs or NULL on failure;
408 /// call [`SDL_GetError()`] for more information. This should be freed
409 /// with [`SDL_free()`] when it is no longer needed.
410 ///
411 /// ## Thread safety
412 /// It is safe to call this function from any thread.
413 ///
414 /// ## Availability
415 /// This function is available since SDL 3.2.0.
416 ///
417 /// ## See also
418 /// - [`SDL_OpenCamera`]
419 pub fn SDL_GetCameras(count: *mut ::core::ffi::c_int) -> *mut SDL_CameraID;
420}
421
422unsafe extern "C" {
423 /// Get the list of native formats/sizes a camera supports.
424 ///
425 /// This returns a list of all formats and frame sizes that a specific camera
426 /// can offer. This is useful if your app can accept a variety of image formats
427 /// and sizes and so want to find the optimal spec that doesn't require
428 /// conversion.
429 ///
430 /// This function isn't strictly required; if you call [`SDL_OpenCamera`] with a
431 /// NULL spec, SDL will choose a native format for you, and if you instead
432 /// specify a desired format, it will transparently convert to the requested
433 /// format on your behalf.
434 ///
435 /// If `count` is not NULL, it will be filled with the number of elements in
436 /// the returned array.
437 ///
438 /// Note that it's legal for a camera to supply an empty list. This is what
439 /// will happen on Emscripten builds, since that platform won't tell _anything_
440 /// about available cameras until you've opened one, and won't even tell if
441 /// there _is_ a camera until the user has given you permission to check
442 /// through a scary warning popup.
443 ///
444 /// ## Parameters
445 /// - `instance_id`: the camera device instance ID.
446 /// - `count`: a pointer filled in with the number of elements in the list,
447 /// may be NULL.
448 ///
449 /// ## Return value
450 /// Returns a NULL terminated array of pointers to [`SDL_CameraSpec`] or NULL on
451 /// failure; call [`SDL_GetError()`] for more information. This is a
452 /// single allocation that should be freed with [`SDL_free()`] when it is
453 /// no longer needed.
454 ///
455 /// ## Thread safety
456 /// It is safe to call this function from any thread.
457 ///
458 /// ## Availability
459 /// This function is available since SDL 3.2.0.
460 ///
461 /// ## See also
462 /// - [`SDL_GetCameras`]
463 /// - [`SDL_OpenCamera`]
464 pub fn SDL_GetCameraSupportedFormats(
465 instance_id: SDL_CameraID,
466 count: *mut ::core::ffi::c_int,
467 ) -> *mut *mut SDL_CameraSpec;
468}
469
470unsafe extern "C" {
471 /// Get the human-readable device name for a camera.
472 ///
473 /// ## Parameters
474 /// - `instance_id`: the camera device instance ID.
475 ///
476 /// ## Return value
477 /// Returns a human-readable device name or NULL on failure; call
478 /// [`SDL_GetError()`] for more information.
479 ///
480 /// ## Thread safety
481 /// It is safe to call this function from any thread.
482 ///
483 /// ## Availability
484 /// This function is available since SDL 3.2.0.
485 ///
486 /// ## See also
487 /// - [`SDL_GetCameras`]
488 pub fn SDL_GetCameraName(instance_id: SDL_CameraID) -> *const ::core::ffi::c_char;
489}
490
491unsafe extern "C" {
492 /// Get the position of the camera in relation to the system.
493 ///
494 /// Most platforms will report UNKNOWN, but mobile devices, like phones, can
495 /// often make a distinction between cameras on the front of the device (that
496 /// points towards the user, for taking "selfies") and cameras on the back (for
497 /// filming in the direction the user is facing).
498 ///
499 /// ## Parameters
500 /// - `instance_id`: the camera device instance ID.
501 ///
502 /// ## Return value
503 /// Returns the position of the camera on the system hardware.
504 ///
505 /// ## Thread safety
506 /// It is safe to call this function from any thread.
507 ///
508 /// ## Availability
509 /// This function is available since SDL 3.2.0.
510 ///
511 /// ## See also
512 /// - [`SDL_GetCameras`]
513 pub fn SDL_GetCameraPosition(instance_id: SDL_CameraID) -> SDL_CameraPosition;
514}
515
516unsafe extern "C" {
517 /// Open a video recording device (a "camera").
518 ///
519 /// You can open the device with any reasonable spec, and if the hardware can't
520 /// directly support it, it will convert data seamlessly to the requested
521 /// format. This might incur overhead, including scaling of image data.
522 ///
523 /// If you would rather accept whatever format the device offers, you can pass
524 /// a NULL spec here and it will choose one for you (and you can use
525 /// SDL_Surface's conversion/scaling functions directly if necessary).
526 ///
527 /// You can call [`SDL_GetCameraFormat()`] to get the actual data format if passing
528 /// a NULL spec here. You can see the exact specs a device can support without
529 /// conversion with [`SDL_GetCameraSupportedFormats()`].
530 ///
531 /// SDL will not attempt to emulate framerate; it will try to set the hardware
532 /// to the rate closest to the requested speed, but it won't attempt to limit
533 /// or duplicate frames artificially; call [`SDL_GetCameraFormat()`] to see the
534 /// actual framerate of the opened the device, and check your timestamps if
535 /// this is crucial to your app!
536 ///
537 /// Note that the camera is not usable until the user approves its use! On some
538 /// platforms, the operating system will prompt the user to permit access to
539 /// the camera, and they can choose Yes or No at that point. Until they do, the
540 /// camera will not be usable. The app should either wait for an
541 /// [`SDL_EVENT_CAMERA_DEVICE_APPROVED`] (or [`SDL_EVENT_CAMERA_DEVICE_DENIED`]) event,
542 /// or poll [`SDL_GetCameraPermissionState()`] occasionally until it returns
543 /// non-zero. On platforms that don't require explicit user approval (and
544 /// perhaps in places where the user previously permitted access), the approval
545 /// event might come immediately, but it might come seconds, minutes, or hours
546 /// later!
547 ///
548 /// ## Parameters
549 /// - `instance_id`: the camera device instance ID.
550 /// - `spec`: the desired format for data the device will provide. Can be
551 /// NULL.
552 ///
553 /// ## Return value
554 /// Returns an [`SDL_Camera`] object or NULL on failure; call [`SDL_GetError()`] for
555 /// more information.
556 ///
557 /// ## Thread safety
558 /// It is safe to call this function from any thread.
559 ///
560 /// ## Availability
561 /// This function is available since SDL 3.2.0.
562 ///
563 /// ## See also
564 /// - [`SDL_GetCameras`]
565 /// - [`SDL_GetCameraFormat`]
566 pub fn SDL_OpenCamera(
567 instance_id: SDL_CameraID,
568 spec: *const SDL_CameraSpec,
569 ) -> *mut SDL_Camera;
570}
571
572unsafe extern "C" {
573 /// Query if camera access has been approved by the user.
574 ///
575 /// Cameras will not function between when the device is opened by the app and
576 /// when the user permits access to the hardware. On some platforms, this
577 /// presents as a popup dialog where the user has to explicitly approve access;
578 /// on others the approval might be implicit and not alert the user at all.
579 ///
580 /// This function can be used to check the status of that approval. It will
581 /// return [`SDL_CAMERA_PERMISSION_STATE_PENDING`] if waiting for user response,
582 /// [`SDL_CAMERA_PERMISSION_STATE_APPROVED`] if the camera is approved for use, and
583 /// [`SDL_CAMERA_PERMISSION_STATE_DENIED`] if the user denied access.
584 ///
585 /// Instead of polling with this function, you can wait for a
586 /// [`SDL_EVENT_CAMERA_DEVICE_APPROVED`] (or [`SDL_EVENT_CAMERA_DEVICE_DENIED`]) event
587 /// in the standard SDL event loop, which is guaranteed to be sent once when
588 /// permission to use the camera is decided.
589 ///
590 /// If a camera is declined, there's nothing to be done but call
591 /// [`SDL_CloseCamera()`] to dispose of it.
592 ///
593 /// ## Parameters
594 /// - `camera`: the opened camera device to query.
595 ///
596 /// ## Return value
597 /// Returns an [`SDL_CameraPermissionState`] value indicating if access is
598 /// granted, or [`SDL_CAMERA_PERMISSION_STATE_PENDING`] if the decision
599 /// is still pending.
600 ///
601 /// ## Thread safety
602 /// It is safe to call this function from any thread.
603 ///
604 /// ## Availability
605 /// This function is available since SDL 3.2.0.
606 ///
607 /// ## See also
608 /// - [`SDL_OpenCamera`]
609 /// - [`SDL_CloseCamera`]
610 pub fn SDL_GetCameraPermissionState(camera: *mut SDL_Camera) -> SDL_CameraPermissionState;
611}
612
613unsafe extern "C" {
614 /// Get the instance ID of an opened camera.
615 ///
616 /// ## Parameters
617 /// - `camera`: an [`SDL_Camera`] to query.
618 ///
619 /// ## Return value
620 /// Returns the instance ID of the specified camera on success or 0 on
621 /// failure; call [`SDL_GetError()`] for more information.
622 ///
623 /// ## Thread safety
624 /// It is safe to call this function from any thread.
625 ///
626 /// ## Availability
627 /// This function is available since SDL 3.2.0.
628 ///
629 /// ## See also
630 /// - [`SDL_OpenCamera`]
631 pub fn SDL_GetCameraID(camera: *mut SDL_Camera) -> SDL_CameraID;
632}
633
634unsafe extern "C" {
635 /// Get the properties associated with an opened camera.
636 ///
637 /// ## Parameters
638 /// - `camera`: the [`SDL_Camera`] obtained from [`SDL_OpenCamera()`].
639 ///
640 /// ## Return value
641 /// Returns a valid property ID on success or 0 on failure; call
642 /// [`SDL_GetError()`] for more information.
643 ///
644 /// ## Thread safety
645 /// It is safe to call this function from any thread.
646 ///
647 /// ## Availability
648 /// This function is available since SDL 3.2.0.
649 pub fn SDL_GetCameraProperties(camera: *mut SDL_Camera) -> SDL_PropertiesID;
650}
651
652unsafe extern "C" {
653 /// Get the spec that a camera is using when generating images.
654 ///
655 /// Note that this might not be the native format of the hardware, as SDL might
656 /// be converting to this format behind the scenes.
657 ///
658 /// If the system is waiting for the user to approve access to the camera, as
659 /// some platforms require, this will return false, but this isn't necessarily
660 /// a fatal error; you should either wait for an
661 /// [`SDL_EVENT_CAMERA_DEVICE_APPROVED`] (or [`SDL_EVENT_CAMERA_DEVICE_DENIED`]) event,
662 /// or poll [`SDL_GetCameraPermissionState()`] occasionally until it returns
663 /// non-zero.
664 ///
665 /// ## Parameters
666 /// - `camera`: opened camera device.
667 /// - `spec`: the [`SDL_CameraSpec`] to be initialized by this function.
668 ///
669 /// ## Return value
670 /// Returns true on success or false on failure; call [`SDL_GetError()`] for more
671 /// information.
672 ///
673 /// ## Thread safety
674 /// It is safe to call this function from any thread.
675 ///
676 /// ## Availability
677 /// This function is available since SDL 3.2.0.
678 ///
679 /// ## See also
680 /// - [`SDL_OpenCamera`]
681 pub fn SDL_GetCameraFormat(
682 camera: *mut SDL_Camera,
683 spec: *mut SDL_CameraSpec,
684 ) -> ::core::primitive::bool;
685}
686
687unsafe extern "C" {
688 /// Acquire a frame.
689 ///
690 /// The frame is a memory pointer to the image data, whose size and format are
691 /// given by the spec requested when opening the device.
692 ///
693 /// This is a non blocking API. If there is a frame available, a non-NULL
694 /// surface is returned, and timestampNS will be filled with a non-zero value.
695 ///
696 /// Note that an error case can also return NULL, but a NULL by itself is
697 /// normal and just signifies that a new frame is not yet available. Note that
698 /// even if a camera device fails outright (a USB camera is unplugged while in
699 /// use, etc), SDL will send an event separately to notify the app, but
700 /// continue to provide blank frames at ongoing intervals until
701 /// [`SDL_CloseCamera()`] is called, so real failure here is almost always an out
702 /// of memory condition.
703 ///
704 /// After use, the frame should be released with [`SDL_ReleaseCameraFrame()`]. If
705 /// you don't do this, the system may stop providing more video!
706 ///
707 /// Do not call [`SDL_DestroySurface()`] on the returned surface! It must be given
708 /// back to the camera subsystem with SDL_ReleaseCameraFrame!
709 ///
710 /// If the system is waiting for the user to approve access to the camera, as
711 /// some platforms require, this will return NULL (no frames available); you
712 /// should either wait for an [`SDL_EVENT_CAMERA_DEVICE_APPROVED`] (or
713 /// [`SDL_EVENT_CAMERA_DEVICE_DENIED`]) event, or poll
714 /// [`SDL_GetCameraPermissionState()`] occasionally until it returns non-zero.
715 ///
716 /// ## Parameters
717 /// - `camera`: opened camera device.
718 /// - `timestampNS`: a pointer filled in with the frame's timestamp, or 0 on
719 /// error. Can be NULL.
720 ///
721 /// ## Return value
722 /// Returns a new frame of video on success, NULL if none is currently
723 /// available.
724 ///
725 /// ## Thread safety
726 /// It is safe to call this function from any thread.
727 ///
728 /// ## Availability
729 /// This function is available since SDL 3.2.0.
730 ///
731 /// ## See also
732 /// - [`SDL_ReleaseCameraFrame`]
733 pub fn SDL_AcquireCameraFrame(
734 camera: *mut SDL_Camera,
735 timestampNS: *mut Uint64,
736 ) -> *mut SDL_Surface;
737}
738
739unsafe extern "C" {
740 /// Release a frame of video acquired from a camera.
741 ///
742 /// Let the back-end re-use the internal buffer for camera.
743 ///
744 /// This function _must_ be called only on surface objects returned by
745 /// [`SDL_AcquireCameraFrame()`]. This function should be called as quickly as
746 /// possible after acquisition, as SDL keeps a small FIFO queue of surfaces for
747 /// video frames; if surfaces aren't released in a timely manner, SDL may drop
748 /// upcoming video frames from the camera.
749 ///
750 /// If the app needs to keep the surface for a significant time, they should
751 /// make a copy of it and release the original.
752 ///
753 /// The app should not use the surface again after calling this function;
754 /// assume the surface is freed and the pointer is invalid.
755 ///
756 /// ## Parameters
757 /// - `camera`: opened camera device.
758 /// - `frame`: the video frame surface to release.
759 ///
760 /// ## Thread safety
761 /// It is safe to call this function from any thread.
762 ///
763 /// ## Availability
764 /// This function is available since SDL 3.2.0.
765 ///
766 /// ## See also
767 /// - [`SDL_AcquireCameraFrame`]
768 pub fn SDL_ReleaseCameraFrame(camera: *mut SDL_Camera, frame: *mut SDL_Surface);
769}
770
771unsafe extern "C" {
772 /// Use this function to shut down camera processing and close the camera
773 /// device.
774 ///
775 /// ## Parameters
776 /// - `camera`: opened camera device.
777 ///
778 /// ## Thread safety
779 /// It is safe to call this function from any thread, but no
780 /// thread may reference `device` once this function is called.
781 ///
782 /// ## Availability
783 /// This function is available since SDL 3.2.0.
784 ///
785 /// ## See also
786 /// - [`SDL_OpenCamera`]
787 pub fn SDL_CloseCamera(camera: *mut SDL_Camera);
788}
789
790/// The opaque structure used to identify an opened SDL camera.
791///
792/// ## Availability
793/// This struct is available since SDL 3.2.0.
794#[repr(C)]
795pub struct SDL_Camera {
796 _opaque: [::core::primitive::u8; 0],
797}
798
799#[cfg(doc)]
800use crate::everything::*;