obs.c 43 KB

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