obs.c 31 KB

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