obs.c 71 KB

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