obs.c 79 KB

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