obs.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363
  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_create_texture(
  116. ovi->output_width, video->conversion_height,
  117. GS_RGBA, 1, NULL, GS_RENDERTARGET);
  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_create_stagesurface(
  132. ovi->output_width, output_height, GS_RGBA);
  133. if (!video->copy_surfaces[i])
  134. return false;
  135. video->render_textures[i] = gs_create_texture(
  136. ovi->base_width, ovi->base_height,
  137. GS_RGBA, 1, NULL, GS_RENDERTARGET);
  138. if (!video->render_textures[i])
  139. return false;
  140. video->output_textures[i] = gs_create_texture(
  141. ovi->output_width, ovi->output_height,
  142. GS_RGBA, 1, NULL, GS_RENDERTARGET);
  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_entercontext(video->graphics);
  172. char *filename = find_libobs_data_file("default.effect");
  173. video->default_effect = gs_create_effect_from_file(filename,
  174. NULL);
  175. bfree(filename);
  176. filename = find_libobs_data_file("solid.effect");
  177. video->solid_effect = gs_create_effect_from_file(filename,
  178. NULL);
  179. bfree(filename);
  180. filename = find_libobs_data_file("format_conversion.effect");
  181. video->conversion_effect = gs_create_effect_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_leavecontext();
  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_entercontext(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_leavecontext();
  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. uint32_t total_frames = video_output_total_frames(video->video);
  248. uint32_t skipped_frames =
  249. video_output_num_skipped_frames(video->video);
  250. double percentage_skipped =
  251. (double)skipped_frames / (double)total_frames * 100.0;
  252. obs_display_free(&video->main_display);
  253. blog(LOG_INFO, "Video session ending");
  254. blog(LOG_INFO, "Total frames: %"PRIu32, total_frames);
  255. if (total_frames) {
  256. blog(LOG_INFO, "Number of skipped frames: "
  257. "%"PRIu32" (%g%%)",
  258. skipped_frames, percentage_skipped);
  259. }
  260. blog(LOG_INFO, "-------------------------------------------");
  261. video_output_close(video->video);
  262. video->video = NULL;
  263. if (!video->graphics)
  264. return;
  265. gs_entercontext(video->graphics);
  266. if (video->mapped_surface) {
  267. stagesurface_unmap(video->mapped_surface);
  268. video->mapped_surface = NULL;
  269. }
  270. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  271. stagesurface_destroy(video->copy_surfaces[i]);
  272. texture_destroy(video->render_textures[i]);
  273. texture_destroy(video->convert_textures[i]);
  274. texture_destroy(video->output_textures[i]);
  275. obs_source_frame_free(&video->convert_frames[i]);
  276. video->copy_surfaces[i] = NULL;
  277. video->render_textures[i] = NULL;
  278. video->convert_textures[i] = NULL;
  279. video->output_textures[i] = NULL;
  280. }
  281. gs_leavecontext();
  282. video->cur_texture = 0;
  283. }
  284. }
  285. static void obs_free_graphics(void)
  286. {
  287. struct obs_core_video *video = &obs->video;
  288. if (video->graphics) {
  289. gs_entercontext(video->graphics);
  290. effect_destroy(video->default_effect);
  291. effect_destroy(video->solid_effect);
  292. effect_destroy(video->conversion_effect);
  293. video->default_effect = NULL;
  294. gs_leavecontext();
  295. gs_destroy(video->graphics);
  296. video->graphics = NULL;
  297. }
  298. }
  299. static bool obs_init_audio(struct audio_output_info *ai)
  300. {
  301. struct obs_core_audio *audio = &obs->audio;
  302. int errorcode;
  303. /* TODO: sound subsystem */
  304. audio->user_volume = 1.0f;
  305. audio->present_volume = 1.0f;
  306. errorcode = audio_output_open(&audio->audio, ai);
  307. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  308. return true;
  309. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  310. blog(LOG_ERROR, "Invalid audio parameters specified");
  311. else
  312. blog(LOG_ERROR, "Could not open audio output");
  313. return false;
  314. }
  315. static void obs_free_audio(void)
  316. {
  317. struct obs_core_audio *audio = &obs->audio;
  318. if (audio->audio)
  319. audio_output_close(audio->audio);
  320. memset(audio, 0, sizeof(struct obs_core_audio));
  321. }
  322. static bool obs_init_data(void)
  323. {
  324. struct obs_core_data *data = &obs->data;
  325. pthread_mutexattr_t attr;
  326. assert(data != NULL);
  327. pthread_mutex_init_value(&obs->data.displays_mutex);
  328. if (pthread_mutexattr_init(&attr) != 0)
  329. return false;
  330. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  331. goto fail;
  332. if (pthread_mutex_init(&data->user_sources_mutex, &attr) != 0)
  333. goto fail;
  334. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  335. goto fail;
  336. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  337. goto fail;
  338. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  339. goto fail;
  340. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  341. goto fail;
  342. if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
  343. goto fail;
  344. if (!obs_view_init(&data->main_view))
  345. goto fail;
  346. data->valid = true;
  347. fail:
  348. pthread_mutexattr_destroy(&attr);
  349. return data->valid;
  350. }
  351. #define FREE_OBS_LINKED_LIST(type) \
  352. do { \
  353. int unfreed = 0; \
  354. while (data->first_ ## type ) { \
  355. obs_ ## type ## _destroy(data->first_ ## type ); \
  356. unfreed++; \
  357. } \
  358. if (unfreed) \
  359. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  360. unfreed); \
  361. } while (false)
  362. static void obs_free_data(void)
  363. {
  364. struct obs_core_data *data = &obs->data;
  365. data->valid = false;
  366. obs_view_free(&data->main_view);
  367. blog(LOG_INFO, "Freeing OBS context data");
  368. if (data->user_sources.num)
  369. blog(LOG_INFO, "\t%d user source(s) were remaining",
  370. (int)data->user_sources.num);
  371. while (data->user_sources.num)
  372. obs_source_remove(data->user_sources.array[0]);
  373. da_free(data->user_sources);
  374. FREE_OBS_LINKED_LIST(source);
  375. FREE_OBS_LINKED_LIST(output);
  376. FREE_OBS_LINKED_LIST(encoder);
  377. FREE_OBS_LINKED_LIST(display);
  378. FREE_OBS_LINKED_LIST(service);
  379. pthread_mutex_destroy(&data->user_sources_mutex);
  380. pthread_mutex_destroy(&data->sources_mutex);
  381. pthread_mutex_destroy(&data->displays_mutex);
  382. pthread_mutex_destroy(&data->outputs_mutex);
  383. pthread_mutex_destroy(&data->encoders_mutex);
  384. pthread_mutex_destroy(&data->services_mutex);
  385. }
  386. static const char *obs_signals[] = {
  387. "void source_create(ptr source)",
  388. "void source_destroy(ptr source)",
  389. "void source_add(ptr source)",
  390. "void source_remove(ptr source)",
  391. "void source_activate(ptr source)",
  392. "void source_deactivate(ptr source)",
  393. "void source_show(ptr source)",
  394. "void source_hide(ptr source)",
  395. "void source_rename(ptr source, string new_name, string prev_name)",
  396. "void source_volume(ptr source, in out float volume)",
  397. "void source_volume_level(ptr source, float level, float magnitude, "
  398. "float peak)",
  399. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  400. "void master_volume(in out float volume)",
  401. NULL
  402. };
  403. static inline bool obs_init_handlers(void)
  404. {
  405. obs->signals = signal_handler_create();
  406. if (!obs->signals)
  407. return false;
  408. obs->procs = proc_handler_create();
  409. if (!obs->procs)
  410. return false;
  411. return signal_handler_add_array(obs->signals, obs_signals);
  412. }
  413. extern const struct obs_source_info scene_info;
  414. extern void log_system_info(void);
  415. static bool obs_init(const char *locale)
  416. {
  417. obs = bzalloc(sizeof(struct obs_core));
  418. log_system_info();
  419. if (!obs_init_data())
  420. return false;
  421. if (!obs_init_handlers())
  422. return false;
  423. obs->locale = bstrdup(locale);
  424. obs_register_source(&scene_info);
  425. add_default_module_paths();
  426. return true;
  427. }
  428. bool obs_startup(const char *locale)
  429. {
  430. bool success;
  431. if (obs) {
  432. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  433. return false;
  434. }
  435. success = obs_init(locale);
  436. if (!success)
  437. obs_shutdown();
  438. return success;
  439. }
  440. void obs_shutdown(void)
  441. {
  442. struct obs_module *module;
  443. if (!obs)
  444. return;
  445. da_free(obs->input_types);
  446. da_free(obs->filter_types);
  447. da_free(obs->encoder_types);
  448. da_free(obs->transition_types);
  449. da_free(obs->output_types);
  450. da_free(obs->service_types);
  451. da_free(obs->modal_ui_callbacks);
  452. da_free(obs->modeless_ui_callbacks);
  453. stop_video();
  454. obs_free_data();
  455. obs_free_video();
  456. obs_free_graphics();
  457. obs_free_audio();
  458. proc_handler_destroy(obs->procs);
  459. signal_handler_destroy(obs->signals);
  460. module = obs->first_module;
  461. while (module) {
  462. struct obs_module *next = module->next;
  463. free_module(module);
  464. module = next;
  465. }
  466. obs->first_module = NULL;
  467. for (size_t i = 0; i < obs->module_paths.num; i++)
  468. free_module_path(obs->module_paths.array+i);
  469. da_free(obs->module_paths);
  470. bfree(obs->locale);
  471. bfree(obs);
  472. obs = NULL;
  473. }
  474. bool obs_initialized(void)
  475. {
  476. return obs != NULL;
  477. }
  478. uint32_t obs_get_version(void)
  479. {
  480. return LIBOBS_API_VER;
  481. }
  482. void obs_set_locale(const char *locale)
  483. {
  484. struct obs_module *module;
  485. if (!obs)
  486. return;
  487. if (obs->locale)
  488. bfree(obs->locale);
  489. obs->locale = bstrdup(locale);
  490. module = obs->first_module;
  491. while (module) {
  492. if (module->set_locale)
  493. module->set_locale(locale);
  494. module = module->next;
  495. }
  496. }
  497. const char *obs_get_locale(void)
  498. {
  499. return obs ? obs->locale : NULL;
  500. }
  501. #define OBS_SIZE_MIN 2
  502. #define OBS_SIZE_MAX (32 * 1024)
  503. static inline bool size_valid(uint32_t width, uint32_t height)
  504. {
  505. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  506. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  507. }
  508. int obs_reset_video(struct obs_video_info *ovi)
  509. {
  510. if (!obs) return OBS_VIDEO_FAIL;
  511. /* don't allow changing of video settings if active. */
  512. if (obs->video.video && video_output_active(obs->video.video))
  513. return OBS_VIDEO_CURRENTLY_ACTIVE;
  514. if (!size_valid(ovi->output_width, ovi->output_height) ||
  515. !size_valid(ovi->base_width, ovi->base_height))
  516. return OBS_VIDEO_INVALID_PARAM;
  517. struct obs_core_video *video = &obs->video;
  518. stop_video();
  519. obs_free_video();
  520. if (!ovi) {
  521. obs_free_graphics();
  522. return OBS_VIDEO_SUCCESS;
  523. }
  524. /* align to multiple-of-two and SSE alignment sizes */
  525. ovi->output_width &= 0xFFFFFFFC;
  526. ovi->output_height &= 0xFFFFFFFE;
  527. if (!video->graphics) {
  528. int errorcode = obs_init_graphics(ovi);
  529. if (errorcode != OBS_VIDEO_SUCCESS)
  530. return errorcode;
  531. }
  532. blog(LOG_INFO, "video settings reset:\n"
  533. "\tbase resolution: %dx%d\n"
  534. "\toutput resolution: %dx%d\n"
  535. "\tfps: %d/%d",
  536. ovi->base_width, ovi->base_height,
  537. ovi->output_width, ovi->output_height,
  538. ovi->fps_num, ovi->fps_den);
  539. return obs_init_video(ovi);
  540. }
  541. bool obs_reset_audio(struct audio_output_info *ai)
  542. {
  543. if (!obs) return false;
  544. /* don't allow changing of audio settings if active. */
  545. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  546. return false;
  547. obs_free_audio();
  548. if(!ai)
  549. return true;
  550. blog(LOG_INFO, "audio settings reset:\n"
  551. "\tsamples per sec: %d\n"
  552. "\tspeakers: %d\n"
  553. "\tbuffering (ms): %d\n",
  554. (int)ai->samples_per_sec,
  555. (int)ai->speakers,
  556. (int)ai->buffer_ms);
  557. return obs_init_audio(ai);
  558. }
  559. bool obs_get_video_info(struct obs_video_info *ovi)
  560. {
  561. struct obs_core_video *video = &obs->video;
  562. const struct video_output_info *info;
  563. if (!obs || !video->graphics)
  564. return false;
  565. info = video_output_getinfo(video->video);
  566. memset(ovi, 0, sizeof(struct obs_video_info));
  567. ovi->base_width = video->base_width;
  568. ovi->base_height = video->base_height;
  569. ovi->output_width = info->width;
  570. ovi->output_height = info->height;
  571. ovi->output_format = info->format;
  572. ovi->fps_num = info->fps_num;
  573. ovi->fps_den = info->fps_den;
  574. return true;
  575. }
  576. bool obs_get_audio_info(struct audio_output_info *aoi)
  577. {
  578. struct obs_core_audio *audio = &obs->audio;
  579. const struct audio_output_info *info;
  580. if (!obs || !audio->audio)
  581. return false;
  582. info = audio_output_getinfo(audio->audio);
  583. memcpy(aoi, info, sizeof(struct audio_output_info));
  584. return true;
  585. }
  586. bool obs_enum_input_types(size_t idx, const char **id)
  587. {
  588. if (!obs) return false;
  589. if (idx >= obs->input_types.num)
  590. return false;
  591. *id = obs->input_types.array[idx].id;
  592. return true;
  593. }
  594. bool obs_enum_filter_types(size_t idx, const char **id)
  595. {
  596. if (!obs) return false;
  597. if (idx >= obs->filter_types.num)
  598. return false;
  599. *id = obs->filter_types.array[idx].id;
  600. return true;
  601. }
  602. bool obs_enum_transition_types(size_t idx, const char **id)
  603. {
  604. if (!obs) return false;
  605. if (idx >= obs->transition_types.num)
  606. return false;
  607. *id = obs->transition_types.array[idx].id;
  608. return true;
  609. }
  610. bool obs_enum_output_types(size_t idx, const char **id)
  611. {
  612. if (!obs) return false;
  613. if (idx >= obs->output_types.num)
  614. return false;
  615. *id = obs->output_types.array[idx].id;
  616. return true;
  617. }
  618. bool obs_enum_encoder_types(size_t idx, const char **id)
  619. {
  620. if (!obs) return false;
  621. if (idx >= obs->encoder_types.num)
  622. return false;
  623. *id = obs->encoder_types.array[idx].id;
  624. return true;
  625. }
  626. bool obs_enum_service_types(size_t idx, const char **id)
  627. {
  628. if (!obs) return false;
  629. if (idx >= obs->service_types.num)
  630. return false;
  631. *id = obs->service_types.array[idx].id;
  632. return true;
  633. }
  634. void obs_enter_graphics(void)
  635. {
  636. if (obs && obs->video.graphics)
  637. gs_entercontext(obs->video.graphics);
  638. }
  639. void obs_leave_graphics(void)
  640. {
  641. if (obs && obs->video.graphics)
  642. gs_leavecontext();
  643. }
  644. audio_t obs_get_audio(void)
  645. {
  646. return (obs != NULL) ? obs->audio.audio : NULL;
  647. }
  648. video_t obs_get_video(void)
  649. {
  650. return (obs != NULL) ? obs->video.video : NULL;
  651. }
  652. /* TODO: optimize this later so it's not just O(N) string lookups */
  653. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  654. const char *task, const char *target)
  655. {
  656. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  657. struct obs_modal_ui *callback = obs->modal_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. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  666. const char *task, const char *target)
  667. {
  668. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  669. struct obs_modeless_ui *callback;
  670. callback = obs->modeless_ui_callbacks.array+i;
  671. if (strcmp(callback->id, id) == 0 &&
  672. strcmp(callback->task, task) == 0 &&
  673. strcmp(callback->target, target) == 0)
  674. return callback;
  675. }
  676. return NULL;
  677. }
  678. int obs_exec_ui(const char *name, const char *task, const char *target,
  679. void *data, void *ui_data)
  680. {
  681. struct obs_modal_ui *callback;
  682. int errorcode = OBS_UI_NOTFOUND;
  683. if (!obs) return errorcode;
  684. callback = get_modal_ui_callback(name, task, target);
  685. if (callback) {
  686. bool success = callback->exec(data, ui_data);
  687. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  688. }
  689. return errorcode;
  690. }
  691. void *obs_create_ui(const char *name, const char *task, const char *target,
  692. void *data, void *ui_data)
  693. {
  694. struct obs_modeless_ui *callback;
  695. if (!obs) return NULL;
  696. callback = get_modeless_ui_callback(name, task, target);
  697. return callback ? callback->create(data, ui_data) : NULL;
  698. }
  699. bool obs_add_source(obs_source_t source)
  700. {
  701. struct calldata params = {0};
  702. if (!obs) return false;
  703. if (!source) return false;
  704. pthread_mutex_lock(&obs->data.sources_mutex);
  705. da_push_back(obs->data.user_sources, &source);
  706. obs_source_addref(source);
  707. pthread_mutex_unlock(&obs->data.sources_mutex);
  708. calldata_setptr(&params, "source", source);
  709. signal_handler_signal(obs->signals, "source_add", &params);
  710. calldata_free(&params);
  711. return true;
  712. }
  713. obs_source_t obs_get_output_source(uint32_t channel)
  714. {
  715. if (!obs) return NULL;
  716. return obs_view_get_source(&obs->data.main_view, channel);
  717. }
  718. void obs_set_output_source(uint32_t channel, obs_source_t source)
  719. {
  720. assert(channel < MAX_CHANNELS);
  721. if (!obs) return;
  722. if (channel >= MAX_CHANNELS) return;
  723. struct obs_source *prev_source;
  724. struct obs_view *view = &obs->data.main_view;
  725. struct calldata params = {0};
  726. pthread_mutex_lock(&view->channels_mutex);
  727. obs_source_addref(source);
  728. prev_source = view->channels[channel];
  729. calldata_setint(&params, "channel", channel);
  730. calldata_setptr(&params, "prev_source", prev_source);
  731. calldata_setptr(&params, "source", source);
  732. signal_handler_signal(obs->signals, "channel_change", &params);
  733. calldata_getptr(&params, "source", &source);
  734. calldata_free(&params);
  735. view->channels[channel] = source;
  736. pthread_mutex_unlock(&view->channels_mutex);
  737. if (source)
  738. obs_source_activate(source, MAIN_VIEW);
  739. if (prev_source) {
  740. obs_source_deactivate(prev_source, MAIN_VIEW);
  741. obs_source_release(prev_source);
  742. }
  743. }
  744. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t), void *param)
  745. {
  746. if (!obs) return;
  747. pthread_mutex_lock(&obs->data.user_sources_mutex);
  748. for (size_t i = 0; i < obs->data.user_sources.num; i++) {
  749. struct obs_source *source = obs->data.user_sources.array[i];
  750. if (!enum_proc(param, source))
  751. break;
  752. }
  753. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  754. }
  755. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  756. void *param)
  757. {
  758. struct obs_context_data **start = pstart, *context;
  759. bool (*enum_proc)(void*, void*) = proc;
  760. assert(start);
  761. assert(mutex);
  762. assert(enum_proc);
  763. pthread_mutex_lock(mutex);
  764. context = *start;
  765. while (context) {
  766. if (!enum_proc(param, context))
  767. break;
  768. context = context->next;
  769. }
  770. pthread_mutex_unlock(mutex);
  771. }
  772. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t), void *param)
  773. {
  774. if (!obs) return;
  775. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  776. enum_proc, param);
  777. }
  778. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t), void *param)
  779. {
  780. if (!obs) return;
  781. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  782. enum_proc, param);
  783. }
  784. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t), void *param)
  785. {
  786. if (!obs) return;
  787. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  788. enum_proc, param);
  789. }
  790. obs_source_t obs_get_source_by_name(const char *name)
  791. {
  792. struct obs_core_data *data = &obs->data;
  793. struct obs_source *source = NULL;
  794. size_t i;
  795. if (!obs) return NULL;
  796. pthread_mutex_lock(&data->user_sources_mutex);
  797. for (i = 0; i < data->user_sources.num; i++) {
  798. struct obs_source *cur_source = data->user_sources.array[i];
  799. if (strcmp(cur_source->context.name, name) == 0) {
  800. source = cur_source;
  801. obs_source_addref(source);
  802. break;
  803. }
  804. }
  805. pthread_mutex_unlock(&data->user_sources_mutex);
  806. return source;
  807. }
  808. static inline void *get_context_by_name(void *vfirst, const char *name,
  809. pthread_mutex_t *mutex)
  810. {
  811. struct obs_context_data **first = vfirst;
  812. struct obs_context_data *context;
  813. pthread_mutex_lock(mutex);
  814. context = *first;
  815. while (context) {
  816. if (strcmp(context->name, name) == 0)
  817. break;
  818. context = context->next;
  819. }
  820. pthread_mutex_unlock(mutex);
  821. return context;
  822. }
  823. obs_output_t obs_get_output_by_name(const char *name)
  824. {
  825. if (!obs) return NULL;
  826. return get_context_by_name(&obs->data.first_output, name,
  827. &obs->data.outputs_mutex);
  828. }
  829. obs_encoder_t obs_get_encoder_by_name(const char *name)
  830. {
  831. if (!obs) return NULL;
  832. return get_context_by_name(&obs->data.first_encoder, name,
  833. &obs->data.encoders_mutex);
  834. }
  835. obs_service_t obs_get_service_by_name(const char *name)
  836. {
  837. if (!obs) return NULL;
  838. return get_context_by_name(&obs->data.first_service, name,
  839. &obs->data.services_mutex);
  840. }
  841. effect_t obs_get_default_effect(void)
  842. {
  843. if (!obs) return NULL;
  844. return obs->video.default_effect;
  845. }
  846. effect_t obs_get_solid_effect(void)
  847. {
  848. if (!obs) return NULL;
  849. return obs->video.solid_effect;
  850. }
  851. signal_handler_t obs_get_signal_handler(void)
  852. {
  853. if (!obs) return NULL;
  854. return obs->signals;
  855. }
  856. proc_handler_t obs_get_proc_handler(void)
  857. {
  858. if (!obs) return NULL;
  859. return obs->procs;
  860. }
  861. void obs_add_draw_callback(
  862. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  863. void *param)
  864. {
  865. if (!obs) return;
  866. obs_display_add_draw_callback(&obs->video.main_display, draw, param);
  867. }
  868. void obs_remove_draw_callback(
  869. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  870. void *param)
  871. {
  872. if (!obs) return;
  873. obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
  874. }
  875. void obs_resize(uint32_t cx, uint32_t cy)
  876. {
  877. if (!obs || !obs->video.video || !obs->video.graphics) return;
  878. obs_display_resize(&obs->video.main_display, cx, cy);
  879. }
  880. void obs_render_main_view(void)
  881. {
  882. if (!obs) return;
  883. obs_view_render(&obs->data.main_view);
  884. }
  885. void obs_set_master_volume(float volume)
  886. {
  887. struct calldata data = {0};
  888. if (!obs) return;
  889. calldata_setfloat(&data, "volume", volume);
  890. signal_handler_signal(obs->signals, "master_volume", &data);
  891. volume = (float)calldata_float(&data, "volume");
  892. calldata_free(&data);
  893. obs->audio.user_volume = volume;
  894. }
  895. void obs_set_present_volume(float volume)
  896. {
  897. if (!obs) return;
  898. obs->audio.present_volume = volume;
  899. }
  900. float obs_get_master_volume(void)
  901. {
  902. return obs ? obs->audio.user_volume : 0.0f;
  903. }
  904. float obs_get_present_volume(void)
  905. {
  906. return obs ? obs->audio.present_volume : 0.0f;
  907. }
  908. obs_source_t obs_load_source(obs_data_t source_data)
  909. {
  910. obs_source_t source;
  911. const char *name = obs_data_getstring(source_data, "name");
  912. const char *id = obs_data_getstring(source_data, "id");
  913. obs_data_t settings = obs_data_getobj(source_data, "settings");
  914. double volume;
  915. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, id, name, settings);
  916. obs_data_set_default_double(source_data, "volume", 1.0);
  917. volume = obs_data_getdouble(source_data, "volume");
  918. obs_source_set_volume(source, (float)volume);
  919. obs_data_release(settings);
  920. return source;
  921. }
  922. void obs_load_sources(obs_data_array_t array)
  923. {
  924. size_t count;
  925. size_t i;
  926. if (!obs) return;
  927. count = obs_data_array_count(array);
  928. pthread_mutex_lock(&obs->data.user_sources_mutex);
  929. for (i = 0; i < count; i++) {
  930. obs_data_t source_data = obs_data_array_item(array, i);
  931. obs_source_t source = obs_load_source(source_data);
  932. obs_add_source(source);
  933. obs_source_release(source);
  934. obs_data_release(source_data);
  935. }
  936. /* tell sources that we want to load */
  937. for (i = 0; i < obs->data.user_sources.num; i++)
  938. obs_source_load(obs->data.user_sources.array[i]);
  939. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  940. }
  941. obs_data_t obs_save_source(obs_source_t source)
  942. {
  943. obs_data_t source_data = obs_data_create();
  944. obs_data_t settings = obs_source_get_settings(source);
  945. float volume = obs_source_get_volume(source);
  946. const char *name = obs_source_get_name(source);
  947. const char *id = obs_source_get_id(source);
  948. obs_source_save(source);
  949. obs_data_setstring(source_data, "name", name);
  950. obs_data_setstring(source_data, "id", id);
  951. obs_data_setobj (source_data, "settings", settings);
  952. obs_data_setdouble(source_data, "volume", volume);
  953. obs_data_release(settings);
  954. return source_data;
  955. }
  956. obs_data_array_t obs_save_sources(void)
  957. {
  958. obs_data_array_t array;
  959. size_t i;
  960. if (!obs) return NULL;
  961. array = obs_data_array_create();
  962. pthread_mutex_lock(&obs->data.user_sources_mutex);
  963. for (i = 0; i < obs->data.user_sources.num; i++) {
  964. obs_source_t source = obs->data.user_sources.array[i];
  965. obs_data_t source_data = obs_save_source(source);
  966. obs_data_array_push_back(array, source_data);
  967. obs_data_release(source_data);
  968. }
  969. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  970. return array;
  971. }
  972. /* ensures that names are never blank */
  973. static inline char *dup_name(const char *name)
  974. {
  975. if (!name || !*name) {
  976. struct dstr unnamed = {0};
  977. dstr_printf(&unnamed, "__unnamed%004lld",
  978. obs->data.unnamed_index++);
  979. return unnamed.array;
  980. } else {
  981. return bstrdup(name);
  982. }
  983. }
  984. static inline bool obs_context_data_init_wrap(
  985. struct obs_context_data *context,
  986. obs_data_t settings,
  987. const char *name)
  988. {
  989. assert(context);
  990. memset(context, 0, sizeof(*context));
  991. pthread_mutex_init_value(&context->rename_cache_mutex);
  992. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  993. return false;
  994. context->signals = signal_handler_create();
  995. if (!context)
  996. return false;
  997. context->procs = proc_handler_create();
  998. if (!context->procs)
  999. return false;
  1000. context->name = dup_name(name);
  1001. context->settings = obs_data_newref(settings);
  1002. return true;
  1003. }
  1004. bool obs_context_data_init(
  1005. struct obs_context_data *context,
  1006. obs_data_t settings,
  1007. const char *name)
  1008. {
  1009. if (obs_context_data_init_wrap(context, settings, name)) {
  1010. return true;
  1011. } else {
  1012. obs_context_data_free(context);
  1013. return false;
  1014. }
  1015. }
  1016. void obs_context_data_free(struct obs_context_data *context)
  1017. {
  1018. signal_handler_destroy(context->signals);
  1019. proc_handler_destroy(context->procs);
  1020. obs_data_release(context->settings);
  1021. obs_context_data_remove(context);
  1022. pthread_mutex_destroy(&context->rename_cache_mutex);
  1023. bfree(context->name);
  1024. for (size_t i = 0; i < context->rename_cache.num; i++)
  1025. bfree(context->rename_cache.array[i]);
  1026. da_free(context->rename_cache);
  1027. memset(context, 0, sizeof(*context));
  1028. }
  1029. void obs_context_data_insert(struct obs_context_data *context,
  1030. pthread_mutex_t *mutex, void *pfirst)
  1031. {
  1032. struct obs_context_data **first = pfirst;
  1033. assert(context);
  1034. assert(mutex);
  1035. assert(first);
  1036. context->mutex = mutex;
  1037. pthread_mutex_lock(mutex);
  1038. context->prev_next = first;
  1039. context->next = *first;
  1040. *first = context;
  1041. if (context->next)
  1042. context->next->prev_next = &context->next;
  1043. pthread_mutex_unlock(mutex);
  1044. }
  1045. void obs_context_data_remove(struct obs_context_data *context)
  1046. {
  1047. if (context && context->mutex) {
  1048. pthread_mutex_lock(context->mutex);
  1049. *context->prev_next = context->next;
  1050. if (context->next)
  1051. context->next->prev_next = context->prev_next;
  1052. pthread_mutex_unlock(context->mutex);
  1053. context->mutex = NULL;
  1054. }
  1055. }
  1056. void obs_context_data_setname(struct obs_context_data *context,
  1057. const char *name)
  1058. {
  1059. pthread_mutex_lock(&context->rename_cache_mutex);
  1060. if (context->name)
  1061. da_push_back(context->rename_cache, &context->name);
  1062. context->name = dup_name(name);
  1063. pthread_mutex_unlock(&context->rename_cache_mutex);
  1064. }