SDL_camera.h 21 KB

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