SDL_syscamera.h 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2024 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. #include "../SDL_internal.h"
  19. #ifndef SDL_syscamera_h_
  20. #define SDL_syscamera_h_
  21. #include "../video/SDL_surface_c.h"
  22. #define DEBUG_CAMERA 0
  23. /* Backends should call this as devices are added to the system (such as
  24. a USB camera being plugged in), and should also be called for
  25. for every device found during DetectDevices(). */
  26. extern SDL_Camera *SDL_AddCamera(const char *name, SDL_CameraPosition position, int num_specs, const SDL_CameraSpec *specs, void *handle);
  27. /* Backends should call this if an opened camera device is lost.
  28. This can happen due to i/o errors, or a device being unplugged, etc. */
  29. extern void SDL_CameraDisconnected(SDL_Camera *device);
  30. // Find an SDL_Camera, selected by a callback. NULL if not found. DOES NOT LOCK THE DEVICE.
  31. extern SDL_Camera *SDL_FindPhysicalCameraByCallback(bool (*callback)(SDL_Camera *device, void *userdata), void *userdata);
  32. // Backends should call this when the user has approved/denied access to a camera.
  33. extern void SDL_CameraPermissionOutcome(SDL_Camera *device, bool approved);
  34. // Backends can call this to get a standardized name for a thread to power a specific camera device.
  35. extern char *SDL_GetCameraThreadName(SDL_Camera *device, char *buf, size_t buflen);
  36. // Backends can call these to change a device's refcount.
  37. extern void RefPhysicalCamera(SDL_Camera *device);
  38. extern void UnrefPhysicalCamera(SDL_Camera *device);
  39. // These functions are the heart of the camera threads. Backends can call them directly if they aren't using the SDL-provided thread.
  40. extern void SDL_CameraThreadSetup(SDL_Camera *device);
  41. extern bool SDL_CameraThreadIterate(SDL_Camera *device);
  42. extern void SDL_CameraThreadShutdown(SDL_Camera *device);
  43. // common utility functionality to gather up camera specs. Not required!
  44. typedef struct CameraFormatAddData
  45. {
  46. SDL_CameraSpec *specs;
  47. int num_specs;
  48. int allocated_specs;
  49. } CameraFormatAddData;
  50. bool SDL_AddCameraFormat(CameraFormatAddData *data, SDL_PixelFormat format, SDL_Colorspace colorspace, int w, int h, int framerate_numerator, int framerate_denominator);
  51. typedef enum SDL_CameraFrameResult
  52. {
  53. SDL_CAMERA_FRAME_ERROR,
  54. SDL_CAMERA_FRAME_SKIP,
  55. SDL_CAMERA_FRAME_READY
  56. } SDL_CameraFrameResult;
  57. typedef struct SurfaceList
  58. {
  59. SDL_Surface *surface;
  60. Uint64 timestampNS;
  61. struct SurfaceList *next;
  62. } SurfaceList;
  63. // Define the SDL camera driver structure
  64. struct SDL_Camera
  65. {
  66. // A mutex for locking
  67. SDL_Mutex *lock;
  68. // Human-readable device name.
  69. char *name;
  70. // Position of camera (front-facing, back-facing, etc).
  71. SDL_CameraPosition position;
  72. // When refcount hits zero, we destroy the device object.
  73. SDL_AtomicInt refcount;
  74. // These are, initially, set from camera_driver, but we might swap them out with Zombie versions on disconnect/failure.
  75. bool (*WaitDevice)(SDL_Camera *device);
  76. SDL_CameraFrameResult (*AcquireFrame)(SDL_Camera *device, SDL_Surface *frame, Uint64 *timestampNS);
  77. void (*ReleaseFrame)(SDL_Camera *device, SDL_Surface *frame);
  78. // All supported formats/dimensions for this device.
  79. SDL_CameraSpec *all_specs;
  80. // Elements in all_specs.
  81. int num_specs;
  82. // The device's actual specification that the camera is outputting, before conversion.
  83. SDL_CameraSpec actual_spec;
  84. // The device's current camera specification, after conversions.
  85. SDL_CameraSpec spec;
  86. // Unique value assigned at creation time.
  87. SDL_CameraID instance_id;
  88. // Driver-specific hardware data on how to open device (`hidden` is driver-specific data _when opened_).
  89. void *handle;
  90. // Dropping the first frame(s) after open seems to help timing on some platforms.
  91. int drop_frames;
  92. // Backend timestamp of first acquired frame, so we can keep these meaningful regardless of epoch.
  93. Uint64 base_timestamp;
  94. // SDL timestamp of first acquired frame, so we can roughly convert to SDL ticks.
  95. Uint64 adjust_timestamp;
  96. // Pixel data flows from the driver into these, then gets converted for the app if necessary.
  97. SDL_Surface *acquire_surface;
  98. // acquire_surface converts or scales to this surface before landing in output_surfaces, if necessary.
  99. SDL_Surface *conversion_surface;
  100. // A queue of surfaces that buffer converted/scaled frames of video until the app claims them.
  101. SurfaceList output_surfaces[8];
  102. SurfaceList filled_output_surfaces; // this is FIFO
  103. SurfaceList empty_output_surfaces; // this is LIFO
  104. SurfaceList app_held_output_surfaces;
  105. // A fake video frame we allocate if the camera fails/disconnects.
  106. Uint8 *zombie_pixels;
  107. // non-zero if acquire_surface needs to be scaled for final output.
  108. int needs_scaling; // -1: downscale, 0: no scaling, 1: upscale
  109. // true if acquire_surface needs to be converted for final output.
  110. bool needs_conversion;
  111. // Current state flags
  112. SDL_AtomicInt shutdown;
  113. SDL_AtomicInt zombie;
  114. // A thread to feed the camera device
  115. SDL_Thread *thread;
  116. // Optional properties.
  117. SDL_PropertiesID props;
  118. // -1: user denied permission, 0: waiting for user response, 1: user approved permission.
  119. int permission;
  120. // Data private to this driver, used when device is opened and running.
  121. struct SDL_PrivateCameraData *hidden;
  122. };
  123. typedef struct SDL_CameraDriverImpl
  124. {
  125. void (*DetectDevices)(void);
  126. bool (*OpenDevice)(SDL_Camera *device, const SDL_CameraSpec *spec);
  127. void (*CloseDevice)(SDL_Camera *device);
  128. bool (*WaitDevice)(SDL_Camera *device);
  129. SDL_CameraFrameResult (*AcquireFrame)(SDL_Camera *device, SDL_Surface *frame, Uint64 *timestampNS); // set frame->pixels, frame->pitch, and *timestampNS!
  130. void (*ReleaseFrame)(SDL_Camera *device, SDL_Surface *frame); // Reclaim frame->pixels and frame->pitch!
  131. void (*FreeDeviceHandle)(SDL_Camera *device); // SDL is done with this device; free the handle from SDL_AddCamera()
  132. void (*Deinitialize)(void);
  133. bool ProvidesOwnCallbackThread;
  134. } SDL_CameraDriverImpl;
  135. typedef struct SDL_PendingCameraEvent
  136. {
  137. Uint32 type;
  138. SDL_CameraID devid;
  139. struct SDL_PendingCameraEvent *next;
  140. } SDL_PendingCameraEvent;
  141. typedef struct SDL_CameraDriver
  142. {
  143. const char *name; // The name of this camera driver
  144. const char *desc; // The description of this camera driver
  145. SDL_CameraDriverImpl impl; // the backend's interface
  146. SDL_RWLock *device_hash_lock; // A rwlock that protects `device_hash`
  147. SDL_HashTable *device_hash; // the collection of currently-available camera devices
  148. SDL_PendingCameraEvent pending_events;
  149. SDL_PendingCameraEvent *pending_events_tail;
  150. SDL_AtomicInt device_count;
  151. SDL_AtomicInt shutting_down; // non-zero during SDL_Quit, so we known not to accept any last-minute device hotplugs.
  152. } SDL_CameraDriver;
  153. typedef struct CameraBootStrap
  154. {
  155. const char *name;
  156. const char *desc;
  157. bool (*init)(SDL_CameraDriverImpl *impl);
  158. bool demand_only; // if true: request explicitly, or it won't be available.
  159. } CameraBootStrap;
  160. // Not all of these are available in a given build. Use #ifdefs, etc.
  161. extern CameraBootStrap DUMMYCAMERA_bootstrap;
  162. extern CameraBootStrap PIPEWIRECAMERA_bootstrap;
  163. extern CameraBootStrap V4L2_bootstrap;
  164. extern CameraBootStrap COREMEDIA_bootstrap;
  165. extern CameraBootStrap ANDROIDCAMERA_bootstrap;
  166. extern CameraBootStrap EMSCRIPTENCAMERA_bootstrap;
  167. extern CameraBootStrap MEDIAFOUNDATION_bootstrap;
  168. #endif // SDL_syscamera_h_