SDL_camera.h 19 KB

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