1
0

obs.c 68 KB

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