obs.c 60 KB

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