obs.c 60 KB

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