obs.c 49 KB

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