obs.c 29 KB

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