obs.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493
  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. static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
  36. {
  37. struct obs_core_video *video = &obs->video;
  38. video->conversion_needed = false;
  39. video->conversion_techs[0] = NULL;
  40. video->conversion_techs[1] = NULL;
  41. video->conversion_techs[2] = NULL;
  42. video->conversion_width_i = 0.f;
  43. switch ((uint32_t)ovi->output_format) {
  44. case VIDEO_FORMAT_I420:
  45. video->conversion_needed = true;
  46. video->conversion_techs[0] = "Planar_Y";
  47. video->conversion_techs[1] = "Planar_U_Left";
  48. video->conversion_techs[2] = "Planar_V_Left";
  49. video->conversion_width_i = 1.f / (float)ovi->output_width;
  50. break;
  51. case VIDEO_FORMAT_NV12:
  52. video->conversion_needed = true;
  53. video->conversion_techs[0] = "NV12_Y";
  54. video->conversion_techs[1] = "NV12_UV";
  55. video->conversion_width_i = 1.f / (float)ovi->output_width;
  56. break;
  57. case VIDEO_FORMAT_I444:
  58. video->conversion_needed = true;
  59. video->conversion_techs[0] = "Planar_Y";
  60. video->conversion_techs[1] = "Planar_U";
  61. video->conversion_techs[2] = "Planar_V";
  62. break;
  63. }
  64. }
  65. static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
  66. {
  67. struct obs_core_video *video = &obs->video;
  68. calc_gpu_conversion_sizes(ovi);
  69. video->using_nv12_tex = ovi->output_format == VIDEO_FORMAT_NV12
  70. ? gs_nv12_available()
  71. : false;
  72. if (!video->conversion_needed) {
  73. blog(LOG_INFO, "GPU conversion not available for format: %u",
  74. (unsigned int)ovi->output_format);
  75. video->gpu_conversion = false;
  76. video->using_nv12_tex = false;
  77. blog(LOG_INFO, "NV12 texture support not available");
  78. return true;
  79. }
  80. if (video->using_nv12_tex)
  81. blog(LOG_INFO, "NV12 texture support enabled");
  82. else
  83. blog(LOG_INFO, "NV12 texture support not available");
  84. #ifdef _WIN32
  85. if (video->using_nv12_tex) {
  86. gs_texture_create_nv12(&video->convert_textures[0],
  87. &video->convert_textures[1],
  88. ovi->output_width, ovi->output_height,
  89. GS_RENDER_TARGET | GS_SHARED_KM_TEX);
  90. } else {
  91. #endif
  92. video->convert_textures[0] =
  93. gs_texture_create(ovi->output_width, ovi->output_height,
  94. GS_R8, 1, NULL, GS_RENDER_TARGET);
  95. const struct video_output_info *info =
  96. video_output_get_info(video->video);
  97. switch (info->format) {
  98. case VIDEO_FORMAT_I420:
  99. video->convert_textures[1] = gs_texture_create(
  100. ovi->output_width / 2, ovi->output_height / 2,
  101. GS_R8, 1, NULL, GS_RENDER_TARGET);
  102. video->convert_textures[2] = gs_texture_create(
  103. ovi->output_width / 2, ovi->output_height / 2,
  104. GS_R8, 1, NULL, GS_RENDER_TARGET);
  105. if (!video->convert_textures[2])
  106. return false;
  107. break;
  108. case VIDEO_FORMAT_NV12:
  109. video->convert_textures[1] = gs_texture_create(
  110. ovi->output_width / 2, ovi->output_height / 2,
  111. GS_R8G8, 1, NULL, GS_RENDER_TARGET);
  112. break;
  113. case VIDEO_FORMAT_I444:
  114. video->convert_textures[1] = gs_texture_create(
  115. ovi->output_width, ovi->output_height, GS_R8, 1,
  116. NULL, GS_RENDER_TARGET);
  117. video->convert_textures[2] = gs_texture_create(
  118. ovi->output_width, ovi->output_height, GS_R8, 1,
  119. NULL, GS_RENDER_TARGET);
  120. if (!video->convert_textures[2])
  121. return false;
  122. break;
  123. }
  124. #ifdef _WIN32
  125. }
  126. #endif
  127. if (!video->convert_textures[0])
  128. return false;
  129. if (!video->convert_textures[1])
  130. return false;
  131. return true;
  132. }
  133. static bool obs_init_gpu_copy_surfaces(struct obs_video_info *ovi, size_t i)
  134. {
  135. struct obs_core_video *video = &obs->video;
  136. video->copy_surfaces[i][0] = gs_stagesurface_create(
  137. ovi->output_width, ovi->output_height, GS_R8);
  138. if (!video->copy_surfaces[i][0])
  139. return false;
  140. const struct video_output_info *info =
  141. video_output_get_info(video->video);
  142. switch (info->format) {
  143. case VIDEO_FORMAT_I420:
  144. video->copy_surfaces[i][1] = gs_stagesurface_create(
  145. ovi->output_width / 2, ovi->output_height / 2, GS_R8);
  146. if (!video->copy_surfaces[i][1])
  147. return false;
  148. video->copy_surfaces[i][2] = gs_stagesurface_create(
  149. ovi->output_width / 2, ovi->output_height / 2, GS_R8);
  150. if (!video->copy_surfaces[i][2])
  151. return false;
  152. break;
  153. case VIDEO_FORMAT_NV12:
  154. video->copy_surfaces[i][1] = gs_stagesurface_create(
  155. ovi->output_width / 2, ovi->output_height / 2, GS_R8G8);
  156. if (!video->copy_surfaces[i][1])
  157. return false;
  158. break;
  159. case VIDEO_FORMAT_I444:
  160. video->copy_surfaces[i][1] = gs_stagesurface_create(
  161. ovi->output_width, ovi->output_height, GS_R8);
  162. if (!video->copy_surfaces[i][1])
  163. return false;
  164. video->copy_surfaces[i][2] = gs_stagesurface_create(
  165. ovi->output_width, ovi->output_height, GS_R8);
  166. if (!video->copy_surfaces[i][2])
  167. return false;
  168. break;
  169. }
  170. return true;
  171. }
  172. static bool obs_init_textures(struct obs_video_info *ovi)
  173. {
  174. struct obs_core_video *video = &obs->video;
  175. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  176. #ifdef _WIN32
  177. if (video->using_nv12_tex) {
  178. video->copy_surfaces[i][0] =
  179. gs_stagesurface_create_nv12(ovi->output_width,
  180. ovi->output_height);
  181. if (!video->copy_surfaces[i][0])
  182. return false;
  183. } else {
  184. #endif
  185. if (video->gpu_conversion) {
  186. if (!obs_init_gpu_copy_surfaces(ovi, i))
  187. return false;
  188. } else {
  189. video->copy_surfaces[i][0] =
  190. gs_stagesurface_create(
  191. ovi->output_width,
  192. ovi->output_height, GS_RGBA);
  193. if (!video->copy_surfaces[i][0])
  194. return false;
  195. }
  196. #ifdef _WIN32
  197. }
  198. #endif
  199. }
  200. video->render_texture = gs_texture_create(ovi->base_width,
  201. ovi->base_height, GS_RGBA, 1,
  202. NULL, GS_RENDER_TARGET);
  203. if (!video->render_texture)
  204. return false;
  205. video->output_texture = gs_texture_create(ovi->output_width,
  206. ovi->output_height, GS_RGBA,
  207. 1, NULL, GS_RENDER_TARGET);
  208. if (!video->output_texture)
  209. return false;
  210. return true;
  211. }
  212. gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file)
  213. {
  214. if (!*effect) {
  215. char *filename = obs_find_data_file(file);
  216. *effect = gs_effect_create_from_file(filename, NULL);
  217. bfree(filename);
  218. }
  219. return *effect;
  220. }
  221. static int obs_init_graphics(struct obs_video_info *ovi)
  222. {
  223. struct obs_core_video *video = &obs->video;
  224. uint8_t transparent_tex_data[2 * 2 * 4] = {0};
  225. const uint8_t *transparent_tex = transparent_tex_data;
  226. struct gs_sampler_info point_sampler = {0};
  227. bool success = true;
  228. int errorcode;
  229. errorcode =
  230. gs_create(&video->graphics, ovi->graphics_module, ovi->adapter);
  231. if (errorcode != GS_SUCCESS) {
  232. switch (errorcode) {
  233. case GS_ERROR_MODULE_NOT_FOUND:
  234. return OBS_VIDEO_MODULE_NOT_FOUND;
  235. case GS_ERROR_NOT_SUPPORTED:
  236. return OBS_VIDEO_NOT_SUPPORTED;
  237. default:
  238. return OBS_VIDEO_FAIL;
  239. }
  240. }
  241. gs_enter_context(video->graphics);
  242. char *filename = obs_find_data_file("default.effect");
  243. video->default_effect = gs_effect_create_from_file(filename, NULL);
  244. bfree(filename);
  245. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  246. filename = obs_find_data_file("default_rect.effect");
  247. video->default_rect_effect =
  248. gs_effect_create_from_file(filename, NULL);
  249. bfree(filename);
  250. }
  251. filename = obs_find_data_file("opaque.effect");
  252. video->opaque_effect = gs_effect_create_from_file(filename, NULL);
  253. bfree(filename);
  254. filename = obs_find_data_file("solid.effect");
  255. video->solid_effect = gs_effect_create_from_file(filename, NULL);
  256. bfree(filename);
  257. filename = obs_find_data_file("repeat.effect");
  258. video->repeat_effect = gs_effect_create_from_file(filename, NULL);
  259. bfree(filename);
  260. filename = obs_find_data_file("format_conversion.effect");
  261. video->conversion_effect = gs_effect_create_from_file(filename, NULL);
  262. bfree(filename);
  263. filename = obs_find_data_file("bicubic_scale.effect");
  264. video->bicubic_effect = gs_effect_create_from_file(filename, NULL);
  265. bfree(filename);
  266. filename = obs_find_data_file("lanczos_scale.effect");
  267. video->lanczos_effect = gs_effect_create_from_file(filename, NULL);
  268. bfree(filename);
  269. filename = obs_find_data_file("area.effect");
  270. video->area_effect = gs_effect_create_from_file(filename, NULL);
  271. bfree(filename);
  272. filename = obs_find_data_file("bilinear_lowres_scale.effect");
  273. video->bilinear_lowres_effect =
  274. gs_effect_create_from_file(filename, NULL);
  275. bfree(filename);
  276. filename = obs_find_data_file("premultiplied_alpha.effect");
  277. video->premultiplied_alpha_effect =
  278. gs_effect_create_from_file(filename, NULL);
  279. bfree(filename);
  280. point_sampler.max_anisotropy = 1;
  281. video->point_sampler = gs_samplerstate_create(&point_sampler);
  282. obs->video.transparent_texture =
  283. gs_texture_create(2, 2, GS_RGBA, 1, &transparent_tex, 0);
  284. if (!video->default_effect)
  285. success = false;
  286. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  287. if (!video->default_rect_effect)
  288. success = false;
  289. }
  290. if (!video->opaque_effect)
  291. success = false;
  292. if (!video->solid_effect)
  293. success = false;
  294. if (!video->conversion_effect)
  295. success = false;
  296. if (!video->premultiplied_alpha_effect)
  297. success = false;
  298. if (!video->transparent_texture)
  299. success = false;
  300. if (!video->point_sampler)
  301. success = false;
  302. gs_leave_context();
  303. return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
  304. }
  305. static inline void set_video_matrix(struct obs_core_video *video,
  306. struct obs_video_info *ovi)
  307. {
  308. struct matrix4 mat;
  309. struct vec4 r_row;
  310. if (format_is_yuv(ovi->output_format)) {
  311. video_format_get_parameters(ovi->colorspace, ovi->range,
  312. (float *)&mat, NULL, NULL);
  313. matrix4_inv(&mat, &mat);
  314. /* swap R and G */
  315. r_row = mat.x;
  316. mat.x = mat.y;
  317. mat.y = r_row;
  318. } else {
  319. matrix4_identity(&mat);
  320. }
  321. memcpy(video->color_matrix, &mat, sizeof(float) * 16);
  322. }
  323. static int obs_init_video(struct obs_video_info *ovi)
  324. {
  325. struct obs_core_video *video = &obs->video;
  326. struct video_output_info vi;
  327. pthread_mutexattr_t attr;
  328. int errorcode;
  329. make_video_info(&vi, ovi);
  330. video->base_width = ovi->base_width;
  331. video->base_height = ovi->base_height;
  332. video->output_width = ovi->output_width;
  333. video->output_height = ovi->output_height;
  334. video->gpu_conversion = ovi->gpu_conversion;
  335. video->scale_type = ovi->scale_type;
  336. set_video_matrix(video, ovi);
  337. errorcode = video_output_open(&video->video, &vi);
  338. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  339. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
  340. blog(LOG_ERROR, "Invalid video parameters specified");
  341. return OBS_VIDEO_INVALID_PARAM;
  342. } else {
  343. blog(LOG_ERROR, "Could not open video output");
  344. }
  345. return OBS_VIDEO_FAIL;
  346. }
  347. gs_enter_context(video->graphics);
  348. if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
  349. return OBS_VIDEO_FAIL;
  350. if (!obs_init_textures(ovi))
  351. return OBS_VIDEO_FAIL;
  352. gs_leave_context();
  353. if (pthread_mutexattr_init(&attr) != 0)
  354. return OBS_VIDEO_FAIL;
  355. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  356. return OBS_VIDEO_FAIL;
  357. if (pthread_mutex_init(&video->gpu_encoder_mutex, NULL) < 0)
  358. return OBS_VIDEO_FAIL;
  359. errorcode = pthread_create(&video->video_thread, NULL,
  360. obs_graphics_thread, obs);
  361. if (errorcode != 0)
  362. return OBS_VIDEO_FAIL;
  363. video->thread_initialized = true;
  364. video->ovi = *ovi;
  365. return OBS_VIDEO_SUCCESS;
  366. }
  367. static void stop_video(void)
  368. {
  369. struct obs_core_video *video = &obs->video;
  370. void *thread_retval;
  371. if (video->video) {
  372. video_output_stop(video->video);
  373. if (video->thread_initialized) {
  374. pthread_join(video->video_thread, &thread_retval);
  375. video->thread_initialized = false;
  376. }
  377. }
  378. }
  379. static void obs_free_video(void)
  380. {
  381. struct obs_core_video *video = &obs->video;
  382. if (video->video) {
  383. video_output_close(video->video);
  384. video->video = NULL;
  385. if (!video->graphics)
  386. return;
  387. gs_enter_context(video->graphics);
  388. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  389. if (video->mapped_surfaces[c]) {
  390. gs_stagesurface_unmap(
  391. video->mapped_surfaces[c]);
  392. video->mapped_surfaces[c] = NULL;
  393. }
  394. }
  395. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  396. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  397. if (video->copy_surfaces[i][c]) {
  398. gs_stagesurface_destroy(
  399. video->copy_surfaces[i][c]);
  400. video->copy_surfaces[i][c] = NULL;
  401. }
  402. }
  403. }
  404. gs_texture_destroy(video->render_texture);
  405. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  406. if (video->convert_textures[c]) {
  407. gs_texture_destroy(video->convert_textures[c]);
  408. video->convert_textures[c] = NULL;
  409. }
  410. }
  411. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  412. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  413. if (video->copy_surfaces[i][c]) {
  414. gs_stagesurface_destroy(
  415. video->copy_surfaces[i][c]);
  416. video->copy_surfaces[i][c] = NULL;
  417. }
  418. }
  419. }
  420. gs_texture_destroy(video->output_texture);
  421. video->render_texture = NULL;
  422. video->output_texture = NULL;
  423. gs_leave_context();
  424. circlebuf_free(&video->vframe_info_buffer);
  425. circlebuf_free(&video->vframe_info_buffer_gpu);
  426. video->texture_rendered = false;
  427. ;
  428. memset(video->textures_copied, 0,
  429. sizeof(video->textures_copied));
  430. video->texture_converted = false;
  431. ;
  432. pthread_mutex_destroy(&video->gpu_encoder_mutex);
  433. pthread_mutex_init_value(&video->gpu_encoder_mutex);
  434. da_free(video->gpu_encoders);
  435. video->gpu_encoder_active = 0;
  436. video->cur_texture = 0;
  437. }
  438. }
  439. static void obs_free_graphics(void)
  440. {
  441. struct obs_core_video *video = &obs->video;
  442. if (video->graphics) {
  443. gs_enter_context(video->graphics);
  444. gs_texture_destroy(video->transparent_texture);
  445. gs_samplerstate_destroy(video->point_sampler);
  446. gs_effect_destroy(video->default_effect);
  447. gs_effect_destroy(video->default_rect_effect);
  448. gs_effect_destroy(video->opaque_effect);
  449. gs_effect_destroy(video->solid_effect);
  450. gs_effect_destroy(video->conversion_effect);
  451. gs_effect_destroy(video->bicubic_effect);
  452. gs_effect_destroy(video->repeat_effect);
  453. gs_effect_destroy(video->lanczos_effect);
  454. gs_effect_destroy(video->area_effect);
  455. gs_effect_destroy(video->bilinear_lowres_effect);
  456. video->default_effect = NULL;
  457. gs_leave_context();
  458. gs_destroy(video->graphics);
  459. video->graphics = NULL;
  460. }
  461. }
  462. static bool obs_init_audio(struct audio_output_info *ai)
  463. {
  464. struct obs_core_audio *audio = &obs->audio;
  465. int errorcode;
  466. pthread_mutexattr_t attr;
  467. pthread_mutex_init_value(&audio->monitoring_mutex);
  468. if (pthread_mutexattr_init(&attr) != 0)
  469. return false;
  470. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  471. return false;
  472. if (pthread_mutex_init(&audio->monitoring_mutex, &attr) != 0)
  473. return false;
  474. audio->user_volume = 1.0f;
  475. audio->monitoring_device_name = bstrdup("Default");
  476. audio->monitoring_device_id = bstrdup("default");
  477. errorcode = audio_output_open(&audio->audio, ai);
  478. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  479. return true;
  480. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  481. blog(LOG_ERROR, "Invalid audio parameters specified");
  482. else
  483. blog(LOG_ERROR, "Could not open audio output");
  484. return false;
  485. }
  486. static void obs_free_audio(void)
  487. {
  488. struct obs_core_audio *audio = &obs->audio;
  489. if (audio->audio)
  490. audio_output_close(audio->audio);
  491. circlebuf_free(&audio->buffered_timestamps);
  492. da_free(audio->render_order);
  493. da_free(audio->root_nodes);
  494. da_free(audio->monitors);
  495. bfree(audio->monitoring_device_name);
  496. bfree(audio->monitoring_device_id);
  497. pthread_mutex_destroy(&audio->monitoring_mutex);
  498. memset(audio, 0, sizeof(struct obs_core_audio));
  499. }
  500. static bool obs_init_data(void)
  501. {
  502. struct obs_core_data *data = &obs->data;
  503. pthread_mutexattr_t attr;
  504. assert(data != NULL);
  505. pthread_mutex_init_value(&obs->data.displays_mutex);
  506. pthread_mutex_init_value(&obs->data.draw_callbacks_mutex);
  507. if (pthread_mutexattr_init(&attr) != 0)
  508. return false;
  509. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  510. goto fail;
  511. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  512. goto fail;
  513. if (pthread_mutex_init(&data->audio_sources_mutex, &attr) != 0)
  514. goto fail;
  515. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  516. goto fail;
  517. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  518. goto fail;
  519. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  520. goto fail;
  521. if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
  522. goto fail;
  523. if (pthread_mutex_init(&obs->data.draw_callbacks_mutex, &attr) != 0)
  524. goto fail;
  525. if (!obs_view_init(&data->main_view))
  526. goto fail;
  527. data->private_data = obs_data_create();
  528. data->valid = true;
  529. fail:
  530. pthread_mutexattr_destroy(&attr);
  531. return data->valid;
  532. }
  533. void obs_main_view_free(struct obs_view *view)
  534. {
  535. if (!view)
  536. return;
  537. for (size_t i = 0; i < MAX_CHANNELS; i++)
  538. obs_source_release(view->channels[i]);
  539. memset(view->channels, 0, sizeof(view->channels));
  540. pthread_mutex_destroy(&view->channels_mutex);
  541. }
  542. #define FREE_OBS_LINKED_LIST(type) \
  543. do { \
  544. int unfreed = 0; \
  545. while (data->first_##type) { \
  546. obs_##type##_destroy(data->first_##type); \
  547. unfreed++; \
  548. } \
  549. if (unfreed) \
  550. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  551. unfreed); \
  552. } while (false)
  553. static void obs_free_data(void)
  554. {
  555. struct obs_core_data *data = &obs->data;
  556. data->valid = false;
  557. obs_main_view_free(&data->main_view);
  558. blog(LOG_INFO, "Freeing OBS context data");
  559. FREE_OBS_LINKED_LIST(source);
  560. FREE_OBS_LINKED_LIST(output);
  561. FREE_OBS_LINKED_LIST(encoder);
  562. FREE_OBS_LINKED_LIST(display);
  563. FREE_OBS_LINKED_LIST(service);
  564. pthread_mutex_destroy(&data->sources_mutex);
  565. pthread_mutex_destroy(&data->audio_sources_mutex);
  566. pthread_mutex_destroy(&data->displays_mutex);
  567. pthread_mutex_destroy(&data->outputs_mutex);
  568. pthread_mutex_destroy(&data->encoders_mutex);
  569. pthread_mutex_destroy(&data->services_mutex);
  570. pthread_mutex_destroy(&data->draw_callbacks_mutex);
  571. da_free(data->draw_callbacks);
  572. da_free(data->tick_callbacks);
  573. obs_data_release(data->private_data);
  574. }
  575. static const char *obs_signals[] = {
  576. "void source_create(ptr source)",
  577. "void source_destroy(ptr source)",
  578. "void source_remove(ptr source)",
  579. "void source_save(ptr source)",
  580. "void source_load(ptr source)",
  581. "void source_activate(ptr source)",
  582. "void source_deactivate(ptr source)",
  583. "void source_show(ptr source)",
  584. "void source_hide(ptr source)",
  585. "void source_rename(ptr source, string new_name, string prev_name)",
  586. "void source_volume(ptr source, in out float volume)",
  587. "void source_volume_level(ptr source, float level, float magnitude, "
  588. "float peak)",
  589. "void source_transition_start(ptr source)",
  590. "void source_transition_video_stop(ptr source)",
  591. "void source_transition_stop(ptr source)",
  592. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  593. "void master_volume(in out float volume)",
  594. "void hotkey_layout_change()",
  595. "void hotkey_register(ptr hotkey)",
  596. "void hotkey_unregister(ptr hotkey)",
  597. "void hotkey_bindings_changed(ptr hotkey)",
  598. NULL,
  599. };
  600. static inline bool obs_init_handlers(void)
  601. {
  602. obs->signals = signal_handler_create();
  603. if (!obs->signals)
  604. return false;
  605. obs->procs = proc_handler_create();
  606. if (!obs->procs)
  607. return false;
  608. return signal_handler_add_array(obs->signals, obs_signals);
  609. }
  610. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  611. static inline bool obs_init_hotkeys(void)
  612. {
  613. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  614. pthread_mutexattr_t attr;
  615. bool success = false;
  616. assert(hotkeys != NULL);
  617. da_init(hotkeys->hotkeys);
  618. hotkeys->signals = obs->signals;
  619. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  620. hotkeys->mute = bstrdup("Mute");
  621. hotkeys->unmute = bstrdup("Unmute");
  622. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  623. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  624. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  625. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  626. if (!obs_hotkeys_platform_init(hotkeys))
  627. return false;
  628. if (pthread_mutexattr_init(&attr) != 0)
  629. return false;
  630. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  631. goto fail;
  632. if (pthread_mutex_init(&hotkeys->mutex, &attr) != 0)
  633. goto fail;
  634. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  635. goto fail;
  636. if (pthread_create(&hotkeys->hotkey_thread, NULL, obs_hotkey_thread,
  637. NULL))
  638. goto fail;
  639. hotkeys->hotkey_thread_initialized = true;
  640. success = true;
  641. fail:
  642. pthread_mutexattr_destroy(&attr);
  643. return success;
  644. }
  645. static inline void stop_hotkeys(void)
  646. {
  647. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  648. void *thread_ret;
  649. if (hotkeys->hotkey_thread_initialized) {
  650. os_event_signal(hotkeys->stop_event);
  651. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  652. hotkeys->hotkey_thread_initialized = false;
  653. }
  654. os_event_destroy(hotkeys->stop_event);
  655. obs_hotkeys_free();
  656. }
  657. static inline void obs_free_hotkeys(void)
  658. {
  659. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  660. bfree(hotkeys->mute);
  661. bfree(hotkeys->unmute);
  662. bfree(hotkeys->push_to_mute);
  663. bfree(hotkeys->push_to_talk);
  664. bfree(hotkeys->sceneitem_show);
  665. bfree(hotkeys->sceneitem_hide);
  666. obs_hotkey_name_map_free();
  667. obs_hotkeys_platform_free(hotkeys);
  668. pthread_mutex_destroy(&hotkeys->mutex);
  669. }
  670. extern const struct obs_source_info scene_info;
  671. extern const struct obs_source_info group_info;
  672. extern void log_system_info(void);
  673. static bool obs_init(const char *locale, const char *module_config_path,
  674. profiler_name_store_t *store)
  675. {
  676. obs = bzalloc(sizeof(struct obs_core));
  677. pthread_mutex_init_value(&obs->audio.monitoring_mutex);
  678. pthread_mutex_init_value(&obs->video.gpu_encoder_mutex);
  679. obs->name_store_owned = !store;
  680. obs->name_store = store ? store : profiler_name_store_create();
  681. if (!obs->name_store) {
  682. blog(LOG_ERROR, "Couldn't create profiler name store");
  683. return false;
  684. }
  685. log_system_info();
  686. if (!obs_init_data())
  687. return false;
  688. if (!obs_init_handlers())
  689. return false;
  690. if (!obs_init_hotkeys())
  691. return false;
  692. if (module_config_path)
  693. obs->module_config_path = bstrdup(module_config_path);
  694. obs->locale = bstrdup(locale);
  695. obs_register_source(&scene_info);
  696. obs_register_source(&group_info);
  697. add_default_module_paths();
  698. return true;
  699. }
  700. #ifdef _WIN32
  701. extern void initialize_com(void);
  702. extern void uninitialize_com(void);
  703. #endif
  704. /* Separate from actual context initialization
  705. * since this can be set before startup and persist
  706. * after shutdown. */
  707. static DARRAY(struct dstr) core_module_paths = {0};
  708. char *obs_find_data_file(const char *file)
  709. {
  710. struct dstr path = {0};
  711. char *result = find_libobs_data_file(file);
  712. if (result)
  713. return result;
  714. for (size_t i = 0; i < core_module_paths.num; ++i) {
  715. if (check_path(file, core_module_paths.array[i].array, &path))
  716. return path.array;
  717. }
  718. dstr_free(&path);
  719. return NULL;
  720. }
  721. void obs_add_data_path(const char *path)
  722. {
  723. struct dstr *new_path = da_push_back_new(core_module_paths);
  724. dstr_init_copy(new_path, path);
  725. da_push_back(core_module_paths, new_path);
  726. }
  727. bool obs_remove_data_path(const char *path)
  728. {
  729. for (size_t i = 0; i < core_module_paths.num; ++i) {
  730. int result = dstr_cmp(&core_module_paths.array[i], path);
  731. if (result == 0) {
  732. dstr_free(&core_module_paths.array[i]);
  733. da_erase(core_module_paths, i);
  734. return true;
  735. }
  736. }
  737. return false;
  738. }
  739. static const char *obs_startup_name = "obs_startup";
  740. bool obs_startup(const char *locale, const char *module_config_path,
  741. profiler_name_store_t *store)
  742. {
  743. bool success;
  744. profile_start(obs_startup_name);
  745. if (obs) {
  746. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  747. return false;
  748. }
  749. #ifdef _WIN32
  750. initialize_com();
  751. #endif
  752. success = obs_init(locale, module_config_path, store);
  753. profile_end(obs_startup_name);
  754. if (!success)
  755. obs_shutdown();
  756. return success;
  757. }
  758. static struct obs_cmdline_args cmdline_args = {0, NULL};
  759. void obs_set_cmdline_args(int argc, const char *const *argv)
  760. {
  761. char *data;
  762. size_t len;
  763. int i;
  764. /* Once argc is set (non-zero) we shouldn't call again */
  765. if (cmdline_args.argc)
  766. return;
  767. cmdline_args.argc = argc;
  768. /* Safely copy over argv */
  769. len = 0;
  770. for (i = 0; i < argc; i++)
  771. len += strlen(argv[i]) + 1;
  772. cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
  773. data = (char *)cmdline_args.argv + sizeof(char *) * (argc + 1);
  774. for (i = 0; i < argc; i++) {
  775. cmdline_args.argv[i] = data;
  776. len = strlen(argv[i]) + 1;
  777. memcpy(data, argv[i], len);
  778. data += len;
  779. }
  780. cmdline_args.argv[argc] = NULL;
  781. }
  782. struct obs_cmdline_args obs_get_cmdline_args(void)
  783. {
  784. return cmdline_args;
  785. }
  786. void obs_shutdown(void)
  787. {
  788. struct obs_module *module;
  789. struct obs_core *core;
  790. if (!obs)
  791. return;
  792. #define FREE_REGISTERED_TYPES(structure, list) \
  793. do { \
  794. for (size_t i = 0; i < list.num; i++) { \
  795. struct structure *item = &list.array[i]; \
  796. if (item->type_data && item->free_type_data) \
  797. item->free_type_data(item->type_data); \
  798. } \
  799. da_free(list); \
  800. } while (false)
  801. FREE_REGISTERED_TYPES(obs_source_info, obs->source_types);
  802. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  803. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  804. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  805. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  806. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  807. #undef FREE_REGISTERED_TYPES
  808. da_free(obs->input_types);
  809. da_free(obs->filter_types);
  810. da_free(obs->transition_types);
  811. stop_video();
  812. stop_hotkeys();
  813. obs_free_audio();
  814. obs_free_data();
  815. obs_free_video();
  816. obs_free_hotkeys();
  817. obs_free_graphics();
  818. proc_handler_destroy(obs->procs);
  819. signal_handler_destroy(obs->signals);
  820. obs->procs = NULL;
  821. obs->signals = NULL;
  822. core = obs;
  823. obs = NULL;
  824. module = core->first_module;
  825. while (module) {
  826. struct obs_module *next = module->next;
  827. free_module(module);
  828. module = next;
  829. }
  830. core->first_module = NULL;
  831. for (size_t i = 0; i < core->module_paths.num; i++)
  832. free_module_path(core->module_paths.array + i);
  833. da_free(core->module_paths);
  834. if (core->name_store_owned)
  835. profiler_name_store_free(core->name_store);
  836. bfree(core->module_config_path);
  837. bfree(core->locale);
  838. bfree(core);
  839. bfree(cmdline_args.argv);
  840. #ifdef _WIN32
  841. uninitialize_com();
  842. #endif
  843. }
  844. bool obs_initialized(void)
  845. {
  846. return obs != NULL;
  847. }
  848. uint32_t obs_get_version(void)
  849. {
  850. return LIBOBS_API_VER;
  851. }
  852. const char *obs_get_version_string(void)
  853. {
  854. return OBS_VERSION;
  855. }
  856. void obs_set_locale(const char *locale)
  857. {
  858. struct obs_module *module;
  859. if (!obs)
  860. return;
  861. if (obs->locale)
  862. bfree(obs->locale);
  863. obs->locale = bstrdup(locale);
  864. module = obs->first_module;
  865. while (module) {
  866. if (module->set_locale)
  867. module->set_locale(locale);
  868. module = module->next;
  869. }
  870. }
  871. const char *obs_get_locale(void)
  872. {
  873. return obs ? obs->locale : NULL;
  874. }
  875. #define OBS_SIZE_MIN 2
  876. #define OBS_SIZE_MAX (32 * 1024)
  877. static inline bool size_valid(uint32_t width, uint32_t height)
  878. {
  879. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  880. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  881. }
  882. int obs_reset_video(struct obs_video_info *ovi)
  883. {
  884. if (!obs)
  885. return OBS_VIDEO_FAIL;
  886. /* don't allow changing of video settings if active. */
  887. if (obs->video.video && obs_video_active())
  888. return OBS_VIDEO_CURRENTLY_ACTIVE;
  889. if (!size_valid(ovi->output_width, ovi->output_height) ||
  890. !size_valid(ovi->base_width, ovi->base_height))
  891. return OBS_VIDEO_INVALID_PARAM;
  892. struct obs_core_video *video = &obs->video;
  893. stop_video();
  894. obs_free_video();
  895. /* align to multiple-of-two and SSE alignment sizes */
  896. ovi->output_width &= 0xFFFFFFFC;
  897. ovi->output_height &= 0xFFFFFFFE;
  898. if (!video->graphics) {
  899. int errorcode = obs_init_graphics(ovi);
  900. if (errorcode != OBS_VIDEO_SUCCESS) {
  901. obs_free_graphics();
  902. return errorcode;
  903. }
  904. }
  905. const char *scale_type_name = "";
  906. switch (ovi->scale_type) {
  907. case OBS_SCALE_DISABLE:
  908. scale_type_name = "Disabled";
  909. break;
  910. case OBS_SCALE_POINT:
  911. scale_type_name = "Point";
  912. break;
  913. case OBS_SCALE_BICUBIC:
  914. scale_type_name = "Bicubic";
  915. break;
  916. case OBS_SCALE_BILINEAR:
  917. scale_type_name = "Bilinear";
  918. break;
  919. case OBS_SCALE_LANCZOS:
  920. scale_type_name = "Lanczos";
  921. break;
  922. case OBS_SCALE_AREA:
  923. scale_type_name = "Area";
  924. break;
  925. }
  926. bool yuv = format_is_yuv(ovi->output_format);
  927. const char *yuv_format = get_video_colorspace_name(ovi->colorspace);
  928. const char *yuv_range =
  929. get_video_range_name(ovi->output_format, ovi->range);
  930. blog(LOG_INFO, "---------------------------------");
  931. blog(LOG_INFO,
  932. "video settings reset:\n"
  933. "\tbase resolution: %dx%d\n"
  934. "\toutput resolution: %dx%d\n"
  935. "\tdownscale filter: %s\n"
  936. "\tfps: %d/%d\n"
  937. "\tformat: %s\n"
  938. "\tYUV mode: %s%s%s",
  939. ovi->base_width, ovi->base_height, ovi->output_width,
  940. ovi->output_height, scale_type_name, ovi->fps_num, ovi->fps_den,
  941. get_video_format_name(ovi->output_format),
  942. yuv ? yuv_format : "None", yuv ? "/" : "", yuv ? yuv_range : "");
  943. return obs_init_video(ovi);
  944. }
  945. bool obs_reset_audio(const struct obs_audio_info *oai)
  946. {
  947. struct audio_output_info ai;
  948. if (!obs)
  949. return false;
  950. /* don't allow changing of audio settings if active. */
  951. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  952. return false;
  953. obs_free_audio();
  954. if (!oai)
  955. return true;
  956. ai.name = "Audio";
  957. ai.samples_per_sec = oai->samples_per_sec;
  958. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  959. ai.speakers = oai->speakers;
  960. ai.input_callback = audio_callback;
  961. blog(LOG_INFO, "---------------------------------");
  962. blog(LOG_INFO,
  963. "audio settings reset:\n"
  964. "\tsamples per sec: %d\n"
  965. "\tspeakers: %d",
  966. (int)ai.samples_per_sec, (int)ai.speakers);
  967. return obs_init_audio(&ai);
  968. }
  969. bool obs_get_video_info(struct obs_video_info *ovi)
  970. {
  971. struct obs_core_video *video = &obs->video;
  972. if (!obs || !video->graphics)
  973. return false;
  974. *ovi = video->ovi;
  975. return true;
  976. }
  977. bool obs_get_audio_info(struct obs_audio_info *oai)
  978. {
  979. struct obs_core_audio *audio = &obs->audio;
  980. const struct audio_output_info *info;
  981. if (!obs || !oai || !audio->audio)
  982. return false;
  983. info = audio_output_get_info(audio->audio);
  984. oai->samples_per_sec = info->samples_per_sec;
  985. oai->speakers = info->speakers;
  986. return true;
  987. }
  988. bool obs_enum_source_types(size_t idx, const char **id)
  989. {
  990. if (!obs)
  991. return false;
  992. if (idx >= obs->source_types.num)
  993. return false;
  994. *id = obs->source_types.array[idx].id;
  995. return true;
  996. }
  997. bool obs_enum_input_types(size_t idx, const char **id)
  998. {
  999. if (!obs)
  1000. return false;
  1001. if (idx >= obs->input_types.num)
  1002. return false;
  1003. *id = obs->input_types.array[idx].id;
  1004. return true;
  1005. }
  1006. bool obs_enum_filter_types(size_t idx, const char **id)
  1007. {
  1008. if (!obs)
  1009. return false;
  1010. if (idx >= obs->filter_types.num)
  1011. return false;
  1012. *id = obs->filter_types.array[idx].id;
  1013. return true;
  1014. }
  1015. bool obs_enum_transition_types(size_t idx, const char **id)
  1016. {
  1017. if (!obs)
  1018. return false;
  1019. if (idx >= obs->transition_types.num)
  1020. return false;
  1021. *id = obs->transition_types.array[idx].id;
  1022. return true;
  1023. }
  1024. bool obs_enum_output_types(size_t idx, const char **id)
  1025. {
  1026. if (!obs)
  1027. return false;
  1028. if (idx >= obs->output_types.num)
  1029. return false;
  1030. *id = obs->output_types.array[idx].id;
  1031. return true;
  1032. }
  1033. bool obs_enum_encoder_types(size_t idx, const char **id)
  1034. {
  1035. if (!obs)
  1036. return false;
  1037. if (idx >= obs->encoder_types.num)
  1038. return false;
  1039. *id = obs->encoder_types.array[idx].id;
  1040. return true;
  1041. }
  1042. bool obs_enum_service_types(size_t idx, const char **id)
  1043. {
  1044. if (!obs)
  1045. return false;
  1046. if (idx >= obs->service_types.num)
  1047. return false;
  1048. *id = obs->service_types.array[idx].id;
  1049. return true;
  1050. }
  1051. void obs_enter_graphics(void)
  1052. {
  1053. if (obs && obs->video.graphics)
  1054. gs_enter_context(obs->video.graphics);
  1055. }
  1056. void obs_leave_graphics(void)
  1057. {
  1058. if (obs && obs->video.graphics)
  1059. gs_leave_context();
  1060. }
  1061. audio_t *obs_get_audio(void)
  1062. {
  1063. return (obs != NULL) ? obs->audio.audio : NULL;
  1064. }
  1065. video_t *obs_get_video(void)
  1066. {
  1067. return (obs != NULL) ? obs->video.video : NULL;
  1068. }
  1069. /* TODO: optimize this later so it's not just O(N) string lookups */
  1070. static inline struct obs_modal_ui *
  1071. get_modal_ui_callback(const char *id, const char *task, const char *target)
  1072. {
  1073. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  1074. struct obs_modal_ui *callback =
  1075. obs->modal_ui_callbacks.array + i;
  1076. if (strcmp(callback->id, id) == 0 &&
  1077. strcmp(callback->task, task) == 0 &&
  1078. strcmp(callback->target, target) == 0)
  1079. return callback;
  1080. }
  1081. return NULL;
  1082. }
  1083. static inline struct obs_modeless_ui *
  1084. get_modeless_ui_callback(const char *id, const char *task, const char *target)
  1085. {
  1086. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  1087. struct obs_modeless_ui *callback;
  1088. callback = obs->modeless_ui_callbacks.array + i;
  1089. if (strcmp(callback->id, id) == 0 &&
  1090. strcmp(callback->task, task) == 0 &&
  1091. strcmp(callback->target, target) == 0)
  1092. return callback;
  1093. }
  1094. return NULL;
  1095. }
  1096. int obs_exec_ui(const char *name, const char *task, const char *target,
  1097. void *data, void *ui_data)
  1098. {
  1099. struct obs_modal_ui *callback;
  1100. int errorcode = OBS_UI_NOTFOUND;
  1101. if (!obs)
  1102. return errorcode;
  1103. callback = get_modal_ui_callback(name, task, target);
  1104. if (callback) {
  1105. bool success = callback->exec(data, ui_data);
  1106. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  1107. }
  1108. return errorcode;
  1109. }
  1110. void *obs_create_ui(const char *name, const char *task, const char *target,
  1111. void *data, void *ui_data)
  1112. {
  1113. struct obs_modeless_ui *callback;
  1114. if (!obs)
  1115. return NULL;
  1116. callback = get_modeless_ui_callback(name, task, target);
  1117. return callback ? callback->create(data, ui_data) : NULL;
  1118. }
  1119. obs_source_t *obs_get_output_source(uint32_t channel)
  1120. {
  1121. if (!obs)
  1122. return NULL;
  1123. return obs_view_get_source(&obs->data.main_view, channel);
  1124. }
  1125. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  1126. {
  1127. assert(channel < MAX_CHANNELS);
  1128. if (!obs)
  1129. return;
  1130. if (channel >= MAX_CHANNELS)
  1131. return;
  1132. struct obs_source *prev_source;
  1133. struct obs_view *view = &obs->data.main_view;
  1134. struct calldata params = {0};
  1135. pthread_mutex_lock(&view->channels_mutex);
  1136. obs_source_addref(source);
  1137. prev_source = view->channels[channel];
  1138. calldata_set_int(&params, "channel", channel);
  1139. calldata_set_ptr(&params, "prev_source", prev_source);
  1140. calldata_set_ptr(&params, "source", source);
  1141. signal_handler_signal(obs->signals, "channel_change", &params);
  1142. calldata_get_ptr(&params, "source", &source);
  1143. calldata_free(&params);
  1144. view->channels[channel] = source;
  1145. pthread_mutex_unlock(&view->channels_mutex);
  1146. if (source)
  1147. obs_source_activate(source, MAIN_VIEW);
  1148. if (prev_source) {
  1149. obs_source_deactivate(prev_source, MAIN_VIEW);
  1150. obs_source_release(prev_source);
  1151. }
  1152. }
  1153. void obs_enum_sources(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1154. {
  1155. obs_source_t *source;
  1156. if (!obs)
  1157. return;
  1158. pthread_mutex_lock(&obs->data.sources_mutex);
  1159. source = obs->data.first_source;
  1160. while (source) {
  1161. obs_source_t *next_source =
  1162. (obs_source_t *)source->context.next;
  1163. if (source->info.id == group_info.id &&
  1164. !enum_proc(param, source)) {
  1165. break;
  1166. } else if (source->info.type == OBS_SOURCE_TYPE_INPUT &&
  1167. !source->context.private &&
  1168. !enum_proc(param, source)) {
  1169. break;
  1170. }
  1171. source = next_source;
  1172. }
  1173. pthread_mutex_unlock(&obs->data.sources_mutex);
  1174. }
  1175. void obs_enum_scenes(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1176. {
  1177. obs_source_t *source;
  1178. if (!obs)
  1179. return;
  1180. pthread_mutex_lock(&obs->data.sources_mutex);
  1181. source = obs->data.first_source;
  1182. while (source) {
  1183. obs_source_t *next_source =
  1184. (obs_source_t *)source->context.next;
  1185. if (source->info.type == OBS_SOURCE_TYPE_SCENE &&
  1186. !source->context.private && !enum_proc(param, source)) {
  1187. break;
  1188. }
  1189. source = next_source;
  1190. }
  1191. pthread_mutex_unlock(&obs->data.sources_mutex);
  1192. }
  1193. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  1194. void *param)
  1195. {
  1196. struct obs_context_data **start = pstart, *context;
  1197. bool (*enum_proc)(void *, void *) = proc;
  1198. assert(start);
  1199. assert(mutex);
  1200. assert(enum_proc);
  1201. pthread_mutex_lock(mutex);
  1202. context = *start;
  1203. while (context) {
  1204. if (!enum_proc(param, context))
  1205. break;
  1206. context = context->next;
  1207. }
  1208. pthread_mutex_unlock(mutex);
  1209. }
  1210. void obs_enum_outputs(bool (*enum_proc)(void *, obs_output_t *), void *param)
  1211. {
  1212. if (!obs)
  1213. return;
  1214. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex, enum_proc,
  1215. param);
  1216. }
  1217. void obs_enum_encoders(bool (*enum_proc)(void *, obs_encoder_t *), void *param)
  1218. {
  1219. if (!obs)
  1220. return;
  1221. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex, enum_proc,
  1222. param);
  1223. }
  1224. void obs_enum_services(bool (*enum_proc)(void *, obs_service_t *), void *param)
  1225. {
  1226. if (!obs)
  1227. return;
  1228. obs_enum(&obs->data.first_service, &obs->data.services_mutex, enum_proc,
  1229. param);
  1230. }
  1231. static inline void *get_context_by_name(void *vfirst, const char *name,
  1232. pthread_mutex_t *mutex,
  1233. void *(*addref)(void *))
  1234. {
  1235. struct obs_context_data **first = vfirst;
  1236. struct obs_context_data *context;
  1237. pthread_mutex_lock(mutex);
  1238. context = *first;
  1239. while (context) {
  1240. if (!context->private && strcmp(context->name, name) == 0) {
  1241. context = addref(context);
  1242. break;
  1243. }
  1244. context = context->next;
  1245. }
  1246. pthread_mutex_unlock(mutex);
  1247. return context;
  1248. }
  1249. static inline void *obs_source_addref_safe_(void *ref)
  1250. {
  1251. return obs_source_get_ref(ref);
  1252. }
  1253. static inline void *obs_output_addref_safe_(void *ref)
  1254. {
  1255. return obs_output_get_ref(ref);
  1256. }
  1257. static inline void *obs_encoder_addref_safe_(void *ref)
  1258. {
  1259. return obs_encoder_get_ref(ref);
  1260. }
  1261. static inline void *obs_service_addref_safe_(void *ref)
  1262. {
  1263. return obs_service_get_ref(ref);
  1264. }
  1265. static inline void *obs_id_(void *data)
  1266. {
  1267. return data;
  1268. }
  1269. obs_source_t *obs_get_source_by_name(const char *name)
  1270. {
  1271. if (!obs)
  1272. return NULL;
  1273. return get_context_by_name(&obs->data.first_source, name,
  1274. &obs->data.sources_mutex,
  1275. obs_source_addref_safe_);
  1276. }
  1277. obs_output_t *obs_get_output_by_name(const char *name)
  1278. {
  1279. if (!obs)
  1280. return NULL;
  1281. return get_context_by_name(&obs->data.first_output, name,
  1282. &obs->data.outputs_mutex,
  1283. obs_output_addref_safe_);
  1284. }
  1285. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1286. {
  1287. if (!obs)
  1288. return NULL;
  1289. return get_context_by_name(&obs->data.first_encoder, name,
  1290. &obs->data.encoders_mutex,
  1291. obs_encoder_addref_safe_);
  1292. }
  1293. obs_service_t *obs_get_service_by_name(const char *name)
  1294. {
  1295. if (!obs)
  1296. return NULL;
  1297. return get_context_by_name(&obs->data.first_service, name,
  1298. &obs->data.services_mutex,
  1299. obs_service_addref_safe_);
  1300. }
  1301. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1302. {
  1303. if (!obs)
  1304. return NULL;
  1305. switch (effect) {
  1306. case OBS_EFFECT_DEFAULT:
  1307. return obs->video.default_effect;
  1308. case OBS_EFFECT_DEFAULT_RECT:
  1309. return obs->video.default_rect_effect;
  1310. case OBS_EFFECT_OPAQUE:
  1311. return obs->video.opaque_effect;
  1312. case OBS_EFFECT_SOLID:
  1313. return obs->video.solid_effect;
  1314. case OBS_EFFECT_REPEAT:
  1315. return obs->video.repeat_effect;
  1316. case OBS_EFFECT_BICUBIC:
  1317. return obs->video.bicubic_effect;
  1318. case OBS_EFFECT_LANCZOS:
  1319. return obs->video.lanczos_effect;
  1320. case OBS_EFFECT_AREA:
  1321. return obs->video.area_effect;
  1322. case OBS_EFFECT_BILINEAR_LOWRES:
  1323. return obs->video.bilinear_lowres_effect;
  1324. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1325. return obs->video.premultiplied_alpha_effect;
  1326. }
  1327. return NULL;
  1328. }
  1329. /* DEPRECATED */
  1330. gs_effect_t *obs_get_default_rect_effect(void)
  1331. {
  1332. if (!obs)
  1333. return NULL;
  1334. return obs->video.default_rect_effect;
  1335. }
  1336. signal_handler_t *obs_get_signal_handler(void)
  1337. {
  1338. if (!obs)
  1339. return NULL;
  1340. return obs->signals;
  1341. }
  1342. proc_handler_t *obs_get_proc_handler(void)
  1343. {
  1344. if (!obs)
  1345. return NULL;
  1346. return obs->procs;
  1347. }
  1348. void obs_render_main_view(void)
  1349. {
  1350. if (!obs)
  1351. return;
  1352. obs_view_render(&obs->data.main_view);
  1353. }
  1354. static void obs_render_main_texture_internal(enum gs_blend_type src_c,
  1355. enum gs_blend_type dest_c,
  1356. enum gs_blend_type src_a,
  1357. enum gs_blend_type dest_a)
  1358. {
  1359. struct obs_core_video *video;
  1360. gs_texture_t *tex;
  1361. gs_effect_t *effect;
  1362. gs_eparam_t *param;
  1363. if (!obs)
  1364. return;
  1365. video = &obs->video;
  1366. if (!video->texture_rendered)
  1367. return;
  1368. tex = video->render_texture;
  1369. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1370. param = gs_effect_get_param_by_name(effect, "image");
  1371. gs_effect_set_texture(param, tex);
  1372. gs_blend_state_push();
  1373. gs_blend_function_separate(src_c, dest_c, src_a, dest_a);
  1374. while (gs_effect_loop(effect, "Draw"))
  1375. gs_draw_sprite(tex, 0, 0, 0);
  1376. gs_blend_state_pop();
  1377. }
  1378. void obs_render_main_texture(void)
  1379. {
  1380. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA,
  1381. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1382. }
  1383. void obs_render_main_texture_src_color_only(void)
  1384. {
  1385. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_ZERO,
  1386. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1387. }
  1388. gs_texture_t *obs_get_main_texture(void)
  1389. {
  1390. struct obs_core_video *video;
  1391. if (!obs)
  1392. return NULL;
  1393. video = &obs->video;
  1394. if (!video->texture_rendered)
  1395. return NULL;
  1396. return video->render_texture;
  1397. }
  1398. void obs_set_master_volume(float volume)
  1399. {
  1400. struct calldata data = {0};
  1401. if (!obs)
  1402. return;
  1403. calldata_set_float(&data, "volume", volume);
  1404. signal_handler_signal(obs->signals, "master_volume", &data);
  1405. volume = (float)calldata_float(&data, "volume");
  1406. calldata_free(&data);
  1407. obs->audio.user_volume = volume;
  1408. }
  1409. float obs_get_master_volume(void)
  1410. {
  1411. return obs ? obs->audio.user_volume : 0.0f;
  1412. }
  1413. static obs_source_t *obs_load_source_type(obs_data_t *source_data)
  1414. {
  1415. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1416. obs_source_t *source;
  1417. const char *name = obs_data_get_string(source_data, "name");
  1418. const char *id = obs_data_get_string(source_data, "id");
  1419. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1420. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1421. double volume;
  1422. double balance;
  1423. int64_t sync;
  1424. uint32_t prev_ver;
  1425. uint32_t caps;
  1426. uint32_t flags;
  1427. uint32_t mixers;
  1428. int di_order;
  1429. int di_mode;
  1430. int monitoring_type;
  1431. source = obs_source_create(id, name, settings, hotkeys);
  1432. obs_data_release(hotkeys);
  1433. prev_ver = (uint32_t)obs_data_get_int(source_data, "prev_ver");
  1434. caps = obs_source_get_output_flags(source);
  1435. obs_data_set_default_double(source_data, "volume", 1.0);
  1436. volume = obs_data_get_double(source_data, "volume");
  1437. obs_source_set_volume(source, (float)volume);
  1438. obs_data_set_default_double(source_data, "balance", 0.5);
  1439. balance = obs_data_get_double(source_data, "balance");
  1440. obs_source_set_balance_value(source, (float)balance);
  1441. sync = obs_data_get_int(source_data, "sync");
  1442. obs_source_set_sync_offset(source, sync);
  1443. obs_data_set_default_int(source_data, "mixers", 0xF);
  1444. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1445. obs_source_set_audio_mixers(source, mixers);
  1446. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1447. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1448. obs_source_set_flags(source, flags);
  1449. obs_data_set_default_bool(source_data, "enabled", true);
  1450. obs_source_set_enabled(source,
  1451. obs_data_get_bool(source_data, "enabled"));
  1452. obs_data_set_default_bool(source_data, "muted", false);
  1453. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1454. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1455. obs_source_enable_push_to_mute(
  1456. source, obs_data_get_bool(source_data, "push-to-mute"));
  1457. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1458. obs_source_set_push_to_mute_delay(
  1459. source, obs_data_get_int(source_data, "push-to-mute-delay"));
  1460. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1461. obs_source_enable_push_to_talk(
  1462. source, obs_data_get_bool(source_data, "push-to-talk"));
  1463. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1464. obs_source_set_push_to_talk_delay(
  1465. source, obs_data_get_int(source_data, "push-to-talk-delay"));
  1466. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1467. obs_source_set_deinterlace_mode(source,
  1468. (enum obs_deinterlace_mode)di_mode);
  1469. di_order =
  1470. (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1471. obs_source_set_deinterlace_field_order(
  1472. source, (enum obs_deinterlace_field_order)di_order);
  1473. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1474. if (prev_ver < MAKE_SEMANTIC_VERSION(24, 0, 0)) {
  1475. if ((caps & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  1476. /* updates older sources to enable monitoring
  1477. * automatically if they added monitoring by default in
  1478. * version 24 */
  1479. monitoring_type = OBS_MONITORING_TYPE_MONITOR_ONLY;
  1480. }
  1481. }
  1482. obs_source_set_monitoring_type(
  1483. source, (enum obs_monitoring_type)monitoring_type);
  1484. obs_data_release(source->private_settings);
  1485. source->private_settings =
  1486. obs_data_get_obj(source_data, "private_settings");
  1487. if (!source->private_settings)
  1488. source->private_settings = obs_data_create();
  1489. if (filters) {
  1490. size_t count = obs_data_array_count(filters);
  1491. for (size_t i = 0; i < count; i++) {
  1492. obs_data_t *filter_data =
  1493. obs_data_array_item(filters, i);
  1494. obs_source_t *filter =
  1495. obs_load_source_type(filter_data);
  1496. if (filter) {
  1497. obs_source_filter_add(source, filter);
  1498. obs_source_release(filter);
  1499. }
  1500. obs_data_release(filter_data);
  1501. }
  1502. obs_data_array_release(filters);
  1503. }
  1504. obs_data_release(settings);
  1505. return source;
  1506. }
  1507. obs_source_t *obs_load_source(obs_data_t *source_data)
  1508. {
  1509. return obs_load_source_type(source_data);
  1510. }
  1511. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1512. void *private_data)
  1513. {
  1514. if (!obs)
  1515. return;
  1516. struct obs_core_data *data = &obs->data;
  1517. DARRAY(obs_source_t *) sources;
  1518. size_t count;
  1519. size_t i;
  1520. da_init(sources);
  1521. count = obs_data_array_count(array);
  1522. da_reserve(sources, count);
  1523. pthread_mutex_lock(&data->sources_mutex);
  1524. for (i = 0; i < count; i++) {
  1525. obs_data_t *source_data = obs_data_array_item(array, i);
  1526. obs_source_t *source = obs_load_source(source_data);
  1527. da_push_back(sources, &source);
  1528. obs_data_release(source_data);
  1529. }
  1530. /* tell sources that we want to load */
  1531. for (i = 0; i < sources.num; i++) {
  1532. obs_source_t *source = sources.array[i];
  1533. obs_data_t *source_data = obs_data_array_item(array, i);
  1534. if (source) {
  1535. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1536. obs_transition_load(source, source_data);
  1537. obs_source_load(source);
  1538. for (size_t i = source->filters.num; i > 0; i--) {
  1539. obs_source_t *filter =
  1540. source->filters.array[i - 1];
  1541. obs_source_load(filter);
  1542. }
  1543. if (cb)
  1544. cb(private_data, source);
  1545. }
  1546. obs_data_release(source_data);
  1547. }
  1548. for (i = 0; i < sources.num; i++)
  1549. obs_source_release(sources.array[i]);
  1550. pthread_mutex_unlock(&data->sources_mutex);
  1551. da_free(sources);
  1552. }
  1553. obs_data_t *obs_save_source(obs_source_t *source)
  1554. {
  1555. obs_data_array_t *filters = obs_data_array_create();
  1556. obs_data_t *source_data = obs_data_create();
  1557. obs_data_t *settings = obs_source_get_settings(source);
  1558. obs_data_t *hotkey_data = source->context.hotkey_data;
  1559. obs_data_t *hotkeys;
  1560. float volume = obs_source_get_volume(source);
  1561. float balance = obs_source_get_balance_value(source);
  1562. uint32_t mixers = obs_source_get_audio_mixers(source);
  1563. int64_t sync = obs_source_get_sync_offset(source);
  1564. uint32_t flags = obs_source_get_flags(source);
  1565. const char *name = obs_source_get_name(source);
  1566. const char *id = obs_source_get_id(source);
  1567. bool enabled = obs_source_enabled(source);
  1568. bool muted = obs_source_muted(source);
  1569. bool push_to_mute = obs_source_push_to_mute_enabled(source);
  1570. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1571. bool push_to_talk = obs_source_push_to_talk_enabled(source);
  1572. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1573. int m_type = (int)obs_source_get_monitoring_type(source);
  1574. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1575. int di_order = (int)obs_source_get_deinterlace_field_order(source);
  1576. obs_source_save(source);
  1577. hotkeys = obs_hotkeys_save_source(source);
  1578. if (hotkeys) {
  1579. obs_data_release(hotkey_data);
  1580. source->context.hotkey_data = hotkeys;
  1581. hotkey_data = hotkeys;
  1582. }
  1583. obs_data_set_int(source_data, "prev_ver", LIBOBS_API_VER);
  1584. obs_data_set_string(source_data, "name", name);
  1585. obs_data_set_string(source_data, "id", id);
  1586. obs_data_set_obj(source_data, "settings", settings);
  1587. obs_data_set_int(source_data, "mixers", mixers);
  1588. obs_data_set_int(source_data, "sync", sync);
  1589. obs_data_set_int(source_data, "flags", flags);
  1590. obs_data_set_double(source_data, "volume", volume);
  1591. obs_data_set_double(source_data, "balance", balance);
  1592. obs_data_set_bool(source_data, "enabled", enabled);
  1593. obs_data_set_bool(source_data, "muted", muted);
  1594. obs_data_set_bool(source_data, "push-to-mute", push_to_mute);
  1595. obs_data_set_int(source_data, "push-to-mute-delay", ptm_delay);
  1596. obs_data_set_bool(source_data, "push-to-talk", push_to_talk);
  1597. obs_data_set_int(source_data, "push-to-talk-delay", ptt_delay);
  1598. obs_data_set_obj(source_data, "hotkeys", hotkey_data);
  1599. obs_data_set_int(source_data, "deinterlace_mode", di_mode);
  1600. obs_data_set_int(source_data, "deinterlace_field_order", di_order);
  1601. obs_data_set_int(source_data, "monitoring_type", m_type);
  1602. obs_data_set_obj(source_data, "private_settings",
  1603. source->private_settings);
  1604. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1605. obs_transition_save(source, source_data);
  1606. pthread_mutex_lock(&source->filter_mutex);
  1607. if (source->filters.num) {
  1608. for (size_t i = source->filters.num; i > 0; i--) {
  1609. obs_source_t *filter = source->filters.array[i - 1];
  1610. obs_data_t *filter_data = obs_save_source(filter);
  1611. obs_data_array_push_back(filters, filter_data);
  1612. obs_data_release(filter_data);
  1613. }
  1614. obs_data_set_array(source_data, "filters", filters);
  1615. }
  1616. pthread_mutex_unlock(&source->filter_mutex);
  1617. obs_data_release(settings);
  1618. obs_data_array_release(filters);
  1619. return source_data;
  1620. }
  1621. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1622. void *data_)
  1623. {
  1624. if (!obs)
  1625. return NULL;
  1626. struct obs_core_data *data = &obs->data;
  1627. obs_data_array_t *array;
  1628. obs_source_t *source;
  1629. array = obs_data_array_create();
  1630. pthread_mutex_lock(&data->sources_mutex);
  1631. source = data->first_source;
  1632. while (source) {
  1633. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1634. !source->context.private && cb(data_, source)) {
  1635. obs_data_t *source_data = obs_save_source(source);
  1636. obs_data_array_push_back(array, source_data);
  1637. obs_data_release(source_data);
  1638. }
  1639. source = (obs_source_t *)source->context.next;
  1640. }
  1641. pthread_mutex_unlock(&data->sources_mutex);
  1642. return array;
  1643. }
  1644. static bool save_source_filter(void *data, obs_source_t *source)
  1645. {
  1646. UNUSED_PARAMETER(data);
  1647. UNUSED_PARAMETER(source);
  1648. return true;
  1649. }
  1650. obs_data_array_t *obs_save_sources(void)
  1651. {
  1652. return obs_save_sources_filtered(save_source_filter, NULL);
  1653. }
  1654. /* ensures that names are never blank */
  1655. static inline char *dup_name(const char *name, bool private)
  1656. {
  1657. if (private && !name)
  1658. return NULL;
  1659. if (!name || !*name) {
  1660. struct dstr unnamed = {0};
  1661. dstr_printf(&unnamed, "__unnamed%04lld",
  1662. obs->data.unnamed_index++);
  1663. return unnamed.array;
  1664. } else {
  1665. return bstrdup(name);
  1666. }
  1667. }
  1668. static inline bool obs_context_data_init_wrap(struct obs_context_data *context,
  1669. enum obs_obj_type type,
  1670. obs_data_t *settings,
  1671. const char *name,
  1672. obs_data_t *hotkey_data,
  1673. bool private)
  1674. {
  1675. assert(context);
  1676. memset(context, 0, sizeof(*context));
  1677. context->private = private;
  1678. context->type = type;
  1679. pthread_mutex_init_value(&context->rename_cache_mutex);
  1680. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1681. return false;
  1682. context->signals = signal_handler_create();
  1683. if (!context->signals)
  1684. return false;
  1685. context->procs = proc_handler_create();
  1686. if (!context->procs)
  1687. return false;
  1688. context->name = dup_name(name, private);
  1689. context->settings = obs_data_newref(settings);
  1690. context->hotkey_data = obs_data_newref(hotkey_data);
  1691. return true;
  1692. }
  1693. bool obs_context_data_init(struct obs_context_data *context,
  1694. enum obs_obj_type type, obs_data_t *settings,
  1695. const char *name, obs_data_t *hotkey_data,
  1696. bool private)
  1697. {
  1698. if (obs_context_data_init_wrap(context, type, settings, name,
  1699. hotkey_data, private)) {
  1700. return true;
  1701. } else {
  1702. obs_context_data_free(context);
  1703. return false;
  1704. }
  1705. }
  1706. void obs_context_data_free(struct obs_context_data *context)
  1707. {
  1708. obs_hotkeys_context_release(context);
  1709. signal_handler_destroy(context->signals);
  1710. proc_handler_destroy(context->procs);
  1711. obs_data_release(context->settings);
  1712. obs_context_data_remove(context);
  1713. pthread_mutex_destroy(&context->rename_cache_mutex);
  1714. bfree(context->name);
  1715. for (size_t i = 0; i < context->rename_cache.num; i++)
  1716. bfree(context->rename_cache.array[i]);
  1717. da_free(context->rename_cache);
  1718. memset(context, 0, sizeof(*context));
  1719. }
  1720. void obs_context_data_insert(struct obs_context_data *context,
  1721. pthread_mutex_t *mutex, void *pfirst)
  1722. {
  1723. struct obs_context_data **first = pfirst;
  1724. assert(context);
  1725. assert(mutex);
  1726. assert(first);
  1727. context->mutex = mutex;
  1728. pthread_mutex_lock(mutex);
  1729. context->prev_next = first;
  1730. context->next = *first;
  1731. *first = context;
  1732. if (context->next)
  1733. context->next->prev_next = &context->next;
  1734. pthread_mutex_unlock(mutex);
  1735. }
  1736. void obs_context_data_remove(struct obs_context_data *context)
  1737. {
  1738. if (context && context->mutex) {
  1739. pthread_mutex_lock(context->mutex);
  1740. if (context->prev_next)
  1741. *context->prev_next = context->next;
  1742. if (context->next)
  1743. context->next->prev_next = context->prev_next;
  1744. pthread_mutex_unlock(context->mutex);
  1745. context->mutex = NULL;
  1746. }
  1747. }
  1748. void obs_context_data_setname(struct obs_context_data *context,
  1749. const char *name)
  1750. {
  1751. pthread_mutex_lock(&context->rename_cache_mutex);
  1752. if (context->name)
  1753. da_push_back(context->rename_cache, &context->name);
  1754. context->name = dup_name(name, context->private);
  1755. pthread_mutex_unlock(&context->rename_cache_mutex);
  1756. }
  1757. profiler_name_store_t *obs_get_profiler_name_store(void)
  1758. {
  1759. if (!obs)
  1760. return NULL;
  1761. return obs->name_store;
  1762. }
  1763. uint64_t obs_get_video_frame_time(void)
  1764. {
  1765. return obs ? obs->video.video_time : 0;
  1766. }
  1767. double obs_get_active_fps(void)
  1768. {
  1769. return obs ? obs->video.video_fps : 0.0;
  1770. }
  1771. uint64_t obs_get_average_frame_time_ns(void)
  1772. {
  1773. return obs ? obs->video.video_avg_frame_time_ns : 0;
  1774. }
  1775. uint64_t obs_get_frame_interval_ns(void)
  1776. {
  1777. return obs ? obs->video.video_frame_interval_ns : 0;
  1778. }
  1779. enum obs_obj_type obs_obj_get_type(void *obj)
  1780. {
  1781. struct obs_context_data *context = obj;
  1782. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  1783. }
  1784. const char *obs_obj_get_id(void *obj)
  1785. {
  1786. struct obs_context_data *context = obj;
  1787. if (!context)
  1788. return NULL;
  1789. switch (context->type) {
  1790. case OBS_OBJ_TYPE_SOURCE:
  1791. return ((obs_source_t *)obj)->info.id;
  1792. case OBS_OBJ_TYPE_OUTPUT:
  1793. return ((obs_output_t *)obj)->info.id;
  1794. case OBS_OBJ_TYPE_ENCODER:
  1795. return ((obs_encoder_t *)obj)->info.id;
  1796. case OBS_OBJ_TYPE_SERVICE:
  1797. return ((obs_service_t *)obj)->info.id;
  1798. default:;
  1799. }
  1800. return NULL;
  1801. }
  1802. bool obs_obj_invalid(void *obj)
  1803. {
  1804. struct obs_context_data *context = obj;
  1805. if (!context)
  1806. return true;
  1807. return !context->data;
  1808. }
  1809. void *obs_obj_get_data(void *obj)
  1810. {
  1811. struct obs_context_data *context = obj;
  1812. if (!context)
  1813. return NULL;
  1814. return context->data;
  1815. }
  1816. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  1817. {
  1818. if (!obs || !name || !id || !*name || !*id)
  1819. return false;
  1820. #if defined(_WIN32) || HAVE_PULSEAUDIO || defined(__APPLE__)
  1821. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  1822. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  1823. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1824. return true;
  1825. }
  1826. if (obs->audio.monitoring_device_name)
  1827. bfree(obs->audio.monitoring_device_name);
  1828. if (obs->audio.monitoring_device_id)
  1829. bfree(obs->audio.monitoring_device_id);
  1830. obs->audio.monitoring_device_name = bstrdup(name);
  1831. obs->audio.monitoring_device_id = bstrdup(id);
  1832. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  1833. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  1834. audio_monitor_reset(monitor);
  1835. }
  1836. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1837. return true;
  1838. #else
  1839. return false;
  1840. #endif
  1841. }
  1842. void obs_get_audio_monitoring_device(const char **name, const char **id)
  1843. {
  1844. if (!obs)
  1845. return;
  1846. if (name)
  1847. *name = obs->audio.monitoring_device_name;
  1848. if (id)
  1849. *id = obs->audio.monitoring_device_id;
  1850. }
  1851. void obs_add_tick_callback(void (*tick)(void *param, float seconds),
  1852. void *param)
  1853. {
  1854. if (!obs)
  1855. return;
  1856. struct tick_callback data = {tick, param};
  1857. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1858. da_insert(obs->data.tick_callbacks, 0, &data);
  1859. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1860. }
  1861. void obs_remove_tick_callback(void (*tick)(void *param, float seconds),
  1862. void *param)
  1863. {
  1864. if (!obs)
  1865. return;
  1866. struct tick_callback data = {tick, param};
  1867. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1868. da_erase_item(obs->data.tick_callbacks, &data);
  1869. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1870. }
  1871. void obs_add_main_render_callback(void (*draw)(void *param, uint32_t cx,
  1872. uint32_t cy),
  1873. void *param)
  1874. {
  1875. if (!obs)
  1876. return;
  1877. struct draw_callback data = {draw, param};
  1878. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1879. da_insert(obs->data.draw_callbacks, 0, &data);
  1880. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1881. }
  1882. void obs_remove_main_render_callback(void (*draw)(void *param, uint32_t cx,
  1883. uint32_t cy),
  1884. void *param)
  1885. {
  1886. if (!obs)
  1887. return;
  1888. struct draw_callback data = {draw, param};
  1889. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1890. da_erase_item(obs->data.draw_callbacks, &data);
  1891. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1892. }
  1893. uint32_t obs_get_total_frames(void)
  1894. {
  1895. return obs ? obs->video.total_frames : 0;
  1896. }
  1897. uint32_t obs_get_lagged_frames(void)
  1898. {
  1899. return obs ? obs->video.lagged_frames : 0;
  1900. }
  1901. void start_raw_video(video_t *v, const struct video_scale_info *conversion,
  1902. void (*callback)(void *param, struct video_data *frame),
  1903. void *param)
  1904. {
  1905. struct obs_core_video *video = &obs->video;
  1906. os_atomic_inc_long(&video->raw_active);
  1907. video_output_connect(v, conversion, callback, param);
  1908. }
  1909. void stop_raw_video(video_t *v,
  1910. void (*callback)(void *param, struct video_data *frame),
  1911. void *param)
  1912. {
  1913. struct obs_core_video *video = &obs->video;
  1914. os_atomic_dec_long(&video->raw_active);
  1915. video_output_disconnect(v, callback, param);
  1916. }
  1917. void obs_add_raw_video_callback(const struct video_scale_info *conversion,
  1918. void (*callback)(void *param,
  1919. struct video_data *frame),
  1920. void *param)
  1921. {
  1922. struct obs_core_video *video = &obs->video;
  1923. if (!obs)
  1924. return;
  1925. start_raw_video(video->video, conversion, callback, param);
  1926. }
  1927. void obs_remove_raw_video_callback(void (*callback)(void *param,
  1928. struct video_data *frame),
  1929. void *param)
  1930. {
  1931. struct obs_core_video *video = &obs->video;
  1932. if (!obs)
  1933. return;
  1934. stop_raw_video(video->video, callback, param);
  1935. }
  1936. void obs_apply_private_data(obs_data_t *settings)
  1937. {
  1938. if (!obs || !settings)
  1939. return;
  1940. obs_data_apply(obs->data.private_data, settings);
  1941. }
  1942. void obs_set_private_data(obs_data_t *settings)
  1943. {
  1944. if (!obs)
  1945. return;
  1946. obs_data_clear(obs->data.private_data);
  1947. if (settings)
  1948. obs_data_apply(obs->data.private_data, settings);
  1949. }
  1950. obs_data_t *obs_get_private_data(void)
  1951. {
  1952. if (!obs)
  1953. return NULL;
  1954. obs_data_t *private_data = obs->data.private_data;
  1955. obs_data_addref(private_data);
  1956. return private_data;
  1957. }
  1958. extern bool init_gpu_encoding(struct obs_core_video *video);
  1959. extern void stop_gpu_encoding_thread(struct obs_core_video *video);
  1960. extern void free_gpu_encoding(struct obs_core_video *video);
  1961. bool start_gpu_encode(obs_encoder_t *encoder)
  1962. {
  1963. struct obs_core_video *video = &obs->video;
  1964. bool success = true;
  1965. obs_enter_graphics();
  1966. pthread_mutex_lock(&video->gpu_encoder_mutex);
  1967. if (!video->gpu_encoders.num)
  1968. success = init_gpu_encoding(video);
  1969. if (success)
  1970. da_push_back(video->gpu_encoders, &encoder);
  1971. else
  1972. free_gpu_encoding(video);
  1973. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  1974. obs_leave_graphics();
  1975. if (success) {
  1976. os_atomic_inc_long(&video->gpu_encoder_active);
  1977. video_output_inc_texture_encoders(video->video);
  1978. }
  1979. return success;
  1980. }
  1981. void stop_gpu_encode(obs_encoder_t *encoder)
  1982. {
  1983. struct obs_core_video *video = &obs->video;
  1984. bool call_free = false;
  1985. os_atomic_dec_long(&video->gpu_encoder_active);
  1986. video_output_dec_texture_encoders(video->video);
  1987. pthread_mutex_lock(&video->gpu_encoder_mutex);
  1988. da_erase_item(video->gpu_encoders, &encoder);
  1989. if (!video->gpu_encoders.num)
  1990. call_free = true;
  1991. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  1992. os_event_wait(video->gpu_encode_inactive);
  1993. if (call_free) {
  1994. stop_gpu_encoding_thread(video);
  1995. obs_enter_graphics();
  1996. pthread_mutex_lock(&video->gpu_encoder_mutex);
  1997. free_gpu_encoding(video);
  1998. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  1999. obs_leave_graphics();
  2000. }
  2001. }
  2002. bool obs_video_active(void)
  2003. {
  2004. struct obs_core_video *video = &obs->video;
  2005. if (!obs)
  2006. return false;
  2007. return os_atomic_load_long(&video->raw_active) > 0 ||
  2008. os_atomic_load_long(&video->gpu_encoder_active) > 0;
  2009. }
  2010. bool obs_nv12_tex_active(void)
  2011. {
  2012. struct obs_core_video *video = &obs->video;
  2013. if (!obs)
  2014. return false;
  2015. return video->using_nv12_tex;
  2016. }