SDL_video_capture.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377
  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_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 SDL_GetError() for more
  113. * 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 SDL_GetError() for more
  150. * 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. *
  190. * The value can be used to fill SDL_VideoCaptureSpec structure.
  191. *
  192. * \param device opened video capture device
  193. * \param index format between 0 and num -1
  194. * \param format pointer output format (SDL_PixelFormatEnum)
  195. * \returns 0 on success or a negative error code on failure; call
  196. * SDL_GetError() for more information.
  197. *
  198. * \since This function is available since SDL 3.0.0.
  199. *
  200. * \sa SDL_GetNumVideoCaptureFormats
  201. */
  202. extern DECLSPEC int SDLCALL SDL_GetVideoCaptureFormat(SDL_VideoCaptureDevice *device,
  203. int index,
  204. Uint32 *format);
  205. /**
  206. * Number of available formats for the device
  207. *
  208. * \param device opened video capture device
  209. * \returns number of formats or a negative error code on failure; call
  210. * SDL_GetError() for more information.
  211. *
  212. * \since This function is available since SDL 3.0.0.
  213. *
  214. * \sa SDL_GetVideoCaptureFormat
  215. * \sa SDL_SetVideoCaptureSpec
  216. */
  217. extern DECLSPEC int SDLCALL SDL_GetNumVideoCaptureFormats(SDL_VideoCaptureDevice *device);
  218. /**
  219. * Get frame sizes of the device and the specified input format.
  220. *
  221. * The value can be used to fill SDL_VideoCaptureSpec structure.
  222. *
  223. * \param device opened video capture device
  224. * \param format a format that can be used by the device (SDL_PixelFormatEnum)
  225. * \param index framesize between 0 and num -1
  226. * \param width output width
  227. * \param height output height
  228. * \returns 0 on success or a negative error code on failure; call
  229. * SDL_GetError() for more information.
  230. *
  231. * \since This function is available since SDL 3.0.0.
  232. *
  233. * \sa SDL_GetNumVideoCaptureFrameSizes
  234. */
  235. extern DECLSPEC int SDLCALL SDL_GetVideoCaptureFrameSize(SDL_VideoCaptureDevice *device, Uint32 format, int index, int *width, int *height);
  236. /**
  237. * Number of different framesizes available for the device and pixel format.
  238. *
  239. * \param device opened video capture device
  240. * \param format frame pixel format (SDL_PixelFormatEnum)
  241. * \returns number of framesizes or a negative error code on failure; call
  242. * SDL_GetError() for more information.
  243. *
  244. * \since This function is available since SDL 3.0.0.
  245. *
  246. * \sa SDL_GetVideoCaptureFrameSize
  247. * \sa SDL_SetVideoCaptureSpec
  248. */
  249. extern DECLSPEC int SDLCALL SDL_GetNumVideoCaptureFrameSizes(SDL_VideoCaptureDevice *device, Uint32 format);
  250. /**
  251. * Get video capture status
  252. *
  253. * \param device opened video capture device
  254. * \returns 0 on success or a negative error code on failure; call
  255. * SDL_GetError() for more information.
  256. *
  257. * \since This function is available since SDL 3.0.0.
  258. *
  259. * \sa SDL_VideoCaptureStatus
  260. */
  261. extern DECLSPEC SDL_VideoCaptureStatus SDLCALL SDL_GetVideoCaptureStatus(SDL_VideoCaptureDevice *device);
  262. /**
  263. * Start video capture
  264. *
  265. * \param device opened video capture device
  266. * \returns 0 on success or a negative error code on failure; call
  267. * SDL_GetError() for more information.
  268. *
  269. * \since This function is available since SDL 3.0.0.
  270. *
  271. * \sa SDL_StopVideoCapture
  272. */
  273. extern DECLSPEC int SDLCALL SDL_StartVideoCapture(SDL_VideoCaptureDevice *device);
  274. /**
  275. * Acquire a frame.
  276. *
  277. * The frame is a memory pointer to the image data, whose size and format are
  278. * given by the the obtained spec.
  279. *
  280. * Non blocking API. If there is a frame available, frame->num_planes is non
  281. * 0. If frame->num_planes is 0 and returned code is 0, there is no frame at
  282. * that time.
  283. *
  284. * After used, the frame should be released with SDL_ReleaseVideoCaptureFrame
  285. *
  286. * \param device opened video capture device
  287. * \param frame pointer to get the frame
  288. * \returns 0 on success or a negative error code on failure; call
  289. * SDL_GetError() for more information.
  290. *
  291. * \since This function is available since SDL 3.0.0.
  292. *
  293. * \sa SDL_ReleaseVideoCaptureFrame
  294. */
  295. extern DECLSPEC int SDLCALL SDL_AcquireVideoCaptureFrame(SDL_VideoCaptureDevice *device, SDL_VideoCaptureFrame *frame);
  296. /**
  297. * Release a frame.
  298. *
  299. * Let the back-end re-use the internal buffer for video capture.
  300. *
  301. * All acquired frames should be released before closing the device.
  302. *
  303. * \param device opened video capture device
  304. * \param frame frame pointer.
  305. * \returns 0 on success or a negative error code on failure; call
  306. * SDL_GetError() for more information.
  307. *
  308. * \since This function is available since SDL 3.0.0.
  309. *
  310. * \sa SDL_AcquireVideoCaptureFrame
  311. */
  312. extern DECLSPEC int SDLCALL SDL_ReleaseVideoCaptureFrame(SDL_VideoCaptureDevice *device, SDL_VideoCaptureFrame *frame);
  313. /**
  314. * Stop Video Capture
  315. *
  316. * \param device opened video capture device
  317. * \returns 0 on success or a negative error code on failure; call
  318. * SDL_GetError() for more information.
  319. *
  320. * \since This function is available since SDL 3.0.0.
  321. *
  322. * \sa SDL_StartVideoCapture
  323. */
  324. extern DECLSPEC int SDLCALL SDL_StopVideoCapture(SDL_VideoCaptureDevice *device);
  325. /**
  326. * Use this function to shut down video_capture processing and close the
  327. * video_capture device.
  328. *
  329. * \param device opened video capture device
  330. *
  331. * \since This function is available since SDL 3.0.0.
  332. *
  333. * \sa SDL_OpenVideoCaptureWithSpec
  334. * \sa SDL_OpenVideoCapture
  335. */
  336. extern DECLSPEC void SDLCALL SDL_CloseVideoCapture(SDL_VideoCaptureDevice *device);
  337. /* Ends C function definitions when using C++ */
  338. #ifdef __cplusplus
  339. }
  340. #endif
  341. #include <SDL3/SDL_close_code.h>
  342. #endif /* SDL_video_capture_h_ */