obs-scene.c 48 KB

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