obs-scene.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451
  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_SCENE,
  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 = obs_source_create("scene", name, NULL,
  566. NULL);
  567. return source->context.data;
  568. }
  569. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name)
  570. {
  571. struct obs_scene *new_scene = obs_scene_create(name);
  572. struct obs_scene_item *item = scene->first_item;
  573. full_lock(scene);
  574. while (item) {
  575. struct obs_source *source = item->source;
  576. if (source) {
  577. struct obs_scene_item *new_item =
  578. obs_scene_add(new_scene, source);
  579. if (!new_item) {
  580. item = item->next;
  581. continue;
  582. }
  583. if (!item->user_visible)
  584. set_visibility(new_item, false);
  585. new_item->selected = item->selected;
  586. new_item->pos = item->pos;
  587. new_item->scale = item->scale;
  588. new_item->align = item->align;
  589. new_item->last_width = item->last_width;
  590. new_item->last_height = item->last_height;
  591. new_item->box_transform = item->box_transform;
  592. new_item->draw_transform = item->draw_transform;
  593. new_item->bounds_type = item->bounds_type;
  594. new_item->bounds_align = item->bounds_align;
  595. new_item->bounds = item->bounds;
  596. }
  597. item = item->next;
  598. }
  599. full_unlock(scene);
  600. return new_scene;
  601. }
  602. void obs_scene_addref(obs_scene_t *scene)
  603. {
  604. if (scene)
  605. obs_source_addref(scene->source);
  606. }
  607. void obs_scene_release(obs_scene_t *scene)
  608. {
  609. if (scene)
  610. obs_source_release(scene->source);
  611. }
  612. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  613. {
  614. return scene ? scene->source : NULL;
  615. }
  616. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  617. {
  618. if (!source || source->info.id != scene_info.id)
  619. return NULL;
  620. return source->context.data;
  621. }
  622. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  623. {
  624. struct obs_scene_item *item;
  625. if (!scene)
  626. return NULL;
  627. full_lock(scene);
  628. item = scene->first_item;
  629. while (item) {
  630. if (strcmp(item->source->context.name, name) == 0)
  631. break;
  632. item = item->next;
  633. }
  634. full_unlock(scene);
  635. return item;
  636. }
  637. void obs_scene_enum_items(obs_scene_t *scene,
  638. bool (*callback)(obs_scene_t*, obs_sceneitem_t*, void*),
  639. void *param)
  640. {
  641. struct obs_scene_item *item;
  642. if (!scene || !callback)
  643. return;
  644. full_lock(scene);
  645. item = scene->first_item;
  646. while (item) {
  647. struct obs_scene_item *next = item->next;
  648. obs_sceneitem_addref(item);
  649. if (!callback(scene, item, param)) {
  650. obs_sceneitem_release(item);
  651. break;
  652. }
  653. obs_sceneitem_release(item);
  654. item = next;
  655. }
  656. full_unlock(scene);
  657. }
  658. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  659. {
  660. long owners = si->ref;
  661. while (owners > 0) {
  662. if (os_atomic_compare_swap_long(&si->ref, owners, owners + 1))
  663. return si;
  664. owners = si->ref;
  665. }
  666. return NULL;
  667. }
  668. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  669. obs_hotkey_t *hotkey, bool pressed)
  670. {
  671. UNUSED_PARAMETER(id);
  672. UNUSED_PARAMETER(hotkey);
  673. obs_sceneitem_t *si = sceneitem_get_ref(data);
  674. if (pressed && si && !si->user_visible) {
  675. obs_sceneitem_set_visible(si, true);
  676. obs_sceneitem_release(si);
  677. return true;
  678. }
  679. obs_sceneitem_release(si);
  680. return false;
  681. }
  682. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  683. obs_hotkey_t *hotkey, bool pressed)
  684. {
  685. UNUSED_PARAMETER(id);
  686. UNUSED_PARAMETER(hotkey);
  687. obs_sceneitem_t *si = sceneitem_get_ref(data);
  688. if (pressed && si && si->user_visible) {
  689. obs_sceneitem_set_visible(si, false);
  690. obs_sceneitem_release(si);
  691. return true;
  692. }
  693. obs_sceneitem_release(si);
  694. return false;
  695. }
  696. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  697. const char *name)
  698. {
  699. struct dstr show = {0};
  700. struct dstr hide = {0};
  701. struct dstr show_desc = {0};
  702. struct dstr hide_desc = {0};
  703. dstr_copy(&show, "libobs.show_scene_item.%1");
  704. dstr_replace(&show, "%1", name);
  705. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  706. dstr_replace(&hide, "%1", name);
  707. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  708. dstr_replace(&show_desc, "%1", name);
  709. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  710. dstr_replace(&hide_desc, "%1", name);
  711. item->toggle_visibility = obs_hotkey_pair_register_source(scene->source,
  712. show.array, show_desc.array,
  713. hide.array, hide_desc.array,
  714. hotkey_show_sceneitem, hotkey_hide_sceneitem,
  715. item, item);
  716. dstr_free(&show);
  717. dstr_free(&hide);
  718. dstr_free(&show_desc);
  719. dstr_free(&hide_desc);
  720. }
  721. static inline bool source_has_audio(obs_source_t *source)
  722. {
  723. return (source->info.output_flags &
  724. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  725. }
  726. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  727. {
  728. struct obs_scene_item *last;
  729. struct obs_scene_item *item;
  730. struct calldata params = {0};
  731. pthread_mutex_t mutex;
  732. struct item_action action = {
  733. .visible = true,
  734. .timestamp = os_gettime_ns()
  735. };
  736. if (!scene)
  737. return NULL;
  738. if (!source) {
  739. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  740. return NULL;
  741. }
  742. if (pthread_mutex_init(&mutex, NULL) != 0) {
  743. blog(LOG_WARNING, "Failed to create scene item mutex");
  744. return NULL;
  745. }
  746. if (!obs_source_add_active_child(scene->source, source)) {
  747. blog(LOG_WARNING, "Failed to add source to scene due to "
  748. "infinite source recursion");
  749. pthread_mutex_destroy(&mutex);
  750. return NULL;
  751. }
  752. item = bzalloc(sizeof(struct obs_scene_item));
  753. item->source = source;
  754. item->parent = scene;
  755. item->ref = 1;
  756. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  757. item->actions_mutex = mutex;
  758. item->user_visible = true;
  759. os_atomic_set_long(&item->active_refs, 1);
  760. vec2_set(&item->scale, 1.0f, 1.0f);
  761. matrix4_identity(&item->draw_transform);
  762. matrix4_identity(&item->box_transform);
  763. obs_source_addref(source);
  764. if (source_has_audio(source)) {
  765. item->visible = false;
  766. da_push_back(item->audio_actions, &action);
  767. } else {
  768. item->visible = true;
  769. }
  770. full_lock(scene);
  771. last = scene->first_item;
  772. if (!last) {
  773. scene->first_item = item;
  774. } else {
  775. while (last->next)
  776. last = last->next;
  777. last->next = item;
  778. item->prev = last;
  779. }
  780. full_unlock(scene);
  781. init_hotkeys(scene, item, obs_source_get_name(source));
  782. calldata_set_ptr(&params, "scene", scene);
  783. calldata_set_ptr(&params, "item", item);
  784. signal_handler_signal(scene->source->context.signals, "item_add",
  785. &params);
  786. calldata_free(&params);
  787. return item;
  788. }
  789. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  790. {
  791. if (item) {
  792. obs_hotkey_pair_unregister(item->toggle_visibility);
  793. pthread_mutex_destroy(&item->actions_mutex);
  794. if (item->source)
  795. obs_source_release(item->source);
  796. da_free(item->audio_actions);
  797. bfree(item);
  798. }
  799. }
  800. void obs_sceneitem_addref(obs_sceneitem_t *item)
  801. {
  802. if (item)
  803. os_atomic_inc_long(&item->ref);
  804. }
  805. void obs_sceneitem_release(obs_sceneitem_t *item)
  806. {
  807. if (!item)
  808. return;
  809. if (os_atomic_dec_long(&item->ref) == 0)
  810. obs_sceneitem_destroy(item);
  811. }
  812. void obs_sceneitem_remove(obs_sceneitem_t *item)
  813. {
  814. obs_scene_t *scene;
  815. if (!item)
  816. return;
  817. scene = item->parent;
  818. if (scene)
  819. full_lock(scene);
  820. if (item->removed) {
  821. if (scene)
  822. full_unlock(scene);
  823. return;
  824. }
  825. item->removed = true;
  826. assert(scene != NULL);
  827. assert(scene->source != NULL);
  828. set_visibility(item, false);
  829. signal_item_remove(item);
  830. detach_sceneitem(item);
  831. full_unlock(scene);
  832. obs_sceneitem_release(item);
  833. }
  834. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  835. {
  836. return item ? item->parent : NULL;
  837. }
  838. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  839. {
  840. return item ? item->source : NULL;
  841. }
  842. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  843. {
  844. struct calldata params = {0};
  845. const char *command = select ? "item_select" : "item_deselect";
  846. if (!item || item->selected == select)
  847. return;
  848. item->selected = select;
  849. calldata_set_ptr(&params, "scene", item->parent);
  850. calldata_set_ptr(&params, "item", item);
  851. signal_handler_signal(item->parent->source->context.signals,
  852. command, &params);
  853. calldata_free(&params);
  854. }
  855. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  856. {
  857. return item ? item->selected : false;
  858. }
  859. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  860. {
  861. if (item) {
  862. vec2_copy(&item->pos, pos);
  863. update_item_transform(item);
  864. }
  865. }
  866. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  867. {
  868. if (item) {
  869. item->rot = rot;
  870. update_item_transform(item);
  871. }
  872. }
  873. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  874. {
  875. if (item) {
  876. vec2_copy(&item->scale, scale);
  877. update_item_transform(item);
  878. }
  879. }
  880. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  881. {
  882. if (item) {
  883. item->align = alignment;
  884. update_item_transform(item);
  885. }
  886. }
  887. static inline void signal_reorder(struct obs_scene_item *item)
  888. {
  889. const char *command = NULL;
  890. struct calldata params = {0};
  891. command = "reorder";
  892. calldata_set_ptr(&params, "scene", item->parent);
  893. signal_handler_signal(item->parent->source->context.signals,
  894. command, &params);
  895. calldata_free(&params);
  896. }
  897. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  898. enum obs_order_movement movement)
  899. {
  900. if (!item) return;
  901. struct obs_scene_item *next, *prev;
  902. struct obs_scene *scene = item->parent;
  903. obs_scene_addref(scene);
  904. full_lock(scene);
  905. next = item->next;
  906. prev = item->prev;
  907. detach_sceneitem(item);
  908. if (movement == OBS_ORDER_MOVE_DOWN) {
  909. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  910. } else if (movement == OBS_ORDER_MOVE_UP) {
  911. attach_sceneitem(scene, item, next ? next : prev);
  912. } else if (movement == OBS_ORDER_MOVE_TOP) {
  913. struct obs_scene_item *last = next;
  914. if (!last) {
  915. last = prev;
  916. } else {
  917. while (last->next)
  918. last = last->next;
  919. }
  920. attach_sceneitem(scene, item, last);
  921. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  922. attach_sceneitem(scene, item, NULL);
  923. }
  924. signal_reorder(item);
  925. full_unlock(scene);
  926. obs_scene_release(scene);
  927. }
  928. void obs_sceneitem_set_order_position(obs_sceneitem_t *item,
  929. int position)
  930. {
  931. if (!item) return;
  932. struct obs_scene *scene = item->parent;
  933. struct obs_scene_item *next;
  934. obs_scene_addref(scene);
  935. full_lock(scene);
  936. detach_sceneitem(item);
  937. next = scene->first_item;
  938. if (position == 0) {
  939. attach_sceneitem(scene, item, NULL);
  940. } else {
  941. for (int i = position; i > 1; --i) {
  942. if (next->next == NULL)
  943. break;
  944. next = next->next;
  945. }
  946. attach_sceneitem(scene, item, next);
  947. }
  948. signal_reorder(item);
  949. full_unlock(scene);
  950. obs_scene_release(scene);
  951. }
  952. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  953. enum obs_bounds_type type)
  954. {
  955. if (item) {
  956. item->bounds_type = type;
  957. update_item_transform(item);
  958. }
  959. }
  960. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  961. uint32_t alignment)
  962. {
  963. if (item) {
  964. item->bounds_align = alignment;
  965. update_item_transform(item);
  966. }
  967. }
  968. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  969. {
  970. if (item) {
  971. item->bounds = *bounds;
  972. update_item_transform(item);
  973. }
  974. }
  975. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  976. {
  977. if (item)
  978. vec2_copy(pos, &item->pos);
  979. }
  980. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  981. {
  982. return item ? item->rot : 0.0f;
  983. }
  984. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  985. {
  986. if (item)
  987. vec2_copy(scale, &item->scale);
  988. }
  989. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  990. {
  991. return item ? item->align : 0;
  992. }
  993. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  994. {
  995. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  996. }
  997. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  998. {
  999. return item ? item->bounds_align : 0;
  1000. }
  1001. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1002. {
  1003. if (item)
  1004. *bounds = item->bounds;
  1005. }
  1006. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1007. struct obs_transform_info *info)
  1008. {
  1009. if (item && info) {
  1010. info->pos = item->pos;
  1011. info->rot = item->rot;
  1012. info->scale = item->scale;
  1013. info->alignment = item->align;
  1014. info->bounds_type = item->bounds_type;
  1015. info->bounds_alignment = item->bounds_align;
  1016. info->bounds = item->bounds;
  1017. }
  1018. }
  1019. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1020. const struct obs_transform_info *info)
  1021. {
  1022. if (item && info) {
  1023. item->pos = info->pos;
  1024. item->rot = info->rot;
  1025. item->scale = info->scale;
  1026. item->align = info->alignment;
  1027. item->bounds_type = info->bounds_type;
  1028. item->bounds_align = info->bounds_alignment;
  1029. item->bounds = info->bounds;
  1030. update_item_transform(item);
  1031. }
  1032. }
  1033. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1034. struct matrix4 *transform)
  1035. {
  1036. if (item)
  1037. matrix4_copy(transform, &item->draw_transform);
  1038. }
  1039. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1040. struct matrix4 *transform)
  1041. {
  1042. if (item)
  1043. matrix4_copy(transform, &item->box_transform);
  1044. }
  1045. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1046. {
  1047. return item ? item->user_visible : false;
  1048. }
  1049. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  1050. {
  1051. struct calldata cd = {0};
  1052. struct item_action action = {
  1053. .visible = visible,
  1054. .timestamp = os_gettime_ns()
  1055. };
  1056. if (!item)
  1057. return false;
  1058. if (item->user_visible == visible)
  1059. return false;
  1060. if (!item->parent)
  1061. return false;
  1062. if (visible) {
  1063. if (os_atomic_inc_long(&item->active_refs) == 1) {
  1064. if (!obs_source_add_active_child(item->parent->source,
  1065. item->source)) {
  1066. os_atomic_dec_long(&item->active_refs);
  1067. return false;
  1068. }
  1069. }
  1070. }
  1071. item->user_visible = visible;
  1072. calldata_set_ptr(&cd, "scene", item->parent);
  1073. calldata_set_ptr(&cd, "item", item);
  1074. calldata_set_bool(&cd, "visible", visible);
  1075. signal_handler_signal(item->parent->source->context.signals,
  1076. "item_visible", &cd);
  1077. calldata_free(&cd);
  1078. if (source_has_audio(item->source)) {
  1079. pthread_mutex_lock(&item->actions_mutex);
  1080. da_push_back(item->audio_actions, &action);
  1081. pthread_mutex_unlock(&item->actions_mutex);
  1082. } else {
  1083. set_visibility(item, visible);
  1084. }
  1085. return true;
  1086. }
  1087. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t * const *items,
  1088. size_t size, bool *order_matches)
  1089. {
  1090. obs_sceneitem_t *item = scene->first_item;
  1091. size_t count = 0;
  1092. while (item) {
  1093. bool found = false;
  1094. for (size_t i = 0; i < size; i++) {
  1095. if (items[i] != item)
  1096. continue;
  1097. if (count != i)
  1098. *order_matches = false;
  1099. found = true;
  1100. break;
  1101. }
  1102. if (!found)
  1103. return false;
  1104. item = item->next;
  1105. count += 1;
  1106. }
  1107. return count == size;
  1108. }
  1109. bool obs_scene_reorder_items(obs_scene_t *scene,
  1110. obs_sceneitem_t * const *item_order, size_t item_order_size)
  1111. {
  1112. if (!scene || !item_order_size)
  1113. return false;
  1114. obs_scene_addref(scene);
  1115. full_lock(scene);
  1116. bool order_matches = true;
  1117. if (!sceneitems_match(scene, item_order, item_order_size,
  1118. &order_matches) || order_matches) {
  1119. full_unlock(scene);
  1120. obs_scene_release(scene);
  1121. return false;
  1122. }
  1123. scene->first_item = item_order[0];
  1124. obs_sceneitem_t *prev = NULL;
  1125. for (size_t i = 0; i < item_order_size; i++) {
  1126. item_order[i]->prev = prev;
  1127. item_order[i]->next = NULL;
  1128. if (prev)
  1129. prev->next = item_order[i];
  1130. prev = item_order[i];
  1131. }
  1132. signal_reorder(scene->first_item);
  1133. full_unlock(scene);
  1134. obs_scene_release(scene);
  1135. return true;
  1136. }
  1137. void obs_scene_atomic_update(obs_scene_t *scene,
  1138. obs_scene_atomic_update_func func, void *data)
  1139. {
  1140. if (!scene)
  1141. return;
  1142. obs_scene_addref(scene);
  1143. full_lock(scene);
  1144. func(data, scene);
  1145. full_unlock(scene);
  1146. obs_scene_release(scene);
  1147. }