SDL_camera.c 55 KB

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