obs.c 79 KB

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