obs.c 59 KB

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