obs.c 62 KB

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