obs-scene.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575
  1. /******************************************************************************
  2. Copyright (C) 2013-2015 by Hugh Bailey <[email protected]>
  3. Philippe Groarke <[email protected]>
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. ******************************************************************************/
  15. #include "util/threading.h"
  16. #include "graphics/math-defs.h"
  17. #include "obs-scene.h"
  18. /* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
  19. * lock the graphics mutex inside either of the scene mutexes.
  20. *
  21. * Another thing that must be done to prevent that cross-lock (and improve
  22. * performance), is to not create/release/update sources within the scene
  23. * mutexes.
  24. *
  25. * It's okay to lock the graphics mutex before locking either of the scene
  26. * mutexes, but not after.
  27. */
  28. static const char *obs_scene_signals[] = {
  29. "void item_add(ptr scene, ptr item)",
  30. "void item_remove(ptr scene, ptr item)",
  31. "void reorder(ptr scene)",
  32. "void item_visible(ptr scene, ptr item, bool visible)",
  33. "void item_select(ptr scene, ptr item)",
  34. "void item_deselect(ptr scene, ptr item)",
  35. "void item_transform(ptr scene, ptr item)",
  36. NULL
  37. };
  38. static inline void signal_item_remove(struct obs_scene_item *item)
  39. {
  40. struct calldata params;
  41. uint8_t stack[128];
  42. calldata_init_fixed(&params, stack, sizeof(stack));
  43. calldata_set_ptr(&params, "scene", item->parent);
  44. calldata_set_ptr(&params, "item", item);
  45. signal_handler_signal(item->parent->source->context.signals,
  46. "item_remove", &params);
  47. }
  48. static const char *scene_getname(void *unused)
  49. {
  50. UNUSED_PARAMETER(unused);
  51. return "Scene";
  52. }
  53. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  54. {
  55. pthread_mutexattr_t attr;
  56. struct obs_scene *scene = bmalloc(sizeof(struct obs_scene));
  57. scene->source = source;
  58. scene->first_item = NULL;
  59. signal_handler_add_array(obs_source_get_signal_handler(source),
  60. obs_scene_signals);
  61. if (pthread_mutexattr_init(&attr) != 0)
  62. goto fail;
  63. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  64. goto fail;
  65. if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
  66. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  67. "mutex");
  68. goto fail;
  69. }
  70. if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
  71. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  72. "mutex");
  73. goto fail;
  74. }
  75. UNUSED_PARAMETER(settings);
  76. return scene;
  77. fail:
  78. pthread_mutexattr_destroy(&attr);
  79. bfree(scene);
  80. return NULL;
  81. }
  82. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  83. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  84. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  85. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  86. static inline void full_lock(struct obs_scene *scene)
  87. {
  88. video_lock(scene);
  89. audio_lock(scene);
  90. }
  91. static inline void full_unlock(struct obs_scene *scene)
  92. {
  93. audio_unlock(scene);
  94. video_unlock(scene);
  95. }
  96. static void set_visibility(struct obs_scene_item *item, bool vis);
  97. static inline void detach_sceneitem(struct obs_scene_item *item);
  98. static inline void remove_without_release(struct obs_scene_item *item)
  99. {
  100. item->removed = true;
  101. set_visibility(item, false);
  102. signal_item_remove(item);
  103. detach_sceneitem(item);
  104. }
  105. static void remove_all_items(struct obs_scene *scene)
  106. {
  107. struct obs_scene_item *item;
  108. DARRAY(struct obs_scene_item*) items;
  109. da_init(items);
  110. full_lock(scene);
  111. item = scene->first_item;
  112. while (item) {
  113. struct obs_scene_item *del_item = item;
  114. item = item->next;
  115. remove_without_release(del_item);
  116. da_push_back(items, &del_item);
  117. }
  118. full_unlock(scene);
  119. for (size_t i = 0; i < items.num; i++)
  120. obs_sceneitem_release(items.array[i]);
  121. da_free(items);
  122. }
  123. static void scene_destroy(void *data)
  124. {
  125. struct obs_scene *scene = data;
  126. remove_all_items(scene);
  127. pthread_mutex_destroy(&scene->video_mutex);
  128. pthread_mutex_destroy(&scene->audio_mutex);
  129. bfree(scene);
  130. }
  131. static void scene_enum_sources(void *data,
  132. obs_source_enum_proc_t enum_callback,
  133. void *param)
  134. {
  135. struct obs_scene *scene = data;
  136. struct obs_scene_item *item;
  137. struct obs_scene_item *next;
  138. full_lock(scene);
  139. item = scene->first_item;
  140. while (item) {
  141. next = item->next;
  142. obs_sceneitem_addref(item);
  143. if (os_atomic_load_long(&item->active_refs) > 0)
  144. enum_callback(scene->source, item->source, param);
  145. obs_sceneitem_release(item);
  146. item = next;
  147. }
  148. full_unlock(scene);
  149. }
  150. static inline void detach_sceneitem(struct obs_scene_item *item)
  151. {
  152. if (item->prev)
  153. item->prev->next = item->next;
  154. else
  155. item->parent->first_item = item->next;
  156. if (item->next)
  157. item->next->prev = item->prev;
  158. item->parent = NULL;
  159. }
  160. static inline void attach_sceneitem(struct obs_scene *parent,
  161. struct obs_scene_item *item, struct obs_scene_item *prev)
  162. {
  163. item->prev = prev;
  164. item->parent = parent;
  165. if (prev) {
  166. item->next = prev->next;
  167. if (prev->next)
  168. prev->next->prev = item;
  169. prev->next = item;
  170. } else {
  171. item->next = parent->first_item;
  172. if (parent->first_item)
  173. parent->first_item->prev = item;
  174. parent->first_item = item;
  175. }
  176. }
  177. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  178. {
  179. if (align & OBS_ALIGN_RIGHT)
  180. v->x += (float)cx;
  181. else if ((align & OBS_ALIGN_LEFT) == 0)
  182. v->x += (float)(cx / 2);
  183. if (align & OBS_ALIGN_BOTTOM)
  184. v->y += (float)cy;
  185. else if ((align & OBS_ALIGN_TOP) == 0)
  186. v->y += (float)(cy / 2);
  187. }
  188. static void calculate_bounds_data(struct obs_scene_item *item,
  189. struct vec2 *origin, struct vec2 *scale,
  190. uint32_t *cx, uint32_t *cy)
  191. {
  192. float width = (float)(*cx) * fabsf(scale->x);
  193. float height = (float)(*cy) * fabsf(scale->y);
  194. float item_aspect = width / height;
  195. float bounds_aspect = item->bounds.x / item->bounds.y;
  196. uint32_t bounds_type = item->bounds_type;
  197. float width_diff, height_diff;
  198. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  199. if (width > item->bounds.x || height > item->bounds.y)
  200. bounds_type = OBS_BOUNDS_SCALE_INNER;
  201. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  202. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  203. bool use_width = (bounds_aspect < item_aspect);
  204. float mul;
  205. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  206. use_width = !use_width;
  207. mul = use_width ?
  208. item->bounds.x / width :
  209. item->bounds.y / height;
  210. vec2_mulf(scale, scale, mul);
  211. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  212. vec2_mulf(scale, scale, item->bounds.x / width);
  213. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  214. vec2_mulf(scale, scale, item->bounds.y / height);
  215. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  216. scale->x = item->bounds.x / (float)(*cx);
  217. scale->y = item->bounds.y / (float)(*cy);
  218. }
  219. width = (float)(*cx) * scale->x;
  220. height = (float)(*cy) * scale->y;
  221. width_diff = item->bounds.x - width;
  222. height_diff = item->bounds.y - height;
  223. *cx = (uint32_t)item->bounds.x;
  224. *cy = (uint32_t)item->bounds.y;
  225. add_alignment(origin, item->bounds_align,
  226. (int)-width_diff, (int)-height_diff);
  227. }
  228. static void update_item_transform(struct obs_scene_item *item)
  229. {
  230. uint32_t width = obs_source_get_width(item->source);
  231. uint32_t height = obs_source_get_height(item->source);
  232. uint32_t cx = width;
  233. uint32_t cy = height;
  234. struct vec2 base_origin;
  235. struct vec2 origin;
  236. struct vec2 scale = item->scale;
  237. struct calldata params;
  238. uint8_t stack[128];
  239. vec2_zero(&base_origin);
  240. vec2_zero(&origin);
  241. /* ----------------------- */
  242. if (item->bounds_type != OBS_BOUNDS_NONE) {
  243. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  244. } else {
  245. cx = (uint32_t)((float)cx * scale.x);
  246. cy = (uint32_t)((float)cy * scale.y);
  247. }
  248. add_alignment(&origin, item->align, (int)cx, (int)cy);
  249. matrix4_identity(&item->draw_transform);
  250. matrix4_scale3f(&item->draw_transform, &item->draw_transform,
  251. scale.x, scale.y, 1.0f);
  252. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  253. -origin.x, -origin.y, 0.0f);
  254. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform,
  255. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  256. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  257. item->pos.x, item->pos.y, 0.0f);
  258. /* ----------------------- */
  259. if (item->bounds_type != OBS_BOUNDS_NONE) {
  260. vec2_copy(&scale, &item->bounds);
  261. } else {
  262. scale.x = (float)width * item->scale.x;
  263. scale.y = (float)height * item->scale.y;
  264. }
  265. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  266. matrix4_identity(&item->box_transform);
  267. matrix4_scale3f(&item->box_transform, &item->box_transform,
  268. scale.x, scale.y, 1.0f);
  269. matrix4_translate3f(&item->box_transform, &item->box_transform,
  270. -base_origin.x, -base_origin.y, 0.0f);
  271. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform,
  272. 0.0f, 0.0f, 1.0f, RAD(item->rot));
  273. matrix4_translate3f(&item->box_transform, &item->box_transform,
  274. item->pos.x, item->pos.y, 0.0f);
  275. /* ----------------------- */
  276. item->last_width = width;
  277. item->last_height = height;
  278. calldata_init_fixed(&params, stack, sizeof(stack));
  279. calldata_set_ptr(&params, "scene", item->parent);
  280. calldata_set_ptr(&params, "item", item);
  281. signal_handler_signal(item->parent->source->context.signals,
  282. "item_transform", &params);
  283. }
  284. static inline bool source_size_changed(struct obs_scene_item *item)
  285. {
  286. uint32_t width = obs_source_get_width(item->source);
  287. uint32_t height = obs_source_get_height(item->source);
  288. return item->last_width != width || item->last_height != height;
  289. }
  290. static void scene_video_render(void *data, gs_effect_t *effect)
  291. {
  292. DARRAY(struct obs_scene_item*) remove_items;
  293. struct obs_scene *scene = data;
  294. struct obs_scene_item *item;
  295. da_init(remove_items);
  296. video_lock(scene);
  297. item = scene->first_item;
  298. gs_blend_state_push();
  299. gs_reset_blend_state();
  300. while (item) {
  301. if (obs_source_removed(item->source)) {
  302. struct obs_scene_item *del_item = item;
  303. item = item->next;
  304. remove_without_release(del_item);
  305. da_push_back(remove_items, &del_item);
  306. continue;
  307. }
  308. if (source_size_changed(item))
  309. update_item_transform(item);
  310. if (item->user_visible) {
  311. gs_matrix_push();
  312. gs_matrix_mul(&item->draw_transform);
  313. obs_source_video_render(item->source);
  314. gs_matrix_pop();
  315. }
  316. item = item->next;
  317. }
  318. gs_blend_state_pop();
  319. video_unlock(scene);
  320. for (size_t i = 0; i < remove_items.num; i++)
  321. obs_sceneitem_release(remove_items.array[i]);
  322. da_free(remove_items);
  323. UNUSED_PARAMETER(effect);
  324. }
  325. static void set_visibility(struct obs_scene_item *item, bool vis)
  326. {
  327. pthread_mutex_lock(&item->actions_mutex);
  328. da_resize(item->audio_actions, 0);
  329. if (os_atomic_load_long(&item->active_refs) > 0) {
  330. if (!vis)
  331. obs_source_remove_active_child(item->parent->source,
  332. item->source);
  333. } else if (vis) {
  334. obs_source_add_active_child(item->parent->source, item->source);
  335. }
  336. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  337. item->visible = vis;
  338. item->user_visible = vis;
  339. pthread_mutex_unlock(&item->actions_mutex);
  340. }
  341. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  342. {
  343. const char *name = obs_data_get_string(item_data, "name");
  344. obs_source_t *source = obs_get_source_by_name(name);
  345. struct obs_scene_item *item;
  346. bool visible;
  347. if (!source) {
  348. blog(LOG_WARNING, "[scene_load_item] Source %s not found!",
  349. name);
  350. return;
  351. }
  352. item = obs_scene_add(scene, source);
  353. if (!item) {
  354. blog(LOG_WARNING, "[scene_load_item] Could not add source '%s' "
  355. "to scene '%s'!",
  356. name, obs_source_get_name(scene->source));
  357. obs_source_release(source);
  358. return;
  359. }
  360. obs_data_set_default_int(item_data, "align",
  361. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  362. item->rot = (float)obs_data_get_double(item_data, "rot");
  363. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  364. visible = obs_data_get_bool(item_data, "visible");
  365. obs_data_get_vec2(item_data, "pos", &item->pos);
  366. obs_data_get_vec2(item_data, "scale", &item->scale);
  367. set_visibility(item, visible);
  368. item->bounds_type =
  369. (enum obs_bounds_type)obs_data_get_int(item_data,
  370. "bounds_type");
  371. item->bounds_align =
  372. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  373. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  374. obs_source_release(source);
  375. update_item_transform(item);
  376. }
  377. static void scene_load(void *scene, obs_data_t *settings)
  378. {
  379. obs_data_array_t *items = obs_data_get_array(settings, "items");
  380. size_t count, i;
  381. remove_all_items(scene);
  382. if (!items) return;
  383. count = obs_data_array_count(items);
  384. for (i = 0; i < count; i++) {
  385. obs_data_t *item_data = obs_data_array_item(items, i);
  386. scene_load_item(scene, item_data);
  387. obs_data_release(item_data);
  388. }
  389. obs_data_array_release(items);
  390. }
  391. static void scene_save_item(obs_data_array_t *array,
  392. struct obs_scene_item *item)
  393. {
  394. obs_data_t *item_data = obs_data_create();
  395. const char *name = obs_source_get_name(item->source);
  396. obs_data_set_string(item_data, "name", name);
  397. obs_data_set_bool (item_data, "visible", item->user_visible);
  398. obs_data_set_double(item_data, "rot", item->rot);
  399. obs_data_set_vec2 (item_data, "pos", &item->pos);
  400. obs_data_set_vec2 (item_data, "scale", &item->scale);
  401. obs_data_set_int (item_data, "align", (int)item->align);
  402. obs_data_set_int (item_data, "bounds_type", (int)item->bounds_type);
  403. obs_data_set_int (item_data, "bounds_align", (int)item->bounds_align);
  404. obs_data_set_vec2 (item_data, "bounds", &item->bounds);
  405. obs_data_array_push_back(array, item_data);
  406. obs_data_release(item_data);
  407. }
  408. static void scene_save(void *data, obs_data_t *settings)
  409. {
  410. struct obs_scene *scene = data;
  411. obs_data_array_t *array = obs_data_array_create();
  412. struct obs_scene_item *item;
  413. full_lock(scene);
  414. item = scene->first_item;
  415. while (item) {
  416. scene_save_item(array, item);
  417. item = item->next;
  418. }
  419. full_unlock(scene);
  420. obs_data_set_array(settings, "items", array);
  421. obs_data_array_release(array);
  422. }
  423. static uint32_t scene_getwidth(void *data)
  424. {
  425. UNUSED_PARAMETER(data);
  426. return obs->video.base_width;
  427. }
  428. static uint32_t scene_getheight(void *data)
  429. {
  430. UNUSED_PARAMETER(data);
  431. return obs->video.base_height;
  432. }
  433. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  434. float **p_buf, uint64_t ts, size_t sample_rate)
  435. {
  436. bool cur_visible = item->visible;
  437. uint64_t frame_num = 0;
  438. size_t deref_count = 0;
  439. float *buf;
  440. if (!*p_buf)
  441. *p_buf = malloc(AUDIO_OUTPUT_FRAMES * sizeof(float));
  442. buf = *p_buf;
  443. pthread_mutex_lock(&item->actions_mutex);
  444. for (size_t i = 0; i < item->audio_actions.num; i++) {
  445. struct item_action action = item->audio_actions.array[i];
  446. uint64_t timestamp = action.timestamp;
  447. uint64_t new_frame_num;
  448. if (timestamp < ts)
  449. timestamp = ts;
  450. new_frame_num = (timestamp - ts) * (uint64_t)sample_rate /
  451. 1000000000ULL;
  452. if (new_frame_num >= AUDIO_OUTPUT_FRAMES)
  453. break;
  454. da_erase(item->audio_actions, i--);
  455. item->visible = action.visible;
  456. if (!item->visible)
  457. deref_count++;
  458. if (new_frame_num > frame_num) {
  459. for (; frame_num < new_frame_num; frame_num++)
  460. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  461. }
  462. cur_visible = item->visible;
  463. }
  464. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  465. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  466. pthread_mutex_unlock(&item->actions_mutex);
  467. while (deref_count--) {
  468. if (os_atomic_dec_long(&item->active_refs) == 0) {
  469. obs_source_remove_active_child(item->parent->source,
  470. item->source);
  471. }
  472. }
  473. }
  474. static inline bool apply_scene_item_volume(struct obs_scene_item *item,
  475. float **buf, uint64_t ts, size_t sample_rate)
  476. {
  477. bool actions_pending;
  478. struct item_action action;
  479. pthread_mutex_lock(&item->actions_mutex);
  480. actions_pending = item->audio_actions.num > 0;
  481. if (actions_pending)
  482. action = item->audio_actions.array[0];
  483. pthread_mutex_unlock(&item->actions_mutex);
  484. if (actions_pending) {
  485. uint64_t duration = (uint64_t)AUDIO_OUTPUT_FRAMES *
  486. 1000000000ULL / (uint64_t)sample_rate;
  487. if (action.timestamp < (ts + duration)) {
  488. apply_scene_item_audio_actions(item, buf, ts,
  489. sample_rate);
  490. return true;
  491. }
  492. }
  493. return false;
  494. }
  495. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  496. size_t pos, size_t count)
  497. {
  498. register float *out = p_out;
  499. register float *buf = buf_in + pos;
  500. register float *in = p_in + pos;
  501. register float *end = in + count;
  502. while (in < end)
  503. *(out++) += *(in++) * *(buf++);
  504. }
  505. static inline void mix_audio(float *p_out, float *p_in,
  506. size_t pos, size_t count)
  507. {
  508. register float *out = p_out;
  509. register float *in = p_in + pos;
  510. register float *end = in + count;
  511. while (in < end)
  512. *(out++) += *(in++);
  513. }
  514. static bool scene_audio_render(void *data, uint64_t *ts_out,
  515. struct obs_source_audio_mix *audio_output, uint32_t mixers,
  516. size_t channels, size_t sample_rate)
  517. {
  518. uint64_t timestamp = 0;
  519. float *buf = NULL;
  520. struct obs_source_audio_mix child_audio;
  521. struct obs_scene *scene = data;
  522. struct obs_scene_item *item;
  523. audio_lock(scene);
  524. item = scene->first_item;
  525. while (item) {
  526. if (!obs_source_audio_pending(item->source)) {
  527. uint64_t source_ts =
  528. obs_source_get_audio_timestamp(item->source);
  529. if (!timestamp || source_ts < timestamp)
  530. timestamp = source_ts;
  531. }
  532. item = item->next;
  533. }
  534. if (!timestamp) {
  535. audio_unlock(scene);
  536. return false;
  537. }
  538. item = scene->first_item;
  539. while (item) {
  540. uint64_t source_ts;
  541. size_t pos, count;
  542. bool apply_buf;
  543. apply_buf = apply_scene_item_volume(item, &buf, timestamp,
  544. sample_rate);
  545. if (obs_source_audio_pending(item->source)) {
  546. item = item->next;
  547. continue;
  548. }
  549. source_ts = obs_source_get_audio_timestamp(item->source);
  550. pos = (size_t)ns_to_audio_frames(sample_rate,
  551. source_ts - timestamp);
  552. count = AUDIO_OUTPUT_FRAMES - pos;
  553. if (!apply_buf && !item->visible) {
  554. item = item->next;
  555. continue;
  556. }
  557. obs_source_get_audio_mix(item->source, &child_audio);
  558. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  559. if ((mixers & (1 << mix)) == 0)
  560. continue;
  561. for (size_t ch = 0; ch < channels; ch++) {
  562. float *out = audio_output->output[mix].data[ch];
  563. float *in = child_audio.output[mix].data[ch];
  564. if (apply_buf)
  565. mix_audio_with_buf(out, in, buf, pos,
  566. count);
  567. else
  568. mix_audio(out, in, pos, count);
  569. }
  570. }
  571. item = item->next;
  572. }
  573. *ts_out = timestamp;
  574. audio_unlock(scene);
  575. free(buf);
  576. return true;
  577. }
  578. const struct obs_source_info scene_info =
  579. {
  580. .id = "scene",
  581. .type = OBS_SOURCE_TYPE_SCENE,
  582. .output_flags = OBS_SOURCE_VIDEO |
  583. OBS_SOURCE_CUSTOM_DRAW |
  584. OBS_SOURCE_COMPOSITE,
  585. .get_name = scene_getname,
  586. .create = scene_create,
  587. .destroy = scene_destroy,
  588. .video_render = scene_video_render,
  589. .audio_render = scene_audio_render,
  590. .get_width = scene_getwidth,
  591. .get_height = scene_getheight,
  592. .load = scene_load,
  593. .save = scene_save,
  594. .enum_active_sources = scene_enum_sources
  595. };
  596. obs_scene_t *obs_scene_create(const char *name)
  597. {
  598. struct obs_source *source = obs_source_create("scene", name, NULL,
  599. NULL);
  600. return source->context.data;
  601. }
  602. obs_scene_t *obs_scene_create_private(const char *name)
  603. {
  604. struct obs_source *source = obs_source_create_private("scene", name,
  605. NULL);
  606. return source->context.data;
  607. }
  608. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  609. {
  610. struct obs_scene_item *item = scene->first_item;
  611. while (item && idx--)
  612. item = item->next;
  613. return item ? item->source : NULL;
  614. }
  615. static inline obs_source_t *dup_child(struct darray *array, size_t idx,
  616. obs_scene_t *new_scene, bool private)
  617. {
  618. DARRAY(struct obs_scene_item*) old_items;
  619. obs_source_t *source;
  620. old_items.da = *array;
  621. source = old_items.array[idx]->source;
  622. /* if the old item is referenced more than once in the old scene,
  623. * make sure they're referenced similarly in the new scene to reduce
  624. * load times */
  625. for (size_t i = 0; i < idx; i++) {
  626. struct obs_scene_item *item = old_items.array[i];
  627. if (item->source == source) {
  628. source = get_child_at_idx(new_scene, i);
  629. obs_source_addref(source);
  630. return source;
  631. }
  632. }
  633. return obs_source_duplicate(source, NULL, private);
  634. }
  635. static inline obs_source_t *new_ref(obs_source_t *source)
  636. {
  637. obs_source_addref(source);
  638. return source;
  639. }
  640. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  641. enum obs_scene_duplicate_type type)
  642. {
  643. bool make_unique = ((int)type & (1<<0)) != 0;
  644. bool make_private = ((int)type & (1<<1)) != 0;
  645. DARRAY(struct obs_scene_item*) items;
  646. struct obs_scene *new_scene;
  647. struct obs_scene_item *item;
  648. struct obs_source *source;
  649. da_init(items);
  650. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  651. return NULL;
  652. /* --------------------------------- */
  653. full_lock(scene);
  654. item = scene->first_item;
  655. while (item) {
  656. da_push_back(items, &item);
  657. obs_sceneitem_addref(item);
  658. item = item->next;
  659. }
  660. full_unlock(scene);
  661. /* --------------------------------- */
  662. new_scene = make_private ?
  663. obs_scene_create_private(name) : obs_scene_create(name);
  664. for (size_t i = 0; i < items.num; i++) {
  665. item = items.array[i];
  666. source = make_unique ?
  667. dup_child(&items.da, i, new_scene, make_private) :
  668. new_ref(item->source);
  669. if (source) {
  670. struct obs_scene_item *new_item =
  671. obs_scene_add(new_scene, source);
  672. if (!new_item) {
  673. obs_source_release(source);
  674. continue;
  675. }
  676. if (!item->user_visible)
  677. set_visibility(new_item, false);
  678. new_item->selected = item->selected;
  679. new_item->pos = item->pos;
  680. new_item->rot = item->rot;
  681. new_item->scale = item->scale;
  682. new_item->align = item->align;
  683. new_item->last_width = item->last_width;
  684. new_item->last_height = item->last_height;
  685. new_item->box_transform = item->box_transform;
  686. new_item->draw_transform = item->draw_transform;
  687. new_item->bounds_type = item->bounds_type;
  688. new_item->bounds_align = item->bounds_align;
  689. new_item->bounds = item->bounds;
  690. obs_source_release(source);
  691. }
  692. }
  693. for (size_t i = 0; i < items.num; i++)
  694. obs_sceneitem_release(items.array[i]);
  695. da_free(items);
  696. return new_scene;
  697. }
  698. void obs_scene_addref(obs_scene_t *scene)
  699. {
  700. if (scene)
  701. obs_source_addref(scene->source);
  702. }
  703. void obs_scene_release(obs_scene_t *scene)
  704. {
  705. if (scene)
  706. obs_source_release(scene->source);
  707. }
  708. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  709. {
  710. return scene ? scene->source : NULL;
  711. }
  712. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  713. {
  714. if (!source || source->info.id != scene_info.id)
  715. return NULL;
  716. return source->context.data;
  717. }
  718. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  719. {
  720. struct obs_scene_item *item;
  721. if (!scene)
  722. return NULL;
  723. full_lock(scene);
  724. item = scene->first_item;
  725. while (item) {
  726. if (strcmp(item->source->context.name, name) == 0)
  727. break;
  728. item = item->next;
  729. }
  730. full_unlock(scene);
  731. return item;
  732. }
  733. void obs_scene_enum_items(obs_scene_t *scene,
  734. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  735. void *param)
  736. {
  737. struct obs_scene_item *item;
  738. if (!scene || !callback)
  739. return;
  740. full_lock(scene);
  741. item = scene->first_item;
  742. while (item) {
  743. struct obs_scene_item *next = item->next;
  744. obs_sceneitem_addref(item);
  745. if (!callback(scene, item, param)) {
  746. obs_sceneitem_release(item);
  747. break;
  748. }
  749. obs_sceneitem_release(item);
  750. item = next;
  751. }
  752. full_unlock(scene);
  753. }
  754. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  755. {
  756. long owners = si->ref;
  757. while (owners > 0) {
  758. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  759. return si;
  760. owners = si->ref;
  761. }
  762. return NULL;
  763. }
  764. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  765. obs_hotkey_t *hotkey, bool pressed)
  766. {
  767. UNUSED_PARAMETER(id);
  768. UNUSED_PARAMETER(hotkey);
  769. obs_sceneitem_t *si = sceneitem_get_ref(data);
  770. if (pressed && si && !si->user_visible) {
  771. obs_sceneitem_set_visible(si, true);
  772. obs_sceneitem_release(si);
  773. return true;
  774. }
  775. obs_sceneitem_release(si);
  776. return false;
  777. }
  778. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  779. obs_hotkey_t *hotkey, bool pressed)
  780. {
  781. UNUSED_PARAMETER(id);
  782. UNUSED_PARAMETER(hotkey);
  783. obs_sceneitem_t *si = sceneitem_get_ref(data);
  784. if (pressed && si && si->user_visible) {
  785. obs_sceneitem_set_visible(si, false);
  786. obs_sceneitem_release(si);
  787. return true;
  788. }
  789. obs_sceneitem_release(si);
  790. return false;
  791. }
  792. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  793. const char *name)
  794. {
  795. struct dstr show = {0};
  796. struct dstr hide = {0};
  797. struct dstr show_desc = {0};
  798. struct dstr hide_desc = {0};
  799. dstr_copy(&show, "libobs.show_scene_item.%1");
  800. dstr_replace(&show, "%1", name);
  801. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  802. dstr_replace(&hide, "%1", name);
  803. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  804. dstr_replace(&show_desc, "%1", name);
  805. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  806. dstr_replace(&hide_desc, "%1", name);
  807. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  808. show.array, show_desc.array,
  809. hide.array, hide_desc.array,
  810. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  811. item, item);
  812. dstr_free(&show);
  813. dstr_free(&hide);
  814. dstr_free(&show_desc);
  815. dstr_free(&hide_desc);
  816. }
  817. static inline bool source_has_audio(obs_source_t *source)
  818. {
  819. return (source->info.output_flags &
  820. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  821. }
  822. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  823. {
  824. struct obs_scene_item *last;
  825. struct obs_scene_item *item;
  826. struct calldata params;
  827. uint8_t stack[128];
  828. pthread_mutex_t mutex;
  829. struct item_action action = {
  830. .visible = true,
  831. .timestamp = os_gettime_ns()
  832. };
  833. if (!scene)
  834. return NULL;
  835. if (!source) {
  836. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  837. return NULL;
  838. }
  839. if (pthread_mutex_init(&mutex, NULL) != 0) {
  840. blog(LOG_WARNING, "Failed to create scene item mutex");
  841. return NULL;
  842. }
  843. if (!obs_source_add_active_child(scene->source, source)) {
  844. blog(LOG_WARNING, "Failed to add source to scene due to "
  845. "infinite source recursion");
  846. pthread_mutex_destroy(&mutex);
  847. return NULL;
  848. }
  849. item = bzalloc(sizeof(struct obs_scene_item));
  850. item->source = source;
  851. item->parent = scene;
  852. item->ref = 1;
  853. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  854. item->actions_mutex = mutex;
  855. item->user_visible = true;
  856. os_atomic_set_long(&item->active_refs, 1);
  857. vec2_set(&item->scale, 1.0f, 1.0f);
  858. matrix4_identity(&item->draw_transform);
  859. matrix4_identity(&item->box_transform);
  860. obs_source_addref(source);
  861. if (source_has_audio(source)) {
  862. item->visible = false;
  863. da_push_back(item->audio_actions, &action);
  864. } else {
  865. item->visible = true;
  866. }
  867. full_lock(scene);
  868. last = scene->first_item;
  869. if (!last) {
  870. scene->first_item = item;
  871. } else {
  872. while (last->next)
  873. last = last->next;
  874. last->next = item;
  875. item->prev = last;
  876. }
  877. full_unlock(scene);
  878. if (!scene->source->context.private)
  879. init_hotkeys(scene, item, obs_source_get_name(source));
  880. calldata_init_fixed(&params, stack, sizeof(stack));
  881. calldata_set_ptr(&params, "scene", scene);
  882. calldata_set_ptr(&params, "item", item);
  883. signal_handler_signal(scene->source->context.signals, "item_add",
  884. &params);
  885. return item;
  886. }
  887. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  888. {
  889. if (item) {
  890. obs_hotkey_pair_unregister(item->toggle_visibility);
  891. pthread_mutex_destroy(&item->actions_mutex);
  892. if (item->source)
  893. obs_source_release(item->source);
  894. da_free(item->audio_actions);
  895. bfree(item);
  896. }
  897. }
  898. void obs_sceneitem_addref(obs_sceneitem_t *item)
  899. {
  900. if (item)
  901. os_atomic_inc_long(&item->ref);
  902. }
  903. void obs_sceneitem_release(obs_sceneitem_t *item)
  904. {
  905. if (!item)
  906. return;
  907. if (os_atomic_dec_long(&item->ref) == 0)
  908. obs_sceneitem_destroy(item);
  909. }
  910. void obs_sceneitem_remove(obs_sceneitem_t *item)
  911. {
  912. obs_scene_t *scene;
  913. if (!item)
  914. return;
  915. scene = item->parent;
  916. if (scene)
  917. full_lock(scene);
  918. if (item->removed) {
  919. if (scene)
  920. full_unlock(scene);
  921. return;
  922. }
  923. item->removed = true;
  924. assert(scene != NULL);
  925. assert(scene->source != NULL);
  926. set_visibility(item, false);
  927. signal_item_remove(item);
  928. detach_sceneitem(item);
  929. full_unlock(scene);
  930. obs_sceneitem_release(item);
  931. }
  932. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  933. {
  934. return item ? item->parent : NULL;
  935. }
  936. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  937. {
  938. return item ? item->source : NULL;
  939. }
  940. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  941. {
  942. struct calldata params;
  943. uint8_t stack[128];
  944. const char *command = select ? "item_select" : "item_deselect";
  945. if (!item || item->selected == select)
  946. return;
  947. item->selected = select;
  948. calldata_init_fixed(&params, stack, sizeof(stack));
  949. calldata_set_ptr(&params, "scene", item->parent);
  950. calldata_set_ptr(&params, "item", item);
  951. signal_handler_signal(item->parent->source->context.signals,
  952. command, &params);
  953. }
  954. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  955. {
  956. return item ? item->selected : false;
  957. }
  958. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  959. {
  960. if (item) {
  961. vec2_copy(&item->pos, pos);
  962. update_item_transform(item);
  963. }
  964. }
  965. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  966. {
  967. if (item) {
  968. item->rot = rot;
  969. update_item_transform(item);
  970. }
  971. }
  972. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  973. {
  974. if (item) {
  975. vec2_copy(&item->scale, scale);
  976. update_item_transform(item);
  977. }
  978. }
  979. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  980. {
  981. if (item) {
  982. item->align = alignment;
  983. update_item_transform(item);
  984. }
  985. }
  986. static inline void signal_reorder(struct obs_scene_item *item)
  987. {
  988. const char *command = NULL;
  989. struct calldata params;
  990. uint8_t stack[128];
  991. command = "reorder";
  992. calldata_init_fixed(&params, stack, sizeof(stack));
  993. calldata_set_ptr(&params, "scene", item->parent);
  994. signal_handler_signal(item->parent->source->context.signals,
  995. command, &params);
  996. }
  997. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  998. enum obs_order_movement movement)
  999. {
  1000. if (!item) return;
  1001. struct obs_scene_item *next, *prev;
  1002. struct obs_scene *scene = item->parent;
  1003. obs_scene_addref(scene);
  1004. full_lock(scene);
  1005. next = item->next;
  1006. prev = item->prev;
  1007. detach_sceneitem(item);
  1008. if (movement == OBS_ORDER_MOVE_DOWN) {
  1009. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1010. } else if (movement == OBS_ORDER_MOVE_UP) {
  1011. attach_sceneitem(scene, item, next ? next : prev);
  1012. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1013. struct obs_scene_item *last = next;
  1014. if (!last) {
  1015. last = prev;
  1016. } else {
  1017. while (last->next)
  1018. last = last->next;
  1019. }
  1020. attach_sceneitem(scene, item, last);
  1021. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1022. attach_sceneitem(scene, item, NULL);
  1023. }
  1024. signal_reorder(item);
  1025. full_unlock(scene);
  1026. obs_scene_release(scene);
  1027. }
  1028. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  1029. int position)
  1030. {
  1031. if (!item) return;
  1032. struct obs_scene *scene = item->parent;
  1033. struct obs_scene_item *next;
  1034. obs_scene_addref(scene);
  1035. full_lock(scene);
  1036. detach_sceneitem(item);
  1037. next = scene->first_item;
  1038. if (position == 0) {
  1039. attach_sceneitem(scene, item, NULL);
  1040. } else {
  1041. for (int i = position; i > 1; --i) {
  1042. if (next->next == NULL)
  1043. break;
  1044. next = next->next;
  1045. }
  1046. attach_sceneitem(scene, item, next);
  1047. }
  1048. signal_reorder(item);
  1049. full_unlock(scene);
  1050. obs_scene_release(scene);
  1051. }
  1052. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1053. enum obs_bounds_type type)
  1054. {
  1055. if (item) {
  1056. item->bounds_type = type;
  1057. update_item_transform(item);
  1058. }
  1059. }
  1060. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1061. uint32_t alignment)
  1062. {
  1063. if (item) {
  1064. item->bounds_align = alignment;
  1065. update_item_transform(item);
  1066. }
  1067. }
  1068. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1069. {
  1070. if (item) {
  1071. item->bounds = *bounds;
  1072. update_item_transform(item);
  1073. }
  1074. }
  1075. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1076. {
  1077. if (item)
  1078. vec2_copy(pos, &item->pos);
  1079. }
  1080. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1081. {
  1082. return item ? item->rot : 0.0f;
  1083. }
  1084. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1085. {
  1086. if (item)
  1087. vec2_copy(scale, &item->scale);
  1088. }
  1089. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1090. {
  1091. return item ? item->align : 0;
  1092. }
  1093. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1094. {
  1095. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1096. }
  1097. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1098. {
  1099. return item ? item->bounds_align : 0;
  1100. }
  1101. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1102. {
  1103. if (item)
  1104. *bounds = item->bounds;
  1105. }
  1106. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1107. struct obs_transform_info *info)
  1108. {
  1109. if (item && info) {
  1110. info->pos = item->pos;
  1111. info->rot = item->rot;
  1112. info->scale = item->scale;
  1113. info->alignment = item->align;
  1114. info->bounds_type = item->bounds_type;
  1115. info->bounds_alignment = item->bounds_align;
  1116. info->bounds = item->bounds;
  1117. }
  1118. }
  1119. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1120. const struct obs_transform_info *info)
  1121. {
  1122. if (item && info) {
  1123. item->pos = info->pos;
  1124. item->rot = info->rot;
  1125. item->scale = info->scale;
  1126. item->align = info->alignment;
  1127. item->bounds_type = info->bounds_type;
  1128. item->bounds_align = info->bounds_alignment;
  1129. item->bounds = info->bounds;
  1130. update_item_transform(item);
  1131. }
  1132. }
  1133. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1134. struct matrix4 *transform)
  1135. {
  1136. if (item)
  1137. matrix4_copy(transform, &item->draw_transform);
  1138. }
  1139. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1140. struct matrix4 *transform)
  1141. {
  1142. if (item)
  1143. matrix4_copy(transform, &item->box_transform);
  1144. }
  1145. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1146. {
  1147. return item ? item->user_visible : false;
  1148. }
  1149. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1150. {
  1151. struct calldata cd;
  1152. uint8_t stack[256];
  1153. struct item_action action = {
  1154. .visible = visible,
  1155. .timestamp = os_gettime_ns()
  1156. };
  1157. if (!item)
  1158. return false;
  1159. if (item->user_visible == visible)
  1160. return false;
  1161. if (!item->parent)
  1162. return false;
  1163. if (visible) {
  1164. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1165. if (!obs_source_add_active_child(item->parent->source,
  1166. item->source)) {
  1167. os_atomic_dec_long(&item->active_refs);
  1168. return false;
  1169. }
  1170. }
  1171. }
  1172. item->user_visible = visible;
  1173. calldata_init_fixed(&cd, stack, sizeof(stack));
  1174. calldata_set_ptr(&cd, "scene", item->parent);
  1175. calldata_set_ptr(&cd, "item", item);
  1176. calldata_set_bool(&cd, "visible", visible);
  1177. signal_handler_signal(item->parent->source->context.signals,
  1178. "item_visible", &cd);
  1179. if (source_has_audio(item->source)) {
  1180. pthread_mutex_lock(&item->actions_mutex);
  1181. da_push_back(item->audio_actions, &action);
  1182. pthread_mutex_unlock(&item->actions_mutex);
  1183. } else {
  1184. set_visibility(item, visible);
  1185. }
  1186. return true;
  1187. }
  1188. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1189. size_t size, bool *order_matches)
  1190. {
  1191. obs_sceneitem_t *item = scene->first_item;
  1192. size_t count = 0;
  1193. while (item) {
  1194. bool found = false;
  1195. for (size_t i = 0; i < size; i++) {
  1196. if (items[i] != item)
  1197. continue;
  1198. if (count != i)
  1199. *order_matches = false;
  1200. found = true;
  1201. break;
  1202. }
  1203. if (!found)
  1204. return false;
  1205. item = item->next;
  1206. count += 1;
  1207. }
  1208. return count == size;
  1209. }
  1210. bool obs_scene_reorder_items(obs_scene_t *scene,
  1211. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1212. {
  1213. if (!scene || !item_order_size)
  1214. return false;
  1215. obs_scene_addref(scene);
  1216. full_lock(scene);
  1217. bool order_matches = true;
  1218. if (!sceneitems_match(scene, item_order, item_order_size,
  1219. &order_matches) || order_matches) {
  1220. full_unlock(scene);
  1221. obs_scene_release(scene);
  1222. return false;
  1223. }
  1224. scene->first_item = item_order[0];
  1225. obs_sceneitem_t *prev = NULL;
  1226. for (size_t i = 0; i < item_order_size; i++) {
  1227. item_order[i]->prev = prev;
  1228. item_order[i]->next = NULL;
  1229. if (prev)
  1230. prev->next = item_order[i];
  1231. prev = item_order[i];
  1232. }
  1233. signal_reorder(scene->first_item);
  1234. full_unlock(scene);
  1235. obs_scene_release(scene);
  1236. return true;
  1237. }
  1238. void obs_scene_atomic_update(obs_scene_t *scene,
  1239. obs_scene_atomic_update_func func, void *data)
  1240. {
  1241. if (!scene)
  1242. return;
  1243. obs_scene_addref(scene);
  1244. full_lock(scene);
  1245. func(data, scene);
  1246. full_unlock(scene);
  1247. obs_scene_release(scene);
  1248. }