obs-scene.c 47 KB

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