obs.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104
  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_update(ptr source)",
  832. "void source_save(ptr source)",
  833. "void source_load(ptr source)",
  834. "void source_activate(ptr source)",
  835. "void source_deactivate(ptr source)",
  836. "void source_show(ptr source)",
  837. "void source_hide(ptr source)",
  838. "void source_audio_activate(ptr source)",
  839. "void source_audio_deactivate(ptr source)",
  840. "void source_rename(ptr source, string new_name, string prev_name)",
  841. "void source_volume(ptr source, in out float volume)",
  842. "void source_volume_level(ptr source, float level, float magnitude, "
  843. "float peak)",
  844. "void source_transition_start(ptr source)",
  845. "void source_transition_video_stop(ptr source)",
  846. "void source_transition_stop(ptr source)",
  847. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  848. "void master_volume(in out float volume)",
  849. "void hotkey_layout_change()",
  850. "void hotkey_register(ptr hotkey)",
  851. "void hotkey_unregister(ptr hotkey)",
  852. "void hotkey_bindings_changed(ptr hotkey)",
  853. NULL,
  854. };
  855. static inline bool obs_init_handlers(void)
  856. {
  857. obs->signals = signal_handler_create();
  858. if (!obs->signals)
  859. return false;
  860. obs->procs = proc_handler_create();
  861. if (!obs->procs)
  862. return false;
  863. return signal_handler_add_array(obs->signals, obs_signals);
  864. }
  865. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  866. static inline bool obs_init_hotkeys(void)
  867. {
  868. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  869. bool success = false;
  870. assert(hotkeys != NULL);
  871. da_init(hotkeys->hotkeys);
  872. hotkeys->signals = obs->signals;
  873. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  874. hotkeys->mute = bstrdup("Mute");
  875. hotkeys->unmute = bstrdup("Unmute");
  876. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  877. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  878. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  879. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  880. if (!obs_hotkeys_platform_init(hotkeys))
  881. return false;
  882. if (pthread_mutex_init_recursive(&hotkeys->mutex) != 0)
  883. goto fail;
  884. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  885. goto fail;
  886. if (pthread_create(&hotkeys->hotkey_thread, NULL, obs_hotkey_thread,
  887. NULL))
  888. goto fail;
  889. hotkeys->strict_modifiers = true;
  890. hotkeys->hotkey_thread_initialized = true;
  891. success = true;
  892. fail:
  893. return success;
  894. }
  895. static inline void stop_hotkeys(void)
  896. {
  897. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  898. void *thread_ret;
  899. if (hotkeys->hotkey_thread_initialized) {
  900. os_event_signal(hotkeys->stop_event);
  901. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  902. hotkeys->hotkey_thread_initialized = false;
  903. }
  904. os_event_destroy(hotkeys->stop_event);
  905. obs_hotkeys_free();
  906. }
  907. static inline void obs_free_hotkeys(void)
  908. {
  909. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  910. bfree(hotkeys->mute);
  911. bfree(hotkeys->unmute);
  912. bfree(hotkeys->push_to_mute);
  913. bfree(hotkeys->push_to_talk);
  914. bfree(hotkeys->sceneitem_show);
  915. bfree(hotkeys->sceneitem_hide);
  916. obs_hotkey_name_map_free();
  917. obs_hotkeys_platform_free(hotkeys);
  918. pthread_mutex_destroy(&hotkeys->mutex);
  919. }
  920. extern const struct obs_source_info scene_info;
  921. extern const struct obs_source_info group_info;
  922. static const char *submix_name(void *unused)
  923. {
  924. UNUSED_PARAMETER(unused);
  925. return "Audio line (internal use only)";
  926. }
  927. const struct obs_source_info audio_line_info = {
  928. .id = "audio_line",
  929. .type = OBS_SOURCE_TYPE_INPUT,
  930. .output_flags = OBS_SOURCE_AUDIO | OBS_SOURCE_CAP_DISABLED |
  931. OBS_SOURCE_SUBMIX,
  932. .get_name = submix_name,
  933. };
  934. extern void log_system_info(void);
  935. static bool obs_init(const char *locale, const char *module_config_path,
  936. profiler_name_store_t *store)
  937. {
  938. obs = bzalloc(sizeof(struct obs_core));
  939. pthread_mutex_init_value(&obs->audio.monitoring_mutex);
  940. pthread_mutex_init_value(&obs->audio.task_mutex);
  941. pthread_mutex_init_value(&obs->video.task_mutex);
  942. pthread_mutex_init_value(&obs->video.mixes_mutex);
  943. obs->name_store_owned = !store;
  944. obs->name_store = store ? store : profiler_name_store_create();
  945. if (!obs->name_store) {
  946. blog(LOG_ERROR, "Couldn't create profiler name store");
  947. return false;
  948. }
  949. log_system_info();
  950. if (!obs_init_data())
  951. return false;
  952. if (!obs_init_handlers())
  953. return false;
  954. if (!obs_init_hotkeys())
  955. return false;
  956. obs->destruction_task_thread = os_task_queue_create();
  957. if (!obs->destruction_task_thread)
  958. return false;
  959. if (module_config_path)
  960. obs->module_config_path = bstrdup(module_config_path);
  961. obs->locale = bstrdup(locale);
  962. obs_register_source(&scene_info);
  963. obs_register_source(&group_info);
  964. obs_register_source(&audio_line_info);
  965. add_default_module_paths();
  966. return true;
  967. }
  968. #ifdef _WIN32
  969. extern bool initialize_com(void);
  970. extern void uninitialize_com(void);
  971. static bool com_initialized = false;
  972. #endif
  973. /* Separate from actual context initialization
  974. * since this can be set before startup and persist
  975. * after shutdown. */
  976. static DARRAY(struct dstr) core_module_paths = {0};
  977. char *obs_find_data_file(const char *file)
  978. {
  979. struct dstr path = {0};
  980. char *result = find_libobs_data_file(file);
  981. if (result)
  982. return result;
  983. for (size_t i = 0; i < core_module_paths.num; ++i) {
  984. if (check_path(file, core_module_paths.array[i].array, &path))
  985. return path.array;
  986. }
  987. dstr_free(&path);
  988. return NULL;
  989. }
  990. void obs_add_data_path(const char *path)
  991. {
  992. struct dstr *new_path = da_push_back_new(core_module_paths);
  993. dstr_init_copy(new_path, path);
  994. }
  995. bool obs_remove_data_path(const char *path)
  996. {
  997. for (size_t i = 0; i < core_module_paths.num; ++i) {
  998. int result = dstr_cmp(&core_module_paths.array[i], path);
  999. if (result == 0) {
  1000. dstr_free(&core_module_paths.array[i]);
  1001. da_erase(core_module_paths, i);
  1002. return true;
  1003. }
  1004. }
  1005. return false;
  1006. }
  1007. static const char *obs_startup_name = "obs_startup";
  1008. bool obs_startup(const char *locale, const char *module_config_path,
  1009. profiler_name_store_t *store)
  1010. {
  1011. bool success;
  1012. profile_start(obs_startup_name);
  1013. if (obs) {
  1014. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  1015. return false;
  1016. }
  1017. #ifdef _WIN32
  1018. com_initialized = initialize_com();
  1019. #endif
  1020. success = obs_init(locale, module_config_path, store);
  1021. profile_end(obs_startup_name);
  1022. if (!success)
  1023. obs_shutdown();
  1024. return success;
  1025. }
  1026. static struct obs_cmdline_args cmdline_args = {0, NULL};
  1027. void obs_set_cmdline_args(int argc, const char *const *argv)
  1028. {
  1029. char *data;
  1030. size_t len;
  1031. int i;
  1032. /* Once argc is set (non-zero) we shouldn't call again */
  1033. if (cmdline_args.argc)
  1034. return;
  1035. cmdline_args.argc = argc;
  1036. /* Safely copy over argv */
  1037. len = 0;
  1038. for (i = 0; i < argc; i++)
  1039. len += strlen(argv[i]) + 1;
  1040. cmdline_args.argv = bmalloc(sizeof(char *) * (argc + 1) + len);
  1041. data = (char *)cmdline_args.argv + sizeof(char *) * (argc + 1);
  1042. for (i = 0; i < argc; i++) {
  1043. cmdline_args.argv[i] = data;
  1044. len = strlen(argv[i]) + 1;
  1045. memcpy(data, argv[i], len);
  1046. data += len;
  1047. }
  1048. cmdline_args.argv[argc] = NULL;
  1049. }
  1050. struct obs_cmdline_args obs_get_cmdline_args(void)
  1051. {
  1052. return cmdline_args;
  1053. }
  1054. void obs_shutdown(void)
  1055. {
  1056. struct obs_module *module;
  1057. obs_wait_for_destroy_queue();
  1058. for (size_t i = 0; i < obs->source_types.num; i++) {
  1059. struct obs_source_info *item = &obs->source_types.array[i];
  1060. if (item->type_data && item->free_type_data)
  1061. item->free_type_data(item->type_data);
  1062. if (item->id)
  1063. bfree((void *)item->id);
  1064. }
  1065. da_free(obs->source_types);
  1066. #define FREE_REGISTERED_TYPES(structure, list) \
  1067. do { \
  1068. for (size_t i = 0; i < list.num; i++) { \
  1069. struct structure *item = &list.array[i]; \
  1070. if (item->type_data && item->free_type_data) \
  1071. item->free_type_data(item->type_data); \
  1072. } \
  1073. da_free(list); \
  1074. } while (false)
  1075. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  1076. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  1077. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  1078. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  1079. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  1080. #undef FREE_REGISTERED_TYPES
  1081. da_free(obs->input_types);
  1082. da_free(obs->filter_types);
  1083. da_free(obs->transition_types);
  1084. stop_video();
  1085. stop_audio();
  1086. stop_hotkeys();
  1087. module = obs->first_module;
  1088. while (module) {
  1089. struct obs_module *next = module->next;
  1090. free_module(module);
  1091. module = next;
  1092. }
  1093. obs->first_module = NULL;
  1094. obs_free_data();
  1095. obs_free_audio();
  1096. obs_free_video();
  1097. os_task_queue_destroy(obs->destruction_task_thread);
  1098. obs_free_hotkeys();
  1099. obs_free_graphics();
  1100. proc_handler_destroy(obs->procs);
  1101. signal_handler_destroy(obs->signals);
  1102. obs->procs = NULL;
  1103. obs->signals = NULL;
  1104. for (size_t i = 0; i < obs->module_paths.num; i++)
  1105. free_module_path(obs->module_paths.array + i);
  1106. da_free(obs->module_paths);
  1107. if (obs->name_store_owned)
  1108. profiler_name_store_free(obs->name_store);
  1109. bfree(obs->module_config_path);
  1110. bfree(obs->locale);
  1111. bfree(obs);
  1112. obs = NULL;
  1113. bfree(cmdline_args.argv);
  1114. #ifdef _WIN32
  1115. if (com_initialized)
  1116. uninitialize_com();
  1117. #endif
  1118. }
  1119. bool obs_initialized(void)
  1120. {
  1121. return obs != NULL;
  1122. }
  1123. uint32_t obs_get_version(void)
  1124. {
  1125. return LIBOBS_API_VER;
  1126. }
  1127. const char *obs_get_version_string(void)
  1128. {
  1129. return OBS_VERSION;
  1130. }
  1131. void obs_set_locale(const char *locale)
  1132. {
  1133. struct obs_module *module;
  1134. if (obs->locale)
  1135. bfree(obs->locale);
  1136. obs->locale = bstrdup(locale);
  1137. module = obs->first_module;
  1138. while (module) {
  1139. if (module->set_locale)
  1140. module->set_locale(locale);
  1141. module = module->next;
  1142. }
  1143. }
  1144. const char *obs_get_locale(void)
  1145. {
  1146. return obs->locale;
  1147. }
  1148. #define OBS_SIZE_MIN 2
  1149. #define OBS_SIZE_MAX (32 * 1024)
  1150. static inline bool size_valid(uint32_t width, uint32_t height)
  1151. {
  1152. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  1153. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  1154. }
  1155. int obs_reset_video(struct obs_video_info *ovi)
  1156. {
  1157. if (!obs)
  1158. return OBS_VIDEO_FAIL;
  1159. /* don't allow changing of video settings if active. */
  1160. if (obs_video_active())
  1161. return OBS_VIDEO_CURRENTLY_ACTIVE;
  1162. if (!size_valid(ovi->output_width, ovi->output_height) ||
  1163. !size_valid(ovi->base_width, ovi->base_height))
  1164. return OBS_VIDEO_INVALID_PARAM;
  1165. stop_video();
  1166. obs_free_video();
  1167. /* align to multiple-of-two and SSE alignment sizes */
  1168. ovi->output_width &= 0xFFFFFFFC;
  1169. ovi->output_height &= 0xFFFFFFFE;
  1170. if (!obs->video.graphics) {
  1171. int errorcode = obs_init_graphics(ovi);
  1172. if (errorcode != OBS_VIDEO_SUCCESS) {
  1173. obs_free_graphics();
  1174. return errorcode;
  1175. }
  1176. }
  1177. const char *scale_type_name = "";
  1178. switch (ovi->scale_type) {
  1179. case OBS_SCALE_DISABLE:
  1180. scale_type_name = "Disabled";
  1181. break;
  1182. case OBS_SCALE_POINT:
  1183. scale_type_name = "Point";
  1184. break;
  1185. case OBS_SCALE_BICUBIC:
  1186. scale_type_name = "Bicubic";
  1187. break;
  1188. case OBS_SCALE_BILINEAR:
  1189. scale_type_name = "Bilinear";
  1190. break;
  1191. case OBS_SCALE_LANCZOS:
  1192. scale_type_name = "Lanczos";
  1193. break;
  1194. case OBS_SCALE_AREA:
  1195. scale_type_name = "Area";
  1196. break;
  1197. }
  1198. bool yuv = format_is_yuv(ovi->output_format);
  1199. const char *yuv_format = get_video_colorspace_name(ovi->colorspace);
  1200. const char *yuv_range =
  1201. get_video_range_name(ovi->output_format, ovi->range);
  1202. blog(LOG_INFO, "---------------------------------");
  1203. blog(LOG_INFO,
  1204. "video settings reset:\n"
  1205. "\tbase resolution: %dx%d\n"
  1206. "\toutput resolution: %dx%d\n"
  1207. "\tdownscale filter: %s\n"
  1208. "\tfps: %d/%d\n"
  1209. "\tformat: %s\n"
  1210. "\tYUV mode: %s%s%s",
  1211. ovi->base_width, ovi->base_height, ovi->output_width,
  1212. ovi->output_height, scale_type_name, ovi->fps_num, ovi->fps_den,
  1213. get_video_format_name(ovi->output_format),
  1214. yuv ? yuv_format : "None", yuv ? "/" : "", yuv ? yuv_range : "");
  1215. return obs_init_video(ovi);
  1216. }
  1217. #ifndef SEC_TO_MSEC
  1218. #define SEC_TO_MSEC 1000
  1219. #endif
  1220. bool obs_reset_audio2(const struct obs_audio_info2 *oai)
  1221. {
  1222. struct obs_core_audio *audio = &obs->audio;
  1223. struct audio_output_info ai;
  1224. /* don't allow changing of audio settings if active. */
  1225. if (!obs || (audio->audio && audio_output_active(audio->audio)))
  1226. return false;
  1227. obs_free_audio();
  1228. if (!oai)
  1229. return true;
  1230. if (oai->max_buffering_ms) {
  1231. uint32_t max_frames = oai->max_buffering_ms *
  1232. oai->samples_per_sec / SEC_TO_MSEC;
  1233. max_frames += (AUDIO_OUTPUT_FRAMES - 1);
  1234. audio->max_buffering_ticks = max_frames / AUDIO_OUTPUT_FRAMES;
  1235. } else {
  1236. audio->max_buffering_ticks = 45;
  1237. }
  1238. audio->fixed_buffer = oai->fixed_buffering;
  1239. int max_buffering_ms = audio->max_buffering_ticks *
  1240. AUDIO_OUTPUT_FRAMES * SEC_TO_MSEC /
  1241. (int)oai->samples_per_sec;
  1242. ai.name = "Audio";
  1243. ai.samples_per_sec = oai->samples_per_sec;
  1244. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  1245. ai.speakers = oai->speakers;
  1246. ai.input_callback = audio_callback;
  1247. blog(LOG_INFO, "---------------------------------");
  1248. blog(LOG_INFO,
  1249. "audio settings reset:\n"
  1250. "\tsamples per sec: %d\n"
  1251. "\tspeakers: %d\n"
  1252. "\tmax buffering: %d milliseconds\n"
  1253. "\tbuffering type: %s",
  1254. (int)ai.samples_per_sec, (int)ai.speakers, max_buffering_ms,
  1255. oai->fixed_buffering ? "fixed" : "dynamically increasing");
  1256. return obs_init_audio(&ai);
  1257. }
  1258. bool obs_reset_audio(const struct obs_audio_info *oai)
  1259. {
  1260. struct obs_audio_info2 oai2 = {
  1261. .samples_per_sec = oai->samples_per_sec,
  1262. .speakers = oai->speakers,
  1263. };
  1264. return obs_reset_audio2(&oai2);
  1265. }
  1266. bool obs_get_video_info(struct obs_video_info *ovi)
  1267. {
  1268. if (!obs->video.graphics)
  1269. return false;
  1270. *ovi = obs->video.ovi;
  1271. return true;
  1272. }
  1273. float obs_get_video_sdr_white_level(void)
  1274. {
  1275. struct obs_core_video *video = &obs->video;
  1276. return video->graphics ? video->sdr_white_level : 300.f;
  1277. }
  1278. float obs_get_video_hdr_nominal_peak_level(void)
  1279. {
  1280. struct obs_core_video *video = &obs->video;
  1281. return video->graphics ? video->hdr_nominal_peak_level : 1000.f;
  1282. }
  1283. void obs_set_video_levels(float sdr_white_level, float hdr_nominal_peak_level)
  1284. {
  1285. struct obs_core_video *video = &obs->video;
  1286. assert(video->graphics);
  1287. video->sdr_white_level = sdr_white_level;
  1288. video->hdr_nominal_peak_level = hdr_nominal_peak_level;
  1289. }
  1290. bool obs_get_audio_info(struct obs_audio_info *oai)
  1291. {
  1292. struct obs_core_audio *audio = &obs->audio;
  1293. const struct audio_output_info *info;
  1294. if (!oai || !audio->audio)
  1295. return false;
  1296. info = audio_output_get_info(audio->audio);
  1297. oai->samples_per_sec = info->samples_per_sec;
  1298. oai->speakers = info->speakers;
  1299. return true;
  1300. }
  1301. bool obs_enum_source_types(size_t idx, const char **id)
  1302. {
  1303. if (idx >= obs->source_types.num)
  1304. return false;
  1305. *id = obs->source_types.array[idx].id;
  1306. return true;
  1307. }
  1308. bool obs_enum_input_types(size_t idx, const char **id)
  1309. {
  1310. if (idx >= obs->input_types.num)
  1311. return false;
  1312. *id = obs->input_types.array[idx].id;
  1313. return true;
  1314. }
  1315. bool obs_enum_input_types2(size_t idx, const char **id,
  1316. const char **unversioned_id)
  1317. {
  1318. if (idx >= obs->input_types.num)
  1319. return false;
  1320. if (id)
  1321. *id = obs->input_types.array[idx].id;
  1322. if (unversioned_id)
  1323. *unversioned_id = obs->input_types.array[idx].unversioned_id;
  1324. return true;
  1325. }
  1326. const char *obs_get_latest_input_type_id(const char *unversioned_id)
  1327. {
  1328. struct obs_source_info *latest = NULL;
  1329. int version = -1;
  1330. if (!unversioned_id)
  1331. return NULL;
  1332. for (size_t i = 0; i < obs->source_types.num; i++) {
  1333. struct obs_source_info *info = &obs->source_types.array[i];
  1334. if (strcmp(info->unversioned_id, unversioned_id) == 0 &&
  1335. (int)info->version > version) {
  1336. latest = info;
  1337. version = info->version;
  1338. }
  1339. }
  1340. assert(!!latest);
  1341. if (!latest)
  1342. return NULL;
  1343. return latest->id;
  1344. }
  1345. bool obs_enum_filter_types(size_t idx, const char **id)
  1346. {
  1347. if (idx >= obs->filter_types.num)
  1348. return false;
  1349. *id = obs->filter_types.array[idx].id;
  1350. return true;
  1351. }
  1352. bool obs_enum_transition_types(size_t idx, const char **id)
  1353. {
  1354. if (idx >= obs->transition_types.num)
  1355. return false;
  1356. *id = obs->transition_types.array[idx].id;
  1357. return true;
  1358. }
  1359. bool obs_enum_output_types(size_t idx, const char **id)
  1360. {
  1361. if (idx >= obs->output_types.num)
  1362. return false;
  1363. *id = obs->output_types.array[idx].id;
  1364. return true;
  1365. }
  1366. bool obs_enum_encoder_types(size_t idx, const char **id)
  1367. {
  1368. if (idx >= obs->encoder_types.num)
  1369. return false;
  1370. *id = obs->encoder_types.array[idx].id;
  1371. return true;
  1372. }
  1373. bool obs_enum_service_types(size_t idx, const char **id)
  1374. {
  1375. if (idx >= obs->service_types.num)
  1376. return false;
  1377. *id = obs->service_types.array[idx].id;
  1378. return true;
  1379. }
  1380. void obs_enter_graphics(void)
  1381. {
  1382. if (obs->video.graphics)
  1383. gs_enter_context(obs->video.graphics);
  1384. }
  1385. void obs_leave_graphics(void)
  1386. {
  1387. if (obs->video.graphics)
  1388. gs_leave_context();
  1389. }
  1390. audio_t *obs_get_audio(void)
  1391. {
  1392. return obs->audio.audio;
  1393. }
  1394. video_t *obs_get_video(void)
  1395. {
  1396. return obs->video.main_mix->video;
  1397. }
  1398. /* TODO: optimize this later so it's not just O(N) string lookups */
  1399. static inline struct obs_modal_ui *
  1400. get_modal_ui_callback(const char *id, const char *task, const char *target)
  1401. {
  1402. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  1403. struct obs_modal_ui *callback =
  1404. obs->modal_ui_callbacks.array + i;
  1405. if (strcmp(callback->id, id) == 0 &&
  1406. strcmp(callback->task, task) == 0 &&
  1407. strcmp(callback->target, target) == 0)
  1408. return callback;
  1409. }
  1410. return NULL;
  1411. }
  1412. static inline struct obs_modeless_ui *
  1413. get_modeless_ui_callback(const char *id, const char *task, const char *target)
  1414. {
  1415. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  1416. struct obs_modeless_ui *callback;
  1417. callback = obs->modeless_ui_callbacks.array + i;
  1418. if (strcmp(callback->id, id) == 0 &&
  1419. strcmp(callback->task, task) == 0 &&
  1420. strcmp(callback->target, target) == 0)
  1421. return callback;
  1422. }
  1423. return NULL;
  1424. }
  1425. int obs_exec_ui(const char *name, const char *task, const char *target,
  1426. void *data, void *ui_data)
  1427. {
  1428. struct obs_modal_ui *callback;
  1429. int errorcode = OBS_UI_NOTFOUND;
  1430. if (!obs)
  1431. return errorcode;
  1432. callback = get_modal_ui_callback(name, task, target);
  1433. if (callback) {
  1434. bool success = callback->exec(data, ui_data);
  1435. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  1436. }
  1437. return errorcode;
  1438. }
  1439. void *obs_create_ui(const char *name, const char *task, const char *target,
  1440. void *data, void *ui_data)
  1441. {
  1442. struct obs_modeless_ui *callback;
  1443. callback = get_modeless_ui_callback(name, task, target);
  1444. return callback ? callback->create(data, ui_data) : NULL;
  1445. }
  1446. obs_source_t *obs_get_output_source(uint32_t channel)
  1447. {
  1448. return obs_view_get_source(&obs->data.main_view, channel);
  1449. }
  1450. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  1451. {
  1452. assert(channel < MAX_CHANNELS);
  1453. if (channel >= MAX_CHANNELS)
  1454. return;
  1455. struct obs_source *prev_source;
  1456. struct obs_view *view = &obs->data.main_view;
  1457. struct calldata params = {0};
  1458. pthread_mutex_lock(&view->channels_mutex);
  1459. source = obs_source_get_ref(source);
  1460. prev_source = view->channels[channel];
  1461. calldata_set_int(&params, "channel", channel);
  1462. calldata_set_ptr(&params, "prev_source", prev_source);
  1463. calldata_set_ptr(&params, "source", source);
  1464. signal_handler_signal(obs->signals, "channel_change", &params);
  1465. calldata_get_ptr(&params, "source", &source);
  1466. calldata_free(&params);
  1467. view->channels[channel] = source;
  1468. pthread_mutex_unlock(&view->channels_mutex);
  1469. if (source)
  1470. obs_source_activate(source, MAIN_VIEW);
  1471. if (prev_source) {
  1472. obs_source_deactivate(prev_source, MAIN_VIEW);
  1473. obs_source_release(prev_source);
  1474. }
  1475. }
  1476. void obs_enum_sources(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1477. {
  1478. obs_source_t *source;
  1479. pthread_mutex_lock(&obs->data.sources_mutex);
  1480. source = obs->data.first_source;
  1481. while (source) {
  1482. obs_source_t *s = obs_source_get_ref(source);
  1483. if (s) {
  1484. if (strcmp(s->info.id, group_info.id) == 0 &&
  1485. !enum_proc(param, s)) {
  1486. obs_source_release(s);
  1487. break;
  1488. } else if (s->info.type == OBS_SOURCE_TYPE_INPUT &&
  1489. !s->context.private &&
  1490. !enum_proc(param, s)) {
  1491. obs_source_release(s);
  1492. break;
  1493. }
  1494. obs_source_release(s);
  1495. }
  1496. source = (obs_source_t *)source->context.next;
  1497. }
  1498. pthread_mutex_unlock(&obs->data.sources_mutex);
  1499. }
  1500. void obs_enum_scenes(bool (*enum_proc)(void *, obs_source_t *), void *param)
  1501. {
  1502. obs_source_t *source;
  1503. pthread_mutex_lock(&obs->data.sources_mutex);
  1504. source = obs->data.first_source;
  1505. while (source) {
  1506. obs_source_t *s = obs_source_get_ref(source);
  1507. if (s) {
  1508. if (source->info.type == OBS_SOURCE_TYPE_SCENE &&
  1509. !source->context.private && !enum_proc(param, s)) {
  1510. obs_source_release(s);
  1511. break;
  1512. }
  1513. obs_source_release(s);
  1514. }
  1515. source = (obs_source_t *)source->context.next;
  1516. }
  1517. pthread_mutex_unlock(&obs->data.sources_mutex);
  1518. }
  1519. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  1520. void *param)
  1521. {
  1522. struct obs_context_data **start = pstart, *context;
  1523. bool (*enum_proc)(void *, void *) = proc;
  1524. assert(start);
  1525. assert(mutex);
  1526. assert(enum_proc);
  1527. pthread_mutex_lock(mutex);
  1528. context = *start;
  1529. while (context) {
  1530. if (!enum_proc(param, context))
  1531. break;
  1532. context = context->next;
  1533. }
  1534. pthread_mutex_unlock(mutex);
  1535. }
  1536. void obs_enum_all_sources(bool (*enum_proc)(void *, obs_source_t *),
  1537. void *param)
  1538. {
  1539. obs_enum(&obs->data.first_source, &obs->data.sources_mutex, enum_proc,
  1540. param);
  1541. }
  1542. void obs_enum_outputs(bool (*enum_proc)(void *, obs_output_t *), void *param)
  1543. {
  1544. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex, enum_proc,
  1545. param);
  1546. }
  1547. void obs_enum_encoders(bool (*enum_proc)(void *, obs_encoder_t *), void *param)
  1548. {
  1549. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex, enum_proc,
  1550. param);
  1551. }
  1552. void obs_enum_services(bool (*enum_proc)(void *, obs_service_t *), void *param)
  1553. {
  1554. obs_enum(&obs->data.first_service, &obs->data.services_mutex, enum_proc,
  1555. param);
  1556. }
  1557. static inline void *get_context_by_name(void *vfirst, const char *name,
  1558. pthread_mutex_t *mutex,
  1559. void *(*addref)(void *))
  1560. {
  1561. struct obs_context_data **first = vfirst;
  1562. struct obs_context_data *context;
  1563. pthread_mutex_lock(mutex);
  1564. context = *first;
  1565. while (context) {
  1566. if (!context->private && strcmp(context->name, name) == 0) {
  1567. context = addref(context);
  1568. break;
  1569. }
  1570. context = context->next;
  1571. }
  1572. pthread_mutex_unlock(mutex);
  1573. return context;
  1574. }
  1575. static inline void *obs_source_addref_safe_(void *ref)
  1576. {
  1577. return obs_source_get_ref(ref);
  1578. }
  1579. static inline void *obs_output_addref_safe_(void *ref)
  1580. {
  1581. return obs_output_get_ref(ref);
  1582. }
  1583. static inline void *obs_encoder_addref_safe_(void *ref)
  1584. {
  1585. return obs_encoder_get_ref(ref);
  1586. }
  1587. static inline void *obs_service_addref_safe_(void *ref)
  1588. {
  1589. return obs_service_get_ref(ref);
  1590. }
  1591. static inline void *obs_id_(void *data)
  1592. {
  1593. return data;
  1594. }
  1595. obs_source_t *obs_get_source_by_name(const char *name)
  1596. {
  1597. return get_context_by_name(&obs->data.first_source, name,
  1598. &obs->data.sources_mutex,
  1599. obs_source_addref_safe_);
  1600. }
  1601. obs_source_t *obs_get_transition_by_name(const char *name)
  1602. {
  1603. struct obs_source **first = &obs->data.first_source;
  1604. struct obs_source *source;
  1605. pthread_mutex_lock(&obs->data.sources_mutex);
  1606. source = *first;
  1607. while (source) {
  1608. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION &&
  1609. strcmp(source->context.name, name) == 0) {
  1610. source = obs_source_addref_safe_(source);
  1611. break;
  1612. }
  1613. source = (void *)source->context.next;
  1614. }
  1615. pthread_mutex_unlock(&obs->data.sources_mutex);
  1616. return source;
  1617. }
  1618. obs_output_t *obs_get_output_by_name(const char *name)
  1619. {
  1620. return get_context_by_name(&obs->data.first_output, name,
  1621. &obs->data.outputs_mutex,
  1622. obs_output_addref_safe_);
  1623. }
  1624. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1625. {
  1626. return get_context_by_name(&obs->data.first_encoder, name,
  1627. &obs->data.encoders_mutex,
  1628. obs_encoder_addref_safe_);
  1629. }
  1630. obs_service_t *obs_get_service_by_name(const char *name)
  1631. {
  1632. return get_context_by_name(&obs->data.first_service, name,
  1633. &obs->data.services_mutex,
  1634. obs_service_addref_safe_);
  1635. }
  1636. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1637. {
  1638. switch (effect) {
  1639. case OBS_EFFECT_DEFAULT:
  1640. return obs->video.default_effect;
  1641. case OBS_EFFECT_DEFAULT_RECT:
  1642. return obs->video.default_rect_effect;
  1643. case OBS_EFFECT_OPAQUE:
  1644. return obs->video.opaque_effect;
  1645. case OBS_EFFECT_SOLID:
  1646. return obs->video.solid_effect;
  1647. case OBS_EFFECT_REPEAT:
  1648. return obs->video.repeat_effect;
  1649. case OBS_EFFECT_BICUBIC:
  1650. return obs->video.bicubic_effect;
  1651. case OBS_EFFECT_LANCZOS:
  1652. return obs->video.lanczos_effect;
  1653. case OBS_EFFECT_AREA:
  1654. return obs->video.area_effect;
  1655. case OBS_EFFECT_BILINEAR_LOWRES:
  1656. return obs->video.bilinear_lowres_effect;
  1657. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1658. return obs->video.premultiplied_alpha_effect;
  1659. }
  1660. return NULL;
  1661. }
  1662. /* OBS_DEPRECATED */
  1663. gs_effect_t *obs_get_default_rect_effect(void)
  1664. {
  1665. return obs->video.default_rect_effect;
  1666. }
  1667. signal_handler_t *obs_get_signal_handler(void)
  1668. {
  1669. return obs->signals;
  1670. }
  1671. proc_handler_t *obs_get_proc_handler(void)
  1672. {
  1673. return obs->procs;
  1674. }
  1675. /* OBS_DEPRECATED */
  1676. void obs_render_main_view(void)
  1677. {
  1678. obs_view_render(&obs->data.main_view);
  1679. }
  1680. static void obs_render_main_texture_internal(enum gs_blend_type src_c,
  1681. enum gs_blend_type dest_c,
  1682. enum gs_blend_type src_a,
  1683. enum gs_blend_type dest_a)
  1684. {
  1685. struct obs_core_video_mix *video;
  1686. gs_texture_t *tex;
  1687. gs_effect_t *effect;
  1688. gs_eparam_t *param;
  1689. video = obs->video.main_mix;
  1690. if (!video->texture_rendered)
  1691. return;
  1692. const enum gs_color_space source_space = video->render_space;
  1693. const enum gs_color_space current_space = gs_get_color_space();
  1694. const char *tech_name = "Draw";
  1695. float multiplier = 1.f;
  1696. switch (current_space) {
  1697. case GS_CS_SRGB:
  1698. case GS_CS_SRGB_16F:
  1699. if (source_space == GS_CS_709_EXTENDED)
  1700. tech_name = "DrawTonemap";
  1701. break;
  1702. case GS_CS_709_SCRGB:
  1703. tech_name = "DrawMultiply";
  1704. multiplier = obs_get_video_sdr_white_level() / 80.f;
  1705. }
  1706. const bool previous = gs_framebuffer_srgb_enabled();
  1707. gs_enable_framebuffer_srgb(true);
  1708. tex = video->render_texture;
  1709. effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
  1710. param = gs_effect_get_param_by_name(effect, "image");
  1711. gs_effect_set_texture_srgb(param, tex);
  1712. param = gs_effect_get_param_by_name(effect, "multiplier");
  1713. gs_effect_set_float(param, multiplier);
  1714. gs_blend_state_push();
  1715. gs_blend_function_separate(src_c, dest_c, src_a, dest_a);
  1716. while (gs_effect_loop(effect, tech_name))
  1717. gs_draw_sprite(tex, 0, 0, 0);
  1718. gs_blend_state_pop();
  1719. gs_enable_framebuffer_srgb(previous);
  1720. }
  1721. void obs_render_main_texture(void)
  1722. {
  1723. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_INVSRCALPHA,
  1724. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1725. }
  1726. void obs_render_main_texture_src_color_only(void)
  1727. {
  1728. obs_render_main_texture_internal(GS_BLEND_ONE, GS_BLEND_ZERO,
  1729. GS_BLEND_ONE, GS_BLEND_INVSRCALPHA);
  1730. }
  1731. gs_texture_t *obs_get_main_texture(void)
  1732. {
  1733. struct obs_core_video_mix *video;
  1734. video = obs->video.main_mix;
  1735. if (!video->texture_rendered)
  1736. return NULL;
  1737. return video->render_texture;
  1738. }
  1739. void obs_set_master_volume(float volume)
  1740. {
  1741. struct calldata data = {0};
  1742. calldata_set_float(&data, "volume", volume);
  1743. signal_handler_signal(obs->signals, "master_volume", &data);
  1744. volume = (float)calldata_float(&data, "volume");
  1745. calldata_free(&data);
  1746. obs->audio.user_volume = volume;
  1747. }
  1748. float obs_get_master_volume(void)
  1749. {
  1750. return obs->audio.user_volume;
  1751. }
  1752. static obs_source_t *obs_load_source_type(obs_data_t *source_data,
  1753. bool is_private)
  1754. {
  1755. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1756. obs_source_t *source;
  1757. const char *name = obs_data_get_string(source_data, "name");
  1758. const char *id = obs_data_get_string(source_data, "id");
  1759. const char *v_id = obs_data_get_string(source_data, "versioned_id");
  1760. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1761. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1762. double volume;
  1763. double balance;
  1764. int64_t sync;
  1765. uint32_t prev_ver;
  1766. uint32_t caps;
  1767. uint32_t flags;
  1768. uint32_t mixers;
  1769. int di_order;
  1770. int di_mode;
  1771. int monitoring_type;
  1772. prev_ver = (uint32_t)obs_data_get_int(source_data, "prev_ver");
  1773. if (!*v_id)
  1774. v_id = id;
  1775. source = obs_source_create_set_last_ver(v_id, name, settings, hotkeys,
  1776. prev_ver, is_private);
  1777. if (source->owns_info_id) {
  1778. bfree((void *)source->info.unversioned_id);
  1779. source->info.unversioned_id = bstrdup(id);
  1780. }
  1781. obs_data_release(hotkeys);
  1782. caps = obs_source_get_output_flags(source);
  1783. obs_data_set_default_double(source_data, "volume", 1.0);
  1784. volume = obs_data_get_double(source_data, "volume");
  1785. obs_source_set_volume(source, (float)volume);
  1786. obs_data_set_default_double(source_data, "balance", 0.5);
  1787. balance = obs_data_get_double(source_data, "balance");
  1788. obs_source_set_balance_value(source, (float)balance);
  1789. sync = obs_data_get_int(source_data, "sync");
  1790. obs_source_set_sync_offset(source, sync);
  1791. obs_data_set_default_int(source_data, "mixers", 0x3F);
  1792. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1793. obs_source_set_audio_mixers(source, mixers);
  1794. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1795. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1796. obs_source_set_flags(source, flags);
  1797. obs_data_set_default_bool(source_data, "enabled", true);
  1798. obs_source_set_enabled(source,
  1799. obs_data_get_bool(source_data, "enabled"));
  1800. obs_data_set_default_bool(source_data, "muted", false);
  1801. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1802. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1803. obs_source_enable_push_to_mute(
  1804. source, obs_data_get_bool(source_data, "push-to-mute"));
  1805. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1806. obs_source_set_push_to_mute_delay(
  1807. source, obs_data_get_int(source_data, "push-to-mute-delay"));
  1808. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1809. obs_source_enable_push_to_talk(
  1810. source, obs_data_get_bool(source_data, "push-to-talk"));
  1811. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1812. obs_source_set_push_to_talk_delay(
  1813. source, obs_data_get_int(source_data, "push-to-talk-delay"));
  1814. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1815. obs_source_set_deinterlace_mode(source,
  1816. (enum obs_deinterlace_mode)di_mode);
  1817. di_order =
  1818. (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1819. obs_source_set_deinterlace_field_order(
  1820. source, (enum obs_deinterlace_field_order)di_order);
  1821. monitoring_type = (int)obs_data_get_int(source_data, "monitoring_type");
  1822. if (prev_ver < MAKE_SEMANTIC_VERSION(23, 2, 2)) {
  1823. if ((caps & OBS_SOURCE_MONITOR_BY_DEFAULT) != 0) {
  1824. /* updates older sources to enable monitoring
  1825. * automatically if they added monitoring by default in
  1826. * version 24 */
  1827. monitoring_type = OBS_MONITORING_TYPE_MONITOR_ONLY;
  1828. obs_source_set_audio_mixers(source, 0x3F);
  1829. }
  1830. }
  1831. obs_source_set_monitoring_type(
  1832. source, (enum obs_monitoring_type)monitoring_type);
  1833. obs_data_release(source->private_settings);
  1834. source->private_settings =
  1835. obs_data_get_obj(source_data, "private_settings");
  1836. if (!source->private_settings)
  1837. source->private_settings = obs_data_create();
  1838. if (filters) {
  1839. size_t count = obs_data_array_count(filters);
  1840. for (size_t i = 0; i < count; i++) {
  1841. obs_data_t *filter_data =
  1842. obs_data_array_item(filters, i);
  1843. obs_source_t *filter =
  1844. obs_load_source_type(filter_data, true);
  1845. if (filter) {
  1846. obs_source_filter_add(source, filter);
  1847. obs_source_release(filter);
  1848. }
  1849. obs_data_release(filter_data);
  1850. }
  1851. obs_data_array_release(filters);
  1852. }
  1853. obs_data_release(settings);
  1854. return source;
  1855. }
  1856. obs_source_t *obs_load_source(obs_data_t *source_data)
  1857. {
  1858. return obs_load_source_type(source_data, false);
  1859. }
  1860. obs_source_t *obs_load_private_source(obs_data_t *source_data)
  1861. {
  1862. return obs_load_source_type(source_data, true);
  1863. }
  1864. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1865. void *private_data)
  1866. {
  1867. struct obs_core_data *data = &obs->data;
  1868. DARRAY(obs_source_t *) sources;
  1869. size_t count;
  1870. size_t i;
  1871. da_init(sources);
  1872. count = obs_data_array_count(array);
  1873. da_reserve(sources, count);
  1874. pthread_mutex_lock(&data->sources_mutex);
  1875. for (i = 0; i < count; i++) {
  1876. obs_data_t *source_data = obs_data_array_item(array, i);
  1877. obs_source_t *source = obs_load_source(source_data);
  1878. da_push_back(sources, &source);
  1879. obs_data_release(source_data);
  1880. }
  1881. /* tell sources that we want to load */
  1882. for (i = 0; i < sources.num; i++) {
  1883. obs_source_t *source = sources.array[i];
  1884. obs_data_t *source_data = obs_data_array_item(array, i);
  1885. if (source) {
  1886. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1887. obs_transition_load(source, source_data);
  1888. obs_source_load2(source);
  1889. if (cb)
  1890. cb(private_data, source);
  1891. }
  1892. obs_data_release(source_data);
  1893. }
  1894. for (i = 0; i < sources.num; i++)
  1895. obs_source_release(sources.array[i]);
  1896. pthread_mutex_unlock(&data->sources_mutex);
  1897. da_free(sources);
  1898. }
  1899. obs_data_t *obs_save_source(obs_source_t *source)
  1900. {
  1901. obs_data_array_t *filters = obs_data_array_create();
  1902. obs_data_t *source_data = obs_data_create();
  1903. obs_data_t *settings = obs_source_get_settings(source);
  1904. obs_data_t *hotkey_data = source->context.hotkey_data;
  1905. obs_data_t *hotkeys;
  1906. float volume = obs_source_get_volume(source);
  1907. float balance = obs_source_get_balance_value(source);
  1908. uint32_t mixers = obs_source_get_audio_mixers(source);
  1909. int64_t sync = obs_source_get_sync_offset(source);
  1910. uint32_t flags = obs_source_get_flags(source);
  1911. const char *name = obs_source_get_name(source);
  1912. const char *id = source->info.unversioned_id;
  1913. const char *v_id = source->info.id;
  1914. bool enabled = obs_source_enabled(source);
  1915. bool muted = obs_source_muted(source);
  1916. bool push_to_mute = obs_source_push_to_mute_enabled(source);
  1917. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1918. bool push_to_talk = obs_source_push_to_talk_enabled(source);
  1919. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1920. int m_type = (int)obs_source_get_monitoring_type(source);
  1921. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1922. int di_order = (int)obs_source_get_deinterlace_field_order(source);
  1923. obs_source_save(source);
  1924. hotkeys = obs_hotkeys_save_source(source);
  1925. if (hotkeys) {
  1926. obs_data_release(hotkey_data);
  1927. source->context.hotkey_data = hotkeys;
  1928. hotkey_data = hotkeys;
  1929. }
  1930. obs_data_set_int(source_data, "prev_ver", LIBOBS_API_VER);
  1931. obs_data_set_string(source_data, "name", name);
  1932. obs_data_set_string(source_data, "id", id);
  1933. obs_data_set_string(source_data, "versioned_id", v_id);
  1934. obs_data_set_obj(source_data, "settings", settings);
  1935. obs_data_set_int(source_data, "mixers", mixers);
  1936. obs_data_set_int(source_data, "sync", sync);
  1937. obs_data_set_int(source_data, "flags", flags);
  1938. obs_data_set_double(source_data, "volume", volume);
  1939. obs_data_set_double(source_data, "balance", balance);
  1940. obs_data_set_bool(source_data, "enabled", enabled);
  1941. obs_data_set_bool(source_data, "muted", muted);
  1942. obs_data_set_bool(source_data, "push-to-mute", push_to_mute);
  1943. obs_data_set_int(source_data, "push-to-mute-delay", ptm_delay);
  1944. obs_data_set_bool(source_data, "push-to-talk", push_to_talk);
  1945. obs_data_set_int(source_data, "push-to-talk-delay", ptt_delay);
  1946. obs_data_set_obj(source_data, "hotkeys", hotkey_data);
  1947. obs_data_set_int(source_data, "deinterlace_mode", di_mode);
  1948. obs_data_set_int(source_data, "deinterlace_field_order", di_order);
  1949. obs_data_set_int(source_data, "monitoring_type", m_type);
  1950. obs_data_set_obj(source_data, "private_settings",
  1951. source->private_settings);
  1952. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1953. obs_transition_save(source, source_data);
  1954. pthread_mutex_lock(&source->filter_mutex);
  1955. if (source->filters.num) {
  1956. for (size_t i = source->filters.num; i > 0; i--) {
  1957. obs_source_t *filter = source->filters.array[i - 1];
  1958. obs_data_t *filter_data = obs_save_source(filter);
  1959. obs_data_array_push_back(filters, filter_data);
  1960. obs_data_release(filter_data);
  1961. }
  1962. obs_data_set_array(source_data, "filters", filters);
  1963. }
  1964. pthread_mutex_unlock(&source->filter_mutex);
  1965. obs_data_release(settings);
  1966. obs_data_array_release(filters);
  1967. return source_data;
  1968. }
  1969. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1970. void *data_)
  1971. {
  1972. struct obs_core_data *data = &obs->data;
  1973. obs_data_array_t *array;
  1974. obs_source_t *source;
  1975. array = obs_data_array_create();
  1976. pthread_mutex_lock(&data->sources_mutex);
  1977. source = data->first_source;
  1978. while (source) {
  1979. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1980. !source->context.private && !source->removed &&
  1981. !source->temp_removed && cb(data_, source)) {
  1982. obs_data_t *source_data = obs_save_source(source);
  1983. obs_data_array_push_back(array, source_data);
  1984. obs_data_release(source_data);
  1985. }
  1986. source = (obs_source_t *)source->context.next;
  1987. }
  1988. pthread_mutex_unlock(&data->sources_mutex);
  1989. return array;
  1990. }
  1991. static bool save_source_filter(void *data, obs_source_t *source)
  1992. {
  1993. UNUSED_PARAMETER(data);
  1994. UNUSED_PARAMETER(source);
  1995. return true;
  1996. }
  1997. obs_data_array_t *obs_save_sources(void)
  1998. {
  1999. return obs_save_sources_filtered(save_source_filter, NULL);
  2000. }
  2001. /* ensures that names are never blank */
  2002. static inline char *dup_name(const char *name, bool private)
  2003. {
  2004. if (private && !name)
  2005. return NULL;
  2006. if (!name || !*name) {
  2007. struct dstr unnamed = {0};
  2008. dstr_printf(&unnamed, "__unnamed%04lld",
  2009. obs->data.unnamed_index++);
  2010. return unnamed.array;
  2011. } else {
  2012. return bstrdup(name);
  2013. }
  2014. }
  2015. static inline bool obs_context_data_init_wrap(struct obs_context_data *context,
  2016. enum obs_obj_type type,
  2017. obs_data_t *settings,
  2018. const char *name,
  2019. obs_data_t *hotkey_data,
  2020. bool private)
  2021. {
  2022. assert(context);
  2023. memset(context, 0, sizeof(*context));
  2024. context->private = private;
  2025. context->type = type;
  2026. pthread_mutex_init_value(&context->rename_cache_mutex);
  2027. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  2028. return false;
  2029. context->signals = signal_handler_create();
  2030. if (!context->signals)
  2031. return false;
  2032. context->procs = proc_handler_create();
  2033. if (!context->procs)
  2034. return false;
  2035. context->name = dup_name(name, private);
  2036. context->settings = obs_data_newref(settings);
  2037. context->hotkey_data = obs_data_newref(hotkey_data);
  2038. return true;
  2039. }
  2040. bool obs_context_data_init(struct obs_context_data *context,
  2041. enum obs_obj_type type, obs_data_t *settings,
  2042. const char *name, obs_data_t *hotkey_data,
  2043. bool private)
  2044. {
  2045. if (obs_context_data_init_wrap(context, type, settings, name,
  2046. hotkey_data, private)) {
  2047. return true;
  2048. } else {
  2049. obs_context_data_free(context);
  2050. return false;
  2051. }
  2052. }
  2053. void obs_context_data_free(struct obs_context_data *context)
  2054. {
  2055. obs_hotkeys_context_release(context);
  2056. signal_handler_destroy(context->signals);
  2057. proc_handler_destroy(context->procs);
  2058. obs_data_release(context->settings);
  2059. obs_context_data_remove(context);
  2060. pthread_mutex_destroy(&context->rename_cache_mutex);
  2061. bfree(context->name);
  2062. for (size_t i = 0; i < context->rename_cache.num; i++)
  2063. bfree(context->rename_cache.array[i]);
  2064. da_free(context->rename_cache);
  2065. memset(context, 0, sizeof(*context));
  2066. }
  2067. void obs_context_init_control(struct obs_context_data *context, void *object,
  2068. obs_destroy_cb destroy)
  2069. {
  2070. context->control = bzalloc(sizeof(obs_weak_object_t));
  2071. context->control->object = object;
  2072. context->destroy = destroy;
  2073. }
  2074. void obs_context_data_insert(struct obs_context_data *context,
  2075. pthread_mutex_t *mutex, void *pfirst)
  2076. {
  2077. struct obs_context_data **first = pfirst;
  2078. assert(context);
  2079. assert(mutex);
  2080. assert(first);
  2081. context->mutex = mutex;
  2082. pthread_mutex_lock(mutex);
  2083. context->prev_next = first;
  2084. context->next = *first;
  2085. *first = context;
  2086. if (context->next)
  2087. context->next->prev_next = &context->next;
  2088. pthread_mutex_unlock(mutex);
  2089. }
  2090. void obs_context_data_remove(struct obs_context_data *context)
  2091. {
  2092. if (context && context->prev_next) {
  2093. pthread_mutex_lock(context->mutex);
  2094. *context->prev_next = context->next;
  2095. if (context->next)
  2096. context->next->prev_next = context->prev_next;
  2097. context->prev_next = NULL;
  2098. pthread_mutex_unlock(context->mutex);
  2099. }
  2100. }
  2101. void obs_context_wait(struct obs_context_data *context)
  2102. {
  2103. pthread_mutex_lock(context->mutex);
  2104. pthread_mutex_unlock(context->mutex);
  2105. }
  2106. void obs_context_data_setname(struct obs_context_data *context,
  2107. const char *name)
  2108. {
  2109. pthread_mutex_lock(&context->rename_cache_mutex);
  2110. if (context->name)
  2111. da_push_back(context->rename_cache, &context->name);
  2112. context->name = dup_name(name, context->private);
  2113. pthread_mutex_unlock(&context->rename_cache_mutex);
  2114. }
  2115. profiler_name_store_t *obs_get_profiler_name_store(void)
  2116. {
  2117. return obs->name_store;
  2118. }
  2119. uint64_t obs_get_video_frame_time(void)
  2120. {
  2121. return obs->video.video_time;
  2122. }
  2123. double obs_get_active_fps(void)
  2124. {
  2125. return obs->video.video_fps;
  2126. }
  2127. uint64_t obs_get_average_frame_time_ns(void)
  2128. {
  2129. return obs->video.video_avg_frame_time_ns;
  2130. }
  2131. uint64_t obs_get_frame_interval_ns(void)
  2132. {
  2133. return obs->video.video_frame_interval_ns;
  2134. }
  2135. enum obs_obj_type obs_obj_get_type(void *obj)
  2136. {
  2137. struct obs_context_data *context = obj;
  2138. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  2139. }
  2140. const char *obs_obj_get_id(void *obj)
  2141. {
  2142. struct obs_context_data *context = obj;
  2143. if (!context)
  2144. return NULL;
  2145. switch (context->type) {
  2146. case OBS_OBJ_TYPE_SOURCE:
  2147. return ((obs_source_t *)obj)->info.id;
  2148. case OBS_OBJ_TYPE_OUTPUT:
  2149. return ((obs_output_t *)obj)->info.id;
  2150. case OBS_OBJ_TYPE_ENCODER:
  2151. return ((obs_encoder_t *)obj)->info.id;
  2152. case OBS_OBJ_TYPE_SERVICE:
  2153. return ((obs_service_t *)obj)->info.id;
  2154. default:;
  2155. }
  2156. return NULL;
  2157. }
  2158. bool obs_obj_invalid(void *obj)
  2159. {
  2160. struct obs_context_data *context = obj;
  2161. if (!context)
  2162. return true;
  2163. return !context->data;
  2164. }
  2165. void *obs_obj_get_data(void *obj)
  2166. {
  2167. struct obs_context_data *context = obj;
  2168. if (!context)
  2169. return NULL;
  2170. return context->data;
  2171. }
  2172. bool obs_obj_is_private(void *obj)
  2173. {
  2174. struct obs_context_data *context = obj;
  2175. if (!context)
  2176. return false;
  2177. return context->private;
  2178. }
  2179. bool obs_set_audio_monitoring_device(const char *name, const char *id)
  2180. {
  2181. if (!name || !id || !*name || !*id)
  2182. return false;
  2183. if (!obs_audio_monitoring_available())
  2184. return false;
  2185. pthread_mutex_lock(&obs->audio.monitoring_mutex);
  2186. if (strcmp(id, obs->audio.monitoring_device_id) == 0) {
  2187. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  2188. return true;
  2189. }
  2190. bfree(obs->audio.monitoring_device_name);
  2191. bfree(obs->audio.monitoring_device_id);
  2192. obs->audio.monitoring_device_name = bstrdup(name);
  2193. obs->audio.monitoring_device_id = bstrdup(id);
  2194. for (size_t i = 0; i < obs->audio.monitors.num; i++) {
  2195. struct audio_monitor *monitor = obs->audio.monitors.array[i];
  2196. audio_monitor_reset(monitor);
  2197. }
  2198. pthread_mutex_unlock(&obs->audio.monitoring_mutex);
  2199. return true;
  2200. }
  2201. void obs_get_audio_monitoring_device(const char **name, const char **id)
  2202. {
  2203. if (name)
  2204. *name = obs->audio.monitoring_device_name;
  2205. if (id)
  2206. *id = obs->audio.monitoring_device_id;
  2207. }
  2208. void obs_add_tick_callback(void (*tick)(void *param, float seconds),
  2209. void *param)
  2210. {
  2211. struct tick_callback data = {tick, param};
  2212. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2213. da_insert(obs->data.tick_callbacks, 0, &data);
  2214. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2215. }
  2216. void obs_remove_tick_callback(void (*tick)(void *param, float seconds),
  2217. void *param)
  2218. {
  2219. struct tick_callback data = {tick, param};
  2220. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2221. da_erase_item(obs->data.tick_callbacks, &data);
  2222. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2223. }
  2224. void obs_add_main_render_callback(void (*draw)(void *param, uint32_t cx,
  2225. uint32_t cy),
  2226. void *param)
  2227. {
  2228. struct draw_callback data = {draw, param};
  2229. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2230. da_insert(obs->data.draw_callbacks, 0, &data);
  2231. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2232. }
  2233. void obs_remove_main_render_callback(void (*draw)(void *param, uint32_t cx,
  2234. uint32_t cy),
  2235. void *param)
  2236. {
  2237. struct draw_callback data = {draw, param};
  2238. pthread_mutex_lock(&obs->data.draw_callbacks_mutex);
  2239. da_erase_item(obs->data.draw_callbacks, &data);
  2240. pthread_mutex_unlock(&obs->data.draw_callbacks_mutex);
  2241. }
  2242. uint32_t obs_get_total_frames(void)
  2243. {
  2244. return obs->video.total_frames;
  2245. }
  2246. uint32_t obs_get_lagged_frames(void)
  2247. {
  2248. return obs->video.lagged_frames;
  2249. }
  2250. struct obs_core_video_mix *get_mix_for_video(video_t *v)
  2251. {
  2252. struct obs_core_video_mix *result = NULL;
  2253. pthread_mutex_lock(&obs->video.mixes_mutex);
  2254. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  2255. struct obs_core_video_mix *mix = obs->video.mixes.array[i];
  2256. if (v == mix->video) {
  2257. result = mix;
  2258. break;
  2259. }
  2260. }
  2261. pthread_mutex_unlock(&obs->video.mixes_mutex);
  2262. return result;
  2263. }
  2264. void start_raw_video(video_t *v, const struct video_scale_info *conversion,
  2265. void (*callback)(void *param, struct video_data *frame),
  2266. void *param)
  2267. {
  2268. struct obs_core_video_mix *video = get_mix_for_video(v);
  2269. if (video)
  2270. os_atomic_inc_long(&video->raw_active);
  2271. video_output_connect(v, conversion, callback, param);
  2272. }
  2273. void stop_raw_video(video_t *v,
  2274. void (*callback)(void *param, struct video_data *frame),
  2275. void *param)
  2276. {
  2277. struct obs_core_video_mix *video = get_mix_for_video(v);
  2278. if (video)
  2279. os_atomic_dec_long(&video->raw_active);
  2280. video_output_disconnect(v, callback, param);
  2281. }
  2282. void obs_add_raw_video_callback(const struct video_scale_info *conversion,
  2283. void (*callback)(void *param,
  2284. struct video_data *frame),
  2285. void *param)
  2286. {
  2287. struct obs_core_video_mix *video = obs->video.main_mix;
  2288. start_raw_video(video->video, conversion, callback, param);
  2289. }
  2290. void obs_remove_raw_video_callback(void (*callback)(void *param,
  2291. struct video_data *frame),
  2292. void *param)
  2293. {
  2294. struct obs_core_video_mix *video = obs->video.main_mix;
  2295. stop_raw_video(video->video, callback, param);
  2296. }
  2297. void obs_add_raw_audio_callback(size_t mix_idx,
  2298. const struct audio_convert_info *conversion,
  2299. audio_output_callback_t callback, void *param)
  2300. {
  2301. struct obs_core_audio *audio = &obs->audio;
  2302. audio_output_connect(audio->audio, mix_idx, conversion, callback,
  2303. param);
  2304. }
  2305. void obs_remove_raw_audio_callback(size_t mix_idx,
  2306. audio_output_callback_t callback,
  2307. void *param)
  2308. {
  2309. struct obs_core_audio *audio = &obs->audio;
  2310. audio_output_disconnect(audio->audio, mix_idx, callback, param);
  2311. }
  2312. void obs_apply_private_data(obs_data_t *settings)
  2313. {
  2314. if (!settings)
  2315. return;
  2316. obs_data_apply(obs->data.private_data, settings);
  2317. }
  2318. void obs_set_private_data(obs_data_t *settings)
  2319. {
  2320. obs_data_clear(obs->data.private_data);
  2321. if (settings)
  2322. obs_data_apply(obs->data.private_data, settings);
  2323. }
  2324. obs_data_t *obs_get_private_data(void)
  2325. {
  2326. obs_data_t *private_data = obs->data.private_data;
  2327. obs_data_addref(private_data);
  2328. return private_data;
  2329. }
  2330. extern bool init_gpu_encoding(struct obs_core_video_mix *video);
  2331. extern void stop_gpu_encoding_thread(struct obs_core_video_mix *video);
  2332. extern void free_gpu_encoding(struct obs_core_video_mix *video);
  2333. bool start_gpu_encode(obs_encoder_t *encoder)
  2334. {
  2335. struct obs_core_video_mix *video = obs->video.main_mix;
  2336. bool success = true;
  2337. obs_enter_graphics();
  2338. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2339. if (!video->gpu_encoders.num)
  2340. success = init_gpu_encoding(video);
  2341. if (success)
  2342. da_push_back(video->gpu_encoders, &encoder);
  2343. else
  2344. free_gpu_encoding(video);
  2345. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2346. obs_leave_graphics();
  2347. if (success) {
  2348. os_atomic_inc_long(&video->gpu_encoder_active);
  2349. video_output_inc_texture_encoders(video->video);
  2350. }
  2351. return success;
  2352. }
  2353. void stop_gpu_encode(obs_encoder_t *encoder)
  2354. {
  2355. struct obs_core_video_mix *video = obs->video.main_mix;
  2356. bool call_free = false;
  2357. os_atomic_dec_long(&video->gpu_encoder_active);
  2358. video_output_dec_texture_encoders(video->video);
  2359. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2360. da_erase_item(video->gpu_encoders, &encoder);
  2361. if (!video->gpu_encoders.num)
  2362. call_free = true;
  2363. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2364. os_event_wait(video->gpu_encode_inactive);
  2365. if (call_free) {
  2366. stop_gpu_encoding_thread(video);
  2367. obs_enter_graphics();
  2368. pthread_mutex_lock(&video->gpu_encoder_mutex);
  2369. free_gpu_encoding(video);
  2370. pthread_mutex_unlock(&video->gpu_encoder_mutex);
  2371. obs_leave_graphics();
  2372. }
  2373. }
  2374. bool obs_video_active(void)
  2375. {
  2376. bool result = false;
  2377. pthread_mutex_lock(&obs->video.mixes_mutex);
  2378. for (size_t i = 0, num = obs->video.mixes.num; i < num; i++) {
  2379. struct obs_core_video_mix *video = obs->video.mixes.array[i];
  2380. if (os_atomic_load_long(&video->raw_active) > 0 ||
  2381. os_atomic_load_long(&video->gpu_encoder_active) > 0) {
  2382. result = true;
  2383. break;
  2384. }
  2385. }
  2386. pthread_mutex_unlock(&obs->video.mixes_mutex);
  2387. return result;
  2388. }
  2389. bool obs_nv12_tex_active(void)
  2390. {
  2391. struct obs_core_video_mix *video = obs->video.main_mix;
  2392. return video->using_nv12_tex;
  2393. }
  2394. bool obs_p010_tex_active(void)
  2395. {
  2396. struct obs_core_video_mix *video = obs->video.main_mix;
  2397. return video->using_p010_tex;
  2398. }
  2399. /* ------------------------------------------------------------------------- */
  2400. /* task stuff */
  2401. struct task_wait_info {
  2402. obs_task_t task;
  2403. void *param;
  2404. os_event_t *event;
  2405. };
  2406. static void task_wait_callback(void *param)
  2407. {
  2408. struct task_wait_info *info = param;
  2409. if (info->task)
  2410. info->task(info->param);
  2411. os_event_signal(info->event);
  2412. }
  2413. THREAD_LOCAL bool is_graphics_thread = false;
  2414. THREAD_LOCAL bool is_audio_thread = false;
  2415. static void set_audio_thread(void *unused)
  2416. {
  2417. is_audio_thread = true;
  2418. UNUSED_PARAMETER(unused);
  2419. }
  2420. bool obs_in_task_thread(enum obs_task_type type)
  2421. {
  2422. if (type == OBS_TASK_GRAPHICS)
  2423. return is_graphics_thread;
  2424. else if (type == OBS_TASK_AUDIO)
  2425. return is_audio_thread;
  2426. else if (type == OBS_TASK_UI)
  2427. return is_ui_thread;
  2428. else if (type == OBS_TASK_DESTROY)
  2429. return os_task_queue_inside(obs->destruction_task_thread);
  2430. assert(false);
  2431. return false;
  2432. }
  2433. void obs_queue_task(enum obs_task_type type, obs_task_t task, void *param,
  2434. bool wait)
  2435. {
  2436. if (type == OBS_TASK_UI) {
  2437. if (obs->ui_task_handler) {
  2438. obs->ui_task_handler(task, param, wait);
  2439. } else {
  2440. blog(LOG_ERROR, "UI task could not be queued, "
  2441. "there's no UI task handler!");
  2442. }
  2443. } else {
  2444. if (obs_in_task_thread(type)) {
  2445. task(param);
  2446. } else if (wait) {
  2447. struct task_wait_info info = {
  2448. .task = task,
  2449. .param = param,
  2450. };
  2451. os_event_init(&info.event, OS_EVENT_TYPE_MANUAL);
  2452. obs_queue_task(type, task_wait_callback, &info, false);
  2453. os_event_wait(info.event);
  2454. os_event_destroy(info.event);
  2455. } else if (type == OBS_TASK_GRAPHICS) {
  2456. struct obs_core_video *video = &obs->video;
  2457. struct obs_task_info info = {task, param};
  2458. pthread_mutex_lock(&video->task_mutex);
  2459. circlebuf_push_back(&video->tasks, &info, sizeof(info));
  2460. pthread_mutex_unlock(&video->task_mutex);
  2461. } else if (type == OBS_TASK_AUDIO) {
  2462. struct obs_core_audio *audio = &obs->audio;
  2463. struct obs_task_info info = {task, param};
  2464. pthread_mutex_lock(&audio->task_mutex);
  2465. circlebuf_push_back(&audio->tasks, &info, sizeof(info));
  2466. pthread_mutex_unlock(&audio->task_mutex);
  2467. } else if (type == OBS_TASK_DESTROY) {
  2468. os_task_t os_task = (os_task_t)task;
  2469. os_task_queue_queue_task(obs->destruction_task_thread,
  2470. os_task, param);
  2471. }
  2472. }
  2473. }
  2474. bool obs_wait_for_destroy_queue(void)
  2475. {
  2476. struct task_wait_info info = {0};
  2477. if (!obs->video.thread_initialized || !obs->audio.audio)
  2478. return false;
  2479. /* allow video and audio threads time to release objects */
  2480. os_event_init(&info.event, OS_EVENT_TYPE_AUTO);
  2481. obs_queue_task(OBS_TASK_GRAPHICS, task_wait_callback, &info, false);
  2482. os_event_wait(info.event);
  2483. obs_queue_task(OBS_TASK_AUDIO, task_wait_callback, &info, false);
  2484. os_event_wait(info.event);
  2485. os_event_destroy(info.event);
  2486. /* wait for destroy task queue */
  2487. return os_task_queue_wait(obs->destruction_task_thread);
  2488. }
  2489. static void set_ui_thread(void *unused)
  2490. {
  2491. is_ui_thread = true;
  2492. UNUSED_PARAMETER(unused);
  2493. }
  2494. void obs_set_ui_task_handler(obs_task_handler_t handler)
  2495. {
  2496. obs->ui_task_handler = handler;
  2497. obs_queue_task(OBS_TASK_UI, set_ui_thread, NULL, false);
  2498. }
  2499. obs_object_t *obs_object_get_ref(obs_object_t *object)
  2500. {
  2501. if (!object)
  2502. return NULL;
  2503. return obs_weak_object_get_object(object->control);
  2504. }
  2505. void obs_object_release(obs_object_t *object)
  2506. {
  2507. if (!obs) {
  2508. blog(LOG_WARNING, "Tried to release an object when the OBS "
  2509. "core is shut down!");
  2510. return;
  2511. }
  2512. if (!object)
  2513. return;
  2514. obs_weak_object_t *control = object->control;
  2515. if (obs_ref_release(&control->ref)) {
  2516. object->destroy(object);
  2517. obs_weak_object_release(control);
  2518. }
  2519. }
  2520. void obs_weak_object_addref(obs_weak_object_t *weak)
  2521. {
  2522. if (!weak)
  2523. return;
  2524. obs_weak_ref_addref(&weak->ref);
  2525. }
  2526. void obs_weak_object_release(obs_weak_object_t *weak)
  2527. {
  2528. if (!weak)
  2529. return;
  2530. if (obs_weak_ref_release(&weak->ref))
  2531. bfree(weak);
  2532. }
  2533. obs_weak_object_t *obs_object_get_weak_object(obs_object_t *object)
  2534. {
  2535. if (!object)
  2536. return NULL;
  2537. obs_weak_object_t *weak = object->control;
  2538. obs_weak_object_addref(weak);
  2539. return weak;
  2540. }
  2541. obs_object_t *obs_weak_object_get_object(obs_weak_object_t *weak)
  2542. {
  2543. if (!weak)
  2544. return NULL;
  2545. if (obs_weak_ref_get_ref(&weak->ref))
  2546. return weak->object;
  2547. return NULL;
  2548. }
  2549. bool obs_weak_object_expired(obs_weak_object_t *weak)
  2550. {
  2551. return weak ? obs_weak_ref_expired(&weak->ref) : true;
  2552. }
  2553. bool obs_weak_object_references_object(obs_weak_object_t *weak,
  2554. obs_object_t *object)
  2555. {
  2556. return weak && object && weak->object == object;
  2557. }