obs.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854
  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. static THREAD_LOCAL bool is_ui_thread = false;
  21. extern void add_default_module_paths(void);
  22. extern char *find_libobs_data_file(const char *file);
  23. static inline void make_video_info(struct video_output_info *vi,
  24. struct obs_video_info *ovi)
  25. {
  26. vi->name = "video";
  27. vi->format = ovi->output_format;
  28. vi->fps_num = ovi->fps_num;
  29. vi->fps_den = ovi->fps_den;
  30. vi->width = ovi->output_width;
  31. vi->height = ovi->output_height;
  32. vi->range = ovi->range;
  33. vi->colorspace = ovi->colorspace;
  34. vi->cache_size = 6;
  35. }
  36. static inline void calc_gpu_conversion_sizes(const struct obs_video_info *ovi)
  37. {
  38. struct obs_core_video *video = &obs->video;
  39. video->conversion_needed = false;
  40. video->conversion_techs[0] = NULL;
  41. video->conversion_techs[1] = NULL;
  42. video->conversion_techs[2] = NULL;
  43. video->conversion_width_i = 0.f;
  44. switch ((uint32_t)ovi->output_format) {
  45. case VIDEO_FORMAT_I420:
  46. video->conversion_needed = true;
  47. video->conversion_techs[0] = "Planar_Y";
  48. video->conversion_techs[1] = "Planar_U_Left";
  49. video->conversion_techs[2] = "Planar_V_Left";
  50. video->conversion_width_i = 1.f / (float)ovi->output_width;
  51. break;
  52. case VIDEO_FORMAT_NV12:
  53. video->conversion_needed = true;
  54. video->conversion_techs[0] = "NV12_Y";
  55. video->conversion_techs[1] = "NV12_UV";
  56. video->conversion_width_i = 1.f / (float)ovi->output_width;
  57. break;
  58. case VIDEO_FORMAT_I444:
  59. video->conversion_needed = true;
  60. video->conversion_techs[0] = "Planar_Y";
  61. video->conversion_techs[1] = "Planar_U";
  62. video->conversion_techs[2] = "Planar_V";
  63. break;
  64. }
  65. }
  66. static bool obs_init_gpu_conversion(struct obs_video_info *ovi)
  67. {
  68. struct obs_core_video *video = &obs->video;
  69. calc_gpu_conversion_sizes(ovi);
  70. video->using_nv12_tex = ovi->output_format == VIDEO_FORMAT_NV12
  71. ? gs_nv12_available()
  72. : false;
  73. if (!video->conversion_needed) {
  74. blog(LOG_INFO, "GPU conversion not available for format: %u",
  75. (unsigned int)ovi->output_format);
  76. video->gpu_conversion = false;
  77. video->using_nv12_tex = false;
  78. blog(LOG_INFO, "NV12 texture support not available");
  79. return true;
  80. }
  81. if (video->using_nv12_tex)
  82. blog(LOG_INFO, "NV12 texture support enabled");
  83. else
  84. blog(LOG_INFO, "NV12 texture support not available");
  85. video->convert_textures[0] = NULL;
  86. video->convert_textures[1] = NULL;
  87. video->convert_textures[2] = NULL;
  88. #ifdef _WIN32
  89. video->convert_textures_encode[0] = NULL;
  90. video->convert_textures_encode[1] = NULL;
  91. video->convert_textures_encode[2] = NULL;
  92. if (video->using_nv12_tex) {
  93. if (!gs_texture_create_nv12(
  94. &video->convert_textures_encode[0],
  95. &video->convert_textures_encode[1],
  96. ovi->output_width, ovi->output_height,
  97. GS_RENDER_TARGET | GS_SHARED_KM_TEX)) {
  98. return false;
  99. }
  100. }
  101. #endif
  102. bool success = true;
  103. const struct video_output_info *info =
  104. video_output_get_info(video->video);
  105. switch (info->format) {
  106. case VIDEO_FORMAT_I420:
  107. video->convert_textures[0] =
  108. gs_texture_create(ovi->output_width, ovi->output_height,
  109. GS_R8, 1, NULL, GS_RENDER_TARGET);
  110. video->convert_textures[1] = gs_texture_create(
  111. ovi->output_width / 2, ovi->output_height / 2, GS_R8, 1,
  112. NULL, GS_RENDER_TARGET);
  113. video->convert_textures[2] = gs_texture_create(
  114. ovi->output_width / 2, ovi->output_height / 2, GS_R8, 1,
  115. NULL, GS_RENDER_TARGET);
  116. if (!video->convert_textures[0] ||
  117. !video->convert_textures[1] || !video->convert_textures[2])
  118. success = false;
  119. break;
  120. case VIDEO_FORMAT_NV12:
  121. video->convert_textures[0] =
  122. gs_texture_create(ovi->output_width, ovi->output_height,
  123. GS_R8, 1, NULL, GS_RENDER_TARGET);
  124. video->convert_textures[1] = gs_texture_create(
  125. ovi->output_width / 2, ovi->output_height / 2, GS_R8G8,
  126. 1, NULL, GS_RENDER_TARGET);
  127. if (!video->convert_textures[0] || !video->convert_textures[1])
  128. success = false;
  129. break;
  130. case VIDEO_FORMAT_I444:
  131. video->convert_textures[0] =
  132. gs_texture_create(ovi->output_width, ovi->output_height,
  133. GS_R8, 1, NULL, GS_RENDER_TARGET);
  134. video->convert_textures[1] =
  135. gs_texture_create(ovi->output_width, ovi->output_height,
  136. GS_R8, 1, NULL, GS_RENDER_TARGET);
  137. video->convert_textures[2] =
  138. gs_texture_create(ovi->output_width, ovi->output_height,
  139. GS_R8, 1, NULL, GS_RENDER_TARGET);
  140. if (!video->convert_textures[0] ||
  141. !video->convert_textures[1] || !video->convert_textures[2])
  142. success = false;
  143. }
  144. if (!success) {
  145. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  146. if (video->convert_textures[c]) {
  147. gs_texture_destroy(video->convert_textures[c]);
  148. video->convert_textures[c] = NULL;
  149. }
  150. #ifdef _WIN32
  151. if (video->convert_textures_encode[c]) {
  152. gs_texture_destroy(
  153. video->convert_textures_encode[c]);
  154. video->convert_textures_encode[c] = NULL;
  155. }
  156. #endif
  157. }
  158. }
  159. return success;
  160. }
  161. static bool obs_init_gpu_copy_surfaces(struct obs_video_info *ovi, size_t i)
  162. {
  163. struct obs_core_video *video = &obs->video;
  164. const struct video_output_info *info =
  165. video_output_get_info(video->video);
  166. switch (info->format) {
  167. case VIDEO_FORMAT_I420:
  168. video->copy_surfaces[i][0] = gs_stagesurface_create(
  169. ovi->output_width, ovi->output_height, GS_R8);
  170. if (!video->copy_surfaces[i][0])
  171. return false;
  172. video->copy_surfaces[i][1] = gs_stagesurface_create(
  173. ovi->output_width / 2, ovi->output_height / 2, GS_R8);
  174. if (!video->copy_surfaces[i][1])
  175. return false;
  176. video->copy_surfaces[i][2] = gs_stagesurface_create(
  177. ovi->output_width / 2, ovi->output_height / 2, GS_R8);
  178. if (!video->copy_surfaces[i][2])
  179. return false;
  180. break;
  181. case VIDEO_FORMAT_NV12:
  182. video->copy_surfaces[i][0] = gs_stagesurface_create(
  183. ovi->output_width, ovi->output_height, GS_R8);
  184. if (!video->copy_surfaces[i][0])
  185. return false;
  186. video->copy_surfaces[i][1] = gs_stagesurface_create(
  187. ovi->output_width / 2, ovi->output_height / 2, GS_R8G8);
  188. if (!video->copy_surfaces[i][1])
  189. return false;
  190. break;
  191. case VIDEO_FORMAT_I444:
  192. video->copy_surfaces[i][0] = gs_stagesurface_create(
  193. ovi->output_width, ovi->output_height, GS_R8);
  194. if (!video->copy_surfaces[i][0])
  195. return false;
  196. video->copy_surfaces[i][1] = gs_stagesurface_create(
  197. ovi->output_width, ovi->output_height, GS_R8);
  198. if (!video->copy_surfaces[i][1])
  199. return false;
  200. video->copy_surfaces[i][2] = gs_stagesurface_create(
  201. ovi->output_width, ovi->output_height, GS_R8);
  202. if (!video->copy_surfaces[i][2])
  203. return false;
  204. break;
  205. default:
  206. break;
  207. }
  208. return true;
  209. }
  210. static bool obs_init_textures(struct obs_video_info *ovi)
  211. {
  212. struct obs_core_video *video = &obs->video;
  213. bool success = true;
  214. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  215. #ifdef _WIN32
  216. if (video->using_nv12_tex) {
  217. video->copy_surfaces_encode[i] =
  218. gs_stagesurface_create_nv12(ovi->output_width,
  219. ovi->output_height);
  220. if (!video->copy_surfaces_encode[i]) {
  221. success = false;
  222. break;
  223. }
  224. }
  225. #endif
  226. if (video->gpu_conversion) {
  227. if (!obs_init_gpu_copy_surfaces(ovi, i)) {
  228. success = false;
  229. break;
  230. }
  231. } else {
  232. video->copy_surfaces[i][0] = gs_stagesurface_create(
  233. ovi->output_width, ovi->output_height, GS_RGBA);
  234. if (!video->copy_surfaces[i][0]) {
  235. success = false;
  236. break;
  237. }
  238. }
  239. }
  240. enum gs_color_format format = GS_RGBA;
  241. enum gs_color_space space = GS_CS_SRGB;
  242. switch (ovi->colorspace) {
  243. case VIDEO_CS_2020_PQ:
  244. case VIDEO_CS_2020_HLG:
  245. format = GS_RGBA16F;
  246. space = GS_CS_709_EXTENDED;
  247. }
  248. video->render_texture = gs_texture_create(ovi->base_width,
  249. ovi->base_height, format, 1,
  250. NULL, GS_RENDER_TARGET);
  251. if (!video->render_texture)
  252. success = false;
  253. video->output_texture = gs_texture_create(ovi->output_width,
  254. ovi->output_height, format, 1,
  255. NULL, GS_RENDER_TARGET);
  256. if (!video->output_texture)
  257. success = false;
  258. if (success) {
  259. video->render_space = space;
  260. } else {
  261. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  262. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  263. if (video->copy_surfaces[i][c]) {
  264. gs_stagesurface_destroy(
  265. video->copy_surfaces[i][c]);
  266. video->copy_surfaces[i][c] = NULL;
  267. }
  268. }
  269. #ifdef _WIN32
  270. if (video->copy_surfaces_encode[i]) {
  271. gs_stagesurface_destroy(
  272. video->copy_surfaces_encode[i]);
  273. video->copy_surfaces_encode[i] = NULL;
  274. }
  275. #endif
  276. }
  277. if (video->render_texture) {
  278. gs_texture_destroy(video->render_texture);
  279. video->render_texture = NULL;
  280. }
  281. if (video->output_texture) {
  282. gs_texture_destroy(video->output_texture);
  283. video->output_texture = NULL;
  284. }
  285. }
  286. return success;
  287. }
  288. gs_effect_t *obs_load_effect(gs_effect_t **effect, const char *file)
  289. {
  290. if (!*effect) {
  291. char *filename = obs_find_data_file(file);
  292. *effect = gs_effect_create_from_file(filename, NULL);
  293. bfree(filename);
  294. }
  295. return *effect;
  296. }
  297. static int obs_init_graphics(struct obs_video_info *ovi)
  298. {
  299. struct obs_core_video *video = &obs->video;
  300. uint8_t transparent_tex_data[2 * 2 * 4] = {0};
  301. const uint8_t *transparent_tex = transparent_tex_data;
  302. struct gs_sampler_info point_sampler = {0};
  303. bool success = true;
  304. int errorcode;
  305. errorcode =
  306. gs_create(&video->graphics, ovi->graphics_module, ovi->adapter);
  307. if (errorcode != GS_SUCCESS) {
  308. switch (errorcode) {
  309. case GS_ERROR_MODULE_NOT_FOUND:
  310. return OBS_VIDEO_MODULE_NOT_FOUND;
  311. case GS_ERROR_NOT_SUPPORTED:
  312. return OBS_VIDEO_NOT_SUPPORTED;
  313. default:
  314. return OBS_VIDEO_FAIL;
  315. }
  316. }
  317. gs_enter_context(video->graphics);
  318. char *filename = obs_find_data_file("default.effect");
  319. video->default_effect = gs_effect_create_from_file(filename, NULL);
  320. bfree(filename);
  321. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  322. filename = obs_find_data_file("default_rect.effect");
  323. video->default_rect_effect =
  324. gs_effect_create_from_file(filename, NULL);
  325. bfree(filename);
  326. }
  327. filename = obs_find_data_file("opaque.effect");
  328. video->opaque_effect = gs_effect_create_from_file(filename, NULL);
  329. bfree(filename);
  330. filename = obs_find_data_file("solid.effect");
  331. video->solid_effect = gs_effect_create_from_file(filename, NULL);
  332. bfree(filename);
  333. filename = obs_find_data_file("repeat.effect");
  334. video->repeat_effect = gs_effect_create_from_file(filename, NULL);
  335. bfree(filename);
  336. filename = obs_find_data_file("format_conversion.effect");
  337. video->conversion_effect = gs_effect_create_from_file(filename, NULL);
  338. bfree(filename);
  339. filename = obs_find_data_file("bicubic_scale.effect");
  340. video->bicubic_effect = gs_effect_create_from_file(filename, NULL);
  341. bfree(filename);
  342. filename = obs_find_data_file("lanczos_scale.effect");
  343. video->lanczos_effect = gs_effect_create_from_file(filename, NULL);
  344. bfree(filename);
  345. filename = obs_find_data_file("area.effect");
  346. video->area_effect = gs_effect_create_from_file(filename, NULL);
  347. bfree(filename);
  348. filename = obs_find_data_file("bilinear_lowres_scale.effect");
  349. video->bilinear_lowres_effect =
  350. gs_effect_create_from_file(filename, NULL);
  351. bfree(filename);
  352. filename = obs_find_data_file("premultiplied_alpha.effect");
  353. video->premultiplied_alpha_effect =
  354. gs_effect_create_from_file(filename, NULL);
  355. bfree(filename);
  356. point_sampler.max_anisotropy = 1;
  357. video->point_sampler = gs_samplerstate_create(&point_sampler);
  358. obs->video.transparent_texture =
  359. gs_texture_create(2, 2, GS_RGBA, 1, &transparent_tex, 0);
  360. if (!video->default_effect)
  361. success = false;
  362. if (gs_get_device_type() == GS_DEVICE_OPENGL) {
  363. if (!video->default_rect_effect)
  364. success = false;
  365. }
  366. if (!video->opaque_effect)
  367. success = false;
  368. if (!video->solid_effect)
  369. success = false;
  370. if (!video->conversion_effect)
  371. success = false;
  372. if (!video->premultiplied_alpha_effect)
  373. success = false;
  374. if (!video->transparent_texture)
  375. success = false;
  376. if (!video->point_sampler)
  377. success = false;
  378. gs_leave_context();
  379. return success ? OBS_VIDEO_SUCCESS : OBS_VIDEO_FAIL;
  380. }
  381. static inline void set_video_matrix(struct obs_core_video *video,
  382. struct obs_video_info *ovi)
  383. {
  384. struct matrix4 mat;
  385. struct vec4 r_row;
  386. if (format_is_yuv(ovi->output_format)) {
  387. video_format_get_parameters(ovi->colorspace, ovi->range,
  388. (float *)&mat, NULL, NULL);
  389. matrix4_inv(&mat, &mat);
  390. /* swap R and G */
  391. r_row = mat.x;
  392. mat.x = mat.y;
  393. mat.y = r_row;
  394. } else {
  395. matrix4_identity(&mat);
  396. }
  397. memcpy(video->color_matrix, &mat, sizeof(float) * 16);
  398. }
  399. static int obs_init_video(struct obs_video_info *ovi)
  400. {
  401. struct obs_core_video *video = &obs->video;
  402. struct video_output_info vi;
  403. int errorcode;
  404. make_video_info(&vi, ovi);
  405. video->base_width = ovi->base_width;
  406. video->base_height = ovi->base_height;
  407. video->output_width = ovi->output_width;
  408. video->output_height = ovi->output_height;
  409. video->gpu_conversion = ovi->gpu_conversion;
  410. video->scale_type = ovi->scale_type;
  411. set_video_matrix(video, ovi);
  412. errorcode = video_output_open(&video->video, &vi);
  413. if (errorcode != VIDEO_OUTPUT_SUCCESS) {
  414. if (errorcode == VIDEO_OUTPUT_INVALIDPARAM) {
  415. blog(LOG_ERROR, "Invalid video parameters specified");
  416. return OBS_VIDEO_INVALID_PARAM;
  417. } else {
  418. blog(LOG_ERROR, "Could not open video output");
  419. }
  420. return OBS_VIDEO_FAIL;
  421. }
  422. gs_enter_context(video->graphics);
  423. if (ovi->gpu_conversion && !obs_init_gpu_conversion(ovi))
  424. return OBS_VIDEO_FAIL;
  425. if (!obs_init_textures(ovi))
  426. return OBS_VIDEO_FAIL;
  427. gs_leave_context();
  428. if (pthread_mutex_init(&video->gpu_encoder_mutex, NULL) < 0)
  429. return OBS_VIDEO_FAIL;
  430. if (pthread_mutex_init(&video->task_mutex, NULL) < 0)
  431. return OBS_VIDEO_FAIL;
  432. #ifdef __APPLE__
  433. errorcode = pthread_create(&video->video_thread, NULL,
  434. obs_graphics_thread_autorelease, obs);
  435. #else
  436. errorcode = pthread_create(&video->video_thread, NULL,
  437. obs_graphics_thread, obs);
  438. #endif
  439. if (errorcode != 0)
  440. return OBS_VIDEO_FAIL;
  441. video->thread_initialized = true;
  442. video->ovi = *ovi;
  443. return OBS_VIDEO_SUCCESS;
  444. }
  445. static void stop_video(void)
  446. {
  447. struct obs_core_video *video = &obs->video;
  448. void *thread_retval;
  449. if (video->video) {
  450. video_output_stop(video->video);
  451. if (video->thread_initialized) {
  452. pthread_join(video->video_thread, &thread_retval);
  453. video->thread_initialized = false;
  454. }
  455. }
  456. }
  457. static void obs_free_video(void)
  458. {
  459. struct obs_core_video *video = &obs->video;
  460. if (video->video) {
  461. video_output_close(video->video);
  462. video->video = NULL;
  463. if (!video->graphics)
  464. return;
  465. gs_enter_context(video->graphics);
  466. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  467. if (video->mapped_surfaces[c]) {
  468. gs_stagesurface_unmap(
  469. video->mapped_surfaces[c]);
  470. video->mapped_surfaces[c] = NULL;
  471. }
  472. }
  473. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  474. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  475. if (video->copy_surfaces[i][c]) {
  476. gs_stagesurface_destroy(
  477. video->copy_surfaces[i][c]);
  478. video->copy_surfaces[i][c] = NULL;
  479. }
  480. video->active_copy_surfaces[i][c] = NULL;
  481. }
  482. #ifdef _WIN32
  483. if (video->copy_surfaces_encode[i]) {
  484. gs_stagesurface_destroy(
  485. video->copy_surfaces_encode[i]);
  486. video->copy_surfaces_encode[i] = NULL;
  487. }
  488. #endif
  489. }
  490. gs_texture_destroy(video->render_texture);
  491. for (size_t c = 0; c < NUM_CHANNELS; c++) {
  492. if (video->convert_textures[c]) {
  493. gs_texture_destroy(video->convert_textures[c]);
  494. video->convert_textures[c] = NULL;
  495. }
  496. #ifdef _WIN32
  497. if (video->convert_textures_encode[c]) {
  498. gs_texture_destroy(
  499. video->convert_textures_encode[c]);
  500. video->convert_textures_encode[c] = NULL;
  501. }
  502. #endif
  503. }
  504. gs_texture_destroy(video->output_texture);
  505. video->render_texture = NULL;
  506. video->output_texture = NULL;
  507. gs_leave_context();
  508. circlebuf_free(&video->vframe_info_buffer);
  509. circlebuf_free(&video->vframe_info_buffer_gpu);
  510. video->texture_rendered = false;
  511. memset(video->textures_copied, 0,
  512. sizeof(video->textures_copied));
  513. video->texture_converted = false;
  514. pthread_mutex_destroy(&video->gpu_encoder_mutex);
  515. pthread_mutex_init_value(&video->gpu_encoder_mutex);
  516. da_free(video->gpu_encoders);
  517. pthread_mutex_destroy(&video->task_mutex);
  518. pthread_mutex_init_value(&video->task_mutex);
  519. circlebuf_free(&video->tasks);
  520. video->gpu_encoder_active = 0;
  521. video->cur_texture = 0;
  522. }
  523. }
  524. static void obs_free_graphics(void)
  525. {
  526. struct obs_core_video *video = &obs->video;
  527. if (video->graphics) {
  528. gs_enter_context(video->graphics);
  529. gs_texture_destroy(video->transparent_texture);
  530. gs_samplerstate_destroy(video->point_sampler);
  531. gs_effect_destroy(video->default_effect);
  532. gs_effect_destroy(video->default_rect_effect);
  533. gs_effect_destroy(video->opaque_effect);
  534. gs_effect_destroy(video->solid_effect);
  535. gs_effect_destroy(video->conversion_effect);
  536. gs_effect_destroy(video->bicubic_effect);
  537. gs_effect_destroy(video->repeat_effect);
  538. gs_effect_destroy(video->lanczos_effect);
  539. gs_effect_destroy(video->area_effect);
  540. gs_effect_destroy(video->bilinear_lowres_effect);
  541. video->default_effect = NULL;
  542. gs_leave_context();
  543. gs_destroy(video->graphics);
  544. video->graphics = NULL;
  545. }
  546. }
  547. static void set_audio_thread(void *unused);
  548. static bool obs_init_audio(struct audio_output_info *ai)
  549. {
  550. struct obs_core_audio *audio = &obs->audio;
  551. int errorcode;
  552. pthread_mutex_init_value(&audio->monitoring_mutex);
  553. if (pthread_mutex_init_recursive(&audio->monitoring_mutex) != 0)
  554. return false;
  555. if (pthread_mutex_init(&audio->task_mutex, NULL) != 0)
  556. return false;
  557. struct obs_task_info audio_init = {.task = set_audio_thread};
  558. circlebuf_push_back(&audio->tasks, &audio_init, sizeof(audio_init));
  559. audio->user_volume = 1.0f;
  560. audio->monitoring_device_name = bstrdup("Default");
  561. audio->monitoring_device_id = bstrdup("default");
  562. errorcode = audio_output_open(&audio->audio, ai);
  563. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  564. return true;
  565. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  566. blog(LOG_ERROR, "Invalid audio parameters specified");
  567. else
  568. blog(LOG_ERROR, "Could not open audio output");
  569. return false;
  570. }
  571. static void stop_audio(void)
  572. {
  573. struct obs_core_audio *audio = &obs->audio;
  574. if (audio->audio) {
  575. audio_output_close(audio->audio);
  576. audio->audio = NULL;
  577. }
  578. }
  579. static void obs_free_audio(void)
  580. {
  581. struct obs_core_audio *audio = &obs->audio;
  582. if (audio->audio)
  583. audio_output_close(audio->audio);
  584. circlebuf_free(&audio->buffered_timestamps);
  585. da_free(audio->render_order);
  586. da_free(audio->root_nodes);
  587. da_free(audio->monitors);
  588. bfree(audio->monitoring_device_name);
  589. bfree(audio->monitoring_device_id);
  590. circlebuf_free(&audio->tasks);
  591. pthread_mutex_destroy(&audio->task_mutex);
  592. pthread_mutex_destroy(&audio->monitoring_mutex);
  593. memset(audio, 0, sizeof(struct obs_core_audio));
  594. }
  595. static bool obs_init_data(void)
  596. {
  597. struct obs_core_data *data = &obs->data;
  598. assert(data != NULL);
  599. pthread_mutex_init_value(&obs->data.displays_mutex);
  600. pthread_mutex_init_value(&obs->data.draw_callbacks_mutex);
  601. if (pthread_mutex_init_recursive(&data->sources_mutex) != 0)
  602. goto fail;
  603. if (pthread_mutex_init_recursive(&data->audio_sources_mutex) != 0)
  604. goto fail;
  605. if (pthread_mutex_init_recursive(&data->displays_mutex) != 0)
  606. goto fail;
  607. if (pthread_mutex_init_recursive(&data->outputs_mutex) != 0)
  608. goto fail;
  609. if (pthread_mutex_init_recursive(&data->encoders_mutex) != 0)
  610. goto fail;
  611. if (pthread_mutex_init_recursive(&data->services_mutex) != 0)
  612. goto fail;
  613. if (pthread_mutex_init_recursive(&obs->data.draw_callbacks_mutex) != 0)
  614. goto fail;
  615. if (!obs_view_init(&data->main_view))
  616. goto fail;
  617. data->private_data = obs_data_create();
  618. data->valid = true;
  619. fail:
  620. return data->valid;
  621. }
  622. void obs_main_view_free(struct obs_view *view)
  623. {
  624. if (!view)
  625. return;
  626. for (size_t i = 0; i < MAX_CHANNELS; i++)
  627. obs_source_release(view->channels[i]);
  628. memset(view->channels, 0, sizeof(view->channels));
  629. pthread_mutex_destroy(&view->channels_mutex);
  630. }
  631. #define FREE_OBS_LINKED_LIST(type) \
  632. do { \
  633. int unfreed = 0; \
  634. while (data->first_##type) { \
  635. obs_##type##_destroy(data->first_##type); \
  636. unfreed++; \
  637. } \
  638. if (unfreed) \
  639. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  640. unfreed); \
  641. } while (false)
  642. static void obs_free_data(void)
  643. {
  644. struct obs_core_data *data = &obs->data;
  645. data->valid = false;
  646. obs_main_view_free(&data->main_view);
  647. blog(LOG_INFO, "Freeing OBS context data");
  648. FREE_OBS_LINKED_LIST(source);
  649. FREE_OBS_LINKED_LIST(output);
  650. FREE_OBS_LINKED_LIST(encoder);
  651. FREE_OBS_LINKED_LIST(display);
  652. FREE_OBS_LINKED_LIST(service);
  653. os_task_queue_wait(obs->destruction_task_thread);
  654. pthread_mutex_destroy(&data->sources_mutex);
  655. pthread_mutex_destroy(&data->audio_sources_mutex);
  656. pthread_mutex_destroy(&data->displays_mutex);
  657. pthread_mutex_destroy(&data->outputs_mutex);
  658. pthread_mutex_destroy(&data->encoders_mutex);
  659. pthread_mutex_destroy(&data->services_mutex);
  660. pthread_mutex_destroy(&data->draw_callbacks_mutex);
  661. da_free(data->draw_callbacks);
  662. da_free(data->tick_callbacks);
  663. obs_data_release(data->private_data);
  664. }
  665. static const char *obs_signals[] = {
  666. "void source_create(ptr source)",
  667. "void source_destroy(ptr source)",
  668. "void source_remove(ptr source)",
  669. "void source_save(ptr source)",
  670. "void source_load(ptr source)",
  671. "void source_activate(ptr source)",
  672. "void source_deactivate(ptr source)",
  673. "void source_show(ptr source)",
  674. "void source_hide(ptr source)",
  675. "void source_audio_activate(ptr source)",
  676. "void source_audio_deactivate(ptr source)",
  677. "void source_rename(ptr source, string new_name, string prev_name)",
  678. "void source_volume(ptr source, in out float volume)",
  679. "void source_volume_level(ptr source, float level, float magnitude, "
  680. "float peak)",
  681. "void source_transition_start(ptr source)",
  682. "void source_transition_video_stop(ptr source)",
  683. "void source_transition_stop(ptr source)",
  684. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  685. "void master_volume(in out float volume)",
  686. "void hotkey_layout_change()",
  687. "void hotkey_register(ptr hotkey)",
  688. "void hotkey_unregister(ptr hotkey)",
  689. "void hotkey_bindings_changed(ptr hotkey)",
  690. NULL,
  691. };
  692. static inline bool obs_init_handlers(void)
  693. {
  694. obs->signals = signal_handler_create();
  695. if (!obs->signals)
  696. return false;
  697. obs->procs = proc_handler_create();
  698. if (!obs->procs)
  699. return false;
  700. return signal_handler_add_array(obs->signals, obs_signals);
  701. }
  702. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  703. static inline bool obs_init_hotkeys(void)
  704. {
  705. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  706. bool success = false;
  707. assert(hotkeys != NULL);
  708. da_init(hotkeys->hotkeys);
  709. hotkeys->signals = obs->signals;
  710. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  711. hotkeys->mute = bstrdup("Mute");
  712. hotkeys->unmute = bstrdup("Unmute");
  713. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  714. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  715. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  716. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  717. if (!obs_hotkeys_platform_init(hotkeys))
  718. return false;
  719. if (pthread_mutex_init_recursive(&hotkeys->mutex) != 0)
  720. goto fail;
  721. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  722. goto fail;
  723. if (pthread_create(&hotkeys->hotkey_thread, NULL, obs_hotkey_thread,
  724. NULL))
  725. goto fail;
  726. hotkeys->hotkey_thread_initialized = true;
  727. success = true;
  728. fail:
  729. return success;
  730. }
  731. static inline void stop_hotkeys(void)
  732. {
  733. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  734. void *thread_ret;
  735. if (hotkeys->hotkey_thread_initialized) {
  736. os_event_signal(hotkeys->stop_event);
  737. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  738. hotkeys->hotkey_thread_initialized = false;
  739. }
  740. os_event_destroy(hotkeys->stop_event);
  741. obs_hotkeys_free();
  742. }
  743. static inline void obs_free_hotkeys(void)
  744. {
  745. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  746. bfree(hotkeys->mute);
  747. bfree(hotkeys->unmute);
  748. bfree(hotkeys->push_to_mute);
  749. bfree(hotkeys->push_to_talk);
  750. bfree(hotkeys->sceneitem_show);
  751. bfree(hotkeys->sceneitem_hide);
  752. obs_hotkey_name_map_free();
  753. obs_hotkeys_platform_free(hotkeys);
  754. pthread_mutex_destroy(&hotkeys->mutex);
  755. }
  756. extern const struct obs_source_info scene_info;
  757. extern const struct obs_source_info group_info;
  758. static const char *submix_name(void *unused)
  759. {
  760. UNUSED_PARAMETER(unused);
  761. return "Audio line (internal use only)";
  762. }
  763. const struct obs_source_info audio_line_info = {
  764. .id = "audio_line",
  765. .type = OBS_SOURCE_TYPE_INPUT,
  766. .output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_DISABLED |
  767. OBS_SOURCE_SUBMIX,
  768. .get_name = submix_name,
  769. };
  770. extern void log_system_info(void);
  771. static bool obs_init(const char *locale, const char *module_config_path,
  772. profiler_name_store_t *store)
  773. {
  774. obs = bzalloc(sizeof(struct obs_core));
  775. pthread_mutex_init_value(&obs->audio.monitoring_mutex);
  776. pthread_mutex_init_value(&obs->audio.task_mutex);
  777. pthread_mutex_init_value(&obs->video.gpu_encoder_mutex);
  778. pthread_mutex_init_value(&obs->video.task_mutex);
  779. obs->name_store_owned = !store;
  780. obs->name_store = store ? store : profiler_name_store_create();
  781. if (!obs->name_store) {
  782. blog(LOG_ERROR, "Couldn't create profiler name store");
  783. return false;
  784. }
  785. log_system_info();
  786. if (!obs_init_data())
  787. return false;
  788. if (!obs_init_handlers())
  789. return false;
  790. if (!obs_init_hotkeys())
  791. return false;
  792. obs->destruction_task_thread = os_task_queue_create();
  793. if (!obs->destruction_task_thread)
  794. return false;
  795. if (module_config_path)
  796. obs->module_config_path = bstrdup(module_config_path);
  797. obs->locale = bstrdup(locale);
  798. obs_register_source(&scene_info);
  799. obs_register_source(&group_info);
  800. obs_register_source(&audio_line_info);
  801. add_default_module_paths();
  802. return true;
  803. }
  804. #ifdef _WIN32
  805. extern bool initialize_com(void);
  806. extern void uninitialize_com(void);
  807. static bool com_initialized = false;
  808. #endif
  809. /* Separate from actual context initialization
  810. * since this can be set before startup and persist
  811. * after shutdown. */
  812. static DARRAY(struct dstr) core_module_paths = {0};
  813. char *obs_find_data_file(const char *file)
  814. {
  815. struct dstr path = {0};
  816. char *result = find_libobs_data_file(file);
  817. if (result)
  818. return result;
  819. for (size_t i = 0; i < core_module_paths.num; ++i) {
  820. if (check_path(file, core_module_paths.array[i].array, &path))
  821. return path.array;
  822. }
  823. dstr_free(&path);
  824. return NULL;
  825. }
  826. void obs_add_data_path(const char *path)
  827. {
  828. struct dstr *new_path = da_push_back_new(core_module_paths);
  829. dstr_init_copy(new_path, path);
  830. }
  831. bool obs_remove_data_path(const char *path)
  832. {
  833. for (size_t i = 0; i < core_module_paths.num; ++i) {
  834. int result = dstr_cmp(&core_module_paths.array[i], path);
  835. if (result == 0) {
  836. dstr_free(&core_module_paths.array[i]);
  837. da_erase(core_module_paths, i);
  838. return true;
  839. }
  840. }
  841. return false;
  842. }
  843. static const char *obs_startup_name = "obs_startup";
  844. bool obs_startup(const char *locale, const char *module_config_path,
  845. profiler_name_store_t *store)
  846. {
  847. bool success;
  848. profile_start(obs_startup_name);
  849. if (obs) {
  850. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  851. return false;
  852. }
  853. #ifdef _WIN32
  854. com_initialized = initialize_com();
  855. #endif
  856. success = obs_init(locale, module_config_path, store);
  857. profile_end(obs_startup_name);
  858. if (!success)
  859. obs_shutdown();
  860. return success;
  861. }
  862. static struct obs_cmdline_args cmdline_args = {0, NULL};
  863. void obs_set_cmdline_args(int argc, const char *const *argv)
  864. {
  865. char *data;
  866. size_t len;
  867. int i;
  868. /* Once argc is set (non-zero) we shouldn't call again */
  869. if (cmdline_args.argc)
  870. return;
  871. cmdline_args.argc = argc;
  872. /* Safely copy over argv */
  873. len = 0;
  874. for (i = 0; i < argc; i++)
  875. len += strlen(argv[i]) + 1;
  876. cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
  877. data = (char *)cmdline_args.argv + sizeof(char *) * (argc + 1);
  878. for (i = 0; i < argc; i++) {
  879. cmdline_args.argv[i] = data;
  880. len = strlen(argv[i]) + 1;
  881. memcpy(data, argv[i], len);
  882. data += len;
  883. }
  884. cmdline_args.argv[argc] = NULL;
  885. }
  886. struct obs_cmdline_args obs_get_cmdline_args(void)
  887. {
  888. return cmdline_args;
  889. }
  890. void obs_shutdown(void)
  891. {
  892. struct obs_module *module;
  893. obs_wait_for_destroy_queue();
  894. for (size_t i = 0; i < obs->source_types.num; i++) {
  895. struct obs_source_info *item = &obs->source_types.array[i];
  896. if (item->type_data && item->free_type_data)
  897. item->free_type_data(item->type_data);
  898. if (item->id)
  899. bfree((void *)item->id);
  900. }
  901. da_free(obs->source_types);
  902. #define FREE_REGISTERED_TYPES(structure, list) \
  903. do { \
  904. for (size_t i = 0; i < list.num; i++) { \
  905. struct structure *item = &list.array[i]; \
  906. if (item->type_data && item->free_type_data) \
  907. item->free_type_data(item->type_data); \
  908. } \
  909. da_free(list); \
  910. } while (false)
  911. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  912. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  913. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  914. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  915. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  916. #undef FREE_REGISTERED_TYPES
  917. da_free(obs->input_types);
  918. da_free(obs->filter_types);
  919. da_free(obs->transition_types);
  920. stop_video();
  921. stop_audio();
  922. stop_hotkeys();
  923. module = obs->first_module;
  924. while (module) {
  925. struct obs_module *next = module->next;
  926. free_module(module);
  927. module = next;
  928. }
  929. obs->first_module = NULL;
  930. obs_free_data();
  931. obs_free_audio();
  932. obs_free_video();
  933. os_task_queue_destroy(obs->destruction_task_thread);
  934. obs_free_hotkeys();
  935. obs_free_graphics();
  936. proc_handler_destroy(obs->procs);
  937. signal_handler_destroy(obs->signals);
  938. obs->procs = NULL;
  939. obs->signals = NULL;
  940. for (size_t i = 0; i < obs->module_paths.num; i++)
  941. free_module_path(obs->module_paths.array + i);
  942. da_free(obs->module_paths);
  943. if (obs->name_store_owned)
  944. profiler_name_store_free(obs->name_store);
  945. bfree(obs->module_config_path);
  946. bfree(obs->locale);
  947. bfree(obs);
  948. obs = NULL;
  949. bfree(cmdline_args.argv);
  950. #ifdef _WIN32
  951. if (com_initialized)
  952. uninitialize_com();
  953. #endif
  954. }
  955. bool obs_initialized(void)
  956. {
  957. return obs != NULL;
  958. }
  959. uint32_t obs_get_version(void)
  960. {
  961. return LIBOBS_API_VER;
  962. }
  963. const char *obs_get_version_string(void)
  964. {
  965. return OBS_VERSION;
  966. }
  967. void obs_set_locale(const char *locale)
  968. {
  969. struct obs_module *module;
  970. if (obs->locale)
  971. bfree(obs->locale);
  972. obs->locale = bstrdup(locale);
  973. module = obs->first_module;
  974. while (module) {
  975. if (module->set_locale)
  976. module->set_locale(locale);
  977. module = module->next;
  978. }
  979. }
  980. const char *obs_get_locale(void)
  981. {
  982. return obs->locale;
  983. }
  984. #define OBS_SIZE_MIN 2
  985. #define OBS_SIZE_MAX (32 * 1024)
  986. static inline bool size_valid(uint32_t width, uint32_t height)
  987. {
  988. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  989. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  990. }
  991. int obs_reset_video(struct obs_video_info *ovi)
  992. {
  993. if (!obs)
  994. return OBS_VIDEO_FAIL;
  995. /* don't allow changing of video settings if active. */
  996. if (obs->video.video && obs_video_active())
  997. return OBS_VIDEO_CURRENTLY_ACTIVE;
  998. if (!size_valid(ovi->output_width, ovi->output_height) ||
  999. !size_valid(ovi->base_width, ovi->base_height))
  1000. return OBS_VIDEO_INVALID_PARAM;
  1001. struct obs_core_video *video = &obs->video;
  1002. stop_video();
  1003. obs_free_video();
  1004. /* align to multiple-of-two and SSE alignment sizes */
  1005. ovi->output_width &= 0xFFFFFFFC;
  1006. ovi->output_height &= 0xFFFFFFFE;
  1007. if (!video->graphics) {
  1008. int errorcode = obs_init_graphics(ovi);
  1009. if (errorcode != OBS_VIDEO_SUCCESS) {
  1010. obs_free_graphics();
  1011. return errorcode;
  1012. }
  1013. }
  1014. const char *scale_type_name = "";
  1015. switch (ovi->scale_type) {
  1016. case OBS_SCALE_DISABLE:
  1017. scale_type_name = "Disabled";
  1018. break;
  1019. case OBS_SCALE_POINT:
  1020. scale_type_name = "Point";
  1021. break;
  1022. case OBS_SCALE_BICUBIC:
  1023. scale_type_name = "Bicubic";
  1024. break;
  1025. case OBS_SCALE_BILINEAR:
  1026. scale_type_name = "Bilinear";
  1027. break;
  1028. case OBS_SCALE_LANCZOS:
  1029. scale_type_name = "Lanczos";
  1030. break;
  1031. case OBS_SCALE_AREA:
  1032. scale_type_name = "Area";
  1033. break;
  1034. }
  1035. bool yuv = format_is_yuv(ovi->output_format);
  1036. const char *yuv_format = get_video_colorspace_name(ovi->colorspace);
  1037. const char *yuv_range =
  1038. get_video_range_name(ovi->output_format, ovi->range);
  1039. blog(LOG_INFO, "---------------------------------");
  1040. blog(LOG_INFO,
  1041. "video settings reset:\n"
  1042. "\tbase resolution: %dx%d\n"
  1043. "\toutput resolution: %dx%d\n"
  1044. "\tdownscale filter: %s\n"
  1045. "\tfps: %d/%d\n"
  1046. "\tformat: %s\n"
  1047. "\tYUV mode: %s%s%s",
  1048. ovi->base_width, ovi->base_height, ovi->output_width,
  1049. ovi->output_height, scale_type_name, ovi->fps_num, ovi->fps_den,
  1050. get_video_format_name(ovi->output_format),
  1051. yuv ? yuv_format : "None", yuv ? "/" : "", yuv ? yuv_range : "");
  1052. return obs_init_video(ovi);
  1053. }
  1054. bool obs_reset_audio(const struct obs_audio_info *oai)
  1055. {
  1056. struct audio_output_info ai;
  1057. /* don't allow changing of audio settings if active. */
  1058. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  1059. return false;
  1060. obs_free_audio();
  1061. if (!oai)
  1062. return true;
  1063. ai.name = "Audio";
  1064. ai.samples_per_sec = oai->samples_per_sec;
  1065. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  1066. ai.speakers = oai->speakers;
  1067. ai.input_callback = audio_callback;
  1068. blog(LOG_INFO, "---------------------------------");
  1069. blog(LOG_INFO,
  1070. "audio settings reset:\n"
  1071. "\tsamples per sec: %d\n"
  1072. "\tspeakers: %d",
  1073. (int)ai.samples_per_sec, (int)ai.speakers);
  1074. return obs_init_audio(&ai);
  1075. }
  1076. bool obs_get_video_info(struct obs_video_info *ovi)
  1077. {
  1078. struct obs_core_video *video = &obs->video;
  1079. if (!video->graphics)
  1080. return false;
  1081. *ovi = video->ovi;
  1082. return true;
  1083. }
  1084. float obs_get_video_sdr_white_level(void)
  1085. {
  1086. struct obs_core_video *video = &obs->video;
  1087. return video->graphics ? video->sdr_white_level : 300.f;
  1088. }
  1089. void obs_set_video_sdr_white_level(float sdr_white_level)
  1090. {
  1091. struct obs_core_video *video = &obs->video;
  1092. assert(video->graphics);
  1093. video->sdr_white_level = (uint32_t)sdr_white_level;
  1094. }
  1095. bool obs_get_audio_info(struct obs_audio_info *oai)
  1096. {
  1097. struct obs_core_audio *audio = &obs->audio;
  1098. const struct audio_output_info *info;
  1099. if (!oai || !audio->audio)
  1100. return false;
  1101. info = audio_output_get_info(audio->audio);
  1102. oai->samples_per_sec = info->samples_per_sec;
  1103. oai->speakers = info->speakers;
  1104. return true;
  1105. }
  1106. bool obs_enum_source_types(size_t idx, const char **id)
  1107. {
  1108. if (idx >= obs->source_types.num)
  1109. return false;
  1110. *id = obs->source_types.array[idx].id;
  1111. return true;
  1112. }
  1113. bool obs_enum_input_types(size_t idx, const char **id)
  1114. {
  1115. if (idx >= obs->input_types.num)
  1116. return false;
  1117. *id = obs->input_types.array[idx].id;
  1118. return true;
  1119. }
  1120. bool obs_enum_input_types2(size_t idx, const char **id,
  1121. const char **unversioned_id)
  1122. {
  1123. if (idx >= obs->input_types.num)
  1124. return false;
  1125. if (id)
  1126. *id = obs->input_types.array[idx].id;
  1127. if (unversioned_id)
  1128. *unversioned_id = obs->input_types.array[idx].unversioned_id;
  1129. return true;
  1130. }
  1131. const char *obs_get_latest_input_type_id(const char *unversioned_id)
  1132. {
  1133. struct obs_source_info *latest = NULL;
  1134. int version = -1;
  1135. if (!unversioned_id)
  1136. return NULL;
  1137. for (size_t i = 0; i < obs->source_types.num; i++) {
  1138. struct obs_source_info *info = &obs->source_types.array[i];
  1139. if (strcmp(info->unversioned_id, unversioned_id) == 0 &&
  1140. (int)info->version > version) {
  1141. latest = info;
  1142. version = info->version;
  1143. }
  1144. }
  1145. assert(!!latest);
  1146. if (!latest)
  1147. return NULL;
  1148. return latest->id;
  1149. }
  1150. bool obs_enum_filter_types(size_t idx, const char **id)
  1151. {
  1152. if (idx >= obs->filter_types.num)
  1153. return false;
  1154. *id = obs->filter_types.array[idx].id;
  1155. return true;
  1156. }
  1157. bool obs_enum_transition_types(size_t idx, const char **id)
  1158. {
  1159. if (idx >= obs->transition_types.num)
  1160. return false;
  1161. *id = obs->transition_types.array[idx].id;
  1162. return true;
  1163. }
  1164. bool obs_enum_output_types(size_t idx, const char **id)
  1165. {
  1166. if (idx >= obs->output_types.num)
  1167. return false;
  1168. *id = obs->output_types.array[idx].id;
  1169. return true;
  1170. }
  1171. bool obs_enum_encoder_types(size_t idx, const char **id)
  1172. {
  1173. if (idx >= obs->encoder_types.num)
  1174. return false;
  1175. *id = obs->encoder_types.array[idx].id;
  1176. return true;
  1177. }
  1178. bool obs_enum_service_types(size_t idx, const char **id)
  1179. {
  1180. if (idx >= obs->service_types.num)
  1181. return false;
  1182. *id = obs->service_types.array[idx].id;
  1183. return true;
  1184. }
  1185. void obs_enter_graphics(void)
  1186. {
  1187. if (obs->video.graphics)
  1188. gs_enter_context(obs->video.graphics);
  1189. }
  1190. void obs_leave_graphics(void)
  1191. {
  1192. if (obs->video.graphics)
  1193. gs_leave_context();
  1194. }
  1195. audio_t *obs_get_audio(void)
  1196. {
  1197. return obs->audio.audio;
  1198. }
  1199. video_t *obs_get_video(void)
  1200. {
  1201. return obs->video.video;
  1202. }
  1203. /* TODO: optimize this later so it's not just O(N) string lookups */
  1204. static inline struct obs_modal_ui *
  1205. get_modal_ui_callback(const char *id, const char *task, const char *target)
  1206. {
  1207. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  1208. struct obs_modal_ui *callback =
  1209. obs->modal_ui_callbacks.array + i;
  1210. if (strcmp(callback->id, id) == 0 &&
  1211. strcmp(callback->task, task) == 0 &&
  1212. strcmp(callback->target, target) == 0)
  1213. return callback;
  1214. }
  1215. return NULL;
  1216. }
  1217. static inline struct obs_modeless_ui *
  1218. get_modeless_ui_callback(const char *id, const char *task, const char *target)
  1219. {
  1220. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  1221. struct obs_modeless_ui *callback;
  1222. callback = obs->modeless_ui_callbacks.array + i;
  1223. if (strcmp(callback->id, id) == 0 &&
  1224. strcmp(callback->task, task) == 0 &&
  1225. strcmp(callback->target, target) == 0)
  1226. return callback;
  1227. }
  1228. return NULL;
  1229. }
  1230. int obs_exec_ui(const char *name, const char *task, const char *target,
  1231. void *data, void *ui_data)
  1232. {
  1233. struct obs_modal_ui *callback;
  1234. int errorcode = OBS_UI_NOTFOUND;
  1235. if (!obs)
  1236. return errorcode;
  1237. callback = get_modal_ui_callback(name, task, target);
  1238. if (callback) {
  1239. bool success = callback->exec(data, ui_data);
  1240. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  1241. }
  1242. return errorcode;
  1243. }
  1244. void *obs_create_ui(const char *name, const char *task, const char *target,
  1245. void *data, void *ui_data)
  1246. {
  1247. struct obs_modeless_ui *callback;
  1248. callback = get_modeless_ui_callback(name, task, target);
  1249. return callback ? callback->create(data, ui_data) : NULL;
  1250. }
  1251. obs_source_t *obs_get_output_source(uint32_t channel)
  1252. {
  1253. return obs_view_get_source(&obs->data.main_view, channel);
  1254. }
  1255. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  1256. {
  1257. assert(channel < MAX_CHANNELS);
  1258. if (channel >= MAX_CHANNELS)
  1259. return;
  1260. struct obs_source *prev_source;
  1261. struct obs_view *view = &obs->data.main_view;
  1262. struct calldata params = {0};
  1263. pthread_mutex_lock(&view->channels_mutex);
  1264. source = obs_source_get_ref(source);
  1265. prev_source = view->channels[channel];
  1266. calldata_set_int(&params, "channel", channel);
  1267. calldata_set_ptr(&params, "prev_source", prev_source);
  1268. calldata_set_ptr(&params, "source", source);
  1269. signal_handler_signal(obs->signals, "channel_change", &params);
  1270. calldata_get_ptr(&params, "source", &source);
  1271. calldata_free(&params);
  1272. view->channels[channel] = source;
  1273. pthread_mutex_unlock(&view->channels_mutex);
  1274. if (source)
  1275. obs_source_activate(source, MAIN_VIEW);
  1276. if (prev_source) {
  1277. obs_source_deactivate(prev_source, MAIN_VIEW);
  1278. obs_source_release(prev_source);
  1279. }
  1280. }
  1281. void obs_enum_sources(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1282. {
  1283. obs_source_t *source;
  1284. pthread_mutex_lock(&obs->data.sources_mutex);
  1285. source = obs->data.first_source;
  1286. while (source) {
  1287. obs_source_t *s = obs_source_get_ref(source);
  1288. if (s) {
  1289. if (strcmp(s->info.id, group_info.id) == 0 &&
  1290. !enum_proc(param, s)) {
  1291. obs_source_release(s);
  1292. break;
  1293. } else if (s->info.type == OBS_SOURCE_TYPE_INPUT &&
  1294. !s->context.private &&
  1295. !enum_proc(param, s)) {
  1296. obs_source_release(s);
  1297. break;
  1298. }
  1299. obs_source_release(s);
  1300. }
  1301. source = (obs_source_t *)source->context.next;
  1302. }
  1303. pthread_mutex_unlock(&obs->data.sources_mutex);
  1304. }
  1305. void obs_enum_scenes(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1306. {
  1307. obs_source_t *source;
  1308. pthread_mutex_lock(&obs->data.sources_mutex);
  1309. source = obs->data.first_source;
  1310. while (source) {
  1311. obs_source_t *s = obs_source_get_ref(source);
  1312. if (s) {
  1313. if (source->info.type == OBS_SOURCE_TYPE_SCENE &&
  1314. !source->context.private && !enum_proc(param, s)) {
  1315. obs_source_release(s);
  1316. break;
  1317. }
  1318. obs_source_release(s);
  1319. }
  1320. source = (obs_source_t *)source->context.next;
  1321. }
  1322. pthread_mutex_unlock(&obs->data.sources_mutex);
  1323. }
  1324. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  1325. void *param)
  1326. {
  1327. struct obs_context_data **start = pstart, *context;
  1328. bool (*enum_proc)(void *, void *) = proc;
  1329. assert(start);
  1330. assert(mutex);
  1331. assert(enum_proc);
  1332. pthread_mutex_lock(mutex);
  1333. context = *start;
  1334. while (context) {
  1335. if (!enum_proc(param, context))
  1336. break;
  1337. context = context->next;
  1338. }
  1339. pthread_mutex_unlock(mutex);
  1340. }
  1341. void obs_enum_all_sources(bool (*enum_proc)(void *, obs_source_t *),
  1342. void *param)
  1343. {
  1344. obs_enum(&obs->data.first_source, &obs->data.sources_mutex, enum_proc,
  1345. param);
  1346. }
  1347. void obs_enum_outputs(bool (*enum_proc)(void *, obs_output_t *), void *param)
  1348. {
  1349. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex, enum_proc,
  1350. param);
  1351. }
  1352. void obs_enum_encoders(bool (*enum_proc)(void *, obs_encoder_t *), void *param)
  1353. {
  1354. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex, enum_proc,
  1355. param);
  1356. }
  1357. void obs_enum_services(bool (*enum_proc)(void *, obs_service_t *), void *param)
  1358. {
  1359. obs_enum(&obs->data.first_service, &obs->data.services_mutex, enum_proc,
  1360. param);
  1361. }
  1362. static inline void *get_context_by_name(void *vfirst, const char *name,
  1363. pthread_mutex_t *mutex,
  1364. void *(*addref)(void *))
  1365. {
  1366. struct obs_context_data **first = vfirst;
  1367. struct obs_context_data *context;
  1368. pthread_mutex_lock(mutex);
  1369. context = *first;
  1370. while (context) {
  1371. if (!context->private && strcmp(context->name, name) == 0) {
  1372. context = addref(context);
  1373. break;
  1374. }
  1375. context = context->next;
  1376. }
  1377. pthread_mutex_unlock(mutex);
  1378. return context;
  1379. }
  1380. static inline void *obs_source_addref_safe_(void *ref)
  1381. {
  1382. return obs_source_get_ref(ref);
  1383. }
  1384. static inline void *obs_output_addref_safe_(void *ref)
  1385. {
  1386. return obs_output_get_ref(ref);
  1387. }
  1388. static inline void *obs_encoder_addref_safe_(void *ref)
  1389. {
  1390. return obs_encoder_get_ref(ref);
  1391. }
  1392. static inline void *obs_service_addref_safe_(void *ref)
  1393. {
  1394. return obs_service_get_ref(ref);
  1395. }
  1396. static inline void *obs_id_(void *data)
  1397. {
  1398. return data;
  1399. }
  1400. obs_source_t *obs_get_source_by_name(const char *name)
  1401. {
  1402. return get_context_by_name(&obs->data.first_source, name,
  1403. &obs->data.sources_mutex,
  1404. obs_source_addref_safe_);
  1405. }
  1406. obs_source_t *obs_get_transition_by_name(const char *name)
  1407. {
  1408. struct obs_source **first = &obs->data.first_source;
  1409. struct obs_source *source;
  1410. pthread_mutex_lock(&obs->data.sources_mutex);
  1411. source = *first;
  1412. while (source) {
  1413. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION &&
  1414. strcmp(source->context.name, name) == 0) {
  1415. source = obs_source_addref_safe_(source);
  1416. break;
  1417. }
  1418. source = (void *)source->context.next;
  1419. }
  1420. pthread_mutex_unlock(&obs->data.sources_mutex);
  1421. return source;
  1422. }
  1423. obs_output_t *obs_get_output_by_name(const char *name)
  1424. {
  1425. return get_context_by_name(&obs->data.first_output, name,
  1426. &obs->data.outputs_mutex,
  1427. obs_output_addref_safe_);
  1428. }
  1429. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1430. {
  1431. return get_context_by_name(&obs->data.first_encoder, name,
  1432. &obs->data.encoders_mutex,
  1433. obs_encoder_addref_safe_);
  1434. }
  1435. obs_service_t *obs_get_service_by_name(const char *name)
  1436. {
  1437. return get_context_by_name(&obs->data.first_service, name,
  1438. &obs->data.services_mutex,
  1439. obs_service_addref_safe_);
  1440. }
  1441. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1442. {
  1443. switch (effect) {
  1444. case OBS_EFFECT_DEFAULT:
  1445. return obs->video.default_effect;
  1446. case OBS_EFFECT_DEFAULT_RECT:
  1447. return obs->video.default_rect_effect;
  1448. case OBS_EFFECT_OPAQUE:
  1449. return obs->video.opaque_effect;
  1450. case OBS_EFFECT_SOLID:
  1451. return obs->video.solid_effect;
  1452. case OBS_EFFECT_REPEAT:
  1453. return obs->video.repeat_effect;
  1454. case OBS_EFFECT_BICUBIC:
  1455. return obs->video.bicubic_effect;
  1456. case OBS_EFFECT_LANCZOS:
  1457. return obs->video.lanczos_effect;
  1458. case OBS_EFFECT_AREA:
  1459. return obs->video.area_effect;
  1460. case OBS_EFFECT_BILINEAR_LOWRES:
  1461. return obs->video.bilinear_lowres_effect;
  1462. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1463. return obs->video.premultiplied_alpha_effect;
  1464. }
  1465. return NULL;
  1466. }
  1467. /* OBS_DEPRECATED */
  1468. gs_effect_t *obs_get_default_rect_effect(void)
  1469. {
  1470. return obs->video.default_rect_effect;
  1471. }
  1472. signal_handler_t *obs_get_signal_handler(void)
  1473. {
  1474. return obs->signals;
  1475. }
  1476. proc_handler_t *obs_get_proc_handler(void)
  1477. {
  1478. return obs->procs;
  1479. }
  1480. /* OBS_DEPRECATED */
  1481. void obs_render_main_view(void)
  1482. {
  1483. obs_view_render(&obs->data.main_view);
  1484. }
  1485. static void obs_render_main_texture_internal(enum gs_blend_type src_c,
  1486. enum gs_blend_type dest_c,
  1487. enum gs_blend_type src_a,
  1488. enum gs_blend_type dest_a)
  1489. {
  1490. struct obs_core_video *video;
  1491. gs_texture_t *tex;
  1492. gs_effect_t *effect;
  1493. gs_eparam_t *param;
  1494. video = &obs->video;
  1495. if (!video->texture_rendered)
  1496. return;
  1497. const enum gs_color_space source_space = video->render_space;
  1498. const enum gs_color_space current_space = gs_get_color_space();
  1499. const char *tech_name = "Draw";
  1500. float multiplier = 1.0f;
  1501. if ((current_space == GS_CS_SRGB) &&
  1502. (source_space == GS_CS_709_EXTENDED)) {
  1503. tech_name = "DrawTonemap";
  1504. } else if (current_space == GS_CS_709_SCRGB) {
  1505. tech_name = "DrawMultiply";
  1506. multiplier = obs_get_video_sdr_white_level() / 80.0f;
  1507. }
  1508. const bool previous = gs_framebuffer_srgb_enabled();
  1509. gs_enable_framebuffer_srgb(true);
  1510. tex = video->render_texture;
  1511. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1512. param = gs_effect_get_param_by_name(effect, "image");
  1513. gs_effect_set_texture_srgb(param, tex);
  1514. param = gs_effect_get_param_by_name(effect, "multiplier");
  1515. gs_effect_set_float(param, multiplier);
  1516. gs_blend_state_push();
  1517. gs_blend_function_separate(src_c, dest_c, src_a, dest_a);
  1518. while (gs_effect_loop(effect, tech_name))
  1519. gs_draw_sprite(tex, 0, 0, 0);
  1520. gs_blend_state_pop();
  1521. gs_enable_framebuffer_srgb(previous);
  1522. }
  1523. void obs_render_main_texture(void)
  1524. {
  1525. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA,
  1526. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1527. }
  1528. void obs_render_main_texture_src_color_only(void)
  1529. {
  1530. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_ZERO,
  1531. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1532. }
  1533. gs_texture_t *obs_get_main_texture(void)
  1534. {
  1535. struct obs_core_video *video;
  1536. video = &obs->video;
  1537. if (!video->texture_rendered)
  1538. return NULL;
  1539. return video->render_texture;
  1540. }
  1541. void obs_set_master_volume(float volume)
  1542. {
  1543. struct calldata data = {0};
  1544. calldata_set_float(&data, "volume", volume);
  1545. signal_handler_signal(obs->signals, "master_volume", &data);
  1546. volume = (float)calldata_float(&data, "volume");
  1547. calldata_free(&data);
  1548. obs->audio.user_volume = volume;
  1549. }
  1550. float obs_get_master_volume(void)
  1551. {
  1552. return obs->audio.user_volume;
  1553. }
  1554. static obs_source_t *obs_load_source_type(obs_data_t *source_data,
  1555. bool is_private)
  1556. {
  1557. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1558. obs_source_t *source;
  1559. const char *name = obs_data_get_string(source_data, "name");
  1560. const char *id = obs_data_get_string(source_data, "id");
  1561. const char *v_id = obs_data_get_string(source_data, "versioned_id");
  1562. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1563. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1564. double volume;
  1565. double balance;
  1566. int64_t sync;
  1567. uint32_t prev_ver;
  1568. uint32_t caps;
  1569. uint32_t flags;
  1570. uint32_t mixers;
  1571. int di_order;
  1572. int di_mode;
  1573. int monitoring_type;
  1574. prev_ver = (uint32_t)obs_data_get_int(source_data, "prev_ver");
  1575. if (!*v_id)
  1576. v_id = id;
  1577. source = obs_source_create_set_last_ver(v_id, name, settings, hotkeys,
  1578. prev_ver, is_private);
  1579. if (source->owns_info_id) {
  1580. bfree((void *)source->info.unversioned_id);
  1581. source->info.unversioned_id = bstrdup(id);
  1582. }
  1583. obs_data_release(hotkeys);
  1584. caps = obs_source_get_output_flags(source);
  1585. obs_data_set_default_double(source_data, "volume", 1.0);
  1586. volume = obs_data_get_double(source_data, "volume");
  1587. obs_source_set_volume(source, (float)volume);
  1588. obs_data_set_default_double(source_data, "balance", 0.5);
  1589. balance = obs_data_get_double(source_data, "balance");
  1590. obs_source_set_balance_value(source, (float)balance);
  1591. sync = obs_data_get_int(source_data, "sync");
  1592. obs_source_set_sync_offset(source, sync);
  1593. obs_data_set_default_int(source_data, "mixers", 0x3F);
  1594. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1595. obs_source_set_audio_mixers(source, mixers);
  1596. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1597. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1598. obs_source_set_flags(source, flags);
  1599. obs_data_set_default_bool(source_data, "enabled", true);
  1600. obs_source_set_enabled(source,
  1601. obs_data_get_bool(source_data, "enabled"));
  1602. obs_data_set_default_bool(source_data, "muted", false);
  1603. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1604. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1605. obs_source_enable_push_to_mute(
  1606. source, obs_data_get_bool(source_data, "push-to-mute"));
  1607. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1608. obs_source_set_push_to_mute_delay(
  1609. source, obs_data_get_int(source_data, "push-to-mute-delay"));
  1610. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1611. obs_source_enable_push_to_talk(
  1612. source, obs_data_get_bool(source_data, "push-to-talk"));
  1613. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1614. obs_source_set_push_to_talk_delay(
  1615. source, obs_data_get_int(source_data, "push-to-talk-delay"));
  1616. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1617. obs_source_set_deinterlace_mode(source,
  1618. (enum obs_deinterlace_mode)di_mode);
  1619. di_order =
  1620. (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1621. obs_source_set_deinterlace_field_order(
  1622. source, (enum obs_deinterlace_field_order)di_order);
  1623. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1624. if (prev_ver < MAKE_SEMANTIC_VERSION(23, 2, 2)) {
  1625. if ((caps & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  1626. /* updates older sources to enable monitoring
  1627. * automatically if they added monitoring by default in
  1628. * version 24 */
  1629. monitoring_type = OBS_MONITORING_TYPE_MONITOR_ONLY;
  1630. obs_source_set_audio_mixers(source, 0x3F);
  1631. }
  1632. }
  1633. obs_source_set_monitoring_type(
  1634. source, (enum obs_monitoring_type)monitoring_type);
  1635. obs_data_release(source->private_settings);
  1636. source->private_settings =
  1637. obs_data_get_obj(source_data, "private_settings");
  1638. if (!source->private_settings)
  1639. source->private_settings = obs_data_create();
  1640. if (filters) {
  1641. size_t count = obs_data_array_count(filters);
  1642. for (size_t i = 0; i < count; i++) {
  1643. obs_data_t *filter_data =
  1644. obs_data_array_item(filters, i);
  1645. obs_source_t *filter =
  1646. obs_load_source_type(filter_data, true);
  1647. if (filter) {
  1648. obs_source_filter_add(source, filter);
  1649. obs_source_release(filter);
  1650. }
  1651. obs_data_release(filter_data);
  1652. }
  1653. obs_data_array_release(filters);
  1654. }
  1655. obs_data_release(settings);
  1656. return source;
  1657. }
  1658. obs_source_t *obs_load_source(obs_data_t *source_data)
  1659. {
  1660. return obs_load_source_type(source_data, false);
  1661. }
  1662. obs_source_t *obs_load_private_source(obs_data_t *source_data)
  1663. {
  1664. return obs_load_source_type(source_data, true);
  1665. }
  1666. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1667. void *private_data)
  1668. {
  1669. struct obs_core_data *data = &obs->data;
  1670. DARRAY(obs_source_t *) sources;
  1671. size_t count;
  1672. size_t i;
  1673. da_init(sources);
  1674. count = obs_data_array_count(array);
  1675. da_reserve(sources, count);
  1676. pthread_mutex_lock(&data->sources_mutex);
  1677. for (i = 0; i < count; i++) {
  1678. obs_data_t *source_data = obs_data_array_item(array, i);
  1679. obs_source_t *source = obs_load_source(source_data);
  1680. da_push_back(sources, &source);
  1681. obs_data_release(source_data);
  1682. }
  1683. /* tell sources that we want to load */
  1684. for (i = 0; i < sources.num; i++) {
  1685. obs_source_t *source = sources.array[i];
  1686. obs_data_t *source_data = obs_data_array_item(array, i);
  1687. if (source) {
  1688. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1689. obs_transition_load(source, source_data);
  1690. obs_source_load2(source);
  1691. if (cb)
  1692. cb(private_data, source);
  1693. }
  1694. obs_data_release(source_data);
  1695. }
  1696. for (i = 0; i < sources.num; i++)
  1697. obs_source_release(sources.array[i]);
  1698. pthread_mutex_unlock(&data->sources_mutex);
  1699. da_free(sources);
  1700. }
  1701. obs_data_t *obs_save_source(obs_source_t *source)
  1702. {
  1703. obs_data_array_t *filters = obs_data_array_create();
  1704. obs_data_t *source_data = obs_data_create();
  1705. obs_data_t *settings = obs_source_get_settings(source);
  1706. obs_data_t *hotkey_data = source->context.hotkey_data;
  1707. obs_data_t *hotkeys;
  1708. float volume = obs_source_get_volume(source);
  1709. float balance = obs_source_get_balance_value(source);
  1710. uint32_t mixers = obs_source_get_audio_mixers(source);
  1711. int64_t sync = obs_source_get_sync_offset(source);
  1712. uint32_t flags = obs_source_get_flags(source);
  1713. const char *name = obs_source_get_name(source);
  1714. const char *id = source->info.unversioned_id;
  1715. const char *v_id = source->info.id;
  1716. bool enabled = obs_source_enabled(source);
  1717. bool muted = obs_source_muted(source);
  1718. bool push_to_mute = obs_source_push_to_mute_enabled(source);
  1719. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1720. bool push_to_talk = obs_source_push_to_talk_enabled(source);
  1721. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1722. int m_type = (int)obs_source_get_monitoring_type(source);
  1723. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1724. int di_order = (int)obs_source_get_deinterlace_field_order(source);
  1725. obs_source_save(source);
  1726. hotkeys = obs_hotkeys_save_source(source);
  1727. if (hotkeys) {
  1728. obs_data_release(hotkey_data);
  1729. source->context.hotkey_data = hotkeys;
  1730. hotkey_data = hotkeys;
  1731. }
  1732. obs_data_set_int(source_data, "prev_ver", LIBOBS_API_VER);
  1733. obs_data_set_string(source_data, "name", name);
  1734. obs_data_set_string(source_data, "id", id);
  1735. obs_data_set_string(source_data, "versioned_id", v_id);
  1736. obs_data_set_obj(source_data, "settings", settings);
  1737. obs_data_set_int(source_data, "mixers", mixers);
  1738. obs_data_set_int(source_data, "sync", sync);
  1739. obs_data_set_int(source_data, "flags", flags);
  1740. obs_data_set_double(source_data, "volume", volume);
  1741. obs_data_set_double(source_data, "balance", balance);
  1742. obs_data_set_bool(source_data, "enabled", enabled);
  1743. obs_data_set_bool(source_data, "muted", muted);
  1744. obs_data_set_bool(source_data, "push-to-mute", push_to_mute);
  1745. obs_data_set_int(source_data, "push-to-mute-delay", ptm_delay);
  1746. obs_data_set_bool(source_data, "push-to-talk", push_to_talk);
  1747. obs_data_set_int(source_data, "push-to-talk-delay", ptt_delay);
  1748. obs_data_set_obj(source_data, "hotkeys", hotkey_data);
  1749. obs_data_set_int(source_data, "deinterlace_mode", di_mode);
  1750. obs_data_set_int(source_data, "deinterlace_field_order", di_order);
  1751. obs_data_set_int(source_data, "monitoring_type", m_type);
  1752. obs_data_set_obj(source_data, "private_settings",
  1753. source->private_settings);
  1754. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1755. obs_transition_save(source, source_data);
  1756. pthread_mutex_lock(&source->filter_mutex);
  1757. if (source->filters.num) {
  1758. for (size_t i = source->filters.num; i > 0; i--) {
  1759. obs_source_t *filter = source->filters.array[i - 1];
  1760. obs_data_t *filter_data = obs_save_source(filter);
  1761. obs_data_array_push_back(filters, filter_data);
  1762. obs_data_release(filter_data);
  1763. }
  1764. obs_data_set_array(source_data, "filters", filters);
  1765. }
  1766. pthread_mutex_unlock(&source->filter_mutex);
  1767. obs_data_release(settings);
  1768. obs_data_array_release(filters);
  1769. return source_data;
  1770. }
  1771. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1772. void *data_)
  1773. {
  1774. struct obs_core_data *data = &obs->data;
  1775. obs_data_array_t *array;
  1776. obs_source_t *source;
  1777. array = obs_data_array_create();
  1778. pthread_mutex_lock(&data->sources_mutex);
  1779. source = data->first_source;
  1780. while (source) {
  1781. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1782. !source->context.private && !source->removed &&
  1783. !source->temp_removed && cb(data_, source)) {
  1784. obs_data_t *source_data = obs_save_source(source);
  1785. obs_data_array_push_back(array, source_data);
  1786. obs_data_release(source_data);
  1787. }
  1788. source = (obs_source_t *)source->context.next;
  1789. }
  1790. pthread_mutex_unlock(&data->sources_mutex);
  1791. return array;
  1792. }
  1793. static bool save_source_filter(void *data, obs_source_t *source)
  1794. {
  1795. UNUSED_PARAMETER(data);
  1796. UNUSED_PARAMETER(source);
  1797. return true;
  1798. }
  1799. obs_data_array_t *obs_save_sources(void)
  1800. {
  1801. return obs_save_sources_filtered(save_source_filter, NULL);
  1802. }
  1803. /* ensures that names are never blank */
  1804. static inline char *dup_name(const char *name, bool private)
  1805. {
  1806. if (private && !name)
  1807. return NULL;
  1808. if (!name || !*name) {
  1809. struct dstr unnamed = {0};
  1810. dstr_printf(&unnamed, "__unnamed%04lld",
  1811. obs->data.unnamed_index++);
  1812. return unnamed.array;
  1813. } else {
  1814. return bstrdup(name);
  1815. }
  1816. }
  1817. static inline bool obs_context_data_init_wrap(struct obs_context_data *context,
  1818. enum obs_obj_type type,
  1819. obs_data_t *settings,
  1820. const char *name,
  1821. obs_data_t *hotkey_data,
  1822. bool private)
  1823. {
  1824. assert(context);
  1825. memset(context, 0, sizeof(*context));
  1826. context->private = private;
  1827. context->type = type;
  1828. pthread_mutex_init_value(&context->rename_cache_mutex);
  1829. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1830. return false;
  1831. context->signals = signal_handler_create();
  1832. if (!context->signals)
  1833. return false;
  1834. context->procs = proc_handler_create();
  1835. if (!context->procs)
  1836. return false;
  1837. context->name = dup_name(name, private);
  1838. context->settings = obs_data_newref(settings);
  1839. context->hotkey_data = obs_data_newref(hotkey_data);
  1840. return true;
  1841. }
  1842. bool obs_context_data_init(struct obs_context_data *context,
  1843. enum obs_obj_type type, obs_data_t *settings,
  1844. const char *name, obs_data_t *hotkey_data,
  1845. bool private)
  1846. {
  1847. if (obs_context_data_init_wrap(context, type, settings, name,
  1848. hotkey_data, private)) {
  1849. return true;
  1850. } else {
  1851. obs_context_data_free(context);
  1852. return false;
  1853. }
  1854. }
  1855. void obs_context_data_free(struct obs_context_data *context)
  1856. {
  1857. obs_hotkeys_context_release(context);
  1858. signal_handler_destroy(context->signals);
  1859. proc_handler_destroy(context->procs);
  1860. obs_data_release(context->settings);
  1861. obs_context_data_remove(context);
  1862. pthread_mutex_destroy(&context->rename_cache_mutex);
  1863. bfree(context->name);
  1864. for (size_t i = 0; i < context->rename_cache.num; i++)
  1865. bfree(context->rename_cache.array[i]);
  1866. da_free(context->rename_cache);
  1867. memset(context, 0, sizeof(*context));
  1868. }
  1869. void obs_context_init_control(struct obs_context_data *context, void *object,
  1870. obs_destroy_cb destroy)
  1871. {
  1872. context->control = bzalloc(sizeof(obs_weak_object_t));
  1873. context->control->object = object;
  1874. context->destroy = destroy;
  1875. }
  1876. void obs_context_data_insert(struct obs_context_data *context,
  1877. pthread_mutex_t *mutex, void *pfirst)
  1878. {
  1879. struct obs_context_data **first = pfirst;
  1880. assert(context);
  1881. assert(mutex);
  1882. assert(first);
  1883. context->mutex = mutex;
  1884. pthread_mutex_lock(mutex);
  1885. context->prev_next = first;
  1886. context->next = *first;
  1887. *first = context;
  1888. if (context->next)
  1889. context->next->prev_next = &context->next;
  1890. pthread_mutex_unlock(mutex);
  1891. }
  1892. void obs_context_data_remove(struct obs_context_data *context)
  1893. {
  1894. if (context && context->prev_next) {
  1895. pthread_mutex_lock(context->mutex);
  1896. *context->prev_next = context->next;
  1897. if (context->next)
  1898. context->next->prev_next = context->prev_next;
  1899. context->prev_next = NULL;
  1900. pthread_mutex_unlock(context->mutex);
  1901. }
  1902. }
  1903. void obs_context_wait(struct obs_context_data *context)
  1904. {
  1905. pthread_mutex_lock(context->mutex);
  1906. pthread_mutex_unlock(context->mutex);
  1907. }
  1908. void obs_context_data_setname(struct obs_context_data *context,
  1909. const char *name)
  1910. {
  1911. pthread_mutex_lock(&context->rename_cache_mutex);
  1912. if (context->name)
  1913. da_push_back(context->rename_cache, &context->name);
  1914. context->name = dup_name(name, context->private);
  1915. pthread_mutex_unlock(&context->rename_cache_mutex);
  1916. }
  1917. profiler_name_store_t *obs_get_profiler_name_store(void)
  1918. {
  1919. return obs->name_store;
  1920. }
  1921. uint64_t obs_get_video_frame_time(void)
  1922. {
  1923. return obs->video.video_time;
  1924. }
  1925. double obs_get_active_fps(void)
  1926. {
  1927. return obs->video.video_fps;
  1928. }
  1929. uint64_t obs_get_average_frame_time_ns(void)
  1930. {
  1931. return obs->video.video_avg_frame_time_ns;
  1932. }
  1933. uint64_t obs_get_frame_interval_ns(void)
  1934. {
  1935. return obs->video.video_frame_interval_ns;
  1936. }
  1937. enum obs_obj_type obs_obj_get_type(void *obj)
  1938. {
  1939. struct obs_context_data *context = obj;
  1940. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  1941. }
  1942. const char *obs_obj_get_id(void *obj)
  1943. {
  1944. struct obs_context_data *context = obj;
  1945. if (!context)
  1946. return NULL;
  1947. switch (context->type) {
  1948. case OBS_OBJ_TYPE_SOURCE:
  1949. return ((obs_source_t *)obj)->info.id;
  1950. case OBS_OBJ_TYPE_OUTPUT:
  1951. return ((obs_output_t *)obj)->info.id;
  1952. case OBS_OBJ_TYPE_ENCODER:
  1953. return ((obs_encoder_t *)obj)->info.id;
  1954. case OBS_OBJ_TYPE_SERVICE:
  1955. return ((obs_service_t *)obj)->info.id;
  1956. default:;
  1957. }
  1958. return NULL;
  1959. }
  1960. bool obs_obj_invalid(void *obj)
  1961. {
  1962. struct obs_context_data *context = obj;
  1963. if (!context)
  1964. return true;
  1965. return !context->data;
  1966. }
  1967. void *obs_obj_get_data(void *obj)
  1968. {
  1969. struct obs_context_data *context = obj;
  1970. if (!context)
  1971. return NULL;
  1972. return context->data;
  1973. }
  1974. bool obs_obj_is_private(void *obj)
  1975. {
  1976. struct obs_context_data *context = obj;
  1977. if (!context)
  1978. return false;
  1979. return context->private;
  1980. }
  1981. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  1982. {
  1983. if (!name || !id || !*name || !*id)
  1984. return false;
  1985. if (!obs_audio_monitoring_available())
  1986. return false;
  1987. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  1988. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  1989. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  1990. return true;
  1991. }
  1992. bfree(obs->audio.monitoring_device_name);
  1993. bfree(obs->audio.monitoring_device_id);
  1994. obs->audio.monitoring_device_name = bstrdup(name);
  1995. obs->audio.monitoring_device_id = bstrdup(id);
  1996. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  1997. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  1998. audio_monitor_reset(monitor);
  1999. }
  2000. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  2001. return true;
  2002. }
  2003. void obs_get_audio_monitoring_device(const char **name, const char **id)
  2004. {
  2005. if (name)
  2006. *name = obs->audio.monitoring_device_name;
  2007. if (id)
  2008. *id = obs->audio.monitoring_device_id;
  2009. }
  2010. void obs_add_tick_callback(void (*tick)(void *param, float seconds),
  2011. void *param)
  2012. {
  2013. struct tick_callback data = {tick, param};
  2014. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2015. da_insert(obs->data.tick_callbacks, 0, &data);
  2016. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2017. }
  2018. void obs_remove_tick_callback(void (*tick)(void *param, float seconds),
  2019. void *param)
  2020. {
  2021. struct tick_callback data = {tick, param};
  2022. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2023. da_erase_item(obs->data.tick_callbacks, &data);
  2024. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2025. }
  2026. void obs_add_main_render_callback(void (*draw)(void *param, uint32_t cx,
  2027. uint32_t cy),
  2028. void *param)
  2029. {
  2030. struct draw_callback data = {draw, param};
  2031. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2032. da_insert(obs->data.draw_callbacks, 0, &data);
  2033. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2034. }
  2035. void obs_remove_main_render_callback(void (*draw)(void *param, uint32_t cx,
  2036. uint32_t cy),
  2037. void *param)
  2038. {
  2039. struct draw_callback data = {draw, param};
  2040. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2041. da_erase_item(obs->data.draw_callbacks, &data);
  2042. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2043. }
  2044. uint32_t obs_get_total_frames(void)
  2045. {
  2046. return obs->video.total_frames;
  2047. }
  2048. uint32_t obs_get_lagged_frames(void)
  2049. {
  2050. return obs->video.lagged_frames;
  2051. }
  2052. void start_raw_video(video_t *v, const struct video_scale_info *conversion,
  2053. void (*callback)(void *param, struct video_data *frame),
  2054. void *param)
  2055. {
  2056. struct obs_core_video *video = &obs->video;
  2057. os_atomic_inc_long(&video->raw_active);
  2058. video_output_connect(v, conversion, callback, param);
  2059. }
  2060. void stop_raw_video(video_t *v,
  2061. void (*callback)(void *param, struct video_data *frame),
  2062. void *param)
  2063. {
  2064. struct obs_core_video *video = &obs->video;
  2065. os_atomic_dec_long(&video->raw_active);
  2066. video_output_disconnect(v, callback, param);
  2067. }
  2068. void obs_add_raw_video_callback(const struct video_scale_info *conversion,
  2069. void (*callback)(void *param,
  2070. struct video_data *frame),
  2071. void *param)
  2072. {
  2073. struct obs_core_video *video = &obs->video;
  2074. start_raw_video(video->video, conversion, callback, param);
  2075. }
  2076. void obs_remove_raw_video_callback(void (*callback)(void *param,
  2077. struct video_data *frame),
  2078. void *param)
  2079. {
  2080. struct obs_core_video *video = &obs->video;
  2081. stop_raw_video(video->video, callback, param);
  2082. }
  2083. void obs_add_raw_audio_callback(size_t mix_idx,
  2084. const struct audio_convert_info *conversion,
  2085. audio_output_callback_t callback, void *param)
  2086. {
  2087. struct obs_core_audio *audio = &obs->audio;
  2088. audio_output_connect(audio->audio, mix_idx, conversion, callback,
  2089. param);
  2090. }
  2091. void obs_remove_raw_audio_callback(size_t mix_idx,
  2092. audio_output_callback_t callback,
  2093. void *param)
  2094. {
  2095. struct obs_core_audio *audio = &obs->audio;
  2096. audio_output_disconnect(audio->audio, mix_idx, callback, param);
  2097. }
  2098. void obs_apply_private_data(obs_data_t *settings)
  2099. {
  2100. if (!settings)
  2101. return;
  2102. obs_data_apply(obs->data.private_data, settings);
  2103. }
  2104. void obs_set_private_data(obs_data_t *settings)
  2105. {
  2106. obs_data_clear(obs->data.private_data);
  2107. if (settings)
  2108. obs_data_apply(obs->data.private_data, settings);
  2109. }
  2110. obs_data_t *obs_get_private_data(void)
  2111. {
  2112. obs_data_t *private_data = obs->data.private_data;
  2113. obs_data_addref(private_data);
  2114. return private_data;
  2115. }
  2116. extern bool init_gpu_encoding(struct obs_core_video *video);
  2117. extern void stop_gpu_encoding_thread(struct obs_core_video *video);
  2118. extern void free_gpu_encoding(struct obs_core_video *video);
  2119. bool start_gpu_encode(obs_encoder_t *encoder)
  2120. {
  2121. struct obs_core_video *video = &obs->video;
  2122. bool success = true;
  2123. obs_enter_graphics();
  2124. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2125. if (!video->gpu_encoders.num)
  2126. success = init_gpu_encoding(video);
  2127. if (success)
  2128. da_push_back(video->gpu_encoders, &encoder);
  2129. else
  2130. free_gpu_encoding(video);
  2131. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2132. obs_leave_graphics();
  2133. if (success) {
  2134. os_atomic_inc_long(&video->gpu_encoder_active);
  2135. video_output_inc_texture_encoders(video->video);
  2136. }
  2137. return success;
  2138. }
  2139. void stop_gpu_encode(obs_encoder_t *encoder)
  2140. {
  2141. struct obs_core_video *video = &obs->video;
  2142. bool call_free = false;
  2143. os_atomic_dec_long(&video->gpu_encoder_active);
  2144. video_output_dec_texture_encoders(video->video);
  2145. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2146. da_erase_item(video->gpu_encoders, &encoder);
  2147. if (!video->gpu_encoders.num)
  2148. call_free = true;
  2149. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2150. os_event_wait(video->gpu_encode_inactive);
  2151. if (call_free) {
  2152. stop_gpu_encoding_thread(video);
  2153. obs_enter_graphics();
  2154. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2155. free_gpu_encoding(video);
  2156. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2157. obs_leave_graphics();
  2158. }
  2159. }
  2160. bool obs_video_active(void)
  2161. {
  2162. struct obs_core_video *video = &obs->video;
  2163. return os_atomic_load_long(&video->raw_active) > 0 ||
  2164. os_atomic_load_long(&video->gpu_encoder_active) > 0;
  2165. }
  2166. bool obs_nv12_tex_active(void)
  2167. {
  2168. struct obs_core_video *video = &obs->video;
  2169. return video->using_nv12_tex;
  2170. }
  2171. /* ------------------------------------------------------------------------- */
  2172. /* task stuff */
  2173. struct task_wait_info {
  2174. obs_task_t task;
  2175. void *param;
  2176. os_event_t *event;
  2177. };
  2178. static void task_wait_callback(void *param)
  2179. {
  2180. struct task_wait_info *info = param;
  2181. if (info->task)
  2182. info->task(info->param);
  2183. os_event_signal(info->event);
  2184. }
  2185. THREAD_LOCAL bool is_graphics_thread = false;
  2186. THREAD_LOCAL bool is_audio_thread = false;
  2187. static void set_audio_thread(void *unused)
  2188. {
  2189. is_audio_thread = true;
  2190. UNUSED_PARAMETER(unused);
  2191. }
  2192. bool obs_in_task_thread(enum obs_task_type type)
  2193. {
  2194. if (type == OBS_TASK_GRAPHICS)
  2195. return is_graphics_thread;
  2196. else if (type == OBS_TASK_AUDIO)
  2197. return is_audio_thread;
  2198. else if (type == OBS_TASK_UI)
  2199. return is_ui_thread;
  2200. else if (type == OBS_TASK_DESTROY)
  2201. return os_task_queue_inside(obs->destruction_task_thread);
  2202. assert(false);
  2203. return false;
  2204. }
  2205. void obs_queue_task(enum obs_task_type type, obs_task_t task, void *param,
  2206. bool wait)
  2207. {
  2208. if (type == OBS_TASK_UI) {
  2209. if (obs->ui_task_handler) {
  2210. obs->ui_task_handler(task, param, wait);
  2211. } else {
  2212. blog(LOG_ERROR, "UI task could not be queued, "
  2213. "there's no UI task handler!");
  2214. }
  2215. } else {
  2216. if (obs_in_task_thread(type)) {
  2217. task(param);
  2218. } else if (wait) {
  2219. struct task_wait_info info = {
  2220. .task = task,
  2221. .param = param,
  2222. };
  2223. os_event_init(&info.event, OS_EVENT_TYPE_MANUAL);
  2224. obs_queue_task(type, task_wait_callback, &info, false);
  2225. os_event_wait(info.event);
  2226. os_event_destroy(info.event);
  2227. } else if (type == OBS_TASK_GRAPHICS) {
  2228. struct obs_core_video *video = &obs->video;
  2229. struct obs_task_info info = {task, param};
  2230. pthread_mutex_lock(&video->task_mutex);
  2231. circlebuf_push_back(&video->tasks, &info, sizeof(info));
  2232. pthread_mutex_unlock(&video->task_mutex);
  2233. } else if (type == OBS_TASK_AUDIO) {
  2234. struct obs_core_audio *audio = &obs->audio;
  2235. struct obs_task_info info = {task, param};
  2236. pthread_mutex_lock(&audio->task_mutex);
  2237. circlebuf_push_back(&audio->tasks, &info, sizeof(info));
  2238. pthread_mutex_unlock(&audio->task_mutex);
  2239. } else if (type == OBS_TASK_DESTROY) {
  2240. os_task_t os_task = (os_task_t)task;
  2241. os_task_queue_queue_task(obs->destruction_task_thread,
  2242. os_task, param);
  2243. }
  2244. }
  2245. }
  2246. bool obs_wait_for_destroy_queue(void)
  2247. {
  2248. struct task_wait_info info = {0};
  2249. if (!obs->video.thread_initialized || !obs->audio.audio)
  2250. return false;
  2251. /* allow video and audio threads time to release objects */
  2252. os_event_init(&info.event, OS_EVENT_TYPE_AUTO);
  2253. obs_queue_task(OBS_TASK_GRAPHICS, task_wait_callback, &info, false);
  2254. os_event_wait(info.event);
  2255. obs_queue_task(OBS_TASK_AUDIO, task_wait_callback, &info, false);
  2256. os_event_wait(info.event);
  2257. os_event_destroy(info.event);
  2258. /* wait for destroy task queue */
  2259. return os_task_queue_wait(obs->destruction_task_thread);
  2260. }
  2261. static void set_ui_thread(void *unused)
  2262. {
  2263. is_ui_thread = true;
  2264. UNUSED_PARAMETER(unused);
  2265. }
  2266. void obs_set_ui_task_handler(obs_task_handler_t handler)
  2267. {
  2268. obs->ui_task_handler = handler;
  2269. obs_queue_task(OBS_TASK_UI, set_ui_thread, NULL, false);
  2270. }
  2271. obs_object_t *obs_object_get_ref(obs_object_t *object)
  2272. {
  2273. if (!object)
  2274. return NULL;
  2275. return obs_weak_object_get_object(object->control);
  2276. }
  2277. void obs_object_release(obs_object_t *object)
  2278. {
  2279. if (!obs) {
  2280. blog(LOG_WARNING, "Tried to release an object when the OBS "
  2281. "core is shut down!");
  2282. return;
  2283. }
  2284. if (!object)
  2285. return;
  2286. obs_weak_object_t *control = object->control;
  2287. if (obs_ref_release(&control->ref)) {
  2288. object->destroy(object);
  2289. obs_weak_object_release(control);
  2290. }
  2291. }
  2292. void obs_weak_object_addref(obs_weak_object_t *weak)
  2293. {
  2294. if (!weak)
  2295. return;
  2296. obs_weak_ref_addref(&weak->ref);
  2297. }
  2298. void obs_weak_object_release(obs_weak_object_t *weak)
  2299. {
  2300. if (!weak)
  2301. return;
  2302. if (obs_weak_ref_release(&weak->ref))
  2303. bfree(weak);
  2304. }
  2305. obs_weak_object_t *obs_object_get_weak_object(obs_object_t *object)
  2306. {
  2307. if (!object)
  2308. return NULL;
  2309. obs_weak_object_t *weak = object->control;
  2310. obs_weak_object_addref(weak);
  2311. return weak;
  2312. }
  2313. obs_object_t *obs_weak_object_get_object(obs_weak_object_t *weak)
  2314. {
  2315. if (!weak)
  2316. return NULL;
  2317. if (obs_weak_ref_get_ref(&weak->ref))
  2318. return weak->object;
  2319. return NULL;
  2320. }
  2321. bool obs_weak_object_expired(obs_weak_object_t *weak)
  2322. {
  2323. return weak ? obs_weak_ref_expired(&weak->ref) : true;
  2324. }
  2325. bool obs_weak_object_references_object(obs_weak_object_t *weak,
  2326. obs_object_t *object)
  2327. {
  2328. return weak && object && weak->object == object;
  2329. }