obs.c 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235
  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 = obs_find_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 = obs_find_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 = obs_find_data_file("default_rect.effect");
  202. video->default_rect_effect = gs_effect_create_from_file(
  203. filename, NULL);
  204. bfree(filename);
  205. }
  206. filename = obs_find_data_file("opaque.effect");
  207. video->opaque_effect = gs_effect_create_from_file(filename,
  208. NULL);
  209. bfree(filename);
  210. filename = obs_find_data_file("solid.effect");
  211. video->solid_effect = gs_effect_create_from_file(filename,
  212. NULL);
  213. bfree(filename);
  214. filename = obs_find_data_file("format_conversion.effect");
  215. video->conversion_effect = gs_effect_create_from_file(filename,
  216. NULL);
  217. bfree(filename);
  218. filename = obs_find_data_file("bicubic_scale.effect");
  219. video->bicubic_effect = gs_effect_create_from_file(filename,
  220. NULL);
  221. bfree(filename);
  222. filename = obs_find_data_file("lanczos_scale.effect");
  223. video->lanczos_effect = gs_effect_create_from_file(filename,
  224. NULL);
  225. bfree(filename);
  226. filename = obs_find_data_file("bilinear_lowres_scale.effect");
  227. video->bilinear_lowres_effect = gs_effect_create_from_file(filename,
  228. NULL);
  229. bfree(filename);
  230. filename = obs_find_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_graphics_thread, obs);
  307. if (errorcode != 0)
  308. return OBS_VIDEO_FAIL;
  309. video->thread_initialized = true;
  310. video->ovi = *ovi;
  311. return OBS_VIDEO_SUCCESS;
  312. }
  313. static void stop_video(void)
  314. {
  315. struct obs_core_video *video = &obs->video;
  316. void *thread_retval;
  317. if (video->video) {
  318. video_output_stop(video->video);
  319. if (video->thread_initialized) {
  320. pthread_join(video->video_thread, &thread_retval);
  321. video->thread_initialized = false;
  322. }
  323. }
  324. }
  325. static void obs_free_video(void)
  326. {
  327. struct obs_core_video *video = &obs->video;
  328. if (video->video) {
  329. video_output_close(video->video);
  330. video->video = NULL;
  331. if (!video->graphics)
  332. return;
  333. gs_enter_context(video->graphics);
  334. if (video->mapped_surface) {
  335. gs_stagesurface_unmap(video->mapped_surface);
  336. video->mapped_surface = NULL;
  337. }
  338. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  339. gs_stagesurface_destroy(video->copy_surfaces[i]);
  340. gs_texture_destroy(video->render_textures[i]);
  341. gs_texture_destroy(video->convert_textures[i]);
  342. gs_texture_destroy(video->output_textures[i]);
  343. video->copy_surfaces[i] = NULL;
  344. video->render_textures[i] = NULL;
  345. video->convert_textures[i] = NULL;
  346. video->output_textures[i] = NULL;
  347. }
  348. gs_leave_context();
  349. circlebuf_free(&video->vframe_info_buffer);
  350. memset(&video->textures_rendered, 0,
  351. sizeof(video->textures_rendered));
  352. memset(&video->textures_output, 0,
  353. sizeof(video->textures_output));
  354. memset(&video->textures_copied, 0,
  355. sizeof(video->textures_copied));
  356. memset(&video->textures_converted, 0,
  357. sizeof(video->textures_converted));
  358. video->cur_texture = 0;
  359. }
  360. }
  361. static void obs_free_graphics(void)
  362. {
  363. struct obs_core_video *video = &obs->video;
  364. if (video->graphics) {
  365. gs_enter_context(video->graphics);
  366. gs_texture_destroy(video->transparent_texture);
  367. gs_samplerstate_destroy(video->point_sampler);
  368. gs_effect_destroy(video->default_effect);
  369. gs_effect_destroy(video->default_rect_effect);
  370. gs_effect_destroy(video->opaque_effect);
  371. gs_effect_destroy(video->solid_effect);
  372. gs_effect_destroy(video->conversion_effect);
  373. gs_effect_destroy(video->bicubic_effect);
  374. gs_effect_destroy(video->lanczos_effect);
  375. gs_effect_destroy(video->bilinear_lowres_effect);
  376. video->default_effect = NULL;
  377. gs_leave_context();
  378. gs_destroy(video->graphics);
  379. video->graphics = NULL;
  380. }
  381. }
  382. static bool obs_init_audio(struct audio_output_info *ai)
  383. {
  384. struct obs_core_audio *audio = &obs->audio;
  385. int errorcode;
  386. pthread_mutexattr_t attr;
  387. pthread_mutex_init_value(&audio->monitoring_mutex);
  388. if (pthread_mutexattr_init(&attr) != 0)
  389. return false;
  390. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  391. return false;
  392. if (pthread_mutex_init(&audio->monitoring_mutex, &attr) != 0)
  393. return false;
  394. audio->user_volume = 1.0f;
  395. audio->monitoring_device_name = bstrdup("Default");
  396. audio->monitoring_device_id = bstrdup("default");
  397. errorcode = audio_output_open(&audio->audio, ai);
  398. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  399. return true;
  400. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  401. blog(LOG_ERROR, "Invalid audio parameters specified");
  402. else
  403. blog(LOG_ERROR, "Could not open audio output");
  404. return false;
  405. }
  406. static void obs_free_audio(void)
  407. {
  408. struct obs_core_audio *audio = &obs->audio;
  409. if (audio->audio)
  410. audio_output_close(audio->audio);
  411. circlebuf_free(&audio->buffered_timestamps);
  412. da_free(audio->render_order);
  413. da_free(audio->root_nodes);
  414. da_free(audio->monitors);
  415. bfree(audio->monitoring_device_name);
  416. bfree(audio->monitoring_device_id);
  417. pthread_mutex_destroy(&audio->monitoring_mutex);
  418. memset(audio, 0, sizeof(struct obs_core_audio));
  419. }
  420. static bool obs_init_data(void)
  421. {
  422. struct obs_core_data *data = &obs->data;
  423. pthread_mutexattr_t attr;
  424. assert(data != NULL);
  425. pthread_mutex_init_value(&obs->data.displays_mutex);
  426. pthread_mutex_init_value(&obs->data.draw_callbacks_mutex);
  427. if (pthread_mutexattr_init(&attr) != 0)
  428. return false;
  429. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  430. goto fail;
  431. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  432. goto fail;
  433. if (pthread_mutex_init(&data->audio_sources_mutex, &attr) != 0)
  434. goto fail;
  435. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  436. goto fail;
  437. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  438. goto fail;
  439. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  440. goto fail;
  441. if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
  442. goto fail;
  443. if (pthread_mutex_init(&obs->data.draw_callbacks_mutex, &attr) != 0)
  444. goto fail;
  445. if (!obs_view_init(&data->main_view))
  446. goto fail;
  447. data->private_data = obs_data_create();
  448. data->valid = true;
  449. fail:
  450. pthread_mutexattr_destroy(&attr);
  451. return data->valid;
  452. }
  453. void obs_main_view_free(struct obs_view *view)
  454. {
  455. if (!view) return;
  456. for (size_t i = 0; i < MAX_CHANNELS; i++)
  457. obs_source_release(view->channels[i]);
  458. memset(view->channels, 0, sizeof(view->channels));
  459. pthread_mutex_destroy(&view->channels_mutex);
  460. }
  461. #define FREE_OBS_LINKED_LIST(type) \
  462. do { \
  463. int unfreed = 0; \
  464. while (data->first_ ## type ) { \
  465. obs_ ## type ## _destroy(data->first_ ## type ); \
  466. unfreed++; \
  467. } \
  468. if (unfreed) \
  469. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  470. unfreed); \
  471. } while (false)
  472. static void obs_free_data(void)
  473. {
  474. struct obs_core_data *data = &obs->data;
  475. data->valid = false;
  476. obs_main_view_free(&data->main_view);
  477. blog(LOG_INFO, "Freeing OBS context data");
  478. FREE_OBS_LINKED_LIST(source);
  479. FREE_OBS_LINKED_LIST(output);
  480. FREE_OBS_LINKED_LIST(encoder);
  481. FREE_OBS_LINKED_LIST(display);
  482. FREE_OBS_LINKED_LIST(service);
  483. pthread_mutex_destroy(&data->sources_mutex);
  484. pthread_mutex_destroy(&data->audio_sources_mutex);
  485. pthread_mutex_destroy(&data->displays_mutex);
  486. pthread_mutex_destroy(&data->outputs_mutex);
  487. pthread_mutex_destroy(&data->encoders_mutex);
  488. pthread_mutex_destroy(&data->services_mutex);
  489. pthread_mutex_destroy(&data->draw_callbacks_mutex);
  490. da_free(data->draw_callbacks);
  491. da_free(data->tick_callbacks);
  492. obs_data_release(data->private_data);
  493. }
  494. static const char *obs_signals[] = {
  495. "void source_create(ptr source)",
  496. "void source_destroy(ptr source)",
  497. "void source_remove(ptr source)",
  498. "void source_save(ptr source)",
  499. "void source_load(ptr source)",
  500. "void source_activate(ptr source)",
  501. "void source_deactivate(ptr source)",
  502. "void source_show(ptr source)",
  503. "void source_hide(ptr source)",
  504. "void source_rename(ptr source, string new_name, string prev_name)",
  505. "void source_volume(ptr source, in out float volume)",
  506. "void source_volume_level(ptr source, float level, float magnitude, "
  507. "float peak)",
  508. "void source_transition_start(ptr source)",
  509. "void source_transition_video_stop(ptr source)",
  510. "void source_transition_stop(ptr source)",
  511. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  512. "void master_volume(in out float volume)",
  513. "void hotkey_layout_change()",
  514. "void hotkey_register(ptr hotkey)",
  515. "void hotkey_unregister(ptr hotkey)",
  516. "void hotkey_bindings_changed(ptr hotkey)",
  517. NULL
  518. };
  519. static inline bool obs_init_handlers(void)
  520. {
  521. obs->signals = signal_handler_create();
  522. if (!obs->signals)
  523. return false;
  524. obs->procs = proc_handler_create();
  525. if (!obs->procs)
  526. return false;
  527. return signal_handler_add_array(obs->signals, obs_signals);
  528. }
  529. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  530. static inline bool obs_init_hotkeys(void)
  531. {
  532. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  533. pthread_mutexattr_t attr;
  534. bool success = false;
  535. assert(hotkeys != NULL);
  536. da_init(hotkeys->hotkeys);
  537. hotkeys->signals = obs->signals;
  538. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  539. hotkeys->mute = bstrdup("Mute");
  540. hotkeys->unmute = bstrdup("Unmute");
  541. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  542. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  543. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  544. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  545. if (!obs_hotkeys_platform_init(hotkeys))
  546. return false;
  547. if (pthread_mutexattr_init(&attr) != 0)
  548. return false;
  549. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  550. goto fail;
  551. if (pthread_mutex_init(&hotkeys->mutex, &attr) != 0)
  552. goto fail;
  553. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  554. goto fail;
  555. if (pthread_create(&hotkeys->hotkey_thread, NULL,
  556. obs_hotkey_thread, NULL))
  557. goto fail;
  558. hotkeys->hotkey_thread_initialized = true;
  559. success = true;
  560. fail:
  561. pthread_mutexattr_destroy(&attr);
  562. return success;
  563. }
  564. static inline void stop_hotkeys(void)
  565. {
  566. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  567. void *thread_ret;
  568. if (hotkeys->hotkey_thread_initialized) {
  569. os_event_signal(hotkeys->stop_event);
  570. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  571. hotkeys->hotkey_thread_initialized = false;
  572. }
  573. os_event_destroy(hotkeys->stop_event);
  574. obs_hotkeys_free();
  575. }
  576. static inline void obs_free_hotkeys(void)
  577. {
  578. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  579. bfree(hotkeys->mute);
  580. bfree(hotkeys->unmute);
  581. bfree(hotkeys->push_to_mute);
  582. bfree(hotkeys->push_to_talk);
  583. bfree(hotkeys->sceneitem_show);
  584. bfree(hotkeys->sceneitem_hide);
  585. obs_hotkey_name_map_free();
  586. obs_hotkeys_platform_free(hotkeys);
  587. pthread_mutex_destroy(&hotkeys->mutex);
  588. }
  589. extern const struct obs_source_info scene_info;
  590. extern const struct obs_source_info group_info;
  591. extern void log_system_info(void);
  592. static bool obs_init(const char *locale, const char *module_config_path,
  593. profiler_name_store_t *store)
  594. {
  595. obs = bzalloc(sizeof(struct obs_core));
  596. pthread_mutex_init_value(&obs->audio.monitoring_mutex);
  597. obs->name_store_owned = !store;
  598. obs->name_store = store ? store : profiler_name_store_create();
  599. if (!obs->name_store) {
  600. blog(LOG_ERROR, "Couldn't create profiler name store");
  601. return false;
  602. }
  603. log_system_info();
  604. if (!obs_init_data())
  605. return false;
  606. if (!obs_init_handlers())
  607. return false;
  608. if (!obs_init_hotkeys())
  609. return false;
  610. if (module_config_path)
  611. obs->module_config_path = bstrdup(module_config_path);
  612. obs->locale = bstrdup(locale);
  613. obs_register_source(&scene_info);
  614. obs_register_source(&group_info);
  615. add_default_module_paths();
  616. return true;
  617. }
  618. #ifdef _WIN32
  619. extern void initialize_com(void);
  620. extern void uninitialize_com(void);
  621. #endif
  622. /* Separate from actual context initialization
  623. * since this can be set before startup and persist
  624. * after shutdown. */
  625. static DARRAY(struct dstr) core_module_paths = {0};
  626. char *obs_find_data_file(const char *file)
  627. {
  628. struct dstr path = {0};
  629. char *result = find_libobs_data_file(file);
  630. if (result)
  631. return result;
  632. for (size_t i = 0; i < core_module_paths.num; ++i) {
  633. if (check_path(file, core_module_paths.array[i].array, &path))
  634. return path.array;
  635. }
  636. dstr_free(&path);
  637. return NULL;
  638. }
  639. void obs_add_data_path(const char *path)
  640. {
  641. struct dstr *new_path = da_push_back_new(core_module_paths);
  642. dstr_init_copy(new_path, path);
  643. da_push_back(core_module_paths, new_path);
  644. }
  645. bool obs_remove_data_path(const char *path)
  646. {
  647. for (size_t i = 0; i < core_module_paths.num; ++i) {
  648. int result = dstr_cmp(&core_module_paths.array[i], path);
  649. if (result == 0) {
  650. dstr_free(&core_module_paths.array[i]);
  651. da_erase(core_module_paths, i);
  652. return true;
  653. }
  654. }
  655. return false;
  656. }
  657. static const char *obs_startup_name = "obs_startup";
  658. bool obs_startup(const char *locale, const char *module_config_path,
  659. profiler_name_store_t *store)
  660. {
  661. bool success;
  662. profile_start(obs_startup_name);
  663. if (obs) {
  664. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  665. return false;
  666. }
  667. #ifdef _WIN32
  668. initialize_com();
  669. #endif
  670. success = obs_init(locale, module_config_path, store);
  671. profile_end(obs_startup_name);
  672. if (!success)
  673. obs_shutdown();
  674. return success;
  675. }
  676. static struct obs_cmdline_args cmdline_args = {0, NULL};
  677. void obs_set_cmdline_args(int argc, char **argv)
  678. {
  679. char *data;
  680. size_t len;
  681. int i;
  682. /* Once argc is set (non-zero) we shouldn't call again */
  683. if (cmdline_args.argc)
  684. return;
  685. cmdline_args.argc = argc;
  686. /* Safely copy over argv */
  687. len = 0;
  688. for (i = 0; i < argc; i++)
  689. len += strlen(argv[i]) + 1;
  690. cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
  691. data = (char *) cmdline_args.argv + sizeof(char *) * (argc + 1);
  692. for (i = 0; i < argc; i++) {
  693. cmdline_args.argv[i] = data;
  694. len = strlen(argv[i]) + 1;
  695. memcpy(data, argv[i], len);
  696. data += len;
  697. }
  698. cmdline_args.argv[argc] = NULL;
  699. }
  700. struct obs_cmdline_args obs_get_cmdline_args(void)
  701. {
  702. return cmdline_args;
  703. }
  704. void obs_shutdown(void)
  705. {
  706. struct obs_module *module;
  707. struct obs_core *core;
  708. if (!obs)
  709. return;
  710. #define FREE_REGISTERED_TYPES(structure, list) \
  711. do { \
  712. for (size_t i = 0; i < list.num; i++) { \
  713. struct structure *item = &list.array[i]; \
  714. if (item->type_data && item->free_type_data) \
  715. item->free_type_data(item->type_data); \
  716. } \
  717. da_free(list); \
  718. } while (false)
  719. FREE_REGISTERED_TYPES(obs_source_info, obs->source_types);
  720. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  721. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  722. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  723. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  724. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  725. #undef FREE_REGISTERED_TYPES
  726. da_free(obs->input_types);
  727. da_free(obs->filter_types);
  728. da_free(obs->transition_types);
  729. stop_video();
  730. stop_hotkeys();
  731. obs_free_audio();
  732. obs_free_data();
  733. obs_free_video();
  734. obs_free_hotkeys();
  735. obs_free_graphics();
  736. proc_handler_destroy(obs->procs);
  737. signal_handler_destroy(obs->signals);
  738. obs->procs = NULL;
  739. obs->signals = NULL;
  740. core = obs;
  741. obs = NULL;
  742. module = core->first_module;
  743. while (module) {
  744. struct obs_module *next = module->next;
  745. free_module(module);
  746. module = next;
  747. }
  748. core->first_module = NULL;
  749. for (size_t i = 0; i < core->module_paths.num; i++)
  750. free_module_path(core->module_paths.array+i);
  751. da_free(core->module_paths);
  752. if (core->name_store_owned)
  753. profiler_name_store_free(core->name_store);
  754. bfree(core->module_config_path);
  755. bfree(core->locale);
  756. bfree(core);
  757. bfree(cmdline_args.argv);
  758. #ifdef _WIN32
  759. uninitialize_com();
  760. #endif
  761. }
  762. bool obs_initialized(void)
  763. {
  764. return obs != NULL;
  765. }
  766. uint32_t obs_get_version(void)
  767. {
  768. return LIBOBS_API_VER;
  769. }
  770. const char *obs_get_version_string(void)
  771. {
  772. return OBS_VERSION;
  773. }
  774. void obs_set_locale(const char *locale)
  775. {
  776. struct obs_module *module;
  777. if (!obs)
  778. return;
  779. if (obs->locale)
  780. bfree(obs->locale);
  781. obs->locale = bstrdup(locale);
  782. module = obs->first_module;
  783. while (module) {
  784. if (module->set_locale)
  785. module->set_locale(locale);
  786. module = module->next;
  787. }
  788. }
  789. const char *obs_get_locale(void)
  790. {
  791. return obs ? obs->locale : NULL;
  792. }
  793. #define OBS_SIZE_MIN 2
  794. #define OBS_SIZE_MAX (32 * 1024)
  795. static inline bool size_valid(uint32_t width, uint32_t height)
  796. {
  797. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  798. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  799. }
  800. int obs_reset_video(struct obs_video_info *ovi)
  801. {
  802. if (!obs) return OBS_VIDEO_FAIL;
  803. /* don't allow changing of video settings if active. */
  804. if (obs->video.video && video_output_active(obs->video.video))
  805. return OBS_VIDEO_CURRENTLY_ACTIVE;
  806. if (!size_valid(ovi->output_width, ovi->output_height) ||
  807. !size_valid(ovi->base_width, ovi->base_height))
  808. return OBS_VIDEO_INVALID_PARAM;
  809. struct obs_core_video *video = &obs->video;
  810. stop_video();
  811. obs_free_video();
  812. /* align to multiple-of-two and SSE alignment sizes */
  813. ovi->output_width &= 0xFFFFFFFC;
  814. ovi->output_height &= 0xFFFFFFFE;
  815. if (!video->graphics) {
  816. int errorcode = obs_init_graphics(ovi);
  817. if (errorcode != OBS_VIDEO_SUCCESS) {
  818. obs_free_graphics();
  819. return errorcode;
  820. }
  821. }
  822. const char *scale_type_name = "";
  823. switch (ovi->scale_type) {
  824. case OBS_SCALE_DISABLE:
  825. scale_type_name = "Disabled";
  826. break;
  827. case OBS_SCALE_POINT:
  828. scale_type_name = "Point";
  829. break;
  830. case OBS_SCALE_BICUBIC:
  831. scale_type_name = "Bicubic";
  832. break;
  833. case OBS_SCALE_BILINEAR:
  834. scale_type_name = "Bilinear";
  835. break;
  836. case OBS_SCALE_LANCZOS:
  837. scale_type_name = "Lanczos";
  838. break;
  839. }
  840. bool yuv = format_is_yuv(ovi->output_format);
  841. const char *yuv_format = get_video_colorspace_name(ovi->colorspace);
  842. const char *yuv_range = get_video_range_name(ovi->range);
  843. blog(LOG_INFO, "---------------------------------");
  844. blog(LOG_INFO, "video settings reset:\n"
  845. "\tbase resolution: %dx%d\n"
  846. "\toutput resolution: %dx%d\n"
  847. "\tdownscale filter: %s\n"
  848. "\tfps: %d/%d\n"
  849. "\tformat: %s\n"
  850. "\tYUV mode: %s%s%s",
  851. ovi->base_width, ovi->base_height,
  852. ovi->output_width, ovi->output_height,
  853. scale_type_name,
  854. ovi->fps_num, ovi->fps_den,
  855. get_video_format_name(ovi->output_format),
  856. yuv ? yuv_format : "None",
  857. yuv ? "/" : "",
  858. yuv ? yuv_range : "");
  859. return obs_init_video(ovi);
  860. }
  861. bool obs_reset_audio(const struct obs_audio_info *oai)
  862. {
  863. struct audio_output_info ai;
  864. if (!obs) return false;
  865. /* don't allow changing of audio settings if active. */
  866. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  867. return false;
  868. obs_free_audio();
  869. if (!oai)
  870. return true;
  871. ai.name = "Audio";
  872. ai.samples_per_sec = oai->samples_per_sec;
  873. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  874. ai.speakers = oai->speakers;
  875. ai.input_callback = audio_callback;
  876. blog(LOG_INFO, "---------------------------------");
  877. blog(LOG_INFO, "audio settings reset:\n"
  878. "\tsamples per sec: %d\n"
  879. "\tspeakers: %d",
  880. (int)ai.samples_per_sec,
  881. (int)ai.speakers);
  882. return obs_init_audio(&ai);
  883. }
  884. bool obs_get_video_info(struct obs_video_info *ovi)
  885. {
  886. struct obs_core_video *video = &obs->video;
  887. if (!obs || !video->graphics)
  888. return false;
  889. *ovi = video->ovi;
  890. return true;
  891. }
  892. bool obs_get_audio_info(struct obs_audio_info *oai)
  893. {
  894. struct obs_core_audio *audio = &obs->audio;
  895. const struct audio_output_info *info;
  896. if (!obs || !oai || !audio->audio)
  897. return false;
  898. info = audio_output_get_info(audio->audio);
  899. oai->samples_per_sec = info->samples_per_sec;
  900. oai->speakers = info->speakers;
  901. return true;
  902. }
  903. bool obs_enum_source_types(size_t idx, const char **id)
  904. {
  905. if (!obs) return false;
  906. if (idx >= obs->source_types.num)
  907. return false;
  908. *id = obs->source_types.array[idx].id;
  909. return true;
  910. }
  911. bool obs_enum_input_types(size_t idx, const char **id)
  912. {
  913. if (!obs) return false;
  914. if (idx >= obs->input_types.num)
  915. return false;
  916. *id = obs->input_types.array[idx].id;
  917. return true;
  918. }
  919. bool obs_enum_filter_types(size_t idx, const char **id)
  920. {
  921. if (!obs) return false;
  922. if (idx >= obs->filter_types.num)
  923. return false;
  924. *id = obs->filter_types.array[idx].id;
  925. return true;
  926. }
  927. bool obs_enum_transition_types(size_t idx, const char **id)
  928. {
  929. if (!obs) return false;
  930. if (idx >= obs->transition_types.num)
  931. return false;
  932. *id = obs->transition_types.array[idx].id;
  933. return true;
  934. }
  935. bool obs_enum_output_types(size_t idx, const char **id)
  936. {
  937. if (!obs) return false;
  938. if (idx >= obs->output_types.num)
  939. return false;
  940. *id = obs->output_types.array[idx].id;
  941. return true;
  942. }
  943. bool obs_enum_encoder_types(size_t idx, const char **id)
  944. {
  945. if (!obs) return false;
  946. if (idx >= obs->encoder_types.num)
  947. return false;
  948. *id = obs->encoder_types.array[idx].id;
  949. return true;
  950. }
  951. bool obs_enum_service_types(size_t idx, const char **id)
  952. {
  953. if (!obs) return false;
  954. if (idx >= obs->service_types.num)
  955. return false;
  956. *id = obs->service_types.array[idx].id;
  957. return true;
  958. }
  959. void obs_enter_graphics(void)
  960. {
  961. if (obs && obs->video.graphics)
  962. gs_enter_context(obs->video.graphics);
  963. }
  964. void obs_leave_graphics(void)
  965. {
  966. if (obs && obs->video.graphics)
  967. gs_leave_context();
  968. }
  969. audio_t *obs_get_audio(void)
  970. {
  971. return (obs != NULL) ? obs->audio.audio : NULL;
  972. }
  973. video_t *obs_get_video(void)
  974. {
  975. return (obs != NULL) ? obs->video.video : NULL;
  976. }
  977. /* TODO: optimize this later so it's not just O(N) string lookups */
  978. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  979. const char *task, const char *target)
  980. {
  981. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  982. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  983. if (strcmp(callback->id, id) == 0 &&
  984. strcmp(callback->task, task) == 0 &&
  985. strcmp(callback->target, target) == 0)
  986. return callback;
  987. }
  988. return NULL;
  989. }
  990. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  991. const char *task, const char *target)
  992. {
  993. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  994. struct obs_modeless_ui *callback;
  995. callback = obs->modeless_ui_callbacks.array+i;
  996. if (strcmp(callback->id, id) == 0 &&
  997. strcmp(callback->task, task) == 0 &&
  998. strcmp(callback->target, target) == 0)
  999. return callback;
  1000. }
  1001. return NULL;
  1002. }
  1003. int obs_exec_ui(const char *name, const char *task, const char *target,
  1004. void *data, void *ui_data)
  1005. {
  1006. struct obs_modal_ui *callback;
  1007. int errorcode = OBS_UI_NOTFOUND;
  1008. if (!obs) return errorcode;
  1009. callback = get_modal_ui_callback(name, task, target);
  1010. if (callback) {
  1011. bool success = callback->exec(data, ui_data);
  1012. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  1013. }
  1014. return errorcode;
  1015. }
  1016. void *obs_create_ui(const char *name, const char *task, const char *target,
  1017. void *data, void *ui_data)
  1018. {
  1019. struct obs_modeless_ui *callback;
  1020. if (!obs) return NULL;
  1021. callback = get_modeless_ui_callback(name, task, target);
  1022. return callback ? callback->create(data, ui_data) : NULL;
  1023. }
  1024. obs_source_t *obs_get_output_source(uint32_t channel)
  1025. {
  1026. if (!obs) return NULL;
  1027. return obs_view_get_source(&obs->data.main_view, channel);
  1028. }
  1029. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  1030. {
  1031. assert(channel < MAX_CHANNELS);
  1032. if (!obs) return;
  1033. if (channel >= MAX_CHANNELS) return;
  1034. struct obs_source *prev_source;
  1035. struct obs_view *view = &obs->data.main_view;
  1036. struct calldata params = {0};
  1037. pthread_mutex_lock(&view->channels_mutex);
  1038. obs_source_addref(source);
  1039. prev_source = view->channels[channel];
  1040. calldata_set_int(&params, "channel", channel);
  1041. calldata_set_ptr(&params, "prev_source", prev_source);
  1042. calldata_set_ptr(&params, "source", source);
  1043. signal_handler_signal(obs->signals, "channel_change", &params);
  1044. calldata_get_ptr(&params, "source", &source);
  1045. calldata_free(&params);
  1046. view->channels[channel] = source;
  1047. pthread_mutex_unlock(&view->channels_mutex);
  1048. if (source)
  1049. obs_source_activate(source, MAIN_VIEW);
  1050. if (prev_source) {
  1051. obs_source_deactivate(prev_source, MAIN_VIEW);
  1052. obs_source_release(prev_source);
  1053. }
  1054. }
  1055. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  1056. {
  1057. obs_source_t *source;
  1058. if (!obs) return;
  1059. pthread_mutex_lock(&obs->data.sources_mutex);
  1060. source = obs->data.first_source;
  1061. while (source) {
  1062. obs_source_t *next_source =
  1063. (obs_source_t*)source->context.next;
  1064. if (source->info.id == group_info.id &&
  1065. !enum_proc(param, source)) {
  1066. break;
  1067. } else if (source->info.type == OBS_SOURCE_TYPE_INPUT &&
  1068. !source->context.private &&
  1069. !enum_proc(param, source)) {
  1070. break;
  1071. }
  1072. source = next_source;
  1073. }
  1074. pthread_mutex_unlock(&obs->data.sources_mutex);
  1075. }
  1076. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  1077. void *param)
  1078. {
  1079. struct obs_context_data **start = pstart, *context;
  1080. bool (*enum_proc)(void*, void*) = proc;
  1081. assert(start);
  1082. assert(mutex);
  1083. assert(enum_proc);
  1084. pthread_mutex_lock(mutex);
  1085. context = *start;
  1086. while (context) {
  1087. if (!enum_proc(param, context))
  1088. break;
  1089. context = context->next;
  1090. }
  1091. pthread_mutex_unlock(mutex);
  1092. }
  1093. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  1094. {
  1095. if (!obs) return;
  1096. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  1097. enum_proc, param);
  1098. }
  1099. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  1100. {
  1101. if (!obs) return;
  1102. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  1103. enum_proc, param);
  1104. }
  1105. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  1106. {
  1107. if (!obs) return;
  1108. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  1109. enum_proc, param);
  1110. }
  1111. static inline void *get_context_by_name(void *vfirst, const char *name,
  1112. pthread_mutex_t *mutex, void *(*addref)(void*))
  1113. {
  1114. struct obs_context_data **first = vfirst;
  1115. struct obs_context_data *context;
  1116. pthread_mutex_lock(mutex);
  1117. context = *first;
  1118. while (context) {
  1119. if (!context->private && strcmp(context->name, name) == 0) {
  1120. context = addref(context);
  1121. break;
  1122. }
  1123. context = context->next;
  1124. }
  1125. pthread_mutex_unlock(mutex);
  1126. return context;
  1127. }
  1128. static inline void *obs_source_addref_safe_(void *ref)
  1129. {
  1130. return obs_source_get_ref(ref);
  1131. }
  1132. static inline void *obs_output_addref_safe_(void *ref)
  1133. {
  1134. return obs_output_get_ref(ref);
  1135. }
  1136. static inline void *obs_encoder_addref_safe_(void *ref)
  1137. {
  1138. return obs_encoder_get_ref(ref);
  1139. }
  1140. static inline void *obs_service_addref_safe_(void *ref)
  1141. {
  1142. return obs_service_get_ref(ref);
  1143. }
  1144. static inline void *obs_id_(void *data)
  1145. {
  1146. return data;
  1147. }
  1148. obs_source_t *obs_get_source_by_name(const char *name)
  1149. {
  1150. if (!obs) return NULL;
  1151. return get_context_by_name(&obs->data.first_source, name,
  1152. &obs->data.sources_mutex, obs_source_addref_safe_);
  1153. }
  1154. obs_output_t *obs_get_output_by_name(const char *name)
  1155. {
  1156. if (!obs) return NULL;
  1157. return get_context_by_name(&obs->data.first_output, name,
  1158. &obs->data.outputs_mutex, obs_output_addref_safe_);
  1159. }
  1160. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1161. {
  1162. if (!obs) return NULL;
  1163. return get_context_by_name(&obs->data.first_encoder, name,
  1164. &obs->data.encoders_mutex, obs_encoder_addref_safe_);
  1165. }
  1166. obs_service_t *obs_get_service_by_name(const char *name)
  1167. {
  1168. if (!obs) return NULL;
  1169. return get_context_by_name(&obs->data.first_service, name,
  1170. &obs->data.services_mutex, obs_service_addref_safe_);
  1171. }
  1172. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1173. {
  1174. if (!obs) return NULL;
  1175. switch (effect) {
  1176. case OBS_EFFECT_DEFAULT:
  1177. return obs->video.default_effect;
  1178. case OBS_EFFECT_DEFAULT_RECT:
  1179. return obs->video.default_rect_effect;
  1180. case OBS_EFFECT_OPAQUE:
  1181. return obs->video.opaque_effect;
  1182. case OBS_EFFECT_SOLID:
  1183. return obs->video.solid_effect;
  1184. case OBS_EFFECT_BICUBIC:
  1185. return obs->video.bicubic_effect;
  1186. case OBS_EFFECT_LANCZOS:
  1187. return obs->video.lanczos_effect;
  1188. case OBS_EFFECT_BILINEAR_LOWRES:
  1189. return obs->video.bilinear_lowres_effect;
  1190. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1191. return obs->video.premultiplied_alpha_effect;
  1192. }
  1193. return NULL;
  1194. }
  1195. /* DEPRECATED */
  1196. gs_effect_t *obs_get_default_rect_effect(void)
  1197. {
  1198. if (!obs) return NULL;
  1199. return obs->video.default_rect_effect;
  1200. }
  1201. signal_handler_t *obs_get_signal_handler(void)
  1202. {
  1203. if (!obs) return NULL;
  1204. return obs->signals;
  1205. }
  1206. proc_handler_t *obs_get_proc_handler(void)
  1207. {
  1208. if (!obs) return NULL;
  1209. return obs->procs;
  1210. }
  1211. void obs_render_main_view(void)
  1212. {
  1213. if (!obs) return;
  1214. obs_view_render(&obs->data.main_view);
  1215. }
  1216. void obs_render_main_texture(void)
  1217. {
  1218. struct obs_core_video *video = &obs->video;
  1219. gs_texture_t *tex;
  1220. gs_effect_t *effect;
  1221. gs_eparam_t *param;
  1222. int last_tex;
  1223. if (!obs) return;
  1224. last_tex = video->cur_texture == 0
  1225. ? NUM_TEXTURES - 1
  1226. : video->cur_texture - 1;
  1227. if (!video->textures_rendered[last_tex])
  1228. return;
  1229. tex = video->render_textures[last_tex];
  1230. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1231. param = gs_effect_get_param_by_name(effect, "image");
  1232. gs_effect_set_texture(param, tex);
  1233. while (gs_effect_loop(effect, "Draw"))
  1234. gs_draw_sprite(tex, 0, 0, 0);
  1235. }
  1236. gs_texture_t *obs_get_main_texture(void)
  1237. {
  1238. struct obs_core_video *video = &obs->video;
  1239. int last_tex;
  1240. if (!obs) return NULL;
  1241. last_tex = video->cur_texture == 0
  1242. ? NUM_TEXTURES - 1
  1243. : video->cur_texture - 1;
  1244. if (!video->textures_rendered[last_tex])
  1245. return NULL;
  1246. return video->render_textures[last_tex];
  1247. }
  1248. void obs_set_master_volume(float volume)
  1249. {
  1250. struct calldata data = {0};
  1251. if (!obs) return;
  1252. calldata_set_float(&data, "volume", volume);
  1253. signal_handler_signal(obs->signals, "master_volume", &data);
  1254. volume = (float)calldata_float(&data, "volume");
  1255. calldata_free(&data);
  1256. obs->audio.user_volume = volume;
  1257. }
  1258. float obs_get_master_volume(void)
  1259. {
  1260. return obs ? obs->audio.user_volume : 0.0f;
  1261. }
  1262. static obs_source_t *obs_load_source_type(obs_data_t *source_data)
  1263. {
  1264. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1265. obs_source_t *source;
  1266. const char *name = obs_data_get_string(source_data, "name");
  1267. const char *id = obs_data_get_string(source_data, "id");
  1268. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1269. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1270. double volume;
  1271. double balance;
  1272. int64_t sync;
  1273. uint32_t flags;
  1274. uint32_t mixers;
  1275. int di_order;
  1276. int di_mode;
  1277. int monitoring_type;
  1278. source = obs_source_create(id, name, settings, hotkeys);
  1279. obs_data_release(hotkeys);
  1280. obs_data_set_default_double(source_data, "volume", 1.0);
  1281. volume = obs_data_get_double(source_data, "volume");
  1282. obs_source_set_volume(source, (float)volume);
  1283. obs_data_set_default_double(source_data, "balance", 0.5);
  1284. balance = obs_data_get_double(source_data, "balance");
  1285. obs_source_set_balance_value(source, (float)balance);
  1286. sync = obs_data_get_int(source_data, "sync");
  1287. obs_source_set_sync_offset(source, sync);
  1288. obs_data_set_default_int(source_data, "mixers", 0xF);
  1289. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1290. obs_source_set_audio_mixers(source, mixers);
  1291. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1292. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1293. obs_source_set_flags(source, flags);
  1294. obs_data_set_default_bool(source_data, "enabled", true);
  1295. obs_source_set_enabled(source,
  1296. obs_data_get_bool(source_data, "enabled"));
  1297. obs_data_set_default_bool(source_data, "muted", false);
  1298. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1299. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1300. obs_source_enable_push_to_mute(source,
  1301. obs_data_get_bool(source_data, "push-to-mute"));
  1302. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1303. obs_source_set_push_to_mute_delay(source,
  1304. obs_data_get_int(source_data, "push-to-mute-delay"));
  1305. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1306. obs_source_enable_push_to_talk(source,
  1307. obs_data_get_bool(source_data, "push-to-talk"));
  1308. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1309. obs_source_set_push_to_talk_delay(source,
  1310. obs_data_get_int(source_data, "push-to-talk-delay"));
  1311. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1312. obs_source_set_deinterlace_mode(source,
  1313. (enum obs_deinterlace_mode)di_mode);
  1314. di_order = (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1315. obs_source_set_deinterlace_field_order(source,
  1316. (enum obs_deinterlace_field_order)di_order);
  1317. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1318. obs_source_set_monitoring_type(source,
  1319. (enum obs_monitoring_type)monitoring_type);
  1320. obs_data_release(source->private_settings);
  1321. source->private_settings =
  1322. obs_data_get_obj(source_data, "private_settings");
  1323. if (!source->private_settings)
  1324. source->private_settings = obs_data_create();
  1325. if (filters) {
  1326. size_t count = obs_data_array_count(filters);
  1327. for (size_t i = 0; i < count; i++) {
  1328. obs_data_t *filter_data =
  1329. obs_data_array_item(filters, i);
  1330. obs_source_t *filter = obs_load_source_type(
  1331. filter_data);
  1332. if (filter) {
  1333. obs_source_filter_add(source, filter);
  1334. obs_source_release(filter);
  1335. }
  1336. obs_data_release(filter_data);
  1337. }
  1338. obs_data_array_release(filters);
  1339. }
  1340. obs_data_release(settings);
  1341. return source;
  1342. }
  1343. obs_source_t *obs_load_source(obs_data_t *source_data)
  1344. {
  1345. return obs_load_source_type(source_data);
  1346. }
  1347. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1348. void *private_data)
  1349. {
  1350. if (!obs) return;
  1351. struct obs_core_data *data = &obs->data;
  1352. DARRAY(obs_source_t*) sources;
  1353. size_t count;
  1354. size_t i;
  1355. da_init(sources);
  1356. count = obs_data_array_count(array);
  1357. da_reserve(sources, count);
  1358. pthread_mutex_lock(&data->sources_mutex);
  1359. for (i = 0; i < count; i++) {
  1360. obs_data_t *source_data = obs_data_array_item(array, i);
  1361. obs_source_t *source = obs_load_source(source_data);
  1362. da_push_back(sources, &source);
  1363. obs_data_release(source_data);
  1364. }
  1365. /* tell sources that we want to load */
  1366. for (i = 0; i < sources.num; i++) {
  1367. obs_source_t *source = sources.array[i];
  1368. obs_data_t *source_data = obs_data_array_item(array, i);
  1369. if (source) {
  1370. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1371. obs_transition_load(source, source_data);
  1372. obs_source_load(source);
  1373. if (cb)
  1374. cb(private_data, source);
  1375. }
  1376. obs_data_release(source_data);
  1377. }
  1378. for (i = 0; i < sources.num; i++)
  1379. obs_source_release(sources.array[i]);
  1380. pthread_mutex_unlock(&data->sources_mutex);
  1381. da_free(sources);
  1382. }
  1383. obs_data_t *obs_save_source(obs_source_t *source)
  1384. {
  1385. obs_data_array_t *filters = obs_data_array_create();
  1386. obs_data_t *source_data = obs_data_create();
  1387. obs_data_t *settings = obs_source_get_settings(source);
  1388. obs_data_t *hotkey_data = source->context.hotkey_data;
  1389. obs_data_t *hotkeys;
  1390. float volume = obs_source_get_volume(source);
  1391. float balance = obs_source_get_balance_value(source);
  1392. uint32_t mixers = obs_source_get_audio_mixers(source);
  1393. int64_t sync = obs_source_get_sync_offset(source);
  1394. uint32_t flags = obs_source_get_flags(source);
  1395. const char *name = obs_source_get_name(source);
  1396. const char *id = obs_source_get_id(source);
  1397. bool enabled = obs_source_enabled(source);
  1398. bool muted = obs_source_muted(source);
  1399. bool push_to_mute= obs_source_push_to_mute_enabled(source);
  1400. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1401. bool push_to_talk= obs_source_push_to_talk_enabled(source);
  1402. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1403. int m_type = (int)obs_source_get_monitoring_type(source);
  1404. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1405. int di_order =
  1406. (int)obs_source_get_deinterlace_field_order(source);
  1407. obs_source_save(source);
  1408. hotkeys = obs_hotkeys_save_source(source);
  1409. if (hotkeys) {
  1410. obs_data_release(hotkey_data);
  1411. source->context.hotkey_data = hotkeys;
  1412. hotkey_data = hotkeys;
  1413. }
  1414. obs_data_set_string(source_data, "name", name);
  1415. obs_data_set_string(source_data, "id", id);
  1416. obs_data_set_obj (source_data, "settings", settings);
  1417. obs_data_set_int (source_data, "mixers", mixers);
  1418. obs_data_set_int (source_data, "sync", sync);
  1419. obs_data_set_int (source_data, "flags", flags);
  1420. obs_data_set_double(source_data, "volume", volume);
  1421. obs_data_set_double(source_data, "balance", balance);
  1422. obs_data_set_bool (source_data, "enabled", enabled);
  1423. obs_data_set_bool (source_data, "muted", muted);
  1424. obs_data_set_bool (source_data, "push-to-mute", push_to_mute);
  1425. obs_data_set_int (source_data, "push-to-mute-delay", ptm_delay);
  1426. obs_data_set_bool (source_data, "push-to-talk", push_to_talk);
  1427. obs_data_set_int (source_data, "push-to-talk-delay", ptt_delay);
  1428. obs_data_set_obj (source_data, "hotkeys", hotkey_data);
  1429. obs_data_set_int (source_data, "deinterlace_mode", di_mode);
  1430. obs_data_set_int (source_data, "deinterlace_field_order", di_order);
  1431. obs_data_set_int (source_data, "monitoring_type", m_type);
  1432. obs_data_set_obj(source_data, "private_settings",
  1433. source->private_settings);
  1434. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1435. obs_transition_save(source, source_data);
  1436. pthread_mutex_lock(&source->filter_mutex);
  1437. if (source->filters.num) {
  1438. for (size_t i = source->filters.num; i > 0; i--) {
  1439. obs_source_t *filter = source->filters.array[i - 1];
  1440. obs_data_t *filter_data = obs_save_source(filter);
  1441. obs_data_array_push_back(filters, filter_data);
  1442. obs_data_release(filter_data);
  1443. }
  1444. obs_data_set_array(source_data, "filters", filters);
  1445. }
  1446. pthread_mutex_unlock(&source->filter_mutex);
  1447. obs_data_release(settings);
  1448. obs_data_array_release(filters);
  1449. return source_data;
  1450. }
  1451. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1452. void *data_)
  1453. {
  1454. if (!obs) return NULL;
  1455. struct obs_core_data *data = &obs->data;
  1456. obs_data_array_t *array;
  1457. obs_source_t *source;
  1458. array = obs_data_array_create();
  1459. pthread_mutex_lock(&data->sources_mutex);
  1460. source = data->first_source;
  1461. while (source) {
  1462. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1463. !source->context.private && cb(data_, source)) {
  1464. obs_data_t *source_data = obs_save_source(source);
  1465. obs_data_array_push_back(array, source_data);
  1466. obs_data_release(source_data);
  1467. }
  1468. source = (obs_source_t*)source->context.next;
  1469. }
  1470. pthread_mutex_unlock(&data->sources_mutex);
  1471. return array;
  1472. }
  1473. static bool save_source_filter(void *data, obs_source_t *source)
  1474. {
  1475. UNUSED_PARAMETER(data);
  1476. UNUSED_PARAMETER(source);
  1477. return true;
  1478. }
  1479. obs_data_array_t *obs_save_sources(void)
  1480. {
  1481. return obs_save_sources_filtered(save_source_filter, NULL);
  1482. }
  1483. /* ensures that names are never blank */
  1484. static inline char *dup_name(const char *name, bool private)
  1485. {
  1486. if (private && !name)
  1487. return NULL;
  1488. if (!name || !*name) {
  1489. struct dstr unnamed = {0};
  1490. dstr_printf(&unnamed, "__unnamed%04lld",
  1491. obs->data.unnamed_index++);
  1492. return unnamed.array;
  1493. } else {
  1494. return bstrdup(name);
  1495. }
  1496. }
  1497. static inline bool obs_context_data_init_wrap(
  1498. struct obs_context_data *context,
  1499. enum obs_obj_type type,
  1500. obs_data_t *settings,
  1501. const char *name,
  1502. obs_data_t *hotkey_data,
  1503. bool private)
  1504. {
  1505. assert(context);
  1506. memset(context, 0, sizeof(*context));
  1507. context->private = private;
  1508. context->type = type;
  1509. pthread_mutex_init_value(&context->rename_cache_mutex);
  1510. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1511. return false;
  1512. context->signals = signal_handler_create();
  1513. if (!context->signals)
  1514. return false;
  1515. context->procs = proc_handler_create();
  1516. if (!context->procs)
  1517. return false;
  1518. context->name = dup_name(name, private);
  1519. context->settings = obs_data_newref(settings);
  1520. context->hotkey_data = obs_data_newref(hotkey_data);
  1521. return true;
  1522. }
  1523. bool obs_context_data_init(
  1524. struct obs_context_data *context,
  1525. enum obs_obj_type type,
  1526. obs_data_t *settings,
  1527. const char *name,
  1528. obs_data_t *hotkey_data,
  1529. bool private)
  1530. {
  1531. if (obs_context_data_init_wrap(context, type, settings, name,
  1532. hotkey_data, private)) {
  1533. return true;
  1534. } else {
  1535. obs_context_data_free(context);
  1536. return false;
  1537. }
  1538. }
  1539. void obs_context_data_free(struct obs_context_data *context)
  1540. {
  1541. obs_hotkeys_context_release(context);
  1542. signal_handler_destroy(context->signals);
  1543. proc_handler_destroy(context->procs);
  1544. obs_data_release(context->settings);
  1545. obs_context_data_remove(context);
  1546. pthread_mutex_destroy(&context->rename_cache_mutex);
  1547. bfree(context->name);
  1548. for (size_t i = 0; i < context->rename_cache.num; i++)
  1549. bfree(context->rename_cache.array[i]);
  1550. da_free(context->rename_cache);
  1551. memset(context, 0, sizeof(*context));
  1552. }
  1553. void obs_context_data_insert(struct obs_context_data *context,
  1554. pthread_mutex_t *mutex, void *pfirst)
  1555. {
  1556. struct obs_context_data **first = pfirst;
  1557. assert(context);
  1558. assert(mutex);
  1559. assert(first);
  1560. context->mutex = mutex;
  1561. pthread_mutex_lock(mutex);
  1562. context->prev_next = first;
  1563. context->next = *first;
  1564. *first = context;
  1565. if (context->next)
  1566. context->next->prev_next = &context->next;
  1567. pthread_mutex_unlock(mutex);
  1568. }
  1569. void obs_context_data_remove(struct obs_context_data *context)
  1570. {
  1571. if (context && context->mutex) {
  1572. pthread_mutex_lock(context->mutex);
  1573. if (context->prev_next)
  1574. *context->prev_next = context->next;
  1575. if (context->next)
  1576. context->next->prev_next = context->prev_next;
  1577. pthread_mutex_unlock(context->mutex);
  1578. context->mutex = NULL;
  1579. }
  1580. }
  1581. void obs_context_data_setname(struct obs_context_data *context,
  1582. const char *name)
  1583. {
  1584. pthread_mutex_lock(&context->rename_cache_mutex);
  1585. if (context->name)
  1586. da_push_back(context->rename_cache, &context->name);
  1587. context->name = dup_name(name, context->private);
  1588. pthread_mutex_unlock(&context->rename_cache_mutex);
  1589. }
  1590. profiler_name_store_t *obs_get_profiler_name_store(void)
  1591. {
  1592. if (!obs)
  1593. return NULL;
  1594. return obs->name_store;
  1595. }
  1596. uint64_t obs_get_video_frame_time(void)
  1597. {
  1598. return obs ? obs->video.video_time : 0;
  1599. }
  1600. double obs_get_active_fps(void)
  1601. {
  1602. return obs ? obs->video.video_fps : 0.0;
  1603. }
  1604. uint64_t obs_get_average_frame_time_ns(void)
  1605. {
  1606. return obs ? obs->video.video_avg_frame_time_ns : 0;
  1607. }
  1608. enum obs_obj_type obs_obj_get_type(void *obj)
  1609. {
  1610. struct obs_context_data *context = obj;
  1611. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  1612. }
  1613. const char *obs_obj_get_id(void *obj)
  1614. {
  1615. struct obs_context_data *context = obj;
  1616. if (!context)
  1617. return NULL;
  1618. switch (context->type) {
  1619. case OBS_OBJ_TYPE_SOURCE: return ((obs_source_t*)obj)->info.id;
  1620. case OBS_OBJ_TYPE_OUTPUT: return ((obs_output_t*)obj)->info.id;
  1621. case OBS_OBJ_TYPE_ENCODER: return ((obs_encoder_t*)obj)->info.id;
  1622. case OBS_OBJ_TYPE_SERVICE: return ((obs_service_t*)obj)->info.id;
  1623. default:;
  1624. }
  1625. return NULL;
  1626. }
  1627. bool obs_obj_invalid(void *obj)
  1628. {
  1629. struct obs_context_data *context = obj;
  1630. if (!context)
  1631. return true;
  1632. return !context->data;
  1633. }
  1634. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  1635. {
  1636. if (!obs || !name || !id || !*name || !*id)
  1637. return false;
  1638. #if defined(_WIN32) || HAVE_PULSEAUDIO || defined(__APPLE__)
  1639. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  1640. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  1641. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1642. return true;
  1643. }
  1644. if (obs->audio.monitoring_device_name)
  1645. bfree(obs->audio.monitoring_device_name);
  1646. if (obs->audio.monitoring_device_id)
  1647. bfree(obs->audio.monitoring_device_id);
  1648. obs->audio.monitoring_device_name = bstrdup(name);
  1649. obs->audio.monitoring_device_id = bstrdup(id);
  1650. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  1651. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  1652. audio_monitor_reset(monitor);
  1653. }
  1654. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1655. return true;
  1656. #else
  1657. return false;
  1658. #endif
  1659. }
  1660. void obs_get_audio_monitoring_device(const char **name, const char **id)
  1661. {
  1662. if (!obs)
  1663. return;
  1664. if (name)
  1665. *name = obs->audio.monitoring_device_name;
  1666. if (id)
  1667. *id = obs->audio.monitoring_device_id;
  1668. }
  1669. void obs_add_tick_callback(
  1670. void (*tick)(void *param, float seconds),
  1671. void *param)
  1672. {
  1673. if (!obs)
  1674. return;
  1675. struct tick_callback data = {tick, param};
  1676. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1677. da_insert(obs->data.tick_callbacks, 0, &data);
  1678. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1679. }
  1680. void obs_remove_tick_callback(
  1681. void (*tick)(void *param, float seconds),
  1682. void *param)
  1683. {
  1684. if (!obs)
  1685. return;
  1686. struct tick_callback data = {tick, param};
  1687. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1688. da_erase_item(obs->data.tick_callbacks, &data);
  1689. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1690. }
  1691. void obs_add_main_render_callback(
  1692. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1693. void *param)
  1694. {
  1695. if (!obs)
  1696. return;
  1697. struct draw_callback data = {draw, param};
  1698. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1699. da_insert(obs->data.draw_callbacks, 0, &data);
  1700. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1701. }
  1702. void obs_remove_main_render_callback(
  1703. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1704. void *param)
  1705. {
  1706. if (!obs)
  1707. return;
  1708. struct draw_callback data = {draw, param};
  1709. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1710. da_erase_item(obs->data.draw_callbacks, &data);
  1711. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1712. }
  1713. uint32_t obs_get_total_frames(void)
  1714. {
  1715. return obs ? obs->video.total_frames : 0;
  1716. }
  1717. uint32_t obs_get_lagged_frames(void)
  1718. {
  1719. return obs ? obs->video.lagged_frames : 0;
  1720. }
  1721. void start_raw_video(video_t *v, const struct video_scale_info *conversion,
  1722. void (*callback)(void *param, struct video_data *frame),
  1723. void *param)
  1724. {
  1725. struct obs_core_video *video = &obs->video;
  1726. os_atomic_inc_long(&video->raw_active);
  1727. video_output_connect(v, conversion, callback, param);
  1728. }
  1729. void stop_raw_video(video_t *v,
  1730. void (*callback)(void *param, struct video_data *frame),
  1731. void *param)
  1732. {
  1733. struct obs_core_video *video = &obs->video;
  1734. os_atomic_dec_long(&video->raw_active);
  1735. video_output_disconnect(v, callback, param);
  1736. }
  1737. void obs_add_raw_video_callback(
  1738. const struct video_scale_info *conversion,
  1739. void (*callback)(void *param, struct video_data *frame),
  1740. void *param)
  1741. {
  1742. struct obs_core_video *video = &obs->video;
  1743. if (!obs)
  1744. return;
  1745. start_raw_video(video->video, conversion, callback, param);
  1746. }
  1747. void obs_remove_raw_video_callback(
  1748. void (*callback)(void *param, struct video_data *frame),
  1749. void *param)
  1750. {
  1751. struct obs_core_video *video = &obs->video;
  1752. if (!obs)
  1753. return;
  1754. stop_raw_video(video->video, callback, param);
  1755. }
  1756. void obs_apply_private_data(obs_data_t *settings)
  1757. {
  1758. if (!obs || !settings)
  1759. return;
  1760. obs_data_apply(obs->data.private_data, settings);
  1761. }
  1762. void obs_set_private_data(obs_data_t *settings)
  1763. {
  1764. if (!obs)
  1765. return;
  1766. obs_data_clear(obs->data.private_data);
  1767. if (settings)
  1768. obs_data_apply(obs->data.private_data, settings);
  1769. }
  1770. obs_data_t *obs_get_private_data(void)
  1771. {
  1772. if (!obs)
  1773. return NULL;
  1774. obs_data_t *private_data = obs->data.private_data;
  1775. obs_data_addref(private_data);
  1776. return private_data;
  1777. }