SDL_camera.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 Sam Lantinga <[email protected]>
  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 opened SDL camera
  43. */
  44. struct SDL_Camera;
  45. typedef struct SDL_Camera SDL_Camera;
  46. /**
  47. * SDL_CameraSpec structure
  48. *
  49. * \sa SDL_GetCameraDeviceSupportedSpecs
  50. * \sa SDL_GetCameraSpec
  51. *
  52. */
  53. typedef struct SDL_CameraSpec
  54. {
  55. Uint32 format; /**< Frame SDL_PixelFormatEnum format */
  56. int width; /**< Frame width */
  57. int height; /**< Frame height */
  58. } SDL_CameraSpec;
  59. /**
  60. * Use this function to get the number of built-in camera drivers.
  61. *
  62. * This function returns a hardcoded number. This never returns a negative
  63. * value; if there are no drivers compiled into this build of SDL, this
  64. * function returns zero. The presence of a driver in this list does not mean
  65. * it will function, it just means SDL is capable of interacting with that
  66. * interface. For example, a build of SDL might have v4l2 support, but if
  67. * there's no kernel support available, SDL's v4l2 driver would fail if used.
  68. *
  69. * By default, SDL tries all drivers, in its preferred order, until one is
  70. * found to be usable.
  71. *
  72. * \returns the number of built-in camera drivers.
  73. *
  74. * \threadsafety It is safe to call this function from any thread.
  75. *
  76. * \since This function is available since SDL 3.0.0.
  77. *
  78. * \sa SDL_GetCameraDriver
  79. */
  80. extern DECLSPEC int SDLCALL SDL_GetNumCameraDrivers(void);
  81. /**
  82. * Use this function to get the name of a built in camera driver.
  83. *
  84. * The list of camera drivers is given in the order that they are normally
  85. * initialized by default; the drivers that seem more reasonable to choose
  86. * first (as far as the SDL developers believe) are earlier in the list.
  87. *
  88. * The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
  89. * "coremedia" or "android". These never have Unicode characters, and are not
  90. * meant to be proper names.
  91. *
  92. * \param index the index of the camera driver; the value ranges from 0 to
  93. * SDL_GetNumCameraDrivers() - 1
  94. * \returns the name of the camera driver at the requested index, or NULL if an
  95. * invalid index was specified.
  96. *
  97. * \threadsafety It is safe to call this function from any thread.
  98. *
  99. * \since This function is available since SDL 3.0.0.
  100. *
  101. * \sa SDL_GetNumCameraDrivers
  102. */
  103. extern DECLSPEC const char *SDLCALL SDL_GetCameraDriver(int index);
  104. /**
  105. * Get the name of the current camera driver.
  106. *
  107. * The returned string points to internal static memory and thus never becomes
  108. * invalid, even if you quit the camera subsystem and initialize a new driver
  109. * (although such a case would return a different static string from another
  110. * call to this function, of course). As such, you should not modify or free
  111. * the returned string.
  112. *
  113. * \returns the name of the current camera driver or NULL if no driver has been
  114. * initialized.
  115. *
  116. * \threadsafety It is safe to call this function from any thread.
  117. *
  118. * \since This function is available since SDL 3.0.0.
  119. */
  120. extern DECLSPEC const char *SDLCALL SDL_GetCurrentCameraDriver(void);
  121. /**
  122. * Get a list of currently connected camera devices.
  123. *
  124. * \param count a pointer filled in with the number of camera devices. Can be NULL.
  125. * \returns a 0 terminated array of camera instance IDs which should be
  126. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  127. * more details.
  128. *
  129. * \threadsafety It is safe to call this function from any thread.
  130. *
  131. * \since This function is available since SDL 3.0.0.
  132. *
  133. * \sa SDL_OpenCamera
  134. */
  135. extern DECLSPEC SDL_CameraDeviceID *SDLCALL SDL_GetCameraDevices(int *count);
  136. /**
  137. * Get the list of native formats/sizes a camera supports.
  138. *
  139. * This returns a list of all formats and frame sizes that a specific
  140. * camera can offer. This is useful if your app can accept a variety
  141. * of image formats and sizes and so want to find the optimal spec
  142. * that doesn't require conversion.
  143. *
  144. * This function isn't strictly required; if you call SDL_OpenCameraDevice
  145. * with a NULL spec, SDL will choose a native format for you, and if you
  146. * instead specify a desired format, it will transparently convert to the
  147. * requested format on your behalf.
  148. *
  149. * If `count` is not NULL, it will be filled with the number of elements
  150. * in the returned array. Additionally, the last element of the array
  151. * has all fields set to zero (this element is not included in `count`).
  152. *
  153. * The returned list is owned by the caller, and should be released with
  154. * SDL_free() when no longer needed.
  155. *
  156. * \param devid the camera device instance ID to query.
  157. * \param count a pointer filled in with the number of elements in the list. Can be NULL.
  158. * \returns a 0 terminated array of SDL_CameraSpecs, which should be
  159. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  160. * more details.
  161. *
  162. * \threadsafety It is safe to call this function from any thread.
  163. *
  164. * \since This function is available since SDL 3.0.0.
  165. *
  166. * \sa SDL_GetCameraDevices
  167. * \sa SDL_OpenCameraDevice
  168. */
  169. extern DECLSPEC SDL_CameraSpec *SDLCALL SDL_GetCameraDeviceSupportedSpecs(SDL_CameraDeviceID devid, int *count);
  170. /**
  171. * Get human-readable device name for a camera.
  172. *
  173. * The returned string is owned by the caller; please release it with
  174. * SDL_free() when done with it.
  175. *
  176. * \param instance_id the camera device instance ID
  177. * \returns Human-readable device name, or NULL on error; call SDL_GetError() for more information.
  178. *
  179. * \threadsafety It is safe to call this function from any thread.
  180. *
  181. * \since This function is available since SDL 3.0.0.
  182. *
  183. * \sa SDL_GetCameraDevices
  184. */
  185. extern DECLSPEC char * SDLCALL SDL_GetCameraDeviceName(SDL_CameraDeviceID instance_id);
  186. /**
  187. * Open a video capture device (a "camera").
  188. *
  189. * You can open the device with any reasonable spec, and if the hardware can't
  190. * directly support it, it will convert data seamlessly to the requested
  191. * format. This might incur overhead, including scaling of image data.
  192. *
  193. * If you would rather accept whatever format the device offers, you can
  194. * pass a NULL spec here and it will choose one for you (and you can use
  195. * SDL_Surface's conversion/scaling functions directly if necessary).
  196. *
  197. * You can call SDL_GetCameraSpec() to get the actual data format if
  198. * passing a NULL spec here. You can see the exact specs a device can
  199. * support without conversion with SDL_GetCameraSupportedSpecs().
  200. *
  201. * \param instance_id the camera device instance ID
  202. * \param spec The desired format for data the device will provide. Can be NULL.
  203. * \returns device, or NULL on failure; call SDL_GetError() for more
  204. * information.
  205. *
  206. * \threadsafety It is safe to call this function from any thread.
  207. *
  208. * \since This function is available since SDL 3.0.0.
  209. *
  210. * \sa SDL_GetCameraDeviceName
  211. * \sa SDL_GetCameraDevices
  212. * \sa SDL_OpenCameraWithSpec
  213. */
  214. extern DECLSPEC SDL_Camera *SDLCALL SDL_OpenCameraDevice(SDL_CameraDeviceID instance_id, const SDL_CameraSpec *spec);
  215. /**
  216. * Get the instance ID of an opened camera.
  217. *
  218. * \param device an SDL_Camera to query
  219. * \returns the instance ID of the specified camera on success or 0 on
  220. * failure; call SDL_GetError() for more information.
  221. *
  222. * \threadsafety It is safe to call this function from any thread.
  223. *
  224. * \since This function is available since SDL 3.0.0.
  225. *
  226. * \sa SDL_OpenCameraDevice
  227. */
  228. extern DECLSPEC SDL_CameraDeviceID SDLCALL SDL_GetCameraInstanceID(SDL_Camera *camera);
  229. /**
  230. * Get the properties associated with an opened camera.
  231. *
  232. * \param device the SDL_Camera obtained from SDL_OpenCameraDevice()
  233. * \returns a valid property ID on success or 0 on failure; call
  234. * SDL_GetError() for more information.
  235. *
  236. * \threadsafety It is safe to call this function from any thread.
  237. *
  238. * \since This function is available since SDL 3.0.0.
  239. *
  240. * \sa SDL_GetProperty
  241. * \sa SDL_SetProperty
  242. */
  243. extern DECLSPEC SDL_PropertiesID SDLCALL SDL_GetCameraProperties(SDL_Camera *camera);
  244. /**
  245. * Get the spec that a camera is using when generating images.
  246. *
  247. * Note that this might not be the native format of the hardware, as SDL
  248. * might be converting to this format behind the scenes.
  249. *
  250. * \param device opened camera device
  251. * \param spec The SDL_CameraSpec to be initialized by this function.
  252. * \returns 0 on success or a negative error code on failure; call
  253. * SDL_GetError() for more information.
  254. *
  255. * \threadsafety It is safe to call this function from any thread.
  256. *
  257. * \since This function is available since SDL 3.0.0.
  258. *
  259. * \sa SDL_OpenCameraDevice
  260. */
  261. extern DECLSPEC int SDLCALL SDL_GetCameraSpec(SDL_Camera *camera, SDL_CameraSpec *spec);
  262. /**
  263. * Acquire a frame.
  264. *
  265. * The frame is a memory pointer to the image data, whose size and format are
  266. * given by the spec requested when opening the device.
  267. *
  268. * This is a non blocking API. If there is a frame available, a non-NULL surface is
  269. * returned, and timestampNS will be filled with a non-zero value.
  270. *
  271. * Note that an error case can also return NULL, but a NULL by itself is normal
  272. * and just signifies that a new frame is not yet available. Note that even if a
  273. * camera device fails outright (a USB camera is unplugged while in use, etc), SDL
  274. * will send an event separately to notify the app, but continue to provide blank
  275. * frames at ongoing intervals until SDL_CloseCamera() is called, so real
  276. * failure here is almost always an out of memory condition.
  277. *
  278. * After use, the frame should be released with SDL_ReleaseCameraFrame(). If you
  279. * don't do this, the system may stop providing more video! If the hardware is
  280. * using DMA to write directly into memory, frames held too long may be overwritten
  281. * with new data.
  282. *
  283. * Do not call SDL_FreeSurface() on the returned surface! It must be given back
  284. * to the camera subsystem with SDL_ReleaseCameraFrame!
  285. *
  286. * \param device opened camera device
  287. * \param timestampNS a pointer filled in with the frame's timestamp, or 0 on error. Can be NULL.
  288. * \returns A new frame of video on success, NULL if none is currently available.
  289. *
  290. * \threadsafety It is safe to call this function from any thread.
  291. *
  292. * \since This function is available since SDL 3.0.0.
  293. *
  294. * \sa SDL_ReleaseCameraFrame
  295. */
  296. extern DECLSPEC SDL_Surface * SDLCALL SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS);
  297. /**
  298. * Release a frame of video acquired from a camera.
  299. *
  300. * Let the back-end re-use the internal buffer for camera.
  301. *
  302. * This function _must_ be called only on surface objects returned by
  303. * SDL_AcquireCameraFrame(). This function should be called as quickly as
  304. * possible after acquisition, as SDL keeps a small FIFO queue of surfaces
  305. * for video frames; if surfaces aren't released in a timely manner, SDL
  306. * may drop upcoming video frames from the camera.
  307. *
  308. * If the app needs to keep the surface for a significant time, they should
  309. * make a copy of it and release the original.
  310. *
  311. * The app should not use the surface again after calling this function;
  312. * assume the surface is freed and the pointer is invalid.
  313. *
  314. * \param device opened camera device
  315. * \param frame The video frame surface to release.
  316. * \returns 0 on success or a negative error code on failure; call
  317. * SDL_GetError() for more information.
  318. *
  319. * \threadsafety It is safe to call this function from any thread.
  320. *
  321. * \since This function is available since SDL 3.0.0.
  322. *
  323. * \sa SDL_AcquireCameraFrame
  324. */
  325. extern DECLSPEC int SDLCALL SDL_ReleaseCameraFrame(SDL_Camera *camera, SDL_Surface *frame);
  326. /**
  327. * Use this function to shut down camera processing and close the
  328. * camera device.
  329. *
  330. * \param device opened camera device
  331. *
  332. * \threadsafety It is safe to call this function from any thread, but
  333. * no thread may reference `device` once this function
  334. * is called.
  335. *
  336. * \since This function is available since SDL 3.0.0.
  337. *
  338. * \sa SDL_OpenCameraWithSpec
  339. * \sa SDL_OpenCamera
  340. */
  341. extern DECLSPEC void SDLCALL SDL_CloseCamera(SDL_Camera *camera);
  342. /* Ends C function definitions when using C++ */
  343. #ifdef __cplusplus
  344. }
  345. #endif
  346. #include <SDL3/SDL_close_code.h>
  347. #endif /* SDL_camera_h_ */