SDL_video_capture.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2023 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. * \file SDL_video_capture.h
  20. *
  21. * Video Capture for the SDL library.
  22. */
  23. #ifndef SDL_video_capture_h_
  24. #define SDL_video_capture_h_
  25. #include "SDL3/SDL_video.h"
  26. #include <SDL3/SDL_begin_code.h>
  27. /* Set up for C function definitions, even when using C++ */
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /**
  32. * This is a unique ID for a video capture device for the time it is connected to the system,
  33. * and is never reused for the lifetime of the application. If the device is
  34. * disconnected and reconnected, it will get a new ID.
  35. *
  36. * The ID value starts at 1 and increments from there. The value 0 is an invalid ID.
  37. *
  38. * \sa SDL_GetVideoCaptureDevices
  39. */
  40. typedef Uint32 SDL_VideoCaptureDeviceID;
  41. /**
  42. * The structure used to identify an SDL video capture device
  43. */
  44. struct SDL_VideoCaptureDevice;
  45. typedef struct SDL_VideoCaptureDevice SDL_VideoCaptureDevice;
  46. #define SDL_VIDEO_CAPTURE_ALLOW_ANY_CHANGE 1
  47. /**
  48. * SDL_VideoCaptureSpec structure
  49. *
  50. * Only those field can be 'desired' when configuring the device:
  51. * - format
  52. * - width
  53. * - height
  54. *
  55. * \sa SDL_GetVideoCaptureFormat
  56. * \sa SDL_GetVideoCaptureFrameSize
  57. *
  58. */
  59. typedef struct SDL_VideoCaptureSpec
  60. {
  61. Uint32 format; /**< Frame SDL_PixelFormatEnum format */
  62. int width; /**< Frame width */
  63. int height; /**< Frame height */
  64. } SDL_VideoCaptureSpec;
  65. /**
  66. * SDL Video Capture Status
  67. *
  68. * Change states but calling the function in this order:
  69. *
  70. * SDL_OpenVideoCapture()
  71. * SDL_SetVideoCaptureSpec() -> Init
  72. * SDL_StartVideoCapture() -> Playing
  73. * SDL_StopVideoCapture() -> Stopped
  74. * SDL_CloseVideoCapture()
  75. *
  76. */
  77. typedef enum
  78. {
  79. SDL_VIDEO_CAPTURE_FAIL = -1, /**< Failed */
  80. SDL_VIDEO_CAPTURE_INIT = 0, /**< Init, spec hasn't been set */
  81. SDL_VIDEO_CAPTURE_STOPPED, /**< Stopped */
  82. SDL_VIDEO_CAPTURE_PLAYING /**< Playing */
  83. } SDL_VideoCaptureStatus;
  84. /**
  85. * SDL Video Capture Status
  86. */
  87. typedef struct SDL_VideoCaptureFrame
  88. {
  89. Uint64 timestampNS; /**< Frame timestamp in nanoseconds when read from the driver */
  90. int num_planes; /**< Number of planes */
  91. Uint8 *data[3]; /**< Pointer to data of i-th plane */
  92. int pitch[3]; /**< Pitch of i-th plane */
  93. void *internal; /**< Private field */
  94. } SDL_VideoCaptureFrame;
  95. /**
  96. * Get a list of currently connected video capture devices.
  97. *
  98. * \param count a pointer filled in with the number of video capture devices
  99. * \returns a 0 terminated array of video capture instance IDs which should be
  100. * freed with SDL_free(), or NULL on error; call SDL_GetError() for
  101. * more details.
  102. *
  103. * \since This function is available since SDL 3.0.0.
  104. *
  105. * \sa SDL_OpenVideoCapture
  106. */
  107. extern DECLSPEC SDL_VideoCaptureDeviceID *SDLCALL SDL_GetVideoCaptureDevices(int *count);
  108. /**
  109. * Open a Video Capture device
  110. *
  111. * \param instance_id the video capture device instance ID
  112. * \returns device, or NULL on failure; call
  113. * SDL_GetError() for more information.
  114. *
  115. * \since This function is available since SDL 3.0.0.
  116. *
  117. * \sa SDL_GetVideoCaptureDeviceName
  118. * \sa SDL_GetVideoCaptureDevices
  119. * \sa SDL_OpenVideoCaptureWithSpec
  120. */
  121. extern DECLSPEC SDL_VideoCaptureDevice *SDLCALL SDL_OpenVideoCapture(SDL_VideoCaptureDeviceID instance_id);
  122. /**
  123. * Set specification
  124. *
  125. * \param device opened video capture device
  126. * \param desired desired video capture spec
  127. * \param obtained obtained video capture spec
  128. * \param allowed_changes allow changes or not
  129. * \returns 0 on success or a negative error code on failure; call
  130. * SDL_GetError() for more information.
  131. *
  132. * \since This function is available since SDL 3.0.0.
  133. *
  134. * \sa SDL_OpenVideoCapture
  135. * \sa SDL_OpenVideoCaptureWithSpec
  136. * \sa SDL_GetVideoCaptureSpec
  137. */
  138. extern DECLSPEC int SDLCALL SDL_SetVideoCaptureSpec(SDL_VideoCaptureDevice *device,
  139. const SDL_VideoCaptureSpec *desired,
  140. SDL_VideoCaptureSpec *obtained,
  141. int allowed_changes);
  142. /**
  143. * Open a Video Capture device and set specification
  144. *
  145. * \param instance_id the video capture device instance ID
  146. * \param desired desired video capture spec
  147. * \param obtained obtained video capture spec
  148. * \param allowed_changes allow changes or not
  149. * \returns device, or NULL on failure; call
  150. * SDL_GetError() for more information.
  151. *
  152. * \since This function is available since SDL 3.0.0.
  153. *
  154. * \sa SDL_OpenVideoCapture
  155. * \sa SDL_SetVideoCaptureSpec
  156. * \sa SDL_GetVideoCaptureSpec
  157. */
  158. extern DECLSPEC SDL_VideoCaptureDevice *SDLCALL SDL_OpenVideoCaptureWithSpec(SDL_VideoCaptureDeviceID instance_id,
  159. const SDL_VideoCaptureSpec *desired,
  160. SDL_VideoCaptureSpec *obtained,
  161. int allowed_changes);
  162. /**
  163. * Get device name
  164. *
  165. * \param instance_id the video capture device instance ID
  166. * \returns device name, shouldn't be freed
  167. *
  168. * \since This function is available since SDL 3.0.0.
  169. *
  170. * \sa SDL_GetVideoCaptureDevices
  171. */
  172. extern DECLSPEC const char * SDLCALL SDL_GetVideoCaptureDeviceName(SDL_VideoCaptureDeviceID instance_id);
  173. /**
  174. * Get the obtained video capture spec
  175. *
  176. * \param device opened video capture device
  177. * \param spec The SDL_VideoCaptureSpec to be initialized by this function.
  178. * \returns 0 on success or a negative error code on failure; call
  179. * SDL_GetError() for more information.
  180. *
  181. * \since This function is available since SDL 3.0.0.
  182. *
  183. * \sa SDL_SetVideoCaptureSpec
  184. * \sa SDL_OpenVideoCaptureWithSpec
  185. */
  186. extern DECLSPEC int SDLCALL SDL_GetVideoCaptureSpec(SDL_VideoCaptureDevice *device, SDL_VideoCaptureSpec *spec);
  187. /**
  188. * Get frame format of video capture device.
  189. * The value can be used to fill SDL_VideoCaptureSpec structure.
  190. *
  191. * \param device opened video capture device
  192. * \param index format between 0 and num -1
  193. * \param format pointer output format (SDL_PixelFormatEnum)
  194. * \returns 0 on success or a negative error code on failure; call
  195. * SDL_GetError() for more information.
  196. *
  197. * \since This function is available since SDL 3.0.0.
  198. *
  199. * \sa SDL_GetNumVideoCaptureFormats
  200. */
  201. extern DECLSPEC int SDLCALL SDL_GetVideoCaptureFormat(SDL_VideoCaptureDevice *device,
  202. int index,
  203. Uint32 *format);
  204. /**
  205. * Number of available formats for the device
  206. *
  207. * \param device opened video capture device
  208. * \returns number of formats or a negative error code on failure; call
  209. * SDL_GetError() for more information.
  210. *
  211. * \since This function is available since SDL 3.0.0.
  212. *
  213. * \sa SDL_GetVideoCaptureFormat
  214. * \sa SDL_SetVideoCaptureSpec
  215. */
  216. extern DECLSPEC int SDLCALL SDL_GetNumVideoCaptureFormats(SDL_VideoCaptureDevice *device);
  217. /**
  218. * Get frame sizes of the device and the specified input format.
  219. * The value can be used to fill SDL_VideoCaptureSpec structure.
  220. *
  221. * \param device opened video capture device
  222. * \param format a format that can be used by the device (SDL_PixelFormatEnum)
  223. * \param index framesize between 0 and num -1
  224. * \param width output width
  225. * \param height output height
  226. * \returns 0 on success or a negative error code on failure; call
  227. * SDL_GetError() for more information.
  228. *
  229. * \since This function is available since SDL 3.0.0.
  230. *
  231. * \sa SDL_GetNumVideoCaptureFrameSizes
  232. */
  233. extern DECLSPEC int SDLCALL SDL_GetVideoCaptureFrameSize(SDL_VideoCaptureDevice *device, Uint32 format, int index, int *width, int *height);
  234. /**
  235. * Number of different framesizes available for the device and pixel format.
  236. *
  237. * \param device opened video capture device
  238. * \param format frame pixel format (SDL_PixelFormatEnum)
  239. * \returns number of framesizes or a negative error code on failure; call
  240. * SDL_GetError() for more information.
  241. *
  242. * \since This function is available since SDL 3.0.0.
  243. *
  244. * \sa SDL_GetVideoCaptureFrameSize
  245. * \sa SDL_SetVideoCaptureSpec
  246. */
  247. extern DECLSPEC int SDLCALL SDL_GetNumVideoCaptureFrameSizes(SDL_VideoCaptureDevice *device, Uint32 format);
  248. /**
  249. * Get video capture status
  250. *
  251. * \param device opened video capture device
  252. * \returns 0 on success or a negative error code on failure; call
  253. * SDL_GetError() for more information.
  254. *
  255. * \since This function is available since SDL 3.0.0.
  256. *
  257. * \sa SDL_VideoCaptureStatus
  258. */
  259. extern DECLSPEC SDL_VideoCaptureStatus SDLCALL SDL_GetVideoCaptureStatus(SDL_VideoCaptureDevice *device);
  260. /**
  261. * Start video capture
  262. *
  263. * \param device opened video capture device
  264. * \returns 0 on success or a negative error code on failure; call
  265. * SDL_GetError() for more information.
  266. *
  267. * \since This function is available since SDL 3.0.0.
  268. *
  269. * \sa SDL_StopVideoCapture
  270. */
  271. extern DECLSPEC int SDLCALL SDL_StartVideoCapture(SDL_VideoCaptureDevice *device);
  272. /**
  273. * Acquire a frame.
  274. * The frame is a memory pointer to the image data, whose size and format
  275. * are given by the the obtained spec.
  276. *
  277. * Non blocking API. If there is a frame available, frame->num_planes is non 0.
  278. * If frame->num_planes is 0 and returned code is 0, there is no frame at that time.
  279. *
  280. * After used, the frame should be released with SDL_ReleaseVideoCaptureFrame
  281. *
  282. * \param device opened video capture device
  283. * \param frame pointer to get the frame
  284. * \returns 0 on success or a negative error code on failure; call
  285. * SDL_GetError() for more information.
  286. *
  287. * \since This function is available since SDL 3.0.0.
  288. *
  289. * \sa SDL_ReleaseVideoCaptureFrame
  290. */
  291. extern DECLSPEC int SDLCALL SDL_AcquireVideoCaptureFrame(SDL_VideoCaptureDevice *device, SDL_VideoCaptureFrame *frame);
  292. /**
  293. * Release a frame. Let the back-end re-use the internal buffer for video capture.
  294. *
  295. * All acquired frames should be released before closing the device.
  296. *
  297. * \param device opened video capture device
  298. * \param frame frame pointer.
  299. * \returns 0 on success or a negative error code on failure; call
  300. * SDL_GetError() for more information.
  301. *
  302. * \since This function is available since SDL 3.0.0.
  303. *
  304. * \sa SDL_AcquireVideoCaptureFrame
  305. */
  306. extern DECLSPEC int SDLCALL SDL_ReleaseVideoCaptureFrame(SDL_VideoCaptureDevice *device, SDL_VideoCaptureFrame *frame);
  307. /**
  308. * Stop Video Capture
  309. *
  310. * \param device opened video capture device
  311. * \returns 0 on success or a negative error code on failure; call
  312. * SDL_GetError() for more information.
  313. *
  314. * \since This function is available since SDL 3.0.0.
  315. *
  316. * \sa SDL_StartVideoCapture
  317. */
  318. extern DECLSPEC int SDLCALL SDL_StopVideoCapture(SDL_VideoCaptureDevice *device);
  319. /**
  320. * Use this function to shut down video_capture processing and close the video_capture device.
  321. *
  322. * \param device opened video capture device
  323. *
  324. * \since This function is available since SDL 3.0.0.
  325. *
  326. * \sa SDL_OpenVideoCaptureWithSpec
  327. * \sa SDL_OpenVideoCapture
  328. */
  329. extern DECLSPEC void SDLCALL SDL_CloseVideoCapture(SDL_VideoCaptureDevice *device);
  330. /* Ends C function definitions when using C++ */
  331. #ifdef __cplusplus
  332. }
  333. #endif
  334. #include <SDL3/SDL_close_code.h>
  335. #endif /* SDL_video_capture_h_ */