obs.c 91 KB

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