obs-scene.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Philippe Groarke <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "util/threading.h"
  16. #include "graphics/math-defs.h"
  17. #include "obs-scene.h"
  18. /* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
  19. * lock the graphics mutex inside either of the scene mutexes.
  20. *
  21. * Another thing that must be done to prevent that cross-lock (and improve
  22. * performance), is to not create/release/update sources within the scene
  23. * mutexes.
  24. *
  25. * It's okay to lock the graphics mutex before locking either of the scene
  26. * mutexes, but not after.
  27. */
  28. static const char *obs_scene_signals[] = {
  29. "void item_add(ptr scene, ptr item)",
  30. "void item_remove(ptr scene, ptr item)",
  31. "void reorder(ptr scene)",
  32. "void item_visible(ptr scene, ptr item, bool visible)",
  33. "void item_select(ptr scene, ptr item)",
  34. "void item_deselect(ptr scene, ptr item)",
  35. "void item_transform(ptr scene, ptr item)",
  36. NULL
  37. };
  38. static inline void signal_item_remove(struct obs_scene_item *item)
  39. {
  40. struct calldata params;
  41. uint8_t stack[128];
  42. calldata_init_fixed(&params, stack, sizeof(stack));
  43. calldata_set_ptr(&params, "scene", item->parent);
  44. calldata_set_ptr(&params, "item", item);
  45. signal_handler_signal(item->parent->source->context.signals,
  46. "item_remove", &params);
  47. }
  48. static const char *scene_getname(void *unused)
  49. {
  50. UNUSED_PARAMETER(unused);
  51. return "Scene";
  52. }
  53. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  54. {
  55. pthread_mutexattr_t attr;
  56. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  57. scene->source = source;
  58. scene->first_item = NULL;
  59. signal_handler_add_array(obs_source_get_signal_handler(source),
  60. obs_scene_signals);
  61. if (pthread_mutexattr_init(&attr) != 0)
  62. goto fail;
  63. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  64. goto fail;
  65. if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
  66. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  67. "mutex");
  68. goto fail;
  69. }
  70. if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
  71. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  72. "mutex");
  73. goto fail;
  74. }
  75. UNUSED_PARAMETER(settings);
  76. return scene;
  77. fail:
  78. pthread_mutexattr_destroy(&attr);
  79. bfree(scene);
  80. return NULL;
  81. }
  82. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  83. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  84. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  85. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  86. static inline void full_lock(struct obs_scene *scene)
  87. {
  88. video_lock(scene);
  89. audio_lock(scene);
  90. }
  91. static inline void full_unlock(struct obs_scene *scene)
  92. {
  93. audio_unlock(scene);
  94. video_unlock(scene);
  95. }
  96. static void set_visibility(struct obs_scene_item *item, bool vis);
  97. static inline void detach_sceneitem(struct obs_scene_item *item);
  98. static inline void remove_without_release(struct obs_scene_item *item)
  99. {
  100. item->removed = true;
  101. set_visibility(item, false);
  102. signal_item_remove(item);
  103. detach_sceneitem(item);
  104. }
  105. static void remove_all_items(struct obs_scene *scene)
  106. {
  107. struct obs_scene_item *item;
  108. DARRAY(struct obs_scene_item*) items;
  109. da_init(items);
  110. full_lock(scene);
  111. item = scene->first_item;
  112. while (item) {
  113. struct obs_scene_item *del_item = item;
  114. item = item->next;
  115. remove_without_release(del_item);
  116. da_push_back(items, &del_item);
  117. }
  118. full_unlock(scene);
  119. for (size_t i = 0; i < items.num; i++)
  120. obs_sceneitem_release(items.array[i]);
  121. da_free(items);
  122. }
  123. static void scene_destroy(void *data)
  124. {
  125. struct obs_scene *scene = data;
  126. remove_all_items(scene);
  127. pthread_mutex_destroy(&scene->video_mutex);
  128. pthread_mutex_destroy(&scene->audio_mutex);
  129. bfree(scene);
  130. }
  131. static void scene_enum_sources(void *data,
  132. obs_source_enum_proc_t enum_callback,
  133. void *param)
  134. {
  135. struct obs_scene *scene = data;
  136. struct obs_scene_item *item;
  137. struct obs_scene_item *next;
  138. full_lock(scene);
  139. item = scene->first_item;
  140. while (item) {
  141. next = item->next;
  142. obs_sceneitem_addref(item);
  143. if (os_atomic_load_long(&item->active_refs) > 0)
  144. enum_callback(scene->source, item->source, param);
  145. obs_sceneitem_release(item);
  146. item = next;
  147. }
  148. full_unlock(scene);
  149. }
  150. static inline void detach_sceneitem(struct obs_scene_item *item)
  151. {
  152. if (item->prev)
  153. item->prev->next = item->next;
  154. else
  155. item->parent->first_item = item->next;
  156. if (item->next)
  157. item->next->prev = item->prev;
  158. item->parent = NULL;
  159. }
  160. static inline void attach_sceneitem(struct obs_scene *parent,
  161. struct obs_scene_item *item, struct obs_scene_item *prev)
  162. {
  163. item->prev = prev;
  164. item->parent = parent;
  165. if (prev) {
  166. item->next = prev->next;
  167. if (prev->next)
  168. prev->next->prev = item;
  169. prev->next = item;
  170. } else {
  171. item->next = parent->first_item;
  172. if (parent->first_item)
  173. parent->first_item->prev = item;
  174. parent->first_item = item;
  175. }
  176. }
  177. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  178. {
  179. if (align & OBS_ALIGN_RIGHT)
  180. v->x += (float)cx;
  181. else if ((align & OBS_ALIGN_LEFT) == 0)
  182. v->x += (float)(cx / 2);
  183. if (align & OBS_ALIGN_BOTTOM)
  184. v->y += (float)cy;
  185. else if ((align & OBS_ALIGN_TOP) == 0)
  186. v->y += (float)(cy / 2);
  187. }
  188. static void calculate_bounds_data(struct obs_scene_item *item,
  189. struct vec2 *origin, struct vec2 *scale,
  190. uint32_t *cx, uint32_t *cy)
  191. {
  192. float width = (float)(*cx) * fabsf(scale->x);
  193. float height = (float)(*cy) * fabsf(scale->y);
  194. float item_aspect = width / height;
  195. float bounds_aspect = item->bounds.x / item->bounds.y;
  196. uint32_t bounds_type = item->bounds_type;
  197. float width_diff, height_diff;
  198. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  199. if (width > item->bounds.x || height > item->bounds.y)
  200. bounds_type = OBS_BOUNDS_SCALE_INNER;
  201. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  202. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  203. bool use_width = (bounds_aspect < item_aspect);
  204. float mul;
  205. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  206. use_width = !use_width;
  207. mul = use_width ?
  208. item->bounds.x / width :
  209. item->bounds.y / height;
  210. vec2_mulf(scale, scale, mul);
  211. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  212. vec2_mulf(scale, scale, item->bounds.x / width);
  213. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  214. vec2_mulf(scale, scale, item->bounds.y / height);
  215. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  216. scale->x = item->bounds.x / (float)(*cx);
  217. scale->y = item->bounds.y / (float)(*cy);
  218. }
  219. width = (float)(*cx) * scale->x;
  220. height = (float)(*cy) * scale->y;
  221. width_diff = item->bounds.x - width;
  222. height_diff = item->bounds.y - height;
  223. *cx = (uint32_t)item->bounds.x;
  224. *cy = (uint32_t)item->bounds.y;
  225. add_alignment(origin, item->bounds_align,
  226. (int)-width_diff, (int)-height_diff);
  227. }
  228. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  229. uint32_t width)
  230. {
  231. uint32_t crop_cx = item->crop.left + item->crop.right;
  232. return (crop_cx > width) ? 2 : (width - crop_cx);
  233. }
  234. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  235. uint32_t height)
  236. {
  237. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  238. return (crop_cy > height) ? 2 : (height - crop_cy);
  239. }
  240. static void update_item_transform(struct obs_scene_item *item)
  241. {
  242. uint32_t width = obs_source_get_width(item->source);
  243. uint32_t height = obs_source_get_height(item->source);
  244. uint32_t cx = calc_cx(item, width);
  245. uint32_t cy = calc_cy(item, height);
  246. struct vec2 base_origin;
  247. struct vec2 origin;
  248. struct vec2 scale = item->scale;
  249. struct calldata params;
  250. uint8_t stack[128];
  251. if (os_atomic_load_long(&item->defer_update) > 0)
  252. return;
  253. width = cx;
  254. height = cy;
  255. vec2_zero(&base_origin);
  256. vec2_zero(&origin);
  257. /* ----------------------- */
  258. if (item->bounds_type != OBS_BOUNDS_NONE) {
  259. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  260. } else {
  261. cx = (uint32_t)((float)cx * scale.x);
  262. cy = (uint32_t)((float)cy * scale.y);
  263. }
  264. add_alignment(&origin, item->align, (int)cx, (int)cy);
  265. matrix4_identity(&item->draw_transform);
  266. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  267. scale.x, scale.y, 1.0f);
  268. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  269. -origin.x, -origin.y, 0.0f);
  270. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  271. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  272. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  273. item->pos.x, item->pos.y, 0.0f);
  274. /* ----------------------- */
  275. if (item->bounds_type != OBS_BOUNDS_NONE) {
  276. vec2_copy(&scale, &item->bounds);
  277. } else {
  278. scale.x = (float)width * item->scale.x;
  279. scale.y = (float)height * item->scale.y;
  280. }
  281. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  282. matrix4_identity(&item->box_transform);
  283. matrix4_scale3f(&item->box_transform, &item->box_transform,
  284. scale.x, scale.y, 1.0f);
  285. matrix4_translate3f(&item->box_transform, &item->box_transform,
  286. -base_origin.x, -base_origin.y, 0.0f);
  287. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  288. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  289. matrix4_translate3f(&item->box_transform, &item->box_transform,
  290. item->pos.x, item->pos.y, 0.0f);
  291. /* ----------------------- */
  292. item->last_width = width;
  293. item->last_height = height;
  294. calldata_init_fixed(&params, stack, sizeof(stack));
  295. calldata_set_ptr(&params, "scene", item->parent);
  296. calldata_set_ptr(&params, "item", item);
  297. signal_handler_signal(item->parent->source->context.signals,
  298. "item_transform", &params);
  299. }
  300. static inline bool source_size_changed(struct obs_scene_item *item)
  301. {
  302. uint32_t width = obs_source_get_width(item->source);
  303. uint32_t height = obs_source_get_height(item->source);
  304. return item->last_width != width || item->last_height != height;
  305. }
  306. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  307. {
  308. return crop->left || crop->right || crop->top || crop->bottom;
  309. }
  310. static inline void render_item(struct obs_scene_item *item)
  311. {
  312. if (item->crop_render) {
  313. uint32_t width = obs_source_get_width(item->source);
  314. uint32_t height = obs_source_get_height(item->source);
  315. uint32_t cx = calc_cx(item, width);
  316. uint32_t cy = calc_cy(item, height);
  317. if (cx && cy && gs_texrender_begin(item->crop_render, cx, cy)) {
  318. float cx_scale = (float)width / (float)cx;
  319. float cy_scale = (float)height / (float)cy;
  320. struct vec4 clear_color;
  321. vec4_zero(&clear_color);
  322. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  323. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  324. -100.0f, 100.0f);
  325. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  326. gs_matrix_translate3f(
  327. -(float)item->crop.left,
  328. -(float)item->crop.top,
  329. 0.0f);
  330. obs_source_video_render(item->source);
  331. gs_texrender_end(item->crop_render);
  332. }
  333. }
  334. gs_matrix_push();
  335. gs_matrix_mul(&item->draw_transform);
  336. if (item->crop_render) {
  337. gs_texture_t *tex = gs_texrender_get_texture(item->crop_render);
  338. while (gs_effect_loop(obs->video.default_effect, "Draw"))
  339. obs_source_draw(tex, 0, 0, 0, 0, 0);
  340. } else {
  341. obs_source_video_render(item->source);
  342. }
  343. gs_matrix_pop();
  344. }
  345. static void scene_video_tick(void *data, float seconds)
  346. {
  347. struct obs_scene *scene = data;
  348. struct obs_scene_item *item;
  349. video_lock(scene);
  350. item = scene->first_item;
  351. while (item) {
  352. if (item->crop_render)
  353. gs_texrender_reset(item->crop_render);
  354. item = item->next;
  355. }
  356. video_unlock(scene);
  357. UNUSED_PARAMETER(seconds);
  358. }
  359. static void scene_video_render(void *data, gs_effect_t *effect)
  360. {
  361. DARRAY(struct obs_scene_item*) remove_items;
  362. struct obs_scene *scene = data;
  363. struct obs_scene_item *item;
  364. da_init(remove_items);
  365. video_lock(scene);
  366. item = scene->first_item;
  367. gs_blend_state_push();
  368. gs_reset_blend_state();
  369. while (item) {
  370. if (obs_source_removed(item->source)) {
  371. struct obs_scene_item *del_item = item;
  372. item = item->next;
  373. remove_without_release(del_item);
  374. da_push_back(remove_items, &del_item);
  375. continue;
  376. }
  377. if (source_size_changed(item))
  378. update_item_transform(item);
  379. if (item->user_visible)
  380. render_item(item);
  381. item = item->next;
  382. }
  383. gs_blend_state_pop();
  384. video_unlock(scene);
  385. for (size_t i = 0; i < remove_items.num; i++)
  386. obs_sceneitem_release(remove_items.array[i]);
  387. da_free(remove_items);
  388. UNUSED_PARAMETER(effect);
  389. }
  390. static void set_visibility(struct obs_scene_item *item, bool vis)
  391. {
  392. pthread_mutex_lock(&item->actions_mutex);
  393. da_resize(item->audio_actions, 0);
  394. if (os_atomic_load_long(&item->active_refs) > 0) {
  395. if (!vis)
  396. obs_source_remove_active_child(item->parent->source,
  397. item->source);
  398. } else if (vis) {
  399. obs_source_add_active_child(item->parent->source, item->source);
  400. }
  401. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  402. item->visible = vis;
  403. item->user_visible = vis;
  404. pthread_mutex_unlock(&item->actions_mutex);
  405. }
  406. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  407. {
  408. const char *name = obs_data_get_string(item_data, "name");
  409. obs_source_t *source = obs_get_source_by_name(name);
  410. struct obs_scene_item *item;
  411. bool visible;
  412. if (!source) {
  413. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  414. name);
  415. return;
  416. }
  417. item = obs_scene_add(scene, source);
  418. if (!item) {
  419. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  420. "to scene '%s'!",
  421. name, obs_source_get_name(scene->source));
  422. obs_source_release(source);
  423. return;
  424. }
  425. obs_data_set_default_int(item_data, "align",
  426. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  427. item->rot = (float)obs_data_get_double(item_data, "rot");
  428. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  429. visible = obs_data_get_bool(item_data, "visible");
  430. obs_data_get_vec2(item_data, "pos", &item->pos);
  431. obs_data_get_vec2(item_data, "scale", &item->scale);
  432. set_visibility(item, visible);
  433. item->bounds_type =
  434. (enum obs_bounds_type)obs_data_get_int(item_data,
  435. "bounds_type");
  436. item->bounds_align =
  437. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  438. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  439. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  440. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  441. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  442. item->crop.bottom = (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  443. if (item->crop_render && !crop_enabled(&item->crop)) {
  444. obs_enter_graphics();
  445. gs_texrender_destroy(item->crop_render);
  446. item->crop_render = NULL;
  447. obs_leave_graphics();
  448. } else if (!item->crop_render && crop_enabled(&item->crop)) {
  449. obs_enter_graphics();
  450. item->crop_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  451. obs_leave_graphics();
  452. }
  453. obs_source_release(source);
  454. update_item_transform(item);
  455. }
  456. static void scene_load(void *scene, obs_data_t *settings)
  457. {
  458. obs_data_array_t *items = obs_data_get_array(settings, "items");
  459. size_t count, i;
  460. remove_all_items(scene);
  461. if (!items) return;
  462. count = obs_data_array_count(items);
  463. for (i = 0; i < count; i++) {
  464. obs_data_t *item_data = obs_data_array_item(items, i);
  465. scene_load_item(scene, item_data);
  466. obs_data_release(item_data);
  467. }
  468. obs_data_array_release(items);
  469. }
  470. static void scene_save_item(obs_data_array_t *array,
  471. struct obs_scene_item *item)
  472. {
  473. obs_data_t *item_data = obs_data_create();
  474. const char *name = obs_source_get_name(item->source);
  475. obs_data_set_string(item_data, "name", name);
  476. obs_data_set_bool (item_data, "visible", item->user_visible);
  477. obs_data_set_double(item_data, "rot", item->rot);
  478. obs_data_set_vec2 (item_data, "pos", &item->pos);
  479. obs_data_set_vec2 (item_data, "scale", &item->scale);
  480. obs_data_set_int (item_data, "align", (int)item->align);
  481. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  482. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  483. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  484. obs_data_set_int (item_data, "crop_left", (int)item->crop.left);
  485. obs_data_set_int (item_data, "crop_top", (int)item->crop.top);
  486. obs_data_set_int (item_data, "crop_right", (int)item->crop.right);
  487. obs_data_set_int (item_data, "crop_bottom", (int)item->crop.bottom);
  488. obs_data_array_push_back(array, item_data);
  489. obs_data_release(item_data);
  490. }
  491. static void scene_save(void *data, obs_data_t *settings)
  492. {
  493. struct obs_scene *scene = data;
  494. obs_data_array_t *array = obs_data_array_create();
  495. struct obs_scene_item *item;
  496. full_lock(scene);
  497. item = scene->first_item;
  498. while (item) {
  499. scene_save_item(array, item);
  500. item = item->next;
  501. }
  502. full_unlock(scene);
  503. obs_data_set_array(settings, "items", array);
  504. obs_data_array_release(array);
  505. }
  506. static uint32_t scene_getwidth(void *data)
  507. {
  508. UNUSED_PARAMETER(data);
  509. return obs->video.base_width;
  510. }
  511. static uint32_t scene_getheight(void *data)
  512. {
  513. UNUSED_PARAMETER(data);
  514. return obs->video.base_height;
  515. }
  516. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  517. float **p_buf, uint64_t ts, size_t sample_rate)
  518. {
  519. bool cur_visible = item->visible;
  520. uint64_t frame_num = 0;
  521. size_t deref_count = 0;
  522. float *buf;
  523. if (!*p_buf)
  524. *p_buf = malloc(AUDIO_OUTPUT_FRAMES * sizeof(float));
  525. buf = *p_buf;
  526. pthread_mutex_lock(&item->actions_mutex);
  527. for (size_t i = 0; i < item->audio_actions.num; i++) {
  528. struct item_action action = item->audio_actions.array[i];
  529. uint64_t timestamp = action.timestamp;
  530. uint64_t new_frame_num;
  531. if (timestamp < ts)
  532. timestamp = ts;
  533. new_frame_num = (timestamp - ts) * (uint64_t)sample_rate /
  534. 1000000000ULL;
  535. if (new_frame_num >= AUDIO_OUTPUT_FRAMES)
  536. break;
  537. da_erase(item->audio_actions, i--);
  538. item->visible = action.visible;
  539. if (!item->visible)
  540. deref_count++;
  541. if (new_frame_num > frame_num) {
  542. for (; frame_num < new_frame_num; frame_num++)
  543. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  544. }
  545. cur_visible = item->visible;
  546. }
  547. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  548. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  549. pthread_mutex_unlock(&item->actions_mutex);
  550. while (deref_count--) {
  551. if (os_atomic_dec_long(&item->active_refs) == 0) {
  552. obs_source_remove_active_child(item->parent->source,
  553. item->source);
  554. }
  555. }
  556. }
  557. static inline bool apply_scene_item_volume(struct obs_scene_item *item,
  558. float **buf, uint64_t ts, size_t sample_rate)
  559. {
  560. bool actions_pending;
  561. struct item_action action;
  562. pthread_mutex_lock(&item->actions_mutex);
  563. actions_pending = item->audio_actions.num > 0;
  564. if (actions_pending)
  565. action = item->audio_actions.array[0];
  566. pthread_mutex_unlock(&item->actions_mutex);
  567. if (actions_pending) {
  568. uint64_t duration = (uint64_t)AUDIO_OUTPUT_FRAMES *
  569. 1000000000ULL / (uint64_t)sample_rate;
  570. if (action.timestamp < (ts + duration)) {
  571. apply_scene_item_audio_actions(item, buf, ts,
  572. sample_rate);
  573. return true;
  574. }
  575. }
  576. return false;
  577. }
  578. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  579. size_t pos, size_t count)
  580. {
  581. register float *out = p_out;
  582. register float *buf = buf_in + pos;
  583. register float *in = p_in + pos;
  584. register float *end = in + count;
  585. while (in < end)
  586. *(out++) += *(in++) * *(buf++);
  587. }
  588. static inline void mix_audio(float *p_out, float *p_in,
  589. size_t pos, size_t count)
  590. {
  591. register float *out = p_out;
  592. register float *in = p_in + pos;
  593. register float *end = in + count;
  594. while (in < end)
  595. *(out++) += *(in++);
  596. }
  597. static bool scene_audio_render(void *data, uint64_t *ts_out,
  598. struct obs_source_audio_mix *audio_output, uint32_t mixers,
  599. size_t channels, size_t sample_rate)
  600. {
  601. uint64_t timestamp = 0;
  602. float *buf = NULL;
  603. struct obs_source_audio_mix child_audio;
  604. struct obs_scene *scene = data;
  605. struct obs_scene_item *item;
  606. audio_lock(scene);
  607. item = scene->first_item;
  608. while (item) {
  609. if (!obs_source_audio_pending(item->source)) {
  610. uint64_t source_ts =
  611. obs_source_get_audio_timestamp(item->source);
  612. if (!timestamp || source_ts < timestamp)
  613. timestamp = source_ts;
  614. }
  615. item = item->next;
  616. }
  617. if (!timestamp) {
  618. audio_unlock(scene);
  619. return false;
  620. }
  621. item = scene->first_item;
  622. while (item) {
  623. uint64_t source_ts;
  624. size_t pos, count;
  625. bool apply_buf;
  626. apply_buf = apply_scene_item_volume(item, &buf, timestamp,
  627. sample_rate);
  628. if (obs_source_audio_pending(item->source)) {
  629. item = item->next;
  630. continue;
  631. }
  632. source_ts = obs_source_get_audio_timestamp(item->source);
  633. pos = (size_t)ns_to_audio_frames(sample_rate,
  634. source_ts - timestamp);
  635. count = AUDIO_OUTPUT_FRAMES - pos;
  636. if (!apply_buf && !item->visible) {
  637. item = item->next;
  638. continue;
  639. }
  640. obs_source_get_audio_mix(item->source, &child_audio);
  641. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  642. if ((mixers & (1 << mix)) == 0)
  643. continue;
  644. for (size_t ch = 0; ch < channels; ch++) {
  645. float *out = audio_output->output[mix].data[ch];
  646. float *in = child_audio.output[mix].data[ch];
  647. if (apply_buf)
  648. mix_audio_with_buf(out, in, buf, pos,
  649. count);
  650. else
  651. mix_audio(out, in, pos, count);
  652. }
  653. }
  654. item = item->next;
  655. }
  656. *ts_out = timestamp;
  657. audio_unlock(scene);
  658. free(buf);
  659. return true;
  660. }
  661. const struct obs_source_info scene_info =
  662. {
  663. .id = "scene",
  664. .type = OBS_SOURCE_TYPE_SCENE,
  665. .output_flags = OBS_SOURCE_VIDEO |
  666. OBS_SOURCE_CUSTOM_DRAW |
  667. OBS_SOURCE_COMPOSITE,
  668. .get_name = scene_getname,
  669. .create = scene_create,
  670. .destroy = scene_destroy,
  671. .video_tick = scene_video_tick,
  672. .video_render = scene_video_render,
  673. .audio_render = scene_audio_render,
  674. .get_width = scene_getwidth,
  675. .get_height = scene_getheight,
  676. .load = scene_load,
  677. .save = scene_save,
  678. .enum_active_sources = scene_enum_sources
  679. };
  680. obs_scene_t *obs_scene_create(const char *name)
  681. {
  682. struct obs_source *source = obs_source_create("scene", name, NULL,
  683. NULL);
  684. return source->context.data;
  685. }
  686. obs_scene_t *obs_scene_create_private(const char *name)
  687. {
  688. struct obs_source *source = obs_source_create_private("scene", name,
  689. NULL);
  690. return source->context.data;
  691. }
  692. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  693. {
  694. struct obs_scene_item *item = scene->first_item;
  695. while (item && idx--)
  696. item = item->next;
  697. return item ? item->source : NULL;
  698. }
  699. static inline obs_source_t *dup_child(struct darray *array, size_t idx,
  700. obs_scene_t *new_scene, bool private)
  701. {
  702. DARRAY(struct obs_scene_item*) old_items;
  703. obs_source_t *source;
  704. old_items.da = *array;
  705. source = old_items.array[idx]->source;
  706. /* if the old item is referenced more than once in the old scene,
  707. * make sure they're referenced similarly in the new scene to reduce
  708. * load times */
  709. for (size_t i = 0; i < idx; i++) {
  710. struct obs_scene_item *item = old_items.array[i];
  711. if (item->source == source) {
  712. source = get_child_at_idx(new_scene, i);
  713. obs_source_addref(source);
  714. return source;
  715. }
  716. }
  717. return obs_source_duplicate(source, NULL, private);
  718. }
  719. static inline obs_source_t *new_ref(obs_source_t *source)
  720. {
  721. obs_source_addref(source);
  722. return source;
  723. }
  724. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  725. enum obs_scene_duplicate_type type)
  726. {
  727. bool make_unique = ((int)type & (1<<0)) != 0;
  728. bool make_private = ((int)type & (1<<1)) != 0;
  729. DARRAY(struct obs_scene_item*) items;
  730. struct obs_scene *new_scene;
  731. struct obs_scene_item *item;
  732. struct obs_source *source;
  733. da_init(items);
  734. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  735. return NULL;
  736. /* --------------------------------- */
  737. full_lock(scene);
  738. item = scene->first_item;
  739. while (item) {
  740. da_push_back(items, &item);
  741. obs_sceneitem_addref(item);
  742. item = item->next;
  743. }
  744. full_unlock(scene);
  745. /* --------------------------------- */
  746. new_scene = make_private ?
  747. obs_scene_create_private(name) : obs_scene_create(name);
  748. for (size_t i = 0; i < items.num; i++) {
  749. item = items.array[i];
  750. source = make_unique ?
  751. dup_child(&items.da, i, new_scene, make_private) :
  752. new_ref(item->source);
  753. if (source) {
  754. struct obs_scene_item *new_item =
  755. obs_scene_add(new_scene, source);
  756. if (!new_item) {
  757. obs_source_release(source);
  758. continue;
  759. }
  760. if (!item->user_visible)
  761. set_visibility(new_item, false);
  762. new_item->selected = item->selected;
  763. new_item->pos = item->pos;
  764. new_item->rot = item->rot;
  765. new_item->scale = item->scale;
  766. new_item->align = item->align;
  767. new_item->last_width = item->last_width;
  768. new_item->last_height = item->last_height;
  769. new_item->box_transform = item->box_transform;
  770. new_item->draw_transform = item->draw_transform;
  771. new_item->bounds_type = item->bounds_type;
  772. new_item->bounds_align = item->bounds_align;
  773. new_item->bounds = item->bounds;
  774. obs_sceneitem_set_crop(new_item, &item->crop);
  775. obs_source_release(source);
  776. }
  777. }
  778. for (size_t i = 0; i < items.num; i++)
  779. obs_sceneitem_release(items.array[i]);
  780. da_free(items);
  781. return new_scene;
  782. }
  783. void obs_scene_addref(obs_scene_t *scene)
  784. {
  785. if (scene)
  786. obs_source_addref(scene->source);
  787. }
  788. void obs_scene_release(obs_scene_t *scene)
  789. {
  790. if (scene)
  791. obs_source_release(scene->source);
  792. }
  793. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  794. {
  795. return scene ? scene->source : NULL;
  796. }
  797. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  798. {
  799. if (!source || source->info.id != scene_info.id)
  800. return NULL;
  801. return source->context.data;
  802. }
  803. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  804. {
  805. struct obs_scene_item *item;
  806. if (!scene)
  807. return NULL;
  808. full_lock(scene);
  809. item = scene->first_item;
  810. while (item) {
  811. if (strcmp(item->source->context.name, name) == 0)
  812. break;
  813. item = item->next;
  814. }
  815. full_unlock(scene);
  816. return item;
  817. }
  818. void obs_scene_enum_items(obs_scene_t *scene,
  819. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  820. void *param)
  821. {
  822. struct obs_scene_item *item;
  823. if (!scene || !callback)
  824. return;
  825. full_lock(scene);
  826. item = scene->first_item;
  827. while (item) {
  828. struct obs_scene_item *next = item->next;
  829. obs_sceneitem_addref(item);
  830. if (!callback(scene, item, param)) {
  831. obs_sceneitem_release(item);
  832. break;
  833. }
  834. obs_sceneitem_release(item);
  835. item = next;
  836. }
  837. full_unlock(scene);
  838. }
  839. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  840. {
  841. long owners = si->ref;
  842. while (owners > 0) {
  843. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  844. return si;
  845. owners = si->ref;
  846. }
  847. return NULL;
  848. }
  849. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  850. obs_hotkey_t *hotkey, bool pressed)
  851. {
  852. UNUSED_PARAMETER(id);
  853. UNUSED_PARAMETER(hotkey);
  854. obs_sceneitem_t *si = sceneitem_get_ref(data);
  855. if (pressed && si && !si->user_visible) {
  856. obs_sceneitem_set_visible(si, true);
  857. obs_sceneitem_release(si);
  858. return true;
  859. }
  860. obs_sceneitem_release(si);
  861. return false;
  862. }
  863. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  864. obs_hotkey_t *hotkey, bool pressed)
  865. {
  866. UNUSED_PARAMETER(id);
  867. UNUSED_PARAMETER(hotkey);
  868. obs_sceneitem_t *si = sceneitem_get_ref(data);
  869. if (pressed && si && si->user_visible) {
  870. obs_sceneitem_set_visible(si, false);
  871. obs_sceneitem_release(si);
  872. return true;
  873. }
  874. obs_sceneitem_release(si);
  875. return false;
  876. }
  877. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  878. const char *name)
  879. {
  880. struct dstr show = {0};
  881. struct dstr hide = {0};
  882. struct dstr show_desc = {0};
  883. struct dstr hide_desc = {0};
  884. dstr_copy(&show, "libobs.show_scene_item.%1");
  885. dstr_replace(&show, "%1", name);
  886. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  887. dstr_replace(&hide, "%1", name);
  888. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  889. dstr_replace(&show_desc, "%1", name);
  890. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  891. dstr_replace(&hide_desc, "%1", name);
  892. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  893. show.array, show_desc.array,
  894. hide.array, hide_desc.array,
  895. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  896. item, item);
  897. dstr_free(&show);
  898. dstr_free(&hide);
  899. dstr_free(&show_desc);
  900. dstr_free(&hide_desc);
  901. }
  902. static inline bool source_has_audio(obs_source_t *source)
  903. {
  904. return (source->info.output_flags &
  905. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  906. }
  907. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  908. {
  909. struct obs_scene_item *last;
  910. struct obs_scene_item *item;
  911. struct calldata params;
  912. uint8_t stack[128];
  913. pthread_mutex_t mutex;
  914. struct item_action action = {
  915. .visible = true,
  916. .timestamp = os_gettime_ns()
  917. };
  918. if (!scene)
  919. return NULL;
  920. if (!source) {
  921. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  922. return NULL;
  923. }
  924. if (pthread_mutex_init(&mutex, NULL) != 0) {
  925. blog(LOG_WARNING, "Failed to create scene item mutex");
  926. return NULL;
  927. }
  928. if (!obs_source_add_active_child(scene->source, source)) {
  929. blog(LOG_WARNING, "Failed to add source to scene due to "
  930. "infinite source recursion");
  931. pthread_mutex_destroy(&mutex);
  932. return NULL;
  933. }
  934. item = bzalloc(sizeof(struct obs_scene_item));
  935. item->source = source;
  936. item->parent = scene;
  937. item->ref = 1;
  938. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  939. item->actions_mutex = mutex;
  940. item->user_visible = true;
  941. os_atomic_set_long(&item->active_refs, 1);
  942. vec2_set(&item->scale, 1.0f, 1.0f);
  943. matrix4_identity(&item->draw_transform);
  944. matrix4_identity(&item->box_transform);
  945. obs_source_addref(source);
  946. if (source_has_audio(source)) {
  947. item->visible = false;
  948. da_push_back(item->audio_actions, &action);
  949. } else {
  950. item->visible = true;
  951. }
  952. full_lock(scene);
  953. last = scene->first_item;
  954. if (!last) {
  955. scene->first_item = item;
  956. } else {
  957. while (last->next)
  958. last = last->next;
  959. last->next = item;
  960. item->prev = last;
  961. }
  962. full_unlock(scene);
  963. if (!scene->source->context.private)
  964. init_hotkeys(scene, item, obs_source_get_name(source));
  965. calldata_init_fixed(&params, stack, sizeof(stack));
  966. calldata_set_ptr(&params, "scene", scene);
  967. calldata_set_ptr(&params, "item", item);
  968. signal_handler_signal(scene->source->context.signals, "item_add",
  969. &params);
  970. return item;
  971. }
  972. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  973. {
  974. if (item) {
  975. if (item->crop_render) {
  976. obs_enter_graphics();
  977. gs_texrender_destroy(item->crop_render);
  978. obs_leave_graphics();
  979. }
  980. obs_hotkey_pair_unregister(item->toggle_visibility);
  981. pthread_mutex_destroy(&item->actions_mutex);
  982. if (item->source)
  983. obs_source_release(item->source);
  984. da_free(item->audio_actions);
  985. bfree(item);
  986. }
  987. }
  988. void obs_sceneitem_addref(obs_sceneitem_t *item)
  989. {
  990. if (item)
  991. os_atomic_inc_long(&item->ref);
  992. }
  993. void obs_sceneitem_release(obs_sceneitem_t *item)
  994. {
  995. if (!item)
  996. return;
  997. if (os_atomic_dec_long(&item->ref) == 0)
  998. obs_sceneitem_destroy(item);
  999. }
  1000. void obs_sceneitem_remove(obs_sceneitem_t *item)
  1001. {
  1002. obs_scene_t *scene;
  1003. if (!item)
  1004. return;
  1005. scene = item->parent;
  1006. if (scene)
  1007. full_lock(scene);
  1008. if (item->removed) {
  1009. if (scene)
  1010. full_unlock(scene);
  1011. return;
  1012. }
  1013. item->removed = true;
  1014. assert(scene != NULL);
  1015. assert(scene->source != NULL);
  1016. set_visibility(item, false);
  1017. signal_item_remove(item);
  1018. detach_sceneitem(item);
  1019. full_unlock(scene);
  1020. obs_sceneitem_release(item);
  1021. }
  1022. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  1023. {
  1024. return item ? item->parent : NULL;
  1025. }
  1026. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  1027. {
  1028. return item ? item->source : NULL;
  1029. }
  1030. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  1031. {
  1032. struct calldata params;
  1033. uint8_t stack[128];
  1034. const char *command = select ? "item_select" : "item_deselect";
  1035. if (!item || item->selected == select)
  1036. return;
  1037. item->selected = select;
  1038. calldata_init_fixed(&params, stack, sizeof(stack));
  1039. calldata_set_ptr(&params, "scene", item->parent);
  1040. calldata_set_ptr(&params, "item", item);
  1041. signal_handler_signal(item->parent->source->context.signals,
  1042. command, &params);
  1043. }
  1044. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  1045. {
  1046. return item ? item->selected : false;
  1047. }
  1048. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  1049. {
  1050. if (item) {
  1051. vec2_copy(&item->pos, pos);
  1052. update_item_transform(item);
  1053. }
  1054. }
  1055. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  1056. {
  1057. if (item) {
  1058. item->rot = rot;
  1059. update_item_transform(item);
  1060. }
  1061. }
  1062. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  1063. {
  1064. if (item) {
  1065. vec2_copy(&item->scale, scale);
  1066. update_item_transform(item);
  1067. }
  1068. }
  1069. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  1070. {
  1071. if (item) {
  1072. item->align = alignment;
  1073. update_item_transform(item);
  1074. }
  1075. }
  1076. static inline void signal_reorder(struct obs_scene_item *item)
  1077. {
  1078. const char *command = NULL;
  1079. struct calldata params;
  1080. uint8_t stack[128];
  1081. command = "reorder";
  1082. calldata_init_fixed(&params, stack, sizeof(stack));
  1083. calldata_set_ptr(&params, "scene", item->parent);
  1084. signal_handler_signal(item->parent->source->context.signals,
  1085. command, &params);
  1086. }
  1087. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  1088. enum obs_order_movement movement)
  1089. {
  1090. if (!item) return;
  1091. struct obs_scene_item *next, *prev;
  1092. struct obs_scene *scene = item->parent;
  1093. obs_scene_addref(scene);
  1094. full_lock(scene);
  1095. next = item->next;
  1096. prev = item->prev;
  1097. detach_sceneitem(item);
  1098. if (movement == OBS_ORDER_MOVE_DOWN) {
  1099. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1100. } else if (movement == OBS_ORDER_MOVE_UP) {
  1101. attach_sceneitem(scene, item, next ? next : prev);
  1102. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1103. struct obs_scene_item *last = next;
  1104. if (!last) {
  1105. last = prev;
  1106. } else {
  1107. while (last->next)
  1108. last = last->next;
  1109. }
  1110. attach_sceneitem(scene, item, last);
  1111. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1112. attach_sceneitem(scene, item, NULL);
  1113. }
  1114. signal_reorder(item);
  1115. full_unlock(scene);
  1116. obs_scene_release(scene);
  1117. }
  1118. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  1119. int position)
  1120. {
  1121. if (!item) return;
  1122. struct obs_scene *scene = item->parent;
  1123. struct obs_scene_item *next;
  1124. obs_scene_addref(scene);
  1125. full_lock(scene);
  1126. detach_sceneitem(item);
  1127. next = scene->first_item;
  1128. if (position == 0) {
  1129. attach_sceneitem(scene, item, NULL);
  1130. } else {
  1131. for (int i = position; i > 1; --i) {
  1132. if (next->next == NULL)
  1133. break;
  1134. next = next->next;
  1135. }
  1136. attach_sceneitem(scene, item, next);
  1137. }
  1138. signal_reorder(item);
  1139. full_unlock(scene);
  1140. obs_scene_release(scene);
  1141. }
  1142. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1143. enum obs_bounds_type type)
  1144. {
  1145. if (item) {
  1146. item->bounds_type = type;
  1147. update_item_transform(item);
  1148. }
  1149. }
  1150. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1151. uint32_t alignment)
  1152. {
  1153. if (item) {
  1154. item->bounds_align = alignment;
  1155. update_item_transform(item);
  1156. }
  1157. }
  1158. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1159. {
  1160. if (item) {
  1161. item->bounds = *bounds;
  1162. update_item_transform(item);
  1163. }
  1164. }
  1165. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1166. {
  1167. if (item)
  1168. vec2_copy(pos, &item->pos);
  1169. }
  1170. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1171. {
  1172. return item ? item->rot : 0.0f;
  1173. }
  1174. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1175. {
  1176. if (item)
  1177. vec2_copy(scale, &item->scale);
  1178. }
  1179. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1180. {
  1181. return item ? item->align : 0;
  1182. }
  1183. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1184. {
  1185. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1186. }
  1187. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1188. {
  1189. return item ? item->bounds_align : 0;
  1190. }
  1191. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1192. {
  1193. if (item)
  1194. *bounds = item->bounds;
  1195. }
  1196. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1197. struct obs_transform_info *info)
  1198. {
  1199. if (item && info) {
  1200. info->pos = item->pos;
  1201. info->rot = item->rot;
  1202. info->scale = item->scale;
  1203. info->alignment = item->align;
  1204. info->bounds_type = item->bounds_type;
  1205. info->bounds_alignment = item->bounds_align;
  1206. info->bounds = item->bounds;
  1207. }
  1208. }
  1209. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1210. const struct obs_transform_info *info)
  1211. {
  1212. if (item && info) {
  1213. item->pos = info->pos;
  1214. item->rot = info->rot;
  1215. item->scale = info->scale;
  1216. item->align = info->alignment;
  1217. item->bounds_type = info->bounds_type;
  1218. item->bounds_align = info->bounds_alignment;
  1219. item->bounds = info->bounds;
  1220. update_item_transform(item);
  1221. }
  1222. }
  1223. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1224. struct matrix4 *transform)
  1225. {
  1226. if (item)
  1227. matrix4_copy(transform, &item->draw_transform);
  1228. }
  1229. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1230. struct matrix4 *transform)
  1231. {
  1232. if (item)
  1233. matrix4_copy(transform, &item->box_transform);
  1234. }
  1235. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1236. {
  1237. return item ? item->user_visible : false;
  1238. }
  1239. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1240. {
  1241. struct calldata cd;
  1242. uint8_t stack[256];
  1243. struct item_action action = {
  1244. .visible = visible,
  1245. .timestamp = os_gettime_ns()
  1246. };
  1247. if (!item)
  1248. return false;
  1249. if (item->user_visible == visible)
  1250. return false;
  1251. if (!item->parent)
  1252. return false;
  1253. if (visible) {
  1254. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1255. if (!obs_source_add_active_child(item->parent->source,
  1256. item->source)) {
  1257. os_atomic_dec_long(&item->active_refs);
  1258. return false;
  1259. }
  1260. }
  1261. }
  1262. item->user_visible = visible;
  1263. calldata_init_fixed(&cd, stack, sizeof(stack));
  1264. calldata_set_ptr(&cd, "scene", item->parent);
  1265. calldata_set_ptr(&cd, "item", item);
  1266. calldata_set_bool(&cd, "visible", visible);
  1267. signal_handler_signal(item->parent->source->context.signals,
  1268. "item_visible", &cd);
  1269. if (source_has_audio(item->source)) {
  1270. pthread_mutex_lock(&item->actions_mutex);
  1271. da_push_back(item->audio_actions, &action);
  1272. pthread_mutex_unlock(&item->actions_mutex);
  1273. } else {
  1274. set_visibility(item, visible);
  1275. }
  1276. return true;
  1277. }
  1278. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1279. size_t size, bool *order_matches)
  1280. {
  1281. obs_sceneitem_t *item = scene->first_item;
  1282. size_t count = 0;
  1283. while (item) {
  1284. bool found = false;
  1285. for (size_t i = 0; i < size; i++) {
  1286. if (items[i] != item)
  1287. continue;
  1288. if (count != i)
  1289. *order_matches = false;
  1290. found = true;
  1291. break;
  1292. }
  1293. if (!found)
  1294. return false;
  1295. item = item->next;
  1296. count += 1;
  1297. }
  1298. return count == size;
  1299. }
  1300. bool obs_scene_reorder_items(obs_scene_t *scene,
  1301. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1302. {
  1303. if (!scene || !item_order_size)
  1304. return false;
  1305. obs_scene_addref(scene);
  1306. full_lock(scene);
  1307. bool order_matches = true;
  1308. if (!sceneitems_match(scene, item_order, item_order_size,
  1309. &order_matches) || order_matches) {
  1310. full_unlock(scene);
  1311. obs_scene_release(scene);
  1312. return false;
  1313. }
  1314. scene->first_item = item_order[0];
  1315. obs_sceneitem_t *prev = NULL;
  1316. for (size_t i = 0; i < item_order_size; i++) {
  1317. item_order[i]->prev = prev;
  1318. item_order[i]->next = NULL;
  1319. if (prev)
  1320. prev->next = item_order[i];
  1321. prev = item_order[i];
  1322. }
  1323. signal_reorder(scene->first_item);
  1324. full_unlock(scene);
  1325. obs_scene_release(scene);
  1326. return true;
  1327. }
  1328. void obs_scene_atomic_update(obs_scene_t *scene,
  1329. obs_scene_atomic_update_func func, void *data)
  1330. {
  1331. if (!scene)
  1332. return;
  1333. obs_scene_addref(scene);
  1334. full_lock(scene);
  1335. func(data, scene);
  1336. full_unlock(scene);
  1337. obs_scene_release(scene);
  1338. }
  1339. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  1340. const struct obs_sceneitem_crop *crop2)
  1341. {
  1342. return crop1->left == crop2->left &&
  1343. crop1->right == crop2->right &&
  1344. crop1->top == crop2->top &&
  1345. crop1->bottom == crop2->bottom;
  1346. }
  1347. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  1348. const struct obs_sceneitem_crop *crop)
  1349. {
  1350. bool now_enabled;
  1351. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  1352. return;
  1353. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  1354. return;
  1355. if (crop_equal(crop, &item->crop))
  1356. return;
  1357. now_enabled = crop_enabled(crop);
  1358. obs_enter_graphics();
  1359. if (!now_enabled) {
  1360. gs_texrender_destroy(item->crop_render);
  1361. item->crop_render = NULL;
  1362. } else if (!item->crop_render) {
  1363. item->crop_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1364. }
  1365. memcpy(&item->crop, crop, sizeof(*crop));
  1366. if (item->crop.left < 0) item->crop.left = 0;
  1367. if (item->crop.right < 0) item->crop.right = 0;
  1368. if (item->crop.top < 0) item->crop.top = 0;
  1369. if (item->crop.bottom < 0) item->crop.bottom = 0;
  1370. obs_leave_graphics();
  1371. update_item_transform(item);
  1372. }
  1373. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  1374. struct obs_sceneitem_crop *crop)
  1375. {
  1376. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  1377. return;
  1378. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  1379. return;
  1380. memcpy(crop, &item->crop, sizeof(*crop));
  1381. }
  1382. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  1383. {
  1384. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  1385. return;
  1386. os_atomic_inc_long(&item->defer_update);
  1387. }
  1388. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  1389. {
  1390. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  1391. return;
  1392. if (os_atomic_dec_long(&item->defer_update) == 0)
  1393. update_item_transform(item);
  1394. }