SDL_camera.h 18 KB

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