obs.c 30 KB

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