SDL_camera.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. * Use this function to get the number of built-in camera drivers.
  97. *
  98. * This function returns a hardcoded number. This never returns a negative
  99. * value; if there are no drivers compiled into this build of SDL, this
  100. * function returns zero. The presence of a driver in this list does not mean
  101. * it will function, it just means SDL is capable of interacting with that
  102. * interface. For example, a build of SDL might have v4l2 support, but if
  103. * there's no kernel support available, SDL's v4l2 driver would fail if used.
  104. *
  105. * By default, SDL tries all drivers, in its preferred order, until one is
  106. * found to be usable.
  107. *
  108. * \returns the number of built-in camera drivers.
  109. *
  110. * \threadsafety It is safe to call this function from any thread.
  111. *
  112. * \since This function is available since SDL 3.0.0.
  113. *
  114. * \sa SDL_GetCameraDriver
  115. */
  116. extern DECLSPEC int SDLCALL SDL_GetNumCameraDrivers(void);
  117. /**
  118. * Use this function to get the name of a built in camera driver.
  119. *
  120. * The list of camera drivers is given in the order that they are normally
  121. * initialized by default; the drivers that seem more reasonable to choose
  122. * first (as far as the SDL developers believe) are earlier in the list.
  123. *
  124. * The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
  125. * "coremedia" or "android". These never have Unicode characters, and are not
  126. * meant to be proper names.
  127. *
  128. * \param index the index of the camera driver; the value ranges from 0 to
  129. * SDL_GetNumCameraDrivers() - 1
  130. * \returns the name of the camera driver at the requested index, or NULL if an
  131. * invalid index was specified.
  132. *
  133. * \threadsafety It is safe to call this function from any thread.
  134. *
  135. * \since This function is available since SDL 3.0.0.
  136. *
  137. * \sa SDL_GetNumCameraDrivers
  138. */
  139. extern DECLSPEC const char *SDLCALL SDL_GetCameraDriver(int index);
  140. /**
  141. * Get the name of the current camera driver.
  142. *
  143. * The returned string points to internal static memory and thus never becomes
  144. * invalid, even if you quit the camera subsystem and initialize a new driver
  145. * (although such a case would return a different static string from another
  146. * call to this function, of course). As such, you should not modify or free
  147. * the returned string.
  148. *
  149. * \returns the name of the current camera driver or NULL if no driver has been
  150. * initialized.
  151. *
  152. * \threadsafety It is safe to call this function from any thread.
  153. *
  154. * \since This function is available since SDL 3.0.0.
  155. */
  156. extern DECLSPEC const char *SDLCALL SDL_GetCurrentCameraDriver(void);
  157. /**
  158. * Get a list of currently connected camera devices.
  159. *
  160. * \param count a pointer filled in with the number of camera devices
  161. * \returns a 0 terminated array of camera instance IDs which should be
  162. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  163. * more details.
  164. *
  165. * \since This function is available since SDL 3.0.0.
  166. *
  167. * \sa SDL_OpenCamera
  168. */
  169. extern DECLSPEC SDL_CameraDeviceID *SDLCALL SDL_GetCameraDevices(int *count);
  170. /**
  171. * Open a Video Capture device
  172. *
  173. * \param instance_id the camera device instance ID
  174. * \returns device, or NULL on failure; call SDL_GetError() for more
  175. * information.
  176. *
  177. * \since This function is available since SDL 3.0.0.
  178. *
  179. * \sa SDL_GetCameraDeviceName
  180. * \sa SDL_GetCameraDevices
  181. * \sa SDL_OpenCameraWithSpec
  182. */
  183. extern DECLSPEC SDL_CameraDevice *SDLCALL SDL_OpenCamera(SDL_CameraDeviceID instance_id);
  184. /**
  185. * Set specification
  186. *
  187. * \param device opened camera device
  188. * \param desired desired camera spec
  189. * \param obtained obtained camera spec
  190. * \param allowed_changes allow changes or not
  191. * \returns 0 on success or a negative error code on failure; call
  192. * SDL_GetError() for more information.
  193. *
  194. * \since This function is available since SDL 3.0.0.
  195. *
  196. * \sa SDL_OpenCamera
  197. * \sa SDL_OpenCameraWithSpec
  198. * \sa SDL_GetCameraSpec
  199. */
  200. extern DECLSPEC int SDLCALL SDL_SetCameraSpec(SDL_CameraDevice *device,
  201. const SDL_CameraSpec *desired,
  202. SDL_CameraSpec *obtained,
  203. int allowed_changes);
  204. /**
  205. * Open a Video Capture device and set specification
  206. *
  207. * \param instance_id the camera device instance ID
  208. * \param desired desired camera spec
  209. * \param obtained obtained camera spec
  210. * \param allowed_changes allow changes or not
  211. * \returns device, or NULL on failure; call SDL_GetError() for more
  212. * information.
  213. *
  214. * \since This function is available since SDL 3.0.0.
  215. *
  216. * \sa SDL_OpenCamera
  217. * \sa SDL_SetCameraSpec
  218. * \sa SDL_GetCameraSpec
  219. */
  220. extern DECLSPEC SDL_CameraDevice *SDLCALL SDL_OpenCameraWithSpec(SDL_CameraDeviceID instance_id,
  221. const SDL_CameraSpec *desired,
  222. SDL_CameraSpec *obtained,
  223. int allowed_changes);
  224. /**
  225. * Get device name
  226. *
  227. * \param instance_id the camera device instance ID
  228. * \returns device name, shouldn't be freed
  229. *
  230. * \since This function is available since SDL 3.0.0.
  231. *
  232. * \sa SDL_GetCameraDevices
  233. */
  234. extern DECLSPEC const char * SDLCALL SDL_GetCameraDeviceName(SDL_CameraDeviceID instance_id);
  235. /**
  236. * Get the obtained camera spec
  237. *
  238. * \param device opened camera device
  239. * \param spec The SDL_CameraSpec to be initialized by this function.
  240. * \returns 0 on success or a negative error code on failure; call
  241. * SDL_GetError() for more information.
  242. *
  243. * \since This function is available since SDL 3.0.0.
  244. *
  245. * \sa SDL_SetCameraSpec
  246. * \sa SDL_OpenCameraWithSpec
  247. */
  248. extern DECLSPEC int SDLCALL SDL_GetCameraSpec(SDL_CameraDevice *device, SDL_CameraSpec *spec);
  249. /**
  250. * Get frame format of camera device.
  251. *
  252. * The value can be used to fill SDL_CameraSpec structure.
  253. *
  254. * \param device opened camera device
  255. * \param index format between 0 and num -1
  256. * \param format pointer output format (SDL_PixelFormatEnum)
  257. * \returns 0 on success or a negative error code on failure; call
  258. * SDL_GetError() for more information.
  259. *
  260. * \since This function is available since SDL 3.0.0.
  261. *
  262. * \sa SDL_GetNumCameraFormats
  263. */
  264. extern DECLSPEC int SDLCALL SDL_GetCameraFormat(SDL_CameraDevice *device,
  265. int index,
  266. Uint32 *format);
  267. /**
  268. * Number of available formats for the device
  269. *
  270. * \param device opened camera device
  271. * \returns number of formats or a negative error code on failure; call
  272. * SDL_GetError() for more information.
  273. *
  274. * \since This function is available since SDL 3.0.0.
  275. *
  276. * \sa SDL_GetCameraFormat
  277. * \sa SDL_SetCameraSpec
  278. */
  279. extern DECLSPEC int SDLCALL SDL_GetNumCameraFormats(SDL_CameraDevice *device);
  280. /**
  281. * Get frame sizes of the device and the specified input format.
  282. *
  283. * The value can be used to fill SDL_CameraSpec structure.
  284. *
  285. * \param device opened camera device
  286. * \param format a format that can be used by the device (SDL_PixelFormatEnum)
  287. * \param index framesize between 0 and num -1
  288. * \param width output width
  289. * \param height output height
  290. * \returns 0 on success or a negative error code on failure; call
  291. * SDL_GetError() for more information.
  292. *
  293. * \since This function is available since SDL 3.0.0.
  294. *
  295. * \sa SDL_GetNumCameraFrameSizes
  296. */
  297. extern DECLSPEC int SDLCALL SDL_GetCameraFrameSize(SDL_CameraDevice *device, Uint32 format, int index, int *width, int *height);
  298. /**
  299. * Number of different framesizes available for the device and pixel format.
  300. *
  301. * \param device opened camera device
  302. * \param format frame pixel format (SDL_PixelFormatEnum)
  303. * \returns number of framesizes or a negative error code on failure; call
  304. * SDL_GetError() for more information.
  305. *
  306. * \since This function is available since SDL 3.0.0.
  307. *
  308. * \sa SDL_GetCameraFrameSize
  309. * \sa SDL_SetCameraSpec
  310. */
  311. extern DECLSPEC int SDLCALL SDL_GetNumCameraFrameSizes(SDL_CameraDevice *device, Uint32 format);
  312. /**
  313. * Get camera status
  314. *
  315. * \param device opened camera device
  316. * \returns 0 on success or a negative error code on failure; call
  317. * SDL_GetError() for more information.
  318. *
  319. * \since This function is available since SDL 3.0.0.
  320. *
  321. * \sa SDL_CameraStatus
  322. */
  323. extern DECLSPEC SDL_CameraStatus SDLCALL SDL_GetCameraStatus(SDL_CameraDevice *device);
  324. /**
  325. * Start camera
  326. *
  327. * \param device opened camera device
  328. * \returns 0 on success or a negative error code on failure; call
  329. * SDL_GetError() for more information.
  330. *
  331. * \since This function is available since SDL 3.0.0.
  332. *
  333. * \sa SDL_StopCamera
  334. */
  335. extern DECLSPEC int SDLCALL SDL_StartCamera(SDL_CameraDevice *device);
  336. /**
  337. * Acquire a frame.
  338. *
  339. * The frame is a memory pointer to the image data, whose size and format are
  340. * given by the the obtained spec.
  341. *
  342. * Non blocking API. If there is a frame available, frame->num_planes is non
  343. * 0. If frame->num_planes is 0 and returned code is 0, there is no frame at
  344. * that time.
  345. *
  346. * After used, the frame should be released with SDL_ReleaseCameraFrame
  347. *
  348. * \param device opened camera device
  349. * \param frame pointer to get the frame
  350. * \returns 0 on success or a negative error code on failure; call
  351. * SDL_GetError() for more information.
  352. *
  353. * \since This function is available since SDL 3.0.0.
  354. *
  355. * \sa SDL_ReleaseCameraFrame
  356. */
  357. extern DECLSPEC int SDLCALL SDL_AcquireCameraFrame(SDL_CameraDevice *device, SDL_CameraFrame *frame);
  358. /**
  359. * Release a frame.
  360. *
  361. * Let the back-end re-use the internal buffer for camera.
  362. *
  363. * All acquired frames should be released before closing the device.
  364. *
  365. * \param device opened camera device
  366. * \param frame frame pointer.
  367. * \returns 0 on success or a negative error code on failure; call
  368. * SDL_GetError() for more information.
  369. *
  370. * \since This function is available since SDL 3.0.0.
  371. *
  372. * \sa SDL_AcquireCameraFrame
  373. */
  374. extern DECLSPEC int SDLCALL SDL_ReleaseCameraFrame(SDL_CameraDevice *device, SDL_CameraFrame *frame);
  375. /**
  376. * Stop Video Capture
  377. *
  378. * \param device opened camera device
  379. * \returns 0 on success or a negative error code on failure; call
  380. * SDL_GetError() for more information.
  381. *
  382. * \since This function is available since SDL 3.0.0.
  383. *
  384. * \sa SDL_StartCamera
  385. */
  386. extern DECLSPEC int SDLCALL SDL_StopCamera(SDL_CameraDevice *device);
  387. /**
  388. * Use this function to shut down camera processing and close the
  389. * camera device.
  390. *
  391. * \param device opened camera device
  392. *
  393. * \since This function is available since SDL 3.0.0.
  394. *
  395. * \sa SDL_OpenCameraWithSpec
  396. * \sa SDL_OpenCamera
  397. */
  398. extern DECLSPEC void SDLCALL SDL_CloseCamera(SDL_CameraDevice *device);
  399. /* Ends C function definitions when using C++ */
  400. #ifdef __cplusplus
  401. }
  402. #endif
  403. #include <SDL3/SDL_close_code.h>
  404. #endif /* SDL_camera_h_ */