obs-scene.c 36 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574
  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->scale = item->scale;
  681. new_item->align = item->align;
  682. new_item->last_width = item->last_width;
  683. new_item->last_height = item->last_height;
  684. new_item->box_transform = item->box_transform;
  685. new_item->draw_transform = item->draw_transform;
  686. new_item->bounds_type = item->bounds_type;
  687. new_item->bounds_align = item->bounds_align;
  688. new_item->bounds = item->bounds;
  689. obs_source_release(source);
  690. }
  691. }
  692. for (size_t i = 0; i < items.num; i++)
  693. obs_sceneitem_release(items.array[i]);
  694. da_free(items);
  695. return new_scene;
  696. }
  697. void obs_scene_addref(obs_scene_t *scene)
  698. {
  699. if (scene)
  700. obs_source_addref(scene->source);
  701. }
  702. void obs_scene_release(obs_scene_t *scene)
  703. {
  704. if (scene)
  705. obs_source_release(scene->source);
  706. }
  707. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  708. {
  709. return scene ? scene->source : NULL;
  710. }
  711. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  712. {
  713. if (!source || source->info.id != scene_info.id)
  714. return NULL;
  715. return source->context.data;
  716. }
  717. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  718. {
  719. struct obs_scene_item *item;
  720. if (!scene)
  721. return NULL;
  722. full_lock(scene);
  723. item = scene->first_item;
  724. while (item) {
  725. if (strcmp(item->source->context.name, name) == 0)
  726. break;
  727. item = item->next;
  728. }
  729. full_unlock(scene);
  730. return item;
  731. }
  732. void obs_scene_enum_items(obs_scene_t *scene,
  733. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  734. void *param)
  735. {
  736. struct obs_scene_item *item;
  737. if (!scene || !callback)
  738. return;
  739. full_lock(scene);
  740. item = scene->first_item;
  741. while (item) {
  742. struct obs_scene_item *next = item->next;
  743. obs_sceneitem_addref(item);
  744. if (!callback(scene, item, param)) {
  745. obs_sceneitem_release(item);
  746. break;
  747. }
  748. obs_sceneitem_release(item);
  749. item = next;
  750. }
  751. full_unlock(scene);
  752. }
  753. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  754. {
  755. long owners = si->ref;
  756. while (owners > 0) {
  757. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  758. return si;
  759. owners = si->ref;
  760. }
  761. return NULL;
  762. }
  763. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  764. obs_hotkey_t *hotkey, bool pressed)
  765. {
  766. UNUSED_PARAMETER(id);
  767. UNUSED_PARAMETER(hotkey);
  768. obs_sceneitem_t *si = sceneitem_get_ref(data);
  769. if (pressed && si && !si->user_visible) {
  770. obs_sceneitem_set_visible(si, true);
  771. obs_sceneitem_release(si);
  772. return true;
  773. }
  774. obs_sceneitem_release(si);
  775. return false;
  776. }
  777. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  778. obs_hotkey_t *hotkey, bool pressed)
  779. {
  780. UNUSED_PARAMETER(id);
  781. UNUSED_PARAMETER(hotkey);
  782. obs_sceneitem_t *si = sceneitem_get_ref(data);
  783. if (pressed && si && si->user_visible) {
  784. obs_sceneitem_set_visible(si, false);
  785. obs_sceneitem_release(si);
  786. return true;
  787. }
  788. obs_sceneitem_release(si);
  789. return false;
  790. }
  791. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  792. const char *name)
  793. {
  794. struct dstr show = {0};
  795. struct dstr hide = {0};
  796. struct dstr show_desc = {0};
  797. struct dstr hide_desc = {0};
  798. dstr_copy(&show, "libobs.show_scene_item.%1");
  799. dstr_replace(&show, "%1", name);
  800. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  801. dstr_replace(&hide, "%1", name);
  802. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  803. dstr_replace(&show_desc, "%1", name);
  804. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  805. dstr_replace(&hide_desc, "%1", name);
  806. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  807. show.array, show_desc.array,
  808. hide.array, hide_desc.array,
  809. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  810. item, item);
  811. dstr_free(&show);
  812. dstr_free(&hide);
  813. dstr_free(&show_desc);
  814. dstr_free(&hide_desc);
  815. }
  816. static inline bool source_has_audio(obs_source_t *source)
  817. {
  818. return (source->info.output_flags &
  819. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  820. }
  821. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  822. {
  823. struct obs_scene_item *last;
  824. struct obs_scene_item *item;
  825. struct calldata params;
  826. uint8_t stack[128];
  827. pthread_mutex_t mutex;
  828. struct item_action action = {
  829. .visible = true,
  830. .timestamp = os_gettime_ns()
  831. };
  832. if (!scene)
  833. return NULL;
  834. if (!source) {
  835. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  836. return NULL;
  837. }
  838. if (pthread_mutex_init(&mutex, NULL) != 0) {
  839. blog(LOG_WARNING, "Failed to create scene item mutex");
  840. return NULL;
  841. }
  842. if (!obs_source_add_active_child(scene->source, source)) {
  843. blog(LOG_WARNING, "Failed to add source to scene due to "
  844. "infinite source recursion");
  845. pthread_mutex_destroy(&mutex);
  846. return NULL;
  847. }
  848. item = bzalloc(sizeof(struct obs_scene_item));
  849. item->source = source;
  850. item->parent = scene;
  851. item->ref = 1;
  852. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  853. item->actions_mutex = mutex;
  854. item->user_visible = true;
  855. os_atomic_set_long(&item->active_refs, 1);
  856. vec2_set(&item->scale, 1.0f, 1.0f);
  857. matrix4_identity(&item->draw_transform);
  858. matrix4_identity(&item->box_transform);
  859. obs_source_addref(source);
  860. if (source_has_audio(source)) {
  861. item->visible = false;
  862. da_push_back(item->audio_actions, &action);
  863. } else {
  864. item->visible = true;
  865. }
  866. full_lock(scene);
  867. last = scene->first_item;
  868. if (!last) {
  869. scene->first_item = item;
  870. } else {
  871. while (last->next)
  872. last = last->next;
  873. last->next = item;
  874. item->prev = last;
  875. }
  876. full_unlock(scene);
  877. if (!scene->source->context.private)
  878. init_hotkeys(scene, item, obs_source_get_name(source));
  879. calldata_init_fixed(&params, stack, sizeof(stack));
  880. calldata_set_ptr(&params, "scene", scene);
  881. calldata_set_ptr(&params, "item", item);
  882. signal_handler_signal(scene->source->context.signals, "item_add",
  883. &params);
  884. return item;
  885. }
  886. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  887. {
  888. if (item) {
  889. obs_hotkey_pair_unregister(item->toggle_visibility);
  890. pthread_mutex_destroy(&item->actions_mutex);
  891. if (item->source)
  892. obs_source_release(item->source);
  893. da_free(item->audio_actions);
  894. bfree(item);
  895. }
  896. }
  897. void obs_sceneitem_addref(obs_sceneitem_t *item)
  898. {
  899. if (item)
  900. os_atomic_inc_long(&item->ref);
  901. }
  902. void obs_sceneitem_release(obs_sceneitem_t *item)
  903. {
  904. if (!item)
  905. return;
  906. if (os_atomic_dec_long(&item->ref) == 0)
  907. obs_sceneitem_destroy(item);
  908. }
  909. void obs_sceneitem_remove(obs_sceneitem_t *item)
  910. {
  911. obs_scene_t *scene;
  912. if (!item)
  913. return;
  914. scene = item->parent;
  915. if (scene)
  916. full_lock(scene);
  917. if (item->removed) {
  918. if (scene)
  919. full_unlock(scene);
  920. return;
  921. }
  922. item->removed = true;
  923. assert(scene != NULL);
  924. assert(scene->source != NULL);
  925. set_visibility(item, false);
  926. signal_item_remove(item);
  927. detach_sceneitem(item);
  928. full_unlock(scene);
  929. obs_sceneitem_release(item);
  930. }
  931. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  932. {
  933. return item ? item->parent : NULL;
  934. }
  935. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  936. {
  937. return item ? item->source : NULL;
  938. }
  939. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  940. {
  941. struct calldata params;
  942. uint8_t stack[128];
  943. const char *command = select ? "item_select" : "item_deselect";
  944. if (!item || item->selected == select)
  945. return;
  946. item->selected = select;
  947. calldata_init_fixed(&params, stack, sizeof(stack));
  948. calldata_set_ptr(&params, "scene", item->parent);
  949. calldata_set_ptr(&params, "item", item);
  950. signal_handler_signal(item->parent->source->context.signals,
  951. command, &params);
  952. }
  953. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  954. {
  955. return item ? item->selected : false;
  956. }
  957. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  958. {
  959. if (item) {
  960. vec2_copy(&item->pos, pos);
  961. update_item_transform(item);
  962. }
  963. }
  964. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  965. {
  966. if (item) {
  967. item->rot = rot;
  968. update_item_transform(item);
  969. }
  970. }
  971. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  972. {
  973. if (item) {
  974. vec2_copy(&item->scale, scale);
  975. update_item_transform(item);
  976. }
  977. }
  978. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  979. {
  980. if (item) {
  981. item->align = alignment;
  982. update_item_transform(item);
  983. }
  984. }
  985. static inline void signal_reorder(struct obs_scene_item *item)
  986. {
  987. const char *command = NULL;
  988. struct calldata params;
  989. uint8_t stack[128];
  990. command = "reorder";
  991. calldata_init_fixed(&params, stack, sizeof(stack));
  992. calldata_set_ptr(&params, "scene", item->parent);
  993. signal_handler_signal(item->parent->source->context.signals,
  994. command, &params);
  995. }
  996. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  997. enum obs_order_movement movement)
  998. {
  999. if (!item) return;
  1000. struct obs_scene_item *next, *prev;
  1001. struct obs_scene *scene = item->parent;
  1002. obs_scene_addref(scene);
  1003. full_lock(scene);
  1004. next = item->next;
  1005. prev = item->prev;
  1006. detach_sceneitem(item);
  1007. if (movement == OBS_ORDER_MOVE_DOWN) {
  1008. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1009. } else if (movement == OBS_ORDER_MOVE_UP) {
  1010. attach_sceneitem(scene, item, next ? next : prev);
  1011. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1012. struct obs_scene_item *last = next;
  1013. if (!last) {
  1014. last = prev;
  1015. } else {
  1016. while (last->next)
  1017. last = last->next;
  1018. }
  1019. attach_sceneitem(scene, item, last);
  1020. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1021. attach_sceneitem(scene, item, NULL);
  1022. }
  1023. signal_reorder(item);
  1024. full_unlock(scene);
  1025. obs_scene_release(scene);
  1026. }
  1027. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  1028. int position)
  1029. {
  1030. if (!item) return;
  1031. struct obs_scene *scene = item->parent;
  1032. struct obs_scene_item *next;
  1033. obs_scene_addref(scene);
  1034. full_lock(scene);
  1035. detach_sceneitem(item);
  1036. next = scene->first_item;
  1037. if (position == 0) {
  1038. attach_sceneitem(scene, item, NULL);
  1039. } else {
  1040. for (int i = position; i > 1; --i) {
  1041. if (next->next == NULL)
  1042. break;
  1043. next = next->next;
  1044. }
  1045. attach_sceneitem(scene, item, next);
  1046. }
  1047. signal_reorder(item);
  1048. full_unlock(scene);
  1049. obs_scene_release(scene);
  1050. }
  1051. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1052. enum obs_bounds_type type)
  1053. {
  1054. if (item) {
  1055. item->bounds_type = type;
  1056. update_item_transform(item);
  1057. }
  1058. }
  1059. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1060. uint32_t alignment)
  1061. {
  1062. if (item) {
  1063. item->bounds_align = alignment;
  1064. update_item_transform(item);
  1065. }
  1066. }
  1067. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1068. {
  1069. if (item) {
  1070. item->bounds = *bounds;
  1071. update_item_transform(item);
  1072. }
  1073. }
  1074. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1075. {
  1076. if (item)
  1077. vec2_copy(pos, &item->pos);
  1078. }
  1079. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1080. {
  1081. return item ? item->rot : 0.0f;
  1082. }
  1083. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1084. {
  1085. if (item)
  1086. vec2_copy(scale, &item->scale);
  1087. }
  1088. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1089. {
  1090. return item ? item->align : 0;
  1091. }
  1092. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1093. {
  1094. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1095. }
  1096. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1097. {
  1098. return item ? item->bounds_align : 0;
  1099. }
  1100. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1101. {
  1102. if (item)
  1103. *bounds = item->bounds;
  1104. }
  1105. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1106. struct obs_transform_info *info)
  1107. {
  1108. if (item && info) {
  1109. info->pos = item->pos;
  1110. info->rot = item->rot;
  1111. info->scale = item->scale;
  1112. info->alignment = item->align;
  1113. info->bounds_type = item->bounds_type;
  1114. info->bounds_alignment = item->bounds_align;
  1115. info->bounds = item->bounds;
  1116. }
  1117. }
  1118. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1119. const struct obs_transform_info *info)
  1120. {
  1121. if (item && info) {
  1122. item->pos = info->pos;
  1123. item->rot = info->rot;
  1124. item->scale = info->scale;
  1125. item->align = info->alignment;
  1126. item->bounds_type = info->bounds_type;
  1127. item->bounds_align = info->bounds_alignment;
  1128. item->bounds = info->bounds;
  1129. update_item_transform(item);
  1130. }
  1131. }
  1132. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1133. struct matrix4 *transform)
  1134. {
  1135. if (item)
  1136. matrix4_copy(transform, &item->draw_transform);
  1137. }
  1138. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1139. struct matrix4 *transform)
  1140. {
  1141. if (item)
  1142. matrix4_copy(transform, &item->box_transform);
  1143. }
  1144. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1145. {
  1146. return item ? item->user_visible : false;
  1147. }
  1148. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1149. {
  1150. struct calldata cd;
  1151. uint8_t stack[256];
  1152. struct item_action action = {
  1153. .visible = visible,
  1154. .timestamp = os_gettime_ns()
  1155. };
  1156. if (!item)
  1157. return false;
  1158. if (item->user_visible == visible)
  1159. return false;
  1160. if (!item->parent)
  1161. return false;
  1162. if (visible) {
  1163. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1164. if (!obs_source_add_active_child(item->parent->source,
  1165. item->source)) {
  1166. os_atomic_dec_long(&item->active_refs);
  1167. return false;
  1168. }
  1169. }
  1170. }
  1171. item->user_visible = visible;
  1172. calldata_init_fixed(&cd, stack, sizeof(stack));
  1173. calldata_set_ptr(&cd, "scene", item->parent);
  1174. calldata_set_ptr(&cd, "item", item);
  1175. calldata_set_bool(&cd, "visible", visible);
  1176. signal_handler_signal(item->parent->source->context.signals,
  1177. "item_visible", &cd);
  1178. if (source_has_audio(item->source)) {
  1179. pthread_mutex_lock(&item->actions_mutex);
  1180. da_push_back(item->audio_actions, &action);
  1181. pthread_mutex_unlock(&item->actions_mutex);
  1182. } else {
  1183. set_visibility(item, visible);
  1184. }
  1185. return true;
  1186. }
  1187. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1188. size_t size, bool *order_matches)
  1189. {
  1190. obs_sceneitem_t *item = scene->first_item;
  1191. size_t count = 0;
  1192. while (item) {
  1193. bool found = false;
  1194. for (size_t i = 0; i < size; i++) {
  1195. if (items[i] != item)
  1196. continue;
  1197. if (count != i)
  1198. *order_matches = false;
  1199. found = true;
  1200. break;
  1201. }
  1202. if (!found)
  1203. return false;
  1204. item = item->next;
  1205. count += 1;
  1206. }
  1207. return count == size;
  1208. }
  1209. bool obs_scene_reorder_items(obs_scene_t *scene,
  1210. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1211. {
  1212. if (!scene || !item_order_size)
  1213. return false;
  1214. obs_scene_addref(scene);
  1215. full_lock(scene);
  1216. bool order_matches = true;
  1217. if (!sceneitems_match(scene, item_order, item_order_size,
  1218. &order_matches) || order_matches) {
  1219. full_unlock(scene);
  1220. obs_scene_release(scene);
  1221. return false;
  1222. }
  1223. scene->first_item = item_order[0];
  1224. obs_sceneitem_t *prev = NULL;
  1225. for (size_t i = 0; i < item_order_size; i++) {
  1226. item_order[i]->prev = prev;
  1227. item_order[i]->next = NULL;
  1228. if (prev)
  1229. prev->next = item_order[i];
  1230. prev = item_order[i];
  1231. }
  1232. signal_reorder(scene->first_item);
  1233. full_unlock(scene);
  1234. obs_scene_release(scene);
  1235. return true;
  1236. }
  1237. void obs_scene_atomic_update(obs_scene_t *scene,
  1238. obs_scene_atomic_update_func func, void *data)
  1239. {
  1240. if (!scene)
  1241. return;
  1242. obs_scene_addref(scene);
  1243. full_lock(scene);
  1244. func(data, scene);
  1245. full_unlock(scene);
  1246. obs_scene_release(scene);
  1247. }