obs.c 79 KB

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