obs-scene.c 49 KB

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