obs.c 32 KB

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