obs.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268
  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("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_volume(ptr source, in out float volume)",
  377. "void source_volume_level(ptr source, float level, float magnitude, "
  378. "float peak)",
  379. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  380. "void master_volume(in out float volume)",
  381. NULL
  382. };
  383. static inline bool obs_init_handlers(void)
  384. {
  385. obs->signals = signal_handler_create();
  386. if (!obs->signals)
  387. return false;
  388. obs->procs = proc_handler_create();
  389. if (!obs->procs)
  390. return false;
  391. return signal_handler_add_array(obs->signals, obs_signals);
  392. }
  393. extern const struct obs_source_info scene_info;
  394. static bool obs_init(const char *locale)
  395. {
  396. obs = bzalloc(sizeof(struct obs_core));
  397. if (!obs_init_data())
  398. return false;
  399. if (!obs_init_handlers())
  400. return false;
  401. obs->locale = bstrdup(locale);
  402. obs_register_source(&scene_info);
  403. return true;
  404. }
  405. bool obs_startup(const char *locale)
  406. {
  407. bool success;
  408. if (obs) {
  409. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  410. return false;
  411. }
  412. success = obs_init(locale);
  413. if (!success)
  414. obs_shutdown();
  415. return success;
  416. }
  417. void obs_shutdown(void)
  418. {
  419. if (!obs)
  420. return;
  421. da_free(obs->input_types);
  422. da_free(obs->filter_types);
  423. da_free(obs->encoder_types);
  424. da_free(obs->transition_types);
  425. da_free(obs->output_types);
  426. da_free(obs->service_types);
  427. da_free(obs->modal_ui_callbacks);
  428. da_free(obs->modeless_ui_callbacks);
  429. stop_video();
  430. obs_free_data();
  431. obs_free_video();
  432. obs_free_graphics();
  433. obs_free_audio();
  434. proc_handler_destroy(obs->procs);
  435. signal_handler_destroy(obs->signals);
  436. for (size_t i = 0; i < obs->modules.num; i++)
  437. free_module(obs->modules.array+i);
  438. da_free(obs->modules);
  439. bfree(obs->locale);
  440. bfree(obs);
  441. obs = NULL;
  442. }
  443. bool obs_initialized(void)
  444. {
  445. return obs != NULL;
  446. }
  447. void obs_set_locale(const char *locale)
  448. {
  449. if (!obs)
  450. return;
  451. if (obs->locale)
  452. bfree(obs->locale);
  453. obs->locale = bstrdup(locale);
  454. for (size_t i = 0; i < obs->modules.num; i++) {
  455. struct obs_module *module = obs->modules.array+i;
  456. if (module->set_locale)
  457. module->set_locale(locale);
  458. }
  459. }
  460. const char *obs_get_locale(void)
  461. {
  462. return obs ? obs->locale : NULL;
  463. }
  464. bool obs_reset_video(struct obs_video_info *ovi)
  465. {
  466. if (!obs) return false;
  467. /* don't allow changing of video settings if active. */
  468. if (obs->video.video && video_output_active(obs->video.video))
  469. return false;
  470. struct obs_core_video *video = &obs->video;
  471. /* align to multiple-of-two and SSE alignment sizes */
  472. ovi->output_width &= 0xFFFFFFFC;
  473. ovi->output_height &= 0xFFFFFFFE;
  474. stop_video();
  475. obs_free_video();
  476. if (!ovi) {
  477. obs_free_graphics();
  478. return true;
  479. }
  480. if (!video->graphics && !obs_init_graphics(ovi))
  481. return false;
  482. return obs_init_video(ovi);
  483. }
  484. bool obs_reset_audio(struct audio_output_info *ai)
  485. {
  486. if (!obs) return false;
  487. /* don't allow changing of audio settings if active. */
  488. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  489. return false;
  490. obs_free_audio();
  491. if(!ai)
  492. return true;
  493. return obs_init_audio(ai);
  494. }
  495. bool obs_get_video_info(struct obs_video_info *ovi)
  496. {
  497. struct obs_core_video *video = &obs->video;
  498. const struct video_output_info *info;
  499. if (!obs || !video->graphics)
  500. return false;
  501. info = video_output_getinfo(video->video);
  502. memset(ovi, 0, sizeof(struct obs_video_info));
  503. ovi->base_width = video->base_width;
  504. ovi->base_height = video->base_height;
  505. ovi->output_width = info->width;
  506. ovi->output_height = info->height;
  507. ovi->output_format = info->format;
  508. ovi->fps_num = info->fps_num;
  509. ovi->fps_den = info->fps_den;
  510. return true;
  511. }
  512. bool obs_get_audio_info(struct audio_output_info *aoi)
  513. {
  514. struct obs_core_audio *audio = &obs->audio;
  515. const struct audio_output_info *info;
  516. if (!obs || !audio->audio)
  517. return false;
  518. info = audio_output_getinfo(audio->audio);
  519. memcpy(aoi, info, sizeof(struct audio_output_info));
  520. return true;
  521. }
  522. bool obs_enum_input_types(size_t idx, const char **id)
  523. {
  524. if (!obs) return false;
  525. if (idx >= obs->input_types.num)
  526. return false;
  527. *id = obs->input_types.array[idx].id;
  528. return true;
  529. }
  530. bool obs_enum_filter_types(size_t idx, const char **id)
  531. {
  532. if (!obs) return false;
  533. if (idx >= obs->filter_types.num)
  534. return false;
  535. *id = obs->filter_types.array[idx].id;
  536. return true;
  537. }
  538. bool obs_enum_transition_types(size_t idx, const char **id)
  539. {
  540. if (!obs) return false;
  541. if (idx >= obs->transition_types.num)
  542. return false;
  543. *id = obs->transition_types.array[idx].id;
  544. return true;
  545. }
  546. bool obs_enum_output_types(size_t idx, const char **id)
  547. {
  548. if (!obs) return false;
  549. if (idx >= obs->output_types.num)
  550. return false;
  551. *id = obs->output_types.array[idx].id;
  552. return true;
  553. }
  554. bool obs_enum_encoder_types(size_t idx, const char **id)
  555. {
  556. if (!obs) return false;
  557. if (idx >= obs->encoder_types.num)
  558. return false;
  559. *id = obs->encoder_types.array[idx].id;
  560. return true;
  561. }
  562. bool obs_enum_service_types(size_t idx, const char **id)
  563. {
  564. if (!obs) return false;
  565. if (idx >= obs->service_types.num)
  566. return false;
  567. *id = obs->service_types.array[idx].id;
  568. return true;
  569. }
  570. graphics_t obs_graphics(void)
  571. {
  572. return (obs != NULL) ? obs->video.graphics : NULL;
  573. }
  574. audio_t obs_audio(void)
  575. {
  576. return (obs != NULL) ? obs->audio.audio : NULL;
  577. }
  578. video_t obs_video(void)
  579. {
  580. return (obs != NULL) ? obs->video.video : NULL;
  581. }
  582. /* TODO: optimize this later so it's not just O(N) string lookups */
  583. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  584. const char *task, const char *target)
  585. {
  586. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  587. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  588. if (strcmp(callback->id, id) == 0 &&
  589. strcmp(callback->task, task) == 0 &&
  590. strcmp(callback->target, target) == 0)
  591. return callback;
  592. }
  593. return NULL;
  594. }
  595. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  596. const char *task, const char *target)
  597. {
  598. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  599. struct obs_modeless_ui *callback;
  600. callback = obs->modeless_ui_callbacks.array+i;
  601. if (strcmp(callback->id, id) == 0 &&
  602. strcmp(callback->task, task) == 0 &&
  603. strcmp(callback->target, target) == 0)
  604. return callback;
  605. }
  606. return NULL;
  607. }
  608. int obs_exec_ui(const char *name, const char *task, const char *target,
  609. void *data, void *ui_data)
  610. {
  611. struct obs_modal_ui *callback;
  612. int errorcode = OBS_UI_NOTFOUND;
  613. if (!obs) return errorcode;
  614. callback = get_modal_ui_callback(name, task, target);
  615. if (callback) {
  616. bool success = callback->exec(data, ui_data);
  617. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  618. }
  619. return errorcode;
  620. }
  621. void *obs_create_ui(const char *name, const char *task, const char *target,
  622. void *data, void *ui_data)
  623. {
  624. struct obs_modeless_ui *callback;
  625. if (!obs) return NULL;
  626. callback = get_modeless_ui_callback(name, task, target);
  627. return callback ? callback->create(data, ui_data) : NULL;
  628. }
  629. bool obs_add_source(obs_source_t source)
  630. {
  631. struct calldata params = {0};
  632. if (!obs) return false;
  633. if (!source) return false;
  634. pthread_mutex_lock(&obs->data.sources_mutex);
  635. da_push_back(obs->data.user_sources, &source);
  636. obs_source_addref(source);
  637. pthread_mutex_unlock(&obs->data.sources_mutex);
  638. calldata_setptr(&params, "source", source);
  639. signal_handler_signal(obs->signals, "source_add", &params);
  640. calldata_free(&params);
  641. return true;
  642. }
  643. obs_source_t obs_get_output_source(uint32_t channel)
  644. {
  645. if (!obs) return NULL;
  646. return obs_view_getsource(&obs->data.main_view, channel);
  647. }
  648. void obs_set_output_source(uint32_t channel, obs_source_t source)
  649. {
  650. assert(channel < MAX_CHANNELS);
  651. if (!obs) return;
  652. if (channel >= MAX_CHANNELS) return;
  653. struct obs_source *prev_source;
  654. struct obs_view *view = &obs->data.main_view;
  655. struct calldata params = {0};
  656. pthread_mutex_lock(&view->channels_mutex);
  657. obs_source_addref(source);
  658. prev_source = view->channels[channel];
  659. calldata_setint(&params, "channel", channel);
  660. calldata_setptr(&params, "prev_source", prev_source);
  661. calldata_setptr(&params, "source", source);
  662. signal_handler_signal(obs->signals, "channel_change", &params);
  663. calldata_getptr(&params, "source", &source);
  664. calldata_free(&params);
  665. view->channels[channel] = source;
  666. pthread_mutex_unlock(&view->channels_mutex);
  667. if (source)
  668. obs_source_activate(source, MAIN_VIEW);
  669. if (prev_source) {
  670. obs_source_deactivate(prev_source, MAIN_VIEW);
  671. obs_source_release(prev_source);
  672. }
  673. }
  674. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t), void *param)
  675. {
  676. if (!obs) return;
  677. pthread_mutex_lock(&obs->data.user_sources_mutex);
  678. for (size_t i = 0; i < obs->data.user_sources.num; i++) {
  679. struct obs_source *source = obs->data.user_sources.array[i];
  680. if (!enum_proc(param, source))
  681. break;
  682. }
  683. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  684. }
  685. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  686. void *param)
  687. {
  688. struct obs_context_data **start = pstart, *context;
  689. bool (*enum_proc)(void*, void*) = proc;
  690. assert(start);
  691. assert(mutex);
  692. assert(enum_proc);
  693. pthread_mutex_lock(mutex);
  694. context = *start;
  695. while (context) {
  696. if (!enum_proc(param, context))
  697. break;
  698. context = context->next;
  699. }
  700. pthread_mutex_unlock(mutex);
  701. }
  702. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t), void *param)
  703. {
  704. if (!obs) return;
  705. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  706. enum_proc, param);
  707. }
  708. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t), void *param)
  709. {
  710. if (!obs) return;
  711. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  712. enum_proc, param);
  713. }
  714. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t), void *param)
  715. {
  716. if (!obs) return;
  717. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  718. enum_proc, param);
  719. }
  720. obs_source_t obs_get_source_by_name(const char *name)
  721. {
  722. struct obs_core_data *data = &obs->data;
  723. struct obs_source *source = NULL;
  724. size_t i;
  725. if (!obs) return NULL;
  726. pthread_mutex_lock(&data->user_sources_mutex);
  727. for (i = 0; i < data->user_sources.num; i++) {
  728. struct obs_source *cur_source = data->user_sources.array[i];
  729. if (strcmp(cur_source->context.name, name) == 0) {
  730. source = cur_source;
  731. obs_source_addref(source);
  732. break;
  733. }
  734. }
  735. pthread_mutex_unlock(&data->user_sources_mutex);
  736. return source;
  737. }
  738. static inline void *get_context_by_name(void *vfirst, const char *name,
  739. pthread_mutex_t *mutex)
  740. {
  741. struct obs_context_data **first = vfirst;
  742. struct obs_context_data *context;
  743. pthread_mutex_lock(mutex);
  744. context = *first;
  745. while (context) {
  746. if (strcmp(context->name, name) == 0)
  747. break;
  748. context = context->next;
  749. }
  750. pthread_mutex_unlock(mutex);
  751. return context;
  752. }
  753. obs_output_t obs_get_output_by_name(const char *name)
  754. {
  755. if (!obs) return NULL;
  756. return get_context_by_name(&obs->data.first_output, name,
  757. &obs->data.outputs_mutex);
  758. }
  759. obs_encoder_t obs_get_encoder_by_name(const char *name)
  760. {
  761. if (!obs) return NULL;
  762. return get_context_by_name(&obs->data.first_encoder, name,
  763. &obs->data.encoders_mutex);
  764. }
  765. obs_service_t obs_get_service_by_name(const char *name)
  766. {
  767. if (!obs) return NULL;
  768. return get_context_by_name(&obs->data.first_service, name,
  769. &obs->data.services_mutex);
  770. }
  771. effect_t obs_get_default_effect(void)
  772. {
  773. if (!obs) return NULL;
  774. return obs->video.default_effect;
  775. }
  776. effect_t obs_get_solid_effect(void)
  777. {
  778. if (!obs) return NULL;
  779. return obs->video.solid_effect;
  780. }
  781. signal_handler_t obs_signalhandler(void)
  782. {
  783. if (!obs) return NULL;
  784. return obs->signals;
  785. }
  786. proc_handler_t obs_prochandler(void)
  787. {
  788. if (!obs) return NULL;
  789. return obs->procs;
  790. }
  791. void obs_add_draw_callback(
  792. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  793. void *param)
  794. {
  795. if (!obs) return;
  796. obs_display_add_draw_callback(&obs->video.main_display, draw, param);
  797. }
  798. void obs_remove_draw_callback(
  799. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  800. void *param)
  801. {
  802. if (!obs) return;
  803. obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
  804. }
  805. void obs_resize(uint32_t cx, uint32_t cy)
  806. {
  807. if (!obs || !obs->video.video || !obs->video.graphics) return;
  808. obs_display_resize(&obs->video.main_display, cx, cy);
  809. }
  810. void obs_render_main_view(void)
  811. {
  812. if (!obs) return;
  813. obs_view_render(&obs->data.main_view);
  814. }
  815. void obs_set_master_volume(float volume)
  816. {
  817. struct calldata data = {0};
  818. if (!obs) return;
  819. calldata_setfloat(&data, "volume", volume);
  820. signal_handler_signal(obs->signals, "master_volume", &data);
  821. volume = (float)calldata_float(&data, "volume");
  822. calldata_free(&data);
  823. obs->audio.user_volume = volume;
  824. }
  825. void obs_set_present_volume(float volume)
  826. {
  827. if (!obs) return;
  828. obs->audio.present_volume = volume;
  829. }
  830. float obs_get_master_volume(void)
  831. {
  832. return obs ? obs->audio.user_volume : 0.0f;
  833. }
  834. float obs_get_present_volume(void)
  835. {
  836. return obs ? obs->audio.present_volume : 0.0f;
  837. }
  838. obs_source_t obs_load_source(obs_data_t source_data)
  839. {
  840. obs_source_t source;
  841. const char *name = obs_data_getstring(source_data, "name");
  842. const char *id = obs_data_getstring(source_data, "id");
  843. obs_data_t settings = obs_data_getobj(source_data, "settings");
  844. double volume;
  845. source = obs_source_create(OBS_SOURCE_TYPE_INPUT, id, name, settings);
  846. obs_data_set_default_double(source_data, "volume", 1.0);
  847. volume = obs_data_getdouble(source_data, "volume");
  848. obs_source_setvolume(source, (float)volume);
  849. obs_data_release(settings);
  850. return source;
  851. }
  852. void obs_load_sources(obs_data_array_t array)
  853. {
  854. size_t count;
  855. size_t i;
  856. if (!obs) return;
  857. count = obs_data_array_count(array);
  858. pthread_mutex_lock(&obs->data.user_sources_mutex);
  859. for (i = 0; i < count; i++) {
  860. obs_data_t source_data = obs_data_array_item(array, i);
  861. obs_source_t source = obs_load_source(source_data);
  862. obs_add_source(source);
  863. obs_source_release(source);
  864. obs_data_release(source_data);
  865. }
  866. /* tell sources that we want to load */
  867. for (i = 0; i < obs->data.user_sources.num; i++)
  868. obs_source_load(obs->data.user_sources.array[i]);
  869. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  870. }
  871. obs_data_t obs_save_source(obs_source_t source)
  872. {
  873. obs_data_t source_data = obs_data_create();
  874. obs_data_t settings = obs_source_getsettings(source);
  875. float volume = obs_source_getvolume(source);
  876. const char *name = obs_source_getname(source);
  877. const char *id;
  878. obs_source_save(source);
  879. obs_source_gettype(source, NULL, &id);
  880. obs_data_setstring(source_data, "name", name);
  881. obs_data_setstring(source_data, "id", id);
  882. obs_data_setobj (source_data, "settings", settings);
  883. obs_data_setdouble(source_data, "volume", volume);
  884. obs_data_release(settings);
  885. return source_data;
  886. }
  887. obs_data_array_t obs_save_sources(void)
  888. {
  889. obs_data_array_t array;
  890. size_t i;
  891. if (!obs) return NULL;
  892. array = obs_data_array_create();
  893. pthread_mutex_lock(&obs->data.user_sources_mutex);
  894. for (i = 0; i < obs->data.user_sources.num; i++) {
  895. obs_source_t source = obs->data.user_sources.array[i];
  896. obs_data_t source_data = obs_save_source(source);
  897. obs_data_array_push_back(array, source_data);
  898. obs_data_release(source_data);
  899. }
  900. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  901. return array;
  902. }
  903. /* ensures that names are never blank */
  904. static inline char *dup_name(const char *name)
  905. {
  906. if (!name || !*name) {
  907. struct dstr unnamed = {0};
  908. dstr_printf(&unnamed, "__unnamed%004lld",
  909. obs->data.unnamed_index++);
  910. return unnamed.array;
  911. } else {
  912. return bstrdup(name);
  913. }
  914. }
  915. static inline bool obs_context_data_init_wrap(
  916. struct obs_context_data *context,
  917. obs_data_t settings,
  918. const char *name)
  919. {
  920. assert(context);
  921. obs_context_data_free(context);
  922. context->signals = signal_handler_create();
  923. if (!context)
  924. return false;
  925. context->procs = proc_handler_create();
  926. if (!context->procs)
  927. return false;
  928. context->name = dup_name(name);
  929. context->settings = obs_data_newref(settings);
  930. return true;
  931. }
  932. bool obs_context_data_init(
  933. struct obs_context_data *context,
  934. obs_data_t settings,
  935. const char *name)
  936. {
  937. if (obs_context_data_init_wrap(context, settings, name)) {
  938. return true;
  939. } else {
  940. obs_context_data_free(context);
  941. return false;
  942. }
  943. }
  944. void obs_context_data_free(struct obs_context_data *context)
  945. {
  946. signal_handler_destroy(context->signals);
  947. proc_handler_destroy(context->procs);
  948. obs_data_release(context->settings);
  949. obs_context_data_remove(context);
  950. bfree(context->name);
  951. memset(context, 0, sizeof(*context));
  952. }
  953. void obs_context_data_insert(struct obs_context_data *context,
  954. pthread_mutex_t *mutex, void *pfirst)
  955. {
  956. struct obs_context_data **first = pfirst;
  957. assert(context);
  958. assert(mutex);
  959. assert(first);
  960. context->mutex = mutex;
  961. pthread_mutex_lock(mutex);
  962. context->prev_next = first;
  963. context->next = *first;
  964. *first = context;
  965. if (context->next)
  966. context->next->prev_next = &context->next;
  967. pthread_mutex_unlock(mutex);
  968. }
  969. void obs_context_data_remove(struct obs_context_data *context)
  970. {
  971. if (context && context->mutex) {
  972. pthread_mutex_lock(context->mutex);
  973. *context->prev_next = context->next;
  974. if (context->next)
  975. context->next->prev_next = context->prev_next;
  976. pthread_mutex_unlock(context->mutex);
  977. context->mutex = NULL;
  978. }
  979. }
  980. void obs_context_data_setname(struct obs_context_data *context,
  981. const char *name)
  982. {
  983. bfree(context->name);
  984. context->name = dup_name(name);
  985. }