obs.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430
  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->output_format,
  907. ovi->range);
  908. blog(LOG_INFO, "---------------------------------");
  909. blog(LOG_INFO, "video settings reset:\n"
  910. "\tbase resolution: %dx%d\n"
  911. "\toutput resolution: %dx%d\n"
  912. "\tdownscale filter: %s\n"
  913. "\tfps: %d/%d\n"
  914. "\tformat: %s\n"
  915. "\tYUV mode: %s%s%s",
  916. ovi->base_width, ovi->base_height,
  917. ovi->output_width, ovi->output_height,
  918. scale_type_name,
  919. ovi->fps_num, ovi->fps_den,
  920. get_video_format_name(ovi->output_format),
  921. yuv ? yuv_format : "None",
  922. yuv ? "/" : "",
  923. yuv ? yuv_range : "");
  924. return obs_init_video(ovi);
  925. }
  926. bool obs_reset_audio(const struct obs_audio_info *oai)
  927. {
  928. struct audio_output_info ai;
  929. if (!obs) return false;
  930. /* don't allow changing of audio settings if active. */
  931. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  932. return false;
  933. obs_free_audio();
  934. if (!oai)
  935. return true;
  936. ai.name = "Audio";
  937. ai.samples_per_sec = oai->samples_per_sec;
  938. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  939. ai.speakers = oai->speakers;
  940. ai.input_callback = audio_callback;
  941. blog(LOG_INFO, "---------------------------------");
  942. blog(LOG_INFO, "audio settings reset:\n"
  943. "\tsamples per sec: %d\n"
  944. "\tspeakers: %d",
  945. (int)ai.samples_per_sec,
  946. (int)ai.speakers);
  947. return obs_init_audio(&ai);
  948. }
  949. bool obs_get_video_info(struct obs_video_info *ovi)
  950. {
  951. struct obs_core_video *video = &obs->video;
  952. if (!obs || !video->graphics)
  953. return false;
  954. *ovi = video->ovi;
  955. return true;
  956. }
  957. bool obs_get_audio_info(struct obs_audio_info *oai)
  958. {
  959. struct obs_core_audio *audio = &obs->audio;
  960. const struct audio_output_info *info;
  961. if (!obs || !oai || !audio->audio)
  962. return false;
  963. info = audio_output_get_info(audio->audio);
  964. oai->samples_per_sec = info->samples_per_sec;
  965. oai->speakers = info->speakers;
  966. return true;
  967. }
  968. bool obs_enum_source_types(size_t idx, const char **id)
  969. {
  970. if (!obs) return false;
  971. if (idx >= obs->source_types.num)
  972. return false;
  973. *id = obs->source_types.array[idx].id;
  974. return true;
  975. }
  976. bool obs_enum_input_types(size_t idx, const char **id)
  977. {
  978. if (!obs) return false;
  979. if (idx >= obs->input_types.num)
  980. return false;
  981. *id = obs->input_types.array[idx].id;
  982. return true;
  983. }
  984. bool obs_enum_filter_types(size_t idx, const char **id)
  985. {
  986. if (!obs) return false;
  987. if (idx >= obs->filter_types.num)
  988. return false;
  989. *id = obs->filter_types.array[idx].id;
  990. return true;
  991. }
  992. bool obs_enum_transition_types(size_t idx, const char **id)
  993. {
  994. if (!obs) return false;
  995. if (idx >= obs->transition_types.num)
  996. return false;
  997. *id = obs->transition_types.array[idx].id;
  998. return true;
  999. }
  1000. bool obs_enum_output_types(size_t idx, const char **id)
  1001. {
  1002. if (!obs) return false;
  1003. if (idx >= obs->output_types.num)
  1004. return false;
  1005. *id = obs->output_types.array[idx].id;
  1006. return true;
  1007. }
  1008. bool obs_enum_encoder_types(size_t idx, const char **id)
  1009. {
  1010. if (!obs) return false;
  1011. if (idx >= obs->encoder_types.num)
  1012. return false;
  1013. *id = obs->encoder_types.array[idx].id;
  1014. return true;
  1015. }
  1016. bool obs_enum_service_types(size_t idx, const char **id)
  1017. {
  1018. if (!obs) return false;
  1019. if (idx >= obs->service_types.num)
  1020. return false;
  1021. *id = obs->service_types.array[idx].id;
  1022. return true;
  1023. }
  1024. void obs_enter_graphics(void)
  1025. {
  1026. if (obs && obs->video.graphics)
  1027. gs_enter_context(obs->video.graphics);
  1028. }
  1029. void obs_leave_graphics(void)
  1030. {
  1031. if (obs && obs->video.graphics)
  1032. gs_leave_context();
  1033. }
  1034. audio_t *obs_get_audio(void)
  1035. {
  1036. return (obs != NULL) ? obs->audio.audio : NULL;
  1037. }
  1038. video_t *obs_get_video(void)
  1039. {
  1040. return (obs != NULL) ? obs->video.video : NULL;
  1041. }
  1042. /* TODO: optimize this later so it's not just O(N) string lookups */
  1043. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  1044. const char *task, const char *target)
  1045. {
  1046. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  1047. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  1048. if (strcmp(callback->id, id) == 0 &&
  1049. strcmp(callback->task, task) == 0 &&
  1050. strcmp(callback->target, target) == 0)
  1051. return callback;
  1052. }
  1053. return NULL;
  1054. }
  1055. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  1056. const char *task, const char *target)
  1057. {
  1058. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  1059. struct obs_modeless_ui *callback;
  1060. callback = obs->modeless_ui_callbacks.array+i;
  1061. if (strcmp(callback->id, id) == 0 &&
  1062. strcmp(callback->task, task) == 0 &&
  1063. strcmp(callback->target, target) == 0)
  1064. return callback;
  1065. }
  1066. return NULL;
  1067. }
  1068. int obs_exec_ui(const char *name, const char *task, const char *target,
  1069. void *data, void *ui_data)
  1070. {
  1071. struct obs_modal_ui *callback;
  1072. int errorcode = OBS_UI_NOTFOUND;
  1073. if (!obs) return errorcode;
  1074. callback = get_modal_ui_callback(name, task, target);
  1075. if (callback) {
  1076. bool success = callback->exec(data, ui_data);
  1077. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  1078. }
  1079. return errorcode;
  1080. }
  1081. void *obs_create_ui(const char *name, const char *task, const char *target,
  1082. void *data, void *ui_data)
  1083. {
  1084. struct obs_modeless_ui *callback;
  1085. if (!obs) return NULL;
  1086. callback = get_modeless_ui_callback(name, task, target);
  1087. return callback ? callback->create(data, ui_data) : NULL;
  1088. }
  1089. obs_source_t *obs_get_output_source(uint32_t channel)
  1090. {
  1091. if (!obs) return NULL;
  1092. return obs_view_get_source(&obs->data.main_view, channel);
  1093. }
  1094. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  1095. {
  1096. assert(channel < MAX_CHANNELS);
  1097. if (!obs) return;
  1098. if (channel >= MAX_CHANNELS) return;
  1099. struct obs_source *prev_source;
  1100. struct obs_view *view = &obs->data.main_view;
  1101. struct calldata params = {0};
  1102. pthread_mutex_lock(&view->channels_mutex);
  1103. obs_source_addref(source);
  1104. prev_source = view->channels[channel];
  1105. calldata_set_int(&params, "channel", channel);
  1106. calldata_set_ptr(&params, "prev_source", prev_source);
  1107. calldata_set_ptr(&params, "source", source);
  1108. signal_handler_signal(obs->signals, "channel_change", &params);
  1109. calldata_get_ptr(&params, "source", &source);
  1110. calldata_free(&params);
  1111. view->channels[channel] = source;
  1112. pthread_mutex_unlock(&view->channels_mutex);
  1113. if (source)
  1114. obs_source_activate(source, MAIN_VIEW);
  1115. if (prev_source) {
  1116. obs_source_deactivate(prev_source, MAIN_VIEW);
  1117. obs_source_release(prev_source);
  1118. }
  1119. }
  1120. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  1121. {
  1122. obs_source_t *source;
  1123. if (!obs) return;
  1124. pthread_mutex_lock(&obs->data.sources_mutex);
  1125. source = obs->data.first_source;
  1126. while (source) {
  1127. obs_source_t *next_source =
  1128. (obs_source_t*)source->context.next;
  1129. if (source->info.id == group_info.id &&
  1130. !enum_proc(param, source)) {
  1131. break;
  1132. } else if (source->info.type == OBS_SOURCE_TYPE_INPUT &&
  1133. !source->context.private &&
  1134. !enum_proc(param, source)) {
  1135. break;
  1136. }
  1137. source = next_source;
  1138. }
  1139. pthread_mutex_unlock(&obs->data.sources_mutex);
  1140. }
  1141. void obs_enum_scenes(bool (*enum_proc)(void*, obs_source_t*), void *param)
  1142. {
  1143. obs_source_t *source;
  1144. if (!obs) return;
  1145. pthread_mutex_lock(&obs->data.sources_mutex);
  1146. source = obs->data.first_source;
  1147. while (source) {
  1148. obs_source_t *next_source =
  1149. (obs_source_t*)source->context.next;
  1150. if (source->info.type == OBS_SOURCE_TYPE_SCENE &&
  1151. !source->context.private &&
  1152. !enum_proc(param, source)) {
  1153. break;
  1154. }
  1155. source = next_source;
  1156. }
  1157. pthread_mutex_unlock(&obs->data.sources_mutex);
  1158. }
  1159. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  1160. void *param)
  1161. {
  1162. struct obs_context_data **start = pstart, *context;
  1163. bool (*enum_proc)(void*, void*) = proc;
  1164. assert(start);
  1165. assert(mutex);
  1166. assert(enum_proc);
  1167. pthread_mutex_lock(mutex);
  1168. context = *start;
  1169. while (context) {
  1170. if (!enum_proc(param, context))
  1171. break;
  1172. context = context->next;
  1173. }
  1174. pthread_mutex_unlock(mutex);
  1175. }
  1176. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  1177. {
  1178. if (!obs) return;
  1179. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  1180. enum_proc, param);
  1181. }
  1182. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  1183. {
  1184. if (!obs) return;
  1185. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  1186. enum_proc, param);
  1187. }
  1188. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  1189. {
  1190. if (!obs) return;
  1191. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  1192. enum_proc, param);
  1193. }
  1194. static inline void *get_context_by_name(void *vfirst, const char *name,
  1195. pthread_mutex_t *mutex, void *(*addref)(void*))
  1196. {
  1197. struct obs_context_data **first = vfirst;
  1198. struct obs_context_data *context;
  1199. pthread_mutex_lock(mutex);
  1200. context = *first;
  1201. while (context) {
  1202. if (!context->private && strcmp(context->name, name) == 0) {
  1203. context = addref(context);
  1204. break;
  1205. }
  1206. context = context->next;
  1207. }
  1208. pthread_mutex_unlock(mutex);
  1209. return context;
  1210. }
  1211. static inline void *obs_source_addref_safe_(void *ref)
  1212. {
  1213. return obs_source_get_ref(ref);
  1214. }
  1215. static inline void *obs_output_addref_safe_(void *ref)
  1216. {
  1217. return obs_output_get_ref(ref);
  1218. }
  1219. static inline void *obs_encoder_addref_safe_(void *ref)
  1220. {
  1221. return obs_encoder_get_ref(ref);
  1222. }
  1223. static inline void *obs_service_addref_safe_(void *ref)
  1224. {
  1225. return obs_service_get_ref(ref);
  1226. }
  1227. static inline void *obs_id_(void *data)
  1228. {
  1229. return data;
  1230. }
  1231. obs_source_t *obs_get_source_by_name(const char *name)
  1232. {
  1233. if (!obs) return NULL;
  1234. return get_context_by_name(&obs->data.first_source, name,
  1235. &obs->data.sources_mutex, obs_source_addref_safe_);
  1236. }
  1237. obs_output_t *obs_get_output_by_name(const char *name)
  1238. {
  1239. if (!obs) return NULL;
  1240. return get_context_by_name(&obs->data.first_output, name,
  1241. &obs->data.outputs_mutex, obs_output_addref_safe_);
  1242. }
  1243. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1244. {
  1245. if (!obs) return NULL;
  1246. return get_context_by_name(&obs->data.first_encoder, name,
  1247. &obs->data.encoders_mutex, obs_encoder_addref_safe_);
  1248. }
  1249. obs_service_t *obs_get_service_by_name(const char *name)
  1250. {
  1251. if (!obs) return NULL;
  1252. return get_context_by_name(&obs->data.first_service, name,
  1253. &obs->data.services_mutex, obs_service_addref_safe_);
  1254. }
  1255. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1256. {
  1257. if (!obs) return NULL;
  1258. switch (effect) {
  1259. case OBS_EFFECT_DEFAULT:
  1260. return obs->video.default_effect;
  1261. case OBS_EFFECT_DEFAULT_RECT:
  1262. return obs->video.default_rect_effect;
  1263. case OBS_EFFECT_OPAQUE:
  1264. return obs->video.opaque_effect;
  1265. case OBS_EFFECT_SOLID:
  1266. return obs->video.solid_effect;
  1267. case OBS_EFFECT_REPEAT:
  1268. return obs->video.repeat_effect;
  1269. case OBS_EFFECT_BICUBIC:
  1270. return obs->video.bicubic_effect;
  1271. case OBS_EFFECT_LANCZOS:
  1272. return obs->video.lanczos_effect;
  1273. case OBS_EFFECT_AREA:
  1274. return obs->video.area_effect;
  1275. case OBS_EFFECT_BILINEAR_LOWRES:
  1276. return obs->video.bilinear_lowres_effect;
  1277. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1278. return obs->video.premultiplied_alpha_effect;
  1279. }
  1280. return NULL;
  1281. }
  1282. /* DEPRECATED */
  1283. gs_effect_t *obs_get_default_rect_effect(void)
  1284. {
  1285. if (!obs) return NULL;
  1286. return obs->video.default_rect_effect;
  1287. }
  1288. signal_handler_t *obs_get_signal_handler(void)
  1289. {
  1290. if (!obs) return NULL;
  1291. return obs->signals;
  1292. }
  1293. proc_handler_t *obs_get_proc_handler(void)
  1294. {
  1295. if (!obs) return NULL;
  1296. return obs->procs;
  1297. }
  1298. void obs_render_main_view(void)
  1299. {
  1300. if (!obs) return;
  1301. obs_view_render(&obs->data.main_view);
  1302. }
  1303. void obs_render_main_texture(void)
  1304. {
  1305. struct obs_core_video *video = &obs->video;
  1306. gs_texture_t *tex;
  1307. gs_effect_t *effect;
  1308. gs_eparam_t *param;
  1309. int last_tex;
  1310. if (!obs) return;
  1311. last_tex = video->cur_texture == 0
  1312. ? NUM_TEXTURES - 1
  1313. : video->cur_texture - 1;
  1314. if (!video->textures_rendered[last_tex])
  1315. return;
  1316. tex = video->render_textures[last_tex];
  1317. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1318. param = gs_effect_get_param_by_name(effect, "image");
  1319. gs_effect_set_texture(param, tex);
  1320. gs_blend_state_push();
  1321. gs_blend_function(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1322. while (gs_effect_loop(effect, "Draw"))
  1323. gs_draw_sprite(tex, 0, 0, 0);
  1324. gs_blend_state_pop();
  1325. }
  1326. gs_texture_t *obs_get_main_texture(void)
  1327. {
  1328. struct obs_core_video *video = &obs->video;
  1329. int last_tex;
  1330. if (!obs) return NULL;
  1331. last_tex = video->cur_texture == 0
  1332. ? NUM_TEXTURES - 1
  1333. : video->cur_texture - 1;
  1334. if (!video->textures_rendered[last_tex])
  1335. return NULL;
  1336. return video->render_textures[last_tex];
  1337. }
  1338. void obs_set_master_volume(float volume)
  1339. {
  1340. struct calldata data = {0};
  1341. if (!obs) return;
  1342. calldata_set_float(&data, "volume", volume);
  1343. signal_handler_signal(obs->signals, "master_volume", &data);
  1344. volume = (float)calldata_float(&data, "volume");
  1345. calldata_free(&data);
  1346. obs->audio.user_volume = volume;
  1347. }
  1348. float obs_get_master_volume(void)
  1349. {
  1350. return obs ? obs->audio.user_volume : 0.0f;
  1351. }
  1352. static obs_source_t *obs_load_source_type(obs_data_t *source_data)
  1353. {
  1354. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1355. obs_source_t *source;
  1356. const char *name = obs_data_get_string(source_data, "name");
  1357. const char *id = obs_data_get_string(source_data, "id");
  1358. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1359. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1360. double volume;
  1361. double balance;
  1362. int64_t sync;
  1363. uint32_t flags;
  1364. uint32_t mixers;
  1365. int di_order;
  1366. int di_mode;
  1367. int monitoring_type;
  1368. source = obs_source_create(id, name, settings, hotkeys);
  1369. obs_data_release(hotkeys);
  1370. obs_data_set_default_double(source_data, "volume", 1.0);
  1371. volume = obs_data_get_double(source_data, "volume");
  1372. obs_source_set_volume(source, (float)volume);
  1373. obs_data_set_default_double(source_data, "balance", 0.5);
  1374. balance = obs_data_get_double(source_data, "balance");
  1375. obs_source_set_balance_value(source, (float)balance);
  1376. sync = obs_data_get_int(source_data, "sync");
  1377. obs_source_set_sync_offset(source, sync);
  1378. obs_data_set_default_int(source_data, "mixers", 0xF);
  1379. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1380. obs_source_set_audio_mixers(source, mixers);
  1381. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1382. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1383. obs_source_set_flags(source, flags);
  1384. obs_data_set_default_bool(source_data, "enabled", true);
  1385. obs_source_set_enabled(source,
  1386. obs_data_get_bool(source_data, "enabled"));
  1387. obs_data_set_default_bool(source_data, "muted", false);
  1388. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1389. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1390. obs_source_enable_push_to_mute(source,
  1391. obs_data_get_bool(source_data, "push-to-mute"));
  1392. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1393. obs_source_set_push_to_mute_delay(source,
  1394. obs_data_get_int(source_data, "push-to-mute-delay"));
  1395. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1396. obs_source_enable_push_to_talk(source,
  1397. obs_data_get_bool(source_data, "push-to-talk"));
  1398. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1399. obs_source_set_push_to_talk_delay(source,
  1400. obs_data_get_int(source_data, "push-to-talk-delay"));
  1401. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1402. obs_source_set_deinterlace_mode(source,
  1403. (enum obs_deinterlace_mode)di_mode);
  1404. di_order = (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1405. obs_source_set_deinterlace_field_order(source,
  1406. (enum obs_deinterlace_field_order)di_order);
  1407. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1408. obs_source_set_monitoring_type(source,
  1409. (enum obs_monitoring_type)monitoring_type);
  1410. obs_data_release(source->private_settings);
  1411. source->private_settings =
  1412. obs_data_get_obj(source_data, "private_settings");
  1413. if (!source->private_settings)
  1414. source->private_settings = obs_data_create();
  1415. if (filters) {
  1416. size_t count = obs_data_array_count(filters);
  1417. for (size_t i = 0; i < count; i++) {
  1418. obs_data_t *filter_data =
  1419. obs_data_array_item(filters, i);
  1420. obs_source_t *filter = obs_load_source_type(
  1421. filter_data);
  1422. if (filter) {
  1423. obs_source_filter_add(source, filter);
  1424. obs_source_release(filter);
  1425. }
  1426. obs_data_release(filter_data);
  1427. }
  1428. obs_data_array_release(filters);
  1429. }
  1430. obs_data_release(settings);
  1431. return source;
  1432. }
  1433. obs_source_t *obs_load_source(obs_data_t *source_data)
  1434. {
  1435. return obs_load_source_type(source_data);
  1436. }
  1437. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1438. void *private_data)
  1439. {
  1440. if (!obs) return;
  1441. struct obs_core_data *data = &obs->data;
  1442. DARRAY(obs_source_t*) sources;
  1443. size_t count;
  1444. size_t i;
  1445. da_init(sources);
  1446. count = obs_data_array_count(array);
  1447. da_reserve(sources, count);
  1448. pthread_mutex_lock(&data->sources_mutex);
  1449. for (i = 0; i < count; i++) {
  1450. obs_data_t *source_data = obs_data_array_item(array, i);
  1451. obs_source_t *source = obs_load_source(source_data);
  1452. da_push_back(sources, &source);
  1453. obs_data_release(source_data);
  1454. }
  1455. /* tell sources that we want to load */
  1456. for (i = 0; i < sources.num; i++) {
  1457. obs_source_t *source = sources.array[i];
  1458. obs_data_t *source_data = obs_data_array_item(array, i);
  1459. if (source) {
  1460. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1461. obs_transition_load(source, source_data);
  1462. obs_source_load(source);
  1463. for (size_t i = source->filters.num; i > 0; i--) {
  1464. obs_source_t *filter =
  1465. source->filters.array[i - 1];
  1466. obs_source_load(filter);
  1467. }
  1468. if (cb)
  1469. cb(private_data, source);
  1470. }
  1471. obs_data_release(source_data);
  1472. }
  1473. for (i = 0; i < sources.num; i++)
  1474. obs_source_release(sources.array[i]);
  1475. pthread_mutex_unlock(&data->sources_mutex);
  1476. da_free(sources);
  1477. }
  1478. obs_data_t *obs_save_source(obs_source_t *source)
  1479. {
  1480. obs_data_array_t *filters = obs_data_array_create();
  1481. obs_data_t *source_data = obs_data_create();
  1482. obs_data_t *settings = obs_source_get_settings(source);
  1483. obs_data_t *hotkey_data = source->context.hotkey_data;
  1484. obs_data_t *hotkeys;
  1485. float volume = obs_source_get_volume(source);
  1486. float balance = obs_source_get_balance_value(source);
  1487. uint32_t mixers = obs_source_get_audio_mixers(source);
  1488. int64_t sync = obs_source_get_sync_offset(source);
  1489. uint32_t flags = obs_source_get_flags(source);
  1490. const char *name = obs_source_get_name(source);
  1491. const char *id = obs_source_get_id(source);
  1492. bool enabled = obs_source_enabled(source);
  1493. bool muted = obs_source_muted(source);
  1494. bool push_to_mute= obs_source_push_to_mute_enabled(source);
  1495. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1496. bool push_to_talk= obs_source_push_to_talk_enabled(source);
  1497. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1498. int m_type = (int)obs_source_get_monitoring_type(source);
  1499. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1500. int di_order =
  1501. (int)obs_source_get_deinterlace_field_order(source);
  1502. obs_source_save(source);
  1503. hotkeys = obs_hotkeys_save_source(source);
  1504. if (hotkeys) {
  1505. obs_data_release(hotkey_data);
  1506. source->context.hotkey_data = hotkeys;
  1507. hotkey_data = hotkeys;
  1508. }
  1509. obs_data_set_string(source_data, "name", name);
  1510. obs_data_set_string(source_data, "id", id);
  1511. obs_data_set_obj (source_data, "settings", settings);
  1512. obs_data_set_int (source_data, "mixers", mixers);
  1513. obs_data_set_int (source_data, "sync", sync);
  1514. obs_data_set_int (source_data, "flags", flags);
  1515. obs_data_set_double(source_data, "volume", volume);
  1516. obs_data_set_double(source_data, "balance", balance);
  1517. obs_data_set_bool (source_data, "enabled", enabled);
  1518. obs_data_set_bool (source_data, "muted", muted);
  1519. obs_data_set_bool (source_data, "push-to-mute", push_to_mute);
  1520. obs_data_set_int (source_data, "push-to-mute-delay", ptm_delay);
  1521. obs_data_set_bool (source_data, "push-to-talk", push_to_talk);
  1522. obs_data_set_int (source_data, "push-to-talk-delay", ptt_delay);
  1523. obs_data_set_obj (source_data, "hotkeys", hotkey_data);
  1524. obs_data_set_int (source_data, "deinterlace_mode", di_mode);
  1525. obs_data_set_int (source_data, "deinterlace_field_order", di_order);
  1526. obs_data_set_int (source_data, "monitoring_type", m_type);
  1527. obs_data_set_obj(source_data, "private_settings",
  1528. source->private_settings);
  1529. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1530. obs_transition_save(source, source_data);
  1531. pthread_mutex_lock(&source->filter_mutex);
  1532. if (source->filters.num) {
  1533. for (size_t i = source->filters.num; i > 0; i--) {
  1534. obs_source_t *filter = source->filters.array[i - 1];
  1535. obs_data_t *filter_data = obs_save_source(filter);
  1536. obs_data_array_push_back(filters, filter_data);
  1537. obs_data_release(filter_data);
  1538. }
  1539. obs_data_set_array(source_data, "filters", filters);
  1540. }
  1541. pthread_mutex_unlock(&source->filter_mutex);
  1542. obs_data_release(settings);
  1543. obs_data_array_release(filters);
  1544. return source_data;
  1545. }
  1546. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1547. void *data_)
  1548. {
  1549. if (!obs) return NULL;
  1550. struct obs_core_data *data = &obs->data;
  1551. obs_data_array_t *array;
  1552. obs_source_t *source;
  1553. array = obs_data_array_create();
  1554. pthread_mutex_lock(&data->sources_mutex);
  1555. source = data->first_source;
  1556. while (source) {
  1557. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1558. !source->context.private && cb(data_, source)) {
  1559. obs_data_t *source_data = obs_save_source(source);
  1560. obs_data_array_push_back(array, source_data);
  1561. obs_data_release(source_data);
  1562. }
  1563. source = (obs_source_t*)source->context.next;
  1564. }
  1565. pthread_mutex_unlock(&data->sources_mutex);
  1566. return array;
  1567. }
  1568. static bool save_source_filter(void *data, obs_source_t *source)
  1569. {
  1570. UNUSED_PARAMETER(data);
  1571. UNUSED_PARAMETER(source);
  1572. return true;
  1573. }
  1574. obs_data_array_t *obs_save_sources(void)
  1575. {
  1576. return obs_save_sources_filtered(save_source_filter, NULL);
  1577. }
  1578. /* ensures that names are never blank */
  1579. static inline char *dup_name(const char *name, bool private)
  1580. {
  1581. if (private && !name)
  1582. return NULL;
  1583. if (!name || !*name) {
  1584. struct dstr unnamed = {0};
  1585. dstr_printf(&unnamed, "__unnamed%04lld",
  1586. obs->data.unnamed_index++);
  1587. return unnamed.array;
  1588. } else {
  1589. return bstrdup(name);
  1590. }
  1591. }
  1592. static inline bool obs_context_data_init_wrap(
  1593. struct obs_context_data *context,
  1594. enum obs_obj_type type,
  1595. obs_data_t *settings,
  1596. const char *name,
  1597. obs_data_t *hotkey_data,
  1598. bool private)
  1599. {
  1600. assert(context);
  1601. memset(context, 0, sizeof(*context));
  1602. context->private = private;
  1603. context->type = type;
  1604. pthread_mutex_init_value(&context->rename_cache_mutex);
  1605. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1606. return false;
  1607. context->signals = signal_handler_create();
  1608. if (!context->signals)
  1609. return false;
  1610. context->procs = proc_handler_create();
  1611. if (!context->procs)
  1612. return false;
  1613. context->name = dup_name(name, private);
  1614. context->settings = obs_data_newref(settings);
  1615. context->hotkey_data = obs_data_newref(hotkey_data);
  1616. return true;
  1617. }
  1618. bool obs_context_data_init(
  1619. struct obs_context_data *context,
  1620. enum obs_obj_type type,
  1621. obs_data_t *settings,
  1622. const char *name,
  1623. obs_data_t *hotkey_data,
  1624. bool private)
  1625. {
  1626. if (obs_context_data_init_wrap(context, type, settings, name,
  1627. hotkey_data, private)) {
  1628. return true;
  1629. } else {
  1630. obs_context_data_free(context);
  1631. return false;
  1632. }
  1633. }
  1634. void obs_context_data_free(struct obs_context_data *context)
  1635. {
  1636. obs_hotkeys_context_release(context);
  1637. signal_handler_destroy(context->signals);
  1638. proc_handler_destroy(context->procs);
  1639. obs_data_release(context->settings);
  1640. obs_context_data_remove(context);
  1641. pthread_mutex_destroy(&context->rename_cache_mutex);
  1642. bfree(context->name);
  1643. for (size_t i = 0; i < context->rename_cache.num; i++)
  1644. bfree(context->rename_cache.array[i]);
  1645. da_free(context->rename_cache);
  1646. memset(context, 0, sizeof(*context));
  1647. }
  1648. void obs_context_data_insert(struct obs_context_data *context,
  1649. pthread_mutex_t *mutex, void *pfirst)
  1650. {
  1651. struct obs_context_data **first = pfirst;
  1652. assert(context);
  1653. assert(mutex);
  1654. assert(first);
  1655. context->mutex = mutex;
  1656. pthread_mutex_lock(mutex);
  1657. context->prev_next = first;
  1658. context->next = *first;
  1659. *first = context;
  1660. if (context->next)
  1661. context->next->prev_next = &context->next;
  1662. pthread_mutex_unlock(mutex);
  1663. }
  1664. void obs_context_data_remove(struct obs_context_data *context)
  1665. {
  1666. if (context && context->mutex) {
  1667. pthread_mutex_lock(context->mutex);
  1668. if (context->prev_next)
  1669. *context->prev_next = context->next;
  1670. if (context->next)
  1671. context->next->prev_next = context->prev_next;
  1672. pthread_mutex_unlock(context->mutex);
  1673. context->mutex = NULL;
  1674. }
  1675. }
  1676. void obs_context_data_setname(struct obs_context_data *context,
  1677. const char *name)
  1678. {
  1679. pthread_mutex_lock(&context->rename_cache_mutex);
  1680. if (context->name)
  1681. da_push_back(context->rename_cache, &context->name);
  1682. context->name = dup_name(name, context->private);
  1683. pthread_mutex_unlock(&context->rename_cache_mutex);
  1684. }
  1685. profiler_name_store_t *obs_get_profiler_name_store(void)
  1686. {
  1687. if (!obs)
  1688. return NULL;
  1689. return obs->name_store;
  1690. }
  1691. uint64_t obs_get_video_frame_time(void)
  1692. {
  1693. return obs ? obs->video.video_time : 0;
  1694. }
  1695. double obs_get_active_fps(void)
  1696. {
  1697. return obs ? obs->video.video_fps : 0.0;
  1698. }
  1699. uint64_t obs_get_average_frame_time_ns(void)
  1700. {
  1701. return obs ? obs->video.video_avg_frame_time_ns : 0;
  1702. }
  1703. enum obs_obj_type obs_obj_get_type(void *obj)
  1704. {
  1705. struct obs_context_data *context = obj;
  1706. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  1707. }
  1708. const char *obs_obj_get_id(void *obj)
  1709. {
  1710. struct obs_context_data *context = obj;
  1711. if (!context)
  1712. return NULL;
  1713. switch (context->type) {
  1714. case OBS_OBJ_TYPE_SOURCE: return ((obs_source_t*)obj)->info.id;
  1715. case OBS_OBJ_TYPE_OUTPUT: return ((obs_output_t*)obj)->info.id;
  1716. case OBS_OBJ_TYPE_ENCODER: return ((obs_encoder_t*)obj)->info.id;
  1717. case OBS_OBJ_TYPE_SERVICE: return ((obs_service_t*)obj)->info.id;
  1718. default:;
  1719. }
  1720. return NULL;
  1721. }
  1722. bool obs_obj_invalid(void *obj)
  1723. {
  1724. struct obs_context_data *context = obj;
  1725. if (!context)
  1726. return true;
  1727. return !context->data;
  1728. }
  1729. void *obs_obj_get_data(void *obj)
  1730. {
  1731. struct obs_context_data *context = obj;
  1732. if (!context)
  1733. return NULL;
  1734. return context->data;
  1735. }
  1736. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  1737. {
  1738. if (!obs || !name || !id || !*name || !*id)
  1739. return false;
  1740. #if defined(_WIN32) || HAVE_PULSEAUDIO || defined(__APPLE__)
  1741. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  1742. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  1743. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1744. return true;
  1745. }
  1746. if (obs->audio.monitoring_device_name)
  1747. bfree(obs->audio.monitoring_device_name);
  1748. if (obs->audio.monitoring_device_id)
  1749. bfree(obs->audio.monitoring_device_id);
  1750. obs->audio.monitoring_device_name = bstrdup(name);
  1751. obs->audio.monitoring_device_id = bstrdup(id);
  1752. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  1753. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  1754. audio_monitor_reset(monitor);
  1755. }
  1756. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1757. return true;
  1758. #else
  1759. return false;
  1760. #endif
  1761. }
  1762. void obs_get_audio_monitoring_device(const char **name, const char **id)
  1763. {
  1764. if (!obs)
  1765. return;
  1766. if (name)
  1767. *name = obs->audio.monitoring_device_name;
  1768. if (id)
  1769. *id = obs->audio.monitoring_device_id;
  1770. }
  1771. void obs_add_tick_callback(
  1772. void (*tick)(void *param, float seconds),
  1773. void *param)
  1774. {
  1775. if (!obs)
  1776. return;
  1777. struct tick_callback data = {tick, param};
  1778. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1779. da_insert(obs->data.tick_callbacks, 0, &data);
  1780. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1781. }
  1782. void obs_remove_tick_callback(
  1783. void (*tick)(void *param, float seconds),
  1784. void *param)
  1785. {
  1786. if (!obs)
  1787. return;
  1788. struct tick_callback data = {tick, param};
  1789. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1790. da_erase_item(obs->data.tick_callbacks, &data);
  1791. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1792. }
  1793. void obs_add_main_render_callback(
  1794. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1795. void *param)
  1796. {
  1797. if (!obs)
  1798. return;
  1799. struct draw_callback data = {draw, param};
  1800. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1801. da_insert(obs->data.draw_callbacks, 0, &data);
  1802. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1803. }
  1804. void obs_remove_main_render_callback(
  1805. void (*draw)(void *param, uint32_t cx, uint32_t cy),
  1806. void *param)
  1807. {
  1808. if (!obs)
  1809. return;
  1810. struct draw_callback data = {draw, param};
  1811. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  1812. da_erase_item(obs->data.draw_callbacks, &data);
  1813. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  1814. }
  1815. uint32_t obs_get_total_frames(void)
  1816. {
  1817. return obs ? obs->video.total_frames : 0;
  1818. }
  1819. uint32_t obs_get_lagged_frames(void)
  1820. {
  1821. return obs ? obs->video.lagged_frames : 0;
  1822. }
  1823. void start_raw_video(video_t *v, const struct video_scale_info *conversion,
  1824. void (*callback)(void *param, struct video_data *frame),
  1825. void *param)
  1826. {
  1827. struct obs_core_video *video = &obs->video;
  1828. os_atomic_inc_long(&video->raw_active);
  1829. video_output_connect(v, conversion, callback, param);
  1830. }
  1831. void stop_raw_video(video_t *v,
  1832. void (*callback)(void *param, struct video_data *frame),
  1833. void *param)
  1834. {
  1835. struct obs_core_video *video = &obs->video;
  1836. os_atomic_dec_long(&video->raw_active);
  1837. video_output_disconnect(v, callback, param);
  1838. }
  1839. void obs_add_raw_video_callback(
  1840. const struct video_scale_info *conversion,
  1841. void (*callback)(void *param, struct video_data *frame),
  1842. void *param)
  1843. {
  1844. struct obs_core_video *video = &obs->video;
  1845. if (!obs)
  1846. return;
  1847. start_raw_video(video->video, conversion, callback, param);
  1848. }
  1849. void obs_remove_raw_video_callback(
  1850. void (*callback)(void *param, struct video_data *frame),
  1851. void *param)
  1852. {
  1853. struct obs_core_video *video = &obs->video;
  1854. if (!obs)
  1855. return;
  1856. stop_raw_video(video->video, callback, param);
  1857. }
  1858. void obs_apply_private_data(obs_data_t *settings)
  1859. {
  1860. if (!obs || !settings)
  1861. return;
  1862. obs_data_apply(obs->data.private_data, settings);
  1863. }
  1864. void obs_set_private_data(obs_data_t *settings)
  1865. {
  1866. if (!obs)
  1867. return;
  1868. obs_data_clear(obs->data.private_data);
  1869. if (settings)
  1870. obs_data_apply(obs->data.private_data, settings);
  1871. }
  1872. obs_data_t *obs_get_private_data(void)
  1873. {
  1874. if (!obs)
  1875. return NULL;
  1876. obs_data_t *private_data = obs->data.private_data;
  1877. obs_data_addref(private_data);
  1878. return private_data;
  1879. }
  1880. extern bool init_gpu_encoding(struct obs_core_video *video);
  1881. extern void stop_gpu_encoding_thread(struct obs_core_video *video);
  1882. extern void free_gpu_encoding(struct obs_core_video *video);
  1883. bool start_gpu_encode(obs_encoder_t *encoder)
  1884. {
  1885. struct obs_core_video *video = &obs->video;
  1886. bool success = true;
  1887. obs_enter_graphics();
  1888. pthread_mutex_lock(&video->gpu_encoder_mutex);
  1889. if (!video->gpu_encoders.num)
  1890. success = init_gpu_encoding(video);
  1891. if (success)
  1892. da_push_back(video->gpu_encoders, &encoder);
  1893. else
  1894. free_gpu_encoding(video);
  1895. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  1896. obs_leave_graphics();
  1897. if (success) {
  1898. os_atomic_inc_long(&video->gpu_encoder_active);
  1899. video_output_inc_texture_encoders(video->video);
  1900. }
  1901. return success;
  1902. }
  1903. void stop_gpu_encode(obs_encoder_t *encoder)
  1904. {
  1905. struct obs_core_video *video = &obs->video;
  1906. bool call_free = false;
  1907. os_atomic_dec_long(&video->gpu_encoder_active);
  1908. video_output_dec_texture_encoders(video->video);
  1909. pthread_mutex_lock(&video->gpu_encoder_mutex);
  1910. da_erase_item(video->gpu_encoders, &encoder);
  1911. if (!video->gpu_encoders.num)
  1912. call_free = true;
  1913. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  1914. os_event_wait(video->gpu_encode_inactive);
  1915. if (call_free) {
  1916. stop_gpu_encoding_thread(video);
  1917. obs_enter_graphics();
  1918. pthread_mutex_lock(&video->gpu_encoder_mutex);
  1919. free_gpu_encoding(video);
  1920. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  1921. obs_leave_graphics();
  1922. }
  1923. }
  1924. bool obs_video_active(void)
  1925. {
  1926. struct obs_core_video *video = &obs->video;
  1927. if (!obs)
  1928. return false;
  1929. return os_atomic_load_long(&video->raw_active) > 0 ||
  1930. os_atomic_load_long(&video->gpu_encoder_active) > 0;
  1931. }
  1932. bool obs_nv12_tex_active(void)
  1933. {
  1934. struct obs_core_video *video = &obs->video;
  1935. if (!obs)
  1936. return false;
  1937. return video->using_nv12_tex;
  1938. }