obs.c 90 KB

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