obs.c 60 KB

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