obs-scene.c 46 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921
  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, bool active)
  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 (!active || 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 void scene_enum_active_sources(void *data,
  151. obs_source_enum_proc_t enum_callback,
  152. void *param)
  153. {
  154. scene_enum_sources(data, enum_callback, param, true);
  155. }
  156. static void scene_enum_all_sources(void *data,
  157. obs_source_enum_proc_t enum_callback,
  158. void *param)
  159. {
  160. scene_enum_sources(data, enum_callback, param, false);
  161. }
  162. static inline void detach_sceneitem(struct obs_scene_item *item)
  163. {
  164. if (item->prev)
  165. item->prev->next = item->next;
  166. else
  167. item->parent->first_item = item->next;
  168. if (item->next)
  169. item->next->prev = item->prev;
  170. item->parent = NULL;
  171. }
  172. static inline void attach_sceneitem(struct obs_scene *parent,
  173. struct obs_scene_item *item, struct obs_scene_item *prev)
  174. {
  175. item->prev = prev;
  176. item->parent = parent;
  177. if (prev) {
  178. item->next = prev->next;
  179. if (prev->next)
  180. prev->next->prev = item;
  181. prev->next = item;
  182. } else {
  183. item->next = parent->first_item;
  184. if (parent->first_item)
  185. parent->first_item->prev = item;
  186. parent->first_item = item;
  187. }
  188. }
  189. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  190. {
  191. if (align & OBS_ALIGN_RIGHT)
  192. v->x += (float)cx;
  193. else if ((align & OBS_ALIGN_LEFT) == 0)
  194. v->x += (float)(cx / 2);
  195. if (align & OBS_ALIGN_BOTTOM)
  196. v->y += (float)cy;
  197. else if ((align & OBS_ALIGN_TOP) == 0)
  198. v->y += (float)(cy / 2);
  199. }
  200. static void calculate_bounds_data(struct obs_scene_item *item,
  201. struct vec2 *origin, struct vec2 *scale,
  202. uint32_t *cx, uint32_t *cy)
  203. {
  204. float width = (float)(*cx) * fabsf(scale->x);
  205. float height = (float)(*cy) * fabsf(scale->y);
  206. float item_aspect = width / height;
  207. float bounds_aspect = item->bounds.x / item->bounds.y;
  208. uint32_t bounds_type = item->bounds_type;
  209. float width_diff, height_diff;
  210. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  211. if (width > item->bounds.x || height > item->bounds.y)
  212. bounds_type = OBS_BOUNDS_SCALE_INNER;
  213. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  214. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  215. bool use_width = (bounds_aspect < item_aspect);
  216. float mul;
  217. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  218. use_width = !use_width;
  219. mul = use_width ?
  220. item->bounds.x / width :
  221. item->bounds.y / height;
  222. vec2_mulf(scale, scale, mul);
  223. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  224. vec2_mulf(scale, scale, item->bounds.x / width);
  225. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  226. vec2_mulf(scale, scale, item->bounds.y / height);
  227. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  228. scale->x = item->bounds.x / (float)(*cx);
  229. scale->y = item->bounds.y / (float)(*cy);
  230. }
  231. width = (float)(*cx) * scale->x;
  232. height = (float)(*cy) * scale->y;
  233. width_diff = item->bounds.x - width;
  234. height_diff = item->bounds.y - height;
  235. *cx = (uint32_t)item->bounds.x;
  236. *cy = (uint32_t)item->bounds.y;
  237. add_alignment(origin, item->bounds_align,
  238. (int)-width_diff, (int)-height_diff);
  239. }
  240. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  241. uint32_t width)
  242. {
  243. uint32_t crop_cx = item->crop.left + item->crop.right;
  244. return (crop_cx > width) ? 2 : (width - crop_cx);
  245. }
  246. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  247. uint32_t height)
  248. {
  249. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  250. return (crop_cy > height) ? 2 : (height - crop_cy);
  251. }
  252. static void update_item_transform(struct obs_scene_item *item)
  253. {
  254. uint32_t width = obs_source_get_width(item->source);
  255. uint32_t height = obs_source_get_height(item->source);
  256. uint32_t cx = calc_cx(item, width);
  257. uint32_t cy = calc_cy(item, height);
  258. struct vec2 base_origin;
  259. struct vec2 origin;
  260. struct vec2 scale = item->scale;
  261. struct calldata params;
  262. uint8_t stack[128];
  263. if (os_atomic_load_long(&item->defer_update) > 0)
  264. return;
  265. width = cx;
  266. height = cy;
  267. vec2_zero(&base_origin);
  268. vec2_zero(&origin);
  269. /* ----------------------- */
  270. if (item->bounds_type != OBS_BOUNDS_NONE) {
  271. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  272. } else {
  273. cx = (uint32_t)((float)cx * scale.x);
  274. cy = (uint32_t)((float)cy * scale.y);
  275. }
  276. add_alignment(&origin, item->align, (int)cx, (int)cy);
  277. matrix4_identity(&item->draw_transform);
  278. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  279. scale.x, scale.y, 1.0f);
  280. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  281. -origin.x, -origin.y, 0.0f);
  282. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  283. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  284. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  285. item->pos.x, item->pos.y, 0.0f);
  286. item->output_scale = scale;
  287. /* ----------------------- */
  288. if (item->bounds_type != OBS_BOUNDS_NONE) {
  289. vec2_copy(&scale, &item->bounds);
  290. } else {
  291. scale.x = (float)width * item->scale.x;
  292. scale.y = (float)height * item->scale.y;
  293. }
  294. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  295. matrix4_identity(&item->box_transform);
  296. matrix4_scale3f(&item->box_transform, &item->box_transform,
  297. scale.x, scale.y, 1.0f);
  298. matrix4_translate3f(&item->box_transform, &item->box_transform,
  299. -base_origin.x, -base_origin.y, 0.0f);
  300. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  301. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  302. matrix4_translate3f(&item->box_transform, &item->box_transform,
  303. item->pos.x, item->pos.y, 0.0f);
  304. /* ----------------------- */
  305. item->last_width = width;
  306. item->last_height = height;
  307. calldata_init_fixed(&params, stack, sizeof(stack));
  308. calldata_set_ptr(&params, "scene", item->parent);
  309. calldata_set_ptr(&params, "item", item);
  310. signal_handler_signal(item->parent->source->context.signals,
  311. "item_transform", &params);
  312. }
  313. static inline bool source_size_changed(struct obs_scene_item *item)
  314. {
  315. uint32_t width = obs_source_get_width(item->source);
  316. uint32_t height = obs_source_get_height(item->source);
  317. return item->last_width != width || item->last_height != height;
  318. }
  319. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  320. {
  321. return crop->left || crop->right || crop->top || crop->bottom;
  322. }
  323. static inline bool scale_filter_enabled(const struct obs_scene_item *item)
  324. {
  325. return item->scale_filter != OBS_SCALE_DISABLE;
  326. }
  327. static inline bool item_is_scene(const struct obs_scene_item *item)
  328. {
  329. return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
  330. }
  331. static inline bool item_texture_enabled(const struct obs_scene_item *item)
  332. {
  333. return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
  334. item_is_scene(item);
  335. }
  336. static void render_item_texture(struct obs_scene_item *item)
  337. {
  338. gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
  339. gs_effect_t *effect = obs->video.default_effect;
  340. enum obs_scale_type type = item->scale_filter;
  341. uint32_t cx = gs_texture_get_width(tex);
  342. uint32_t cy = gs_texture_get_height(tex);
  343. if (type != OBS_SCALE_DISABLE) {
  344. if (type == OBS_SCALE_POINT) {
  345. gs_eparam_t *image = gs_effect_get_param_by_name(
  346. effect, "image");
  347. gs_effect_set_next_sampler(image,
  348. obs->video.point_sampler);
  349. } else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
  350. !close_float(item->output_scale.y, 1.0f, EPSILON)) {
  351. gs_eparam_t *scale_param;
  352. if (item->output_scale.x < 0.5f ||
  353. item->output_scale.y < 0.5f) {
  354. effect = obs->video.bilinear_lowres_effect;
  355. } else if (type == OBS_SCALE_BICUBIC) {
  356. effect = obs->video.bicubic_effect;
  357. } else if (type == OBS_SCALE_LANCZOS) {
  358. effect = obs->video.lanczos_effect;
  359. }
  360. scale_param = gs_effect_get_param_by_name(effect,
  361. "base_dimension_i");
  362. if (scale_param) {
  363. struct vec2 base_res_i = {
  364. 1.0f / (float)cx,
  365. 1.0f / (float)cy
  366. };
  367. gs_effect_set_vec2(scale_param, &base_res_i);
  368. }
  369. }
  370. }
  371. while (gs_effect_loop(effect, "Draw"))
  372. obs_source_draw(tex, 0, 0, 0, 0, 0);
  373. }
  374. static inline void render_item(struct obs_scene_item *item)
  375. {
  376. if (item->item_render) {
  377. uint32_t width = obs_source_get_width(item->source);
  378. uint32_t height = obs_source_get_height(item->source);
  379. uint32_t cx = calc_cx(item, width);
  380. uint32_t cy = calc_cy(item, height);
  381. if (cx && cy && gs_texrender_begin(item->item_render, cx, cy)) {
  382. float cx_scale = (float)width / (float)cx;
  383. float cy_scale = (float)height / (float)cy;
  384. struct vec4 clear_color;
  385. vec4_zero(&clear_color);
  386. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  387. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  388. -100.0f, 100.0f);
  389. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  390. gs_matrix_translate3f(
  391. -(float)item->crop.left,
  392. -(float)item->crop.top,
  393. 0.0f);
  394. obs_source_video_render(item->source);
  395. gs_texrender_end(item->item_render);
  396. }
  397. }
  398. gs_matrix_push();
  399. gs_matrix_mul(&item->draw_transform);
  400. if (item->item_render) {
  401. render_item_texture(item);
  402. } else {
  403. obs_source_video_render(item->source);
  404. }
  405. gs_matrix_pop();
  406. }
  407. static void scene_video_tick(void *data, float seconds)
  408. {
  409. struct obs_scene *scene = data;
  410. struct obs_scene_item *item;
  411. video_lock(scene);
  412. item = scene->first_item;
  413. while (item) {
  414. if (item->item_render)
  415. gs_texrender_reset(item->item_render);
  416. item = item->next;
  417. }
  418. video_unlock(scene);
  419. UNUSED_PARAMETER(seconds);
  420. }
  421. static void scene_video_render(void *data, gs_effect_t *effect)
  422. {
  423. DARRAY(struct obs_scene_item*) remove_items;
  424. struct obs_scene *scene = data;
  425. struct obs_scene_item *item;
  426. da_init(remove_items);
  427. video_lock(scene);
  428. item = scene->first_item;
  429. gs_blend_state_push();
  430. gs_reset_blend_state();
  431. while (item) {
  432. if (obs_source_removed(item->source)) {
  433. struct obs_scene_item *del_item = item;
  434. item = item->next;
  435. remove_without_release(del_item);
  436. da_push_back(remove_items, &del_item);
  437. continue;
  438. }
  439. if (source_size_changed(item))
  440. update_item_transform(item);
  441. if (item->user_visible)
  442. render_item(item);
  443. item = item->next;
  444. }
  445. gs_blend_state_pop();
  446. video_unlock(scene);
  447. for (size_t i = 0; i < remove_items.num; i++)
  448. obs_sceneitem_release(remove_items.array[i]);
  449. da_free(remove_items);
  450. UNUSED_PARAMETER(effect);
  451. }
  452. static void set_visibility(struct obs_scene_item *item, bool vis)
  453. {
  454. pthread_mutex_lock(&item->actions_mutex);
  455. da_resize(item->audio_actions, 0);
  456. if (os_atomic_load_long(&item->active_refs) > 0) {
  457. if (!vis)
  458. obs_source_remove_active_child(item->parent->source,
  459. item->source);
  460. } else if (vis) {
  461. obs_source_add_active_child(item->parent->source, item->source);
  462. }
  463. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  464. item->visible = vis;
  465. item->user_visible = vis;
  466. pthread_mutex_unlock(&item->actions_mutex);
  467. }
  468. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  469. {
  470. const char *name = obs_data_get_string(item_data, "name");
  471. obs_source_t *source = obs_get_source_by_name(name);
  472. const char *scale_filter_str;
  473. struct obs_scene_item *item;
  474. bool visible;
  475. if (!source) {
  476. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  477. name);
  478. return;
  479. }
  480. item = obs_scene_add(scene, source);
  481. if (!item) {
  482. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  483. "to scene '%s'!",
  484. name, obs_source_get_name(scene->source));
  485. obs_source_release(source);
  486. return;
  487. }
  488. obs_data_set_default_int(item_data, "align",
  489. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  490. item->rot = (float)obs_data_get_double(item_data, "rot");
  491. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  492. visible = obs_data_get_bool(item_data, "visible");
  493. obs_data_get_vec2(item_data, "pos", &item->pos);
  494. obs_data_get_vec2(item_data, "scale", &item->scale);
  495. set_visibility(item, visible);
  496. item->bounds_type =
  497. (enum obs_bounds_type)obs_data_get_int(item_data,
  498. "bounds_type");
  499. item->bounds_align =
  500. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  501. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  502. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  503. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  504. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  505. item->crop.bottom = (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  506. scale_filter_str = obs_data_get_string(item_data, "scale_filter");
  507. item->scale_filter = OBS_SCALE_DISABLE;
  508. if (scale_filter_str) {
  509. if (astrcmpi(scale_filter_str, "point") == 0)
  510. item->scale_filter = OBS_SCALE_POINT;
  511. else if (astrcmpi(scale_filter_str, "bilinear") == 0)
  512. item->scale_filter = OBS_SCALE_BILINEAR;
  513. else if (astrcmpi(scale_filter_str, "bicubic") == 0)
  514. item->scale_filter = OBS_SCALE_BICUBIC;
  515. else if (astrcmpi(scale_filter_str, "lanczos") == 0)
  516. item->scale_filter = OBS_SCALE_LANCZOS;
  517. }
  518. if (item->item_render && !item_texture_enabled(item)) {
  519. obs_enter_graphics();
  520. gs_texrender_destroy(item->item_render);
  521. item->item_render = NULL;
  522. obs_leave_graphics();
  523. } else if (!item->item_render && item_texture_enabled(item)) {
  524. obs_enter_graphics();
  525. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  526. obs_leave_graphics();
  527. }
  528. obs_source_release(source);
  529. update_item_transform(item);
  530. }
  531. static void scene_load(void *scene, obs_data_t *settings)
  532. {
  533. obs_data_array_t *items = obs_data_get_array(settings, "items");
  534. size_t count, i;
  535. remove_all_items(scene);
  536. if (!items) return;
  537. count = obs_data_array_count(items);
  538. for (i = 0; i < count; i++) {
  539. obs_data_t *item_data = obs_data_array_item(items, i);
  540. scene_load_item(scene, item_data);
  541. obs_data_release(item_data);
  542. }
  543. obs_data_array_release(items);
  544. }
  545. static void scene_save_item(obs_data_array_t *array,
  546. struct obs_scene_item *item)
  547. {
  548. obs_data_t *item_data = obs_data_create();
  549. const char *name = obs_source_get_name(item->source);
  550. const char *scale_filter;
  551. obs_data_set_string(item_data, "name", name);
  552. obs_data_set_bool (item_data, "visible", item->user_visible);
  553. obs_data_set_double(item_data, "rot", item->rot);
  554. obs_data_set_vec2 (item_data, "pos", &item->pos);
  555. obs_data_set_vec2 (item_data, "scale", &item->scale);
  556. obs_data_set_int (item_data, "align", (int)item->align);
  557. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  558. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  559. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  560. obs_data_set_int (item_data, "crop_left", (int)item->crop.left);
  561. obs_data_set_int (item_data, "crop_top", (int)item->crop.top);
  562. obs_data_set_int (item_data, "crop_right", (int)item->crop.right);
  563. obs_data_set_int (item_data, "crop_bottom", (int)item->crop.bottom);
  564. if (item->scale_filter == OBS_SCALE_POINT)
  565. scale_filter = "point";
  566. else if (item->scale_filter == OBS_SCALE_BILINEAR)
  567. scale_filter = "bilinear";
  568. else if (item->scale_filter == OBS_SCALE_BICUBIC)
  569. scale_filter = "bicubic";
  570. else if (item->scale_filter == OBS_SCALE_LANCZOS)
  571. scale_filter = "lanczos";
  572. else
  573. scale_filter = "disable";
  574. obs_data_set_string(item_data, "scale_filter", scale_filter);
  575. obs_data_array_push_back(array, item_data);
  576. obs_data_release(item_data);
  577. }
  578. static void scene_save(void *data, obs_data_t *settings)
  579. {
  580. struct obs_scene *scene = data;
  581. obs_data_array_t *array = obs_data_array_create();
  582. struct obs_scene_item *item;
  583. full_lock(scene);
  584. item = scene->first_item;
  585. while (item) {
  586. scene_save_item(array, item);
  587. item = item->next;
  588. }
  589. full_unlock(scene);
  590. obs_data_set_array(settings, "items", array);
  591. obs_data_array_release(array);
  592. }
  593. static uint32_t scene_getwidth(void *data)
  594. {
  595. UNUSED_PARAMETER(data);
  596. return obs->video.base_width;
  597. }
  598. static uint32_t scene_getheight(void *data)
  599. {
  600. UNUSED_PARAMETER(data);
  601. return obs->video.base_height;
  602. }
  603. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  604. float **p_buf, uint64_t ts, size_t sample_rate)
  605. {
  606. bool cur_visible = item->visible;
  607. uint64_t frame_num = 0;
  608. size_t deref_count = 0;
  609. float *buf = NULL;
  610. if (p_buf) {
  611. if (!*p_buf)
  612. *p_buf = malloc(AUDIO_OUTPUT_FRAMES * sizeof(float));
  613. buf = *p_buf;
  614. }
  615. pthread_mutex_lock(&item->actions_mutex);
  616. for (size_t i = 0; i < item->audio_actions.num; i++) {
  617. struct item_action action = item->audio_actions.array[i];
  618. uint64_t timestamp = action.timestamp;
  619. uint64_t new_frame_num;
  620. if (timestamp < ts)
  621. timestamp = ts;
  622. new_frame_num = (timestamp - ts) * (uint64_t)sample_rate /
  623. 1000000000ULL;
  624. if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
  625. break;
  626. da_erase(item->audio_actions, i--);
  627. item->visible = action.visible;
  628. if (!item->visible)
  629. deref_count++;
  630. if (buf && new_frame_num > frame_num) {
  631. for (; frame_num < new_frame_num; frame_num++)
  632. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  633. }
  634. cur_visible = item->visible;
  635. }
  636. if (buf) {
  637. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  638. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  639. }
  640. pthread_mutex_unlock(&item->actions_mutex);
  641. while (deref_count--) {
  642. if (os_atomic_dec_long(&item->active_refs) == 0) {
  643. obs_source_remove_active_child(item->parent->source,
  644. item->source);
  645. }
  646. }
  647. }
  648. static bool apply_scene_item_volume(struct obs_scene_item *item,
  649. float **buf, uint64_t ts, size_t sample_rate)
  650. {
  651. bool actions_pending;
  652. struct item_action action;
  653. pthread_mutex_lock(&item->actions_mutex);
  654. actions_pending = item->audio_actions.num > 0;
  655. if (actions_pending)
  656. action = item->audio_actions.array[0];
  657. pthread_mutex_unlock(&item->actions_mutex);
  658. if (actions_pending) {
  659. uint64_t duration = (uint64_t)AUDIO_OUTPUT_FRAMES *
  660. 1000000000ULL / (uint64_t)sample_rate;
  661. if (!ts || action.timestamp < (ts + duration)) {
  662. apply_scene_item_audio_actions(item, buf, ts,
  663. sample_rate);
  664. return true;
  665. }
  666. }
  667. return false;
  668. }
  669. static void process_all_audio_actions(struct obs_scene_item *item,
  670. size_t sample_rate)
  671. {
  672. while (apply_scene_item_volume(item, NULL, 0, sample_rate));
  673. }
  674. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  675. size_t pos, size_t count)
  676. {
  677. register float *out = p_out;
  678. register float *buf = buf_in + pos;
  679. register float *in = p_in + pos;
  680. register float *end = in + count;
  681. while (in < end)
  682. *(out++) += *(in++) * *(buf++);
  683. }
  684. static inline void mix_audio(float *p_out, float *p_in,
  685. size_t pos, size_t count)
  686. {
  687. register float *out = p_out;
  688. register float *in = p_in + pos;
  689. register float *end = in + count;
  690. while (in < end)
  691. *(out++) += *(in++);
  692. }
  693. static bool scene_audio_render(void *data, uint64_t *ts_out,
  694. struct obs_source_audio_mix *audio_output, uint32_t mixers,
  695. size_t channels, size_t sample_rate)
  696. {
  697. uint64_t timestamp = 0;
  698. float *buf = NULL;
  699. struct obs_source_audio_mix child_audio;
  700. struct obs_scene *scene = data;
  701. struct obs_scene_item *item;
  702. audio_lock(scene);
  703. item = scene->first_item;
  704. while (item) {
  705. if (!obs_source_audio_pending(item->source)) {
  706. uint64_t source_ts =
  707. obs_source_get_audio_timestamp(item->source);
  708. if (source_ts && (!timestamp || source_ts < timestamp))
  709. timestamp = source_ts;
  710. }
  711. item = item->next;
  712. }
  713. if (!timestamp) {
  714. /* just process all pending audio actions if no audio playing,
  715. * otherwise audio actions will just never be processed */
  716. item = scene->first_item;
  717. while (item) {
  718. process_all_audio_actions(item, sample_rate);
  719. item = item->next;
  720. }
  721. audio_unlock(scene);
  722. return false;
  723. }
  724. item = scene->first_item;
  725. while (item) {
  726. uint64_t source_ts;
  727. size_t pos, count;
  728. bool apply_buf;
  729. apply_buf = apply_scene_item_volume(item, &buf, timestamp,
  730. sample_rate);
  731. if (obs_source_audio_pending(item->source)) {
  732. item = item->next;
  733. continue;
  734. }
  735. source_ts = obs_source_get_audio_timestamp(item->source);
  736. if (!source_ts) {
  737. item = item->next;
  738. continue;
  739. }
  740. pos = (size_t)ns_to_audio_frames(sample_rate,
  741. source_ts - timestamp);
  742. count = AUDIO_OUTPUT_FRAMES - pos;
  743. if (!apply_buf && !item->visible) {
  744. item = item->next;
  745. continue;
  746. }
  747. obs_source_get_audio_mix(item->source, &child_audio);
  748. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  749. if ((mixers & (1 << mix)) == 0)
  750. continue;
  751. for (size_t ch = 0; ch < channels; ch++) {
  752. float *out = audio_output->output[mix].data[ch];
  753. float *in = child_audio.output[mix].data[ch];
  754. if (apply_buf)
  755. mix_audio_with_buf(out, in, buf, pos,
  756. count);
  757. else
  758. mix_audio(out, in, pos, count);
  759. }
  760. }
  761. item = item->next;
  762. }
  763. *ts_out = timestamp;
  764. audio_unlock(scene);
  765. free(buf);
  766. return true;
  767. }
  768. const struct obs_source_info scene_info =
  769. {
  770. .id = "scene",
  771. .type = OBS_SOURCE_TYPE_SCENE,
  772. .output_flags = OBS_SOURCE_VIDEO |
  773. OBS_SOURCE_CUSTOM_DRAW |
  774. OBS_SOURCE_COMPOSITE,
  775. .get_name = scene_getname,
  776. .create = scene_create,
  777. .destroy = scene_destroy,
  778. .video_tick = scene_video_tick,
  779. .video_render = scene_video_render,
  780. .audio_render = scene_audio_render,
  781. .get_width = scene_getwidth,
  782. .get_height = scene_getheight,
  783. .load = scene_load,
  784. .save = scene_save,
  785. .enum_active_sources = scene_enum_active_sources,
  786. .enum_all_sources = scene_enum_all_sources
  787. };
  788. obs_scene_t *obs_scene_create(const char *name)
  789. {
  790. struct obs_source *source = obs_source_create("scene", name, NULL,
  791. NULL);
  792. return source->context.data;
  793. }
  794. obs_scene_t *obs_scene_create_private(const char *name)
  795. {
  796. struct obs_source *source = obs_source_create_private("scene", name,
  797. NULL);
  798. return source->context.data;
  799. }
  800. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  801. {
  802. struct obs_scene_item *item = scene->first_item;
  803. while (item && idx--)
  804. item = item->next;
  805. return item ? item->source : NULL;
  806. }
  807. static inline obs_source_t *dup_child(struct darray *array, size_t idx,
  808. obs_scene_t *new_scene, bool private)
  809. {
  810. DARRAY(struct obs_scene_item*) old_items;
  811. obs_source_t *source;
  812. old_items.da = *array;
  813. source = old_items.array[idx]->source;
  814. /* if the old item is referenced more than once in the old scene,
  815. * make sure they're referenced similarly in the new scene to reduce
  816. * load times */
  817. for (size_t i = 0; i < idx; i++) {
  818. struct obs_scene_item *item = old_items.array[i];
  819. if (item->source == source) {
  820. source = get_child_at_idx(new_scene, i);
  821. obs_source_addref(source);
  822. return source;
  823. }
  824. }
  825. return obs_source_duplicate(source, NULL, private);
  826. }
  827. static inline obs_source_t *new_ref(obs_source_t *source)
  828. {
  829. obs_source_addref(source);
  830. return source;
  831. }
  832. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  833. enum obs_scene_duplicate_type type)
  834. {
  835. bool make_unique = ((int)type & (1<<0)) != 0;
  836. bool make_private = ((int)type & (1<<1)) != 0;
  837. DARRAY(struct obs_scene_item*) items;
  838. struct obs_scene *new_scene;
  839. struct obs_scene_item *item;
  840. struct obs_source *source;
  841. da_init(items);
  842. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  843. return NULL;
  844. /* --------------------------------- */
  845. full_lock(scene);
  846. item = scene->first_item;
  847. while (item) {
  848. da_push_back(items, &item);
  849. obs_sceneitem_addref(item);
  850. item = item->next;
  851. }
  852. full_unlock(scene);
  853. /* --------------------------------- */
  854. new_scene = make_private ?
  855. obs_scene_create_private(name) : obs_scene_create(name);
  856. for (size_t i = 0; i < items.num; i++) {
  857. item = items.array[i];
  858. source = make_unique ?
  859. dup_child(&items.da, i, new_scene, make_private) :
  860. new_ref(item->source);
  861. if (source) {
  862. struct obs_scene_item *new_item =
  863. obs_scene_add(new_scene, source);
  864. if (!new_item) {
  865. obs_source_release(source);
  866. continue;
  867. }
  868. if (!item->user_visible)
  869. set_visibility(new_item, false);
  870. new_item->selected = item->selected;
  871. new_item->pos = item->pos;
  872. new_item->rot = item->rot;
  873. new_item->scale = item->scale;
  874. new_item->align = item->align;
  875. new_item->last_width = item->last_width;
  876. new_item->last_height = item->last_height;
  877. new_item->output_scale = item->output_scale;
  878. new_item->scale_filter = item->scale_filter;
  879. new_item->box_transform = item->box_transform;
  880. new_item->draw_transform = item->draw_transform;
  881. new_item->bounds_type = item->bounds_type;
  882. new_item->bounds_align = item->bounds_align;
  883. new_item->bounds = item->bounds;
  884. obs_sceneitem_set_crop(new_item, &item->crop);
  885. obs_source_release(source);
  886. }
  887. }
  888. for (size_t i = 0; i < items.num; i++)
  889. obs_sceneitem_release(items.array[i]);
  890. da_free(items);
  891. return new_scene;
  892. }
  893. void obs_scene_addref(obs_scene_t *scene)
  894. {
  895. if (scene)
  896. obs_source_addref(scene->source);
  897. }
  898. void obs_scene_release(obs_scene_t *scene)
  899. {
  900. if (scene)
  901. obs_source_release(scene->source);
  902. }
  903. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  904. {
  905. return scene ? scene->source : NULL;
  906. }
  907. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  908. {
  909. if (!source || source->info.id != scene_info.id)
  910. return NULL;
  911. return source->context.data;
  912. }
  913. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  914. {
  915. struct obs_scene_item *item;
  916. if (!scene)
  917. return NULL;
  918. full_lock(scene);
  919. item = scene->first_item;
  920. while (item) {
  921. if (strcmp(item->source->context.name, name) == 0)
  922. break;
  923. item = item->next;
  924. }
  925. full_unlock(scene);
  926. return item;
  927. }
  928. void obs_scene_enum_items(obs_scene_t *scene,
  929. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  930. void *param)
  931. {
  932. struct obs_scene_item *item;
  933. if (!scene || !callback)
  934. return;
  935. full_lock(scene);
  936. item = scene->first_item;
  937. while (item) {
  938. struct obs_scene_item *next = item->next;
  939. obs_sceneitem_addref(item);
  940. if (!callback(scene, item, param)) {
  941. obs_sceneitem_release(item);
  942. break;
  943. }
  944. obs_sceneitem_release(item);
  945. item = next;
  946. }
  947. full_unlock(scene);
  948. }
  949. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  950. {
  951. long owners = si->ref;
  952. while (owners > 0) {
  953. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  954. return si;
  955. owners = si->ref;
  956. }
  957. return NULL;
  958. }
  959. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  960. obs_hotkey_t *hotkey, bool pressed)
  961. {
  962. UNUSED_PARAMETER(id);
  963. UNUSED_PARAMETER(hotkey);
  964. obs_sceneitem_t *si = sceneitem_get_ref(data);
  965. if (pressed && si && !si->user_visible) {
  966. obs_sceneitem_set_visible(si, true);
  967. obs_sceneitem_release(si);
  968. return true;
  969. }
  970. obs_sceneitem_release(si);
  971. return false;
  972. }
  973. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  974. obs_hotkey_t *hotkey, bool pressed)
  975. {
  976. UNUSED_PARAMETER(id);
  977. UNUSED_PARAMETER(hotkey);
  978. obs_sceneitem_t *si = sceneitem_get_ref(data);
  979. if (pressed && si && si->user_visible) {
  980. obs_sceneitem_set_visible(si, false);
  981. obs_sceneitem_release(si);
  982. return true;
  983. }
  984. obs_sceneitem_release(si);
  985. return false;
  986. }
  987. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  988. const char *name)
  989. {
  990. struct dstr show = {0};
  991. struct dstr hide = {0};
  992. struct dstr show_desc = {0};
  993. struct dstr hide_desc = {0};
  994. dstr_copy(&show, "libobs.show_scene_item.%1");
  995. dstr_replace(&show, "%1", name);
  996. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  997. dstr_replace(&hide, "%1", name);
  998. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  999. dstr_replace(&show_desc, "%1", name);
  1000. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1001. dstr_replace(&hide_desc, "%1", name);
  1002. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  1003. show.array, show_desc.array,
  1004. hide.array, hide_desc.array,
  1005. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  1006. item, item);
  1007. dstr_free(&show);
  1008. dstr_free(&hide);
  1009. dstr_free(&show_desc);
  1010. dstr_free(&hide_desc);
  1011. }
  1012. static inline bool source_has_audio(obs_source_t *source)
  1013. {
  1014. return (source->info.output_flags &
  1015. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  1016. }
  1017. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  1018. {
  1019. struct obs_scene_item *last;
  1020. struct obs_scene_item *item;
  1021. struct calldata params;
  1022. uint8_t stack[128];
  1023. pthread_mutex_t mutex;
  1024. struct item_action action = {
  1025. .visible = true,
  1026. .timestamp = os_gettime_ns()
  1027. };
  1028. if (!scene)
  1029. return NULL;
  1030. if (!source) {
  1031. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  1032. return NULL;
  1033. }
  1034. if (pthread_mutex_init(&mutex, NULL) != 0) {
  1035. blog(LOG_WARNING, "Failed to create scene item mutex");
  1036. return NULL;
  1037. }
  1038. if (!obs_source_add_active_child(scene->source, source)) {
  1039. blog(LOG_WARNING, "Failed to add source to scene due to "
  1040. "infinite source recursion");
  1041. pthread_mutex_destroy(&mutex);
  1042. return NULL;
  1043. }
  1044. item = bzalloc(sizeof(struct obs_scene_item));
  1045. item->source = source;
  1046. item->parent = scene;
  1047. item->ref = 1;
  1048. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1049. item->actions_mutex = mutex;
  1050. item->user_visible = true;
  1051. os_atomic_set_long(&item->active_refs, 1);
  1052. vec2_set(&item->scale, 1.0f, 1.0f);
  1053. matrix4_identity(&item->draw_transform);
  1054. matrix4_identity(&item->box_transform);
  1055. obs_source_addref(source);
  1056. if (source_has_audio(source)) {
  1057. item->visible = false;
  1058. da_push_back(item->audio_actions, &action);
  1059. } else {
  1060. item->visible = true;
  1061. }
  1062. if (item_texture_enabled(item)) {
  1063. obs_enter_graphics();
  1064. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1065. obs_leave_graphics();
  1066. }
  1067. full_lock(scene);
  1068. last = scene->first_item;
  1069. if (!last) {
  1070. scene->first_item = item;
  1071. } else {
  1072. while (last->next)
  1073. last = last->next;
  1074. last->next = item;
  1075. item->prev = last;
  1076. }
  1077. full_unlock(scene);
  1078. if (!scene->source->context.private)
  1079. init_hotkeys(scene, item, obs_source_get_name(source));
  1080. calldata_init_fixed(&params, stack, sizeof(stack));
  1081. calldata_set_ptr(&params, "scene", scene);
  1082. calldata_set_ptr(&params, "item", item);
  1083. signal_handler_signal(scene->source->context.signals, "item_add",
  1084. &params);
  1085. return item;
  1086. }
  1087. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  1088. {
  1089. if (item) {
  1090. if (item->item_render) {
  1091. obs_enter_graphics();
  1092. gs_texrender_destroy(item->item_render);
  1093. obs_leave_graphics();
  1094. }
  1095. obs_hotkey_pair_unregister(item->toggle_visibility);
  1096. pthread_mutex_destroy(&item->actions_mutex);
  1097. if (item->source)
  1098. obs_source_release(item->source);
  1099. da_free(item->audio_actions);
  1100. bfree(item);
  1101. }
  1102. }
  1103. void obs_sceneitem_addref(obs_sceneitem_t *item)
  1104. {
  1105. if (item)
  1106. os_atomic_inc_long(&item->ref);
  1107. }
  1108. void obs_sceneitem_release(obs_sceneitem_t *item)
  1109. {
  1110. if (!item)
  1111. return;
  1112. if (os_atomic_dec_long(&item->ref) == 0)
  1113. obs_sceneitem_destroy(item);
  1114. }
  1115. void obs_sceneitem_remove(obs_sceneitem_t *item)
  1116. {
  1117. obs_scene_t *scene;
  1118. if (!item)
  1119. return;
  1120. scene = item->parent;
  1121. full_lock(scene);
  1122. if (item->removed) {
  1123. if (scene)
  1124. full_unlock(scene);
  1125. return;
  1126. }
  1127. item->removed = true;
  1128. assert(scene != NULL);
  1129. assert(scene->source != NULL);
  1130. set_visibility(item, false);
  1131. signal_item_remove(item);
  1132. detach_sceneitem(item);
  1133. full_unlock(scene);
  1134. obs_sceneitem_release(item);
  1135. }
  1136. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  1137. {
  1138. return item ? item->parent : NULL;
  1139. }
  1140. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  1141. {
  1142. return item ? item->source : NULL;
  1143. }
  1144. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  1145. {
  1146. struct calldata params;
  1147. uint8_t stack[128];
  1148. const char *command = select ? "item_select" : "item_deselect";
  1149. if (!item || item->selected == select || !item->parent)
  1150. return;
  1151. item->selected = select;
  1152. calldata_init_fixed(&params, stack, sizeof(stack));
  1153. calldata_set_ptr(&params, "scene", item->parent);
  1154. calldata_set_ptr(&params, "item", item);
  1155. signal_handler_signal(item->parent->source->context.signals,
  1156. command, &params);
  1157. }
  1158. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  1159. {
  1160. return item ? item->selected : false;
  1161. }
  1162. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  1163. {
  1164. if (item) {
  1165. vec2_copy(&item->pos, pos);
  1166. update_item_transform(item);
  1167. }
  1168. }
  1169. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  1170. {
  1171. if (item) {
  1172. item->rot = rot;
  1173. update_item_transform(item);
  1174. }
  1175. }
  1176. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  1177. {
  1178. if (item) {
  1179. vec2_copy(&item->scale, scale);
  1180. update_item_transform(item);
  1181. }
  1182. }
  1183. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  1184. {
  1185. if (item) {
  1186. item->align = alignment;
  1187. update_item_transform(item);
  1188. }
  1189. }
  1190. static inline void signal_reorder(struct obs_scene_item *item)
  1191. {
  1192. const char *command = NULL;
  1193. struct calldata params;
  1194. uint8_t stack[128];
  1195. command = "reorder";
  1196. calldata_init_fixed(&params, stack, sizeof(stack));
  1197. calldata_set_ptr(&params, "scene", item->parent);
  1198. signal_handler_signal(item->parent->source->context.signals,
  1199. command, &params);
  1200. }
  1201. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  1202. enum obs_order_movement movement)
  1203. {
  1204. if (!item) return;
  1205. struct obs_scene_item *next, *prev;
  1206. struct obs_scene *scene = item->parent;
  1207. obs_scene_addref(scene);
  1208. full_lock(scene);
  1209. next = item->next;
  1210. prev = item->prev;
  1211. detach_sceneitem(item);
  1212. if (movement == OBS_ORDER_MOVE_DOWN) {
  1213. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1214. } else if (movement == OBS_ORDER_MOVE_UP) {
  1215. attach_sceneitem(scene, item, next ? next : prev);
  1216. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1217. struct obs_scene_item *last = next;
  1218. if (!last) {
  1219. last = prev;
  1220. } else {
  1221. while (last->next)
  1222. last = last->next;
  1223. }
  1224. attach_sceneitem(scene, item, last);
  1225. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1226. attach_sceneitem(scene, item, NULL);
  1227. }
  1228. signal_reorder(item);
  1229. full_unlock(scene);
  1230. obs_scene_release(scene);
  1231. }
  1232. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  1233. int position)
  1234. {
  1235. if (!item) return;
  1236. struct obs_scene *scene = item->parent;
  1237. struct obs_scene_item *next;
  1238. obs_scene_addref(scene);
  1239. full_lock(scene);
  1240. detach_sceneitem(item);
  1241. next = scene->first_item;
  1242. if (position == 0) {
  1243. attach_sceneitem(scene, item, NULL);
  1244. } else {
  1245. for (int i = position; i > 1; --i) {
  1246. if (next->next == NULL)
  1247. break;
  1248. next = next->next;
  1249. }
  1250. attach_sceneitem(scene, item, next);
  1251. }
  1252. signal_reorder(item);
  1253. full_unlock(scene);
  1254. obs_scene_release(scene);
  1255. }
  1256. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1257. enum obs_bounds_type type)
  1258. {
  1259. if (item) {
  1260. item->bounds_type = type;
  1261. update_item_transform(item);
  1262. }
  1263. }
  1264. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1265. uint32_t alignment)
  1266. {
  1267. if (item) {
  1268. item->bounds_align = alignment;
  1269. update_item_transform(item);
  1270. }
  1271. }
  1272. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1273. {
  1274. if (item) {
  1275. item->bounds = *bounds;
  1276. update_item_transform(item);
  1277. }
  1278. }
  1279. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1280. {
  1281. if (item)
  1282. vec2_copy(pos, &item->pos);
  1283. }
  1284. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1285. {
  1286. return item ? item->rot : 0.0f;
  1287. }
  1288. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1289. {
  1290. if (item)
  1291. vec2_copy(scale, &item->scale);
  1292. }
  1293. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1294. {
  1295. return item ? item->align : 0;
  1296. }
  1297. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1298. {
  1299. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1300. }
  1301. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1302. {
  1303. return item ? item->bounds_align : 0;
  1304. }
  1305. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1306. {
  1307. if (item)
  1308. *bounds = item->bounds;
  1309. }
  1310. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1311. struct obs_transform_info *info)
  1312. {
  1313. if (item && info) {
  1314. info->pos = item->pos;
  1315. info->rot = item->rot;
  1316. info->scale = item->scale;
  1317. info->alignment = item->align;
  1318. info->bounds_type = item->bounds_type;
  1319. info->bounds_alignment = item->bounds_align;
  1320. info->bounds = item->bounds;
  1321. }
  1322. }
  1323. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1324. const struct obs_transform_info *info)
  1325. {
  1326. if (item && info) {
  1327. item->pos = info->pos;
  1328. item->rot = info->rot;
  1329. item->scale = info->scale;
  1330. item->align = info->alignment;
  1331. item->bounds_type = info->bounds_type;
  1332. item->bounds_align = info->bounds_alignment;
  1333. item->bounds = info->bounds;
  1334. update_item_transform(item);
  1335. }
  1336. }
  1337. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1338. struct matrix4 *transform)
  1339. {
  1340. if (item)
  1341. matrix4_copy(transform, &item->draw_transform);
  1342. }
  1343. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1344. struct matrix4 *transform)
  1345. {
  1346. if (item)
  1347. matrix4_copy(transform, &item->box_transform);
  1348. }
  1349. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1350. {
  1351. return item ? item->user_visible : false;
  1352. }
  1353. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1354. {
  1355. struct calldata cd;
  1356. uint8_t stack[256];
  1357. struct item_action action = {
  1358. .visible = visible,
  1359. .timestamp = os_gettime_ns()
  1360. };
  1361. if (!item)
  1362. return false;
  1363. if (item->user_visible == visible)
  1364. return false;
  1365. if (!item->parent)
  1366. return false;
  1367. if (visible) {
  1368. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1369. if (!obs_source_add_active_child(item->parent->source,
  1370. item->source)) {
  1371. os_atomic_dec_long(&item->active_refs);
  1372. return false;
  1373. }
  1374. }
  1375. }
  1376. item->user_visible = visible;
  1377. calldata_init_fixed(&cd, stack, sizeof(stack));
  1378. calldata_set_ptr(&cd, "scene", item->parent);
  1379. calldata_set_ptr(&cd, "item", item);
  1380. calldata_set_bool(&cd, "visible", visible);
  1381. signal_handler_signal(item->parent->source->context.signals,
  1382. "item_visible", &cd);
  1383. if (source_has_audio(item->source)) {
  1384. pthread_mutex_lock(&item->actions_mutex);
  1385. da_push_back(item->audio_actions, &action);
  1386. pthread_mutex_unlock(&item->actions_mutex);
  1387. } else {
  1388. set_visibility(item, visible);
  1389. }
  1390. return true;
  1391. }
  1392. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1393. size_t size, bool *order_matches)
  1394. {
  1395. obs_sceneitem_t *item = scene->first_item;
  1396. size_t count = 0;
  1397. while (item) {
  1398. bool found = false;
  1399. for (size_t i = 0; i < size; i++) {
  1400. if (items[i] != item)
  1401. continue;
  1402. if (count != i)
  1403. *order_matches = false;
  1404. found = true;
  1405. break;
  1406. }
  1407. if (!found)
  1408. return false;
  1409. item = item->next;
  1410. count += 1;
  1411. }
  1412. return count == size;
  1413. }
  1414. bool obs_scene_reorder_items(obs_scene_t *scene,
  1415. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1416. {
  1417. if (!scene || !item_order_size)
  1418. return false;
  1419. obs_scene_addref(scene);
  1420. full_lock(scene);
  1421. bool order_matches = true;
  1422. if (!sceneitems_match(scene, item_order, item_order_size,
  1423. &order_matches) || order_matches) {
  1424. full_unlock(scene);
  1425. obs_scene_release(scene);
  1426. return false;
  1427. }
  1428. scene->first_item = item_order[0];
  1429. obs_sceneitem_t *prev = NULL;
  1430. for (size_t i = 0; i < item_order_size; i++) {
  1431. item_order[i]->prev = prev;
  1432. item_order[i]->next = NULL;
  1433. if (prev)
  1434. prev->next = item_order[i];
  1435. prev = item_order[i];
  1436. }
  1437. signal_reorder(scene->first_item);
  1438. full_unlock(scene);
  1439. obs_scene_release(scene);
  1440. return true;
  1441. }
  1442. void obs_scene_atomic_update(obs_scene_t *scene,
  1443. obs_scene_atomic_update_func func, void *data)
  1444. {
  1445. if (!scene)
  1446. return;
  1447. obs_scene_addref(scene);
  1448. full_lock(scene);
  1449. func(data, scene);
  1450. full_unlock(scene);
  1451. obs_scene_release(scene);
  1452. }
  1453. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  1454. const struct obs_sceneitem_crop *crop2)
  1455. {
  1456. return crop1->left == crop2->left &&
  1457. crop1->right == crop2->right &&
  1458. crop1->top == crop2->top &&
  1459. crop1->bottom == crop2->bottom;
  1460. }
  1461. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  1462. const struct obs_sceneitem_crop *crop)
  1463. {
  1464. bool item_tex_now_enabled;
  1465. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  1466. return;
  1467. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  1468. return;
  1469. if (crop_equal(crop, &item->crop))
  1470. return;
  1471. item_tex_now_enabled = crop_enabled(crop) ||
  1472. scale_filter_enabled(item) || item_is_scene(item);
  1473. obs_enter_graphics();
  1474. if (!item_tex_now_enabled) {
  1475. gs_texrender_destroy(item->item_render);
  1476. item->item_render = NULL;
  1477. } else if (!item->item_render) {
  1478. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1479. }
  1480. memcpy(&item->crop, crop, sizeof(*crop));
  1481. if (item->crop.left < 0) item->crop.left = 0;
  1482. if (item->crop.right < 0) item->crop.right = 0;
  1483. if (item->crop.top < 0) item->crop.top = 0;
  1484. if (item->crop.bottom < 0) item->crop.bottom = 0;
  1485. obs_leave_graphics();
  1486. update_item_transform(item);
  1487. }
  1488. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  1489. struct obs_sceneitem_crop *crop)
  1490. {
  1491. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  1492. return;
  1493. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  1494. return;
  1495. memcpy(crop, &item->crop, sizeof(*crop));
  1496. }
  1497. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  1498. enum obs_scale_type filter)
  1499. {
  1500. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  1501. return;
  1502. item->scale_filter = filter;
  1503. obs_enter_graphics();
  1504. if (!item_texture_enabled(item)) {
  1505. gs_texrender_destroy(item->item_render);
  1506. item->item_render = NULL;
  1507. } else if (!item->item_render) {
  1508. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1509. }
  1510. obs_leave_graphics();
  1511. update_item_transform(item);
  1512. }
  1513. enum obs_scale_type obs_sceneitem_get_scale_filter(
  1514. obs_sceneitem_t *item)
  1515. {
  1516. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter") ?
  1517. item->scale_filter : OBS_SCALE_DISABLE;
  1518. }
  1519. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  1520. {
  1521. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  1522. return;
  1523. os_atomic_inc_long(&item->defer_update);
  1524. }
  1525. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  1526. {
  1527. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  1528. return;
  1529. if (os_atomic_dec_long(&item->defer_update) == 0)
  1530. update_item_transform(item);
  1531. }