obs.c 60 KB

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