obs.c 29 KB

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