obs.c 94 KB

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