obs.c 26 KB

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