obs.c 64 KB

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