obs.c 91 KB

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