obs.c 46 KB

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