obs-scene.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510
  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. 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. 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_int(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 = obs_data_get_int(temp, "rot");
  1734. info.alignment = 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 = obs_data_get_int(temp, "bounds_alignment");
  1738. obs_data_get_vec2(temp, "bounds", &info.bounds);
  1739. crop.top = obs_data_get_int(temp, "top");
  1740. crop.bottom = obs_data_get_int(temp, "bottom");
  1741. crop.left = obs_data_get_int(temp, "left");
  1742. crop.right = obs_data_get_int(temp, "right");
  1743. obs_sceneitem_defer_update_begin(item);
  1744. obs_sceneitem_set_info(item, &info);
  1745. obs_sceneitem_set_crop(item, &crop);
  1746. obs_sceneitem_defer_update_end(item);
  1747. }
  1748. void obs_scene_load_transform_states(const char *data)
  1749. {
  1750. obs_data_t *dat = obs_data_create_from_json(data);
  1751. obs_data_array_t *item_ids = obs_data_get_array(dat, "item_ids");
  1752. obs_source_t *source =
  1753. obs_get_source_by_name(obs_data_get_string(dat, "scene_name"));
  1754. obs_scene_t *scene = obs_scene_from_source(source);
  1755. obs_data_array_enum(item_ids, load_transform_states, (void *)scene);
  1756. obs_data_release(dat);
  1757. obs_data_array_release(item_ids);
  1758. obs_source_release(source);
  1759. }
  1760. void obs_sceneitem_select(obs_sceneitem_t *item, bool select)
  1761. {
  1762. struct calldata params;
  1763. uint8_t stack[128];
  1764. const char *command = select ? "item_select" : "item_deselect";
  1765. if (!item || item->selected == select || !item->parent)
  1766. return;
  1767. item->selected = select;
  1768. calldata_init_fixed(&params, stack, sizeof(stack));
  1769. calldata_set_ptr(&params, "item", item);
  1770. signal_parent(item->parent, command, &params);
  1771. }
  1772. bool obs_sceneitem_selected(const obs_sceneitem_t *item)
  1773. {
  1774. return item ? item->selected : false;
  1775. }
  1776. #define do_update_transform(item) \
  1777. do { \
  1778. if (!item->parent || item->parent->is_group) \
  1779. os_atomic_set_bool(&item->update_transform, true); \
  1780. else \
  1781. update_item_transform(item, false); \
  1782. } while (false)
  1783. void obs_sceneitem_set_pos(obs_sceneitem_t *item, const struct vec2 *pos)
  1784. {
  1785. if (item) {
  1786. vec2_copy(&item->pos, pos);
  1787. do_update_transform(item);
  1788. }
  1789. }
  1790. void obs_sceneitem_set_rot(obs_sceneitem_t *item, float rot)
  1791. {
  1792. if (item) {
  1793. item->rot = rot;
  1794. do_update_transform(item);
  1795. }
  1796. }
  1797. void obs_sceneitem_set_scale(obs_sceneitem_t *item, const struct vec2 *scale)
  1798. {
  1799. if (item) {
  1800. vec2_copy(&item->scale, scale);
  1801. do_update_transform(item);
  1802. }
  1803. }
  1804. void obs_sceneitem_set_alignment(obs_sceneitem_t *item, uint32_t alignment)
  1805. {
  1806. if (item) {
  1807. item->align = alignment;
  1808. do_update_transform(item);
  1809. }
  1810. }
  1811. static inline void signal_reorder(struct obs_scene_item *item)
  1812. {
  1813. const char *command = NULL;
  1814. struct calldata params;
  1815. uint8_t stack[128];
  1816. command = "reorder";
  1817. calldata_init_fixed(&params, stack, sizeof(stack));
  1818. signal_parent(item->parent, command, &params);
  1819. }
  1820. static inline void signal_refresh(obs_scene_t *scene)
  1821. {
  1822. const char *command = NULL;
  1823. struct calldata params;
  1824. uint8_t stack[128];
  1825. command = "refresh";
  1826. calldata_init_fixed(&params, stack, sizeof(stack));
  1827. signal_parent(scene, command, &params);
  1828. }
  1829. void obs_sceneitem_set_order(obs_sceneitem_t *item,
  1830. enum obs_order_movement movement)
  1831. {
  1832. if (!item)
  1833. return;
  1834. struct obs_scene_item *next, *prev;
  1835. struct obs_scene *scene = item->parent;
  1836. obs_scene_addref(scene);
  1837. full_lock(scene);
  1838. next = item->next;
  1839. prev = item->prev;
  1840. detach_sceneitem(item);
  1841. if (movement == OBS_ORDER_MOVE_DOWN) {
  1842. attach_sceneitem(scene, item, prev ? prev->prev : NULL);
  1843. } else if (movement == OBS_ORDER_MOVE_UP) {
  1844. attach_sceneitem(scene, item, next ? next : prev);
  1845. } else if (movement == OBS_ORDER_MOVE_TOP) {
  1846. struct obs_scene_item *last = next;
  1847. if (!last) {
  1848. last = prev;
  1849. } else {
  1850. while (last->next)
  1851. last = last->next;
  1852. }
  1853. attach_sceneitem(scene, item, last);
  1854. } else if (movement == OBS_ORDER_MOVE_BOTTOM) {
  1855. attach_sceneitem(scene, item, NULL);
  1856. }
  1857. full_unlock(scene);
  1858. signal_reorder(item);
  1859. obs_scene_release(scene);
  1860. }
  1861. int obs_sceneitem_get_order_position(obs_sceneitem_t *item)
  1862. {
  1863. struct obs_scene *scene = item->parent;
  1864. struct obs_scene_item *next = scene->first_item;
  1865. full_lock(scene);
  1866. int index = 0;
  1867. while (next && next != item) {
  1868. next = next->next;
  1869. ++index;
  1870. }
  1871. full_unlock(scene);
  1872. return index;
  1873. }
  1874. void obs_sceneitem_set_order_position(obs_sceneitem_t *item, int position)
  1875. {
  1876. if (!item)
  1877. return;
  1878. struct obs_scene *scene = item->parent;
  1879. struct obs_scene_item *next;
  1880. obs_scene_addref(scene);
  1881. full_lock(scene);
  1882. detach_sceneitem(item);
  1883. next = scene->first_item;
  1884. if (position == 0) {
  1885. attach_sceneitem(scene, item, NULL);
  1886. } else {
  1887. for (int i = position; i > 1; --i) {
  1888. if (next->next == NULL)
  1889. break;
  1890. next = next->next;
  1891. }
  1892. attach_sceneitem(scene, item, next);
  1893. }
  1894. full_unlock(scene);
  1895. signal_reorder(item);
  1896. obs_scene_release(scene);
  1897. }
  1898. void obs_sceneitem_set_bounds_type(obs_sceneitem_t *item,
  1899. enum obs_bounds_type type)
  1900. {
  1901. if (item) {
  1902. item->bounds_type = type;
  1903. do_update_transform(item);
  1904. }
  1905. }
  1906. void obs_sceneitem_set_bounds_alignment(obs_sceneitem_t *item,
  1907. uint32_t alignment)
  1908. {
  1909. if (item) {
  1910. item->bounds_align = alignment;
  1911. do_update_transform(item);
  1912. }
  1913. }
  1914. void obs_sceneitem_set_bounds(obs_sceneitem_t *item, const struct vec2 *bounds)
  1915. {
  1916. if (item) {
  1917. item->bounds = *bounds;
  1918. do_update_transform(item);
  1919. }
  1920. }
  1921. void obs_sceneitem_get_pos(const obs_sceneitem_t *item, struct vec2 *pos)
  1922. {
  1923. if (item)
  1924. vec2_copy(pos, &item->pos);
  1925. }
  1926. float obs_sceneitem_get_rot(const obs_sceneitem_t *item)
  1927. {
  1928. return item ? item->rot : 0.0f;
  1929. }
  1930. void obs_sceneitem_get_scale(const obs_sceneitem_t *item, struct vec2 *scale)
  1931. {
  1932. if (item)
  1933. vec2_copy(scale, &item->scale);
  1934. }
  1935. uint32_t obs_sceneitem_get_alignment(const obs_sceneitem_t *item)
  1936. {
  1937. return item ? item->align : 0;
  1938. }
  1939. enum obs_bounds_type obs_sceneitem_get_bounds_type(const obs_sceneitem_t *item)
  1940. {
  1941. return item ? item->bounds_type : OBS_BOUNDS_NONE;
  1942. }
  1943. uint32_t obs_sceneitem_get_bounds_alignment(const obs_sceneitem_t *item)
  1944. {
  1945. return item ? item->bounds_align : 0;
  1946. }
  1947. void obs_sceneitem_get_bounds(const obs_sceneitem_t *item, struct vec2 *bounds)
  1948. {
  1949. if (item)
  1950. *bounds = item->bounds;
  1951. }
  1952. void obs_sceneitem_get_info(const obs_sceneitem_t *item,
  1953. struct obs_transform_info *info)
  1954. {
  1955. if (item && info) {
  1956. info->pos = item->pos;
  1957. info->rot = item->rot;
  1958. info->scale = item->scale;
  1959. info->alignment = item->align;
  1960. info->bounds_type = item->bounds_type;
  1961. info->bounds_alignment = item->bounds_align;
  1962. info->bounds = item->bounds;
  1963. }
  1964. }
  1965. void obs_sceneitem_set_info(obs_sceneitem_t *item,
  1966. const struct obs_transform_info *info)
  1967. {
  1968. if (item && info) {
  1969. item->pos = info->pos;
  1970. item->rot = info->rot;
  1971. item->scale = info->scale;
  1972. item->align = info->alignment;
  1973. item->bounds_type = info->bounds_type;
  1974. item->bounds_align = info->bounds_alignment;
  1975. item->bounds = info->bounds;
  1976. do_update_transform(item);
  1977. }
  1978. }
  1979. void obs_sceneitem_get_draw_transform(const obs_sceneitem_t *item,
  1980. struct matrix4 *transform)
  1981. {
  1982. if (item)
  1983. matrix4_copy(transform, &item->draw_transform);
  1984. }
  1985. void obs_sceneitem_get_box_transform(const obs_sceneitem_t *item,
  1986. struct matrix4 *transform)
  1987. {
  1988. if (item)
  1989. matrix4_copy(transform, &item->box_transform);
  1990. }
  1991. void obs_sceneitem_get_box_scale(const obs_sceneitem_t *item,
  1992. struct vec2 *scale)
  1993. {
  1994. if (item)
  1995. *scale = item->box_scale;
  1996. }
  1997. bool obs_sceneitem_visible(const obs_sceneitem_t *item)
  1998. {
  1999. return item ? item->user_visible : false;
  2000. }
  2001. static bool group_item_transition(obs_scene_t *scene, obs_sceneitem_t *item,
  2002. void *param)
  2003. {
  2004. if (!param || !item)
  2005. return true;
  2006. const bool visible = *(bool *)param;
  2007. if (obs_sceneitem_visible(item))
  2008. obs_sceneitem_do_transition(item, visible);
  2009. UNUSED_PARAMETER(scene);
  2010. return true;
  2011. }
  2012. bool obs_sceneitem_set_visible(obs_sceneitem_t *item, bool visible)
  2013. {
  2014. struct calldata cd;
  2015. uint8_t stack[256];
  2016. struct item_action action = {.visible = visible,
  2017. .timestamp = os_gettime_ns()};
  2018. if (!item)
  2019. return false;
  2020. if (item->user_visible == visible)
  2021. return false;
  2022. if (!item->parent)
  2023. return false;
  2024. obs_sceneitem_do_transition(item, visible);
  2025. if (obs_sceneitem_is_group(item))
  2026. obs_sceneitem_group_enum_items(item, group_item_transition,
  2027. &visible);
  2028. item->user_visible = visible;
  2029. if (visible) {
  2030. if (os_atomic_inc_long(&item->active_refs) == 1) {
  2031. if (!obs_source_add_active_child(item->parent->source,
  2032. item->source)) {
  2033. os_atomic_dec_long(&item->active_refs);
  2034. return false;
  2035. }
  2036. }
  2037. }
  2038. calldata_init_fixed(&cd, stack, sizeof(stack));
  2039. calldata_set_ptr(&cd, "item", item);
  2040. calldata_set_bool(&cd, "visible", visible);
  2041. signal_parent(item->parent, "item_visible", &cd);
  2042. if (source_has_audio(item->source)) {
  2043. pthread_mutex_lock(&item->actions_mutex);
  2044. da_push_back(item->audio_actions, &action);
  2045. pthread_mutex_unlock(&item->actions_mutex);
  2046. } else {
  2047. set_visibility(item, visible);
  2048. }
  2049. return true;
  2050. }
  2051. bool obs_sceneitem_locked(const obs_sceneitem_t *item)
  2052. {
  2053. return item ? item->locked : false;
  2054. }
  2055. bool obs_sceneitem_set_locked(obs_sceneitem_t *item, bool lock)
  2056. {
  2057. struct calldata cd;
  2058. uint8_t stack[256];
  2059. if (!item)
  2060. return false;
  2061. if (item->locked == lock)
  2062. return false;
  2063. if (!item->parent)
  2064. return false;
  2065. item->locked = lock;
  2066. calldata_init_fixed(&cd, stack, sizeof(stack));
  2067. calldata_set_ptr(&cd, "item", item);
  2068. calldata_set_bool(&cd, "locked", lock);
  2069. signal_parent(item->parent, "item_locked", &cd);
  2070. return true;
  2071. }
  2072. static bool sceneitems_match(obs_scene_t *scene, obs_sceneitem_t *const *items,
  2073. size_t size, bool *order_matches)
  2074. {
  2075. obs_sceneitem_t *item = scene->first_item;
  2076. size_t count = 0;
  2077. while (item) {
  2078. bool found = false;
  2079. for (size_t i = 0; i < size; i++) {
  2080. if (items[i] != item)
  2081. continue;
  2082. if (count != i)
  2083. *order_matches = false;
  2084. found = true;
  2085. break;
  2086. }
  2087. if (!found)
  2088. return false;
  2089. item = item->next;
  2090. count += 1;
  2091. }
  2092. return count == size;
  2093. }
  2094. bool obs_scene_reorder_items(obs_scene_t *scene,
  2095. obs_sceneitem_t *const *item_order,
  2096. size_t item_order_size)
  2097. {
  2098. if (!scene || !item_order_size)
  2099. return false;
  2100. obs_scene_addref(scene);
  2101. full_lock(scene);
  2102. bool order_matches = true;
  2103. if (!sceneitems_match(scene, item_order, item_order_size,
  2104. &order_matches) ||
  2105. order_matches) {
  2106. full_unlock(scene);
  2107. obs_scene_release(scene);
  2108. return false;
  2109. }
  2110. scene->first_item = item_order[0];
  2111. obs_sceneitem_t *prev = NULL;
  2112. for (size_t i = 0; i < item_order_size; i++) {
  2113. item_order[i]->prev = prev;
  2114. item_order[i]->next = NULL;
  2115. if (prev)
  2116. prev->next = item_order[i];
  2117. prev = item_order[i];
  2118. }
  2119. full_unlock(scene);
  2120. signal_reorder(scene->first_item);
  2121. obs_scene_release(scene);
  2122. return true;
  2123. }
  2124. void obs_scene_atomic_update(obs_scene_t *scene,
  2125. obs_scene_atomic_update_func func, void *data)
  2126. {
  2127. if (!scene)
  2128. return;
  2129. obs_scene_addref(scene);
  2130. full_lock(scene);
  2131. func(data, scene);
  2132. full_unlock(scene);
  2133. obs_scene_release(scene);
  2134. }
  2135. static inline bool crop_equal(const struct obs_sceneitem_crop *crop1,
  2136. const struct obs_sceneitem_crop *crop2)
  2137. {
  2138. return crop1->left == crop2->left && crop1->right == crop2->right &&
  2139. crop1->top == crop2->top && crop1->bottom == crop2->bottom;
  2140. }
  2141. void obs_sceneitem_set_crop(obs_sceneitem_t *item,
  2142. const struct obs_sceneitem_crop *crop)
  2143. {
  2144. if (!obs_ptr_valid(item, "obs_sceneitem_set_crop"))
  2145. return;
  2146. if (!obs_ptr_valid(crop, "obs_sceneitem_set_crop"))
  2147. return;
  2148. if (crop_equal(crop, &item->crop))
  2149. return;
  2150. memcpy(&item->crop, crop, sizeof(*crop));
  2151. if (item->crop.left < 0)
  2152. item->crop.left = 0;
  2153. if (item->crop.right < 0)
  2154. item->crop.right = 0;
  2155. if (item->crop.top < 0)
  2156. item->crop.top = 0;
  2157. if (item->crop.bottom < 0)
  2158. item->crop.bottom = 0;
  2159. os_atomic_set_bool(&item->update_transform, true);
  2160. }
  2161. void obs_sceneitem_get_crop(const obs_sceneitem_t *item,
  2162. struct obs_sceneitem_crop *crop)
  2163. {
  2164. if (!obs_ptr_valid(item, "obs_sceneitem_get_crop"))
  2165. return;
  2166. if (!obs_ptr_valid(crop, "obs_sceneitem_get_crop"))
  2167. return;
  2168. memcpy(crop, &item->crop, sizeof(*crop));
  2169. }
  2170. void obs_sceneitem_set_scale_filter(obs_sceneitem_t *item,
  2171. enum obs_scale_type filter)
  2172. {
  2173. if (!obs_ptr_valid(item, "obs_sceneitem_set_scale_filter"))
  2174. return;
  2175. item->scale_filter = filter;
  2176. os_atomic_set_bool(&item->update_transform, true);
  2177. }
  2178. enum obs_scale_type obs_sceneitem_get_scale_filter(obs_sceneitem_t *item)
  2179. {
  2180. return obs_ptr_valid(item, "obs_sceneitem_get_scale_filter")
  2181. ? item->scale_filter
  2182. : OBS_SCALE_DISABLE;
  2183. }
  2184. void obs_sceneitem_defer_update_begin(obs_sceneitem_t *item)
  2185. {
  2186. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_begin"))
  2187. return;
  2188. os_atomic_inc_long(&item->defer_update);
  2189. }
  2190. void obs_sceneitem_defer_update_end(obs_sceneitem_t *item)
  2191. {
  2192. if (!obs_ptr_valid(item, "obs_sceneitem_defer_update_end"))
  2193. return;
  2194. if (os_atomic_dec_long(&item->defer_update) == 0)
  2195. do_update_transform(item);
  2196. }
  2197. void obs_sceneitem_defer_group_resize_begin(obs_sceneitem_t *item)
  2198. {
  2199. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_begin"))
  2200. return;
  2201. os_atomic_inc_long(&item->defer_group_resize);
  2202. }
  2203. void obs_sceneitem_defer_group_resize_end(obs_sceneitem_t *item)
  2204. {
  2205. if (!obs_ptr_valid(item, "obs_sceneitem_defer_group_resize_end"))
  2206. return;
  2207. if (os_atomic_dec_long(&item->defer_group_resize) == 0)
  2208. os_atomic_set_bool(&item->update_group_resize, true);
  2209. }
  2210. int64_t obs_sceneitem_get_id(const obs_sceneitem_t *item)
  2211. {
  2212. if (!obs_ptr_valid(item, "obs_sceneitem_get_id"))
  2213. return 0;
  2214. return item->id;
  2215. }
  2216. void obs_sceneitem_set_id(obs_sceneitem_t *item, int64_t id)
  2217. {
  2218. item->id = id;
  2219. }
  2220. obs_data_t *obs_sceneitem_get_private_settings(obs_sceneitem_t *item)
  2221. {
  2222. if (!obs_ptr_valid(item, "obs_sceneitem_get_private_settings"))
  2223. return NULL;
  2224. obs_data_addref(item->private_settings);
  2225. return item->private_settings;
  2226. }
  2227. static inline void transform_val(struct vec2 *v2, struct matrix4 *transform)
  2228. {
  2229. struct vec3 v;
  2230. vec3_set(&v, v2->x, v2->y, 0.0f);
  2231. vec3_transform(&v, &v, transform);
  2232. v2->x = v.x;
  2233. v2->y = v.y;
  2234. }
  2235. static void get_ungrouped_transform(obs_sceneitem_t *group, struct vec2 *pos,
  2236. struct vec2 *scale, float *rot)
  2237. {
  2238. struct matrix4 transform;
  2239. struct matrix4 mat;
  2240. struct vec4 x_base;
  2241. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2242. matrix4_copy(&transform, &group->draw_transform);
  2243. transform_val(pos, &transform);
  2244. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2245. vec4_set(&mat.x, scale->x, 0.0f, 0.0f, 0.0f);
  2246. vec4_set(&mat.y, 0.0f, scale->y, 0.0f, 0.0f);
  2247. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2248. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2249. matrix4_mul(&mat, &mat, &transform);
  2250. scale->x = vec4_len(&mat.x) * (scale->x > 0.0f ? 1.0f : -1.0f);
  2251. scale->y = vec4_len(&mat.y) * (scale->y > 0.0f ? 1.0f : -1.0f);
  2252. *rot += group->rot;
  2253. }
  2254. static void remove_group_transform(obs_sceneitem_t *group,
  2255. obs_sceneitem_t *item)
  2256. {
  2257. obs_scene_t *parent = item->parent;
  2258. if (!parent || !group)
  2259. return;
  2260. get_ungrouped_transform(group, &item->pos, &item->scale, &item->rot);
  2261. update_item_transform(item, false);
  2262. }
  2263. static void apply_group_transform(obs_sceneitem_t *item, obs_sceneitem_t *group)
  2264. {
  2265. struct matrix4 transform;
  2266. struct matrix4 mat;
  2267. struct vec4 x_base;
  2268. vec4_set(&x_base, 1.0f, 0.0f, 0.0f, 0.0f);
  2269. matrix4_inv(&transform, &group->draw_transform);
  2270. transform_val(&item->pos, &transform);
  2271. vec4_set(&transform.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2272. vec4_set(&mat.x, item->scale.x, 0.0f, 0.0f, 0.0f);
  2273. vec4_set(&mat.y, 0.0f, item->scale.y, 0.0f, 0.0f);
  2274. vec4_set(&mat.z, 0.0f, 0.0f, 1.0f, 0.0f);
  2275. vec4_set(&mat.t, 0.0f, 0.0f, 0.0f, 1.0f);
  2276. matrix4_mul(&mat, &mat, &transform);
  2277. item->scale.x =
  2278. vec4_len(&mat.x) * (item->scale.x > 0.0f ? 1.0f : -1.0f);
  2279. item->scale.y =
  2280. vec4_len(&mat.y) * (item->scale.y > 0.0f ? 1.0f : -1.0f);
  2281. item->rot -= group->rot;
  2282. update_item_transform(item, false);
  2283. }
  2284. static bool resize_scene_base(obs_scene_t *scene, struct vec2 *minv,
  2285. struct vec2 *maxv, struct vec2 *scale)
  2286. {
  2287. vec2_set(minv, M_INFINITE, M_INFINITE);
  2288. vec2_set(maxv, -M_INFINITE, -M_INFINITE);
  2289. obs_sceneitem_t *item = scene->first_item;
  2290. if (!item) {
  2291. scene->cx = 0;
  2292. scene->cy = 0;
  2293. return false;
  2294. }
  2295. while (item) {
  2296. #define get_min_max(x_val, y_val) \
  2297. do { \
  2298. struct vec3 v; \
  2299. vec3_set(&v, x_val, y_val, 0.0f); \
  2300. vec3_transform(&v, &v, &item->box_transform); \
  2301. if (v.x < minv->x) \
  2302. minv->x = v.x; \
  2303. if (v.y < minv->y) \
  2304. minv->y = v.y; \
  2305. if (v.x > maxv->x) \
  2306. maxv->x = v.x; \
  2307. if (v.y > maxv->y) \
  2308. maxv->y = v.y; \
  2309. } while (false)
  2310. get_min_max(0.0f, 0.0f);
  2311. get_min_max(1.0f, 0.0f);
  2312. get_min_max(0.0f, 1.0f);
  2313. get_min_max(1.0f, 1.0f);
  2314. #undef get_min_max
  2315. item = item->next;
  2316. }
  2317. item = scene->first_item;
  2318. while (item) {
  2319. vec2_sub(&item->pos, &item->pos, minv);
  2320. update_item_transform(item, false);
  2321. item = item->next;
  2322. }
  2323. vec2_sub(scale, maxv, minv);
  2324. scene->cx = (uint32_t)ceilf(scale->x);
  2325. scene->cy = (uint32_t)ceilf(scale->y);
  2326. return true;
  2327. }
  2328. static void resize_scene(obs_scene_t *scene)
  2329. {
  2330. struct vec2 minv;
  2331. struct vec2 maxv;
  2332. struct vec2 scale;
  2333. resize_scene_base(scene, &minv, &maxv, &scale);
  2334. }
  2335. /* assumes group scene and parent scene is locked */
  2336. static void resize_group(obs_sceneitem_t *group)
  2337. {
  2338. obs_scene_t *scene = group->source->context.data;
  2339. struct vec2 minv;
  2340. struct vec2 maxv;
  2341. struct vec2 scale;
  2342. if (os_atomic_load_long(&group->defer_group_resize) > 0)
  2343. return;
  2344. if (!resize_scene_base(scene, &minv, &maxv, &scale))
  2345. return;
  2346. if (group->bounds_type == OBS_BOUNDS_NONE) {
  2347. struct vec2 new_pos;
  2348. if ((group->align & OBS_ALIGN_LEFT) != 0)
  2349. new_pos.x = minv.x;
  2350. else if ((group->align & OBS_ALIGN_RIGHT) != 0)
  2351. new_pos.x = maxv.x;
  2352. else
  2353. new_pos.x = (maxv.x - minv.x) * 0.5f + minv.x;
  2354. if ((group->align & OBS_ALIGN_TOP) != 0)
  2355. new_pos.y = minv.y;
  2356. else if ((group->align & OBS_ALIGN_BOTTOM) != 0)
  2357. new_pos.y = maxv.y;
  2358. else
  2359. new_pos.y = (maxv.y - minv.y) * 0.5f + minv.y;
  2360. transform_val(&new_pos, &group->draw_transform);
  2361. vec2_copy(&group->pos, &new_pos);
  2362. }
  2363. os_atomic_set_bool(&group->update_group_resize, false);
  2364. update_item_transform(group, false);
  2365. }
  2366. obs_sceneitem_t *obs_scene_add_group(obs_scene_t *scene, const char *name)
  2367. {
  2368. return obs_scene_insert_group(scene, name, NULL, 0);
  2369. }
  2370. obs_sceneitem_t *obs_scene_add_group2(obs_scene_t *scene, const char *name,
  2371. bool signal)
  2372. {
  2373. return obs_scene_insert_group2(scene, name, NULL, 0, signal);
  2374. }
  2375. obs_sceneitem_t *obs_scene_insert_group(obs_scene_t *scene, const char *name,
  2376. obs_sceneitem_t **items, size_t count)
  2377. {
  2378. if (!scene)
  2379. return NULL;
  2380. /* don't allow groups or sub-items of other groups */
  2381. for (size_t i = count; i > 0; i--) {
  2382. obs_sceneitem_t *item = items[i - 1];
  2383. if (item->parent != scene || item->is_group)
  2384. return NULL;
  2385. }
  2386. obs_scene_t *sub_scene = create_id("group", name);
  2387. obs_sceneitem_t *last_item = items ? items[count - 1] : NULL;
  2388. obs_sceneitem_t *item =
  2389. obs_scene_add_internal(scene, sub_scene->source, last_item);
  2390. obs_scene_release(sub_scene);
  2391. if (!items || !count)
  2392. return item;
  2393. /* ------------------------- */
  2394. full_lock(scene);
  2395. full_lock(sub_scene);
  2396. sub_scene->first_item = items[0];
  2397. for (size_t i = count; i > 0; i--) {
  2398. size_t idx = i - 1;
  2399. remove_group_transform(item, items[idx]);
  2400. detach_sceneitem(items[idx]);
  2401. }
  2402. for (size_t i = 0; i < count; i++) {
  2403. size_t idx = i;
  2404. if (idx != (count - 1)) {
  2405. size_t next_idx = idx + 1;
  2406. items[idx]->next = items[next_idx];
  2407. items[next_idx]->prev = items[idx];
  2408. } else {
  2409. items[idx]->next = NULL;
  2410. }
  2411. items[idx]->parent = sub_scene;
  2412. apply_group_transform(items[idx], item);
  2413. }
  2414. items[0]->prev = NULL;
  2415. resize_group(item);
  2416. full_unlock(sub_scene);
  2417. full_unlock(scene);
  2418. /* ------------------------- */
  2419. return item;
  2420. }
  2421. obs_sceneitem_t *obs_scene_insert_group2(obs_scene_t *scene, const char *name,
  2422. obs_sceneitem_t **items, size_t count,
  2423. bool signal)
  2424. {
  2425. obs_sceneitem_t *item =
  2426. obs_scene_insert_group(scene, name, items, count);
  2427. if (signal && item)
  2428. signal_refresh(scene);
  2429. return item;
  2430. }
  2431. obs_sceneitem_t *obs_scene_get_group(obs_scene_t *scene, const char *name)
  2432. {
  2433. if (!scene || !name || !*name) {
  2434. return NULL;
  2435. }
  2436. obs_sceneitem_t *group = NULL;
  2437. obs_sceneitem_t *item;
  2438. full_lock(scene);
  2439. item = scene->first_item;
  2440. while (item) {
  2441. if (item->is_group && item->source->context.name) {
  2442. if (strcmp(item->source->context.name, name) == 0) {
  2443. group = item;
  2444. break;
  2445. }
  2446. }
  2447. item = item->next;
  2448. }
  2449. full_unlock(scene);
  2450. return group;
  2451. }
  2452. bool obs_sceneitem_is_group(obs_sceneitem_t *item)
  2453. {
  2454. return item && item->is_group;
  2455. }
  2456. obs_scene_t *obs_sceneitem_group_get_scene(const obs_sceneitem_t *item)
  2457. {
  2458. return (item && item->is_group) ? item->source->context.data : NULL;
  2459. }
  2460. void obs_sceneitem_group_ungroup(obs_sceneitem_t *item)
  2461. {
  2462. if (!item || !item->is_group)
  2463. return;
  2464. obs_scene_t *scene = item->parent;
  2465. obs_scene_t *subscene = item->source->context.data;
  2466. obs_sceneitem_t *insert_after = item;
  2467. obs_sceneitem_t *first;
  2468. obs_sceneitem_t *last;
  2469. full_lock(scene);
  2470. /* ------------------------- */
  2471. full_lock(subscene);
  2472. first = subscene->first_item;
  2473. last = first;
  2474. while (last) {
  2475. obs_sceneitem_t *dst;
  2476. remove_group_transform(item, last);
  2477. dst = obs_scene_add_internal(scene, last->source, insert_after);
  2478. duplicate_item_data(dst, last, true, true, true);
  2479. apply_group_transform(last, item);
  2480. if (!last->next)
  2481. break;
  2482. insert_after = dst;
  2483. last = last->next;
  2484. }
  2485. full_unlock(subscene);
  2486. /* ------------------------- */
  2487. detach_sceneitem(item);
  2488. full_unlock(scene);
  2489. obs_sceneitem_release(item);
  2490. }
  2491. void obs_sceneitem_group_ungroup2(obs_sceneitem_t *item, bool signal)
  2492. {
  2493. obs_scene_t *scene = item->parent;
  2494. obs_sceneitem_group_ungroup(item);
  2495. if (signal)
  2496. signal_refresh(scene);
  2497. }
  2498. void obs_sceneitem_group_add_item(obs_sceneitem_t *group, obs_sceneitem_t *item)
  2499. {
  2500. if (!group || !group->is_group || !item)
  2501. return;
  2502. obs_scene_t *scene = group->parent;
  2503. obs_scene_t *groupscene = group->source->context.data;
  2504. if (item->parent != scene)
  2505. return;
  2506. if (item->parent == groupscene)
  2507. return;
  2508. /* ------------------------- */
  2509. full_lock(scene);
  2510. full_lock(groupscene);
  2511. remove_group_transform(group, item);
  2512. detach_sceneitem(item);
  2513. attach_sceneitem(groupscene, item, NULL);
  2514. apply_group_transform(item, group);
  2515. resize_group(group);
  2516. full_unlock(groupscene);
  2517. full_unlock(scene);
  2518. /* ------------------------- */
  2519. signal_refresh(scene);
  2520. }
  2521. void obs_sceneitem_group_remove_item(obs_sceneitem_t *group,
  2522. obs_sceneitem_t *item)
  2523. {
  2524. if (!item || !group || !group->is_group)
  2525. return;
  2526. obs_scene_t *groupscene = item->parent;
  2527. obs_scene_t *scene = group->parent;
  2528. /* ------------------------- */
  2529. full_lock(scene);
  2530. full_lock(groupscene);
  2531. remove_group_transform(group, item);
  2532. detach_sceneitem(item);
  2533. attach_sceneitem(scene, item, NULL);
  2534. resize_group(group);
  2535. full_unlock(groupscene);
  2536. full_unlock(scene);
  2537. /* ------------------------- */
  2538. signal_refresh(scene);
  2539. }
  2540. static void
  2541. build_current_order_info(obs_scene_t *scene,
  2542. struct obs_sceneitem_order_info **items_out,
  2543. size_t *size_out)
  2544. {
  2545. DARRAY(struct obs_sceneitem_order_info) items;
  2546. da_init(items);
  2547. obs_sceneitem_t *item = scene->first_item;
  2548. while (item) {
  2549. da_push_back(items, &item);
  2550. if (item->is_group) {
  2551. obs_scene_t *sub_scene = item->source->context.data;
  2552. full_lock(sub_scene);
  2553. obs_sceneitem_t *sub_item = sub_scene->first_item;
  2554. while (sub_item) {
  2555. da_push_back(items, &item);
  2556. sub_item = sub_item->next;
  2557. }
  2558. full_unlock(sub_scene);
  2559. }
  2560. item = item->next;
  2561. }
  2562. *items_out = items.array;
  2563. *size_out = items.num;
  2564. }
  2565. static bool sceneitems_match2(obs_scene_t *scene,
  2566. struct obs_sceneitem_order_info *items,
  2567. size_t size)
  2568. {
  2569. struct obs_sceneitem_order_info *cur_items;
  2570. size_t cur_size;
  2571. build_current_order_info(scene, &cur_items, &cur_size);
  2572. if (cur_size != size) {
  2573. bfree(cur_items);
  2574. return false;
  2575. }
  2576. for (size_t i = 0; i < size; i++) {
  2577. struct obs_sceneitem_order_info *new = &items[i];
  2578. struct obs_sceneitem_order_info *old = &cur_items[i];
  2579. if (new->group != old->group || new->item != old->item) {
  2580. bfree(cur_items);
  2581. return false;
  2582. }
  2583. }
  2584. bfree(cur_items);
  2585. return true;
  2586. }
  2587. static obs_sceneitem_t *
  2588. get_sceneitem_parent_group(obs_scene_t *scene, obs_sceneitem_t *group_subitem)
  2589. {
  2590. if (group_subitem->is_group)
  2591. return NULL;
  2592. obs_sceneitem_t *item = scene->first_item;
  2593. while (item) {
  2594. if (item->is_group &&
  2595. item->source->context.data == group_subitem->parent)
  2596. return item;
  2597. item = item->next;
  2598. }
  2599. return NULL;
  2600. }
  2601. bool obs_scene_reorder_items2(obs_scene_t *scene,
  2602. struct obs_sceneitem_order_info *item_order,
  2603. size_t item_order_size)
  2604. {
  2605. if (!scene || !item_order_size || !item_order)
  2606. return false;
  2607. obs_scene_addref(scene);
  2608. full_lock(scene);
  2609. if (sceneitems_match2(scene, item_order, item_order_size)) {
  2610. full_unlock(scene);
  2611. obs_scene_release(scene);
  2612. return false;
  2613. }
  2614. for (size_t i = 0; i < item_order_size; i++) {
  2615. struct obs_sceneitem_order_info *info = &item_order[i];
  2616. if (!info->item->is_group) {
  2617. obs_sceneitem_t *group =
  2618. get_sceneitem_parent_group(scene, info->item);
  2619. remove_group_transform(group, info->item);
  2620. }
  2621. }
  2622. scene->first_item = item_order[0].item;
  2623. obs_sceneitem_t *prev = NULL;
  2624. for (size_t i = 0; i < item_order_size; i++) {
  2625. struct obs_sceneitem_order_info *info = &item_order[i];
  2626. obs_sceneitem_t *item = info->item;
  2627. if (info->item->is_group) {
  2628. obs_sceneitem_t *sub_prev = NULL;
  2629. obs_scene_t *sub_scene =
  2630. info->item->source->context.data;
  2631. sub_scene->first_item = NULL;
  2632. obs_scene_addref(sub_scene);
  2633. full_lock(sub_scene);
  2634. for (i++; i < item_order_size; i++) {
  2635. struct obs_sceneitem_order_info *sub_info =
  2636. &item_order[i];
  2637. obs_sceneitem_t *sub_item = sub_info->item;
  2638. if (sub_info->group != info->item) {
  2639. i--;
  2640. break;
  2641. }
  2642. if (!sub_scene->first_item)
  2643. sub_scene->first_item = sub_item;
  2644. sub_item->prev = sub_prev;
  2645. sub_item->next = NULL;
  2646. sub_item->parent = sub_scene;
  2647. if (sub_prev)
  2648. sub_prev->next = sub_item;
  2649. apply_group_transform(sub_info->item,
  2650. sub_info->group);
  2651. sub_prev = sub_item;
  2652. }
  2653. resize_group(info->item);
  2654. full_unlock(sub_scene);
  2655. obs_scene_release(sub_scene);
  2656. }
  2657. item->prev = prev;
  2658. item->next = NULL;
  2659. item->parent = scene;
  2660. if (prev)
  2661. prev->next = item;
  2662. prev = item;
  2663. }
  2664. full_unlock(scene);
  2665. signal_reorder(scene->first_item);
  2666. obs_scene_release(scene);
  2667. return true;
  2668. }
  2669. obs_sceneitem_t *obs_sceneitem_get_group(obs_scene_t *scene,
  2670. obs_sceneitem_t *group_subitem)
  2671. {
  2672. if (!scene || !group_subitem || group_subitem->is_group)
  2673. return NULL;
  2674. full_lock(scene);
  2675. obs_sceneitem_t *group =
  2676. get_sceneitem_parent_group(scene, group_subitem);
  2677. full_unlock(scene);
  2678. return group;
  2679. }
  2680. bool obs_source_is_group(const obs_source_t *source)
  2681. {
  2682. return source && strcmp(source->info.id, group_info.id) == 0;
  2683. }
  2684. bool obs_scene_is_group(const obs_scene_t *scene)
  2685. {
  2686. return scene ? scene->is_group : false;
  2687. }
  2688. void obs_sceneitem_group_enum_items(obs_sceneitem_t *group,
  2689. bool (*callback)(obs_scene_t *,
  2690. obs_sceneitem_t *, void *),
  2691. void *param)
  2692. {
  2693. if (!group || !group->is_group)
  2694. return;
  2695. obs_scene_t *scene = group->source->context.data;
  2696. if (scene)
  2697. obs_scene_enum_items(scene, callback, param);
  2698. }
  2699. void obs_sceneitem_force_update_transform(obs_sceneitem_t *item)
  2700. {
  2701. if (!item)
  2702. return;
  2703. if (os_atomic_set_bool(&item->update_transform, false))
  2704. update_item_transform(item, false);
  2705. }
  2706. void obs_sceneitem_set_show_transition(obs_sceneitem_t *item,
  2707. obs_source_t *transition)
  2708. {
  2709. if (!item)
  2710. return;
  2711. if (item->show_transition)
  2712. obs_source_release(item->show_transition);
  2713. item->show_transition = transition;
  2714. if (item->show_transition)
  2715. obs_source_addref(item->show_transition);
  2716. }
  2717. void obs_sceneitem_set_show_transition_duration(obs_sceneitem_t *item,
  2718. uint32_t duration_ms)
  2719. {
  2720. if (!item)
  2721. return;
  2722. item->show_transition_duration = duration_ms;
  2723. }
  2724. obs_source_t *obs_sceneitem_get_show_transition(obs_sceneitem_t *item)
  2725. {
  2726. if (!item)
  2727. return NULL;
  2728. return item->show_transition;
  2729. }
  2730. uint32_t obs_sceneitem_get_show_transition_duration(obs_sceneitem_t *item)
  2731. {
  2732. if (!item)
  2733. return 0;
  2734. return item->show_transition_duration;
  2735. }
  2736. void obs_sceneitem_set_hide_transition(obs_sceneitem_t *item,
  2737. obs_source_t *transition)
  2738. {
  2739. if (!item)
  2740. return;
  2741. if (item->hide_transition)
  2742. obs_source_release(item->hide_transition);
  2743. item->hide_transition = transition;
  2744. if (item->hide_transition)
  2745. obs_source_addref(item->hide_transition);
  2746. }
  2747. void obs_sceneitem_set_hide_transition_duration(obs_sceneitem_t *item,
  2748. uint32_t duration_ms)
  2749. {
  2750. if (!item)
  2751. return;
  2752. item->hide_transition_duration = duration_ms;
  2753. }
  2754. obs_source_t *obs_sceneitem_get_hide_transition(obs_sceneitem_t *item)
  2755. {
  2756. if (!item)
  2757. return NULL;
  2758. return item->hide_transition;
  2759. }
  2760. uint32_t obs_sceneitem_get_hide_transition_duration(obs_sceneitem_t *item)
  2761. {
  2762. if (!item)
  2763. return 0;
  2764. return item->hide_transition_duration;
  2765. }
  2766. void obs_sceneitem_transition_stop(void *data, calldata_t *calldata)
  2767. {
  2768. obs_source_t *parent = data;
  2769. obs_source_t *transition;
  2770. calldata_get_ptr(calldata, "source", &transition);
  2771. obs_source_remove_active_child(parent, transition);
  2772. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  2773. if (sh)
  2774. signal_handler_disconnect(sh, "transition_stop",
  2775. obs_sceneitem_transition_stop,
  2776. parent);
  2777. }
  2778. void obs_sceneitem_do_transition(obs_sceneitem_t *item, bool visible)
  2779. {
  2780. if (!item)
  2781. return;
  2782. if (transition_active(item->show_transition))
  2783. obs_transition_force_stop(item->show_transition);
  2784. if (transition_active(item->hide_transition))
  2785. obs_transition_force_stop(item->hide_transition);
  2786. obs_source_t *transition =
  2787. visible ? obs_sceneitem_get_show_transition(item)
  2788. : obs_sceneitem_get_hide_transition(item);
  2789. if (!transition)
  2790. return;
  2791. int duration =
  2792. (int)(visible ? obs_sceneitem_get_show_transition_duration(item)
  2793. : obs_sceneitem_get_hide_transition_duration(
  2794. item));
  2795. const int cx = obs_source_get_width(item->source);
  2796. const int cy = obs_source_get_height(item->source);
  2797. obs_transition_set_size(transition, cx, cy);
  2798. obs_transition_set_alignment(transition, OBS_ALIGN_CENTER);
  2799. obs_transition_set_scale_type(transition, OBS_TRANSITION_SCALE_ASPECT);
  2800. if (duration == 0)
  2801. duration = 300;
  2802. obs_scene_t *scene = obs_sceneitem_get_scene(item);
  2803. obs_source_t *parent = obs_scene_get_source(scene);
  2804. obs_source_add_active_child(parent, transition);
  2805. signal_handler_t *sh = obs_source_get_signal_handler(transition);
  2806. if (sh)
  2807. signal_handler_connect(sh, "transition_stop",
  2808. obs_sceneitem_transition_stop, parent);
  2809. if (!visible) {
  2810. obs_transition_set(transition, item->source);
  2811. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  2812. duration, NULL);
  2813. } else {
  2814. obs_transition_set(transition, NULL);
  2815. obs_transition_start(transition, OBS_TRANSITION_MODE_AUTO,
  2816. duration, item->source);
  2817. }
  2818. }