obs.c 60 KB

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