obs-scene.c 49 KB

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