obs.c 90 KB

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