obs-scene.c 45 KB

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