obs.c 62 KB

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