obs.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720
  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. if (!obs_hotkeys_platform_init(hotkeys))
  509. return false;
  510. if (pthread_mutexattr_init(&attr) != 0)
  511. return false;
  512. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  513. goto fail;
  514. if (pthread_mutex_init(&hotkeys->mutex, &attr) != 0)
  515. goto fail;
  516. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  517. goto fail;
  518. if (pthread_create(&hotkeys->hotkey_thread, NULL,
  519. obs_hotkey_thread, NULL))
  520. goto fail;
  521. hotkeys->hotkey_thread_initialized = true;
  522. success = true;
  523. fail:
  524. pthread_mutexattr_destroy(&attr);
  525. return success;
  526. }
  527. static inline void stop_hotkeys(void)
  528. {
  529. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  530. void *thread_ret;
  531. if (hotkeys->hotkey_thread_initialized) {
  532. os_event_signal(hotkeys->stop_event);
  533. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  534. hotkeys->hotkey_thread_initialized = false;
  535. }
  536. os_event_destroy(hotkeys->stop_event);
  537. obs_hotkeys_free();
  538. }
  539. static inline void obs_free_hotkeys(void)
  540. {
  541. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  542. obs_hotkey_name_map_free();
  543. obs_hotkeys_platform_free(hotkeys);
  544. pthread_mutex_destroy(&hotkeys->mutex);
  545. }
  546. extern const struct obs_source_info scene_info;
  547. extern void log_system_info(void);
  548. static bool obs_init(const char *locale)
  549. {
  550. obs = bzalloc(sizeof(struct obs_core));
  551. log_system_info();
  552. if (!obs_init_data())
  553. return false;
  554. if (!obs_init_handlers())
  555. return false;
  556. if (!obs_init_hotkeys())
  557. return false;
  558. obs->locale = bstrdup(locale);
  559. obs_register_source(&scene_info);
  560. add_default_module_paths();
  561. return true;
  562. }
  563. #ifdef _WIN32
  564. extern void initialize_crash_handler(void);
  565. #endif
  566. bool obs_startup(const char *locale)
  567. {
  568. bool success;
  569. if (obs) {
  570. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  571. return false;
  572. }
  573. #ifdef _WIN32
  574. initialize_crash_handler();
  575. #endif
  576. success = obs_init(locale);
  577. if (!success)
  578. obs_shutdown();
  579. return success;
  580. }
  581. void obs_shutdown(void)
  582. {
  583. struct obs_module *module;
  584. if (!obs)
  585. return;
  586. da_free(obs->input_types);
  587. da_free(obs->filter_types);
  588. da_free(obs->encoder_types);
  589. da_free(obs->transition_types);
  590. da_free(obs->output_types);
  591. da_free(obs->service_types);
  592. da_free(obs->modal_ui_callbacks);
  593. da_free(obs->modeless_ui_callbacks);
  594. stop_video();
  595. stop_hotkeys();
  596. obs_free_data();
  597. obs_free_video();
  598. obs_free_hotkeys();
  599. obs_free_graphics();
  600. obs_free_audio();
  601. proc_handler_destroy(obs->procs);
  602. signal_handler_destroy(obs->signals);
  603. module = obs->first_module;
  604. while (module) {
  605. struct obs_module *next = module->next;
  606. free_module(module);
  607. module = next;
  608. }
  609. obs->first_module = NULL;
  610. for (size_t i = 0; i < obs->module_paths.num; i++)
  611. free_module_path(obs->module_paths.array+i);
  612. da_free(obs->module_paths);
  613. bfree(obs->locale);
  614. bfree(obs);
  615. obs = NULL;
  616. }
  617. bool obs_initialized(void)
  618. {
  619. return obs != NULL;
  620. }
  621. uint32_t obs_get_version(void)
  622. {
  623. return LIBOBS_API_VER;
  624. }
  625. void obs_set_locale(const char *locale)
  626. {
  627. struct obs_module *module;
  628. if (!obs)
  629. return;
  630. if (obs->locale)
  631. bfree(obs->locale);
  632. obs->locale = bstrdup(locale);
  633. module = obs->first_module;
  634. while (module) {
  635. if (module->set_locale)
  636. module->set_locale(locale);
  637. module = module->next;
  638. }
  639. }
  640. const char *obs_get_locale(void)
  641. {
  642. return obs ? obs->locale : NULL;
  643. }
  644. #define OBS_SIZE_MIN 2
  645. #define OBS_SIZE_MAX (32 * 1024)
  646. static inline bool size_valid(uint32_t width, uint32_t height)
  647. {
  648. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  649. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  650. }
  651. int obs_reset_video(struct obs_video_info *ovi)
  652. {
  653. if (!obs) return OBS_VIDEO_FAIL;
  654. /* don't allow changing of video settings if active. */
  655. if (obs->video.video && video_output_active(obs->video.video))
  656. return OBS_VIDEO_CURRENTLY_ACTIVE;
  657. if (!size_valid(ovi->output_width, ovi->output_height) ||
  658. !size_valid(ovi->base_width, ovi->base_height))
  659. return OBS_VIDEO_INVALID_PARAM;
  660. struct obs_core_video *video = &obs->video;
  661. stop_video();
  662. obs_free_video();
  663. if (!ovi) {
  664. obs_free_graphics();
  665. return OBS_VIDEO_SUCCESS;
  666. }
  667. /* align to multiple-of-two and SSE alignment sizes */
  668. ovi->output_width &= 0xFFFFFFFC;
  669. ovi->output_height &= 0xFFFFFFFE;
  670. if (!video->graphics) {
  671. int errorcode = obs_init_graphics(ovi);
  672. if (errorcode != OBS_VIDEO_SUCCESS) {
  673. obs_free_graphics();
  674. return errorcode;
  675. }
  676. }
  677. blog(LOG_INFO, "video settings reset:\n"
  678. "\tbase resolution: %dx%d\n"
  679. "\toutput resolution: %dx%d\n"
  680. "\tfps: %d/%d\n"
  681. "\tformat: %s",
  682. ovi->base_width, ovi->base_height,
  683. ovi->output_width, ovi->output_height,
  684. ovi->fps_num, ovi->fps_den,
  685. get_video_format_name(ovi->output_format));
  686. return obs_init_video(ovi);
  687. }
  688. bool obs_reset_audio(const struct obs_audio_info *oai)
  689. {
  690. struct audio_output_info ai;
  691. if (!obs) return false;
  692. /* don't allow changing of audio settings if active. */
  693. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  694. return false;
  695. obs_free_audio();
  696. if (!oai)
  697. return true;
  698. ai.name = "Audio";
  699. ai.samples_per_sec = oai->samples_per_sec;
  700. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  701. ai.speakers = oai->speakers;
  702. ai.buffer_ms = oai->buffer_ms;
  703. blog(LOG_INFO, "audio settings reset:\n"
  704. "\tsamples per sec: %d\n"
  705. "\tspeakers: %d\n"
  706. "\tbuffering (ms): %d\n",
  707. (int)ai.samples_per_sec,
  708. (int)ai.speakers,
  709. (int)ai.buffer_ms);
  710. return obs_init_audio(&ai);
  711. }
  712. bool obs_get_video_info(struct obs_video_info *ovi)
  713. {
  714. struct obs_core_video *video = &obs->video;
  715. const struct video_output_info *info;
  716. if (!obs || !video->graphics)
  717. return false;
  718. info = video_output_get_info(video->video);
  719. if (!info)
  720. return false;
  721. memset(ovi, 0, sizeof(struct obs_video_info));
  722. ovi->base_width = video->base_width;
  723. ovi->base_height = video->base_height;
  724. ovi->gpu_conversion= video->gpu_conversion;
  725. ovi->scale_type = video->scale_type;
  726. ovi->colorspace = info->colorspace;
  727. ovi->range = info->range;
  728. ovi->output_width = info->width;
  729. ovi->output_height = info->height;
  730. ovi->output_format = info->format;
  731. ovi->fps_num = info->fps_num;
  732. ovi->fps_den = info->fps_den;
  733. return true;
  734. }
  735. bool obs_get_audio_info(struct obs_audio_info *oai)
  736. {
  737. struct obs_core_audio *audio = &obs->audio;
  738. const struct audio_output_info *info;
  739. if (!obs || !oai || !audio->audio)
  740. return false;
  741. info = audio_output_get_info(audio->audio);
  742. oai->samples_per_sec = info->samples_per_sec;
  743. oai->speakers = info->speakers;
  744. oai->buffer_ms = info->buffer_ms;
  745. return true;
  746. }
  747. bool obs_enum_input_types(size_t idx, const char **id)
  748. {
  749. if (!obs) return false;
  750. if (idx >= obs->input_types.num)
  751. return false;
  752. *id = obs->input_types.array[idx].id;
  753. return true;
  754. }
  755. bool obs_enum_filter_types(size_t idx, const char **id)
  756. {
  757. if (!obs) return false;
  758. if (idx >= obs->filter_types.num)
  759. return false;
  760. *id = obs->filter_types.array[idx].id;
  761. return true;
  762. }
  763. bool obs_enum_transition_types(size_t idx, const char **id)
  764. {
  765. if (!obs) return false;
  766. if (idx >= obs->transition_types.num)
  767. return false;
  768. *id = obs->transition_types.array[idx].id;
  769. return true;
  770. }
  771. bool obs_enum_output_types(size_t idx, const char **id)
  772. {
  773. if (!obs) return false;
  774. if (idx >= obs->output_types.num)
  775. return false;
  776. *id = obs->output_types.array[idx].id;
  777. return true;
  778. }
  779. bool obs_enum_encoder_types(size_t idx, const char **id)
  780. {
  781. if (!obs) return false;
  782. if (idx >= obs->encoder_types.num)
  783. return false;
  784. *id = obs->encoder_types.array[idx].id;
  785. return true;
  786. }
  787. bool obs_enum_service_types(size_t idx, const char **id)
  788. {
  789. if (!obs) return false;
  790. if (idx >= obs->service_types.num)
  791. return false;
  792. *id = obs->service_types.array[idx].id;
  793. return true;
  794. }
  795. void obs_enter_graphics(void)
  796. {
  797. if (obs && obs->video.graphics)
  798. gs_enter_context(obs->video.graphics);
  799. }
  800. void obs_leave_graphics(void)
  801. {
  802. if (obs && obs->video.graphics)
  803. gs_leave_context();
  804. }
  805. audio_t *obs_get_audio(void)
  806. {
  807. return (obs != NULL) ? obs->audio.audio : NULL;
  808. }
  809. video_t *obs_get_video(void)
  810. {
  811. return (obs != NULL) ? obs->video.video : NULL;
  812. }
  813. /* TODO: optimize this later so it's not just O(N) string lookups */
  814. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  815. const char *task, const char *target)
  816. {
  817. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  818. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  819. if (strcmp(callback->id, id) == 0 &&
  820. strcmp(callback->task, task) == 0 &&
  821. strcmp(callback->target, target) == 0)
  822. return callback;
  823. }
  824. return NULL;
  825. }
  826. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  827. const char *task, const char *target)
  828. {
  829. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  830. struct obs_modeless_ui *callback;
  831. callback = obs->modeless_ui_callbacks.array+i;
  832. if (strcmp(callback->id, id) == 0 &&
  833. strcmp(callback->task, task) == 0 &&
  834. strcmp(callback->target, target) == 0)
  835. return callback;
  836. }
  837. return NULL;
  838. }
  839. int obs_exec_ui(const char *name, const char *task, const char *target,
  840. void *data, void *ui_data)
  841. {
  842. struct obs_modal_ui *callback;
  843. int errorcode = OBS_UI_NOTFOUND;
  844. if (!obs) return errorcode;
  845. callback = get_modal_ui_callback(name, task, target);
  846. if (callback) {
  847. bool success = callback->exec(data, ui_data);
  848. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  849. }
  850. return errorcode;
  851. }
  852. void *obs_create_ui(const char *name, const char *task, const char *target,
  853. void *data, void *ui_data)
  854. {
  855. struct obs_modeless_ui *callback;
  856. if (!obs) return NULL;
  857. callback = get_modeless_ui_callback(name, task, target);
  858. return callback ? callback->create(data, ui_data) : NULL;
  859. }
  860. bool obs_add_source(obs_source_t *source)
  861. {
  862. struct calldata params = {0};
  863. if (!obs) return false;
  864. if (!source) return false;
  865. pthread_mutex_lock(&obs->data.sources_mutex);
  866. da_push_back(obs->data.user_sources, &source);
  867. obs_source_addref(source);
  868. pthread_mutex_unlock(&obs->data.sources_mutex);
  869. calldata_set_ptr(&params, "source", source);
  870. signal_handler_signal(obs->signals, "source_add", &params);
  871. calldata_free(&params);
  872. return true;
  873. }
  874. obs_source_t *obs_get_output_source(uint32_t channel)
  875. {
  876. if (!obs) return NULL;
  877. return obs_view_get_source(&obs->data.main_view, channel);
  878. }
  879. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  880. {
  881. assert(channel < MAX_CHANNELS);
  882. if (!obs) return;
  883. if (channel >= MAX_CHANNELS) return;
  884. struct obs_source *prev_source;
  885. struct obs_view *view = &obs->data.main_view;
  886. struct calldata params = {0};
  887. pthread_mutex_lock(&view->channels_mutex);
  888. obs_source_addref(source);
  889. prev_source = view->channels[channel];
  890. calldata_set_int(&params, "channel", channel);
  891. calldata_set_ptr(&params, "prev_source", prev_source);
  892. calldata_set_ptr(&params, "source", source);
  893. signal_handler_signal(obs->signals, "channel_change", &params);
  894. calldata_get_ptr(&params, "source", &source);
  895. calldata_free(&params);
  896. view->channels[channel] = source;
  897. pthread_mutex_unlock(&view->channels_mutex);
  898. if (source)
  899. obs_source_activate(source, MAIN_VIEW);
  900. if (prev_source) {
  901. obs_source_deactivate(prev_source, MAIN_VIEW);
  902. obs_source_release(prev_source);
  903. }
  904. }
  905. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  906. {
  907. if (!obs) return;
  908. pthread_mutex_lock(&obs->data.user_sources_mutex);
  909. for (size_t i = 0; i < obs->data.user_sources.num; i++) {
  910. struct obs_source *source = obs->data.user_sources.array[i];
  911. if (!enum_proc(param, source))
  912. break;
  913. }
  914. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  915. }
  916. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  917. void *param)
  918. {
  919. struct obs_context_data **start = pstart, *context;
  920. bool (*enum_proc)(void*, void*) = proc;
  921. assert(start);
  922. assert(mutex);
  923. assert(enum_proc);
  924. pthread_mutex_lock(mutex);
  925. context = *start;
  926. while (context) {
  927. if (!enum_proc(param, context))
  928. break;
  929. context = context->next;
  930. }
  931. pthread_mutex_unlock(mutex);
  932. }
  933. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  934. {
  935. if (!obs) return;
  936. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  937. enum_proc, param);
  938. }
  939. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  940. {
  941. if (!obs) return;
  942. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  943. enum_proc, param);
  944. }
  945. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  946. {
  947. if (!obs) return;
  948. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  949. enum_proc, param);
  950. }
  951. obs_source_t *obs_get_source_by_name(const char *name)
  952. {
  953. struct obs_core_data *data = &obs->data;
  954. struct obs_source *source = NULL;
  955. size_t i;
  956. if (!obs) return NULL;
  957. pthread_mutex_lock(&data->user_sources_mutex);
  958. for (i = 0; i < data->user_sources.num; i++) {
  959. struct obs_source *cur_source = data->user_sources.array[i];
  960. if (strcmp(cur_source->context.name, name) == 0) {
  961. source = cur_source;
  962. obs_source_addref(source);
  963. break;
  964. }
  965. }
  966. pthread_mutex_unlock(&data->user_sources_mutex);
  967. return source;
  968. }
  969. static inline void *get_context_by_name(void *vfirst, const char *name,
  970. pthread_mutex_t *mutex, void *(*addref)(void*))
  971. {
  972. struct obs_context_data **first = vfirst;
  973. struct obs_context_data *context;
  974. pthread_mutex_lock(mutex);
  975. context = *first;
  976. while (context) {
  977. if (strcmp(context->name, name) == 0) {
  978. context = addref(context);
  979. break;
  980. }
  981. context = context->next;
  982. }
  983. pthread_mutex_unlock(mutex);
  984. return context;
  985. }
  986. static inline void *obs_output_addref_safe_(void *ref)
  987. {
  988. return obs_output_get_ref(ref);
  989. }
  990. static inline void *obs_encoder_addref_safe_(void *ref)
  991. {
  992. return obs_encoder_get_ref(ref);
  993. }
  994. static inline void *obs_service_addref_safe_(void *ref)
  995. {
  996. return obs_service_get_ref(ref);
  997. }
  998. static inline void *obs_id_(void *data)
  999. {
  1000. return data;
  1001. }
  1002. obs_output_t *obs_get_output_by_name(const char *name)
  1003. {
  1004. if (!obs) return NULL;
  1005. return get_context_by_name(&obs->data.first_output, name,
  1006. &obs->data.outputs_mutex, obs_output_addref_safe_);
  1007. }
  1008. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1009. {
  1010. if (!obs) return NULL;
  1011. return get_context_by_name(&obs->data.first_encoder, name,
  1012. &obs->data.encoders_mutex, obs_encoder_addref_safe_);
  1013. }
  1014. obs_service_t *obs_get_service_by_name(const char *name)
  1015. {
  1016. if (!obs) return NULL;
  1017. return get_context_by_name(&obs->data.first_service, name,
  1018. &obs->data.services_mutex, obs_service_addref_safe_);
  1019. }
  1020. gs_effect_t *obs_get_default_effect(void)
  1021. {
  1022. if (!obs) return NULL;
  1023. return obs->video.default_effect;
  1024. }
  1025. gs_effect_t *obs_get_default_rect_effect(void)
  1026. {
  1027. if (!obs) return NULL;
  1028. return obs->video.default_rect_effect;
  1029. }
  1030. gs_effect_t *obs_get_opaque_effect(void)
  1031. {
  1032. if (!obs) return NULL;
  1033. return obs->video.opaque_effect;
  1034. }
  1035. gs_effect_t *obs_get_solid_effect(void)
  1036. {
  1037. if (!obs) return NULL;
  1038. return obs->video.solid_effect;
  1039. }
  1040. gs_effect_t *obs_get_bicubic_effect(void)
  1041. {
  1042. if (!obs) return NULL;
  1043. return obs->video.bicubic_effect;
  1044. }
  1045. gs_effect_t *obs_get_lanczos_effect(void)
  1046. {
  1047. if (!obs) return NULL;
  1048. return obs->video.lanczos_effect;
  1049. }
  1050. gs_effect_t *obs_get_bilinear_lowres_effect(void)
  1051. {
  1052. if (!obs) return NULL;
  1053. return obs->video.bilinear_lowres_effect;
  1054. }
  1055. signal_handler_t *obs_get_signal_handler(void)
  1056. {
  1057. if (!obs) return NULL;
  1058. return obs->signals;
  1059. }
  1060. proc_handler_t *obs_get_proc_handler(void)
  1061. {
  1062. if (!obs) return NULL;
  1063. return obs->procs;
  1064. }
  1065. void obs_add_draw_callback(
  1066. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1067. void *param)
  1068. {
  1069. if (!obs) return;
  1070. obs_display_add_draw_callback(&obs->video.main_display, draw, param);
  1071. }
  1072. void obs_remove_draw_callback(
  1073. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1074. void *param)
  1075. {
  1076. if (!obs) return;
  1077. obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
  1078. }
  1079. void obs_resize(uint32_t cx, uint32_t cy)
  1080. {
  1081. if (!obs || !obs->video.video || !obs->video.graphics) return;
  1082. obs_display_resize(&obs->video.main_display, cx, cy);
  1083. }
  1084. void obs_render_main_view(void)
  1085. {
  1086. if (!obs) return;
  1087. obs_view_render(&obs->data.main_view);
  1088. }
  1089. void obs_set_master_volume(float volume)
  1090. {
  1091. struct calldata data = {0};
  1092. if (!obs) return;
  1093. calldata_set_float(&data, "volume", volume);
  1094. signal_handler_signal(obs->signals, "master_volume", &data);
  1095. volume = (float)calldata_float(&data, "volume");
  1096. calldata_free(&data);
  1097. obs->audio.user_volume = volume;
  1098. }
  1099. void obs_set_present_volume(float volume)
  1100. {
  1101. if (!obs) return;
  1102. obs->audio.present_volume = volume;
  1103. }
  1104. float obs_get_master_volume(void)
  1105. {
  1106. return obs ? obs->audio.user_volume : 0.0f;
  1107. }
  1108. float obs_get_present_volume(void)
  1109. {
  1110. return obs ? obs->audio.present_volume : 0.0f;
  1111. }
  1112. static obs_source_t *obs_load_source_type(obs_data_t *source_data,
  1113. enum obs_source_type type)
  1114. {
  1115. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1116. obs_source_t *source;
  1117. const char *name = obs_data_get_string(source_data, "name");
  1118. const char *id = obs_data_get_string(source_data, "id");
  1119. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1120. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1121. double volume;
  1122. int64_t sync;
  1123. uint32_t flags;
  1124. uint32_t mixers;
  1125. source = obs_source_create(type, id, name, settings, hotkeys);
  1126. obs_data_release(hotkeys);
  1127. obs_data_set_default_double(source_data, "volume", 1.0);
  1128. volume = obs_data_get_double(source_data, "volume");
  1129. obs_source_set_volume(source, (float)volume);
  1130. sync = obs_data_get_int(source_data, "sync");
  1131. obs_source_set_sync_offset(source, sync);
  1132. obs_data_set_default_int(source_data, "mixers", 0xF);
  1133. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1134. obs_source_set_audio_mixers(source, mixers);
  1135. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1136. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1137. obs_source_set_flags(source, flags);
  1138. obs_data_set_default_bool(source_data, "enabled", true);
  1139. obs_source_set_enabled(source,
  1140. obs_data_get_bool(source_data, "enabled"));
  1141. obs_data_set_default_bool(source_data, "muted", false);
  1142. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1143. if (filters) {
  1144. size_t count = obs_data_array_count(filters);
  1145. for (size_t i = 0; i < count; i++) {
  1146. obs_data_t *filter_data =
  1147. obs_data_array_item(filters, i);
  1148. obs_source_t *filter = obs_load_source_type(
  1149. filter_data, OBS_SOURCE_TYPE_FILTER);
  1150. if (filter) {
  1151. obs_source_filter_add(source, filter);
  1152. obs_source_release(filter);
  1153. }
  1154. obs_data_release(filter_data);
  1155. }
  1156. obs_data_array_release(filters);
  1157. }
  1158. obs_data_release(settings);
  1159. return source;
  1160. }
  1161. obs_source_t *obs_load_source(obs_data_t *source_data)
  1162. {
  1163. return obs_load_source_type(source_data, OBS_SOURCE_TYPE_INPUT);
  1164. }
  1165. void obs_load_sources(obs_data_array_t *array)
  1166. {
  1167. size_t count;
  1168. size_t i;
  1169. if (!obs) return;
  1170. count = obs_data_array_count(array);
  1171. pthread_mutex_lock(&obs->data.user_sources_mutex);
  1172. for (i = 0; i < count; i++) {
  1173. obs_data_t *source_data = obs_data_array_item(array, i);
  1174. obs_source_t *source = obs_load_source(source_data);
  1175. obs_add_source(source);
  1176. obs_source_release(source);
  1177. obs_data_release(source_data);
  1178. }
  1179. /* tell sources that we want to load */
  1180. for (i = 0; i < obs->data.user_sources.num; i++)
  1181. obs_source_load(obs->data.user_sources.array[i]);
  1182. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  1183. }
  1184. obs_data_t *obs_save_source(obs_source_t *source)
  1185. {
  1186. obs_data_array_t *filters = obs_data_array_create();
  1187. obs_data_t *source_data = obs_data_create();
  1188. obs_data_t *settings = obs_source_get_settings(source);
  1189. obs_data_t *hotkey_data = source->context.hotkey_data;
  1190. obs_data_t *hotkeys;
  1191. float volume = obs_source_get_volume(source);
  1192. uint32_t mixers = obs_source_get_audio_mixers(source);
  1193. int64_t sync = obs_source_get_sync_offset(source);
  1194. uint32_t flags = obs_source_get_flags(source);
  1195. const char *name = obs_source_get_name(source);
  1196. const char *id = obs_source_get_id(source);
  1197. bool enabled = obs_source_enabled(source);
  1198. bool muted = obs_source_muted(source);
  1199. obs_source_save(source);
  1200. hotkeys = obs_hotkeys_save_source(source);
  1201. if (hotkeys) {
  1202. obs_data_release(hotkey_data);
  1203. source->context.hotkey_data = hotkeys;
  1204. hotkey_data = hotkeys;
  1205. }
  1206. obs_data_set_string(source_data, "name", name);
  1207. obs_data_set_string(source_data, "id", id);
  1208. obs_data_set_obj (source_data, "settings", settings);
  1209. obs_data_set_int (source_data, "mixers", mixers);
  1210. obs_data_set_int (source_data, "sync", sync);
  1211. obs_data_set_int (source_data, "flags", flags);
  1212. obs_data_set_double(source_data, "volume", volume);
  1213. obs_data_set_bool (source_data, "enabled", enabled);
  1214. obs_data_set_bool (source_data, "muted", muted);
  1215. obs_data_set_obj (source_data, "hotkeys", hotkey_data);
  1216. pthread_mutex_lock(&source->filter_mutex);
  1217. if (source->filters.num) {
  1218. for (size_t i = source->filters.num; i > 0; i--) {
  1219. obs_source_t *filter = source->filters.array[i - 1];
  1220. obs_data_t *filter_data = obs_save_source(filter);
  1221. obs_data_array_push_back(filters, filter_data);
  1222. obs_data_release(filter_data);
  1223. }
  1224. obs_data_set_array(source_data, "filters", filters);
  1225. }
  1226. pthread_mutex_unlock(&source->filter_mutex);
  1227. obs_data_release(settings);
  1228. obs_data_array_release(filters);
  1229. return source_data;
  1230. }
  1231. obs_data_array_t *obs_save_sources(void)
  1232. {
  1233. obs_data_array_t *array;
  1234. size_t i;
  1235. if (!obs) return NULL;
  1236. array = obs_data_array_create();
  1237. pthread_mutex_lock(&obs->data.user_sources_mutex);
  1238. for (i = 0; i < obs->data.user_sources.num; i++) {
  1239. obs_source_t *source = obs->data.user_sources.array[i];
  1240. obs_data_t *source_data = obs_save_source(source);
  1241. obs_data_array_push_back(array, source_data);
  1242. obs_data_release(source_data);
  1243. }
  1244. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  1245. return array;
  1246. }
  1247. /* ensures that names are never blank */
  1248. static inline char *dup_name(const char *name)
  1249. {
  1250. if (!name || !*name) {
  1251. struct dstr unnamed = {0};
  1252. dstr_printf(&unnamed, "__unnamed%004lld",
  1253. obs->data.unnamed_index++);
  1254. return unnamed.array;
  1255. } else {
  1256. return bstrdup(name);
  1257. }
  1258. }
  1259. static inline bool obs_context_data_init_wrap(
  1260. struct obs_context_data *context,
  1261. obs_data_t *settings,
  1262. const char *name,
  1263. obs_data_t *hotkey_data)
  1264. {
  1265. assert(context);
  1266. memset(context, 0, sizeof(*context));
  1267. pthread_mutex_init_value(&context->rename_cache_mutex);
  1268. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1269. return false;
  1270. context->signals = signal_handler_create();
  1271. if (!context->signals)
  1272. return false;
  1273. context->procs = proc_handler_create();
  1274. if (!context->procs)
  1275. return false;
  1276. context->name = dup_name(name);
  1277. context->settings = obs_data_newref(settings);
  1278. context->hotkey_data = obs_data_newref(hotkey_data);
  1279. return true;
  1280. }
  1281. bool obs_context_data_init(
  1282. struct obs_context_data *context,
  1283. obs_data_t *settings,
  1284. const char *name,
  1285. obs_data_t *hotkey_data)
  1286. {
  1287. if (obs_context_data_init_wrap(context, settings, name, hotkey_data)) {
  1288. return true;
  1289. } else {
  1290. obs_context_data_free(context);
  1291. return false;
  1292. }
  1293. }
  1294. void obs_context_data_free(struct obs_context_data *context)
  1295. {
  1296. obs_hotkeys_context_release(context);
  1297. signal_handler_destroy(context->signals);
  1298. proc_handler_destroy(context->procs);
  1299. obs_data_release(context->settings);
  1300. obs_context_data_remove(context);
  1301. pthread_mutex_destroy(&context->rename_cache_mutex);
  1302. bfree(context->name);
  1303. for (size_t i = 0; i < context->rename_cache.num; i++)
  1304. bfree(context->rename_cache.array[i]);
  1305. da_free(context->rename_cache);
  1306. memset(context, 0, sizeof(*context));
  1307. }
  1308. void obs_context_data_insert(struct obs_context_data *context,
  1309. pthread_mutex_t *mutex, void *pfirst)
  1310. {
  1311. struct obs_context_data **first = pfirst;
  1312. assert(context);
  1313. assert(mutex);
  1314. assert(first);
  1315. context->mutex = mutex;
  1316. pthread_mutex_lock(mutex);
  1317. context->prev_next = first;
  1318. context->next = *first;
  1319. *first = context;
  1320. if (context->next)
  1321. context->next->prev_next = &context->next;
  1322. pthread_mutex_unlock(mutex);
  1323. }
  1324. void obs_context_data_remove(struct obs_context_data *context)
  1325. {
  1326. if (context && context->mutex) {
  1327. pthread_mutex_lock(context->mutex);
  1328. if (context->prev_next)
  1329. *context->prev_next = context->next;
  1330. if (context->next)
  1331. context->next->prev_next = context->prev_next;
  1332. pthread_mutex_unlock(context->mutex);
  1333. context->mutex = NULL;
  1334. }
  1335. }
  1336. void obs_context_data_setname(struct obs_context_data *context,
  1337. const char *name)
  1338. {
  1339. pthread_mutex_lock(&context->rename_cache_mutex);
  1340. if (context->name)
  1341. da_push_back(context->rename_cache, &context->name);
  1342. context->name = dup_name(name);
  1343. pthread_mutex_unlock(&context->rename_cache_mutex);
  1344. }
  1345. void obs_preview_set_enabled(bool enable)
  1346. {
  1347. if (obs)
  1348. obs->video.main_display.enabled = enable;
  1349. }
  1350. bool obs_preview_enabled(void)
  1351. {
  1352. return obs ? obs->video.main_display.enabled : false;
  1353. }