obs.c 64 KB

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