obs.c 91 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525
  1. /******************************************************************************
  2. Copyright (C) 2023 by Lain Bailey <[email protected]>
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation, either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ******************************************************************************/
  14. #include <inttypes.h>
  15. #include "graphics/matrix4.h"
  16. #include "callback/calldata.h"
  17. #include "obs.h"
  18. #include "obs-internal.h"
  19. struct obs_core *obs = NULL;
  20. static THREAD_LOCAL bool is_ui_thread = false;
  21. extern void add_default_module_paths(void);
  22. extern char *find_libobs_data_file(const char *file);
  23. static inline void make_video_info(struct video_output_info *vi,
  24. struct obs_video_info *ovi)
  25. {
  26. vi->name = "video";
  27. vi->format = ovi->output_format;
  28. vi->fps_num = ovi->fps_num;
  29. vi->fps_den = ovi->fps_den;
  30. vi->width = ovi->output_width;
  31. vi->height = ovi->output_height;
  32. vi->range = ovi->range;
  33. vi->colorspace = ovi->colorspace;
  34. vi->cache_size = 6;
  35. }
  36. static inline void calc_gpu_conversion_sizes(struct obs_core_video_mix *video)
  37. {
  38. const struct video_output_info *info =
  39. video_output_get_info(video->video);
  40. video->conversion_needed = false;
  41. video->conversion_techs[0] = NULL;
  42. video->conversion_techs[1] = NULL;
  43. video->conversion_techs[2] = NULL;
  44. video->conversion_width_i = 0.f;
  45. video->conversion_height_i = 0.f;
  46. switch (info->format) {
  47. case VIDEO_FORMAT_I420:
  48. video->conversion_needed = true;
  49. video->conversion_techs[0] = "Planar_Y";
  50. video->conversion_techs[1] = "Planar_U_Left";
  51. video->conversion_techs[2] = "Planar_V_Left";
  52. video->conversion_width_i = 1.f / (float)info->width;
  53. break;
  54. case VIDEO_FORMAT_NV12:
  55. video->conversion_needed = true;
  56. video->conversion_techs[0] = "NV12_Y";
  57. video->conversion_techs[1] = "NV12_UV";
  58. video->conversion_width_i = 1.f / (float)info->width;
  59. break;
  60. case VIDEO_FORMAT_I444:
  61. video->conversion_needed = true;
  62. video->conversion_techs[0] = "Planar_Y";
  63. video->conversion_techs[1] = "Planar_U";
  64. video->conversion_techs[2] = "Planar_V";
  65. break;
  66. case VIDEO_FORMAT_I010:
  67. video->conversion_needed = true;
  68. video->conversion_width_i = 1.f / (float)info->width;
  69. video->conversion_height_i = 1.f / (float)info->height;
  70. if (info->colorspace == VIDEO_CS_2100_PQ) {
  71. video->conversion_techs[0] = "I010_PQ_Y";
  72. video->conversion_techs[1] = "I010_PQ_U";
  73. video->conversion_techs[2] = "I010_PQ_V";
  74. } else if (info->colorspace == VIDEO_CS_2100_HLG) {
  75. video->conversion_techs[0] = "I010_HLG_Y";
  76. video->conversion_techs[1] = "I010_HLG_U";
  77. video->conversion_techs[2] = "I010_HLG_V";
  78. } else {
  79. video->conversion_techs[0] = "I010_SRGB_Y";
  80. video->conversion_techs[1] = "I010_SRGB_U";
  81. video->conversion_techs[2] = "I010_SRGB_V";
  82. }
  83. break;
  84. case VIDEO_FORMAT_P010:
  85. video->conversion_needed = true;
  86. video->conversion_width_i = 1.f / (float)info->width;
  87. video->conversion_height_i = 1.f / (float)info->height;
  88. if (info->colorspace == VIDEO_CS_2100_PQ) {
  89. video->conversion_techs[0] = "P010_PQ_Y";
  90. video->conversion_techs[1] = "P010_PQ_UV";
  91. } else if (info->colorspace == VIDEO_CS_2100_HLG) {
  92. video->conversion_techs[0] = "P010_HLG_Y";
  93. video->conversion_techs[1] = "P010_HLG_UV";
  94. } else {
  95. video->conversion_techs[0] = "P010_SRGB_Y";
  96. video->conversion_techs[1] = "P010_SRGB_UV";
  97. }
  98. break;
  99. case VIDEO_FORMAT_P216:
  100. video->conversion_needed = true;
  101. video->conversion_width_i = 1.f / (float)info->width;
  102. video->conversion_height_i = 1.f / (float)info->height;
  103. if (info->colorspace == VIDEO_CS_2100_PQ) {
  104. video->conversion_techs[0] = "P216_PQ_Y";
  105. video->conversion_techs[1] = "P216_PQ_UV";
  106. } else if (info->colorspace == VIDEO_CS_2100_HLG) {
  107. video->conversion_techs[0] = "P216_HLG_Y";
  108. video->conversion_techs[1] = "P216_HLG_UV";
  109. } else {
  110. video->conversion_techs[0] = "P216_SRGB_Y";
  111. video->conversion_techs[1] = "P216_SRGB_UV";
  112. }
  113. break;
  114. case VIDEO_FORMAT_P416:
  115. video->conversion_needed = true;
  116. video->conversion_width_i = 1.f / (float)info->width;
  117. video->conversion_height_i = 1.f / (float)info->height;
  118. if (info->colorspace == VIDEO_CS_2100_PQ) {
  119. video->conversion_techs[0] = "P416_PQ_Y";
  120. video->conversion_techs[1] = "P416_PQ_UV";
  121. } else if (info->colorspace == VIDEO_CS_2100_HLG) {
  122. video->conversion_techs[0] = "P416_HLG_Y";
  123. video->conversion_techs[1] = "P416_HLG_UV";
  124. } else {
  125. video->conversion_techs[0] = "P416_SRGB_Y";
  126. video->conversion_techs[1] = "P416_SRGB_UV";
  127. }
  128. break;
  129. default:
  130. break;
  131. }
  132. }
  133. static bool obs_init_gpu_conversion(struct obs_core_video_mix *video)
  134. {
  135. const struct video_output_info *info =
  136. video_output_get_info(video->video);
  137. calc_gpu_conversion_sizes(video);
  138. video->using_nv12_tex =
  139. info->format == VIDEO_FORMAT_NV12 ? gs_nv12_available() : false;
  140. video->using_p010_tex =
  141. info->format == VIDEO_FORMAT_P010 ? gs_p010_available() : false;
  142. if (!video->conversion_needed) {
  143. blog(LOG_INFO, "GPU conversion not available for format: %u",
  144. (unsigned int)info->format);
  145. video->gpu_conversion = false;
  146. video->using_nv12_tex = false;
  147. video->using_p010_tex = false;
  148. blog(LOG_INFO, "NV12 texture support not available");
  149. return true;
  150. }
  151. if (video->using_nv12_tex)
  152. blog(LOG_INFO, "NV12 texture support enabled");
  153. else
  154. blog(LOG_INFO, "NV12 texture support not available");
  155. if (video->using_p010_tex)
  156. blog(LOG_INFO, "P010 texture support enabled");
  157. else
  158. blog(LOG_INFO, "P010 texture support not available");
  159. video->convert_textures[0] = NULL;
  160. video->convert_textures[1] = NULL;
  161. video->convert_textures[2] = NULL;
  162. #ifdef _WIN32
  163. video->convert_textures_encode[0] = NULL;
  164. video->convert_textures_encode[1] = NULL;
  165. video->convert_textures_encode[2] = NULL;
  166. if (video->using_nv12_tex) {
  167. if (!gs_texture_create_nv12(&video->convert_textures_encode[0],
  168. &video->convert_textures_encode[1],
  169. info->width, info->height,
  170. GS_RENDER_TARGET |
  171. GS_SHARED_KM_TEX)) {
  172. return false;
  173. }
  174. } else if (video->using_p010_tex) {
  175. if (!gs_texture_create_p010(&video->convert_textures_encode[0],
  176. &video->convert_textures_encode[1],
  177. info->width, info->height,
  178. GS_RENDER_TARGET |
  179. GS_SHARED_KM_TEX)) {
  180. return false;
  181. }
  182. }
  183. #endif
  184. bool success = true;
  185. switch (info->format) {
  186. case VIDEO_FORMAT_I420:
  187. video->convert_textures[0] =
  188. gs_texture_create(info->width, info->height, GS_R8, 1,
  189. NULL, GS_RENDER_TARGET);
  190. video->convert_textures[1] =
  191. gs_texture_create(info->width / 2, info->height / 2,
  192. GS_R8, 1, NULL, GS_RENDER_TARGET);
  193. video->convert_textures[2] =
  194. gs_texture_create(info->width / 2, info->height / 2,
  195. GS_R8, 1, NULL, GS_RENDER_TARGET);
  196. if (!video->convert_textures[0] ||
  197. !video->convert_textures[1] || !video->convert_textures[2])
  198. success = false;
  199. break;
  200. case VIDEO_FORMAT_NV12:
  201. video->convert_textures[0] =
  202. gs_texture_create(info->width, info->height, GS_R8, 1,
  203. NULL, GS_RENDER_TARGET);
  204. video->convert_textures[1] =
  205. gs_texture_create(info->width / 2, info->height / 2,
  206. GS_R8G8, 1, NULL, GS_RENDER_TARGET);
  207. if (!video->convert_textures[0] || !video->convert_textures[1])
  208. success = false;
  209. break;
  210. case VIDEO_FORMAT_I444:
  211. video->convert_textures[0] =
  212. gs_texture_create(info->width, info->height, GS_R8, 1,
  213. NULL, GS_RENDER_TARGET);
  214. video->convert_textures[1] =
  215. gs_texture_create(info->width, info->height, GS_R8, 1,
  216. NULL, GS_RENDER_TARGET);
  217. video->convert_textures[2] =
  218. gs_texture_create(info->width, info->height, GS_R8, 1,
  219. NULL, GS_RENDER_TARGET);
  220. if (!video->convert_textures[0] ||
  221. !video->convert_textures[1] || !video->convert_textures[2])
  222. success = false;
  223. break;
  224. case VIDEO_FORMAT_I010:
  225. video->convert_textures[0] =
  226. gs_texture_create(info->width, info->height, GS_R16, 1,
  227. NULL, GS_RENDER_TARGET);
  228. video->convert_textures[1] =
  229. gs_texture_create(info->width / 2, info->height / 2,
  230. GS_R16, 1, NULL, GS_RENDER_TARGET);
  231. video->convert_textures[2] =
  232. gs_texture_create(info->width / 2, info->height / 2,
  233. GS_R16, 1, NULL, GS_RENDER_TARGET);
  234. if (!video->convert_textures[0] ||
  235. !video->convert_textures[1] || !video->convert_textures[2])
  236. success = false;
  237. break;
  238. case VIDEO_FORMAT_P010:
  239. video->convert_textures[0] =
  240. gs_texture_create(info->width, info->height, GS_R16, 1,
  241. NULL, GS_RENDER_TARGET);
  242. video->convert_textures[1] =
  243. gs_texture_create(info->width / 2, info->height / 2,
  244. GS_RG16, 1, NULL, GS_RENDER_TARGET);
  245. if (!video->convert_textures[0] || !video->convert_textures[1])
  246. success = false;
  247. break;
  248. case VIDEO_FORMAT_P216:
  249. video->convert_textures[0] =
  250. gs_texture_create(info->width, info->height, GS_R16, 1,
  251. NULL, GS_RENDER_TARGET);
  252. video->convert_textures[1] =
  253. gs_texture_create(info->width / 2, info->height,
  254. GS_RG16, 1, NULL, GS_RENDER_TARGET);
  255. if (!video->convert_textures[0] || !video->convert_textures[1])
  256. success = false;
  257. break;
  258. case VIDEO_FORMAT_P416:
  259. video->convert_textures[0] =
  260. gs_texture_create(info->width, info->height, GS_R16, 1,
  261. NULL, GS_RENDER_TARGET);
  262. video->convert_textures[1] =
  263. gs_texture_create(info->width, info->height, GS_RG16, 1,
  264. NULL, GS_RENDER_TARGET);
  265. if (!video->convert_textures[0] || !video->convert_textures[1])
  266. success = false;
  267. break;
  268. default:
  269. break;
  270. }
  271. if (!success) {
  272. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  273. if (video->convert_textures[c]) {
  274. gs_texture_destroy(video->convert_textures[c]);
  275. video->convert_textures[c] = NULL;
  276. }
  277. #ifdef _WIN32
  278. if (video->convert_textures_encode[c]) {
  279. gs_texture_destroy(
  280. video->convert_textures_encode[c]);
  281. video->convert_textures_encode[c] = NULL;
  282. }
  283. #endif
  284. }
  285. }
  286. return success;
  287. }
  288. static bool obs_init_gpu_copy_surfaces(struct obs_core_video_mix *video,
  289. size_t i)
  290. {
  291. const struct video_output_info *info =
  292. video_output_get_info(video->video);
  293. switch (info->format) {
  294. case VIDEO_FORMAT_I420:
  295. video->copy_surfaces[i][0] = gs_stagesurface_create(
  296. info->width, info->height, GS_R8);
  297. if (!video->copy_surfaces[i][0])
  298. return false;
  299. video->copy_surfaces[i][1] = gs_stagesurface_create(
  300. info->width / 2, info->height / 2, GS_R8);
  301. if (!video->copy_surfaces[i][1])
  302. return false;
  303. video->copy_surfaces[i][2] = gs_stagesurface_create(
  304. info->width / 2, info->height / 2, GS_R8);
  305. if (!video->copy_surfaces[i][2])
  306. return false;
  307. break;
  308. case VIDEO_FORMAT_NV12:
  309. video->copy_surfaces[i][0] = gs_stagesurface_create(
  310. info->width, info->height, GS_R8);
  311. if (!video->copy_surfaces[i][0])
  312. return false;
  313. video->copy_surfaces[i][1] = gs_stagesurface_create(
  314. info->width / 2, info->height / 2, GS_R8G8);
  315. if (!video->copy_surfaces[i][1])
  316. return false;
  317. break;
  318. case VIDEO_FORMAT_I444:
  319. video->copy_surfaces[i][0] = gs_stagesurface_create(
  320. info->width, info->height, GS_R8);
  321. if (!video->copy_surfaces[i][0])
  322. return false;
  323. video->copy_surfaces[i][1] = gs_stagesurface_create(
  324. info->width, info->height, GS_R8);
  325. if (!video->copy_surfaces[i][1])
  326. return false;
  327. video->copy_surfaces[i][2] = gs_stagesurface_create(
  328. info->width, info->height, GS_R8);
  329. if (!video->copy_surfaces[i][2])
  330. return false;
  331. break;
  332. case VIDEO_FORMAT_I010:
  333. video->copy_surfaces[i][0] = gs_stagesurface_create(
  334. info->width, info->height, GS_R16);
  335. if (!video->copy_surfaces[i][0])
  336. return false;
  337. video->copy_surfaces[i][1] = gs_stagesurface_create(
  338. info->width / 2, info->height / 2, GS_R16);
  339. if (!video->copy_surfaces[i][1])
  340. return false;
  341. video->copy_surfaces[i][2] = gs_stagesurface_create(
  342. info->width / 2, info->height / 2, GS_R16);
  343. if (!video->copy_surfaces[i][2])
  344. return false;
  345. break;
  346. case VIDEO_FORMAT_P010:
  347. video->copy_surfaces[i][0] = gs_stagesurface_create(
  348. info->width, info->height, GS_R16);
  349. if (!video->copy_surfaces[i][0])
  350. return false;
  351. video->copy_surfaces[i][1] = gs_stagesurface_create(
  352. info->width / 2, info->height / 2, GS_RG16);
  353. if (!video->copy_surfaces[i][1])
  354. return false;
  355. break;
  356. case VIDEO_FORMAT_P216:
  357. video->copy_surfaces[i][0] = gs_stagesurface_create(
  358. info->width, info->height, GS_R16);
  359. if (!video->copy_surfaces[i][0])
  360. return false;
  361. video->copy_surfaces[i][1] = gs_stagesurface_create(
  362. info->width / 2, info->height, GS_RG16);
  363. if (!video->copy_surfaces[i][1])
  364. return false;
  365. break;
  366. case VIDEO_FORMAT_P416:
  367. video->copy_surfaces[i][0] = gs_stagesurface_create(
  368. info->width, info->height, GS_R16);
  369. if (!video->copy_surfaces[i][0])
  370. return false;
  371. video->copy_surfaces[i][1] = gs_stagesurface_create(
  372. info->width, info->height, GS_RG16);
  373. if (!video->copy_surfaces[i][1])
  374. return false;
  375. break;
  376. default:
  377. break;
  378. }
  379. return true;
  380. }
  381. static bool obs_init_textures(struct obs_core_video_mix *video)
  382. {
  383. const struct video_output_info *info =
  384. video_output_get_info(video->video);
  385. bool success = true;
  386. enum gs_color_format format = GS_BGRA;
  387. switch (info->format) {
  388. case VIDEO_FORMAT_I010:
  389. case VIDEO_FORMAT_P010:
  390. case VIDEO_FORMAT_I210:
  391. case VIDEO_FORMAT_I412:
  392. case VIDEO_FORMAT_YA2L:
  393. case VIDEO_FORMAT_P216:
  394. case VIDEO_FORMAT_P416:
  395. format = GS_RGBA16F;
  396. break;
  397. default:
  398. break;
  399. }
  400. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  401. #ifdef _WIN32
  402. if (video->using_nv12_tex) {
  403. video->copy_surfaces_encode[i] =
  404. gs_stagesurface_create_nv12(info->width,
  405. info->height);
  406. if (!video->copy_surfaces_encode[i]) {
  407. success = false;
  408. break;
  409. }
  410. } else if (video->using_p010_tex) {
  411. video->copy_surfaces_encode[i] =
  412. gs_stagesurface_create_p010(info->width,
  413. info->height);
  414. if (!video->copy_surfaces_encode[i]) {
  415. success = false;
  416. break;
  417. }
  418. }
  419. #endif
  420. if (video->gpu_conversion) {
  421. if (!obs_init_gpu_copy_surfaces(video, i)) {
  422. success = false;
  423. break;
  424. }
  425. } else {
  426. video->copy_surfaces[i][0] = gs_stagesurface_create(
  427. info->width, info->height, format);
  428. if (!video->copy_surfaces[i][0]) {
  429. success = false;
  430. break;
  431. }
  432. }
  433. }
  434. enum gs_color_space space = GS_CS_SRGB;
  435. switch (info->colorspace) {
  436. case VIDEO_CS_2100_PQ:
  437. case VIDEO_CS_2100_HLG:
  438. space = GS_CS_709_EXTENDED;
  439. break;
  440. default:
  441. switch (info->format) {
  442. case VIDEO_FORMAT_I010:
  443. case VIDEO_FORMAT_P010:
  444. case VIDEO_FORMAT_P216:
  445. case VIDEO_FORMAT_P416:
  446. space = GS_CS_SRGB_16F;
  447. break;
  448. default:
  449. space = GS_CS_SRGB;
  450. break;
  451. }
  452. break;
  453. }
  454. video->render_texture =
  455. gs_texture_create(video->ovi.base_width, video->ovi.base_height,
  456. format, 1, NULL, GS_RENDER_TARGET);
  457. if (!video->render_texture)
  458. success = false;
  459. video->output_texture = gs_texture_create(
  460. info->width, info->height, format, 1, NULL, GS_RENDER_TARGET);
  461. if (!video->output_texture)
  462. success = false;
  463. if (success) {
  464. video->render_space = space;
  465. } else {
  466. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  467. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  468. if (video->copy_surfaces[i][c]) {
  469. gs_stagesurface_destroy(
  470. video->copy_surfaces[i][c]);
  471. video->copy_surfaces[i][c] = NULL;
  472. }
  473. }
  474. #ifdef _WIN32
  475. if (video->copy_surfaces_encode[i]) {
  476. gs_stagesurface_destroy(
  477. video->copy_surfaces_encode[i]);
  478. video->copy_surfaces_encode[i] = NULL;
  479. }
  480. #endif
  481. }
  482. if (video->render_texture) {
  483. gs_texture_destroy(video->render_texture);
  484. video->render_texture = NULL;
  485. }
  486. if (video->output_texture) {
  487. gs_texture_destroy(video->output_texture);
  488. video->output_texture = NULL;
  489. }
  490. }
  491. return success;
  492. }
  493. gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file)
  494. {
  495. if (!*effect) {
  496. char *filename = obs_find_data_file(file);
  497. *effect = gs_effect_create_from_file(filename, NULL);
  498. bfree(filename);
  499. }
  500. return *effect;
  501. }
  502. static int obs_init_graphics(struct obs_video_info *ovi)
  503. {
  504. struct obs_core_video *video = &obs->video;
  505. uint8_t transparent_tex_data[2 * 2 * 4] = {0};
  506. const uint8_t *transparent_tex = transparent_tex_data;
  507. struct gs_sampler_info point_sampler = {0};
  508. bool success = true;
  509. int errorcode;
  510. errorcode =
  511. gs_create(&video->graphics, ovi->graphics_module, ovi->adapter);
  512. if (errorcode != GS_SUCCESS) {
  513. switch (errorcode) {
  514. case GS_ERROR_MODULE_NOT_FOUND:
  515. return OBS_VIDEO_MODULE_NOT_FOUND;
  516. case GS_ERROR_NOT_SUPPORTED:
  517. return OBS_VIDEO_NOT_SUPPORTED;
  518. default:
  519. return OBS_VIDEO_FAIL;
  520. }
  521. }
  522. gs_enter_context(video->graphics);
  523. char *filename = obs_find_data_file("default.effect");
  524. video->default_effect = gs_effect_create_from_file(filename, NULL);
  525. bfree(filename);
  526. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  527. filename = obs_find_data_file("default_rect.effect");
  528. video->default_rect_effect =
  529. gs_effect_create_from_file(filename, NULL);
  530. bfree(filename);
  531. }
  532. filename = obs_find_data_file("opaque.effect");
  533. video->opaque_effect = gs_effect_create_from_file(filename, NULL);
  534. bfree(filename);
  535. filename = obs_find_data_file("solid.effect");
  536. video->solid_effect = gs_effect_create_from_file(filename, NULL);
  537. bfree(filename);
  538. filename = obs_find_data_file("repeat.effect");
  539. video->repeat_effect = gs_effect_create_from_file(filename, NULL);
  540. bfree(filename);
  541. filename = obs_find_data_file("format_conversion.effect");
  542. video->conversion_effect = gs_effect_create_from_file(filename, NULL);
  543. bfree(filename);
  544. filename = obs_find_data_file("bicubic_scale.effect");
  545. video->bicubic_effect = gs_effect_create_from_file(filename, NULL);
  546. bfree(filename);
  547. filename = obs_find_data_file("lanczos_scale.effect");
  548. video->lanczos_effect = gs_effect_create_from_file(filename, NULL);
  549. bfree(filename);
  550. filename = obs_find_data_file("area.effect");
  551. video->area_effect = gs_effect_create_from_file(filename, NULL);
  552. bfree(filename);
  553. filename = obs_find_data_file("bilinear_lowres_scale.effect");
  554. video->bilinear_lowres_effect =
  555. gs_effect_create_from_file(filename, NULL);
  556. bfree(filename);
  557. filename = obs_find_data_file("premultiplied_alpha.effect");
  558. video->premultiplied_alpha_effect =
  559. gs_effect_create_from_file(filename, NULL);
  560. bfree(filename);
  561. point_sampler.max_anisotropy = 1;
  562. video->point_sampler = gs_samplerstate_create(&point_sampler);
  563. obs->video.transparent_texture =
  564. gs_texture_create(2, 2, GS_RGBA, 1, &transparent_tex, 0);
  565. if (!video->default_effect)
  566. success = false;
  567. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  568. if (!video->default_rect_effect)
  569. success = false;
  570. }
  571. if (!video->opaque_effect)
  572. success = false;
  573. if (!video->solid_effect)
  574. success = false;
  575. if (!video->conversion_effect)
  576. success = false;
  577. if (!video->premultiplied_alpha_effect)
  578. success = false;
  579. if (!video->transparent_texture)
  580. success = false;
  581. if (!video->point_sampler)
  582. success = false;
  583. gs_leave_context();
  584. return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
  585. }
  586. static inline void set_video_matrix(struct obs_core_video_mix *video,
  587. struct video_output_info *info)
  588. {
  589. struct matrix4 mat;
  590. struct vec4 r_row;
  591. if (format_is_yuv(info->format)) {
  592. video_format_get_parameters_for_format(
  593. info->colorspace, info->range, info->format,
  594. (float *)&mat, NULL, NULL);
  595. matrix4_inv(&mat, &mat);
  596. /* swap R and G */
  597. r_row = mat.x;
  598. mat.x = mat.y;
  599. mat.y = r_row;
  600. } else {
  601. matrix4_identity(&mat);
  602. }
  603. memcpy(video->color_matrix, &mat, sizeof(float) * 16);
  604. }
  605. static int obs_init_video_mix(struct obs_video_info *ovi,
  606. struct obs_core_video_mix *video)
  607. {
  608. struct video_output_info vi;
  609. pthread_mutex_init_value(&video->gpu_encoder_mutex);
  610. make_video_info(&vi, ovi);
  611. video->ovi = *ovi;
  612. /* main view graphics thread drives all frame output,
  613. * so share FPS settings for aux views */
  614. pthread_mutex_lock(&obs->video.mixes_mutex);
  615. size_t num = obs->video.mixes.num;
  616. if (num && obs->video.main_mix) {
  617. struct obs_video_info main_ovi = obs->video.main_mix->ovi;
  618. video->ovi.fps_num = main_ovi.fps_num;
  619. video->ovi.fps_den = main_ovi.fps_den;
  620. }
  621. pthread_mutex_unlock(&obs->video.mixes_mutex);
  622. video->gpu_conversion = ovi->gpu_conversion;
  623. video->gpu_was_active = false;
  624. video->raw_was_active = false;
  625. video->was_active = false;
  626. set_video_matrix(video, &vi);
  627. int errorcode = video_output_open(&video->video, &vi);
  628. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  629. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
  630. blog(LOG_ERROR, "Invalid video parameters specified");
  631. return OBS_VIDEO_INVALID_PARAM;
  632. } else {
  633. blog(LOG_ERROR, "Could not open video output");
  634. }
  635. return OBS_VIDEO_FAIL;
  636. }
  637. if (pthread_mutex_init(&video->gpu_encoder_mutex, NULL) < 0)
  638. return OBS_VIDEO_FAIL;
  639. gs_enter_context(obs->video.graphics);
  640. if (video->gpu_conversion && !obs_init_gpu_conversion(video))
  641. return OBS_VIDEO_FAIL;
  642. if (!obs_init_textures(video))
  643. return OBS_VIDEO_FAIL;
  644. gs_leave_context();
  645. return OBS_VIDEO_SUCCESS;
  646. }
  647. struct obs_core_video_mix *obs_create_video_mix(struct obs_video_info *ovi)
  648. {
  649. struct obs_core_video_mix *video =
  650. bzalloc(sizeof(struct obs_core_video_mix));
  651. if (obs_init_video_mix(ovi, video) != OBS_VIDEO_SUCCESS) {
  652. bfree(video);
  653. video = NULL;
  654. }
  655. return video;
  656. }
  657. static int obs_init_video(struct obs_video_info *ovi)
  658. {
  659. struct obs_core_video *video = &obs->video;
  660. video->video_frame_interval_ns =
  661. util_mul_div64(1000000000ULL, ovi->fps_den, ovi->fps_num);
  662. video->video_half_frame_interval_ns =
  663. util_mul_div64(500000000ULL, ovi->fps_den, ovi->fps_num);
  664. if (pthread_mutex_init(&video->task_mutex, NULL) < 0)
  665. return OBS_VIDEO_FAIL;
  666. if (pthread_mutex_init(&video->mixes_mutex, NULL) < 0)
  667. return OBS_VIDEO_FAIL;
  668. if (!obs_view_add2(&obs->data.main_view, ovi))
  669. return OBS_VIDEO_FAIL;
  670. int errorcode;
  671. #ifdef __APPLE__
  672. pthread_attr_t attr;
  673. pthread_attr_init(&attr);
  674. pthread_attr_set_qos_class_np(&attr, QOS_CLASS_USER_INTERACTIVE, 0);
  675. errorcode = pthread_create(&video->video_thread, &attr,
  676. obs_graphics_thread_autorelease, obs);
  677. #else
  678. errorcode = pthread_create(&video->video_thread, NULL,
  679. obs_graphics_thread, obs);
  680. #endif
  681. if (errorcode != 0)
  682. return OBS_VIDEO_FAIL;
  683. video->thread_initialized = true;
  684. return OBS_VIDEO_SUCCESS;
  685. }
  686. static void stop_video(void)
  687. {
  688. pthread_mutex_lock(&obs->video.mixes_mutex);
  689. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++)
  690. video_output_stop(obs->video.mixes.array[i]->video);
  691. pthread_mutex_unlock(&obs->video.mixes_mutex);
  692. struct obs_core_video *video = &obs->video;
  693. void *thread_retval;
  694. if (video->thread_initialized) {
  695. pthread_join(video->video_thread, &thread_retval);
  696. video->thread_initialized = false;
  697. }
  698. }
  699. static void obs_free_render_textures(struct obs_core_video_mix *video)
  700. {
  701. if (!obs->video.graphics)
  702. return;
  703. gs_enter_context(obs->video.graphics);
  704. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  705. if (video->mapped_surfaces[c]) {
  706. gs_stagesurface_unmap(video->mapped_surfaces[c]);
  707. video->mapped_surfaces[c] = NULL;
  708. }
  709. }
  710. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  711. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  712. if (video->copy_surfaces[i][c]) {
  713. gs_stagesurface_destroy(
  714. video->copy_surfaces[i][c]);
  715. video->copy_surfaces[i][c] = NULL;
  716. }
  717. video->active_copy_surfaces[i][c] = NULL;
  718. }
  719. #ifdef _WIN32
  720. if (video->copy_surfaces_encode[i]) {
  721. gs_stagesurface_destroy(video->copy_surfaces_encode[i]);
  722. video->copy_surfaces_encode[i] = NULL;
  723. }
  724. #endif
  725. }
  726. gs_texture_destroy(video->render_texture);
  727. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  728. if (video->convert_textures[c]) {
  729. gs_texture_destroy(video->convert_textures[c]);
  730. video->convert_textures[c] = NULL;
  731. }
  732. #ifdef _WIN32
  733. if (video->convert_textures_encode[c]) {
  734. gs_texture_destroy(video->convert_textures_encode[c]);
  735. video->convert_textures_encode[c] = NULL;
  736. }
  737. #endif
  738. }
  739. gs_texture_destroy(video->output_texture);
  740. video->render_texture = NULL;
  741. video->output_texture = NULL;
  742. gs_leave_context();
  743. }
  744. void obs_free_video_mix(struct obs_core_video_mix *video)
  745. {
  746. if (video->video) {
  747. video_output_close(video->video);
  748. video->video = NULL;
  749. obs_free_render_textures(video);
  750. circlebuf_free(&video->vframe_info_buffer);
  751. circlebuf_free(&video->vframe_info_buffer_gpu);
  752. video->texture_rendered = false;
  753. memset(video->textures_copied, 0,
  754. sizeof(video->textures_copied));
  755. video->texture_converted = false;
  756. pthread_mutex_destroy(&video->gpu_encoder_mutex);
  757. pthread_mutex_init_value(&video->gpu_encoder_mutex);
  758. da_free(video->gpu_encoders);
  759. video->gpu_encoder_active = 0;
  760. video->cur_texture = 0;
  761. }
  762. bfree(video);
  763. }
  764. static void obs_free_video(void)
  765. {
  766. pthread_mutex_lock(&obs->video.mixes_mutex);
  767. size_t num_views = 0;
  768. for (size_t i = 0; i < obs->video.mixes.num; i++) {
  769. struct obs_core_video_mix *video = obs->video.mixes.array[i];
  770. if (video && video->view)
  771. num_views++;
  772. obs_free_video_mix(video);
  773. obs->video.mixes.array[i] = NULL;
  774. }
  775. if (num_views > 0)
  776. blog(LOG_WARNING, "Number of remaining views: %ld", num_views);
  777. pthread_mutex_unlock(&obs->video.mixes_mutex);
  778. pthread_mutex_destroy(&obs->video.mixes_mutex);
  779. pthread_mutex_init_value(&obs->video.mixes_mutex);
  780. da_free(obs->video.mixes);
  781. pthread_mutex_destroy(&obs->video.task_mutex);
  782. pthread_mutex_init_value(&obs->video.task_mutex);
  783. circlebuf_free(&obs->video.tasks);
  784. }
  785. static void obs_free_graphics(void)
  786. {
  787. struct obs_core_video *video = &obs->video;
  788. if (video->graphics) {
  789. gs_enter_context(video->graphics);
  790. gs_texture_destroy(video->transparent_texture);
  791. gs_samplerstate_destroy(video->point_sampler);
  792. gs_effect_destroy(video->default_effect);
  793. gs_effect_destroy(video->default_rect_effect);
  794. gs_effect_destroy(video->opaque_effect);
  795. gs_effect_destroy(video->solid_effect);
  796. gs_effect_destroy(video->conversion_effect);
  797. gs_effect_destroy(video->bicubic_effect);
  798. gs_effect_destroy(video->repeat_effect);
  799. gs_effect_destroy(video->lanczos_effect);
  800. gs_effect_destroy(video->area_effect);
  801. gs_effect_destroy(video->bilinear_lowres_effect);
  802. video->default_effect = NULL;
  803. gs_leave_context();
  804. gs_destroy(video->graphics);
  805. video->graphics = NULL;
  806. }
  807. }
  808. static void set_audio_thread(void *unused);
  809. static bool obs_init_audio(struct audio_output_info *ai)
  810. {
  811. struct obs_core_audio *audio = &obs->audio;
  812. int errorcode;
  813. pthread_mutex_init_value(&audio->monitoring_mutex);
  814. if (pthread_mutex_init_recursive(&audio->monitoring_mutex) != 0)
  815. return false;
  816. if (pthread_mutex_init(&audio->task_mutex, NULL) != 0)
  817. return false;
  818. struct obs_task_info audio_init = {.task = set_audio_thread};
  819. circlebuf_push_back(&audio->tasks, &audio_init, sizeof(audio_init));
  820. audio->monitoring_device_name = bstrdup("Default");
  821. audio->monitoring_device_id = bstrdup("default");
  822. errorcode = audio_output_open(&audio->audio, ai);
  823. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  824. return true;
  825. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  826. blog(LOG_ERROR, "Invalid audio parameters specified");
  827. else
  828. blog(LOG_ERROR, "Could not open audio output");
  829. return false;
  830. }
  831. static void stop_audio(void)
  832. {
  833. struct obs_core_audio *audio = &obs->audio;
  834. if (audio->audio) {
  835. audio_output_close(audio->audio);
  836. audio->audio = NULL;
  837. }
  838. }
  839. static void obs_free_audio(void)
  840. {
  841. struct obs_core_audio *audio = &obs->audio;
  842. if (audio->audio)
  843. audio_output_close(audio->audio);
  844. circlebuf_free(&audio->buffered_timestamps);
  845. da_free(audio->render_order);
  846. da_free(audio->root_nodes);
  847. da_free(audio->monitors);
  848. bfree(audio->monitoring_device_name);
  849. bfree(audio->monitoring_device_id);
  850. circlebuf_free(&audio->tasks);
  851. pthread_mutex_destroy(&audio->task_mutex);
  852. pthread_mutex_destroy(&audio->monitoring_mutex);
  853. memset(audio, 0, sizeof(struct obs_core_audio));
  854. }
  855. static bool obs_init_data(void)
  856. {
  857. struct obs_core_data *data = &obs->data;
  858. assert(data != NULL);
  859. pthread_mutex_init_value(&obs->data.displays_mutex);
  860. pthread_mutex_init_value(&obs->data.draw_callbacks_mutex);
  861. if (pthread_mutex_init_recursive(&data->sources_mutex) != 0)
  862. goto fail;
  863. if (pthread_mutex_init_recursive(&data->audio_sources_mutex) != 0)
  864. goto fail;
  865. if (pthread_mutex_init_recursive(&data->displays_mutex) != 0)
  866. goto fail;
  867. if (pthread_mutex_init_recursive(&data->outputs_mutex) != 0)
  868. goto fail;
  869. if (pthread_mutex_init_recursive(&data->encoders_mutex) != 0)
  870. goto fail;
  871. if (pthread_mutex_init_recursive(&data->services_mutex) != 0)
  872. goto fail;
  873. if (pthread_mutex_init_recursive(&obs->data.draw_callbacks_mutex) != 0)
  874. goto fail;
  875. if (!obs_view_init(&data->main_view))
  876. goto fail;
  877. data->sources = NULL;
  878. data->public_sources = NULL;
  879. data->private_data = obs_data_create();
  880. data->valid = true;
  881. fail:
  882. return data->valid;
  883. }
  884. void obs_main_view_free(struct obs_view *view)
  885. {
  886. if (!view)
  887. return;
  888. for (size_t i = 0; i < MAX_CHANNELS; i++)
  889. obs_source_release(view->channels[i]);
  890. memset(view->channels, 0, sizeof(view->channels));
  891. pthread_mutex_destroy(&view->channels_mutex);
  892. }
  893. #define FREE_OBS_HASH_TABLE(handle, table, type) \
  894. do { \
  895. struct obs_context_data *ctx, *tmp; \
  896. int unfreed = 0; \
  897. HASH_ITER (handle, *(struct obs_context_data **)table, ctx, \
  898. tmp) { \
  899. obs_##type##_destroy((obs_##type##_t *)ctx); \
  900. unfreed++; \
  901. } \
  902. if (unfreed) \
  903. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  904. unfreed); \
  905. } while (false)
  906. #define FREE_OBS_LINKED_LIST(type) \
  907. do { \
  908. int unfreed = 0; \
  909. while (data->first_##type) { \
  910. obs_##type##_destroy(data->first_##type); \
  911. unfreed++; \
  912. } \
  913. if (unfreed) \
  914. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  915. unfreed); \
  916. } while (false)
  917. static void obs_free_data(void)
  918. {
  919. struct obs_core_data *data = &obs->data;
  920. data->valid = false;
  921. obs_view_remove(&data->main_view);
  922. obs_main_view_free(&data->main_view);
  923. blog(LOG_INFO, "Freeing OBS context data");
  924. FREE_OBS_LINKED_LIST(output);
  925. FREE_OBS_LINKED_LIST(encoder);
  926. FREE_OBS_LINKED_LIST(display);
  927. FREE_OBS_LINKED_LIST(service);
  928. FREE_OBS_HASH_TABLE(hh, &data->public_sources, source);
  929. FREE_OBS_HASH_TABLE(hh_uuid, &data->sources, source);
  930. os_task_queue_wait(obs->destruction_task_thread);
  931. pthread_mutex_destroy(&data->sources_mutex);
  932. pthread_mutex_destroy(&data->audio_sources_mutex);
  933. pthread_mutex_destroy(&data->displays_mutex);
  934. pthread_mutex_destroy(&data->outputs_mutex);
  935. pthread_mutex_destroy(&data->encoders_mutex);
  936. pthread_mutex_destroy(&data->services_mutex);
  937. pthread_mutex_destroy(&data->draw_callbacks_mutex);
  938. da_free(data->draw_callbacks);
  939. da_free(data->rendered_callbacks);
  940. da_free(data->tick_callbacks);
  941. obs_data_release(data->private_data);
  942. for (size_t i = 0; i < data->protocols.num; i++)
  943. bfree(data->protocols.array[i]);
  944. da_free(data->protocols);
  945. da_free(data->sources_to_tick);
  946. }
  947. static const char *obs_signals[] = {
  948. "void source_create(ptr source)",
  949. "void source_destroy(ptr source)",
  950. "void source_remove(ptr source)",
  951. "void source_update(ptr source)",
  952. "void source_save(ptr source)",
  953. "void source_load(ptr source)",
  954. "void source_activate(ptr source)",
  955. "void source_deactivate(ptr source)",
  956. "void source_show(ptr source)",
  957. "void source_hide(ptr source)",
  958. "void source_audio_activate(ptr source)",
  959. "void source_audio_deactivate(ptr source)",
  960. "void source_rename(ptr source, string new_name, string prev_name)",
  961. "void source_volume(ptr source, in out float volume)",
  962. "void source_volume_level(ptr source, float level, float magnitude, "
  963. "float peak)",
  964. "void source_transition_start(ptr source)",
  965. "void source_transition_video_stop(ptr source)",
  966. "void source_transition_stop(ptr source)",
  967. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  968. "void hotkey_layout_change()",
  969. "void hotkey_register(ptr hotkey)",
  970. "void hotkey_unregister(ptr hotkey)",
  971. "void hotkey_bindings_changed(ptr hotkey)",
  972. NULL,
  973. };
  974. static inline bool obs_init_handlers(void)
  975. {
  976. obs->signals = signal_handler_create();
  977. if (!obs->signals)
  978. return false;
  979. obs->procs = proc_handler_create();
  980. if (!obs->procs)
  981. return false;
  982. return signal_handler_add_array(obs->signals, obs_signals);
  983. }
  984. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  985. static inline bool obs_init_hotkeys(void)
  986. {
  987. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  988. bool success = false;
  989. assert(hotkeys != NULL);
  990. hotkeys->hotkeys = NULL;
  991. hotkeys->hotkey_pairs = NULL;
  992. hotkeys->signals = obs->signals;
  993. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  994. hotkeys->mute = bstrdup("Mute");
  995. hotkeys->unmute = bstrdup("Unmute");
  996. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  997. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  998. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  999. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  1000. if (!obs_hotkeys_platform_init(hotkeys))
  1001. return false;
  1002. if (pthread_mutex_init_recursive(&hotkeys->mutex) != 0)
  1003. goto fail;
  1004. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  1005. goto fail;
  1006. if (pthread_create(&hotkeys->hotkey_thread, NULL, obs_hotkey_thread,
  1007. NULL))
  1008. goto fail;
  1009. hotkeys->strict_modifiers = true;
  1010. hotkeys->hotkey_thread_initialized = true;
  1011. success = true;
  1012. fail:
  1013. return success;
  1014. }
  1015. static inline void stop_hotkeys(void)
  1016. {
  1017. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  1018. void *thread_ret;
  1019. if (hotkeys->hotkey_thread_initialized) {
  1020. os_event_signal(hotkeys->stop_event);
  1021. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  1022. hotkeys->hotkey_thread_initialized = false;
  1023. }
  1024. os_event_destroy(hotkeys->stop_event);
  1025. obs_hotkeys_free();
  1026. }
  1027. static inline void obs_free_hotkeys(void)
  1028. {
  1029. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  1030. bfree(hotkeys->mute);
  1031. bfree(hotkeys->unmute);
  1032. bfree(hotkeys->push_to_mute);
  1033. bfree(hotkeys->push_to_talk);
  1034. bfree(hotkeys->sceneitem_show);
  1035. bfree(hotkeys->sceneitem_hide);
  1036. obs_hotkey_name_map_free();
  1037. obs_hotkeys_platform_free(hotkeys);
  1038. pthread_mutex_destroy(&hotkeys->mutex);
  1039. }
  1040. extern const struct obs_source_info scene_info;
  1041. extern const struct obs_source_info group_info;
  1042. static const char *submix_name(void *unused)
  1043. {
  1044. UNUSED_PARAMETER(unused);
  1045. return "Audio line (internal use only)";
  1046. }
  1047. const struct obs_source_info audio_line_info = {
  1048. .id = "audio_line",
  1049. .type = OBS_SOURCE_TYPE_INPUT,
  1050. .output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_DISABLED |
  1051. OBS_SOURCE_SUBMIX,
  1052. .get_name = submix_name,
  1053. };
  1054. extern void log_system_info(void);
  1055. static bool obs_init(const char *locale, const char *module_config_path,
  1056. profiler_name_store_t *store)
  1057. {
  1058. obs = bzalloc(sizeof(struct obs_core));
  1059. pthread_mutex_init_value(&obs->audio.monitoring_mutex);
  1060. pthread_mutex_init_value(&obs->audio.task_mutex);
  1061. pthread_mutex_init_value(&obs->video.task_mutex);
  1062. pthread_mutex_init_value(&obs->video.mixes_mutex);
  1063. obs->name_store_owned = !store;
  1064. obs->name_store = store ? store : profiler_name_store_create();
  1065. if (!obs->name_store) {
  1066. blog(LOG_ERROR, "Couldn't create profiler name store");
  1067. return false;
  1068. }
  1069. log_system_info();
  1070. if (!obs_init_data())
  1071. return false;
  1072. if (!obs_init_handlers())
  1073. return false;
  1074. if (!obs_init_hotkeys())
  1075. return false;
  1076. obs->destruction_task_thread = os_task_queue_create();
  1077. if (!obs->destruction_task_thread)
  1078. return false;
  1079. if (module_config_path)
  1080. obs->module_config_path = bstrdup(module_config_path);
  1081. obs->locale = bstrdup(locale);
  1082. obs_register_source(&scene_info);
  1083. obs_register_source(&group_info);
  1084. obs_register_source(&audio_line_info);
  1085. add_default_module_paths();
  1086. return true;
  1087. }
  1088. #ifdef _WIN32
  1089. extern bool initialize_com(void);
  1090. extern void uninitialize_com(void);
  1091. static bool com_initialized = false;
  1092. #endif
  1093. /* Separate from actual context initialization
  1094. * since this can be set before startup and persist
  1095. * after shutdown. */
  1096. static DARRAY(struct dstr) core_module_paths = {0};
  1097. char *obs_find_data_file(const char *file)
  1098. {
  1099. struct dstr path = {0};
  1100. char *result = find_libobs_data_file(file);
  1101. if (result)
  1102. return result;
  1103. for (size_t i = 0; i < core_module_paths.num; ++i) {
  1104. if (check_path(file, core_module_paths.array[i].array, &path))
  1105. return path.array;
  1106. }
  1107. blog(LOG_ERROR, "Failed to find file '%s' in libobs data directory",
  1108. file);
  1109. dstr_free(&path);
  1110. return NULL;
  1111. }
  1112. void obs_add_data_path(const char *path)
  1113. {
  1114. struct dstr *new_path = da_push_back_new(core_module_paths);
  1115. dstr_init_copy(new_path, path);
  1116. }
  1117. bool obs_remove_data_path(const char *path)
  1118. {
  1119. for (size_t i = 0; i < core_module_paths.num; ++i) {
  1120. int result = dstr_cmp(&core_module_paths.array[i], path);
  1121. if (result == 0) {
  1122. dstr_free(&core_module_paths.array[i]);
  1123. da_erase(core_module_paths, i);
  1124. return true;
  1125. }
  1126. }
  1127. return false;
  1128. }
  1129. static const char *obs_startup_name = "obs_startup";
  1130. bool obs_startup(const char *locale, const char *module_config_path,
  1131. profiler_name_store_t *store)
  1132. {
  1133. bool success;
  1134. profile_start(obs_startup_name);
  1135. if (obs) {
  1136. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  1137. return false;
  1138. }
  1139. #ifdef _WIN32
  1140. com_initialized = initialize_com();
  1141. #endif
  1142. success = obs_init(locale, module_config_path, store);
  1143. profile_end(obs_startup_name);
  1144. if (!success)
  1145. obs_shutdown();
  1146. return success;
  1147. }
  1148. static struct obs_cmdline_args cmdline_args = {0, NULL};
  1149. void obs_set_cmdline_args(int argc, const char *const *argv)
  1150. {
  1151. char *data;
  1152. size_t len;
  1153. int i;
  1154. /* Once argc is set (non-zero) we shouldn't call again */
  1155. if (cmdline_args.argc)
  1156. return;
  1157. cmdline_args.argc = argc;
  1158. /* Safely copy over argv */
  1159. len = 0;
  1160. for (i = 0; i < argc; i++)
  1161. len += strlen(argv[i]) + 1;
  1162. cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
  1163. data = (char *)cmdline_args.argv + sizeof(char *) * (argc + 1);
  1164. for (i = 0; i < argc; i++) {
  1165. cmdline_args.argv[i] = data;
  1166. len = strlen(argv[i]) + 1;
  1167. memcpy(data, argv[i], len);
  1168. data += len;
  1169. }
  1170. cmdline_args.argv[argc] = NULL;
  1171. }
  1172. struct obs_cmdline_args obs_get_cmdline_args(void)
  1173. {
  1174. return cmdline_args;
  1175. }
  1176. void obs_shutdown(void)
  1177. {
  1178. struct obs_module *module;
  1179. obs_wait_for_destroy_queue();
  1180. for (size_t i = 0; i < obs->source_types.num; i++) {
  1181. struct obs_source_info *item = &obs->source_types.array[i];
  1182. if (item->type_data && item->free_type_data)
  1183. item->free_type_data(item->type_data);
  1184. if (item->id)
  1185. bfree((void *)item->id);
  1186. }
  1187. da_free(obs->source_types);
  1188. #define FREE_REGISTERED_TYPES(structure, list) \
  1189. do { \
  1190. for (size_t i = 0; i < list.num; i++) { \
  1191. struct structure *item = &list.array[i]; \
  1192. if (item->type_data && item->free_type_data) \
  1193. item->free_type_data(item->type_data); \
  1194. } \
  1195. da_free(list); \
  1196. } while (false)
  1197. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  1198. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  1199. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  1200. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  1201. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  1202. #undef FREE_REGISTERED_TYPES
  1203. da_free(obs->input_types);
  1204. da_free(obs->filter_types);
  1205. da_free(obs->transition_types);
  1206. stop_video();
  1207. stop_audio();
  1208. stop_hotkeys();
  1209. module = obs->first_module;
  1210. while (module) {
  1211. struct obs_module *next = module->next;
  1212. free_module(module);
  1213. module = next;
  1214. }
  1215. obs->first_module = NULL;
  1216. obs_free_data();
  1217. obs_free_audio();
  1218. obs_free_video();
  1219. os_task_queue_destroy(obs->destruction_task_thread);
  1220. obs_free_hotkeys();
  1221. obs_free_graphics();
  1222. proc_handler_destroy(obs->procs);
  1223. signal_handler_destroy(obs->signals);
  1224. obs->procs = NULL;
  1225. obs->signals = NULL;
  1226. for (size_t i = 0; i < obs->module_paths.num; i++)
  1227. free_module_path(obs->module_paths.array + i);
  1228. da_free(obs->module_paths);
  1229. for (size_t i = 0; i < obs->safe_modules.num; i++)
  1230. bfree(obs->safe_modules.array[i]);
  1231. da_free(obs->safe_modules);
  1232. if (obs->name_store_owned)
  1233. profiler_name_store_free(obs->name_store);
  1234. bfree(obs->module_config_path);
  1235. bfree(obs->locale);
  1236. bfree(obs);
  1237. obs = NULL;
  1238. bfree(cmdline_args.argv);
  1239. #ifdef _WIN32
  1240. if (com_initialized)
  1241. uninitialize_com();
  1242. #endif
  1243. }
  1244. bool obs_initialized(void)
  1245. {
  1246. return obs != NULL;
  1247. }
  1248. uint32_t obs_get_version(void)
  1249. {
  1250. return LIBOBS_API_VER;
  1251. }
  1252. const char *obs_get_version_string(void)
  1253. {
  1254. return OBS_VERSION;
  1255. }
  1256. void obs_set_locale(const char *locale)
  1257. {
  1258. struct obs_module *module;
  1259. if (obs->locale)
  1260. bfree(obs->locale);
  1261. obs->locale = bstrdup(locale);
  1262. module = obs->first_module;
  1263. while (module) {
  1264. if (module->set_locale)
  1265. module->set_locale(locale);
  1266. module = module->next;
  1267. }
  1268. }
  1269. const char *obs_get_locale(void)
  1270. {
  1271. return obs->locale;
  1272. }
  1273. #define OBS_SIZE_MIN 2
  1274. #define OBS_SIZE_MAX (32 * 1024)
  1275. static inline bool size_valid(uint32_t width, uint32_t height)
  1276. {
  1277. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  1278. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  1279. }
  1280. int obs_reset_video(struct obs_video_info *ovi)
  1281. {
  1282. if (!obs)
  1283. return OBS_VIDEO_FAIL;
  1284. /* don't allow changing of video settings if active. */
  1285. if (obs_video_active())
  1286. return OBS_VIDEO_CURRENTLY_ACTIVE;
  1287. if (!size_valid(ovi->output_width, ovi->output_height) ||
  1288. !size_valid(ovi->base_width, ovi->base_height))
  1289. return OBS_VIDEO_INVALID_PARAM;
  1290. stop_video();
  1291. obs_free_video();
  1292. /* align to multiple-of-two and SSE alignment sizes */
  1293. ovi->output_width &= 0xFFFFFFFC;
  1294. ovi->output_height &= 0xFFFFFFFE;
  1295. if (!obs->video.graphics) {
  1296. int errorcode = obs_init_graphics(ovi);
  1297. if (errorcode != OBS_VIDEO_SUCCESS) {
  1298. obs_free_graphics();
  1299. return errorcode;
  1300. }
  1301. }
  1302. const char *scale_type_name = "";
  1303. switch (ovi->scale_type) {
  1304. case OBS_SCALE_DISABLE:
  1305. scale_type_name = "Disabled";
  1306. break;
  1307. case OBS_SCALE_POINT:
  1308. scale_type_name = "Point";
  1309. break;
  1310. case OBS_SCALE_BICUBIC:
  1311. scale_type_name = "Bicubic";
  1312. break;
  1313. case OBS_SCALE_BILINEAR:
  1314. scale_type_name = "Bilinear";
  1315. break;
  1316. case OBS_SCALE_LANCZOS:
  1317. scale_type_name = "Lanczos";
  1318. break;
  1319. case OBS_SCALE_AREA:
  1320. scale_type_name = "Area";
  1321. break;
  1322. }
  1323. bool yuv = format_is_yuv(ovi->output_format);
  1324. const char *yuv_format = get_video_colorspace_name(ovi->colorspace);
  1325. const char *yuv_range =
  1326. get_video_range_name(ovi->output_format, ovi->range);
  1327. blog(LOG_INFO, "---------------------------------");
  1328. blog(LOG_INFO,
  1329. "video settings reset:\n"
  1330. "\tbase resolution: %dx%d\n"
  1331. "\toutput resolution: %dx%d\n"
  1332. "\tdownscale filter: %s\n"
  1333. "\tfps: %d/%d\n"
  1334. "\tformat: %s\n"
  1335. "\tYUV mode: %s%s%s",
  1336. ovi->base_width, ovi->base_height, ovi->output_width,
  1337. ovi->output_height, scale_type_name, ovi->fps_num, ovi->fps_den,
  1338. get_video_format_name(ovi->output_format),
  1339. yuv ? yuv_format : "None", yuv ? "/" : "", yuv ? yuv_range : "");
  1340. return obs_init_video(ovi);
  1341. }
  1342. #ifndef SEC_TO_MSEC
  1343. #define SEC_TO_MSEC 1000
  1344. #endif
  1345. bool obs_reset_audio2(const struct obs_audio_info2 *oai)
  1346. {
  1347. struct obs_core_audio *audio = &obs->audio;
  1348. struct audio_output_info ai;
  1349. /* don't allow changing of audio settings if active. */
  1350. if (!obs || (audio->audio && audio_output_active(audio->audio)))
  1351. return false;
  1352. obs_free_audio();
  1353. if (!oai)
  1354. return true;
  1355. if (oai->max_buffering_ms) {
  1356. uint32_t max_frames = oai->max_buffering_ms *
  1357. oai->samples_per_sec / SEC_TO_MSEC;
  1358. max_frames += (AUDIO_OUTPUT_FRAMES - 1);
  1359. audio->max_buffering_ticks = max_frames / AUDIO_OUTPUT_FRAMES;
  1360. } else {
  1361. audio->max_buffering_ticks = 45;
  1362. }
  1363. audio->fixed_buffer = oai->fixed_buffering;
  1364. int max_buffering_ms = audio->max_buffering_ticks *
  1365. AUDIO_OUTPUT_FRAMES * SEC_TO_MSEC /
  1366. (int)oai->samples_per_sec;
  1367. ai.name = "Audio";
  1368. ai.samples_per_sec = oai->samples_per_sec;
  1369. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  1370. ai.speakers = oai->speakers;
  1371. ai.input_callback = audio_callback;
  1372. blog(LOG_INFO, "---------------------------------");
  1373. blog(LOG_INFO,
  1374. "audio settings reset:\n"
  1375. "\tsamples per sec: %d\n"
  1376. "\tspeakers: %d\n"
  1377. "\tmax buffering: %d milliseconds\n"
  1378. "\tbuffering type: %s",
  1379. (int)ai.samples_per_sec, (int)ai.speakers, max_buffering_ms,
  1380. oai->fixed_buffering ? "fixed" : "dynamically increasing");
  1381. return obs_init_audio(&ai);
  1382. }
  1383. bool obs_reset_audio(const struct obs_audio_info *oai)
  1384. {
  1385. struct obs_audio_info2 oai2 = {
  1386. .samples_per_sec = oai->samples_per_sec,
  1387. .speakers = oai->speakers,
  1388. };
  1389. return obs_reset_audio2(&oai2);
  1390. }
  1391. bool obs_get_video_info(struct obs_video_info *ovi)
  1392. {
  1393. if (!obs->video.graphics || !obs->video.main_mix)
  1394. return false;
  1395. *ovi = obs->video.main_mix->ovi;
  1396. return true;
  1397. }
  1398. float obs_get_video_sdr_white_level(void)
  1399. {
  1400. struct obs_core_video *video = &obs->video;
  1401. return video->graphics ? video->sdr_white_level : 300.f;
  1402. }
  1403. float obs_get_video_hdr_nominal_peak_level(void)
  1404. {
  1405. struct obs_core_video *video = &obs->video;
  1406. return video->graphics ? video->hdr_nominal_peak_level : 1000.f;
  1407. }
  1408. void obs_set_video_levels(float sdr_white_level, float hdr_nominal_peak_level)
  1409. {
  1410. struct obs_core_video *video = &obs->video;
  1411. assert(video->graphics);
  1412. video->sdr_white_level = sdr_white_level;
  1413. video->hdr_nominal_peak_level = hdr_nominal_peak_level;
  1414. }
  1415. bool obs_get_audio_info(struct obs_audio_info *oai)
  1416. {
  1417. struct obs_core_audio *audio = &obs->audio;
  1418. const struct audio_output_info *info;
  1419. if (!oai || !audio->audio)
  1420. return false;
  1421. info = audio_output_get_info(audio->audio);
  1422. oai->samples_per_sec = info->samples_per_sec;
  1423. oai->speakers = info->speakers;
  1424. return true;
  1425. }
  1426. bool obs_enum_source_types(size_t idx, const char **id)
  1427. {
  1428. if (idx >= obs->source_types.num)
  1429. return false;
  1430. *id = obs->source_types.array[idx].id;
  1431. return true;
  1432. }
  1433. bool obs_enum_input_types(size_t idx, const char **id)
  1434. {
  1435. if (idx >= obs->input_types.num)
  1436. return false;
  1437. *id = obs->input_types.array[idx].id;
  1438. return true;
  1439. }
  1440. bool obs_enum_input_types2(size_t idx, const char **id,
  1441. const char **unversioned_id)
  1442. {
  1443. if (idx >= obs->input_types.num)
  1444. return false;
  1445. if (id)
  1446. *id = obs->input_types.array[idx].id;
  1447. if (unversioned_id)
  1448. *unversioned_id = obs->input_types.array[idx].unversioned_id;
  1449. return true;
  1450. }
  1451. const char *obs_get_latest_input_type_id(const char *unversioned_id)
  1452. {
  1453. struct obs_source_info *latest = NULL;
  1454. int version = -1;
  1455. if (!unversioned_id)
  1456. return NULL;
  1457. for (size_t i = 0; i < obs->source_types.num; i++) {
  1458. struct obs_source_info *info = &obs->source_types.array[i];
  1459. if (strcmp(info->unversioned_id, unversioned_id) == 0 &&
  1460. (int)info->version > version) {
  1461. latest = info;
  1462. version = info->version;
  1463. }
  1464. }
  1465. assert(!!latest);
  1466. if (!latest)
  1467. return NULL;
  1468. return latest->id;
  1469. }
  1470. bool obs_enum_filter_types(size_t idx, const char **id)
  1471. {
  1472. if (idx >= obs->filter_types.num)
  1473. return false;
  1474. *id = obs->filter_types.array[idx].id;
  1475. return true;
  1476. }
  1477. bool obs_enum_transition_types(size_t idx, const char **id)
  1478. {
  1479. if (idx >= obs->transition_types.num)
  1480. return false;
  1481. *id = obs->transition_types.array[idx].id;
  1482. return true;
  1483. }
  1484. bool obs_enum_output_types(size_t idx, const char **id)
  1485. {
  1486. if (idx >= obs->output_types.num)
  1487. return false;
  1488. *id = obs->output_types.array[idx].id;
  1489. return true;
  1490. }
  1491. bool obs_enum_encoder_types(size_t idx, const char **id)
  1492. {
  1493. if (idx >= obs->encoder_types.num)
  1494. return false;
  1495. *id = obs->encoder_types.array[idx].id;
  1496. return true;
  1497. }
  1498. bool obs_enum_service_types(size_t idx, const char **id)
  1499. {
  1500. if (idx >= obs->service_types.num)
  1501. return false;
  1502. *id = obs->service_types.array[idx].id;
  1503. return true;
  1504. }
  1505. void obs_enter_graphics(void)
  1506. {
  1507. if (obs->video.graphics)
  1508. gs_enter_context(obs->video.graphics);
  1509. }
  1510. void obs_leave_graphics(void)
  1511. {
  1512. if (obs->video.graphics)
  1513. gs_leave_context();
  1514. }
  1515. audio_t *obs_get_audio(void)
  1516. {
  1517. return obs->audio.audio;
  1518. }
  1519. video_t *obs_get_video(void)
  1520. {
  1521. return obs->video.main_mix->video;
  1522. }
  1523. /* TODO: optimize this later so it's not just O(N) string lookups */
  1524. static inline struct obs_modal_ui *
  1525. get_modal_ui_callback(const char *id, const char *task, const char *target)
  1526. {
  1527. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  1528. struct obs_modal_ui *callback =
  1529. obs->modal_ui_callbacks.array + i;
  1530. if (strcmp(callback->id, id) == 0 &&
  1531. strcmp(callback->task, task) == 0 &&
  1532. strcmp(callback->target, target) == 0)
  1533. return callback;
  1534. }
  1535. return NULL;
  1536. }
  1537. static inline struct obs_modeless_ui *
  1538. get_modeless_ui_callback(const char *id, const char *task, const char *target)
  1539. {
  1540. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  1541. struct obs_modeless_ui *callback;
  1542. callback = obs->modeless_ui_callbacks.array + i;
  1543. if (strcmp(callback->id, id) == 0 &&
  1544. strcmp(callback->task, task) == 0 &&
  1545. strcmp(callback->target, target) == 0)
  1546. return callback;
  1547. }
  1548. return NULL;
  1549. }
  1550. int obs_exec_ui(const char *name, const char *task, const char *target,
  1551. void *data, void *ui_data)
  1552. {
  1553. struct obs_modal_ui *callback;
  1554. int errorcode = OBS_UI_NOTFOUND;
  1555. if (!obs)
  1556. return errorcode;
  1557. callback = get_modal_ui_callback(name, task, target);
  1558. if (callback) {
  1559. bool success = callback->exec(data, ui_data);
  1560. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  1561. }
  1562. return errorcode;
  1563. }
  1564. void *obs_create_ui(const char *name, const char *task, const char *target,
  1565. void *data, void *ui_data)
  1566. {
  1567. struct obs_modeless_ui *callback;
  1568. callback = get_modeless_ui_callback(name, task, target);
  1569. return callback ? callback->create(data, ui_data) : NULL;
  1570. }
  1571. obs_source_t *obs_get_output_source(uint32_t channel)
  1572. {
  1573. return obs_view_get_source(&obs->data.main_view, channel);
  1574. }
  1575. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  1576. {
  1577. assert(channel < MAX_CHANNELS);
  1578. if (channel >= MAX_CHANNELS)
  1579. return;
  1580. struct obs_source *prev_source;
  1581. struct obs_view *view = &obs->data.main_view;
  1582. struct calldata params = {0};
  1583. pthread_mutex_lock(&view->channels_mutex);
  1584. source = obs_source_get_ref(source);
  1585. prev_source = view->channels[channel];
  1586. calldata_set_int(&params, "channel", channel);
  1587. calldata_set_ptr(&params, "prev_source", prev_source);
  1588. calldata_set_ptr(&params, "source", source);
  1589. signal_handler_signal(obs->signals, "channel_change", &params);
  1590. calldata_get_ptr(&params, "source", &source);
  1591. calldata_free(&params);
  1592. view->channels[channel] = source;
  1593. pthread_mutex_unlock(&view->channels_mutex);
  1594. if (source)
  1595. obs_source_activate(source, MAIN_VIEW);
  1596. if (prev_source) {
  1597. obs_source_deactivate(prev_source, MAIN_VIEW);
  1598. obs_source_release(prev_source);
  1599. }
  1600. }
  1601. void obs_enum_sources(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1602. {
  1603. obs_source_t *source;
  1604. pthread_mutex_lock(&obs->data.sources_mutex);
  1605. source = obs->data.public_sources;
  1606. while (source) {
  1607. obs_source_t *s = obs_source_get_ref(source);
  1608. if (s) {
  1609. if (s->info.type == OBS_SOURCE_TYPE_INPUT &&
  1610. !enum_proc(param, s)) {
  1611. obs_source_release(s);
  1612. break;
  1613. } else if (strcmp(s->info.id, group_info.id) == 0 &&
  1614. !enum_proc(param, s)) {
  1615. obs_source_release(s);
  1616. break;
  1617. }
  1618. obs_source_release(s);
  1619. }
  1620. source = (obs_source_t *)source->context.hh.next;
  1621. }
  1622. pthread_mutex_unlock(&obs->data.sources_mutex);
  1623. }
  1624. void obs_enum_scenes(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1625. {
  1626. obs_source_t *source;
  1627. pthread_mutex_lock(&obs->data.sources_mutex);
  1628. source = obs->data.public_sources;
  1629. while (source) {
  1630. obs_source_t *s = obs_source_get_ref(source);
  1631. if (s) {
  1632. if (source->info.type == OBS_SOURCE_TYPE_SCENE &&
  1633. !enum_proc(param, s)) {
  1634. obs_source_release(s);
  1635. break;
  1636. }
  1637. obs_source_release(s);
  1638. }
  1639. source = (obs_source_t *)source->context.hh.next;
  1640. }
  1641. pthread_mutex_unlock(&obs->data.sources_mutex);
  1642. }
  1643. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  1644. void *param)
  1645. {
  1646. struct obs_context_data **start = pstart, *context;
  1647. bool (*enum_proc)(void *, void *) = proc;
  1648. assert(start);
  1649. assert(mutex);
  1650. assert(enum_proc);
  1651. pthread_mutex_lock(mutex);
  1652. context = *start;
  1653. while (context) {
  1654. if (!enum_proc(param, context))
  1655. break;
  1656. context = context->next;
  1657. }
  1658. pthread_mutex_unlock(mutex);
  1659. }
  1660. static inline void obs_enum_uuid(void *pstart, pthread_mutex_t *mutex,
  1661. void *proc, void *param)
  1662. {
  1663. struct obs_context_data **start = pstart, *context, *tmp;
  1664. bool (*enum_proc)(void *, void *) = proc;
  1665. assert(start);
  1666. assert(mutex);
  1667. assert(enum_proc);
  1668. pthread_mutex_lock(mutex);
  1669. HASH_ITER (hh_uuid, *start, context, tmp) {
  1670. if (!enum_proc(param, context))
  1671. break;
  1672. }
  1673. pthread_mutex_unlock(mutex);
  1674. }
  1675. void obs_enum_all_sources(bool (*enum_proc)(void *, obs_source_t *),
  1676. void *param)
  1677. {
  1678. obs_enum_uuid(&obs->data.sources, &obs->data.sources_mutex, enum_proc,
  1679. param);
  1680. }
  1681. void obs_enum_outputs(bool (*enum_proc)(void *, obs_output_t *), void *param)
  1682. {
  1683. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex, enum_proc,
  1684. param);
  1685. }
  1686. void obs_enum_encoders(bool (*enum_proc)(void *, obs_encoder_t *), void *param)
  1687. {
  1688. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex, enum_proc,
  1689. param);
  1690. }
  1691. void obs_enum_services(bool (*enum_proc)(void *, obs_service_t *), void *param)
  1692. {
  1693. obs_enum(&obs->data.first_service, &obs->data.services_mutex, enum_proc,
  1694. param);
  1695. }
  1696. static inline void *get_context_by_name(void *vfirst, const char *name,
  1697. pthread_mutex_t *mutex,
  1698. void *(*addref)(void *))
  1699. {
  1700. struct obs_context_data **first = vfirst;
  1701. struct obs_context_data *context;
  1702. pthread_mutex_lock(mutex);
  1703. /* If context list head has a hash table, look the name up in there */
  1704. if (*first && (*first)->hh.tbl) {
  1705. HASH_FIND_STR(*first, name, context);
  1706. } else {
  1707. context = *first;
  1708. while (context) {
  1709. if (!context->private &&
  1710. strcmp(context->name, name) == 0) {
  1711. break;
  1712. }
  1713. context = context->next;
  1714. }
  1715. }
  1716. if (context)
  1717. addref(context);
  1718. pthread_mutex_unlock(mutex);
  1719. return context;
  1720. }
  1721. static void *get_context_by_uuid(void *ptable, const char *uuid,
  1722. pthread_mutex_t *mutex,
  1723. void *(*addref)(void *))
  1724. {
  1725. struct obs_context_data **ht = ptable;
  1726. struct obs_context_data *context;
  1727. pthread_mutex_lock(mutex);
  1728. HASH_FIND_UUID(*ht, uuid, context);
  1729. if (context)
  1730. addref(context);
  1731. pthread_mutex_unlock(mutex);
  1732. return context;
  1733. }
  1734. static inline void *obs_source_addref_safe_(void *ref)
  1735. {
  1736. return obs_source_get_ref(ref);
  1737. }
  1738. static inline void *obs_output_addref_safe_(void *ref)
  1739. {
  1740. return obs_output_get_ref(ref);
  1741. }
  1742. static inline void *obs_encoder_addref_safe_(void *ref)
  1743. {
  1744. return obs_encoder_get_ref(ref);
  1745. }
  1746. static inline void *obs_service_addref_safe_(void *ref)
  1747. {
  1748. return obs_service_get_ref(ref);
  1749. }
  1750. static inline void *obs_id_(void *data)
  1751. {
  1752. return data;
  1753. }
  1754. obs_source_t *obs_get_source_by_name(const char *name)
  1755. {
  1756. return get_context_by_name(&obs->data.public_sources, name,
  1757. &obs->data.sources_mutex,
  1758. obs_source_addref_safe_);
  1759. }
  1760. obs_source_t *obs_get_source_by_uuid(const char *uuid)
  1761. {
  1762. return get_context_by_uuid(&obs->data.sources, uuid,
  1763. &obs->data.sources_mutex,
  1764. obs_source_addref_safe_);
  1765. }
  1766. obs_source_t *obs_get_transition_by_name(const char *name)
  1767. {
  1768. struct obs_source **first = &obs->data.sources;
  1769. struct obs_source *source;
  1770. pthread_mutex_lock(&obs->data.sources_mutex);
  1771. /* Transitions are private but can be found via this method, so we
  1772. * can't look them up by name in the public_sources hash table. */
  1773. source = *first;
  1774. while (source) {
  1775. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION &&
  1776. strcmp(source->context.name, name) == 0) {
  1777. source = obs_source_addref_safe_(source);
  1778. break;
  1779. }
  1780. source = (void *)source->context.hh_uuid.next;
  1781. }
  1782. pthread_mutex_unlock(&obs->data.sources_mutex);
  1783. return source;
  1784. }
  1785. obs_source_t *obs_get_transition_by_uuid(const char *uuid)
  1786. {
  1787. obs_source_t *source = obs_get_source_by_uuid(uuid);
  1788. if (source && source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1789. return source;
  1790. else if (source)
  1791. obs_source_release(source);
  1792. return NULL;
  1793. }
  1794. obs_output_t *obs_get_output_by_name(const char *name)
  1795. {
  1796. return get_context_by_name(&obs->data.first_output, name,
  1797. &obs->data.outputs_mutex,
  1798. obs_output_addref_safe_);
  1799. }
  1800. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1801. {
  1802. return get_context_by_name(&obs->data.first_encoder, name,
  1803. &obs->data.encoders_mutex,
  1804. obs_encoder_addref_safe_);
  1805. }
  1806. obs_service_t *obs_get_service_by_name(const char *name)
  1807. {
  1808. return get_context_by_name(&obs->data.first_service, name,
  1809. &obs->data.services_mutex,
  1810. obs_service_addref_safe_);
  1811. }
  1812. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1813. {
  1814. switch (effect) {
  1815. case OBS_EFFECT_DEFAULT:
  1816. return obs->video.default_effect;
  1817. case OBS_EFFECT_DEFAULT_RECT:
  1818. return obs->video.default_rect_effect;
  1819. case OBS_EFFECT_OPAQUE:
  1820. return obs->video.opaque_effect;
  1821. case OBS_EFFECT_SOLID:
  1822. return obs->video.solid_effect;
  1823. case OBS_EFFECT_REPEAT:
  1824. return obs->video.repeat_effect;
  1825. case OBS_EFFECT_BICUBIC:
  1826. return obs->video.bicubic_effect;
  1827. case OBS_EFFECT_LANCZOS:
  1828. return obs->video.lanczos_effect;
  1829. case OBS_EFFECT_AREA:
  1830. return obs->video.area_effect;
  1831. case OBS_EFFECT_BILINEAR_LOWRES:
  1832. return obs->video.bilinear_lowres_effect;
  1833. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1834. return obs->video.premultiplied_alpha_effect;
  1835. }
  1836. return NULL;
  1837. }
  1838. /* OBS_DEPRECATED */
  1839. gs_effect_t *obs_get_default_rect_effect(void)
  1840. {
  1841. return obs->video.default_rect_effect;
  1842. }
  1843. signal_handler_t *obs_get_signal_handler(void)
  1844. {
  1845. return obs->signals;
  1846. }
  1847. proc_handler_t *obs_get_proc_handler(void)
  1848. {
  1849. return obs->procs;
  1850. }
  1851. /* OBS_DEPRECATED */
  1852. void obs_render_main_view(void)
  1853. {
  1854. obs_view_render(&obs->data.main_view);
  1855. }
  1856. static void obs_render_main_texture_internal(enum gs_blend_type src_c,
  1857. enum gs_blend_type dest_c,
  1858. enum gs_blend_type src_a,
  1859. enum gs_blend_type dest_a)
  1860. {
  1861. struct obs_core_video_mix *video;
  1862. gs_texture_t *tex;
  1863. gs_effect_t *effect;
  1864. gs_eparam_t *param;
  1865. video = obs->video.main_mix;
  1866. if (!video->texture_rendered)
  1867. return;
  1868. const enum gs_color_space source_space = video->render_space;
  1869. const enum gs_color_space current_space = gs_get_color_space();
  1870. const char *tech_name = "Draw";
  1871. float multiplier = 1.f;
  1872. switch (current_space) {
  1873. case GS_CS_SRGB:
  1874. case GS_CS_SRGB_16F:
  1875. if (source_space == GS_CS_709_EXTENDED)
  1876. tech_name = "DrawTonemap";
  1877. break;
  1878. case GS_CS_709_SCRGB:
  1879. tech_name = "DrawMultiply";
  1880. multiplier = obs_get_video_sdr_white_level() / 80.f;
  1881. break;
  1882. case GS_CS_709_EXTENDED:
  1883. break;
  1884. }
  1885. const bool previous = gs_framebuffer_srgb_enabled();
  1886. gs_enable_framebuffer_srgb(true);
  1887. tex = video->render_texture;
  1888. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1889. param = gs_effect_get_param_by_name(effect, "image");
  1890. gs_effect_set_texture_srgb(param, tex);
  1891. param = gs_effect_get_param_by_name(effect, "multiplier");
  1892. gs_effect_set_float(param, multiplier);
  1893. gs_blend_state_push();
  1894. gs_blend_function_separate(src_c, dest_c, src_a, dest_a);
  1895. while (gs_effect_loop(effect, tech_name))
  1896. gs_draw_sprite(tex, 0, 0, 0);
  1897. gs_blend_state_pop();
  1898. gs_enable_framebuffer_srgb(previous);
  1899. }
  1900. void obs_render_main_texture(void)
  1901. {
  1902. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA,
  1903. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1904. }
  1905. void obs_render_main_texture_src_color_only(void)
  1906. {
  1907. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_ZERO,
  1908. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1909. }
  1910. gs_texture_t *obs_get_main_texture(void)
  1911. {
  1912. struct obs_core_video_mix *video;
  1913. video = obs->video.main_mix;
  1914. if (!video->texture_rendered)
  1915. return NULL;
  1916. return video->render_texture;
  1917. }
  1918. void obs_set_master_volume(float volume)
  1919. {
  1920. UNUSED_PARAMETER(volume);
  1921. }
  1922. float obs_get_master_volume(void)
  1923. {
  1924. return 1.f;
  1925. }
  1926. static obs_source_t *obs_load_source_type(obs_data_t *source_data,
  1927. bool is_private)
  1928. {
  1929. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1930. obs_source_t *source;
  1931. const char *name = obs_data_get_string(source_data, "name");
  1932. const char *uuid = obs_data_get_string(source_data, "uuid");
  1933. const char *id = obs_data_get_string(source_data, "id");
  1934. const char *v_id = obs_data_get_string(source_data, "versioned_id");
  1935. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1936. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1937. double volume;
  1938. double balance;
  1939. int64_t sync;
  1940. uint32_t prev_ver;
  1941. uint32_t caps;
  1942. uint32_t flags;
  1943. uint32_t mixers;
  1944. int di_order;
  1945. int di_mode;
  1946. int monitoring_type;
  1947. prev_ver = (uint32_t)obs_data_get_int(source_data, "prev_ver");
  1948. if (!*v_id)
  1949. v_id = id;
  1950. source = obs_source_create_set_last_ver(v_id, name, uuid, settings,
  1951. hotkeys, prev_ver, is_private);
  1952. if (source->owns_info_id) {
  1953. bfree((void *)source->info.unversioned_id);
  1954. source->info.unversioned_id = bstrdup(id);
  1955. }
  1956. obs_data_release(hotkeys);
  1957. caps = obs_source_get_output_flags(source);
  1958. obs_data_set_default_double(source_data, "volume", 1.0);
  1959. volume = obs_data_get_double(source_data, "volume");
  1960. obs_source_set_volume(source, (float)volume);
  1961. obs_data_set_default_double(source_data, "balance", 0.5);
  1962. balance = obs_data_get_double(source_data, "balance");
  1963. obs_source_set_balance_value(source, (float)balance);
  1964. sync = obs_data_get_int(source_data, "sync");
  1965. obs_source_set_sync_offset(source, sync);
  1966. obs_data_set_default_int(source_data, "mixers", 0x3F);
  1967. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1968. obs_source_set_audio_mixers(source, mixers);
  1969. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1970. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1971. obs_source_set_flags(source, flags);
  1972. obs_data_set_default_bool(source_data, "enabled", true);
  1973. obs_source_set_enabled(source,
  1974. obs_data_get_bool(source_data, "enabled"));
  1975. obs_data_set_default_bool(source_data, "muted", false);
  1976. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1977. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1978. obs_source_enable_push_to_mute(
  1979. source, obs_data_get_bool(source_data, "push-to-mute"));
  1980. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1981. obs_source_set_push_to_mute_delay(
  1982. source, obs_data_get_int(source_data, "push-to-mute-delay"));
  1983. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1984. obs_source_enable_push_to_talk(
  1985. source, obs_data_get_bool(source_data, "push-to-talk"));
  1986. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1987. obs_source_set_push_to_talk_delay(
  1988. source, obs_data_get_int(source_data, "push-to-talk-delay"));
  1989. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1990. obs_source_set_deinterlace_mode(source,
  1991. (enum obs_deinterlace_mode)di_mode);
  1992. di_order =
  1993. (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1994. obs_source_set_deinterlace_field_order(
  1995. source, (enum obs_deinterlace_field_order)di_order);
  1996. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1997. if (prev_ver < MAKE_SEMANTIC_VERSION(23, 2, 2)) {
  1998. if ((caps & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  1999. /* updates older sources to enable monitoring
  2000. * automatically if they added monitoring by default in
  2001. * version 24 */
  2002. monitoring_type = OBS_MONITORING_TYPE_MONITOR_ONLY;
  2003. obs_source_set_audio_mixers(source, 0x3F);
  2004. }
  2005. }
  2006. obs_source_set_monitoring_type(
  2007. source, (enum obs_monitoring_type)monitoring_type);
  2008. obs_data_release(source->private_settings);
  2009. source->private_settings =
  2010. obs_data_get_obj(source_data, "private_settings");
  2011. if (!source->private_settings)
  2012. source->private_settings = obs_data_create();
  2013. if (filters) {
  2014. size_t count = obs_data_array_count(filters);
  2015. for (size_t i = 0; i < count; i++) {
  2016. obs_data_t *filter_data =
  2017. obs_data_array_item(filters, i);
  2018. obs_source_t *filter =
  2019. obs_load_source_type(filter_data, true);
  2020. if (filter) {
  2021. obs_source_filter_add(source, filter);
  2022. obs_source_release(filter);
  2023. }
  2024. obs_data_release(filter_data);
  2025. }
  2026. obs_data_array_release(filters);
  2027. }
  2028. obs_data_release(settings);
  2029. return source;
  2030. }
  2031. obs_source_t *obs_load_source(obs_data_t *source_data)
  2032. {
  2033. return obs_load_source_type(source_data, false);
  2034. }
  2035. obs_source_t *obs_load_private_source(obs_data_t *source_data)
  2036. {
  2037. return obs_load_source_type(source_data, true);
  2038. }
  2039. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  2040. void *private_data)
  2041. {
  2042. struct obs_core_data *data = &obs->data;
  2043. DARRAY(obs_source_t *) sources;
  2044. size_t count;
  2045. size_t i;
  2046. da_init(sources);
  2047. count = obs_data_array_count(array);
  2048. da_reserve(sources, count);
  2049. pthread_mutex_lock(&data->sources_mutex);
  2050. for (i = 0; i < count; i++) {
  2051. obs_data_t *source_data = obs_data_array_item(array, i);
  2052. obs_source_t *source = obs_load_source(source_data);
  2053. da_push_back(sources, &source);
  2054. obs_data_release(source_data);
  2055. }
  2056. /* tell sources that we want to load */
  2057. for (i = 0; i < sources.num; i++) {
  2058. obs_source_t *source = sources.array[i];
  2059. obs_data_t *source_data = obs_data_array_item(array, i);
  2060. if (source) {
  2061. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  2062. obs_transition_load(source, source_data);
  2063. obs_source_load2(source);
  2064. if (cb)
  2065. cb(private_data, source);
  2066. }
  2067. obs_data_release(source_data);
  2068. }
  2069. for (i = 0; i < sources.num; i++)
  2070. obs_source_release(sources.array[i]);
  2071. pthread_mutex_unlock(&data->sources_mutex);
  2072. da_free(sources);
  2073. }
  2074. obs_data_t *obs_save_source(obs_source_t *source)
  2075. {
  2076. obs_data_array_t *filters = obs_data_array_create();
  2077. obs_data_t *source_data = obs_data_create();
  2078. obs_data_t *settings = obs_source_get_settings(source);
  2079. obs_data_t *hotkey_data = source->context.hotkey_data;
  2080. obs_data_t *hotkeys;
  2081. float volume = obs_source_get_volume(source);
  2082. float balance = obs_source_get_balance_value(source);
  2083. uint32_t mixers = obs_source_get_audio_mixers(source);
  2084. int64_t sync = obs_source_get_sync_offset(source);
  2085. uint32_t flags = obs_source_get_flags(source);
  2086. const char *name = obs_source_get_name(source);
  2087. const char *uuid = obs_source_get_uuid(source);
  2088. const char *id = source->info.unversioned_id;
  2089. const char *v_id = source->info.id;
  2090. bool enabled = obs_source_enabled(source);
  2091. bool muted = obs_source_muted(source);
  2092. bool push_to_mute = obs_source_push_to_mute_enabled(source);
  2093. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  2094. bool push_to_talk = obs_source_push_to_talk_enabled(source);
  2095. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  2096. int m_type = (int)obs_source_get_monitoring_type(source);
  2097. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  2098. int di_order = (int)obs_source_get_deinterlace_field_order(source);
  2099. obs_source_save(source);
  2100. hotkeys = obs_hotkeys_save_source(source);
  2101. if (hotkeys) {
  2102. obs_data_release(hotkey_data);
  2103. source->context.hotkey_data = hotkeys;
  2104. hotkey_data = hotkeys;
  2105. }
  2106. obs_data_set_int(source_data, "prev_ver", LIBOBS_API_VER);
  2107. obs_data_set_string(source_data, "name", name);
  2108. obs_data_set_string(source_data, "uuid", uuid);
  2109. obs_data_set_string(source_data, "id", id);
  2110. obs_data_set_string(source_data, "versioned_id", v_id);
  2111. obs_data_set_obj(source_data, "settings", settings);
  2112. obs_data_set_int(source_data, "mixers", mixers);
  2113. obs_data_set_int(source_data, "sync", sync);
  2114. obs_data_set_int(source_data, "flags", flags);
  2115. obs_data_set_double(source_data, "volume", volume);
  2116. obs_data_set_double(source_data, "balance", balance);
  2117. obs_data_set_bool(source_data, "enabled", enabled);
  2118. obs_data_set_bool(source_data, "muted", muted);
  2119. obs_data_set_bool(source_data, "push-to-mute", push_to_mute);
  2120. obs_data_set_int(source_data, "push-to-mute-delay", ptm_delay);
  2121. obs_data_set_bool(source_data, "push-to-talk", push_to_talk);
  2122. obs_data_set_int(source_data, "push-to-talk-delay", ptt_delay);
  2123. obs_data_set_obj(source_data, "hotkeys", hotkey_data);
  2124. obs_data_set_int(source_data, "deinterlace_mode", di_mode);
  2125. obs_data_set_int(source_data, "deinterlace_field_order", di_order);
  2126. obs_data_set_int(source_data, "monitoring_type", m_type);
  2127. obs_data_set_obj(source_data, "private_settings",
  2128. source->private_settings);
  2129. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  2130. obs_transition_save(source, source_data);
  2131. pthread_mutex_lock(&source->filter_mutex);
  2132. if (source->filters.num) {
  2133. for (size_t i = source->filters.num; i > 0; i--) {
  2134. obs_source_t *filter = source->filters.array[i - 1];
  2135. obs_data_t *filter_data = obs_save_source(filter);
  2136. obs_data_array_push_back(filters, filter_data);
  2137. obs_data_release(filter_data);
  2138. }
  2139. obs_data_set_array(source_data, "filters", filters);
  2140. }
  2141. pthread_mutex_unlock(&source->filter_mutex);
  2142. obs_data_release(settings);
  2143. obs_data_array_release(filters);
  2144. return source_data;
  2145. }
  2146. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  2147. void *data_)
  2148. {
  2149. struct obs_core_data *data = &obs->data;
  2150. obs_data_array_t *array;
  2151. obs_source_t *source;
  2152. array = obs_data_array_create();
  2153. pthread_mutex_lock(&data->sources_mutex);
  2154. source = data->public_sources;
  2155. while (source) {
  2156. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  2157. !source->removed && !source->temp_removed &&
  2158. cb(data_, source)) {
  2159. obs_data_t *source_data = obs_save_source(source);
  2160. obs_data_array_push_back(array, source_data);
  2161. obs_data_release(source_data);
  2162. }
  2163. source = (obs_source_t *)source->context.hh.next;
  2164. }
  2165. pthread_mutex_unlock(&data->sources_mutex);
  2166. return array;
  2167. }
  2168. static bool save_source_filter(void *data, obs_source_t *source)
  2169. {
  2170. UNUSED_PARAMETER(data);
  2171. UNUSED_PARAMETER(source);
  2172. return true;
  2173. }
  2174. obs_data_array_t *obs_save_sources(void)
  2175. {
  2176. return obs_save_sources_filtered(save_source_filter, NULL);
  2177. }
  2178. void obs_reset_source_uuids()
  2179. {
  2180. pthread_mutex_lock(&obs->data.sources_mutex);
  2181. /* Move all sources to a new hash table */
  2182. struct obs_context_data *ht =
  2183. (struct obs_context_data *)obs->data.sources;
  2184. struct obs_context_data *new_ht = NULL;
  2185. struct obs_context_data *ctx, *tmp;
  2186. HASH_ITER (hh_uuid, ht, ctx, tmp) {
  2187. HASH_DELETE(hh_uuid, ht, ctx);
  2188. bfree((void *)ctx->uuid);
  2189. ctx->uuid = os_generate_uuid();
  2190. HASH_ADD_UUID(new_ht, uuid, ctx);
  2191. }
  2192. /* The old table will be automatically freed once the last element has
  2193. * been removed, so we can simply overwrite the pointer. */
  2194. obs->data.sources = (struct obs_source *)new_ht;
  2195. pthread_mutex_unlock(&obs->data.sources_mutex);
  2196. }
  2197. /* ensures that names are never blank */
  2198. static inline char *dup_name(const char *name, bool private)
  2199. {
  2200. if (private && !name)
  2201. return NULL;
  2202. if (!name || !*name) {
  2203. struct dstr unnamed = {0};
  2204. dstr_printf(&unnamed, "__unnamed%04lld",
  2205. obs->data.unnamed_index++);
  2206. return unnamed.array;
  2207. } else {
  2208. return bstrdup(name);
  2209. }
  2210. }
  2211. static inline bool
  2212. obs_context_data_init_wrap(struct obs_context_data *context,
  2213. enum obs_obj_type type, obs_data_t *settings,
  2214. const char *name, const char *uuid,
  2215. obs_data_t *hotkey_data, bool private)
  2216. {
  2217. assert(context);
  2218. memset(context, 0, sizeof(*context));
  2219. context->private = private;
  2220. context->type = type;
  2221. pthread_mutex_init_value(&context->rename_cache_mutex);
  2222. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  2223. return false;
  2224. context->signals = signal_handler_create();
  2225. if (!context->signals)
  2226. return false;
  2227. context->procs = proc_handler_create();
  2228. if (!context->procs)
  2229. return false;
  2230. if (uuid && strlen(uuid) == UUID_STR_LENGTH)
  2231. context->uuid = bstrdup(uuid);
  2232. /* Only automatically generate UUIDs for sources */
  2233. else if (type == OBS_OBJ_TYPE_SOURCE)
  2234. context->uuid = os_generate_uuid();
  2235. context->name = dup_name(name, private);
  2236. context->settings = obs_data_newref(settings);
  2237. context->hotkey_data = obs_data_newref(hotkey_data);
  2238. return true;
  2239. }
  2240. bool obs_context_data_init(struct obs_context_data *context,
  2241. enum obs_obj_type type, obs_data_t *settings,
  2242. const char *name, const char *uuid,
  2243. obs_data_t *hotkey_data, bool private)
  2244. {
  2245. if (obs_context_data_init_wrap(context, type, settings, name, uuid,
  2246. hotkey_data, private)) {
  2247. return true;
  2248. } else {
  2249. obs_context_data_free(context);
  2250. return false;
  2251. }
  2252. }
  2253. void obs_context_data_free(struct obs_context_data *context)
  2254. {
  2255. obs_hotkeys_context_release(context);
  2256. signal_handler_destroy(context->signals);
  2257. proc_handler_destroy(context->procs);
  2258. obs_data_release(context->settings);
  2259. obs_context_data_remove(context);
  2260. pthread_mutex_destroy(&context->rename_cache_mutex);
  2261. bfree(context->name);
  2262. bfree((void *)context->uuid);
  2263. for (size_t i = 0; i < context->rename_cache.num; i++)
  2264. bfree(context->rename_cache.array[i]);
  2265. da_free(context->rename_cache);
  2266. memset(context, 0, sizeof(*context));
  2267. }
  2268. void obs_context_init_control(struct obs_context_data *context, void *object,
  2269. obs_destroy_cb destroy)
  2270. {
  2271. context->control = bzalloc(sizeof(obs_weak_object_t));
  2272. context->control->object = object;
  2273. context->destroy = destroy;
  2274. }
  2275. void obs_context_data_insert(struct obs_context_data *context,
  2276. pthread_mutex_t *mutex, void *pfirst)
  2277. {
  2278. struct obs_context_data **first = pfirst;
  2279. assert(context);
  2280. assert(mutex);
  2281. assert(first);
  2282. context->mutex = mutex;
  2283. pthread_mutex_lock(mutex);
  2284. context->prev_next = first;
  2285. context->next = *first;
  2286. *first = context;
  2287. if (context->next)
  2288. context->next->prev_next = &context->next;
  2289. pthread_mutex_unlock(mutex);
  2290. }
  2291. static inline char *obs_context_deduplicate_name(void *phash, const char *name)
  2292. {
  2293. struct obs_context_data *head = phash;
  2294. struct obs_context_data *item = NULL;
  2295. HASH_FIND_STR(head, name, item);
  2296. if (!item)
  2297. return NULL;
  2298. struct dstr new_name = {0};
  2299. int suffix = 2;
  2300. while (item) {
  2301. dstr_printf(&new_name, "%s %d", name, suffix++);
  2302. HASH_FIND_STR(head, new_name.array, item);
  2303. }
  2304. return new_name.array;
  2305. }
  2306. void obs_context_data_insert_name(struct obs_context_data *context,
  2307. pthread_mutex_t *mutex, void *pfirst)
  2308. {
  2309. struct obs_context_data **first = pfirst;
  2310. char *new_name;
  2311. assert(context);
  2312. assert(mutex);
  2313. assert(first);
  2314. context->mutex = mutex;
  2315. pthread_mutex_lock(mutex);
  2316. /* Ensure name is not a duplicate. */
  2317. new_name = obs_context_deduplicate_name(*first, context->name);
  2318. if (new_name) {
  2319. blog(LOG_WARNING,
  2320. "Attempted to insert context with duplicate name \"%s\"!"
  2321. " Name has been changed to \"%s\"",
  2322. context->name, new_name);
  2323. /* Since this happens before the context creation finishes,
  2324. * do not bother to add it to the rename cache. */
  2325. bfree(context->name);
  2326. context->name = new_name;
  2327. }
  2328. HASH_ADD_STR(*first, name, context);
  2329. pthread_mutex_unlock(mutex);
  2330. }
  2331. void obs_context_data_insert_uuid(struct obs_context_data *context,
  2332. pthread_mutex_t *mutex, void *pfirst_uuid)
  2333. {
  2334. struct obs_context_data **first_uuid = pfirst_uuid;
  2335. struct obs_context_data *item = NULL;
  2336. assert(context);
  2337. assert(mutex);
  2338. assert(first_uuid);
  2339. context->mutex = mutex;
  2340. pthread_mutex_lock(mutex);
  2341. /* Ensure UUID is not a duplicate.
  2342. * This should only ever happen if a scene collection file has been
  2343. * manually edited and an entry has been duplicated without removing
  2344. * or regenerating the UUID. */
  2345. HASH_FIND_UUID(*first_uuid, context->uuid, item);
  2346. if (item) {
  2347. blog(LOG_WARNING,
  2348. "Attempted to insert context with duplicate UUID \"%s\"!",
  2349. context->uuid);
  2350. /* It is practically impossible for the new UUID to be a
  2351. * duplicate, so don't bother checking again. */
  2352. bfree((void *)context->uuid);
  2353. context->uuid = os_generate_uuid();
  2354. }
  2355. HASH_ADD_UUID(*first_uuid, uuid, context);
  2356. pthread_mutex_unlock(mutex);
  2357. }
  2358. void obs_context_data_remove(struct obs_context_data *context)
  2359. {
  2360. if (context && context->prev_next) {
  2361. pthread_mutex_lock(context->mutex);
  2362. *context->prev_next = context->next;
  2363. if (context->next)
  2364. context->next->prev_next = context->prev_next;
  2365. context->prev_next = NULL;
  2366. pthread_mutex_unlock(context->mutex);
  2367. }
  2368. }
  2369. void obs_context_data_remove_name(struct obs_context_data *context, void *phead)
  2370. {
  2371. struct obs_context_data **head = phead;
  2372. assert(head);
  2373. if (!context)
  2374. return;
  2375. pthread_mutex_lock(context->mutex);
  2376. HASH_DELETE(hh, *head, context);
  2377. pthread_mutex_unlock(context->mutex);
  2378. }
  2379. void obs_context_data_remove_uuid(struct obs_context_data *context,
  2380. void *puuid_head)
  2381. {
  2382. struct obs_context_data **uuid_head = puuid_head;
  2383. assert(uuid_head);
  2384. if (!context || !context->uuid || !uuid_head)
  2385. return;
  2386. pthread_mutex_lock(context->mutex);
  2387. HASH_DELETE(hh_uuid, *uuid_head, context);
  2388. pthread_mutex_unlock(context->mutex);
  2389. }
  2390. void obs_context_wait(struct obs_context_data *context)
  2391. {
  2392. pthread_mutex_lock(context->mutex);
  2393. pthread_mutex_unlock(context->mutex);
  2394. }
  2395. void obs_context_data_setname(struct obs_context_data *context,
  2396. const char *name)
  2397. {
  2398. pthread_mutex_lock(&context->rename_cache_mutex);
  2399. if (context->name)
  2400. da_push_back(context->rename_cache, &context->name);
  2401. context->name = dup_name(name, context->private);
  2402. pthread_mutex_unlock(&context->rename_cache_mutex);
  2403. }
  2404. void obs_context_data_setname_ht(struct obs_context_data *context,
  2405. const char *name, void *phead)
  2406. {
  2407. struct obs_context_data **head = phead;
  2408. char *new_name;
  2409. pthread_mutex_lock(context->mutex);
  2410. pthread_mutex_lock(&context->rename_cache_mutex);
  2411. HASH_DEL(*head, context);
  2412. if (context->name)
  2413. da_push_back(context->rename_cache, &context->name);
  2414. /* Ensure new name is not a duplicate. */
  2415. new_name = obs_context_deduplicate_name(*head, name);
  2416. if (new_name) {
  2417. blog(LOG_WARNING,
  2418. "Attempted to rename context to duplicate name \"%s\"!"
  2419. " New name has been changed to \"%s\"",
  2420. context->name, new_name);
  2421. context->name = new_name;
  2422. } else {
  2423. context->name = dup_name(name, context->private);
  2424. }
  2425. HASH_ADD_STR(*head, name, context);
  2426. pthread_mutex_unlock(&context->rename_cache_mutex);
  2427. pthread_mutex_unlock(context->mutex);
  2428. }
  2429. profiler_name_store_t *obs_get_profiler_name_store(void)
  2430. {
  2431. return obs->name_store;
  2432. }
  2433. uint64_t obs_get_video_frame_time(void)
  2434. {
  2435. return obs->video.video_time;
  2436. }
  2437. double obs_get_active_fps(void)
  2438. {
  2439. return obs->video.video_fps;
  2440. }
  2441. uint64_t obs_get_average_frame_time_ns(void)
  2442. {
  2443. return obs->video.video_avg_frame_time_ns;
  2444. }
  2445. uint64_t obs_get_frame_interval_ns(void)
  2446. {
  2447. return obs->video.video_frame_interval_ns;
  2448. }
  2449. enum obs_obj_type obs_obj_get_type(void *obj)
  2450. {
  2451. struct obs_context_data *context = obj;
  2452. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  2453. }
  2454. const char *obs_obj_get_id(void *obj)
  2455. {
  2456. struct obs_context_data *context = obj;
  2457. if (!context)
  2458. return NULL;
  2459. switch (context->type) {
  2460. case OBS_OBJ_TYPE_SOURCE:
  2461. return ((obs_source_t *)obj)->info.id;
  2462. case OBS_OBJ_TYPE_OUTPUT:
  2463. return ((obs_output_t *)obj)->info.id;
  2464. case OBS_OBJ_TYPE_ENCODER:
  2465. return ((obs_encoder_t *)obj)->info.id;
  2466. case OBS_OBJ_TYPE_SERVICE:
  2467. return ((obs_service_t *)obj)->info.id;
  2468. default:;
  2469. }
  2470. return NULL;
  2471. }
  2472. bool obs_obj_invalid(void *obj)
  2473. {
  2474. struct obs_context_data *context = obj;
  2475. if (!context)
  2476. return true;
  2477. return !context->data;
  2478. }
  2479. void *obs_obj_get_data(void *obj)
  2480. {
  2481. struct obs_context_data *context = obj;
  2482. if (!context)
  2483. return NULL;
  2484. return context->data;
  2485. }
  2486. bool obs_obj_is_private(void *obj)
  2487. {
  2488. struct obs_context_data *context = obj;
  2489. if (!context)
  2490. return false;
  2491. return context->private;
  2492. }
  2493. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  2494. {
  2495. if (!name || !id || !*name || !*id)
  2496. return false;
  2497. if (!obs_audio_monitoring_available())
  2498. return false;
  2499. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  2500. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  2501. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  2502. return true;
  2503. }
  2504. bfree(obs->audio.monitoring_device_name);
  2505. bfree(obs->audio.monitoring_device_id);
  2506. obs->audio.monitoring_device_name = bstrdup(name);
  2507. obs->audio.monitoring_device_id = bstrdup(id);
  2508. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  2509. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  2510. audio_monitor_reset(monitor);
  2511. }
  2512. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  2513. return true;
  2514. }
  2515. void obs_get_audio_monitoring_device(const char **name, const char **id)
  2516. {
  2517. if (name)
  2518. *name = obs->audio.monitoring_device_name;
  2519. if (id)
  2520. *id = obs->audio.monitoring_device_id;
  2521. }
  2522. void obs_add_tick_callback(void (*tick)(void *param, float seconds),
  2523. void *param)
  2524. {
  2525. struct tick_callback data = {tick, param};
  2526. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2527. da_insert(obs->data.tick_callbacks, 0, &data);
  2528. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2529. }
  2530. void obs_remove_tick_callback(void (*tick)(void *param, float seconds),
  2531. void *param)
  2532. {
  2533. struct tick_callback data = {tick, param};
  2534. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2535. da_erase_item(obs->data.tick_callbacks, &data);
  2536. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2537. }
  2538. void obs_add_main_render_callback(void (*draw)(void *param, uint32_t cx,
  2539. uint32_t cy),
  2540. void *param)
  2541. {
  2542. struct draw_callback data = {draw, param};
  2543. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2544. da_insert(obs->data.draw_callbacks, 0, &data);
  2545. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2546. }
  2547. void obs_remove_main_render_callback(void (*draw)(void *param, uint32_t cx,
  2548. uint32_t cy),
  2549. void *param)
  2550. {
  2551. struct draw_callback data = {draw, param};
  2552. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2553. da_erase_item(obs->data.draw_callbacks, &data);
  2554. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2555. }
  2556. void obs_add_main_rendered_callback(void (*rendered)(void *param), void *param)
  2557. {
  2558. struct rendered_callback data = {rendered, param};
  2559. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2560. da_insert(obs->data.rendered_callbacks, 0, &data);
  2561. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2562. }
  2563. void obs_remove_main_rendered_callback(void (*rendered)(void *param),
  2564. void *param)
  2565. {
  2566. struct rendered_callback data = {rendered, param};
  2567. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2568. da_erase_item(obs->data.rendered_callbacks, &data);
  2569. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2570. }
  2571. uint32_t obs_get_total_frames(void)
  2572. {
  2573. return obs->video.total_frames;
  2574. }
  2575. uint32_t obs_get_lagged_frames(void)
  2576. {
  2577. return obs->video.lagged_frames;
  2578. }
  2579. struct obs_core_video_mix *get_mix_for_video(video_t *v)
  2580. {
  2581. struct obs_core_video_mix *result = NULL;
  2582. pthread_mutex_lock(&obs->video.mixes_mutex);
  2583. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  2584. struct obs_core_video_mix *mix = obs->video.mixes.array[i];
  2585. if (v == mix->video) {
  2586. result = mix;
  2587. break;
  2588. }
  2589. }
  2590. pthread_mutex_unlock(&obs->video.mixes_mutex);
  2591. return result;
  2592. }
  2593. void start_raw_video(video_t *v, const struct video_scale_info *conversion,
  2594. uint32_t frame_rate_divisor,
  2595. void (*callback)(void *param, struct video_data *frame),
  2596. void *param)
  2597. {
  2598. struct obs_core_video_mix *video = get_mix_for_video(v);
  2599. if (video)
  2600. os_atomic_inc_long(&video->raw_active);
  2601. video_output_connect2(v, conversion, frame_rate_divisor, callback,
  2602. param);
  2603. }
  2604. void stop_raw_video(video_t *v,
  2605. void (*callback)(void *param, struct video_data *frame),
  2606. void *param)
  2607. {
  2608. struct obs_core_video_mix *video = get_mix_for_video(v);
  2609. if (video)
  2610. os_atomic_dec_long(&video->raw_active);
  2611. video_output_disconnect(v, callback, param);
  2612. }
  2613. void obs_add_raw_video_callback(const struct video_scale_info *conversion,
  2614. void (*callback)(void *param,
  2615. struct video_data *frame),
  2616. void *param)
  2617. {
  2618. obs_add_raw_video_callback2(conversion, 1, callback, param);
  2619. }
  2620. void obs_add_raw_video_callback2(
  2621. const struct video_scale_info *conversion, uint32_t frame_rate_divisor,
  2622. void (*callback)(void *param, struct video_data *frame), void *param)
  2623. {
  2624. struct obs_core_video_mix *video = obs->video.main_mix;
  2625. start_raw_video(video->video, conversion, frame_rate_divisor, callback,
  2626. param);
  2627. }
  2628. void obs_remove_raw_video_callback(void (*callback)(void *param,
  2629. struct video_data *frame),
  2630. void *param)
  2631. {
  2632. struct obs_core_video_mix *video = obs->video.main_mix;
  2633. stop_raw_video(video->video, callback, param);
  2634. }
  2635. void obs_add_raw_audio_callback(size_t mix_idx,
  2636. const struct audio_convert_info *conversion,
  2637. audio_output_callback_t callback, void *param)
  2638. {
  2639. struct obs_core_audio *audio = &obs->audio;
  2640. audio_output_connect(audio->audio, mix_idx, conversion, callback,
  2641. param);
  2642. }
  2643. void obs_remove_raw_audio_callback(size_t mix_idx,
  2644. audio_output_callback_t callback,
  2645. void *param)
  2646. {
  2647. struct obs_core_audio *audio = &obs->audio;
  2648. audio_output_disconnect(audio->audio, mix_idx, callback, param);
  2649. }
  2650. void obs_apply_private_data(obs_data_t *settings)
  2651. {
  2652. if (!settings)
  2653. return;
  2654. obs_data_apply(obs->data.private_data, settings);
  2655. }
  2656. void obs_set_private_data(obs_data_t *settings)
  2657. {
  2658. obs_data_clear(obs->data.private_data);
  2659. if (settings)
  2660. obs_data_apply(obs->data.private_data, settings);
  2661. }
  2662. obs_data_t *obs_get_private_data(void)
  2663. {
  2664. obs_data_t *private_data = obs->data.private_data;
  2665. obs_data_addref(private_data);
  2666. return private_data;
  2667. }
  2668. extern bool init_gpu_encoding(struct obs_core_video_mix *video);
  2669. extern void stop_gpu_encoding_thread(struct obs_core_video_mix *video);
  2670. extern void free_gpu_encoding(struct obs_core_video_mix *video);
  2671. bool start_gpu_encode(obs_encoder_t *encoder)
  2672. {
  2673. struct obs_core_video_mix *video = get_mix_for_video(encoder->media);
  2674. bool success = true;
  2675. obs_enter_graphics();
  2676. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2677. if (!video->gpu_encoders.num)
  2678. success = init_gpu_encoding(video);
  2679. if (success)
  2680. da_push_back(video->gpu_encoders, &encoder);
  2681. else
  2682. free_gpu_encoding(video);
  2683. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2684. obs_leave_graphics();
  2685. if (success) {
  2686. os_atomic_inc_long(&video->gpu_encoder_active);
  2687. video_output_inc_texture_encoders(video->video);
  2688. }
  2689. return success;
  2690. }
  2691. void stop_gpu_encode(obs_encoder_t *encoder)
  2692. {
  2693. struct obs_core_video_mix *video = get_mix_for_video(encoder->media);
  2694. bool call_free = false;
  2695. os_atomic_dec_long(&video->gpu_encoder_active);
  2696. video_output_dec_texture_encoders(video->video);
  2697. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2698. da_erase_item(video->gpu_encoders, &encoder);
  2699. if (!video->gpu_encoders.num)
  2700. call_free = true;
  2701. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2702. os_event_wait(video->gpu_encode_inactive);
  2703. if (call_free) {
  2704. stop_gpu_encoding_thread(video);
  2705. obs_enter_graphics();
  2706. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2707. free_gpu_encoding(video);
  2708. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2709. obs_leave_graphics();
  2710. }
  2711. }
  2712. bool obs_video_active(void)
  2713. {
  2714. bool result = false;
  2715. pthread_mutex_lock(&obs->video.mixes_mutex);
  2716. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  2717. struct obs_core_video_mix *video = obs->video.mixes.array[i];
  2718. if (os_atomic_load_long(&video->raw_active) > 0 ||
  2719. os_atomic_load_long(&video->gpu_encoder_active) > 0) {
  2720. result = true;
  2721. break;
  2722. }
  2723. }
  2724. pthread_mutex_unlock(&obs->video.mixes_mutex);
  2725. return result;
  2726. }
  2727. bool obs_nv12_tex_active(void)
  2728. {
  2729. struct obs_core_video_mix *video = obs->video.main_mix;
  2730. return video->using_nv12_tex;
  2731. }
  2732. bool obs_p010_tex_active(void)
  2733. {
  2734. struct obs_core_video_mix *video = obs->video.main_mix;
  2735. return video->using_p010_tex;
  2736. }
  2737. /* ------------------------------------------------------------------------- */
  2738. /* task stuff */
  2739. struct task_wait_info {
  2740. obs_task_t task;
  2741. void *param;
  2742. os_event_t *event;
  2743. };
  2744. static void task_wait_callback(void *param)
  2745. {
  2746. struct task_wait_info *info = param;
  2747. if (info->task)
  2748. info->task(info->param);
  2749. os_event_signal(info->event);
  2750. }
  2751. THREAD_LOCAL bool is_graphics_thread = false;
  2752. THREAD_LOCAL bool is_audio_thread = false;
  2753. static void set_audio_thread(void *unused)
  2754. {
  2755. is_audio_thread = true;
  2756. UNUSED_PARAMETER(unused);
  2757. }
  2758. bool obs_in_task_thread(enum obs_task_type type)
  2759. {
  2760. if (type == OBS_TASK_GRAPHICS)
  2761. return is_graphics_thread;
  2762. else if (type == OBS_TASK_AUDIO)
  2763. return is_audio_thread;
  2764. else if (type == OBS_TASK_UI)
  2765. return is_ui_thread;
  2766. else if (type == OBS_TASK_DESTROY)
  2767. return os_task_queue_inside(obs->destruction_task_thread);
  2768. assert(false);
  2769. return false;
  2770. }
  2771. void obs_queue_task(enum obs_task_type type, obs_task_t task, void *param,
  2772. bool wait)
  2773. {
  2774. if (type == OBS_TASK_UI) {
  2775. if (obs->ui_task_handler) {
  2776. obs->ui_task_handler(task, param, wait);
  2777. } else {
  2778. blog(LOG_ERROR, "UI task could not be queued, "
  2779. "there's no UI task handler!");
  2780. }
  2781. } else {
  2782. if (obs_in_task_thread(type)) {
  2783. task(param);
  2784. } else if (wait) {
  2785. struct task_wait_info info = {
  2786. .task = task,
  2787. .param = param,
  2788. };
  2789. os_event_init(&info.event, OS_EVENT_TYPE_MANUAL);
  2790. obs_queue_task(type, task_wait_callback, &info, false);
  2791. os_event_wait(info.event);
  2792. os_event_destroy(info.event);
  2793. } else if (type == OBS_TASK_GRAPHICS) {
  2794. struct obs_core_video *video = &obs->video;
  2795. struct obs_task_info info = {task, param};
  2796. pthread_mutex_lock(&video->task_mutex);
  2797. circlebuf_push_back(&video->tasks, &info, sizeof(info));
  2798. pthread_mutex_unlock(&video->task_mutex);
  2799. } else if (type == OBS_TASK_AUDIO) {
  2800. struct obs_core_audio *audio = &obs->audio;
  2801. struct obs_task_info info = {task, param};
  2802. pthread_mutex_lock(&audio->task_mutex);
  2803. circlebuf_push_back(&audio->tasks, &info, sizeof(info));
  2804. pthread_mutex_unlock(&audio->task_mutex);
  2805. } else if (type == OBS_TASK_DESTROY) {
  2806. os_task_t os_task = (os_task_t)task;
  2807. os_task_queue_queue_task(obs->destruction_task_thread,
  2808. os_task, param);
  2809. }
  2810. }
  2811. }
  2812. bool obs_wait_for_destroy_queue(void)
  2813. {
  2814. struct task_wait_info info = {0};
  2815. if (!obs->video.thread_initialized || !obs->audio.audio)
  2816. return false;
  2817. /* allow video and audio threads time to release objects */
  2818. os_event_init(&info.event, OS_EVENT_TYPE_AUTO);
  2819. obs_queue_task(OBS_TASK_GRAPHICS, task_wait_callback, &info, false);
  2820. os_event_wait(info.event);
  2821. obs_queue_task(OBS_TASK_AUDIO, task_wait_callback, &info, false);
  2822. os_event_wait(info.event);
  2823. os_event_destroy(info.event);
  2824. /* wait for destroy task queue */
  2825. return os_task_queue_wait(obs->destruction_task_thread);
  2826. }
  2827. static void set_ui_thread(void *unused)
  2828. {
  2829. is_ui_thread = true;
  2830. UNUSED_PARAMETER(unused);
  2831. }
  2832. void obs_set_ui_task_handler(obs_task_handler_t handler)
  2833. {
  2834. obs->ui_task_handler = handler;
  2835. obs_queue_task(OBS_TASK_UI, set_ui_thread, NULL, false);
  2836. }
  2837. obs_object_t *obs_object_get_ref(obs_object_t *object)
  2838. {
  2839. if (!object)
  2840. return NULL;
  2841. return obs_weak_object_get_object(object->control);
  2842. }
  2843. void obs_object_release(obs_object_t *object)
  2844. {
  2845. if (!obs) {
  2846. blog(LOG_WARNING, "Tried to release an object when the OBS "
  2847. "core is shut down!");
  2848. return;
  2849. }
  2850. if (!object)
  2851. return;
  2852. obs_weak_object_t *control = object->control;
  2853. if (obs_ref_release(&control->ref)) {
  2854. object->destroy(object);
  2855. obs_weak_object_release(control);
  2856. }
  2857. }
  2858. void obs_weak_object_addref(obs_weak_object_t *weak)
  2859. {
  2860. if (!weak)
  2861. return;
  2862. obs_weak_ref_addref(&weak->ref);
  2863. }
  2864. void obs_weak_object_release(obs_weak_object_t *weak)
  2865. {
  2866. if (!weak)
  2867. return;
  2868. if (obs_weak_ref_release(&weak->ref))
  2869. bfree(weak);
  2870. }
  2871. obs_weak_object_t *obs_object_get_weak_object(obs_object_t *object)
  2872. {
  2873. if (!object)
  2874. return NULL;
  2875. obs_weak_object_t *weak = object->control;
  2876. obs_weak_object_addref(weak);
  2877. return weak;
  2878. }
  2879. obs_object_t *obs_weak_object_get_object(obs_weak_object_t *weak)
  2880. {
  2881. if (!weak)
  2882. return NULL;
  2883. if (obs_weak_ref_get_ref(&weak->ref))
  2884. return weak->object;
  2885. return NULL;
  2886. }
  2887. bool obs_weak_object_expired(obs_weak_object_t *weak)
  2888. {
  2889. return weak ? obs_weak_ref_expired(&weak->ref) : true;
  2890. }
  2891. bool obs_weak_object_references_object(obs_weak_object_t *weak,
  2892. obs_object_t *object)
  2893. {
  2894. return weak && object && weak->object == object;
  2895. }
  2896. bool obs_is_output_protocol_registered(const char *protocol)
  2897. {
  2898. for (size_t i = 0; i < obs->data.protocols.num; i++) {
  2899. if (strcmp(protocol, obs->data.protocols.array[i]) == 0)
  2900. return true;
  2901. }
  2902. return false;
  2903. }
  2904. bool obs_enum_output_protocols(size_t idx, char **protocol)
  2905. {
  2906. if (idx >= obs->data.protocols.num)
  2907. return false;
  2908. *protocol = obs->data.protocols.array[idx];
  2909. return true;
  2910. }