obs.c 89 KB

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