obs.c 77 KB

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