obs.c 43 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757
  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 "graphics/matrix4.h"
  16. #include "callback/calldata.h"
  17. #include "obs.h"
  18. #include "obs-internal.h"
  19. struct obs_core *obs = NULL;
  20. extern void add_default_module_paths(void);
  21. extern char *find_libobs_data_file(const char *file);
  22. static inline void make_gs_init_data(struct gs_init_data *gid,
  23. const struct obs_video_info *ovi)
  24. {
  25. memcpy(&gid->window, &ovi->window, sizeof(struct gs_window));
  26. gid->cx = ovi->window_width;
  27. gid->cy = ovi->window_height;
  28. gid->num_backbuffers = 2;
  29. gid->format = GS_RGBA;
  30. gid->zsformat = GS_ZS_NONE;
  31. gid->adapter = ovi->adapter;
  32. }
  33. static inline void make_video_info(struct video_output_info *vi,
  34. struct obs_video_info *ovi)
  35. {
  36. vi->name = "video";
  37. vi->format = ovi->output_format;
  38. vi->fps_num = ovi->fps_num;
  39. vi->fps_den = ovi->fps_den;
  40. vi->width = ovi->output_width;
  41. vi->height = ovi->output_height;
  42. vi->range = ovi->range;
  43. vi->colorspace = ovi->colorspace;
  44. vi->cache_size = 6;
  45. }
  46. #define PIXEL_SIZE 4
  47. #define GET_ALIGN(val, align) \
  48. (((val) + (align-1)) & ~(align-1))
  49. static inline void set_420p_sizes(const struct obs_video_info *ovi)
  50. {
  51. struct obs_core_video *video = &obs->video;
  52. uint32_t chroma_pixels;
  53. uint32_t total_bytes;
  54. chroma_pixels = (ovi->output_width * ovi->output_height / 4);
  55. chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
  56. video->plane_offsets[0] = 0;
  57. video->plane_offsets[1] = ovi->output_width * ovi->output_height;
  58. video->plane_offsets[2] = video->plane_offsets[1] + chroma_pixels;
  59. video->plane_linewidth[0] = ovi->output_width;
  60. video->plane_linewidth[1] = ovi->output_width/2;
  61. video->plane_linewidth[2] = ovi->output_width/2;
  62. video->plane_sizes[0] = video->plane_offsets[1];
  63. video->plane_sizes[1] = video->plane_sizes[0]/4;
  64. video->plane_sizes[2] = video->plane_sizes[1];
  65. total_bytes = video->plane_offsets[2] + chroma_pixels;
  66. video->conversion_height =
  67. (total_bytes/PIXEL_SIZE + ovi->output_width-1) /
  68. ovi->output_width;
  69. video->conversion_height = GET_ALIGN(video->conversion_height, 2);
  70. video->conversion_tech = "Planar420";
  71. }
  72. static inline void set_nv12_sizes(const struct obs_video_info *ovi)
  73. {
  74. struct obs_core_video *video = &obs->video;
  75. uint32_t chroma_pixels;
  76. uint32_t total_bytes;
  77. chroma_pixels = (ovi->output_width * ovi->output_height / 2);
  78. chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
  79. video->plane_offsets[0] = 0;
  80. video->plane_offsets[1] = ovi->output_width * ovi->output_height;
  81. video->plane_linewidth[0] = ovi->output_width;
  82. video->plane_linewidth[1] = ovi->output_width;
  83. video->plane_sizes[0] = video->plane_offsets[1];
  84. video->plane_sizes[1] = video->plane_sizes[0]/2;
  85. total_bytes = video->plane_offsets[1] + chroma_pixels;
  86. video->conversion_height =
  87. (total_bytes/PIXEL_SIZE + ovi->output_width-1) /
  88. ovi->output_width;
  89. video->conversion_height = GET_ALIGN(video->conversion_height, 2);
  90. video->conversion_tech = "NV12";
  91. }
  92. static inline void set_444p_sizes(const struct obs_video_info *ovi)
  93. {
  94. struct obs_core_video *video = &obs->video;
  95. uint32_t chroma_pixels;
  96. uint32_t total_bytes;
  97. chroma_pixels = (ovi->output_width * ovi->output_height);
  98. chroma_pixels = GET_ALIGN(chroma_pixels, PIXEL_SIZE);
  99. video->plane_offsets[0] = 0;
  100. video->plane_offsets[1] = chroma_pixels;
  101. video->plane_offsets[2] = chroma_pixels + chroma_pixels;
  102. video->plane_linewidth[0] = ovi->output_width;
  103. video->plane_linewidth[1] = ovi->output_width;
  104. video->plane_linewidth[2] = ovi->output_width;
  105. video->plane_sizes[0] = chroma_pixels;
  106. video->plane_sizes[1] = chroma_pixels;
  107. video->plane_sizes[2] = chroma_pixels;
  108. total_bytes = video->plane_offsets[2] + chroma_pixels;
  109. video->conversion_height =
  110. (total_bytes/PIXEL_SIZE + ovi->output_width-1) /
  111. ovi->output_width;
  112. video->conversion_height = GET_ALIGN(video->conversion_height, 2);
  113. video->conversion_tech = "Planar444";
  114. }
  115. static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
  116. {
  117. obs->video.conversion_height = 0;
  118. memset(obs->video.plane_offsets, 0, sizeof(obs->video.plane_offsets));
  119. memset(obs->video.plane_sizes, 0, sizeof(obs->video.plane_sizes));
  120. memset(obs->video.plane_linewidth, 0,
  121. sizeof(obs->video.plane_linewidth));
  122. switch ((uint32_t)ovi->output_format) {
  123. case VIDEO_FORMAT_I420:
  124. set_420p_sizes(ovi);
  125. break;
  126. case VIDEO_FORMAT_NV12:
  127. set_nv12_sizes(ovi);
  128. break;
  129. case VIDEO_FORMAT_I444:
  130. set_444p_sizes(ovi);
  131. break;
  132. }
  133. }
  134. static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
  135. {
  136. struct obs_core_video *video = &obs->video;
  137. calc_gpu_conversion_sizes(ovi);
  138. if (!video->conversion_height) {
  139. blog(LOG_INFO, "GPU conversion not available for format: %u",
  140. (unsigned int)ovi->output_format);
  141. video->gpu_conversion = false;
  142. return true;
  143. }
  144. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  145. video->convert_textures[i] = gs_texture_create(
  146. ovi->output_width, video->conversion_height,
  147. GS_RGBA, 1, NULL, GS_RENDER_TARGET);
  148. if (!video->convert_textures[i])
  149. return false;
  150. }
  151. return true;
  152. }
  153. static bool obs_init_textures(struct obs_video_info *ovi)
  154. {
  155. struct obs_core_video *video = &obs->video;
  156. uint32_t output_height = video->gpu_conversion ?
  157. video->conversion_height : ovi->output_height;
  158. size_t i;
  159. for (i = 0; i < NUM_TEXTURES; i++) {
  160. video->copy_surfaces[i] = gs_stagesurface_create(
  161. ovi->output_width, output_height, GS_RGBA);
  162. if (!video->copy_surfaces[i])
  163. return false;
  164. video->render_textures[i] = gs_texture_create(
  165. ovi->base_width, ovi->base_height,
  166. GS_RGBA, 1, NULL, GS_RENDER_TARGET);
  167. if (!video->render_textures[i])
  168. return false;
  169. video->output_textures[i] = gs_texture_create(
  170. ovi->output_width, ovi->output_height,
  171. GS_RGBA, 1, NULL, GS_RENDER_TARGET);
  172. if (!video->output_textures[i])
  173. return false;
  174. }
  175. return true;
  176. }
  177. static int obs_init_graphics(struct obs_video_info *ovi)
  178. {
  179. struct obs_core_video *video = &obs->video;
  180. struct gs_init_data graphics_data;
  181. bool success = true;
  182. int errorcode;
  183. make_gs_init_data(&graphics_data, ovi);
  184. errorcode = gs_create(&video->graphics, ovi->graphics_module,
  185. &graphics_data);
  186. if (errorcode != GS_SUCCESS) {
  187. switch (errorcode) {
  188. case GS_ERROR_MODULE_NOT_FOUND:
  189. return OBS_VIDEO_MODULE_NOT_FOUND;
  190. case GS_ERROR_NOT_SUPPORTED:
  191. return OBS_VIDEO_NOT_SUPPORTED;
  192. default:
  193. return OBS_VIDEO_FAIL;
  194. }
  195. }
  196. gs_enter_context(video->graphics);
  197. char *filename = find_libobs_data_file("default.effect");
  198. video->default_effect = gs_effect_create_from_file(filename,
  199. NULL);
  200. bfree(filename);
  201. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  202. filename = find_libobs_data_file("default_rect.effect");
  203. video->default_rect_effect = gs_effect_create_from_file(
  204. filename, NULL);
  205. bfree(filename);
  206. }
  207. filename = find_libobs_data_file("opaque.effect");
  208. video->opaque_effect = gs_effect_create_from_file(filename,
  209. NULL);
  210. bfree(filename);
  211. filename = find_libobs_data_file("solid.effect");
  212. video->solid_effect = gs_effect_create_from_file(filename,
  213. NULL);
  214. bfree(filename);
  215. filename = find_libobs_data_file("format_conversion.effect");
  216. video->conversion_effect = gs_effect_create_from_file(filename,
  217. NULL);
  218. bfree(filename);
  219. filename = find_libobs_data_file("bicubic_scale.effect");
  220. video->bicubic_effect = gs_effect_create_from_file(filename,
  221. NULL);
  222. bfree(filename);
  223. filename = find_libobs_data_file("lanczos_scale.effect");
  224. video->lanczos_effect = gs_effect_create_from_file(filename,
  225. NULL);
  226. bfree(filename);
  227. filename = find_libobs_data_file("bilinear_lowres_scale.effect");
  228. video->bilinear_lowres_effect = gs_effect_create_from_file(filename,
  229. NULL);
  230. bfree(filename);
  231. if (!video->default_effect)
  232. success = false;
  233. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  234. if (!video->default_rect_effect)
  235. success = false;
  236. }
  237. if (!video->opaque_effect)
  238. success = false;
  239. if (!video->solid_effect)
  240. success = false;
  241. if (!video->conversion_effect)
  242. success = false;
  243. gs_leave_context();
  244. return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
  245. }
  246. static inline void set_video_matrix(struct obs_core_video *video,
  247. struct obs_video_info *ovi)
  248. {
  249. struct matrix4 mat;
  250. struct vec4 r_row;
  251. if (format_is_yuv(ovi->output_format)) {
  252. video_format_get_parameters(ovi->colorspace, ovi->range,
  253. (float*)&mat, NULL, NULL);
  254. matrix4_inv(&mat, &mat);
  255. /* swap R and G */
  256. r_row = mat.x;
  257. mat.x = mat.y;
  258. mat.y = r_row;
  259. } else {
  260. matrix4_identity(&mat);
  261. }
  262. memcpy(video->color_matrix, &mat, sizeof(float) * 16);
  263. }
  264. static int obs_init_video(struct obs_video_info *ovi)
  265. {
  266. struct obs_core_video *video = &obs->video;
  267. struct video_output_info vi;
  268. int errorcode;
  269. make_video_info(&vi, ovi);
  270. video->base_width = ovi->base_width;
  271. video->base_height = ovi->base_height;
  272. video->output_width = ovi->output_width;
  273. video->output_height = ovi->output_height;
  274. video->gpu_conversion = ovi->gpu_conversion;
  275. video->scale_type = ovi->scale_type;
  276. set_video_matrix(video, ovi);
  277. errorcode = video_output_open(&video->video, &vi);
  278. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  279. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
  280. blog(LOG_ERROR, "Invalid video parameters specified");
  281. return OBS_VIDEO_INVALID_PARAM;
  282. } else {
  283. blog(LOG_ERROR, "Could not open video output");
  284. }
  285. return OBS_VIDEO_FAIL;
  286. }
  287. if (!obs_display_init(&video->main_display, NULL))
  288. return OBS_VIDEO_FAIL;
  289. video->main_display.cx = ovi->window_width;
  290. video->main_display.cy = ovi->window_height;
  291. gs_enter_context(video->graphics);
  292. if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
  293. return OBS_VIDEO_FAIL;
  294. if (!obs_init_textures(ovi))
  295. return OBS_VIDEO_FAIL;
  296. gs_leave_context();
  297. errorcode = pthread_create(&video->video_thread, NULL,
  298. obs_video_thread, obs);
  299. if (errorcode != 0)
  300. return OBS_VIDEO_FAIL;
  301. video->thread_initialized = true;
  302. return OBS_VIDEO_SUCCESS;
  303. }
  304. static void stop_video(void)
  305. {
  306. struct obs_core_video *video = &obs->video;
  307. void *thread_retval;
  308. if (video->video) {
  309. video_output_stop(video->video);
  310. if (video->thread_initialized) {
  311. pthread_join(video->video_thread, &thread_retval);
  312. video->thread_initialized = false;
  313. }
  314. }
  315. }
  316. static void obs_free_video(void)
  317. {
  318. struct obs_core_video *video = &obs->video;
  319. if (video->video) {
  320. obs_display_free(&video->main_display);
  321. video_output_close(video->video);
  322. video->video = NULL;
  323. if (!video->graphics)
  324. return;
  325. gs_enter_context(video->graphics);
  326. if (video->mapped_surface) {
  327. gs_stagesurface_unmap(video->mapped_surface);
  328. video->mapped_surface = NULL;
  329. }
  330. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  331. gs_stagesurface_destroy(video->copy_surfaces[i]);
  332. gs_texture_destroy(video->render_textures[i]);
  333. gs_texture_destroy(video->convert_textures[i]);
  334. gs_texture_destroy(video->output_textures[i]);
  335. video->copy_surfaces[i] = NULL;
  336. video->render_textures[i] = NULL;
  337. video->convert_textures[i] = NULL;
  338. video->output_textures[i] = NULL;
  339. }
  340. gs_leave_context();
  341. circlebuf_free(&video->vframe_info_buffer);
  342. memset(&video->textures_rendered, 0,
  343. sizeof(video->textures_rendered));
  344. memset(&video->textures_output, 0,
  345. sizeof(video->textures_output));
  346. memset(&video->textures_copied, 0,
  347. sizeof(video->textures_copied));
  348. memset(&video->textures_converted, 0,
  349. sizeof(video->textures_converted));
  350. video->cur_texture = 0;
  351. }
  352. }
  353. static void obs_free_graphics(void)
  354. {
  355. struct obs_core_video *video = &obs->video;
  356. if (video->graphics) {
  357. gs_enter_context(video->graphics);
  358. gs_effect_destroy(video->default_effect);
  359. gs_effect_destroy(video->default_rect_effect);
  360. gs_effect_destroy(video->opaque_effect);
  361. gs_effect_destroy(video->solid_effect);
  362. gs_effect_destroy(video->conversion_effect);
  363. gs_effect_destroy(video->bicubic_effect);
  364. gs_effect_destroy(video->lanczos_effect);
  365. gs_effect_destroy(video->bilinear_lowres_effect);
  366. video->default_effect = NULL;
  367. gs_leave_context();
  368. gs_destroy(video->graphics);
  369. video->graphics = NULL;
  370. }
  371. }
  372. static bool obs_init_audio(struct audio_output_info *ai)
  373. {
  374. struct obs_core_audio *audio = &obs->audio;
  375. int errorcode;
  376. /* TODO: sound subsystem */
  377. audio->user_volume = 1.0f;
  378. audio->present_volume = 1.0f;
  379. errorcode = audio_output_open(&audio->audio, ai);
  380. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  381. return true;
  382. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  383. blog(LOG_ERROR, "Invalid audio parameters specified");
  384. else
  385. blog(LOG_ERROR, "Could not open audio output");
  386. return false;
  387. }
  388. static void obs_free_audio(void)
  389. {
  390. struct obs_core_audio *audio = &obs->audio;
  391. if (audio->audio)
  392. audio_output_close(audio->audio);
  393. memset(audio, 0, sizeof(struct obs_core_audio));
  394. }
  395. static bool obs_init_data(void)
  396. {
  397. struct obs_core_data *data = &obs->data;
  398. pthread_mutexattr_t attr;
  399. assert(data != NULL);
  400. pthread_mutex_init_value(&obs->data.displays_mutex);
  401. if (pthread_mutexattr_init(&attr) != 0)
  402. return false;
  403. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  404. goto fail;
  405. if (pthread_mutex_init(&data->user_sources_mutex, &attr) != 0)
  406. goto fail;
  407. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  408. goto fail;
  409. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  410. goto fail;
  411. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  412. goto fail;
  413. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  414. goto fail;
  415. if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
  416. goto fail;
  417. if (!obs_view_init(&data->main_view))
  418. goto fail;
  419. data->valid = true;
  420. fail:
  421. pthread_mutexattr_destroy(&attr);
  422. return data->valid;
  423. }
  424. void obs_main_view_free(struct obs_view *view)
  425. {
  426. if (!view) return;
  427. for (size_t i = 0; i < MAX_CHANNELS; i++)
  428. obs_source_release(view->channels[i]);
  429. memset(view->channels, 0, sizeof(view->channels));
  430. pthread_mutex_destroy(&view->channels_mutex);
  431. }
  432. #define FREE_OBS_LINKED_LIST(type) \
  433. do { \
  434. int unfreed = 0; \
  435. while (data->first_ ## type ) { \
  436. obs_ ## type ## _destroy(data->first_ ## type ); \
  437. unfreed++; \
  438. } \
  439. if (unfreed) \
  440. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  441. unfreed); \
  442. } while (false)
  443. static void obs_free_data(void)
  444. {
  445. struct obs_core_data *data = &obs->data;
  446. data->valid = false;
  447. obs_main_view_free(&data->main_view);
  448. blog(LOG_INFO, "Freeing OBS context data");
  449. if (data->user_sources.num)
  450. blog(LOG_INFO, "\t%d user source(s) were remaining",
  451. (int)data->user_sources.num);
  452. while (data->user_sources.num)
  453. obs_source_remove(data->user_sources.array[0]);
  454. da_free(data->user_sources);
  455. FREE_OBS_LINKED_LIST(source);
  456. FREE_OBS_LINKED_LIST(output);
  457. FREE_OBS_LINKED_LIST(encoder);
  458. FREE_OBS_LINKED_LIST(display);
  459. FREE_OBS_LINKED_LIST(service);
  460. pthread_mutex_destroy(&data->user_sources_mutex);
  461. pthread_mutex_destroy(&data->sources_mutex);
  462. pthread_mutex_destroy(&data->displays_mutex);
  463. pthread_mutex_destroy(&data->outputs_mutex);
  464. pthread_mutex_destroy(&data->encoders_mutex);
  465. pthread_mutex_destroy(&data->services_mutex);
  466. }
  467. static const char *obs_signals[] = {
  468. "void source_create(ptr source)",
  469. "void source_destroy(ptr source)",
  470. "void source_add(ptr source)",
  471. "void source_remove(ptr source)",
  472. "void source_activate(ptr source)",
  473. "void source_deactivate(ptr source)",
  474. "void source_show(ptr source)",
  475. "void source_hide(ptr source)",
  476. "void source_rename(ptr source, string new_name, string prev_name)",
  477. "void source_volume(ptr source, in out float volume)",
  478. "void source_volume_level(ptr source, float level, float magnitude, "
  479. "float peak)",
  480. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  481. "void master_volume(in out float volume)",
  482. "void hotkey_layout_change()",
  483. "void hotkey_register(ptr hotkey)",
  484. "void hotkey_unregister(ptr hotkey)",
  485. "void hotkey_bindings_changed(ptr hotkey)",
  486. NULL
  487. };
  488. static inline bool obs_init_handlers(void)
  489. {
  490. obs->signals = signal_handler_create();
  491. if (!obs->signals)
  492. return false;
  493. obs->procs = proc_handler_create();
  494. if (!obs->procs)
  495. return false;
  496. return signal_handler_add_array(obs->signals, obs_signals);
  497. }
  498. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  499. static inline bool obs_init_hotkeys(void)
  500. {
  501. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  502. pthread_mutexattr_t attr;
  503. bool success = false;
  504. assert(hotkeys != NULL);
  505. da_init(hotkeys->hotkeys);
  506. hotkeys->signals = obs->signals;
  507. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  508. hotkeys->mute = bstrdup("Mute");
  509. hotkeys->unmute = bstrdup("Unmute");
  510. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  511. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  512. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  513. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  514. if (!obs_hotkeys_platform_init(hotkeys))
  515. return false;
  516. if (pthread_mutexattr_init(&attr) != 0)
  517. return false;
  518. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  519. goto fail;
  520. if (pthread_mutex_init(&hotkeys->mutex, &attr) != 0)
  521. goto fail;
  522. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  523. goto fail;
  524. if (pthread_create(&hotkeys->hotkey_thread, NULL,
  525. obs_hotkey_thread, NULL))
  526. goto fail;
  527. hotkeys->hotkey_thread_initialized = true;
  528. success = true;
  529. fail:
  530. pthread_mutexattr_destroy(&attr);
  531. return success;
  532. }
  533. static inline void stop_hotkeys(void)
  534. {
  535. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  536. void *thread_ret;
  537. if (hotkeys->hotkey_thread_initialized) {
  538. os_event_signal(hotkeys->stop_event);
  539. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  540. hotkeys->hotkey_thread_initialized = false;
  541. }
  542. os_event_destroy(hotkeys->stop_event);
  543. obs_hotkeys_free();
  544. }
  545. static inline void obs_free_hotkeys(void)
  546. {
  547. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  548. bfree(hotkeys->mute);
  549. bfree(hotkeys->unmute);
  550. bfree(hotkeys->push_to_mute);
  551. bfree(hotkeys->push_to_talk);
  552. bfree(hotkeys->sceneitem_show);
  553. bfree(hotkeys->sceneitem_hide);
  554. obs_hotkey_name_map_free();
  555. obs_hotkeys_platform_free(hotkeys);
  556. pthread_mutex_destroy(&hotkeys->mutex);
  557. }
  558. extern const struct obs_source_info scene_info;
  559. extern void log_system_info(void);
  560. static bool obs_init(const char *locale)
  561. {
  562. obs = bzalloc(sizeof(struct obs_core));
  563. log_system_info();
  564. if (!obs_init_data())
  565. return false;
  566. if (!obs_init_handlers())
  567. return false;
  568. if (!obs_init_hotkeys())
  569. return false;
  570. obs->locale = bstrdup(locale);
  571. obs_register_source(&scene_info);
  572. add_default_module_paths();
  573. return true;
  574. }
  575. #ifdef _WIN32
  576. extern void initialize_crash_handler(void);
  577. #endif
  578. bool obs_startup(const char *locale)
  579. {
  580. bool success;
  581. if (obs) {
  582. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  583. return false;
  584. }
  585. #ifdef _WIN32
  586. initialize_crash_handler();
  587. #endif
  588. success = obs_init(locale);
  589. if (!success)
  590. obs_shutdown();
  591. return success;
  592. }
  593. void obs_shutdown(void)
  594. {
  595. struct obs_module *module;
  596. if (!obs)
  597. return;
  598. da_free(obs->input_types);
  599. da_free(obs->filter_types);
  600. da_free(obs->encoder_types);
  601. da_free(obs->transition_types);
  602. da_free(obs->output_types);
  603. da_free(obs->service_types);
  604. da_free(obs->modal_ui_callbacks);
  605. da_free(obs->modeless_ui_callbacks);
  606. stop_video();
  607. stop_hotkeys();
  608. obs_free_data();
  609. obs_free_video();
  610. obs_free_hotkeys();
  611. obs_free_graphics();
  612. obs_free_audio();
  613. proc_handler_destroy(obs->procs);
  614. signal_handler_destroy(obs->signals);
  615. module = obs->first_module;
  616. while (module) {
  617. struct obs_module *next = module->next;
  618. free_module(module);
  619. module = next;
  620. }
  621. obs->first_module = NULL;
  622. for (size_t i = 0; i < obs->module_paths.num; i++)
  623. free_module_path(obs->module_paths.array+i);
  624. da_free(obs->module_paths);
  625. bfree(obs->locale);
  626. bfree(obs);
  627. obs = NULL;
  628. }
  629. bool obs_initialized(void)
  630. {
  631. return obs != NULL;
  632. }
  633. uint32_t obs_get_version(void)
  634. {
  635. return LIBOBS_API_VER;
  636. }
  637. void obs_set_locale(const char *locale)
  638. {
  639. struct obs_module *module;
  640. if (!obs)
  641. return;
  642. if (obs->locale)
  643. bfree(obs->locale);
  644. obs->locale = bstrdup(locale);
  645. module = obs->first_module;
  646. while (module) {
  647. if (module->set_locale)
  648. module->set_locale(locale);
  649. module = module->next;
  650. }
  651. }
  652. const char *obs_get_locale(void)
  653. {
  654. return obs ? obs->locale : NULL;
  655. }
  656. #define OBS_SIZE_MIN 2
  657. #define OBS_SIZE_MAX (32 * 1024)
  658. static inline bool size_valid(uint32_t width, uint32_t height)
  659. {
  660. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  661. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  662. }
  663. int obs_reset_video(struct obs_video_info *ovi)
  664. {
  665. if (!obs) return OBS_VIDEO_FAIL;
  666. /* don't allow changing of video settings if active. */
  667. if (obs->video.video && video_output_active(obs->video.video))
  668. return OBS_VIDEO_CURRENTLY_ACTIVE;
  669. if (!size_valid(ovi->output_width, ovi->output_height) ||
  670. !size_valid(ovi->base_width, ovi->base_height))
  671. return OBS_VIDEO_INVALID_PARAM;
  672. struct obs_core_video *video = &obs->video;
  673. stop_video();
  674. obs_free_video();
  675. if (!ovi) {
  676. obs_free_graphics();
  677. return OBS_VIDEO_SUCCESS;
  678. }
  679. /* align to multiple-of-two and SSE alignment sizes */
  680. ovi->output_width &= 0xFFFFFFFC;
  681. ovi->output_height &= 0xFFFFFFFE;
  682. if (!video->graphics) {
  683. int errorcode = obs_init_graphics(ovi);
  684. if (errorcode != OBS_VIDEO_SUCCESS) {
  685. obs_free_graphics();
  686. return errorcode;
  687. }
  688. }
  689. blog(LOG_INFO, "video settings reset:\n"
  690. "\tbase resolution: %dx%d\n"
  691. "\toutput resolution: %dx%d\n"
  692. "\tfps: %d/%d\n"
  693. "\tformat: %s",
  694. ovi->base_width, ovi->base_height,
  695. ovi->output_width, ovi->output_height,
  696. ovi->fps_num, ovi->fps_den,
  697. get_video_format_name(ovi->output_format));
  698. return obs_init_video(ovi);
  699. }
  700. bool obs_reset_audio(const struct obs_audio_info *oai)
  701. {
  702. struct audio_output_info ai;
  703. if (!obs) return false;
  704. /* don't allow changing of audio settings if active. */
  705. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  706. return false;
  707. obs_free_audio();
  708. if (!oai)
  709. return true;
  710. ai.name = "Audio";
  711. ai.samples_per_sec = oai->samples_per_sec;
  712. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  713. ai.speakers = oai->speakers;
  714. ai.buffer_ms = oai->buffer_ms;
  715. blog(LOG_INFO, "audio settings reset:\n"
  716. "\tsamples per sec: %d\n"
  717. "\tspeakers: %d\n"
  718. "\tbuffering (ms): %d\n",
  719. (int)ai.samples_per_sec,
  720. (int)ai.speakers,
  721. (int)ai.buffer_ms);
  722. return obs_init_audio(&ai);
  723. }
  724. bool obs_get_video_info(struct obs_video_info *ovi)
  725. {
  726. struct obs_core_video *video = &obs->video;
  727. const struct video_output_info *info;
  728. if (!obs || !video->graphics)
  729. return false;
  730. info = video_output_get_info(video->video);
  731. if (!info)
  732. return false;
  733. memset(ovi, 0, sizeof(struct obs_video_info));
  734. ovi->base_width = video->base_width;
  735. ovi->base_height = video->base_height;
  736. ovi->gpu_conversion= video->gpu_conversion;
  737. ovi->scale_type = video->scale_type;
  738. ovi->colorspace = info->colorspace;
  739. ovi->range = info->range;
  740. ovi->output_width = info->width;
  741. ovi->output_height = info->height;
  742. ovi->output_format = info->format;
  743. ovi->fps_num = info->fps_num;
  744. ovi->fps_den = info->fps_den;
  745. return true;
  746. }
  747. bool obs_get_audio_info(struct obs_audio_info *oai)
  748. {
  749. struct obs_core_audio *audio = &obs->audio;
  750. const struct audio_output_info *info;
  751. if (!obs || !oai || !audio->audio)
  752. return false;
  753. info = audio_output_get_info(audio->audio);
  754. oai->samples_per_sec = info->samples_per_sec;
  755. oai->speakers = info->speakers;
  756. oai->buffer_ms = info->buffer_ms;
  757. return true;
  758. }
  759. bool obs_enum_input_types(size_t idx, const char **id)
  760. {
  761. if (!obs) return false;
  762. if (idx >= obs->input_types.num)
  763. return false;
  764. *id = obs->input_types.array[idx].id;
  765. return true;
  766. }
  767. bool obs_enum_filter_types(size_t idx, const char **id)
  768. {
  769. if (!obs) return false;
  770. if (idx >= obs->filter_types.num)
  771. return false;
  772. *id = obs->filter_types.array[idx].id;
  773. return true;
  774. }
  775. bool obs_enum_transition_types(size_t idx, const char **id)
  776. {
  777. if (!obs) return false;
  778. if (idx >= obs->transition_types.num)
  779. return false;
  780. *id = obs->transition_types.array[idx].id;
  781. return true;
  782. }
  783. bool obs_enum_output_types(size_t idx, const char **id)
  784. {
  785. if (!obs) return false;
  786. if (idx >= obs->output_types.num)
  787. return false;
  788. *id = obs->output_types.array[idx].id;
  789. return true;
  790. }
  791. bool obs_enum_encoder_types(size_t idx, const char **id)
  792. {
  793. if (!obs) return false;
  794. if (idx >= obs->encoder_types.num)
  795. return false;
  796. *id = obs->encoder_types.array[idx].id;
  797. return true;
  798. }
  799. bool obs_enum_service_types(size_t idx, const char **id)
  800. {
  801. if (!obs) return false;
  802. if (idx >= obs->service_types.num)
  803. return false;
  804. *id = obs->service_types.array[idx].id;
  805. return true;
  806. }
  807. void obs_enter_graphics(void)
  808. {
  809. if (obs && obs->video.graphics)
  810. gs_enter_context(obs->video.graphics);
  811. }
  812. void obs_leave_graphics(void)
  813. {
  814. if (obs && obs->video.graphics)
  815. gs_leave_context();
  816. }
  817. audio_t *obs_get_audio(void)
  818. {
  819. return (obs != NULL) ? obs->audio.audio : NULL;
  820. }
  821. video_t *obs_get_video(void)
  822. {
  823. return (obs != NULL) ? obs->video.video : NULL;
  824. }
  825. /* TODO: optimize this later so it's not just O(N) string lookups */
  826. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  827. const char *task, const char *target)
  828. {
  829. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  830. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  831. if (strcmp(callback->id, id) == 0 &&
  832. strcmp(callback->task, task) == 0 &&
  833. strcmp(callback->target, target) == 0)
  834. return callback;
  835. }
  836. return NULL;
  837. }
  838. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  839. const char *task, const char *target)
  840. {
  841. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  842. struct obs_modeless_ui *callback;
  843. callback = obs->modeless_ui_callbacks.array+i;
  844. if (strcmp(callback->id, id) == 0 &&
  845. strcmp(callback->task, task) == 0 &&
  846. strcmp(callback->target, target) == 0)
  847. return callback;
  848. }
  849. return NULL;
  850. }
  851. int obs_exec_ui(const char *name, const char *task, const char *target,
  852. void *data, void *ui_data)
  853. {
  854. struct obs_modal_ui *callback;
  855. int errorcode = OBS_UI_NOTFOUND;
  856. if (!obs) return errorcode;
  857. callback = get_modal_ui_callback(name, task, target);
  858. if (callback) {
  859. bool success = callback->exec(data, ui_data);
  860. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  861. }
  862. return errorcode;
  863. }
  864. void *obs_create_ui(const char *name, const char *task, const char *target,
  865. void *data, void *ui_data)
  866. {
  867. struct obs_modeless_ui *callback;
  868. if (!obs) return NULL;
  869. callback = get_modeless_ui_callback(name, task, target);
  870. return callback ? callback->create(data, ui_data) : NULL;
  871. }
  872. bool obs_add_source(obs_source_t *source)
  873. {
  874. struct calldata params = {0};
  875. if (!obs) return false;
  876. if (!source) return false;
  877. pthread_mutex_lock(&obs->data.sources_mutex);
  878. da_push_back(obs->data.user_sources, &source);
  879. obs_source_addref(source);
  880. pthread_mutex_unlock(&obs->data.sources_mutex);
  881. calldata_set_ptr(&params, "source", source);
  882. signal_handler_signal(obs->signals, "source_add", &params);
  883. calldata_free(&params);
  884. return true;
  885. }
  886. obs_source_t *obs_get_output_source(uint32_t channel)
  887. {
  888. if (!obs) return NULL;
  889. return obs_view_get_source(&obs->data.main_view, channel);
  890. }
  891. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  892. {
  893. assert(channel < MAX_CHANNELS);
  894. if (!obs) return;
  895. if (channel >= MAX_CHANNELS) return;
  896. struct obs_source *prev_source;
  897. struct obs_view *view = &obs->data.main_view;
  898. struct calldata params = {0};
  899. pthread_mutex_lock(&view->channels_mutex);
  900. obs_source_addref(source);
  901. prev_source = view->channels[channel];
  902. calldata_set_int(&params, "channel", channel);
  903. calldata_set_ptr(&params, "prev_source", prev_source);
  904. calldata_set_ptr(&params, "source", source);
  905. signal_handler_signal(obs->signals, "channel_change", &params);
  906. calldata_get_ptr(&params, "source", &source);
  907. calldata_free(&params);
  908. view->channels[channel] = source;
  909. pthread_mutex_unlock(&view->channels_mutex);
  910. if (source)
  911. obs_source_activate(source, MAIN_VIEW);
  912. if (prev_source) {
  913. obs_source_deactivate(prev_source, MAIN_VIEW);
  914. obs_source_release(prev_source);
  915. }
  916. }
  917. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  918. {
  919. if (!obs) return;
  920. pthread_mutex_lock(&obs->data.user_sources_mutex);
  921. for (size_t i = 0; i < obs->data.user_sources.num; i++) {
  922. struct obs_source *source = obs->data.user_sources.array[i];
  923. if (!enum_proc(param, source))
  924. break;
  925. }
  926. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  927. }
  928. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  929. void *param)
  930. {
  931. struct obs_context_data **start = pstart, *context;
  932. bool (*enum_proc)(void*, void*) = proc;
  933. assert(start);
  934. assert(mutex);
  935. assert(enum_proc);
  936. pthread_mutex_lock(mutex);
  937. context = *start;
  938. while (context) {
  939. if (!enum_proc(param, context))
  940. break;
  941. context = context->next;
  942. }
  943. pthread_mutex_unlock(mutex);
  944. }
  945. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  946. {
  947. if (!obs) return;
  948. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  949. enum_proc, param);
  950. }
  951. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  952. {
  953. if (!obs) return;
  954. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  955. enum_proc, param);
  956. }
  957. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  958. {
  959. if (!obs) return;
  960. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  961. enum_proc, param);
  962. }
  963. obs_source_t *obs_get_source_by_name(const char *name)
  964. {
  965. struct obs_core_data *data = &obs->data;
  966. struct obs_source *source = NULL;
  967. size_t i;
  968. if (!obs) return NULL;
  969. pthread_mutex_lock(&data->user_sources_mutex);
  970. for (i = 0; i < data->user_sources.num; i++) {
  971. struct obs_source *cur_source = data->user_sources.array[i];
  972. if (strcmp(cur_source->context.name, name) == 0) {
  973. source = cur_source;
  974. obs_source_addref(source);
  975. break;
  976. }
  977. }
  978. pthread_mutex_unlock(&data->user_sources_mutex);
  979. return source;
  980. }
  981. static inline void *get_context_by_name(void *vfirst, const char *name,
  982. pthread_mutex_t *mutex, void *(*addref)(void*))
  983. {
  984. struct obs_context_data **first = vfirst;
  985. struct obs_context_data *context;
  986. pthread_mutex_lock(mutex);
  987. context = *first;
  988. while (context) {
  989. if (strcmp(context->name, name) == 0) {
  990. context = addref(context);
  991. break;
  992. }
  993. context = context->next;
  994. }
  995. pthread_mutex_unlock(mutex);
  996. return context;
  997. }
  998. static inline void *obs_output_addref_safe_(void *ref)
  999. {
  1000. return obs_output_get_ref(ref);
  1001. }
  1002. static inline void *obs_encoder_addref_safe_(void *ref)
  1003. {
  1004. return obs_encoder_get_ref(ref);
  1005. }
  1006. static inline void *obs_service_addref_safe_(void *ref)
  1007. {
  1008. return obs_service_get_ref(ref);
  1009. }
  1010. static inline void *obs_id_(void *data)
  1011. {
  1012. return data;
  1013. }
  1014. obs_output_t *obs_get_output_by_name(const char *name)
  1015. {
  1016. if (!obs) return NULL;
  1017. return get_context_by_name(&obs->data.first_output, name,
  1018. &obs->data.outputs_mutex, obs_output_addref_safe_);
  1019. }
  1020. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1021. {
  1022. if (!obs) return NULL;
  1023. return get_context_by_name(&obs->data.first_encoder, name,
  1024. &obs->data.encoders_mutex, obs_encoder_addref_safe_);
  1025. }
  1026. obs_service_t *obs_get_service_by_name(const char *name)
  1027. {
  1028. if (!obs) return NULL;
  1029. return get_context_by_name(&obs->data.first_service, name,
  1030. &obs->data.services_mutex, obs_service_addref_safe_);
  1031. }
  1032. gs_effect_t *obs_get_default_effect(void)
  1033. {
  1034. if (!obs) return NULL;
  1035. return obs->video.default_effect;
  1036. }
  1037. gs_effect_t *obs_get_default_rect_effect(void)
  1038. {
  1039. if (!obs) return NULL;
  1040. return obs->video.default_rect_effect;
  1041. }
  1042. gs_effect_t *obs_get_opaque_effect(void)
  1043. {
  1044. if (!obs) return NULL;
  1045. return obs->video.opaque_effect;
  1046. }
  1047. gs_effect_t *obs_get_solid_effect(void)
  1048. {
  1049. if (!obs) return NULL;
  1050. return obs->video.solid_effect;
  1051. }
  1052. gs_effect_t *obs_get_bicubic_effect(void)
  1053. {
  1054. if (!obs) return NULL;
  1055. return obs->video.bicubic_effect;
  1056. }
  1057. gs_effect_t *obs_get_lanczos_effect(void)
  1058. {
  1059. if (!obs) return NULL;
  1060. return obs->video.lanczos_effect;
  1061. }
  1062. gs_effect_t *obs_get_bilinear_lowres_effect(void)
  1063. {
  1064. if (!obs) return NULL;
  1065. return obs->video.bilinear_lowres_effect;
  1066. }
  1067. signal_handler_t *obs_get_signal_handler(void)
  1068. {
  1069. if (!obs) return NULL;
  1070. return obs->signals;
  1071. }
  1072. proc_handler_t *obs_get_proc_handler(void)
  1073. {
  1074. if (!obs) return NULL;
  1075. return obs->procs;
  1076. }
  1077. void obs_add_draw_callback(
  1078. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1079. void *param)
  1080. {
  1081. if (!obs) return;
  1082. obs_display_add_draw_callback(&obs->video.main_display, draw, param);
  1083. }
  1084. void obs_remove_draw_callback(
  1085. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1086. void *param)
  1087. {
  1088. if (!obs) return;
  1089. obs_display_remove_draw_callback(&obs->video.main_display, draw, param);
  1090. }
  1091. void obs_resize(uint32_t cx, uint32_t cy)
  1092. {
  1093. if (!obs || !obs->video.video || !obs->video.graphics) return;
  1094. obs_display_resize(&obs->video.main_display, cx, cy);
  1095. }
  1096. void obs_render_main_view(void)
  1097. {
  1098. if (!obs) return;
  1099. obs_view_render(&obs->data.main_view);
  1100. }
  1101. void obs_set_master_volume(float volume)
  1102. {
  1103. struct calldata data = {0};
  1104. if (!obs) return;
  1105. calldata_set_float(&data, "volume", volume);
  1106. signal_handler_signal(obs->signals, "master_volume", &data);
  1107. volume = (float)calldata_float(&data, "volume");
  1108. calldata_free(&data);
  1109. obs->audio.user_volume = volume;
  1110. }
  1111. void obs_set_present_volume(float volume)
  1112. {
  1113. if (!obs) return;
  1114. obs->audio.present_volume = volume;
  1115. }
  1116. float obs_get_master_volume(void)
  1117. {
  1118. return obs ? obs->audio.user_volume : 0.0f;
  1119. }
  1120. float obs_get_present_volume(void)
  1121. {
  1122. return obs ? obs->audio.present_volume : 0.0f;
  1123. }
  1124. static obs_source_t *obs_load_source_type(obs_data_t *source_data,
  1125. enum obs_source_type type)
  1126. {
  1127. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1128. obs_source_t *source;
  1129. const char *name = obs_data_get_string(source_data, "name");
  1130. const char *id = obs_data_get_string(source_data, "id");
  1131. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1132. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1133. double volume;
  1134. int64_t sync;
  1135. uint32_t flags;
  1136. uint32_t mixers;
  1137. source = obs_source_create(type, id, name, settings, hotkeys);
  1138. obs_data_release(hotkeys);
  1139. obs_data_set_default_double(source_data, "volume", 1.0);
  1140. volume = obs_data_get_double(source_data, "volume");
  1141. obs_source_set_volume(source, (float)volume);
  1142. sync = obs_data_get_int(source_data, "sync");
  1143. obs_source_set_sync_offset(source, sync);
  1144. obs_data_set_default_int(source_data, "mixers", 0xF);
  1145. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1146. obs_source_set_audio_mixers(source, mixers);
  1147. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1148. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1149. obs_source_set_flags(source, flags);
  1150. obs_data_set_default_bool(source_data, "enabled", true);
  1151. obs_source_set_enabled(source,
  1152. obs_data_get_bool(source_data, "enabled"));
  1153. obs_data_set_default_bool(source_data, "muted", false);
  1154. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1155. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1156. obs_source_enable_push_to_mute(source,
  1157. obs_data_get_bool(source_data, "push-to-mute"));
  1158. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1159. obs_source_set_push_to_mute_delay(source,
  1160. obs_data_get_int(source_data, "push-to-mute-delay"));
  1161. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1162. obs_source_enable_push_to_talk(source,
  1163. obs_data_get_bool(source_data, "push-to-talk"));
  1164. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1165. obs_source_set_push_to_talk_delay(source,
  1166. obs_data_get_int(source_data, "push-to-talk-delay"));
  1167. if (filters) {
  1168. size_t count = obs_data_array_count(filters);
  1169. for (size_t i = 0; i < count; i++) {
  1170. obs_data_t *filter_data =
  1171. obs_data_array_item(filters, i);
  1172. obs_source_t *filter = obs_load_source_type(
  1173. filter_data, OBS_SOURCE_TYPE_FILTER);
  1174. if (filter) {
  1175. obs_source_filter_add(source, filter);
  1176. obs_source_release(filter);
  1177. }
  1178. obs_data_release(filter_data);
  1179. }
  1180. obs_data_array_release(filters);
  1181. }
  1182. obs_data_release(settings);
  1183. return source;
  1184. }
  1185. obs_source_t *obs_load_source(obs_data_t *source_data)
  1186. {
  1187. return obs_load_source_type(source_data, OBS_SOURCE_TYPE_INPUT);
  1188. }
  1189. void obs_load_sources(obs_data_array_t *array)
  1190. {
  1191. size_t count;
  1192. size_t i;
  1193. if (!obs) return;
  1194. count = obs_data_array_count(array);
  1195. pthread_mutex_lock(&obs->data.user_sources_mutex);
  1196. for (i = 0; i < count; i++) {
  1197. obs_data_t *source_data = obs_data_array_item(array, i);
  1198. obs_source_t *source = obs_load_source(source_data);
  1199. obs_add_source(source);
  1200. obs_source_release(source);
  1201. obs_data_release(source_data);
  1202. }
  1203. /* tell sources that we want to load */
  1204. for (i = 0; i < obs->data.user_sources.num; i++)
  1205. obs_source_load(obs->data.user_sources.array[i]);
  1206. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  1207. }
  1208. obs_data_t *obs_save_source(obs_source_t *source)
  1209. {
  1210. obs_data_array_t *filters = obs_data_array_create();
  1211. obs_data_t *source_data = obs_data_create();
  1212. obs_data_t *settings = obs_source_get_settings(source);
  1213. obs_data_t *hotkey_data = source->context.hotkey_data;
  1214. obs_data_t *hotkeys;
  1215. float volume = obs_source_get_volume(source);
  1216. uint32_t mixers = obs_source_get_audio_mixers(source);
  1217. int64_t sync = obs_source_get_sync_offset(source);
  1218. uint32_t flags = obs_source_get_flags(source);
  1219. const char *name = obs_source_get_name(source);
  1220. const char *id = obs_source_get_id(source);
  1221. bool enabled = obs_source_enabled(source);
  1222. bool muted = obs_source_muted(source);
  1223. bool push_to_mute= obs_source_push_to_mute_enabled(source);
  1224. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1225. bool push_to_talk= obs_source_push_to_talk_enabled(source);
  1226. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1227. obs_source_save(source);
  1228. hotkeys = obs_hotkeys_save_source(source);
  1229. if (hotkeys) {
  1230. obs_data_release(hotkey_data);
  1231. source->context.hotkey_data = hotkeys;
  1232. hotkey_data = hotkeys;
  1233. }
  1234. obs_data_set_string(source_data, "name", name);
  1235. obs_data_set_string(source_data, "id", id);
  1236. obs_data_set_obj (source_data, "settings", settings);
  1237. obs_data_set_int (source_data, "mixers", mixers);
  1238. obs_data_set_int (source_data, "sync", sync);
  1239. obs_data_set_int (source_data, "flags", flags);
  1240. obs_data_set_double(source_data, "volume", volume);
  1241. obs_data_set_bool (source_data, "enabled", enabled);
  1242. obs_data_set_bool (source_data, "muted", muted);
  1243. obs_data_set_bool (source_data, "push-to-mute", push_to_mute);
  1244. obs_data_set_int (source_data, "push-to-mute-delay", ptm_delay);
  1245. obs_data_set_bool (source_data, "push-to-talk", push_to_talk);
  1246. obs_data_set_int (source_data, "push-to-talk-delay", ptt_delay);
  1247. obs_data_set_obj (source_data, "hotkeys", hotkey_data);
  1248. pthread_mutex_lock(&source->filter_mutex);
  1249. if (source->filters.num) {
  1250. for (size_t i = source->filters.num; i > 0; i--) {
  1251. obs_source_t *filter = source->filters.array[i - 1];
  1252. obs_data_t *filter_data = obs_save_source(filter);
  1253. obs_data_array_push_back(filters, filter_data);
  1254. obs_data_release(filter_data);
  1255. }
  1256. obs_data_set_array(source_data, "filters", filters);
  1257. }
  1258. pthread_mutex_unlock(&source->filter_mutex);
  1259. obs_data_release(settings);
  1260. obs_data_array_release(filters);
  1261. return source_data;
  1262. }
  1263. obs_data_array_t *obs_save_sources(void)
  1264. {
  1265. obs_data_array_t *array;
  1266. size_t i;
  1267. if (!obs) return NULL;
  1268. array = obs_data_array_create();
  1269. pthread_mutex_lock(&obs->data.user_sources_mutex);
  1270. for (i = 0; i < obs->data.user_sources.num; i++) {
  1271. obs_source_t *source = obs->data.user_sources.array[i];
  1272. obs_data_t *source_data = obs_save_source(source);
  1273. obs_data_array_push_back(array, source_data);
  1274. obs_data_release(source_data);
  1275. }
  1276. pthread_mutex_unlock(&obs->data.user_sources_mutex);
  1277. return array;
  1278. }
  1279. /* ensures that names are never blank */
  1280. static inline char *dup_name(const char *name)
  1281. {
  1282. if (!name || !*name) {
  1283. struct dstr unnamed = {0};
  1284. dstr_printf(&unnamed, "__unnamed%004lld",
  1285. obs->data.unnamed_index++);
  1286. return unnamed.array;
  1287. } else {
  1288. return bstrdup(name);
  1289. }
  1290. }
  1291. static inline bool obs_context_data_init_wrap(
  1292. struct obs_context_data *context,
  1293. obs_data_t *settings,
  1294. const char *name,
  1295. obs_data_t *hotkey_data)
  1296. {
  1297. assert(context);
  1298. memset(context, 0, sizeof(*context));
  1299. pthread_mutex_init_value(&context->rename_cache_mutex);
  1300. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1301. return false;
  1302. context->signals = signal_handler_create();
  1303. if (!context->signals)
  1304. return false;
  1305. context->procs = proc_handler_create();
  1306. if (!context->procs)
  1307. return false;
  1308. context->name = dup_name(name);
  1309. context->settings = obs_data_newref(settings);
  1310. context->hotkey_data = obs_data_newref(hotkey_data);
  1311. return true;
  1312. }
  1313. bool obs_context_data_init(
  1314. struct obs_context_data *context,
  1315. obs_data_t *settings,
  1316. const char *name,
  1317. obs_data_t *hotkey_data)
  1318. {
  1319. if (obs_context_data_init_wrap(context, settings, name, hotkey_data)) {
  1320. return true;
  1321. } else {
  1322. obs_context_data_free(context);
  1323. return false;
  1324. }
  1325. }
  1326. void obs_context_data_free(struct obs_context_data *context)
  1327. {
  1328. obs_hotkeys_context_release(context);
  1329. signal_handler_destroy(context->signals);
  1330. proc_handler_destroy(context->procs);
  1331. obs_data_release(context->settings);
  1332. obs_context_data_remove(context);
  1333. pthread_mutex_destroy(&context->rename_cache_mutex);
  1334. bfree(context->name);
  1335. for (size_t i = 0; i < context->rename_cache.num; i++)
  1336. bfree(context->rename_cache.array[i]);
  1337. da_free(context->rename_cache);
  1338. memset(context, 0, sizeof(*context));
  1339. }
  1340. void obs_context_data_insert(struct obs_context_data *context,
  1341. pthread_mutex_t *mutex, void *pfirst)
  1342. {
  1343. struct obs_context_data **first = pfirst;
  1344. assert(context);
  1345. assert(mutex);
  1346. assert(first);
  1347. context->mutex = mutex;
  1348. pthread_mutex_lock(mutex);
  1349. context->prev_next = first;
  1350. context->next = *first;
  1351. *first = context;
  1352. if (context->next)
  1353. context->next->prev_next = &context->next;
  1354. pthread_mutex_unlock(mutex);
  1355. }
  1356. void obs_context_data_remove(struct obs_context_data *context)
  1357. {
  1358. if (context && context->mutex) {
  1359. pthread_mutex_lock(context->mutex);
  1360. if (context->prev_next)
  1361. *context->prev_next = context->next;
  1362. if (context->next)
  1363. context->next->prev_next = context->prev_next;
  1364. pthread_mutex_unlock(context->mutex);
  1365. context->mutex = NULL;
  1366. }
  1367. }
  1368. void obs_context_data_setname(struct obs_context_data *context,
  1369. const char *name)
  1370. {
  1371. pthread_mutex_lock(&context->rename_cache_mutex);
  1372. if (context->name)
  1373. da_push_back(context->rename_cache, &context->name);
  1374. context->name = dup_name(name);
  1375. pthread_mutex_unlock(&context->rename_cache_mutex);
  1376. }
  1377. void obs_preview_set_enabled(bool enable)
  1378. {
  1379. if (obs)
  1380. obs->video.main_display.enabled = enable;
  1381. }
  1382. bool obs_preview_enabled(void)
  1383. {
  1384. return obs ? obs->video.main_display.enabled : false;
  1385. }