obs-scene.c 49 KB

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