obs.c 57 KB

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