obs.c 39 KB

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