obs.c 80 KB

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