obs-scene.c 86 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511
  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 "util/util_uint64.h"
  17. #include "graphics/math-defs.h"
  18. #include "obs-scene.h"
  19. #include "obs-internal.h"
  20. const struct obs_source_info group_info;
  21. static void resize_group(obs_sceneitem_t *group);
  22. static void resize_scene(obs_scene_t *scene);
  23. static void signal_parent(obs_scene_t *parent, const char *name,
  24. calldata_t *params);
  25. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  26. struct vec2 *scale, float *rot);
  27. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop);
  28. static inline bool item_texture_enabled(const struct obs_scene_item *item);
  29. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  30. const char *name);
  31. /* NOTE: For proper mutex lock order (preventing mutual cross-locks), never
  32. * lock the graphics mutex inside either of the scene mutexes.
  33. *
  34. * Another thing that must be done to prevent that cross-lock (and improve
  35. * performance), is to not create/release/update sources within the scene
  36. * mutexes.
  37. *
  38. * It's okay to lock the graphics mutex before locking either of the scene
  39. * mutexes, but not after.
  40. */
  41. static const char *obs_scene_signals[] = {
  42. "void item_add(ptr scene, ptr item)",
  43. "void item_remove(ptr scene, ptr item)",
  44. "void reorder(ptr scene)",
  45. "void refresh(ptr scene)",
  46. "void item_visible(ptr scene, ptr item, bool visible)",
  47. "void item_select(ptr scene, ptr item)",
  48. "void item_deselect(ptr scene, ptr item)",
  49. "void item_transform(ptr scene, ptr item)",
  50. "void item_locked(ptr scene, ptr item, bool locked)",
  51. NULL,
  52. };
  53. static inline void signal_item_remove(struct obs_scene_item *item)
  54. {
  55. struct calldata params;
  56. uint8_t stack[128];
  57. calldata_init_fixed(&params, stack, sizeof(stack));
  58. calldata_set_ptr(&params, "item", item);
  59. signal_parent(item->parent, "item_remove", &params);
  60. }
  61. static const char *scene_getname(void *unused)
  62. {
  63. UNUSED_PARAMETER(unused);
  64. return "Scene";
  65. }
  66. static const char *group_getname(void *unused)
  67. {
  68. UNUSED_PARAMETER(unused);
  69. return "Group";
  70. }
  71. static void *scene_create(obs_data_t *settings, struct obs_source *source)
  72. {
  73. pthread_mutexattr_t attr;
  74. struct obs_scene *scene = bzalloc(sizeof(struct obs_scene));
  75. scene->source = source;
  76. if (strcmp(source->info.id, group_info.id) == 0) {
  77. scene->is_group = true;
  78. scene->custom_size = true;
  79. scene->cx = 0;
  80. scene->cy = 0;
  81. }
  82. signal_handler_add_array(obs_source_get_signal_handler(source),
  83. obs_scene_signals);
  84. if (pthread_mutexattr_init(&attr) != 0)
  85. goto fail;
  86. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  87. goto fail;
  88. if (pthread_mutex_init(&scene->audio_mutex, &attr) != 0) {
  89. blog(LOG_ERROR, "scene_create: Couldn't initialize audio "
  90. "mutex");
  91. goto fail;
  92. }
  93. if (pthread_mutex_init(&scene->video_mutex, &attr) != 0) {
  94. blog(LOG_ERROR, "scene_create: Couldn't initialize video "
  95. "mutex");
  96. goto fail;
  97. }
  98. UNUSED_PARAMETER(settings);
  99. return scene;
  100. fail:
  101. pthread_mutexattr_destroy(&attr);
  102. bfree(scene);
  103. return NULL;
  104. }
  105. #define audio_lock(scene) pthread_mutex_lock(&scene->audio_mutex)
  106. #define video_lock(scene) pthread_mutex_lock(&scene->video_mutex)
  107. #define audio_unlock(scene) pthread_mutex_unlock(&scene->audio_mutex)
  108. #define video_unlock(scene) pthread_mutex_unlock(&scene->video_mutex)
  109. static inline void full_lock(struct obs_scene *scene)
  110. {
  111. video_lock(scene);
  112. audio_lock(scene);
  113. }
  114. static inline void full_unlock(struct obs_scene *scene)
  115. {
  116. audio_unlock(scene);
  117. video_unlock(scene);
  118. }
  119. static void set_visibility(struct obs_scene_item *item, bool vis);
  120. static inline void detach_sceneitem(struct obs_scene_item *item);
  121. static inline void remove_without_release(struct obs_scene_item *item)
  122. {
  123. item->removed = true;
  124. set_visibility(item, false);
  125. signal_item_remove(item);
  126. detach_sceneitem(item);
  127. }
  128. static void remove_all_items(struct obs_scene *scene)
  129. {
  130. struct obs_scene_item *item;
  131. DARRAY(struct obs_scene_item *) items;
  132. da_init(items);
  133. full_lock(scene);
  134. item = scene->first_item;
  135. while (item) {
  136. struct obs_scene_item *del_item = item;
  137. item = item->next;
  138. remove_without_release(del_item);
  139. da_push_back(items, &del_item);
  140. }
  141. full_unlock(scene);
  142. for (size_t i = 0; i < items.num; i++)
  143. obs_sceneitem_release(items.array[i]);
  144. da_free(items);
  145. }
  146. static void scene_destroy(void *data)
  147. {
  148. struct obs_scene *scene = data;
  149. remove_all_items(scene);
  150. pthread_mutex_destroy(&scene->video_mutex);
  151. pthread_mutex_destroy(&scene->audio_mutex);
  152. bfree(scene);
  153. }
  154. static inline bool transition_active(obs_source_t *transition)
  155. {
  156. return transition && (transition->transitioning_audio ||
  157. transition->transitioning_video);
  158. }
  159. static void scene_enum_sources(void *data, obs_source_enum_proc_t enum_callback,
  160. void *param, bool active)
  161. {
  162. struct obs_scene *scene = data;
  163. struct obs_scene_item *item;
  164. struct obs_scene_item *next;
  165. full_lock(scene);
  166. item = scene->first_item;
  167. while (item) {
  168. next = item->next;
  169. obs_sceneitem_addref(item);
  170. if (active) {
  171. if (item->visible &&
  172. transition_active(item->show_transition))
  173. enum_callback(scene->source,
  174. item->show_transition, param);
  175. else if (!item->visible &&
  176. transition_active(item->hide_transition))
  177. enum_callback(scene->source,
  178. item->hide_transition, param);
  179. else if (os_atomic_load_long(&item->active_refs) > 0)
  180. enum_callback(scene->source, item->source,
  181. param);
  182. } else {
  183. if (item->show_transition)
  184. enum_callback(scene->source,
  185. item->show_transition, param);
  186. if (item->hide_transition)
  187. enum_callback(scene->source,
  188. item->hide_transition, param);
  189. enum_callback(scene->source, item->source, param);
  190. }
  191. obs_sceneitem_release(item);
  192. item = next;
  193. }
  194. full_unlock(scene);
  195. }
  196. static void scene_enum_active_sources(void *data,
  197. obs_source_enum_proc_t enum_callback,
  198. void *param)
  199. {
  200. scene_enum_sources(data, enum_callback, param, true);
  201. }
  202. static void scene_enum_all_sources(void *data,
  203. obs_source_enum_proc_t enum_callback,
  204. void *param)
  205. {
  206. scene_enum_sources(data, enum_callback, param, false);
  207. }
  208. static inline void detach_sceneitem(struct obs_scene_item *item)
  209. {
  210. if (item->prev)
  211. item->prev->next = item->next;
  212. else
  213. item->parent->first_item = item->next;
  214. if (item->next)
  215. item->next->prev = item->prev;
  216. item->parent = NULL;
  217. }
  218. static inline void attach_sceneitem(struct obs_scene *parent,
  219. struct obs_scene_item *item,
  220. struct obs_scene_item *prev)
  221. {
  222. item->prev = prev;
  223. item->parent = parent;
  224. if (prev) {
  225. item->next = prev->next;
  226. if (prev->next)
  227. prev->next->prev = item;
  228. prev->next = item;
  229. } else {
  230. item->next = parent->first_item;
  231. if (parent->first_item)
  232. parent->first_item->prev = item;
  233. parent->first_item = item;
  234. }
  235. }
  236. void add_alignment(struct vec2 *v, uint32_t align, int cx, int cy)
  237. {
  238. if (align & OBS_ALIGN_RIGHT)
  239. v->x += (float)cx;
  240. else if ((align & OBS_ALIGN_LEFT) == 0)
  241. v->x += (float)(cx / 2);
  242. if (align & OBS_ALIGN_BOTTOM)
  243. v->y += (float)cy;
  244. else if ((align & OBS_ALIGN_TOP) == 0)
  245. v->y += (float)(cy / 2);
  246. }
  247. static void calculate_bounds_data(struct obs_scene_item *item,
  248. struct vec2 *origin, struct vec2 *scale,
  249. uint32_t *cx, uint32_t *cy)
  250. {
  251. float width = (float)(*cx) * fabsf(scale->x);
  252. float height = (float)(*cy) * fabsf(scale->y);
  253. float item_aspect = width / height;
  254. float bounds_aspect = item->bounds.x / item->bounds.y;
  255. uint32_t bounds_type = item->bounds_type;
  256. float width_diff, height_diff;
  257. if (item->bounds_type == OBS_BOUNDS_MAX_ONLY)
  258. if (width > item->bounds.x || height > item->bounds.y)
  259. bounds_type = OBS_BOUNDS_SCALE_INNER;
  260. if (bounds_type == OBS_BOUNDS_SCALE_INNER ||
  261. bounds_type == OBS_BOUNDS_SCALE_OUTER) {
  262. bool use_width = (bounds_aspect < item_aspect);
  263. float mul;
  264. if (item->bounds_type == OBS_BOUNDS_SCALE_OUTER)
  265. use_width = !use_width;
  266. mul = use_width ? item->bounds.x / width
  267. : item->bounds.y / height;
  268. vec2_mulf(scale, scale, mul);
  269. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_WIDTH) {
  270. vec2_mulf(scale, scale, item->bounds.x / width);
  271. } else if (bounds_type == OBS_BOUNDS_SCALE_TO_HEIGHT) {
  272. vec2_mulf(scale, scale, item->bounds.y / height);
  273. } else if (bounds_type == OBS_BOUNDS_STRETCH) {
  274. scale->x = item->bounds.x / (float)(*cx);
  275. scale->y = item->bounds.y / (float)(*cy);
  276. }
  277. width = (float)(*cx) * scale->x;
  278. height = (float)(*cy) * scale->y;
  279. width_diff = item->bounds.x - width;
  280. height_diff = item->bounds.y - height;
  281. *cx = (uint32_t)item->bounds.x;
  282. *cy = (uint32_t)item->bounds.y;
  283. add_alignment(origin, item->bounds_align, (int)-width_diff,
  284. (int)-height_diff);
  285. }
  286. static inline uint32_t calc_cx(const struct obs_scene_item *item,
  287. uint32_t width)
  288. {
  289. uint32_t crop_cx = item->crop.left + item->crop.right;
  290. return (crop_cx > width) ? 2 : (width - crop_cx);
  291. }
  292. static inline uint32_t calc_cy(const struct obs_scene_item *item,
  293. uint32_t height)
  294. {
  295. uint32_t crop_cy = item->crop.top + item->crop.bottom;
  296. return (crop_cy > height) ? 2 : (height - crop_cy);
  297. }
  298. static void update_item_transform(struct obs_scene_item *item, bool update_tex)
  299. {
  300. uint32_t width;
  301. uint32_t height;
  302. uint32_t cx;
  303. uint32_t cy;
  304. struct vec2 base_origin;
  305. struct vec2 origin;
  306. struct vec2 scale;
  307. struct calldata params;
  308. uint8_t stack[128];
  309. if (os_atomic_load_long(&item->defer_update) > 0)
  310. return;
  311. width = obs_source_get_width(item->source);
  312. height = obs_source_get_height(item->source);
  313. cx = calc_cx(item, width);
  314. cy = calc_cy(item, height);
  315. scale = item->scale;
  316. item->last_width = width;
  317. item->last_height = height;
  318. width = cx;
  319. height = cy;
  320. vec2_zero(&base_origin);
  321. vec2_zero(&origin);
  322. /* ----------------------- */
  323. if (item->bounds_type != OBS_BOUNDS_NONE) {
  324. calculate_bounds_data(item, &origin, &scale, &cx, &cy);
  325. } else {
  326. cx = (uint32_t)((float)cx * scale.x);
  327. cy = (uint32_t)((float)cy * scale.y);
  328. }
  329. add_alignment(&origin, item->align, (int)cx, (int)cy);
  330. matrix4_identity(&item->draw_transform);
  331. matrix4_scale3f(&item->draw_transform, &item->draw_transform, scale.x,
  332. scale.y, 1.0f);
  333. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  334. -origin.x, -origin.y, 0.0f);
  335. matrix4_rotate_aa4f(&item->draw_transform, &item->draw_transform, 0.0f,
  336. 0.0f, 1.0f, RAD(item->rot));
  337. matrix4_translate3f(&item->draw_transform, &item->draw_transform,
  338. item->pos.x, item->pos.y, 0.0f);
  339. item->output_scale = scale;
  340. /* ----------------------- */
  341. if (item->bounds_type != OBS_BOUNDS_NONE) {
  342. vec2_copy(&scale, &item->bounds);
  343. } else {
  344. scale.x = (float)width * item->scale.x;
  345. scale.y = (float)height * item->scale.y;
  346. }
  347. item->box_scale = scale;
  348. add_alignment(&base_origin, item->align, (int)scale.x, (int)scale.y);
  349. matrix4_identity(&item->box_transform);
  350. matrix4_scale3f(&item->box_transform, &item->box_transform, scale.x,
  351. scale.y, 1.0f);
  352. matrix4_translate3f(&item->box_transform, &item->box_transform,
  353. -base_origin.x, -base_origin.y, 0.0f);
  354. matrix4_rotate_aa4f(&item->box_transform, &item->box_transform, 0.0f,
  355. 0.0f, 1.0f, RAD(item->rot));
  356. matrix4_translate3f(&item->box_transform, &item->box_transform,
  357. item->pos.x, item->pos.y, 0.0f);
  358. /* ----------------------- */
  359. calldata_init_fixed(&params, stack, sizeof(stack));
  360. calldata_set_ptr(&params, "item", item);
  361. signal_parent(item->parent, "item_transform", &params);
  362. if (!update_tex)
  363. return;
  364. if (item->item_render && !item_texture_enabled(item)) {
  365. obs_enter_graphics();
  366. gs_texrender_destroy(item->item_render);
  367. item->item_render = NULL;
  368. obs_leave_graphics();
  369. } else if (!item->item_render && item_texture_enabled(item)) {
  370. obs_enter_graphics();
  371. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  372. obs_leave_graphics();
  373. }
  374. os_atomic_set_bool(&item->update_transform, false);
  375. }
  376. static inline bool source_size_changed(struct obs_scene_item *item)
  377. {
  378. uint32_t width = obs_source_get_width(item->source);
  379. uint32_t height = obs_source_get_height(item->source);
  380. return item->last_width != width || item->last_height != height;
  381. }
  382. static inline bool crop_enabled(const struct obs_sceneitem_crop *crop)
  383. {
  384. return crop->left || crop->right || crop->top || crop->bottom;
  385. }
  386. static inline bool scale_filter_enabled(const struct obs_scene_item *item)
  387. {
  388. return item->scale_filter != OBS_SCALE_DISABLE;
  389. }
  390. static inline bool item_is_scene(const struct obs_scene_item *item)
  391. {
  392. return item->source && item->source->info.type == OBS_SOURCE_TYPE_SCENE;
  393. }
  394. static inline bool item_texture_enabled(const struct obs_scene_item *item)
  395. {
  396. return crop_enabled(&item->crop) || scale_filter_enabled(item) ||
  397. (item_is_scene(item) && !item->is_group);
  398. }
  399. static void render_item_texture(struct obs_scene_item *item)
  400. {
  401. gs_texture_t *tex = gs_texrender_get_texture(item->item_render);
  402. if (!tex) {
  403. return;
  404. }
  405. GS_DEBUG_MARKER_BEGIN(GS_DEBUG_COLOR_ITEM_TEXTURE,
  406. "render_item_texture");
  407. gs_effect_t *effect = obs->video.default_effect;
  408. enum obs_scale_type type = item->scale_filter;
  409. uint32_t cx = gs_texture_get_width(tex);
  410. uint32_t cy = gs_texture_get_height(tex);
  411. const char *tech = "Draw";
  412. if (type != OBS_SCALE_DISABLE) {
  413. if (type == OBS_SCALE_POINT) {
  414. gs_eparam_t *image =
  415. gs_effect_get_param_by_name(effect, "image");
  416. gs_effect_set_next_sampler(image,
  417. obs->video.point_sampler);
  418. } else if (!close_float(item->output_scale.x, 1.0f, EPSILON) ||
  419. !close_float(item->output_scale.y, 1.0f, EPSILON)) {
  420. gs_eparam_t *scale_param;
  421. gs_eparam_t *scale_i_param;
  422. if (item->output_scale.x < 0.5f ||
  423. item->output_scale.y < 0.5f) {
  424. effect = obs->video.bilinear_lowres_effect;
  425. } else if (type == OBS_SCALE_BICUBIC) {
  426. effect = obs->video.bicubic_effect;
  427. } else if (type == OBS_SCALE_LANCZOS) {
  428. effect = obs->video.lanczos_effect;
  429. } else if (type == OBS_SCALE_AREA) {
  430. effect = obs->video.area_effect;
  431. if ((item->output_scale.x >= 1.0f) &&
  432. (item->output_scale.y >= 1.0f))
  433. tech = "DrawUpscale";
  434. }
  435. scale_param = gs_effect_get_param_by_name(
  436. effect, "base_dimension");
  437. if (scale_param) {
  438. struct vec2 base_res = {(float)cx, (float)cy};
  439. gs_effect_set_vec2(scale_param, &base_res);
  440. }
  441. scale_i_param = gs_effect_get_param_by_name(
  442. effect, "base_dimension_i");
  443. if (scale_i_param) {
  444. struct vec2 base_res_i = {1.0f / (float)cx,
  445. 1.0f / (float)cy};
  446. gs_effect_set_vec2(scale_i_param, &base_res_i);
  447. }
  448. }
  449. }
  450. gs_blend_state_push();
  451. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  452. while (gs_effect_loop(effect, tech))
  453. obs_source_draw(tex, 0, 0, 0, 0, 0);
  454. gs_blend_state_pop();
  455. GS_DEBUG_MARKER_END();
  456. }
  457. static inline void render_item(struct obs_scene_item *item)
  458. {
  459. GS_DEBUG_MARKER_BEGIN_FORMAT(GS_DEBUG_COLOR_ITEM, "Item: %s",
  460. obs_source_get_name(item->source));
  461. if (item->item_render) {
  462. uint32_t width = obs_source_get_width(item->source);
  463. uint32_t height = obs_source_get_height(item->source);
  464. if (!width || !height) {
  465. goto cleanup;
  466. }
  467. uint32_t cx = calc_cx(item, width);
  468. uint32_t cy = calc_cy(item, height);
  469. if (cx && cy && gs_texrender_begin(item->item_render, cx, cy)) {
  470. float cx_scale = (float)width / (float)cx;
  471. float cy_scale = (float)height / (float)cy;
  472. struct vec4 clear_color;
  473. vec4_zero(&clear_color);
  474. gs_clear(GS_CLEAR_COLOR, &clear_color, 0.0f, 0);
  475. gs_ortho(0.0f, (float)width, 0.0f, (float)height,
  476. -100.0f, 100.0f);
  477. gs_matrix_scale3f(cx_scale, cy_scale, 1.0f);
  478. gs_matrix_translate3f(-(float)item->crop.left,
  479. -(float)item->crop.top, 0.0f);
  480. if (item->user_visible &&
  481. transition_active(item->show_transition)) {
  482. const int cx =
  483. obs_source_get_width(item->source);
  484. const int cy =
  485. obs_source_get_height(item->source);
  486. obs_transition_set_size(item->show_transition,
  487. cx, cy);
  488. obs_source_video_render(item->show_transition);
  489. } else if (!item->user_visible &&
  490. transition_active(item->hide_transition)) {
  491. const int cx =
  492. obs_source_get_width(item->source);
  493. const int cy =
  494. obs_source_get_height(item->source);
  495. obs_transition_set_size(item->hide_transition,
  496. cx, cy);
  497. obs_source_video_render(item->hide_transition);
  498. } else {
  499. obs_source_video_render(item->source);
  500. }
  501. gs_texrender_end(item->item_render);
  502. }
  503. }
  504. const bool previous = gs_set_linear_srgb(true);
  505. gs_matrix_push();
  506. gs_matrix_mul(&item->draw_transform);
  507. if (item->item_render) {
  508. render_item_texture(item);
  509. } else if (item->user_visible &&
  510. transition_active(item->show_transition)) {
  511. const int cx = obs_source_get_width(item->source);
  512. const int cy = obs_source_get_height(item->source);
  513. obs_transition_set_size(item->show_transition, cx, cy);
  514. obs_source_video_render(item->show_transition);
  515. } else if (!item->user_visible &&
  516. transition_active(item->hide_transition)) {
  517. const int cx = obs_source_get_width(item->source);
  518. const int cy = obs_source_get_height(item->source);
  519. obs_transition_set_size(item->hide_transition, cx, cy);
  520. obs_source_video_render(item->hide_transition);
  521. } else {
  522. obs_source_video_render(item->source);
  523. }
  524. gs_matrix_pop();
  525. gs_set_linear_srgb(previous);
  526. cleanup:
  527. GS_DEBUG_MARKER_END();
  528. }
  529. static void scene_video_tick(void *data, float seconds)
  530. {
  531. struct obs_scene *scene = data;
  532. struct obs_scene_item *item;
  533. video_lock(scene);
  534. item = scene->first_item;
  535. while (item) {
  536. if (item->item_render)
  537. gs_texrender_reset(item->item_render);
  538. item = item->next;
  539. }
  540. video_unlock(scene);
  541. UNUSED_PARAMETER(seconds);
  542. }
  543. /* assumes video lock */
  544. static void
  545. update_transforms_and_prune_sources(obs_scene_t *scene,
  546. struct darray *remove_items,
  547. obs_sceneitem_t *group_sceneitem)
  548. {
  549. struct obs_scene_item *item = scene->first_item;
  550. bool rebuild_group =
  551. group_sceneitem &&
  552. os_atomic_load_bool(&group_sceneitem->update_group_resize);
  553. while (item) {
  554. if (obs_source_removed(item->source)) {
  555. struct obs_scene_item *del_item = item;
  556. item = item->next;
  557. remove_without_release(del_item);
  558. darray_push_back(sizeof(struct obs_scene_item *),
  559. remove_items, &del_item);
  560. rebuild_group = true;
  561. continue;
  562. }
  563. if (item->is_group) {
  564. obs_scene_t *group_scene = item->source->context.data;
  565. video_lock(group_scene);
  566. update_transforms_and_prune_sources(group_scene,
  567. remove_items, item);
  568. video_unlock(group_scene);
  569. }
  570. if (os_atomic_load_bool(&item->update_transform) ||
  571. source_size_changed(item)) {
  572. update_item_transform(item, true);
  573. rebuild_group = true;
  574. }
  575. item = item->next;
  576. }
  577. if (rebuild_group && group_sceneitem)
  578. resize_group(group_sceneitem);
  579. }
  580. static void scene_video_render(void *data, gs_effect_t *effect)
  581. {
  582. DARRAY(struct obs_scene_item *) remove_items;
  583. struct obs_scene *scene = data;
  584. struct obs_scene_item *item;
  585. da_init(remove_items);
  586. video_lock(scene);
  587. if (!scene->is_group) {
  588. update_transforms_and_prune_sources(scene, &remove_items.da,
  589. NULL);
  590. }
  591. gs_blend_state_push();
  592. gs_reset_blend_state();
  593. item = scene->first_item;
  594. while (item) {
  595. if (item->user_visible ||
  596. transition_active(item->hide_transition))
  597. render_item(item);
  598. item = item->next;
  599. }
  600. gs_blend_state_pop();
  601. video_unlock(scene);
  602. for (size_t i = 0; i < remove_items.num; i++)
  603. obs_sceneitem_release(remove_items.array[i]);
  604. da_free(remove_items);
  605. UNUSED_PARAMETER(effect);
  606. }
  607. static void set_visibility(struct obs_scene_item *item, bool vis)
  608. {
  609. pthread_mutex_lock(&item->actions_mutex);
  610. da_resize(item->audio_actions, 0);
  611. if (os_atomic_load_long(&item->active_refs) > 0) {
  612. if (!vis)
  613. obs_source_remove_active_child(item->parent->source,
  614. item->source);
  615. } else if (vis) {
  616. obs_source_add_active_child(item->parent->source, item->source);
  617. }
  618. os_atomic_set_long(&item->active_refs, vis ? 1 : 0);
  619. item->visible = vis;
  620. item->user_visible = vis;
  621. pthread_mutex_unlock(&item->actions_mutex);
  622. }
  623. static void scene_load(void *data, obs_data_t *settings);
  624. static void scene_load_item(struct obs_scene *scene, obs_data_t *item_data)
  625. {
  626. const char *name = obs_data_get_string(item_data, "name");
  627. obs_source_t *source;
  628. const char *scale_filter_str;
  629. struct obs_scene_item *item;
  630. bool visible;
  631. bool lock;
  632. if (obs_data_get_bool(item_data, "group_item_backup"))
  633. return;
  634. source = obs_get_source_by_name(name);
  635. if (!source) {
  636. blog(LOG_WARNING,
  637. "[scene_load_item] Source %s not "
  638. "found!",
  639. name);
  640. return;
  641. }
  642. item = obs_scene_add(scene, source);
  643. if (!item) {
  644. blog(LOG_WARNING,
  645. "[scene_load_item] Could not add source '%s' "
  646. "to scene '%s'!",
  647. name, obs_source_get_name(scene->source));
  648. obs_source_release(source);
  649. return;
  650. }
  651. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  652. obs_data_set_default_int(item_data, "align",
  653. OBS_ALIGN_TOP | OBS_ALIGN_LEFT);
  654. if (obs_data_has_user_value(item_data, "id"))
  655. item->id = obs_data_get_int(item_data, "id");
  656. item->rot = (float)obs_data_get_double(item_data, "rot");
  657. item->align = (uint32_t)obs_data_get_int(item_data, "align");
  658. visible = obs_data_get_bool(item_data, "visible");
  659. lock = obs_data_get_bool(item_data, "locked");
  660. obs_data_get_vec2(item_data, "pos", &item->pos);
  661. obs_data_get_vec2(item_data, "scale", &item->scale);
  662. obs_data_release(item->private_settings);
  663. item->private_settings =
  664. obs_data_get_obj(item_data, "private_settings");
  665. if (!item->private_settings)
  666. item->private_settings = obs_data_create();
  667. set_visibility(item, visible);
  668. obs_sceneitem_set_locked(item, lock);
  669. item->bounds_type = (enum obs_bounds_type)obs_data_get_int(
  670. item_data, "bounds_type");
  671. item->bounds_align =
  672. (uint32_t)obs_data_get_int(item_data, "bounds_align");
  673. obs_data_get_vec2(item_data, "bounds", &item->bounds);
  674. item->crop.left = (uint32_t)obs_data_get_int(item_data, "crop_left");
  675. item->crop.top = (uint32_t)obs_data_get_int(item_data, "crop_top");
  676. item->crop.right = (uint32_t)obs_data_get_int(item_data, "crop_right");
  677. item->crop.bottom =
  678. (uint32_t)obs_data_get_int(item_data, "crop_bottom");
  679. scale_filter_str = obs_data_get_string(item_data, "scale_filter");
  680. item->scale_filter = OBS_SCALE_DISABLE;
  681. if (scale_filter_str) {
  682. if (astrcmpi(scale_filter_str, "point") == 0)
  683. item->scale_filter = OBS_SCALE_POINT;
  684. else if (astrcmpi(scale_filter_str, "bilinear") == 0)
  685. item->scale_filter = OBS_SCALE_BILINEAR;
  686. else if (astrcmpi(scale_filter_str, "bicubic") == 0)
  687. item->scale_filter = OBS_SCALE_BICUBIC;
  688. else if (astrcmpi(scale_filter_str, "lanczos") == 0)
  689. item->scale_filter = OBS_SCALE_LANCZOS;
  690. else if (astrcmpi(scale_filter_str, "area") == 0)
  691. item->scale_filter = OBS_SCALE_AREA;
  692. }
  693. obs_data_t *show_data = obs_data_get_obj(item_data, "show_transition");
  694. if (show_data) {
  695. const char *id = obs_data_get_string(show_data, "id");
  696. if (id && strlen(id)) {
  697. const char *tn = obs_data_get_string(show_data, "name");
  698. obs_data_t *s =
  699. obs_data_get_obj(show_data, "transition");
  700. obs_source_t *t = obs_source_create_private(id, tn, s);
  701. obs_sceneitem_set_show_transition(item, t);
  702. obs_source_release(t);
  703. obs_data_release(s);
  704. }
  705. item->show_transition_duration =
  706. (uint32_t)obs_data_get_int(show_data, "duration");
  707. obs_data_release(show_data);
  708. }
  709. obs_data_t *hide_data = obs_data_get_obj(item_data, "hide_transition");
  710. if (hide_data) {
  711. const char *id = obs_data_get_string(hide_data, "id");
  712. if (id && strlen(id)) {
  713. const char *tn = obs_data_get_string(hide_data, "name");
  714. obs_data_t *s =
  715. obs_data_get_obj(hide_data, "transition");
  716. obs_source_t *t = obs_source_create_private(id, tn, s);
  717. obs_sceneitem_set_hide_transition(item, t);
  718. obs_source_release(t);
  719. obs_data_release(s);
  720. }
  721. item->hide_transition_duration =
  722. (uint32_t)obs_data_get_int(hide_data, "duration");
  723. obs_data_release(hide_data);
  724. }
  725. if (item->item_render && !item_texture_enabled(item)) {
  726. obs_enter_graphics();
  727. gs_texrender_destroy(item->item_render);
  728. item->item_render = NULL;
  729. obs_leave_graphics();
  730. } else if (!item->item_render && item_texture_enabled(item)) {
  731. obs_enter_graphics();
  732. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  733. obs_leave_graphics();
  734. }
  735. obs_source_release(source);
  736. update_item_transform(item, false);
  737. }
  738. static void scene_load(void *data, obs_data_t *settings)
  739. {
  740. struct obs_scene *scene = data;
  741. obs_data_array_t *items = obs_data_get_array(settings, "items");
  742. size_t count, i;
  743. remove_all_items(scene);
  744. if (!items)
  745. return;
  746. count = obs_data_array_count(items);
  747. for (i = 0; i < count; i++) {
  748. obs_data_t *item_data = obs_data_array_item(items, i);
  749. scene_load_item(scene, item_data);
  750. obs_data_release(item_data);
  751. }
  752. if (obs_data_has_user_value(settings, "id_counter"))
  753. scene->id_counter = obs_data_get_int(settings, "id_counter");
  754. if (obs_data_get_bool(settings, "custom_size")) {
  755. scene->cx = (uint32_t)obs_data_get_int(settings, "cx");
  756. scene->cy = (uint32_t)obs_data_get_int(settings, "cy");
  757. scene->custom_size = true;
  758. }
  759. obs_data_array_release(items);
  760. }
  761. static void scene_save(void *data, obs_data_t *settings);
  762. static void scene_save_item(obs_data_array_t *array,
  763. struct obs_scene_item *item,
  764. struct obs_scene_item *backup_group)
  765. {
  766. obs_data_t *item_data = obs_data_create();
  767. const char *name = obs_source_get_name(item->source);
  768. const char *scale_filter;
  769. struct vec2 pos = item->pos;
  770. struct vec2 scale = item->scale;
  771. float rot = item->rot;
  772. if (backup_group) {
  773. get_ungrouped_transform(backup_group, &pos, &scale, &rot);
  774. }
  775. obs_data_set_string(item_data, "name", name);
  776. obs_data_set_bool(item_data, "visible", item->user_visible);
  777. obs_data_set_bool(item_data, "locked", item->locked);
  778. obs_data_set_double(item_data, "rot", rot);
  779. obs_data_set_vec2(item_data, "pos", &pos);
  780. obs_data_set_vec2(item_data, "scale", &scale);
  781. obs_data_set_int(item_data, "align", (int)item->align);
  782. obs_data_set_int(item_data, "bounds_type", (int)item->bounds_type);
  783. obs_data_set_int(item_data, "bounds_align", (int)item->bounds_align);
  784. obs_data_set_vec2(item_data, "bounds", &item->bounds);
  785. obs_data_set_int(item_data, "crop_left", (int)item->crop.left);
  786. obs_data_set_int(item_data, "crop_top", (int)item->crop.top);
  787. obs_data_set_int(item_data, "crop_right", (int)item->crop.right);
  788. obs_data_set_int(item_data, "crop_bottom", (int)item->crop.bottom);
  789. obs_data_set_int(item_data, "id", item->id);
  790. obs_data_set_bool(item_data, "group_item_backup", !!backup_group);
  791. if (item->is_group) {
  792. obs_scene_t *group_scene = item->source->context.data;
  793. obs_sceneitem_t *group_item;
  794. /* save group items as part of main scene, but ignored.
  795. * causes an automatic ungroup if scene collection file
  796. * is loaded in previous versions. */
  797. full_lock(group_scene);
  798. group_item = group_scene->first_item;
  799. while (group_item) {
  800. scene_save_item(array, group_item, item);
  801. group_item = group_item->next;
  802. }
  803. full_unlock(group_scene);
  804. }
  805. if (item->scale_filter == OBS_SCALE_POINT)
  806. scale_filter = "point";
  807. else if (item->scale_filter == OBS_SCALE_BILINEAR)
  808. scale_filter = "bilinear";
  809. else if (item->scale_filter == OBS_SCALE_BICUBIC)
  810. scale_filter = "bicubic";
  811. else if (item->scale_filter == OBS_SCALE_LANCZOS)
  812. scale_filter = "lanczos";
  813. else if (item->scale_filter == OBS_SCALE_AREA)
  814. scale_filter = "area";
  815. else
  816. scale_filter = "disable";
  817. obs_data_set_string(item_data, "scale_filter", scale_filter);
  818. obs_data_t *show_data = obs_data_create();
  819. if (item->show_transition) {
  820. obs_data_set_string(
  821. show_data, "id",
  822. obs_source_get_unversioned_id(item->show_transition));
  823. obs_data_set_string(show_data, "versioned_id",
  824. obs_source_get_id(item->show_transition));
  825. obs_data_set_string(show_data, "name",
  826. obs_source_get_name(item->show_transition));
  827. obs_data_t *s = obs_source_get_settings(item->show_transition);
  828. obs_data_set_obj(show_data, "transition", s);
  829. obs_data_release(s);
  830. }
  831. obs_data_set_int(show_data, "duration", item->show_transition_duration);
  832. obs_data_set_obj(item_data, "show_transition", show_data);
  833. obs_data_release(show_data);
  834. obs_data_t *hide_data = obs_data_create();
  835. if (item->hide_transition) {
  836. obs_data_set_string(
  837. hide_data, "id",
  838. obs_source_get_unversioned_id(item->hide_transition));
  839. obs_data_set_string(hide_data, "versioned_id",
  840. obs_source_get_id(item->hide_transition));
  841. obs_data_set_string(hide_data, "name",
  842. obs_source_get_name(item->hide_transition));
  843. obs_data_t *s = obs_source_get_settings(item->hide_transition);
  844. obs_data_set_obj(hide_data, "transition", s);
  845. obs_data_release(s);
  846. }
  847. obs_data_set_int(hide_data, "duration", item->hide_transition_duration);
  848. obs_data_set_obj(item_data, "hide_transition", hide_data);
  849. obs_data_release(hide_data);
  850. obs_data_set_obj(item_data, "private_settings", item->private_settings);
  851. obs_data_array_push_back(array, item_data);
  852. obs_data_release(item_data);
  853. }
  854. static void scene_save(void *data, obs_data_t *settings)
  855. {
  856. struct obs_scene *scene = data;
  857. obs_data_array_t *array = obs_data_array_create();
  858. struct obs_scene_item *item;
  859. full_lock(scene);
  860. item = scene->first_item;
  861. while (item) {
  862. scene_save_item(array, item, NULL);
  863. item = item->next;
  864. }
  865. obs_data_set_int(settings, "id_counter", scene->id_counter);
  866. obs_data_set_bool(settings, "custom_size", scene->custom_size);
  867. if (scene->custom_size) {
  868. obs_data_set_int(settings, "cx", scene->cx);
  869. obs_data_set_int(settings, "cy", scene->cy);
  870. }
  871. full_unlock(scene);
  872. obs_data_set_array(settings, "items", array);
  873. obs_data_array_release(array);
  874. }
  875. static uint32_t scene_getwidth(void *data)
  876. {
  877. obs_scene_t *scene = data;
  878. return scene->custom_size ? scene->cx : obs->video.base_width;
  879. }
  880. static uint32_t scene_getheight(void *data)
  881. {
  882. obs_scene_t *scene = data;
  883. return scene->custom_size ? scene->cy : obs->video.base_height;
  884. }
  885. static void apply_scene_item_audio_actions(struct obs_scene_item *item,
  886. float *buf, uint64_t ts,
  887. size_t sample_rate)
  888. {
  889. bool cur_visible = item->visible;
  890. uint64_t frame_num = 0;
  891. size_t deref_count = 0;
  892. pthread_mutex_lock(&item->actions_mutex);
  893. for (size_t i = 0; i < item->audio_actions.num; i++) {
  894. struct item_action action = item->audio_actions.array[i];
  895. uint64_t timestamp = action.timestamp;
  896. uint64_t new_frame_num;
  897. if (timestamp < ts)
  898. timestamp = ts;
  899. new_frame_num = util_mul_div64(timestamp - ts, sample_rate,
  900. 1000000000ULL);
  901. if (ts && new_frame_num >= AUDIO_OUTPUT_FRAMES)
  902. break;
  903. da_erase(item->audio_actions, i--);
  904. item->visible = action.visible;
  905. if (!item->visible)
  906. deref_count++;
  907. if (buf && new_frame_num > frame_num) {
  908. for (; frame_num < new_frame_num; frame_num++)
  909. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  910. }
  911. cur_visible = item->visible;
  912. }
  913. if (buf) {
  914. for (; frame_num < AUDIO_OUTPUT_FRAMES; frame_num++)
  915. buf[frame_num] = cur_visible ? 1.0f : 0.0f;
  916. }
  917. pthread_mutex_unlock(&item->actions_mutex);
  918. while (deref_count--) {
  919. if (os_atomic_dec_long(&item->active_refs) == 0) {
  920. obs_source_remove_active_child(item->parent->source,
  921. item->source);
  922. }
  923. }
  924. }
  925. static bool apply_scene_item_volume(struct obs_scene_item *item, float *buf,
  926. uint64_t ts, size_t sample_rate)
  927. {
  928. bool actions_pending;
  929. struct item_action action;
  930. pthread_mutex_lock(&item->actions_mutex);
  931. actions_pending = item->audio_actions.num > 0;
  932. if (actions_pending)
  933. action = item->audio_actions.array[0];
  934. pthread_mutex_unlock(&item->actions_mutex);
  935. if (actions_pending) {
  936. uint64_t duration = util_mul_div64(AUDIO_OUTPUT_FRAMES,
  937. 1000000000ULL, sample_rate);
  938. if (!ts || action.timestamp < (ts + duration)) {
  939. apply_scene_item_audio_actions(item, buf, ts,
  940. sample_rate);
  941. return true;
  942. }
  943. }
  944. return false;
  945. }
  946. static void process_all_audio_actions(struct obs_scene_item *item,
  947. size_t sample_rate)
  948. {
  949. while (apply_scene_item_volume(item, NULL, 0, sample_rate))
  950. ;
  951. }
  952. static void mix_audio_with_buf(float *p_out, float *p_in, float *buf_in,
  953. size_t pos, size_t count)
  954. {
  955. register float *out = p_out;
  956. register float *buf = buf_in + pos;
  957. register float *in = p_in + pos;
  958. register float *end = in + count;
  959. while (in < end)
  960. *(out++) += *(in++) * *(buf++);
  961. }
  962. static inline void mix_audio(float *p_out, float *p_in, size_t pos,
  963. size_t count)
  964. {
  965. register float *out = p_out;
  966. register float *in = p_in + pos;
  967. register float *end = in + count;
  968. while (in < end)
  969. *(out++) += *(in++);
  970. }
  971. static bool scene_audio_render(void *data, uint64_t *ts_out,
  972. struct obs_source_audio_mix *audio_output,
  973. uint32_t mixers, size_t channels,
  974. size_t sample_rate)
  975. {
  976. uint64_t timestamp = 0;
  977. float buf[AUDIO_OUTPUT_FRAMES];
  978. struct obs_source_audio_mix child_audio;
  979. struct obs_scene *scene = data;
  980. struct obs_scene_item *item;
  981. audio_lock(scene);
  982. item = scene->first_item;
  983. while (item) {
  984. struct obs_source *source;
  985. if (item->visible && transition_active(item->show_transition))
  986. source = item->show_transition;
  987. else if (!item->visible &&
  988. transition_active(item->hide_transition))
  989. source = item->hide_transition;
  990. else
  991. source = item->source;
  992. if (!obs_source_audio_pending(source) &&
  993. (item->visible ||
  994. transition_active(item->hide_transition))) {
  995. uint64_t source_ts =
  996. obs_source_get_audio_timestamp(source);
  997. if (source_ts && (!timestamp || source_ts < timestamp))
  998. timestamp = source_ts;
  999. }
  1000. item = item->next;
  1001. }
  1002. if (!timestamp) {
  1003. /* just process all pending audio actions if no audio playing,
  1004. * otherwise audio actions will just never be processed */
  1005. item = scene->first_item;
  1006. while (item) {
  1007. process_all_audio_actions(item, sample_rate);
  1008. item = item->next;
  1009. }
  1010. audio_unlock(scene);
  1011. return false;
  1012. }
  1013. item = scene->first_item;
  1014. while (item) {
  1015. uint64_t source_ts;
  1016. size_t pos, count;
  1017. bool apply_buf;
  1018. struct obs_source *source;
  1019. if (item->visible && transition_active(item->show_transition))
  1020. source = item->show_transition;
  1021. else if (!item->visible &&
  1022. transition_active(item->hide_transition))
  1023. source = item->hide_transition;
  1024. else
  1025. source = item->source;
  1026. apply_buf = apply_scene_item_volume(item, buf, timestamp,
  1027. sample_rate);
  1028. if (obs_source_audio_pending(source)) {
  1029. item = item->next;
  1030. continue;
  1031. }
  1032. source_ts = obs_source_get_audio_timestamp(source);
  1033. if (!source_ts) {
  1034. item = item->next;
  1035. continue;
  1036. }
  1037. pos = (size_t)ns_to_audio_frames(sample_rate,
  1038. source_ts - timestamp);
  1039. count = AUDIO_OUTPUT_FRAMES - pos;
  1040. if (!apply_buf && !item->visible &&
  1041. !transition_active(item->hide_transition)) {
  1042. item = item->next;
  1043. continue;
  1044. }
  1045. obs_source_get_audio_mix(source, &child_audio);
  1046. for (size_t mix = 0; mix < MAX_AUDIO_MIXES; mix++) {
  1047. if ((mixers & (1 << mix)) == 0)
  1048. continue;
  1049. for (size_t ch = 0; ch < channels; ch++) {
  1050. float *out = audio_output->output[mix].data[ch];
  1051. float *in = child_audio.output[mix].data[ch];
  1052. if (apply_buf)
  1053. mix_audio_with_buf(out, in, buf, pos,
  1054. count);
  1055. else
  1056. mix_audio(out, in, pos, count);
  1057. }
  1058. }
  1059. item = item->next;
  1060. }
  1061. *ts_out = timestamp;
  1062. audio_unlock(scene);
  1063. return true;
  1064. }
  1065. const struct obs_source_info scene_info = {
  1066. .id = "scene",
  1067. .type = OBS_SOURCE_TYPE_SCENE,
  1068. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1069. OBS_SOURCE_COMPOSITE | OBS_SOURCE_DO_NOT_DUPLICATE,
  1070. .get_name = scene_getname,
  1071. .create = scene_create,
  1072. .destroy = scene_destroy,
  1073. .video_tick = scene_video_tick,
  1074. .video_render = scene_video_render,
  1075. .audio_render = scene_audio_render,
  1076. .get_width = scene_getwidth,
  1077. .get_height = scene_getheight,
  1078. .load = scene_load,
  1079. .save = scene_save,
  1080. .enum_active_sources = scene_enum_active_sources,
  1081. .enum_all_sources = scene_enum_all_sources};
  1082. const struct obs_source_info group_info = {
  1083. .id = "group",
  1084. .type = OBS_SOURCE_TYPE_SCENE,
  1085. .output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW |
  1086. OBS_SOURCE_COMPOSITE,
  1087. .get_name = group_getname,
  1088. .create = scene_create,
  1089. .destroy = scene_destroy,
  1090. .video_tick = scene_video_tick,
  1091. .video_render = scene_video_render,
  1092. .audio_render = scene_audio_render,
  1093. .get_width = scene_getwidth,
  1094. .get_height = scene_getheight,
  1095. .load = scene_load,
  1096. .save = scene_save,
  1097. .enum_active_sources = scene_enum_active_sources,
  1098. .enum_all_sources = scene_enum_all_sources};
  1099. static inline obs_scene_t *create_id(const char *id, const char *name)
  1100. {
  1101. struct obs_source *source = obs_source_create(id, name, NULL, NULL);
  1102. return source->context.data;
  1103. }
  1104. static inline obs_scene_t *create_private_id(const char *id, const char *name)
  1105. {
  1106. struct obs_source *source = obs_source_create_private(id, name, NULL);
  1107. return source->context.data;
  1108. }
  1109. obs_scene_t *obs_scene_create(const char *name)
  1110. {
  1111. return create_id("scene", name);
  1112. }
  1113. obs_scene_t *obs_scene_create_private(const char *name)
  1114. {
  1115. return create_private_id("scene", name);
  1116. }
  1117. static obs_source_t *get_child_at_idx(obs_scene_t *scene, size_t idx)
  1118. {
  1119. struct obs_scene_item *item = scene->first_item;
  1120. while (item && idx--)
  1121. item = item->next;
  1122. return item ? item->source : NULL;
  1123. }
  1124. static inline obs_source_t *dup_child(struct darray *array, size_t idx,
  1125. obs_scene_t *new_scene, bool private)
  1126. {
  1127. DARRAY(struct obs_scene_item *) old_items;
  1128. obs_source_t *source;
  1129. old_items.da = *array;
  1130. source = old_items.array[idx]->source;
  1131. /* if the old item is referenced more than once in the old scene,
  1132. * make sure they're referenced similarly in the new scene to reduce
  1133. * load times */
  1134. for (size_t i = 0; i < idx; i++) {
  1135. struct obs_scene_item *item = old_items.array[i];
  1136. if (item->source == source) {
  1137. source = get_child_at_idx(new_scene, i);
  1138. obs_source_addref(source);
  1139. return source;
  1140. }
  1141. }
  1142. return obs_source_duplicate(
  1143. source, private ? obs_source_get_name(source) : NULL, private);
  1144. }
  1145. static inline obs_source_t *new_ref(obs_source_t *source)
  1146. {
  1147. obs_source_addref(source);
  1148. return source;
  1149. }
  1150. static inline void duplicate_item_data(struct obs_scene_item *dst,
  1151. struct obs_scene_item *src,
  1152. bool defer_texture_update,
  1153. bool duplicate_hotkeys,
  1154. bool duplicate_private_data)
  1155. {
  1156. struct obs_scene *dst_scene = dst->parent;
  1157. if (!src->user_visible)
  1158. set_visibility(dst, false);
  1159. dst->selected = src->selected;
  1160. dst->pos = src->pos;
  1161. dst->rot = src->rot;
  1162. dst->scale = src->scale;
  1163. dst->align = src->align;
  1164. dst->last_width = src->last_width;
  1165. dst->last_height = src->last_height;
  1166. dst->output_scale = src->output_scale;
  1167. dst->scale_filter = src->scale_filter;
  1168. dst->box_transform = src->box_transform;
  1169. dst->box_scale = src->box_scale;
  1170. dst->draw_transform = src->draw_transform;
  1171. dst->bounds_type = src->bounds_type;
  1172. dst->bounds_align = src->bounds_align;
  1173. dst->bounds = src->bounds;
  1174. if (src->show_transition) {
  1175. obs_source_t *transition = obs_source_duplicate(
  1176. src->show_transition,
  1177. obs_source_get_name(src->show_transition), true);
  1178. obs_sceneitem_set_show_transition(dst, transition);
  1179. obs_source_release(transition);
  1180. }
  1181. if (src->hide_transition) {
  1182. obs_source_t *transition = obs_source_duplicate(
  1183. src->hide_transition,
  1184. obs_source_get_name(src->hide_transition), true);
  1185. obs_sceneitem_set_hide_transition(dst, transition);
  1186. obs_source_release(transition);
  1187. }
  1188. dst->show_transition_duration = src->show_transition_duration;
  1189. dst->hide_transition_duration = src->hide_transition_duration;
  1190. if (duplicate_hotkeys && !dst_scene->source->context.private) {
  1191. obs_data_array_t *data0 = NULL;
  1192. obs_data_array_t *data1 = NULL;
  1193. obs_hotkey_pair_save(src->toggle_visibility, &data0, &data1);
  1194. obs_hotkey_pair_load(dst->toggle_visibility, data0, data1);
  1195. obs_data_array_release(data0);
  1196. obs_data_array_release(data1);
  1197. }
  1198. obs_sceneitem_set_crop(dst, &src->crop);
  1199. obs_sceneitem_set_locked(dst, src->locked);
  1200. if (defer_texture_update) {
  1201. os_atomic_set_bool(&dst->update_transform, true);
  1202. } else {
  1203. if (!dst->item_render && item_texture_enabled(dst)) {
  1204. obs_enter_graphics();
  1205. dst->item_render =
  1206. gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1207. obs_leave_graphics();
  1208. }
  1209. }
  1210. if (duplicate_private_data) {
  1211. obs_data_apply(dst->private_settings, src->private_settings);
  1212. }
  1213. }
  1214. obs_scene_t *obs_scene_duplicate(obs_scene_t *scene, const char *name,
  1215. enum obs_scene_duplicate_type type)
  1216. {
  1217. bool make_unique = ((int)type & (1 << 0)) != 0;
  1218. bool make_private = ((int)type & (1 << 1)) != 0;
  1219. DARRAY(struct obs_scene_item *) items;
  1220. struct obs_scene *new_scene;
  1221. struct obs_scene_item *item;
  1222. struct obs_source *source;
  1223. da_init(items);
  1224. if (!obs_ptr_valid(scene, "obs_scene_duplicate"))
  1225. return NULL;
  1226. /* --------------------------------- */
  1227. full_lock(scene);
  1228. item = scene->first_item;
  1229. while (item) {
  1230. da_push_back(items, &item);
  1231. obs_sceneitem_addref(item);
  1232. item = item->next;
  1233. }
  1234. full_unlock(scene);
  1235. /* --------------------------------- */
  1236. new_scene = make_private
  1237. ? create_private_id(scene->source->info.id, name)
  1238. : create_id(scene->source->info.id, name);
  1239. obs_source_copy_filters(new_scene->source, scene->source);
  1240. obs_data_apply(new_scene->source->private_settings,
  1241. scene->source->private_settings);
  1242. /* never duplicate sub-items for groups */
  1243. if (scene->is_group)
  1244. make_unique = false;
  1245. for (size_t i = 0; i < items.num; i++) {
  1246. item = items.array[i];
  1247. source = make_unique ? dup_child(&items.da, i, new_scene,
  1248. make_private)
  1249. : new_ref(item->source);
  1250. if (source) {
  1251. struct obs_scene_item *new_item =
  1252. obs_scene_add(new_scene, source);
  1253. if (!new_item) {
  1254. obs_source_release(source);
  1255. continue;
  1256. }
  1257. duplicate_item_data(new_item, item, false, false,
  1258. false);
  1259. obs_source_release(source);
  1260. }
  1261. }
  1262. for (size_t i = 0; i < items.num; i++)
  1263. obs_sceneitem_release(items.array[i]);
  1264. if (new_scene->is_group)
  1265. resize_scene(new_scene);
  1266. da_free(items);
  1267. return new_scene;
  1268. }
  1269. void obs_scene_addref(obs_scene_t *scene)
  1270. {
  1271. if (scene)
  1272. obs_source_addref(scene->source);
  1273. }
  1274. void obs_scene_release(obs_scene_t *scene)
  1275. {
  1276. if (scene)
  1277. obs_source_release(scene->source);
  1278. }
  1279. obs_source_t *obs_scene_get_source(const obs_scene_t *scene)
  1280. {
  1281. return scene ? scene->source : NULL;
  1282. }
  1283. obs_scene_t *obs_scene_from_source(const obs_source_t *source)
  1284. {
  1285. if (!source || strcmp(source->info.id, scene_info.id) != 0)
  1286. return NULL;
  1287. return source->context.data;
  1288. }
  1289. obs_scene_t *obs_group_from_source(const obs_source_t *source)
  1290. {
  1291. if (!source || strcmp(source->info.id, group_info.id) != 0)
  1292. return NULL;
  1293. return source->context.data;
  1294. }
  1295. obs_sceneitem_t *obs_scene_find_source(obs_scene_t *scene, const char *name)
  1296. {
  1297. struct obs_scene_item *item;
  1298. if (!scene)
  1299. return NULL;
  1300. full_lock(scene);
  1301. item = scene->first_item;
  1302. while (item) {
  1303. if (strcmp(item->source->context.name, name) == 0)
  1304. break;
  1305. item = item->next;
  1306. }
  1307. full_unlock(scene);
  1308. return item;
  1309. }
  1310. obs_sceneitem_t *obs_scene_find_source_recursive(obs_scene_t *scene,
  1311. const char *name)
  1312. {
  1313. struct obs_scene_item *item;
  1314. if (!scene)
  1315. return NULL;
  1316. full_lock(scene);
  1317. item = scene->first_item;
  1318. while (item) {
  1319. if (strcmp(item->source->context.name, name) == 0)
  1320. break;
  1321. if (item->is_group) {
  1322. obs_scene_t *group = item->source->context.data;
  1323. obs_sceneitem_t *child =
  1324. obs_scene_find_source(group, name);
  1325. if (child) {
  1326. item = child;
  1327. break;
  1328. }
  1329. }
  1330. item = item->next;
  1331. }
  1332. full_unlock(scene);
  1333. return item;
  1334. }
  1335. struct sceneitem_check {
  1336. obs_source_t *source_in;
  1337. obs_sceneitem_t *item_out;
  1338. };
  1339. bool check_sceneitem_exists(obs_scene_t *scene, obs_sceneitem_t *item,
  1340. void *vp_check)
  1341. {
  1342. UNUSED_PARAMETER(scene);
  1343. struct sceneitem_check *check = (struct sceneitem_check *)vp_check;
  1344. if (obs_sceneitem_get_source(item) == check->source_in) {
  1345. check->item_out = item;
  1346. obs_sceneitem_addref(item);
  1347. return false;
  1348. }
  1349. return true;
  1350. }
  1351. obs_sceneitem_t *obs_scene_sceneitem_from_source(obs_scene_t *scene,
  1352. obs_source_t *source)
  1353. {
  1354. struct sceneitem_check check = {source, NULL};
  1355. obs_scene_enum_items(scene, check_sceneitem_exists, (void *)&check);
  1356. return check.item_out;
  1357. }
  1358. obs_sceneitem_t *obs_scene_find_sceneitem_by_id(obs_scene_t *scene, int64_t id)
  1359. {
  1360. struct obs_scene_item *item;
  1361. if (!scene)
  1362. return NULL;
  1363. full_lock(scene);
  1364. item = scene->first_item;
  1365. while (item) {
  1366. if (item->id == id)
  1367. break;
  1368. item = item->next;
  1369. }
  1370. full_unlock(scene);
  1371. return item;
  1372. }
  1373. void obs_scene_enum_items(obs_scene_t *scene,
  1374. bool (*callback)(obs_scene_t *, obs_sceneitem_t *,
  1375. void *),
  1376. void *param)
  1377. {
  1378. struct obs_scene_item *item;
  1379. if (!scene || !callback)
  1380. return;
  1381. full_lock(scene);
  1382. item = scene->first_item;
  1383. while (item) {
  1384. struct obs_scene_item *next = item->next;
  1385. obs_sceneitem_addref(item);
  1386. if (!callback(scene, item, param)) {
  1387. obs_sceneitem_release(item);
  1388. break;
  1389. }
  1390. obs_sceneitem_release(item);
  1391. item = next;
  1392. }
  1393. full_unlock(scene);
  1394. }
  1395. static obs_sceneitem_t *sceneitem_get_ref(obs_sceneitem_t *si)
  1396. {
  1397. long owners = os_atomic_load_long(&si->ref);
  1398. while (owners > 0) {
  1399. if (os_atomic_compare_exchange_long(&si->ref, &owners,
  1400. owners + 1)) {
  1401. return si;
  1402. }
  1403. }
  1404. return NULL;
  1405. }
  1406. static bool hotkey_show_sceneitem(void *data, obs_hotkey_pair_id id,
  1407. obs_hotkey_t *hotkey, bool pressed)
  1408. {
  1409. UNUSED_PARAMETER(id);
  1410. UNUSED_PARAMETER(hotkey);
  1411. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1412. if (pressed && si && !si->user_visible) {
  1413. obs_sceneitem_set_visible(si, true);
  1414. obs_sceneitem_release(si);
  1415. return true;
  1416. }
  1417. obs_sceneitem_release(si);
  1418. return false;
  1419. }
  1420. static bool hotkey_hide_sceneitem(void *data, obs_hotkey_pair_id id,
  1421. obs_hotkey_t *hotkey, bool pressed)
  1422. {
  1423. UNUSED_PARAMETER(id);
  1424. UNUSED_PARAMETER(hotkey);
  1425. obs_sceneitem_t *si = sceneitem_get_ref(data);
  1426. if (pressed && si && si->user_visible) {
  1427. obs_sceneitem_set_visible(si, false);
  1428. obs_sceneitem_release(si);
  1429. return true;
  1430. }
  1431. obs_sceneitem_release(si);
  1432. return false;
  1433. }
  1434. static void init_hotkeys(obs_scene_t *scene, obs_sceneitem_t *item,
  1435. const char *name)
  1436. {
  1437. struct dstr show = {0};
  1438. struct dstr hide = {0};
  1439. struct dstr show_desc = {0};
  1440. struct dstr hide_desc = {0};
  1441. dstr_copy(&show, "libobs.show_scene_item.%1");
  1442. dstr_replace(&show, "%1", name);
  1443. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  1444. dstr_replace(&hide, "%1", name);
  1445. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1446. dstr_replace(&show_desc, "%1", name);
  1447. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1448. dstr_replace(&hide_desc, "%1", name);
  1449. item->toggle_visibility = obs_hotkey_pair_register_source(
  1450. scene->source, show.array, show_desc.array, hide.array,
  1451. hide_desc.array, hotkey_show_sceneitem, hotkey_hide_sceneitem,
  1452. item, item);
  1453. dstr_free(&show);
  1454. dstr_free(&hide);
  1455. dstr_free(&show_desc);
  1456. dstr_free(&hide_desc);
  1457. }
  1458. static void sceneitem_rename_hotkey(const obs_sceneitem_t *scene_item,
  1459. const char *new_name)
  1460. {
  1461. struct dstr show = {0};
  1462. struct dstr hide = {0};
  1463. struct dstr show_desc = {0};
  1464. struct dstr hide_desc = {0};
  1465. dstr_copy(&show, "libobs.show_scene_item.%1");
  1466. dstr_replace(&show, "%1", new_name);
  1467. dstr_copy(&hide, "libobs.hide_scene_item.%1");
  1468. dstr_replace(&hide, "%1", new_name);
  1469. obs_hotkey_pair_set_names(scene_item->toggle_visibility, show.array,
  1470. hide.array);
  1471. dstr_copy(&show_desc, obs->hotkeys.sceneitem_show);
  1472. dstr_replace(&show_desc, "%1", new_name);
  1473. dstr_copy(&hide_desc, obs->hotkeys.sceneitem_hide);
  1474. dstr_replace(&hide_desc, "%1", new_name);
  1475. obs_hotkey_pair_set_descriptions(scene_item->toggle_visibility,
  1476. show_desc.array, hide_desc.array);
  1477. dstr_free(&show);
  1478. dstr_free(&hide);
  1479. dstr_free(&show_desc);
  1480. dstr_free(&hide_desc);
  1481. }
  1482. static void sceneitem_renamed(void *param, calldata_t *data)
  1483. {
  1484. obs_sceneitem_t *scene_item = param;
  1485. const char *name = calldata_string(data, "new_name");
  1486. sceneitem_rename_hotkey(scene_item, name);
  1487. }
  1488. static inline bool source_has_audio(obs_source_t *source)
  1489. {
  1490. return (source->info.output_flags &
  1491. (OBS_SOURCE_AUDIO | OBS_SOURCE_COMPOSITE)) != 0;
  1492. }
  1493. static obs_sceneitem_t *obs_scene_add_internal(obs_scene_t *scene,
  1494. obs_source_t *source,
  1495. obs_sceneitem_t *insert_after)
  1496. {
  1497. struct obs_scene_item *last;
  1498. struct obs_scene_item *item;
  1499. pthread_mutex_t mutex;
  1500. struct item_action action = {.visible = true,
  1501. .timestamp = os_gettime_ns()};
  1502. if (!scene)
  1503. return NULL;
  1504. if (!source) {
  1505. blog(LOG_ERROR, "Tried to add a NULL source to a scene");
  1506. return NULL;
  1507. }
  1508. if (pthread_mutex_init(&mutex, NULL) != 0) {
  1509. blog(LOG_WARNING, "Failed to create scene item mutex");
  1510. return NULL;
  1511. }
  1512. if (!obs_source_add_active_child(scene->source, source)) {
  1513. blog(LOG_WARNING, "Failed to add source to scene due to "
  1514. "infinite source recursion");
  1515. pthread_mutex_destroy(&mutex);
  1516. return NULL;
  1517. }
  1518. item = bzalloc(sizeof(struct obs_scene_item));
  1519. item->source = source;
  1520. item->id = ++scene->id_counter;
  1521. item->parent = scene;
  1522. item->ref = 1;
  1523. item->align = OBS_ALIGN_TOP | OBS_ALIGN_LEFT;
  1524. item->actions_mutex = mutex;
  1525. item->user_visible = true;
  1526. item->locked = false;
  1527. item->is_group = strcmp(source->info.id, group_info.id) == 0;
  1528. item->private_settings = obs_data_create();
  1529. item->toggle_visibility = OBS_INVALID_HOTKEY_PAIR_ID;
  1530. os_atomic_set_long(&item->active_refs, 1);
  1531. vec2_set(&item->scale, 1.0f, 1.0f);
  1532. matrix4_identity(&item->draw_transform);
  1533. matrix4_identity(&item->box_transform);
  1534. obs_source_addref(source);
  1535. if (source_has_audio(source)) {
  1536. item->visible = false;
  1537. da_push_back(item->audio_actions, &action);
  1538. } else {
  1539. item->visible = true;
  1540. }
  1541. if (item_texture_enabled(item)) {
  1542. obs_enter_graphics();
  1543. item->item_render = gs_texrender_create(GS_RGBA, GS_ZS_NONE);
  1544. obs_leave_graphics();
  1545. }
  1546. full_lock(scene);
  1547. if (insert_after) {
  1548. obs_sceneitem_t *next = insert_after->next;
  1549. if (next)
  1550. next->prev = item;
  1551. item->next = insert_after->next;
  1552. item->prev = insert_after;
  1553. insert_after->next = item;
  1554. } else {
  1555. last = scene->first_item;
  1556. if (!last) {
  1557. scene->first_item = item;
  1558. } else {
  1559. while (last->next)
  1560. last = last->next;
  1561. last->next = item;
  1562. item->prev = last;
  1563. }
  1564. }
  1565. full_unlock(scene);
  1566. if (!scene->source->context.private)
  1567. init_hotkeys(scene, item, obs_source_get_name(source));
  1568. signal_handler_connect(obs_source_get_signal_handler(source), "rename",
  1569. sceneitem_renamed, item);
  1570. return item;
  1571. }
  1572. obs_sceneitem_t *obs_scene_add(obs_scene_t *scene, obs_source_t *source)
  1573. {
  1574. obs_sceneitem_t *item = obs_scene_add_internal(scene, source, NULL);
  1575. struct calldata params;
  1576. uint8_t stack[128];
  1577. if (!item)
  1578. return NULL;
  1579. calldata_init_fixed(&params, stack, sizeof(stack));
  1580. calldata_set_ptr(&params, "scene", scene);
  1581. calldata_set_ptr(&params, "item", item);
  1582. signal_handler_signal(scene->source->context.signals, "item_add",
  1583. &params);
  1584. return item;
  1585. }
  1586. static void obs_sceneitem_destroy(obs_sceneitem_t *item)
  1587. {
  1588. if (item) {
  1589. if (item->item_render) {
  1590. obs_enter_graphics();
  1591. gs_texrender_destroy(item->item_render);
  1592. obs_leave_graphics();
  1593. }
  1594. obs_data_release(item->private_settings);
  1595. obs_hotkey_pair_unregister(item->toggle_visibility);
  1596. pthread_mutex_destroy(&item->actions_mutex);
  1597. signal_handler_disconnect(
  1598. obs_source_get_signal_handler(item->source), "rename",
  1599. sceneitem_renamed, item);
  1600. if (item->show_transition)
  1601. obs_source_release(item->show_transition);
  1602. if (item->hide_transition)
  1603. obs_source_release(item->hide_transition);
  1604. if (item->source)
  1605. obs_source_release(item->source);
  1606. da_free(item->audio_actions);
  1607. bfree(item);
  1608. }
  1609. }
  1610. void obs_sceneitem_addref(obs_sceneitem_t *item)
  1611. {
  1612. if (item)
  1613. os_atomic_inc_long(&item->ref);
  1614. }
  1615. void obs_sceneitem_release(obs_sceneitem_t *item)
  1616. {
  1617. if (!item)
  1618. return;
  1619. if (os_atomic_dec_long(&item->ref) == 0)
  1620. obs_sceneitem_destroy(item);
  1621. }
  1622. void obs_sceneitem_remove(obs_sceneitem_t *item)
  1623. {
  1624. obs_scene_t *scene;
  1625. if (!item)
  1626. return;
  1627. scene = item->parent;
  1628. full_lock(scene);
  1629. if (item->removed) {
  1630. if (scene)
  1631. full_unlock(scene);
  1632. return;
  1633. }
  1634. item->removed = true;
  1635. assert(scene != NULL);
  1636. assert(scene->source != NULL);
  1637. set_visibility(item, false);
  1638. obs_sceneitem_set_show_transition(item, NULL);
  1639. obs_sceneitem_set_hide_transition(item, NULL);
  1640. signal_item_remove(item);
  1641. detach_sceneitem(item);
  1642. full_unlock(scene);
  1643. obs_sceneitem_release(item);
  1644. }
  1645. void obs_sceneitem_save(obs_sceneitem_t *item, obs_data_array_t *arr)
  1646. {
  1647. scene_save_item(arr, item, NULL);
  1648. }
  1649. void sceneitem_restore(obs_data_t *data, void *vp)
  1650. {
  1651. obs_scene_t *scene = (obs_scene_t *)vp;
  1652. scene_load_item(scene, data);
  1653. }
  1654. void obs_sceneitems_add(obs_scene_t *scene, obs_data_array_t *data)
  1655. {
  1656. obs_data_array_enum(data, sceneitem_restore, scene);
  1657. }
  1658. obs_scene_t *obs_sceneitem_get_scene(const obs_sceneitem_t *item)
  1659. {
  1660. return item ? item->parent : NULL;
  1661. }
  1662. obs_source_t *obs_sceneitem_get_source(const obs_sceneitem_t *item)
  1663. {
  1664. return item ? item->source : NULL;
  1665. }
  1666. static void signal_parent(obs_scene_t *parent, const char *command,
  1667. calldata_t *params)
  1668. {
  1669. calldata_set_ptr(params, "scene", parent);
  1670. signal_handler_signal(parent->source->context.signals, command, params);
  1671. }
  1672. struct passthrough {
  1673. obs_data_array_t *ids;
  1674. bool all_items;
  1675. };
  1676. bool save_transform_states(obs_scene_t *scene, obs_sceneitem_t *item,
  1677. void *vp_pass)
  1678. {
  1679. struct passthrough *pass = (struct passthrough *)vp_pass;
  1680. if (obs_sceneitem_selected(item) || pass->all_items) {
  1681. obs_data_t *temp = obs_data_create();
  1682. obs_data_array_t *item_ids = (obs_data_array_t *)pass->ids;
  1683. struct obs_transform_info info;
  1684. struct obs_sceneitem_crop crop;
  1685. obs_sceneitem_get_info(item, &info);
  1686. obs_sceneitem_get_crop(item, &crop);
  1687. struct vec2 pos = info.pos;
  1688. struct vec2 scale = info.scale;
  1689. float rot = info.rot;
  1690. uint32_t alignment = info.alignment;
  1691. uint32_t bounds_type = info.bounds_type;
  1692. uint32_t bounds_alignment = info.bounds_alignment;
  1693. struct vec2 bounds = info.bounds;
  1694. obs_data_set_int(temp, "id", obs_sceneitem_get_id(item));
  1695. obs_data_set_vec2(temp, "pos", &pos);
  1696. obs_data_set_vec2(temp, "scale", &scale);
  1697. obs_data_set_double(temp, "rot", rot);
  1698. obs_data_set_int(temp, "alignment", alignment);
  1699. obs_data_set_int(temp, "bounds_type", bounds_type);
  1700. obs_data_set_vec2(temp, "bounds", &bounds);
  1701. obs_data_set_int(temp, "bounds_alignment", bounds_alignment);
  1702. obs_data_set_int(temp, "top", crop.top);
  1703. obs_data_set_int(temp, "bottom", crop.bottom);
  1704. obs_data_set_int(temp, "left", crop.left);
  1705. obs_data_set_int(temp, "right", crop.right);
  1706. obs_data_array_push_back(item_ids, temp);
  1707. obs_data_release(temp);
  1708. }
  1709. UNUSED_PARAMETER(scene);
  1710. return true;
  1711. }
  1712. obs_data_t *obs_scene_save_transform_states(obs_scene_t *scene, bool all_items)
  1713. {
  1714. obs_data_t *wrapper = obs_data_create();
  1715. obs_data_array_t *item_ids = obs_data_array_create();
  1716. struct passthrough pass = {item_ids, all_items};
  1717. obs_scene_enum_items(scene, save_transform_states, (void *)&pass);
  1718. obs_data_set_array(wrapper, "item_ids", item_ids);
  1719. obs_data_set_string(wrapper, "scene_name",
  1720. obs_source_get_name(obs_scene_get_source(scene)));
  1721. obs_data_array_release(item_ids);
  1722. return wrapper;
  1723. }
  1724. void load_transform_states(obs_data_t *temp, void *vp_scene)
  1725. {
  1726. obs_scene_t *scene = (obs_scene_t *)vp_scene;
  1727. int64_t id = obs_data_get_int(temp, "id");
  1728. obs_sceneitem_t *item = obs_scene_find_sceneitem_by_id(scene, id);
  1729. struct obs_transform_info info;
  1730. struct obs_sceneitem_crop crop;
  1731. obs_data_get_vec2(temp, "pos", &info.pos);
  1732. obs_data_get_vec2(temp, "scale", &info.scale);
  1733. info.rot = (float)obs_data_get_double(temp, "rot");
  1734. info.alignment = (uint32_t)obs_data_get_int(temp, "alignment");
  1735. info.bounds_type =
  1736. (enum obs_bounds_type)obs_data_get_int(temp, "bounds_type");
  1737. info.bounds_alignment =
  1738. (uint32_t)obs_data_get_int(temp, "bounds_alignment");
  1739. obs_data_get_vec2(temp, "bounds", &info.bounds);
  1740. crop.top = (int)obs_data_get_int(temp, "top");
  1741. crop.bottom = (int)obs_data_get_int(temp, "bottom");
  1742. crop.left = (int)obs_data_get_int(temp, "left");
  1743. crop.right = (int)obs_data_get_int(temp, "right");
  1744. obs_sceneitem_defer_update_begin(item);
  1745. obs_sceneitem_set_info(item, &info);
  1746. obs_sceneitem_set_crop(item, &crop);
  1747. obs_sceneitem_defer_update_end(item);
  1748. }
  1749. void obs_scene_load_transform_states(const char *data)
  1750. {
  1751. obs_data_t *dat = obs_data_create_from_json(data);
  1752. obs_data_array_t *item_ids = obs_data_get_array(dat, "item_ids");
  1753. obs_source_t *source =
  1754. obs_get_source_by_name(obs_data_get_string(dat, "scene_name"));
  1755. obs_scene_t *scene = obs_scene_from_source(source);
  1756. obs_data_array_enum(item_ids, load_transform_states, (void *)scene);
  1757. obs_data_release(dat);
  1758. obs_data_array_release(item_ids);
  1759. obs_source_release(source);
  1760. }
  1761. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  1762. {
  1763. struct calldata params;
  1764. uint8_t stack[128];
  1765. const char *command = select ? "item_select" : "item_deselect";
  1766. if (!item || item->selected == select || !item->parent)
  1767. return;
  1768. item->selected = select;
  1769. calldata_init_fixed(&params, stack, sizeof(stack));
  1770. calldata_set_ptr(&params, "item", item);
  1771. signal_parent(item->parent, command, &params);
  1772. }
  1773. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  1774. {
  1775. return item ? item->selected : false;
  1776. }
  1777. #define do_update_transform(item) \
  1778. do { \
  1779. if (!item->parent || item->parent->is_group) \
  1780. os_atomic_set_bool(&item->update_transform, true); \
  1781. else \
  1782. update_item_transform(item, false); \
  1783. } while (false)
  1784. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  1785. {
  1786. if (item) {
  1787. vec2_copy(&item->pos, pos);
  1788. do_update_transform(item);
  1789. }
  1790. }
  1791. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  1792. {
  1793. if (item) {
  1794. item->rot = rot;
  1795. do_update_transform(item);
  1796. }
  1797. }
  1798. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  1799. {
  1800. if (item) {
  1801. vec2_copy(&item->scale, scale);
  1802. do_update_transform(item);
  1803. }
  1804. }
  1805. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  1806. {
  1807. if (item) {
  1808. item->align = alignment;
  1809. do_update_transform(item);
  1810. }
  1811. }
  1812. static inline void signal_reorder(struct obs_scene_item *item)
  1813. {
  1814. const char *command = NULL;
  1815. struct calldata params;
  1816. uint8_t stack[128];
  1817. command = "reorder";
  1818. calldata_init_fixed(&params, stack, sizeof(stack));
  1819. signal_parent(item->parent, command, &params);
  1820. }
  1821. static inline void signal_refresh(obs_scene_t *scene)
  1822. {
  1823. const char *command = NULL;
  1824. struct calldata params;
  1825. uint8_t stack[128];
  1826. command = "refresh";
  1827. calldata_init_fixed(&params, stack, sizeof(stack));
  1828. signal_parent(scene, command, &params);
  1829. }
  1830. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  1831. enum obs_order_movement movement)
  1832. {
  1833. if (!item)
  1834. return;
  1835. struct obs_scene_item *next, *prev;
  1836. struct obs_scene *scene = item->parent;
  1837. obs_scene_addref(scene);
  1838. full_lock(scene);
  1839. next = item->next;
  1840. prev = item->prev;
  1841. detach_sceneitem(item);
  1842. if (movement == OBS_ORDER_MOVE_DOWN) {
  1843. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1844. } else if (movement == OBS_ORDER_MOVE_UP) {
  1845. attach_sceneitem(scene, item, next ? next : prev);
  1846. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1847. struct obs_scene_item *last = next;
  1848. if (!last) {
  1849. last = prev;
  1850. } else {
  1851. while (last->next)
  1852. last = last->next;
  1853. }
  1854. attach_sceneitem(scene, item, last);
  1855. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1856. attach_sceneitem(scene, item, NULL);
  1857. }
  1858. full_unlock(scene);
  1859. signal_reorder(item);
  1860. obs_scene_release(scene);
  1861. }
  1862. int obs_sceneitem_get_order_position(obs_sceneitem_t *item)
  1863. {
  1864. struct obs_scene *scene = item->parent;
  1865. struct obs_scene_item *next = scene->first_item;
  1866. full_lock(scene);
  1867. int index = 0;
  1868. while (next && next != item) {
  1869. next = next->next;
  1870. ++index;
  1871. }
  1872. full_unlock(scene);
  1873. return index;
  1874. }
  1875. void obs_sceneitem_set_order_position(obs_sceneitem_t *item, int position)
  1876. {
  1877. if (!item)
  1878. return;
  1879. struct obs_scene *scene = item->parent;
  1880. struct obs_scene_item *next;
  1881. obs_scene_addref(scene);
  1882. full_lock(scene);
  1883. detach_sceneitem(item);
  1884. next = scene->first_item;
  1885. if (position == 0) {
  1886. attach_sceneitem(scene, item, NULL);
  1887. } else {
  1888. for (int i = position; i > 1; --i) {
  1889. if (next->next == NULL)
  1890. break;
  1891. next = next->next;
  1892. }
  1893. attach_sceneitem(scene, item, next);
  1894. }
  1895. full_unlock(scene);
  1896. signal_reorder(item);
  1897. obs_scene_release(scene);
  1898. }
  1899. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1900. enum obs_bounds_type type)
  1901. {
  1902. if (item) {
  1903. item->bounds_type = type;
  1904. do_update_transform(item);
  1905. }
  1906. }
  1907. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1908. uint32_t alignment)
  1909. {
  1910. if (item) {
  1911. item->bounds_align = alignment;
  1912. do_update_transform(item);
  1913. }
  1914. }
  1915. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1916. {
  1917. if (item) {
  1918. item->bounds = *bounds;
  1919. do_update_transform(item);
  1920. }
  1921. }
  1922. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1923. {
  1924. if (item)
  1925. vec2_copy(pos, &item->pos);
  1926. }
  1927. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1928. {
  1929. return item ? item->rot : 0.0f;
  1930. }
  1931. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1932. {
  1933. if (item)
  1934. vec2_copy(scale, &item->scale);
  1935. }
  1936. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1937. {
  1938. return item ? item->align : 0;
  1939. }
  1940. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1941. {
  1942. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1943. }
  1944. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1945. {
  1946. return item ? item->bounds_align : 0;
  1947. }
  1948. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1949. {
  1950. if (item)
  1951. *bounds = item->bounds;
  1952. }
  1953. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1954. struct obs_transform_info *info)
  1955. {
  1956. if (item && info) {
  1957. info->pos = item->pos;
  1958. info->rot = item->rot;
  1959. info->scale = item->scale;
  1960. info->alignment = item->align;
  1961. info->bounds_type = item->bounds_type;
  1962. info->bounds_alignment = item->bounds_align;
  1963. info->bounds = item->bounds;
  1964. }
  1965. }
  1966. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1967. const struct obs_transform_info *info)
  1968. {
  1969. if (item && info) {
  1970. item->pos = info->pos;
  1971. item->rot = info->rot;
  1972. item->scale = info->scale;
  1973. item->align = info->alignment;
  1974. item->bounds_type = info->bounds_type;
  1975. item->bounds_align = info->bounds_alignment;
  1976. item->bounds = info->bounds;
  1977. do_update_transform(item);
  1978. }
  1979. }
  1980. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1981. struct matrix4 *transform)
  1982. {
  1983. if (item)
  1984. matrix4_copy(transform, &item->draw_transform);
  1985. }
  1986. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1987. struct matrix4 *transform)
  1988. {
  1989. if (item)
  1990. matrix4_copy(transform, &item->box_transform);
  1991. }
  1992. void obs_sceneitem_get_box_scale(const obs_sceneitem_t *item,
  1993. struct vec2 *scale)
  1994. {
  1995. if (item)
  1996. *scale = item->box_scale;
  1997. }
  1998. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1999. {
  2000. return item ? item->user_visible : false;
  2001. }
  2002. static bool group_item_transition(obs_scene_t *scene, obs_sceneitem_t *item,
  2003. void *param)
  2004. {
  2005. if (!param || !item)
  2006. return true;
  2007. const bool visible = *(bool *)param;
  2008. if (obs_sceneitem_visible(item))
  2009. obs_sceneitem_do_transition(item, visible);
  2010. UNUSED_PARAMETER(scene);
  2011. return true;
  2012. }
  2013. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  2014. {
  2015. struct calldata cd;
  2016. uint8_t stack[256];
  2017. struct item_action action = {.visible = visible,
  2018. .timestamp = os_gettime_ns()};
  2019. if (!item)
  2020. return false;
  2021. if (item->user_visible == visible)
  2022. return false;
  2023. if (!item->parent)
  2024. return false;
  2025. obs_sceneitem_do_transition(item, visible);
  2026. if (obs_sceneitem_is_group(item))
  2027. obs_sceneitem_group_enum_items(item, group_item_transition,
  2028. &visible);
  2029. item->user_visible = visible;
  2030. if (visible) {
  2031. if (os_atomic_inc_long(&item->active_refs) == 1) {
  2032. if (!obs_source_add_active_child(item->parent->source,
  2033. item->source)) {
  2034. os_atomic_dec_long(&item->active_refs);
  2035. return false;
  2036. }
  2037. }
  2038. }
  2039. calldata_init_fixed(&cd, stack, sizeof(stack));
  2040. calldata_set_ptr(&cd, "item", item);
  2041. calldata_set_bool(&cd, "visible", visible);
  2042. signal_parent(item->parent, "item_visible", &cd);
  2043. if (source_has_audio(item->source)) {
  2044. pthread_mutex_lock(&item->actions_mutex);
  2045. da_push_back(item->audio_actions, &action);
  2046. pthread_mutex_unlock(&item->actions_mutex);
  2047. } else {
  2048. set_visibility(item, visible);
  2049. }
  2050. return true;
  2051. }
  2052. bool obs_sceneitem_locked(const obs_sceneitem_t *item)
  2053. {
  2054. return item ? item->locked : false;
  2055. }
  2056. bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
  2057. {
  2058. struct calldata cd;
  2059. uint8_t stack[256];
  2060. if (!item)
  2061. return false;
  2062. if (item->locked == lock)
  2063. return false;
  2064. if (!item->parent)
  2065. return false;
  2066. item->locked = lock;
  2067. calldata_init_fixed(&cd, stack, sizeof(stack));
  2068. calldata_set_ptr(&cd, "item", item);
  2069. calldata_set_bool(&cd, "locked", lock);
  2070. signal_parent(item->parent, "item_locked", &cd);
  2071. return true;
  2072. }
  2073. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t *const *items,
  2074. size_t size, bool *order_matches)
  2075. {
  2076. obs_sceneitem_t *item = scene->first_item;
  2077. size_t count = 0;
  2078. while (item) {
  2079. bool found = false;
  2080. for (size_t i = 0; i < size; i++) {
  2081. if (items[i] != item)
  2082. continue;
  2083. if (count != i)
  2084. *order_matches = false;
  2085. found = true;
  2086. break;
  2087. }
  2088. if (!found)
  2089. return false;
  2090. item = item->next;
  2091. count += 1;
  2092. }
  2093. return count == size;
  2094. }
  2095. bool obs_scene_reorder_items(obs_scene_t *scene,
  2096. obs_sceneitem_t *const *item_order,
  2097. size_t item_order_size)
  2098. {
  2099. if (!scene || !item_order_size)
  2100. return false;
  2101. obs_scene_addref(scene);
  2102. full_lock(scene);
  2103. bool order_matches = true;
  2104. if (!sceneitems_match(scene, item_order, item_order_size,
  2105. &order_matches) ||
  2106. order_matches) {
  2107. full_unlock(scene);
  2108. obs_scene_release(scene);
  2109. return false;
  2110. }
  2111. scene->first_item = item_order[0];
  2112. obs_sceneitem_t *prev = NULL;
  2113. for (size_t i = 0; i < item_order_size; i++) {
  2114. item_order[i]->prev = prev;
  2115. item_order[i]->next = NULL;
  2116. if (prev)
  2117. prev->next = item_order[i];
  2118. prev = item_order[i];
  2119. }
  2120. full_unlock(scene);
  2121. signal_reorder(scene->first_item);
  2122. obs_scene_release(scene);
  2123. return true;
  2124. }
  2125. void obs_scene_atomic_update(obs_scene_t *scene,
  2126. obs_scene_atomic_update_func func, void *data)
  2127. {
  2128. if (!scene)
  2129. return;
  2130. obs_scene_addref(scene);
  2131. full_lock(scene);
  2132. func(data, scene);
  2133. full_unlock(scene);
  2134. obs_scene_release(scene);
  2135. }
  2136. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  2137. const struct obs_sceneitem_crop *crop2)
  2138. {
  2139. return crop1->left == crop2->left && crop1->right == crop2->right &&
  2140. crop1->top == crop2->top && crop1->bottom == crop2->bottom;
  2141. }
  2142. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  2143. const struct obs_sceneitem_crop *crop)
  2144. {
  2145. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  2146. return;
  2147. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  2148. return;
  2149. if (crop_equal(crop, &item->crop))
  2150. return;
  2151. memcpy(&item->crop, crop, sizeof(*crop));
  2152. if (item->crop.left < 0)
  2153. item->crop.left = 0;
  2154. if (item->crop.right < 0)
  2155. item->crop.right = 0;
  2156. if (item->crop.top < 0)
  2157. item->crop.top = 0;
  2158. if (item->crop.bottom < 0)
  2159. item->crop.bottom = 0;
  2160. os_atomic_set_bool(&item->update_transform, true);
  2161. }
  2162. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  2163. struct obs_sceneitem_crop *crop)
  2164. {
  2165. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  2166. return;
  2167. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  2168. return;
  2169. memcpy(crop, &item->crop, sizeof(*crop));
  2170. }
  2171. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  2172. enum obs_scale_type filter)
  2173. {
  2174. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  2175. return;
  2176. item->scale_filter = filter;
  2177. os_atomic_set_bool(&item->update_transform, true);
  2178. }
  2179. enum obs_scale_type obs_sceneitem_get_scale_filter(obs_sceneitem_t *item)
  2180. {
  2181. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter")
  2182. ? item->scale_filter
  2183. : OBS_SCALE_DISABLE;
  2184. }
  2185. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  2186. {
  2187. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  2188. return;
  2189. os_atomic_inc_long(&item->defer_update);
  2190. }
  2191. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  2192. {
  2193. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  2194. return;
  2195. if (os_atomic_dec_long(&item->defer_update) == 0)
  2196. do_update_transform(item);
  2197. }
  2198. void obs_sceneitem_defer_group_resize_begin(obs_sceneitem_t *item)
  2199. {
  2200. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_begin"))
  2201. return;
  2202. os_atomic_inc_long(&item->defer_group_resize);
  2203. }
  2204. void obs_sceneitem_defer_group_resize_end(obs_sceneitem_t *item)
  2205. {
  2206. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_end"))
  2207. return;
  2208. if (os_atomic_dec_long(&item->defer_group_resize) == 0)
  2209. os_atomic_set_bool(&item->update_group_resize, true);
  2210. }
  2211. int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
  2212. {
  2213. if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
  2214. return 0;
  2215. return item->id;
  2216. }
  2217. void obs_sceneitem_set_id(obs_sceneitem_t *item, int64_t id)
  2218. {
  2219. item->id = id;
  2220. }
  2221. obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
  2222. {
  2223. if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
  2224. return NULL;
  2225. obs_data_addref(item->private_settings);
  2226. return item->private_settings;
  2227. }
  2228. static inline void transform_val(struct vec2 *v2, struct matrix4 *transform)
  2229. {
  2230. struct vec3 v;
  2231. vec3_set(&v, v2->x, v2->y, 0.0f);
  2232. vec3_transform(&v, &v, transform);
  2233. v2->x = v.x;
  2234. v2->y = v.y;
  2235. }
  2236. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  2237. struct vec2 *scale, float *rot)
  2238. {
  2239. struct matrix4 transform;
  2240. struct matrix4 mat;
  2241. struct vec4 x_base;
  2242. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2243. matrix4_copy(&transform, &group->draw_transform);
  2244. transform_val(pos, &transform);
  2245. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2246. vec4_set(&mat.x, scale->x, 0.0f, 0.0f, 0.0f);
  2247. vec4_set(&mat.y, 0.0f, scale->y, 0.0f, 0.0f);
  2248. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2249. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2250. matrix4_mul(&mat, &mat, &transform);
  2251. scale->x = vec4_len(&mat.x) * (scale->x > 0.0f ? 1.0f : -1.0f);
  2252. scale->y = vec4_len(&mat.y) * (scale->y > 0.0f ? 1.0f : -1.0f);
  2253. *rot += group->rot;
  2254. }
  2255. static void remove_group_transform(obs_sceneitem_t *group,
  2256. obs_sceneitem_t *item)
  2257. {
  2258. obs_scene_t *parent = item->parent;
  2259. if (!parent || !group)
  2260. return;
  2261. get_ungrouped_transform(group, &item->pos, &item->scale, &item->rot);
  2262. update_item_transform(item, false);
  2263. }
  2264. static void apply_group_transform(obs_sceneitem_t *item, obs_sceneitem_t *group)
  2265. {
  2266. struct matrix4 transform;
  2267. struct matrix4 mat;
  2268. struct vec4 x_base;
  2269. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2270. matrix4_inv(&transform, &group->draw_transform);
  2271. transform_val(&item->pos, &transform);
  2272. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2273. vec4_set(&mat.x, item->scale.x, 0.0f, 0.0f, 0.0f);
  2274. vec4_set(&mat.y, 0.0f, item->scale.y, 0.0f, 0.0f);
  2275. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2276. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2277. matrix4_mul(&mat, &mat, &transform);
  2278. item->scale.x =
  2279. vec4_len(&mat.x) * (item->scale.x > 0.0f ? 1.0f : -1.0f);
  2280. item->scale.y =
  2281. vec4_len(&mat.y) * (item->scale.y > 0.0f ? 1.0f : -1.0f);
  2282. item->rot -= group->rot;
  2283. update_item_transform(item, false);
  2284. }
  2285. static bool resize_scene_base(obs_scene_t *scene, struct vec2 *minv,
  2286. struct vec2 *maxv, struct vec2 *scale)
  2287. {
  2288. vec2_set(minv, M_INFINITE, M_INFINITE);
  2289. vec2_set(maxv, -M_INFINITE, -M_INFINITE);
  2290. obs_sceneitem_t *item = scene->first_item;
  2291. if (!item) {
  2292. scene->cx = 0;
  2293. scene->cy = 0;
  2294. return false;
  2295. }
  2296. while (item) {
  2297. #define get_min_max(x_val, y_val) \
  2298. do { \
  2299. struct vec3 v; \
  2300. vec3_set(&v, x_val, y_val, 0.0f); \
  2301. vec3_transform(&v, &v, &item->box_transform); \
  2302. if (v.x < minv->x) \
  2303. minv->x = v.x; \
  2304. if (v.y < minv->y) \
  2305. minv->y = v.y; \
  2306. if (v.x > maxv->x) \
  2307. maxv->x = v.x; \
  2308. if (v.y > maxv->y) \
  2309. maxv->y = v.y; \
  2310. } while (false)
  2311. get_min_max(0.0f, 0.0f);
  2312. get_min_max(1.0f, 0.0f);
  2313. get_min_max(0.0f, 1.0f);
  2314. get_min_max(1.0f, 1.0f);
  2315. #undef get_min_max
  2316. item = item->next;
  2317. }
  2318. item = scene->first_item;
  2319. while (item) {
  2320. vec2_sub(&item->pos, &item->pos, minv);
  2321. update_item_transform(item, false);
  2322. item = item->next;
  2323. }
  2324. vec2_sub(scale, maxv, minv);
  2325. scene->cx = (uint32_t)ceilf(scale->x);
  2326. scene->cy = (uint32_t)ceilf(scale->y);
  2327. return true;
  2328. }
  2329. static void resize_scene(obs_scene_t *scene)
  2330. {
  2331. struct vec2 minv;
  2332. struct vec2 maxv;
  2333. struct vec2 scale;
  2334. resize_scene_base(scene, &minv, &maxv, &scale);
  2335. }
  2336. /* assumes group scene and parent scene is locked */
  2337. static void resize_group(obs_sceneitem_t *group)
  2338. {
  2339. obs_scene_t *scene = group->source->context.data;
  2340. struct vec2 minv;
  2341. struct vec2 maxv;
  2342. struct vec2 scale;
  2343. if (os_atomic_load_long(&group->defer_group_resize) > 0)
  2344. return;
  2345. if (!resize_scene_base(scene, &minv, &maxv, &scale))
  2346. return;
  2347. if (group->bounds_type == OBS_BOUNDS_NONE) {
  2348. struct vec2 new_pos;
  2349. if ((group->align & OBS_ALIGN_LEFT) != 0)
  2350. new_pos.x = minv.x;
  2351. else if ((group->align & OBS_ALIGN_RIGHT) != 0)
  2352. new_pos.x = maxv.x;
  2353. else
  2354. new_pos.x = (maxv.x - minv.x) * 0.5f + minv.x;
  2355. if ((group->align & OBS_ALIGN_TOP) != 0)
  2356. new_pos.y = minv.y;
  2357. else if ((group->align & OBS_ALIGN_BOTTOM) != 0)
  2358. new_pos.y = maxv.y;
  2359. else
  2360. new_pos.y = (maxv.y - minv.y) * 0.5f + minv.y;
  2361. transform_val(&new_pos, &group->draw_transform);
  2362. vec2_copy(&group->pos, &new_pos);
  2363. }
  2364. os_atomic_set_bool(&group->update_group_resize, false);
  2365. update_item_transform(group, false);
  2366. }
  2367. obs_sceneitem_t *obs_scene_add_group(obs_scene_t *scene, const char *name)
  2368. {
  2369. return obs_scene_insert_group(scene, name, NULL, 0);
  2370. }
  2371. obs_sceneitem_t *obs_scene_add_group2(obs_scene_t *scene, const char *name,
  2372. bool signal)
  2373. {
  2374. return obs_scene_insert_group2(scene, name, NULL, 0, signal);
  2375. }
  2376. obs_sceneitem_t *obs_scene_insert_group(obs_scene_t *scene, const char *name,
  2377. obs_sceneitem_t **items, size_t count)
  2378. {
  2379. if (!scene)
  2380. return NULL;
  2381. /* don't allow groups or sub-items of other groups */
  2382. for (size_t i = count; i > 0; i--) {
  2383. obs_sceneitem_t *item = items[i - 1];
  2384. if (item->parent != scene || item->is_group)
  2385. return NULL;
  2386. }
  2387. obs_scene_t *sub_scene = create_id("group", name);
  2388. obs_sceneitem_t *last_item = items ? items[count - 1] : NULL;
  2389. obs_sceneitem_t *item =
  2390. obs_scene_add_internal(scene, sub_scene->source, last_item);
  2391. obs_scene_release(sub_scene);
  2392. if (!items || !count)
  2393. return item;
  2394. /* ------------------------- */
  2395. full_lock(scene);
  2396. full_lock(sub_scene);
  2397. sub_scene->first_item = items[0];
  2398. for (size_t i = count; i > 0; i--) {
  2399. size_t idx = i - 1;
  2400. remove_group_transform(item, items[idx]);
  2401. detach_sceneitem(items[idx]);
  2402. }
  2403. for (size_t i = 0; i < count; i++) {
  2404. size_t idx = i;
  2405. if (idx != (count - 1)) {
  2406. size_t next_idx = idx + 1;
  2407. items[idx]->next = items[next_idx];
  2408. items[next_idx]->prev = items[idx];
  2409. } else {
  2410. items[idx]->next = NULL;
  2411. }
  2412. items[idx]->parent = sub_scene;
  2413. apply_group_transform(items[idx], item);
  2414. }
  2415. items[0]->prev = NULL;
  2416. resize_group(item);
  2417. full_unlock(sub_scene);
  2418. full_unlock(scene);
  2419. /* ------------------------- */
  2420. return item;
  2421. }
  2422. obs_sceneitem_t *obs_scene_insert_group2(obs_scene_t *scene, const char *name,
  2423. obs_sceneitem_t **items, size_t count,
  2424. bool signal)
  2425. {
  2426. obs_sceneitem_t *item =
  2427. obs_scene_insert_group(scene, name, items, count);
  2428. if (signal && item)
  2429. signal_refresh(scene);
  2430. return item;
  2431. }
  2432. obs_sceneitem_t *obs_scene_get_group(obs_scene_t *scene, const char *name)
  2433. {
  2434. if (!scene || !name || !*name) {
  2435. return NULL;
  2436. }
  2437. obs_sceneitem_t *group = NULL;
  2438. obs_sceneitem_t *item;
  2439. full_lock(scene);
  2440. item = scene->first_item;
  2441. while (item) {
  2442. if (item->is_group && item->source->context.name) {
  2443. if (strcmp(item->source->context.name, name) == 0) {
  2444. group = item;
  2445. break;
  2446. }
  2447. }
  2448. item = item->next;
  2449. }
  2450. full_unlock(scene);
  2451. return group;
  2452. }
  2453. bool obs_sceneitem_is_group(obs_sceneitem_t *item)
  2454. {
  2455. return item && item->is_group;
  2456. }
  2457. obs_scene_t *obs_sceneitem_group_get_scene(const obs_sceneitem_t *item)
  2458. {
  2459. return (item && item->is_group) ? item->source->context.data : NULL;
  2460. }
  2461. void obs_sceneitem_group_ungroup(obs_sceneitem_t *item)
  2462. {
  2463. if (!item || !item->is_group)
  2464. return;
  2465. obs_scene_t *scene = item->parent;
  2466. obs_scene_t *subscene = item->source->context.data;
  2467. obs_sceneitem_t *insert_after = item;
  2468. obs_sceneitem_t *first;
  2469. obs_sceneitem_t *last;
  2470. full_lock(scene);
  2471. /* ------------------------- */
  2472. full_lock(subscene);
  2473. first = subscene->first_item;
  2474. last = first;
  2475. while (last) {
  2476. obs_sceneitem_t *dst;
  2477. remove_group_transform(item, last);
  2478. dst = obs_scene_add_internal(scene, last->source, insert_after);
  2479. duplicate_item_data(dst, last, true, true, true);
  2480. apply_group_transform(last, item);
  2481. if (!last->next)
  2482. break;
  2483. insert_after = dst;
  2484. last = last->next;
  2485. }
  2486. full_unlock(subscene);
  2487. /* ------------------------- */
  2488. detach_sceneitem(item);
  2489. full_unlock(scene);
  2490. obs_sceneitem_release(item);
  2491. }
  2492. void obs_sceneitem_group_ungroup2(obs_sceneitem_t *item, bool signal)
  2493. {
  2494. obs_scene_t *scene = item->parent;
  2495. obs_sceneitem_group_ungroup(item);
  2496. if (signal)
  2497. signal_refresh(scene);
  2498. }
  2499. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2500. {
  2501. if (!group || !group->is_group || !item)
  2502. return;
  2503. obs_scene_t *scene = group->parent;
  2504. obs_scene_t *groupscene = group->source->context.data;
  2505. if (item->parent != scene)
  2506. return;
  2507. if (item->parent == groupscene)
  2508. return;
  2509. /* ------------------------- */
  2510. full_lock(scene);
  2511. full_lock(groupscene);
  2512. remove_group_transform(group, item);
  2513. detach_sceneitem(item);
  2514. attach_sceneitem(groupscene, item, NULL);
  2515. apply_group_transform(item, group);
  2516. resize_group(group);
  2517. full_unlock(groupscene);
  2518. full_unlock(scene);
  2519. /* ------------------------- */
  2520. signal_refresh(scene);
  2521. }
  2522. void obs_sceneitem_group_remove_item(obs_sceneitem_t *group,
  2523. obs_sceneitem_t *item)
  2524. {
  2525. if (!item || !group || !group->is_group)
  2526. return;
  2527. obs_scene_t *groupscene = item->parent;
  2528. obs_scene_t *scene = group->parent;
  2529. /* ------------------------- */
  2530. full_lock(scene);
  2531. full_lock(groupscene);
  2532. remove_group_transform(group, item);
  2533. detach_sceneitem(item);
  2534. attach_sceneitem(scene, item, NULL);
  2535. resize_group(group);
  2536. full_unlock(groupscene);
  2537. full_unlock(scene);
  2538. /* ------------------------- */
  2539. signal_refresh(scene);
  2540. }
  2541. static void
  2542. build_current_order_info(obs_scene_t *scene,
  2543. struct obs_sceneitem_order_info **items_out,
  2544. size_t *size_out)
  2545. {
  2546. DARRAY(struct obs_sceneitem_order_info) items;
  2547. da_init(items);
  2548. obs_sceneitem_t *item = scene->first_item;
  2549. while (item) {
  2550. da_push_back(items, &item);
  2551. if (item->is_group) {
  2552. obs_scene_t *sub_scene = item->source->context.data;
  2553. full_lock(sub_scene);
  2554. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2555. while (sub_item) {
  2556. da_push_back(items, &item);
  2557. sub_item = sub_item->next;
  2558. }
  2559. full_unlock(sub_scene);
  2560. }
  2561. item = item->next;
  2562. }
  2563. *items_out = items.array;
  2564. *size_out = items.num;
  2565. }
  2566. static bool sceneitems_match2(obs_scene_t *scene,
  2567. struct obs_sceneitem_order_info *items,
  2568. size_t size)
  2569. {
  2570. struct obs_sceneitem_order_info *cur_items;
  2571. size_t cur_size;
  2572. build_current_order_info(scene, &cur_items, &cur_size);
  2573. if (cur_size != size) {
  2574. bfree(cur_items);
  2575. return false;
  2576. }
  2577. for (size_t i = 0; i < size; i++) {
  2578. struct obs_sceneitem_order_info *new = &items[i];
  2579. struct obs_sceneitem_order_info *old = &cur_items[i];
  2580. if (new->group != old->group || new->item != old->item) {
  2581. bfree(cur_items);
  2582. return false;
  2583. }
  2584. }
  2585. bfree(cur_items);
  2586. return true;
  2587. }
  2588. static obs_sceneitem_t *
  2589. get_sceneitem_parent_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem)
  2590. {
  2591. if (group_subitem->is_group)
  2592. return NULL;
  2593. obs_sceneitem_t *item = scene->first_item;
  2594. while (item) {
  2595. if (item->is_group &&
  2596. item->source->context.data == group_subitem->parent)
  2597. return item;
  2598. item = item->next;
  2599. }
  2600. return NULL;
  2601. }
  2602. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2603. struct obs_sceneitem_order_info *item_order,
  2604. size_t item_order_size)
  2605. {
  2606. if (!scene || !item_order_size || !item_order)
  2607. return false;
  2608. obs_scene_addref(scene);
  2609. full_lock(scene);
  2610. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2611. full_unlock(scene);
  2612. obs_scene_release(scene);
  2613. return false;
  2614. }
  2615. for (size_t i = 0; i < item_order_size; i++) {
  2616. struct obs_sceneitem_order_info *info = &item_order[i];
  2617. if (!info->item->is_group) {
  2618. obs_sceneitem_t *group =
  2619. get_sceneitem_parent_group(scene, info->item);
  2620. remove_group_transform(group, info->item);
  2621. }
  2622. }
  2623. scene->first_item = item_order[0].item;
  2624. obs_sceneitem_t *prev = NULL;
  2625. for (size_t i = 0; i < item_order_size; i++) {
  2626. struct obs_sceneitem_order_info *info = &item_order[i];
  2627. obs_sceneitem_t *item = info->item;
  2628. if (info->item->is_group) {
  2629. obs_sceneitem_t *sub_prev = NULL;
  2630. obs_scene_t *sub_scene =
  2631. info->item->source->context.data;
  2632. sub_scene->first_item = NULL;
  2633. obs_scene_addref(sub_scene);
  2634. full_lock(sub_scene);
  2635. for (i++; i < item_order_size; i++) {
  2636. struct obs_sceneitem_order_info *sub_info =
  2637. &item_order[i];
  2638. obs_sceneitem_t *sub_item = sub_info->item;
  2639. if (sub_info->group != info->item) {
  2640. i--;
  2641. break;
  2642. }
  2643. if (!sub_scene->first_item)
  2644. sub_scene->first_item = sub_item;
  2645. sub_item->prev = sub_prev;
  2646. sub_item->next = NULL;
  2647. sub_item->parent = sub_scene;
  2648. if (sub_prev)
  2649. sub_prev->next = sub_item;
  2650. apply_group_transform(sub_info->item,
  2651. sub_info->group);
  2652. sub_prev = sub_item;
  2653. }
  2654. resize_group(info->item);
  2655. full_unlock(sub_scene);
  2656. obs_scene_release(sub_scene);
  2657. }
  2658. item->prev = prev;
  2659. item->next = NULL;
  2660. item->parent = scene;
  2661. if (prev)
  2662. prev->next = item;
  2663. prev = item;
  2664. }
  2665. full_unlock(scene);
  2666. signal_reorder(scene->first_item);
  2667. obs_scene_release(scene);
  2668. return true;
  2669. }
  2670. obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene,
  2671. obs_sceneitem_t *group_subitem)
  2672. {
  2673. if (!scene || !group_subitem || group_subitem->is_group)
  2674. return NULL;
  2675. full_lock(scene);
  2676. obs_sceneitem_t *group =
  2677. get_sceneitem_parent_group(scene, group_subitem);
  2678. full_unlock(scene);
  2679. return group;
  2680. }
  2681. bool obs_source_is_group(const obs_source_t *source)
  2682. {
  2683. return source && strcmp(source->info.id, group_info.id) == 0;
  2684. }
  2685. bool obs_scene_is_group(const obs_scene_t *scene)
  2686. {
  2687. return scene ? scene->is_group : false;
  2688. }
  2689. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  2690. bool (*callback)(obs_scene_t *,
  2691. obs_sceneitem_t *, void *),
  2692. void *param)
  2693. {
  2694. if (!group || !group->is_group)
  2695. return;
  2696. obs_scene_t *scene = group->source->context.data;
  2697. if (scene)
  2698. obs_scene_enum_items(scene, callback, param);
  2699. }
  2700. void obs_sceneitem_force_update_transform(obs_sceneitem_t *item)
  2701. {
  2702. if (!item)
  2703. return;
  2704. if (os_atomic_set_bool(&item->update_transform, false))
  2705. update_item_transform(item, false);
  2706. }
  2707. void obs_sceneitem_set_show_transition(obs_sceneitem_t *item,
  2708. obs_source_t *transition)
  2709. {
  2710. if (!item)
  2711. return;
  2712. if (item->show_transition)
  2713. obs_source_release(item->show_transition);
  2714. item->show_transition = transition;
  2715. if (item->show_transition)
  2716. obs_source_addref(item->show_transition);
  2717. }
  2718. void obs_sceneitem_set_show_transition_duration(obs_sceneitem_t *item,
  2719. uint32_t duration_ms)
  2720. {
  2721. if (!item)
  2722. return;
  2723. item->show_transition_duration = duration_ms;
  2724. }
  2725. obs_source_t *obs_sceneitem_get_show_transition(obs_sceneitem_t *item)
  2726. {
  2727. if (!item)
  2728. return NULL;
  2729. return item->show_transition;
  2730. }
  2731. uint32_t obs_sceneitem_get_show_transition_duration(obs_sceneitem_t *item)
  2732. {
  2733. if (!item)
  2734. return 0;
  2735. return item->show_transition_duration;
  2736. }
  2737. void obs_sceneitem_set_hide_transition(obs_sceneitem_t *item,
  2738. obs_source_t *transition)
  2739. {
  2740. if (!item)
  2741. return;
  2742. if (item->hide_transition)
  2743. obs_source_release(item->hide_transition);
  2744. item->hide_transition = transition;
  2745. if (item->hide_transition)
  2746. obs_source_addref(item->hide_transition);
  2747. }
  2748. void obs_sceneitem_set_hide_transition_duration(obs_sceneitem_t *item,
  2749. uint32_t duration_ms)
  2750. {
  2751. if (!item)
  2752. return;
  2753. item->hide_transition_duration = duration_ms;
  2754. }
  2755. obs_source_t *obs_sceneitem_get_hide_transition(obs_sceneitem_t *item)
  2756. {
  2757. if (!item)
  2758. return NULL;
  2759. return item->hide_transition;
  2760. }
  2761. uint32_t obs_sceneitem_get_hide_transition_duration(obs_sceneitem_t *item)
  2762. {
  2763. if (!item)
  2764. return 0;
  2765. return item->hide_transition_duration;
  2766. }
  2767. void obs_sceneitem_transition_stop(void *data, calldata_t *calldata)
  2768. {
  2769. obs_source_t *parent = data;
  2770. obs_source_t *transition;
  2771. calldata_get_ptr(calldata, "source", &transition);
  2772. obs_source_remove_active_child(parent, transition);
  2773. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  2774. if (sh)
  2775. signal_handler_disconnect(sh, "transition_stop",
  2776. obs_sceneitem_transition_stop,
  2777. parent);
  2778. }
  2779. void obs_sceneitem_do_transition(obs_sceneitem_t *item, bool visible)
  2780. {
  2781. if (!item)
  2782. return;
  2783. if (transition_active(item->show_transition))
  2784. obs_transition_force_stop(item->show_transition);
  2785. if (transition_active(item->hide_transition))
  2786. obs_transition_force_stop(item->hide_transition);
  2787. obs_source_t *transition =
  2788. visible ? obs_sceneitem_get_show_transition(item)
  2789. : obs_sceneitem_get_hide_transition(item);
  2790. if (!transition)
  2791. return;
  2792. int duration =
  2793. (int)(visible ? obs_sceneitem_get_show_transition_duration(item)
  2794. : obs_sceneitem_get_hide_transition_duration(
  2795. item));
  2796. const int cx = obs_source_get_width(item->source);
  2797. const int cy = obs_source_get_height(item->source);
  2798. obs_transition_set_size(transition, cx, cy);
  2799. obs_transition_set_alignment(transition, OBS_ALIGN_CENTER);
  2800. obs_transition_set_scale_type(transition, OBS_TRANSITION_SCALE_ASPECT);
  2801. if (duration == 0)
  2802. duration = 300;
  2803. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  2804. obs_source_t *parent = obs_scene_get_source(scene);
  2805. obs_source_add_active_child(parent, transition);
  2806. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  2807. if (sh)
  2808. signal_handler_connect(sh, "transition_stop",
  2809. obs_sceneitem_transition_stop, parent);
  2810. if (!visible) {
  2811. obs_transition_set(transition, item->source);
  2812. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  2813. duration, NULL);
  2814. } else {
  2815. obs_transition_set(transition, NULL);
  2816. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  2817. duration, item->source);
  2818. }
  2819. }