obs.c 44 KB

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