obs.c 89 KB

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