obs.c 55 KB

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