obs.c 95 KB

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