obs.c 65 KB

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