obs.c 63 KB

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