obs.c 76 KB

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