obs.c 36 KB

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