SDL_camera.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /**
  19. * \file SDL_camera.h
  20. *
  21. * Video Capture for the SDL library.
  22. */
  23. #ifndef SDL_camera_h_
  24. #define SDL_camera_h_
  25. #include "SDL3/SDL_video.h"
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * This is a unique ID for a camera device for the time it is connected to the system,
  33. * and is never reused for the lifetime of the application. If the device is
  34. * disconnected and reconnected, it will get a new ID.
  35. *
  36. * The ID value starts at 1 and increments from there. The value 0 is an invalid ID.
  37. *
  38. * \sa SDL_GetCameraDevices
  39. */
  40. typedef Uint32 SDL_CameraDeviceID;
  41. /**
  42. * The structure used to identify an SDL camera device
  43. */
  44. struct SDL_CameraDevice;
  45. typedef struct SDL_CameraDevice SDL_CameraDevice;
  46. #define SDL_CAMERA_ALLOW_ANY_CHANGE 1
  47. /**
  48. * SDL_CameraSpec structure
  49. *
  50. * Only those field can be 'desired' when configuring the device:
  51. * - format
  52. * - width
  53. * - height
  54. *
  55. * \sa SDL_GetCameraFormat
  56. * \sa SDL_GetCameraFrameSize
  57. *
  58. */
  59. typedef struct SDL_CameraSpec
  60. {
  61. Uint32 format; /**< Frame SDL_PixelFormatEnum format */
  62. int width; /**< Frame width */
  63. int height; /**< Frame height */
  64. } SDL_CameraSpec;
  65. /**
  66. * SDL Camera Status
  67. *
  68. * Change states but calling the function in this order:
  69. *
  70. * SDL_OpenCamera()
  71. * SDL_SetCameraSpec() -> Init
  72. * SDL_StartCamera() -> Playing
  73. * SDL_StopCamera() -> Stopped
  74. * SDL_CloseCamera()
  75. *
  76. */
  77. typedef enum
  78. {
  79. SDL_CAMERA_FAIL = -1, /**< Failed */
  80. SDL_CAMERA_INIT = 0, /**< Init, spec hasn't been set */
  81. SDL_CAMERA_STOPPED, /**< Stopped */
  82. SDL_CAMERA_PLAYING /**< Playing */
  83. } SDL_CameraStatus;
  84. /**
  85. * SDL Video Capture Status
  86. */
  87. typedef struct SDL_CameraFrame
  88. {
  89. Uint64 timestampNS; /**< Frame timestamp in nanoseconds when read from the driver */
  90. int num_planes; /**< Number of planes */
  91. Uint8 *data[3]; /**< Pointer to data of i-th plane */
  92. int pitch[3]; /**< Pitch of i-th plane */
  93. void *internal; /**< Private field */
  94. } SDL_CameraFrame;
  95. /**
  96. * Get a list of currently connected camera devices.
  97. *
  98. * \param count a pointer filled in with the number of camera devices
  99. * \returns a 0 terminated array of camera instance IDs which should be
  100. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  101. * more details.
  102. *
  103. * \since This function is available since SDL 3.0.0.
  104. *
  105. * \sa SDL_OpenCamera
  106. */
  107. extern DECLSPEC SDL_CameraDeviceID *SDLCALL SDL_GetCameraDevices(int *count);
  108. /**
  109. * Open a Video Capture device
  110. *
  111. * \param instance_id the camera device instance ID
  112. * \returns device, or NULL on failure; call SDL_GetError() for more
  113. * information.
  114. *
  115. * \since This function is available since SDL 3.0.0.
  116. *
  117. * \sa SDL_GetCameraDeviceName
  118. * \sa SDL_GetCameraDevices
  119. * \sa SDL_OpenCameraWithSpec
  120. */
  121. extern DECLSPEC SDL_CameraDevice *SDLCALL SDL_OpenCamera(SDL_CameraDeviceID instance_id);
  122. /**
  123. * Set specification
  124. *
  125. * \param device opened camera device
  126. * \param desired desired camera spec
  127. * \param obtained obtained camera spec
  128. * \param allowed_changes allow changes or not
  129. * \returns 0 on success or a negative error code on failure; call
  130. * SDL_GetError() for more information.
  131. *
  132. * \since This function is available since SDL 3.0.0.
  133. *
  134. * \sa SDL_OpenCamera
  135. * \sa SDL_OpenCameraWithSpec
  136. * \sa SDL_GetCameraSpec
  137. */
  138. extern DECLSPEC int SDLCALL SDL_SetCameraSpec(SDL_CameraDevice *device,
  139. const SDL_CameraSpec *desired,
  140. SDL_CameraSpec *obtained,
  141. int allowed_changes);
  142. /**
  143. * Open a Video Capture device and set specification
  144. *
  145. * \param instance_id the camera device instance ID
  146. * \param desired desired camera spec
  147. * \param obtained obtained camera spec
  148. * \param allowed_changes allow changes or not
  149. * \returns device, or NULL on failure; call SDL_GetError() for more
  150. * information.
  151. *
  152. * \since This function is available since SDL 3.0.0.
  153. *
  154. * \sa SDL_OpenCamera
  155. * \sa SDL_SetCameraSpec
  156. * \sa SDL_GetCameraSpec
  157. */
  158. extern DECLSPEC SDL_CameraDevice *SDLCALL SDL_OpenCameraWithSpec(SDL_CameraDeviceID instance_id,
  159. const SDL_CameraSpec *desired,
  160. SDL_CameraSpec *obtained,
  161. int allowed_changes);
  162. /**
  163. * Get device name
  164. *
  165. * \param instance_id the camera device instance ID
  166. * \returns device name, shouldn't be freed
  167. *
  168. * \since This function is available since SDL 3.0.0.
  169. *
  170. * \sa SDL_GetCameraDevices
  171. */
  172. extern DECLSPEC const char * SDLCALL SDL_GetCameraDeviceName(SDL_CameraDeviceID instance_id);
  173. /**
  174. * Get the obtained camera spec
  175. *
  176. * \param device opened camera device
  177. * \param spec The SDL_CameraSpec to be initialized by this function.
  178. * \returns 0 on success or a negative error code on failure; call
  179. * SDL_GetError() for more information.
  180. *
  181. * \since This function is available since SDL 3.0.0.
  182. *
  183. * \sa SDL_SetCameraSpec
  184. * \sa SDL_OpenCameraWithSpec
  185. */
  186. extern DECLSPEC int SDLCALL SDL_GetCameraSpec(SDL_CameraDevice *device, SDL_CameraSpec *spec);
  187. /**
  188. * Get frame format of camera device.
  189. *
  190. * The value can be used to fill SDL_CameraSpec structure.
  191. *
  192. * \param device opened camera device
  193. * \param index format between 0 and num -1
  194. * \param format pointer output format (SDL_PixelFormatEnum)
  195. * \returns 0 on success or a negative error code on failure; call
  196. * SDL_GetError() for more information.
  197. *
  198. * \since This function is available since SDL 3.0.0.
  199. *
  200. * \sa SDL_GetNumCameraFormats
  201. */
  202. extern DECLSPEC int SDLCALL SDL_GetCameraFormat(SDL_CameraDevice *device,
  203. int index,
  204. Uint32 *format);
  205. /**
  206. * Number of available formats for the device
  207. *
  208. * \param device opened camera device
  209. * \returns number of formats or a negative error code on failure; call
  210. * SDL_GetError() for more information.
  211. *
  212. * \since This function is available since SDL 3.0.0.
  213. *
  214. * \sa SDL_GetCameraFormat
  215. * \sa SDL_SetCameraSpec
  216. */
  217. extern DECLSPEC int SDLCALL SDL_GetNumCameraFormats(SDL_CameraDevice *device);
  218. /**
  219. * Get frame sizes of the device and the specified input format.
  220. *
  221. * The value can be used to fill SDL_CameraSpec structure.
  222. *
  223. * \param device opened camera device
  224. * \param format a format that can be used by the device (SDL_PixelFormatEnum)
  225. * \param index framesize between 0 and num -1
  226. * \param width output width
  227. * \param height output height
  228. * \returns 0 on success or a negative error code on failure; call
  229. * SDL_GetError() for more information.
  230. *
  231. * \since This function is available since SDL 3.0.0.
  232. *
  233. * \sa SDL_GetNumCameraFrameSizes
  234. */
  235. extern DECLSPEC int SDLCALL SDL_GetCameraFrameSize(SDL_CameraDevice *device, Uint32 format, int index, int *width, int *height);
  236. /**
  237. * Number of different framesizes available for the device and pixel format.
  238. *
  239. * \param device opened camera device
  240. * \param format frame pixel format (SDL_PixelFormatEnum)
  241. * \returns number of framesizes or a negative error code on failure; call
  242. * SDL_GetError() for more information.
  243. *
  244. * \since This function is available since SDL 3.0.0.
  245. *
  246. * \sa SDL_GetCameraFrameSize
  247. * \sa SDL_SetCameraSpec
  248. */
  249. extern DECLSPEC int SDLCALL SDL_GetNumCameraFrameSizes(SDL_CameraDevice *device, Uint32 format);
  250. /**
  251. * Get camera status
  252. *
  253. * \param device opened camera device
  254. * \returns 0 on success or a negative error code on failure; call
  255. * SDL_GetError() for more information.
  256. *
  257. * \since This function is available since SDL 3.0.0.
  258. *
  259. * \sa SDL_CameraStatus
  260. */
  261. extern DECLSPEC SDL_CameraStatus SDLCALL SDL_GetCameraStatus(SDL_CameraDevice *device);
  262. /**
  263. * Start camera
  264. *
  265. * \param device opened camera device
  266. * \returns 0 on success or a negative error code on failure; call
  267. * SDL_GetError() for more information.
  268. *
  269. * \since This function is available since SDL 3.0.0.
  270. *
  271. * \sa SDL_StopCamera
  272. */
  273. extern DECLSPEC int SDLCALL SDL_StartCamera(SDL_CameraDevice *device);
  274. /**
  275. * Acquire a frame.
  276. *
  277. * The frame is a memory pointer to the image data, whose size and format are
  278. * given by the the obtained spec.
  279. *
  280. * Non blocking API. If there is a frame available, frame->num_planes is non
  281. * 0. If frame->num_planes is 0 and returned code is 0, there is no frame at
  282. * that time.
  283. *
  284. * After used, the frame should be released with SDL_ReleaseCameraFrame
  285. *
  286. * \param device opened camera device
  287. * \param frame pointer to get the frame
  288. * \returns 0 on success or a negative error code on failure; call
  289. * SDL_GetError() for more information.
  290. *
  291. * \since This function is available since SDL 3.0.0.
  292. *
  293. * \sa SDL_ReleaseCameraFrame
  294. */
  295. extern DECLSPEC int SDLCALL SDL_AcquireCameraFrame(SDL_CameraDevice *device, SDL_CameraFrame *frame);
  296. /**
  297. * Release a frame.
  298. *
  299. * Let the back-end re-use the internal buffer for camera.
  300. *
  301. * All acquired frames should be released before closing the device.
  302. *
  303. * \param device opened camera device
  304. * \param frame frame pointer.
  305. * \returns 0 on success or a negative error code on failure; call
  306. * SDL_GetError() for more information.
  307. *
  308. * \since This function is available since SDL 3.0.0.
  309. *
  310. * \sa SDL_AcquireCameraFrame
  311. */
  312. extern DECLSPEC int SDLCALL SDL_ReleaseCameraFrame(SDL_CameraDevice *device, SDL_CameraFrame *frame);
  313. /**
  314. * Stop Video Capture
  315. *
  316. * \param device opened camera device
  317. * \returns 0 on success or a negative error code on failure; call
  318. * SDL_GetError() for more information.
  319. *
  320. * \since This function is available since SDL 3.0.0.
  321. *
  322. * \sa SDL_StartCamera
  323. */
  324. extern DECLSPEC int SDLCALL SDL_StopCamera(SDL_CameraDevice *device);
  325. /**
  326. * Use this function to shut down camera processing and close the
  327. * camera device.
  328. *
  329. * \param device opened camera device
  330. *
  331. * \since This function is available since SDL 3.0.0.
  332. *
  333. * \sa SDL_OpenCameraWithSpec
  334. * \sa SDL_OpenCamera
  335. */
  336. extern DECLSPEC void SDLCALL SDL_CloseCamera(SDL_CameraDevice *device);
  337. /* Ends C function definitions when using C++ */
  338. #ifdef __cplusplus
  339. }
  340. #endif
  341. #include <SDL3/SDL_close_code.h>
  342. #endif /* SDL_camera_h_ */