obs.c 90 KB

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