obs.c 44 KB

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