obs.c 32 KB

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