obs.c 60 KB

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