obs.c 60 KB

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