SDL_camera.h 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. * # CategoryCamera
  20. *
  21. * Video capture for the SDL library.
  22. *
  23. * This API lets apps read input from video sources, like webcams. Camera
  24. * devices can be enumerated, queried, and opened. Once opened, it will
  25. * provide SDL_Surface objects as new frames of video come in. These surfaces
  26. * can be uploaded to an SDL_Texture or processed as pixels in memory.
  27. */
  28. #ifndef SDL_camera_h_
  29. #define SDL_camera_h_
  30. #include <SDL3/SDL_stdinc.h>
  31. #include <SDL3/SDL_error.h>
  32. #include <SDL3/SDL_pixels.h>
  33. #include <SDL3/SDL_properties.h>
  34. #include <SDL3/SDL_surface.h>
  35. #include <SDL3/SDL_begin_code.h>
  36. /* Set up for C function definitions, even when using C++ */
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. /**
  41. * This is a unique ID for a camera device for the time it is connected to the
  42. * system, and is never reused for the lifetime of the application.
  43. *
  44. * If the device is disconnected and reconnected, it will get a new ID.
  45. *
  46. * The value 0 is an invalid ID.
  47. *
  48. * \since This datatype is available since SDL 3.1.3.
  49. *
  50. * \sa SDL_GetCameras
  51. */
  52. typedef Uint32 SDL_CameraID;
  53. /**
  54. * The opaque structure used to identify an opened SDL camera.
  55. *
  56. * \since This struct is available since SDL 3.1.3.
  57. */
  58. typedef struct SDL_Camera SDL_Camera;
  59. /**
  60. * The details of an output format for a camera device.
  61. *
  62. * Cameras often support multiple formats; each one will be encapsulated in
  63. * this struct.
  64. *
  65. * \since This struct is available since SDL 3.1.3.
  66. *
  67. * \sa SDL_GetCameraSupportedFormats
  68. * \sa SDL_GetCameraFormat
  69. */
  70. typedef struct SDL_CameraSpec
  71. {
  72. SDL_PixelFormat format; /**< Frame format */
  73. SDL_Colorspace colorspace; /**< Frame colorspace */
  74. int width; /**< Frame width */
  75. int height; /**< Frame height */
  76. int framerate_numerator; /**< Frame rate numerator ((num / denom) == FPS, (denom / num) == duration in seconds) */
  77. int framerate_denominator; /**< Frame rate demoninator ((num / denom) == FPS, (denom / num) == duration in seconds) */
  78. } SDL_CameraSpec;
  79. /**
  80. * The position of camera in relation to system device.
  81. *
  82. * \since This enum is available since SDL 3.1.3.
  83. *
  84. * \sa SDL_GetCameraPosition
  85. */
  86. typedef enum SDL_CameraPosition
  87. {
  88. SDL_CAMERA_POSITION_UNKNOWN,
  89. SDL_CAMERA_POSITION_FRONT_FACING,
  90. SDL_CAMERA_POSITION_BACK_FACING
  91. } SDL_CameraPosition;
  92. /**
  93. * Use this function to get the number of built-in camera drivers.
  94. *
  95. * This function returns a hardcoded number. This never returns a negative
  96. * value; if there are no drivers compiled into this build of SDL, this
  97. * function returns zero. The presence of a driver in this list does not mean
  98. * it will function, it just means SDL is capable of interacting with that
  99. * interface. For example, a build of SDL might have v4l2 support, but if
  100. * there's no kernel support available, SDL's v4l2 driver would fail if used.
  101. *
  102. * By default, SDL tries all drivers, in its preferred order, until one is
  103. * found to be usable.
  104. *
  105. * \returns the number of built-in camera drivers.
  106. *
  107. * \threadsafety It is safe to call this function from any thread.
  108. *
  109. * \since This function is available since SDL 3.1.3.
  110. *
  111. * \sa SDL_GetCameraDriver
  112. */
  113. extern SDL_DECLSPEC int SDLCALL SDL_GetNumCameraDrivers(void);
  114. /**
  115. * Use this function to get the name of a built in camera driver.
  116. *
  117. * The list of camera drivers is given in the order that they are normally
  118. * initialized by default; the drivers that seem more reasonable to choose
  119. * first (as far as the SDL developers believe) are earlier in the list.
  120. *
  121. * The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
  122. * "coremedia" or "android". These never have Unicode characters, and are not
  123. * meant to be proper names.
  124. *
  125. * \param index the index of the camera driver; the value ranges from 0 to
  126. * SDL_GetNumCameraDrivers() - 1.
  127. * \returns the name of the camera driver at the requested index, or NULL if
  128. * an invalid index was specified.
  129. *
  130. * \threadsafety It is safe to call this function from any thread.
  131. *
  132. * \since This function is available since SDL 3.1.3.
  133. *
  134. * \sa SDL_GetNumCameraDrivers
  135. */
  136. extern SDL_DECLSPEC const char * SDLCALL SDL_GetCameraDriver(int index);
  137. /**
  138. * Get the name of the current camera driver.
  139. *
  140. * The names of drivers are all simple, low-ASCII identifiers, like "v4l2",
  141. * "coremedia" or "android". These never have Unicode characters, and are not
  142. * meant to be proper names.
  143. *
  144. * \returns the name of the current camera driver or NULL if no driver has
  145. * been initialized.
  146. *
  147. * \threadsafety It is safe to call this function from any thread.
  148. *
  149. * \since This function is available since SDL 3.1.3.
  150. */
  151. extern SDL_DECLSPEC const char * SDLCALL SDL_GetCurrentCameraDriver(void);
  152. /**
  153. * Get a list of currently connected camera devices.
  154. *
  155. * \param count a pointer filled in with the number of cameras returned, may
  156. * be NULL.
  157. * \returns a 0 terminated array of camera instance IDs or NULL on failure;
  158. * call SDL_GetError() for more information. This should be freed
  159. * with SDL_free() when it is no longer needed.
  160. *
  161. * \threadsafety It is safe to call this function from any thread.
  162. *
  163. * \since This function is available since SDL 3.1.3.
  164. *
  165. * \sa SDL_OpenCamera
  166. */
  167. extern SDL_DECLSPEC SDL_CameraID * SDLCALL SDL_GetCameras(int *count);
  168. /**
  169. * Get the list of native formats/sizes a camera supports.
  170. *
  171. * This returns a list of all formats and frame sizes that a specific camera
  172. * can offer. This is useful if your app can accept a variety of image formats
  173. * and sizes and so want to find the optimal spec that doesn't require
  174. * conversion.
  175. *
  176. * This function isn't strictly required; if you call SDL_OpenCamera with a
  177. * NULL spec, SDL will choose a native format for you, and if you instead
  178. * specify a desired format, it will transparently convert to the requested
  179. * format on your behalf.
  180. *
  181. * If `count` is not NULL, it will be filled with the number of elements in
  182. * the returned array.
  183. *
  184. * Note that it's legal for a camera to supply an empty list. This is what
  185. * will happen on Emscripten builds, since that platform won't tell _anything_
  186. * about available cameras until you've opened one, and won't even tell if
  187. * there _is_ a camera until the user has given you permission to check
  188. * through a scary warning popup.
  189. *
  190. * \param devid the camera device instance ID to query.
  191. * \param count a pointer filled in with the number of elements in the list,
  192. * may be NULL.
  193. * \returns a NULL terminated array of pointers to SDL_CameraSpec or NULL on
  194. * failure; call SDL_GetError() for more information. This is a
  195. * single allocation that should be freed with SDL_free() when it is
  196. * no longer needed.
  197. *
  198. * \threadsafety It is safe to call this function from any thread.
  199. *
  200. * \since This function is available since SDL 3.1.3.
  201. *
  202. * \sa SDL_GetCameras
  203. * \sa SDL_OpenCamera
  204. */
  205. extern SDL_DECLSPEC SDL_CameraSpec ** SDLCALL SDL_GetCameraSupportedFormats(SDL_CameraID devid, int *count);
  206. /**
  207. * Get the human-readable device name for a camera.
  208. *
  209. * \param instance_id the camera device instance ID.
  210. * \returns a human-readable device name or NULL on failure; call
  211. * SDL_GetError() for more information.
  212. *
  213. * \threadsafety It is safe to call this function from any thread.
  214. *
  215. * \since This function is available since SDL 3.1.3.
  216. *
  217. * \sa SDL_GetCameras
  218. */
  219. extern SDL_DECLSPEC const char * SDLCALL SDL_GetCameraName(SDL_CameraID instance_id);
  220. /**
  221. * Get the position of the camera in relation to the system.
  222. *
  223. * Most platforms will report UNKNOWN, but mobile devices, like phones, can
  224. * often make a distinction between cameras on the front of the device (that
  225. * points towards the user, for taking "selfies") and cameras on the back (for
  226. * filming in the direction the user is facing).
  227. *
  228. * \param instance_id the camera device instance ID.
  229. * \returns the position of the camera on the system hardware.
  230. *
  231. * \threadsafety It is safe to call this function from any thread.
  232. *
  233. * \since This function is available since SDL 3.1.3.
  234. *
  235. * \sa SDL_GetCameras
  236. */
  237. extern SDL_DECLSPEC SDL_CameraPosition SDLCALL SDL_GetCameraPosition(SDL_CameraID instance_id);
  238. /**
  239. * Open a video recording device (a "camera").
  240. *
  241. * You can open the device with any reasonable spec, and if the hardware can't
  242. * directly support it, it will convert data seamlessly to the requested
  243. * format. This might incur overhead, including scaling of image data.
  244. *
  245. * If you would rather accept whatever format the device offers, you can pass
  246. * a NULL spec here and it will choose one for you (and you can use
  247. * SDL_Surface's conversion/scaling functions directly if necessary).
  248. *
  249. * You can call SDL_GetCameraFormat() to get the actual data format if passing
  250. * a NULL spec here. You can see the exact specs a device can support without
  251. * conversion with SDL_GetCameraSupportedFormats().
  252. *
  253. * SDL will not attempt to emulate framerate; it will try to set the hardware
  254. * to the rate closest to the requested speed, but it won't attempt to limit
  255. * or duplicate frames artificially; call SDL_GetCameraFormat() to see the
  256. * actual framerate of the opened the device, and check your timestamps if
  257. * this is crucial to your app!
  258. *
  259. * Note that the camera is not usable until the user approves its use! On some
  260. * platforms, the operating system will prompt the user to permit access to
  261. * the camera, and they can choose Yes or No at that point. Until they do, the
  262. * camera will not be usable. The app should either wait for an
  263. * SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event,
  264. * or poll SDL_GetCameraPermissionState() occasionally until it returns
  265. * non-zero. On platforms that don't require explicit user approval (and
  266. * perhaps in places where the user previously permitted access), the approval
  267. * event might come immediately, but it might come seconds, minutes, or hours
  268. * later!
  269. *
  270. * \param instance_id the camera device instance ID.
  271. * \param spec the desired format for data the device will provide. Can be
  272. * NULL.
  273. * \returns an SDL_Camera object or NULL on failure; call SDL_GetError() for
  274. * more information.
  275. *
  276. * \threadsafety It is safe to call this function from any thread.
  277. *
  278. * \since This function is available since SDL 3.1.3.
  279. *
  280. * \sa SDL_GetCameras
  281. * \sa SDL_GetCameraFormat
  282. */
  283. extern SDL_DECLSPEC SDL_Camera * SDLCALL SDL_OpenCamera(SDL_CameraID instance_id, const SDL_CameraSpec *spec);
  284. /**
  285. * Query if camera access has been approved by the user.
  286. *
  287. * Cameras will not function between when the device is opened by the app and
  288. * when the user permits access to the hardware. On some platforms, this
  289. * presents as a popup dialog where the user has to explicitly approve access;
  290. * on others the approval might be implicit and not alert the user at all.
  291. *
  292. * This function can be used to check the status of that approval. It will
  293. * return 0 if still waiting for user response, 1 if the camera is approved
  294. * for use, and -1 if the user denied access.
  295. *
  296. * Instead of polling with this function, you can wait for a
  297. * SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event
  298. * in the standard SDL event loop, which is guaranteed to be sent once when
  299. * permission to use the camera is decided.
  300. *
  301. * If a camera is declined, there's nothing to be done but call
  302. * SDL_CloseCamera() to dispose of it.
  303. *
  304. * \param camera the opened camera device to query.
  305. * \returns -1 if user denied access to the camera, 1 if user approved access,
  306. * 0 if no decision has been made yet.
  307. *
  308. * \threadsafety It is safe to call this function from any thread.
  309. *
  310. * \since This function is available since SDL 3.1.3.
  311. *
  312. * \sa SDL_OpenCamera
  313. * \sa SDL_CloseCamera
  314. */
  315. extern SDL_DECLSPEC int SDLCALL SDL_GetCameraPermissionState(SDL_Camera *camera);
  316. /**
  317. * Get the instance ID of an opened camera.
  318. *
  319. * \param camera an SDL_Camera to query.
  320. * \returns the instance ID of the specified camera on success or 0 on
  321. * failure; call SDL_GetError() for more information.
  322. *
  323. * \threadsafety It is safe to call this function from any thread.
  324. *
  325. * \since This function is available since SDL 3.1.3.
  326. *
  327. * \sa SDL_OpenCamera
  328. */
  329. extern SDL_DECLSPEC SDL_CameraID SDLCALL SDL_GetCameraID(SDL_Camera *camera);
  330. /**
  331. * Get the properties associated with an opened camera.
  332. *
  333. * \param camera the SDL_Camera obtained from SDL_OpenCamera().
  334. * \returns a valid property ID on success or 0 on failure; call
  335. * SDL_GetError() for more information.
  336. *
  337. * \threadsafety It is safe to call this function from any thread.
  338. *
  339. * \since This function is available since SDL 3.1.3.
  340. */
  341. extern SDL_DECLSPEC SDL_PropertiesID SDLCALL SDL_GetCameraProperties(SDL_Camera *camera);
  342. /**
  343. * Get the spec that a camera is using when generating images.
  344. *
  345. * Note that this might not be the native format of the hardware, as SDL might
  346. * be converting to this format behind the scenes.
  347. *
  348. * If the system is waiting for the user to approve access to the camera, as
  349. * some platforms require, this will return false, but this isn't necessarily
  350. * a fatal error; you should either wait for an
  351. * SDL_EVENT_CAMERA_DEVICE_APPROVED (or SDL_EVENT_CAMERA_DEVICE_DENIED) event,
  352. * or poll SDL_GetCameraPermissionState() occasionally until it returns
  353. * non-zero.
  354. *
  355. * \param camera opened camera device.
  356. * \param spec the SDL_CameraSpec to be initialized by this function.
  357. * \returns true on success or false on failure; call SDL_GetError() for more
  358. * information.
  359. *
  360. * \threadsafety It is safe to call this function from any thread.
  361. *
  362. * \since This function is available since SDL 3.1.3.
  363. *
  364. * \sa SDL_OpenCamera
  365. */
  366. extern SDL_DECLSPEC bool SDLCALL SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec);
  367. /**
  368. * Acquire a frame.
  369. *
  370. * The frame is a memory pointer to the image data, whose size and format are
  371. * given by the spec requested when opening the device.
  372. *
  373. * This is a non blocking API. If there is a frame available, a non-NULL
  374. * surface is returned, and timestampNS will be filled with a non-zero value.
  375. *
  376. * Note that an error case can also return NULL, but a NULL by itself is
  377. * normal and just signifies that a new frame is not yet available. Note that
  378. * even if a camera device fails outright (a USB camera is unplugged while in
  379. * use, etc), SDL will send an event separately to notify the app, but
  380. * continue to provide blank frames at ongoing intervals until
  381. * SDL_CloseCamera() is called, so real failure here is almost always an out
  382. * of memory condition.
  383. *
  384. * After use, the frame should be released with SDL_ReleaseCameraFrame(). If
  385. * you don't do this, the system may stop providing more video!
  386. *
  387. * Do not call SDL_FreeSurface() on the returned surface! It must be given
  388. * back to the camera subsystem with SDL_ReleaseCameraFrame!
  389. *
  390. * If the system is waiting for the user to approve access to the camera, as
  391. * some platforms require, this will return NULL (no frames available); you
  392. * should either wait for an SDL_EVENT_CAMERA_DEVICE_APPROVED (or
  393. * SDL_EVENT_CAMERA_DEVICE_DENIED) event, or poll
  394. * SDL_GetCameraPermissionState() occasionally until it returns non-zero.
  395. *
  396. * \param camera opened camera device.
  397. * \param timestampNS a pointer filled in with the frame's timestamp, or 0 on
  398. * error. Can be NULL.
  399. * \returns a new frame of video on success, NULL if none is currently
  400. * available.
  401. *
  402. * \threadsafety It is safe to call this function from any thread.
  403. *
  404. * \since This function is available since SDL 3.1.3.
  405. *
  406. * \sa SDL_ReleaseCameraFrame
  407. */
  408. extern SDL_DECLSPEC SDL_Surface * SDLCALL SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS);
  409. /**
  410. * Release a frame of video acquired from a camera.
  411. *
  412. * Let the back-end re-use the internal buffer for camera.
  413. *
  414. * This function _must_ be called only on surface objects returned by
  415. * SDL_AcquireCameraFrame(). This function should be called as quickly as
  416. * possible after acquisition, as SDL keeps a small FIFO queue of surfaces for
  417. * video frames; if surfaces aren't released in a timely manner, SDL may drop
  418. * upcoming video frames from the camera.
  419. *
  420. * If the app needs to keep the surface for a significant time, they should
  421. * make a copy of it and release the original.
  422. *
  423. * The app should not use the surface again after calling this function;
  424. * assume the surface is freed and the pointer is invalid.
  425. *
  426. * \param camera opened camera device.
  427. * \param frame the video frame surface to release.
  428. *
  429. * \threadsafety It is safe to call this function from any thread.
  430. *
  431. * \since This function is available since SDL 3.1.3.
  432. *
  433. * \sa SDL_AcquireCameraFrame
  434. */
  435. extern SDL_DECLSPEC void SDLCALL SDL_ReleaseCameraFrame(SDL_Camera *camera, SDL_Surface *frame);
  436. /**
  437. * Use this function to shut down camera processing and close the camera
  438. * device.
  439. *
  440. * \param camera opened camera device.
  441. *
  442. * \threadsafety It is safe to call this function from any thread, but no
  443. * thread may reference `device` once this function is called.
  444. *
  445. * \since This function is available since SDL 3.1.3.
  446. *
  447. * \sa SDL_OpenCameraWithSpec
  448. * \sa SDL_OpenCamera
  449. */
  450. extern SDL_DECLSPEC void SDLCALL SDL_CloseCamera(SDL_Camera *camera);
  451. /* Ends C function definitions when using C++ */
  452. #ifdef __cplusplus
  453. }
  454. #endif
  455. #include <SDL3/SDL_close_code.h>
  456. #endif /* SDL_camera_h_ */