obs.c 32 KB

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