obs.c 30 KB

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