obs.c 56 KB

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