obs.c 49 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981
  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 = find_libobs_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 = find_libobs_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 = find_libobs_data_file("default_rect.effect");
  202. video->default_rect_effect = gs_effect_create_from_file(
  203. filename, NULL);
  204. bfree(filename);
  205. }
  206. filename = find_libobs_data_file("opaque.effect");
  207. video->opaque_effect = gs_effect_create_from_file(filename,
  208. NULL);
  209. bfree(filename);
  210. filename = find_libobs_data_file("solid.effect");
  211. video->solid_effect = gs_effect_create_from_file(filename,
  212. NULL);
  213. bfree(filename);
  214. filename = find_libobs_data_file("format_conversion.effect");
  215. video->conversion_effect = gs_effect_create_from_file(filename,
  216. NULL);
  217. bfree(filename);
  218. filename = find_libobs_data_file("bicubic_scale.effect");
  219. video->bicubic_effect = gs_effect_create_from_file(filename,
  220. NULL);
  221. bfree(filename);
  222. filename = find_libobs_data_file("lanczos_scale.effect");
  223. video->lanczos_effect = gs_effect_create_from_file(filename,
  224. NULL);
  225. bfree(filename);
  226. filename = find_libobs_data_file("bilinear_lowres_scale.effect");
  227. video->bilinear_lowres_effect = gs_effect_create_from_file(filename,
  228. NULL);
  229. bfree(filename);
  230. filename = find_libobs_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_video_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, NULL) != 0)
  444. goto fail;
  445. if (!obs_view_init(&data->main_view))
  446. goto fail;
  447. data->valid = true;
  448. fail:
  449. pthread_mutexattr_destroy(&attr);
  450. return data->valid;
  451. }
  452. void obs_main_view_free(struct obs_view *view)
  453. {
  454. if (!view) return;
  455. for (size_t i = 0; i < MAX_CHANNELS; i++)
  456. obs_source_release(view->channels[i]);
  457. memset(view->channels, 0, sizeof(view->channels));
  458. pthread_mutex_destroy(&view->channels_mutex);
  459. }
  460. #define FREE_OBS_LINKED_LIST(type) \
  461. do { \
  462. int unfreed = 0; \
  463. while (data->first_ ## type ) { \
  464. obs_ ## type ## _destroy(data->first_ ## type ); \
  465. unfreed++; \
  466. } \
  467. if (unfreed) \
  468. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  469. unfreed); \
  470. } while (false)
  471. static void obs_free_data(void)
  472. {
  473. struct obs_core_data *data = &obs->data;
  474. data->valid = false;
  475. obs_main_view_free(&data->main_view);
  476. blog(LOG_INFO, "Freeing OBS context data");
  477. FREE_OBS_LINKED_LIST(source);
  478. FREE_OBS_LINKED_LIST(output);
  479. FREE_OBS_LINKED_LIST(encoder);
  480. FREE_OBS_LINKED_LIST(display);
  481. FREE_OBS_LINKED_LIST(service);
  482. pthread_mutex_destroy(&data->sources_mutex);
  483. pthread_mutex_destroy(&data->audio_sources_mutex);
  484. pthread_mutex_destroy(&data->displays_mutex);
  485. pthread_mutex_destroy(&data->outputs_mutex);
  486. pthread_mutex_destroy(&data->encoders_mutex);
  487. pthread_mutex_destroy(&data->services_mutex);
  488. pthread_mutex_destroy(&data->draw_callbacks_mutex);
  489. da_free(data->draw_callbacks);
  490. }
  491. static const char *obs_signals[] = {
  492. "void source_create(ptr source)",
  493. "void source_destroy(ptr source)",
  494. "void source_remove(ptr source)",
  495. "void source_save(ptr source)",
  496. "void source_load(ptr source)",
  497. "void source_activate(ptr source)",
  498. "void source_deactivate(ptr source)",
  499. "void source_show(ptr source)",
  500. "void source_hide(ptr source)",
  501. "void source_rename(ptr source, string new_name, string prev_name)",
  502. "void source_volume(ptr source, in out float volume)",
  503. "void source_volume_level(ptr source, float level, float magnitude, "
  504. "float peak)",
  505. "void source_transition_start(ptr source)",
  506. "void source_transition_video_stop(ptr source)",
  507. "void source_transition_stop(ptr source)",
  508. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  509. "void master_volume(in out float volume)",
  510. "void hotkey_layout_change()",
  511. "void hotkey_register(ptr hotkey)",
  512. "void hotkey_unregister(ptr hotkey)",
  513. "void hotkey_bindings_changed(ptr hotkey)",
  514. NULL
  515. };
  516. static inline bool obs_init_handlers(void)
  517. {
  518. obs->signals = signal_handler_create();
  519. if (!obs->signals)
  520. return false;
  521. obs->procs = proc_handler_create();
  522. if (!obs->procs)
  523. return false;
  524. return signal_handler_add_array(obs->signals, obs_signals);
  525. }
  526. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  527. static inline bool obs_init_hotkeys(void)
  528. {
  529. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  530. pthread_mutexattr_t attr;
  531. bool success = false;
  532. assert(hotkeys != NULL);
  533. da_init(hotkeys->hotkeys);
  534. hotkeys->signals = obs->signals;
  535. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  536. hotkeys->mute = bstrdup("Mute");
  537. hotkeys->unmute = bstrdup("Unmute");
  538. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  539. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  540. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  541. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  542. if (!obs_hotkeys_platform_init(hotkeys))
  543. return false;
  544. if (pthread_mutexattr_init(&attr) != 0)
  545. return false;
  546. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  547. goto fail;
  548. if (pthread_mutex_init(&hotkeys->mutex, &attr) != 0)
  549. goto fail;
  550. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  551. goto fail;
  552. if (pthread_create(&hotkeys->hotkey_thread, NULL,
  553. obs_hotkey_thread, NULL))
  554. goto fail;
  555. hotkeys->hotkey_thread_initialized = true;
  556. success = true;
  557. fail:
  558. pthread_mutexattr_destroy(&attr);
  559. return success;
  560. }
  561. static inline void stop_hotkeys(void)
  562. {
  563. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  564. void *thread_ret;
  565. if (hotkeys->hotkey_thread_initialized) {
  566. os_event_signal(hotkeys->stop_event);
  567. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  568. hotkeys->hotkey_thread_initialized = false;
  569. }
  570. os_event_destroy(hotkeys->stop_event);
  571. obs_hotkeys_free();
  572. }
  573. static inline void obs_free_hotkeys(void)
  574. {
  575. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  576. bfree(hotkeys->mute);
  577. bfree(hotkeys->unmute);
  578. bfree(hotkeys->push_to_mute);
  579. bfree(hotkeys->push_to_talk);
  580. bfree(hotkeys->sceneitem_show);
  581. bfree(hotkeys->sceneitem_hide);
  582. obs_hotkey_name_map_free();
  583. obs_hotkeys_platform_free(hotkeys);
  584. pthread_mutex_destroy(&hotkeys->mutex);
  585. }
  586. extern const struct obs_source_info scene_info;
  587. extern void log_system_info(void);
  588. static bool obs_init(const char *locale, const char *module_config_path,
  589. profiler_name_store_t *store)
  590. {
  591. obs = bzalloc(sizeof(struct obs_core));
  592. pthread_mutex_init_value(&obs->audio.monitoring_mutex);
  593. obs->name_store_owned = !store;
  594. obs->name_store = store ? store : profiler_name_store_create();
  595. if (!obs->name_store) {
  596. blog(LOG_ERROR, "Couldn't create profiler name store");
  597. return false;
  598. }
  599. log_system_info();
  600. if (!obs_init_data())
  601. return false;
  602. if (!obs_init_handlers())
  603. return false;
  604. if (!obs_init_hotkeys())
  605. return false;
  606. if (module_config_path)
  607. obs->module_config_path = bstrdup(module_config_path);
  608. obs->locale = bstrdup(locale);
  609. obs_register_source(&scene_info);
  610. add_default_module_paths();
  611. return true;
  612. }
  613. #ifdef _WIN32
  614. extern void initialize_crash_handler(void);
  615. extern void initialize_com(void);
  616. extern void uninitialize_com(void);
  617. #endif
  618. static const char *obs_startup_name = "obs_startup";
  619. bool obs_startup(const char *locale, const char *module_config_path,
  620. profiler_name_store_t *store)
  621. {
  622. bool success;
  623. profile_start(obs_startup_name);
  624. if (obs) {
  625. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  626. return false;
  627. }
  628. #ifdef _WIN32
  629. initialize_crash_handler();
  630. initialize_com();
  631. #endif
  632. success = obs_init(locale, module_config_path, store);
  633. profile_end(obs_startup_name);
  634. if (!success)
  635. obs_shutdown();
  636. return success;
  637. }
  638. void obs_shutdown(void)
  639. {
  640. struct obs_module *module;
  641. if (!obs)
  642. return;
  643. #define FREE_REGISTERED_TYPES(structure, list) \
  644. do { \
  645. for (size_t i = 0; i < list.num; i++) { \
  646. struct structure *item = &list.array[i]; \
  647. if (item->type_data && item->free_type_data) \
  648. item->free_type_data(item->type_data); \
  649. } \
  650. da_free(list); \
  651. } while (false)
  652. FREE_REGISTERED_TYPES(obs_source_info, obs->source_types);
  653. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  654. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  655. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  656. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  657. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  658. #undef FREE_REGISTERED_TYPES
  659. da_free(obs->input_types);
  660. da_free(obs->filter_types);
  661. da_free(obs->transition_types);
  662. stop_video();
  663. stop_hotkeys();
  664. obs_free_audio();
  665. obs_free_data();
  666. obs_free_video();
  667. obs_free_hotkeys();
  668. obs_free_graphics();
  669. proc_handler_destroy(obs->procs);
  670. signal_handler_destroy(obs->signals);
  671. obs->procs = NULL;
  672. obs->signals = NULL;
  673. module = obs->first_module;
  674. while (module) {
  675. struct obs_module *next = module->next;
  676. free_module(module);
  677. module = next;
  678. }
  679. obs->first_module = NULL;
  680. for (size_t i = 0; i < obs->module_paths.num; i++)
  681. free_module_path(obs->module_paths.array+i);
  682. da_free(obs->module_paths);
  683. if (obs->name_store_owned)
  684. profiler_name_store_free(obs->name_store);
  685. bfree(obs->module_config_path);
  686. bfree(obs->locale);
  687. bfree(obs);
  688. obs = NULL;
  689. #ifdef _WIN32
  690. uninitialize_com();
  691. #endif
  692. }
  693. bool obs_initialized(void)
  694. {
  695. return obs != NULL;
  696. }
  697. uint32_t obs_get_version(void)
  698. {
  699. return LIBOBS_API_VER;
  700. }
  701. const char *obs_get_version_string(void)
  702. {
  703. return OBS_VERSION;
  704. }
  705. void obs_set_locale(const char *locale)
  706. {
  707. struct obs_module *module;
  708. if (!obs)
  709. return;
  710. if (obs->locale)
  711. bfree(obs->locale);
  712. obs->locale = bstrdup(locale);
  713. module = obs->first_module;
  714. while (module) {
  715. if (module->set_locale)
  716. module->set_locale(locale);
  717. module = module->next;
  718. }
  719. }
  720. const char *obs_get_locale(void)
  721. {
  722. return obs ? obs->locale : NULL;
  723. }
  724. #define OBS_SIZE_MIN 2
  725. #define OBS_SIZE_MAX (32 * 1024)
  726. static inline bool size_valid(uint32_t width, uint32_t height)
  727. {
  728. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  729. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  730. }
  731. int obs_reset_video(struct obs_video_info *ovi)
  732. {
  733. if (!obs) return OBS_VIDEO_FAIL;
  734. /* don't allow changing of video settings if active. */
  735. if (obs->video.video && video_output_active(obs->video.video))
  736. return OBS_VIDEO_CURRENTLY_ACTIVE;
  737. if (!size_valid(ovi->output_width, ovi->output_height) ||
  738. !size_valid(ovi->base_width, ovi->base_height))
  739. return OBS_VIDEO_INVALID_PARAM;
  740. struct obs_core_video *video = &obs->video;
  741. stop_video();
  742. obs_free_video();
  743. /* align to multiple-of-two and SSE alignment sizes */
  744. ovi->output_width &= 0xFFFFFFFC;
  745. ovi->output_height &= 0xFFFFFFFE;
  746. if (!video->graphics) {
  747. int errorcode = obs_init_graphics(ovi);
  748. if (errorcode != OBS_VIDEO_SUCCESS) {
  749. obs_free_graphics();
  750. return errorcode;
  751. }
  752. }
  753. const char *scale_type_name = "";
  754. switch (ovi->scale_type) {
  755. case OBS_SCALE_DISABLE:
  756. scale_type_name = "Disabled";
  757. break;
  758. case OBS_SCALE_POINT:
  759. scale_type_name = "Point";
  760. break;
  761. case OBS_SCALE_BICUBIC:
  762. scale_type_name = "Bicubic";
  763. break;
  764. case OBS_SCALE_BILINEAR:
  765. scale_type_name = "Bilinear";
  766. break;
  767. case OBS_SCALE_LANCZOS:
  768. scale_type_name = "Lanczos";
  769. break;
  770. }
  771. blog(LOG_INFO, "---------------------------------");
  772. blog(LOG_INFO, "video settings reset:\n"
  773. "\tbase resolution: %dx%d\n"
  774. "\toutput resolution: %dx%d\n"
  775. "\tdownscale filter: %s\n"
  776. "\tfps: %d/%d\n"
  777. "\tformat: %s",
  778. ovi->base_width, ovi->base_height,
  779. ovi->output_width, ovi->output_height,
  780. scale_type_name,
  781. ovi->fps_num, ovi->fps_den,
  782. get_video_format_name(ovi->output_format));
  783. return obs_init_video(ovi);
  784. }
  785. bool obs_reset_audio(const struct obs_audio_info *oai)
  786. {
  787. struct audio_output_info ai;
  788. if (!obs) return false;
  789. /* don't allow changing of audio settings if active. */
  790. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  791. return false;
  792. obs_free_audio();
  793. if (!oai)
  794. return true;
  795. ai.name = "Audio";
  796. ai.samples_per_sec = oai->samples_per_sec;
  797. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  798. ai.speakers = oai->speakers;
  799. ai.input_callback = audio_callback;
  800. blog(LOG_INFO, "---------------------------------");
  801. blog(LOG_INFO, "audio settings reset:\n"
  802. "\tsamples per sec: %d\n"
  803. "\tspeakers: %d",
  804. (int)ai.samples_per_sec,
  805. (int)ai.speakers);
  806. return obs_init_audio(&ai);
  807. }
  808. bool obs_get_video_info(struct obs_video_info *ovi)
  809. {
  810. struct obs_core_video *video = &obs->video;
  811. if (!obs || !video->graphics)
  812. return false;
  813. *ovi = video->ovi;
  814. return true;
  815. }
  816. bool obs_get_audio_info(struct obs_audio_info *oai)
  817. {
  818. struct obs_core_audio *audio = &obs->audio;
  819. const struct audio_output_info *info;
  820. if (!obs || !oai || !audio->audio)
  821. return false;
  822. info = audio_output_get_info(audio->audio);
  823. oai->samples_per_sec = info->samples_per_sec;
  824. oai->speakers = info->speakers;
  825. return true;
  826. }
  827. bool obs_enum_source_types(size_t idx, const char **id)
  828. {
  829. if (!obs) return false;
  830. if (idx >= obs->source_types.num)
  831. return false;
  832. *id = obs->source_types.array[idx].id;
  833. return true;
  834. }
  835. bool obs_enum_input_types(size_t idx, const char **id)
  836. {
  837. if (!obs) return false;
  838. if (idx >= obs->input_types.num)
  839. return false;
  840. *id = obs->input_types.array[idx].id;
  841. return true;
  842. }
  843. bool obs_enum_filter_types(size_t idx, const char **id)
  844. {
  845. if (!obs) return false;
  846. if (idx >= obs->filter_types.num)
  847. return false;
  848. *id = obs->filter_types.array[idx].id;
  849. return true;
  850. }
  851. bool obs_enum_transition_types(size_t idx, const char **id)
  852. {
  853. if (!obs) return false;
  854. if (idx >= obs->transition_types.num)
  855. return false;
  856. *id = obs->transition_types.array[idx].id;
  857. return true;
  858. }
  859. bool obs_enum_output_types(size_t idx, const char **id)
  860. {
  861. if (!obs) return false;
  862. if (idx >= obs->output_types.num)
  863. return false;
  864. *id = obs->output_types.array[idx].id;
  865. return true;
  866. }
  867. bool obs_enum_encoder_types(size_t idx, const char **id)
  868. {
  869. if (!obs) return false;
  870. if (idx >= obs->encoder_types.num)
  871. return false;
  872. *id = obs->encoder_types.array[idx].id;
  873. return true;
  874. }
  875. bool obs_enum_service_types(size_t idx, const char **id)
  876. {
  877. if (!obs) return false;
  878. if (idx >= obs->service_types.num)
  879. return false;
  880. *id = obs->service_types.array[idx].id;
  881. return true;
  882. }
  883. void obs_enter_graphics(void)
  884. {
  885. if (obs && obs->video.graphics)
  886. gs_enter_context(obs->video.graphics);
  887. }
  888. void obs_leave_graphics(void)
  889. {
  890. if (obs && obs->video.graphics)
  891. gs_leave_context();
  892. }
  893. audio_t *obs_get_audio(void)
  894. {
  895. return (obs != NULL) ? obs->audio.audio : NULL;
  896. }
  897. video_t *obs_get_video(void)
  898. {
  899. return (obs != NULL) ? obs->video.video : NULL;
  900. }
  901. /* TODO: optimize this later so it's not just O(N) string lookups */
  902. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  903. const char *task, const char *target)
  904. {
  905. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  906. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  907. if (strcmp(callback->id, id) == 0 &&
  908. strcmp(callback->task, task) == 0 &&
  909. strcmp(callback->target, target) == 0)
  910. return callback;
  911. }
  912. return NULL;
  913. }
  914. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  915. const char *task, const char *target)
  916. {
  917. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  918. struct obs_modeless_ui *callback;
  919. callback = obs->modeless_ui_callbacks.array+i;
  920. if (strcmp(callback->id, id) == 0 &&
  921. strcmp(callback->task, task) == 0 &&
  922. strcmp(callback->target, target) == 0)
  923. return callback;
  924. }
  925. return NULL;
  926. }
  927. int obs_exec_ui(const char *name, const char *task, const char *target,
  928. void *data, void *ui_data)
  929. {
  930. struct obs_modal_ui *callback;
  931. int errorcode = OBS_UI_NOTFOUND;
  932. if (!obs) return errorcode;
  933. callback = get_modal_ui_callback(name, task, target);
  934. if (callback) {
  935. bool success = callback->exec(data, ui_data);
  936. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  937. }
  938. return errorcode;
  939. }
  940. void *obs_create_ui(const char *name, const char *task, const char *target,
  941. void *data, void *ui_data)
  942. {
  943. struct obs_modeless_ui *callback;
  944. if (!obs) return NULL;
  945. callback = get_modeless_ui_callback(name, task, target);
  946. return callback ? callback->create(data, ui_data) : NULL;
  947. }
  948. obs_source_t *obs_get_output_source(uint32_t channel)
  949. {
  950. if (!obs) return NULL;
  951. return obs_view_get_source(&obs->data.main_view, channel);
  952. }
  953. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  954. {
  955. assert(channel < MAX_CHANNELS);
  956. if (!obs) return;
  957. if (channel >= MAX_CHANNELS) return;
  958. struct obs_source *prev_source;
  959. struct obs_view *view = &obs->data.main_view;
  960. struct calldata params = {0};
  961. pthread_mutex_lock(&view->channels_mutex);
  962. obs_source_addref(source);
  963. prev_source = view->channels[channel];
  964. calldata_set_int(&params, "channel", channel);
  965. calldata_set_ptr(&params, "prev_source", prev_source);
  966. calldata_set_ptr(&params, "source", source);
  967. signal_handler_signal(obs->signals, "channel_change", &params);
  968. calldata_get_ptr(&params, "source", &source);
  969. calldata_free(&params);
  970. view->channels[channel] = source;
  971. pthread_mutex_unlock(&view->channels_mutex);
  972. if (source)
  973. obs_source_activate(source, MAIN_VIEW);
  974. if (prev_source) {
  975. obs_source_deactivate(prev_source, MAIN_VIEW);
  976. obs_source_release(prev_source);
  977. }
  978. }
  979. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  980. {
  981. obs_source_t *source;
  982. if (!obs) return;
  983. pthread_mutex_lock(&obs->data.sources_mutex);
  984. source = obs->data.first_source;
  985. while (source) {
  986. obs_source_t *next_source =
  987. (obs_source_t*)source->context.next;
  988. if ((source->info.type == OBS_SOURCE_TYPE_INPUT) != 0 &&
  989. !source->context.private &&
  990. !enum_proc(param, source))
  991. break;
  992. source = next_source;
  993. }
  994. pthread_mutex_unlock(&obs->data.sources_mutex);
  995. }
  996. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  997. void *param)
  998. {
  999. struct obs_context_data **start = pstart, *context;
  1000. bool (*enum_proc)(void*, void*) = proc;
  1001. assert(start);
  1002. assert(mutex);
  1003. assert(enum_proc);
  1004. pthread_mutex_lock(mutex);
  1005. context = *start;
  1006. while (context) {
  1007. if (!enum_proc(param, context))
  1008. break;
  1009. context = context->next;
  1010. }
  1011. pthread_mutex_unlock(mutex);
  1012. }
  1013. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  1014. {
  1015. if (!obs) return;
  1016. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  1017. enum_proc, param);
  1018. }
  1019. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  1020. {
  1021. if (!obs) return;
  1022. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  1023. enum_proc, param);
  1024. }
  1025. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  1026. {
  1027. if (!obs) return;
  1028. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  1029. enum_proc, param);
  1030. }
  1031. static inline void *get_context_by_name(void *vfirst, const char *name,
  1032. pthread_mutex_t *mutex, void *(*addref)(void*))
  1033. {
  1034. struct obs_context_data **first = vfirst;
  1035. struct obs_context_data *context;
  1036. pthread_mutex_lock(mutex);
  1037. context = *first;
  1038. while (context) {
  1039. if (!context->private && strcmp(context->name, name) == 0) {
  1040. context = addref(context);
  1041. break;
  1042. }
  1043. context = context->next;
  1044. }
  1045. pthread_mutex_unlock(mutex);
  1046. return context;
  1047. }
  1048. static inline void *obs_source_addref_safe_(void *ref)
  1049. {
  1050. return obs_source_get_ref(ref);
  1051. }
  1052. static inline void *obs_output_addref_safe_(void *ref)
  1053. {
  1054. return obs_output_get_ref(ref);
  1055. }
  1056. static inline void *obs_encoder_addref_safe_(void *ref)
  1057. {
  1058. return obs_encoder_get_ref(ref);
  1059. }
  1060. static inline void *obs_service_addref_safe_(void *ref)
  1061. {
  1062. return obs_service_get_ref(ref);
  1063. }
  1064. static inline void *obs_id_(void *data)
  1065. {
  1066. return data;
  1067. }
  1068. obs_source_t *obs_get_source_by_name(const char *name)
  1069. {
  1070. if (!obs) return NULL;
  1071. return get_context_by_name(&obs->data.first_source, name,
  1072. &obs->data.sources_mutex, obs_source_addref_safe_);
  1073. }
  1074. obs_output_t *obs_get_output_by_name(const char *name)
  1075. {
  1076. if (!obs) return NULL;
  1077. return get_context_by_name(&obs->data.first_output, name,
  1078. &obs->data.outputs_mutex, obs_output_addref_safe_);
  1079. }
  1080. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1081. {
  1082. if (!obs) return NULL;
  1083. return get_context_by_name(&obs->data.first_encoder, name,
  1084. &obs->data.encoders_mutex, obs_encoder_addref_safe_);
  1085. }
  1086. obs_service_t *obs_get_service_by_name(const char *name)
  1087. {
  1088. if (!obs) return NULL;
  1089. return get_context_by_name(&obs->data.first_service, name,
  1090. &obs->data.services_mutex, obs_service_addref_safe_);
  1091. }
  1092. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1093. {
  1094. if (!obs) return NULL;
  1095. switch (effect) {
  1096. case OBS_EFFECT_DEFAULT:
  1097. return obs->video.default_effect;
  1098. case OBS_EFFECT_DEFAULT_RECT:
  1099. return obs->video.default_rect_effect;
  1100. case OBS_EFFECT_OPAQUE:
  1101. return obs->video.opaque_effect;
  1102. case OBS_EFFECT_SOLID:
  1103. return obs->video.solid_effect;
  1104. case OBS_EFFECT_BICUBIC:
  1105. return obs->video.bicubic_effect;
  1106. case OBS_EFFECT_LANCZOS:
  1107. return obs->video.lanczos_effect;
  1108. case OBS_EFFECT_BILINEAR_LOWRES:
  1109. return obs->video.bilinear_lowres_effect;
  1110. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1111. return obs->video.premultiplied_alpha_effect;
  1112. }
  1113. return NULL;
  1114. }
  1115. /* DEPRECATED */
  1116. gs_effect_t *obs_get_default_rect_effect(void)
  1117. {
  1118. if (!obs) return NULL;
  1119. return obs->video.default_rect_effect;
  1120. }
  1121. signal_handler_t *obs_get_signal_handler(void)
  1122. {
  1123. if (!obs) return NULL;
  1124. return obs->signals;
  1125. }
  1126. proc_handler_t *obs_get_proc_handler(void)
  1127. {
  1128. if (!obs) return NULL;
  1129. return obs->procs;
  1130. }
  1131. void obs_render_main_view(void)
  1132. {
  1133. if (!obs) return;
  1134. obs_view_render(&obs->data.main_view);
  1135. }
  1136. void obs_set_master_volume(float volume)
  1137. {
  1138. struct calldata data = {0};
  1139. if (!obs) return;
  1140. calldata_set_float(&data, "volume", volume);
  1141. signal_handler_signal(obs->signals, "master_volume", &data);
  1142. volume = (float)calldata_float(&data, "volume");
  1143. calldata_free(&data);
  1144. obs->audio.user_volume = volume;
  1145. }
  1146. float obs_get_master_volume(void)
  1147. {
  1148. return obs ? obs->audio.user_volume : 0.0f;
  1149. }
  1150. static obs_source_t *obs_load_source_type(obs_data_t *source_data)
  1151. {
  1152. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1153. obs_source_t *source;
  1154. const char *name = obs_data_get_string(source_data, "name");
  1155. const char *id = obs_data_get_string(source_data, "id");
  1156. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1157. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1158. double volume;
  1159. int64_t sync;
  1160. uint32_t flags;
  1161. uint32_t mixers;
  1162. int di_order;
  1163. int di_mode;
  1164. int monitoring_type;
  1165. source = obs_source_create(id, name, settings, hotkeys);
  1166. obs_data_release(hotkeys);
  1167. obs_data_set_default_double(source_data, "volume", 1.0);
  1168. volume = obs_data_get_double(source_data, "volume");
  1169. obs_source_set_volume(source, (float)volume);
  1170. sync = obs_data_get_int(source_data, "sync");
  1171. obs_source_set_sync_offset(source, sync);
  1172. obs_data_set_default_int(source_data, "mixers", 0xF);
  1173. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1174. obs_source_set_audio_mixers(source, mixers);
  1175. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1176. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1177. obs_source_set_flags(source, flags);
  1178. obs_data_set_default_bool(source_data, "enabled", true);
  1179. obs_source_set_enabled(source,
  1180. obs_data_get_bool(source_data, "enabled"));
  1181. obs_data_set_default_bool(source_data, "muted", false);
  1182. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1183. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1184. obs_source_enable_push_to_mute(source,
  1185. obs_data_get_bool(source_data, "push-to-mute"));
  1186. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1187. obs_source_set_push_to_mute_delay(source,
  1188. obs_data_get_int(source_data, "push-to-mute-delay"));
  1189. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1190. obs_source_enable_push_to_talk(source,
  1191. obs_data_get_bool(source_data, "push-to-talk"));
  1192. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1193. obs_source_set_push_to_talk_delay(source,
  1194. obs_data_get_int(source_data, "push-to-talk-delay"));
  1195. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1196. obs_source_set_deinterlace_mode(source,
  1197. (enum obs_deinterlace_mode)di_mode);
  1198. di_order = (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1199. obs_source_set_deinterlace_field_order(source,
  1200. (enum obs_deinterlace_field_order)di_order);
  1201. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1202. obs_source_set_monitoring_type(source,
  1203. (enum obs_monitoring_type)monitoring_type);
  1204. if (filters) {
  1205. size_t count = obs_data_array_count(filters);
  1206. for (size_t i = 0; i < count; i++) {
  1207. obs_data_t *filter_data =
  1208. obs_data_array_item(filters, i);
  1209. obs_source_t *filter = obs_load_source_type(
  1210. filter_data);
  1211. if (filter) {
  1212. obs_source_filter_add(source, filter);
  1213. obs_source_release(filter);
  1214. }
  1215. obs_data_release(filter_data);
  1216. }
  1217. obs_data_array_release(filters);
  1218. }
  1219. obs_data_release(settings);
  1220. return source;
  1221. }
  1222. obs_source_t *obs_load_source(obs_data_t *source_data)
  1223. {
  1224. return obs_load_source_type(source_data);
  1225. }
  1226. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1227. void *private_data)
  1228. {
  1229. if (!obs) return;
  1230. struct obs_core_data *data = &obs->data;
  1231. DARRAY(obs_source_t*) sources;
  1232. size_t count;
  1233. size_t i;
  1234. da_init(sources);
  1235. count = obs_data_array_count(array);
  1236. da_reserve(sources, count);
  1237. pthread_mutex_lock(&data->sources_mutex);
  1238. for (i = 0; i < count; i++) {
  1239. obs_data_t *source_data = obs_data_array_item(array, i);
  1240. obs_source_t *source = obs_load_source(source_data);
  1241. da_push_back(sources, &source);
  1242. obs_data_release(source_data);
  1243. }
  1244. /* tell sources that we want to load */
  1245. for (i = 0; i < sources.num; i++) {
  1246. obs_source_t *source = sources.array[i];
  1247. obs_data_t *source_data = obs_data_array_item(array, i);
  1248. if (source) {
  1249. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1250. obs_transition_load(source, source_data);
  1251. obs_source_load(source);
  1252. cb(private_data, source);
  1253. }
  1254. obs_data_release(source_data);
  1255. }
  1256. for (i = 0; i < sources.num; i++)
  1257. obs_source_release(sources.array[i]);
  1258. pthread_mutex_unlock(&data->sources_mutex);
  1259. da_free(sources);
  1260. }
  1261. obs_data_t *obs_save_source(obs_source_t *source)
  1262. {
  1263. obs_data_array_t *filters = obs_data_array_create();
  1264. obs_data_t *source_data = obs_data_create();
  1265. obs_data_t *settings = obs_source_get_settings(source);
  1266. obs_data_t *hotkey_data = source->context.hotkey_data;
  1267. obs_data_t *hotkeys;
  1268. float volume = obs_source_get_volume(source);
  1269. uint32_t mixers = obs_source_get_audio_mixers(source);
  1270. int64_t sync = obs_source_get_sync_offset(source);
  1271. uint32_t flags = obs_source_get_flags(source);
  1272. const char *name = obs_source_get_name(source);
  1273. const char *id = obs_source_get_id(source);
  1274. bool enabled = obs_source_enabled(source);
  1275. bool muted = obs_source_muted(source);
  1276. bool push_to_mute= obs_source_push_to_mute_enabled(source);
  1277. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1278. bool push_to_talk= obs_source_push_to_talk_enabled(source);
  1279. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1280. int m_type = (int)obs_source_get_monitoring_type(source);
  1281. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1282. int di_order =
  1283. (int)obs_source_get_deinterlace_field_order(source);
  1284. obs_source_save(source);
  1285. hotkeys = obs_hotkeys_save_source(source);
  1286. if (hotkeys) {
  1287. obs_data_release(hotkey_data);
  1288. source->context.hotkey_data = hotkeys;
  1289. hotkey_data = hotkeys;
  1290. }
  1291. obs_data_set_string(source_data, "name", name);
  1292. obs_data_set_string(source_data, "id", id);
  1293. obs_data_set_obj (source_data, "settings", settings);
  1294. obs_data_set_int (source_data, "mixers", mixers);
  1295. obs_data_set_int (source_data, "sync", sync);
  1296. obs_data_set_int (source_data, "flags", flags);
  1297. obs_data_set_double(source_data, "volume", volume);
  1298. obs_data_set_bool (source_data, "enabled", enabled);
  1299. obs_data_set_bool (source_data, "muted", muted);
  1300. obs_data_set_bool (source_data, "push-to-mute", push_to_mute);
  1301. obs_data_set_int (source_data, "push-to-mute-delay", ptm_delay);
  1302. obs_data_set_bool (source_data, "push-to-talk", push_to_talk);
  1303. obs_data_set_int (source_data, "push-to-talk-delay", ptt_delay);
  1304. obs_data_set_obj (source_data, "hotkeys", hotkey_data);
  1305. obs_data_set_int (source_data, "deinterlace_mode", di_mode);
  1306. obs_data_set_int (source_data, "deinterlace_field_order", di_order);
  1307. obs_data_set_int (source_data, "monitoring_type", m_type);
  1308. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1309. obs_transition_save(source, source_data);
  1310. pthread_mutex_lock(&source->filter_mutex);
  1311. if (source->filters.num) {
  1312. for (size_t i = source->filters.num; i > 0; i--) {
  1313. obs_source_t *filter = source->filters.array[i - 1];
  1314. obs_data_t *filter_data = obs_save_source(filter);
  1315. obs_data_array_push_back(filters, filter_data);
  1316. obs_data_release(filter_data);
  1317. }
  1318. obs_data_set_array(source_data, "filters", filters);
  1319. }
  1320. pthread_mutex_unlock(&source->filter_mutex);
  1321. obs_data_release(settings);
  1322. obs_data_array_release(filters);
  1323. return source_data;
  1324. }
  1325. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1326. void *data_)
  1327. {
  1328. if (!obs) return NULL;
  1329. struct obs_core_data *data = &obs->data;
  1330. obs_data_array_t *array;
  1331. obs_source_t *source;
  1332. array = obs_data_array_create();
  1333. pthread_mutex_lock(&data->sources_mutex);
  1334. source = data->first_source;
  1335. while (source) {
  1336. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1337. !source->context.private && cb(data_, source)) {
  1338. obs_data_t *source_data = obs_save_source(source);
  1339. obs_data_array_push_back(array, source_data);
  1340. obs_data_release(source_data);
  1341. }
  1342. source = (obs_source_t*)source->context.next;
  1343. }
  1344. pthread_mutex_unlock(&data->sources_mutex);
  1345. return array;
  1346. }
  1347. static bool save_source_filter(void *data, obs_source_t *source)
  1348. {
  1349. UNUSED_PARAMETER(data);
  1350. UNUSED_PARAMETER(source);
  1351. return true;
  1352. }
  1353. obs_data_array_t *obs_save_sources(void)
  1354. {
  1355. return obs_save_sources_filtered(save_source_filter, NULL);
  1356. }
  1357. /* ensures that names are never blank */
  1358. static inline char *dup_name(const char *name, bool private)
  1359. {
  1360. if (private && !name)
  1361. return NULL;
  1362. if (!name || !*name) {
  1363. struct dstr unnamed = {0};
  1364. dstr_printf(&unnamed, "__unnamed%04lld",
  1365. obs->data.unnamed_index++);
  1366. return unnamed.array;
  1367. } else {
  1368. return bstrdup(name);
  1369. }
  1370. }
  1371. static inline bool obs_context_data_init_wrap(
  1372. struct obs_context_data *context,
  1373. enum obs_obj_type type,
  1374. obs_data_t *settings,
  1375. const char *name,
  1376. obs_data_t *hotkey_data,
  1377. bool private)
  1378. {
  1379. assert(context);
  1380. memset(context, 0, sizeof(*context));
  1381. context->private = private;
  1382. context->type = type;
  1383. pthread_mutex_init_value(&context->rename_cache_mutex);
  1384. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1385. return false;
  1386. context->signals = signal_handler_create();
  1387. if (!context->signals)
  1388. return false;
  1389. context->procs = proc_handler_create();
  1390. if (!context->procs)
  1391. return false;
  1392. context->name = dup_name(name, private);
  1393. context->settings = obs_data_newref(settings);
  1394. context->hotkey_data = obs_data_newref(hotkey_data);
  1395. return true;
  1396. }
  1397. bool obs_context_data_init(
  1398. struct obs_context_data *context,
  1399. enum obs_obj_type type,
  1400. obs_data_t *settings,
  1401. const char *name,
  1402. obs_data_t *hotkey_data,
  1403. bool private)
  1404. {
  1405. if (obs_context_data_init_wrap(context, type, settings, name,
  1406. hotkey_data, private)) {
  1407. return true;
  1408. } else {
  1409. obs_context_data_free(context);
  1410. return false;
  1411. }
  1412. }
  1413. void obs_context_data_free(struct obs_context_data *context)
  1414. {
  1415. obs_hotkeys_context_release(context);
  1416. signal_handler_destroy(context->signals);
  1417. proc_handler_destroy(context->procs);
  1418. obs_data_release(context->settings);
  1419. obs_context_data_remove(context);
  1420. pthread_mutex_destroy(&context->rename_cache_mutex);
  1421. bfree(context->name);
  1422. for (size_t i = 0; i < context->rename_cache.num; i++)
  1423. bfree(context->rename_cache.array[i]);
  1424. da_free(context->rename_cache);
  1425. memset(context, 0, sizeof(*context));
  1426. }
  1427. void obs_context_data_insert(struct obs_context_data *context,
  1428. pthread_mutex_t *mutex, void *pfirst)
  1429. {
  1430. struct obs_context_data **first = pfirst;
  1431. assert(context);
  1432. assert(mutex);
  1433. assert(first);
  1434. context->mutex = mutex;
  1435. pthread_mutex_lock(mutex);
  1436. context->prev_next = first;
  1437. context->next = *first;
  1438. *first = context;
  1439. if (context->next)
  1440. context->next->prev_next = &context->next;
  1441. pthread_mutex_unlock(mutex);
  1442. }
  1443. void obs_context_data_remove(struct obs_context_data *context)
  1444. {
  1445. if (context && context->mutex) {
  1446. pthread_mutex_lock(context->mutex);
  1447. if (context->prev_next)
  1448. *context->prev_next = context->next;
  1449. if (context->next)
  1450. context->next->prev_next = context->prev_next;
  1451. pthread_mutex_unlock(context->mutex);
  1452. context->mutex = NULL;
  1453. }
  1454. }
  1455. void obs_context_data_setname(struct obs_context_data *context,
  1456. const char *name)
  1457. {
  1458. pthread_mutex_lock(&context->rename_cache_mutex);
  1459. if (context->name)
  1460. da_push_back(context->rename_cache, &context->name);
  1461. context->name = dup_name(name, context->private);
  1462. pthread_mutex_unlock(&context->rename_cache_mutex);
  1463. }
  1464. profiler_name_store_t *obs_get_profiler_name_store(void)
  1465. {
  1466. if (!obs)
  1467. return NULL;
  1468. return obs->name_store;
  1469. }
  1470. uint64_t obs_get_video_frame_time(void)
  1471. {
  1472. return obs ? obs->video.video_time : 0;
  1473. }
  1474. double obs_get_active_fps(void)
  1475. {
  1476. return obs ? obs->video.video_fps : 0.0;
  1477. }
  1478. uint64_t obs_get_average_frame_time_ns(void)
  1479. {
  1480. return obs ? obs->video.video_avg_frame_time_ns : 0;
  1481. }
  1482. enum obs_obj_type obs_obj_get_type(void *obj)
  1483. {
  1484. struct obs_context_data *context = obj;
  1485. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  1486. }
  1487. const char *obs_obj_get_id(void *obj)
  1488. {
  1489. struct obs_context_data *context = obj;
  1490. if (!context)
  1491. return NULL;
  1492. switch (context->type) {
  1493. case OBS_OBJ_TYPE_SOURCE: return ((obs_source_t*)obj)->info.id;
  1494. case OBS_OBJ_TYPE_OUTPUT: return ((obs_output_t*)obj)->info.id;
  1495. case OBS_OBJ_TYPE_ENCODER: return ((obs_encoder_t*)obj)->info.id;
  1496. case OBS_OBJ_TYPE_SERVICE: return ((obs_service_t*)obj)->info.id;
  1497. default:;
  1498. }
  1499. return NULL;
  1500. }
  1501. bool obs_obj_invalid(void *obj)
  1502. {
  1503. struct obs_context_data *context = obj;
  1504. if (!context)
  1505. return true;
  1506. return !context->data;
  1507. }
  1508. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  1509. {
  1510. if (!obs || !name || !id || !*name || !*id)
  1511. return false;
  1512. #if defined(_WIN32) || HAVE_PULSEAUDIO
  1513. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  1514. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  1515. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1516. return true;
  1517. }
  1518. if (obs->audio.monitoring_device_name)
  1519. bfree(obs->audio.monitoring_device_name);
  1520. if (obs->audio.monitoring_device_id)
  1521. bfree(obs->audio.monitoring_device_id);
  1522. obs->audio.monitoring_device_name = bstrdup(name);
  1523. obs->audio.monitoring_device_id = bstrdup(id);
  1524. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  1525. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  1526. audio_monitor_reset(monitor);
  1527. }
  1528. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1529. return true;
  1530. #else
  1531. return false;
  1532. #endif
  1533. }
  1534. void obs_get_audio_monitoring_device(const char **name, const char **id)
  1535. {
  1536. if (!obs)
  1537. return;
  1538. if (name)
  1539. *name = obs->audio.monitoring_device_name;
  1540. if (id)
  1541. *id = obs->audio.monitoring_device_id;
  1542. }
  1543. void obs_add_main_render_callback(
  1544. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1545. void *param)
  1546. {
  1547. if (!obs)
  1548. return;
  1549. struct draw_callback data = {draw, param};
  1550. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1551. da_push_back(obs->data.draw_callbacks, &data);
  1552. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1553. }
  1554. void obs_remove_main_render_callback(
  1555. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1556. void *param)
  1557. {
  1558. if (!obs)
  1559. return;
  1560. struct draw_callback data = {draw, param};
  1561. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1562. da_erase_item(obs->data.draw_callbacks, &data);
  1563. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1564. }
  1565. uint32_t obs_get_total_frames(void)
  1566. {
  1567. return obs ? obs->video.total_frames : 0;
  1568. }
  1569. uint32_t obs_get_lagged_frames(void)
  1570. {
  1571. return obs ? obs->video.lagged_frames : 0;
  1572. }