obs.c 72 KB

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