SDL3pp
A slim C++ wrapper for SDL3
Loading...
Searching...
No Matches
SDL3pp_camera.h
1#ifndef SDL3PP_CAMERA_H_
2#define SDL3PP_CAMERA_H_
3
4#include <SDL3/SDL_camera.h>
5#include "SDL3pp_error.h"
6#include "SDL3pp_pixels.h"
7#include "SDL3pp_properties.h"
8#include "SDL3pp_stdinc.h"
9#include "SDL3pp_surface.h"
10
11namespace SDL {
12
60// Forward decl
61struct Camera;
62
64using CameraRaw = SDL_Camera*;
65
66// Forward decl
67struct CameraRef;
68
71{
73
76 : value(value)
77 {
78 }
79
81 constexpr CameraParam(std::nullptr_t = nullptr)
82 : value(nullptr)
83 {
84 }
85
87 constexpr explicit operator bool() const { return !!value; }
88
90 constexpr auto operator<=>(const CameraParam& other) const = default;
91
93 constexpr operator CameraRaw() const { return value; }
94};
95
96// Forward decl
97struct CameraFrame;
98
111using CameraID = SDL_CameraID;
112
124using CameraSpec = SDL_CameraSpec;
125
133using CameraPosition = SDL_CameraPosition;
134
136 SDL_CAMERA_POSITION_UNKNOWN;
137
139 SDL_CAMERA_POSITION_FRONT_FACING;
140
142 SDL_CAMERA_POSITION_BACK_FACING;
143
144#if SDL_VERSION_ATLEAST(3, 4, 0)
145
153using CameraPermissionState = SDL_CameraPermissionState;
154
156 SDL_CAMERA_PERMISSION_STATE_DENIED;
157
159 SDL_CAMERA_PERMISSION_STATE_PENDING;
160
162 SDL_CAMERA_PERMISSION_STATE_APPROVED;
163#else
164
172using CameraPermissionState = int;
173
174#endif // SDL_VERSION_ATLEAST(3, 4, 0)
175
184{
185 CameraRaw m_resource = nullptr;
186
187public:
189 constexpr Camera(std::nullptr_t = nullptr) noexcept
190 : m_resource(0)
191 {
192 }
193
201 constexpr explicit Camera(const CameraRaw resource) noexcept
202 : m_resource(resource)
203 {
204 }
205
206protected:
208 constexpr Camera(const Camera& other) noexcept = default;
209
210public:
212 constexpr Camera(Camera&& other) noexcept
213 : Camera(other.release())
214 {
215 }
216
217 constexpr Camera(const CameraRef& other) = delete;
218
219 constexpr Camera(CameraRef&& other) = delete;
220
266 : m_resource(SDL_OpenCamera(instance_id, spec))
267 {
268 }
269
271 ~Camera() { SDL_CloseCamera(m_resource); }
272
274 constexpr Camera& operator=(Camera&& other) noexcept
275 {
276 std::swap(m_resource, other.m_resource);
277 return *this;
278 }
279
280protected:
282 constexpr Camera& operator=(const Camera& other) noexcept = default;
283
284public:
286 constexpr CameraRaw get() const noexcept { return m_resource; }
287
289 constexpr CameraRaw release() noexcept
290 {
291 auto r = m_resource;
292 m_resource = nullptr;
293 return r;
294 }
295
297 constexpr auto operator<=>(const Camera& other) const noexcept = default;
298
300 constexpr explicit operator bool() const noexcept { return !!m_resource; }
301
303 constexpr operator CameraParam() const noexcept { return {m_resource}; }
304
316 void Close();
317
351
364 CameraID GetID();
365
377
398 std::optional<CameraSpec> GetFormat();
399
440 CameraFrame AcquireFrame(Uint64* timestampNS = nullptr);
441
467 void ReleaseFrame(CameraFrame&& lock);
468};
469
472{
473 using Camera::Camera;
474
482 CameraRef(CameraParam resource) noexcept
483 : Camera(resource.value)
484 {
485 }
486
494 CameraRef(CameraRaw resource) noexcept
495 : Camera(resource)
496 {
497 }
498
500 constexpr CameraRef(const CameraRef& other) noexcept = default;
501
504};
505
507class CameraFrame : public Surface
508{
509 CameraRef m_lock;
510
511public:
553 CameraFrame(CameraRef resource, Uint64* timestampNS = nullptr);
554
556 CameraFrame(const CameraFrame& other) = delete;
557
559 constexpr CameraFrame(CameraFrame&& other) noexcept
560 : Surface(std::move(other))
561 , m_lock(other.m_lock)
562 {
563 other.m_lock = {};
564 }
565
590
591 CameraFrame& operator=(const CameraFrame& other) = delete;
592
595 {
596 std::swap(m_lock, other.m_lock);
597 return *this;
598 }
599
601 constexpr operator bool() const
602 {
603 return bool(m_lock) && Surface::operator bool();
604 }
605
629 void reset();
630
632 CameraRef get() { return m_lock; }
633
635 void release()
636 {
638 m_lock.release();
639 }
640};
641
663inline int GetNumCameraDrivers() { return SDL_GetNumCameraDrivers(); }
664
687inline const char* GetCameraDriver(int index)
688{
689 return SDL_GetCameraDriver(index);
690}
691
706inline const char* GetCurrentCameraDriver()
707{
708 return SDL_GetCurrentCameraDriver();
709}
710
724{
725 int count;
726 auto data = SDL_GetCameras(&count);
727 return OwnArray<CameraID>(data, count);
728}
729
763{
764 int count;
765 auto data = SDL_GetCameraSupportedFormats(instance_id, &count);
766 return OwnArray<CameraSpec*>(data, count);
767}
768
782inline const char* GetCameraName(CameraID instance_id)
783{
784 return SDL_GetCameraName(instance_id);
785}
786
805{
806 return SDL_GetCameraPosition(instance_id);
807}
808
853inline Camera OpenCamera(CameraID instance_id,
855{
856 return Camera(instance_id, spec);
857}
858
892{
893 return SDL_GetCameraPermissionState(camera);
894}
895
897{
898 return SDL::GetCameraPermissionState(m_resource);
899}
900
915{
916 return CheckError(SDL_GetCameraID(camera));
917}
918
919inline CameraID Camera::GetID() { return SDL::GetCameraID(m_resource); }
920
933{
934 return {CheckError(SDL_GetCameraProperties(camera))};
935}
936
938{
939 return SDL::GetCameraProperties(m_resource);
940}
941
963inline std::optional<CameraSpec> GetCameraFormat(CameraParam camera)
964{
965 if (CameraSpec spec; SDL_GetCameraFormat(camera, &spec)) return spec;
966 return std::nullopt;
967}
968
969inline std::optional<CameraSpec> Camera::GetFormat()
970{
971 return SDL::GetCameraFormat(m_resource);
972}
973
1015 Uint64* timestampNS = nullptr)
1016{
1017 return Surface::Borrow(SDL_AcquireCameraFrame(camera, timestampNS));
1018}
1019
1021{
1022 return {CameraRef(*this)};
1023}
1024
1025inline CameraFrame::CameraFrame(CameraRef resource, Uint64* timestampNS)
1026 : Surface(AcquireCameraFrame(resource, timestampNS))
1027 , m_lock(std::move(resource))
1028{
1029 if (!*this) m_lock.release();
1030}
1031
1059{
1060 SDL_ReleaseCameraFrame(camera, frame);
1061}
1062
1064{
1065 SDL_assert_paranoid(lock.get() == *this);
1066 lock.reset();
1067}
1068
1070{
1071 if (!m_lock) return;
1073 m_lock = {};
1074}
1075
1088inline void CloseCamera(CameraRaw camera) { SDL_CloseCamera(camera); }
1089
1090inline void Camera::Close() { CloseCamera(release()); }
1091
1093
1094} // namespace SDL
1095
1096#endif /* SDL3PP_CAMERA_H_ */
Camera Frame.
Definition: SDL3pp_camera.h:508
CameraFrame & operator=(CameraFrame &&other) noexcept
Assignment operator.
Definition: SDL3pp_camera.h:594
void release()
Releases the lock without unlocking.
Definition: SDL3pp_camera.h:635
CameraFrame(const CameraFrame &other)=delete
Copy constructor.
CameraRef get()
Get the reference to locked resource.
Definition: SDL3pp_camera.h:632
~CameraFrame()
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:589
constexpr CameraFrame(CameraFrame &&other) noexcept
Move constructor.
Definition: SDL3pp_camera.h:559
The opaque structure used to identify an opened SDL camera.
Definition: SDL3pp_camera.h:184
constexpr Camera(const Camera &other) noexcept=default
Copy constructor.
constexpr Camera & operator=(const Camera &other) noexcept=default
Assignment operator.
constexpr auto operator<=>(const Camera &other) const noexcept=default
Comparison.
constexpr CameraRaw get() const noexcept
Retrieves underlying CameraRaw.
Definition: SDL3pp_camera.h:286
constexpr CameraRaw release() noexcept
Retrieves underlying CameraRaw and clear this.
Definition: SDL3pp_camera.h:289
constexpr Camera(const CameraRaw resource) noexcept
Constructs from CameraParam.
Definition: SDL3pp_camera.h:201
Camera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition: SDL3pp_camera.h:265
constexpr Camera(Camera &&other) noexcept
Move constructor.
Definition: SDL3pp_camera.h:212
constexpr Camera & operator=(Camera &&other) noexcept
Assignment operator.
Definition: SDL3pp_camera.h:274
~Camera()
Destructor.
Definition: SDL3pp_camera.h:271
constexpr Camera(std::nullptr_t=nullptr) noexcept
Default ctor.
Definition: SDL3pp_camera.h:189
Optional-like shim for references.
Definition: SDL3pp_optionalRef.h:20
Base class for SDL memory allocated array wrap.
Definition: SDL3pp_ownPtr.h:44
A collection of pixels used in software blitting.
Definition: SDL3pp_surface.h:227
constexpr SurfaceRaw release() noexcept
Retrieves underlying SurfaceRaw and clear this.
Definition: SDL3pp_surface.h:536
static constexpr Surface Borrow(SurfaceParam resource)
Safely borrows the from SurfaceParam.
Definition: SDL3pp_surface.h:411
#define SDL_assert_paranoid(condition)
An assertion test that is performed only when built with paranoid settings.
Definition: SDL3pp_assert.h:383
Surface AcquireCameraFrame(CameraParam camera, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:1014
CameraPermissionState GetPermissionState()
Query if camera access has been approved by the user.
Definition: SDL3pp_camera.h:896
CameraFrame AcquireFrame(Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:1020
constexpr CameraPosition CAMERA_POSITION_FRONT_FACING
CAMERA_POSITION_FRONT_FACING.
Definition: SDL3pp_camera.h:138
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_APPROVED
CAMERA_PERMISSION_STATE_APPROVED.
Definition: SDL3pp_camera.h:161
SDL_CameraPermissionState CameraPermissionState
The current state of a request for camera access.
Definition: SDL3pp_camera.h:153
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_PENDING
CAMERA_PERMISSION_STATE_PENDING.
Definition: SDL3pp_camera.h:158
PropertiesRef GetCameraProperties(CameraParam camera)
Get the properties associated with an opened camera.
Definition: SDL3pp_camera.h:932
CameraID GetCameraID(CameraParam camera)
Get the instance ID of an opened camera.
Definition: SDL3pp_camera.h:914
void CloseCamera(CameraRaw camera)
Use this function to shut down camera processing and close the camera device.
Definition: SDL3pp_camera.h:1088
CameraPermissionState GetCameraPermissionState(CameraParam camera)
Query if camera access has been approved by the user.
Definition: SDL3pp_camera.h:891
constexpr CameraPosition CAMERA_POSITION_UNKNOWN
CAMERA_POSITION_UNKNOWN.
Definition: SDL3pp_camera.h:135
void Close()
Use this function to shut down camera processing and close the camera device.
Definition: SDL3pp_camera.h:1090
constexpr CameraPosition CAMERA_POSITION_BACK_FACING
CAMERA_POSITION_BACK_FACING.
Definition: SDL3pp_camera.h:141
void ReleaseFrame(CameraFrame &&lock)
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:1063
void ReleaseCameraFrame(CameraParam camera, SurfaceParam frame)
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:1058
Camera OpenCamera(CameraID instance_id, OptionalRef< const CameraSpec > spec={})
Open a video recording device (a "camera").
Definition: SDL3pp_camera.h:853
int GetNumCameraDrivers()
Use this function to get the number of built-in camera drivers.
Definition: SDL3pp_camera.h:663
constexpr CameraPermissionState CAMERA_PERMISSION_STATE_DENIED
CAMERA_PERMISSION_STATE_DENIED.
Definition: SDL3pp_camera.h:155
OwnArray< CameraID > GetCameras()
Get a list of currently connected camera devices.
Definition: SDL3pp_camera.h:723
SDL_CameraPosition CameraPosition
The position of camera in relation to system device.
Definition: SDL3pp_camera.h:133
SDL_CameraSpec CameraSpec
The details of an output format for a camera device.
Definition: SDL3pp_camera.h:124
OwnArray< CameraSpec * > GetCameraSupportedFormats(CameraID instance_id)
Get the list of native formats/sizes a camera supports.
Definition: SDL3pp_camera.h:762
SDL_Camera * CameraRaw
Alias to raw representation for Camera.
Definition: SDL3pp_camera.h:64
CameraFrame(CameraRef resource, Uint64 *timestampNS=nullptr)
Acquire a frame.
Definition: SDL3pp_camera.h:1025
const char * GetCameraDriver(int index)
Use this function to get the name of a built in camera driver.
Definition: SDL3pp_camera.h:687
PropertiesRef GetProperties()
Get the properties associated with an opened camera.
Definition: SDL3pp_camera.h:937
const char * GetCameraName(CameraID instance_id)
Get the human-readable device name for a camera.
Definition: SDL3pp_camera.h:782
std::optional< CameraSpec > GetCameraFormat(CameraParam camera)
Get the spec that a camera is using when generating images.
Definition: SDL3pp_camera.h:963
void reset()
Release a frame of video acquired from a camera.
Definition: SDL3pp_camera.h:1069
CameraID GetID()
Get the instance ID of an opened camera.
Definition: SDL3pp_camera.h:919
std::optional< CameraSpec > GetFormat()
Get the spec that a camera is using when generating images.
Definition: SDL3pp_camera.h:969
const char * GetCurrentCameraDriver()
Get the name of the current camera driver.
Definition: SDL3pp_camera.h:706
SDL_CameraID CameraID
This is a unique ID for a camera device for the time it is connected to the system,...
Definition: SDL3pp_camera.h:111
CameraPosition GetCameraPosition(CameraID instance_id)
Get the position of the camera in relation to the system.
Definition: SDL3pp_camera.h:804
constexpr void CheckError(bool result)
Check and throw if returned value from SDL is an error.
Definition: SDL3pp_error.h:197
::Uint64 Uint64
An unsigned 64-bit integer type.
Definition: SDL3pp_stdinc.h:371
Main include header for the SDL3pp library.
Safely wrap Camera for non owning parameters.
Definition: SDL3pp_camera.h:71
constexpr auto operator<=>(const CameraParam &other) const =default
Comparison.
constexpr CameraParam(std::nullptr_t=nullptr)
Constructs null/invalid.
Definition: SDL3pp_camera.h:81
constexpr CameraParam(CameraRaw value)
Constructs from CameraRaw.
Definition: SDL3pp_camera.h:75
CameraRaw value
parameter's CameraRaw
Definition: SDL3pp_camera.h:72
Semi-safe reference for Camera.
Definition: SDL3pp_camera.h:472
~CameraRef()
Destructor.
Definition: SDL3pp_camera.h:503
constexpr CameraRef(const CameraRef &other) noexcept=default
Copy constructor.
CameraRef(CameraRaw resource) noexcept
Constructs from CameraParam.
Definition: SDL3pp_camera.h:494
CameraRef(CameraParam resource) noexcept
Constructs from CameraParam.
Definition: SDL3pp_camera.h:482
Semi-safe reference for Properties.
Definition: SDL3pp_properties.h:716
Safely wrap Surface for non owning parameters.
Definition: SDL3pp_surface.h:53