SDL_camera.c 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485
  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. #include "SDL_syscamera.h"
  20. #include "SDL_camera_c.h"
  21. #include "../video/SDL_pixels_c.h"
  22. #include "../thread/SDL_systhread.h"
  23. // A lot of this is a simplified version of SDL_audio.c; if fixing stuff here,
  24. // maybe check that file, too.
  25. // Available camera drivers
  26. static const CameraBootStrap *const bootstrap[] = {
  27. #ifdef SDL_CAMERA_DRIVER_V4L2
  28. &V4L2_bootstrap,
  29. #endif
  30. #ifdef SDL_CAMERA_DRIVER_COREMEDIA
  31. &COREMEDIA_bootstrap,
  32. #endif
  33. #ifdef SDL_CAMERA_DRIVER_ANDROID
  34. &ANDROIDCAMERA_bootstrap,
  35. #endif
  36. #ifdef SDL_CAMERA_DRIVER_EMSCRIPTEN
  37. &EMSCRIPTENCAMERA_bootstrap,
  38. #endif
  39. #ifdef SDL_CAMERA_DRIVER_MEDIAFOUNDATION
  40. &MEDIAFOUNDATION_bootstrap,
  41. #endif
  42. #ifdef SDL_CAMERA_DRIVER_DUMMY
  43. &DUMMYCAMERA_bootstrap,
  44. #endif
  45. NULL
  46. };
  47. static SDL_CameraDriver camera_driver;
  48. int SDL_GetNumCameraDrivers(void)
  49. {
  50. return SDL_arraysize(bootstrap) - 1;
  51. }
  52. const char *SDL_GetCameraDriver(int index)
  53. {
  54. if (index >= 0 && index < SDL_GetNumCameraDrivers()) {
  55. return bootstrap[index]->name;
  56. }
  57. return NULL;
  58. }
  59. const char *SDL_GetCurrentCameraDriver(void)
  60. {
  61. return camera_driver.name;
  62. }
  63. char *SDL_GetCameraThreadName(SDL_CameraDevice *device, char *buf, size_t buflen)
  64. {
  65. (void)SDL_snprintf(buf, buflen, "SDLCamera%d", (int) device->instance_id);
  66. return buf;
  67. }
  68. int SDL_AddCameraFormat(CameraFormatAddData *data, Uint32 fmt, int w, int h, int interval_numerator, int interval_denominator)
  69. {
  70. SDL_assert(data != NULL);
  71. if (data->allocated_specs <= data->num_specs) {
  72. const int newalloc = data->allocated_specs ? (data->allocated_specs * 2) : 16;
  73. void *ptr = SDL_realloc(data->specs, sizeof (SDL_CameraSpec) * newalloc);
  74. if (!ptr) {
  75. return -1;
  76. }
  77. data->specs = (SDL_CameraSpec *) ptr;
  78. data->allocated_specs = newalloc;
  79. }
  80. SDL_CameraSpec *spec = &data->specs[data->num_specs];
  81. spec->format = fmt;
  82. spec->width = w;
  83. spec->height = h;
  84. spec->interval_numerator = interval_numerator;
  85. spec->interval_denominator = interval_denominator;
  86. data->num_specs++;
  87. return 0;
  88. }
  89. // Zombie device implementation...
  90. // These get used when a device is disconnected or fails. Apps that ignore the
  91. // loss notifications will get black frames but otherwise keep functioning.
  92. static int ZombieWaitDevice(SDL_CameraDevice *device)
  93. {
  94. if (!SDL_AtomicGet(&device->shutdown)) {
  95. // !!! FIXME: this is bad for several reasons (uses double, could be precalculated, doesn't track elasped time).
  96. const double duration = ((double) device->actual_spec.interval_numerator / ((double) device->actual_spec.interval_denominator));
  97. SDL_Delay((Uint32) (duration * 1000.0));
  98. }
  99. return 0;
  100. }
  101. static size_t GetFrameBufLen(const SDL_CameraSpec *spec)
  102. {
  103. const size_t w = (const size_t) spec->width;
  104. const size_t h = (const size_t) spec->height;
  105. const size_t wxh = w * h;
  106. const Uint32 fmt = spec->format;
  107. switch (fmt) {
  108. // Some YUV formats have a larger Y plane than their U or V planes.
  109. case SDL_PIXELFORMAT_YV12:
  110. case SDL_PIXELFORMAT_IYUV:
  111. case SDL_PIXELFORMAT_NV12:
  112. case SDL_PIXELFORMAT_NV21:
  113. return wxh + (wxh / 2);
  114. default: break;
  115. }
  116. // this is correct for most things.
  117. return wxh * SDL_BYTESPERPIXEL(fmt);
  118. }
  119. static int ZombieAcquireFrame(SDL_CameraDevice *device, SDL_Surface *frame, Uint64 *timestampNS)
  120. {
  121. const SDL_CameraSpec *spec = &device->actual_spec;
  122. if (!device->zombie_pixels) {
  123. // attempt to allocate and initialize a fake frame of pixels.
  124. const size_t buflen = GetFrameBufLen(&device->actual_spec);
  125. device->zombie_pixels = SDL_aligned_alloc(SDL_SIMDGetAlignment(), buflen);
  126. if (!device->zombie_pixels) {
  127. *timestampNS = 0;
  128. return 0; // oh well, say there isn't a frame yet, so we'll go back to waiting. Maybe allocation will succeed later...?
  129. }
  130. Uint8 *dst = device->zombie_pixels;
  131. switch (spec->format) {
  132. // in YUV formats, the U and V values must be 128 to get a black frame. If set to zero, it'll be bright green.
  133. case SDL_PIXELFORMAT_YV12:
  134. case SDL_PIXELFORMAT_IYUV:
  135. case SDL_PIXELFORMAT_NV12:
  136. case SDL_PIXELFORMAT_NV21:
  137. SDL_memset(dst, 0, spec->width * spec->height); // set Y to zero.
  138. SDL_memset(dst + (spec->width * spec->height), 128, (spec->width * spec->height) / 2); // set U and V to 128.
  139. break;
  140. case SDL_PIXELFORMAT_YUY2:
  141. case SDL_PIXELFORMAT_YVYU:
  142. // Interleaved Y1[U1|V1]Y2[U2|V2].
  143. for (size_t i = 0; i < buflen; i += 4) {
  144. dst[i] = 0;
  145. dst[i+1] = 128;
  146. dst[i+2] = 0;
  147. dst[i+3] = 128;
  148. }
  149. break;
  150. case SDL_PIXELFORMAT_UYVY:
  151. // Interleaved [U1|V1]Y1[U2|V2]Y2.
  152. for (size_t i = 0; i < buflen; i += 4) {
  153. dst[i] = 128;
  154. dst[i+1] = 0;
  155. dst[i+2] = 128;
  156. dst[i+3] = 0;
  157. }
  158. break;
  159. default:
  160. // just zero everything else, it'll _probably_ be okay.
  161. SDL_memset(dst, 0, buflen);
  162. break;
  163. }
  164. }
  165. *timestampNS = SDL_GetTicksNS();
  166. frame->pixels = device->zombie_pixels;
  167. // SDL (currently) wants the pitch of YUV formats to be the pitch of the (1-byte-per-pixel) Y plane.
  168. frame->pitch = spec->width;
  169. if (!SDL_ISPIXELFORMAT_FOURCC(spec->format)) { // checking if it's not FOURCC to only do this for non-YUV data is good enough for now.
  170. frame->pitch *= SDL_BYTESPERPIXEL(spec->format);
  171. }
  172. #if DEBUG_CAMERA
  173. SDL_Log("CAMERA: dev[%p] Acquired Zombie frame, timestamp %llu", device, (unsigned long long) *timestampNS);
  174. #endif
  175. return 1; // frame is available.
  176. }
  177. static void ZombieReleaseFrame(SDL_CameraDevice *device, SDL_Surface *frame) // Reclaim frame->pixels and frame->pitch!
  178. {
  179. if (frame->pixels != device->zombie_pixels) {
  180. // this was a frame from before the disconnect event; let the backend make an attempt to free it.
  181. camera_driver.impl.ReleaseFrame(device, frame);
  182. }
  183. // we just leave zombie_pixels alone, as we'll reuse it for every new frame until the camera is closed.
  184. }
  185. static void ClosePhysicalCameraDevice(SDL_CameraDevice *device)
  186. {
  187. if (!device) {
  188. return;
  189. }
  190. SDL_AtomicSet(&device->shutdown, 1);
  191. // !!! FIXME: the close_cond stuff from audio might help the race condition here.
  192. if (device->thread != NULL) {
  193. SDL_WaitThread(device->thread, NULL);
  194. device->thread = NULL;
  195. }
  196. // release frames that are queued up somewhere...
  197. if (!device->needs_conversion && !device->needs_scaling) {
  198. for (SurfaceList *i = device->filled_output_surfaces.next; i != NULL; i = i->next) {
  199. device->ReleaseFrame(device, i->surface);
  200. }
  201. for (SurfaceList *i = device->app_held_output_surfaces.next; i != NULL; i = i->next) {
  202. device->ReleaseFrame(device, i->surface);
  203. }
  204. }
  205. camera_driver.impl.CloseDevice(device);
  206. SDL_DestroyProperties(device->props);
  207. SDL_DestroySurface(device->acquire_surface);
  208. device->acquire_surface = NULL;
  209. SDL_DestroySurface(device->conversion_surface);
  210. device->conversion_surface = NULL;
  211. for (int i = 0; i < SDL_arraysize(device->output_surfaces); i++) {
  212. SDL_DestroySurface(device->output_surfaces[i].surface);
  213. }
  214. SDL_zeroa(device->output_surfaces);
  215. SDL_aligned_free(device->zombie_pixels);
  216. device->zombie_pixels = NULL;
  217. device->filled_output_surfaces.next = NULL;
  218. device->empty_output_surfaces.next = NULL;
  219. device->app_held_output_surfaces.next = NULL;
  220. device->base_timestamp = 0;
  221. device->adjust_timestamp = 0;
  222. }
  223. // this must not be called while `device` is still in a device list, or while a device's camera thread is still running.
  224. static void DestroyPhysicalCameraDevice(SDL_CameraDevice *device)
  225. {
  226. if (device) {
  227. // Destroy any logical devices that still exist...
  228. ClosePhysicalCameraDevice(device);
  229. camera_driver.impl.FreeDeviceHandle(device);
  230. SDL_DestroyMutex(device->lock);
  231. SDL_free(device->all_specs);
  232. SDL_free(device->name);
  233. SDL_free(device);
  234. }
  235. }
  236. // Don't hold the device lock when calling this, as we may destroy the device!
  237. void UnrefPhysicalCameraDevice(SDL_CameraDevice *device)
  238. {
  239. if (SDL_AtomicDecRef(&device->refcount)) {
  240. // take it out of the device list.
  241. SDL_LockRWLockForWriting(camera_driver.device_hash_lock);
  242. if (SDL_RemoveFromHashTable(camera_driver.device_hash, (const void *) (uintptr_t) device->instance_id)) {
  243. SDL_AtomicAdd(&camera_driver.device_count, -1);
  244. }
  245. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  246. DestroyPhysicalCameraDevice(device); // ...and nuke it.
  247. }
  248. }
  249. void RefPhysicalCameraDevice(SDL_CameraDevice *device)
  250. {
  251. SDL_AtomicIncRef(&device->refcount);
  252. }
  253. static void ObtainPhysicalCameraDeviceObj(SDL_CameraDevice *device) SDL_NO_THREAD_SAFETY_ANALYSIS // !!! FIXMEL SDL_ACQUIRE
  254. {
  255. if (device) {
  256. RefPhysicalCameraDevice(device);
  257. SDL_LockMutex(device->lock);
  258. }
  259. }
  260. static SDL_CameraDevice *ObtainPhysicalCameraDevice(SDL_CameraDeviceID devid) // !!! FIXME: SDL_ACQUIRE
  261. {
  262. if (!SDL_GetCurrentCameraDriver()) {
  263. SDL_SetError("Camera subsystem is not initialized");
  264. return NULL;
  265. }
  266. SDL_CameraDevice *device = NULL;
  267. SDL_LockRWLockForReading(camera_driver.device_hash_lock);
  268. SDL_FindInHashTable(camera_driver.device_hash, (const void *) (uintptr_t) devid, (const void **) &device);
  269. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  270. if (!device) {
  271. SDL_SetError("Invalid camera device instance ID");
  272. } else {
  273. ObtainPhysicalCameraDeviceObj(device);
  274. }
  275. return device;
  276. }
  277. static void ReleaseCameraDevice(SDL_CameraDevice *device) SDL_NO_THREAD_SAFETY_ANALYSIS // !!! FIXME: SDL_RELEASE
  278. {
  279. if (device) {
  280. SDL_UnlockMutex(device->lock);
  281. UnrefPhysicalCameraDevice(device);
  282. }
  283. }
  284. // we want these sorted by format first, so you can find a block of all
  285. // resolutions that are supported for a format. The formats are sorted in
  286. // "best" order, but that's subjective: right now, we prefer planar
  287. // formats, since they're likely what the cameras prefer to produce
  288. // anyhow, and they basically send the same information in less space
  289. // than an RGB-style format. After that, sort by bits-per-pixel.
  290. // we want specs sorted largest to smallest dimensions, larger width taking precedence over larger height.
  291. static int SDLCALL CameraSpecCmp(const void *vpa, const void *vpb)
  292. {
  293. const SDL_CameraSpec *a = (const SDL_CameraSpec *) vpa;
  294. const SDL_CameraSpec *b = (const SDL_CameraSpec *) vpb;
  295. // driver shouldn't send specs like this, check here since we're eventually going to sniff the whole array anyhow.
  296. SDL_assert(a->format != SDL_PIXELFORMAT_UNKNOWN);
  297. SDL_assert(a->width > 0);
  298. SDL_assert(a->height > 0);
  299. SDL_assert(b->format != SDL_PIXELFORMAT_UNKNOWN);
  300. SDL_assert(b->width > 0);
  301. SDL_assert(b->height > 0);
  302. const Uint32 afmt = a->format;
  303. const Uint32 bfmt = b->format;
  304. if (SDL_ISPIXELFORMAT_FOURCC(afmt) && !SDL_ISPIXELFORMAT_FOURCC(bfmt)) {
  305. return -1;
  306. } else if (!SDL_ISPIXELFORMAT_FOURCC(afmt) && SDL_ISPIXELFORMAT_FOURCC(bfmt)) {
  307. return 1;
  308. } else if (SDL_BITSPERPIXEL(afmt) > SDL_BITSPERPIXEL(bfmt)) {
  309. return -1;
  310. } else if (SDL_BITSPERPIXEL(bfmt) > SDL_BITSPERPIXEL(afmt)) {
  311. return 1;
  312. } else if (a->width > b->width) {
  313. return -1;
  314. } else if (b->width > a->width) {
  315. return 1;
  316. } else if (a->height > b->height) {
  317. return -1;
  318. } else if (b->height > a->height) {
  319. return 1;
  320. }
  321. // still here? We care about framerate less than format or size, but faster is better than slow.
  322. if (a->interval_numerator && !b->interval_numerator) {
  323. return -1;
  324. } else if (!a->interval_numerator && b->interval_numerator) {
  325. return 1;
  326. }
  327. const float fpsa = ((float) a->interval_denominator)/ ((float) a->interval_numerator);
  328. const float fpsb = ((float) b->interval_denominator)/ ((float) b->interval_numerator);
  329. if (fpsa > fpsb) {
  330. return -1;
  331. } else if (fpsb > fpsa) {
  332. return 1;
  333. }
  334. return 0; // apparently, they're equal.
  335. }
  336. // The camera backends call this when a new device is plugged in.
  337. SDL_CameraDevice *SDL_AddCameraDevice(const char *name, int num_specs, const SDL_CameraSpec *specs, void *handle)
  338. {
  339. SDL_assert(name != NULL);
  340. SDL_assert(num_specs >= 0);
  341. SDL_assert((specs != NULL) == (num_specs > 0));
  342. SDL_assert(handle != NULL);
  343. SDL_LockRWLockForReading(camera_driver.device_hash_lock);
  344. const int shutting_down = SDL_AtomicGet(&camera_driver.shutting_down);
  345. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  346. if (shutting_down) {
  347. return NULL; // we're shutting down, don't add any devices that are hotplugged at the last possible moment.
  348. }
  349. SDL_CameraDevice *device = (SDL_CameraDevice *)SDL_calloc(1, sizeof(SDL_CameraDevice));
  350. if (!device) {
  351. return NULL;
  352. }
  353. device->name = SDL_strdup(name);
  354. if (!device->name) {
  355. SDL_free(device);
  356. return NULL;
  357. }
  358. device->lock = SDL_CreateMutex();
  359. if (!device->lock) {
  360. SDL_free(device->name);
  361. SDL_free(device);
  362. return NULL;
  363. }
  364. device->all_specs = SDL_calloc(num_specs + 1, sizeof (*specs));
  365. if (!device->all_specs) {
  366. SDL_DestroyMutex(device->lock);
  367. SDL_free(device->name);
  368. SDL_free(device);
  369. return NULL;
  370. }
  371. if (num_specs > 0) {
  372. SDL_memcpy(device->all_specs, specs, sizeof (*specs) * num_specs);
  373. SDL_qsort(device->all_specs, num_specs, sizeof (*specs), CameraSpecCmp);
  374. // weed out duplicates, just in case.
  375. for (int i = 0; i < num_specs; i++) {
  376. SDL_CameraSpec *a = &device->all_specs[i];
  377. SDL_CameraSpec *b = &device->all_specs[i + 1];
  378. if (SDL_memcmp(a, b, sizeof (*a)) == 0) {
  379. SDL_memmove(a, b, sizeof (*specs) * (num_specs - i));
  380. i--;
  381. num_specs--;
  382. }
  383. }
  384. }
  385. #if DEBUG_CAMERA
  386. SDL_Log("CAMERA: Adding device ('%s') with %d spec%s%s", name, num_specs, (num_specs == 1) ? "" : "s", (num_specs == 0) ? "" : ":");
  387. for (int i = 0; i < num_specs; i++) {
  388. const SDL_CameraSpec *spec = &device->all_specs[i];
  389. SDL_Log("CAMERA: - fmt=%s, w=%d, h=%d, numerator=%d, denominator=%d", SDL_GetPixelFormatName(spec->format), spec->width, spec->height, spec->interval_numerator, spec->interval_denominator);
  390. }
  391. #endif
  392. device->num_specs = num_specs;
  393. device->handle = handle;
  394. device->instance_id = SDL_GetNextObjectID();
  395. SDL_AtomicSet(&device->shutdown, 0);
  396. SDL_AtomicSet(&device->zombie, 0);
  397. RefPhysicalCameraDevice(device);
  398. SDL_LockRWLockForWriting(camera_driver.device_hash_lock);
  399. if (SDL_InsertIntoHashTable(camera_driver.device_hash, (const void *) (uintptr_t) device->instance_id, device)) {
  400. SDL_AtomicAdd(&camera_driver.device_count, 1);
  401. } else {
  402. SDL_DestroyMutex(device->lock);
  403. SDL_free(device->all_specs);
  404. SDL_free(device->name);
  405. SDL_free(device);
  406. device = NULL;
  407. }
  408. // Add a device add event to the pending list, to be pushed when the event queue is pumped (away from any of our internal threads).
  409. if (device) {
  410. SDL_PendingCameraDeviceEvent *p = (SDL_PendingCameraDeviceEvent *) SDL_malloc(sizeof (SDL_PendingCameraDeviceEvent));
  411. if (p) { // if allocation fails, you won't get an event, but we can't help that.
  412. p->type = SDL_EVENT_CAMERA_DEVICE_ADDED;
  413. p->devid = device->instance_id;
  414. p->next = NULL;
  415. SDL_assert(camera_driver.pending_events_tail != NULL);
  416. SDL_assert(camera_driver.pending_events_tail->next == NULL);
  417. camera_driver.pending_events_tail->next = p;
  418. camera_driver.pending_events_tail = p;
  419. }
  420. }
  421. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  422. return device;
  423. }
  424. // Called when a device is removed from the system, or it fails unexpectedly, from any thread, possibly even the camera device's thread.
  425. void SDL_CameraDeviceDisconnected(SDL_CameraDevice *device)
  426. {
  427. if (!device) {
  428. return;
  429. }
  430. #if DEBUG_CAMERA
  431. SDL_Log("CAMERA: DISCONNECTED! dev[%p]", device);
  432. #endif
  433. // Save off removal info in a list so we can send events for each, next
  434. // time the event queue pumps, in case something tries to close a device
  435. // from an event filter, as this would risk deadlocks and other disasters
  436. // if done from the device thread.
  437. SDL_PendingCameraDeviceEvent pending;
  438. pending.next = NULL;
  439. SDL_PendingCameraDeviceEvent *pending_tail = &pending;
  440. ObtainPhysicalCameraDeviceObj(device);
  441. const SDL_bool first_disconnect = SDL_AtomicCompareAndSwap(&device->zombie, 0, 1);
  442. if (first_disconnect) { // if already disconnected this device, don't do it twice.
  443. // Swap in "Zombie" versions of the usual platform interfaces, so the device will keep
  444. // making progress until the app closes it. Otherwise, streams might continue to
  445. // accumulate waste data that never drains, apps that depend on audio callbacks to
  446. // progress will freeze, etc.
  447. device->WaitDevice = ZombieWaitDevice;
  448. device->AcquireFrame = ZombieAcquireFrame;
  449. device->ReleaseFrame = ZombieReleaseFrame;
  450. // Zombie functions will just report the timestamp as SDL_GetTicksNS(), so we don't need to adjust anymore to get it to match.
  451. device->adjust_timestamp = 0;
  452. device->base_timestamp = 0;
  453. SDL_PendingCameraDeviceEvent *p = (SDL_PendingCameraDeviceEvent *) SDL_malloc(sizeof (SDL_PendingCameraDeviceEvent));
  454. if (p) { // if this failed, no event for you, but you have deeper problems anyhow.
  455. p->type = SDL_EVENT_CAMERA_DEVICE_REMOVED;
  456. p->devid = device->instance_id;
  457. p->next = NULL;
  458. pending_tail->next = p;
  459. pending_tail = p;
  460. }
  461. }
  462. ReleaseCameraDevice(device);
  463. if (first_disconnect) {
  464. if (pending.next) { // NULL if event is disabled or disaster struck.
  465. SDL_LockRWLockForWriting(camera_driver.device_hash_lock);
  466. SDL_assert(camera_driver.pending_events_tail != NULL);
  467. SDL_assert(camera_driver.pending_events_tail->next == NULL);
  468. camera_driver.pending_events_tail->next = pending.next;
  469. camera_driver.pending_events_tail = pending_tail;
  470. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  471. }
  472. }
  473. }
  474. void SDL_CameraDevicePermissionOutcome(SDL_CameraDevice *device, SDL_bool approved)
  475. {
  476. if (!device) {
  477. return;
  478. }
  479. SDL_PendingCameraDeviceEvent pending;
  480. pending.next = NULL;
  481. SDL_PendingCameraDeviceEvent *pending_tail = &pending;
  482. const int permission = approved ? 1 : -1;
  483. ObtainPhysicalCameraDeviceObj(device);
  484. if (device->permission != permission) {
  485. device->permission = permission;
  486. SDL_PendingCameraDeviceEvent *p = (SDL_PendingCameraDeviceEvent *) SDL_malloc(sizeof (SDL_PendingCameraDeviceEvent));
  487. if (p) { // if this failed, no event for you, but you have deeper problems anyhow.
  488. p->type = approved ? SDL_EVENT_CAMERA_DEVICE_APPROVED : SDL_EVENT_CAMERA_DEVICE_DENIED;
  489. p->devid = device->instance_id;
  490. p->next = NULL;
  491. pending_tail->next = p;
  492. pending_tail = p;
  493. }
  494. }
  495. ReleaseCameraDevice(device);
  496. SDL_LockRWLockForWriting(camera_driver.device_hash_lock);
  497. SDL_assert(camera_driver.pending_events_tail != NULL);
  498. SDL_assert(camera_driver.pending_events_tail->next == NULL);
  499. camera_driver.pending_events_tail->next = pending.next;
  500. camera_driver.pending_events_tail = pending_tail;
  501. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  502. }
  503. SDL_CameraDevice *SDL_FindPhysicalCameraDeviceByCallback(SDL_bool (*callback)(SDL_CameraDevice *device, void *userdata), void *userdata)
  504. {
  505. if (!SDL_GetCurrentCameraDriver()) {
  506. SDL_SetError("Camera subsystem is not initialized");
  507. return NULL;
  508. }
  509. const void *key;
  510. const void *value;
  511. void *iter = NULL;
  512. SDL_LockRWLockForReading(camera_driver.device_hash_lock);
  513. while (SDL_IterateHashTable(camera_driver.device_hash, &key, &value, &iter)) {
  514. SDL_CameraDevice *device = (SDL_CameraDevice *) value;
  515. if (callback(device, userdata)) { // found it?
  516. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  517. return device;
  518. }
  519. }
  520. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  521. SDL_SetError("Device not found");
  522. return NULL;
  523. }
  524. void SDL_CloseCamera(SDL_Camera *camera)
  525. {
  526. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  527. ClosePhysicalCameraDevice(device);
  528. }
  529. int SDL_GetCameraFormat(SDL_Camera *camera, SDL_CameraSpec *spec)
  530. {
  531. if (!camera) {
  532. return SDL_InvalidParamError("camera");
  533. } else if (!spec) {
  534. return SDL_InvalidParamError("spec");
  535. }
  536. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  537. ObtainPhysicalCameraDeviceObj(device);
  538. const int retval = (device->permission > 0) ? 0 : SDL_SetError("Camera permission has not been granted");
  539. if (retval == 0) {
  540. SDL_copyp(spec, &device->spec);
  541. } else {
  542. SDL_zerop(spec);
  543. }
  544. ReleaseCameraDevice(device);
  545. return 0;
  546. }
  547. char *SDL_GetCameraDeviceName(SDL_CameraDeviceID instance_id)
  548. {
  549. char *retval = NULL;
  550. SDL_CameraDevice *device = ObtainPhysicalCameraDevice(instance_id);
  551. if (device) {
  552. retval = SDL_strdup(device->name);
  553. ReleaseCameraDevice(device);
  554. }
  555. return retval;
  556. }
  557. SDL_CameraDeviceID *SDL_GetCameraDevices(int *count)
  558. {
  559. int dummy_count;
  560. if (!count) {
  561. count = &dummy_count;
  562. }
  563. if (!SDL_GetCurrentCameraDriver()) {
  564. *count = 0;
  565. SDL_SetError("Camera subsystem is not initialized");
  566. return NULL;
  567. }
  568. SDL_CameraDeviceID *retval = NULL;
  569. SDL_LockRWLockForReading(camera_driver.device_hash_lock);
  570. int num_devices = SDL_AtomicGet(&camera_driver.device_count);
  571. retval = (SDL_CameraDeviceID *) SDL_malloc((num_devices + 1) * sizeof (SDL_CameraDeviceID));
  572. if (!retval) {
  573. num_devices = 0;
  574. } else {
  575. int devs_seen = 0;
  576. const void *key;
  577. const void *value;
  578. void *iter = NULL;
  579. while (SDL_IterateHashTable(camera_driver.device_hash, &key, &value, &iter)) {
  580. retval[devs_seen++] = (SDL_CameraDeviceID) (uintptr_t) key;
  581. }
  582. SDL_assert(devs_seen == num_devices);
  583. retval[devs_seen] = 0; // null-terminated.
  584. }
  585. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  586. *count = num_devices;
  587. return retval;
  588. }
  589. SDL_CameraSpec *SDL_GetCameraDeviceSupportedFormats(SDL_CameraDeviceID instance_id, int *count)
  590. {
  591. if (count) {
  592. *count = 0;
  593. }
  594. SDL_CameraDevice *device = ObtainPhysicalCameraDevice(instance_id);
  595. if (!device) {
  596. return NULL;
  597. }
  598. SDL_CameraSpec *retval = (SDL_CameraSpec *) SDL_calloc(device->num_specs + 1, sizeof (SDL_CameraSpec));
  599. if (retval) {
  600. SDL_memcpy(retval, device->all_specs, sizeof (SDL_CameraSpec) * device->num_specs);
  601. if (count) {
  602. *count = device->num_specs;
  603. }
  604. }
  605. ReleaseCameraDevice(device);
  606. return retval;
  607. }
  608. // Camera device thread. This is split into chunks, so drivers that need to control this directly can use the pieces they need without duplicating effort.
  609. void SDL_CameraThreadSetup(SDL_CameraDevice *device)
  610. {
  611. //camera_driver.impl.ThreadInit(device);
  612. #ifdef SDL_VIDEO_DRIVER_ANDROID
  613. // TODO
  614. /*
  615. {
  616. // Set thread priority to THREAD_PRIORITY_VIDEO
  617. extern void Android_JNI_CameraSetThreadPriority(int, int);
  618. Android_JNI_CameraSetThreadPriority(device->iscapture, device);
  619. }*/
  620. #else
  621. // The camera capture is always a high priority thread
  622. SDL_SetThreadPriority(SDL_THREAD_PRIORITY_HIGH);
  623. #endif
  624. }
  625. SDL_bool SDL_CameraThreadIterate(SDL_CameraDevice *device)
  626. {
  627. SDL_LockMutex(device->lock);
  628. if (SDL_AtomicGet(&device->shutdown)) {
  629. SDL_UnlockMutex(device->lock);
  630. return SDL_FALSE; // we're done, shut it down.
  631. }
  632. const int permission = device->permission;
  633. if (permission <= 0) {
  634. SDL_UnlockMutex(device->lock);
  635. return (permission < 0) ? SDL_FALSE : SDL_TRUE; // if permission was denied, shut it down. if undecided, we're done for now.
  636. }
  637. SDL_bool failed = SDL_FALSE; // set to true if disaster worthy of treating the device as lost has happened.
  638. SDL_Surface *acquired = NULL;
  639. SDL_Surface *output_surface = NULL;
  640. SurfaceList *slist = NULL;
  641. Uint64 timestampNS = 0;
  642. // AcquireFrame SHOULD NOT BLOCK, as we are holding a lock right now. Block in WaitDevice instead!
  643. const int rc = device->AcquireFrame(device, device->acquire_surface, &timestampNS);
  644. if (rc == 1) { // new frame acquired!
  645. #if DEBUG_CAMERA
  646. SDL_Log("CAMERA: New frame available! pixels=%p pitch=%d", device->acquire_surface->pixels, device->acquire_surface->pitch);
  647. #endif
  648. if (device->drop_frames > 0) {
  649. #if DEBUG_CAMERA
  650. SDL_Log("CAMERA: Dropping an initial frame");
  651. #endif
  652. device->drop_frames--;
  653. device->ReleaseFrame(device, device->acquire_surface);
  654. device->acquire_surface->pixels = NULL;
  655. device->acquire_surface->pitch = 0;
  656. } else if (device->empty_output_surfaces.next == NULL) {
  657. // uhoh, no output frames available! Either the app is slow, or it forgot to release frames when done with them. Drop this new frame.
  658. #if DEBUG_CAMERA
  659. SDL_Log("CAMERA: No empty output surfaces! Dropping frame!");
  660. #endif
  661. device->ReleaseFrame(device, device->acquire_surface);
  662. device->acquire_surface->pixels = NULL;
  663. device->acquire_surface->pitch = 0;
  664. } else {
  665. if (!device->adjust_timestamp) {
  666. device->adjust_timestamp = SDL_GetTicksNS();
  667. device->base_timestamp = timestampNS;
  668. }
  669. timestampNS = (timestampNS - device->base_timestamp) + device->adjust_timestamp;
  670. slist = device->empty_output_surfaces.next;
  671. output_surface = slist->surface;
  672. device->empty_output_surfaces.next = slist->next;
  673. acquired = device->acquire_surface;
  674. slist->timestampNS = timestampNS;
  675. }
  676. } else if (rc == 0) { // no frame available yet; not an error.
  677. #if 0 //DEBUG_CAMERA
  678. SDL_Log("CAMERA: No frame available yet.");
  679. #endif
  680. } else { // fatal error!
  681. SDL_assert(rc == -1);
  682. #if DEBUG_CAMERA
  683. SDL_Log("CAMERA: dev[%p] error AcquireFrame: %s", device, SDL_GetError());
  684. #endif
  685. failed = SDL_TRUE;
  686. }
  687. // we can let go of the lock once we've tried to grab a frame of video and maybe moved the output frame off the empty list.
  688. // this lets us chew up the CPU for conversion and scaling without blocking other threads.
  689. SDL_UnlockMutex(device->lock);
  690. if (failed) {
  691. SDL_assert(slist == NULL);
  692. SDL_assert(acquired == NULL);
  693. SDL_CameraDeviceDisconnected(device); // doh.
  694. } else if (acquired) { // we have a new frame, scale/convert if necessary and queue it for the app!
  695. SDL_assert(slist != NULL);
  696. if (!device->needs_scaling && !device->needs_conversion) { // no conversion needed? Just move the pointer/pitch into the output surface.
  697. #if DEBUG_CAMERA
  698. SDL_Log("CAMERA: Frame is going through without conversion!");
  699. #endif
  700. output_surface->pixels = acquired->pixels;
  701. output_surface->pitch = acquired->pitch;
  702. } else { // convert/scale into a different surface.
  703. #if DEBUG_CAMERA
  704. SDL_Log("CAMERA: Frame is getting converted!");
  705. #endif
  706. SDL_Surface *srcsurf = acquired;
  707. if (device->needs_scaling == -1) { // downscaling? Do it first. -1: downscale, 0: no scaling, 1: upscale
  708. SDL_Surface *dstsurf = device->needs_conversion ? device->conversion_surface : output_surface;
  709. SDL_SoftStretch(srcsurf, NULL, dstsurf, NULL, SDL_SCALEMODE_NEAREST); // !!! FIXME: linear scale? letterboxing?
  710. srcsurf = dstsurf;
  711. }
  712. if (device->needs_conversion) {
  713. SDL_Surface *dstsurf = (device->needs_scaling == 1) ? device->conversion_surface : output_surface;
  714. SDL_ConvertPixels(srcsurf->w, srcsurf->h,
  715. srcsurf->format->format, srcsurf->pixels, srcsurf->pitch,
  716. dstsurf->format->format, dstsurf->pixels, dstsurf->pitch);
  717. srcsurf = dstsurf;
  718. }
  719. if (device->needs_scaling == 1) { // upscaling? Do it last. -1: downscale, 0: no scaling, 1: upscale
  720. SDL_SoftStretch(srcsurf, NULL, output_surface, NULL, SDL_SCALEMODE_NEAREST); // !!! FIXME: linear scale? letterboxing?
  721. }
  722. // we made a copy, so we can give the driver back its resources.
  723. device->ReleaseFrame(device, acquired);
  724. }
  725. // we either released these already after we copied the data, or the pointer was migrated to output_surface.
  726. acquired->pixels = NULL;
  727. acquired->pitch = 0;
  728. // make the filled output surface available to the app.
  729. SDL_LockMutex(device->lock);
  730. slist->next = device->filled_output_surfaces.next;
  731. device->filled_output_surfaces.next = slist;
  732. SDL_UnlockMutex(device->lock);
  733. }
  734. return SDL_TRUE; // always go on if not shutting down, even if device failed.
  735. }
  736. void SDL_CameraThreadShutdown(SDL_CameraDevice *device)
  737. {
  738. //device->FlushCapture(device);
  739. //camera_driver.impl.ThreadDeinit(device);
  740. //SDL_CameraThreadFinalize(device);
  741. }
  742. // Actual thread entry point, if driver didn't handle this itself.
  743. static int SDLCALL CameraThread(void *devicep)
  744. {
  745. SDL_CameraDevice *device = (SDL_CameraDevice *) devicep;
  746. #if DEBUG_CAMERA
  747. SDL_Log("CAMERA: dev[%p] Start thread 'CameraThread'", devicep);
  748. #endif
  749. SDL_assert(device != NULL);
  750. SDL_CameraThreadSetup(device);
  751. do {
  752. if (device->WaitDevice(device) < 0) {
  753. SDL_CameraDeviceDisconnected(device); // doh. (but don't break out of the loop, just be a zombie for now!)
  754. }
  755. } while (SDL_CameraThreadIterate(device));
  756. SDL_CameraThreadShutdown(device);
  757. #if DEBUG_CAMERA
  758. SDL_Log("CAMERA: dev[%p] End thread 'CameraThread'", devicep);
  759. #endif
  760. return 0;
  761. }
  762. static void ChooseBestCameraSpec(SDL_CameraDevice *device, const SDL_CameraSpec *spec, SDL_CameraSpec *closest)
  763. {
  764. // Find the closest available native format/size...
  765. //
  766. // We want the exact size if possible, even if we have
  767. // to convert formats, because we can _probably_ do that
  768. // conversion losslessly at less expense verses scaling.
  769. //
  770. // Failing that, we want the size that's closest to the
  771. // requested aspect ratio, then the closest size within
  772. // that.
  773. SDL_zerop(closest);
  774. SDL_assert(((Uint32) SDL_PIXELFORMAT_UNKNOWN) == 0); // since we SDL_zerop'd to this value.
  775. if (device->num_specs == 0) { // device listed no specs! You get whatever you want!
  776. if (spec) {
  777. SDL_copyp(closest, spec);
  778. }
  779. return;
  780. } else if (!spec) { // nothing specifically requested, get the best format we can...
  781. // we sorted this into the "best" format order when adding the camera.
  782. SDL_copyp(closest, &device->all_specs[0]);
  783. } else { // specific thing requested, try to get as close to that as possible...
  784. const int num_specs = device->num_specs;
  785. int wantw = spec->width;
  786. int wanth = spec->height;
  787. // Find the sizes with the closest aspect ratio and then find the best fit of those.
  788. const float wantaspect = ((float)wantw) / ((float) wanth);
  789. const float epsilon = 1e-6f;
  790. float closestaspect = -9999999.0f;
  791. float closestdiff = 999999.0f;
  792. int closestdiffw = 9999999;
  793. for (int i = 0; i < num_specs; i++) {
  794. const SDL_CameraSpec *thisspec = &device->all_specs[i];
  795. const int thisw = thisspec->width;
  796. const int thish = thisspec->height;
  797. const float thisaspect = ((float)thisw) / ((float) thish);
  798. const float aspectdiff = SDL_fabs(wantaspect - thisaspect);
  799. const float diff = SDL_fabs(closestaspect - thisaspect);
  800. const int diffw = SDL_abs(thisw - wantw);
  801. if (diff < epsilon) { // matches current closestaspect? See if resolution is closer in size.
  802. if (diffw < closestdiffw) {
  803. closestdiffw = diffw;
  804. closest->width = thisw;
  805. closest->height = thish;
  806. }
  807. } else if (aspectdiff < closestdiff) { // this is a closer aspect ratio? Take it, reset resolution checks.
  808. closestdiff = aspectdiff;
  809. closestaspect = thisaspect;
  810. closestdiffw = diffw;
  811. closest->width = thisw;
  812. closest->height = thish;
  813. }
  814. }
  815. SDL_assert(closest->width > 0);
  816. SDL_assert(closest->height > 0);
  817. // okay, we have what we think is the best resolution, now we just need the best format that supports it...
  818. const Uint32 wantfmt = spec->format;
  819. Uint32 bestfmt = SDL_PIXELFORMAT_UNKNOWN;
  820. for (int i = 0; i < num_specs; i++) {
  821. const SDL_CameraSpec *thisspec = &device->all_specs[i];
  822. if ((thisspec->width == closest->width) && (thisspec->height == closest->height)) {
  823. if (bestfmt == SDL_PIXELFORMAT_UNKNOWN) {
  824. bestfmt = thisspec->format; // spec list is sorted by what we consider "best" format, so unless we find an exact match later, first size match is the one!
  825. }
  826. if (thisspec->format == wantfmt) {
  827. bestfmt = thisspec->format;
  828. break; // exact match, stop looking.
  829. }
  830. }
  831. }
  832. SDL_assert(bestfmt != SDL_PIXELFORMAT_UNKNOWN);
  833. closest->format = bestfmt;
  834. // We have a resolution and a format, find the closest framerate...
  835. const float wantfps = spec->interval_numerator ? (spec->interval_denominator / spec->interval_numerator) : 0.0f;
  836. float closestfps = 9999999.0f;
  837. for (int i = 0; i < num_specs; i++) {
  838. const SDL_CameraSpec *thisspec = &device->all_specs[i];
  839. if ((thisspec->format == closest->format) && (thisspec->width == closest->width) && (thisspec->height == closest->height)) {
  840. if ((thisspec->interval_numerator == spec->interval_numerator) && (thisspec->interval_denominator == spec->interval_denominator)) {
  841. closest->interval_numerator = thisspec->interval_numerator;
  842. closest->interval_denominator = thisspec->interval_denominator;
  843. break; // exact match, stop looking.
  844. }
  845. const float thisfps = thisspec->interval_numerator ? (thisspec->interval_denominator / thisspec->interval_numerator) : 0.0f;
  846. const float fpsdiff = SDL_fabs(wantfps - thisfps);
  847. if (fpsdiff < closestfps) { // this is a closest FPS? Take it until something closer arrives.
  848. closestfps = fpsdiff;
  849. closest->interval_numerator = thisspec->interval_numerator;
  850. closest->interval_denominator = thisspec->interval_denominator;
  851. }
  852. }
  853. }
  854. }
  855. SDL_assert(closest->width > 0);
  856. SDL_assert(closest->height > 0);
  857. SDL_assert(closest->format != SDL_PIXELFORMAT_UNKNOWN);
  858. }
  859. SDL_Camera *SDL_OpenCameraDevice(SDL_CameraDeviceID instance_id, const SDL_CameraSpec *spec)
  860. {
  861. if (spec) {
  862. if ((spec->width <= 0) || (spec->height <= 0)) {
  863. SDL_SetError("Requested spec frame size is invalid");
  864. return NULL;
  865. } else if (spec->format == SDL_PIXELFORMAT_UNKNOWN) {
  866. SDL_SetError("Requested spec format is invalid");
  867. return NULL;
  868. }
  869. }
  870. SDL_CameraDevice *device = ObtainPhysicalCameraDevice(instance_id);
  871. if (!device) {
  872. return NULL;
  873. }
  874. if (device->hidden != NULL) {
  875. ReleaseCameraDevice(device);
  876. SDL_SetError("Camera already opened"); // we may remove this limitation at some point.
  877. return NULL;
  878. }
  879. SDL_AtomicSet(&device->shutdown, 0);
  880. // These start with the backend's implementation, but we might swap them out with zombie versions later.
  881. device->WaitDevice = camera_driver.impl.WaitDevice;
  882. device->AcquireFrame = camera_driver.impl.AcquireFrame;
  883. device->ReleaseFrame = camera_driver.impl.ReleaseFrame;
  884. SDL_CameraSpec closest;
  885. ChooseBestCameraSpec(device, spec, &closest);
  886. #if DEBUG_CAMERA
  887. SDL_Log("CAMERA: App wanted [(%dx%d) fmt=%s interval=%d/%d], chose [(%dx%d) fmt=%s interval=%d/%d]",
  888. spec ? spec->width : -1, spec ? spec->height : -1, spec ? SDL_GetPixelFormatName(spec->format) : "(null)", spec ? spec->interval_numerator : -1, spec ? spec->interval_denominator : -1,
  889. closest.width, closest.height, SDL_GetPixelFormatName(closest.format), closest.interval_numerator, closest.interval_denominator);
  890. #endif
  891. if (camera_driver.impl.OpenDevice(device, &closest) < 0) {
  892. ClosePhysicalCameraDevice(device); // in case anything is half-initialized.
  893. ReleaseCameraDevice(device);
  894. return NULL;
  895. }
  896. if (!spec) {
  897. SDL_copyp(&device->spec, &closest);
  898. } else {
  899. SDL_copyp(&device->spec, spec);
  900. }
  901. SDL_copyp(&device->actual_spec, &closest);
  902. if ((closest.width == device->spec.width) && (closest.height == device->spec.height)) {
  903. device->needs_scaling = 0;
  904. } else {
  905. const Uint64 srcarea = ((Uint64) closest.width) * ((Uint64) closest.height);
  906. const Uint64 dstarea = ((Uint64) device->spec.width) * ((Uint64) device->spec.height);
  907. if (dstarea <= srcarea) {
  908. device->needs_scaling = -1; // downscaling (or changing to new aspect ratio with same area)
  909. } else {
  910. device->needs_scaling = 1; // upscaling
  911. }
  912. }
  913. device->needs_conversion = (closest.format != device->spec.format);
  914. device->acquire_surface = SDL_CreateSurfaceFrom(NULL, closest.width, closest.height, 0, closest.format);
  915. if (!device->acquire_surface) {
  916. ClosePhysicalCameraDevice(device);
  917. ReleaseCameraDevice(device);
  918. return NULL;
  919. }
  920. // if we have to scale _and_ convert, we need a middleman surface, since we can't do both changes at once.
  921. if (device->needs_scaling && device->needs_conversion) {
  922. const SDL_bool downsampling_first = (device->needs_scaling < 0);
  923. const SDL_CameraSpec *s = downsampling_first ? &device->spec : &closest;
  924. const Uint32 fmt = downsampling_first ? closest.format : device->spec.format;
  925. device->conversion_surface = SDL_CreateSurface(s->width, s->height, fmt);
  926. }
  927. // output surfaces are in the app-requested format. If no conversion is necessary, we'll just use the pointers
  928. // the backend fills into acquired_surface, and you can get all the way from DMA access in the camera hardware
  929. // to the app without a single copy. Otherwise, these will be full surfaces that hold converted/scaled copies.
  930. for (int i = 0; i < (SDL_arraysize(device->output_surfaces) - 1); i++) {
  931. device->output_surfaces[i].next = &device->output_surfaces[i + 1];
  932. }
  933. device->empty_output_surfaces.next = device->output_surfaces;
  934. for (int i = 0; i < SDL_arraysize(device->output_surfaces); i++) {
  935. SDL_Surface *surf;
  936. if (device->needs_scaling || device->needs_conversion) {
  937. surf = SDL_CreateSurface(device->spec.width, device->spec.height, device->spec.format);
  938. } else {
  939. surf = SDL_CreateSurfaceFrom(NULL, device->spec.width, device->spec.height, 0, device->spec.format);
  940. }
  941. if (!surf) {
  942. ClosePhysicalCameraDevice(device);
  943. ReleaseCameraDevice(device);
  944. return NULL;
  945. }
  946. device->output_surfaces[i].surface = surf;
  947. }
  948. device->drop_frames = 1;
  949. // Start the camera thread if necessary
  950. if (!camera_driver.impl.ProvidesOwnCallbackThread) {
  951. char threadname[64];
  952. SDL_GetCameraThreadName(device, threadname, sizeof (threadname));
  953. device->thread = SDL_CreateThreadInternal(CameraThread, threadname, 0, device);
  954. if (!device->thread) {
  955. ClosePhysicalCameraDevice(device);
  956. ReleaseCameraDevice(device);
  957. SDL_SetError("Couldn't create camera thread");
  958. return NULL;
  959. }
  960. }
  961. ReleaseCameraDevice(device); // unlock, we're good to go!
  962. return (SDL_Camera *) device; // currently there's no separation between physical and logical device.
  963. }
  964. SDL_Surface *SDL_AcquireCameraFrame(SDL_Camera *camera, Uint64 *timestampNS)
  965. {
  966. if (timestampNS) {
  967. *timestampNS = 0;
  968. }
  969. if (!camera) {
  970. SDL_InvalidParamError("camera");
  971. return NULL;
  972. }
  973. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  974. ObtainPhysicalCameraDeviceObj(device);
  975. if (device->permission <= 0) {
  976. ReleaseCameraDevice(device);
  977. SDL_SetError("Camera permission has not been granted");
  978. return NULL;
  979. }
  980. SDL_Surface *retval = NULL;
  981. // frames are in this list from newest to oldest, so find the end of the list...
  982. SurfaceList *slistprev = &device->filled_output_surfaces;
  983. SurfaceList *slist = slistprev;
  984. while (slist->next) {
  985. slistprev = slist;
  986. slist = slist->next;
  987. }
  988. const SDL_bool list_is_empty = (slist == slistprev);
  989. if (!list_is_empty) { // report the oldest frame.
  990. if (timestampNS) {
  991. *timestampNS = slist->timestampNS;
  992. }
  993. retval = slist->surface;
  994. slistprev->next = slist->next; // remove from filled list.
  995. slist->next = device->app_held_output_surfaces.next; // add to app_held list.
  996. device->app_held_output_surfaces.next = slist;
  997. }
  998. ReleaseCameraDevice(device);
  999. return retval;
  1000. }
  1001. int SDL_ReleaseCameraFrame(SDL_Camera *camera, SDL_Surface *frame)
  1002. {
  1003. if (!camera) {
  1004. return SDL_InvalidParamError("camera");
  1005. } else if (frame == NULL) {
  1006. return SDL_InvalidParamError("frame");
  1007. }
  1008. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  1009. ObtainPhysicalCameraDeviceObj(device);
  1010. SurfaceList *slistprev = &device->app_held_output_surfaces;
  1011. SurfaceList *slist;
  1012. for (slist = slistprev->next; slist != NULL; slist = slist->next) {
  1013. if (slist->surface == frame) {
  1014. break;
  1015. }
  1016. slistprev = slist;
  1017. }
  1018. if (!slist) {
  1019. ReleaseCameraDevice(device);
  1020. return SDL_SetError("Surface was not acquired from this camera, or was already released");
  1021. }
  1022. // this pointer was owned by the backend (DMA memory or whatever), clear it out.
  1023. if (!device->needs_conversion && !device->needs_scaling) {
  1024. device->ReleaseFrame(device, frame);
  1025. frame->pixels = NULL;
  1026. frame->pitch = 0;
  1027. }
  1028. slist->timestampNS = 0;
  1029. // remove from app_held list...
  1030. slistprev->next = slist->next;
  1031. // insert at front of empty list (and we'll use it first when we need to fill a new frame).
  1032. slist->next = device->empty_output_surfaces.next;
  1033. device->empty_output_surfaces.next = slist;
  1034. ReleaseCameraDevice(device);
  1035. return 0;
  1036. }
  1037. SDL_CameraDeviceID SDL_GetCameraInstanceID(SDL_Camera *camera)
  1038. {
  1039. SDL_CameraDeviceID retval = 0;
  1040. if (!camera) {
  1041. SDL_InvalidParamError("camera");
  1042. } else {
  1043. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  1044. ObtainPhysicalCameraDeviceObj(device);
  1045. retval = device->instance_id;
  1046. ReleaseCameraDevice(device);
  1047. }
  1048. return retval;
  1049. }
  1050. SDL_PropertiesID SDL_GetCameraProperties(SDL_Camera *camera)
  1051. {
  1052. SDL_PropertiesID retval = 0;
  1053. if (!camera) {
  1054. SDL_InvalidParamError("camera");
  1055. } else {
  1056. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  1057. ObtainPhysicalCameraDeviceObj(device);
  1058. if (device->props == 0) {
  1059. device->props = SDL_CreateProperties();
  1060. }
  1061. retval = device->props;
  1062. ReleaseCameraDevice(device);
  1063. }
  1064. return retval;
  1065. }
  1066. int SDL_GetCameraPermissionState(SDL_Camera *camera)
  1067. {
  1068. int retval;
  1069. if (!camera) {
  1070. retval = SDL_InvalidParamError("camera");
  1071. } else {
  1072. SDL_CameraDevice *device = (SDL_CameraDevice *) camera; // currently there's no separation between physical and logical device.
  1073. ObtainPhysicalCameraDeviceObj(device);
  1074. retval = device->permission;
  1075. ReleaseCameraDevice(device);
  1076. }
  1077. return retval;
  1078. }
  1079. static void CompleteCameraEntryPoints(void)
  1080. {
  1081. // this doesn't currently fill in stub implementations, it just asserts the backend filled them all in.
  1082. #define FILL_STUB(x) SDL_assert(camera_driver.impl.x != NULL)
  1083. FILL_STUB(DetectDevices);
  1084. FILL_STUB(OpenDevice);
  1085. FILL_STUB(CloseDevice);
  1086. FILL_STUB(AcquireFrame);
  1087. FILL_STUB(ReleaseFrame);
  1088. FILL_STUB(FreeDeviceHandle);
  1089. FILL_STUB(Deinitialize);
  1090. #undef FILL_STUB
  1091. }
  1092. void SDL_QuitCamera(void)
  1093. {
  1094. if (!camera_driver.name) { // not initialized?!
  1095. return;
  1096. }
  1097. SDL_LockRWLockForWriting(camera_driver.device_hash_lock);
  1098. SDL_AtomicSet(&camera_driver.shutting_down, 1);
  1099. SDL_HashTable *device_hash = camera_driver.device_hash;
  1100. camera_driver.device_hash = NULL;
  1101. SDL_PendingCameraDeviceEvent *pending_events = camera_driver.pending_events.next;
  1102. camera_driver.pending_events.next = NULL;
  1103. SDL_AtomicSet(&camera_driver.device_count, 0);
  1104. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  1105. SDL_PendingCameraDeviceEvent *pending_next = NULL;
  1106. for (SDL_PendingCameraDeviceEvent *i = pending_events; i; i = pending_next) {
  1107. pending_next = i->next;
  1108. SDL_free(i);
  1109. }
  1110. const void *key;
  1111. const void *value;
  1112. void *iter = NULL;
  1113. while (SDL_IterateHashTable(device_hash, &key, &value, &iter)) {
  1114. DestroyPhysicalCameraDevice((SDL_CameraDevice *) value);
  1115. }
  1116. // Free the driver data
  1117. camera_driver.impl.Deinitialize();
  1118. SDL_DestroyRWLock(camera_driver.device_hash_lock);
  1119. SDL_DestroyHashTable(device_hash);
  1120. SDL_zero(camera_driver);
  1121. }
  1122. static Uint32 HashCameraDeviceID(const void *key, void *data)
  1123. {
  1124. // The values are unique incrementing integers, starting at 1, so just return minus 1 to start with bucket zero.
  1125. return ((Uint32) ((uintptr_t) key)) - 1;
  1126. }
  1127. static SDL_bool MatchCameraDeviceID(const void *a, const void *b, void *data)
  1128. {
  1129. return (a == b); // simple integers, just compare them as pointer values.
  1130. }
  1131. static void NukeCameraDeviceHashItem(const void *key, const void *value, void *data)
  1132. {
  1133. // no-op, keys and values in this hashtable are treated as Plain Old Data and don't get freed here.
  1134. }
  1135. int SDL_CameraInit(const char *driver_name)
  1136. {
  1137. if (SDL_GetCurrentCameraDriver()) {
  1138. SDL_QuitCamera(); // shutdown driver if already running.
  1139. }
  1140. SDL_RWLock *device_hash_lock = SDL_CreateRWLock(); // create this early, so if it fails we don't have to tear down the whole camera subsystem.
  1141. if (!device_hash_lock) {
  1142. return -1;
  1143. }
  1144. SDL_HashTable *device_hash = SDL_CreateHashTable(NULL, 8, HashCameraDeviceID, MatchCameraDeviceID, NukeCameraDeviceHashItem, SDL_FALSE);
  1145. if (!device_hash) {
  1146. SDL_DestroyRWLock(device_hash_lock);
  1147. return -1;
  1148. }
  1149. // Select the proper camera driver
  1150. if (!driver_name) {
  1151. driver_name = SDL_GetHint(SDL_HINT_CAMERA_DRIVER);
  1152. }
  1153. SDL_bool initialized = SDL_FALSE;
  1154. SDL_bool tried_to_init = SDL_FALSE;
  1155. if (driver_name && (*driver_name != 0)) {
  1156. char *driver_name_copy = SDL_strdup(driver_name);
  1157. const char *driver_attempt = driver_name_copy;
  1158. if (!driver_name_copy) {
  1159. SDL_DestroyRWLock(device_hash_lock);
  1160. SDL_DestroyHashTable(device_hash);
  1161. return -1;
  1162. }
  1163. while (driver_attempt && (*driver_attempt != 0) && !initialized) {
  1164. char *driver_attempt_end = SDL_strchr(driver_attempt, ',');
  1165. if (driver_attempt_end) {
  1166. *driver_attempt_end = '\0';
  1167. }
  1168. for (int i = 0; bootstrap[i]; i++) {
  1169. if (SDL_strcasecmp(bootstrap[i]->name, driver_attempt) == 0) {
  1170. tried_to_init = SDL_TRUE;
  1171. SDL_zero(camera_driver);
  1172. camera_driver.pending_events_tail = &camera_driver.pending_events;
  1173. camera_driver.device_hash_lock = device_hash_lock;
  1174. camera_driver.device_hash = device_hash;
  1175. if (bootstrap[i]->init(&camera_driver.impl)) {
  1176. camera_driver.name = bootstrap[i]->name;
  1177. camera_driver.desc = bootstrap[i]->desc;
  1178. initialized = SDL_TRUE;
  1179. }
  1180. break;
  1181. }
  1182. }
  1183. driver_attempt = (driver_attempt_end) ? (driver_attempt_end + 1) : NULL;
  1184. }
  1185. SDL_free(driver_name_copy);
  1186. } else {
  1187. for (int i = 0; !initialized && bootstrap[i]; i++) {
  1188. if (bootstrap[i]->demand_only) {
  1189. continue;
  1190. }
  1191. tried_to_init = SDL_TRUE;
  1192. SDL_zero(camera_driver);
  1193. camera_driver.pending_events_tail = &camera_driver.pending_events;
  1194. camera_driver.device_hash_lock = device_hash_lock;
  1195. camera_driver.device_hash = device_hash;
  1196. if (bootstrap[i]->init(&camera_driver.impl)) {
  1197. camera_driver.name = bootstrap[i]->name;
  1198. camera_driver.desc = bootstrap[i]->desc;
  1199. initialized = SDL_TRUE;
  1200. }
  1201. }
  1202. }
  1203. if (!initialized) {
  1204. // specific drivers will set the error message if they fail, but otherwise we do it here.
  1205. if (!tried_to_init) {
  1206. if (driver_name) {
  1207. SDL_SetError("Camera driver '%s' not available", driver_name);
  1208. } else {
  1209. SDL_SetError("No available camera driver");
  1210. }
  1211. }
  1212. SDL_zero(camera_driver);
  1213. SDL_DestroyRWLock(device_hash_lock);
  1214. SDL_DestroyHashTable(device_hash);
  1215. return -1; // No driver was available, so fail.
  1216. }
  1217. CompleteCameraEntryPoints();
  1218. // Make sure we have a list of devices available at startup...
  1219. camera_driver.impl.DetectDevices();
  1220. return 0;
  1221. }
  1222. // This is an internal function, so SDL_PumpEvents() can check for pending camera device events.
  1223. // ("UpdateSubsystem" is the same naming that the other things that hook into PumpEvents use.)
  1224. void SDL_UpdateCamera(void)
  1225. {
  1226. SDL_LockRWLockForReading(camera_driver.device_hash_lock);
  1227. SDL_PendingCameraDeviceEvent *pending_events = camera_driver.pending_events.next;
  1228. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  1229. if (!pending_events) {
  1230. return; // nothing to do, check next time.
  1231. }
  1232. // okay, let's take this whole list of events so we can dump the lock, and new ones can queue up for a later update.
  1233. SDL_LockRWLockForWriting(camera_driver.device_hash_lock);
  1234. pending_events = camera_driver.pending_events.next; // in case this changed...
  1235. camera_driver.pending_events.next = NULL;
  1236. camera_driver.pending_events_tail = &camera_driver.pending_events;
  1237. SDL_UnlockRWLock(camera_driver.device_hash_lock);
  1238. SDL_PendingCameraDeviceEvent *pending_next = NULL;
  1239. for (SDL_PendingCameraDeviceEvent *i = pending_events; i; i = pending_next) {
  1240. pending_next = i->next;
  1241. if (SDL_EventEnabled(i->type)) {
  1242. SDL_Event event;
  1243. SDL_zero(event);
  1244. event.type = i->type;
  1245. event.adevice.which = (Uint32) i->devid;
  1246. SDL_PushEvent(&event);
  1247. }
  1248. SDL_free(i);
  1249. }
  1250. }