obs.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  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 = find_libobs_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 = find_libobs_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 = find_libobs_data_file("default_rect.effect");
  202. video->default_rect_effect = gs_effect_create_from_file(
  203. filename, NULL);
  204. bfree(filename);
  205. }
  206. filename = find_libobs_data_file("opaque.effect");
  207. video->opaque_effect = gs_effect_create_from_file(filename,
  208. NULL);
  209. bfree(filename);
  210. filename = find_libobs_data_file("solid.effect");
  211. video->solid_effect = gs_effect_create_from_file(filename,
  212. NULL);
  213. bfree(filename);
  214. filename = find_libobs_data_file("format_conversion.effect");
  215. video->conversion_effect = gs_effect_create_from_file(filename,
  216. NULL);
  217. bfree(filename);
  218. filename = find_libobs_data_file("bicubic_scale.effect");
  219. video->bicubic_effect = gs_effect_create_from_file(filename,
  220. NULL);
  221. bfree(filename);
  222. filename = find_libobs_data_file("lanczos_scale.effect");
  223. video->lanczos_effect = gs_effect_create_from_file(filename,
  224. NULL);
  225. bfree(filename);
  226. filename = find_libobs_data_file("bilinear_lowres_scale.effect");
  227. video->bilinear_lowres_effect = gs_effect_create_from_file(filename,
  228. NULL);
  229. bfree(filename);
  230. filename = find_libobs_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_video_thread, obs);
  307. if (errorcode != 0)
  308. return OBS_VIDEO_FAIL;
  309. video->thread_initialized = true;
  310. return OBS_VIDEO_SUCCESS;
  311. }
  312. static void stop_video(void)
  313. {
  314. struct obs_core_video *video = &obs->video;
  315. void *thread_retval;
  316. if (video->video) {
  317. video_output_stop(video->video);
  318. if (video->thread_initialized) {
  319. pthread_join(video->video_thread, &thread_retval);
  320. video->thread_initialized = false;
  321. }
  322. }
  323. }
  324. static void obs_free_video(void)
  325. {
  326. struct obs_core_video *video = &obs->video;
  327. if (video->video) {
  328. video_output_close(video->video);
  329. video->video = NULL;
  330. if (!video->graphics)
  331. return;
  332. gs_enter_context(video->graphics);
  333. if (video->mapped_surface) {
  334. gs_stagesurface_unmap(video->mapped_surface);
  335. video->mapped_surface = NULL;
  336. }
  337. for (size_t i = 0; i < NUM_TEXTURES; i++) {
  338. gs_stagesurface_destroy(video->copy_surfaces[i]);
  339. gs_texture_destroy(video->render_textures[i]);
  340. gs_texture_destroy(video->convert_textures[i]);
  341. gs_texture_destroy(video->output_textures[i]);
  342. video->copy_surfaces[i] = NULL;
  343. video->render_textures[i] = NULL;
  344. video->convert_textures[i] = NULL;
  345. video->output_textures[i] = NULL;
  346. }
  347. gs_leave_context();
  348. circlebuf_free(&video->vframe_info_buffer);
  349. memset(&video->textures_rendered, 0,
  350. sizeof(video->textures_rendered));
  351. memset(&video->textures_output, 0,
  352. sizeof(video->textures_output));
  353. memset(&video->textures_copied, 0,
  354. sizeof(video->textures_copied));
  355. memset(&video->textures_converted, 0,
  356. sizeof(video->textures_converted));
  357. video->cur_texture = 0;
  358. }
  359. }
  360. static void obs_free_graphics(void)
  361. {
  362. struct obs_core_video *video = &obs->video;
  363. if (video->graphics) {
  364. gs_enter_context(video->graphics);
  365. gs_texture_destroy(video->transparent_texture);
  366. gs_samplerstate_destroy(video->point_sampler);
  367. gs_effect_destroy(video->default_effect);
  368. gs_effect_destroy(video->default_rect_effect);
  369. gs_effect_destroy(video->opaque_effect);
  370. gs_effect_destroy(video->solid_effect);
  371. gs_effect_destroy(video->conversion_effect);
  372. gs_effect_destroy(video->bicubic_effect);
  373. gs_effect_destroy(video->lanczos_effect);
  374. gs_effect_destroy(video->bilinear_lowres_effect);
  375. video->default_effect = NULL;
  376. gs_leave_context();
  377. gs_destroy(video->graphics);
  378. video->graphics = NULL;
  379. }
  380. }
  381. static bool obs_init_audio(struct audio_output_info *ai)
  382. {
  383. struct obs_core_audio *audio = &obs->audio;
  384. int errorcode;
  385. /* TODO: sound subsystem */
  386. audio->user_volume = 1.0f;
  387. errorcode = audio_output_open(&audio->audio, ai);
  388. if (errorcode == AUDIO_OUTPUT_SUCCESS)
  389. return true;
  390. else if (errorcode == AUDIO_OUTPUT_INVALIDPARAM)
  391. blog(LOG_ERROR, "Invalid audio parameters specified");
  392. else
  393. blog(LOG_ERROR, "Could not open audio output");
  394. return false;
  395. }
  396. static void obs_free_audio(void)
  397. {
  398. struct obs_core_audio *audio = &obs->audio;
  399. if (audio->audio)
  400. audio_output_close(audio->audio);
  401. circlebuf_free(&audio->buffered_timestamps);
  402. da_free(audio->render_order);
  403. da_free(audio->root_nodes);
  404. memset(audio, 0, sizeof(struct obs_core_audio));
  405. }
  406. static bool obs_init_data(void)
  407. {
  408. struct obs_core_data *data = &obs->data;
  409. pthread_mutexattr_t attr;
  410. assert(data != NULL);
  411. pthread_mutex_init_value(&obs->data.displays_mutex);
  412. if (pthread_mutexattr_init(&attr) != 0)
  413. return false;
  414. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  415. goto fail;
  416. if (pthread_mutex_init(&data->sources_mutex, &attr) != 0)
  417. goto fail;
  418. if (pthread_mutex_init(&data->audio_sources_mutex, &attr) != 0)
  419. goto fail;
  420. if (pthread_mutex_init(&data->displays_mutex, &attr) != 0)
  421. goto fail;
  422. if (pthread_mutex_init(&data->outputs_mutex, &attr) != 0)
  423. goto fail;
  424. if (pthread_mutex_init(&data->encoders_mutex, &attr) != 0)
  425. goto fail;
  426. if (pthread_mutex_init(&data->services_mutex, &attr) != 0)
  427. goto fail;
  428. if (!obs_view_init(&data->main_view))
  429. goto fail;
  430. data->valid = true;
  431. fail:
  432. pthread_mutexattr_destroy(&attr);
  433. return data->valid;
  434. }
  435. void obs_main_view_free(struct obs_view *view)
  436. {
  437. if (!view) return;
  438. for (size_t i = 0; i < MAX_CHANNELS; i++)
  439. obs_source_release(view->channels[i]);
  440. memset(view->channels, 0, sizeof(view->channels));
  441. pthread_mutex_destroy(&view->channels_mutex);
  442. }
  443. #define FREE_OBS_LINKED_LIST(type) \
  444. do { \
  445. int unfreed = 0; \
  446. while (data->first_ ## type ) { \
  447. obs_ ## type ## _destroy(data->first_ ## type ); \
  448. unfreed++; \
  449. } \
  450. if (unfreed) \
  451. blog(LOG_INFO, "\t%d " #type "(s) were remaining", \
  452. unfreed); \
  453. } while (false)
  454. static void obs_free_data(void)
  455. {
  456. struct obs_core_data *data = &obs->data;
  457. data->valid = false;
  458. obs_main_view_free(&data->main_view);
  459. blog(LOG_INFO, "Freeing OBS context data");
  460. FREE_OBS_LINKED_LIST(source);
  461. FREE_OBS_LINKED_LIST(output);
  462. FREE_OBS_LINKED_LIST(encoder);
  463. FREE_OBS_LINKED_LIST(display);
  464. FREE_OBS_LINKED_LIST(service);
  465. pthread_mutex_destroy(&data->sources_mutex);
  466. pthread_mutex_destroy(&data->audio_sources_mutex);
  467. pthread_mutex_destroy(&data->displays_mutex);
  468. pthread_mutex_destroy(&data->outputs_mutex);
  469. pthread_mutex_destroy(&data->encoders_mutex);
  470. pthread_mutex_destroy(&data->services_mutex);
  471. }
  472. static const char *obs_signals[] = {
  473. "void source_create(ptr source)",
  474. "void source_destroy(ptr source)",
  475. "void source_remove(ptr source)",
  476. "void source_save(ptr source)",
  477. "void source_load(ptr source)",
  478. "void source_activate(ptr source)",
  479. "void source_deactivate(ptr source)",
  480. "void source_show(ptr source)",
  481. "void source_hide(ptr source)",
  482. "void source_rename(ptr source, string new_name, string prev_name)",
  483. "void source_volume(ptr source, in out float volume)",
  484. "void source_volume_level(ptr source, float level, float magnitude, "
  485. "float peak)",
  486. "void source_transition_start(ptr source)",
  487. "void source_transition_video_stop(ptr source)",
  488. "void source_transition_stop(ptr source)",
  489. "void channel_change(int channel, in out ptr source, ptr prev_source)",
  490. "void master_volume(in out float volume)",
  491. "void hotkey_layout_change()",
  492. "void hotkey_register(ptr hotkey)",
  493. "void hotkey_unregister(ptr hotkey)",
  494. "void hotkey_bindings_changed(ptr hotkey)",
  495. NULL
  496. };
  497. static inline bool obs_init_handlers(void)
  498. {
  499. obs->signals = signal_handler_create();
  500. if (!obs->signals)
  501. return false;
  502. obs->procs = proc_handler_create();
  503. if (!obs->procs)
  504. return false;
  505. return signal_handler_add_array(obs->signals, obs_signals);
  506. }
  507. static pthread_once_t obs_pthread_once_init_token = PTHREAD_ONCE_INIT;
  508. static inline bool obs_init_hotkeys(void)
  509. {
  510. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  511. pthread_mutexattr_t attr;
  512. bool success = false;
  513. assert(hotkeys != NULL);
  514. da_init(hotkeys->hotkeys);
  515. hotkeys->signals = obs->signals;
  516. hotkeys->name_map_init_token = obs_pthread_once_init_token;
  517. hotkeys->mute = bstrdup("Mute");
  518. hotkeys->unmute = bstrdup("Unmute");
  519. hotkeys->push_to_mute = bstrdup("Push-to-mute");
  520. hotkeys->push_to_talk = bstrdup("Push-to-talk");
  521. hotkeys->sceneitem_show = bstrdup("Show '%1'");
  522. hotkeys->sceneitem_hide = bstrdup("Hide '%1'");
  523. if (!obs_hotkeys_platform_init(hotkeys))
  524. return false;
  525. if (pthread_mutexattr_init(&attr) != 0)
  526. return false;
  527. if (pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_RECURSIVE) != 0)
  528. goto fail;
  529. if (pthread_mutex_init(&hotkeys->mutex, &attr) != 0)
  530. goto fail;
  531. if (os_event_init(&hotkeys->stop_event, OS_EVENT_TYPE_MANUAL) != 0)
  532. goto fail;
  533. if (pthread_create(&hotkeys->hotkey_thread, NULL,
  534. obs_hotkey_thread, NULL))
  535. goto fail;
  536. hotkeys->hotkey_thread_initialized = true;
  537. success = true;
  538. fail:
  539. pthread_mutexattr_destroy(&attr);
  540. return success;
  541. }
  542. static inline void stop_hotkeys(void)
  543. {
  544. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  545. void *thread_ret;
  546. if (hotkeys->hotkey_thread_initialized) {
  547. os_event_signal(hotkeys->stop_event);
  548. pthread_join(hotkeys->hotkey_thread, &thread_ret);
  549. hotkeys->hotkey_thread_initialized = false;
  550. }
  551. os_event_destroy(hotkeys->stop_event);
  552. obs_hotkeys_free();
  553. }
  554. static inline void obs_free_hotkeys(void)
  555. {
  556. struct obs_core_hotkeys *hotkeys = &obs->hotkeys;
  557. bfree(hotkeys->mute);
  558. bfree(hotkeys->unmute);
  559. bfree(hotkeys->push_to_mute);
  560. bfree(hotkeys->push_to_talk);
  561. bfree(hotkeys->sceneitem_show);
  562. bfree(hotkeys->sceneitem_hide);
  563. obs_hotkey_name_map_free();
  564. obs_hotkeys_platform_free(hotkeys);
  565. pthread_mutex_destroy(&hotkeys->mutex);
  566. }
  567. extern const struct obs_source_info scene_info;
  568. extern void log_system_info(void);
  569. static bool obs_init(const char *locale, const char *module_config_path,
  570. profiler_name_store_t *store)
  571. {
  572. obs = bzalloc(sizeof(struct obs_core));
  573. obs->name_store_owned = !store;
  574. obs->name_store = store ? store : profiler_name_store_create();
  575. if (!obs->name_store) {
  576. blog(LOG_ERROR, "Couldn't create profiler name store");
  577. return false;
  578. }
  579. log_system_info();
  580. if (!obs_init_data())
  581. return false;
  582. if (!obs_init_handlers())
  583. return false;
  584. if (!obs_init_hotkeys())
  585. return false;
  586. if (module_config_path)
  587. obs->module_config_path = bstrdup(module_config_path);
  588. obs->locale = bstrdup(locale);
  589. obs_register_source(&scene_info);
  590. add_default_module_paths();
  591. return true;
  592. }
  593. #ifdef _WIN32
  594. extern void initialize_crash_handler(void);
  595. extern void initialize_com(void);
  596. extern void uninitialize_com(void);
  597. #endif
  598. static const char *obs_startup_name = "obs_startup";
  599. bool obs_startup(const char *locale, const char *module_config_path,
  600. profiler_name_store_t *store)
  601. {
  602. bool success;
  603. profile_start(obs_startup_name);
  604. if (obs) {
  605. blog(LOG_WARNING, "Tried to call obs_startup more than once");
  606. return false;
  607. }
  608. #ifdef _WIN32
  609. initialize_crash_handler();
  610. initialize_com();
  611. #endif
  612. success = obs_init(locale, module_config_path, store);
  613. profile_end(obs_startup_name);
  614. if (!success)
  615. obs_shutdown();
  616. return success;
  617. }
  618. void obs_shutdown(void)
  619. {
  620. struct obs_module *module;
  621. if (!obs)
  622. return;
  623. #define FREE_REGISTERED_TYPES(structure, list) \
  624. do { \
  625. for (size_t i = 0; i < list.num; i++) { \
  626. struct structure *item = &list.array[i]; \
  627. if (item->type_data && item->free_type_data) \
  628. item->free_type_data(item->type_data); \
  629. } \
  630. da_free(list); \
  631. } while (false)
  632. FREE_REGISTERED_TYPES(obs_source_info, obs->source_types);
  633. FREE_REGISTERED_TYPES(obs_source_info, obs->input_types);
  634. FREE_REGISTERED_TYPES(obs_source_info, obs->filter_types);
  635. FREE_REGISTERED_TYPES(obs_source_info, obs->transition_types);
  636. FREE_REGISTERED_TYPES(obs_output_info, obs->output_types);
  637. FREE_REGISTERED_TYPES(obs_encoder_info, obs->encoder_types);
  638. FREE_REGISTERED_TYPES(obs_service_info, obs->service_types);
  639. FREE_REGISTERED_TYPES(obs_modal_ui, obs->modal_ui_callbacks);
  640. FREE_REGISTERED_TYPES(obs_modeless_ui, obs->modeless_ui_callbacks);
  641. #undef FREE_REGISTERED_TYPES
  642. stop_video();
  643. stop_hotkeys();
  644. obs_free_audio();
  645. obs_free_data();
  646. obs_free_video();
  647. obs_free_hotkeys();
  648. obs_free_graphics();
  649. proc_handler_destroy(obs->procs);
  650. signal_handler_destroy(obs->signals);
  651. obs->procs = NULL;
  652. obs->signals = NULL;
  653. module = obs->first_module;
  654. while (module) {
  655. struct obs_module *next = module->next;
  656. free_module(module);
  657. module = next;
  658. }
  659. obs->first_module = NULL;
  660. for (size_t i = 0; i < obs->module_paths.num; i++)
  661. free_module_path(obs->module_paths.array+i);
  662. da_free(obs->module_paths);
  663. if (obs->name_store_owned)
  664. profiler_name_store_free(obs->name_store);
  665. bfree(obs->module_config_path);
  666. bfree(obs->locale);
  667. bfree(obs);
  668. obs = NULL;
  669. #ifdef _WIN32
  670. uninitialize_com();
  671. #endif
  672. }
  673. bool obs_initialized(void)
  674. {
  675. return obs != NULL;
  676. }
  677. uint32_t obs_get_version(void)
  678. {
  679. return LIBOBS_API_VER;
  680. }
  681. void obs_set_locale(const char *locale)
  682. {
  683. struct obs_module *module;
  684. if (!obs)
  685. return;
  686. if (obs->locale)
  687. bfree(obs->locale);
  688. obs->locale = bstrdup(locale);
  689. module = obs->first_module;
  690. while (module) {
  691. if (module->set_locale)
  692. module->set_locale(locale);
  693. module = module->next;
  694. }
  695. }
  696. const char *obs_get_locale(void)
  697. {
  698. return obs ? obs->locale : NULL;
  699. }
  700. #define OBS_SIZE_MIN 2
  701. #define OBS_SIZE_MAX (32 * 1024)
  702. static inline bool size_valid(uint32_t width, uint32_t height)
  703. {
  704. return (width >= OBS_SIZE_MIN && height >= OBS_SIZE_MIN &&
  705. width <= OBS_SIZE_MAX && height <= OBS_SIZE_MAX);
  706. }
  707. int obs_reset_video(struct obs_video_info *ovi)
  708. {
  709. if (!obs) return OBS_VIDEO_FAIL;
  710. /* don't allow changing of video settings if active. */
  711. if (obs->video.video && video_output_active(obs->video.video))
  712. return OBS_VIDEO_CURRENTLY_ACTIVE;
  713. if (!size_valid(ovi->output_width, ovi->output_height) ||
  714. !size_valid(ovi->base_width, ovi->base_height))
  715. return OBS_VIDEO_INVALID_PARAM;
  716. struct obs_core_video *video = &obs->video;
  717. stop_video();
  718. obs_free_video();
  719. if (!ovi) {
  720. obs_free_graphics();
  721. return OBS_VIDEO_SUCCESS;
  722. }
  723. /* align to multiple-of-two and SSE alignment sizes */
  724. ovi->output_width &= 0xFFFFFFFC;
  725. ovi->output_height &= 0xFFFFFFFE;
  726. if (!video->graphics) {
  727. int errorcode = obs_init_graphics(ovi);
  728. if (errorcode != OBS_VIDEO_SUCCESS) {
  729. obs_free_graphics();
  730. return errorcode;
  731. }
  732. }
  733. const char *scale_type_name = "";
  734. switch (ovi->scale_type) {
  735. case OBS_SCALE_DISABLE:
  736. scale_type_name = "Disabled";
  737. break;
  738. case OBS_SCALE_POINT:
  739. scale_type_name = "Point";
  740. break;
  741. case OBS_SCALE_BICUBIC:
  742. scale_type_name = "Bicubic";
  743. break;
  744. case OBS_SCALE_BILINEAR:
  745. scale_type_name = "Bilinear";
  746. break;
  747. case OBS_SCALE_LANCZOS:
  748. scale_type_name = "Lanczos";
  749. break;
  750. }
  751. blog(LOG_INFO, "---------------------------------");
  752. blog(LOG_INFO, "video settings reset:\n"
  753. "\tbase resolution: %dx%d\n"
  754. "\toutput resolution: %dx%d\n"
  755. "\tdownscale filter: %s\n"
  756. "\tfps: %d/%d\n"
  757. "\tformat: %s",
  758. ovi->base_width, ovi->base_height,
  759. ovi->output_width, ovi->output_height,
  760. scale_type_name,
  761. ovi->fps_num, ovi->fps_den,
  762. get_video_format_name(ovi->output_format));
  763. return obs_init_video(ovi);
  764. }
  765. bool obs_reset_audio(const struct obs_audio_info *oai)
  766. {
  767. struct audio_output_info ai;
  768. if (!obs) return false;
  769. /* don't allow changing of audio settings if active. */
  770. if (obs->audio.audio && audio_output_active(obs->audio.audio))
  771. return false;
  772. obs_free_audio();
  773. if (!oai)
  774. return true;
  775. ai.name = "Audio";
  776. ai.samples_per_sec = oai->samples_per_sec;
  777. ai.format = AUDIO_FORMAT_FLOAT_PLANAR;
  778. ai.speakers = oai->speakers;
  779. ai.input_callback = audio_callback;
  780. blog(LOG_INFO, "---------------------------------");
  781. blog(LOG_INFO, "audio settings reset:\n"
  782. "\tsamples per sec: %d\n"
  783. "\tspeakers: %d",
  784. (int)ai.samples_per_sec,
  785. (int)ai.speakers);
  786. return obs_init_audio(&ai);
  787. }
  788. bool obs_get_video_info(struct obs_video_info *ovi)
  789. {
  790. struct obs_core_video *video = &obs->video;
  791. const struct video_output_info *info;
  792. if (!obs || !video->graphics)
  793. return false;
  794. info = video_output_get_info(video->video);
  795. if (!info)
  796. return false;
  797. memset(ovi, 0, sizeof(struct obs_video_info));
  798. ovi->base_width = video->base_width;
  799. ovi->base_height = video->base_height;
  800. ovi->gpu_conversion= video->gpu_conversion;
  801. ovi->scale_type = video->scale_type;
  802. ovi->colorspace = info->colorspace;
  803. ovi->range = info->range;
  804. ovi->output_width = info->width;
  805. ovi->output_height = info->height;
  806. ovi->output_format = info->format;
  807. ovi->fps_num = info->fps_num;
  808. ovi->fps_den = info->fps_den;
  809. return true;
  810. }
  811. bool obs_get_audio_info(struct obs_audio_info *oai)
  812. {
  813. struct obs_core_audio *audio = &obs->audio;
  814. const struct audio_output_info *info;
  815. if (!obs || !oai || !audio->audio)
  816. return false;
  817. info = audio_output_get_info(audio->audio);
  818. oai->samples_per_sec = info->samples_per_sec;
  819. oai->speakers = info->speakers;
  820. return true;
  821. }
  822. bool obs_enum_source_types(size_t idx, const char **id)
  823. {
  824. if (!obs) return false;
  825. if (idx >= obs->source_types.num)
  826. return false;
  827. *id = obs->source_types.array[idx].id;
  828. return true;
  829. }
  830. bool obs_enum_input_types(size_t idx, const char **id)
  831. {
  832. if (!obs) return false;
  833. if (idx >= obs->input_types.num)
  834. return false;
  835. *id = obs->input_types.array[idx].id;
  836. return true;
  837. }
  838. bool obs_enum_filter_types(size_t idx, const char **id)
  839. {
  840. if (!obs) return false;
  841. if (idx >= obs->filter_types.num)
  842. return false;
  843. *id = obs->filter_types.array[idx].id;
  844. return true;
  845. }
  846. bool obs_enum_transition_types(size_t idx, const char **id)
  847. {
  848. if (!obs) return false;
  849. if (idx >= obs->transition_types.num)
  850. return false;
  851. *id = obs->transition_types.array[idx].id;
  852. return true;
  853. }
  854. bool obs_enum_output_types(size_t idx, const char **id)
  855. {
  856. if (!obs) return false;
  857. if (idx >= obs->output_types.num)
  858. return false;
  859. *id = obs->output_types.array[idx].id;
  860. return true;
  861. }
  862. bool obs_enum_encoder_types(size_t idx, const char **id)
  863. {
  864. if (!obs) return false;
  865. if (idx >= obs->encoder_types.num)
  866. return false;
  867. *id = obs->encoder_types.array[idx].id;
  868. return true;
  869. }
  870. bool obs_enum_service_types(size_t idx, const char **id)
  871. {
  872. if (!obs) return false;
  873. if (idx >= obs->service_types.num)
  874. return false;
  875. *id = obs->service_types.array[idx].id;
  876. return true;
  877. }
  878. void obs_enter_graphics(void)
  879. {
  880. if (obs && obs->video.graphics)
  881. gs_enter_context(obs->video.graphics);
  882. }
  883. void obs_leave_graphics(void)
  884. {
  885. if (obs && obs->video.graphics)
  886. gs_leave_context();
  887. }
  888. audio_t *obs_get_audio(void)
  889. {
  890. return (obs != NULL) ? obs->audio.audio : NULL;
  891. }
  892. video_t *obs_get_video(void)
  893. {
  894. return (obs != NULL) ? obs->video.video : NULL;
  895. }
  896. /* TODO: optimize this later so it's not just O(N) string lookups */
  897. static inline struct obs_modal_ui *get_modal_ui_callback(const char *id,
  898. const char *task, const char *target)
  899. {
  900. for (size_t i = 0; i < obs->modal_ui_callbacks.num; i++) {
  901. struct obs_modal_ui *callback = obs->modal_ui_callbacks.array+i;
  902. if (strcmp(callback->id, id) == 0 &&
  903. strcmp(callback->task, task) == 0 &&
  904. strcmp(callback->target, target) == 0)
  905. return callback;
  906. }
  907. return NULL;
  908. }
  909. static inline struct obs_modeless_ui *get_modeless_ui_callback(const char *id,
  910. const char *task, const char *target)
  911. {
  912. for (size_t i = 0; i < obs->modeless_ui_callbacks.num; i++) {
  913. struct obs_modeless_ui *callback;
  914. callback = obs->modeless_ui_callbacks.array+i;
  915. if (strcmp(callback->id, id) == 0 &&
  916. strcmp(callback->task, task) == 0 &&
  917. strcmp(callback->target, target) == 0)
  918. return callback;
  919. }
  920. return NULL;
  921. }
  922. int obs_exec_ui(const char *name, const char *task, const char *target,
  923. void *data, void *ui_data)
  924. {
  925. struct obs_modal_ui *callback;
  926. int errorcode = OBS_UI_NOTFOUND;
  927. if (!obs) return errorcode;
  928. callback = get_modal_ui_callback(name, task, target);
  929. if (callback) {
  930. bool success = callback->exec(data, ui_data);
  931. errorcode = success ? OBS_UI_SUCCESS : OBS_UI_CANCEL;
  932. }
  933. return errorcode;
  934. }
  935. void *obs_create_ui(const char *name, const char *task, const char *target,
  936. void *data, void *ui_data)
  937. {
  938. struct obs_modeless_ui *callback;
  939. if (!obs) return NULL;
  940. callback = get_modeless_ui_callback(name, task, target);
  941. return callback ? callback->create(data, ui_data) : NULL;
  942. }
  943. obs_source_t *obs_get_output_source(uint32_t channel)
  944. {
  945. if (!obs) return NULL;
  946. return obs_view_get_source(&obs->data.main_view, channel);
  947. }
  948. void obs_set_output_source(uint32_t channel, obs_source_t *source)
  949. {
  950. assert(channel < MAX_CHANNELS);
  951. if (!obs) return;
  952. if (channel >= MAX_CHANNELS) return;
  953. struct obs_source *prev_source;
  954. struct obs_view *view = &obs->data.main_view;
  955. struct calldata params = {0};
  956. pthread_mutex_lock(&view->channels_mutex);
  957. obs_source_addref(source);
  958. prev_source = view->channels[channel];
  959. calldata_set_int(&params, "channel", channel);
  960. calldata_set_ptr(&params, "prev_source", prev_source);
  961. calldata_set_ptr(&params, "source", source);
  962. signal_handler_signal(obs->signals, "channel_change", &params);
  963. calldata_get_ptr(&params, "source", &source);
  964. calldata_free(&params);
  965. view->channels[channel] = source;
  966. pthread_mutex_unlock(&view->channels_mutex);
  967. if (source)
  968. obs_source_activate(source, MAIN_VIEW);
  969. if (prev_source) {
  970. obs_source_deactivate(prev_source, MAIN_VIEW);
  971. obs_source_release(prev_source);
  972. }
  973. }
  974. void obs_enum_sources(bool (*enum_proc)(void*, obs_source_t*), void *param)
  975. {
  976. obs_source_t *source;
  977. if (!obs) return;
  978. pthread_mutex_lock(&obs->data.sources_mutex);
  979. source = obs->data.first_source;
  980. while (source) {
  981. obs_source_t *next_source =
  982. (obs_source_t*)source->context.next;
  983. if ((source->info.type == OBS_SOURCE_TYPE_INPUT) != 0 &&
  984. !source->context.private &&
  985. !enum_proc(param, source))
  986. break;
  987. source = next_source;
  988. }
  989. pthread_mutex_unlock(&obs->data.sources_mutex);
  990. }
  991. static inline void obs_enum(void *pstart, pthread_mutex_t *mutex, void *proc,
  992. void *param)
  993. {
  994. struct obs_context_data **start = pstart, *context;
  995. bool (*enum_proc)(void*, void*) = proc;
  996. assert(start);
  997. assert(mutex);
  998. assert(enum_proc);
  999. pthread_mutex_lock(mutex);
  1000. context = *start;
  1001. while (context) {
  1002. if (!enum_proc(param, context))
  1003. break;
  1004. context = context->next;
  1005. }
  1006. pthread_mutex_unlock(mutex);
  1007. }
  1008. void obs_enum_outputs(bool (*enum_proc)(void*, obs_output_t*), void *param)
  1009. {
  1010. if (!obs) return;
  1011. obs_enum(&obs->data.first_output, &obs->data.outputs_mutex,
  1012. enum_proc, param);
  1013. }
  1014. void obs_enum_encoders(bool (*enum_proc)(void*, obs_encoder_t*), void *param)
  1015. {
  1016. if (!obs) return;
  1017. obs_enum(&obs->data.first_encoder, &obs->data.encoders_mutex,
  1018. enum_proc, param);
  1019. }
  1020. void obs_enum_services(bool (*enum_proc)(void*, obs_service_t*), void *param)
  1021. {
  1022. if (!obs) return;
  1023. obs_enum(&obs->data.first_service, &obs->data.services_mutex,
  1024. enum_proc, param);
  1025. }
  1026. static inline void *get_context_by_name(void *vfirst, const char *name,
  1027. pthread_mutex_t *mutex, void *(*addref)(void*))
  1028. {
  1029. struct obs_context_data **first = vfirst;
  1030. struct obs_context_data *context;
  1031. pthread_mutex_lock(mutex);
  1032. context = *first;
  1033. while (context) {
  1034. if (!context->private && strcmp(context->name, name) == 0) {
  1035. context = addref(context);
  1036. break;
  1037. }
  1038. context = context->next;
  1039. }
  1040. pthread_mutex_unlock(mutex);
  1041. return context;
  1042. }
  1043. static inline void *obs_source_addref_safe_(void *ref)
  1044. {
  1045. return obs_source_get_ref(ref);
  1046. }
  1047. static inline void *obs_output_addref_safe_(void *ref)
  1048. {
  1049. return obs_output_get_ref(ref);
  1050. }
  1051. static inline void *obs_encoder_addref_safe_(void *ref)
  1052. {
  1053. return obs_encoder_get_ref(ref);
  1054. }
  1055. static inline void *obs_service_addref_safe_(void *ref)
  1056. {
  1057. return obs_service_get_ref(ref);
  1058. }
  1059. static inline void *obs_id_(void *data)
  1060. {
  1061. return data;
  1062. }
  1063. obs_source_t *obs_get_source_by_name(const char *name)
  1064. {
  1065. if (!obs) return NULL;
  1066. return get_context_by_name(&obs->data.first_source, name,
  1067. &obs->data.sources_mutex, obs_source_addref_safe_);
  1068. }
  1069. obs_output_t *obs_get_output_by_name(const char *name)
  1070. {
  1071. if (!obs) return NULL;
  1072. return get_context_by_name(&obs->data.first_output, name,
  1073. &obs->data.outputs_mutex, obs_output_addref_safe_);
  1074. }
  1075. obs_encoder_t *obs_get_encoder_by_name(const char *name)
  1076. {
  1077. if (!obs) return NULL;
  1078. return get_context_by_name(&obs->data.first_encoder, name,
  1079. &obs->data.encoders_mutex, obs_encoder_addref_safe_);
  1080. }
  1081. obs_service_t *obs_get_service_by_name(const char *name)
  1082. {
  1083. if (!obs) return NULL;
  1084. return get_context_by_name(&obs->data.first_service, name,
  1085. &obs->data.services_mutex, obs_service_addref_safe_);
  1086. }
  1087. gs_effect_t *obs_get_base_effect(enum obs_base_effect effect)
  1088. {
  1089. if (!obs) return NULL;
  1090. switch (effect) {
  1091. case OBS_EFFECT_DEFAULT:
  1092. return obs->video.default_effect;
  1093. case OBS_EFFECT_DEFAULT_RECT:
  1094. return obs->video.default_rect_effect;
  1095. case OBS_EFFECT_OPAQUE:
  1096. return obs->video.opaque_effect;
  1097. case OBS_EFFECT_SOLID:
  1098. return obs->video.solid_effect;
  1099. case OBS_EFFECT_BICUBIC:
  1100. return obs->video.bicubic_effect;
  1101. case OBS_EFFECT_LANCZOS:
  1102. return obs->video.lanczos_effect;
  1103. case OBS_EFFECT_BILINEAR_LOWRES:
  1104. return obs->video.bilinear_lowres_effect;
  1105. case OBS_EFFECT_PREMULTIPLIED_ALPHA:
  1106. return obs->video.premultiplied_alpha_effect;
  1107. }
  1108. return NULL;
  1109. }
  1110. /* DEPRECATED */
  1111. gs_effect_t *obs_get_default_rect_effect(void)
  1112. {
  1113. if (!obs) return NULL;
  1114. return obs->video.default_rect_effect;
  1115. }
  1116. signal_handler_t *obs_get_signal_handler(void)
  1117. {
  1118. if (!obs) return NULL;
  1119. return obs->signals;
  1120. }
  1121. proc_handler_t *obs_get_proc_handler(void)
  1122. {
  1123. if (!obs) return NULL;
  1124. return obs->procs;
  1125. }
  1126. void obs_render_main_view(void)
  1127. {
  1128. if (!obs) return;
  1129. obs_view_render(&obs->data.main_view);
  1130. }
  1131. void obs_set_master_volume(float volume)
  1132. {
  1133. struct calldata data = {0};
  1134. if (!obs) return;
  1135. calldata_set_float(&data, "volume", volume);
  1136. signal_handler_signal(obs->signals, "master_volume", &data);
  1137. volume = (float)calldata_float(&data, "volume");
  1138. calldata_free(&data);
  1139. obs->audio.user_volume = volume;
  1140. }
  1141. float obs_get_master_volume(void)
  1142. {
  1143. return obs ? obs->audio.user_volume : 0.0f;
  1144. }
  1145. static obs_source_t *obs_load_source_type(obs_data_t *source_data)
  1146. {
  1147. obs_data_array_t *filters = obs_data_get_array(source_data, "filters");
  1148. obs_source_t *source;
  1149. const char *name = obs_data_get_string(source_data, "name");
  1150. const char *id = obs_data_get_string(source_data, "id");
  1151. obs_data_t *settings = obs_data_get_obj(source_data, "settings");
  1152. obs_data_t *hotkeys = obs_data_get_obj(source_data, "hotkeys");
  1153. double volume;
  1154. int64_t sync;
  1155. uint32_t flags;
  1156. uint32_t mixers;
  1157. int di_order;
  1158. int di_mode;
  1159. source = obs_source_create(id, name, settings, hotkeys);
  1160. obs_data_release(hotkeys);
  1161. obs_data_set_default_double(source_data, "volume", 1.0);
  1162. volume = obs_data_get_double(source_data, "volume");
  1163. obs_source_set_volume(source, (float)volume);
  1164. sync = obs_data_get_int(source_data, "sync");
  1165. obs_source_set_sync_offset(source, sync);
  1166. obs_data_set_default_int(source_data, "mixers", 0xF);
  1167. mixers = (uint32_t)obs_data_get_int(source_data, "mixers");
  1168. obs_source_set_audio_mixers(source, mixers);
  1169. obs_data_set_default_int(source_data, "flags", source->default_flags);
  1170. flags = (uint32_t)obs_data_get_int(source_data, "flags");
  1171. obs_source_set_flags(source, flags);
  1172. obs_data_set_default_bool(source_data, "enabled", true);
  1173. obs_source_set_enabled(source,
  1174. obs_data_get_bool(source_data, "enabled"));
  1175. obs_data_set_default_bool(source_data, "muted", false);
  1176. obs_source_set_muted(source, obs_data_get_bool(source_data, "muted"));
  1177. obs_data_set_default_bool(source_data, "push-to-mute", false);
  1178. obs_source_enable_push_to_mute(source,
  1179. obs_data_get_bool(source_data, "push-to-mute"));
  1180. obs_data_set_default_int(source_data, "push-to-mute-delay", 0);
  1181. obs_source_set_push_to_mute_delay(source,
  1182. obs_data_get_int(source_data, "push-to-mute-delay"));
  1183. obs_data_set_default_bool(source_data, "push-to-talk", false);
  1184. obs_source_enable_push_to_talk(source,
  1185. obs_data_get_bool(source_data, "push-to-talk"));
  1186. obs_data_set_default_int(source_data, "push-to-talk-delay", 0);
  1187. obs_source_set_push_to_talk_delay(source,
  1188. obs_data_get_int(source_data, "push-to-talk-delay"));
  1189. di_mode = (int)obs_data_get_int(source_data, "deinterlace_mode");
  1190. obs_source_set_deinterlace_mode(source,
  1191. (enum obs_deinterlace_mode)di_mode);
  1192. di_order = (int)obs_data_get_int(source_data, "deinterlace_field_order");
  1193. obs_source_set_deinterlace_field_order(source,
  1194. (enum obs_deinterlace_field_order)di_order);
  1195. if (filters) {
  1196. size_t count = obs_data_array_count(filters);
  1197. for (size_t i = 0; i < count; i++) {
  1198. obs_data_t *filter_data =
  1199. obs_data_array_item(filters, i);
  1200. obs_source_t *filter = obs_load_source_type(
  1201. filter_data);
  1202. if (filter) {
  1203. obs_source_filter_add(source, filter);
  1204. obs_source_release(filter);
  1205. }
  1206. obs_data_release(filter_data);
  1207. }
  1208. obs_data_array_release(filters);
  1209. }
  1210. obs_data_release(settings);
  1211. return source;
  1212. }
  1213. obs_source_t *obs_load_source(obs_data_t *source_data)
  1214. {
  1215. return obs_load_source_type(source_data);
  1216. }
  1217. void obs_load_sources(obs_data_array_t *array, obs_load_source_cb cb,
  1218. void *private_data)
  1219. {
  1220. if (!obs) return;
  1221. struct obs_core_data *data = &obs->data;
  1222. DARRAY(obs_source_t*) sources;
  1223. size_t count;
  1224. size_t i;
  1225. da_init(sources);
  1226. count = obs_data_array_count(array);
  1227. da_reserve(sources, count);
  1228. pthread_mutex_lock(&data->sources_mutex);
  1229. for (i = 0; i < count; i++) {
  1230. obs_data_t *source_data = obs_data_array_item(array, i);
  1231. obs_source_t *source = obs_load_source(source_data);
  1232. da_push_back(sources, &source);
  1233. obs_data_release(source_data);
  1234. }
  1235. /* tell sources that we want to load */
  1236. for (i = 0; i < sources.num; i++) {
  1237. obs_source_t *source = sources.array[i];
  1238. obs_data_t *source_data = obs_data_array_item(array, i);
  1239. if (source) {
  1240. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1241. obs_transition_load(source, source_data);
  1242. obs_source_load(source);
  1243. cb(private_data, source);
  1244. }
  1245. obs_data_release(source_data);
  1246. }
  1247. for (i = 0; i < sources.num; i++)
  1248. obs_source_release(sources.array[i]);
  1249. pthread_mutex_unlock(&data->sources_mutex);
  1250. da_free(sources);
  1251. }
  1252. obs_data_t *obs_save_source(obs_source_t *source)
  1253. {
  1254. obs_data_array_t *filters = obs_data_array_create();
  1255. obs_data_t *source_data = obs_data_create();
  1256. obs_data_t *settings = obs_source_get_settings(source);
  1257. obs_data_t *hotkey_data = source->context.hotkey_data;
  1258. obs_data_t *hotkeys;
  1259. float volume = obs_source_get_volume(source);
  1260. uint32_t mixers = obs_source_get_audio_mixers(source);
  1261. int64_t sync = obs_source_get_sync_offset(source);
  1262. uint32_t flags = obs_source_get_flags(source);
  1263. const char *name = obs_source_get_name(source);
  1264. const char *id = obs_source_get_id(source);
  1265. bool enabled = obs_source_enabled(source);
  1266. bool muted = obs_source_muted(source);
  1267. bool push_to_mute= obs_source_push_to_mute_enabled(source);
  1268. uint64_t ptm_delay = obs_source_get_push_to_mute_delay(source);
  1269. bool push_to_talk= obs_source_push_to_talk_enabled(source);
  1270. uint64_t ptt_delay = obs_source_get_push_to_talk_delay(source);
  1271. int di_mode = (int)obs_source_get_deinterlace_mode(source);
  1272. int di_order =
  1273. (int)obs_source_get_deinterlace_field_order(source);
  1274. obs_source_save(source);
  1275. hotkeys = obs_hotkeys_save_source(source);
  1276. if (hotkeys) {
  1277. obs_data_release(hotkey_data);
  1278. source->context.hotkey_data = hotkeys;
  1279. hotkey_data = hotkeys;
  1280. }
  1281. obs_data_set_string(source_data, "name", name);
  1282. obs_data_set_string(source_data, "id", id);
  1283. obs_data_set_obj (source_data, "settings", settings);
  1284. obs_data_set_int (source_data, "mixers", mixers);
  1285. obs_data_set_int (source_data, "sync", sync);
  1286. obs_data_set_int (source_data, "flags", flags);
  1287. obs_data_set_double(source_data, "volume", volume);
  1288. obs_data_set_bool (source_data, "enabled", enabled);
  1289. obs_data_set_bool (source_data, "muted", muted);
  1290. obs_data_set_bool (source_data, "push-to-mute", push_to_mute);
  1291. obs_data_set_int (source_data, "push-to-mute-delay", ptm_delay);
  1292. obs_data_set_bool (source_data, "push-to-talk", push_to_talk);
  1293. obs_data_set_int (source_data, "push-to-talk-delay", ptt_delay);
  1294. obs_data_set_obj (source_data, "hotkeys", hotkey_data);
  1295. obs_data_set_int (source_data, "deinterlace_mode", di_mode);
  1296. obs_data_set_int (source_data, "deinterlace_field_order", di_order);
  1297. if (source->info.type == OBS_SOURCE_TYPE_TRANSITION)
  1298. obs_transition_save(source, source_data);
  1299. pthread_mutex_lock(&source->filter_mutex);
  1300. if (source->filters.num) {
  1301. for (size_t i = source->filters.num; i > 0; i--) {
  1302. obs_source_t *filter = source->filters.array[i - 1];
  1303. obs_data_t *filter_data = obs_save_source(filter);
  1304. obs_data_array_push_back(filters, filter_data);
  1305. obs_data_release(filter_data);
  1306. }
  1307. obs_data_set_array(source_data, "filters", filters);
  1308. }
  1309. pthread_mutex_unlock(&source->filter_mutex);
  1310. obs_data_release(settings);
  1311. obs_data_array_release(filters);
  1312. return source_data;
  1313. }
  1314. obs_data_array_t *obs_save_sources_filtered(obs_save_source_filter_cb cb,
  1315. void *data_)
  1316. {
  1317. if (!obs) return NULL;
  1318. struct obs_core_data *data = &obs->data;
  1319. obs_data_array_t *array;
  1320. obs_source_t *source;
  1321. array = obs_data_array_create();
  1322. pthread_mutex_lock(&data->sources_mutex);
  1323. source = data->first_source;
  1324. while (source) {
  1325. if ((source->info.type != OBS_SOURCE_TYPE_FILTER) != 0 &&
  1326. !source->context.private && cb(data_, source)) {
  1327. obs_data_t *source_data = obs_save_source(source);
  1328. obs_data_array_push_back(array, source_data);
  1329. obs_data_release(source_data);
  1330. }
  1331. source = (obs_source_t*)source->context.next;
  1332. }
  1333. pthread_mutex_unlock(&data->sources_mutex);
  1334. return array;
  1335. }
  1336. static bool save_source_filter(void *data, obs_source_t *source)
  1337. {
  1338. UNUSED_PARAMETER(data);
  1339. UNUSED_PARAMETER(source);
  1340. return true;
  1341. }
  1342. obs_data_array_t *obs_save_sources(void)
  1343. {
  1344. return obs_save_sources_filtered(save_source_filter, NULL);
  1345. }
  1346. /* ensures that names are never blank */
  1347. static inline char *dup_name(const char *name, bool private)
  1348. {
  1349. if (private && !name)
  1350. return NULL;
  1351. if (!name || !*name) {
  1352. struct dstr unnamed = {0};
  1353. dstr_printf(&unnamed, "__unnamed%04lld",
  1354. obs->data.unnamed_index++);
  1355. return unnamed.array;
  1356. } else {
  1357. return bstrdup(name);
  1358. }
  1359. }
  1360. static inline bool obs_context_data_init_wrap(
  1361. struct obs_context_data *context,
  1362. enum obs_obj_type type,
  1363. obs_data_t *settings,
  1364. const char *name,
  1365. obs_data_t *hotkey_data,
  1366. bool private)
  1367. {
  1368. assert(context);
  1369. memset(context, 0, sizeof(*context));
  1370. context->private = private;
  1371. context->type = type;
  1372. pthread_mutex_init_value(&context->rename_cache_mutex);
  1373. if (pthread_mutex_init(&context->rename_cache_mutex, NULL) < 0)
  1374. return false;
  1375. context->signals = signal_handler_create();
  1376. if (!context->signals)
  1377. return false;
  1378. context->procs = proc_handler_create();
  1379. if (!context->procs)
  1380. return false;
  1381. context->name = dup_name(name, private);
  1382. context->settings = obs_data_newref(settings);
  1383. context->hotkey_data = obs_data_newref(hotkey_data);
  1384. return true;
  1385. }
  1386. bool obs_context_data_init(
  1387. struct obs_context_data *context,
  1388. enum obs_obj_type type,
  1389. obs_data_t *settings,
  1390. const char *name,
  1391. obs_data_t *hotkey_data,
  1392. bool private)
  1393. {
  1394. if (obs_context_data_init_wrap(context, type, settings, name,
  1395. hotkey_data, private)) {
  1396. return true;
  1397. } else {
  1398. obs_context_data_free(context);
  1399. return false;
  1400. }
  1401. }
  1402. void obs_context_data_free(struct obs_context_data *context)
  1403. {
  1404. obs_hotkeys_context_release(context);
  1405. signal_handler_destroy(context->signals);
  1406. proc_handler_destroy(context->procs);
  1407. obs_data_release(context->settings);
  1408. obs_context_data_remove(context);
  1409. pthread_mutex_destroy(&context->rename_cache_mutex);
  1410. bfree(context->name);
  1411. for (size_t i = 0; i < context->rename_cache.num; i++)
  1412. bfree(context->rename_cache.array[i]);
  1413. da_free(context->rename_cache);
  1414. memset(context, 0, sizeof(*context));
  1415. }
  1416. void obs_context_data_insert(struct obs_context_data *context,
  1417. pthread_mutex_t *mutex, void *pfirst)
  1418. {
  1419. struct obs_context_data **first = pfirst;
  1420. assert(context);
  1421. assert(mutex);
  1422. assert(first);
  1423. context->mutex = mutex;
  1424. pthread_mutex_lock(mutex);
  1425. context->prev_next = first;
  1426. context->next = *first;
  1427. *first = context;
  1428. if (context->next)
  1429. context->next->prev_next = &context->next;
  1430. pthread_mutex_unlock(mutex);
  1431. }
  1432. void obs_context_data_remove(struct obs_context_data *context)
  1433. {
  1434. if (context && context->mutex) {
  1435. pthread_mutex_lock(context->mutex);
  1436. if (context->prev_next)
  1437. *context->prev_next = context->next;
  1438. if (context->next)
  1439. context->next->prev_next = context->prev_next;
  1440. pthread_mutex_unlock(context->mutex);
  1441. context->mutex = NULL;
  1442. }
  1443. }
  1444. void obs_context_data_setname(struct obs_context_data *context,
  1445. const char *name)
  1446. {
  1447. pthread_mutex_lock(&context->rename_cache_mutex);
  1448. if (context->name)
  1449. da_push_back(context->rename_cache, &context->name);
  1450. context->name = dup_name(name, context->private);
  1451. pthread_mutex_unlock(&context->rename_cache_mutex);
  1452. }
  1453. profiler_name_store_t *obs_get_profiler_name_store(void)
  1454. {
  1455. if (!obs)
  1456. return NULL;
  1457. return obs->name_store;
  1458. }
  1459. uint64_t obs_get_video_frame_time(void)
  1460. {
  1461. return obs ? obs->video.video_time : 0;
  1462. }
  1463. double obs_get_active_fps(void)
  1464. {
  1465. return obs ? obs->video.video_fps : 0.0;
  1466. }
  1467. enum obs_obj_type obs_obj_get_type(void *obj)
  1468. {
  1469. struct obs_context_data *context = obj;
  1470. return context ? context->type : OBS_OBJ_TYPE_INVALID;
  1471. }
  1472. const char *obs_obj_get_id(void *obj)
  1473. {
  1474. struct obs_context_data *context = obj;
  1475. if (!context)
  1476. return NULL;
  1477. switch (context->type) {
  1478. case OBS_OBJ_TYPE_SOURCE: return ((obs_source_t*)obj)->info.id;
  1479. case OBS_OBJ_TYPE_OUTPUT: return ((obs_output_t*)obj)->info.id;
  1480. case OBS_OBJ_TYPE_ENCODER: return ((obs_encoder_t*)obj)->info.id;
  1481. case OBS_OBJ_TYPE_SERVICE: return ((obs_service_t*)obj)->info.id;
  1482. default:;
  1483. }
  1484. return NULL;
  1485. }
  1486. bool obs_obj_invalid(void *obj)
  1487. {
  1488. struct obs_context_data *context = obj;
  1489. if (!context)
  1490. return true;
  1491. return !context->data;
  1492. }