obs-scene.c 33 KB

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