obs.c 44 KB

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