obs.c 79 KB

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