obs.c 67 KB

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