obs.c 59 KB

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