obs.c 43 KB

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