obs.c 90 KB

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