obs.c 59 KB

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